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