* elflink.h (elf_gc_mark): Pass in the section whose relocs we are
[deliverable/binutils-gdb.git] / bfd / elf64-ppc.c
1 /* PowerPC64-specific support for 64-bit ELF.
2 Copyright 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
3 Written by Linus Nordberg, Swox AB <info@swox.com>,
4 based on elf32-ppc.c by Ian Lance Taylor.
5
6 This file is part of BFD, the Binary File Descriptor library.
7
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.
12
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.
17
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. */
21
22 /* This file is based on the 64-bit PowerPC ELF ABI. It is also based
23 on the file elf32-ppc.c. */
24
25 #include "bfd.h"
26 #include "sysdep.h"
27 #include "bfdlink.h"
28 #include "libbfd.h"
29 #include "elf-bfd.h"
30 #include "elf/ppc.h"
31 #include "elf64-ppc.h"
32
33 #define USE_RELA /* we want RELA relocations, not REL. */
34
35
36 static void ppc_howto_init
37 PARAMS ((void));
38 static reloc_howto_type *ppc64_elf_reloc_type_lookup
39 PARAMS ((bfd *abfd, bfd_reloc_code_real_type code));
40 static void ppc64_elf_info_to_howto
41 PARAMS ((bfd *abfd, arelent *cache_ptr, Elf64_Internal_Rela *dst));
42 static bfd_reloc_status_type ppc64_elf_ha_reloc
43 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
44 static bfd_reloc_status_type ppc64_elf_brtaken_reloc
45 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
46 static bfd_reloc_status_type ppc64_elf_sectoff_reloc
47 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
48 static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
49 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
50 static bfd_reloc_status_type ppc64_elf_toc_reloc
51 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
52 static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
53 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
54 static bfd_reloc_status_type ppc64_elf_toc64_reloc
55 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
56 static bfd_reloc_status_type ppc64_elf_unhandled_reloc
57 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
58 static void ppc64_elf_get_symbol_info
59 PARAMS ((bfd *, asymbol *, symbol_info *));
60 static boolean ppc64_elf_object_p
61 PARAMS ((bfd *));
62 static boolean ppc64_elf_set_private_flags
63 PARAMS ((bfd *, flagword));
64 static boolean ppc64_elf_merge_private_bfd_data
65 PARAMS ((bfd *, bfd *));
66 static boolean ppc64_elf_section_from_shdr
67 PARAMS ((bfd *, Elf64_Internal_Shdr *, const char *));
68
69
70 /* The name of the dynamic interpreter. This is put in the .interp
71 section. */
72 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
73
74 /* The size in bytes of an entry in the procedure linkage table. */
75 #define PLT_ENTRY_SIZE 24
76
77 /* The initial size of the plt reserved for the dynamic linker. */
78 #define PLT_INITIAL_ENTRY_SIZE PLT_ENTRY_SIZE
79
80 /* TOC base pointers offset from start of TOC. */
81 #define TOC_BASE_OFF (0x8000)
82
83 /* .plt call stub instructions. */
84 #define ADDIS_R12_R2 0x3d820000 /* addis %r12,%r2,xxx@ha */
85 #define STD_R2_40R1 0xf8410028 /* std %r2,40(%r1) */
86 #define LD_R11_0R12 0xe96c0000 /* ld %r11,xxx+0@l(%r12) */
87 #define LD_R2_0R12 0xe84c0000 /* ld %r2,xxx+8@l(%r12) */
88 #define MTCTR_R11 0x7d6903a6 /* mtctr %r11 */
89 /* ld %r11,xxx+16@l(%r12) */
90 #define BCTR 0x4e800420 /* bctr */
91
92 /* The normal stub is this size. */
93 #define PLT_CALL_STUB_SIZE (7*4)
94
95 /* But sometimes the .plt entry crosses a 64k boundary, and we need
96 to adjust the high word with this insn. */
97 #define ADDIS_R12_R12_1 0x3d8c0001 /* addis %r12,%r12,1 */
98
99 /* The .glink fixup call stub is the same as the .plt call stub, but
100 the first instruction restores r2, and the std is omitted. */
101 #define LD_R2_40R1 0xe8410028 /* ld %r2,40(%r1) */
102
103 /* Always allow this much space. */
104 #define GLINK_CALL_STUB_SIZE (8*4)
105
106 /* Pad with this. */
107 #define NOP 0x60000000
108
109 /* Some other nops. */
110 #define CROR_151515 0x4def7b82
111 #define CROR_313131 0x4ffffb82
112
113 /* .glink entries for the first 32k functions are two instructions. */
114 #define LI_R0_0 0x38000000 /* li %r0,0 */
115 #define B_DOT 0x48000000 /* b . */
116
117 /* After that, we need two instructions to load the index, followed by
118 a branch. */
119 #define LIS_R0_0 0x3c000000 /* lis %r0,0 */
120 #define ORI_R0_R0_0 0x60000000 /* ori %r0,%r0,0 */
121
122 /* Instructions to save and restore floating point regs. */
123 #define STFD_FR0_0R1 0xd8010000 /* stfd %fr0,0(%r1) */
124 #define LFD_FR0_0R1 0xc8010000 /* lfd %fr0,0(%r1) */
125 #define BLR 0x4e800020 /* blr */
126
127 /* Since .opd is an array of descriptors and each entry will end up
128 with identical R_PPC64_RELATIVE relocs, there is really no need to
129 propagate .opd relocs; The dynamic linker should be taught to
130 relocate .opd without reloc entries. */
131 #ifndef NO_OPD_RELOCS
132 #define NO_OPD_RELOCS 0
133 #endif
134 \f
135 /* Relocation HOWTO's. */
136 static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC_max];
137
138 static reloc_howto_type ppc64_elf_howto_raw[] = {
139 /* This reloc does nothing. */
140 HOWTO (R_PPC64_NONE, /* type */
141 0, /* rightshift */
142 2, /* size (0 = byte, 1 = short, 2 = long) */
143 32, /* bitsize */
144 false, /* pc_relative */
145 0, /* bitpos */
146 complain_overflow_bitfield, /* complain_on_overflow */
147 bfd_elf_generic_reloc, /* special_function */
148 "R_PPC64_NONE", /* name */
149 false, /* partial_inplace */
150 0, /* src_mask */
151 0, /* dst_mask */
152 false), /* pcrel_offset */
153
154 /* A standard 32 bit relocation. */
155 HOWTO (R_PPC64_ADDR32, /* type */
156 0, /* rightshift */
157 2, /* size (0 = byte, 1 = short, 2 = long) */
158 32, /* bitsize */
159 false, /* pc_relative */
160 0, /* bitpos */
161 complain_overflow_bitfield, /* complain_on_overflow */
162 bfd_elf_generic_reloc, /* special_function */
163 "R_PPC64_ADDR32", /* name */
164 false, /* partial_inplace */
165 0, /* src_mask */
166 0xffffffff, /* dst_mask */
167 false), /* pcrel_offset */
168
169 /* An absolute 26 bit branch; the lower two bits must be zero.
170 FIXME: we don't check that, we just clear them. */
171 HOWTO (R_PPC64_ADDR24, /* type */
172 0, /* rightshift */
173 2, /* size (0 = byte, 1 = short, 2 = long) */
174 26, /* bitsize */
175 false, /* pc_relative */
176 0, /* bitpos */
177 complain_overflow_bitfield, /* complain_on_overflow */
178 bfd_elf_generic_reloc, /* special_function */
179 "R_PPC64_ADDR24", /* name */
180 false, /* partial_inplace */
181 0, /* src_mask */
182 0x3fffffc, /* dst_mask */
183 false), /* pcrel_offset */
184
185 /* A standard 16 bit relocation. */
186 HOWTO (R_PPC64_ADDR16, /* type */
187 0, /* rightshift */
188 1, /* size (0 = byte, 1 = short, 2 = long) */
189 16, /* bitsize */
190 false, /* pc_relative */
191 0, /* bitpos */
192 complain_overflow_bitfield, /* complain_on_overflow */
193 bfd_elf_generic_reloc, /* special_function */
194 "R_PPC64_ADDR16", /* name */
195 false, /* partial_inplace */
196 0, /* src_mask */
197 0xffff, /* dst_mask */
198 false), /* pcrel_offset */
199
200 /* A 16 bit relocation without overflow. */
201 HOWTO (R_PPC64_ADDR16_LO, /* type */
202 0, /* rightshift */
203 1, /* size (0 = byte, 1 = short, 2 = long) */
204 16, /* bitsize */
205 false, /* pc_relative */
206 0, /* bitpos */
207 complain_overflow_dont,/* complain_on_overflow */
208 bfd_elf_generic_reloc, /* special_function */
209 "R_PPC64_ADDR16_LO", /* name */
210 false, /* partial_inplace */
211 0, /* src_mask */
212 0xffff, /* dst_mask */
213 false), /* pcrel_offset */
214
215 /* Bits 16-31 of an address. */
216 HOWTO (R_PPC64_ADDR16_HI, /* type */
217 16, /* rightshift */
218 1, /* size (0 = byte, 1 = short, 2 = long) */
219 16, /* bitsize */
220 false, /* pc_relative */
221 0, /* bitpos */
222 complain_overflow_dont, /* complain_on_overflow */
223 bfd_elf_generic_reloc, /* special_function */
224 "R_PPC64_ADDR16_HI", /* name */
225 false, /* partial_inplace */
226 0, /* src_mask */
227 0xffff, /* dst_mask */
228 false), /* pcrel_offset */
229
230 /* Bits 16-31 of an address, plus 1 if the contents of the low 16
231 bits, treated as a signed number, is negative. */
232 HOWTO (R_PPC64_ADDR16_HA, /* type */
233 16, /* rightshift */
234 1, /* size (0 = byte, 1 = short, 2 = long) */
235 16, /* bitsize */
236 false, /* pc_relative */
237 0, /* bitpos */
238 complain_overflow_dont, /* complain_on_overflow */
239 ppc64_elf_ha_reloc, /* special_function */
240 "R_PPC64_ADDR16_HA", /* name */
241 false, /* partial_inplace */
242 0, /* src_mask */
243 0xffff, /* dst_mask */
244 false), /* pcrel_offset */
245
246 /* An absolute 16 bit branch; the lower two bits must be zero.
247 FIXME: we don't check that, we just clear them. */
248 HOWTO (R_PPC64_ADDR14, /* type */
249 0, /* rightshift */
250 2, /* size (0 = byte, 1 = short, 2 = long) */
251 16, /* bitsize */
252 false, /* pc_relative */
253 0, /* bitpos */
254 complain_overflow_bitfield, /* complain_on_overflow */
255 bfd_elf_generic_reloc, /* special_function */
256 "R_PPC64_ADDR14", /* name */
257 false, /* partial_inplace */
258 0, /* src_mask */
259 0xfffc, /* dst_mask */
260 false), /* pcrel_offset */
261
262 /* An absolute 16 bit branch, for which bit 10 should be set to
263 indicate that the branch is expected to be taken. The lower two
264 bits must be zero. */
265 HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
266 0, /* rightshift */
267 2, /* size (0 = byte, 1 = short, 2 = long) */
268 16, /* bitsize */
269 false, /* pc_relative */
270 0, /* bitpos */
271 complain_overflow_bitfield, /* complain_on_overflow */
272 ppc64_elf_brtaken_reloc, /* special_function */
273 "R_PPC64_ADDR14_BRTAKEN",/* name */
274 false, /* partial_inplace */
275 0, /* src_mask */
276 0xfffc, /* dst_mask */
277 false), /* pcrel_offset */
278
279 /* An absolute 16 bit branch, for which bit 10 should be set to
280 indicate that the branch is not expected to be taken. The lower
281 two bits must be zero. */
282 HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
283 0, /* rightshift */
284 2, /* size (0 = byte, 1 = short, 2 = long) */
285 16, /* bitsize */
286 false, /* pc_relative */
287 0, /* bitpos */
288 complain_overflow_bitfield, /* complain_on_overflow */
289 ppc64_elf_brtaken_reloc, /* special_function */
290 "R_PPC64_ADDR14_BRNTAKEN",/* name */
291 false, /* partial_inplace */
292 0, /* src_mask */
293 0xfffc, /* dst_mask */
294 false), /* pcrel_offset */
295
296 /* A relative 26 bit branch; the lower two bits must be zero. */
297 HOWTO (R_PPC64_REL24, /* type */
298 0, /* rightshift */
299 2, /* size (0 = byte, 1 = short, 2 = long) */
300 26, /* bitsize */
301 true, /* pc_relative */
302 0, /* bitpos */
303 complain_overflow_signed, /* complain_on_overflow */
304 bfd_elf_generic_reloc, /* special_function */
305 "R_PPC64_REL24", /* name */
306 false, /* partial_inplace */
307 0, /* src_mask */
308 0x3fffffc, /* dst_mask */
309 true), /* pcrel_offset */
310
311 /* A relative 16 bit branch; the lower two bits must be zero. */
312 HOWTO (R_PPC64_REL14, /* type */
313 0, /* rightshift */
314 2, /* size (0 = byte, 1 = short, 2 = long) */
315 16, /* bitsize */
316 true, /* pc_relative */
317 0, /* bitpos */
318 complain_overflow_signed, /* complain_on_overflow */
319 bfd_elf_generic_reloc, /* special_function */
320 "R_PPC64_REL14", /* name */
321 false, /* partial_inplace */
322 0, /* src_mask */
323 0xfffc, /* dst_mask */
324 true), /* pcrel_offset */
325
326 /* A relative 16 bit branch. Bit 10 should be set to indicate that
327 the branch is expected to be taken. The lower two bits must be
328 zero. */
329 HOWTO (R_PPC64_REL14_BRTAKEN, /* type */
330 0, /* rightshift */
331 2, /* size (0 = byte, 1 = short, 2 = long) */
332 16, /* bitsize */
333 true, /* pc_relative */
334 0, /* bitpos */
335 complain_overflow_signed, /* complain_on_overflow */
336 ppc64_elf_brtaken_reloc, /* special_function */
337 "R_PPC64_REL14_BRTAKEN", /* name */
338 false, /* partial_inplace */
339 0, /* src_mask */
340 0xfffc, /* dst_mask */
341 true), /* pcrel_offset */
342
343 /* A relative 16 bit branch. Bit 10 should be set to indicate that
344 the branch is not expected to be taken. The lower two bits must
345 be zero. */
346 HOWTO (R_PPC64_REL14_BRNTAKEN, /* type */
347 0, /* rightshift */
348 2, /* size (0 = byte, 1 = short, 2 = long) */
349 16, /* bitsize */
350 true, /* pc_relative */
351 0, /* bitpos */
352 complain_overflow_signed, /* complain_on_overflow */
353 ppc64_elf_brtaken_reloc, /* special_function */
354 "R_PPC64_REL14_BRNTAKEN",/* name */
355 false, /* partial_inplace */
356 0, /* src_mask */
357 0xfffc, /* dst_mask */
358 true), /* pcrel_offset */
359
360 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
361 symbol. */
362 HOWTO (R_PPC64_GOT16, /* type */
363 0, /* rightshift */
364 1, /* size (0 = byte, 1 = short, 2 = long) */
365 16, /* bitsize */
366 false, /* pc_relative */
367 0, /* bitpos */
368 complain_overflow_signed, /* complain_on_overflow */
369 ppc64_elf_unhandled_reloc, /* special_function */
370 "R_PPC64_GOT16", /* name */
371 false, /* partial_inplace */
372 0, /* src_mask */
373 0xffff, /* dst_mask */
374 false), /* pcrel_offset */
375
376 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
377 the symbol. */
378 HOWTO (R_PPC64_GOT16_LO, /* type */
379 0, /* rightshift */
380 1, /* size (0 = byte, 1 = short, 2 = long) */
381 16, /* bitsize */
382 false, /* pc_relative */
383 0, /* bitpos */
384 complain_overflow_dont, /* complain_on_overflow */
385 ppc64_elf_unhandled_reloc, /* special_function */
386 "R_PPC64_GOT16_LO", /* name */
387 false, /* partial_inplace */
388 0, /* src_mask */
389 0xffff, /* dst_mask */
390 false), /* pcrel_offset */
391
392 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
393 the symbol. */
394 HOWTO (R_PPC64_GOT16_HI, /* type */
395 16, /* rightshift */
396 1, /* size (0 = byte, 1 = short, 2 = long) */
397 16, /* bitsize */
398 false, /* pc_relative */
399 0, /* bitpos */
400 complain_overflow_dont,/* complain_on_overflow */
401 ppc64_elf_unhandled_reloc, /* special_function */
402 "R_PPC64_GOT16_HI", /* name */
403 false, /* partial_inplace */
404 0, /* src_mask */
405 0xffff, /* dst_mask */
406 false), /* pcrel_offset */
407
408 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
409 the symbol. */
410 HOWTO (R_PPC64_GOT16_HA, /* type */
411 16, /* rightshift */
412 1, /* size (0 = byte, 1 = short, 2 = long) */
413 16, /* bitsize */
414 false, /* pc_relative */
415 0, /* bitpos */
416 complain_overflow_dont,/* complain_on_overflow */
417 ppc64_elf_unhandled_reloc, /* special_function */
418 "R_PPC64_GOT16_HA", /* name */
419 false, /* partial_inplace */
420 0, /* src_mask */
421 0xffff, /* dst_mask */
422 false), /* pcrel_offset */
423
424 /* This is used only by the dynamic linker. The symbol should exist
425 both in the object being run and in some shared library. The
426 dynamic linker copies the data addressed by the symbol from the
427 shared library into the object, because the object being
428 run has to have the data at some particular address. */
429 HOWTO (R_PPC64_COPY, /* type */
430 0, /* rightshift */
431 2, /* size (0 = byte, 1 = short, 2 = long) */
432 32, /* bitsize */
433 false, /* pc_relative */
434 0, /* bitpos */
435 complain_overflow_bitfield, /* complain_on_overflow */
436 ppc64_elf_unhandled_reloc, /* special_function */
437 "R_PPC64_COPY", /* name */
438 false, /* partial_inplace */
439 0, /* src_mask */
440 0, /* dst_mask */
441 false), /* pcrel_offset */
442
443 /* Like R_PPC64_ADDR64, but used when setting global offset table
444 entries. */
445 HOWTO (R_PPC64_GLOB_DAT, /* type */
446 0, /* rightshift */
447 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
448 64, /* bitsize */
449 false, /* pc_relative */
450 0, /* bitpos */
451 complain_overflow_dont, /* complain_on_overflow */
452 ppc64_elf_unhandled_reloc, /* special_function */
453 "R_PPC64_GLOB_DAT", /* name */
454 false, /* partial_inplace */
455 0, /* src_mask */
456 0xffffffffffffffff, /* dst_mask */
457 false), /* pcrel_offset */
458
459 /* Created by the link editor. Marks a procedure linkage table
460 entry for a symbol. */
461 HOWTO (R_PPC64_JMP_SLOT, /* type */
462 0, /* rightshift */
463 0, /* size (0 = byte, 1 = short, 2 = long) */
464 0, /* bitsize */
465 false, /* pc_relative */
466 0, /* bitpos */
467 complain_overflow_dont, /* complain_on_overflow */
468 ppc64_elf_unhandled_reloc, /* special_function */
469 "R_PPC64_JMP_SLOT", /* name */
470 false, /* partial_inplace */
471 0, /* src_mask */
472 0, /* dst_mask */
473 false), /* pcrel_offset */
474
475 /* Used only by the dynamic linker. When the object is run, this
476 doubleword64 is set to the load address of the object, plus the
477 addend. */
478 HOWTO (R_PPC64_RELATIVE, /* type */
479 0, /* rightshift */
480 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
481 64, /* bitsize */
482 false, /* pc_relative */
483 0, /* bitpos */
484 complain_overflow_dont, /* complain_on_overflow */
485 bfd_elf_generic_reloc, /* special_function */
486 "R_PPC64_RELATIVE", /* name */
487 false, /* partial_inplace */
488 0, /* src_mask */
489 0xffffffffffffffff, /* dst_mask */
490 false), /* pcrel_offset */
491
492 /* Like R_PPC64_ADDR32, but may be unaligned. */
493 HOWTO (R_PPC64_UADDR32, /* type */
494 0, /* rightshift */
495 2, /* size (0 = byte, 1 = short, 2 = long) */
496 32, /* bitsize */
497 false, /* pc_relative */
498 0, /* bitpos */
499 complain_overflow_bitfield, /* complain_on_overflow */
500 bfd_elf_generic_reloc, /* special_function */
501 "R_PPC64_UADDR32", /* name */
502 false, /* partial_inplace */
503 0, /* src_mask */
504 0xffffffff, /* dst_mask */
505 false), /* pcrel_offset */
506
507 /* Like R_PPC64_ADDR16, but may be unaligned. */
508 HOWTO (R_PPC64_UADDR16, /* type */
509 0, /* rightshift */
510 1, /* size (0 = byte, 1 = short, 2 = long) */
511 16, /* bitsize */
512 false, /* pc_relative */
513 0, /* bitpos */
514 complain_overflow_bitfield, /* complain_on_overflow */
515 bfd_elf_generic_reloc, /* special_function */
516 "R_PPC64_UADDR16", /* name */
517 false, /* partial_inplace */
518 0, /* src_mask */
519 0xffff, /* dst_mask */
520 false), /* pcrel_offset */
521
522 /* 32-bit PC relative. */
523 HOWTO (R_PPC64_REL32, /* type */
524 0, /* rightshift */
525 2, /* size (0 = byte, 1 = short, 2 = long) */
526 32, /* bitsize */
527 true, /* pc_relative */
528 0, /* bitpos */
529 /* FIXME: Verify. Was complain_overflow_bitfield. */
530 complain_overflow_signed, /* complain_on_overflow */
531 bfd_elf_generic_reloc, /* special_function */
532 "R_PPC64_REL32", /* name */
533 false, /* partial_inplace */
534 0, /* src_mask */
535 0xffffffff, /* dst_mask */
536 true), /* pcrel_offset */
537
538 /* 32-bit relocation to the symbol's procedure linkage table. */
539 HOWTO (R_PPC64_PLT32, /* type */
540 0, /* rightshift */
541 2, /* size (0 = byte, 1 = short, 2 = long) */
542 32, /* bitsize */
543 false, /* pc_relative */
544 0, /* bitpos */
545 complain_overflow_bitfield, /* complain_on_overflow */
546 ppc64_elf_unhandled_reloc, /* special_function */
547 "R_PPC64_PLT32", /* name */
548 false, /* partial_inplace */
549 0, /* src_mask */
550 0, /* dst_mask */
551 false), /* pcrel_offset */
552
553 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
554 FIXME: R_PPC64_PLTREL32 not supported. */
555 HOWTO (R_PPC64_PLTREL32, /* type */
556 0, /* rightshift */
557 2, /* size (0 = byte, 1 = short, 2 = long) */
558 32, /* bitsize */
559 true, /* pc_relative */
560 0, /* bitpos */
561 complain_overflow_signed, /* complain_on_overflow */
562 bfd_elf_generic_reloc, /* special_function */
563 "R_PPC64_PLTREL32", /* name */
564 false, /* partial_inplace */
565 0, /* src_mask */
566 0, /* dst_mask */
567 true), /* pcrel_offset */
568
569 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
570 the symbol. */
571 HOWTO (R_PPC64_PLT16_LO, /* type */
572 0, /* rightshift */
573 1, /* size (0 = byte, 1 = short, 2 = long) */
574 16, /* bitsize */
575 false, /* pc_relative */
576 0, /* bitpos */
577 complain_overflow_dont, /* complain_on_overflow */
578 ppc64_elf_unhandled_reloc, /* special_function */
579 "R_PPC64_PLT16_LO", /* name */
580 false, /* partial_inplace */
581 0, /* src_mask */
582 0xffff, /* dst_mask */
583 false), /* pcrel_offset */
584
585 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
586 the symbol. */
587 HOWTO (R_PPC64_PLT16_HI, /* type */
588 16, /* rightshift */
589 1, /* size (0 = byte, 1 = short, 2 = long) */
590 16, /* bitsize */
591 false, /* pc_relative */
592 0, /* bitpos */
593 complain_overflow_dont, /* complain_on_overflow */
594 ppc64_elf_unhandled_reloc, /* special_function */
595 "R_PPC64_PLT16_HI", /* name */
596 false, /* partial_inplace */
597 0, /* src_mask */
598 0xffff, /* dst_mask */
599 false), /* pcrel_offset */
600
601 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
602 the symbol. */
603 HOWTO (R_PPC64_PLT16_HA, /* type */
604 16, /* rightshift */
605 1, /* size (0 = byte, 1 = short, 2 = long) */
606 16, /* bitsize */
607 false, /* pc_relative */
608 0, /* bitpos */
609 complain_overflow_dont, /* complain_on_overflow */
610 ppc64_elf_unhandled_reloc, /* special_function */
611 "R_PPC64_PLT16_HA", /* name */
612 false, /* partial_inplace */
613 0, /* src_mask */
614 0xffff, /* dst_mask */
615 false), /* pcrel_offset */
616
617 /* 16-bit section relative relocation. */
618 HOWTO (R_PPC64_SECTOFF, /* type */
619 0, /* rightshift */
620 1, /* size (0 = byte, 1 = short, 2 = long) */
621 16, /* bitsize */
622 false, /* pc_relative */
623 0, /* bitpos */
624 complain_overflow_bitfield, /* complain_on_overflow */
625 ppc64_elf_sectoff_reloc, /* special_function */
626 "R_PPC64_SECTOFF", /* name */
627 false, /* partial_inplace */
628 0, /* src_mask */
629 0xffff, /* dst_mask */
630 false), /* pcrel_offset */
631
632 /* Like R_PPC64_SECTOFF, but no overflow warning. */
633 HOWTO (R_PPC64_SECTOFF_LO, /* type */
634 0, /* rightshift */
635 1, /* size (0 = byte, 1 = short, 2 = long) */
636 16, /* bitsize */
637 false, /* pc_relative */
638 0, /* bitpos */
639 complain_overflow_dont, /* complain_on_overflow */
640 ppc64_elf_sectoff_reloc, /* special_function */
641 "R_PPC64_SECTOFF_LO", /* name */
642 false, /* partial_inplace */
643 0, /* src_mask */
644 0xffff, /* dst_mask */
645 false), /* pcrel_offset */
646
647 /* 16-bit upper half section relative relocation. */
648 HOWTO (R_PPC64_SECTOFF_HI, /* type */
649 16, /* rightshift */
650 1, /* size (0 = byte, 1 = short, 2 = long) */
651 16, /* bitsize */
652 false, /* pc_relative */
653 0, /* bitpos */
654 complain_overflow_dont, /* complain_on_overflow */
655 ppc64_elf_sectoff_reloc, /* special_function */
656 "R_PPC64_SECTOFF_HI", /* name */
657 false, /* partial_inplace */
658 0, /* src_mask */
659 0xffff, /* dst_mask */
660 false), /* pcrel_offset */
661
662 /* 16-bit upper half adjusted section relative relocation. */
663 HOWTO (R_PPC64_SECTOFF_HA, /* type */
664 16, /* rightshift */
665 1, /* size (0 = byte, 1 = short, 2 = long) */
666 16, /* bitsize */
667 false, /* pc_relative */
668 0, /* bitpos */
669 complain_overflow_dont, /* complain_on_overflow */
670 ppc64_elf_sectoff_ha_reloc, /* special_function */
671 "R_PPC64_SECTOFF_HA", /* name */
672 false, /* partial_inplace */
673 0, /* src_mask */
674 0xffff, /* dst_mask */
675 false), /* pcrel_offset */
676
677 /* Like R_PPC64_REL24 without touching the two least significant
678 bits. Should have been named R_PPC64_REL30! */
679 HOWTO (R_PPC64_ADDR30, /* type */
680 2, /* rightshift */
681 2, /* size (0 = byte, 1 = short, 2 = long) */
682 30, /* bitsize */
683 true, /* pc_relative */
684 0, /* bitpos */
685 complain_overflow_dont, /* complain_on_overflow */
686 bfd_elf_generic_reloc, /* special_function */
687 "R_PPC64_ADDR30", /* name */
688 false, /* partial_inplace */
689 0, /* src_mask */
690 0xfffffffc, /* dst_mask */
691 true), /* pcrel_offset */
692
693 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
694
695 /* A standard 64-bit relocation. */
696 HOWTO (R_PPC64_ADDR64, /* type */
697 0, /* rightshift */
698 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
699 64, /* bitsize */
700 false, /* pc_relative */
701 0, /* bitpos */
702 complain_overflow_dont, /* complain_on_overflow */
703 bfd_elf_generic_reloc, /* special_function */
704 "R_PPC64_ADDR64", /* name */
705 false, /* partial_inplace */
706 0, /* src_mask */
707 0xffffffffffffffff, /* dst_mask */
708 false), /* pcrel_offset */
709
710 /* The bits 32-47 of an address. */
711 HOWTO (R_PPC64_ADDR16_HIGHER, /* type */
712 32, /* rightshift */
713 1, /* size (0 = byte, 1 = short, 2 = long) */
714 16, /* bitsize */
715 false, /* pc_relative */
716 0, /* bitpos */
717 complain_overflow_dont, /* complain_on_overflow */
718 bfd_elf_generic_reloc, /* special_function */
719 "R_PPC64_ADDR16_HIGHER", /* name */
720 false, /* partial_inplace */
721 0, /* src_mask */
722 0xffff, /* dst_mask */
723 false), /* pcrel_offset */
724
725 /* The bits 32-47 of an address, plus 1 if the contents of the low
726 16 bits, treated as a signed number, is negative. */
727 HOWTO (R_PPC64_ADDR16_HIGHERA, /* type */
728 32, /* rightshift */
729 1, /* size (0 = byte, 1 = short, 2 = long) */
730 16, /* bitsize */
731 false, /* pc_relative */
732 0, /* bitpos */
733 complain_overflow_dont, /* complain_on_overflow */
734 ppc64_elf_ha_reloc, /* special_function */
735 "R_PPC64_ADDR16_HIGHERA", /* name */
736 false, /* partial_inplace */
737 0, /* src_mask */
738 0xffff, /* dst_mask */
739 false), /* pcrel_offset */
740
741 /* The bits 48-63 of an address. */
742 HOWTO (R_PPC64_ADDR16_HIGHEST,/* type */
743 48, /* rightshift */
744 1, /* size (0 = byte, 1 = short, 2 = long) */
745 16, /* bitsize */
746 false, /* pc_relative */
747 0, /* bitpos */
748 complain_overflow_dont, /* complain_on_overflow */
749 bfd_elf_generic_reloc, /* special_function */
750 "R_PPC64_ADDR16_HIGHEST", /* name */
751 false, /* partial_inplace */
752 0, /* src_mask */
753 0xffff, /* dst_mask */
754 false), /* pcrel_offset */
755
756 /* The bits 48-63 of an address, plus 1 if the contents of the low
757 16 bits, treated as a signed number, is negative. */
758 HOWTO (R_PPC64_ADDR16_HIGHESTA,/* type */
759 48, /* rightshift */
760 1, /* size (0 = byte, 1 = short, 2 = long) */
761 16, /* bitsize */
762 false, /* pc_relative */
763 0, /* bitpos */
764 complain_overflow_dont, /* complain_on_overflow */
765 ppc64_elf_ha_reloc, /* special_function */
766 "R_PPC64_ADDR16_HIGHESTA", /* name */
767 false, /* partial_inplace */
768 0, /* src_mask */
769 0xffff, /* dst_mask */
770 false), /* pcrel_offset */
771
772 /* Like ADDR64, but may be unaligned. */
773 HOWTO (R_PPC64_UADDR64, /* type */
774 0, /* rightshift */
775 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
776 64, /* bitsize */
777 false, /* pc_relative */
778 0, /* bitpos */
779 complain_overflow_dont, /* complain_on_overflow */
780 bfd_elf_generic_reloc, /* special_function */
781 "R_PPC64_UADDR64", /* name */
782 false, /* partial_inplace */
783 0, /* src_mask */
784 0xffffffffffffffff, /* dst_mask */
785 false), /* pcrel_offset */
786
787 /* 64-bit relative relocation. */
788 HOWTO (R_PPC64_REL64, /* type */
789 0, /* rightshift */
790 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
791 64, /* bitsize */
792 true, /* pc_relative */
793 0, /* bitpos */
794 complain_overflow_dont, /* complain_on_overflow */
795 bfd_elf_generic_reloc, /* special_function */
796 "R_PPC64_REL64", /* name */
797 false, /* partial_inplace */
798 0, /* src_mask */
799 0xffffffffffffffff, /* dst_mask */
800 true), /* pcrel_offset */
801
802 /* 64-bit relocation to the symbol's procedure linkage table. */
803 HOWTO (R_PPC64_PLT64, /* type */
804 0, /* rightshift */
805 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
806 64, /* bitsize */
807 false, /* pc_relative */
808 0, /* bitpos */
809 complain_overflow_dont, /* complain_on_overflow */
810 ppc64_elf_unhandled_reloc, /* special_function */
811 "R_PPC64_PLT64", /* name */
812 false, /* partial_inplace */
813 0, /* src_mask */
814 0, /* dst_mask */
815 false), /* pcrel_offset */
816
817 /* 64-bit PC relative relocation to the symbol's procedure linkage
818 table. */
819 /* FIXME: R_PPC64_PLTREL64 not supported. */
820 HOWTO (R_PPC64_PLTREL64, /* type */
821 0, /* rightshift */
822 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
823 64, /* bitsize */
824 true, /* pc_relative */
825 0, /* bitpos */
826 complain_overflow_dont, /* complain_on_overflow */
827 ppc64_elf_unhandled_reloc, /* special_function */
828 "R_PPC64_PLTREL64", /* name */
829 false, /* partial_inplace */
830 0, /* src_mask */
831 0, /* dst_mask */
832 true), /* pcrel_offset */
833
834 /* 16 bit TOC-relative relocation. */
835
836 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
837 HOWTO (R_PPC64_TOC16, /* type */
838 0, /* rightshift */
839 1, /* size (0 = byte, 1 = short, 2 = long) */
840 16, /* bitsize */
841 false, /* pc_relative */
842 0, /* bitpos */
843 complain_overflow_signed, /* complain_on_overflow */
844 ppc64_elf_toc_reloc, /* special_function */
845 "R_PPC64_TOC16", /* name */
846 false, /* partial_inplace */
847 0, /* src_mask */
848 0xffff, /* dst_mask */
849 false), /* pcrel_offset */
850
851 /* 16 bit TOC-relative relocation without overflow. */
852
853 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
854 HOWTO (R_PPC64_TOC16_LO, /* type */
855 0, /* rightshift */
856 1, /* size (0 = byte, 1 = short, 2 = long) */
857 16, /* bitsize */
858 false, /* pc_relative */
859 0, /* bitpos */
860 complain_overflow_dont, /* complain_on_overflow */
861 ppc64_elf_toc_reloc, /* special_function */
862 "R_PPC64_TOC16_LO", /* name */
863 false, /* partial_inplace */
864 0, /* src_mask */
865 0xffff, /* dst_mask */
866 false), /* pcrel_offset */
867
868 /* 16 bit TOC-relative relocation, high 16 bits. */
869
870 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
871 HOWTO (R_PPC64_TOC16_HI, /* type */
872 16, /* rightshift */
873 1, /* size (0 = byte, 1 = short, 2 = long) */
874 16, /* bitsize */
875 false, /* pc_relative */
876 0, /* bitpos */
877 complain_overflow_dont, /* complain_on_overflow */
878 ppc64_elf_toc_reloc, /* special_function */
879 "R_PPC64_TOC16_HI", /* name */
880 false, /* partial_inplace */
881 0, /* src_mask */
882 0xffff, /* dst_mask */
883 false), /* pcrel_offset */
884
885 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
886 contents of the low 16 bits, treated as a signed number, is
887 negative. */
888
889 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
890 HOWTO (R_PPC64_TOC16_HA, /* type */
891 16, /* rightshift */
892 1, /* size (0 = byte, 1 = short, 2 = long) */
893 16, /* bitsize */
894 false, /* pc_relative */
895 0, /* bitpos */
896 complain_overflow_dont, /* complain_on_overflow */
897 ppc64_elf_toc_ha_reloc, /* special_function */
898 "R_PPC64_TOC16_HA", /* name */
899 false, /* partial_inplace */
900 0, /* src_mask */
901 0xffff, /* dst_mask */
902 false), /* pcrel_offset */
903
904 /* 64-bit relocation; insert value of TOC base (.TOC.). */
905
906 /* R_PPC64_TOC 51 doubleword64 .TOC. */
907 HOWTO (R_PPC64_TOC, /* type */
908 0, /* rightshift */
909 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
910 64, /* bitsize */
911 false, /* pc_relative */
912 0, /* bitpos */
913 complain_overflow_bitfield, /* complain_on_overflow */
914 ppc64_elf_toc64_reloc, /* special_function */
915 "R_PPC64_TOC", /* name */
916 false, /* partial_inplace */
917 0, /* src_mask */
918 0xffffffffffffffff, /* dst_mask */
919 false), /* pcrel_offset */
920
921 /* Like R_PPC64_GOT16, but also informs the link editor that the
922 value to relocate may (!) refer to a PLT entry which the link
923 editor (a) may replace with the symbol value. If the link editor
924 is unable to fully resolve the symbol, it may (b) create a PLT
925 entry and store the address to the new PLT entry in the GOT.
926 This permits lazy resolution of function symbols at run time.
927 The link editor may also skip all of this and just (c) emit a
928 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
929 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
930 HOWTO (R_PPC64_PLTGOT16, /* type */
931 0, /* rightshift */
932 1, /* size (0 = byte, 1 = short, 2 = long) */
933 16, /* bitsize */
934 false, /* pc_relative */
935 0, /* bitpos */
936 complain_overflow_signed, /* complain_on_overflow */
937 ppc64_elf_unhandled_reloc, /* special_function */
938 "R_PPC64_PLTGOT16", /* name */
939 false, /* partial_inplace */
940 0, /* src_mask */
941 0xffff, /* dst_mask */
942 false), /* pcrel_offset */
943
944 /* Like R_PPC64_PLTGOT16, but without overflow. */
945 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
946 HOWTO (R_PPC64_PLTGOT16_LO, /* type */
947 0, /* rightshift */
948 1, /* size (0 = byte, 1 = short, 2 = long) */
949 16, /* bitsize */
950 false, /* pc_relative */
951 0, /* bitpos */
952 complain_overflow_dont, /* complain_on_overflow */
953 ppc64_elf_unhandled_reloc, /* special_function */
954 "R_PPC64_PLTGOT16_LO", /* name */
955 false, /* partial_inplace */
956 0, /* src_mask */
957 0xffff, /* dst_mask */
958 false), /* pcrel_offset */
959
960 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
961 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
962 HOWTO (R_PPC64_PLTGOT16_HI, /* type */
963 16, /* rightshift */
964 1, /* size (0 = byte, 1 = short, 2 = long) */
965 16, /* bitsize */
966 false, /* pc_relative */
967 0, /* bitpos */
968 complain_overflow_dont, /* complain_on_overflow */
969 ppc64_elf_unhandled_reloc, /* special_function */
970 "R_PPC64_PLTGOT16_HI", /* name */
971 false, /* partial_inplace */
972 0, /* src_mask */
973 0xffff, /* dst_mask */
974 false), /* pcrel_offset */
975
976 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
977 1 if the contents of the low 16 bits, treated as a signed number,
978 is negative. */
979 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
980 HOWTO (R_PPC64_PLTGOT16_HA, /* type */
981 16, /* rightshift */
982 1, /* size (0 = byte, 1 = short, 2 = long) */
983 16, /* bitsize */
984 false, /* pc_relative */
985 0, /* bitpos */
986 complain_overflow_dont,/* complain_on_overflow */
987 ppc64_elf_unhandled_reloc, /* special_function */
988 "R_PPC64_PLTGOT16_HA", /* name */
989 false, /* partial_inplace */
990 0, /* src_mask */
991 0xffff, /* dst_mask */
992 false), /* pcrel_offset */
993
994 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
995 HOWTO (R_PPC64_ADDR16_DS, /* type */
996 0, /* rightshift */
997 1, /* size (0 = byte, 1 = short, 2 = long) */
998 16, /* bitsize */
999 false, /* pc_relative */
1000 0, /* bitpos */
1001 complain_overflow_bitfield, /* complain_on_overflow */
1002 bfd_elf_generic_reloc, /* special_function */
1003 "R_PPC64_ADDR16_DS", /* name */
1004 false, /* partial_inplace */
1005 0, /* src_mask */
1006 0xfffc, /* dst_mask */
1007 false), /* pcrel_offset */
1008
1009 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
1010 HOWTO (R_PPC64_ADDR16_LO_DS, /* type */
1011 0, /* rightshift */
1012 1, /* size (0 = byte, 1 = short, 2 = long) */
1013 16, /* bitsize */
1014 false, /* pc_relative */
1015 0, /* bitpos */
1016 complain_overflow_dont,/* complain_on_overflow */
1017 bfd_elf_generic_reloc, /* special_function */
1018 "R_PPC64_ADDR16_LO_DS",/* name */
1019 false, /* partial_inplace */
1020 0, /* src_mask */
1021 0xfffc, /* dst_mask */
1022 false), /* pcrel_offset */
1023
1024 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
1025 HOWTO (R_PPC64_GOT16_DS, /* type */
1026 0, /* rightshift */
1027 1, /* size (0 = byte, 1 = short, 2 = long) */
1028 16, /* bitsize */
1029 false, /* pc_relative */
1030 0, /* bitpos */
1031 complain_overflow_signed, /* complain_on_overflow */
1032 ppc64_elf_unhandled_reloc, /* special_function */
1033 "R_PPC64_GOT16_DS", /* name */
1034 false, /* partial_inplace */
1035 0, /* src_mask */
1036 0xfffc, /* dst_mask */
1037 false), /* pcrel_offset */
1038
1039 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
1040 HOWTO (R_PPC64_GOT16_LO_DS, /* type */
1041 0, /* rightshift */
1042 1, /* size (0 = byte, 1 = short, 2 = long) */
1043 16, /* bitsize */
1044 false, /* pc_relative */
1045 0, /* bitpos */
1046 complain_overflow_dont, /* complain_on_overflow */
1047 ppc64_elf_unhandled_reloc, /* special_function */
1048 "R_PPC64_GOT16_LO_DS", /* name */
1049 false, /* partial_inplace */
1050 0, /* src_mask */
1051 0xfffc, /* dst_mask */
1052 false), /* pcrel_offset */
1053
1054 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
1055 HOWTO (R_PPC64_PLT16_LO_DS, /* type */
1056 0, /* rightshift */
1057 1, /* size (0 = byte, 1 = short, 2 = long) */
1058 16, /* bitsize */
1059 false, /* pc_relative */
1060 0, /* bitpos */
1061 complain_overflow_dont, /* complain_on_overflow */
1062 ppc64_elf_unhandled_reloc, /* special_function */
1063 "R_PPC64_PLT16_LO_DS", /* name */
1064 false, /* partial_inplace */
1065 0, /* src_mask */
1066 0xfffc, /* dst_mask */
1067 false), /* pcrel_offset */
1068
1069 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
1070 HOWTO (R_PPC64_SECTOFF_DS, /* type */
1071 0, /* rightshift */
1072 1, /* size (0 = byte, 1 = short, 2 = long) */
1073 16, /* bitsize */
1074 false, /* pc_relative */
1075 0, /* bitpos */
1076 complain_overflow_bitfield, /* complain_on_overflow */
1077 ppc64_elf_sectoff_reloc, /* special_function */
1078 "R_PPC64_SECTOFF_DS", /* name */
1079 false, /* partial_inplace */
1080 0, /* src_mask */
1081 0xfffc, /* dst_mask */
1082 false), /* pcrel_offset */
1083
1084 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
1085 HOWTO (R_PPC64_SECTOFF_LO_DS, /* type */
1086 0, /* rightshift */
1087 1, /* size (0 = byte, 1 = short, 2 = long) */
1088 16, /* bitsize */
1089 false, /* pc_relative */
1090 0, /* bitpos */
1091 complain_overflow_dont, /* complain_on_overflow */
1092 ppc64_elf_sectoff_reloc, /* special_function */
1093 "R_PPC64_SECTOFF_LO_DS",/* name */
1094 false, /* partial_inplace */
1095 0, /* src_mask */
1096 0xfffc, /* dst_mask */
1097 false), /* pcrel_offset */
1098
1099 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
1100 HOWTO (R_PPC64_TOC16_DS, /* type */
1101 0, /* rightshift */
1102 1, /* size (0 = byte, 1 = short, 2 = long) */
1103 16, /* bitsize */
1104 false, /* pc_relative */
1105 0, /* bitpos */
1106 complain_overflow_signed, /* complain_on_overflow */
1107 ppc64_elf_toc_reloc, /* special_function */
1108 "R_PPC64_TOC16_DS", /* name */
1109 false, /* partial_inplace */
1110 0, /* src_mask */
1111 0xfffc, /* dst_mask */
1112 false), /* pcrel_offset */
1113
1114 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
1115 HOWTO (R_PPC64_TOC16_LO_DS, /* type */
1116 0, /* rightshift */
1117 1, /* size (0 = byte, 1 = short, 2 = long) */
1118 16, /* bitsize */
1119 false, /* pc_relative */
1120 0, /* bitpos */
1121 complain_overflow_dont, /* complain_on_overflow */
1122 ppc64_elf_toc_reloc, /* special_function */
1123 "R_PPC64_TOC16_LO_DS", /* name */
1124 false, /* partial_inplace */
1125 0, /* src_mask */
1126 0xfffc, /* dst_mask */
1127 false), /* pcrel_offset */
1128
1129 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
1130 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
1131 HOWTO (R_PPC64_PLTGOT16_DS, /* type */
1132 0, /* rightshift */
1133 1, /* size (0 = byte, 1 = short, 2 = long) */
1134 16, /* bitsize */
1135 false, /* pc_relative */
1136 0, /* bitpos */
1137 complain_overflow_signed, /* complain_on_overflow */
1138 ppc64_elf_unhandled_reloc, /* special_function */
1139 "R_PPC64_PLTGOT16_DS", /* name */
1140 false, /* partial_inplace */
1141 0, /* src_mask */
1142 0xfffc, /* dst_mask */
1143 false), /* pcrel_offset */
1144
1145 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
1146 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1147 HOWTO (R_PPC64_PLTGOT16_LO_DS,/* type */
1148 0, /* rightshift */
1149 1, /* size (0 = byte, 1 = short, 2 = long) */
1150 16, /* bitsize */
1151 false, /* pc_relative */
1152 0, /* bitpos */
1153 complain_overflow_dont, /* complain_on_overflow */
1154 ppc64_elf_unhandled_reloc, /* special_function */
1155 "R_PPC64_PLTGOT16_LO_DS",/* name */
1156 false, /* partial_inplace */
1157 0, /* src_mask */
1158 0xfffc, /* dst_mask */
1159 false), /* pcrel_offset */
1160
1161 /* GNU extension to record C++ vtable hierarchy. */
1162 HOWTO (R_PPC64_GNU_VTINHERIT, /* type */
1163 0, /* rightshift */
1164 0, /* size (0 = byte, 1 = short, 2 = long) */
1165 0, /* bitsize */
1166 false, /* pc_relative */
1167 0, /* bitpos */
1168 complain_overflow_dont, /* complain_on_overflow */
1169 NULL, /* special_function */
1170 "R_PPC64_GNU_VTINHERIT", /* name */
1171 false, /* partial_inplace */
1172 0, /* src_mask */
1173 0, /* dst_mask */
1174 false), /* pcrel_offset */
1175
1176 /* GNU extension to record C++ vtable member usage. */
1177 HOWTO (R_PPC64_GNU_VTENTRY, /* type */
1178 0, /* rightshift */
1179 0, /* size (0 = byte, 1 = short, 2 = long) */
1180 0, /* bitsize */
1181 false, /* pc_relative */
1182 0, /* bitpos */
1183 complain_overflow_dont, /* complain_on_overflow */
1184 NULL, /* special_function */
1185 "R_PPC64_GNU_VTENTRY", /* name */
1186 false, /* partial_inplace */
1187 0, /* src_mask */
1188 0, /* dst_mask */
1189 false), /* pcrel_offset */
1190 };
1191
1192 \f
1193 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
1194 be done. */
1195
1196 static void
1197 ppc_howto_init ()
1198 {
1199 unsigned int i, type;
1200
1201 for (i = 0;
1202 i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
1203 i++)
1204 {
1205 type = ppc64_elf_howto_raw[i].type;
1206 BFD_ASSERT (type < (sizeof (ppc64_elf_howto_table)
1207 / sizeof (ppc64_elf_howto_table[0])));
1208 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
1209 }
1210 }
1211
1212 static reloc_howto_type *
1213 ppc64_elf_reloc_type_lookup (abfd, code)
1214 bfd *abfd ATTRIBUTE_UNUSED;
1215 bfd_reloc_code_real_type code;
1216 {
1217 enum elf_ppc_reloc_type ppc_reloc = R_PPC_NONE;
1218
1219 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
1220 /* Initialize howto table if needed. */
1221 ppc_howto_init ();
1222
1223 switch ((int) code)
1224 {
1225 default:
1226 return (reloc_howto_type *) NULL;
1227
1228 case BFD_RELOC_NONE: ppc_reloc = R_PPC64_NONE;
1229 break;
1230 case BFD_RELOC_32: ppc_reloc = R_PPC64_ADDR32;
1231 break;
1232 case BFD_RELOC_PPC_BA26: ppc_reloc = R_PPC64_ADDR24;
1233 break;
1234 case BFD_RELOC_16: ppc_reloc = R_PPC64_ADDR16;
1235 break;
1236 case BFD_RELOC_LO16: ppc_reloc = R_PPC64_ADDR16_LO;
1237 break;
1238 case BFD_RELOC_HI16: ppc_reloc = R_PPC64_ADDR16_HI;
1239 break;
1240 case BFD_RELOC_HI16_S: ppc_reloc = R_PPC64_ADDR16_HA;
1241 break;
1242 case BFD_RELOC_PPC_BA16: ppc_reloc = R_PPC64_ADDR14;
1243 break;
1244 case BFD_RELOC_PPC_BA16_BRTAKEN: ppc_reloc = R_PPC64_ADDR14_BRTAKEN;
1245 break;
1246 case BFD_RELOC_PPC_BA16_BRNTAKEN: ppc_reloc = R_PPC64_ADDR14_BRNTAKEN;
1247 break;
1248 case BFD_RELOC_PPC_B26: ppc_reloc = R_PPC64_REL24;
1249 break;
1250 case BFD_RELOC_PPC_B16: ppc_reloc = R_PPC64_REL14;
1251 break;
1252 case BFD_RELOC_PPC_B16_BRTAKEN: ppc_reloc = R_PPC64_REL14_BRTAKEN;
1253 break;
1254 case BFD_RELOC_PPC_B16_BRNTAKEN: ppc_reloc = R_PPC64_REL14_BRNTAKEN;
1255 break;
1256 case BFD_RELOC_16_GOTOFF: ppc_reloc = R_PPC64_GOT16;
1257 break;
1258 case BFD_RELOC_LO16_GOTOFF: ppc_reloc = R_PPC64_GOT16_LO;
1259 break;
1260 case BFD_RELOC_HI16_GOTOFF: ppc_reloc = R_PPC64_GOT16_HI;
1261 break;
1262 case BFD_RELOC_HI16_S_GOTOFF: ppc_reloc = R_PPC64_GOT16_HA;
1263 break;
1264 case BFD_RELOC_PPC_COPY: ppc_reloc = R_PPC64_COPY;
1265 break;
1266 case BFD_RELOC_PPC_GLOB_DAT: ppc_reloc = R_PPC64_GLOB_DAT;
1267 break;
1268 case BFD_RELOC_32_PCREL: ppc_reloc = R_PPC64_REL32;
1269 break;
1270 case BFD_RELOC_32_PLTOFF: ppc_reloc = R_PPC64_PLT32;
1271 break;
1272 case BFD_RELOC_32_PLT_PCREL: ppc_reloc = R_PPC64_PLTREL32;
1273 break;
1274 case BFD_RELOC_LO16_PLTOFF: ppc_reloc = R_PPC64_PLT16_LO;
1275 break;
1276 case BFD_RELOC_HI16_PLTOFF: ppc_reloc = R_PPC64_PLT16_HI;
1277 break;
1278 case BFD_RELOC_HI16_S_PLTOFF: ppc_reloc = R_PPC64_PLT16_HA;
1279 break;
1280 case BFD_RELOC_16_BASEREL: ppc_reloc = R_PPC64_SECTOFF;
1281 break;
1282 case BFD_RELOC_LO16_BASEREL: ppc_reloc = R_PPC64_SECTOFF_LO;
1283 break;
1284 case BFD_RELOC_HI16_BASEREL: ppc_reloc = R_PPC64_SECTOFF_HI;
1285 break;
1286 case BFD_RELOC_HI16_S_BASEREL: ppc_reloc = R_PPC64_SECTOFF_HA;
1287 break;
1288 case BFD_RELOC_CTOR: ppc_reloc = R_PPC64_ADDR64;
1289 break;
1290 case BFD_RELOC_64: ppc_reloc = R_PPC64_ADDR64;
1291 break;
1292 case BFD_RELOC_PPC64_HIGHER: ppc_reloc = R_PPC64_ADDR16_HIGHER;
1293 break;
1294 case BFD_RELOC_PPC64_HIGHER_S: ppc_reloc = R_PPC64_ADDR16_HIGHERA;
1295 break;
1296 case BFD_RELOC_PPC64_HIGHEST: ppc_reloc = R_PPC64_ADDR16_HIGHEST;
1297 break;
1298 case BFD_RELOC_PPC64_HIGHEST_S: ppc_reloc = R_PPC64_ADDR16_HIGHESTA;
1299 break;
1300 case BFD_RELOC_64_PCREL: ppc_reloc = R_PPC64_REL64;
1301 break;
1302 case BFD_RELOC_64_PLTOFF: ppc_reloc = R_PPC64_PLT64;
1303 break;
1304 case BFD_RELOC_64_PLT_PCREL: ppc_reloc = R_PPC64_PLTREL64;
1305 break;
1306 case BFD_RELOC_PPC_TOC16: ppc_reloc = R_PPC64_TOC16;
1307 break;
1308 case BFD_RELOC_PPC64_TOC16_LO: ppc_reloc = R_PPC64_TOC16_LO;
1309 break;
1310 case BFD_RELOC_PPC64_TOC16_HI: ppc_reloc = R_PPC64_TOC16_HI;
1311 break;
1312 case BFD_RELOC_PPC64_TOC16_HA: ppc_reloc = R_PPC64_TOC16_HA;
1313 break;
1314 case BFD_RELOC_PPC64_TOC: ppc_reloc = R_PPC64_TOC;
1315 break;
1316 case BFD_RELOC_PPC64_PLTGOT16: ppc_reloc = R_PPC64_PLTGOT16;
1317 break;
1318 case BFD_RELOC_PPC64_PLTGOT16_LO: ppc_reloc = R_PPC64_PLTGOT16_LO;
1319 break;
1320 case BFD_RELOC_PPC64_PLTGOT16_HI: ppc_reloc = R_PPC64_PLTGOT16_HI;
1321 break;
1322 case BFD_RELOC_PPC64_PLTGOT16_HA: ppc_reloc = R_PPC64_PLTGOT16_HA;
1323 break;
1324 case BFD_RELOC_PPC64_ADDR16_DS: ppc_reloc = R_PPC64_ADDR16_DS;
1325 break;
1326 case BFD_RELOC_PPC64_ADDR16_LO_DS: ppc_reloc = R_PPC64_ADDR16_LO_DS;
1327 break;
1328 case BFD_RELOC_PPC64_GOT16_DS: ppc_reloc = R_PPC64_GOT16_DS;
1329 break;
1330 case BFD_RELOC_PPC64_GOT16_LO_DS: ppc_reloc = R_PPC64_GOT16_LO_DS;
1331 break;
1332 case BFD_RELOC_PPC64_PLT16_LO_DS: ppc_reloc = R_PPC64_PLT16_LO_DS;
1333 break;
1334 case BFD_RELOC_PPC64_SECTOFF_DS: ppc_reloc = R_PPC64_SECTOFF_DS;
1335 break;
1336 case BFD_RELOC_PPC64_SECTOFF_LO_DS: ppc_reloc = R_PPC64_SECTOFF_LO_DS;
1337 break;
1338 case BFD_RELOC_PPC64_TOC16_DS: ppc_reloc = R_PPC64_TOC16_DS;
1339 break;
1340 case BFD_RELOC_PPC64_TOC16_LO_DS: ppc_reloc = R_PPC64_TOC16_LO_DS;
1341 break;
1342 case BFD_RELOC_PPC64_PLTGOT16_DS: ppc_reloc = R_PPC64_PLTGOT16_DS;
1343 break;
1344 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: ppc_reloc = R_PPC64_PLTGOT16_LO_DS;
1345 break;
1346 case BFD_RELOC_VTABLE_INHERIT: ppc_reloc = R_PPC64_GNU_VTINHERIT;
1347 break;
1348 case BFD_RELOC_VTABLE_ENTRY: ppc_reloc = R_PPC64_GNU_VTENTRY;
1349 break;
1350 }
1351
1352 return ppc64_elf_howto_table[(int) ppc_reloc];
1353 };
1354
1355 /* Set the howto pointer for a PowerPC ELF reloc. */
1356
1357 static void
1358 ppc64_elf_info_to_howto (abfd, cache_ptr, dst)
1359 bfd *abfd ATTRIBUTE_UNUSED;
1360 arelent *cache_ptr;
1361 Elf64_Internal_Rela *dst;
1362 {
1363 unsigned int type;
1364
1365 /* Initialize howto table if needed. */
1366 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
1367 ppc_howto_init ();
1368
1369 type = ELF64_R_TYPE (dst->r_info);
1370 BFD_ASSERT (type < (sizeof (ppc64_elf_howto_table)
1371 / sizeof (ppc64_elf_howto_table[0])));
1372 cache_ptr->howto = ppc64_elf_howto_table[type];
1373 }
1374
1375 /* Handle the R_PPC_ADDR16_HA and similar relocs. */
1376
1377 static bfd_reloc_status_type
1378 ppc64_elf_ha_reloc (abfd, reloc_entry, symbol, data,
1379 input_section, output_bfd, error_message)
1380 bfd *abfd;
1381 arelent *reloc_entry;
1382 asymbol *symbol;
1383 PTR data;
1384 asection *input_section;
1385 bfd *output_bfd;
1386 char **error_message;
1387 {
1388 /* If this is a relocatable link (output_bfd test tells us), just
1389 call the generic function. Any adjustment will be done at final
1390 link time. */
1391 if (output_bfd != NULL)
1392 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1393 input_section, output_bfd, error_message);
1394
1395 /* Adjust the addend for sign extension of the low 16 bits.
1396 We won't actually be using the low 16 bits, so trashing them
1397 doesn't matter. */
1398 reloc_entry->addend += 0x8000;
1399 return bfd_reloc_continue;
1400 }
1401
1402 static bfd_reloc_status_type
1403 ppc64_elf_brtaken_reloc (abfd, reloc_entry, symbol, data,
1404 input_section, output_bfd, error_message)
1405 bfd *abfd;
1406 arelent *reloc_entry;
1407 asymbol *symbol;
1408 PTR data;
1409 asection *input_section;
1410 bfd *output_bfd;
1411 char **error_message;
1412 {
1413 long insn;
1414 enum elf_ppc_reloc_type r_type;
1415 bfd_size_type octets;
1416 /* Disabled until we sort out how ld should choose 'y' vs 'at'. */
1417 boolean is_power4 = false;
1418
1419 /* If this is a relocatable link (output_bfd test tells us), just
1420 call the generic function. Any adjustment will be done at final
1421 link time. */
1422 if (output_bfd != NULL)
1423 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1424 input_section, output_bfd, error_message);
1425
1426 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
1427 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1428 insn &= ~(0x01 << 21);
1429 r_type = (enum elf_ppc_reloc_type) reloc_entry->howto->type;
1430 if (r_type == R_PPC64_ADDR14_BRTAKEN
1431 || r_type == R_PPC64_REL14_BRTAKEN)
1432 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
1433
1434 if (is_power4)
1435 {
1436 /* Set 'a' bit. This is 0b00010 in BO field for branch
1437 on CR(BI) insns (BO == 001at or 011at), and 0b01000
1438 for branch on CTR insns (BO == 1a00t or 1a01t). */
1439 if ((insn & (0x14 << 21)) == (0x04 << 21))
1440 insn |= 0x02 << 21;
1441 else if ((insn & (0x14 << 21)) == (0x10 << 21))
1442 insn |= 0x08 << 21;
1443 else
1444 return bfd_reloc_continue;
1445 }
1446 else
1447 {
1448 bfd_vma target = 0;
1449 bfd_vma from;
1450
1451 if (!bfd_is_com_section (symbol->section))
1452 target = symbol->value;
1453 target += symbol->section->output_section->vma;
1454 target += symbol->section->output_offset;
1455 target += reloc_entry->addend;
1456
1457 from = (reloc_entry->address
1458 + input_section->output_offset
1459 + input_section->output_section->vma);
1460
1461 /* Invert 'y' bit if not the default. */
1462 if ((bfd_signed_vma) (target - from) < 0)
1463 insn ^= 0x01 << 21;
1464 }
1465 bfd_put_32 (abfd, (bfd_vma) insn, (bfd_byte *) data + octets);
1466 return bfd_reloc_continue;
1467 }
1468
1469 static bfd_reloc_status_type
1470 ppc64_elf_sectoff_reloc (abfd, reloc_entry, symbol, data,
1471 input_section, output_bfd, error_message)
1472 bfd *abfd;
1473 arelent *reloc_entry;
1474 asymbol *symbol;
1475 PTR data;
1476 asection *input_section;
1477 bfd *output_bfd;
1478 char **error_message;
1479 {
1480 /* If this is a relocatable link (output_bfd test tells us), just
1481 call the generic function. Any adjustment will be done at final
1482 link time. */
1483 if (output_bfd != NULL)
1484 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1485 input_section, output_bfd, error_message);
1486
1487 /* Subtract the symbol section base address. */
1488 reloc_entry->addend -= symbol->section->output_section->vma;
1489 return bfd_reloc_continue;
1490 }
1491
1492 static bfd_reloc_status_type
1493 ppc64_elf_sectoff_ha_reloc (abfd, reloc_entry, symbol, data,
1494 input_section, output_bfd, error_message)
1495 bfd *abfd;
1496 arelent *reloc_entry;
1497 asymbol *symbol;
1498 PTR data;
1499 asection *input_section;
1500 bfd *output_bfd;
1501 char **error_message;
1502 {
1503 /* If this is a relocatable link (output_bfd test tells us), just
1504 call the generic function. Any adjustment will be done at final
1505 link time. */
1506 if (output_bfd != NULL)
1507 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1508 input_section, output_bfd, error_message);
1509
1510 /* Subtract the symbol section base address. */
1511 reloc_entry->addend -= symbol->section->output_section->vma;
1512
1513 /* Adjust the addend for sign extension of the low 16 bits. */
1514 reloc_entry->addend += 0x8000;
1515 return bfd_reloc_continue;
1516 }
1517
1518 static bfd_reloc_status_type
1519 ppc64_elf_toc_reloc (abfd, reloc_entry, symbol, data,
1520 input_section, output_bfd, error_message)
1521 bfd *abfd;
1522 arelent *reloc_entry;
1523 asymbol *symbol;
1524 PTR data;
1525 asection *input_section;
1526 bfd *output_bfd;
1527 char **error_message;
1528 {
1529 bfd_vma TOCstart;
1530
1531 /* If this is a relocatable link (output_bfd test tells us), just
1532 call the generic function. Any adjustment will be done at final
1533 link time. */
1534 if (output_bfd != NULL)
1535 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1536 input_section, output_bfd, error_message);
1537
1538 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1539 if (TOCstart == 0)
1540 TOCstart = ppc64_elf_toc (input_section->output_section->owner);
1541
1542 /* Subtract the TOC base address. */
1543 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1544 return bfd_reloc_continue;
1545 }
1546
1547 static bfd_reloc_status_type
1548 ppc64_elf_toc_ha_reloc (abfd, reloc_entry, symbol, data,
1549 input_section, output_bfd, error_message)
1550 bfd *abfd;
1551 arelent *reloc_entry;
1552 asymbol *symbol;
1553 PTR data;
1554 asection *input_section;
1555 bfd *output_bfd;
1556 char **error_message;
1557 {
1558 bfd_vma TOCstart;
1559
1560 /* If this is a relocatable link (output_bfd test tells us), just
1561 call the generic function. Any adjustment will be done at final
1562 link time. */
1563 if (output_bfd != NULL)
1564 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1565 input_section, output_bfd, error_message);
1566
1567 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1568 if (TOCstart == 0)
1569 TOCstart = ppc64_elf_toc (input_section->output_section->owner);
1570
1571 /* Subtract the TOC base address. */
1572 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1573
1574 /* Adjust the addend for sign extension of the low 16 bits. */
1575 reloc_entry->addend += 0x8000;
1576 return bfd_reloc_continue;
1577 }
1578
1579 static bfd_reloc_status_type
1580 ppc64_elf_toc64_reloc (abfd, reloc_entry, symbol, data,
1581 input_section, output_bfd, error_message)
1582 bfd *abfd;
1583 arelent *reloc_entry;
1584 asymbol *symbol;
1585 PTR data;
1586 asection *input_section;
1587 bfd *output_bfd;
1588 char **error_message;
1589 {
1590 bfd_vma TOCstart;
1591 bfd_size_type octets;
1592
1593 /* If this is a relocatable link (output_bfd test tells us), just
1594 call the generic function. Any adjustment will be done at final
1595 link time. */
1596 if (output_bfd != NULL)
1597 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1598 input_section, output_bfd, error_message);
1599
1600 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1601 if (TOCstart == 0)
1602 TOCstart = ppc64_elf_toc (input_section->output_section->owner);
1603
1604 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
1605 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
1606 return bfd_reloc_ok;
1607 }
1608
1609 static bfd_reloc_status_type
1610 ppc64_elf_unhandled_reloc (abfd, reloc_entry, symbol, data,
1611 input_section, output_bfd, error_message)
1612 bfd *abfd;
1613 arelent *reloc_entry;
1614 asymbol *symbol;
1615 PTR data;
1616 asection *input_section;
1617 bfd *output_bfd;
1618 char **error_message;
1619 {
1620 /* If this is a relocatable link (output_bfd test tells us), just
1621 call the generic function. Any adjustment will be done at final
1622 link time. */
1623 if (output_bfd != NULL)
1624 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1625 input_section, output_bfd, error_message);
1626
1627 if (error_message != NULL)
1628 {
1629 static char buf[60];
1630 sprintf (buf, "generic linker can't handle %s",
1631 reloc_entry->howto->name);
1632 *error_message = buf;
1633 }
1634 return bfd_reloc_dangerous;
1635 }
1636
1637 /* Return symbol info as per usual for ELF targets, except that
1638 symbols in .opd are given 'd' or 'D' for type. */
1639
1640 static void
1641 ppc64_elf_get_symbol_info (abfd, symbol, ret)
1642 bfd *abfd;
1643 asymbol *symbol;
1644 symbol_info *ret;
1645 {
1646 _bfd_elf_get_symbol_info (abfd, symbol, ret);
1647 if (ret->type == '?'
1648 && (symbol->flags & (BSF_GLOBAL | BSF_LOCAL)) != 0
1649 && strcmp (symbol->section->name, ".opd") == 0)
1650 ret->type = (symbol->flags & BSF_GLOBAL) != 0 ? 'D' : 'd';
1651 }
1652
1653 /* Fix bad default arch selected for a 64 bit input bfd when the
1654 default is 32 bit. */
1655
1656 static boolean
1657 ppc64_elf_object_p (abfd)
1658 bfd *abfd;
1659 {
1660 if (abfd->arch_info->the_default && abfd->arch_info->bits_per_word == 32)
1661 {
1662 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
1663
1664 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
1665 {
1666 /* Relies on arch after 32 bit default being 64 bit default. */
1667 abfd->arch_info = abfd->arch_info->next;
1668 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
1669 }
1670 }
1671 return true;
1672 }
1673
1674 /* Function to set whether a module needs the -mrelocatable bit set. */
1675
1676 static boolean
1677 ppc64_elf_set_private_flags (abfd, flags)
1678 bfd *abfd;
1679 flagword flags;
1680 {
1681 BFD_ASSERT (!elf_flags_init (abfd)
1682 || elf_elfheader (abfd)->e_flags == flags);
1683
1684 elf_elfheader (abfd)->e_flags = flags;
1685 elf_flags_init (abfd) = true;
1686 return true;
1687 }
1688
1689 /* Merge backend specific data from an object file to the output
1690 object file when linking. */
1691 static boolean
1692 ppc64_elf_merge_private_bfd_data (ibfd, obfd)
1693 bfd *ibfd;
1694 bfd *obfd;
1695 {
1696 flagword old_flags;
1697 flagword new_flags;
1698 boolean error;
1699
1700 /* Check if we have the same endianess. */
1701 if (ibfd->xvec->byteorder != obfd->xvec->byteorder
1702 && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
1703 {
1704 const char *msg;
1705
1706 if (bfd_big_endian (ibfd))
1707 msg = _("%s: compiled for a big endian system and target is little endian");
1708 else
1709 msg = _("%s: compiled for a little endian system and target is big endian");
1710
1711 (*_bfd_error_handler) (msg, bfd_archive_filename (ibfd));
1712
1713 bfd_set_error (bfd_error_wrong_format);
1714 return false;
1715 }
1716
1717 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1718 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1719 return true;
1720
1721 new_flags = elf_elfheader (ibfd)->e_flags;
1722 old_flags = elf_elfheader (obfd)->e_flags;
1723 if (!elf_flags_init (obfd))
1724 {
1725 /* First call, no flags set. */
1726 elf_flags_init (obfd) = true;
1727 elf_elfheader (obfd)->e_flags = new_flags;
1728 }
1729
1730 else if (new_flags == old_flags)
1731 /* Compatible flags are ok. */
1732 ;
1733
1734 else
1735 {
1736 /* Incompatible flags. Warn about -mrelocatable mismatch.
1737 Allow -mrelocatable-lib to be linked with either. */
1738 error = false;
1739 if ((new_flags & EF_PPC_RELOCATABLE) != 0
1740 && (old_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0)
1741 {
1742 error = true;
1743 (*_bfd_error_handler)
1744 (_("%s: compiled with -mrelocatable and linked with modules compiled normally"),
1745 bfd_archive_filename (ibfd));
1746 }
1747 else if ((new_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0
1748 && (old_flags & EF_PPC_RELOCATABLE) != 0)
1749 {
1750 error = true;
1751 (*_bfd_error_handler)
1752 (_("%s: compiled normally and linked with modules compiled with -mrelocatable"),
1753 bfd_archive_filename (ibfd));
1754 }
1755
1756 /* The output is -mrelocatable-lib iff both the input files are. */
1757 if (! (new_flags & EF_PPC_RELOCATABLE_LIB))
1758 elf_elfheader (obfd)->e_flags &= ~EF_PPC_RELOCATABLE_LIB;
1759
1760 /* The output is -mrelocatable iff it can't be -mrelocatable-lib,
1761 but each input file is either -mrelocatable or -mrelocatable-lib. */
1762 if (! (elf_elfheader (obfd)->e_flags & EF_PPC_RELOCATABLE_LIB)
1763 && (new_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE))
1764 && (old_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE)))
1765 elf_elfheader (obfd)->e_flags |= EF_PPC_RELOCATABLE;
1766
1767 /* Do not warn about eabi vs. V.4 mismatch, just or in the bit
1768 if any module uses it. */
1769 elf_elfheader (obfd)->e_flags |= (new_flags & EF_PPC_EMB);
1770
1771 new_flags &= ~(EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
1772 old_flags &= ~(EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
1773
1774 /* Warn about any other mismatches. */
1775 if (new_flags != old_flags)
1776 {
1777 error = true;
1778 (*_bfd_error_handler)
1779 (_("%s: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
1780 bfd_archive_filename (ibfd), (long) new_flags, (long) old_flags);
1781 }
1782
1783 if (error)
1784 {
1785 bfd_set_error (bfd_error_bad_value);
1786 return false;
1787 }
1788 }
1789
1790 return true;
1791 }
1792
1793 /* Handle a PowerPC specific section when reading an object file. This
1794 is called when elfcode.h finds a section with an unknown type. */
1795
1796 static boolean
1797 ppc64_elf_section_from_shdr (abfd, hdr, name)
1798 bfd *abfd;
1799 Elf64_Internal_Shdr *hdr;
1800 const char *name;
1801 {
1802 asection *newsect;
1803 flagword flags;
1804
1805 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1806 return false;
1807
1808 newsect = hdr->bfd_section;
1809 flags = bfd_get_section_flags (abfd, newsect);
1810 if (hdr->sh_flags & SHF_EXCLUDE)
1811 flags |= SEC_EXCLUDE;
1812
1813 if (hdr->sh_type == SHT_ORDERED)
1814 flags |= SEC_SORT_ENTRIES;
1815
1816 bfd_set_section_flags (abfd, newsect, flags);
1817 return true;
1818 }
1819 \f
1820 /* The following functions are specific to the ELF linker, while
1821 functions above are used generally. Those named ppc64_elf_* are
1822 called by the main ELF linker code. They appear in this file more
1823 or less in the order in which they are called. eg.
1824 ppc64_elf_check_relocs is called early in the link process,
1825 ppc64_elf_finish_dynamic_sections is one of the last functions
1826 called.
1827
1828 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
1829 functions have both a function code symbol and a function descriptor
1830 symbol. A call to foo in a relocatable object file looks like:
1831
1832 . .text
1833 . x:
1834 . bl .foo
1835 . nop
1836
1837 The function definition in another object file might be:
1838
1839 . .section .opd
1840 . foo: .quad .foo
1841 . .quad .TOC.@tocbase
1842 . .quad 0
1843 .
1844 . .text
1845 . .foo: blr
1846
1847 When the linker resolves the call during a static link, the branch
1848 unsurprisingly just goes to .foo and the .opd information is unused.
1849 If the function definition is in a shared library, things are a little
1850 different: The call goes via a plt call stub, the opd information gets
1851 copied to the plt, and the linker patches the nop.
1852
1853 . x:
1854 . bl .foo_stub
1855 . ld 2,40(1)
1856 .
1857 .
1858 . .foo_stub:
1859 . addis 12,2,Lfoo@toc@ha # in practice, the call stub
1860 . addi 12,12,Lfoo@toc@l # is slightly optimised, but
1861 . std 2,40(1) # this is the general idea
1862 . ld 11,0(12)
1863 . ld 2,8(12)
1864 . mtctr 11
1865 . ld 11,16(12)
1866 . bctr
1867 .
1868 . .section .plt
1869 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
1870
1871 The "reloc ()" notation is supposed to indicate that the linker emits
1872 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
1873 copying.
1874
1875 What are the difficulties here? Well, firstly, the relocations
1876 examined by the linker in check_relocs are against the function code
1877 sym .foo, while the dynamic relocation in the plt is emitted against
1878 the function descriptor symbol, foo. Somewhere along the line, we need
1879 to carefully copy dynamic link information from one symbol to the other.
1880 Secondly, the generic part of the elf linker will make .foo a dynamic
1881 symbol as is normal for most other backends. We need foo dynamic
1882 instead, at least for an application final link. However, when
1883 creating a shared library containing foo, we need to have both symbols
1884 dynamic so that references to .foo are satisfied during the early
1885 stages of linking. Otherwise the linker might decide to pull in a
1886 definition from some other object, eg. a static library. */
1887
1888 /* The linker needs to keep track of the number of relocs that it
1889 decides to copy as dynamic relocs in check_relocs for each symbol.
1890 This is so that it can later discard them if they are found to be
1891 unnecessary. We store the information in a field extending the
1892 regular ELF linker hash table. */
1893
1894 struct ppc_dyn_relocs
1895 {
1896 struct ppc_dyn_relocs *next;
1897
1898 /* The input section of the reloc. */
1899 asection *sec;
1900
1901 /* Total number of relocs copied for the input section. */
1902 bfd_size_type count;
1903
1904 /* Number of pc-relative relocs copied for the input section. */
1905 bfd_size_type pc_count;
1906 };
1907
1908 /* Of those relocs that might be copied as dynamic relocs, this macro
1909 selects between relative and absolute types. */
1910
1911 #define IS_ABSOLUTE_RELOC(RTYPE) \
1912 ((RTYPE) != R_PPC64_REL32 \
1913 && (RTYPE) != R_PPC64_REL64 \
1914 && (RTYPE) != R_PPC64_ADDR30)
1915
1916 /* Section name for stubs is the associated section name plus this
1917 string. */
1918 #define STUB_SUFFIX ".stub"
1919
1920 /* Linker stubs.
1921 ppc_stub_long_branch:
1922 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
1923 destination, but a 24 bit branch in a stub section will reach.
1924 . b dest
1925
1926 ppc_stub_plt_branch:
1927 Similar to the above, but a 24 bit branch in the stub section won't
1928 reach its destination.
1929 . addis %r12,%r2,xxx@ha
1930 . ld %r11,xxx@l(%r12)
1931 . mtctr %r11
1932 . bctr
1933
1934 ppc_stub_plt_call:
1935 Used to call a function in a shared library.
1936 . addis %r12,%r2,xxx@ha
1937 . std %r2,40(%r1)
1938 . ld %r11,xxx+0@l(%r12)
1939 . ld %r2,xxx+8@l(%r12)
1940 . mtctr %r11
1941 . ld %r11,xxx+16@l(%r12)
1942 . bctr
1943 */
1944
1945 enum ppc_stub_type {
1946 ppc_stub_none,
1947 ppc_stub_long_branch,
1948 ppc_stub_plt_branch,
1949 ppc_stub_plt_call
1950 };
1951
1952 struct ppc_stub_hash_entry {
1953
1954 /* Base hash table entry structure. */
1955 struct bfd_hash_entry root;
1956
1957 /* The stub section. */
1958 asection *stub_sec;
1959
1960 /* Offset within stub_sec of the beginning of this stub. */
1961 bfd_vma stub_offset;
1962
1963 /* Given the symbol's value and its section we can determine its final
1964 value when building the stubs (so the stub knows where to jump. */
1965 bfd_vma target_value;
1966 asection *target_section;
1967
1968 enum ppc_stub_type stub_type;
1969
1970 /* The symbol table entry, if any, that this was derived from. */
1971 struct ppc_link_hash_entry *h;
1972
1973 /* Where this stub is being called from, or, in the case of combined
1974 stub sections, the first input section in the group. */
1975 asection *id_sec;
1976 };
1977
1978 struct ppc_branch_hash_entry {
1979
1980 /* Base hash table entry structure. */
1981 struct bfd_hash_entry root;
1982
1983 /* Offset within .branch_lt. */
1984 unsigned int offset;
1985
1986 /* Generation marker. */
1987 unsigned int iter;
1988 };
1989
1990 struct ppc_link_hash_entry
1991 {
1992 struct elf_link_hash_entry elf;
1993
1994 /* A pointer to the most recently used stub hash entry against this
1995 symbol. */
1996 struct ppc_stub_hash_entry *stub_cache;
1997
1998 /* Track dynamic relocs copied for this symbol. */
1999 struct ppc_dyn_relocs *dyn_relocs;
2000
2001 /* Link between function code and descriptor symbols. */
2002 struct elf_link_hash_entry *oh;
2003
2004 /* Flag function code and descriptor symbols. */
2005 unsigned int is_func:1;
2006 unsigned int is_func_descriptor:1;
2007 unsigned int is_entry:1;
2008 };
2009
2010 /* ppc64 ELF linker hash table. */
2011
2012 struct ppc_link_hash_table
2013 {
2014 struct elf_link_hash_table elf;
2015
2016 /* The stub hash table. */
2017 struct bfd_hash_table stub_hash_table;
2018
2019 /* Another hash table for plt_branch stubs. */
2020 struct bfd_hash_table branch_hash_table;
2021
2022 /* Linker stub bfd. */
2023 bfd *stub_bfd;
2024
2025 /* Linker call-backs. */
2026 asection * (*add_stub_section) PARAMS ((const char *, asection *));
2027 void (*layout_sections_again) PARAMS ((void));
2028
2029 /* Array to keep track of which stub sections have been created, and
2030 information on stub grouping. */
2031 struct map_stub {
2032 /* This is the section to which stubs in the group will be attached. */
2033 asection *link_sec;
2034 /* The stub section. */
2035 asection *stub_sec;
2036 } *stub_group;
2037
2038 /* Assorted information used by ppc64_elf_size_stubs. */
2039 unsigned int bfd_count;
2040 int top_index;
2041 asection **input_list;
2042 Elf_Internal_Sym **all_local_syms;
2043
2044 /* Short-cuts to get to dynamic linker sections. */
2045 asection *sgot;
2046 asection *srelgot;
2047 asection *splt;
2048 asection *srelplt;
2049 asection *sdynbss;
2050 asection *srelbss;
2051 asection *sglink;
2052 asection *sfpr;
2053 asection *sbrlt;
2054 asection *srelbrlt;
2055
2056 /* Set on error. */
2057 unsigned int stub_error;
2058
2059 /* Flag set when small branches are detected. Used to
2060 select suitable defaults for the stub group size. */
2061 unsigned int has_14bit_branch;
2062
2063 /* Set if we detect a reference undefined weak symbol. */
2064 unsigned int have_undefweak;
2065
2066 /* Incremented every time we size stubs. */
2067 unsigned int stub_iteration;
2068
2069 /* Small local sym to section mapping cache. */
2070 struct sym_sec_cache sym_sec;
2071 };
2072
2073 static struct bfd_hash_entry *stub_hash_newfunc
2074 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
2075 static struct bfd_hash_entry *branch_hash_newfunc
2076 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
2077 static struct bfd_hash_entry *link_hash_newfunc
2078 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
2079 static struct bfd_link_hash_table *ppc64_elf_link_hash_table_create
2080 PARAMS ((bfd *));
2081 static void ppc64_elf_link_hash_table_free
2082 PARAMS ((struct bfd_link_hash_table *));
2083 static char *ppc_stub_name
2084 PARAMS ((const asection *, const asection *,
2085 const struct ppc_link_hash_entry *, const Elf_Internal_Rela *));
2086 static struct ppc_stub_hash_entry *ppc_get_stub_entry
2087 PARAMS ((const asection *, const asection *, struct elf_link_hash_entry *,
2088 const Elf_Internal_Rela *, struct ppc_link_hash_table *));
2089 static struct ppc_stub_hash_entry *ppc_add_stub
2090 PARAMS ((const char *, asection *, struct ppc_link_hash_table *));
2091 static boolean create_linkage_sections
2092 PARAMS ((bfd *, struct bfd_link_info *));
2093 static boolean create_got_section
2094 PARAMS ((bfd *, struct bfd_link_info *));
2095 static boolean ppc64_elf_create_dynamic_sections
2096 PARAMS ((bfd *, struct bfd_link_info *));
2097 static void ppc64_elf_copy_indirect_symbol
2098 PARAMS ((struct elf_link_hash_entry *, struct elf_link_hash_entry *));
2099 static boolean ppc64_elf_check_relocs
2100 PARAMS ((bfd *, struct bfd_link_info *, asection *,
2101 const Elf_Internal_Rela *));
2102 static asection * ppc64_elf_gc_mark_hook
2103 PARAMS ((asection *, struct bfd_link_info *, Elf_Internal_Rela *,
2104 struct elf_link_hash_entry *, Elf_Internal_Sym *));
2105 static boolean ppc64_elf_gc_sweep_hook
2106 PARAMS ((bfd *, struct bfd_link_info *, asection *,
2107 const Elf_Internal_Rela *));
2108 static boolean func_desc_adjust
2109 PARAMS ((struct elf_link_hash_entry *, PTR));
2110 static boolean ppc64_elf_func_desc_adjust
2111 PARAMS ((bfd *, struct bfd_link_info *));
2112 static boolean ppc64_elf_adjust_dynamic_symbol
2113 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
2114 static void ppc64_elf_hide_symbol
2115 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *, boolean));
2116 static boolean edit_opd
2117 PARAMS ((bfd *, struct bfd_link_info *));
2118 static boolean allocate_dynrelocs
2119 PARAMS ((struct elf_link_hash_entry *, PTR));
2120 static boolean readonly_dynrelocs
2121 PARAMS ((struct elf_link_hash_entry *, PTR));
2122 static enum elf_reloc_type_class ppc64_elf_reloc_type_class
2123 PARAMS ((const Elf_Internal_Rela *));
2124 static boolean ppc64_elf_size_dynamic_sections
2125 PARAMS ((bfd *, struct bfd_link_info *));
2126 static INLINE enum ppc_stub_type ppc_type_of_stub
2127 PARAMS ((asection *, const Elf_Internal_Rela *,
2128 struct ppc_link_hash_entry **, bfd_vma));
2129 static bfd_byte *build_plt_stub
2130 PARAMS ((bfd *, bfd_byte *, int, int));
2131 static boolean ppc_build_one_stub
2132 PARAMS ((struct bfd_hash_entry *, PTR));
2133 static boolean ppc_size_one_stub
2134 PARAMS ((struct bfd_hash_entry *, PTR));
2135 static void group_sections
2136 PARAMS ((struct ppc_link_hash_table *, bfd_size_type, boolean));
2137 static boolean get_local_syms
2138 PARAMS ((bfd *, struct ppc_link_hash_table *));
2139 static boolean ppc64_elf_fake_sections
2140 PARAMS ((bfd *, Elf64_Internal_Shdr *, asection *));
2141 static boolean ppc64_elf_relocate_section
2142 PARAMS ((bfd *, struct bfd_link_info *info, bfd *, asection *, bfd_byte *,
2143 Elf_Internal_Rela *relocs, Elf_Internal_Sym *local_syms,
2144 asection **));
2145 static boolean ppc64_elf_finish_dynamic_symbol
2146 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
2147 Elf_Internal_Sym *));
2148 static boolean ppc64_elf_finish_dynamic_sections
2149 PARAMS ((bfd *, struct bfd_link_info *));
2150
2151 /* Get the ppc64 ELF linker hash table from a link_info structure. */
2152
2153 #define ppc_hash_table(p) \
2154 ((struct ppc_link_hash_table *) ((p)->hash))
2155
2156 #define ppc_stub_hash_lookup(table, string, create, copy) \
2157 ((struct ppc_stub_hash_entry *) \
2158 bfd_hash_lookup ((table), (string), (create), (copy)))
2159
2160 #define ppc_branch_hash_lookup(table, string, create, copy) \
2161 ((struct ppc_branch_hash_entry *) \
2162 bfd_hash_lookup ((table), (string), (create), (copy)))
2163
2164 /* Create an entry in the stub hash table. */
2165
2166 static struct bfd_hash_entry *
2167 stub_hash_newfunc (entry, table, string)
2168 struct bfd_hash_entry *entry;
2169 struct bfd_hash_table *table;
2170 const char *string;
2171 {
2172 /* Allocate the structure if it has not already been allocated by a
2173 subclass. */
2174 if (entry == NULL)
2175 {
2176 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
2177 if (entry == NULL)
2178 return entry;
2179 }
2180
2181 /* Call the allocation method of the superclass. */
2182 entry = bfd_hash_newfunc (entry, table, string);
2183 if (entry != NULL)
2184 {
2185 struct ppc_stub_hash_entry *eh;
2186
2187 /* Initialize the local fields. */
2188 eh = (struct ppc_stub_hash_entry *) entry;
2189 eh->stub_sec = NULL;
2190 eh->stub_offset = 0;
2191 eh->target_value = 0;
2192 eh->target_section = NULL;
2193 eh->stub_type = ppc_stub_none;
2194 eh->h = NULL;
2195 eh->id_sec = NULL;
2196 }
2197
2198 return entry;
2199 }
2200
2201 /* Create an entry in the branch hash table. */
2202
2203 static struct bfd_hash_entry *
2204 branch_hash_newfunc (entry, table, string)
2205 struct bfd_hash_entry *entry;
2206 struct bfd_hash_table *table;
2207 const char *string;
2208 {
2209 /* Allocate the structure if it has not already been allocated by a
2210 subclass. */
2211 if (entry == NULL)
2212 {
2213 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
2214 if (entry == NULL)
2215 return entry;
2216 }
2217
2218 /* Call the allocation method of the superclass. */
2219 entry = bfd_hash_newfunc (entry, table, string);
2220 if (entry != NULL)
2221 {
2222 struct ppc_branch_hash_entry *eh;
2223
2224 /* Initialize the local fields. */
2225 eh = (struct ppc_branch_hash_entry *) entry;
2226 eh->offset = 0;
2227 eh->iter = 0;
2228 }
2229
2230 return entry;
2231 }
2232
2233 /* Create an entry in a ppc64 ELF linker hash table. */
2234
2235 static struct bfd_hash_entry *
2236 link_hash_newfunc (entry, table, string)
2237 struct bfd_hash_entry *entry;
2238 struct bfd_hash_table *table;
2239 const char *string;
2240 {
2241 /* Allocate the structure if it has not already been allocated by a
2242 subclass. */
2243 if (entry == NULL)
2244 {
2245 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
2246 if (entry == NULL)
2247 return entry;
2248 }
2249
2250 /* Call the allocation method of the superclass. */
2251 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
2252 if (entry != NULL)
2253 {
2254 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
2255
2256 eh->stub_cache = NULL;
2257 eh->dyn_relocs = NULL;
2258 eh->oh = NULL;
2259 eh->is_func = 0;
2260 eh->is_func_descriptor = 0;
2261 eh->is_entry = 0;
2262 }
2263
2264 return entry;
2265 }
2266
2267 /* Create a ppc64 ELF linker hash table. */
2268
2269 static struct bfd_link_hash_table *
2270 ppc64_elf_link_hash_table_create (abfd)
2271 bfd *abfd;
2272 {
2273 struct ppc_link_hash_table *htab;
2274 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
2275
2276 htab = (struct ppc_link_hash_table *) bfd_malloc (amt);
2277 if (htab == NULL)
2278 return NULL;
2279
2280 if (! _bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc))
2281 {
2282 free (htab);
2283 return NULL;
2284 }
2285
2286 /* Init the stub hash table too. */
2287 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc))
2288 return NULL;
2289
2290 /* And the branch hash table. */
2291 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc))
2292 return NULL;
2293
2294 htab->stub_bfd = NULL;
2295 htab->add_stub_section = NULL;
2296 htab->layout_sections_again = NULL;
2297 htab->stub_group = NULL;
2298 htab->all_local_syms = NULL;
2299 htab->sgot = NULL;
2300 htab->srelgot = NULL;
2301 htab->splt = NULL;
2302 htab->srelplt = NULL;
2303 htab->sdynbss = NULL;
2304 htab->srelbss = NULL;
2305 htab->sglink = NULL;
2306 htab->sfpr = NULL;
2307 htab->sbrlt = NULL;
2308 htab->srelbrlt = NULL;
2309 htab->stub_error = 0;
2310 htab->has_14bit_branch = 0;
2311 htab->have_undefweak = 0;
2312 htab->stub_iteration = 0;
2313 htab->sym_sec.abfd = NULL;
2314
2315 return &htab->elf.root;
2316 }
2317
2318 /* Free the derived linker hash table. */
2319
2320 static void
2321 ppc64_elf_link_hash_table_free (hash)
2322 struct bfd_link_hash_table *hash;
2323 {
2324 struct ppc_link_hash_table *ret = (struct ppc_link_hash_table *) hash;
2325
2326 bfd_hash_table_free (&ret->stub_hash_table);
2327 bfd_hash_table_free (&ret->branch_hash_table);
2328 _bfd_generic_link_hash_table_free (hash);
2329 }
2330
2331 /* Build a name for an entry in the stub hash table. */
2332
2333 static char *
2334 ppc_stub_name (input_section, sym_sec, h, rel)
2335 const asection *input_section;
2336 const asection *sym_sec;
2337 const struct ppc_link_hash_entry *h;
2338 const Elf_Internal_Rela *rel;
2339 {
2340 char *stub_name;
2341 bfd_size_type len;
2342
2343 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
2344 offsets from a sym as a branch target? In fact, we could
2345 probably assume the addend is always zero. */
2346 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
2347
2348 if (h)
2349 {
2350 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
2351 stub_name = bfd_malloc (len);
2352 if (stub_name != NULL)
2353 {
2354 sprintf (stub_name, "%08x_%s+%x",
2355 input_section->id & 0xffffffff,
2356 h->elf.root.root.string,
2357 (int) rel->r_addend & 0xffffffff);
2358 }
2359 }
2360 else
2361 {
2362 len = 8 + 1 + 8 + 1 + 8 + 1 + 16 + 1;
2363 stub_name = bfd_malloc (len);
2364 if (stub_name != NULL)
2365 {
2366 sprintf (stub_name, "%08x_%x:%x+%x",
2367 input_section->id & 0xffffffff,
2368 sym_sec->id & 0xffffffff,
2369 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
2370 (int) rel->r_addend & 0xffffffff);
2371 }
2372 }
2373 return stub_name;
2374 }
2375
2376 /* Look up an entry in the stub hash. Stub entries are cached because
2377 creating the stub name takes a bit of time. */
2378
2379 static struct ppc_stub_hash_entry *
2380 ppc_get_stub_entry (input_section, sym_sec, hash, rel, htab)
2381 const asection *input_section;
2382 const asection *sym_sec;
2383 struct elf_link_hash_entry *hash;
2384 const Elf_Internal_Rela *rel;
2385 struct ppc_link_hash_table *htab;
2386 {
2387 struct ppc_stub_hash_entry *stub_entry;
2388 struct ppc_link_hash_entry *h = (struct ppc_link_hash_entry *) hash;
2389 const asection *id_sec;
2390
2391 /* If this input section is part of a group of sections sharing one
2392 stub section, then use the id of the first section in the group.
2393 Stub names need to include a section id, as there may well be
2394 more than one stub used to reach say, printf, and we need to
2395 distinguish between them. */
2396 id_sec = htab->stub_group[input_section->id].link_sec;
2397
2398 if (h != NULL && h->stub_cache != NULL
2399 && h->stub_cache->h == h
2400 && h->stub_cache->id_sec == id_sec)
2401 {
2402 stub_entry = h->stub_cache;
2403 }
2404 else
2405 {
2406 char *stub_name;
2407
2408 stub_name = ppc_stub_name (id_sec, sym_sec, h, rel);
2409 if (stub_name == NULL)
2410 return NULL;
2411
2412 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
2413 stub_name, false, false);
2414 if (h != NULL)
2415 h->stub_cache = stub_entry;
2416
2417 free (stub_name);
2418 }
2419
2420 return stub_entry;
2421 }
2422
2423 /* Add a new stub entry to the stub hash. Not all fields of the new
2424 stub entry are initialised. */
2425
2426 static struct ppc_stub_hash_entry *
2427 ppc_add_stub (stub_name, section, htab)
2428 const char *stub_name;
2429 asection *section;
2430 struct ppc_link_hash_table *htab;
2431 {
2432 asection *link_sec;
2433 asection *stub_sec;
2434 struct ppc_stub_hash_entry *stub_entry;
2435
2436 link_sec = htab->stub_group[section->id].link_sec;
2437 stub_sec = htab->stub_group[section->id].stub_sec;
2438 if (stub_sec == NULL)
2439 {
2440 stub_sec = htab->stub_group[link_sec->id].stub_sec;
2441 if (stub_sec == NULL)
2442 {
2443 size_t namelen;
2444 bfd_size_type len;
2445 char *s_name;
2446
2447 namelen = strlen (link_sec->name);
2448 len = namelen + sizeof (STUB_SUFFIX);
2449 s_name = bfd_alloc (htab->stub_bfd, len);
2450 if (s_name == NULL)
2451 return NULL;
2452
2453 memcpy (s_name, link_sec->name, namelen);
2454 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
2455 stub_sec = (*htab->add_stub_section) (s_name, link_sec);
2456 if (stub_sec == NULL)
2457 return NULL;
2458 htab->stub_group[link_sec->id].stub_sec = stub_sec;
2459 }
2460 htab->stub_group[section->id].stub_sec = stub_sec;
2461 }
2462
2463 /* Enter this entry into the linker stub hash table. */
2464 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
2465 true, false);
2466 if (stub_entry == NULL)
2467 {
2468 (*_bfd_error_handler) (_("%s: cannot create stub entry %s"),
2469 bfd_archive_filename (section->owner),
2470 stub_name);
2471 return NULL;
2472 }
2473
2474 stub_entry->stub_sec = stub_sec;
2475 stub_entry->stub_offset = 0;
2476 stub_entry->id_sec = link_sec;
2477 return stub_entry;
2478 }
2479
2480 /* Create sections for linker generated code. */
2481
2482 static boolean
2483 create_linkage_sections (dynobj, info)
2484 bfd *dynobj;
2485 struct bfd_link_info *info;
2486 {
2487 struct ppc_link_hash_table *htab;
2488 flagword flags;
2489
2490 htab = ppc_hash_table (info);
2491
2492 /* Create .sfpr for code to save and restore fp regs. */
2493 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
2494 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2495 htab->sfpr = bfd_make_section_anyway (dynobj, ".sfpr");
2496 if (htab->sfpr == NULL
2497 || ! bfd_set_section_flags (dynobj, htab->sfpr, flags)
2498 || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
2499 return false;
2500
2501 /* Create .glink for lazy dynamic linking support. */
2502 htab->sglink = bfd_make_section_anyway (dynobj, ".glink");
2503 if (htab->sglink == NULL
2504 || ! bfd_set_section_flags (dynobj, htab->sglink, flags)
2505 || ! bfd_set_section_alignment (dynobj, htab->sglink, 2))
2506 return false;
2507
2508 /* Create .branch_lt for plt_branch stubs. */
2509 flags = (SEC_ALLOC | SEC_LOAD
2510 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2511 htab->sbrlt = bfd_make_section_anyway (dynobj, ".branch_lt");
2512 if (htab->sbrlt == NULL
2513 || ! bfd_set_section_flags (dynobj, htab->sbrlt, flags)
2514 || ! bfd_set_section_alignment (dynobj, htab->sbrlt, 3))
2515 return false;
2516
2517 if (info->shared)
2518 {
2519 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
2520 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2521 htab->srelbrlt = bfd_make_section_anyway (dynobj, ".rela.branch_lt");
2522 if (!htab->srelbrlt
2523 || ! bfd_set_section_flags (dynobj, htab->srelbrlt, flags)
2524 || ! bfd_set_section_alignment (dynobj, htab->srelbrlt, 3))
2525 return false;
2526 }
2527 return true;
2528 }
2529
2530 /* Create .got and .rela.got sections in DYNOBJ, and set up
2531 shortcuts to them in our hash table. */
2532
2533 static boolean
2534 create_got_section (dynobj, info)
2535 bfd *dynobj;
2536 struct bfd_link_info *info;
2537 {
2538 struct ppc_link_hash_table *htab;
2539
2540 if (! _bfd_elf_create_got_section (dynobj, info))
2541 return false;
2542
2543 htab = ppc_hash_table (info);
2544 htab->sgot = bfd_get_section_by_name (dynobj, ".got");
2545 if (!htab->sgot)
2546 abort ();
2547
2548 htab->srelgot = bfd_make_section (dynobj, ".rela.got");
2549 if (!htab->srelgot
2550 || ! bfd_set_section_flags (dynobj, htab->srelgot,
2551 (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
2552 | SEC_IN_MEMORY | SEC_LINKER_CREATED
2553 | SEC_READONLY))
2554 || ! bfd_set_section_alignment (dynobj, htab->srelgot, 3))
2555 return false;
2556 return true;
2557 }
2558
2559 /* Create the dynamic sections, and set up shortcuts. */
2560
2561 static boolean
2562 ppc64_elf_create_dynamic_sections (dynobj, info)
2563 bfd *dynobj;
2564 struct bfd_link_info *info;
2565 {
2566 struct ppc_link_hash_table *htab;
2567
2568 htab = ppc_hash_table (info);
2569 if (!htab->sgot && !create_got_section (dynobj, info))
2570 return false;
2571
2572 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
2573 return false;
2574
2575 htab->splt = bfd_get_section_by_name (dynobj, ".plt");
2576 htab->srelplt = bfd_get_section_by_name (dynobj, ".rela.plt");
2577 htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
2578 if (!info->shared)
2579 htab->srelbss = bfd_get_section_by_name (dynobj, ".rela.bss");
2580
2581 if (!htab->splt || !htab->srelplt || !htab->sdynbss
2582 || (!info->shared && !htab->srelbss))
2583 abort ();
2584
2585 return true;
2586 }
2587
2588 /* Copy the extra info we tack onto an elf_link_hash_entry. */
2589
2590 static void
2591 ppc64_elf_copy_indirect_symbol (dir, ind)
2592 struct elf_link_hash_entry *dir, *ind;
2593 {
2594 struct ppc_link_hash_entry *edir, *eind;
2595
2596 edir = (struct ppc_link_hash_entry *) dir;
2597 eind = (struct ppc_link_hash_entry *) ind;
2598
2599 if (eind->dyn_relocs != NULL)
2600 {
2601 if (edir->dyn_relocs != NULL)
2602 {
2603 struct ppc_dyn_relocs **pp;
2604 struct ppc_dyn_relocs *p;
2605
2606 if (ind->root.type == bfd_link_hash_indirect)
2607 abort ();
2608
2609 /* Add reloc counts against the weak sym to the strong sym
2610 list. Merge any entries against the same section. */
2611 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
2612 {
2613 struct ppc_dyn_relocs *q;
2614
2615 for (q = edir->dyn_relocs; q != NULL; q = q->next)
2616 if (q->sec == p->sec)
2617 {
2618 q->pc_count += p->pc_count;
2619 q->count += p->count;
2620 *pp = p->next;
2621 break;
2622 }
2623 if (q == NULL)
2624 pp = &p->next;
2625 }
2626 *pp = edir->dyn_relocs;
2627 }
2628
2629 edir->dyn_relocs = eind->dyn_relocs;
2630 eind->dyn_relocs = NULL;
2631 }
2632
2633 edir->is_func |= eind->is_func;
2634 edir->is_func_descriptor |= eind->is_func_descriptor;
2635 edir->is_entry |= eind->is_entry;
2636
2637 _bfd_elf_link_hash_copy_indirect (dir, ind);
2638 }
2639
2640 /* Set a flag, used by ppc64_elf_gc_mark_hook, on the entry symbol and
2641 symbols undefined on the command-line. */
2642
2643 boolean
2644 ppc64_elf_mark_entry_syms (info)
2645 struct bfd_link_info *info;
2646 {
2647 struct ppc_link_hash_table *htab;
2648 struct bfd_sym_chain *sym;
2649
2650 htab = ppc_hash_table (info);
2651 for (sym = info->gc_sym_list; sym; sym = sym->next)
2652 {
2653 struct elf_link_hash_entry *h;
2654
2655 h = elf_link_hash_lookup (&htab->elf, sym->name, false, false, false);
2656 if (h != NULL)
2657 ((struct ppc_link_hash_entry *) h)->is_entry = 1;
2658 }
2659 return true;
2660 }
2661
2662 /* Look through the relocs for a section during the first phase, and
2663 calculate needed space in the global offset table, procedure
2664 linkage table, and dynamic reloc sections. */
2665
2666 static boolean
2667 ppc64_elf_check_relocs (abfd, info, sec, relocs)
2668 bfd *abfd;
2669 struct bfd_link_info *info;
2670 asection *sec;
2671 const Elf_Internal_Rela *relocs;
2672 {
2673 struct ppc_link_hash_table *htab;
2674 Elf_Internal_Shdr *symtab_hdr;
2675 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
2676 const Elf_Internal_Rela *rel;
2677 const Elf_Internal_Rela *rel_end;
2678 asection *sreloc;
2679 asection **opd_sym_map;
2680
2681 if (info->relocateable)
2682 return true;
2683
2684 htab = ppc_hash_table (info);
2685 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2686
2687 sym_hashes = elf_sym_hashes (abfd);
2688 sym_hashes_end = (sym_hashes
2689 + symtab_hdr->sh_size / sizeof (Elf64_External_Sym)
2690 - symtab_hdr->sh_info);
2691
2692 sreloc = NULL;
2693 opd_sym_map = NULL;
2694 if (strcmp (bfd_get_section_name (abfd, sec), ".opd") == 0)
2695 {
2696 /* Garbage collection needs some extra help with .opd sections.
2697 We don't want to necessarily keep everything referenced by
2698 relocs in .opd, as that would keep all functions. Instead,
2699 if we reference an .opd symbol (a function descriptor), we
2700 want to keep the function code symbol's section. This is
2701 easy for global symbols, but for local syms we need to keep
2702 information about the associated function section. Later, if
2703 edit_opd deletes entries, we'll use this array to adjust
2704 local syms in .opd. */
2705 union opd_info {
2706 asection *func_section;
2707 long entry_adjust;
2708 };
2709 bfd_size_type amt;
2710
2711 amt = sec->_raw_size * sizeof (union opd_info) / 24;
2712 opd_sym_map = (asection **) bfd_zalloc (abfd, amt);
2713 if (opd_sym_map == NULL)
2714 return false;
2715 elf_section_data (sec)->tdata = opd_sym_map;
2716 }
2717
2718 if (htab->elf.dynobj == NULL)
2719 htab->elf.dynobj = abfd;
2720 if (htab->sfpr == NULL
2721 && !create_linkage_sections (htab->elf.dynobj, info))
2722 return false;
2723
2724 rel_end = relocs + sec->reloc_count;
2725 for (rel = relocs; rel < rel_end; rel++)
2726 {
2727 unsigned long r_symndx;
2728 struct elf_link_hash_entry *h;
2729 enum elf_ppc_reloc_type r_type;
2730
2731 r_symndx = ELF64_R_SYM (rel->r_info);
2732 if (r_symndx < symtab_hdr->sh_info)
2733 h = NULL;
2734 else
2735 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2736
2737 r_type = (enum elf_ppc_reloc_type) ELF64_R_TYPE (rel->r_info);
2738 switch (r_type)
2739 {
2740 /* GOT16 relocations */
2741 case R_PPC64_GOT16:
2742 case R_PPC64_GOT16_DS:
2743 case R_PPC64_GOT16_HA:
2744 case R_PPC64_GOT16_HI:
2745 case R_PPC64_GOT16_LO:
2746 case R_PPC64_GOT16_LO_DS:
2747
2748 /* This symbol requires a global offset table entry. */
2749 if (htab->sgot == NULL
2750 && !create_got_section (htab->elf.dynobj, info))
2751 return false;
2752
2753 if (h != NULL)
2754 {
2755 h->got.refcount += 1;
2756 }
2757 else
2758 {
2759 bfd_signed_vma *local_got_refcounts;
2760
2761 /* This is a global offset table entry for a local symbol. */
2762 local_got_refcounts = elf_local_got_refcounts (abfd);
2763 if (local_got_refcounts == NULL)
2764 {
2765 bfd_size_type size;
2766
2767 size = symtab_hdr->sh_info;
2768 size *= sizeof (bfd_signed_vma);
2769 local_got_refcounts = ((bfd_signed_vma *)
2770 bfd_zalloc (abfd, size));
2771 if (local_got_refcounts == NULL)
2772 return false;
2773 elf_local_got_refcounts (abfd) = local_got_refcounts;
2774 }
2775 local_got_refcounts[r_symndx] += 1;
2776 }
2777 break;
2778
2779 case R_PPC64_PLT16_HA:
2780 case R_PPC64_PLT16_HI:
2781 case R_PPC64_PLT16_LO:
2782 case R_PPC64_PLT32:
2783 case R_PPC64_PLT64:
2784 /* This symbol requires a procedure linkage table entry. We
2785 actually build the entry in adjust_dynamic_symbol,
2786 because this might be a case of linking PIC code without
2787 linking in any dynamic objects, in which case we don't
2788 need to generate a procedure linkage table after all. */
2789 if (h == NULL)
2790 {
2791 /* It does not make sense to have a procedure linkage
2792 table entry for a local symbol. */
2793 bfd_set_error (bfd_error_bad_value);
2794 return false;
2795 }
2796
2797 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
2798 h->plt.refcount += 1;
2799 ((struct ppc_link_hash_entry *) h)->is_func = 1;
2800 break;
2801
2802 /* The following relocations don't need to propagate the
2803 relocation if linking a shared object since they are
2804 section relative. */
2805 case R_PPC64_SECTOFF:
2806 case R_PPC64_SECTOFF_LO:
2807 case R_PPC64_SECTOFF_HI:
2808 case R_PPC64_SECTOFF_HA:
2809 case R_PPC64_SECTOFF_DS:
2810 case R_PPC64_SECTOFF_LO_DS:
2811 case R_PPC64_TOC16:
2812 case R_PPC64_TOC16_LO:
2813 case R_PPC64_TOC16_HI:
2814 case R_PPC64_TOC16_HA:
2815 case R_PPC64_TOC16_DS:
2816 case R_PPC64_TOC16_LO_DS:
2817 break;
2818
2819 /* This relocation describes the C++ object vtable hierarchy.
2820 Reconstruct it for later use during GC. */
2821 case R_PPC64_GNU_VTINHERIT:
2822 if (!_bfd_elf64_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
2823 return false;
2824 break;
2825
2826 /* This relocation describes which C++ vtable entries are actually
2827 used. Record for later use during GC. */
2828 case R_PPC64_GNU_VTENTRY:
2829 if (!_bfd_elf64_gc_record_vtentry (abfd, sec, h, rel->r_addend))
2830 return false;
2831 break;
2832
2833 case R_PPC64_REL14:
2834 case R_PPC64_REL14_BRTAKEN:
2835 case R_PPC64_REL14_BRNTAKEN:
2836 htab->has_14bit_branch = 1;
2837 /* Fall through. */
2838
2839 case R_PPC64_REL24:
2840 if (h != NULL
2841 && h->root.root.string[0] == '.'
2842 && h->root.root.string[1] != 0)
2843 {
2844 /* We may need a .plt entry if the function this reloc
2845 refers to is in a shared lib. */
2846 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
2847 h->plt.refcount += 1;
2848 ((struct ppc_link_hash_entry *) h)->is_func = 1;
2849 }
2850 break;
2851
2852 case R_PPC64_ADDR64:
2853 if (opd_sym_map != NULL
2854 && h != NULL
2855 && h->root.root.string[0] == '.'
2856 && h->root.root.string[1] != 0)
2857 {
2858 struct elf_link_hash_entry *fdh;
2859
2860 fdh = elf_link_hash_lookup (&htab->elf, h->root.root.string + 1,
2861 false, false, false);
2862 if (fdh != NULL)
2863 {
2864 ((struct ppc_link_hash_entry *) fdh)->is_func_descriptor = 1;
2865 ((struct ppc_link_hash_entry *) fdh)->oh = h;
2866 ((struct ppc_link_hash_entry *) h)->is_func = 1;
2867 ((struct ppc_link_hash_entry *) h)->oh = fdh;
2868 }
2869 }
2870 if (opd_sym_map != NULL
2871 && h == NULL
2872 && rel + 1 < rel_end
2873 && ((enum elf_ppc_reloc_type) ELF64_R_TYPE ((rel + 1)->r_info)
2874 == R_PPC64_TOC))
2875 {
2876 asection *s;
2877
2878 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec, sec,
2879 r_symndx);
2880 if (s == NULL)
2881 return false;
2882 else if (s != sec)
2883 opd_sym_map[rel->r_offset / 24] = s;
2884 }
2885 /* Fall through. */
2886
2887 case R_PPC64_REL64:
2888 case R_PPC64_REL32:
2889 case R_PPC64_ADDR14:
2890 case R_PPC64_ADDR14_BRNTAKEN:
2891 case R_PPC64_ADDR14_BRTAKEN:
2892 case R_PPC64_ADDR16:
2893 case R_PPC64_ADDR16_DS:
2894 case R_PPC64_ADDR16_HA:
2895 case R_PPC64_ADDR16_HI:
2896 case R_PPC64_ADDR16_HIGHER:
2897 case R_PPC64_ADDR16_HIGHERA:
2898 case R_PPC64_ADDR16_HIGHEST:
2899 case R_PPC64_ADDR16_HIGHESTA:
2900 case R_PPC64_ADDR16_LO:
2901 case R_PPC64_ADDR16_LO_DS:
2902 case R_PPC64_ADDR24:
2903 case R_PPC64_ADDR30:
2904 case R_PPC64_ADDR32:
2905 case R_PPC64_UADDR16:
2906 case R_PPC64_UADDR32:
2907 case R_PPC64_UADDR64:
2908 case R_PPC64_TOC:
2909 /* Don't propagate .opd relocs. */
2910 if (NO_OPD_RELOCS && opd_sym_map != NULL)
2911 break;
2912
2913 /* If we are creating a shared library, and this is a reloc
2914 against a global symbol, or a non PC relative reloc
2915 against a local symbol, then we need to copy the reloc
2916 into the shared library. However, if we are linking with
2917 -Bsymbolic, we do not need to copy a reloc against a
2918 global symbol which is defined in an object we are
2919 including in the link (i.e., DEF_REGULAR is set). At
2920 this point we have not seen all the input files, so it is
2921 possible that DEF_REGULAR is not set now but will be set
2922 later (it is never cleared). In case of a weak definition,
2923 DEF_REGULAR may be cleared later by a strong definition in
2924 a shared library. We account for that possibility below by
2925 storing information in the relocs_copied field of the hash
2926 table entry. A similar situation occurs when creating
2927 shared libraries and symbol visibility changes render the
2928 symbol local.
2929
2930 If on the other hand, we are creating an executable, we
2931 may need to keep relocations for symbols satisfied by a
2932 dynamic library if we manage to avoid copy relocs for the
2933 symbol. */
2934 if ((info->shared
2935 && (sec->flags & SEC_ALLOC) != 0
2936 && (IS_ABSOLUTE_RELOC (r_type)
2937 || (h != NULL
2938 && (! info->symbolic
2939 || h->root.type == bfd_link_hash_defweak
2940 || (h->elf_link_hash_flags
2941 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
2942 || (!info->shared
2943 && (sec->flags & SEC_ALLOC) != 0
2944 && h != NULL
2945 && (h->root.type == bfd_link_hash_defweak
2946 || (h->elf_link_hash_flags
2947 & ELF_LINK_HASH_DEF_REGULAR) == 0)))
2948 {
2949 struct ppc_dyn_relocs *p;
2950 struct ppc_dyn_relocs **head;
2951
2952 /* We must copy these reloc types into the output file.
2953 Create a reloc section in dynobj and make room for
2954 this reloc. */
2955 if (sreloc == NULL)
2956 {
2957 const char *name;
2958 bfd *dynobj;
2959
2960 name = (bfd_elf_string_from_elf_section
2961 (abfd,
2962 elf_elfheader (abfd)->e_shstrndx,
2963 elf_section_data (sec)->rel_hdr.sh_name));
2964 if (name == NULL)
2965 return false;
2966
2967 if (strncmp (name, ".rela", 5) != 0
2968 || strcmp (bfd_get_section_name (abfd, sec),
2969 name + 5) != 0)
2970 {
2971 (*_bfd_error_handler)
2972 (_("%s: bad relocation section name `%s\'"),
2973 bfd_archive_filename (abfd), name);
2974 bfd_set_error (bfd_error_bad_value);
2975 }
2976
2977 dynobj = htab->elf.dynobj;
2978 sreloc = bfd_get_section_by_name (dynobj, name);
2979 if (sreloc == NULL)
2980 {
2981 flagword flags;
2982
2983 sreloc = bfd_make_section (dynobj, name);
2984 flags = (SEC_HAS_CONTENTS | SEC_READONLY
2985 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2986 if ((sec->flags & SEC_ALLOC) != 0)
2987 flags |= SEC_ALLOC | SEC_LOAD;
2988 if (sreloc == NULL
2989 || ! bfd_set_section_flags (dynobj, sreloc, flags)
2990 || ! bfd_set_section_alignment (dynobj, sreloc, 3))
2991 return false;
2992 }
2993 elf_section_data (sec)->sreloc = sreloc;
2994 }
2995
2996 /* If this is a global symbol, we count the number of
2997 relocations we need for this symbol. */
2998 if (h != NULL)
2999 {
3000 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
3001 }
3002 else
3003 {
3004 /* Track dynamic relocs needed for local syms too.
3005 We really need local syms available to do this
3006 easily. Oh well. */
3007
3008 asection *s;
3009 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
3010 sec, r_symndx);
3011 if (s == NULL)
3012 return false;
3013
3014 head = ((struct ppc_dyn_relocs **)
3015 &elf_section_data (s)->local_dynrel);
3016 }
3017
3018 p = *head;
3019 if (p == NULL || p->sec != sec)
3020 {
3021 p = ((struct ppc_dyn_relocs *)
3022 bfd_alloc (htab->elf.dynobj,
3023 (bfd_size_type) sizeof *p));
3024 if (p == NULL)
3025 return false;
3026 p->next = *head;
3027 *head = p;
3028 p->sec = sec;
3029 p->count = 0;
3030 p->pc_count = 0;
3031 }
3032
3033 p->count += 1;
3034 if (!IS_ABSOLUTE_RELOC (r_type))
3035 p->pc_count += 1;
3036 }
3037 break;
3038
3039 default:
3040 break;
3041 }
3042 }
3043
3044 return true;
3045 }
3046
3047 /* Return the section that should be marked against GC for a given
3048 relocation. */
3049
3050 static asection *
3051 ppc64_elf_gc_mark_hook (sec, info, rel, h, sym)
3052 asection *sec;
3053 struct bfd_link_info *info ATTRIBUTE_UNUSED;
3054 Elf_Internal_Rela *rel;
3055 struct elf_link_hash_entry *h;
3056 Elf_Internal_Sym *sym;
3057 {
3058 asection *rsec = NULL;
3059
3060 if (h != NULL)
3061 {
3062 enum elf_ppc_reloc_type r_type;
3063 struct ppc_link_hash_entry *fdh;
3064
3065 r_type = (enum elf_ppc_reloc_type) ELF64_R_TYPE (rel->r_info);
3066 switch (r_type)
3067 {
3068 case R_PPC64_GNU_VTINHERIT:
3069 case R_PPC64_GNU_VTENTRY:
3070 break;
3071
3072 default:
3073 switch (h->root.type)
3074 {
3075 case bfd_link_hash_defined:
3076 case bfd_link_hash_defweak:
3077 fdh = (struct ppc_link_hash_entry *) h;
3078
3079 /* Function descriptor syms cause the associated
3080 function code sym section to be marked. */
3081 if (fdh->is_func_descriptor)
3082 rsec = fdh->oh->root.u.def.section;
3083
3084 /* Function entry syms return NULL if they are in .opd
3085 and are not ._start (or others undefined on the ld
3086 command line). Thus we avoid marking all function
3087 sections, as all functions are referenced in .opd. */
3088 else if ((fdh->oh != NULL
3089 && ((struct ppc_link_hash_entry *) fdh->oh)->is_entry)
3090 || elf_section_data (sec)->tdata == NULL)
3091 rsec = h->root.u.def.section;
3092 break;
3093
3094 case bfd_link_hash_common:
3095 rsec = h->root.u.c.p->section;
3096 break;
3097
3098 default:
3099 break;
3100 }
3101 }
3102 }
3103 else
3104 {
3105 asection **opd_sym_section;
3106
3107 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
3108 opd_sym_section = (asection **) elf_section_data (rsec)->tdata;
3109 if (opd_sym_section != NULL)
3110 rsec = opd_sym_section[sym->st_value / 24];
3111 else if (elf_section_data (sec)->tdata != NULL)
3112 rsec = NULL;
3113 }
3114
3115 return rsec;
3116 }
3117
3118 /* Update the .got, .plt. and dynamic reloc reference counts for the
3119 section being removed. */
3120
3121 static boolean
3122 ppc64_elf_gc_sweep_hook (abfd, info, sec, relocs)
3123 bfd *abfd;
3124 struct bfd_link_info *info ATTRIBUTE_UNUSED;
3125 asection *sec;
3126 const Elf_Internal_Rela *relocs;
3127 {
3128 Elf_Internal_Shdr *symtab_hdr;
3129 struct elf_link_hash_entry **sym_hashes;
3130 bfd_signed_vma *local_got_refcounts;
3131 const Elf_Internal_Rela *rel, *relend;
3132
3133 elf_section_data (sec)->local_dynrel = NULL;
3134
3135 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3136 sym_hashes = elf_sym_hashes (abfd);
3137 local_got_refcounts = elf_local_got_refcounts (abfd);
3138
3139 relend = relocs + sec->reloc_count;
3140 for (rel = relocs; rel < relend; rel++)
3141 {
3142 unsigned long r_symndx;
3143 enum elf_ppc_reloc_type r_type;
3144 struct elf_link_hash_entry *h;
3145
3146 r_symndx = ELF64_R_SYM (rel->r_info);
3147 r_type = (enum elf_ppc_reloc_type) ELF64_R_TYPE (rel->r_info);
3148 switch (r_type)
3149 {
3150 case R_PPC64_GOT16:
3151 case R_PPC64_GOT16_DS:
3152 case R_PPC64_GOT16_HA:
3153 case R_PPC64_GOT16_HI:
3154 case R_PPC64_GOT16_LO:
3155 case R_PPC64_GOT16_LO_DS:
3156 if (r_symndx >= symtab_hdr->sh_info)
3157 {
3158 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3159 if (h->got.refcount > 0)
3160 h->got.refcount--;
3161 }
3162 else
3163 {
3164 if (local_got_refcounts[r_symndx] > 0)
3165 local_got_refcounts[r_symndx]--;
3166 }
3167 break;
3168
3169 case R_PPC64_PLT16_HA:
3170 case R_PPC64_PLT16_HI:
3171 case R_PPC64_PLT16_LO:
3172 case R_PPC64_PLT32:
3173 case R_PPC64_PLT64:
3174 if (r_symndx >= symtab_hdr->sh_info)
3175 {
3176 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3177 if (h->plt.refcount > 0)
3178 h->plt.refcount--;
3179 }
3180 break;
3181
3182 case R_PPC64_REL14:
3183 case R_PPC64_REL14_BRNTAKEN:
3184 case R_PPC64_REL14_BRTAKEN:
3185 case R_PPC64_REL24:
3186 if (r_symndx >= symtab_hdr->sh_info)
3187 {
3188 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3189 if (h->plt.refcount > 0)
3190 h->plt.refcount--;
3191 }
3192 break;
3193
3194 case R_PPC64_REL32:
3195 case R_PPC64_REL64:
3196 if (r_symndx >= symtab_hdr->sh_info)
3197 {
3198 struct ppc_link_hash_entry *eh;
3199 struct ppc_dyn_relocs **pp;
3200 struct ppc_dyn_relocs *p;
3201
3202 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3203 eh = (struct ppc_link_hash_entry *) h;
3204
3205 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
3206 if (p->sec == sec)
3207 {
3208 p->pc_count -= 1;
3209 p->count -= 1;
3210 if (p->count == 0)
3211 *pp = p->next;
3212 break;
3213 }
3214 }
3215 break;
3216
3217 case R_PPC64_ADDR14:
3218 case R_PPC64_ADDR14_BRNTAKEN:
3219 case R_PPC64_ADDR14_BRTAKEN:
3220 case R_PPC64_ADDR16:
3221 case R_PPC64_ADDR16_DS:
3222 case R_PPC64_ADDR16_HA:
3223 case R_PPC64_ADDR16_HI:
3224 case R_PPC64_ADDR16_HIGHER:
3225 case R_PPC64_ADDR16_HIGHERA:
3226 case R_PPC64_ADDR16_HIGHEST:
3227 case R_PPC64_ADDR16_HIGHESTA:
3228 case R_PPC64_ADDR16_LO:
3229 case R_PPC64_ADDR16_LO_DS:
3230 case R_PPC64_ADDR24:
3231 case R_PPC64_ADDR30:
3232 case R_PPC64_ADDR32:
3233 case R_PPC64_ADDR64:
3234 case R_PPC64_UADDR16:
3235 case R_PPC64_UADDR32:
3236 case R_PPC64_UADDR64:
3237 case R_PPC64_TOC:
3238 if (r_symndx >= symtab_hdr->sh_info)
3239 {
3240 struct ppc_link_hash_entry *eh;
3241 struct ppc_dyn_relocs **pp;
3242 struct ppc_dyn_relocs *p;
3243
3244 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3245 eh = (struct ppc_link_hash_entry *) h;
3246
3247 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
3248 if (p->sec == sec)
3249 {
3250 p->count -= 1;
3251 if (p->count == 0)
3252 *pp = p->next;
3253 break;
3254 }
3255 }
3256 break;
3257
3258 default:
3259 break;
3260 }
3261 }
3262 return true;
3263 }
3264
3265 /* Called via elf_link_hash_traverse to transfer dynamic linking
3266 information on function code symbol entries to their corresponding
3267 function descriptor symbol entries. */
3268 static boolean
3269 func_desc_adjust (h, inf)
3270 struct elf_link_hash_entry *h;
3271 PTR inf;
3272 {
3273 struct bfd_link_info *info;
3274 struct ppc_link_hash_table *htab;
3275
3276 if (h->root.type == bfd_link_hash_indirect)
3277 return true;
3278
3279 if (h->root.type == bfd_link_hash_warning)
3280 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3281
3282 info = (struct bfd_link_info *) inf;
3283 htab = ppc_hash_table (info);
3284
3285 /* If this is a function code symbol, transfer dynamic linking
3286 information to the function descriptor symbol. */
3287 if (!((struct ppc_link_hash_entry *) h)->is_func)
3288 return true;
3289
3290 if (h->root.type == bfd_link_hash_undefweak
3291 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR))
3292 htab->have_undefweak = true;
3293
3294 if (h->plt.refcount > 0
3295 && h->root.root.string[0] == '.'
3296 && h->root.root.string[1] != '\0')
3297 {
3298 struct elf_link_hash_entry *fdh = ((struct ppc_link_hash_entry *) h)->oh;
3299 boolean force_local;
3300
3301 /* Find the corresponding function descriptor symbol. Create it
3302 as undefined if necessary. */
3303
3304 if (fdh == NULL)
3305 fdh = elf_link_hash_lookup (&htab->elf, h->root.root.string + 1,
3306 false, false, true);
3307
3308 if (fdh == NULL
3309 && info->shared
3310 && (h->root.type == bfd_link_hash_undefined
3311 || h->root.type == bfd_link_hash_undefweak))
3312 {
3313 bfd *abfd;
3314 asymbol *newsym;
3315
3316 abfd = h->root.u.undef.abfd;
3317 newsym = bfd_make_empty_symbol (abfd);
3318 newsym->name = h->root.root.string + 1;
3319 newsym->section = bfd_und_section_ptr;
3320 newsym->value = 0;
3321 newsym->flags = BSF_OBJECT;
3322 if (h->root.type == bfd_link_hash_undefweak)
3323 newsym->flags |= BSF_WEAK;
3324
3325 if ( !(_bfd_generic_link_add_one_symbol
3326 (info, abfd, newsym->name, newsym->flags,
3327 newsym->section, newsym->value, NULL, false, false,
3328 (struct bfd_link_hash_entry **) &fdh)))
3329 {
3330 return false;
3331 }
3332 fdh->elf_link_hash_flags &= ~ELF_LINK_NON_ELF;
3333 }
3334
3335 if (fdh != NULL
3336 && (fdh->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0
3337 && (info->shared
3338 || (fdh->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
3339 || (fdh->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0))
3340 {
3341 if (fdh->dynindx == -1)
3342 if (! bfd_elf64_link_record_dynamic_symbol (info, fdh))
3343 return false;
3344 fdh->elf_link_hash_flags |= (h->elf_link_hash_flags
3345 & (ELF_LINK_HASH_REF_REGULAR
3346 | ELF_LINK_HASH_REF_DYNAMIC
3347 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
3348 | ELF_LINK_NON_GOT_REF));
3349 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3350 {
3351 fdh->plt.refcount = h->plt.refcount;
3352 fdh->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
3353 }
3354 ((struct ppc_link_hash_entry *) fdh)->is_func_descriptor = 1;
3355 ((struct ppc_link_hash_entry *) fdh)->oh = h;
3356 ((struct ppc_link_hash_entry *) h)->oh = fdh;
3357 }
3358
3359 /* Now that the info is on the function descriptor, clear the
3360 function code sym info. Any function code syms for which we
3361 don't have a definition in a regular file, we force local.
3362 This prevents a shared library from exporting syms that have
3363 been imported from another library. Function code syms that
3364 are really in the library we must leave global to prevent the
3365 linker dragging in a definition from a static library. */
3366 force_local = (info->shared
3367 && ((h->elf_link_hash_flags
3368 & ELF_LINK_HASH_DEF_REGULAR) == 0
3369 || fdh == NULL
3370 || (fdh->elf_link_hash_flags
3371 & ELF_LINK_HASH_DEF_REGULAR) == 0
3372 || (fdh->elf_link_hash_flags
3373 & ELF_LINK_FORCED_LOCAL) != 0));
3374 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
3375 }
3376
3377 return true;
3378 }
3379
3380 #define MIN_SAVE_FPR 14
3381 #define MAX_SAVE_FPR 31
3382
3383 /* Called near the start of bfd_elf_size_dynamic_sections. We use
3384 this hook to a) provide some gcc support functions, and b) transfer
3385 dynamic linking information gathered so far on function code symbol
3386 entries, to their corresponding function descriptor symbol entries. */
3387 static boolean
3388 ppc64_elf_func_desc_adjust (obfd, info)
3389 bfd *obfd ATTRIBUTE_UNUSED;
3390 struct bfd_link_info *info;
3391 {
3392 struct ppc_link_hash_table *htab;
3393 unsigned int lowest_savef = MAX_SAVE_FPR + 2;
3394 unsigned int lowest_restf = MAX_SAVE_FPR + 2;
3395 unsigned int i;
3396 struct elf_link_hash_entry *h;
3397 bfd_byte *p;
3398 char sym[10];
3399
3400 htab = ppc_hash_table (info);
3401
3402 if (htab->sfpr == NULL)
3403 /* We don't have any relocs. */
3404 return true;
3405
3406 /* First provide any missing ._savef* and ._restf* functions. */
3407 memcpy (sym, "._savef14", 10);
3408 for (i = MIN_SAVE_FPR; i <= MAX_SAVE_FPR; i++)
3409 {
3410 sym[7] = i / 10 + '0';
3411 sym[8] = i % 10 + '0';
3412 h = elf_link_hash_lookup (&htab->elf, sym, false, false, true);
3413 if (h != NULL
3414 && h->root.type == bfd_link_hash_undefined)
3415 {
3416 if (lowest_savef > i)
3417 lowest_savef = i;
3418 h->root.type = bfd_link_hash_defined;
3419 h->root.u.def.section = htab->sfpr;
3420 h->root.u.def.value = (i - lowest_savef) * 4;
3421 h->type = STT_FUNC;
3422 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3423 _bfd_elf_link_hash_hide_symbol (info, h, info->shared);
3424 }
3425 }
3426
3427 memcpy (sym, "._restf14", 10);
3428 for (i = MIN_SAVE_FPR; i <= MAX_SAVE_FPR; i++)
3429 {
3430 sym[7] = i / 10 + '0';
3431 sym[8] = i % 10 + '0';
3432 h = elf_link_hash_lookup (&htab->elf, sym, false, false, true);
3433 if (h != NULL
3434 && h->root.type == bfd_link_hash_undefined)
3435 {
3436 if (lowest_restf > i)
3437 lowest_restf = i;
3438 h->root.type = bfd_link_hash_defined;
3439 h->root.u.def.section = htab->sfpr;
3440 h->root.u.def.value = ((MAX_SAVE_FPR + 2 - lowest_savef) * 4
3441 + (i - lowest_restf) * 4);
3442 h->type = STT_FUNC;
3443 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3444 _bfd_elf_link_hash_hide_symbol (info, h, info->shared);
3445 }
3446 }
3447
3448 elf_link_hash_traverse (&htab->elf, func_desc_adjust, (PTR) info);
3449
3450 htab->sfpr->_raw_size = ((MAX_SAVE_FPR + 2 - lowest_savef) * 4
3451 + (MAX_SAVE_FPR + 2 - lowest_restf) * 4);
3452
3453 if (htab->sfpr->_raw_size == 0)
3454 {
3455 if (!htab->have_undefweak)
3456 {
3457 _bfd_strip_section_from_output (info, htab->sfpr);
3458 return true;
3459 }
3460
3461 htab->sfpr->_raw_size = 4;
3462 }
3463
3464 p = (bfd_byte *) bfd_alloc (htab->elf.dynobj, htab->sfpr->_raw_size);
3465 if (p == NULL)
3466 return false;
3467 htab->sfpr->contents = p;
3468
3469 for (i = lowest_savef; i <= MAX_SAVE_FPR; i++)
3470 {
3471 unsigned int fpr = i << 21;
3472 unsigned int stackoff = (1 << 16) - (MAX_SAVE_FPR + 1 - i) * 8;
3473 bfd_put_32 (htab->elf.dynobj, STFD_FR0_0R1 + fpr + stackoff, p);
3474 p += 4;
3475 }
3476 if (lowest_savef <= MAX_SAVE_FPR)
3477 {
3478 bfd_put_32 (htab->elf.dynobj, BLR, p);
3479 p += 4;
3480 }
3481
3482 for (i = lowest_restf; i <= MAX_SAVE_FPR; i++)
3483 {
3484 unsigned int fpr = i << 21;
3485 unsigned int stackoff = (1 << 16) - (MAX_SAVE_FPR + 1 - i) * 8;
3486 bfd_put_32 (htab->elf.dynobj, LFD_FR0_0R1 + fpr + stackoff, p);
3487 p += 4;
3488 }
3489 if (lowest_restf <= MAX_SAVE_FPR
3490 || htab->sfpr->_raw_size == 4)
3491 {
3492 bfd_put_32 (htab->elf.dynobj, BLR, p);
3493 }
3494
3495 return true;
3496 }
3497
3498 /* Adjust a symbol defined by a dynamic object and referenced by a
3499 regular object. The current definition is in some section of the
3500 dynamic object, but we're not including those sections. We have to
3501 change the definition to something the rest of the link can
3502 understand. */
3503
3504 static boolean
3505 ppc64_elf_adjust_dynamic_symbol (info, h)
3506 struct bfd_link_info *info;
3507 struct elf_link_hash_entry *h;
3508 {
3509 struct ppc_link_hash_table *htab;
3510 struct ppc_link_hash_entry * eh;
3511 struct ppc_dyn_relocs *p;
3512 asection *s;
3513 unsigned int power_of_two;
3514
3515 htab = ppc_hash_table (info);
3516
3517 /* Deal with function syms. */
3518 if (h->type == STT_FUNC
3519 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
3520 {
3521 /* Clear procedure linkage table information for any symbol that
3522 won't need a .plt entry. */
3523 if (!((struct ppc_link_hash_entry *) h)->is_func_descriptor
3524 || h->plt.refcount <= 0
3525 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0
3526 || (! info->shared
3527 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
3528 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0))
3529 {
3530 h->plt.offset = (bfd_vma) -1;
3531 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
3532 }
3533 return true;
3534 }
3535 else
3536 h->plt.offset = (bfd_vma) -1;
3537
3538 /* If this is a weak symbol, and there is a real definition, the
3539 processor independent code will have arranged for us to see the
3540 real definition first, and we can just use the same value. */
3541 if (h->weakdef != NULL)
3542 {
3543 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
3544 || h->weakdef->root.type == bfd_link_hash_defweak);
3545 h->root.u.def.section = h->weakdef->root.u.def.section;
3546 h->root.u.def.value = h->weakdef->root.u.def.value;
3547 return true;
3548 }
3549
3550 /* This is a reference to a symbol defined by a dynamic object which
3551 is not a function. */
3552
3553 /* If we are creating a shared library, we must presume that the
3554 only references to the symbol are via the global offset table.
3555 For such cases we need not do anything here; the relocations will
3556 be handled correctly by relocate_section. */
3557 if (info->shared)
3558 return true;
3559
3560 /* If there are no references to this symbol that do not use the
3561 GOT, we don't need to generate a copy reloc. */
3562 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
3563 return true;
3564
3565 eh = (struct ppc_link_hash_entry *) h;
3566 for (p = eh->dyn_relocs; p != NULL; p = p->next)
3567 {
3568 s = p->sec->output_section;
3569 if (s != NULL && (s->flags & SEC_READONLY) != 0)
3570 break;
3571 }
3572
3573 /* If we didn't find any dynamic relocs in read-only sections, then
3574 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
3575 if (p == NULL)
3576 {
3577 h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
3578 return true;
3579 }
3580
3581 /* We must allocate the symbol in our .dynbss section, which will
3582 become part of the .bss section of the executable. There will be
3583 an entry for this symbol in the .dynsym section. The dynamic
3584 object will contain position independent code, so all references
3585 from the dynamic object to this symbol will go through the global
3586 offset table. The dynamic linker will use the .dynsym entry to
3587 determine the address it must put in the global offset table, so
3588 both the dynamic object and the regular object will refer to the
3589 same memory location for the variable. */
3590
3591 /* We must generate a R_PPC_COPY reloc to tell the dynamic linker to
3592 copy the initial value out of the dynamic object and into the
3593 runtime process image. We need to remember the offset into the
3594 .rela.bss section we are going to use. */
3595 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
3596 {
3597 htab->srelbss->_raw_size += sizeof (Elf64_External_Rela);
3598 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
3599 }
3600
3601 /* We need to figure out the alignment required for this symbol. I
3602 have no idea how ELF linkers handle this. */
3603 power_of_two = bfd_log2 (h->size);
3604 if (power_of_two > 4)
3605 power_of_two = 4;
3606
3607 /* Apply the required alignment. */
3608 s = htab->sdynbss;
3609 s->_raw_size = BFD_ALIGN (s->_raw_size, (bfd_size_type) (1 << power_of_two));
3610 if (power_of_two > bfd_get_section_alignment (htab->elf.dynobj, s))
3611 {
3612 if (! bfd_set_section_alignment (htab->elf.dynobj, s, power_of_two))
3613 return false;
3614 }
3615
3616 /* Define the symbol as being at this point in the section. */
3617 h->root.u.def.section = s;
3618 h->root.u.def.value = s->_raw_size;
3619
3620 /* Increment the section size to make room for the symbol. */
3621 s->_raw_size += h->size;
3622
3623 return true;
3624 }
3625
3626 /* If given a function descriptor symbol, hide both the function code
3627 sym and the descriptor. */
3628 static void
3629 ppc64_elf_hide_symbol (info, h, force_local)
3630 struct bfd_link_info *info;
3631 struct elf_link_hash_entry *h;
3632 boolean force_local;
3633 {
3634 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
3635
3636 if (((struct ppc_link_hash_entry *) h)->is_func_descriptor)
3637 {
3638 struct elf_link_hash_entry *fh = ((struct ppc_link_hash_entry *) h)->oh;
3639
3640 if (fh == NULL)
3641 {
3642 const char *p, *q;
3643 struct ppc_link_hash_table *htab;
3644 char save;
3645
3646 /* We aren't supposed to use alloca in BFD because on
3647 systems which do not have alloca the version in libiberty
3648 calls xmalloc, which might cause the program to crash
3649 when it runs out of memory. This function doesn't have a
3650 return status, so there's no way to gracefully return an
3651 error. So cheat. We know that string[-1] can be safely
3652 dereferenced; It's either a string in an ELF string
3653 table, or allocated in an objalloc structure. */
3654
3655 p = h->root.root.string - 1;
3656 save = *p;
3657 *(char *) p = '.';
3658 htab = ppc_hash_table (info);
3659 fh = elf_link_hash_lookup (&htab->elf, p, false, false, false);
3660 *(char *) p = save;
3661
3662 /* Unfortunately, if it so happens that the string we were
3663 looking for was allocated immediately before this string,
3664 then we overwrote the string terminator. That's the only
3665 reason the lookup should fail. */
3666 if (fh == NULL)
3667 {
3668 q = h->root.root.string + strlen (h->root.root.string);
3669 while (q >= h->root.root.string && *q == *p)
3670 --q, --p;
3671 if (q < h->root.root.string && *p == '.')
3672 fh = elf_link_hash_lookup (&htab->elf, p, false, false, false);
3673 }
3674 if (fh != NULL)
3675 {
3676 ((struct ppc_link_hash_entry *) h)->oh = fh;
3677 ((struct ppc_link_hash_entry *) fh)->oh = h;
3678 }
3679 }
3680 if (fh != NULL)
3681 _bfd_elf_link_hash_hide_symbol (info, fh, force_local);
3682 }
3683 }
3684
3685 static boolean
3686 edit_opd (obfd, info)
3687 bfd *obfd;
3688 struct bfd_link_info *info;
3689 {
3690 bfd *ibfd;
3691 unsigned int bfd_indx;
3692 struct ppc_link_hash_table *htab = ppc_hash_table (info);
3693
3694 for (bfd_indx = 0, ibfd = info->input_bfds;
3695 ibfd != NULL;
3696 ibfd = ibfd->link_next, bfd_indx++)
3697 {
3698 asection *sec;
3699 Elf_Internal_Rela *relstart, *rel, *relend;
3700 Elf_Internal_Shdr *symtab_hdr;
3701 struct elf_link_hash_entry **sym_hashes;
3702 bfd_vma offset;
3703 long *adjust;
3704 boolean need_edit;
3705
3706 sec = bfd_get_section_by_name (ibfd, ".opd");
3707 if (sec == NULL)
3708 continue;
3709
3710 adjust = (long *) elf_section_data (sec)->tdata;
3711 BFD_ASSERT (adjust != NULL);
3712 memset (adjust, 0, (size_t) sec->_raw_size * sizeof (long) / 24);
3713
3714 if (sec->output_section == bfd_abs_section_ptr)
3715 continue;
3716
3717 /* Look through the section relocs. */
3718 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
3719 continue;
3720
3721 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
3722 sym_hashes = elf_sym_hashes (ibfd);
3723
3724 /* Read the relocations. */
3725 relstart = _bfd_elf64_link_read_relocs (obfd, sec, (PTR) NULL,
3726 (Elf_Internal_Rela *) NULL,
3727 info->keep_memory);
3728 if (relstart == NULL)
3729 return false;
3730
3731 /* First run through the relocs to check they are sane, and to
3732 determine whether we need to edit this opd section. */
3733 need_edit = false;
3734 offset = 0;
3735 relend = relstart + sec->reloc_count;
3736 for (rel = relstart; rel < relend; rel++)
3737 {
3738 enum elf_ppc_reloc_type r_type;
3739 unsigned long r_symndx;
3740 asection *sym_sec;
3741 struct elf_link_hash_entry *h;
3742 Elf_Internal_Sym *sym;
3743
3744 /* .opd contains a regular array of 24 byte entries. We're
3745 only interested in the reloc pointing to a function entry
3746 point. */
3747 r_type = (enum elf_ppc_reloc_type) ELF64_R_TYPE (rel->r_info);
3748 if (r_type == R_PPC64_TOC)
3749 continue;
3750
3751 if (r_type != R_PPC64_ADDR64)
3752 {
3753 (*_bfd_error_handler)
3754 (_("%s: unexpected reloc type %u in .opd section"),
3755 bfd_archive_filename (ibfd), r_type);
3756 need_edit = false;
3757 break;
3758 }
3759
3760 if (rel + 1 >= relend)
3761 continue;
3762 r_type = (enum elf_ppc_reloc_type) ELF64_R_TYPE ((rel + 1)->r_info);
3763 if (r_type != R_PPC64_TOC)
3764 continue;
3765
3766 if (rel->r_offset != offset)
3767 {
3768 /* If someone messes with .opd alignment then after a
3769 "ld -r" we might have padding in the middle of .opd.
3770 Also, there's nothing to prevent someone putting
3771 something silly in .opd with the assembler. No .opd
3772 optimization for them! */
3773 (*_bfd_error_handler)
3774 (_("%s: .opd is not a regular array of opd entries"),
3775 bfd_archive_filename (ibfd));
3776 need_edit = false;
3777 break;
3778 }
3779
3780 r_symndx = ELF64_R_SYM (rel->r_info);
3781 sym_sec = NULL;
3782 h = NULL;
3783 sym = NULL;
3784 if (r_symndx >= symtab_hdr->sh_info)
3785 {
3786 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3787 while (h->root.type == bfd_link_hash_indirect
3788 || h->root.type == bfd_link_hash_warning)
3789 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3790 if (h->root.type == bfd_link_hash_defined
3791 || h->root.type == bfd_link_hash_defweak)
3792 sym_sec = h->root.u.def.section;
3793 }
3794 else
3795 {
3796 sym = htab->all_local_syms[bfd_indx] + r_symndx;
3797 if ((sym->st_shndx != SHN_UNDEF
3798 && sym->st_shndx < SHN_LORESERVE)
3799 || sym->st_shndx > SHN_HIRESERVE)
3800 sym_sec = bfd_section_from_elf_index (ibfd, sym->st_shndx);
3801 }
3802
3803 if (sym_sec == NULL || sym_sec->owner == NULL)
3804 {
3805 (*_bfd_error_handler)
3806 (_("%s: undefined sym `%s' in .opd section"),
3807 bfd_archive_filename (ibfd),
3808 h != NULL ? h->root.root.string : "<local symbol>");
3809 need_edit = false;
3810 break;
3811 }
3812
3813 if (sym_sec->output_section == bfd_abs_section_ptr)
3814 {
3815 /* OK, we've found a function that's excluded from the
3816 link. */
3817 need_edit = true;
3818 }
3819
3820 offset += 24;
3821 }
3822
3823 if (need_edit)
3824 {
3825 Elf_Internal_Rela *write_rel;
3826 bfd_byte *rptr, *wptr;
3827 boolean skip;
3828
3829 /* This seems a waste of time as input .opd sections are all
3830 zeros as generated by gcc, but I suppose there's no reason
3831 this will always be so. We might start putting something in
3832 the third word of .opd entries. */
3833 if ((sec->flags & SEC_IN_MEMORY) == 0)
3834 {
3835 bfd_byte *loc = bfd_alloc (ibfd, sec->_raw_size);
3836 if (loc == NULL)
3837 return false;
3838 if (! bfd_get_section_contents (ibfd, sec, loc, (bfd_vma) 0,
3839 sec->_raw_size))
3840 return false;
3841 sec->contents = loc;
3842 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
3843 }
3844
3845 elf_section_data (sec)->relocs = relstart;
3846
3847 wptr = sec->contents;
3848 rptr = sec->contents;
3849 write_rel = relstart;
3850 skip = false;
3851 offset = 0;
3852 for (rel = relstart; rel < relend; rel++)
3853 {
3854 if (rel->r_offset == offset)
3855 {
3856 unsigned long r_symndx;
3857 asection *sym_sec;
3858 struct elf_link_hash_entry *h;
3859 Elf_Internal_Sym *sym;
3860
3861 r_symndx = ELF64_R_SYM (rel->r_info);
3862 sym_sec = NULL;
3863 h = NULL;
3864 sym = NULL;
3865 if (r_symndx >= symtab_hdr->sh_info)
3866 {
3867 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3868 while (h->root.type == bfd_link_hash_indirect
3869 || h->root.type == bfd_link_hash_warning)
3870 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3871 if (h->root.type == bfd_link_hash_defined
3872 || h->root.type == bfd_link_hash_defweak)
3873 sym_sec = h->root.u.def.section;
3874 }
3875 else
3876 {
3877 sym = htab->all_local_syms[bfd_indx] + r_symndx;
3878 if ((sym->st_shndx != SHN_UNDEF
3879 && sym->st_shndx < SHN_LORESERVE)
3880 || sym->st_shndx > SHN_HIRESERVE)
3881 sym_sec = bfd_section_from_elf_index (ibfd,
3882 sym->st_shndx);
3883 }
3884
3885 skip = sym_sec->output_section == bfd_abs_section_ptr;
3886 if (!skip)
3887 {
3888 /* We'll be keeping this opd entry. */
3889
3890 if (h != NULL)
3891 {
3892 /* Redefine the function descriptor symbol
3893 to this location in the opd section.
3894 We've checked above that opd relocs are
3895 ordered. */
3896 struct elf_link_hash_entry *fdh;
3897 struct ppc_link_hash_entry *fh;
3898
3899 fh = (struct ppc_link_hash_entry *) h;
3900 BFD_ASSERT (fh->is_func);
3901 fdh = fh->oh;
3902 fdh->root.u.def.value = wptr - sec->contents;
3903 }
3904 else
3905 {
3906 /* Local syms are a bit tricky. Other parts
3907 of the linker re-read them so it's not
3908 possible to tweak local sym values. In
3909 any case, we'd need to look through the
3910 local syms for the function descriptor
3911 sym which we don't have at the moment.
3912 So keep an array of adjustments. */
3913 adjust[(rel->r_offset + wptr - rptr) / 24]
3914 = wptr - rptr;
3915 }
3916
3917 if (wptr != rptr)
3918 memcpy (wptr, rptr, 24);
3919 wptr += 24;
3920 }
3921 rptr += 24;
3922 offset += 24;
3923 }
3924
3925 /* We need to adjust any reloc offsets to point to the
3926 new opd entries. While we're at it, we may as well
3927 remove redundant relocs. */
3928 if (!skip)
3929 {
3930 rel->r_offset += wptr - rptr;
3931 if (write_rel != rel)
3932 memcpy (write_rel, rel, sizeof (*rel));
3933 ++write_rel;
3934 }
3935 }
3936
3937 sec->_cooked_size = wptr - sec->contents;
3938 sec->reloc_count = write_rel - relstart;
3939 }
3940 else if (elf_section_data (sec)->relocs == NULL)
3941 free (relstart);
3942 }
3943
3944 return true;
3945 }
3946
3947 /* This is the condition under which ppc64_elf_finish_dynamic_symbol
3948 will be called from elflink.h. If elflink.h doesn't call our
3949 finish_dynamic_symbol routine, we'll need to do something about
3950 initializing any .plt and .got entries in ppc64_elf_relocate_section. */
3951 #define WILL_CALL_FINISH_DYNAMIC_SYMBOL(DYN, INFO, H) \
3952 ((DYN) \
3953 && ((INFO)->shared \
3954 || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0) \
3955 && ((H)->dynindx != -1 \
3956 || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0))
3957
3958 /* Allocate space in .plt, .got and associated reloc sections for
3959 dynamic relocs. */
3960
3961 static boolean
3962 allocate_dynrelocs (h, inf)
3963 struct elf_link_hash_entry *h;
3964 PTR inf;
3965 {
3966 struct bfd_link_info *info;
3967 struct ppc_link_hash_table *htab;
3968 asection *s;
3969 struct ppc_link_hash_entry *eh;
3970 struct ppc_dyn_relocs *p;
3971
3972 if (h->root.type == bfd_link_hash_indirect)
3973 return true;
3974
3975 if (h->root.type == bfd_link_hash_warning)
3976 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3977
3978 info = (struct bfd_link_info *) inf;
3979 htab = ppc_hash_table (info);
3980
3981 if (htab->elf.dynamic_sections_created
3982 && h->plt.refcount > 0
3983 && h->dynindx != -1)
3984 {
3985 BFD_ASSERT (((struct ppc_link_hash_entry *) h)->is_func_descriptor);
3986
3987 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info, h))
3988 {
3989 /* If this is the first .plt entry, make room for the special
3990 first entry. */
3991 s = htab->splt;
3992 if (s->_raw_size == 0)
3993 s->_raw_size += PLT_INITIAL_ENTRY_SIZE;
3994
3995 h->plt.offset = s->_raw_size;
3996
3997 /* Make room for this entry. */
3998 s->_raw_size += PLT_ENTRY_SIZE;
3999
4000 /* Make room for the .glink code. */
4001 s = htab->sglink;
4002 if (s->_raw_size == 0)
4003 s->_raw_size += GLINK_CALL_STUB_SIZE;
4004 /* We need bigger stubs past index 32767. */
4005 if (s->_raw_size >= GLINK_CALL_STUB_SIZE + 32768*2*4)
4006 s->_raw_size += 4;
4007 s->_raw_size += 2*4;
4008
4009 /* We also need to make an entry in the .rela.plt section. */
4010 s = htab->srelplt;
4011 s->_raw_size += sizeof (Elf64_External_Rela);
4012 }
4013 else
4014 {
4015 h->plt.offset = (bfd_vma) -1;
4016 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
4017 }
4018 }
4019 else
4020 {
4021 h->plt.offset = (bfd_vma) -1;
4022 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
4023 }
4024
4025 if (h->got.refcount > 0)
4026 {
4027 boolean dyn;
4028
4029 /* Make sure this symbol is output as a dynamic symbol.
4030 Undefined weak syms won't yet be marked as dynamic. */
4031 if (h->dynindx == -1
4032 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
4033 {
4034 if (! bfd_elf64_link_record_dynamic_symbol (info, h))
4035 return false;
4036 }
4037
4038 s = htab->sgot;
4039 h->got.offset = s->_raw_size;
4040 s->_raw_size += 8;
4041 dyn = htab->elf.dynamic_sections_created;
4042 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info, h))
4043 htab->srelgot->_raw_size += sizeof (Elf64_External_Rela);
4044 }
4045 else
4046 h->got.offset = (bfd_vma) -1;
4047
4048 eh = (struct ppc_link_hash_entry *) h;
4049 if (eh->dyn_relocs == NULL)
4050 return true;
4051
4052 /* In the shared -Bsymbolic case, discard space allocated for
4053 dynamic pc-relative relocs against symbols which turn out to be
4054 defined in regular objects. For the normal shared case, discard
4055 space for relocs that have become local due to symbol visibility
4056 changes. */
4057
4058 if (info->shared)
4059 {
4060 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
4061 && ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0
4062 || info->symbolic))
4063 {
4064 struct ppc_dyn_relocs **pp;
4065
4066 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
4067 {
4068 p->count -= p->pc_count;
4069 p->pc_count = 0;
4070 if (p->count == 0)
4071 *pp = p->next;
4072 else
4073 pp = &p->next;
4074 }
4075 }
4076 }
4077 else
4078 {
4079 /* For the non-shared case, discard space for relocs against
4080 symbols which turn out to need copy relocs or are not
4081 dynamic. */
4082
4083 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
4084 && (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
4085 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
4086 || (htab->elf.dynamic_sections_created
4087 && (h->root.type == bfd_link_hash_undefweak
4088 || h->root.type == bfd_link_hash_undefined))))
4089 {
4090 /* Make sure this symbol is output as a dynamic symbol.
4091 Undefined weak syms won't yet be marked as dynamic. */
4092 if (h->dynindx == -1
4093 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
4094 {
4095 if (! bfd_elf64_link_record_dynamic_symbol (info, h))
4096 return false;
4097 }
4098
4099 /* If that succeeded, we know we'll be keeping all the
4100 relocs. */
4101 if (h->dynindx != -1)
4102 goto keep;
4103 }
4104
4105 eh->dyn_relocs = NULL;
4106
4107 keep: ;
4108 }
4109
4110 /* Finally, allocate space. */
4111 for (p = eh->dyn_relocs; p != NULL; p = p->next)
4112 {
4113 asection *sreloc = elf_section_data (p->sec)->sreloc;
4114 sreloc->_raw_size += p->count * sizeof (Elf64_External_Rela);
4115 }
4116
4117 return true;
4118 }
4119
4120 /* Find any dynamic relocs that apply to read-only sections. */
4121
4122 static boolean
4123 readonly_dynrelocs (h, inf)
4124 struct elf_link_hash_entry *h;
4125 PTR inf;
4126 {
4127 struct ppc_link_hash_entry *eh;
4128 struct ppc_dyn_relocs *p;
4129
4130 if (h->root.type == bfd_link_hash_warning)
4131 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4132
4133 eh = (struct ppc_link_hash_entry *) h;
4134 for (p = eh->dyn_relocs; p != NULL; p = p->next)
4135 {
4136 asection *s = p->sec->output_section;
4137
4138 if (s != NULL && (s->flags & SEC_READONLY) != 0)
4139 {
4140 struct bfd_link_info *info = (struct bfd_link_info *) inf;
4141
4142 info->flags |= DF_TEXTREL;
4143
4144 /* Not an error, just cut short the traversal. */
4145 return false;
4146 }
4147 }
4148 return true;
4149 }
4150
4151 /* Set the sizes of the dynamic sections. */
4152
4153 static boolean
4154 ppc64_elf_size_dynamic_sections (output_bfd, info)
4155 bfd *output_bfd ATTRIBUTE_UNUSED;
4156 struct bfd_link_info *info;
4157 {
4158 struct ppc_link_hash_table *htab;
4159 bfd *dynobj;
4160 asection *s;
4161 boolean relocs;
4162 bfd *ibfd;
4163
4164 htab = ppc_hash_table (info);
4165 dynobj = htab->elf.dynobj;
4166 if (dynobj == NULL)
4167 abort ();
4168
4169 if (htab->elf.dynamic_sections_created)
4170 {
4171 /* Set the contents of the .interp section to the interpreter. */
4172 if (! info->shared)
4173 {
4174 s = bfd_get_section_by_name (dynobj, ".interp");
4175 if (s == NULL)
4176 abort ();
4177 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
4178 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
4179 }
4180 }
4181
4182 /* Set up .got offsets for local syms, and space for local dynamic
4183 relocs. */
4184 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
4185 {
4186 bfd_signed_vma *local_got;
4187 bfd_signed_vma *end_local_got;
4188 bfd_size_type locsymcount;
4189 Elf_Internal_Shdr *symtab_hdr;
4190 asection *srel;
4191
4192 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
4193 continue;
4194
4195 for (s = ibfd->sections; s != NULL; s = s->next)
4196 {
4197 struct ppc_dyn_relocs *p;
4198
4199 for (p = *((struct ppc_dyn_relocs **)
4200 &elf_section_data (s)->local_dynrel);
4201 p != NULL;
4202 p = p->next)
4203 {
4204 if (!bfd_is_abs_section (p->sec)
4205 && bfd_is_abs_section (p->sec->output_section))
4206 {
4207 /* Input section has been discarded, either because
4208 it is a copy of a linkonce section or due to
4209 linker script /DISCARD/, so we'll be discarding
4210 the relocs too. */
4211 }
4212 else if (p->count != 0)
4213 {
4214 srel = elf_section_data (p->sec)->sreloc;
4215 srel->_raw_size += p->count * sizeof (Elf64_External_Rela);
4216 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
4217 info->flags |= DF_TEXTREL;
4218 }
4219 }
4220 }
4221
4222 local_got = elf_local_got_refcounts (ibfd);
4223 if (!local_got)
4224 continue;
4225
4226 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
4227 locsymcount = symtab_hdr->sh_info;
4228 end_local_got = local_got + locsymcount;
4229 s = htab->sgot;
4230 srel = htab->srelgot;
4231 for (; local_got < end_local_got; ++local_got)
4232 {
4233 if (*local_got > 0)
4234 {
4235 *local_got = s->_raw_size;
4236 s->_raw_size += 8;
4237 if (info->shared)
4238 srel->_raw_size += sizeof (Elf64_External_Rela);
4239 }
4240 else
4241 *local_got = (bfd_vma) -1;
4242 }
4243 }
4244
4245 if (!get_local_syms (info->input_bfds, htab))
4246 return false;
4247
4248 if (!edit_opd (output_bfd, info))
4249 return false;
4250
4251 /* Allocate global sym .plt and .got entries, and space for global
4252 sym dynamic relocs. */
4253 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, (PTR) info);
4254
4255 /* We now have determined the sizes of the various dynamic sections.
4256 Allocate memory for them. */
4257 relocs = false;
4258 for (s = dynobj->sections; s != NULL; s = s->next)
4259 {
4260 if ((s->flags & SEC_LINKER_CREATED) == 0)
4261 continue;
4262
4263 if (s == htab->sbrlt || s == htab->srelbrlt)
4264 /* These haven't been allocated yet; don't strip. */
4265 continue;
4266 else if (s == htab->splt
4267 || s == htab->sgot
4268 || s == htab->sglink)
4269 {
4270 /* Strip this section if we don't need it; see the
4271 comment below. */
4272 }
4273 else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
4274 {
4275 if (s->_raw_size == 0)
4276 {
4277 /* If we don't need this section, strip it from the
4278 output file. This is mostly to handle .rela.bss and
4279 .rela.plt. We must create both sections in
4280 create_dynamic_sections, because they must be created
4281 before the linker maps input sections to output
4282 sections. The linker does that before
4283 adjust_dynamic_symbol is called, and it is that
4284 function which decides whether anything needs to go
4285 into these sections. */
4286 }
4287 else
4288 {
4289 if (s != htab->srelplt)
4290 relocs = true;
4291
4292 /* We use the reloc_count field as a counter if we need
4293 to copy relocs into the output file. */
4294 s->reloc_count = 0;
4295 }
4296 }
4297 else
4298 {
4299 /* It's not one of our sections, so don't allocate space. */
4300 continue;
4301 }
4302
4303 if (s->_raw_size == 0)
4304 {
4305 _bfd_strip_section_from_output (info, s);
4306 continue;
4307 }
4308
4309 /* .plt is in the bss section. We don't initialise it. */
4310 if ((s->flags & SEC_LOAD) == 0)
4311 continue;
4312
4313 /* Allocate memory for the section contents. We use bfd_zalloc
4314 here in case unused entries are not reclaimed before the
4315 section's contents are written out. This should not happen,
4316 but this way if it does, we get a R_PPC64_NONE reloc instead
4317 of garbage. */
4318 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
4319 if (s->contents == NULL)
4320 return false;
4321 }
4322
4323 if (htab->elf.dynamic_sections_created)
4324 {
4325 /* Add some entries to the .dynamic section. We fill in the
4326 values later, in ppc64_elf_finish_dynamic_sections, but we
4327 must add the entries now so that we get the correct size for
4328 the .dynamic section. The DT_DEBUG entry is filled in by the
4329 dynamic linker and used by the debugger. */
4330 #define add_dynamic_entry(TAG, VAL) \
4331 bfd_elf64_add_dynamic_entry (info, (bfd_vma) (TAG), (bfd_vma) (VAL))
4332
4333 if (!info->shared)
4334 {
4335 if (!add_dynamic_entry (DT_DEBUG, 0))
4336 return false;
4337 }
4338
4339 if (htab->splt != NULL && htab->splt->_raw_size != 0)
4340 {
4341 if (!add_dynamic_entry (DT_PLTGOT, 0)
4342 || !add_dynamic_entry (DT_PLTRELSZ, 0)
4343 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
4344 || !add_dynamic_entry (DT_JMPREL, 0)
4345 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
4346 return false;
4347 }
4348
4349 if (NO_OPD_RELOCS)
4350 {
4351 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
4352 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
4353 return false;
4354 }
4355
4356 if (relocs)
4357 {
4358 if (!add_dynamic_entry (DT_RELA, 0)
4359 || !add_dynamic_entry (DT_RELASZ, 0)
4360 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
4361 return false;
4362
4363 /* If any dynamic relocs apply to a read-only section,
4364 then we need a DT_TEXTREL entry. */
4365 if ((info->flags & DF_TEXTREL) == 0)
4366 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs,
4367 (PTR) info);
4368
4369 if ((info->flags & DF_TEXTREL) != 0)
4370 {
4371 if (!add_dynamic_entry (DT_TEXTREL, 0))
4372 return false;
4373 }
4374 }
4375 }
4376 #undef add_dynamic_entry
4377
4378 return true;
4379 }
4380
4381 /* Determine the type of stub needed, if any, for a call. */
4382
4383 static INLINE enum ppc_stub_type
4384 ppc_type_of_stub (input_sec, rel, hash, destination)
4385 asection *input_sec;
4386 const Elf_Internal_Rela *rel;
4387 struct ppc_link_hash_entry **hash;
4388 bfd_vma destination;
4389 {
4390 struct ppc_link_hash_entry *h = *hash;
4391 bfd_vma location;
4392 bfd_vma branch_offset;
4393 bfd_vma max_branch_offset;
4394 unsigned int r_type;
4395
4396 if (h != NULL)
4397 {
4398 if (h->oh != NULL
4399 && h->oh->plt.offset != (bfd_vma) -1
4400 && h->oh->dynindx != -1)
4401 {
4402 *hash = (struct ppc_link_hash_entry *) h->oh;
4403 return ppc_stub_plt_call;
4404 }
4405
4406 if (h->elf.root.type == bfd_link_hash_undefweak
4407 || h->elf.root.type == bfd_link_hash_undefined)
4408 return ppc_stub_none;
4409 }
4410
4411 /* Determine where the call point is. */
4412 location = (input_sec->output_offset
4413 + input_sec->output_section->vma
4414 + rel->r_offset);
4415
4416 branch_offset = destination - location;
4417 r_type = ELF64_R_TYPE (rel->r_info);
4418
4419 /* Determine if a long branch stub is needed. */
4420 max_branch_offset = 1 << 25;
4421 if (r_type != (unsigned int) R_PPC64_REL24)
4422 max_branch_offset = 1 << 15;
4423
4424 if (branch_offset + max_branch_offset >= 2 * max_branch_offset)
4425 /* We need a stub. Figure out whether a long_branch or plt_branch
4426 is needed later. */
4427 return ppc_stub_long_branch;
4428
4429 return ppc_stub_none;
4430 }
4431
4432 /* Build a .plt call stub. */
4433
4434 static bfd_byte *
4435 build_plt_stub (obfd, p, offset, glink)
4436 bfd *obfd;
4437 bfd_byte *p;
4438 int offset;
4439 int glink;
4440 {
4441 #define PPC_LO(v) ((v) & 0xffff)
4442 #define PPC_HI(v) (((v) >> 16) & 0xffff)
4443 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
4444
4445 if (glink)
4446 bfd_put_32 (obfd, LD_R2_40R1, p), p += 4;
4447 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
4448 if (!glink)
4449 bfd_put_32 (obfd, STD_R2_40R1, p), p += 4;
4450 bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset), p), p += 4;
4451 if (PPC_HA (offset + 8) != PPC_HA (offset))
4452 bfd_put_32 (obfd, ADDIS_R12_R12_1, p), p += 4;
4453 offset += 8;
4454 bfd_put_32 (obfd, LD_R2_0R12 | PPC_LO (offset), p), p += 4;
4455 if (PPC_HA (offset + 8) != PPC_HA (offset))
4456 bfd_put_32 (obfd, ADDIS_R12_R12_1, p), p += 4;
4457 offset += 8;
4458 bfd_put_32 (obfd, MTCTR_R11, p), p += 4;
4459 bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset), p), p += 4;
4460 bfd_put_32 (obfd, BCTR, p), p += 4;
4461 return p;
4462 }
4463
4464 static boolean
4465 ppc_build_one_stub (gen_entry, in_arg)
4466 struct bfd_hash_entry *gen_entry;
4467 PTR in_arg;
4468 {
4469 struct ppc_stub_hash_entry *stub_entry;
4470 struct ppc_branch_hash_entry *br_entry;
4471 struct bfd_link_info *info;
4472 struct ppc_link_hash_table *htab;
4473 asection *stub_sec;
4474 bfd *stub_bfd;
4475 bfd_byte *loc;
4476 bfd_byte *p;
4477 unsigned int indx;
4478 bfd_vma off;
4479 int size;
4480
4481 /* Massage our args to the form they really have. */
4482 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
4483 info = (struct bfd_link_info *) in_arg;
4484
4485 htab = ppc_hash_table (info);
4486 stub_sec = stub_entry->stub_sec;
4487
4488 /* Make a note of the offset within the stubs for this entry. */
4489 stub_entry->stub_offset = stub_sec->_cooked_size;
4490 loc = stub_sec->contents + stub_entry->stub_offset;
4491
4492 stub_bfd = stub_sec->owner;
4493
4494 switch (stub_entry->stub_type)
4495 {
4496 case ppc_stub_long_branch:
4497 /* Branches are relative. This is where we are going to. */
4498 off = (stub_entry->target_value
4499 + stub_entry->target_section->output_offset
4500 + stub_entry->target_section->output_section->vma);
4501
4502 /* And this is where we are coming from. */
4503 off -= (stub_entry->stub_offset
4504 + stub_sec->output_offset
4505 + stub_sec->output_section->vma);
4506
4507 BFD_ASSERT (off + (1 << 25) < (bfd_vma) (1 << 26));
4508
4509 bfd_put_32 (stub_bfd, (bfd_vma) B_DOT | (off & 0x3fffffc), loc);
4510 size = 4;
4511 break;
4512
4513 case ppc_stub_plt_branch:
4514 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
4515 stub_entry->root.string + 9,
4516 false, false);
4517 if (br_entry == NULL)
4518 {
4519 (*_bfd_error_handler) (_("can't find branch stub `%s'"),
4520 stub_entry->root.string + 9);
4521 htab->stub_error = true;
4522 return false;
4523 }
4524
4525 off = (stub_entry->target_value
4526 + stub_entry->target_section->output_offset
4527 + stub_entry->target_section->output_section->vma);
4528
4529 bfd_put_64 (htab->sbrlt->owner, off,
4530 htab->sbrlt->contents + br_entry->offset);
4531
4532 if (info->shared)
4533 {
4534 /* Create a reloc for the branch lookup table entry. */
4535 Elf_Internal_Rela rela;
4536 Elf64_External_Rela *r;
4537
4538 rela.r_offset = (br_entry->offset
4539 + htab->sbrlt->output_offset
4540 + htab->sbrlt->output_section->vma);
4541 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
4542 rela.r_addend = off;
4543
4544 r = (Elf64_External_Rela *) htab->srelbrlt->contents;
4545 r += htab->srelbrlt->reloc_count++;
4546 bfd_elf64_swap_reloca_out (htab->srelbrlt->owner, &rela, r);
4547 }
4548
4549 off = (br_entry->offset
4550 + htab->sbrlt->output_offset
4551 + htab->sbrlt->output_section->vma
4552 - elf_gp (htab->sbrlt->output_section->owner)
4553 - TOC_BASE_OFF);
4554
4555 if (off + 0x80000000 > 0xffffffff || (off & 7) != 0)
4556 {
4557 (*_bfd_error_handler)
4558 (_("linkage table error against `%s'"),
4559 stub_entry->root.string);
4560 bfd_set_error (bfd_error_bad_value);
4561 htab->stub_error = true;
4562 return false;
4563 }
4564
4565 indx = off;
4566 bfd_put_32 (stub_bfd, (bfd_vma) ADDIS_R12_R2 | PPC_HA (indx), loc);
4567 bfd_put_32 (stub_bfd, (bfd_vma) LD_R11_0R12 | PPC_LO (indx), loc + 4);
4568 bfd_put_32 (stub_bfd, (bfd_vma) MTCTR_R11, loc + 8);
4569 bfd_put_32 (stub_bfd, (bfd_vma) BCTR, loc + 12);
4570 size = 16;
4571 break;
4572
4573 case ppc_stub_plt_call:
4574 /* Build the .glink lazy link call stub. */
4575 p = htab->sglink->contents + htab->sglink->_cooked_size;
4576 indx = htab->sglink->reloc_count;
4577 if (indx < 0x8000)
4578 {
4579 bfd_put_32 (htab->sglink->owner, LI_R0_0 | indx, p);
4580 p += 4;
4581 }
4582 else
4583 {
4584 bfd_put_32 (htab->sglink->owner, LIS_R0_0 | PPC_HI (indx), p);
4585 p += 4;
4586 bfd_put_32 (htab->sglink->owner, ORI_R0_R0_0 | PPC_LO (indx), p);
4587 p += 4;
4588 }
4589 bfd_put_32 (htab->sglink->owner,
4590 B_DOT | ((htab->sglink->contents - p) & 0x3fffffc), p);
4591 p += 4;
4592 htab->sglink->_cooked_size = p - htab->sglink->contents;
4593 htab->sglink->reloc_count += 1;
4594
4595 /* Do the best we can for shared libraries built without
4596 exporting ".foo" for each "foo". This can happen when symbol
4597 versioning scripts strip all bar a subset of symbols. */
4598 if (stub_entry->h->oh->root.type != bfd_link_hash_defined
4599 && stub_entry->h->oh->root.type != bfd_link_hash_defweak)
4600 {
4601 /* Point the symbol at the stub. There may be multiple stubs,
4602 we don't really care; The main thing is to make this sym
4603 defined somewhere. */
4604 stub_entry->h->oh->root.type = bfd_link_hash_defined;
4605 stub_entry->h->oh->root.u.def.section = stub_entry->stub_sec;
4606 stub_entry->h->oh->root.u.def.value = stub_entry->stub_offset;
4607 }
4608
4609 /* Now build the stub. */
4610 off = stub_entry->h->elf.plt.offset;
4611 if (off >= (bfd_vma) -2)
4612 abort ();
4613
4614 off &= ~ (bfd_vma) 1;
4615 off += (htab->splt->output_offset
4616 + htab->splt->output_section->vma
4617 - elf_gp (htab->splt->output_section->owner)
4618 - TOC_BASE_OFF);
4619
4620 if (off + 0x80000000 > 0xffffffff || (off & 7) != 0)
4621 {
4622 (*_bfd_error_handler)
4623 (_("linkage table error against `%s'"),
4624 stub_entry->h->elf.root.root.string);
4625 bfd_set_error (bfd_error_bad_value);
4626 htab->stub_error = true;
4627 return false;
4628 }
4629
4630 p = build_plt_stub (stub_bfd, loc, (int) off, 0);
4631 size = p - loc;
4632 break;
4633
4634 default:
4635 BFD_FAIL ();
4636 return false;
4637 }
4638
4639 stub_sec->_cooked_size += size;
4640 return true;
4641 }
4642
4643 /* As above, but don't actually build the stub. Just bump offset so
4644 we know stub section sizes, and select plt_branch stubs where
4645 long_branch stubs won't do. */
4646
4647 static boolean
4648 ppc_size_one_stub (gen_entry, in_arg)
4649 struct bfd_hash_entry *gen_entry;
4650 PTR in_arg;
4651 {
4652 struct ppc_stub_hash_entry *stub_entry;
4653 struct ppc_link_hash_table *htab;
4654 bfd_vma off;
4655 int size;
4656
4657 /* Massage our args to the form they really have. */
4658 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
4659 htab = (struct ppc_link_hash_table *) in_arg;
4660
4661 if (stub_entry->stub_type == ppc_stub_plt_call)
4662 {
4663 off = stub_entry->h->elf.plt.offset & ~(bfd_vma) 1;
4664 off += (htab->splt->output_offset
4665 + htab->splt->output_section->vma
4666 - elf_gp (htab->splt->output_section->owner)
4667 - TOC_BASE_OFF);
4668
4669 size = 28;
4670 if (PPC_HA ((int) off + 16) != PPC_HA ((int) off))
4671 size += 4;
4672 }
4673 else
4674 {
4675 /* ppc_stub_long_branch or ppc_stub_plt_branch. */
4676 stub_entry->stub_type = ppc_stub_long_branch;
4677 size = 4;
4678
4679 off = (stub_entry->target_value
4680 + stub_entry->target_section->output_offset
4681 + stub_entry->target_section->output_section->vma);
4682 off -= (stub_entry->stub_sec->_raw_size
4683 + stub_entry->stub_sec->output_offset
4684 + stub_entry->stub_sec->output_section->vma);
4685
4686 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
4687 {
4688 struct ppc_branch_hash_entry *br_entry;
4689
4690 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
4691 stub_entry->root.string + 9,
4692 true, false);
4693 if (br_entry == NULL)
4694 {
4695 (*_bfd_error_handler) (_("can't build branch stub `%s'"),
4696 stub_entry->root.string + 9);
4697 htab->stub_error = true;
4698 return false;
4699 }
4700
4701 if (br_entry->iter != htab->stub_iteration)
4702 {
4703 br_entry->iter = htab->stub_iteration;
4704 br_entry->offset = htab->sbrlt->_raw_size;
4705 htab->sbrlt->_raw_size += 8;
4706 }
4707 stub_entry->stub_type = ppc_stub_plt_branch;
4708 size = 16;
4709 }
4710 }
4711
4712 stub_entry->stub_sec->_raw_size += size;
4713 return true;
4714 }
4715
4716 /* Set up various things so that we can make a list of input sections
4717 for each output section included in the link. Returns -1 on error,
4718 0 when no stubs will be needed, and 1 on success. */
4719
4720 int
4721 ppc64_elf_setup_section_lists (output_bfd, info)
4722 bfd *output_bfd;
4723 struct bfd_link_info *info;
4724 {
4725 bfd *input_bfd;
4726 int top_id, top_index;
4727 asection *section;
4728 asection **input_list, **list;
4729 bfd_size_type amt;
4730 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4731
4732 if (htab->elf.root.creator->flavour != bfd_target_elf_flavour
4733 || htab->sbrlt == NULL)
4734 return 0;
4735
4736 /* Find the top input section id. */
4737 for (input_bfd = info->input_bfds, top_id = 0;
4738 input_bfd != NULL;
4739 input_bfd = input_bfd->link_next)
4740 {
4741 for (section = input_bfd->sections;
4742 section != NULL;
4743 section = section->next)
4744 {
4745 if (top_id < section->id)
4746 top_id = section->id;
4747 }
4748 }
4749
4750 amt = sizeof (struct map_stub) * (top_id + 1);
4751 htab->stub_group = (struct map_stub *) bfd_zmalloc (amt);
4752 if (htab->stub_group == NULL)
4753 return -1;
4754
4755 /* We can't use output_bfd->section_count here to find the top output
4756 section index as some sections may have been removed, and
4757 _bfd_strip_section_from_output doesn't renumber the indices. */
4758 for (section = output_bfd->sections, top_index = 0;
4759 section != NULL;
4760 section = section->next)
4761 {
4762 if (top_index < section->index)
4763 top_index = section->index;
4764 }
4765
4766 htab->top_index = top_index;
4767 amt = sizeof (asection *) * (top_index + 1);
4768 input_list = (asection **) bfd_malloc (amt);
4769 htab->input_list = input_list;
4770 if (input_list == NULL)
4771 return -1;
4772
4773 /* For sections we aren't interested in, mark their entries with a
4774 value we can check later. */
4775 list = input_list + top_index;
4776 do
4777 *list = bfd_abs_section_ptr;
4778 while (list-- != input_list);
4779
4780 for (section = output_bfd->sections;
4781 section != NULL;
4782 section = section->next)
4783 {
4784 if ((section->flags & SEC_CODE) != 0)
4785 input_list[section->index] = NULL;
4786 }
4787
4788 return 1;
4789 }
4790
4791 /* The linker repeatedly calls this function for each input section,
4792 in the order that input sections are linked into output sections.
4793 Build lists of input sections to determine groupings between which
4794 we may insert linker stubs. */
4795
4796 void
4797 ppc64_elf_next_input_section (info, isec)
4798 struct bfd_link_info *info;
4799 asection *isec;
4800 {
4801 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4802
4803 if (isec->output_section->index <= htab->top_index)
4804 {
4805 asection **list = htab->input_list + isec->output_section->index;
4806 if (*list != bfd_abs_section_ptr)
4807 {
4808 /* Steal the link_sec pointer for our list. */
4809 #define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
4810 /* This happens to make the list in reverse order,
4811 which is what we want. */
4812 PREV_SEC (isec) = *list;
4813 *list = isec;
4814 }
4815 }
4816 }
4817
4818 /* See whether we can group stub sections together. Grouping stub
4819 sections may result in fewer stubs. More importantly, we need to
4820 put all .init* and .fini* stubs at the beginning of the .init or
4821 .fini output sections respectively, because glibc splits the
4822 _init and _fini functions into multiple parts. Putting a stub in
4823 the middle of a function is not a good idea. */
4824
4825 static void
4826 group_sections (htab, stub_group_size, stubs_always_before_branch)
4827 struct ppc_link_hash_table *htab;
4828 bfd_size_type stub_group_size;
4829 boolean stubs_always_before_branch;
4830 {
4831 asection **list = htab->input_list + htab->top_index;
4832 do
4833 {
4834 asection *tail = *list;
4835 if (tail == bfd_abs_section_ptr)
4836 continue;
4837 while (tail != NULL)
4838 {
4839 asection *curr;
4840 asection *prev;
4841 bfd_size_type total;
4842
4843 curr = tail;
4844 if (tail->_cooked_size)
4845 total = tail->_cooked_size;
4846 else
4847 total = tail->_raw_size;
4848 while ((prev = PREV_SEC (curr)) != NULL
4849 && ((total += curr->output_offset - prev->output_offset)
4850 < stub_group_size))
4851 curr = prev;
4852
4853 /* OK, the size from the start of CURR to the end is less
4854 than stub_group_size and thus can be handled by one stub
4855 section. (or the tail section is itself larger than
4856 stub_group_size, in which case we may be toast.) We
4857 should really be keeping track of the total size of stubs
4858 added here, as stubs contribute to the final output
4859 section size. That's a little tricky, and this way will
4860 only break if stubs added make the total size more than
4861 2^25, ie. for the default stub_group_size, if stubs total
4862 more than 2834432 bytes, or over 100000 plt call stubs. */
4863 do
4864 {
4865 prev = PREV_SEC (tail);
4866 /* Set up this stub group. */
4867 htab->stub_group[tail->id].link_sec = curr;
4868 }
4869 while (tail != curr && (tail = prev) != NULL);
4870
4871 /* But wait, there's more! Input sections up to stub_group_size
4872 bytes before the stub section can be handled by it too. */
4873 if (!stubs_always_before_branch)
4874 {
4875 total = 0;
4876 while (prev != NULL
4877 && ((total += tail->output_offset - prev->output_offset)
4878 < stub_group_size))
4879 {
4880 tail = prev;
4881 prev = PREV_SEC (tail);
4882 htab->stub_group[tail->id].link_sec = curr;
4883 }
4884 }
4885 tail = prev;
4886 }
4887 }
4888 while (list-- != htab->input_list);
4889 free (htab->input_list);
4890 #undef PREV_SEC
4891 }
4892
4893 /* Read in all local syms for all input bfds. */
4894
4895 static boolean
4896 get_local_syms (input_bfd, htab)
4897 bfd *input_bfd;
4898 struct ppc_link_hash_table *htab;
4899 {
4900 unsigned int bfd_indx;
4901 bfd *ibfd;
4902 Elf_Internal_Sym *local_syms, **all_local_syms;
4903 bfd_size_type amt;
4904
4905 if (htab->all_local_syms != NULL)
4906 return true;
4907
4908 /* We want to read in symbol extension records only once. To do this
4909 we need to read in the local symbols in parallel and save them for
4910 later use; so hold pointers to the local symbols in an array. */
4911 for (ibfd = input_bfd, bfd_indx = 0; ibfd != NULL; ibfd = ibfd->link_next)
4912 bfd_indx += 1;
4913 htab->bfd_count = bfd_indx;
4914 amt = sizeof (Elf_Internal_Sym *) * bfd_indx;
4915 all_local_syms = (Elf_Internal_Sym **) bfd_zmalloc (amt);
4916 htab->all_local_syms = all_local_syms;
4917 if (all_local_syms == NULL)
4918 return false;
4919
4920 /* Walk over all the input BFDs, swapping in local symbols. */
4921 for (bfd_indx = 0;
4922 input_bfd != NULL;
4923 input_bfd = input_bfd->link_next, bfd_indx++)
4924 {
4925 Elf_Internal_Shdr *symtab_hdr;
4926 Elf_Internal_Shdr *shndx_hdr;
4927 Elf_Internal_Sym *isym;
4928 Elf64_External_Sym *ext_syms, *esym, *end_sy;
4929 Elf_External_Sym_Shndx *shndx_buf, *shndx;
4930 bfd_size_type sec_size;
4931
4932 if (bfd_get_flavour (input_bfd) != bfd_target_elf_flavour)
4933 continue;
4934
4935 /* We'll need the symbol table in a second. */
4936 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4937 if (symtab_hdr->sh_info == 0)
4938 continue;
4939
4940 /* We need an array of the local symbols attached to the input bfd.
4941 Unfortunately, we're going to have to read & swap them in. */
4942 sec_size = symtab_hdr->sh_info;
4943 sec_size *= sizeof (Elf_Internal_Sym);
4944 local_syms = (Elf_Internal_Sym *) bfd_malloc (sec_size);
4945 if (local_syms == NULL)
4946 return false;
4947
4948 all_local_syms[bfd_indx] = local_syms;
4949 sec_size = symtab_hdr->sh_info;
4950 sec_size *= sizeof (Elf64_External_Sym);
4951 ext_syms = (Elf64_External_Sym *) bfd_malloc (sec_size);
4952 if (ext_syms == NULL)
4953 return false;
4954
4955 if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
4956 || bfd_bread ((PTR) ext_syms, sec_size, input_bfd) != sec_size)
4957 {
4958 error_ret_free_ext_syms:
4959 free (ext_syms);
4960 return false;
4961 }
4962
4963 shndx_buf = NULL;
4964 shndx_hdr = &elf_tdata (input_bfd)->symtab_shndx_hdr;
4965 if (shndx_hdr->sh_size != 0)
4966 {
4967 sec_size = symtab_hdr->sh_info;
4968 sec_size *= sizeof (Elf_External_Sym_Shndx);
4969 shndx_buf = (Elf_External_Sym_Shndx *) bfd_malloc (sec_size);
4970 if (shndx_buf == NULL)
4971 goto error_ret_free_ext_syms;
4972
4973 if (bfd_seek (input_bfd, shndx_hdr->sh_offset, SEEK_SET) != 0
4974 || bfd_bread ((PTR) shndx_buf, sec_size, input_bfd) != sec_size)
4975 {
4976 free (shndx_buf);
4977 goto error_ret_free_ext_syms;
4978 }
4979 }
4980
4981 /* Swap the local symbols in. */
4982 for (esym = ext_syms, end_sy = esym + symtab_hdr->sh_info,
4983 isym = local_syms, shndx = shndx_buf;
4984 esym < end_sy;
4985 esym++, isym++, shndx = (shndx ? shndx + 1 : NULL))
4986 bfd_elf64_swap_symbol_in (input_bfd, (const PTR) esym,
4987 (const PTR) shndx, isym);
4988
4989 /* Now we can free the external symbols. */
4990 free (shndx_buf);
4991 free (ext_syms);
4992 }
4993
4994 return true;
4995 }
4996
4997 /* Determine and set the size of the stub section for a final link.
4998
4999 The basic idea here is to examine all the relocations looking for
5000 PC-relative calls to a target that is unreachable with a "bl"
5001 instruction. */
5002
5003 boolean
5004 ppc64_elf_size_stubs (output_bfd, stub_bfd, info, group_size,
5005 add_stub_section, layout_sections_again)
5006 bfd *output_bfd;
5007 bfd *stub_bfd;
5008 struct bfd_link_info *info;
5009 bfd_signed_vma group_size;
5010 asection * (*add_stub_section) PARAMS ((const char *, asection *));
5011 void (*layout_sections_again) PARAMS ((void));
5012 {
5013 bfd_size_type stub_group_size;
5014 boolean stubs_always_before_branch;
5015 boolean ret = false;
5016 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5017
5018 /* Stash our params away. */
5019 htab->stub_bfd = stub_bfd;
5020 htab->add_stub_section = add_stub_section;
5021 htab->layout_sections_again = layout_sections_again;
5022 stubs_always_before_branch = group_size < 0;
5023 if (group_size < 0)
5024 stub_group_size = -group_size;
5025 else
5026 stub_group_size = group_size;
5027 if (stub_group_size == 1)
5028 {
5029 /* Default values. */
5030 stub_group_size = 30720000;
5031 if (htab->has_14bit_branch)
5032 stub_group_size = 30000;
5033 }
5034
5035 group_sections (htab, stub_group_size, stubs_always_before_branch);
5036
5037 if (! get_local_syms (info->input_bfds, htab))
5038 {
5039 if (htab->all_local_syms)
5040 goto error_ret_free_local;
5041 return false;
5042 }
5043
5044 while (1)
5045 {
5046 bfd *input_bfd;
5047 unsigned int bfd_indx;
5048 asection *stub_sec;
5049 boolean stub_changed;
5050
5051 htab->stub_iteration += 1;
5052 stub_changed = false;
5053
5054 for (input_bfd = info->input_bfds, bfd_indx = 0;
5055 input_bfd != NULL;
5056 input_bfd = input_bfd->link_next, bfd_indx++)
5057 {
5058 Elf_Internal_Shdr *symtab_hdr;
5059 asection *section;
5060 Elf_Internal_Sym *local_syms;
5061
5062 /* We'll need the symbol table in a second. */
5063 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5064 if (symtab_hdr->sh_info == 0)
5065 continue;
5066
5067 local_syms = htab->all_local_syms[bfd_indx];
5068
5069 /* Walk over each section attached to the input bfd. */
5070 for (section = input_bfd->sections;
5071 section != NULL;
5072 section = section->next)
5073 {
5074 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
5075
5076 /* If there aren't any relocs, then there's nothing more
5077 to do. */
5078 if ((section->flags & SEC_RELOC) == 0
5079 || section->reloc_count == 0)
5080 continue;
5081
5082 /* If this section is a link-once section that will be
5083 discarded, then don't create any stubs. */
5084 if (section->output_section == NULL
5085 || section->output_section->owner != output_bfd)
5086 continue;
5087
5088 /* Get the relocs. */
5089 internal_relocs
5090 = _bfd_elf64_link_read_relocs (input_bfd, section, NULL,
5091 (Elf_Internal_Rela *) NULL,
5092 info->keep_memory);
5093 if (internal_relocs == NULL)
5094 goto error_ret_free_local;
5095
5096 /* Now examine each relocation. */
5097 irela = internal_relocs;
5098 irelaend = irela + section->reloc_count;
5099 for (; irela < irelaend; irela++)
5100 {
5101 unsigned int r_type, r_indx;
5102 enum ppc_stub_type stub_type;
5103 struct ppc_stub_hash_entry *stub_entry;
5104 asection *sym_sec;
5105 bfd_vma sym_value;
5106 bfd_vma destination;
5107 struct ppc_link_hash_entry *hash;
5108 char *stub_name;
5109 const asection *id_sec;
5110
5111 r_type = ELF64_R_TYPE (irela->r_info);
5112 r_indx = ELF64_R_SYM (irela->r_info);
5113
5114 if (r_type >= (unsigned int) R_PPC_max)
5115 {
5116 bfd_set_error (bfd_error_bad_value);
5117 error_ret_free_internal:
5118 if (elf_section_data (section)->relocs == NULL)
5119 free (internal_relocs);
5120 goto error_ret_free_local;
5121 }
5122
5123 /* Only look for stubs on branch instructions. */
5124 if (r_type != (unsigned int) R_PPC64_REL24
5125 && r_type != (unsigned int) R_PPC64_REL14
5126 && r_type != (unsigned int) R_PPC64_REL14_BRTAKEN
5127 && r_type != (unsigned int) R_PPC64_REL14_BRNTAKEN)
5128 continue;
5129
5130 /* Now determine the call target, its name, value,
5131 section. */
5132 sym_sec = NULL;
5133 sym_value = 0;
5134 destination = 0;
5135 hash = NULL;
5136 if (r_indx < symtab_hdr->sh_info)
5137 {
5138 /* It's a local symbol. */
5139 Elf_Internal_Sym *sym;
5140 Elf_Internal_Shdr *hdr;
5141
5142 sym = local_syms + r_indx;
5143 hdr = elf_elfsections (input_bfd)[sym->st_shndx];
5144 sym_sec = hdr->bfd_section;
5145 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
5146 sym_value = sym->st_value;
5147 destination = (sym_value + irela->r_addend
5148 + sym_sec->output_offset
5149 + sym_sec->output_section->vma);
5150 }
5151 else
5152 {
5153 /* It's an external symbol. */
5154 int e_indx;
5155
5156 e_indx = r_indx - symtab_hdr->sh_info;
5157 hash = ((struct ppc_link_hash_entry *)
5158 elf_sym_hashes (input_bfd)[e_indx]);
5159
5160 while (hash->elf.root.type == bfd_link_hash_indirect
5161 || hash->elf.root.type == bfd_link_hash_warning)
5162 hash = ((struct ppc_link_hash_entry *)
5163 hash->elf.root.u.i.link);
5164
5165 if (hash->elf.root.type == bfd_link_hash_defined
5166 || hash->elf.root.type == bfd_link_hash_defweak)
5167 {
5168 sym_sec = hash->elf.root.u.def.section;
5169 sym_value = hash->elf.root.u.def.value;
5170 if (sym_sec->output_section != NULL)
5171 destination = (sym_value + irela->r_addend
5172 + sym_sec->output_offset
5173 + sym_sec->output_section->vma);
5174 }
5175 else if (hash->elf.root.type == bfd_link_hash_undefweak)
5176 ;
5177 else if (hash->elf.root.type == bfd_link_hash_undefined)
5178 ;
5179 else
5180 {
5181 bfd_set_error (bfd_error_bad_value);
5182 goto error_ret_free_internal;
5183 }
5184 }
5185
5186 /* Determine what (if any) linker stub is needed. */
5187 stub_type = ppc_type_of_stub (section, irela, &hash,
5188 destination);
5189 if (stub_type == ppc_stub_none)
5190 continue;
5191
5192 /* Support for grouping stub sections. */
5193 id_sec = htab->stub_group[section->id].link_sec;
5194
5195 /* Get the name of this stub. */
5196 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
5197 if (!stub_name)
5198 goto error_ret_free_internal;
5199
5200 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
5201 stub_name, false, false);
5202 if (stub_entry != NULL)
5203 {
5204 /* The proper stub has already been created. */
5205 free (stub_name);
5206 continue;
5207 }
5208
5209 stub_entry = ppc_add_stub (stub_name, section, htab);
5210 if (stub_entry == NULL)
5211 {
5212 free (stub_name);
5213 goto error_ret_free_internal;
5214 }
5215
5216 stub_entry->target_value = sym_value;
5217 stub_entry->target_section = sym_sec;
5218 stub_entry->stub_type = stub_type;
5219 stub_entry->h = hash;
5220 stub_changed = true;
5221 }
5222
5223 /* We're done with the internal relocs, free them. */
5224 if (elf_section_data (section)->relocs == NULL)
5225 free (internal_relocs);
5226 }
5227 }
5228
5229 if (!stub_changed)
5230 break;
5231
5232 /* OK, we've added some stubs. Find out the new size of the
5233 stub sections. */
5234 for (stub_sec = htab->stub_bfd->sections;
5235 stub_sec != NULL;
5236 stub_sec = stub_sec->next)
5237 {
5238 stub_sec->_raw_size = 0;
5239 stub_sec->_cooked_size = 0;
5240 }
5241 htab->sbrlt->_raw_size = 0;
5242 htab->sbrlt->_cooked_size = 0;
5243
5244 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, htab);
5245
5246 /* Ask the linker to do its stuff. */
5247 (*htab->layout_sections_again) ();
5248 }
5249
5250 /* It would be nice to strip .branch_lt from the output if the
5251 section is empty, but it's too late. If we strip sections here,
5252 the dynamic symbol table is corrupted since the section symbol
5253 for the stripped section isn't written. */
5254
5255 ret = true;
5256
5257 error_ret_free_local:
5258 while (htab->bfd_count-- > 0)
5259 if (htab->all_local_syms[htab->bfd_count])
5260 free (htab->all_local_syms[htab->bfd_count]);
5261 free (htab->all_local_syms);
5262
5263 return ret;
5264 }
5265
5266 /* Called after we have determined section placement. If sections
5267 move, we'll be called again. Provide a value for TOCstart. */
5268
5269 bfd_vma
5270 ppc64_elf_toc (obfd)
5271 bfd *obfd;
5272 {
5273 asection *s;
5274 bfd_vma TOCstart;
5275
5276 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
5277 order. The TOC starts where the first of these sections starts. */
5278 s = bfd_get_section_by_name (obfd, ".got");
5279 if (s == NULL)
5280 s = bfd_get_section_by_name (obfd, ".toc");
5281 if (s == NULL)
5282 s = bfd_get_section_by_name (obfd, ".tocbss");
5283 if (s == NULL)
5284 s = bfd_get_section_by_name (obfd, ".plt");
5285 if (s == NULL)
5286 {
5287 /* This may happen for
5288 o references to TOC base (SYM@toc / TOC[tc0]) without a
5289 .toc directive
5290 o bad linker script
5291 o --gc-sections and empty TOC sections
5292
5293 FIXME: Warn user? */
5294
5295 /* Look for a likely section. We probably won't even be
5296 using TOCstart. */
5297 for (s = obfd->sections; s != NULL; s = s->next)
5298 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY))
5299 == (SEC_ALLOC | SEC_SMALL_DATA))
5300 break;
5301 if (s == NULL)
5302 for (s = obfd->sections; s != NULL; s = s->next)
5303 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA))
5304 == (SEC_ALLOC | SEC_SMALL_DATA))
5305 break;
5306 if (s == NULL)
5307 for (s = obfd->sections; s != NULL; s = s->next)
5308 if ((s->flags & (SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
5309 break;
5310 if (s == NULL)
5311 for (s = obfd->sections; s != NULL; s = s->next)
5312 if ((s->flags & SEC_ALLOC) == SEC_ALLOC)
5313 break;
5314 }
5315
5316 TOCstart = 0;
5317 if (s != NULL)
5318 TOCstart = s->output_section->vma + s->output_offset;
5319
5320 return TOCstart;
5321 }
5322
5323 /* Build all the stubs associated with the current output file.
5324 The stubs are kept in a hash table attached to the main linker
5325 hash table. This function is called via gldelf64ppc_finish. */
5326
5327 boolean
5328 ppc64_elf_build_stubs (info)
5329 struct bfd_link_info *info;
5330 {
5331 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5332 asection *stub_sec;
5333 bfd_vma plt_r2;
5334 bfd_byte *p;
5335
5336 for (stub_sec = htab->stub_bfd->sections;
5337 stub_sec != NULL;
5338 stub_sec = stub_sec->next)
5339 {
5340 bfd_size_type size;
5341
5342 /* Allocate memory to hold the linker stubs. */
5343 size = stub_sec->_raw_size;
5344 if (size != 0)
5345 {
5346 stub_sec->contents = (bfd_byte *) bfd_zalloc (htab->stub_bfd, size);
5347 if (stub_sec->contents == NULL)
5348 return false;
5349 }
5350 stub_sec->_cooked_size = 0;
5351 }
5352
5353 if (htab->splt != NULL)
5354 {
5355 /* Build the .glink plt call stub. */
5356 plt_r2 = (htab->splt->output_offset
5357 + htab->splt->output_section->vma
5358 - elf_gp (htab->splt->output_section->owner)
5359 - TOC_BASE_OFF);
5360 p = htab->sglink->contents;
5361 p = build_plt_stub (htab->sglink->owner, p, (int) plt_r2, 1);
5362 while (p - htab->sglink->contents < GLINK_CALL_STUB_SIZE)
5363 {
5364 bfd_put_32 (htab->sglink->owner, NOP, p);
5365 p += 4;
5366 }
5367 htab->sglink->_cooked_size = p - htab->sglink->contents;
5368
5369 /* Use reloc_count to count entries. */
5370 htab->sglink->reloc_count = 0;
5371 }
5372
5373 if (htab->sbrlt->_raw_size != 0)
5374 {
5375 htab->sbrlt->contents = (bfd_byte *) bfd_zalloc (htab->sbrlt->owner,
5376 htab->sbrlt->_raw_size);
5377 if (htab->sbrlt->contents == NULL)
5378 return false;
5379 }
5380
5381 /* Build the stubs as directed by the stub hash table. */
5382 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
5383 htab->sglink->reloc_count = 0;
5384
5385 for (stub_sec = htab->stub_bfd->sections;
5386 stub_sec != NULL;
5387 stub_sec = stub_sec->next)
5388 {
5389 if (stub_sec->_raw_size != stub_sec->_cooked_size)
5390 break;
5391 }
5392
5393 if (stub_sec != NULL
5394 || htab->sglink->_raw_size != htab->sglink->_cooked_size)
5395 {
5396 htab->stub_error = true;
5397 (*_bfd_error_handler) (_("stubs don't match calculated size"));
5398 }
5399
5400 return !htab->stub_error;
5401 }
5402
5403 /* Set up any other section flags and such that may be necessary. */
5404
5405 static boolean
5406 ppc64_elf_fake_sections (abfd, shdr, asect)
5407 bfd *abfd ATTRIBUTE_UNUSED;
5408 Elf64_Internal_Shdr *shdr;
5409 asection *asect;
5410 {
5411 if ((asect->flags & SEC_EXCLUDE) != 0)
5412 shdr->sh_flags |= SHF_EXCLUDE;
5413
5414 if ((asect->flags & SEC_SORT_ENTRIES) != 0)
5415 shdr->sh_type = SHT_ORDERED;
5416
5417 return true;
5418 }
5419
5420 /* The RELOCATE_SECTION function is called by the ELF backend linker
5421 to handle the relocations for a section.
5422
5423 The relocs are always passed as Rela structures; if the section
5424 actually uses Rel structures, the r_addend field will always be
5425 zero.
5426
5427 This function is responsible for adjust the section contents as
5428 necessary, and (if using Rela relocs and generating a
5429 relocateable output file) adjusting the reloc addend as
5430 necessary.
5431
5432 This function does not have to worry about setting the reloc
5433 address or the reloc symbol index.
5434
5435 LOCAL_SYMS is a pointer to the swapped in local symbols.
5436
5437 LOCAL_SECTIONS is an array giving the section in the input file
5438 corresponding to the st_shndx field of each local symbol.
5439
5440 The global hash table entry for the global symbols can be found
5441 via elf_sym_hashes (input_bfd).
5442
5443 When generating relocateable output, this function must handle
5444 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
5445 going to be the section symbol corresponding to the output
5446 section, which means that the addend must be adjusted
5447 accordingly. */
5448
5449 static boolean
5450 ppc64_elf_relocate_section (output_bfd, info, input_bfd, input_section,
5451 contents, relocs, local_syms, local_sections)
5452 bfd *output_bfd;
5453 struct bfd_link_info *info;
5454 bfd *input_bfd;
5455 asection *input_section;
5456 bfd_byte *contents;
5457 Elf_Internal_Rela *relocs;
5458 Elf_Internal_Sym *local_syms;
5459 asection **local_sections;
5460 {
5461 struct ppc_link_hash_table *htab;
5462 Elf_Internal_Shdr *symtab_hdr;
5463 struct elf_link_hash_entry **sym_hashes;
5464 Elf_Internal_Rela *rel;
5465 Elf_Internal_Rela *relend;
5466 bfd_vma *local_got_offsets;
5467 bfd_vma TOCstart;
5468 boolean ret = true;
5469 boolean is_opd;
5470 /* Disabled until we sort out how ld should choose 'y' vs 'at'. */
5471 boolean is_power4 = false;
5472
5473 if (info->relocateable)
5474 return true;
5475
5476 /* Initialize howto table if needed. */
5477 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5478 ppc_howto_init ();
5479
5480 htab = ppc_hash_table (info);
5481 local_got_offsets = elf_local_got_offsets (input_bfd);
5482 TOCstart = elf_gp (output_bfd);
5483 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5484 sym_hashes = elf_sym_hashes (input_bfd);
5485 is_opd = elf_section_data (input_section)->tdata != NULL;
5486
5487 rel = relocs;
5488 relend = relocs + input_section->reloc_count;
5489 for (; rel < relend; rel++)
5490 {
5491 enum elf_ppc_reloc_type r_type;
5492 bfd_vma offset;
5493 bfd_vma addend;
5494 bfd_reloc_status_type r;
5495 Elf_Internal_Sym *sym;
5496 asection *sec;
5497 struct elf_link_hash_entry *h;
5498 struct elf_link_hash_entry *fdh;
5499 const char *sym_name;
5500 unsigned long r_symndx;
5501 bfd_vma relocation;
5502 boolean unresolved_reloc;
5503 boolean warned;
5504 long insn;
5505 struct ppc_stub_hash_entry *stub_entry;
5506 bfd_vma max_br_offset;
5507 bfd_vma from;
5508
5509 r_type = (enum elf_ppc_reloc_type) ELF64_R_TYPE (rel->r_info);
5510 r_symndx = ELF64_R_SYM (rel->r_info);
5511 offset = rel->r_offset;
5512 addend = rel->r_addend;
5513 r = bfd_reloc_other;
5514 sym = (Elf_Internal_Sym *) 0;
5515 sec = (asection *) 0;
5516 h = (struct elf_link_hash_entry *) 0;
5517 sym_name = (const char *) 0;
5518 unresolved_reloc = false;
5519 warned = false;
5520
5521 if (r_type == R_PPC64_TOC)
5522 {
5523 /* Relocation value is TOC base. Symbol is ignored. */
5524 relocation = TOCstart + TOC_BASE_OFF;
5525 }
5526 else if (r_symndx < symtab_hdr->sh_info)
5527 {
5528 /* It's a local symbol. */
5529 sym = local_syms + r_symndx;
5530 sec = local_sections[r_symndx];
5531 sym_name = "<local symbol>";
5532
5533 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, sec, rel);
5534 /* rel may have changed, update our copy of addend. */
5535 addend = rel->r_addend;
5536
5537 if (elf_section_data (sec) != NULL)
5538 {
5539 long *opd_sym_adjust;
5540
5541 opd_sym_adjust = (long *) elf_section_data (sec)->tdata;
5542 if (opd_sym_adjust != NULL && sym->st_value % 24 == 0)
5543 relocation += opd_sym_adjust[sym->st_value / 24];
5544 }
5545 }
5546 else
5547 {
5548 /* It's a global symbol. */
5549 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5550 while (h->root.type == bfd_link_hash_indirect
5551 || h->root.type == bfd_link_hash_warning)
5552 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5553 sym_name = h->root.root.string;
5554 relocation = 0;
5555 if (h->root.type == bfd_link_hash_defined
5556 || h->root.type == bfd_link_hash_defweak)
5557 {
5558 sec = h->root.u.def.section;
5559 if (sec->output_section == NULL)
5560 /* Set a flag that will be cleared later if we find a
5561 relocation value for this symbol. output_section
5562 is typically NULL for symbols satisfied by a shared
5563 library. */
5564 unresolved_reloc = true;
5565 else
5566 relocation = (h->root.u.def.value
5567 + sec->output_section->vma
5568 + sec->output_offset);
5569 }
5570 else if (h->root.type == bfd_link_hash_undefweak)
5571 ;
5572 else if (info->shared
5573 && (!info->symbolic || info->allow_shlib_undefined)
5574 && !info->no_undefined
5575 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
5576 ;
5577 else
5578 {
5579 if (! ((*info->callbacks->undefined_symbol)
5580 (info, h->root.root.string, input_bfd, input_section,
5581 offset, (!info->shared
5582 || info->no_undefined
5583 || ELF_ST_VISIBILITY (h->other)))))
5584 return false;
5585 warned = true;
5586 }
5587 }
5588
5589 /* First handle relocations that tweak non-addend part of insn. */
5590 insn = 0;
5591 switch (r_type)
5592 {
5593 default:
5594 break;
5595
5596 /* Branch taken prediction relocations. */
5597 case R_PPC64_ADDR14_BRTAKEN:
5598 case R_PPC64_REL14_BRTAKEN:
5599 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
5600 /* Fall thru. */
5601
5602 /* Branch not taken prediction relocations. */
5603 case R_PPC64_ADDR14_BRNTAKEN:
5604 case R_PPC64_REL14_BRNTAKEN:
5605 insn |= bfd_get_32 (output_bfd, contents + offset) & ~(0x01 << 21);
5606 if (is_power4)
5607 {
5608 /* Set 'a' bit. This is 0b00010 in BO field for branch
5609 on CR(BI) insns (BO == 001at or 011at), and 0b01000
5610 for branch on CTR insns (BO == 1a00t or 1a01t). */
5611 if ((insn & (0x14 << 21)) == (0x04 << 21))
5612 insn |= 0x02 << 21;
5613 else if ((insn & (0x14 << 21)) == (0x10 << 21))
5614 insn |= 0x08 << 21;
5615 else
5616 break;
5617 }
5618 else
5619 {
5620 from = (offset
5621 + input_section->output_offset
5622 + input_section->output_section->vma);
5623
5624 /* Invert 'y' bit if not the default. */
5625 if ((bfd_signed_vma) (relocation + addend - from) < 0)
5626 insn ^= 0x01 << 21;
5627 }
5628
5629 bfd_put_32 (output_bfd, (bfd_vma) insn, contents + offset);
5630 break;
5631
5632 case R_PPC64_REL24:
5633 /* A REL24 branching to a linkage function is followed by a
5634 nop. We replace the nop with a ld in order to restore
5635 the TOC base pointer. Only calls to shared objects need
5636 to alter the TOC base. These are recognized by their
5637 need for a PLT entry. */
5638 if (h != NULL
5639 && (fdh = ((struct ppc_link_hash_entry *) h)->oh) != NULL
5640 && fdh->plt.offset != (bfd_vma) -1
5641 && (stub_entry = ppc_get_stub_entry (input_section, sec, fdh,
5642 rel, htab)) != NULL)
5643 {
5644 boolean can_plt_call = 0;
5645
5646 if (offset + 8 <= input_section->_cooked_size)
5647 {
5648 insn = bfd_get_32 (input_bfd, contents + offset + 4);
5649 if (insn == NOP
5650 || insn == CROR_151515 || insn == CROR_313131)
5651 {
5652 bfd_put_32 (input_bfd, (bfd_vma) LD_R2_40R1,
5653 contents + offset + 4);
5654 can_plt_call = 1;
5655 }
5656 }
5657
5658 if (!can_plt_call)
5659 {
5660 /* If this is a plain branch rather than a branch
5661 and link, don't require a nop. */
5662 insn = bfd_get_32 (input_bfd, contents + offset);
5663 if ((insn & 1) == 0)
5664 can_plt_call = 1;
5665 }
5666
5667 if (can_plt_call)
5668 {
5669 relocation = (stub_entry->stub_offset
5670 + stub_entry->stub_sec->output_offset
5671 + stub_entry->stub_sec->output_section->vma);
5672 addend = 0;
5673 unresolved_reloc = false;
5674 }
5675 }
5676
5677 if (h != NULL
5678 && h->root.type == bfd_link_hash_undefweak
5679 && relocation == 0
5680 && addend == 0)
5681 {
5682 /* Tweak calls to undefined weak functions to point at a
5683 blr. We can thus call a weak function without first
5684 checking whether the function is defined. We have a
5685 blr at the end of .sfpr. */
5686 BFD_ASSERT (htab->sfpr->_raw_size != 0);
5687 relocation = (htab->sfpr->_raw_size - 4
5688 + htab->sfpr->output_offset
5689 + htab->sfpr->output_section->vma);
5690 from = (offset
5691 + input_section->output_offset
5692 + input_section->output_section->vma);
5693
5694 /* But let's not be silly about it. If the blr isn't in
5695 reach, just go to the next instruction. */
5696 if (relocation - from + (1 << 25) >= (1 << 26)
5697 || htab->sfpr->_raw_size == 0)
5698 relocation = from + 4;
5699 }
5700 break;
5701 }
5702
5703 /* Set `addend'. */
5704 switch (r_type)
5705 {
5706 default:
5707 (*_bfd_error_handler)
5708 (_("%s: unknown relocation type %d for symbol %s"),
5709 bfd_archive_filename (input_bfd), (int) r_type, sym_name);
5710
5711 bfd_set_error (bfd_error_bad_value);
5712 ret = false;
5713 continue;
5714
5715 case R_PPC64_NONE:
5716 case R_PPC_GNU_VTINHERIT:
5717 case R_PPC_GNU_VTENTRY:
5718 continue;
5719
5720 /* GOT16 relocations. Like an ADDR16 using the symbol's
5721 address in the GOT as relocation value instead of the
5722 symbols value itself. Also, create a GOT entry for the
5723 symbol and put the symbol value there. */
5724 case R_PPC64_GOT16:
5725 case R_PPC64_GOT16_LO:
5726 case R_PPC64_GOT16_HI:
5727 case R_PPC64_GOT16_HA:
5728 case R_PPC64_GOT16_DS:
5729 case R_PPC64_GOT16_LO_DS:
5730 {
5731 /* Relocation is to the entry for this symbol in the global
5732 offset table. */
5733 bfd_vma off;
5734
5735 if (htab->sgot == NULL)
5736 abort ();
5737
5738 if (h != NULL)
5739 {
5740 boolean dyn;
5741
5742 off = h->got.offset;
5743 dyn = htab->elf.dynamic_sections_created;
5744 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info, h)
5745 || (info->shared
5746 && (info->symbolic
5747 || h->dynindx == -1
5748 || (h->elf_link_hash_flags
5749 & ELF_LINK_FORCED_LOCAL))
5750 && (h->elf_link_hash_flags
5751 & ELF_LINK_HASH_DEF_REGULAR)))
5752 {
5753 /* This is actually a static link, or it is a
5754 -Bsymbolic link and the symbol is defined
5755 locally, or the symbol was forced to be local
5756 because of a version file. We must initialize
5757 this entry in the global offset table. Since the
5758 offset must always be a multiple of 8, we use the
5759 least significant bit to record whether we have
5760 initialized it already.
5761
5762 When doing a dynamic link, we create a .rel.got
5763 relocation entry to initialize the value. This
5764 is done in the finish_dynamic_symbol routine. */
5765 if ((off & 1) != 0)
5766 off &= ~1;
5767 else
5768 {
5769 bfd_put_64 (output_bfd, relocation,
5770 htab->sgot->contents + off);
5771 h->got.offset |= 1;
5772 }
5773 }
5774 else
5775 unresolved_reloc = false;
5776 }
5777 else
5778 {
5779 if (local_got_offsets == NULL)
5780 abort ();
5781
5782 off = local_got_offsets[r_symndx];
5783
5784 /* The offset must always be a multiple of 8. We use
5785 the least significant bit to record whether we have
5786 already processed this entry. */
5787 if ((off & 1) != 0)
5788 off &= ~1;
5789 else
5790 {
5791 bfd_put_64 (output_bfd, relocation,
5792 htab->sgot->contents + off);
5793
5794 if (info->shared)
5795 {
5796 Elf_Internal_Rela outrel;
5797 Elf64_External_Rela *loc;
5798
5799 /* We need to generate a R_PPC64_RELATIVE reloc
5800 for the dynamic linker. */
5801 outrel.r_offset = (htab->sgot->output_section->vma
5802 + htab->sgot->output_offset
5803 + off);
5804 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
5805 outrel.r_addend = relocation;
5806 loc = (Elf64_External_Rela *) htab->srelgot->contents;
5807 loc += htab->srelgot->reloc_count++;
5808 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
5809 }
5810
5811 local_got_offsets[r_symndx] |= 1;
5812 }
5813 }
5814
5815 if (off >= (bfd_vma) -2)
5816 abort ();
5817
5818 relocation = htab->sgot->output_offset + off;
5819
5820 /* TOC base (r2) is TOC start plus 0x8000. */
5821 addend -= TOC_BASE_OFF;
5822 }
5823 break;
5824
5825 case R_PPC64_PLT16_HA:
5826 case R_PPC64_PLT16_HI:
5827 case R_PPC64_PLT16_LO:
5828 case R_PPC64_PLT32:
5829 case R_PPC64_PLT64:
5830 /* Relocation is to the entry for this symbol in the
5831 procedure linkage table. */
5832
5833 /* Resolve a PLT reloc against a local symbol directly,
5834 without using the procedure linkage table. */
5835 if (h == NULL)
5836 break;
5837
5838 if (h->plt.offset == (bfd_vma) -1
5839 || htab->splt == NULL)
5840 {
5841 /* We didn't make a PLT entry for this symbol. This
5842 happens when statically linking PIC code, or when
5843 using -Bsymbolic. */
5844 break;
5845 }
5846
5847 relocation = (htab->splt->output_section->vma
5848 + htab->splt->output_offset
5849 + h->plt.offset);
5850 unresolved_reloc = false;
5851 break;
5852
5853 /* TOC16 relocs. We want the offset relative to the TOC base,
5854 which is the address of the start of the TOC plus 0x8000.
5855 The TOC consists of sections .got, .toc, .tocbss, and .plt,
5856 in this order. */
5857 case R_PPC64_TOC16:
5858 case R_PPC64_TOC16_LO:
5859 case R_PPC64_TOC16_HI:
5860 case R_PPC64_TOC16_DS:
5861 case R_PPC64_TOC16_LO_DS:
5862 case R_PPC64_TOC16_HA:
5863 addend -= TOCstart + TOC_BASE_OFF;
5864 break;
5865
5866 /* Relocate against the beginning of the section. */
5867 case R_PPC64_SECTOFF:
5868 case R_PPC64_SECTOFF_LO:
5869 case R_PPC64_SECTOFF_HI:
5870 case R_PPC64_SECTOFF_DS:
5871 case R_PPC64_SECTOFF_LO_DS:
5872 case R_PPC64_SECTOFF_HA:
5873 if (sec != (asection *) 0)
5874 addend -= sec->output_section->vma;
5875 break;
5876
5877 case R_PPC64_REL14:
5878 case R_PPC64_REL14_BRNTAKEN:
5879 case R_PPC64_REL14_BRTAKEN:
5880 case R_PPC64_REL24:
5881 break;
5882
5883 /* Relocations that may need to be propagated if this is a
5884 dynamic object. */
5885 case R_PPC64_REL32:
5886 case R_PPC64_REL64:
5887 case R_PPC64_ADDR14:
5888 case R_PPC64_ADDR14_BRNTAKEN:
5889 case R_PPC64_ADDR14_BRTAKEN:
5890 case R_PPC64_ADDR16:
5891 case R_PPC64_ADDR16_DS:
5892 case R_PPC64_ADDR16_HA:
5893 case R_PPC64_ADDR16_HI:
5894 case R_PPC64_ADDR16_HIGHER:
5895 case R_PPC64_ADDR16_HIGHERA:
5896 case R_PPC64_ADDR16_HIGHEST:
5897 case R_PPC64_ADDR16_HIGHESTA:
5898 case R_PPC64_ADDR16_LO:
5899 case R_PPC64_ADDR16_LO_DS:
5900 case R_PPC64_ADDR24:
5901 case R_PPC64_ADDR30:
5902 case R_PPC64_ADDR32:
5903 case R_PPC64_ADDR64:
5904 case R_PPC64_UADDR16:
5905 case R_PPC64_UADDR32:
5906 case R_PPC64_UADDR64:
5907 /* r_symndx will be zero only for relocs against symbols
5908 from removed linkonce sections, or sections discarded by
5909 a linker script. */
5910 if (r_symndx == 0)
5911 break;
5912 /* Fall thru. */
5913
5914 case R_PPC64_TOC:
5915 if ((input_section->flags & SEC_ALLOC) == 0)
5916 break;
5917
5918 if (NO_OPD_RELOCS && is_opd)
5919 break;
5920
5921 if ((info->shared
5922 && (IS_ABSOLUTE_RELOC (r_type)
5923 || (h != NULL
5924 && h->dynindx != -1
5925 && (! info->symbolic
5926 || (h->elf_link_hash_flags
5927 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
5928 || (!info->shared
5929 && h != NULL
5930 && h->dynindx != -1
5931 && (h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
5932 && (((h->elf_link_hash_flags
5933 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
5934 && (h->elf_link_hash_flags
5935 & ELF_LINK_HASH_DEF_REGULAR) == 0)
5936 || h->root.type == bfd_link_hash_undefweak
5937 || h->root.type == bfd_link_hash_undefined)))
5938 {
5939 Elf_Internal_Rela outrel;
5940 boolean skip, relocate;
5941 asection *sreloc;
5942 Elf64_External_Rela *loc;
5943
5944 /* When generating a dynamic object, these relocations
5945 are copied into the output file to be resolved at run
5946 time. */
5947
5948 skip = false;
5949 relocate = false;
5950
5951 outrel.r_offset =
5952 _bfd_elf_section_offset (output_bfd, info, input_section,
5953 rel->r_offset);
5954 if (outrel.r_offset == (bfd_vma) -1)
5955 skip = true;
5956 else if (outrel.r_offset == (bfd_vma) -2)
5957 skip = true, relocate = true;
5958 outrel.r_offset += (input_section->output_section->vma
5959 + input_section->output_offset);
5960 outrel.r_addend = addend;
5961
5962 if (skip)
5963 memset (&outrel, 0, sizeof outrel);
5964 else if (h != NULL
5965 && h->dynindx != -1
5966 && !is_opd
5967 && (!IS_ABSOLUTE_RELOC (r_type)
5968 || !info->shared
5969 || !info->symbolic
5970 || (h->elf_link_hash_flags
5971 & ELF_LINK_HASH_DEF_REGULAR) == 0))
5972 outrel.r_info = ELF64_R_INFO (h->dynindx, r_type);
5973 else
5974 {
5975 /* This symbol is local, or marked to become local,
5976 or this is an opd section reloc which must point
5977 at a local function. */
5978 outrel.r_addend += relocation;
5979 relocate = true;
5980 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
5981 {
5982 if (is_opd && h != NULL)
5983 {
5984 /* Lie about opd entries. This case occurs
5985 when building shared libraries and we
5986 reference a function in another shared
5987 lib. The same thing happens for a weak
5988 definition in an application that's
5989 overridden by a strong definition in a
5990 shared lib. (I believe this is a generic
5991 bug in binutils handling of weak syms.)
5992 In these cases we won't use the opd
5993 entry in this lib. */
5994 unresolved_reloc = false;
5995 }
5996 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
5997 }
5998 else
5999 {
6000 long indx = 0;
6001
6002 if (bfd_is_abs_section (sec))
6003 ;
6004 else if (sec == NULL || sec->owner == NULL)
6005 {
6006 bfd_set_error (bfd_error_bad_value);
6007 return false;
6008 }
6009 else
6010 {
6011 asection *osec;
6012
6013 osec = sec->output_section;
6014 indx = elf_section_data (osec)->dynindx;
6015
6016 /* We are turning this relocation into one
6017 against a section symbol, so subtract out
6018 the output section's address but not the
6019 offset of the input section in the output
6020 section. */
6021 outrel.r_addend -= osec->vma;
6022 }
6023
6024 outrel.r_info = ELF64_R_INFO (indx, r_type);
6025 }
6026 }
6027
6028 sreloc = elf_section_data (input_section)->sreloc;
6029 if (sreloc == NULL)
6030 abort ();
6031
6032 loc = (Elf64_External_Rela *) sreloc->contents;
6033 loc += sreloc->reloc_count++;
6034 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
6035
6036 /* If this reloc is against an external symbol, it will
6037 be computed at runtime, so there's no need to do
6038 anything now. */
6039 if (! relocate)
6040 continue;
6041 }
6042 break;
6043
6044 case R_PPC64_COPY:
6045 case R_PPC64_GLOB_DAT:
6046 case R_PPC64_JMP_SLOT:
6047 case R_PPC64_RELATIVE:
6048 /* We shouldn't ever see these dynamic relocs in relocatable
6049 files. */
6050 /* Fall thru */
6051
6052 case R_PPC64_PLTGOT16:
6053 case R_PPC64_PLTGOT16_DS:
6054 case R_PPC64_PLTGOT16_HA:
6055 case R_PPC64_PLTGOT16_HI:
6056 case R_PPC64_PLTGOT16_LO:
6057 case R_PPC64_PLTGOT16_LO_DS:
6058 case R_PPC64_PLTREL32:
6059 case R_PPC64_PLTREL64:
6060 /* These ones haven't been implemented yet. */
6061
6062 (*_bfd_error_handler)
6063 (_("%s: Relocation %s is not supported for symbol %s."),
6064 bfd_archive_filename (input_bfd),
6065 ppc64_elf_howto_table[(int) r_type]->name, sym_name);
6066
6067 bfd_set_error (bfd_error_invalid_operation);
6068 ret = false;
6069 continue;
6070 }
6071
6072 /* Do any further special processing. */
6073 switch (r_type)
6074 {
6075 default:
6076 break;
6077
6078 case R_PPC64_ADDR16_HA:
6079 case R_PPC64_ADDR16_HIGHERA:
6080 case R_PPC64_ADDR16_HIGHESTA:
6081 case R_PPC64_PLT16_HA:
6082 case R_PPC64_TOC16_HA:
6083 case R_PPC64_SECTOFF_HA:
6084 /* It's just possible that this symbol is a weak symbol
6085 that's not actually defined anywhere. In that case,
6086 'sec' would be NULL, and we should leave the symbol
6087 alone (it will be set to zero elsewhere in the link). */
6088 if (sec != NULL)
6089 /* Add 0x10000 if sign bit in 0:15 is set. */
6090 addend += ((relocation + addend) & 0x8000) << 1;
6091 break;
6092
6093 case R_PPC64_ADDR16_DS:
6094 case R_PPC64_ADDR16_LO_DS:
6095 case R_PPC64_GOT16_DS:
6096 case R_PPC64_GOT16_LO_DS:
6097 case R_PPC64_PLT16_LO_DS:
6098 case R_PPC64_SECTOFF_DS:
6099 case R_PPC64_SECTOFF_LO_DS:
6100 case R_PPC64_TOC16_DS:
6101 case R_PPC64_TOC16_LO_DS:
6102 case R_PPC64_PLTGOT16_DS:
6103 case R_PPC64_PLTGOT16_LO_DS:
6104 if (((relocation + addend) & 3) != 0)
6105 {
6106 (*_bfd_error_handler)
6107 (_("%s: error: relocation %s not a multiple of 4"),
6108 bfd_archive_filename (input_bfd),
6109 ppc64_elf_howto_table[(int) r_type]->name);
6110 bfd_set_error (bfd_error_bad_value);
6111 ret = false;
6112 continue;
6113 }
6114 break;
6115
6116 case R_PPC64_REL14:
6117 case R_PPC64_REL14_BRNTAKEN:
6118 case R_PPC64_REL14_BRTAKEN:
6119 max_br_offset = 1 << 15;
6120 goto branch_check;
6121
6122 case R_PPC64_REL24:
6123 max_br_offset = 1 << 25;
6124
6125 branch_check:
6126 /* If the branch is out of reach, then redirect the
6127 call to the local stub for this function. */
6128 from = (offset
6129 + input_section->output_offset
6130 + input_section->output_section->vma);
6131 if (relocation + addend - from + max_br_offset >= 2 * max_br_offset
6132 && (stub_entry = ppc_get_stub_entry (input_section, sec, h,
6133 rel, htab)) != NULL)
6134 {
6135 /* Munge up the value and addend so that we call the stub
6136 rather than the procedure directly. */
6137 relocation = (stub_entry->stub_offset
6138 + stub_entry->stub_sec->output_offset
6139 + stub_entry->stub_sec->output_section->vma);
6140 addend = 0;
6141 }
6142 break;
6143 }
6144
6145 /* FIXME: Why do we allow debugging sections to escape this error?
6146 More importantly, why do we not emit dynamic relocs above in
6147 debugging sections (which are ! SEC_ALLOC)? If we had
6148 emitted the dynamic reloc, we could remove the fudge here. */
6149 if (unresolved_reloc
6150 && !(info->shared
6151 && (input_section->flags & SEC_DEBUGGING) != 0
6152 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0))
6153 {
6154 (*_bfd_error_handler)
6155 (_("%s(%s+0x%lx): unresolvable relocation against symbol `%s'"),
6156 bfd_archive_filename (input_bfd),
6157 bfd_get_section_name (input_bfd, input_section),
6158 (long) rel->r_offset,
6159 h->root.root.string);
6160 ret = false;
6161 }
6162
6163 r = _bfd_final_link_relocate (ppc64_elf_howto_table[(int) r_type],
6164 input_bfd,
6165 input_section,
6166 contents,
6167 offset,
6168 relocation,
6169 addend);
6170
6171 if (r != bfd_reloc_ok)
6172 {
6173 const char *name;
6174
6175 if (h != NULL)
6176 {
6177 if (h->root.type == bfd_link_hash_undefweak
6178 && ppc64_elf_howto_table[(int) r_type]->pc_relative)
6179 {
6180 /* Assume this is a call protected by other code that
6181 detects the symbol is undefined. If this is the case,
6182 we can safely ignore the overflow. If not, the
6183 program is hosed anyway, and a little warning isn't
6184 going to help. */
6185
6186 continue;
6187 }
6188
6189 name = h->root.root.string;
6190 }
6191 else
6192 {
6193 name = bfd_elf_string_from_elf_section (input_bfd,
6194 symtab_hdr->sh_link,
6195 sym->st_name);
6196 if (name == NULL)
6197 continue;
6198 if (*name == '\0')
6199 name = bfd_section_name (input_bfd, sec);
6200 }
6201
6202 if (r == bfd_reloc_overflow)
6203 {
6204 if (warned)
6205 continue;
6206 if (!((*info->callbacks->reloc_overflow)
6207 (info, name, ppc64_elf_howto_table[(int) r_type]->name,
6208 rel->r_addend, input_bfd, input_section, offset)))
6209 return false;
6210 }
6211 else
6212 {
6213 (*_bfd_error_handler)
6214 (_("%s(%s+0x%lx): reloc against `%s': error %d"),
6215 bfd_archive_filename (input_bfd),
6216 bfd_get_section_name (input_bfd, input_section),
6217 (long) rel->r_offset, name, (int) r);
6218 ret = false;
6219 }
6220 }
6221 }
6222
6223 return ret;
6224 }
6225
6226 /* Finish up dynamic symbol handling. We set the contents of various
6227 dynamic sections here. */
6228
6229 static boolean
6230 ppc64_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
6231 bfd *output_bfd;
6232 struct bfd_link_info *info;
6233 struct elf_link_hash_entry *h;
6234 Elf_Internal_Sym *sym;
6235 {
6236 struct ppc_link_hash_table *htab;
6237 bfd *dynobj;
6238
6239 htab = ppc_hash_table (info);
6240 dynobj = htab->elf.dynobj;
6241
6242 if (h->plt.offset != (bfd_vma) -1
6243 && ((struct ppc_link_hash_entry *) h)->is_func_descriptor)
6244 {
6245 Elf_Internal_Rela rela;
6246 Elf64_External_Rela *loc;
6247
6248 /* This symbol has an entry in the procedure linkage table. Set
6249 it up. */
6250
6251 if (htab->splt == NULL
6252 || htab->srelplt == NULL
6253 || htab->sglink == NULL)
6254 abort ();
6255
6256 /* Create a JMP_SLOT reloc to inform the dynamic linker to
6257 fill in the PLT entry. */
6258
6259 rela.r_offset = (htab->splt->output_section->vma
6260 + htab->splt->output_offset
6261 + h->plt.offset);
6262 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
6263 rela.r_addend = 0;
6264
6265 loc = (Elf64_External_Rela *) htab->srelplt->contents;
6266 loc += (h->plt.offset - PLT_INITIAL_ENTRY_SIZE) / PLT_ENTRY_SIZE;
6267 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
6268 }
6269
6270 if (h->got.offset != (bfd_vma) -1)
6271 {
6272 Elf_Internal_Rela rela;
6273 Elf64_External_Rela *loc;
6274
6275 /* This symbol has an entry in the global offset table. Set it
6276 up. */
6277
6278 if (htab->sgot == NULL || htab->srelgot == NULL)
6279 abort ();
6280
6281 rela.r_offset = (htab->sgot->output_section->vma
6282 + htab->sgot->output_offset
6283 + (h->got.offset &~ (bfd_vma) 1));
6284
6285 /* If this is a static link, or it is a -Bsymbolic link and the
6286 symbol is defined locally or was forced to be local because
6287 of a version file, we just want to emit a RELATIVE reloc.
6288 The entry in the global offset table will already have been
6289 initialized in the relocate_section function. */
6290 if (info->shared
6291 && (info->symbolic
6292 || h->dynindx == -1
6293 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL))
6294 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
6295 {
6296 BFD_ASSERT((h->got.offset & 1) != 0);
6297 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
6298 rela.r_addend = (h->root.u.def.value
6299 + h->root.u.def.section->output_section->vma
6300 + h->root.u.def.section->output_offset);
6301 }
6302 else
6303 {
6304 BFD_ASSERT ((h->got.offset & 1) == 0);
6305 bfd_put_64 (output_bfd, (bfd_vma) 0,
6306 htab->sgot->contents + h->got.offset);
6307 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_GLOB_DAT);
6308 rela.r_addend = 0;
6309 }
6310
6311 loc = (Elf64_External_Rela *) htab->srelgot->contents;
6312 loc += htab->srelgot->reloc_count++;
6313 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
6314 }
6315
6316 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
6317 {
6318 Elf_Internal_Rela rela;
6319 Elf64_External_Rela *loc;
6320
6321 /* This symbol needs a copy reloc. Set it up. */
6322
6323 if (h->dynindx == -1
6324 || (h->root.type != bfd_link_hash_defined
6325 && h->root.type != bfd_link_hash_defweak)
6326 || htab->srelbss == NULL)
6327 abort ();
6328
6329 rela.r_offset = (h->root.u.def.value
6330 + h->root.u.def.section->output_section->vma
6331 + h->root.u.def.section->output_offset);
6332 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
6333 rela.r_addend = 0;
6334 loc = (Elf64_External_Rela *) htab->srelbss->contents;
6335 loc += htab->srelbss->reloc_count++;
6336 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
6337 }
6338
6339 /* Mark some specially defined symbols as absolute. */
6340 if (strcmp (h->root.root.string, "_DYNAMIC") == 0)
6341 sym->st_shndx = SHN_ABS;
6342
6343 return true;
6344 }
6345
6346 /* Used to decide how to sort relocs in an optimal manner for the
6347 dynamic linker, before writing them out. */
6348
6349 static enum elf_reloc_type_class
6350 ppc64_elf_reloc_type_class (rela)
6351 const Elf_Internal_Rela *rela;
6352 {
6353 enum elf_ppc_reloc_type r_type;
6354
6355 r_type = (enum elf_ppc_reloc_type) ELF64_R_TYPE (rela->r_info);
6356 switch (r_type)
6357 {
6358 case R_PPC64_RELATIVE:
6359 return reloc_class_relative;
6360 case R_PPC64_JMP_SLOT:
6361 return reloc_class_plt;
6362 case R_PPC64_COPY:
6363 return reloc_class_copy;
6364 default:
6365 return reloc_class_normal;
6366 }
6367 }
6368
6369 /* Finish up the dynamic sections. */
6370
6371 static boolean
6372 ppc64_elf_finish_dynamic_sections (output_bfd, info)
6373 bfd *output_bfd;
6374 struct bfd_link_info *info;
6375 {
6376 struct ppc_link_hash_table *htab;
6377 bfd *dynobj;
6378 asection *sdyn;
6379
6380 htab = ppc_hash_table (info);
6381 dynobj = htab->elf.dynobj;
6382 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
6383
6384 if (htab->elf.dynamic_sections_created)
6385 {
6386 Elf64_External_Dyn *dyncon, *dynconend;
6387
6388 if (sdyn == NULL || htab->sgot == NULL)
6389 abort ();
6390
6391 dyncon = (Elf64_External_Dyn *) sdyn->contents;
6392 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
6393 for (; dyncon < dynconend; dyncon++)
6394 {
6395 Elf_Internal_Dyn dyn;
6396 asection *s;
6397
6398 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
6399
6400 switch (dyn.d_tag)
6401 {
6402 default:
6403 continue;
6404
6405 case DT_PPC64_GLINK:
6406 dyn.d_un.d_ptr = (htab->sglink->output_section->vma
6407 + htab->sglink->output_offset);
6408 break;
6409
6410 case DT_PPC64_OPD:
6411 s = bfd_get_section_by_name (output_bfd, ".opd");
6412 if (s != NULL)
6413 dyn.d_un.d_ptr = s->vma;
6414 break;
6415
6416 case DT_PPC64_OPDSZ:
6417 s = bfd_get_section_by_name (output_bfd, ".opd");
6418 if (s != NULL)
6419 dyn.d_un.d_val = s->_raw_size;
6420 break;
6421
6422 case DT_PLTGOT:
6423 dyn.d_un.d_ptr = (htab->splt->output_section->vma
6424 + htab->splt->output_offset);
6425 break;
6426
6427 case DT_JMPREL:
6428 dyn.d_un.d_ptr = (htab->srelplt->output_section->vma
6429 + htab->srelplt->output_offset);
6430 break;
6431
6432 case DT_PLTRELSZ:
6433 dyn.d_un.d_val = htab->srelplt->_raw_size;
6434 break;
6435
6436 case DT_RELASZ:
6437 /* Don't count procedure linkage table relocs in the
6438 overall reloc count. */
6439 if (htab->srelplt != NULL)
6440 dyn.d_un.d_val -= htab->srelplt->_raw_size;
6441 break;
6442 }
6443
6444 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
6445 }
6446 }
6447
6448 if (htab->sgot != NULL && htab->sgot->_raw_size != 0)
6449 {
6450 /* Fill in the first entry in the global offset table.
6451 We use it to hold the link-time TOCbase. */
6452 bfd_put_64 (output_bfd,
6453 elf_gp (output_bfd) + TOC_BASE_OFF,
6454 htab->sgot->contents);
6455
6456 /* Set .got entry size. */
6457 elf_section_data (htab->sgot->output_section)->this_hdr.sh_entsize = 8;
6458 }
6459
6460 if (htab->splt != NULL && htab->splt->_raw_size != 0)
6461 {
6462 /* Set .plt entry size. */
6463 elf_section_data (htab->splt->output_section)->this_hdr.sh_entsize
6464 = PLT_ENTRY_SIZE;
6465 }
6466
6467 return true;
6468 }
6469
6470 #define TARGET_LITTLE_SYM bfd_elf64_powerpcle_vec
6471 #define TARGET_LITTLE_NAME "elf64-powerpcle"
6472 #define TARGET_BIG_SYM bfd_elf64_powerpc_vec
6473 #define TARGET_BIG_NAME "elf64-powerpc"
6474 #define ELF_ARCH bfd_arch_powerpc
6475 #define ELF_MACHINE_CODE EM_PPC64
6476 #define ELF_MAXPAGESIZE 0x10000
6477 #define elf_info_to_howto ppc64_elf_info_to_howto
6478
6479 #ifdef EM_CYGNUS_POWERPC
6480 #define ELF_MACHINE_ALT1 EM_CYGNUS_POWERPC
6481 #endif
6482
6483 #ifdef EM_PPC_OLD
6484 #define ELF_MACHINE_ALT2 EM_PPC_OLD
6485 #endif
6486
6487 #define elf_backend_want_got_sym 0
6488 #define elf_backend_want_plt_sym 0
6489 #define elf_backend_plt_alignment 3
6490 #define elf_backend_plt_not_loaded 1
6491 #define elf_backend_got_symbol_offset 0
6492 #define elf_backend_got_header_size 8
6493 #define elf_backend_plt_header_size PLT_INITIAL_ENTRY_SIZE
6494 #define elf_backend_can_gc_sections 1
6495 #define elf_backend_can_refcount 1
6496 #define elf_backend_rela_normal 1
6497
6498 #define bfd_elf64_bfd_reloc_type_lookup ppc64_elf_reloc_type_lookup
6499 #define bfd_elf64_bfd_set_private_flags ppc64_elf_set_private_flags
6500 #define bfd_elf64_bfd_merge_private_bfd_data ppc64_elf_merge_private_bfd_data
6501 #define bfd_elf64_bfd_link_hash_table_create ppc64_elf_link_hash_table_create
6502 #define bfd_elf64_bfd_link_hash_table_free ppc64_elf_link_hash_table_free
6503 #define bfd_elf64_get_symbol_info ppc64_elf_get_symbol_info
6504
6505 #define elf_backend_object_p ppc64_elf_object_p
6506 #define elf_backend_section_from_shdr ppc64_elf_section_from_shdr
6507 #define elf_backend_create_dynamic_sections ppc64_elf_create_dynamic_sections
6508 #define elf_backend_copy_indirect_symbol ppc64_elf_copy_indirect_symbol
6509 #define elf_backend_check_relocs ppc64_elf_check_relocs
6510 #define elf_backend_gc_mark_hook ppc64_elf_gc_mark_hook
6511 #define elf_backend_gc_sweep_hook ppc64_elf_gc_sweep_hook
6512 #define elf_backend_adjust_dynamic_symbol ppc64_elf_adjust_dynamic_symbol
6513 #define elf_backend_hide_symbol ppc64_elf_hide_symbol
6514 #define elf_backend_always_size_sections ppc64_elf_func_desc_adjust
6515 #define elf_backend_size_dynamic_sections ppc64_elf_size_dynamic_sections
6516 #define elf_backend_fake_sections ppc64_elf_fake_sections
6517 #define elf_backend_relocate_section ppc64_elf_relocate_section
6518 #define elf_backend_finish_dynamic_symbol ppc64_elf_finish_dynamic_symbol
6519 #define elf_backend_reloc_type_class ppc64_elf_reloc_type_class
6520 #define elf_backend_finish_dynamic_sections ppc64_elf_finish_dynamic_sections
6521
6522 #include "elf64-target.h"
This page took 0.175032 seconds and 4 git commands to generate.