* elf-bfd.h (struct elf_reloc_cookie): Remove locsym_shndx,
[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 int top_index;
2040 asection **input_list;
2041
2042 /* Short-cuts to get to dynamic linker sections. */
2043 asection *sgot;
2044 asection *srelgot;
2045 asection *splt;
2046 asection *srelplt;
2047 asection *sdynbss;
2048 asection *srelbss;
2049 asection *sglink;
2050 asection *sfpr;
2051 asection *sbrlt;
2052 asection *srelbrlt;
2053
2054 /* Set on error. */
2055 unsigned int stub_error;
2056
2057 /* Flag set when small branches are detected. Used to
2058 select suitable defaults for the stub group size. */
2059 unsigned int has_14bit_branch;
2060
2061 /* Set if we detect a reference undefined weak symbol. */
2062 unsigned int have_undefweak;
2063
2064 /* Incremented every time we size stubs. */
2065 unsigned int stub_iteration;
2066
2067 /* Small local sym to section mapping cache. */
2068 struct sym_sec_cache sym_sec;
2069 };
2070
2071 static struct bfd_hash_entry *stub_hash_newfunc
2072 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
2073 static struct bfd_hash_entry *branch_hash_newfunc
2074 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
2075 static struct bfd_hash_entry *link_hash_newfunc
2076 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
2077 static struct bfd_link_hash_table *ppc64_elf_link_hash_table_create
2078 PARAMS ((bfd *));
2079 static void ppc64_elf_link_hash_table_free
2080 PARAMS ((struct bfd_link_hash_table *));
2081 static char *ppc_stub_name
2082 PARAMS ((const asection *, const asection *,
2083 const struct ppc_link_hash_entry *, const Elf_Internal_Rela *));
2084 static struct ppc_stub_hash_entry *ppc_get_stub_entry
2085 PARAMS ((const asection *, const asection *, struct elf_link_hash_entry *,
2086 const Elf_Internal_Rela *, struct ppc_link_hash_table *));
2087 static struct ppc_stub_hash_entry *ppc_add_stub
2088 PARAMS ((const char *, asection *, struct ppc_link_hash_table *));
2089 static boolean create_linkage_sections
2090 PARAMS ((bfd *, struct bfd_link_info *));
2091 static boolean create_got_section
2092 PARAMS ((bfd *, struct bfd_link_info *));
2093 static boolean ppc64_elf_create_dynamic_sections
2094 PARAMS ((bfd *, struct bfd_link_info *));
2095 static void ppc64_elf_copy_indirect_symbol
2096 PARAMS ((struct elf_link_hash_entry *, struct elf_link_hash_entry *));
2097 static boolean ppc64_elf_check_relocs
2098 PARAMS ((bfd *, struct bfd_link_info *, asection *,
2099 const Elf_Internal_Rela *));
2100 static asection * ppc64_elf_gc_mark_hook
2101 PARAMS ((asection *, struct bfd_link_info *, Elf_Internal_Rela *,
2102 struct elf_link_hash_entry *, Elf_Internal_Sym *));
2103 static boolean ppc64_elf_gc_sweep_hook
2104 PARAMS ((bfd *, struct bfd_link_info *, asection *,
2105 const Elf_Internal_Rela *));
2106 static boolean func_desc_adjust
2107 PARAMS ((struct elf_link_hash_entry *, PTR));
2108 static boolean ppc64_elf_func_desc_adjust
2109 PARAMS ((bfd *, struct bfd_link_info *));
2110 static boolean ppc64_elf_adjust_dynamic_symbol
2111 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
2112 static void ppc64_elf_hide_symbol
2113 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *, boolean));
2114 static boolean edit_opd
2115 PARAMS ((bfd *, struct bfd_link_info *));
2116 static boolean allocate_dynrelocs
2117 PARAMS ((struct elf_link_hash_entry *, PTR));
2118 static boolean readonly_dynrelocs
2119 PARAMS ((struct elf_link_hash_entry *, PTR));
2120 static enum elf_reloc_type_class ppc64_elf_reloc_type_class
2121 PARAMS ((const Elf_Internal_Rela *));
2122 static boolean ppc64_elf_size_dynamic_sections
2123 PARAMS ((bfd *, struct bfd_link_info *));
2124 static INLINE enum ppc_stub_type ppc_type_of_stub
2125 PARAMS ((asection *, const Elf_Internal_Rela *,
2126 struct ppc_link_hash_entry **, bfd_vma));
2127 static bfd_byte *build_plt_stub
2128 PARAMS ((bfd *, bfd_byte *, int, int));
2129 static boolean ppc_build_one_stub
2130 PARAMS ((struct bfd_hash_entry *, PTR));
2131 static boolean ppc_size_one_stub
2132 PARAMS ((struct bfd_hash_entry *, PTR));
2133 static void group_sections
2134 PARAMS ((struct ppc_link_hash_table *, bfd_size_type, boolean));
2135 static boolean ppc64_elf_fake_sections
2136 PARAMS ((bfd *, Elf64_Internal_Shdr *, asection *));
2137 static boolean ppc64_elf_relocate_section
2138 PARAMS ((bfd *, struct bfd_link_info *info, bfd *, asection *, bfd_byte *,
2139 Elf_Internal_Rela *relocs, Elf_Internal_Sym *local_syms,
2140 asection **));
2141 static boolean ppc64_elf_finish_dynamic_symbol
2142 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
2143 Elf_Internal_Sym *));
2144 static boolean ppc64_elf_finish_dynamic_sections
2145 PARAMS ((bfd *, struct bfd_link_info *));
2146
2147 /* Get the ppc64 ELF linker hash table from a link_info structure. */
2148
2149 #define ppc_hash_table(p) \
2150 ((struct ppc_link_hash_table *) ((p)->hash))
2151
2152 #define ppc_stub_hash_lookup(table, string, create, copy) \
2153 ((struct ppc_stub_hash_entry *) \
2154 bfd_hash_lookup ((table), (string), (create), (copy)))
2155
2156 #define ppc_branch_hash_lookup(table, string, create, copy) \
2157 ((struct ppc_branch_hash_entry *) \
2158 bfd_hash_lookup ((table), (string), (create), (copy)))
2159
2160 /* Create an entry in the stub hash table. */
2161
2162 static struct bfd_hash_entry *
2163 stub_hash_newfunc (entry, table, string)
2164 struct bfd_hash_entry *entry;
2165 struct bfd_hash_table *table;
2166 const char *string;
2167 {
2168 /* Allocate the structure if it has not already been allocated by a
2169 subclass. */
2170 if (entry == NULL)
2171 {
2172 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
2173 if (entry == NULL)
2174 return entry;
2175 }
2176
2177 /* Call the allocation method of the superclass. */
2178 entry = bfd_hash_newfunc (entry, table, string);
2179 if (entry != NULL)
2180 {
2181 struct ppc_stub_hash_entry *eh;
2182
2183 /* Initialize the local fields. */
2184 eh = (struct ppc_stub_hash_entry *) entry;
2185 eh->stub_sec = NULL;
2186 eh->stub_offset = 0;
2187 eh->target_value = 0;
2188 eh->target_section = NULL;
2189 eh->stub_type = ppc_stub_none;
2190 eh->h = NULL;
2191 eh->id_sec = NULL;
2192 }
2193
2194 return entry;
2195 }
2196
2197 /* Create an entry in the branch hash table. */
2198
2199 static struct bfd_hash_entry *
2200 branch_hash_newfunc (entry, table, string)
2201 struct bfd_hash_entry *entry;
2202 struct bfd_hash_table *table;
2203 const char *string;
2204 {
2205 /* Allocate the structure if it has not already been allocated by a
2206 subclass. */
2207 if (entry == NULL)
2208 {
2209 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
2210 if (entry == NULL)
2211 return entry;
2212 }
2213
2214 /* Call the allocation method of the superclass. */
2215 entry = bfd_hash_newfunc (entry, table, string);
2216 if (entry != NULL)
2217 {
2218 struct ppc_branch_hash_entry *eh;
2219
2220 /* Initialize the local fields. */
2221 eh = (struct ppc_branch_hash_entry *) entry;
2222 eh->offset = 0;
2223 eh->iter = 0;
2224 }
2225
2226 return entry;
2227 }
2228
2229 /* Create an entry in a ppc64 ELF linker hash table. */
2230
2231 static struct bfd_hash_entry *
2232 link_hash_newfunc (entry, table, string)
2233 struct bfd_hash_entry *entry;
2234 struct bfd_hash_table *table;
2235 const char *string;
2236 {
2237 /* Allocate the structure if it has not already been allocated by a
2238 subclass. */
2239 if (entry == NULL)
2240 {
2241 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
2242 if (entry == NULL)
2243 return entry;
2244 }
2245
2246 /* Call the allocation method of the superclass. */
2247 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
2248 if (entry != NULL)
2249 {
2250 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
2251
2252 eh->stub_cache = NULL;
2253 eh->dyn_relocs = NULL;
2254 eh->oh = NULL;
2255 eh->is_func = 0;
2256 eh->is_func_descriptor = 0;
2257 eh->is_entry = 0;
2258 }
2259
2260 return entry;
2261 }
2262
2263 /* Create a ppc64 ELF linker hash table. */
2264
2265 static struct bfd_link_hash_table *
2266 ppc64_elf_link_hash_table_create (abfd)
2267 bfd *abfd;
2268 {
2269 struct ppc_link_hash_table *htab;
2270 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
2271
2272 htab = (struct ppc_link_hash_table *) bfd_malloc (amt);
2273 if (htab == NULL)
2274 return NULL;
2275
2276 if (! _bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc))
2277 {
2278 free (htab);
2279 return NULL;
2280 }
2281
2282 /* Init the stub hash table too. */
2283 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc))
2284 return NULL;
2285
2286 /* And the branch hash table. */
2287 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc))
2288 return NULL;
2289
2290 htab->stub_bfd = NULL;
2291 htab->add_stub_section = NULL;
2292 htab->layout_sections_again = NULL;
2293 htab->stub_group = NULL;
2294 htab->sgot = NULL;
2295 htab->srelgot = NULL;
2296 htab->splt = NULL;
2297 htab->srelplt = NULL;
2298 htab->sdynbss = NULL;
2299 htab->srelbss = NULL;
2300 htab->sglink = NULL;
2301 htab->sfpr = NULL;
2302 htab->sbrlt = NULL;
2303 htab->srelbrlt = NULL;
2304 htab->stub_error = 0;
2305 htab->has_14bit_branch = 0;
2306 htab->have_undefweak = 0;
2307 htab->stub_iteration = 0;
2308 htab->sym_sec.abfd = NULL;
2309
2310 return &htab->elf.root;
2311 }
2312
2313 /* Free the derived linker hash table. */
2314
2315 static void
2316 ppc64_elf_link_hash_table_free (hash)
2317 struct bfd_link_hash_table *hash;
2318 {
2319 struct ppc_link_hash_table *ret = (struct ppc_link_hash_table *) hash;
2320
2321 bfd_hash_table_free (&ret->stub_hash_table);
2322 bfd_hash_table_free (&ret->branch_hash_table);
2323 _bfd_generic_link_hash_table_free (hash);
2324 }
2325
2326 /* Build a name for an entry in the stub hash table. */
2327
2328 static char *
2329 ppc_stub_name (input_section, sym_sec, h, rel)
2330 const asection *input_section;
2331 const asection *sym_sec;
2332 const struct ppc_link_hash_entry *h;
2333 const Elf_Internal_Rela *rel;
2334 {
2335 char *stub_name;
2336 bfd_size_type len;
2337
2338 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
2339 offsets from a sym as a branch target? In fact, we could
2340 probably assume the addend is always zero. */
2341 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
2342
2343 if (h)
2344 {
2345 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
2346 stub_name = bfd_malloc (len);
2347 if (stub_name != NULL)
2348 {
2349 sprintf (stub_name, "%08x_%s+%x",
2350 input_section->id & 0xffffffff,
2351 h->elf.root.root.string,
2352 (int) rel->r_addend & 0xffffffff);
2353 }
2354 }
2355 else
2356 {
2357 len = 8 + 1 + 8 + 1 + 8 + 1 + 16 + 1;
2358 stub_name = bfd_malloc (len);
2359 if (stub_name != NULL)
2360 {
2361 sprintf (stub_name, "%08x_%x:%x+%x",
2362 input_section->id & 0xffffffff,
2363 sym_sec->id & 0xffffffff,
2364 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
2365 (int) rel->r_addend & 0xffffffff);
2366 }
2367 }
2368 return stub_name;
2369 }
2370
2371 /* Look up an entry in the stub hash. Stub entries are cached because
2372 creating the stub name takes a bit of time. */
2373
2374 static struct ppc_stub_hash_entry *
2375 ppc_get_stub_entry (input_section, sym_sec, hash, rel, htab)
2376 const asection *input_section;
2377 const asection *sym_sec;
2378 struct elf_link_hash_entry *hash;
2379 const Elf_Internal_Rela *rel;
2380 struct ppc_link_hash_table *htab;
2381 {
2382 struct ppc_stub_hash_entry *stub_entry;
2383 struct ppc_link_hash_entry *h = (struct ppc_link_hash_entry *) hash;
2384 const asection *id_sec;
2385
2386 /* If this input section is part of a group of sections sharing one
2387 stub section, then use the id of the first section in the group.
2388 Stub names need to include a section id, as there may well be
2389 more than one stub used to reach say, printf, and we need to
2390 distinguish between them. */
2391 id_sec = htab->stub_group[input_section->id].link_sec;
2392
2393 if (h != NULL && h->stub_cache != NULL
2394 && h->stub_cache->h == h
2395 && h->stub_cache->id_sec == id_sec)
2396 {
2397 stub_entry = h->stub_cache;
2398 }
2399 else
2400 {
2401 char *stub_name;
2402
2403 stub_name = ppc_stub_name (id_sec, sym_sec, h, rel);
2404 if (stub_name == NULL)
2405 return NULL;
2406
2407 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
2408 stub_name, false, false);
2409 if (h != NULL)
2410 h->stub_cache = stub_entry;
2411
2412 free (stub_name);
2413 }
2414
2415 return stub_entry;
2416 }
2417
2418 /* Add a new stub entry to the stub hash. Not all fields of the new
2419 stub entry are initialised. */
2420
2421 static struct ppc_stub_hash_entry *
2422 ppc_add_stub (stub_name, section, htab)
2423 const char *stub_name;
2424 asection *section;
2425 struct ppc_link_hash_table *htab;
2426 {
2427 asection *link_sec;
2428 asection *stub_sec;
2429 struct ppc_stub_hash_entry *stub_entry;
2430
2431 link_sec = htab->stub_group[section->id].link_sec;
2432 stub_sec = htab->stub_group[section->id].stub_sec;
2433 if (stub_sec == NULL)
2434 {
2435 stub_sec = htab->stub_group[link_sec->id].stub_sec;
2436 if (stub_sec == NULL)
2437 {
2438 size_t namelen;
2439 bfd_size_type len;
2440 char *s_name;
2441
2442 namelen = strlen (link_sec->name);
2443 len = namelen + sizeof (STUB_SUFFIX);
2444 s_name = bfd_alloc (htab->stub_bfd, len);
2445 if (s_name == NULL)
2446 return NULL;
2447
2448 memcpy (s_name, link_sec->name, namelen);
2449 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
2450 stub_sec = (*htab->add_stub_section) (s_name, link_sec);
2451 if (stub_sec == NULL)
2452 return NULL;
2453 htab->stub_group[link_sec->id].stub_sec = stub_sec;
2454 }
2455 htab->stub_group[section->id].stub_sec = stub_sec;
2456 }
2457
2458 /* Enter this entry into the linker stub hash table. */
2459 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
2460 true, false);
2461 if (stub_entry == NULL)
2462 {
2463 (*_bfd_error_handler) (_("%s: cannot create stub entry %s"),
2464 bfd_archive_filename (section->owner),
2465 stub_name);
2466 return NULL;
2467 }
2468
2469 stub_entry->stub_sec = stub_sec;
2470 stub_entry->stub_offset = 0;
2471 stub_entry->id_sec = link_sec;
2472 return stub_entry;
2473 }
2474
2475 /* Create sections for linker generated code. */
2476
2477 static boolean
2478 create_linkage_sections (dynobj, info)
2479 bfd *dynobj;
2480 struct bfd_link_info *info;
2481 {
2482 struct ppc_link_hash_table *htab;
2483 flagword flags;
2484
2485 htab = ppc_hash_table (info);
2486
2487 /* Create .sfpr for code to save and restore fp regs. */
2488 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
2489 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2490 htab->sfpr = bfd_make_section_anyway (dynobj, ".sfpr");
2491 if (htab->sfpr == NULL
2492 || ! bfd_set_section_flags (dynobj, htab->sfpr, flags)
2493 || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
2494 return false;
2495
2496 /* Create .glink for lazy dynamic linking support. */
2497 htab->sglink = bfd_make_section_anyway (dynobj, ".glink");
2498 if (htab->sglink == NULL
2499 || ! bfd_set_section_flags (dynobj, htab->sglink, flags)
2500 || ! bfd_set_section_alignment (dynobj, htab->sglink, 2))
2501 return false;
2502
2503 /* Create .branch_lt for plt_branch stubs. */
2504 flags = (SEC_ALLOC | SEC_LOAD
2505 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2506 htab->sbrlt = bfd_make_section_anyway (dynobj, ".branch_lt");
2507 if (htab->sbrlt == NULL
2508 || ! bfd_set_section_flags (dynobj, htab->sbrlt, flags)
2509 || ! bfd_set_section_alignment (dynobj, htab->sbrlt, 3))
2510 return false;
2511
2512 if (info->shared)
2513 {
2514 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
2515 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2516 htab->srelbrlt = bfd_make_section_anyway (dynobj, ".rela.branch_lt");
2517 if (!htab->srelbrlt
2518 || ! bfd_set_section_flags (dynobj, htab->srelbrlt, flags)
2519 || ! bfd_set_section_alignment (dynobj, htab->srelbrlt, 3))
2520 return false;
2521 }
2522 return true;
2523 }
2524
2525 /* Create .got and .rela.got sections in DYNOBJ, and set up
2526 shortcuts to them in our hash table. */
2527
2528 static boolean
2529 create_got_section (dynobj, info)
2530 bfd *dynobj;
2531 struct bfd_link_info *info;
2532 {
2533 struct ppc_link_hash_table *htab;
2534
2535 if (! _bfd_elf_create_got_section (dynobj, info))
2536 return false;
2537
2538 htab = ppc_hash_table (info);
2539 htab->sgot = bfd_get_section_by_name (dynobj, ".got");
2540 if (!htab->sgot)
2541 abort ();
2542
2543 htab->srelgot = bfd_make_section (dynobj, ".rela.got");
2544 if (!htab->srelgot
2545 || ! bfd_set_section_flags (dynobj, htab->srelgot,
2546 (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
2547 | SEC_IN_MEMORY | SEC_LINKER_CREATED
2548 | SEC_READONLY))
2549 || ! bfd_set_section_alignment (dynobj, htab->srelgot, 3))
2550 return false;
2551 return true;
2552 }
2553
2554 /* Create the dynamic sections, and set up shortcuts. */
2555
2556 static boolean
2557 ppc64_elf_create_dynamic_sections (dynobj, info)
2558 bfd *dynobj;
2559 struct bfd_link_info *info;
2560 {
2561 struct ppc_link_hash_table *htab;
2562
2563 htab = ppc_hash_table (info);
2564 if (!htab->sgot && !create_got_section (dynobj, info))
2565 return false;
2566
2567 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
2568 return false;
2569
2570 htab->splt = bfd_get_section_by_name (dynobj, ".plt");
2571 htab->srelplt = bfd_get_section_by_name (dynobj, ".rela.plt");
2572 htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
2573 if (!info->shared)
2574 htab->srelbss = bfd_get_section_by_name (dynobj, ".rela.bss");
2575
2576 if (!htab->splt || !htab->srelplt || !htab->sdynbss
2577 || (!info->shared && !htab->srelbss))
2578 abort ();
2579
2580 return true;
2581 }
2582
2583 /* Copy the extra info we tack onto an elf_link_hash_entry. */
2584
2585 static void
2586 ppc64_elf_copy_indirect_symbol (dir, ind)
2587 struct elf_link_hash_entry *dir, *ind;
2588 {
2589 struct ppc_link_hash_entry *edir, *eind;
2590
2591 edir = (struct ppc_link_hash_entry *) dir;
2592 eind = (struct ppc_link_hash_entry *) ind;
2593
2594 if (eind->dyn_relocs != NULL)
2595 {
2596 if (edir->dyn_relocs != NULL)
2597 {
2598 struct ppc_dyn_relocs **pp;
2599 struct ppc_dyn_relocs *p;
2600
2601 if (ind->root.type == bfd_link_hash_indirect)
2602 abort ();
2603
2604 /* Add reloc counts against the weak sym to the strong sym
2605 list. Merge any entries against the same section. */
2606 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
2607 {
2608 struct ppc_dyn_relocs *q;
2609
2610 for (q = edir->dyn_relocs; q != NULL; q = q->next)
2611 if (q->sec == p->sec)
2612 {
2613 q->pc_count += p->pc_count;
2614 q->count += p->count;
2615 *pp = p->next;
2616 break;
2617 }
2618 if (q == NULL)
2619 pp = &p->next;
2620 }
2621 *pp = edir->dyn_relocs;
2622 }
2623
2624 edir->dyn_relocs = eind->dyn_relocs;
2625 eind->dyn_relocs = NULL;
2626 }
2627
2628 edir->is_func |= eind->is_func;
2629 edir->is_func_descriptor |= eind->is_func_descriptor;
2630 edir->is_entry |= eind->is_entry;
2631
2632 _bfd_elf_link_hash_copy_indirect (dir, ind);
2633 }
2634
2635 /* Set a flag, used by ppc64_elf_gc_mark_hook, on the entry symbol and
2636 symbols undefined on the command-line. */
2637
2638 boolean
2639 ppc64_elf_mark_entry_syms (info)
2640 struct bfd_link_info *info;
2641 {
2642 struct ppc_link_hash_table *htab;
2643 struct bfd_sym_chain *sym;
2644
2645 htab = ppc_hash_table (info);
2646 for (sym = info->gc_sym_list; sym; sym = sym->next)
2647 {
2648 struct elf_link_hash_entry *h;
2649
2650 h = elf_link_hash_lookup (&htab->elf, sym->name, false, false, false);
2651 if (h != NULL)
2652 ((struct ppc_link_hash_entry *) h)->is_entry = 1;
2653 }
2654 return true;
2655 }
2656
2657 /* Look through the relocs for a section during the first phase, and
2658 calculate needed space in the global offset table, procedure
2659 linkage table, and dynamic reloc sections. */
2660
2661 static boolean
2662 ppc64_elf_check_relocs (abfd, info, sec, relocs)
2663 bfd *abfd;
2664 struct bfd_link_info *info;
2665 asection *sec;
2666 const Elf_Internal_Rela *relocs;
2667 {
2668 struct ppc_link_hash_table *htab;
2669 Elf_Internal_Shdr *symtab_hdr;
2670 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
2671 const Elf_Internal_Rela *rel;
2672 const Elf_Internal_Rela *rel_end;
2673 asection *sreloc;
2674 asection **opd_sym_map;
2675
2676 if (info->relocateable)
2677 return true;
2678
2679 htab = ppc_hash_table (info);
2680 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2681
2682 sym_hashes = elf_sym_hashes (abfd);
2683 sym_hashes_end = (sym_hashes
2684 + symtab_hdr->sh_size / sizeof (Elf64_External_Sym)
2685 - symtab_hdr->sh_info);
2686
2687 sreloc = NULL;
2688 opd_sym_map = NULL;
2689 if (strcmp (bfd_get_section_name (abfd, sec), ".opd") == 0)
2690 {
2691 /* Garbage collection needs some extra help with .opd sections.
2692 We don't want to necessarily keep everything referenced by
2693 relocs in .opd, as that would keep all functions. Instead,
2694 if we reference an .opd symbol (a function descriptor), we
2695 want to keep the function code symbol's section. This is
2696 easy for global symbols, but for local syms we need to keep
2697 information about the associated function section. Later, if
2698 edit_opd deletes entries, we'll use this array to adjust
2699 local syms in .opd. */
2700 union opd_info {
2701 asection *func_section;
2702 long entry_adjust;
2703 };
2704 bfd_size_type amt;
2705
2706 amt = sec->_raw_size * sizeof (union opd_info) / 24;
2707 opd_sym_map = (asection **) bfd_zalloc (abfd, amt);
2708 if (opd_sym_map == NULL)
2709 return false;
2710 elf_section_data (sec)->tdata = opd_sym_map;
2711 }
2712
2713 if (htab->elf.dynobj == NULL)
2714 htab->elf.dynobj = abfd;
2715 if (htab->sfpr == NULL
2716 && !create_linkage_sections (htab->elf.dynobj, info))
2717 return false;
2718
2719 rel_end = relocs + sec->reloc_count;
2720 for (rel = relocs; rel < rel_end; rel++)
2721 {
2722 unsigned long r_symndx;
2723 struct elf_link_hash_entry *h;
2724 enum elf_ppc_reloc_type r_type;
2725
2726 r_symndx = ELF64_R_SYM (rel->r_info);
2727 if (r_symndx < symtab_hdr->sh_info)
2728 h = NULL;
2729 else
2730 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2731
2732 r_type = (enum elf_ppc_reloc_type) ELF64_R_TYPE (rel->r_info);
2733 switch (r_type)
2734 {
2735 /* GOT16 relocations */
2736 case R_PPC64_GOT16:
2737 case R_PPC64_GOT16_DS:
2738 case R_PPC64_GOT16_HA:
2739 case R_PPC64_GOT16_HI:
2740 case R_PPC64_GOT16_LO:
2741 case R_PPC64_GOT16_LO_DS:
2742
2743 /* This symbol requires a global offset table entry. */
2744 if (htab->sgot == NULL
2745 && !create_got_section (htab->elf.dynobj, info))
2746 return false;
2747
2748 if (h != NULL)
2749 {
2750 h->got.refcount += 1;
2751 }
2752 else
2753 {
2754 bfd_signed_vma *local_got_refcounts;
2755
2756 /* This is a global offset table entry for a local symbol. */
2757 local_got_refcounts = elf_local_got_refcounts (abfd);
2758 if (local_got_refcounts == NULL)
2759 {
2760 bfd_size_type size;
2761
2762 size = symtab_hdr->sh_info;
2763 size *= sizeof (bfd_signed_vma);
2764 local_got_refcounts = ((bfd_signed_vma *)
2765 bfd_zalloc (abfd, size));
2766 if (local_got_refcounts == NULL)
2767 return false;
2768 elf_local_got_refcounts (abfd) = local_got_refcounts;
2769 }
2770 local_got_refcounts[r_symndx] += 1;
2771 }
2772 break;
2773
2774 case R_PPC64_PLT16_HA:
2775 case R_PPC64_PLT16_HI:
2776 case R_PPC64_PLT16_LO:
2777 case R_PPC64_PLT32:
2778 case R_PPC64_PLT64:
2779 /* This symbol requires a procedure linkage table entry. We
2780 actually build the entry in adjust_dynamic_symbol,
2781 because this might be a case of linking PIC code without
2782 linking in any dynamic objects, in which case we don't
2783 need to generate a procedure linkage table after all. */
2784 if (h == NULL)
2785 {
2786 /* It does not make sense to have a procedure linkage
2787 table entry for a local symbol. */
2788 bfd_set_error (bfd_error_bad_value);
2789 return false;
2790 }
2791
2792 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
2793 h->plt.refcount += 1;
2794 ((struct ppc_link_hash_entry *) h)->is_func = 1;
2795 break;
2796
2797 /* The following relocations don't need to propagate the
2798 relocation if linking a shared object since they are
2799 section relative. */
2800 case R_PPC64_SECTOFF:
2801 case R_PPC64_SECTOFF_LO:
2802 case R_PPC64_SECTOFF_HI:
2803 case R_PPC64_SECTOFF_HA:
2804 case R_PPC64_SECTOFF_DS:
2805 case R_PPC64_SECTOFF_LO_DS:
2806 case R_PPC64_TOC16:
2807 case R_PPC64_TOC16_LO:
2808 case R_PPC64_TOC16_HI:
2809 case R_PPC64_TOC16_HA:
2810 case R_PPC64_TOC16_DS:
2811 case R_PPC64_TOC16_LO_DS:
2812 break;
2813
2814 /* This relocation describes the C++ object vtable hierarchy.
2815 Reconstruct it for later use during GC. */
2816 case R_PPC64_GNU_VTINHERIT:
2817 if (!_bfd_elf64_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
2818 return false;
2819 break;
2820
2821 /* This relocation describes which C++ vtable entries are actually
2822 used. Record for later use during GC. */
2823 case R_PPC64_GNU_VTENTRY:
2824 if (!_bfd_elf64_gc_record_vtentry (abfd, sec, h, rel->r_addend))
2825 return false;
2826 break;
2827
2828 case R_PPC64_REL14:
2829 case R_PPC64_REL14_BRTAKEN:
2830 case R_PPC64_REL14_BRNTAKEN:
2831 htab->has_14bit_branch = 1;
2832 /* Fall through. */
2833
2834 case R_PPC64_REL24:
2835 if (h != NULL
2836 && h->root.root.string[0] == '.'
2837 && h->root.root.string[1] != 0)
2838 {
2839 /* We may need a .plt entry if the function this reloc
2840 refers to is in a shared lib. */
2841 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
2842 h->plt.refcount += 1;
2843 ((struct ppc_link_hash_entry *) h)->is_func = 1;
2844 }
2845 break;
2846
2847 case R_PPC64_ADDR64:
2848 if (opd_sym_map != NULL
2849 && h != NULL
2850 && h->root.root.string[0] == '.'
2851 && h->root.root.string[1] != 0)
2852 {
2853 struct elf_link_hash_entry *fdh;
2854
2855 fdh = elf_link_hash_lookup (&htab->elf, h->root.root.string + 1,
2856 false, false, false);
2857 if (fdh != NULL)
2858 {
2859 ((struct ppc_link_hash_entry *) fdh)->is_func_descriptor = 1;
2860 ((struct ppc_link_hash_entry *) fdh)->oh = h;
2861 ((struct ppc_link_hash_entry *) h)->is_func = 1;
2862 ((struct ppc_link_hash_entry *) h)->oh = fdh;
2863 }
2864 }
2865 if (opd_sym_map != NULL
2866 && h == NULL
2867 && rel + 1 < rel_end
2868 && ((enum elf_ppc_reloc_type) ELF64_R_TYPE ((rel + 1)->r_info)
2869 == R_PPC64_TOC))
2870 {
2871 asection *s;
2872
2873 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec, sec,
2874 r_symndx);
2875 if (s == NULL)
2876 return false;
2877 else if (s != sec)
2878 opd_sym_map[rel->r_offset / 24] = s;
2879 }
2880 /* Fall through. */
2881
2882 case R_PPC64_REL64:
2883 case R_PPC64_REL32:
2884 case R_PPC64_ADDR14:
2885 case R_PPC64_ADDR14_BRNTAKEN:
2886 case R_PPC64_ADDR14_BRTAKEN:
2887 case R_PPC64_ADDR16:
2888 case R_PPC64_ADDR16_DS:
2889 case R_PPC64_ADDR16_HA:
2890 case R_PPC64_ADDR16_HI:
2891 case R_PPC64_ADDR16_HIGHER:
2892 case R_PPC64_ADDR16_HIGHERA:
2893 case R_PPC64_ADDR16_HIGHEST:
2894 case R_PPC64_ADDR16_HIGHESTA:
2895 case R_PPC64_ADDR16_LO:
2896 case R_PPC64_ADDR16_LO_DS:
2897 case R_PPC64_ADDR24:
2898 case R_PPC64_ADDR30:
2899 case R_PPC64_ADDR32:
2900 case R_PPC64_UADDR16:
2901 case R_PPC64_UADDR32:
2902 case R_PPC64_UADDR64:
2903 case R_PPC64_TOC:
2904 /* Don't propagate .opd relocs. */
2905 if (NO_OPD_RELOCS && opd_sym_map != NULL)
2906 break;
2907
2908 /* If we are creating a shared library, and this is a reloc
2909 against a global symbol, or a non PC relative reloc
2910 against a local symbol, then we need to copy the reloc
2911 into the shared library. However, if we are linking with
2912 -Bsymbolic, we do not need to copy a reloc against a
2913 global symbol which is defined in an object we are
2914 including in the link (i.e., DEF_REGULAR is set). At
2915 this point we have not seen all the input files, so it is
2916 possible that DEF_REGULAR is not set now but will be set
2917 later (it is never cleared). In case of a weak definition,
2918 DEF_REGULAR may be cleared later by a strong definition in
2919 a shared library. We account for that possibility below by
2920 storing information in the relocs_copied field of the hash
2921 table entry. A similar situation occurs when creating
2922 shared libraries and symbol visibility changes render the
2923 symbol local.
2924
2925 If on the other hand, we are creating an executable, we
2926 may need to keep relocations for symbols satisfied by a
2927 dynamic library if we manage to avoid copy relocs for the
2928 symbol. */
2929 if ((info->shared
2930 && (sec->flags & SEC_ALLOC) != 0
2931 && (IS_ABSOLUTE_RELOC (r_type)
2932 || (h != NULL
2933 && (! info->symbolic
2934 || h->root.type == bfd_link_hash_defweak
2935 || (h->elf_link_hash_flags
2936 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
2937 || (!info->shared
2938 && (sec->flags & SEC_ALLOC) != 0
2939 && h != NULL
2940 && (h->root.type == bfd_link_hash_defweak
2941 || (h->elf_link_hash_flags
2942 & ELF_LINK_HASH_DEF_REGULAR) == 0)))
2943 {
2944 struct ppc_dyn_relocs *p;
2945 struct ppc_dyn_relocs **head;
2946
2947 /* We must copy these reloc types into the output file.
2948 Create a reloc section in dynobj and make room for
2949 this reloc. */
2950 if (sreloc == NULL)
2951 {
2952 const char *name;
2953 bfd *dynobj;
2954
2955 name = (bfd_elf_string_from_elf_section
2956 (abfd,
2957 elf_elfheader (abfd)->e_shstrndx,
2958 elf_section_data (sec)->rel_hdr.sh_name));
2959 if (name == NULL)
2960 return false;
2961
2962 if (strncmp (name, ".rela", 5) != 0
2963 || strcmp (bfd_get_section_name (abfd, sec),
2964 name + 5) != 0)
2965 {
2966 (*_bfd_error_handler)
2967 (_("%s: bad relocation section name `%s\'"),
2968 bfd_archive_filename (abfd), name);
2969 bfd_set_error (bfd_error_bad_value);
2970 }
2971
2972 dynobj = htab->elf.dynobj;
2973 sreloc = bfd_get_section_by_name (dynobj, name);
2974 if (sreloc == NULL)
2975 {
2976 flagword flags;
2977
2978 sreloc = bfd_make_section (dynobj, name);
2979 flags = (SEC_HAS_CONTENTS | SEC_READONLY
2980 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2981 if ((sec->flags & SEC_ALLOC) != 0)
2982 flags |= SEC_ALLOC | SEC_LOAD;
2983 if (sreloc == NULL
2984 || ! bfd_set_section_flags (dynobj, sreloc, flags)
2985 || ! bfd_set_section_alignment (dynobj, sreloc, 3))
2986 return false;
2987 }
2988 elf_section_data (sec)->sreloc = sreloc;
2989 }
2990
2991 /* If this is a global symbol, we count the number of
2992 relocations we need for this symbol. */
2993 if (h != NULL)
2994 {
2995 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
2996 }
2997 else
2998 {
2999 /* Track dynamic relocs needed for local syms too.
3000 We really need local syms available to do this
3001 easily. Oh well. */
3002
3003 asection *s;
3004 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
3005 sec, r_symndx);
3006 if (s == NULL)
3007 return false;
3008
3009 head = ((struct ppc_dyn_relocs **)
3010 &elf_section_data (s)->local_dynrel);
3011 }
3012
3013 p = *head;
3014 if (p == NULL || p->sec != sec)
3015 {
3016 p = ((struct ppc_dyn_relocs *)
3017 bfd_alloc (htab->elf.dynobj,
3018 (bfd_size_type) sizeof *p));
3019 if (p == NULL)
3020 return false;
3021 p->next = *head;
3022 *head = p;
3023 p->sec = sec;
3024 p->count = 0;
3025 p->pc_count = 0;
3026 }
3027
3028 p->count += 1;
3029 if (!IS_ABSOLUTE_RELOC (r_type))
3030 p->pc_count += 1;
3031 }
3032 break;
3033
3034 default:
3035 break;
3036 }
3037 }
3038
3039 return true;
3040 }
3041
3042 /* Return the section that should be marked against GC for a given
3043 relocation. */
3044
3045 static asection *
3046 ppc64_elf_gc_mark_hook (sec, info, rel, h, sym)
3047 asection *sec;
3048 struct bfd_link_info *info ATTRIBUTE_UNUSED;
3049 Elf_Internal_Rela *rel;
3050 struct elf_link_hash_entry *h;
3051 Elf_Internal_Sym *sym;
3052 {
3053 asection *rsec = NULL;
3054
3055 if (h != NULL)
3056 {
3057 enum elf_ppc_reloc_type r_type;
3058 struct ppc_link_hash_entry *fdh;
3059
3060 r_type = (enum elf_ppc_reloc_type) ELF64_R_TYPE (rel->r_info);
3061 switch (r_type)
3062 {
3063 case R_PPC64_GNU_VTINHERIT:
3064 case R_PPC64_GNU_VTENTRY:
3065 break;
3066
3067 default:
3068 switch (h->root.type)
3069 {
3070 case bfd_link_hash_defined:
3071 case bfd_link_hash_defweak:
3072 fdh = (struct ppc_link_hash_entry *) h;
3073
3074 /* Function descriptor syms cause the associated
3075 function code sym section to be marked. */
3076 if (fdh->is_func_descriptor)
3077 rsec = fdh->oh->root.u.def.section;
3078
3079 /* Function entry syms return NULL if they are in .opd
3080 and are not ._start (or others undefined on the ld
3081 command line). Thus we avoid marking all function
3082 sections, as all functions are referenced in .opd. */
3083 else if ((fdh->oh != NULL
3084 && ((struct ppc_link_hash_entry *) fdh->oh)->is_entry)
3085 || elf_section_data (sec)->tdata == NULL)
3086 rsec = h->root.u.def.section;
3087 break;
3088
3089 case bfd_link_hash_common:
3090 rsec = h->root.u.c.p->section;
3091 break;
3092
3093 default:
3094 break;
3095 }
3096 }
3097 }
3098 else
3099 {
3100 asection **opd_sym_section;
3101
3102 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
3103 opd_sym_section = (asection **) elf_section_data (rsec)->tdata;
3104 if (opd_sym_section != NULL)
3105 rsec = opd_sym_section[sym->st_value / 24];
3106 else if (elf_section_data (sec)->tdata != NULL)
3107 rsec = NULL;
3108 }
3109
3110 return rsec;
3111 }
3112
3113 /* Update the .got, .plt. and dynamic reloc reference counts for the
3114 section being removed. */
3115
3116 static boolean
3117 ppc64_elf_gc_sweep_hook (abfd, info, sec, relocs)
3118 bfd *abfd;
3119 struct bfd_link_info *info ATTRIBUTE_UNUSED;
3120 asection *sec;
3121 const Elf_Internal_Rela *relocs;
3122 {
3123 Elf_Internal_Shdr *symtab_hdr;
3124 struct elf_link_hash_entry **sym_hashes;
3125 bfd_signed_vma *local_got_refcounts;
3126 const Elf_Internal_Rela *rel, *relend;
3127
3128 elf_section_data (sec)->local_dynrel = NULL;
3129
3130 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3131 sym_hashes = elf_sym_hashes (abfd);
3132 local_got_refcounts = elf_local_got_refcounts (abfd);
3133
3134 relend = relocs + sec->reloc_count;
3135 for (rel = relocs; rel < relend; rel++)
3136 {
3137 unsigned long r_symndx;
3138 enum elf_ppc_reloc_type r_type;
3139 struct elf_link_hash_entry *h;
3140
3141 r_symndx = ELF64_R_SYM (rel->r_info);
3142 r_type = (enum elf_ppc_reloc_type) ELF64_R_TYPE (rel->r_info);
3143 switch (r_type)
3144 {
3145 case R_PPC64_GOT16:
3146 case R_PPC64_GOT16_DS:
3147 case R_PPC64_GOT16_HA:
3148 case R_PPC64_GOT16_HI:
3149 case R_PPC64_GOT16_LO:
3150 case R_PPC64_GOT16_LO_DS:
3151 if (r_symndx >= symtab_hdr->sh_info)
3152 {
3153 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3154 if (h->got.refcount > 0)
3155 h->got.refcount--;
3156 }
3157 else
3158 {
3159 if (local_got_refcounts[r_symndx] > 0)
3160 local_got_refcounts[r_symndx]--;
3161 }
3162 break;
3163
3164 case R_PPC64_PLT16_HA:
3165 case R_PPC64_PLT16_HI:
3166 case R_PPC64_PLT16_LO:
3167 case R_PPC64_PLT32:
3168 case R_PPC64_PLT64:
3169 if (r_symndx >= symtab_hdr->sh_info)
3170 {
3171 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3172 if (h->plt.refcount > 0)
3173 h->plt.refcount--;
3174 }
3175 break;
3176
3177 case R_PPC64_REL14:
3178 case R_PPC64_REL14_BRNTAKEN:
3179 case R_PPC64_REL14_BRTAKEN:
3180 case R_PPC64_REL24:
3181 if (r_symndx >= symtab_hdr->sh_info)
3182 {
3183 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3184 if (h->plt.refcount > 0)
3185 h->plt.refcount--;
3186 }
3187 break;
3188
3189 case R_PPC64_REL32:
3190 case R_PPC64_REL64:
3191 if (r_symndx >= symtab_hdr->sh_info)
3192 {
3193 struct ppc_link_hash_entry *eh;
3194 struct ppc_dyn_relocs **pp;
3195 struct ppc_dyn_relocs *p;
3196
3197 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3198 eh = (struct ppc_link_hash_entry *) h;
3199
3200 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
3201 if (p->sec == sec)
3202 {
3203 p->pc_count -= 1;
3204 p->count -= 1;
3205 if (p->count == 0)
3206 *pp = p->next;
3207 break;
3208 }
3209 }
3210 break;
3211
3212 case R_PPC64_ADDR14:
3213 case R_PPC64_ADDR14_BRNTAKEN:
3214 case R_PPC64_ADDR14_BRTAKEN:
3215 case R_PPC64_ADDR16:
3216 case R_PPC64_ADDR16_DS:
3217 case R_PPC64_ADDR16_HA:
3218 case R_PPC64_ADDR16_HI:
3219 case R_PPC64_ADDR16_HIGHER:
3220 case R_PPC64_ADDR16_HIGHERA:
3221 case R_PPC64_ADDR16_HIGHEST:
3222 case R_PPC64_ADDR16_HIGHESTA:
3223 case R_PPC64_ADDR16_LO:
3224 case R_PPC64_ADDR16_LO_DS:
3225 case R_PPC64_ADDR24:
3226 case R_PPC64_ADDR30:
3227 case R_PPC64_ADDR32:
3228 case R_PPC64_ADDR64:
3229 case R_PPC64_UADDR16:
3230 case R_PPC64_UADDR32:
3231 case R_PPC64_UADDR64:
3232 case R_PPC64_TOC:
3233 if (r_symndx >= symtab_hdr->sh_info)
3234 {
3235 struct ppc_link_hash_entry *eh;
3236 struct ppc_dyn_relocs **pp;
3237 struct ppc_dyn_relocs *p;
3238
3239 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3240 eh = (struct ppc_link_hash_entry *) h;
3241
3242 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
3243 if (p->sec == sec)
3244 {
3245 p->count -= 1;
3246 if (p->count == 0)
3247 *pp = p->next;
3248 break;
3249 }
3250 }
3251 break;
3252
3253 default:
3254 break;
3255 }
3256 }
3257 return true;
3258 }
3259
3260 /* Called via elf_link_hash_traverse to transfer dynamic linking
3261 information on function code symbol entries to their corresponding
3262 function descriptor symbol entries. */
3263 static boolean
3264 func_desc_adjust (h, inf)
3265 struct elf_link_hash_entry *h;
3266 PTR inf;
3267 {
3268 struct bfd_link_info *info;
3269 struct ppc_link_hash_table *htab;
3270
3271 if (h->root.type == bfd_link_hash_indirect)
3272 return true;
3273
3274 if (h->root.type == bfd_link_hash_warning)
3275 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3276
3277 info = (struct bfd_link_info *) inf;
3278 htab = ppc_hash_table (info);
3279
3280 /* If this is a function code symbol, transfer dynamic linking
3281 information to the function descriptor symbol. */
3282 if (!((struct ppc_link_hash_entry *) h)->is_func)
3283 return true;
3284
3285 if (h->root.type == bfd_link_hash_undefweak
3286 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR))
3287 htab->have_undefweak = true;
3288
3289 if (h->plt.refcount > 0
3290 && h->root.root.string[0] == '.'
3291 && h->root.root.string[1] != '\0')
3292 {
3293 struct elf_link_hash_entry *fdh = ((struct ppc_link_hash_entry *) h)->oh;
3294 boolean force_local;
3295
3296 /* Find the corresponding function descriptor symbol. Create it
3297 as undefined if necessary. */
3298
3299 if (fdh == NULL)
3300 fdh = elf_link_hash_lookup (&htab->elf, h->root.root.string + 1,
3301 false, false, true);
3302
3303 if (fdh == NULL
3304 && info->shared
3305 && (h->root.type == bfd_link_hash_undefined
3306 || h->root.type == bfd_link_hash_undefweak))
3307 {
3308 bfd *abfd;
3309 asymbol *newsym;
3310
3311 abfd = h->root.u.undef.abfd;
3312 newsym = bfd_make_empty_symbol (abfd);
3313 newsym->name = h->root.root.string + 1;
3314 newsym->section = bfd_und_section_ptr;
3315 newsym->value = 0;
3316 newsym->flags = BSF_OBJECT;
3317 if (h->root.type == bfd_link_hash_undefweak)
3318 newsym->flags |= BSF_WEAK;
3319
3320 if ( !(_bfd_generic_link_add_one_symbol
3321 (info, abfd, newsym->name, newsym->flags,
3322 newsym->section, newsym->value, NULL, false, false,
3323 (struct bfd_link_hash_entry **) &fdh)))
3324 {
3325 return false;
3326 }
3327 fdh->elf_link_hash_flags &= ~ELF_LINK_NON_ELF;
3328 }
3329
3330 if (fdh != NULL
3331 && (fdh->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0
3332 && (info->shared
3333 || (fdh->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
3334 || (fdh->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0))
3335 {
3336 if (fdh->dynindx == -1)
3337 if (! bfd_elf64_link_record_dynamic_symbol (info, fdh))
3338 return false;
3339 fdh->elf_link_hash_flags |= (h->elf_link_hash_flags
3340 & (ELF_LINK_HASH_REF_REGULAR
3341 | ELF_LINK_HASH_REF_DYNAMIC
3342 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
3343 | ELF_LINK_NON_GOT_REF));
3344 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3345 {
3346 fdh->plt.refcount = h->plt.refcount;
3347 fdh->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
3348 }
3349 ((struct ppc_link_hash_entry *) fdh)->is_func_descriptor = 1;
3350 ((struct ppc_link_hash_entry *) fdh)->oh = h;
3351 ((struct ppc_link_hash_entry *) h)->oh = fdh;
3352 }
3353
3354 /* Now that the info is on the function descriptor, clear the
3355 function code sym info. Any function code syms for which we
3356 don't have a definition in a regular file, we force local.
3357 This prevents a shared library from exporting syms that have
3358 been imported from another library. Function code syms that
3359 are really in the library we must leave global to prevent the
3360 linker dragging in a definition from a static library. */
3361 force_local = (info->shared
3362 && ((h->elf_link_hash_flags
3363 & ELF_LINK_HASH_DEF_REGULAR) == 0
3364 || fdh == NULL
3365 || (fdh->elf_link_hash_flags
3366 & ELF_LINK_HASH_DEF_REGULAR) == 0
3367 || (fdh->elf_link_hash_flags
3368 & ELF_LINK_FORCED_LOCAL) != 0));
3369 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
3370 }
3371
3372 return true;
3373 }
3374
3375 #define MIN_SAVE_FPR 14
3376 #define MAX_SAVE_FPR 31
3377
3378 /* Called near the start of bfd_elf_size_dynamic_sections. We use
3379 this hook to a) provide some gcc support functions, and b) transfer
3380 dynamic linking information gathered so far on function code symbol
3381 entries, to their corresponding function descriptor symbol entries. */
3382 static boolean
3383 ppc64_elf_func_desc_adjust (obfd, info)
3384 bfd *obfd ATTRIBUTE_UNUSED;
3385 struct bfd_link_info *info;
3386 {
3387 struct ppc_link_hash_table *htab;
3388 unsigned int lowest_savef = MAX_SAVE_FPR + 2;
3389 unsigned int lowest_restf = MAX_SAVE_FPR + 2;
3390 unsigned int i;
3391 struct elf_link_hash_entry *h;
3392 bfd_byte *p;
3393 char sym[10];
3394
3395 htab = ppc_hash_table (info);
3396
3397 if (htab->sfpr == NULL)
3398 /* We don't have any relocs. */
3399 return true;
3400
3401 /* First provide any missing ._savef* and ._restf* functions. */
3402 memcpy (sym, "._savef14", 10);
3403 for (i = MIN_SAVE_FPR; i <= MAX_SAVE_FPR; i++)
3404 {
3405 sym[7] = i / 10 + '0';
3406 sym[8] = i % 10 + '0';
3407 h = elf_link_hash_lookup (&htab->elf, sym, false, false, true);
3408 if (h != NULL
3409 && h->root.type == bfd_link_hash_undefined)
3410 {
3411 if (lowest_savef > i)
3412 lowest_savef = i;
3413 h->root.type = bfd_link_hash_defined;
3414 h->root.u.def.section = htab->sfpr;
3415 h->root.u.def.value = (i - lowest_savef) * 4;
3416 h->type = STT_FUNC;
3417 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3418 _bfd_elf_link_hash_hide_symbol (info, h, info->shared);
3419 }
3420 }
3421
3422 memcpy (sym, "._restf14", 10);
3423 for (i = MIN_SAVE_FPR; i <= MAX_SAVE_FPR; i++)
3424 {
3425 sym[7] = i / 10 + '0';
3426 sym[8] = i % 10 + '0';
3427 h = elf_link_hash_lookup (&htab->elf, sym, false, false, true);
3428 if (h != NULL
3429 && h->root.type == bfd_link_hash_undefined)
3430 {
3431 if (lowest_restf > i)
3432 lowest_restf = i;
3433 h->root.type = bfd_link_hash_defined;
3434 h->root.u.def.section = htab->sfpr;
3435 h->root.u.def.value = ((MAX_SAVE_FPR + 2 - lowest_savef) * 4
3436 + (i - lowest_restf) * 4);
3437 h->type = STT_FUNC;
3438 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3439 _bfd_elf_link_hash_hide_symbol (info, h, info->shared);
3440 }
3441 }
3442
3443 elf_link_hash_traverse (&htab->elf, func_desc_adjust, (PTR) info);
3444
3445 htab->sfpr->_raw_size = ((MAX_SAVE_FPR + 2 - lowest_savef) * 4
3446 + (MAX_SAVE_FPR + 2 - lowest_restf) * 4);
3447
3448 if (htab->sfpr->_raw_size == 0)
3449 {
3450 if (!htab->have_undefweak)
3451 {
3452 _bfd_strip_section_from_output (info, htab->sfpr);
3453 return true;
3454 }
3455
3456 htab->sfpr->_raw_size = 4;
3457 }
3458
3459 p = (bfd_byte *) bfd_alloc (htab->elf.dynobj, htab->sfpr->_raw_size);
3460 if (p == NULL)
3461 return false;
3462 htab->sfpr->contents = p;
3463
3464 for (i = lowest_savef; i <= MAX_SAVE_FPR; i++)
3465 {
3466 unsigned int fpr = i << 21;
3467 unsigned int stackoff = (1 << 16) - (MAX_SAVE_FPR + 1 - i) * 8;
3468 bfd_put_32 (htab->elf.dynobj, STFD_FR0_0R1 + fpr + stackoff, p);
3469 p += 4;
3470 }
3471 if (lowest_savef <= MAX_SAVE_FPR)
3472 {
3473 bfd_put_32 (htab->elf.dynobj, BLR, p);
3474 p += 4;
3475 }
3476
3477 for (i = lowest_restf; i <= MAX_SAVE_FPR; i++)
3478 {
3479 unsigned int fpr = i << 21;
3480 unsigned int stackoff = (1 << 16) - (MAX_SAVE_FPR + 1 - i) * 8;
3481 bfd_put_32 (htab->elf.dynobj, LFD_FR0_0R1 + fpr + stackoff, p);
3482 p += 4;
3483 }
3484 if (lowest_restf <= MAX_SAVE_FPR
3485 || htab->sfpr->_raw_size == 4)
3486 {
3487 bfd_put_32 (htab->elf.dynobj, BLR, p);
3488 }
3489
3490 return true;
3491 }
3492
3493 /* Adjust a symbol defined by a dynamic object and referenced by a
3494 regular object. The current definition is in some section of the
3495 dynamic object, but we're not including those sections. We have to
3496 change the definition to something the rest of the link can
3497 understand. */
3498
3499 static boolean
3500 ppc64_elf_adjust_dynamic_symbol (info, h)
3501 struct bfd_link_info *info;
3502 struct elf_link_hash_entry *h;
3503 {
3504 struct ppc_link_hash_table *htab;
3505 struct ppc_link_hash_entry * eh;
3506 struct ppc_dyn_relocs *p;
3507 asection *s;
3508 unsigned int power_of_two;
3509
3510 htab = ppc_hash_table (info);
3511
3512 /* Deal with function syms. */
3513 if (h->type == STT_FUNC
3514 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
3515 {
3516 /* Clear procedure linkage table information for any symbol that
3517 won't need a .plt entry. */
3518 if (!((struct ppc_link_hash_entry *) h)->is_func_descriptor
3519 || h->plt.refcount <= 0
3520 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0
3521 || (! info->shared
3522 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
3523 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0))
3524 {
3525 h->plt.offset = (bfd_vma) -1;
3526 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
3527 }
3528 return true;
3529 }
3530 else
3531 h->plt.offset = (bfd_vma) -1;
3532
3533 /* If this is a weak symbol, and there is a real definition, the
3534 processor independent code will have arranged for us to see the
3535 real definition first, and we can just use the same value. */
3536 if (h->weakdef != NULL)
3537 {
3538 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
3539 || h->weakdef->root.type == bfd_link_hash_defweak);
3540 h->root.u.def.section = h->weakdef->root.u.def.section;
3541 h->root.u.def.value = h->weakdef->root.u.def.value;
3542 return true;
3543 }
3544
3545 /* This is a reference to a symbol defined by a dynamic object which
3546 is not a function. */
3547
3548 /* If we are creating a shared library, we must presume that the
3549 only references to the symbol are via the global offset table.
3550 For such cases we need not do anything here; the relocations will
3551 be handled correctly by relocate_section. */
3552 if (info->shared)
3553 return true;
3554
3555 /* If there are no references to this symbol that do not use the
3556 GOT, we don't need to generate a copy reloc. */
3557 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
3558 return true;
3559
3560 eh = (struct ppc_link_hash_entry *) h;
3561 for (p = eh->dyn_relocs; p != NULL; p = p->next)
3562 {
3563 s = p->sec->output_section;
3564 if (s != NULL && (s->flags & SEC_READONLY) != 0)
3565 break;
3566 }
3567
3568 /* If we didn't find any dynamic relocs in read-only sections, then
3569 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
3570 if (p == NULL)
3571 {
3572 h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
3573 return true;
3574 }
3575
3576 /* We must allocate the symbol in our .dynbss section, which will
3577 become part of the .bss section of the executable. There will be
3578 an entry for this symbol in the .dynsym section. The dynamic
3579 object will contain position independent code, so all references
3580 from the dynamic object to this symbol will go through the global
3581 offset table. The dynamic linker will use the .dynsym entry to
3582 determine the address it must put in the global offset table, so
3583 both the dynamic object and the regular object will refer to the
3584 same memory location for the variable. */
3585
3586 /* We must generate a R_PPC_COPY reloc to tell the dynamic linker to
3587 copy the initial value out of the dynamic object and into the
3588 runtime process image. We need to remember the offset into the
3589 .rela.bss section we are going to use. */
3590 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
3591 {
3592 htab->srelbss->_raw_size += sizeof (Elf64_External_Rela);
3593 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
3594 }
3595
3596 /* We need to figure out the alignment required for this symbol. I
3597 have no idea how ELF linkers handle this. */
3598 power_of_two = bfd_log2 (h->size);
3599 if (power_of_two > 4)
3600 power_of_two = 4;
3601
3602 /* Apply the required alignment. */
3603 s = htab->sdynbss;
3604 s->_raw_size = BFD_ALIGN (s->_raw_size, (bfd_size_type) (1 << power_of_two));
3605 if (power_of_two > bfd_get_section_alignment (htab->elf.dynobj, s))
3606 {
3607 if (! bfd_set_section_alignment (htab->elf.dynobj, s, power_of_two))
3608 return false;
3609 }
3610
3611 /* Define the symbol as being at this point in the section. */
3612 h->root.u.def.section = s;
3613 h->root.u.def.value = s->_raw_size;
3614
3615 /* Increment the section size to make room for the symbol. */
3616 s->_raw_size += h->size;
3617
3618 return true;
3619 }
3620
3621 /* If given a function descriptor symbol, hide both the function code
3622 sym and the descriptor. */
3623 static void
3624 ppc64_elf_hide_symbol (info, h, force_local)
3625 struct bfd_link_info *info;
3626 struct elf_link_hash_entry *h;
3627 boolean force_local;
3628 {
3629 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
3630
3631 if (((struct ppc_link_hash_entry *) h)->is_func_descriptor)
3632 {
3633 struct elf_link_hash_entry *fh = ((struct ppc_link_hash_entry *) h)->oh;
3634
3635 if (fh == NULL)
3636 {
3637 const char *p, *q;
3638 struct ppc_link_hash_table *htab;
3639 char save;
3640
3641 /* We aren't supposed to use alloca in BFD because on
3642 systems which do not have alloca the version in libiberty
3643 calls xmalloc, which might cause the program to crash
3644 when it runs out of memory. This function doesn't have a
3645 return status, so there's no way to gracefully return an
3646 error. So cheat. We know that string[-1] can be safely
3647 dereferenced; It's either a string in an ELF string
3648 table, or allocated in an objalloc structure. */
3649
3650 p = h->root.root.string - 1;
3651 save = *p;
3652 *(char *) p = '.';
3653 htab = ppc_hash_table (info);
3654 fh = elf_link_hash_lookup (&htab->elf, p, false, false, false);
3655 *(char *) p = save;
3656
3657 /* Unfortunately, if it so happens that the string we were
3658 looking for was allocated immediately before this string,
3659 then we overwrote the string terminator. That's the only
3660 reason the lookup should fail. */
3661 if (fh == NULL)
3662 {
3663 q = h->root.root.string + strlen (h->root.root.string);
3664 while (q >= h->root.root.string && *q == *p)
3665 --q, --p;
3666 if (q < h->root.root.string && *p == '.')
3667 fh = elf_link_hash_lookup (&htab->elf, p, false, false, false);
3668 }
3669 if (fh != NULL)
3670 {
3671 ((struct ppc_link_hash_entry *) h)->oh = fh;
3672 ((struct ppc_link_hash_entry *) fh)->oh = h;
3673 }
3674 }
3675 if (fh != NULL)
3676 _bfd_elf_link_hash_hide_symbol (info, fh, force_local);
3677 }
3678 }
3679
3680 static boolean
3681 edit_opd (obfd, info)
3682 bfd *obfd;
3683 struct bfd_link_info *info;
3684 {
3685 bfd *ibfd;
3686 unsigned int bfd_indx;
3687
3688 for (bfd_indx = 0, ibfd = info->input_bfds;
3689 ibfd != NULL;
3690 ibfd = ibfd->link_next, bfd_indx++)
3691 {
3692 asection *sec;
3693 Elf_Internal_Rela *relstart, *rel, *relend;
3694 Elf_Internal_Shdr *symtab_hdr;
3695 Elf_Internal_Sym *local_syms;
3696 struct elf_link_hash_entry **sym_hashes;
3697 bfd_vma offset;
3698 long *adjust;
3699 boolean need_edit;
3700
3701 sec = bfd_get_section_by_name (ibfd, ".opd");
3702 if (sec == NULL)
3703 continue;
3704
3705 adjust = (long *) elf_section_data (sec)->tdata;
3706 BFD_ASSERT (adjust != NULL);
3707 memset (adjust, 0, (size_t) sec->_raw_size * sizeof (long) / 24);
3708
3709 if (sec->output_section == bfd_abs_section_ptr)
3710 continue;
3711
3712 /* Look through the section relocs. */
3713 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
3714 continue;
3715
3716 local_syms = NULL;
3717 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
3718 sym_hashes = elf_sym_hashes (ibfd);
3719
3720 /* Read the relocations. */
3721 relstart = _bfd_elf64_link_read_relocs (obfd, sec, (PTR) NULL,
3722 (Elf_Internal_Rela *) NULL,
3723 info->keep_memory);
3724 if (relstart == NULL)
3725 return false;
3726
3727 /* First run through the relocs to check they are sane, and to
3728 determine whether we need to edit this opd section. */
3729 need_edit = false;
3730 offset = 0;
3731 relend = relstart + sec->reloc_count;
3732 for (rel = relstart; rel < relend; rel++)
3733 {
3734 enum elf_ppc_reloc_type r_type;
3735 unsigned long r_symndx;
3736 asection *sym_sec;
3737 struct elf_link_hash_entry *h;
3738 Elf_Internal_Sym *sym;
3739
3740 /* .opd contains a regular array of 24 byte entries. We're
3741 only interested in the reloc pointing to a function entry
3742 point. */
3743 r_type = (enum elf_ppc_reloc_type) ELF64_R_TYPE (rel->r_info);
3744 if (r_type == R_PPC64_TOC)
3745 continue;
3746
3747 if (r_type != R_PPC64_ADDR64)
3748 {
3749 (*_bfd_error_handler)
3750 (_("%s: unexpected reloc type %u in .opd section"),
3751 bfd_archive_filename (ibfd), r_type);
3752 need_edit = false;
3753 break;
3754 }
3755
3756 if (rel + 1 >= relend)
3757 continue;
3758 r_type = (enum elf_ppc_reloc_type) ELF64_R_TYPE ((rel + 1)->r_info);
3759 if (r_type != R_PPC64_TOC)
3760 continue;
3761
3762 if (rel->r_offset != offset)
3763 {
3764 /* If someone messes with .opd alignment then after a
3765 "ld -r" we might have padding in the middle of .opd.
3766 Also, there's nothing to prevent someone putting
3767 something silly in .opd with the assembler. No .opd
3768 optimization for them! */
3769 (*_bfd_error_handler)
3770 (_("%s: .opd is not a regular array of opd entries"),
3771 bfd_archive_filename (ibfd));
3772 need_edit = false;
3773 break;
3774 }
3775
3776 r_symndx = ELF64_R_SYM (rel->r_info);
3777 sym_sec = NULL;
3778 h = NULL;
3779 sym = NULL;
3780 if (r_symndx >= symtab_hdr->sh_info)
3781 {
3782 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3783 while (h->root.type == bfd_link_hash_indirect
3784 || h->root.type == bfd_link_hash_warning)
3785 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3786 if (h->root.type == bfd_link_hash_defined
3787 || h->root.type == bfd_link_hash_defweak)
3788 sym_sec = h->root.u.def.section;
3789 }
3790 else
3791 {
3792 if (local_syms == NULL)
3793 {
3794 local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
3795 if (local_syms == NULL)
3796 local_syms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
3797 symtab_hdr->sh_info, 0,
3798 NULL, NULL, NULL);
3799 if (local_syms == NULL)
3800 goto error_free_rel;
3801 }
3802 sym = local_syms + r_symndx;
3803 if ((sym->st_shndx != SHN_UNDEF
3804 && sym->st_shndx < SHN_LORESERVE)
3805 || sym->st_shndx > SHN_HIRESERVE)
3806 sym_sec = bfd_section_from_elf_index (ibfd, sym->st_shndx);
3807 }
3808
3809 if (sym_sec == NULL || sym_sec->owner == NULL)
3810 {
3811 (*_bfd_error_handler)
3812 (_("%s: undefined sym `%s' in .opd section"),
3813 bfd_archive_filename (ibfd),
3814 h != NULL ? h->root.root.string : "<local symbol>");
3815 need_edit = false;
3816 break;
3817 }
3818
3819 if (sym_sec->output_section == bfd_abs_section_ptr)
3820 {
3821 /* OK, we've found a function that's excluded from the
3822 link. */
3823 need_edit = true;
3824 }
3825
3826 offset += 24;
3827 }
3828
3829 if (need_edit)
3830 {
3831 Elf_Internal_Rela *write_rel;
3832 bfd_byte *rptr, *wptr;
3833 boolean skip;
3834
3835 /* This seems a waste of time as input .opd sections are all
3836 zeros as generated by gcc, but I suppose there's no reason
3837 this will always be so. We might start putting something in
3838 the third word of .opd entries. */
3839 if ((sec->flags & SEC_IN_MEMORY) == 0)
3840 {
3841 bfd_byte *loc = bfd_alloc (ibfd, sec->_raw_size);
3842 if (loc == NULL
3843 || !bfd_get_section_contents (ibfd, sec, loc, (bfd_vma) 0,
3844 sec->_raw_size))
3845 {
3846 if (local_syms != NULL
3847 && symtab_hdr->contents != (unsigned char *) local_syms)
3848 free (local_syms);
3849 error_free_rel:
3850 if (elf_section_data (sec)->relocs != relstart)
3851 free (relstart);
3852 return false;
3853 }
3854 sec->contents = loc;
3855 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
3856 }
3857
3858 elf_section_data (sec)->relocs = relstart;
3859
3860 wptr = sec->contents;
3861 rptr = sec->contents;
3862 write_rel = relstart;
3863 skip = false;
3864 offset = 0;
3865 for (rel = relstart; rel < relend; rel++)
3866 {
3867 if (rel->r_offset == offset)
3868 {
3869 unsigned long r_symndx;
3870 asection *sym_sec;
3871 struct elf_link_hash_entry *h;
3872 Elf_Internal_Sym *sym;
3873
3874 r_symndx = ELF64_R_SYM (rel->r_info);
3875 sym_sec = NULL;
3876 h = NULL;
3877 sym = NULL;
3878 if (r_symndx >= symtab_hdr->sh_info)
3879 {
3880 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3881 while (h->root.type == bfd_link_hash_indirect
3882 || h->root.type == bfd_link_hash_warning)
3883 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3884 if (h->root.type == bfd_link_hash_defined
3885 || h->root.type == bfd_link_hash_defweak)
3886 sym_sec = h->root.u.def.section;
3887 }
3888 else
3889 {
3890 sym = local_syms + r_symndx;
3891 if ((sym->st_shndx != SHN_UNDEF
3892 && sym->st_shndx < SHN_LORESERVE)
3893 || sym->st_shndx > SHN_HIRESERVE)
3894 sym_sec = bfd_section_from_elf_index (ibfd,
3895 sym->st_shndx);
3896 }
3897
3898 skip = sym_sec->output_section == bfd_abs_section_ptr;
3899 if (!skip)
3900 {
3901 /* We'll be keeping this opd entry. */
3902
3903 if (h != NULL)
3904 {
3905 /* Redefine the function descriptor symbol
3906 to this location in the opd section.
3907 We've checked above that opd relocs are
3908 ordered. */
3909 struct elf_link_hash_entry *fdh;
3910 struct ppc_link_hash_entry *fh;
3911
3912 fh = (struct ppc_link_hash_entry *) h;
3913 BFD_ASSERT (fh->is_func);
3914 fdh = fh->oh;
3915 fdh->root.u.def.value = wptr - sec->contents;
3916 }
3917 else
3918 {
3919 /* Local syms are a bit tricky. We could
3920 tweak them as they can be cached, but
3921 we'd need to look through the local syms
3922 for the function descriptor sym which we
3923 don't have at the moment. So keep an
3924 array of adjustments. */
3925 adjust[(rel->r_offset + wptr - rptr) / 24]
3926 = wptr - rptr;
3927 }
3928
3929 if (wptr != rptr)
3930 memcpy (wptr, rptr, 24);
3931 wptr += 24;
3932 }
3933 rptr += 24;
3934 offset += 24;
3935 }
3936
3937 /* We need to adjust any reloc offsets to point to the
3938 new opd entries. While we're at it, we may as well
3939 remove redundant relocs. */
3940 if (!skip)
3941 {
3942 rel->r_offset += wptr - rptr;
3943 if (write_rel != rel)
3944 memcpy (write_rel, rel, sizeof (*rel));
3945 ++write_rel;
3946 }
3947 }
3948
3949 sec->_cooked_size = wptr - sec->contents;
3950 sec->reloc_count = write_rel - relstart;
3951 }
3952 else if (elf_section_data (sec)->relocs != relstart)
3953 free (relstart);
3954
3955 if (local_syms != NULL
3956 && symtab_hdr->contents != (unsigned char *) local_syms)
3957 {
3958 if (!info->keep_memory)
3959 free (local_syms);
3960 else
3961 symtab_hdr->contents = (unsigned char *) local_syms;
3962 }
3963 }
3964
3965 return true;
3966 }
3967
3968 /* This is the condition under which ppc64_elf_finish_dynamic_symbol
3969 will be called from elflink.h. If elflink.h doesn't call our
3970 finish_dynamic_symbol routine, we'll need to do something about
3971 initializing any .plt and .got entries in ppc64_elf_relocate_section. */
3972 #define WILL_CALL_FINISH_DYNAMIC_SYMBOL(DYN, INFO, H) \
3973 ((DYN) \
3974 && ((INFO)->shared \
3975 || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0) \
3976 && ((H)->dynindx != -1 \
3977 || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0))
3978
3979 /* Allocate space in .plt, .got and associated reloc sections for
3980 dynamic relocs. */
3981
3982 static boolean
3983 allocate_dynrelocs (h, inf)
3984 struct elf_link_hash_entry *h;
3985 PTR inf;
3986 {
3987 struct bfd_link_info *info;
3988 struct ppc_link_hash_table *htab;
3989 asection *s;
3990 struct ppc_link_hash_entry *eh;
3991 struct ppc_dyn_relocs *p;
3992
3993 if (h->root.type == bfd_link_hash_indirect)
3994 return true;
3995
3996 if (h->root.type == bfd_link_hash_warning)
3997 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3998
3999 info = (struct bfd_link_info *) inf;
4000 htab = ppc_hash_table (info);
4001
4002 if (htab->elf.dynamic_sections_created
4003 && h->plt.refcount > 0
4004 && h->dynindx != -1)
4005 {
4006 BFD_ASSERT (((struct ppc_link_hash_entry *) h)->is_func_descriptor);
4007
4008 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info, h))
4009 {
4010 /* If this is the first .plt entry, make room for the special
4011 first entry. */
4012 s = htab->splt;
4013 if (s->_raw_size == 0)
4014 s->_raw_size += PLT_INITIAL_ENTRY_SIZE;
4015
4016 h->plt.offset = s->_raw_size;
4017
4018 /* Make room for this entry. */
4019 s->_raw_size += PLT_ENTRY_SIZE;
4020
4021 /* Make room for the .glink code. */
4022 s = htab->sglink;
4023 if (s->_raw_size == 0)
4024 s->_raw_size += GLINK_CALL_STUB_SIZE;
4025 /* We need bigger stubs past index 32767. */
4026 if (s->_raw_size >= GLINK_CALL_STUB_SIZE + 32768*2*4)
4027 s->_raw_size += 4;
4028 s->_raw_size += 2*4;
4029
4030 /* We also need to make an entry in the .rela.plt section. */
4031 s = htab->srelplt;
4032 s->_raw_size += sizeof (Elf64_External_Rela);
4033 }
4034 else
4035 {
4036 h->plt.offset = (bfd_vma) -1;
4037 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
4038 }
4039 }
4040 else
4041 {
4042 h->plt.offset = (bfd_vma) -1;
4043 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
4044 }
4045
4046 if (h->got.refcount > 0)
4047 {
4048 boolean dyn;
4049
4050 /* Make sure this symbol is output as a dynamic symbol.
4051 Undefined weak syms won't yet be marked as dynamic. */
4052 if (h->dynindx == -1
4053 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
4054 {
4055 if (! bfd_elf64_link_record_dynamic_symbol (info, h))
4056 return false;
4057 }
4058
4059 s = htab->sgot;
4060 h->got.offset = s->_raw_size;
4061 s->_raw_size += 8;
4062 dyn = htab->elf.dynamic_sections_created;
4063 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info, h))
4064 htab->srelgot->_raw_size += sizeof (Elf64_External_Rela);
4065 }
4066 else
4067 h->got.offset = (bfd_vma) -1;
4068
4069 eh = (struct ppc_link_hash_entry *) h;
4070 if (eh->dyn_relocs == NULL)
4071 return true;
4072
4073 /* In the shared -Bsymbolic case, discard space allocated for
4074 dynamic pc-relative relocs against symbols which turn out to be
4075 defined in regular objects. For the normal shared case, discard
4076 space for relocs that have become local due to symbol visibility
4077 changes. */
4078
4079 if (info->shared)
4080 {
4081 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
4082 && ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0
4083 || info->symbolic))
4084 {
4085 struct ppc_dyn_relocs **pp;
4086
4087 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
4088 {
4089 p->count -= p->pc_count;
4090 p->pc_count = 0;
4091 if (p->count == 0)
4092 *pp = p->next;
4093 else
4094 pp = &p->next;
4095 }
4096 }
4097 }
4098 else
4099 {
4100 /* For the non-shared case, discard space for relocs against
4101 symbols which turn out to need copy relocs or are not
4102 dynamic. */
4103
4104 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
4105 && (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
4106 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
4107 || (htab->elf.dynamic_sections_created
4108 && (h->root.type == bfd_link_hash_undefweak
4109 || h->root.type == bfd_link_hash_undefined))))
4110 {
4111 /* Make sure this symbol is output as a dynamic symbol.
4112 Undefined weak syms won't yet be marked as dynamic. */
4113 if (h->dynindx == -1
4114 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
4115 {
4116 if (! bfd_elf64_link_record_dynamic_symbol (info, h))
4117 return false;
4118 }
4119
4120 /* If that succeeded, we know we'll be keeping all the
4121 relocs. */
4122 if (h->dynindx != -1)
4123 goto keep;
4124 }
4125
4126 eh->dyn_relocs = NULL;
4127
4128 keep: ;
4129 }
4130
4131 /* Finally, allocate space. */
4132 for (p = eh->dyn_relocs; p != NULL; p = p->next)
4133 {
4134 asection *sreloc = elf_section_data (p->sec)->sreloc;
4135 sreloc->_raw_size += p->count * sizeof (Elf64_External_Rela);
4136 }
4137
4138 return true;
4139 }
4140
4141 /* Find any dynamic relocs that apply to read-only sections. */
4142
4143 static boolean
4144 readonly_dynrelocs (h, inf)
4145 struct elf_link_hash_entry *h;
4146 PTR inf;
4147 {
4148 struct ppc_link_hash_entry *eh;
4149 struct ppc_dyn_relocs *p;
4150
4151 if (h->root.type == bfd_link_hash_warning)
4152 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4153
4154 eh = (struct ppc_link_hash_entry *) h;
4155 for (p = eh->dyn_relocs; p != NULL; p = p->next)
4156 {
4157 asection *s = p->sec->output_section;
4158
4159 if (s != NULL && (s->flags & SEC_READONLY) != 0)
4160 {
4161 struct bfd_link_info *info = (struct bfd_link_info *) inf;
4162
4163 info->flags |= DF_TEXTREL;
4164
4165 /* Not an error, just cut short the traversal. */
4166 return false;
4167 }
4168 }
4169 return true;
4170 }
4171
4172 /* Set the sizes of the dynamic sections. */
4173
4174 static boolean
4175 ppc64_elf_size_dynamic_sections (output_bfd, info)
4176 bfd *output_bfd ATTRIBUTE_UNUSED;
4177 struct bfd_link_info *info;
4178 {
4179 struct ppc_link_hash_table *htab;
4180 bfd *dynobj;
4181 asection *s;
4182 boolean relocs;
4183 bfd *ibfd;
4184
4185 htab = ppc_hash_table (info);
4186 dynobj = htab->elf.dynobj;
4187 if (dynobj == NULL)
4188 abort ();
4189
4190 if (htab->elf.dynamic_sections_created)
4191 {
4192 /* Set the contents of the .interp section to the interpreter. */
4193 if (! info->shared)
4194 {
4195 s = bfd_get_section_by_name (dynobj, ".interp");
4196 if (s == NULL)
4197 abort ();
4198 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
4199 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
4200 }
4201 }
4202
4203 /* Set up .got offsets for local syms, and space for local dynamic
4204 relocs. */
4205 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
4206 {
4207 bfd_signed_vma *local_got;
4208 bfd_signed_vma *end_local_got;
4209 bfd_size_type locsymcount;
4210 Elf_Internal_Shdr *symtab_hdr;
4211 asection *srel;
4212
4213 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
4214 continue;
4215
4216 for (s = ibfd->sections; s != NULL; s = s->next)
4217 {
4218 struct ppc_dyn_relocs *p;
4219
4220 for (p = *((struct ppc_dyn_relocs **)
4221 &elf_section_data (s)->local_dynrel);
4222 p != NULL;
4223 p = p->next)
4224 {
4225 if (!bfd_is_abs_section (p->sec)
4226 && bfd_is_abs_section (p->sec->output_section))
4227 {
4228 /* Input section has been discarded, either because
4229 it is a copy of a linkonce section or due to
4230 linker script /DISCARD/, so we'll be discarding
4231 the relocs too. */
4232 }
4233 else if (p->count != 0)
4234 {
4235 srel = elf_section_data (p->sec)->sreloc;
4236 srel->_raw_size += p->count * sizeof (Elf64_External_Rela);
4237 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
4238 info->flags |= DF_TEXTREL;
4239 }
4240 }
4241 }
4242
4243 local_got = elf_local_got_refcounts (ibfd);
4244 if (!local_got)
4245 continue;
4246
4247 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
4248 locsymcount = symtab_hdr->sh_info;
4249 end_local_got = local_got + locsymcount;
4250 s = htab->sgot;
4251 srel = htab->srelgot;
4252 for (; local_got < end_local_got; ++local_got)
4253 {
4254 if (*local_got > 0)
4255 {
4256 *local_got = s->_raw_size;
4257 s->_raw_size += 8;
4258 if (info->shared)
4259 srel->_raw_size += sizeof (Elf64_External_Rela);
4260 }
4261 else
4262 *local_got = (bfd_vma) -1;
4263 }
4264 }
4265
4266 if (!edit_opd (output_bfd, info))
4267 return false;
4268
4269 /* Allocate global sym .plt and .got entries, and space for global
4270 sym dynamic relocs. */
4271 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, (PTR) info);
4272
4273 /* We now have determined the sizes of the various dynamic sections.
4274 Allocate memory for them. */
4275 relocs = false;
4276 for (s = dynobj->sections; s != NULL; s = s->next)
4277 {
4278 if ((s->flags & SEC_LINKER_CREATED) == 0)
4279 continue;
4280
4281 if (s == htab->sbrlt || s == htab->srelbrlt)
4282 /* These haven't been allocated yet; don't strip. */
4283 continue;
4284 else if (s == htab->splt
4285 || s == htab->sgot
4286 || s == htab->sglink)
4287 {
4288 /* Strip this section if we don't need it; see the
4289 comment below. */
4290 }
4291 else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
4292 {
4293 if (s->_raw_size == 0)
4294 {
4295 /* If we don't need this section, strip it from the
4296 output file. This is mostly to handle .rela.bss and
4297 .rela.plt. We must create both sections in
4298 create_dynamic_sections, because they must be created
4299 before the linker maps input sections to output
4300 sections. The linker does that before
4301 adjust_dynamic_symbol is called, and it is that
4302 function which decides whether anything needs to go
4303 into these sections. */
4304 }
4305 else
4306 {
4307 if (s != htab->srelplt)
4308 relocs = true;
4309
4310 /* We use the reloc_count field as a counter if we need
4311 to copy relocs into the output file. */
4312 s->reloc_count = 0;
4313 }
4314 }
4315 else
4316 {
4317 /* It's not one of our sections, so don't allocate space. */
4318 continue;
4319 }
4320
4321 if (s->_raw_size == 0)
4322 {
4323 _bfd_strip_section_from_output (info, s);
4324 continue;
4325 }
4326
4327 /* .plt is in the bss section. We don't initialise it. */
4328 if ((s->flags & SEC_LOAD) == 0)
4329 continue;
4330
4331 /* Allocate memory for the section contents. We use bfd_zalloc
4332 here in case unused entries are not reclaimed before the
4333 section's contents are written out. This should not happen,
4334 but this way if it does, we get a R_PPC64_NONE reloc instead
4335 of garbage. */
4336 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
4337 if (s->contents == NULL)
4338 return false;
4339 }
4340
4341 if (htab->elf.dynamic_sections_created)
4342 {
4343 /* Add some entries to the .dynamic section. We fill in the
4344 values later, in ppc64_elf_finish_dynamic_sections, but we
4345 must add the entries now so that we get the correct size for
4346 the .dynamic section. The DT_DEBUG entry is filled in by the
4347 dynamic linker and used by the debugger. */
4348 #define add_dynamic_entry(TAG, VAL) \
4349 bfd_elf64_add_dynamic_entry (info, (bfd_vma) (TAG), (bfd_vma) (VAL))
4350
4351 if (!info->shared)
4352 {
4353 if (!add_dynamic_entry (DT_DEBUG, 0))
4354 return false;
4355 }
4356
4357 if (htab->splt != NULL && htab->splt->_raw_size != 0)
4358 {
4359 if (!add_dynamic_entry (DT_PLTGOT, 0)
4360 || !add_dynamic_entry (DT_PLTRELSZ, 0)
4361 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
4362 || !add_dynamic_entry (DT_JMPREL, 0)
4363 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
4364 return false;
4365 }
4366
4367 if (NO_OPD_RELOCS)
4368 {
4369 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
4370 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
4371 return false;
4372 }
4373
4374 if (relocs)
4375 {
4376 if (!add_dynamic_entry (DT_RELA, 0)
4377 || !add_dynamic_entry (DT_RELASZ, 0)
4378 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
4379 return false;
4380
4381 /* If any dynamic relocs apply to a read-only section,
4382 then we need a DT_TEXTREL entry. */
4383 if ((info->flags & DF_TEXTREL) == 0)
4384 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs,
4385 (PTR) info);
4386
4387 if ((info->flags & DF_TEXTREL) != 0)
4388 {
4389 if (!add_dynamic_entry (DT_TEXTREL, 0))
4390 return false;
4391 }
4392 }
4393 }
4394 #undef add_dynamic_entry
4395
4396 return true;
4397 }
4398
4399 /* Determine the type of stub needed, if any, for a call. */
4400
4401 static INLINE enum ppc_stub_type
4402 ppc_type_of_stub (input_sec, rel, hash, destination)
4403 asection *input_sec;
4404 const Elf_Internal_Rela *rel;
4405 struct ppc_link_hash_entry **hash;
4406 bfd_vma destination;
4407 {
4408 struct ppc_link_hash_entry *h = *hash;
4409 bfd_vma location;
4410 bfd_vma branch_offset;
4411 bfd_vma max_branch_offset;
4412 unsigned int r_type;
4413
4414 if (h != NULL)
4415 {
4416 if (h->oh != NULL
4417 && h->oh->plt.offset != (bfd_vma) -1
4418 && h->oh->dynindx != -1)
4419 {
4420 *hash = (struct ppc_link_hash_entry *) h->oh;
4421 return ppc_stub_plt_call;
4422 }
4423
4424 if (h->elf.root.type == bfd_link_hash_undefweak
4425 || h->elf.root.type == bfd_link_hash_undefined)
4426 return ppc_stub_none;
4427 }
4428
4429 /* Determine where the call point is. */
4430 location = (input_sec->output_offset
4431 + input_sec->output_section->vma
4432 + rel->r_offset);
4433
4434 branch_offset = destination - location;
4435 r_type = ELF64_R_TYPE (rel->r_info);
4436
4437 /* Determine if a long branch stub is needed. */
4438 max_branch_offset = 1 << 25;
4439 if (r_type != (unsigned int) R_PPC64_REL24)
4440 max_branch_offset = 1 << 15;
4441
4442 if (branch_offset + max_branch_offset >= 2 * max_branch_offset)
4443 /* We need a stub. Figure out whether a long_branch or plt_branch
4444 is needed later. */
4445 return ppc_stub_long_branch;
4446
4447 return ppc_stub_none;
4448 }
4449
4450 /* Build a .plt call stub. */
4451
4452 static bfd_byte *
4453 build_plt_stub (obfd, p, offset, glink)
4454 bfd *obfd;
4455 bfd_byte *p;
4456 int offset;
4457 int glink;
4458 {
4459 #define PPC_LO(v) ((v) & 0xffff)
4460 #define PPC_HI(v) (((v) >> 16) & 0xffff)
4461 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
4462
4463 if (glink)
4464 bfd_put_32 (obfd, LD_R2_40R1, p), p += 4;
4465 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
4466 if (!glink)
4467 bfd_put_32 (obfd, STD_R2_40R1, p), p += 4;
4468 bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset), p), p += 4;
4469 if (PPC_HA (offset + 8) != PPC_HA (offset))
4470 bfd_put_32 (obfd, ADDIS_R12_R12_1, p), p += 4;
4471 offset += 8;
4472 bfd_put_32 (obfd, LD_R2_0R12 | PPC_LO (offset), p), p += 4;
4473 if (PPC_HA (offset + 8) != PPC_HA (offset))
4474 bfd_put_32 (obfd, ADDIS_R12_R12_1, p), p += 4;
4475 offset += 8;
4476 bfd_put_32 (obfd, MTCTR_R11, p), p += 4;
4477 bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset), p), p += 4;
4478 bfd_put_32 (obfd, BCTR, p), p += 4;
4479 return p;
4480 }
4481
4482 static boolean
4483 ppc_build_one_stub (gen_entry, in_arg)
4484 struct bfd_hash_entry *gen_entry;
4485 PTR in_arg;
4486 {
4487 struct ppc_stub_hash_entry *stub_entry;
4488 struct ppc_branch_hash_entry *br_entry;
4489 struct bfd_link_info *info;
4490 struct ppc_link_hash_table *htab;
4491 asection *stub_sec;
4492 bfd *stub_bfd;
4493 bfd_byte *loc;
4494 bfd_byte *p;
4495 unsigned int indx;
4496 bfd_vma off;
4497 int size;
4498
4499 /* Massage our args to the form they really have. */
4500 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
4501 info = (struct bfd_link_info *) in_arg;
4502
4503 htab = ppc_hash_table (info);
4504 stub_sec = stub_entry->stub_sec;
4505
4506 /* Make a note of the offset within the stubs for this entry. */
4507 stub_entry->stub_offset = stub_sec->_cooked_size;
4508 loc = stub_sec->contents + stub_entry->stub_offset;
4509
4510 stub_bfd = stub_sec->owner;
4511
4512 switch (stub_entry->stub_type)
4513 {
4514 case ppc_stub_long_branch:
4515 /* Branches are relative. This is where we are going to. */
4516 off = (stub_entry->target_value
4517 + stub_entry->target_section->output_offset
4518 + stub_entry->target_section->output_section->vma);
4519
4520 /* And this is where we are coming from. */
4521 off -= (stub_entry->stub_offset
4522 + stub_sec->output_offset
4523 + stub_sec->output_section->vma);
4524
4525 BFD_ASSERT (off + (1 << 25) < (bfd_vma) (1 << 26));
4526
4527 bfd_put_32 (stub_bfd, (bfd_vma) B_DOT | (off & 0x3fffffc), loc);
4528 size = 4;
4529 break;
4530
4531 case ppc_stub_plt_branch:
4532 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
4533 stub_entry->root.string + 9,
4534 false, false);
4535 if (br_entry == NULL)
4536 {
4537 (*_bfd_error_handler) (_("can't find branch stub `%s'"),
4538 stub_entry->root.string + 9);
4539 htab->stub_error = true;
4540 return false;
4541 }
4542
4543 off = (stub_entry->target_value
4544 + stub_entry->target_section->output_offset
4545 + stub_entry->target_section->output_section->vma);
4546
4547 bfd_put_64 (htab->sbrlt->owner, off,
4548 htab->sbrlt->contents + br_entry->offset);
4549
4550 if (info->shared)
4551 {
4552 /* Create a reloc for the branch lookup table entry. */
4553 Elf_Internal_Rela rela;
4554 Elf64_External_Rela *r;
4555
4556 rela.r_offset = (br_entry->offset
4557 + htab->sbrlt->output_offset
4558 + htab->sbrlt->output_section->vma);
4559 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
4560 rela.r_addend = off;
4561
4562 r = (Elf64_External_Rela *) htab->srelbrlt->contents;
4563 r += htab->srelbrlt->reloc_count++;
4564 bfd_elf64_swap_reloca_out (htab->srelbrlt->owner, &rela, r);
4565 }
4566
4567 off = (br_entry->offset
4568 + htab->sbrlt->output_offset
4569 + htab->sbrlt->output_section->vma
4570 - elf_gp (htab->sbrlt->output_section->owner)
4571 - TOC_BASE_OFF);
4572
4573 if (off + 0x80000000 > 0xffffffff || (off & 7) != 0)
4574 {
4575 (*_bfd_error_handler)
4576 (_("linkage table error against `%s'"),
4577 stub_entry->root.string);
4578 bfd_set_error (bfd_error_bad_value);
4579 htab->stub_error = true;
4580 return false;
4581 }
4582
4583 indx = off;
4584 bfd_put_32 (stub_bfd, (bfd_vma) ADDIS_R12_R2 | PPC_HA (indx), loc);
4585 bfd_put_32 (stub_bfd, (bfd_vma) LD_R11_0R12 | PPC_LO (indx), loc + 4);
4586 bfd_put_32 (stub_bfd, (bfd_vma) MTCTR_R11, loc + 8);
4587 bfd_put_32 (stub_bfd, (bfd_vma) BCTR, loc + 12);
4588 size = 16;
4589 break;
4590
4591 case ppc_stub_plt_call:
4592 /* Build the .glink lazy link call stub. */
4593 p = htab->sglink->contents + htab->sglink->_cooked_size;
4594 indx = htab->sglink->reloc_count;
4595 if (indx < 0x8000)
4596 {
4597 bfd_put_32 (htab->sglink->owner, LI_R0_0 | indx, p);
4598 p += 4;
4599 }
4600 else
4601 {
4602 bfd_put_32 (htab->sglink->owner, LIS_R0_0 | PPC_HI (indx), p);
4603 p += 4;
4604 bfd_put_32 (htab->sglink->owner, ORI_R0_R0_0 | PPC_LO (indx), p);
4605 p += 4;
4606 }
4607 bfd_put_32 (htab->sglink->owner,
4608 B_DOT | ((htab->sglink->contents - p) & 0x3fffffc), p);
4609 p += 4;
4610 htab->sglink->_cooked_size = p - htab->sglink->contents;
4611 htab->sglink->reloc_count += 1;
4612
4613 /* Do the best we can for shared libraries built without
4614 exporting ".foo" for each "foo". This can happen when symbol
4615 versioning scripts strip all bar a subset of symbols. */
4616 if (stub_entry->h->oh->root.type != bfd_link_hash_defined
4617 && stub_entry->h->oh->root.type != bfd_link_hash_defweak)
4618 {
4619 /* Point the symbol at the stub. There may be multiple stubs,
4620 we don't really care; The main thing is to make this sym
4621 defined somewhere. */
4622 stub_entry->h->oh->root.type = bfd_link_hash_defined;
4623 stub_entry->h->oh->root.u.def.section = stub_entry->stub_sec;
4624 stub_entry->h->oh->root.u.def.value = stub_entry->stub_offset;
4625 }
4626
4627 /* Now build the stub. */
4628 off = stub_entry->h->elf.plt.offset;
4629 if (off >= (bfd_vma) -2)
4630 abort ();
4631
4632 off &= ~ (bfd_vma) 1;
4633 off += (htab->splt->output_offset
4634 + htab->splt->output_section->vma
4635 - elf_gp (htab->splt->output_section->owner)
4636 - TOC_BASE_OFF);
4637
4638 if (off + 0x80000000 > 0xffffffff || (off & 7) != 0)
4639 {
4640 (*_bfd_error_handler)
4641 (_("linkage table error against `%s'"),
4642 stub_entry->h->elf.root.root.string);
4643 bfd_set_error (bfd_error_bad_value);
4644 htab->stub_error = true;
4645 return false;
4646 }
4647
4648 p = build_plt_stub (stub_bfd, loc, (int) off, 0);
4649 size = p - loc;
4650 break;
4651
4652 default:
4653 BFD_FAIL ();
4654 return false;
4655 }
4656
4657 stub_sec->_cooked_size += size;
4658 return true;
4659 }
4660
4661 /* As above, but don't actually build the stub. Just bump offset so
4662 we know stub section sizes, and select plt_branch stubs where
4663 long_branch stubs won't do. */
4664
4665 static boolean
4666 ppc_size_one_stub (gen_entry, in_arg)
4667 struct bfd_hash_entry *gen_entry;
4668 PTR in_arg;
4669 {
4670 struct ppc_stub_hash_entry *stub_entry;
4671 struct ppc_link_hash_table *htab;
4672 bfd_vma off;
4673 int size;
4674
4675 /* Massage our args to the form they really have. */
4676 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
4677 htab = (struct ppc_link_hash_table *) in_arg;
4678
4679 if (stub_entry->stub_type == ppc_stub_plt_call)
4680 {
4681 off = stub_entry->h->elf.plt.offset & ~(bfd_vma) 1;
4682 off += (htab->splt->output_offset
4683 + htab->splt->output_section->vma
4684 - elf_gp (htab->splt->output_section->owner)
4685 - TOC_BASE_OFF);
4686
4687 size = 28;
4688 if (PPC_HA ((int) off + 16) != PPC_HA ((int) off))
4689 size += 4;
4690 }
4691 else
4692 {
4693 /* ppc_stub_long_branch or ppc_stub_plt_branch. */
4694 stub_entry->stub_type = ppc_stub_long_branch;
4695 size = 4;
4696
4697 off = (stub_entry->target_value
4698 + stub_entry->target_section->output_offset
4699 + stub_entry->target_section->output_section->vma);
4700 off -= (stub_entry->stub_sec->_raw_size
4701 + stub_entry->stub_sec->output_offset
4702 + stub_entry->stub_sec->output_section->vma);
4703
4704 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
4705 {
4706 struct ppc_branch_hash_entry *br_entry;
4707
4708 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
4709 stub_entry->root.string + 9,
4710 true, false);
4711 if (br_entry == NULL)
4712 {
4713 (*_bfd_error_handler) (_("can't build branch stub `%s'"),
4714 stub_entry->root.string + 9);
4715 htab->stub_error = true;
4716 return false;
4717 }
4718
4719 if (br_entry->iter != htab->stub_iteration)
4720 {
4721 br_entry->iter = htab->stub_iteration;
4722 br_entry->offset = htab->sbrlt->_raw_size;
4723 htab->sbrlt->_raw_size += 8;
4724 }
4725 stub_entry->stub_type = ppc_stub_plt_branch;
4726 size = 16;
4727 }
4728 }
4729
4730 stub_entry->stub_sec->_raw_size += size;
4731 return true;
4732 }
4733
4734 /* Set up various things so that we can make a list of input sections
4735 for each output section included in the link. Returns -1 on error,
4736 0 when no stubs will be needed, and 1 on success. */
4737
4738 int
4739 ppc64_elf_setup_section_lists (output_bfd, info)
4740 bfd *output_bfd;
4741 struct bfd_link_info *info;
4742 {
4743 bfd *input_bfd;
4744 int top_id, top_index;
4745 asection *section;
4746 asection **input_list, **list;
4747 bfd_size_type amt;
4748 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4749
4750 if (htab->elf.root.creator->flavour != bfd_target_elf_flavour
4751 || htab->sbrlt == NULL)
4752 return 0;
4753
4754 /* Find the top input section id. */
4755 for (input_bfd = info->input_bfds, top_id = 0;
4756 input_bfd != NULL;
4757 input_bfd = input_bfd->link_next)
4758 {
4759 for (section = input_bfd->sections;
4760 section != NULL;
4761 section = section->next)
4762 {
4763 if (top_id < section->id)
4764 top_id = section->id;
4765 }
4766 }
4767
4768 amt = sizeof (struct map_stub) * (top_id + 1);
4769 htab->stub_group = (struct map_stub *) bfd_zmalloc (amt);
4770 if (htab->stub_group == NULL)
4771 return -1;
4772
4773 /* We can't use output_bfd->section_count here to find the top output
4774 section index as some sections may have been removed, and
4775 _bfd_strip_section_from_output doesn't renumber the indices. */
4776 for (section = output_bfd->sections, top_index = 0;
4777 section != NULL;
4778 section = section->next)
4779 {
4780 if (top_index < section->index)
4781 top_index = section->index;
4782 }
4783
4784 htab->top_index = top_index;
4785 amt = sizeof (asection *) * (top_index + 1);
4786 input_list = (asection **) bfd_malloc (amt);
4787 htab->input_list = input_list;
4788 if (input_list == NULL)
4789 return -1;
4790
4791 /* For sections we aren't interested in, mark their entries with a
4792 value we can check later. */
4793 list = input_list + top_index;
4794 do
4795 *list = bfd_abs_section_ptr;
4796 while (list-- != input_list);
4797
4798 for (section = output_bfd->sections;
4799 section != NULL;
4800 section = section->next)
4801 {
4802 if ((section->flags & SEC_CODE) != 0)
4803 input_list[section->index] = NULL;
4804 }
4805
4806 return 1;
4807 }
4808
4809 /* The linker repeatedly calls this function for each input section,
4810 in the order that input sections are linked into output sections.
4811 Build lists of input sections to determine groupings between which
4812 we may insert linker stubs. */
4813
4814 void
4815 ppc64_elf_next_input_section (info, isec)
4816 struct bfd_link_info *info;
4817 asection *isec;
4818 {
4819 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4820
4821 if (isec->output_section->index <= htab->top_index)
4822 {
4823 asection **list = htab->input_list + isec->output_section->index;
4824 if (*list != bfd_abs_section_ptr)
4825 {
4826 /* Steal the link_sec pointer for our list. */
4827 #define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
4828 /* This happens to make the list in reverse order,
4829 which is what we want. */
4830 PREV_SEC (isec) = *list;
4831 *list = isec;
4832 }
4833 }
4834 }
4835
4836 /* See whether we can group stub sections together. Grouping stub
4837 sections may result in fewer stubs. More importantly, we need to
4838 put all .init* and .fini* stubs at the beginning of the .init or
4839 .fini output sections respectively, because glibc splits the
4840 _init and _fini functions into multiple parts. Putting a stub in
4841 the middle of a function is not a good idea. */
4842
4843 static void
4844 group_sections (htab, stub_group_size, stubs_always_before_branch)
4845 struct ppc_link_hash_table *htab;
4846 bfd_size_type stub_group_size;
4847 boolean stubs_always_before_branch;
4848 {
4849 asection **list = htab->input_list + htab->top_index;
4850 do
4851 {
4852 asection *tail = *list;
4853 if (tail == bfd_abs_section_ptr)
4854 continue;
4855 while (tail != NULL)
4856 {
4857 asection *curr;
4858 asection *prev;
4859 bfd_size_type total;
4860
4861 curr = tail;
4862 if (tail->_cooked_size)
4863 total = tail->_cooked_size;
4864 else
4865 total = tail->_raw_size;
4866 while ((prev = PREV_SEC (curr)) != NULL
4867 && ((total += curr->output_offset - prev->output_offset)
4868 < stub_group_size))
4869 curr = prev;
4870
4871 /* OK, the size from the start of CURR to the end is less
4872 than stub_group_size and thus can be handled by one stub
4873 section. (or the tail section is itself larger than
4874 stub_group_size, in which case we may be toast.) We
4875 should really be keeping track of the total size of stubs
4876 added here, as stubs contribute to the final output
4877 section size. That's a little tricky, and this way will
4878 only break if stubs added make the total size more than
4879 2^25, ie. for the default stub_group_size, if stubs total
4880 more than 2834432 bytes, or over 100000 plt call stubs. */
4881 do
4882 {
4883 prev = PREV_SEC (tail);
4884 /* Set up this stub group. */
4885 htab->stub_group[tail->id].link_sec = curr;
4886 }
4887 while (tail != curr && (tail = prev) != NULL);
4888
4889 /* But wait, there's more! Input sections up to stub_group_size
4890 bytes before the stub section can be handled by it too. */
4891 if (!stubs_always_before_branch)
4892 {
4893 total = 0;
4894 while (prev != NULL
4895 && ((total += tail->output_offset - prev->output_offset)
4896 < stub_group_size))
4897 {
4898 tail = prev;
4899 prev = PREV_SEC (tail);
4900 htab->stub_group[tail->id].link_sec = curr;
4901 }
4902 }
4903 tail = prev;
4904 }
4905 }
4906 while (list-- != htab->input_list);
4907 free (htab->input_list);
4908 #undef PREV_SEC
4909 }
4910
4911 /* Determine and set the size of the stub section for a final link.
4912
4913 The basic idea here is to examine all the relocations looking for
4914 PC-relative calls to a target that is unreachable with a "bl"
4915 instruction. */
4916
4917 boolean
4918 ppc64_elf_size_stubs (output_bfd, stub_bfd, info, group_size,
4919 add_stub_section, layout_sections_again)
4920 bfd *output_bfd;
4921 bfd *stub_bfd;
4922 struct bfd_link_info *info;
4923 bfd_signed_vma group_size;
4924 asection * (*add_stub_section) PARAMS ((const char *, asection *));
4925 void (*layout_sections_again) PARAMS ((void));
4926 {
4927 bfd_size_type stub_group_size;
4928 boolean stubs_always_before_branch;
4929 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4930
4931 /* Stash our params away. */
4932 htab->stub_bfd = stub_bfd;
4933 htab->add_stub_section = add_stub_section;
4934 htab->layout_sections_again = layout_sections_again;
4935 stubs_always_before_branch = group_size < 0;
4936 if (group_size < 0)
4937 stub_group_size = -group_size;
4938 else
4939 stub_group_size = group_size;
4940 if (stub_group_size == 1)
4941 {
4942 /* Default values. */
4943 stub_group_size = 30720000;
4944 if (htab->has_14bit_branch)
4945 stub_group_size = 30000;
4946 }
4947
4948 group_sections (htab, stub_group_size, stubs_always_before_branch);
4949
4950 while (1)
4951 {
4952 bfd *input_bfd;
4953 unsigned int bfd_indx;
4954 asection *stub_sec;
4955 boolean stub_changed;
4956
4957 htab->stub_iteration += 1;
4958 stub_changed = false;
4959
4960 for (input_bfd = info->input_bfds, bfd_indx = 0;
4961 input_bfd != NULL;
4962 input_bfd = input_bfd->link_next, bfd_indx++)
4963 {
4964 Elf_Internal_Shdr *symtab_hdr;
4965 asection *section;
4966 Elf_Internal_Sym *local_syms = NULL;
4967
4968 /* We'll need the symbol table in a second. */
4969 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4970 if (symtab_hdr->sh_info == 0)
4971 continue;
4972
4973 /* Walk over each section attached to the input bfd. */
4974 for (section = input_bfd->sections;
4975 section != NULL;
4976 section = section->next)
4977 {
4978 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
4979
4980 /* If there aren't any relocs, then there's nothing more
4981 to do. */
4982 if ((section->flags & SEC_RELOC) == 0
4983 || section->reloc_count == 0)
4984 continue;
4985
4986 /* If this section is a link-once section that will be
4987 discarded, then don't create any stubs. */
4988 if (section->output_section == NULL
4989 || section->output_section->owner != output_bfd)
4990 continue;
4991
4992 /* Get the relocs. */
4993 internal_relocs
4994 = _bfd_elf64_link_read_relocs (input_bfd, section, NULL,
4995 (Elf_Internal_Rela *) NULL,
4996 info->keep_memory);
4997 if (internal_relocs == NULL)
4998 goto error_ret_free_local;
4999
5000 /* Now examine each relocation. */
5001 irela = internal_relocs;
5002 irelaend = irela + section->reloc_count;
5003 for (; irela < irelaend; irela++)
5004 {
5005 unsigned int r_type, r_indx;
5006 enum ppc_stub_type stub_type;
5007 struct ppc_stub_hash_entry *stub_entry;
5008 asection *sym_sec;
5009 bfd_vma sym_value;
5010 bfd_vma destination;
5011 struct ppc_link_hash_entry *hash;
5012 char *stub_name;
5013 const asection *id_sec;
5014
5015 r_type = ELF64_R_TYPE (irela->r_info);
5016 r_indx = ELF64_R_SYM (irela->r_info);
5017
5018 if (r_type >= (unsigned int) R_PPC_max)
5019 {
5020 bfd_set_error (bfd_error_bad_value);
5021 goto error_ret_free_internal;
5022 }
5023
5024 /* Only look for stubs on branch instructions. */
5025 if (r_type != (unsigned int) R_PPC64_REL24
5026 && r_type != (unsigned int) R_PPC64_REL14
5027 && r_type != (unsigned int) R_PPC64_REL14_BRTAKEN
5028 && r_type != (unsigned int) R_PPC64_REL14_BRNTAKEN)
5029 continue;
5030
5031 /* Now determine the call target, its name, value,
5032 section. */
5033 sym_sec = NULL;
5034 sym_value = 0;
5035 destination = 0;
5036 hash = NULL;
5037 if (r_indx < symtab_hdr->sh_info)
5038 {
5039 /* It's a local symbol. */
5040 Elf_Internal_Sym *sym;
5041 Elf_Internal_Shdr *hdr;
5042
5043 if (local_syms == NULL)
5044 {
5045 local_syms
5046 = (Elf_Internal_Sym *) symtab_hdr->contents;
5047 if (local_syms == NULL)
5048 local_syms
5049 = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
5050 symtab_hdr->sh_info, 0,
5051 NULL, NULL, NULL);
5052 if (local_syms == NULL)
5053 goto error_ret_free_internal;
5054 }
5055 sym = local_syms + r_indx;
5056 hdr = elf_elfsections (input_bfd)[sym->st_shndx];
5057 sym_sec = hdr->bfd_section;
5058 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
5059 sym_value = sym->st_value;
5060 destination = (sym_value + irela->r_addend
5061 + sym_sec->output_offset
5062 + sym_sec->output_section->vma);
5063 }
5064 else
5065 {
5066 /* It's an external symbol. */
5067 int e_indx;
5068
5069 e_indx = r_indx - symtab_hdr->sh_info;
5070 hash = ((struct ppc_link_hash_entry *)
5071 elf_sym_hashes (input_bfd)[e_indx]);
5072
5073 while (hash->elf.root.type == bfd_link_hash_indirect
5074 || hash->elf.root.type == bfd_link_hash_warning)
5075 hash = ((struct ppc_link_hash_entry *)
5076 hash->elf.root.u.i.link);
5077
5078 if (hash->elf.root.type == bfd_link_hash_defined
5079 || hash->elf.root.type == bfd_link_hash_defweak)
5080 {
5081 sym_sec = hash->elf.root.u.def.section;
5082 sym_value = hash->elf.root.u.def.value;
5083 if (sym_sec->output_section != NULL)
5084 destination = (sym_value + irela->r_addend
5085 + sym_sec->output_offset
5086 + sym_sec->output_section->vma);
5087 }
5088 else if (hash->elf.root.type == bfd_link_hash_undefweak)
5089 ;
5090 else if (hash->elf.root.type == bfd_link_hash_undefined)
5091 ;
5092 else
5093 {
5094 bfd_set_error (bfd_error_bad_value);
5095 goto error_ret_free_internal;
5096 }
5097 }
5098
5099 /* Determine what (if any) linker stub is needed. */
5100 stub_type = ppc_type_of_stub (section, irela, &hash,
5101 destination);
5102 if (stub_type == ppc_stub_none)
5103 continue;
5104
5105 /* Support for grouping stub sections. */
5106 id_sec = htab->stub_group[section->id].link_sec;
5107
5108 /* Get the name of this stub. */
5109 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
5110 if (!stub_name)
5111 goto error_ret_free_internal;
5112
5113 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
5114 stub_name, false, false);
5115 if (stub_entry != NULL)
5116 {
5117 /* The proper stub has already been created. */
5118 free (stub_name);
5119 continue;
5120 }
5121
5122 stub_entry = ppc_add_stub (stub_name, section, htab);
5123 if (stub_entry == NULL)
5124 {
5125 free (stub_name);
5126 error_ret_free_internal:
5127 if (elf_section_data (section)->relocs == NULL)
5128 free (internal_relocs);
5129 error_ret_free_local:
5130 if (local_syms != NULL
5131 && (symtab_hdr->contents
5132 != (unsigned char *) local_syms))
5133 free (local_syms);
5134 return false;
5135 }
5136
5137 stub_entry->target_value = sym_value;
5138 stub_entry->target_section = sym_sec;
5139 stub_entry->stub_type = stub_type;
5140 stub_entry->h = hash;
5141 stub_changed = true;
5142 }
5143
5144 /* We're done with the internal relocs, free them. */
5145 if (elf_section_data (section)->relocs != internal_relocs)
5146 free (internal_relocs);
5147 }
5148
5149 if (local_syms != NULL
5150 && symtab_hdr->contents != (unsigned char *) local_syms)
5151 {
5152 if (!info->keep_memory)
5153 free (local_syms);
5154 else
5155 symtab_hdr->contents = (unsigned char *) local_syms;
5156 }
5157 }
5158
5159 if (!stub_changed)
5160 break;
5161
5162 /* OK, we've added some stubs. Find out the new size of the
5163 stub sections. */
5164 for (stub_sec = htab->stub_bfd->sections;
5165 stub_sec != NULL;
5166 stub_sec = stub_sec->next)
5167 {
5168 stub_sec->_raw_size = 0;
5169 stub_sec->_cooked_size = 0;
5170 }
5171 htab->sbrlt->_raw_size = 0;
5172 htab->sbrlt->_cooked_size = 0;
5173
5174 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, htab);
5175
5176 /* Ask the linker to do its stuff. */
5177 (*htab->layout_sections_again) ();
5178 }
5179
5180 /* It would be nice to strip .branch_lt from the output if the
5181 section is empty, but it's too late. If we strip sections here,
5182 the dynamic symbol table is corrupted since the section symbol
5183 for the stripped section isn't written. */
5184
5185 return true;
5186 }
5187
5188 /* Called after we have determined section placement. If sections
5189 move, we'll be called again. Provide a value for TOCstart. */
5190
5191 bfd_vma
5192 ppc64_elf_toc (obfd)
5193 bfd *obfd;
5194 {
5195 asection *s;
5196 bfd_vma TOCstart;
5197
5198 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
5199 order. The TOC starts where the first of these sections starts. */
5200 s = bfd_get_section_by_name (obfd, ".got");
5201 if (s == NULL)
5202 s = bfd_get_section_by_name (obfd, ".toc");
5203 if (s == NULL)
5204 s = bfd_get_section_by_name (obfd, ".tocbss");
5205 if (s == NULL)
5206 s = bfd_get_section_by_name (obfd, ".plt");
5207 if (s == NULL)
5208 {
5209 /* This may happen for
5210 o references to TOC base (SYM@toc / TOC[tc0]) without a
5211 .toc directive
5212 o bad linker script
5213 o --gc-sections and empty TOC sections
5214
5215 FIXME: Warn user? */
5216
5217 /* Look for a likely section. We probably won't even be
5218 using TOCstart. */
5219 for (s = obfd->sections; s != NULL; s = s->next)
5220 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY))
5221 == (SEC_ALLOC | SEC_SMALL_DATA))
5222 break;
5223 if (s == NULL)
5224 for (s = obfd->sections; s != NULL; s = s->next)
5225 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA))
5226 == (SEC_ALLOC | SEC_SMALL_DATA))
5227 break;
5228 if (s == NULL)
5229 for (s = obfd->sections; s != NULL; s = s->next)
5230 if ((s->flags & (SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
5231 break;
5232 if (s == NULL)
5233 for (s = obfd->sections; s != NULL; s = s->next)
5234 if ((s->flags & SEC_ALLOC) == SEC_ALLOC)
5235 break;
5236 }
5237
5238 TOCstart = 0;
5239 if (s != NULL)
5240 TOCstart = s->output_section->vma + s->output_offset;
5241
5242 return TOCstart;
5243 }
5244
5245 /* Build all the stubs associated with the current output file.
5246 The stubs are kept in a hash table attached to the main linker
5247 hash table. This function is called via gldelf64ppc_finish. */
5248
5249 boolean
5250 ppc64_elf_build_stubs (info)
5251 struct bfd_link_info *info;
5252 {
5253 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5254 asection *stub_sec;
5255 bfd_vma plt_r2;
5256 bfd_byte *p;
5257
5258 for (stub_sec = htab->stub_bfd->sections;
5259 stub_sec != NULL;
5260 stub_sec = stub_sec->next)
5261 {
5262 bfd_size_type size;
5263
5264 /* Allocate memory to hold the linker stubs. */
5265 size = stub_sec->_raw_size;
5266 if (size != 0)
5267 {
5268 stub_sec->contents = (bfd_byte *) bfd_zalloc (htab->stub_bfd, size);
5269 if (stub_sec->contents == NULL)
5270 return false;
5271 }
5272 stub_sec->_cooked_size = 0;
5273 }
5274
5275 if (htab->splt != NULL)
5276 {
5277 /* Build the .glink plt call stub. */
5278 plt_r2 = (htab->splt->output_offset
5279 + htab->splt->output_section->vma
5280 - elf_gp (htab->splt->output_section->owner)
5281 - TOC_BASE_OFF);
5282 p = htab->sglink->contents;
5283 p = build_plt_stub (htab->sglink->owner, p, (int) plt_r2, 1);
5284 while (p - htab->sglink->contents < GLINK_CALL_STUB_SIZE)
5285 {
5286 bfd_put_32 (htab->sglink->owner, NOP, p);
5287 p += 4;
5288 }
5289 htab->sglink->_cooked_size = p - htab->sglink->contents;
5290
5291 /* Use reloc_count to count entries. */
5292 htab->sglink->reloc_count = 0;
5293 }
5294
5295 if (htab->sbrlt->_raw_size != 0)
5296 {
5297 htab->sbrlt->contents = (bfd_byte *) bfd_zalloc (htab->sbrlt->owner,
5298 htab->sbrlt->_raw_size);
5299 if (htab->sbrlt->contents == NULL)
5300 return false;
5301 }
5302
5303 /* Build the stubs as directed by the stub hash table. */
5304 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
5305 htab->sglink->reloc_count = 0;
5306
5307 for (stub_sec = htab->stub_bfd->sections;
5308 stub_sec != NULL;
5309 stub_sec = stub_sec->next)
5310 {
5311 if (stub_sec->_raw_size != stub_sec->_cooked_size)
5312 break;
5313 }
5314
5315 if (stub_sec != NULL
5316 || htab->sglink->_raw_size != htab->sglink->_cooked_size)
5317 {
5318 htab->stub_error = true;
5319 (*_bfd_error_handler) (_("stubs don't match calculated size"));
5320 }
5321
5322 return !htab->stub_error;
5323 }
5324
5325 /* Set up any other section flags and such that may be necessary. */
5326
5327 static boolean
5328 ppc64_elf_fake_sections (abfd, shdr, asect)
5329 bfd *abfd ATTRIBUTE_UNUSED;
5330 Elf64_Internal_Shdr *shdr;
5331 asection *asect;
5332 {
5333 if ((asect->flags & SEC_EXCLUDE) != 0)
5334 shdr->sh_flags |= SHF_EXCLUDE;
5335
5336 if ((asect->flags & SEC_SORT_ENTRIES) != 0)
5337 shdr->sh_type = SHT_ORDERED;
5338
5339 return true;
5340 }
5341
5342 /* The RELOCATE_SECTION function is called by the ELF backend linker
5343 to handle the relocations for a section.
5344
5345 The relocs are always passed as Rela structures; if the section
5346 actually uses Rel structures, the r_addend field will always be
5347 zero.
5348
5349 This function is responsible for adjust the section contents as
5350 necessary, and (if using Rela relocs and generating a
5351 relocateable output file) adjusting the reloc addend as
5352 necessary.
5353
5354 This function does not have to worry about setting the reloc
5355 address or the reloc symbol index.
5356
5357 LOCAL_SYMS is a pointer to the swapped in local symbols.
5358
5359 LOCAL_SECTIONS is an array giving the section in the input file
5360 corresponding to the st_shndx field of each local symbol.
5361
5362 The global hash table entry for the global symbols can be found
5363 via elf_sym_hashes (input_bfd).
5364
5365 When generating relocateable output, this function must handle
5366 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
5367 going to be the section symbol corresponding to the output
5368 section, which means that the addend must be adjusted
5369 accordingly. */
5370
5371 static boolean
5372 ppc64_elf_relocate_section (output_bfd, info, input_bfd, input_section,
5373 contents, relocs, local_syms, local_sections)
5374 bfd *output_bfd;
5375 struct bfd_link_info *info;
5376 bfd *input_bfd;
5377 asection *input_section;
5378 bfd_byte *contents;
5379 Elf_Internal_Rela *relocs;
5380 Elf_Internal_Sym *local_syms;
5381 asection **local_sections;
5382 {
5383 struct ppc_link_hash_table *htab;
5384 Elf_Internal_Shdr *symtab_hdr;
5385 struct elf_link_hash_entry **sym_hashes;
5386 Elf_Internal_Rela *rel;
5387 Elf_Internal_Rela *relend;
5388 bfd_vma *local_got_offsets;
5389 bfd_vma TOCstart;
5390 boolean ret = true;
5391 boolean is_opd;
5392 /* Disabled until we sort out how ld should choose 'y' vs 'at'. */
5393 boolean is_power4 = false;
5394
5395 if (info->relocateable)
5396 return true;
5397
5398 /* Initialize howto table if needed. */
5399 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5400 ppc_howto_init ();
5401
5402 htab = ppc_hash_table (info);
5403 local_got_offsets = elf_local_got_offsets (input_bfd);
5404 TOCstart = elf_gp (output_bfd);
5405 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5406 sym_hashes = elf_sym_hashes (input_bfd);
5407 is_opd = elf_section_data (input_section)->tdata != NULL;
5408
5409 rel = relocs;
5410 relend = relocs + input_section->reloc_count;
5411 for (; rel < relend; rel++)
5412 {
5413 enum elf_ppc_reloc_type r_type;
5414 bfd_vma offset;
5415 bfd_vma addend;
5416 bfd_reloc_status_type r;
5417 Elf_Internal_Sym *sym;
5418 asection *sec;
5419 struct elf_link_hash_entry *h;
5420 struct elf_link_hash_entry *fdh;
5421 const char *sym_name;
5422 unsigned long r_symndx;
5423 bfd_vma relocation;
5424 boolean unresolved_reloc;
5425 boolean warned;
5426 long insn;
5427 struct ppc_stub_hash_entry *stub_entry;
5428 bfd_vma max_br_offset;
5429 bfd_vma from;
5430
5431 r_type = (enum elf_ppc_reloc_type) ELF64_R_TYPE (rel->r_info);
5432 r_symndx = ELF64_R_SYM (rel->r_info);
5433 offset = rel->r_offset;
5434 addend = rel->r_addend;
5435 r = bfd_reloc_other;
5436 sym = (Elf_Internal_Sym *) 0;
5437 sec = (asection *) 0;
5438 h = (struct elf_link_hash_entry *) 0;
5439 sym_name = (const char *) 0;
5440 unresolved_reloc = false;
5441 warned = false;
5442
5443 if (r_type == R_PPC64_TOC)
5444 {
5445 /* Relocation value is TOC base. Symbol is ignored. */
5446 relocation = TOCstart + TOC_BASE_OFF;
5447 }
5448 else if (r_symndx < symtab_hdr->sh_info)
5449 {
5450 /* It's a local symbol. */
5451 sym = local_syms + r_symndx;
5452 sec = local_sections[r_symndx];
5453 sym_name = "<local symbol>";
5454
5455 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, sec, rel);
5456 /* rel may have changed, update our copy of addend. */
5457 addend = rel->r_addend;
5458
5459 if (elf_section_data (sec) != NULL)
5460 {
5461 long *opd_sym_adjust;
5462
5463 opd_sym_adjust = (long *) elf_section_data (sec)->tdata;
5464 if (opd_sym_adjust != NULL && sym->st_value % 24 == 0)
5465 relocation += opd_sym_adjust[sym->st_value / 24];
5466 }
5467 }
5468 else
5469 {
5470 /* It's a global symbol. */
5471 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5472 while (h->root.type == bfd_link_hash_indirect
5473 || h->root.type == bfd_link_hash_warning)
5474 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5475 sym_name = h->root.root.string;
5476 relocation = 0;
5477 if (h->root.type == bfd_link_hash_defined
5478 || h->root.type == bfd_link_hash_defweak)
5479 {
5480 sec = h->root.u.def.section;
5481 if (sec->output_section == NULL)
5482 /* Set a flag that will be cleared later if we find a
5483 relocation value for this symbol. output_section
5484 is typically NULL for symbols satisfied by a shared
5485 library. */
5486 unresolved_reloc = true;
5487 else
5488 relocation = (h->root.u.def.value
5489 + sec->output_section->vma
5490 + sec->output_offset);
5491 }
5492 else if (h->root.type == bfd_link_hash_undefweak)
5493 ;
5494 else if (info->shared
5495 && (!info->symbolic || info->allow_shlib_undefined)
5496 && !info->no_undefined
5497 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
5498 ;
5499 else
5500 {
5501 if (! ((*info->callbacks->undefined_symbol)
5502 (info, h->root.root.string, input_bfd, input_section,
5503 offset, (!info->shared
5504 || info->no_undefined
5505 || ELF_ST_VISIBILITY (h->other)))))
5506 return false;
5507 warned = true;
5508 }
5509 }
5510
5511 /* First handle relocations that tweak non-addend part of insn. */
5512 insn = 0;
5513 switch (r_type)
5514 {
5515 default:
5516 break;
5517
5518 /* Branch taken prediction relocations. */
5519 case R_PPC64_ADDR14_BRTAKEN:
5520 case R_PPC64_REL14_BRTAKEN:
5521 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
5522 /* Fall thru. */
5523
5524 /* Branch not taken prediction relocations. */
5525 case R_PPC64_ADDR14_BRNTAKEN:
5526 case R_PPC64_REL14_BRNTAKEN:
5527 insn |= bfd_get_32 (output_bfd, contents + offset) & ~(0x01 << 21);
5528 if (is_power4)
5529 {
5530 /* Set 'a' bit. This is 0b00010 in BO field for branch
5531 on CR(BI) insns (BO == 001at or 011at), and 0b01000
5532 for branch on CTR insns (BO == 1a00t or 1a01t). */
5533 if ((insn & (0x14 << 21)) == (0x04 << 21))
5534 insn |= 0x02 << 21;
5535 else if ((insn & (0x14 << 21)) == (0x10 << 21))
5536 insn |= 0x08 << 21;
5537 else
5538 break;
5539 }
5540 else
5541 {
5542 from = (offset
5543 + input_section->output_offset
5544 + input_section->output_section->vma);
5545
5546 /* Invert 'y' bit if not the default. */
5547 if ((bfd_signed_vma) (relocation + addend - from) < 0)
5548 insn ^= 0x01 << 21;
5549 }
5550
5551 bfd_put_32 (output_bfd, (bfd_vma) insn, contents + offset);
5552 break;
5553
5554 case R_PPC64_REL24:
5555 /* A REL24 branching to a linkage function is followed by a
5556 nop. We replace the nop with a ld in order to restore
5557 the TOC base pointer. Only calls to shared objects need
5558 to alter the TOC base. These are recognized by their
5559 need for a PLT entry. */
5560 if (h != NULL
5561 && (fdh = ((struct ppc_link_hash_entry *) h)->oh) != NULL
5562 && fdh->plt.offset != (bfd_vma) -1
5563 && (stub_entry = ppc_get_stub_entry (input_section, sec, fdh,
5564 rel, htab)) != NULL)
5565 {
5566 boolean can_plt_call = 0;
5567
5568 if (offset + 8 <= input_section->_cooked_size)
5569 {
5570 insn = bfd_get_32 (input_bfd, contents + offset + 4);
5571 if (insn == NOP
5572 || insn == CROR_151515 || insn == CROR_313131)
5573 {
5574 bfd_put_32 (input_bfd, (bfd_vma) LD_R2_40R1,
5575 contents + offset + 4);
5576 can_plt_call = 1;
5577 }
5578 }
5579
5580 if (!can_plt_call)
5581 {
5582 /* If this is a plain branch rather than a branch
5583 and link, don't require a nop. */
5584 insn = bfd_get_32 (input_bfd, contents + offset);
5585 if ((insn & 1) == 0)
5586 can_plt_call = 1;
5587 }
5588
5589 if (can_plt_call)
5590 {
5591 relocation = (stub_entry->stub_offset
5592 + stub_entry->stub_sec->output_offset
5593 + stub_entry->stub_sec->output_section->vma);
5594 addend = 0;
5595 unresolved_reloc = false;
5596 }
5597 }
5598
5599 if (h != NULL
5600 && h->root.type == bfd_link_hash_undefweak
5601 && relocation == 0
5602 && addend == 0)
5603 {
5604 /* Tweak calls to undefined weak functions to point at a
5605 blr. We can thus call a weak function without first
5606 checking whether the function is defined. We have a
5607 blr at the end of .sfpr. */
5608 BFD_ASSERT (htab->sfpr->_raw_size != 0);
5609 relocation = (htab->sfpr->_raw_size - 4
5610 + htab->sfpr->output_offset
5611 + htab->sfpr->output_section->vma);
5612 from = (offset
5613 + input_section->output_offset
5614 + input_section->output_section->vma);
5615
5616 /* But let's not be silly about it. If the blr isn't in
5617 reach, just go to the next instruction. */
5618 if (relocation - from + (1 << 25) >= (1 << 26)
5619 || htab->sfpr->_raw_size == 0)
5620 relocation = from + 4;
5621 }
5622 break;
5623 }
5624
5625 /* Set `addend'. */
5626 switch (r_type)
5627 {
5628 default:
5629 (*_bfd_error_handler)
5630 (_("%s: unknown relocation type %d for symbol %s"),
5631 bfd_archive_filename (input_bfd), (int) r_type, sym_name);
5632
5633 bfd_set_error (bfd_error_bad_value);
5634 ret = false;
5635 continue;
5636
5637 case R_PPC64_NONE:
5638 case R_PPC_GNU_VTINHERIT:
5639 case R_PPC_GNU_VTENTRY:
5640 continue;
5641
5642 /* GOT16 relocations. Like an ADDR16 using the symbol's
5643 address in the GOT as relocation value instead of the
5644 symbols value itself. Also, create a GOT entry for the
5645 symbol and put the symbol value there. */
5646 case R_PPC64_GOT16:
5647 case R_PPC64_GOT16_LO:
5648 case R_PPC64_GOT16_HI:
5649 case R_PPC64_GOT16_HA:
5650 case R_PPC64_GOT16_DS:
5651 case R_PPC64_GOT16_LO_DS:
5652 {
5653 /* Relocation is to the entry for this symbol in the global
5654 offset table. */
5655 bfd_vma off;
5656
5657 if (htab->sgot == NULL)
5658 abort ();
5659
5660 if (h != NULL)
5661 {
5662 boolean dyn;
5663
5664 off = h->got.offset;
5665 dyn = htab->elf.dynamic_sections_created;
5666 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info, h)
5667 || (info->shared
5668 && (info->symbolic
5669 || h->dynindx == -1
5670 || (h->elf_link_hash_flags
5671 & ELF_LINK_FORCED_LOCAL))
5672 && (h->elf_link_hash_flags
5673 & ELF_LINK_HASH_DEF_REGULAR)))
5674 {
5675 /* This is actually a static link, or it is a
5676 -Bsymbolic link and the symbol is defined
5677 locally, or the symbol was forced to be local
5678 because of a version file. We must initialize
5679 this entry in the global offset table. Since the
5680 offset must always be a multiple of 8, we use the
5681 least significant bit to record whether we have
5682 initialized it already.
5683
5684 When doing a dynamic link, we create a .rel.got
5685 relocation entry to initialize the value. This
5686 is done in the finish_dynamic_symbol routine. */
5687 if ((off & 1) != 0)
5688 off &= ~1;
5689 else
5690 {
5691 bfd_put_64 (output_bfd, relocation,
5692 htab->sgot->contents + off);
5693 h->got.offset |= 1;
5694 }
5695 }
5696 else
5697 unresolved_reloc = false;
5698 }
5699 else
5700 {
5701 if (local_got_offsets == NULL)
5702 abort ();
5703
5704 off = local_got_offsets[r_symndx];
5705
5706 /* The offset must always be a multiple of 8. We use
5707 the least significant bit to record whether we have
5708 already processed this entry. */
5709 if ((off & 1) != 0)
5710 off &= ~1;
5711 else
5712 {
5713 bfd_put_64 (output_bfd, relocation,
5714 htab->sgot->contents + off);
5715
5716 if (info->shared)
5717 {
5718 Elf_Internal_Rela outrel;
5719 Elf64_External_Rela *loc;
5720
5721 /* We need to generate a R_PPC64_RELATIVE reloc
5722 for the dynamic linker. */
5723 outrel.r_offset = (htab->sgot->output_section->vma
5724 + htab->sgot->output_offset
5725 + off);
5726 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
5727 outrel.r_addend = relocation;
5728 loc = (Elf64_External_Rela *) htab->srelgot->contents;
5729 loc += htab->srelgot->reloc_count++;
5730 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
5731 }
5732
5733 local_got_offsets[r_symndx] |= 1;
5734 }
5735 }
5736
5737 if (off >= (bfd_vma) -2)
5738 abort ();
5739
5740 relocation = htab->sgot->output_offset + off;
5741
5742 /* TOC base (r2) is TOC start plus 0x8000. */
5743 addend -= TOC_BASE_OFF;
5744 }
5745 break;
5746
5747 case R_PPC64_PLT16_HA:
5748 case R_PPC64_PLT16_HI:
5749 case R_PPC64_PLT16_LO:
5750 case R_PPC64_PLT32:
5751 case R_PPC64_PLT64:
5752 /* Relocation is to the entry for this symbol in the
5753 procedure linkage table. */
5754
5755 /* Resolve a PLT reloc against a local symbol directly,
5756 without using the procedure linkage table. */
5757 if (h == NULL)
5758 break;
5759
5760 if (h->plt.offset == (bfd_vma) -1
5761 || htab->splt == NULL)
5762 {
5763 /* We didn't make a PLT entry for this symbol. This
5764 happens when statically linking PIC code, or when
5765 using -Bsymbolic. */
5766 break;
5767 }
5768
5769 relocation = (htab->splt->output_section->vma
5770 + htab->splt->output_offset
5771 + h->plt.offset);
5772 unresolved_reloc = false;
5773 break;
5774
5775 /* TOC16 relocs. We want the offset relative to the TOC base,
5776 which is the address of the start of the TOC plus 0x8000.
5777 The TOC consists of sections .got, .toc, .tocbss, and .plt,
5778 in this order. */
5779 case R_PPC64_TOC16:
5780 case R_PPC64_TOC16_LO:
5781 case R_PPC64_TOC16_HI:
5782 case R_PPC64_TOC16_DS:
5783 case R_PPC64_TOC16_LO_DS:
5784 case R_PPC64_TOC16_HA:
5785 addend -= TOCstart + TOC_BASE_OFF;
5786 break;
5787
5788 /* Relocate against the beginning of the section. */
5789 case R_PPC64_SECTOFF:
5790 case R_PPC64_SECTOFF_LO:
5791 case R_PPC64_SECTOFF_HI:
5792 case R_PPC64_SECTOFF_DS:
5793 case R_PPC64_SECTOFF_LO_DS:
5794 case R_PPC64_SECTOFF_HA:
5795 if (sec != (asection *) 0)
5796 addend -= sec->output_section->vma;
5797 break;
5798
5799 case R_PPC64_REL14:
5800 case R_PPC64_REL14_BRNTAKEN:
5801 case R_PPC64_REL14_BRTAKEN:
5802 case R_PPC64_REL24:
5803 break;
5804
5805 /* Relocations that may need to be propagated if this is a
5806 dynamic object. */
5807 case R_PPC64_REL32:
5808 case R_PPC64_REL64:
5809 case R_PPC64_ADDR14:
5810 case R_PPC64_ADDR14_BRNTAKEN:
5811 case R_PPC64_ADDR14_BRTAKEN:
5812 case R_PPC64_ADDR16:
5813 case R_PPC64_ADDR16_DS:
5814 case R_PPC64_ADDR16_HA:
5815 case R_PPC64_ADDR16_HI:
5816 case R_PPC64_ADDR16_HIGHER:
5817 case R_PPC64_ADDR16_HIGHERA:
5818 case R_PPC64_ADDR16_HIGHEST:
5819 case R_PPC64_ADDR16_HIGHESTA:
5820 case R_PPC64_ADDR16_LO:
5821 case R_PPC64_ADDR16_LO_DS:
5822 case R_PPC64_ADDR24:
5823 case R_PPC64_ADDR30:
5824 case R_PPC64_ADDR32:
5825 case R_PPC64_ADDR64:
5826 case R_PPC64_UADDR16:
5827 case R_PPC64_UADDR32:
5828 case R_PPC64_UADDR64:
5829 /* r_symndx will be zero only for relocs against symbols
5830 from removed linkonce sections, or sections discarded by
5831 a linker script. */
5832 if (r_symndx == 0)
5833 break;
5834 /* Fall thru. */
5835
5836 case R_PPC64_TOC:
5837 if ((input_section->flags & SEC_ALLOC) == 0)
5838 break;
5839
5840 if (NO_OPD_RELOCS && is_opd)
5841 break;
5842
5843 if ((info->shared
5844 && (IS_ABSOLUTE_RELOC (r_type)
5845 || (h != NULL
5846 && h->dynindx != -1
5847 && (! info->symbolic
5848 || (h->elf_link_hash_flags
5849 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
5850 || (!info->shared
5851 && h != NULL
5852 && h->dynindx != -1
5853 && (h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
5854 && (((h->elf_link_hash_flags
5855 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
5856 && (h->elf_link_hash_flags
5857 & ELF_LINK_HASH_DEF_REGULAR) == 0)
5858 || h->root.type == bfd_link_hash_undefweak
5859 || h->root.type == bfd_link_hash_undefined)))
5860 {
5861 Elf_Internal_Rela outrel;
5862 boolean skip, relocate;
5863 asection *sreloc;
5864 Elf64_External_Rela *loc;
5865
5866 /* When generating a dynamic object, these relocations
5867 are copied into the output file to be resolved at run
5868 time. */
5869
5870 skip = false;
5871 relocate = false;
5872
5873 outrel.r_offset =
5874 _bfd_elf_section_offset (output_bfd, info, input_section,
5875 rel->r_offset);
5876 if (outrel.r_offset == (bfd_vma) -1)
5877 skip = true;
5878 else if (outrel.r_offset == (bfd_vma) -2)
5879 skip = true, relocate = true;
5880 outrel.r_offset += (input_section->output_section->vma
5881 + input_section->output_offset);
5882 outrel.r_addend = addend;
5883
5884 if (skip)
5885 memset (&outrel, 0, sizeof outrel);
5886 else if (h != NULL
5887 && h->dynindx != -1
5888 && !is_opd
5889 && (!IS_ABSOLUTE_RELOC (r_type)
5890 || !info->shared
5891 || !info->symbolic
5892 || (h->elf_link_hash_flags
5893 & ELF_LINK_HASH_DEF_REGULAR) == 0))
5894 outrel.r_info = ELF64_R_INFO (h->dynindx, r_type);
5895 else
5896 {
5897 /* This symbol is local, or marked to become local,
5898 or this is an opd section reloc which must point
5899 at a local function. */
5900 outrel.r_addend += relocation;
5901 relocate = true;
5902 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
5903 {
5904 if (is_opd && h != NULL)
5905 {
5906 /* Lie about opd entries. This case occurs
5907 when building shared libraries and we
5908 reference a function in another shared
5909 lib. The same thing happens for a weak
5910 definition in an application that's
5911 overridden by a strong definition in a
5912 shared lib. (I believe this is a generic
5913 bug in binutils handling of weak syms.)
5914 In these cases we won't use the opd
5915 entry in this lib. */
5916 unresolved_reloc = false;
5917 }
5918 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
5919 }
5920 else
5921 {
5922 long indx = 0;
5923
5924 if (bfd_is_abs_section (sec))
5925 ;
5926 else if (sec == NULL || sec->owner == NULL)
5927 {
5928 bfd_set_error (bfd_error_bad_value);
5929 return false;
5930 }
5931 else
5932 {
5933 asection *osec;
5934
5935 osec = sec->output_section;
5936 indx = elf_section_data (osec)->dynindx;
5937
5938 /* We are turning this relocation into one
5939 against a section symbol, so subtract out
5940 the output section's address but not the
5941 offset of the input section in the output
5942 section. */
5943 outrel.r_addend -= osec->vma;
5944 }
5945
5946 outrel.r_info = ELF64_R_INFO (indx, r_type);
5947 }
5948 }
5949
5950 sreloc = elf_section_data (input_section)->sreloc;
5951 if (sreloc == NULL)
5952 abort ();
5953
5954 loc = (Elf64_External_Rela *) sreloc->contents;
5955 loc += sreloc->reloc_count++;
5956 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
5957
5958 /* If this reloc is against an external symbol, it will
5959 be computed at runtime, so there's no need to do
5960 anything now. */
5961 if (! relocate)
5962 continue;
5963 }
5964 break;
5965
5966 case R_PPC64_COPY:
5967 case R_PPC64_GLOB_DAT:
5968 case R_PPC64_JMP_SLOT:
5969 case R_PPC64_RELATIVE:
5970 /* We shouldn't ever see these dynamic relocs in relocatable
5971 files. */
5972 /* Fall thru */
5973
5974 case R_PPC64_PLTGOT16:
5975 case R_PPC64_PLTGOT16_DS:
5976 case R_PPC64_PLTGOT16_HA:
5977 case R_PPC64_PLTGOT16_HI:
5978 case R_PPC64_PLTGOT16_LO:
5979 case R_PPC64_PLTGOT16_LO_DS:
5980 case R_PPC64_PLTREL32:
5981 case R_PPC64_PLTREL64:
5982 /* These ones haven't been implemented yet. */
5983
5984 (*_bfd_error_handler)
5985 (_("%s: Relocation %s is not supported for symbol %s."),
5986 bfd_archive_filename (input_bfd),
5987 ppc64_elf_howto_table[(int) r_type]->name, sym_name);
5988
5989 bfd_set_error (bfd_error_invalid_operation);
5990 ret = false;
5991 continue;
5992 }
5993
5994 /* Do any further special processing. */
5995 switch (r_type)
5996 {
5997 default:
5998 break;
5999
6000 case R_PPC64_ADDR16_HA:
6001 case R_PPC64_ADDR16_HIGHERA:
6002 case R_PPC64_ADDR16_HIGHESTA:
6003 case R_PPC64_PLT16_HA:
6004 case R_PPC64_TOC16_HA:
6005 case R_PPC64_SECTOFF_HA:
6006 /* It's just possible that this symbol is a weak symbol
6007 that's not actually defined anywhere. In that case,
6008 'sec' would be NULL, and we should leave the symbol
6009 alone (it will be set to zero elsewhere in the link). */
6010 if (sec != NULL)
6011 /* Add 0x10000 if sign bit in 0:15 is set. */
6012 addend += ((relocation + addend) & 0x8000) << 1;
6013 break;
6014
6015 case R_PPC64_ADDR16_DS:
6016 case R_PPC64_ADDR16_LO_DS:
6017 case R_PPC64_GOT16_DS:
6018 case R_PPC64_GOT16_LO_DS:
6019 case R_PPC64_PLT16_LO_DS:
6020 case R_PPC64_SECTOFF_DS:
6021 case R_PPC64_SECTOFF_LO_DS:
6022 case R_PPC64_TOC16_DS:
6023 case R_PPC64_TOC16_LO_DS:
6024 case R_PPC64_PLTGOT16_DS:
6025 case R_PPC64_PLTGOT16_LO_DS:
6026 if (((relocation + addend) & 3) != 0)
6027 {
6028 (*_bfd_error_handler)
6029 (_("%s: error: relocation %s not a multiple of 4"),
6030 bfd_archive_filename (input_bfd),
6031 ppc64_elf_howto_table[(int) r_type]->name);
6032 bfd_set_error (bfd_error_bad_value);
6033 ret = false;
6034 continue;
6035 }
6036 break;
6037
6038 case R_PPC64_REL14:
6039 case R_PPC64_REL14_BRNTAKEN:
6040 case R_PPC64_REL14_BRTAKEN:
6041 max_br_offset = 1 << 15;
6042 goto branch_check;
6043
6044 case R_PPC64_REL24:
6045 max_br_offset = 1 << 25;
6046
6047 branch_check:
6048 /* If the branch is out of reach, then redirect the
6049 call to the local stub for this function. */
6050 from = (offset
6051 + input_section->output_offset
6052 + input_section->output_section->vma);
6053 if (relocation + addend - from + max_br_offset >= 2 * max_br_offset
6054 && (stub_entry = ppc_get_stub_entry (input_section, sec, h,
6055 rel, htab)) != NULL)
6056 {
6057 /* Munge up the value and addend so that we call the stub
6058 rather than the procedure directly. */
6059 relocation = (stub_entry->stub_offset
6060 + stub_entry->stub_sec->output_offset
6061 + stub_entry->stub_sec->output_section->vma);
6062 addend = 0;
6063 }
6064 break;
6065 }
6066
6067 /* FIXME: Why do we allow debugging sections to escape this error?
6068 More importantly, why do we not emit dynamic relocs above in
6069 debugging sections (which are ! SEC_ALLOC)? If we had
6070 emitted the dynamic reloc, we could remove the fudge here. */
6071 if (unresolved_reloc
6072 && !(info->shared
6073 && (input_section->flags & SEC_DEBUGGING) != 0
6074 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0))
6075 {
6076 (*_bfd_error_handler)
6077 (_("%s(%s+0x%lx): unresolvable relocation against symbol `%s'"),
6078 bfd_archive_filename (input_bfd),
6079 bfd_get_section_name (input_bfd, input_section),
6080 (long) rel->r_offset,
6081 h->root.root.string);
6082 ret = false;
6083 }
6084
6085 r = _bfd_final_link_relocate (ppc64_elf_howto_table[(int) r_type],
6086 input_bfd,
6087 input_section,
6088 contents,
6089 offset,
6090 relocation,
6091 addend);
6092
6093 if (r != bfd_reloc_ok)
6094 {
6095 const char *name;
6096
6097 if (h != NULL)
6098 {
6099 if (h->root.type == bfd_link_hash_undefweak
6100 && ppc64_elf_howto_table[(int) r_type]->pc_relative)
6101 {
6102 /* Assume this is a call protected by other code that
6103 detects the symbol is undefined. If this is the case,
6104 we can safely ignore the overflow. If not, the
6105 program is hosed anyway, and a little warning isn't
6106 going to help. */
6107
6108 continue;
6109 }
6110
6111 name = h->root.root.string;
6112 }
6113 else
6114 {
6115 name = bfd_elf_string_from_elf_section (input_bfd,
6116 symtab_hdr->sh_link,
6117 sym->st_name);
6118 if (name == NULL)
6119 continue;
6120 if (*name == '\0')
6121 name = bfd_section_name (input_bfd, sec);
6122 }
6123
6124 if (r == bfd_reloc_overflow)
6125 {
6126 if (warned)
6127 continue;
6128 if (!((*info->callbacks->reloc_overflow)
6129 (info, name, ppc64_elf_howto_table[(int) r_type]->name,
6130 rel->r_addend, input_bfd, input_section, offset)))
6131 return false;
6132 }
6133 else
6134 {
6135 (*_bfd_error_handler)
6136 (_("%s(%s+0x%lx): reloc against `%s': error %d"),
6137 bfd_archive_filename (input_bfd),
6138 bfd_get_section_name (input_bfd, input_section),
6139 (long) rel->r_offset, name, (int) r);
6140 ret = false;
6141 }
6142 }
6143 }
6144
6145 return ret;
6146 }
6147
6148 /* Finish up dynamic symbol handling. We set the contents of various
6149 dynamic sections here. */
6150
6151 static boolean
6152 ppc64_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
6153 bfd *output_bfd;
6154 struct bfd_link_info *info;
6155 struct elf_link_hash_entry *h;
6156 Elf_Internal_Sym *sym;
6157 {
6158 struct ppc_link_hash_table *htab;
6159 bfd *dynobj;
6160
6161 htab = ppc_hash_table (info);
6162 dynobj = htab->elf.dynobj;
6163
6164 if (h->plt.offset != (bfd_vma) -1
6165 && ((struct ppc_link_hash_entry *) h)->is_func_descriptor)
6166 {
6167 Elf_Internal_Rela rela;
6168 Elf64_External_Rela *loc;
6169
6170 /* This symbol has an entry in the procedure linkage table. Set
6171 it up. */
6172
6173 if (htab->splt == NULL
6174 || htab->srelplt == NULL
6175 || htab->sglink == NULL)
6176 abort ();
6177
6178 /* Create a JMP_SLOT reloc to inform the dynamic linker to
6179 fill in the PLT entry. */
6180
6181 rela.r_offset = (htab->splt->output_section->vma
6182 + htab->splt->output_offset
6183 + h->plt.offset);
6184 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
6185 rela.r_addend = 0;
6186
6187 loc = (Elf64_External_Rela *) htab->srelplt->contents;
6188 loc += (h->plt.offset - PLT_INITIAL_ENTRY_SIZE) / PLT_ENTRY_SIZE;
6189 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
6190 }
6191
6192 if (h->got.offset != (bfd_vma) -1)
6193 {
6194 Elf_Internal_Rela rela;
6195 Elf64_External_Rela *loc;
6196
6197 /* This symbol has an entry in the global offset table. Set it
6198 up. */
6199
6200 if (htab->sgot == NULL || htab->srelgot == NULL)
6201 abort ();
6202
6203 rela.r_offset = (htab->sgot->output_section->vma
6204 + htab->sgot->output_offset
6205 + (h->got.offset &~ (bfd_vma) 1));
6206
6207 /* If this is a static link, or it is a -Bsymbolic link and the
6208 symbol is defined locally or was forced to be local because
6209 of a version file, we just want to emit a RELATIVE reloc.
6210 The entry in the global offset table will already have been
6211 initialized in the relocate_section function. */
6212 if (info->shared
6213 && (info->symbolic
6214 || h->dynindx == -1
6215 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL))
6216 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
6217 {
6218 BFD_ASSERT((h->got.offset & 1) != 0);
6219 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
6220 rela.r_addend = (h->root.u.def.value
6221 + h->root.u.def.section->output_section->vma
6222 + h->root.u.def.section->output_offset);
6223 }
6224 else
6225 {
6226 BFD_ASSERT ((h->got.offset & 1) == 0);
6227 bfd_put_64 (output_bfd, (bfd_vma) 0,
6228 htab->sgot->contents + h->got.offset);
6229 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_GLOB_DAT);
6230 rela.r_addend = 0;
6231 }
6232
6233 loc = (Elf64_External_Rela *) htab->srelgot->contents;
6234 loc += htab->srelgot->reloc_count++;
6235 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
6236 }
6237
6238 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
6239 {
6240 Elf_Internal_Rela rela;
6241 Elf64_External_Rela *loc;
6242
6243 /* This symbol needs a copy reloc. Set it up. */
6244
6245 if (h->dynindx == -1
6246 || (h->root.type != bfd_link_hash_defined
6247 && h->root.type != bfd_link_hash_defweak)
6248 || htab->srelbss == NULL)
6249 abort ();
6250
6251 rela.r_offset = (h->root.u.def.value
6252 + h->root.u.def.section->output_section->vma
6253 + h->root.u.def.section->output_offset);
6254 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
6255 rela.r_addend = 0;
6256 loc = (Elf64_External_Rela *) htab->srelbss->contents;
6257 loc += htab->srelbss->reloc_count++;
6258 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
6259 }
6260
6261 /* Mark some specially defined symbols as absolute. */
6262 if (strcmp (h->root.root.string, "_DYNAMIC") == 0)
6263 sym->st_shndx = SHN_ABS;
6264
6265 return true;
6266 }
6267
6268 /* Used to decide how to sort relocs in an optimal manner for the
6269 dynamic linker, before writing them out. */
6270
6271 static enum elf_reloc_type_class
6272 ppc64_elf_reloc_type_class (rela)
6273 const Elf_Internal_Rela *rela;
6274 {
6275 enum elf_ppc_reloc_type r_type;
6276
6277 r_type = (enum elf_ppc_reloc_type) ELF64_R_TYPE (rela->r_info);
6278 switch (r_type)
6279 {
6280 case R_PPC64_RELATIVE:
6281 return reloc_class_relative;
6282 case R_PPC64_JMP_SLOT:
6283 return reloc_class_plt;
6284 case R_PPC64_COPY:
6285 return reloc_class_copy;
6286 default:
6287 return reloc_class_normal;
6288 }
6289 }
6290
6291 /* Finish up the dynamic sections. */
6292
6293 static boolean
6294 ppc64_elf_finish_dynamic_sections (output_bfd, info)
6295 bfd *output_bfd;
6296 struct bfd_link_info *info;
6297 {
6298 struct ppc_link_hash_table *htab;
6299 bfd *dynobj;
6300 asection *sdyn;
6301
6302 htab = ppc_hash_table (info);
6303 dynobj = htab->elf.dynobj;
6304 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
6305
6306 if (htab->elf.dynamic_sections_created)
6307 {
6308 Elf64_External_Dyn *dyncon, *dynconend;
6309
6310 if (sdyn == NULL || htab->sgot == NULL)
6311 abort ();
6312
6313 dyncon = (Elf64_External_Dyn *) sdyn->contents;
6314 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
6315 for (; dyncon < dynconend; dyncon++)
6316 {
6317 Elf_Internal_Dyn dyn;
6318 asection *s;
6319
6320 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
6321
6322 switch (dyn.d_tag)
6323 {
6324 default:
6325 continue;
6326
6327 case DT_PPC64_GLINK:
6328 dyn.d_un.d_ptr = (htab->sglink->output_section->vma
6329 + htab->sglink->output_offset);
6330 break;
6331
6332 case DT_PPC64_OPD:
6333 s = bfd_get_section_by_name (output_bfd, ".opd");
6334 if (s != NULL)
6335 dyn.d_un.d_ptr = s->vma;
6336 break;
6337
6338 case DT_PPC64_OPDSZ:
6339 s = bfd_get_section_by_name (output_bfd, ".opd");
6340 if (s != NULL)
6341 dyn.d_un.d_val = s->_raw_size;
6342 break;
6343
6344 case DT_PLTGOT:
6345 dyn.d_un.d_ptr = (htab->splt->output_section->vma
6346 + htab->splt->output_offset);
6347 break;
6348
6349 case DT_JMPREL:
6350 dyn.d_un.d_ptr = (htab->srelplt->output_section->vma
6351 + htab->srelplt->output_offset);
6352 break;
6353
6354 case DT_PLTRELSZ:
6355 dyn.d_un.d_val = htab->srelplt->_raw_size;
6356 break;
6357
6358 case DT_RELASZ:
6359 /* Don't count procedure linkage table relocs in the
6360 overall reloc count. */
6361 if (htab->srelplt != NULL)
6362 dyn.d_un.d_val -= htab->srelplt->_raw_size;
6363 break;
6364 }
6365
6366 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
6367 }
6368 }
6369
6370 if (htab->sgot != NULL && htab->sgot->_raw_size != 0)
6371 {
6372 /* Fill in the first entry in the global offset table.
6373 We use it to hold the link-time TOCbase. */
6374 bfd_put_64 (output_bfd,
6375 elf_gp (output_bfd) + TOC_BASE_OFF,
6376 htab->sgot->contents);
6377
6378 /* Set .got entry size. */
6379 elf_section_data (htab->sgot->output_section)->this_hdr.sh_entsize = 8;
6380 }
6381
6382 if (htab->splt != NULL && htab->splt->_raw_size != 0)
6383 {
6384 /* Set .plt entry size. */
6385 elf_section_data (htab->splt->output_section)->this_hdr.sh_entsize
6386 = PLT_ENTRY_SIZE;
6387 }
6388
6389 return true;
6390 }
6391
6392 #define TARGET_LITTLE_SYM bfd_elf64_powerpcle_vec
6393 #define TARGET_LITTLE_NAME "elf64-powerpcle"
6394 #define TARGET_BIG_SYM bfd_elf64_powerpc_vec
6395 #define TARGET_BIG_NAME "elf64-powerpc"
6396 #define ELF_ARCH bfd_arch_powerpc
6397 #define ELF_MACHINE_CODE EM_PPC64
6398 #define ELF_MAXPAGESIZE 0x10000
6399 #define elf_info_to_howto ppc64_elf_info_to_howto
6400
6401 #ifdef EM_CYGNUS_POWERPC
6402 #define ELF_MACHINE_ALT1 EM_CYGNUS_POWERPC
6403 #endif
6404
6405 #ifdef EM_PPC_OLD
6406 #define ELF_MACHINE_ALT2 EM_PPC_OLD
6407 #endif
6408
6409 #define elf_backend_want_got_sym 0
6410 #define elf_backend_want_plt_sym 0
6411 #define elf_backend_plt_alignment 3
6412 #define elf_backend_plt_not_loaded 1
6413 #define elf_backend_got_symbol_offset 0
6414 #define elf_backend_got_header_size 8
6415 #define elf_backend_plt_header_size PLT_INITIAL_ENTRY_SIZE
6416 #define elf_backend_can_gc_sections 1
6417 #define elf_backend_can_refcount 1
6418 #define elf_backend_rela_normal 1
6419
6420 #define bfd_elf64_bfd_reloc_type_lookup ppc64_elf_reloc_type_lookup
6421 #define bfd_elf64_bfd_set_private_flags ppc64_elf_set_private_flags
6422 #define bfd_elf64_bfd_merge_private_bfd_data ppc64_elf_merge_private_bfd_data
6423 #define bfd_elf64_bfd_link_hash_table_create ppc64_elf_link_hash_table_create
6424 #define bfd_elf64_bfd_link_hash_table_free ppc64_elf_link_hash_table_free
6425 #define bfd_elf64_get_symbol_info ppc64_elf_get_symbol_info
6426
6427 #define elf_backend_object_p ppc64_elf_object_p
6428 #define elf_backend_section_from_shdr ppc64_elf_section_from_shdr
6429 #define elf_backend_create_dynamic_sections ppc64_elf_create_dynamic_sections
6430 #define elf_backend_copy_indirect_symbol ppc64_elf_copy_indirect_symbol
6431 #define elf_backend_check_relocs ppc64_elf_check_relocs
6432 #define elf_backend_gc_mark_hook ppc64_elf_gc_mark_hook
6433 #define elf_backend_gc_sweep_hook ppc64_elf_gc_sweep_hook
6434 #define elf_backend_adjust_dynamic_symbol ppc64_elf_adjust_dynamic_symbol
6435 #define elf_backend_hide_symbol ppc64_elf_hide_symbol
6436 #define elf_backend_always_size_sections ppc64_elf_func_desc_adjust
6437 #define elf_backend_size_dynamic_sections ppc64_elf_size_dynamic_sections
6438 #define elf_backend_fake_sections ppc64_elf_fake_sections
6439 #define elf_backend_relocate_section ppc64_elf_relocate_section
6440 #define elf_backend_finish_dynamic_symbol ppc64_elf_finish_dynamic_symbol
6441 #define elf_backend_reloc_type_class ppc64_elf_reloc_type_class
6442 #define elf_backend_finish_dynamic_sections ppc64_elf_finish_dynamic_sections
6443
6444 #include "elf64-target.h"
This page took 0.204565 seconds and 4 git commands to generate.