daily update
[deliverable/binutils-gdb.git] / bfd / elf64-ppc.c
CommitLineData
5bd4f169
AM
1/* PowerPC64-specific support for 64-bit ELF.
2 Copyright 1999, 2000, 2001 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
6This file is part of BFD, the Binary File Descriptor library.
7
8This program is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 2 of the License, or
11(at your option) any later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with this program; if not, write to the Free Software
20Foundation, 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
32#define USE_RELA /* we want RELA relocations, not REL. */
33
34
35static void ppc_howto_init
36 PARAMS ((void));
37static reloc_howto_type *ppc64_elf_reloc_type_lookup
38 PARAMS ((bfd *abfd, bfd_reloc_code_real_type code));
39static void ppc64_elf_info_to_howto
40 PARAMS ((bfd *abfd, arelent *cache_ptr, Elf64_Internal_Rela *dst));
41static bfd_reloc_status_type ppc64_elf_addr16_ha_reloc
42 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
43static boolean ppc64_elf_set_private_flags
44 PARAMS ((bfd *, flagword));
45static boolean ppc64_elf_copy_private_bfd_data
46 PARAMS ((bfd *, bfd *));
47static boolean ppc64_elf_merge_private_bfd_data
48 PARAMS ((bfd *, bfd *));
49static boolean ppc64_elf_section_from_shdr
50 PARAMS ((bfd *, Elf64_Internal_Shdr *, char *));
65f38f15
AM
51static struct bfd_hash_entry *link_hash_newfunc
52 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
53static struct bfd_link_hash_table *ppc64_elf_link_hash_table_create
54 PARAMS ((bfd *));
55static boolean create_got_section
56 PARAMS ((bfd *, struct bfd_link_info *));
5bd4f169
AM
57static boolean ppc64_elf_create_dynamic_sections
58 PARAMS ((bfd *, struct bfd_link_info *));
65f38f15
AM
59static void ppc64_elf_copy_indirect_symbol
60 PARAMS ((struct elf_link_hash_entry *, struct elf_link_hash_entry *));
5bd4f169
AM
61static boolean ppc64_elf_check_relocs
62 PARAMS ((bfd *, struct bfd_link_info *, asection *,
63 const Elf_Internal_Rela *));
64static asection * ppc64_elf_gc_mark_hook
65 PARAMS ((bfd *abfd, struct bfd_link_info *info, Elf_Internal_Rela *rel,
66 struct elf_link_hash_entry *h, Elf_Internal_Sym *sym));
67static boolean ppc64_elf_gc_sweep_hook
68 PARAMS ((bfd *abfd, struct bfd_link_info *info, asection *sec,
69 const Elf_Internal_Rela *relocs));
70static boolean ppc64_elf_adjust_dynamic_symbol
71 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
65f38f15
AM
72static boolean allocate_dynrelocs
73 PARAMS ((struct elf_link_hash_entry *, PTR));
74static boolean readonly_dynrelocs
5bd4f169 75 PARAMS ((struct elf_link_hash_entry *, PTR));
65f38f15
AM
76static enum elf_reloc_type_class ppc64_elf_reloc_type_class
77 PARAMS ((const Elf_Internal_Rela *));
5bd4f169
AM
78static boolean ppc64_elf_size_dynamic_sections
79 PARAMS ((bfd *, struct bfd_link_info *));
80static boolean ppc64_elf_final_link
81 PARAMS ((bfd *, struct bfd_link_info *));
82static boolean ppc64_elf_fake_sections
83 PARAMS ((bfd *, Elf64_Internal_Shdr *, asection *));
84static boolean ppc64_elf_relocate_section
85 PARAMS ((bfd *, struct bfd_link_info *info, bfd *, asection *, bfd_byte *,
86 Elf_Internal_Rela *relocs, Elf_Internal_Sym *local_syms,
87 asection **));
88static boolean ppc64_elf_finish_dynamic_symbol
89 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
90 Elf_Internal_Sym *));
91static boolean ppc64_elf_finish_dynamic_sections
92 PARAMS ((bfd *, struct bfd_link_info *));
93
94
95/* Branch prediction bit for branch taken relocs. */
96#define BRANCH_PREDICT_BIT 0x200000
97
98/* Mask to set RA in memory instructions. */
99#define RA_REGISTER_MASK 0x001f0000
100
101/* Value to shift register by to insert RA. */
102#define RA_REGISTER_SHIFT 16
103
104/* The name of the dynamic interpreter. This is put in the .interp
105 section. */
106#define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
107
108/* The size in bytes of an entry in the procedure linkage table. */
109#define PLT_ENTRY_SIZE 24
110
111/* The initial size of the plt reserved for the dynamic linker. */
112#define PLT_INITIAL_ENTRY_SIZE 96
113
114/* TOC base pointers offset from start of TOC. */
115#define TOC_BASE_OFF (0x8000)
116
117/* Global linkage stub. The first instruction has its offset patched. */
118/* FIXME: Need traceback table? */
119static const unsigned int ppc64_elf_glink_code[] = {
120 0xe9820000, /* ld %r12,0(%r2) */
121 0xf8410028, /* std %r2,40(%r1) */
122 0xe80c0000, /* ld %r0,0(%r12) */
123 0xe84c0008, /* ld %r2,8(%r12) */
124 0x7c0903a6, /* mtctr %r0 */
125 0x4e800420 /* bctr */
126};
127
128#define PPC64_ELF_GLINK_SIZE \
129 ((sizeof ppc64_elf_glink_code / sizeof ppc64_elf_glink_code[0]) * 4)
130\f
131/* Relocation HOWTO's. */
132static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC_max];
133
134static reloc_howto_type ppc64_elf_howto_raw[] = {
135 /* This reloc does nothing. */
136 HOWTO (R_PPC64_NONE, /* type */
137 0, /* rightshift */
138 2, /* size (0 = byte, 1 = short, 2 = long) */
139 32, /* bitsize */
140 false, /* pc_relative */
141 0, /* bitpos */
142 complain_overflow_bitfield, /* complain_on_overflow */
143 bfd_elf_generic_reloc, /* special_function */
144 "R_PPC64_NONE", /* name */
145 false, /* partial_inplace */
146 0, /* src_mask */
147 0, /* dst_mask */
148 false), /* pcrel_offset */
149
150 /* A standard 32 bit relocation. */
151 HOWTO (R_PPC64_ADDR32, /* type */
152 0, /* rightshift */
153 2, /* size (0 = byte, 1 = short, 2 = long) */
154 32, /* bitsize */
155 false, /* pc_relative */
156 0, /* bitpos */
157 complain_overflow_bitfield, /* complain_on_overflow */
158 bfd_elf_generic_reloc, /* special_function */
159 "R_PPC64_ADDR32", /* name */
160 false, /* partial_inplace */
161 0, /* src_mask */
162 0xffffffff, /* dst_mask */
163 false), /* pcrel_offset */
164
165 /* An absolute 26 bit branch; the lower two bits must be zero.
166 FIXME: we don't check that, we just clear them. */
167 HOWTO (R_PPC64_ADDR24, /* type */
168 0, /* rightshift */
169 2, /* size (0 = byte, 1 = short, 2 = long) */
170 26, /* bitsize */
171 false, /* pc_relative */
172 0, /* bitpos */
173 complain_overflow_bitfield, /* complain_on_overflow */
174 bfd_elf_generic_reloc, /* special_function */
175 "R_PPC64_ADDR24", /* name */
176 false, /* partial_inplace */
177 0, /* src_mask */
178 0x3fffffc, /* dst_mask */
179 false), /* pcrel_offset */
180
181 /* A standard 16 bit relocation. */
182 HOWTO (R_PPC64_ADDR16, /* type */
183 0, /* rightshift */
184 1, /* size (0 = byte, 1 = short, 2 = long) */
185 16, /* bitsize */
186 false, /* pc_relative */
187 0, /* bitpos */
188 complain_overflow_bitfield, /* complain_on_overflow */
189 bfd_elf_generic_reloc, /* special_function */
190 "R_PPC64_ADDR16", /* name */
191 false, /* partial_inplace */
192 0, /* src_mask */
193 0xffff, /* dst_mask */
194 false), /* pcrel_offset */
195
196 /* A 16 bit relocation without overflow. */
197 HOWTO (R_PPC64_ADDR16_LO, /* type */
198 0, /* rightshift */
199 1, /* size (0 = byte, 1 = short, 2 = long) */
200 16, /* bitsize */
201 false, /* pc_relative */
202 0, /* bitpos */
203 complain_overflow_dont,/* complain_on_overflow */
204 bfd_elf_generic_reloc, /* special_function */
205 "R_PPC64_ADDR16_LO", /* name */
206 false, /* partial_inplace */
207 0, /* src_mask */
208 0xffff, /* dst_mask */
209 false), /* pcrel_offset */
210
211 /* Bits 16-31 of an address. */
212 HOWTO (R_PPC64_ADDR16_HI, /* type */
213 16, /* rightshift */
214 1, /* size (0 = byte, 1 = short, 2 = long) */
215 16, /* bitsize */
216 false, /* pc_relative */
217 0, /* bitpos */
218 complain_overflow_dont, /* complain_on_overflow */
219 bfd_elf_generic_reloc, /* special_function */
220 "R_PPC64_ADDR16_HI", /* name */
221 false, /* partial_inplace */
222 0, /* src_mask */
223 0xffff, /* dst_mask */
224 false), /* pcrel_offset */
225
226 /* Bits 16-31 of an address, plus 1 if the contents of the low 16
227 bits, treated as a signed number, is negative. */
228 HOWTO (R_PPC64_ADDR16_HA, /* type */
229 16, /* rightshift */
230 1, /* size (0 = byte, 1 = short, 2 = long) */
231 16, /* bitsize */
232 false, /* pc_relative */
233 0, /* bitpos */
234 complain_overflow_dont, /* complain_on_overflow */
235 ppc64_elf_addr16_ha_reloc, /* special_function */
236 "R_PPC64_ADDR16_HA", /* name */
237 false, /* partial_inplace */
238 0, /* src_mask */
239 0xffff, /* dst_mask */
240 false), /* pcrel_offset */
241
242 /* An absolute 16 bit branch; the lower two bits must be zero.
243 FIXME: we don't check that, we just clear them. */
244 HOWTO (R_PPC64_ADDR14, /* type */
245 0, /* rightshift */
246 2, /* size (0 = byte, 1 = short, 2 = long) */
247 16, /* bitsize */
248 false, /* pc_relative */
249 0, /* bitpos */
250 complain_overflow_bitfield, /* complain_on_overflow */
251 bfd_elf_generic_reloc, /* special_function */
252 "R_PPC64_ADDR14", /* name */
253 false, /* partial_inplace */
254 0, /* src_mask */
255 0xfffc, /* dst_mask */
256 false), /* pcrel_offset */
257
258 /* An absolute 16 bit branch, for which bit 10 should be set to
259 indicate that the branch is expected to be taken. The lower two
260 bits must be zero. */
261 HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
262 0, /* rightshift */
263 2, /* size (0 = byte, 1 = short, 2 = long) */
264 16, /* bitsize */
265 false, /* pc_relative */
266 0, /* bitpos */
267 complain_overflow_bitfield, /* complain_on_overflow */
268 bfd_elf_generic_reloc, /* special_function */
269 "R_PPC64_ADDR14_BRTAKEN",/* name */
270 false, /* partial_inplace */
271 0, /* src_mask */
272 0xfffc, /* dst_mask */
273 false), /* pcrel_offset */
274
275 /* An absolute 16 bit branch, for which bit 10 should be set to
276 indicate that the branch is not expected to be taken. The lower
277 two bits must be zero. */
278 HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
279 0, /* rightshift */
280 2, /* size (0 = byte, 1 = short, 2 = long) */
281 16, /* bitsize */
282 false, /* pc_relative */
283 0, /* bitpos */
284 complain_overflow_bitfield, /* complain_on_overflow */
285 bfd_elf_generic_reloc, /* special_function */
286 "R_PPC64_ADDR14_BRNTAKEN",/* name */
287 false, /* partial_inplace */
288 0, /* src_mask */
289 0xfffc, /* dst_mask */
290 false), /* pcrel_offset */
291
292 /* A relative 26 bit branch; the lower two bits must be zero. */
293 HOWTO (R_PPC64_REL24, /* type */
294 0, /* rightshift */
295 2, /* size (0 = byte, 1 = short, 2 = long) */
296 26, /* bitsize */
297 true, /* pc_relative */
298 0, /* bitpos */
299 complain_overflow_signed, /* complain_on_overflow */
300 bfd_elf_generic_reloc, /* special_function */
301 "R_PPC64_REL24", /* name */
302 false, /* partial_inplace */
303 0, /* src_mask */
304 0x3fffffc, /* dst_mask */
305 true), /* pcrel_offset */
306
307 /* A relative 16 bit branch; the lower two bits must be zero. */
308 HOWTO (R_PPC64_REL14, /* type */
309 0, /* rightshift */
310 2, /* size (0 = byte, 1 = short, 2 = long) */
311 16, /* bitsize */
312 true, /* pc_relative */
313 0, /* bitpos */
314 complain_overflow_signed, /* complain_on_overflow */
315 bfd_elf_generic_reloc, /* special_function */
316 "R_PPC64_REL14", /* name */
317 false, /* partial_inplace */
318 0, /* src_mask */
319 0xfffc, /* dst_mask */
320 true), /* pcrel_offset */
321
322 /* A relative 16 bit branch. Bit 10 should be set to indicate that
323 the branch is expected to be taken. The lower two bits must be
324 zero. */
325 HOWTO (R_PPC64_REL14_BRTAKEN, /* type */
326 0, /* rightshift */
327 2, /* size (0 = byte, 1 = short, 2 = long) */
328 16, /* bitsize */
329 true, /* pc_relative */
330 0, /* bitpos */
331 complain_overflow_signed, /* complain_on_overflow */
332 bfd_elf_generic_reloc, /* special_function */
333 "R_PPC64_REL14_BRTAKEN", /* name */
334 false, /* partial_inplace */
335 0, /* src_mask */
336 0xfffc, /* dst_mask */
337 true), /* pcrel_offset */
338
339 /* A relative 16 bit branch. Bit 10 should be set to indicate that
340 the branch is not expected to be taken. The lower two bits must
341 be zero. */
342 HOWTO (R_PPC64_REL14_BRNTAKEN, /* type */
343 0, /* rightshift */
344 2, /* size (0 = byte, 1 = short, 2 = long) */
345 16, /* bitsize */
346 true, /* pc_relative */
347 0, /* bitpos */
348 complain_overflow_signed, /* complain_on_overflow */
349 bfd_elf_generic_reloc, /* special_function */
350 "R_PPC64_REL14_BRNTAKEN",/* name */
351 false, /* partial_inplace */
352 0, /* src_mask */
353 0xfffc, /* dst_mask */
354 true), /* pcrel_offset */
355
356 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
357 symbol. */
358 HOWTO (R_PPC64_GOT16, /* type */
359 0, /* rightshift */
360 1, /* size (0 = byte, 1 = short, 2 = long) */
361 16, /* bitsize */
362 false, /* pc_relative */
363 0, /* bitpos */
364 complain_overflow_signed, /* complain_on_overflow */
365 bfd_elf_generic_reloc, /* special_function */
366 "R_PPC64_GOT16", /* name */
367 false, /* partial_inplace */
368 0, /* src_mask */
369 0xffff, /* dst_mask */
370 false), /* pcrel_offset */
371
372 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
373 the symbol. */
374 HOWTO (R_PPC64_GOT16_LO, /* type */
375 0, /* rightshift */
376 1, /* size (0 = byte, 1 = short, 2 = long) */
377 16, /* bitsize */
378 false, /* pc_relative */
379 0, /* bitpos */
380 complain_overflow_dont, /* complain_on_overflow */
381 bfd_elf_generic_reloc, /* special_function */
382 "R_PPC64_GOT16_LO", /* name */
383 false, /* partial_inplace */
384 0, /* src_mask */
385 0xffff, /* dst_mask */
386 false), /* pcrel_offset */
387
388 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
389 the symbol. */
390 HOWTO (R_PPC64_GOT16_HI, /* type */
391 16, /* rightshift */
392 1, /* size (0 = byte, 1 = short, 2 = long) */
393 16, /* bitsize */
394 false, /* pc_relative */
395 0, /* bitpos */
396 complain_overflow_dont,/* complain_on_overflow */
397 bfd_elf_generic_reloc, /* special_function */
398 "R_PPC64_GOT16_HI", /* name */
399 false, /* partial_inplace */
400 0, /* src_mask */
401 0xffff, /* dst_mask */
402 false), /* pcrel_offset */
403
404 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
405 the symbol. */
406 HOWTO (R_PPC64_GOT16_HA, /* type */
407 16, /* rightshift */
408 1, /* size (0 = byte, 1 = short, 2 = long) */
409 16, /* bitsize */
410 false, /* pc_relative */
411 0, /* bitpos */
412 complain_overflow_dont,/* complain_on_overflow */
413 ppc64_elf_addr16_ha_reloc, /* special_function */
414 "R_PPC64_GOT16_HA", /* name */
415 false, /* partial_inplace */
416 0, /* src_mask */
417 0xffff, /* dst_mask */
418 false), /* pcrel_offset */
419
420 /* This is used only by the dynamic linker. The symbol should exist
421 both in the object being run and in some shared library. The
422 dynamic linker copies the data addressed by the symbol from the
423 shared library into the object, because the object being
424 run has to have the data at some particular address. */
425 HOWTO (R_PPC64_COPY, /* type */
426 0, /* rightshift */
427 2, /* size (0 = byte, 1 = short, 2 = long) */
428 32, /* bitsize */
429 false, /* pc_relative */
430 0, /* bitpos */
431 complain_overflow_bitfield, /* complain_on_overflow */
432 bfd_elf_generic_reloc, /* special_function */
433 "R_PPC64_COPY", /* name */
434 false, /* partial_inplace */
435 0, /* src_mask */
436 0, /* dst_mask */
437 false), /* pcrel_offset */
438
439 /* Like R_PPC64_ADDR64, but used when setting global offset table
440 entries. */
441 HOWTO (R_PPC64_GLOB_DAT, /* type */
442 0, /* rightshift */
443 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
444 64, /* bitsize */
445 false, /* pc_relative */
446 0, /* bitpos */
447 complain_overflow_dont, /* complain_on_overflow */
448 bfd_elf_generic_reloc, /* special_function */
449 "R_PPC64_GLOB_DAT", /* name */
450 false, /* partial_inplace */
451 0, /* src_mask */
452 0xffffffffffffffff, /* dst_mask */
453 false), /* pcrel_offset */
454
455 /* Created by the link editor. Marks a procedure linkage table
456 entry for a symbol. */
457 HOWTO (R_PPC64_JMP_SLOT, /* type */
458 0, /* rightshift */
459 0, /* size (0 = byte, 1 = short, 2 = long) */
460 0, /* bitsize */
461 false, /* pc_relative */
462 0, /* bitpos */
463 complain_overflow_dont, /* complain_on_overflow */
464 bfd_elf_generic_reloc, /* special_function */
465 "R_PPC64_JMP_SLOT", /* name */
466 false, /* partial_inplace */
467 0, /* src_mask */
468 0, /* dst_mask */
469 false), /* pcrel_offset */
470
471 /* Used only by the dynamic linker. When the object is run, this
472 doubleword64 is set to the load address of the object, plus the
473 addend. */
474 HOWTO (R_PPC64_RELATIVE, /* type */
475 0, /* rightshift */
476 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
477 64, /* bitsize */
478 false, /* pc_relative */
479 0, /* bitpos */
480 complain_overflow_dont, /* complain_on_overflow */
481 bfd_elf_generic_reloc, /* special_function */
482 "R_PPC64_RELATIVE", /* name */
483 false, /* partial_inplace */
484 0, /* src_mask */
485 0xffffffffffffffff, /* dst_mask */
486 false), /* pcrel_offset */
487
488 /* Like R_PPC64_ADDR32, but may be unaligned. */
489 HOWTO (R_PPC64_UADDR32, /* type */
490 0, /* rightshift */
491 2, /* size (0 = byte, 1 = short, 2 = long) */
492 32, /* bitsize */
493 false, /* pc_relative */
494 0, /* bitpos */
495 complain_overflow_bitfield, /* complain_on_overflow */
496 bfd_elf_generic_reloc, /* special_function */
497 "R_PPC64_UADDR32", /* name */
498 false, /* partial_inplace */
499 0, /* src_mask */
500 0xffffffff, /* dst_mask */
501 false), /* pcrel_offset */
502
503 /* Like R_PPC64_ADDR16, but may be unaligned. */
504 HOWTO (R_PPC64_UADDR16, /* type */
505 0, /* rightshift */
506 1, /* size (0 = byte, 1 = short, 2 = long) */
507 16, /* bitsize */
508 false, /* pc_relative */
509 0, /* bitpos */
510 complain_overflow_bitfield, /* complain_on_overflow */
511 bfd_elf_generic_reloc, /* special_function */
512 "R_PPC64_UADDR16", /* name */
513 false, /* partial_inplace */
514 0, /* src_mask */
515 0xffff, /* dst_mask */
516 false), /* pcrel_offset */
517
518 /* 32-bit PC relative. */
519 HOWTO (R_PPC64_REL32, /* type */
520 0, /* rightshift */
521 2, /* size (0 = byte, 1 = short, 2 = long) */
522 32, /* bitsize */
523 true, /* pc_relative */
524 0, /* bitpos */
525 /* FIXME: Verify. Was complain_overflow_bitfield. */
526 complain_overflow_signed, /* complain_on_overflow */
527 bfd_elf_generic_reloc, /* special_function */
528 "R_PPC64_REL32", /* name */
529 false, /* partial_inplace */
530 0, /* src_mask */
531 0xffffffff, /* dst_mask */
532 true), /* pcrel_offset */
533
534 /* 32-bit relocation to the symbol's procedure linkage table.
535 FIXME: R_PPC64_PLT32 not supported. */
536 HOWTO (R_PPC64_PLT32, /* type */
537 0, /* rightshift */
538 2, /* size (0 = byte, 1 = short, 2 = long) */
539 32, /* bitsize */
540 false, /* pc_relative */
541 0, /* bitpos */
542 complain_overflow_bitfield, /* complain_on_overflow */
543 bfd_elf_generic_reloc, /* special_function */
544 "R_PPC64_PLT32", /* name */
545 false, /* partial_inplace */
546 0, /* src_mask */
547 0, /* dst_mask */
548 false), /* pcrel_offset */
549
550 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
551 FIXME: R_PPC64_PLTREL32 not supported. */
552 HOWTO (R_PPC64_PLTREL32, /* type */
553 0, /* rightshift */
554 2, /* size (0 = byte, 1 = short, 2 = long) */
555 32, /* bitsize */
556 true, /* pc_relative */
557 0, /* bitpos */
558 complain_overflow_signed, /* complain_on_overflow */
559 bfd_elf_generic_reloc, /* special_function */
560 "R_PPC64_PLTREL32", /* name */
561 false, /* partial_inplace */
562 0, /* src_mask */
563 0, /* dst_mask */
564 true), /* pcrel_offset */
565
566 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
567 the symbol. */
568 HOWTO (R_PPC64_PLT16_LO, /* type */
569 0, /* rightshift */
570 1, /* size (0 = byte, 1 = short, 2 = long) */
571 16, /* bitsize */
572 false, /* pc_relative */
573 0, /* bitpos */
574 complain_overflow_dont, /* complain_on_overflow */
575 bfd_elf_generic_reloc, /* special_function */
576 "R_PPC64_PLT16_LO", /* name */
577 false, /* partial_inplace */
578 0, /* src_mask */
579 0xffff, /* dst_mask */
580 false), /* pcrel_offset */
581
582 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
583 the symbol. */
584 HOWTO (R_PPC64_PLT16_HI, /* type */
585 16, /* rightshift */
586 1, /* size (0 = byte, 1 = short, 2 = long) */
587 16, /* bitsize */
588 false, /* pc_relative */
589 0, /* bitpos */
590 complain_overflow_dont, /* complain_on_overflow */
591 bfd_elf_generic_reloc, /* special_function */
592 "R_PPC64_PLT16_HI", /* name */
593 false, /* partial_inplace */
594 0, /* src_mask */
595 0xffff, /* dst_mask */
596 false), /* pcrel_offset */
597
598 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
599 the symbol. */
600 HOWTO (R_PPC64_PLT16_HA, /* type */
601 16, /* rightshift */
602 1, /* size (0 = byte, 1 = short, 2 = long) */
603 16, /* bitsize */
604 false, /* pc_relative */
605 0, /* bitpos */
606 complain_overflow_dont, /* complain_on_overflow */
607 ppc64_elf_addr16_ha_reloc, /* special_function */
608 "R_PPC64_PLT16_HA", /* name */
609 false, /* partial_inplace */
610 0, /* src_mask */
611 0xffff, /* dst_mask */
612 false), /* pcrel_offset */
613
614 /* 32-bit section relative relocation. */
615 /* FIXME: Verify R_PPC64_SECTOFF. Seems strange with size=2 and
616 dst_mask=0. */
617 HOWTO (R_PPC64_SECTOFF, /* type */
618 0, /* rightshift */
619 2, /* size (0 = byte, 1 = short, 2 = long) */
620 32, /* bitsize */
621 true, /* pc_relative */
622 0, /* bitpos */
623 complain_overflow_bitfield, /* complain_on_overflow */
624 bfd_elf_generic_reloc, /* special_function */
625 "R_PPC64_SECTOFF", /* name */
626 false, /* partial_inplace */
627 0, /* src_mask */
628 0, /* dst_mask */
629 true), /* pcrel_offset */
630
631 /* 16-bit lower half section relative relocation. */
632 HOWTO (R_PPC64_SECTOFF_LO, /* type */
633 0, /* rightshift */
634 1, /* size (0 = byte, 1 = short, 2 = long) */
635 16, /* bitsize */
636 false, /* pc_relative */
637 0, /* bitpos */
638 complain_overflow_dont, /* complain_on_overflow */
639 bfd_elf_generic_reloc, /* special_function */
640 "R_PPC64_SECTOFF_LO", /* name */
641 false, /* partial_inplace */
642 0, /* src_mask */
643 0xffff, /* dst_mask */
644 false), /* pcrel_offset */
645
646 /* 16-bit upper half section relative relocation. */
647 HOWTO (R_PPC64_SECTOFF_HI, /* type */
648 16, /* rightshift */
649 1, /* size (0 = byte, 1 = short, 2 = long) */
650 16, /* bitsize */
651 false, /* pc_relative */
652 0, /* bitpos */
653 complain_overflow_dont, /* complain_on_overflow */
654 bfd_elf_generic_reloc, /* special_function */
655 "R_PPC64_SECTOFF_HI", /* name */
656 false, /* partial_inplace */
657 0, /* src_mask */
658 0xffff, /* dst_mask */
659 false), /* pcrel_offset */
660
661 /* 16-bit upper half adjusted section relative relocation. */
662 HOWTO (R_PPC64_SECTOFF_HA, /* type */
663 16, /* rightshift */
664 1, /* size (0 = byte, 1 = short, 2 = long) */
665 16, /* bitsize */
666 false, /* pc_relative */
667 0, /* bitpos */
668 complain_overflow_dont, /* complain_on_overflow */
669 ppc64_elf_addr16_ha_reloc, /* special_function */
670 "R_PPC64_SECTOFF_HA", /* name */
671 false, /* partial_inplace */
672 0, /* src_mask */
673 0xffff, /* dst_mask */
674 false), /* pcrel_offset */
675
676 /* Like R_PPC64_REL24 without touching the two least significant
677 bits. */
678 /* FIXME: Verify R_PPC64_ADDR30. */
679 HOWTO (R_PPC64_ADDR30, /* type */
680 2, /* rightshift */
681 2, /* size (0 = byte, 1 = short, 2 = long) */
682 30, /* bitsize */
683 true, /* pc_relative */
684 0, /* bitpos */
685 complain_overflow_dont, /* complain_on_overflow */
686 bfd_elf_generic_reloc, /* special_function */
687 "R_PPC64_ADDR30", /* name */
688 false, /* partial_inplace */
689 0, /* src_mask */
690 0xfffffffc, /* dst_mask */
691 true), /* pcrel_offset */
692
693 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
694
695 /* A standard 64-bit relocation. */
696 HOWTO (R_PPC64_ADDR64, /* type */
697 0, /* rightshift */
698 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
699 64, /* bitsize */
700 false, /* pc_relative */
701 0, /* bitpos */
702 complain_overflow_dont, /* complain_on_overflow */
703 bfd_elf_generic_reloc, /* special_function */
704 "R_PPC64_ADDR64", /* name */
705 false, /* partial_inplace */
706 0, /* src_mask */
707 0xffffffffffffffff, /* dst_mask */
708 false), /* pcrel_offset */
709
710 /* The bits 32-47 of an address. */
711 HOWTO (R_PPC64_ADDR16_HIGHER, /* type */
712 32, /* rightshift */
713 1, /* size (0 = byte, 1 = short, 2 = long) */
714 16, /* bitsize */
715 false, /* pc_relative */
716 0, /* bitpos */
717 complain_overflow_dont, /* complain_on_overflow */
718 bfd_elf_generic_reloc, /* special_function */
719 "R_PPC64_ADDR16_HIGHER", /* name */
720 false, /* partial_inplace */
721 0, /* src_mask */
722 0xffff, /* dst_mask */
723 false), /* pcrel_offset */
724
725 /* The bits 32-47 of an address, plus 1 if the contents of the low
726 16 bits, treated as a signed number, is negative. */
727 HOWTO (R_PPC64_ADDR16_HIGHERA, /* type */
728 32, /* rightshift */
729 1, /* size (0 = byte, 1 = short, 2 = long) */
730 16, /* bitsize */
731 false, /* pc_relative */
732 0, /* bitpos */
733 complain_overflow_dont, /* complain_on_overflow */
734 ppc64_elf_addr16_ha_reloc, /* special_function */
735 "R_PPC64_ADDR16_HIGHERA", /* name */
736 false, /* partial_inplace */
737 0, /* src_mask */
738 0xffff, /* dst_mask */
739 false), /* pcrel_offset */
740
741 /* The bits 48-63 of an address. */
742 HOWTO (R_PPC64_ADDR16_HIGHEST,/* type */
743 48, /* rightshift */
744 1, /* size (0 = byte, 1 = short, 2 = long) */
745 16, /* bitsize */
746 false, /* pc_relative */
747 0, /* bitpos */
748 complain_overflow_dont, /* complain_on_overflow */
749 bfd_elf_generic_reloc, /* special_function */
750 "R_PPC64_ADDR16_HIGHEST", /* name */
751 false, /* partial_inplace */
752 0, /* src_mask */
753 0xffff, /* dst_mask */
754 false), /* pcrel_offset */
755
756 /* The bits 48-63 of an address, plus 1 if the contents of the low
757 16 bits, treated as a signed number, is negative. */
758 HOWTO (R_PPC64_ADDR16_HIGHESTA,/* type */
759 48, /* rightshift */
760 1, /* size (0 = byte, 1 = short, 2 = long) */
761 16, /* bitsize */
762 false, /* pc_relative */
763 0, /* bitpos */
764 complain_overflow_dont, /* complain_on_overflow */
765 ppc64_elf_addr16_ha_reloc, /* special_function */
766 "R_PPC64_ADDR16_HIGHESTA", /* name */
767 false, /* partial_inplace */
768 0, /* src_mask */
769 0xffff, /* dst_mask */
770 false), /* pcrel_offset */
771
772 /* Like ADDR64, but may be unaligned. */
773 HOWTO (R_PPC64_UADDR64, /* type */
774 0, /* rightshift */
775 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
776 64, /* bitsize */
777 false, /* pc_relative */
778 0, /* bitpos */
779 complain_overflow_dont, /* complain_on_overflow */
780 bfd_elf_generic_reloc, /* special_function */
781 "R_PPC64_UADDR64", /* name */
782 false, /* partial_inplace */
783 0, /* src_mask */
784 0xffffffffffffffff, /* dst_mask */
785 false), /* pcrel_offset */
786
787 /* 64-bit relative relocation. */
788 HOWTO (R_PPC64_REL64, /* type */
789 0, /* rightshift */
790 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
791 64, /* bitsize */
792 true, /* pc_relative */
793 0, /* bitpos */
794 complain_overflow_dont, /* complain_on_overflow */
795 bfd_elf_generic_reloc, /* special_function */
796 "R_PPC64_REL64", /* name */
797 false, /* partial_inplace */
798 0, /* src_mask */
799 0xffffffffffffffff, /* dst_mask */
800 true), /* pcrel_offset */
801
802 /* 64-bit relocation to the symbol's procedure linkage table. */
803 /* FIXME: R_PPC64_PLT64 not supported. */
804 HOWTO (R_PPC64_PLT64, /* type */
805 0, /* rightshift */
806 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
807 64, /* bitsize */
808 false, /* pc_relative */
809 0, /* bitpos */
810 complain_overflow_dont, /* complain_on_overflow */
811 bfd_elf_generic_reloc, /* special_function */
812 "R_PPC64_PLT64", /* name */
813 false, /* partial_inplace */
814 0, /* src_mask */
815 0, /* dst_mask */
816 false), /* pcrel_offset */
817
818 /* 64-bit PC relative relocation to the symbol's procedure linkage
819 table. */
820 /* FIXME: R_PPC64_PLTREL64 not supported. */
821 HOWTO (R_PPC64_PLTREL64, /* type */
822 0, /* rightshift */
823 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
824 64, /* bitsize */
825 true, /* pc_relative */
826 0, /* bitpos */
827 complain_overflow_dont, /* complain_on_overflow */
828 bfd_elf_generic_reloc, /* special_function */
829 "R_PPC64_PLTREL64", /* name */
830 false, /* partial_inplace */
831 0, /* src_mask */
832 0, /* dst_mask */
833 true), /* pcrel_offset */
834
835 /* 16 bit TOC-relative relocation. */
836
837 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
838 HOWTO (R_PPC64_TOC16, /* type */
839 0, /* rightshift */
840 1, /* size (0 = byte, 1 = short, 2 = long) */
841 16, /* bitsize */
842 false, /* pc_relative */
843 0, /* bitpos */
844 complain_overflow_signed, /* complain_on_overflow */
845 bfd_elf_generic_reloc, /* special_function */
846 "R_PPC64_TOC16", /* name */
847 false, /* partial_inplace */
848 0, /* src_mask */
849 0xffff, /* dst_mask */
850 false), /* pcrel_offset */
851
852 /* 16 bit TOC-relative relocation without overflow. */
853
854 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
855 HOWTO (R_PPC64_TOC16_LO, /* type */
856 0, /* rightshift */
857 1, /* size (0 = byte, 1 = short, 2 = long) */
858 16, /* bitsize */
859 false, /* pc_relative */
860 0, /* bitpos */
861 complain_overflow_dont, /* complain_on_overflow */
862 bfd_elf_generic_reloc, /* special_function */
863 "R_PPC64_TOC16_LO", /* name */
864 false, /* partial_inplace */
865 0, /* src_mask */
866 0xffff, /* dst_mask */
867 false), /* pcrel_offset */
868
869 /* 16 bit TOC-relative relocation, high 16 bits. */
870
871 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
872 HOWTO (R_PPC64_TOC16_HI, /* type */
873 16, /* rightshift */
874 1, /* size (0 = byte, 1 = short, 2 = long) */
875 16, /* bitsize */
876 false, /* pc_relative */
877 0, /* bitpos */
878 complain_overflow_dont, /* complain_on_overflow */
879 bfd_elf_generic_reloc, /* special_function */
880 "R_PPC64_TOC16_HI", /* name */
881 false, /* partial_inplace */
882 0, /* src_mask */
883 0xffff, /* dst_mask */
884 false), /* pcrel_offset */
885
886 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
887 contents of the low 16 bits, treated as a signed number, is
888 negative. */
889
890 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
891 HOWTO (R_PPC64_TOC16_HA, /* type */
892 16, /* rightshift */
893 1, /* size (0 = byte, 1 = short, 2 = long) */
894 16, /* bitsize */
895 false, /* pc_relative */
896 0, /* bitpos */
897 complain_overflow_dont, /* complain_on_overflow */
898 ppc64_elf_addr16_ha_reloc, /* special_function */
899 "R_PPC64_TOC16_HA", /* name */
900 false, /* partial_inplace */
901 0, /* src_mask */
902 0xffff, /* dst_mask */
903 false), /* pcrel_offset */
904
905 /* 64-bit relocation; insert value of TOC base (.TOC.). */
906
907 /* R_PPC64_TOC 51 doubleword64 .TOC. */
908 HOWTO (R_PPC64_TOC, /* type */
909 0, /* rightshift */
910 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
911 64, /* bitsize */
912 false, /* pc_relative */
913 0, /* bitpos */
914 complain_overflow_bitfield, /* complain_on_overflow */
915 bfd_elf_generic_reloc, /* special_function */
916 "R_PPC64_TOC", /* name */
917 false, /* partial_inplace */
918 0, /* src_mask */
919 0xffffffffffffffff, /* dst_mask */
920 false), /* pcrel_offset */
921
922 /* Like R_PPC64_GOT16, but also informs the link editor that the
923 value to relocate may (!) refer to a PLT entry which the link
924 editor (a) may replace with the symbol value. If the link editor
925 is unable to fully resolve the symbol, it may (b) create a PLT
926 entry and store the address to the new PLT entry in the GOT.
927 This permits lazy resolution of function symbols at run time.
928 The link editor may also skip all of this and just (c) emit a
929 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
930 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
931 HOWTO (R_PPC64_PLTGOT16, /* type */
932 0, /* rightshift */
933 1, /* size (0 = byte, 1 = short, 2 = long) */
934 16, /* bitsize */
935 false, /* pc_relative */
936 0, /* bitpos */
937 complain_overflow_signed, /* complain_on_overflow */
938 bfd_elf_generic_reloc, /* special_function */
939 "R_PPC64_PLTGOT16", /* name */
940 false, /* partial_inplace */
941 0, /* src_mask */
942 0xffff, /* dst_mask */
943 false), /* pcrel_offset */
944
945 /* Like R_PPC64_PLTGOT16, but without overflow. */
946 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
947 HOWTO (R_PPC64_PLTGOT16_LO, /* type */
948 0, /* rightshift */
949 1, /* size (0 = byte, 1 = short, 2 = long) */
950 16, /* bitsize */
951 false, /* pc_relative */
952 0, /* bitpos */
953 complain_overflow_dont, /* complain_on_overflow */
954 bfd_elf_generic_reloc, /* special_function */
955 "R_PPC64_PLTGOT16_LO", /* name */
956 false, /* partial_inplace */
957 0, /* src_mask */
958 0xffff, /* dst_mask */
959 false), /* pcrel_offset */
960
961 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
962 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
963 HOWTO (R_PPC64_PLTGOT16_HI, /* type */
964 16, /* rightshift */
965 1, /* size (0 = byte, 1 = short, 2 = long) */
966 16, /* bitsize */
967 false, /* pc_relative */
968 0, /* bitpos */
969 complain_overflow_dont, /* complain_on_overflow */
970 bfd_elf_generic_reloc, /* special_function */
971 "R_PPC64_PLTGOT16_HI", /* name */
972 false, /* partial_inplace */
973 0, /* src_mask */
974 0xffff, /* dst_mask */
975 false), /* pcrel_offset */
976
977 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
978 1 if the contents of the low 16 bits, treated as a signed number,
979 is negative. */
980 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
981 HOWTO (R_PPC64_PLTGOT16_HA, /* type */
982 16, /* rightshift */
983 1, /* size (0 = byte, 1 = short, 2 = long) */
984 16, /* bitsize */
985 false, /* pc_relative */
986 0, /* bitpos */
987 complain_overflow_dont,/* complain_on_overflow */
988 ppc64_elf_addr16_ha_reloc, /* special_function */
989 "R_PPC64_PLTGOT16_HA", /* name */
990 false, /* partial_inplace */
991 0, /* src_mask */
992 0xffff, /* dst_mask */
993 false), /* pcrel_offset */
994
995 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
996 HOWTO (R_PPC64_ADDR16_DS, /* type */
997 0, /* rightshift */
998 1, /* size (0 = byte, 1 = short, 2 = long) */
999 16, /* bitsize */
1000 false, /* pc_relative */
1001 0, /* bitpos */
1002 complain_overflow_bitfield, /* complain_on_overflow */
1003 bfd_elf_generic_reloc, /* special_function */
1004 "R_PPC64_ADDR16_DS", /* name */
1005 false, /* partial_inplace */
1006 0, /* src_mask */
1007 0xfffc, /* dst_mask */
1008 false), /* pcrel_offset */
1009
1010 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
1011 HOWTO (R_PPC64_ADDR16_LO_DS, /* type */
1012 0, /* rightshift */
1013 1, /* size (0 = byte, 1 = short, 2 = long) */
1014 16, /* bitsize */
1015 false, /* pc_relative */
1016 0, /* bitpos */
1017 complain_overflow_dont,/* complain_on_overflow */
1018 bfd_elf_generic_reloc, /* special_function */
1019 "R_PPC64_ADDR16_LO_DS",/* name */
1020 false, /* partial_inplace */
1021 0, /* src_mask */
1022 0xfffc, /* dst_mask */
1023 false), /* pcrel_offset */
1024
1025 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
1026 HOWTO (R_PPC64_GOT16_DS, /* type */
1027 0, /* rightshift */
1028 1, /* size (0 = byte, 1 = short, 2 = long) */
1029 16, /* bitsize */
1030 false, /* pc_relative */
1031 0, /* bitpos */
1032 complain_overflow_signed, /* complain_on_overflow */
1033 bfd_elf_generic_reloc, /* special_function */
1034 "R_PPC64_GOT16_DS", /* name */
1035 false, /* partial_inplace */
1036 0, /* src_mask */
1037 0xfffc, /* dst_mask */
1038 false), /* pcrel_offset */
1039
1040 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
1041 HOWTO (R_PPC64_GOT16_LO_DS, /* type */
1042 0, /* rightshift */
1043 1, /* size (0 = byte, 1 = short, 2 = long) */
1044 16, /* bitsize */
1045 false, /* pc_relative */
1046 0, /* bitpos */
1047 complain_overflow_dont, /* complain_on_overflow */
1048 bfd_elf_generic_reloc, /* special_function */
1049 "R_PPC64_GOT16_LO_DS", /* name */
1050 false, /* partial_inplace */
1051 0, /* src_mask */
1052 0xfffc, /* dst_mask */
1053 false), /* pcrel_offset */
1054
1055 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
1056 HOWTO (R_PPC64_PLT16_LO_DS, /* type */
1057 0, /* rightshift */
1058 1, /* size (0 = byte, 1 = short, 2 = long) */
1059 16, /* bitsize */
1060 false, /* pc_relative */
1061 0, /* bitpos */
1062 complain_overflow_dont, /* complain_on_overflow */
1063 bfd_elf_generic_reloc, /* special_function */
1064 "R_PPC64_PLT16_LO_DS", /* name */
1065 false, /* partial_inplace */
1066 0, /* src_mask */
1067 0xfffc, /* dst_mask */
1068 false), /* pcrel_offset */
1069
1070 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
1071 /* FIXME: Verify R_PPC64_SECTOFF. Seems strange with size=2 and
1072 dst_mask=0. */
1073 HOWTO (R_PPC64_SECTOFF_DS, /* type */
1074 0, /* rightshift */
1075 2, /* size (0 = byte, 1 = short, 2 = long) */
1076 32, /* bitsize */
1077 true, /* pc_relative */
1078 0, /* bitpos */
1079 complain_overflow_bitfield, /* complain_on_overflow */
1080 bfd_elf_generic_reloc, /* special_function */
1081 "R_PPC64_SECTOFF_DS", /* name */
1082 false, /* partial_inplace */
1083 0, /* src_mask */
1084 0, /* dst_mask */
1085 true), /* pcrel_offset */
1086
1087 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
1088 HOWTO (R_PPC64_SECTOFF_LO_DS, /* type */
1089 0, /* rightshift */
1090 1, /* size (0 = byte, 1 = short, 2 = long) */
1091 16, /* bitsize */
1092 false, /* pc_relative */
1093 0, /* bitpos */
1094 complain_overflow_dont, /* complain_on_overflow */
1095 bfd_elf_generic_reloc, /* special_function */
1096 "R_PPC64_SECTOFF_LO_DS",/* name */
1097 false, /* partial_inplace */
1098 0, /* src_mask */
1099 0xfffc, /* dst_mask */
1100 false), /* pcrel_offset */
1101
1102 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
1103 HOWTO (R_PPC64_TOC16_DS, /* type */
1104 0, /* rightshift */
1105 1, /* size (0 = byte, 1 = short, 2 = long) */
1106 16, /* bitsize */
1107 false, /* pc_relative */
1108 0, /* bitpos */
1109 complain_overflow_signed, /* complain_on_overflow */
1110 bfd_elf_generic_reloc, /* special_function */
1111 "R_PPC64_TOC16_DS", /* name */
1112 false, /* partial_inplace */
1113 0, /* src_mask */
1114 0xfffc, /* dst_mask */
1115 false), /* pcrel_offset */
1116
1117 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
1118 HOWTO (R_PPC64_TOC16_LO_DS, /* type */
1119 0, /* rightshift */
1120 1, /* size (0 = byte, 1 = short, 2 = long) */
1121 16, /* bitsize */
1122 false, /* pc_relative */
1123 0, /* bitpos */
1124 complain_overflow_dont, /* complain_on_overflow */
1125 bfd_elf_generic_reloc, /* special_function */
1126 "R_PPC64_TOC16_LO_DS", /* name */
1127 false, /* partial_inplace */
1128 0, /* src_mask */
1129 0xfffc, /* dst_mask */
1130 false), /* pcrel_offset */
1131
1132 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
1133 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
1134 HOWTO (R_PPC64_PLTGOT16_DS, /* type */
1135 0, /* rightshift */
1136 1, /* size (0 = byte, 1 = short, 2 = long) */
1137 16, /* bitsize */
1138 false, /* pc_relative */
1139 0, /* bitpos */
1140 complain_overflow_signed, /* complain_on_overflow */
1141 bfd_elf_generic_reloc, /* special_function */
1142 "R_PPC64_PLTGOT16_DS", /* name */
1143 false, /* partial_inplace */
1144 0, /* src_mask */
1145 0xfffc, /* dst_mask */
1146 false), /* pcrel_offset */
1147
1148 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
1149 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1150 HOWTO (R_PPC64_PLTGOT16_LO_DS,/* type */
1151 0, /* rightshift */
1152 1, /* size (0 = byte, 1 = short, 2 = long) */
1153 16, /* bitsize */
1154 false, /* pc_relative */
1155 0, /* bitpos */
1156 complain_overflow_dont, /* complain_on_overflow */
1157 bfd_elf_generic_reloc, /* special_function */
1158 "R_PPC64_PLTGOT16_LO_DS",/* name */
1159 false, /* partial_inplace */
1160 0, /* src_mask */
1161 0xfffc, /* dst_mask */
1162 false), /* pcrel_offset */
1163
1164 /* GNU extension to record C++ vtable hierarchy. */
1165 HOWTO (R_PPC64_GNU_VTINHERIT, /* type */
1166 0, /* rightshift */
1167 0, /* size (0 = byte, 1 = short, 2 = long) */
1168 0, /* bitsize */
1169 false, /* pc_relative */
1170 0, /* bitpos */
1171 complain_overflow_dont, /* complain_on_overflow */
1172 NULL, /* special_function */
1173 "R_PPC64_GNU_VTINHERIT", /* name */
1174 false, /* partial_inplace */
1175 0, /* src_mask */
1176 0, /* dst_mask */
1177 false), /* pcrel_offset */
1178
1179 /* GNU extension to record C++ vtable member usage. */
1180 HOWTO (R_PPC64_GNU_VTENTRY, /* type */
1181 0, /* rightshift */
1182 0, /* size (0 = byte, 1 = short, 2 = long) */
1183 0, /* bitsize */
1184 false, /* pc_relative */
1185 0, /* bitpos */
1186 complain_overflow_dont, /* complain_on_overflow */
1187 NULL, /* special_function */
1188 "R_PPC64_GNU_VTENTRY", /* name */
1189 false, /* partial_inplace */
1190 0, /* src_mask */
1191 0, /* dst_mask */
1192 false), /* pcrel_offset */
1193};
1194
1195\f
1196/* Initialize the ppc64_elf_howto_table, so that linear accesses can
1197 be done. */
1198
1199static void
1200ppc_howto_init ()
1201{
1202 unsigned int i, type;
1203
1204 for (i = 0;
1205 i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
1206 i++)
1207 {
1208 type = ppc64_elf_howto_raw[i].type;
1209 BFD_ASSERT (type < (sizeof (ppc64_elf_howto_table)
1210 / sizeof (ppc64_elf_howto_table[0])));
1211 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
1212 }
1213}
1214
1215static reloc_howto_type *
1216ppc64_elf_reloc_type_lookup (abfd, code)
1217 bfd *abfd ATTRIBUTE_UNUSED;
1218 bfd_reloc_code_real_type code;
1219{
1220 enum elf_ppc_reloc_type ppc_reloc = R_PPC_NONE;
1221
1222 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
1223 /* Initialize howto table if needed. */
1224 ppc_howto_init ();
1225
1226 switch ((int) code)
1227 {
1228 default:
1229 return (reloc_howto_type *) NULL;
1230
1231 case BFD_RELOC_NONE: ppc_reloc = R_PPC64_NONE;
1232 break;
1233 case BFD_RELOC_32: ppc_reloc = R_PPC64_ADDR32;
1234 break;
1235 case BFD_RELOC_PPC_BA26: ppc_reloc = R_PPC64_ADDR24;
1236 break;
1237 case BFD_RELOC_16: ppc_reloc = R_PPC64_ADDR16;
1238 break;
1239 case BFD_RELOC_LO16: ppc_reloc = R_PPC64_ADDR16_LO;
1240 break;
1241 case BFD_RELOC_HI16: ppc_reloc = R_PPC64_ADDR16_HI;
1242 break;
1243 case BFD_RELOC_HI16_S: ppc_reloc = R_PPC64_ADDR16_HA;
1244 break;
1245 case BFD_RELOC_PPC_BA16: ppc_reloc = R_PPC64_ADDR14;
1246 break;
1247 case BFD_RELOC_PPC_BA16_BRTAKEN: ppc_reloc = R_PPC64_ADDR14_BRTAKEN;
1248 break;
1249 case BFD_RELOC_PPC_BA16_BRNTAKEN: ppc_reloc = R_PPC64_ADDR14_BRNTAKEN;
1250 break;
1251 case BFD_RELOC_PPC_B26: ppc_reloc = R_PPC64_REL24;
1252 break;
1253 case BFD_RELOC_PPC_B16: ppc_reloc = R_PPC64_REL14;
1254 break;
1255 case BFD_RELOC_PPC_B16_BRTAKEN: ppc_reloc = R_PPC64_REL14_BRTAKEN;
1256 break;
1257 case BFD_RELOC_PPC_B16_BRNTAKEN: ppc_reloc = R_PPC64_REL14_BRNTAKEN;
1258 break;
1259 case BFD_RELOC_16_GOTOFF: ppc_reloc = R_PPC64_GOT16;
1260 break;
1261 case BFD_RELOC_LO16_GOTOFF: ppc_reloc = R_PPC64_GOT16_LO;
1262 break;
1263 case BFD_RELOC_HI16_GOTOFF: ppc_reloc = R_PPC64_GOT16_HI;
1264 break;
1265 case BFD_RELOC_HI16_S_GOTOFF: ppc_reloc = R_PPC64_GOT16_HA;
1266 break;
1267 case BFD_RELOC_PPC_COPY: ppc_reloc = R_PPC64_COPY;
1268 break;
1269 case BFD_RELOC_PPC_GLOB_DAT: ppc_reloc = R_PPC64_GLOB_DAT;
1270 break;
1271 case BFD_RELOC_32_PCREL: ppc_reloc = R_PPC64_REL32;
1272 break;
1273 case BFD_RELOC_32_PLTOFF: ppc_reloc = R_PPC64_PLT32;
1274 break;
1275 case BFD_RELOC_32_PLT_PCREL: ppc_reloc = R_PPC64_PLTREL32;
1276 break;
1277 case BFD_RELOC_LO16_PLTOFF: ppc_reloc = R_PPC64_PLT16_LO;
1278 break;
1279 case BFD_RELOC_HI16_PLTOFF: ppc_reloc = R_PPC64_PLT16_HI;
1280 break;
1281 case BFD_RELOC_HI16_S_PLTOFF: ppc_reloc = R_PPC64_PLT16_HA;
1282 break;
1283 case BFD_RELOC_32_BASEREL: ppc_reloc = R_PPC64_SECTOFF;
1284 break;
1285 case BFD_RELOC_LO16_BASEREL: ppc_reloc = R_PPC64_SECTOFF_LO;
1286 break;
1287 case BFD_RELOC_HI16_BASEREL: ppc_reloc = R_PPC64_SECTOFF_HI;
1288 break;
1289 case BFD_RELOC_HI16_S_BASEREL: ppc_reloc = R_PPC64_SECTOFF_HA;
1290 break;
1291 /* FIXME: Is CTOR 32 or 64 bits? Fix md_apply_fix3 in
1292 gas/config/tc-ppc.c too. */
1293 case BFD_RELOC_CTOR: ppc_reloc = R_PPC64_ADDR32;
1294 break;
1295 case BFD_RELOC_64: ppc_reloc = R_PPC64_ADDR64;
1296 break;
1297 case BFD_RELOC_PPC64_HIGHER: ppc_reloc = R_PPC64_ADDR16_HIGHER;
1298 break;
1299 case BFD_RELOC_PPC64_HIGHER_S: ppc_reloc = R_PPC64_ADDR16_HIGHERA;
1300 break;
1301 case BFD_RELOC_PPC64_HIGHEST: ppc_reloc = R_PPC64_ADDR16_HIGHEST;
1302 break;
1303 case BFD_RELOC_PPC64_HIGHEST_S: ppc_reloc = R_PPC64_ADDR16_HIGHESTA;
1304 break;
1305 case BFD_RELOC_64_PCREL: ppc_reloc = R_PPC64_REL64;
1306 break;
1307 case BFD_RELOC_64_PLTOFF: ppc_reloc = R_PPC64_PLT64;
1308 break;
1309 case BFD_RELOC_64_PLT_PCREL: ppc_reloc = R_PPC64_PLTREL64;
1310 break;
1311 case BFD_RELOC_PPC_TOC16: ppc_reloc = R_PPC64_TOC16;
1312 break;
1313 case BFD_RELOC_PPC64_TOC16_LO: ppc_reloc = R_PPC64_TOC16_LO;
1314 break;
1315 case BFD_RELOC_PPC64_TOC16_HI: ppc_reloc = R_PPC64_TOC16_HI;
1316 break;
1317 case BFD_RELOC_PPC64_TOC16_HA: ppc_reloc = R_PPC64_TOC16_HA;
1318 break;
1319 case BFD_RELOC_PPC64_TOC: ppc_reloc = R_PPC64_TOC;
1320 break;
1321 case BFD_RELOC_PPC64_PLTGOT16: ppc_reloc = R_PPC64_PLTGOT16;
1322 break;
1323 case BFD_RELOC_PPC64_PLTGOT16_LO: ppc_reloc = R_PPC64_PLTGOT16_LO;
1324 break;
1325 case BFD_RELOC_PPC64_PLTGOT16_HI: ppc_reloc = R_PPC64_PLTGOT16_HI;
1326 break;
1327 case BFD_RELOC_PPC64_PLTGOT16_HA: ppc_reloc = R_PPC64_PLTGOT16_HA;
1328 break;
1329 case BFD_RELOC_PPC64_ADDR16_DS: ppc_reloc = R_PPC64_ADDR16_DS;
1330 break;
1331 case BFD_RELOC_PPC64_ADDR16_LO_DS: ppc_reloc = R_PPC64_ADDR16_LO_DS;
1332 break;
1333 case BFD_RELOC_PPC64_GOT16_DS: ppc_reloc = R_PPC64_GOT16_DS;
1334 break;
1335 case BFD_RELOC_PPC64_GOT16_LO_DS: ppc_reloc = R_PPC64_GOT16_LO_DS;
1336 break;
1337 case BFD_RELOC_PPC64_PLT16_LO_DS: ppc_reloc = R_PPC64_PLT16_LO_DS;
1338 break;
1339 case BFD_RELOC_PPC64_SECTOFF_DS: ppc_reloc = R_PPC64_SECTOFF_DS;
1340 break;
1341 case BFD_RELOC_PPC64_SECTOFF_LO_DS: ppc_reloc = R_PPC64_SECTOFF_LO_DS;
1342 break;
1343 case BFD_RELOC_PPC64_TOC16_DS: ppc_reloc = R_PPC64_TOC16_DS;
1344 break;
1345 case BFD_RELOC_PPC64_TOC16_LO_DS: ppc_reloc = R_PPC64_TOC16_LO_DS;
1346 break;
1347 case BFD_RELOC_PPC64_PLTGOT16_DS: ppc_reloc = R_PPC64_PLTGOT16_DS;
1348 break;
1349 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: ppc_reloc = R_PPC64_PLTGOT16_LO_DS;
1350 break;
1351 case BFD_RELOC_VTABLE_INHERIT: ppc_reloc = R_PPC64_GNU_VTINHERIT;
1352 break;
1353 case BFD_RELOC_VTABLE_ENTRY: ppc_reloc = R_PPC64_GNU_VTENTRY;
1354 break;
1355 }
1356
1357 return ppc64_elf_howto_table[(int) ppc_reloc];
1358};
1359
1360/* Set the howto pointer for a PowerPC ELF reloc. */
1361
1362static void
1363ppc64_elf_info_to_howto (abfd, cache_ptr, dst)
1364 bfd *abfd ATTRIBUTE_UNUSED;
1365 arelent *cache_ptr;
1366 Elf64_Internal_Rela *dst;
1367{
65f38f15
AM
1368 unsigned int type;
1369
5bd4f169
AM
1370 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
1371 /* Initialize howto table if needed. */
1372 ppc_howto_init ();
1373
65f38f15
AM
1374 type = ELF64_R_TYPE (dst->r_info);
1375 BFD_ASSERT (type < (sizeof (ppc64_elf_howto_table)
1376 / sizeof (ppc64_elf_howto_table[0])));
1377 cache_ptr->howto = ppc64_elf_howto_table[type];
5bd4f169
AM
1378}
1379
65f38f15 1380/* Handle the R_PPC_ADDR16_HA and similar relocs. */
5bd4f169
AM
1381
1382static bfd_reloc_status_type
1383ppc64_elf_addr16_ha_reloc (abfd, reloc_entry, symbol, data, input_section,
1384 output_bfd, error_message)
1385 bfd *abfd ATTRIBUTE_UNUSED;
1386 arelent *reloc_entry;
1387 asymbol *symbol;
1388 PTR data ATTRIBUTE_UNUSED;
1389 asection *input_section;
1390 bfd *output_bfd;
1391 char **error_message ATTRIBUTE_UNUSED;
1392{
1393 bfd_vma relocation;
1394
1395 if (output_bfd != NULL)
1396 {
1397 reloc_entry->address += input_section->output_offset;
1398 return bfd_reloc_ok;
1399 }
1400
1401 if (reloc_entry->address > input_section->_cooked_size)
1402 return bfd_reloc_outofrange;
1403
1404 if (bfd_is_com_section (symbol->section))
1405 relocation = 0;
1406 else
1407 relocation = symbol->value;
1408
1409 relocation += symbol->section->output_section->vma;
1410 relocation += symbol->section->output_offset;
1411 relocation += reloc_entry->addend;
1412
1413 reloc_entry->addend += (relocation & 0x8000) << 1;
1414
1415 return bfd_reloc_continue;
1416}
1417
1418/* Function to set whether a module needs the -mrelocatable bit set. */
1419
1420static boolean
1421ppc64_elf_set_private_flags (abfd, flags)
1422 bfd *abfd;
1423 flagword flags;
1424{
1425 BFD_ASSERT (!elf_flags_init (abfd)
1426 || elf_elfheader (abfd)->e_flags == flags);
1427
1428 elf_elfheader (abfd)->e_flags = flags;
1429 elf_flags_init (abfd) = true;
1430 return true;
1431}
1432
1433/* Copy backend specific data from one object module to another. */
1434static boolean
1435ppc64_elf_copy_private_bfd_data (ibfd, obfd)
1436 bfd *ibfd;
1437 bfd *obfd;
1438{
1439 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1440 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1441 return true;
1442
1443 BFD_ASSERT (!elf_flags_init (obfd)
1444 || elf_elfheader (obfd)->e_flags == elf_elfheader (ibfd)->e_flags);
1445
1446 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1447 elf_flags_init (obfd) = true;
1448 return true;
1449}
1450
1451/* Merge backend specific data from an object file to the output
1452 object file when linking. */
1453static boolean
1454ppc64_elf_merge_private_bfd_data (ibfd, obfd)
1455 bfd *ibfd;
1456 bfd *obfd;
1457{
1458 flagword old_flags;
1459 flagword new_flags;
1460 boolean error;
1461
1462 /* Check if we have the same endianess. */
1463 if (ibfd->xvec->byteorder != obfd->xvec->byteorder
1464 && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
1465 {
1466 const char *msg;
1467
1468 if (bfd_big_endian (ibfd))
1469 msg = _("%s: compiled for a big endian system and target is little endian");
1470 else
1471 msg = _("%s: compiled for a little endian system and target is big endian");
1472
8f615d07 1473 (*_bfd_error_handler) (msg, bfd_archive_filename (ibfd));
5bd4f169
AM
1474
1475 bfd_set_error (bfd_error_wrong_format);
1476 return false;
1477 }
1478
1479 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1480 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1481 return true;
1482
1483 new_flags = elf_elfheader (ibfd)->e_flags;
1484 old_flags = elf_elfheader (obfd)->e_flags;
1485 if (!elf_flags_init (obfd))
1486 {
1487 /* First call, no flags set. */
1488 elf_flags_init (obfd) = true;
1489 elf_elfheader (obfd)->e_flags = new_flags;
1490 }
1491
1492 else if (new_flags == old_flags)
1493 /* Compatible flags are ok. */
1494 ;
1495
1496 else
1497 {
1498 /* Incompatible flags. Warn about -mrelocatable mismatch.
1499 Allow -mrelocatable-lib to be linked with either. */
1500 error = false;
1501 if ((new_flags & EF_PPC_RELOCATABLE) != 0
1502 && (old_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0)
1503 {
1504 error = true;
1505 (*_bfd_error_handler)
1506 (_("%s: compiled with -mrelocatable and linked with modules compiled normally"),
8f615d07 1507 bfd_archive_filename (ibfd));
5bd4f169
AM
1508 }
1509 else if ((new_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0
1510 && (old_flags & EF_PPC_RELOCATABLE) != 0)
1511 {
1512 error = true;
1513 (*_bfd_error_handler)
1514 (_("%s: compiled normally and linked with modules compiled with -mrelocatable"),
8f615d07 1515 bfd_archive_filename (ibfd));
5bd4f169
AM
1516 }
1517
1518 /* The output is -mrelocatable-lib iff both the input files are. */
1519 if (! (new_flags & EF_PPC_RELOCATABLE_LIB))
1520 elf_elfheader (obfd)->e_flags &= ~EF_PPC_RELOCATABLE_LIB;
1521
1522 /* The output is -mrelocatable iff it can't be -mrelocatable-lib,
1523 but each input file is either -mrelocatable or -mrelocatable-lib. */
1524 if (! (elf_elfheader (obfd)->e_flags & EF_PPC_RELOCATABLE_LIB)
1525 && (new_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE))
1526 && (old_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE)))
1527 elf_elfheader (obfd)->e_flags |= EF_PPC_RELOCATABLE;
1528
1529 /* Do not warn about eabi vs. V.4 mismatch, just or in the bit
1530 if any module uses it. */
1531 elf_elfheader (obfd)->e_flags |= (new_flags & EF_PPC_EMB);
1532
1533 new_flags &= ~(EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
1534 old_flags &= ~(EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
1535
1536 /* Warn about any other mismatches. */
1537 if (new_flags != old_flags)
1538 {
1539 error = true;
1540 (*_bfd_error_handler)
1541 (_("%s: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
8f615d07 1542 bfd_archive_filename (ibfd), (long) new_flags, (long) old_flags);
5bd4f169
AM
1543 }
1544
1545 if (error)
1546 {
1547 bfd_set_error (bfd_error_bad_value);
1548 return false;
1549 }
1550 }
1551
1552 return true;
1553}
1554
1555/* Handle a PowerPC specific section when reading an object file. This
1556 is called when elfcode.h finds a section with an unknown type. */
1557
1558static boolean
1559ppc64_elf_section_from_shdr (abfd, hdr, name)
1560 bfd *abfd;
1561 Elf64_Internal_Shdr *hdr;
1562 char *name;
1563{
1564 asection *newsect;
1565 flagword flags;
1566
1567 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1568 return false;
1569
1570 newsect = hdr->bfd_section;
1571 flags = bfd_get_section_flags (abfd, newsect);
1572 if (hdr->sh_flags & SHF_EXCLUDE)
1573 flags |= SEC_EXCLUDE;
1574
1575 if (hdr->sh_type == SHT_ORDERED)
1576 flags |= SEC_SORT_ENTRIES;
1577
1578 bfd_set_section_flags (abfd, newsect, flags);
1579 return true;
1580}
1581\f
65f38f15
AM
1582/* The following functions are specific to the ELF linker, while
1583 functions above are used generally. Those named ppc64_elf_* are
1584 called by the main ELF linker code. They appear in this file more
1585 or less in the order in which they are called. eg.
1586 ppc64_elf_check_relocs is called early in the link process,
1587 ppc64_elf_finish_dynamic_sections is one of the last functions
1588 called. */
1589
1590/* The linker needs to keep track of the number of relocs that it
1591 decides to copy as dynamic relocs in check_relocs for each symbol.
1592 This is so that it can later discard them if they are found to be
1593 unnecessary. We store the information in a field extending the
1594 regular ELF linker hash table. */
1595
1596struct ppc_dyn_relocs
1597{
1598 struct ppc_dyn_relocs *next;
1599
1600 /* The input section of the reloc. */
1601 asection *sec;
1602
1603 /* Total number of relocs copied for the input section. */
1604 bfd_size_type count;
1605
1606 /* Number of pc-relative relocs copied for the input section. */
1607 bfd_size_type pc_count;
1608};
1609
1610/* Of those relocs that might be copied as dynamic relocs, this macro
1611 selects between relative and absolute types. */
1612
1613#define IS_ABSOLUTE_RELOC(RTYPE) \
1614 ((RTYPE) != R_PPC64_REL14 \
1615 && (RTYPE) != R_PPC64_REL14_BRNTAKEN \
1616 && (RTYPE) != R_PPC64_REL14_BRTAKEN \
1617 && (RTYPE) != R_PPC64_REL24 \
1618 && (RTYPE) != R_PPC64_REL32 \
1619 && (RTYPE) != R_PPC64_REL64)
1620
1621/* ppc64 ELF linker hash entry. */
1622
1623struct ppc_link_hash_entry
1624{
1625 struct elf_link_hash_entry elf;
1626
1627 /* Track dynamic relocs copied for this symbol. */
1628 struct ppc_dyn_relocs *dyn_relocs;
1629};
1630
1631/* ppc64 ELF linker hash table. */
1632
1633struct ppc_link_hash_table
1634{
1635 struct elf_link_hash_table elf;
1636
1637 /* Short-cuts to get to dynamic linker sections. */
1638 asection *sgot;
1639 asection *srelgot;
1640 asection *splt;
1641 asection *srelplt;
1642 asection *sdynbss;
1643 asection *srelbss;
1644 asection *sglink;
1645};
1646
1647/* Get the ppc64 ELF linker hash table from a link_info structure. */
1648
1649#define ppc_hash_table(p) \
1650 ((struct ppc_link_hash_table *) ((p)->hash))
1651
1652/* Create an entry in a ppc64 ELF linker hash table. */
1653
1654static struct bfd_hash_entry *
1655link_hash_newfunc (entry, table, string)
1656 struct bfd_hash_entry *entry;
1657 struct bfd_hash_table *table;
1658 const char *string;
1659{
1660 /* Allocate the structure if it has not already been allocated by a
1661 subclass. */
1662 if (entry == NULL)
1663 {
1664 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
1665 if (entry == NULL)
1666 return entry;
1667 }
1668
1669 /* Call the allocation method of the superclass. */
1670 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
1671 if (entry != NULL)
1672 {
1673 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
1674
1675 eh->dyn_relocs = NULL;
1676 }
1677
1678 return entry;
1679}
1680
1681/* Create a ppc64 ELF linker hash table. */
1682
1683static struct bfd_link_hash_table *
1684ppc64_elf_link_hash_table_create (abfd)
1685 bfd *abfd;
1686{
1687 struct ppc_link_hash_table *htab;
1688 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
1689
1690 htab = (struct ppc_link_hash_table *) bfd_alloc (abfd, amt);
1691 if (htab == NULL)
1692 return NULL;
1693
1694 if (! _bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc))
1695 {
1696 bfd_release (abfd, htab);
1697 return NULL;
1698 }
1699
1700 htab->sgot = NULL;
1701 htab->srelgot = NULL;
1702 htab->splt = NULL;
1703 htab->srelplt = NULL;
1704 htab->sdynbss = NULL;
1705 htab->srelbss = NULL;
1706 htab->sglink = NULL;
1707
1708 return &htab->elf.root;
1709}
1710
1711/* Create .got and .rela.got sections in DYNOBJ, and set up
1712 shortcuts to them in our hash table. */
1713
1714static boolean
1715create_got_section (dynobj, info)
1716 bfd *dynobj;
1717 struct bfd_link_info *info;
1718{
1719 struct ppc_link_hash_table *htab;
1720
1721 if (! _bfd_elf_create_got_section (dynobj, info))
1722 return false;
1723
1724 htab = ppc_hash_table (info);
1725 htab->sgot = bfd_get_section_by_name (dynobj, ".got");
1726 if (!htab->sgot)
1727 abort ();
1728
1729 htab->srelgot = bfd_make_section (dynobj, ".rela.got");
1730 if (!htab->srelgot
1731 || ! bfd_set_section_flags (dynobj, htab->srelgot,
1732 (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
1733 | SEC_IN_MEMORY | SEC_LINKER_CREATED
1734 | SEC_READONLY))
1735 || ! bfd_set_section_alignment (dynobj, htab->srelgot, 3))
1736 return false;
1737 return true;
1738}
5bd4f169
AM
1739
1740/* Create the .glink section as well as the ordinary dynamic
1741 sections. */
1742
1743static boolean
65f38f15
AM
1744ppc64_elf_create_dynamic_sections (dynobj, info)
1745 bfd *dynobj;
5bd4f169
AM
1746 struct bfd_link_info *info;
1747{
65f38f15 1748 struct ppc_link_hash_table *htab;
5bd4f169
AM
1749 flagword flags;
1750
65f38f15
AM
1751 htab = ppc_hash_table (info);
1752 if (!htab->sgot && !create_got_section (dynobj, info))
5bd4f169
AM
1753 return false;
1754
65f38f15 1755 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
5bd4f169 1756 return false;
65f38f15
AM
1757
1758 htab->splt = bfd_get_section_by_name (dynobj, ".plt");
1759 htab->srelplt = bfd_get_section_by_name (dynobj, ".rela.plt");
1760 htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
1761 if (!info->shared)
1762 htab->srelbss = bfd_get_section_by_name (dynobj, ".rela.bss");
1763
1764 if (!htab->splt || !htab->srelplt || !htab->sdynbss
1765 || (!info->shared && !htab->srelbss))
1766 abort ();
1767
1768 /* Our .plt just contains pointers, no code. */
1769 flags = bfd_get_section_flags (dynobj, htab->splt);
5bd4f169 1770 flags &= ~SEC_CODE;
65f38f15 1771 if (! bfd_set_section_flags (dynobj, htab->splt, flags))
5bd4f169
AM
1772 return false;
1773
1774 /* Create .glink for global linkage functions. */
1775 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
1776 | SEC_LINKER_CREATED);
65f38f15
AM
1777 htab->sglink = bfd_make_section (dynobj, ".glink");
1778 if (htab->sglink == NULL
1779 || ! bfd_set_section_flags (dynobj, htab->sglink, flags)
1780 || ! bfd_set_section_alignment (dynobj, htab->sglink, 3))
5bd4f169
AM
1781 return false;
1782
1783 return true;
1784}
1785
65f38f15
AM
1786/* Copy the extra info we tack onto an elf_link_hash_entry. */
1787
1788static void
1789ppc64_elf_copy_indirect_symbol (dir, ind)
1790 struct elf_link_hash_entry *dir, *ind;
1791{
1792 struct ppc_link_hash_entry *edir, *eind;
1793
1794 edir = (struct ppc_link_hash_entry *) dir;
1795 eind = (struct ppc_link_hash_entry *) ind;
1796
1797 if (edir->dyn_relocs == NULL)
1798 {
1799 edir->dyn_relocs = eind->dyn_relocs;
1800 eind->dyn_relocs = NULL;
1801 }
1802 else if (eind->dyn_relocs != NULL)
1803 abort ();
1804
1805 _bfd_elf_link_hash_copy_indirect (dir, ind);
1806}
1807
5bd4f169 1808/* Look through the relocs for a section during the first phase, and
65f38f15
AM
1809 calculate needed space in the global offset table, procedure
1810 linkage table, and dynamic reloc sections. */
5bd4f169
AM
1811
1812static boolean
1813ppc64_elf_check_relocs (abfd, info, sec, relocs)
1814 bfd *abfd;
1815 struct bfd_link_info *info;
1816 asection *sec;
1817 const Elf_Internal_Rela *relocs;
1818{
65f38f15 1819 struct ppc_link_hash_table *htab;
5bd4f169
AM
1820 Elf_Internal_Shdr *symtab_hdr;
1821 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
1822 const Elf_Internal_Rela *rel;
1823 const Elf_Internal_Rela *rel_end;
5bd4f169 1824 asection *sreloc;
5bd4f169
AM
1825
1826 if (info->relocateable)
1827 return true;
1828
65f38f15 1829 htab = ppc_hash_table (info);
5bd4f169 1830 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5bd4f169
AM
1831
1832 sym_hashes = elf_sym_hashes (abfd);
1833 sym_hashes_end = (sym_hashes
1834 + symtab_hdr->sh_size / sizeof (Elf64_External_Sym));
1835 if (!elf_bad_symtab (abfd))
1836 sym_hashes_end -= symtab_hdr->sh_info;
1837
1838 sreloc = NULL;
1839
1840 rel_end = relocs + sec->reloc_count;
1841 for (rel = relocs; rel < rel_end; rel++)
1842 {
1843 unsigned long r_symndx;
1844 struct elf_link_hash_entry *h;
a33d1f77 1845 enum elf_ppc_reloc_type r_type;
5bd4f169
AM
1846
1847 r_symndx = ELF64_R_SYM (rel->r_info);
1848 if (r_symndx < symtab_hdr->sh_info)
1849 h = NULL;
1850 else
1851 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1852
a33d1f77
AM
1853 r_type = (enum elf_ppc_reloc_type) ELF64_R_TYPE (rel->r_info);
1854 switch (r_type)
5bd4f169
AM
1855 {
1856 /* GOT16 relocations */
1857 case R_PPC64_GOT16:
5bd4f169 1858 case R_PPC64_GOT16_DS:
65f38f15
AM
1859 case R_PPC64_GOT16_HA:
1860 case R_PPC64_GOT16_HI:
1861 case R_PPC64_GOT16_LO:
5bd4f169 1862 case R_PPC64_GOT16_LO_DS:
5bd4f169 1863
65f38f15
AM
1864 /* This symbol requires a global offset table entry. */
1865 if (htab->sgot == NULL)
5bd4f169 1866 {
65f38f15
AM
1867 if (htab->elf.dynobj == NULL)
1868 htab->elf.dynobj = abfd;
1869 if (!create_got_section (htab->elf.dynobj, info))
5bd4f169 1870 return false;
5bd4f169
AM
1871 }
1872
1873 if (h != NULL)
1874 {
65f38f15 1875 h->got.refcount += 1;
5bd4f169
AM
1876 }
1877 else
1878 {
65f38f15
AM
1879 bfd_signed_vma *local_got_refcounts;
1880
5bd4f169 1881 /* This is a global offset table entry for a local symbol. */
65f38f15 1882 local_got_refcounts = elf_local_got_refcounts (abfd);
5bd4f169
AM
1883 if (local_got_refcounts == NULL)
1884 {
dc810e39 1885 bfd_size_type size;
5bd4f169 1886
dc810e39
AM
1887 size = symtab_hdr->sh_info;
1888 size *= sizeof (bfd_signed_vma);
1889 local_got_refcounts = ((bfd_signed_vma *)
65f38f15 1890 bfd_zalloc (abfd, size));
5bd4f169
AM
1891 if (local_got_refcounts == NULL)
1892 return false;
1893 elf_local_got_refcounts (abfd) = local_got_refcounts;
5bd4f169 1894 }
65f38f15 1895 local_got_refcounts[r_symndx] += 1;
5bd4f169
AM
1896 }
1897 break;
1898
5bd4f169 1899 case R_PPC64_PLT16_HA:
65f38f15
AM
1900 case R_PPC64_PLT16_HI:
1901 case R_PPC64_PLT16_LO:
1902 case R_PPC64_PLT32:
1903 case R_PPC64_PLT64:
5bd4f169
AM
1904 /* This symbol requires a procedure linkage table entry. We
1905 actually build the entry in adjust_dynamic_symbol,
1906 because this might be a case of linking PIC code without
1907 linking in any dynamic objects, in which case we don't
1908 need to generate a procedure linkage table after all. */
5bd4f169
AM
1909 if (h == NULL)
1910 {
1911 /* It does not make sense to have a procedure linkage
1912 table entry for a local symbol. */
1913 bfd_set_error (bfd_error_bad_value);
1914 return false;
1915 }
1916
65f38f15
AM
1917 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
1918 h->plt.refcount += 1;
5bd4f169
AM
1919 break;
1920
1921 /* The following relocations don't need to propagate the
1922 relocation if linking a shared object since they are
1923 section relative. */
1924 case R_PPC64_SECTOFF:
1925 case R_PPC64_SECTOFF_LO:
1926 case R_PPC64_SECTOFF_HI:
1927 case R_PPC64_SECTOFF_HA:
1928 case R_PPC64_SECTOFF_DS:
1929 case R_PPC64_SECTOFF_LO_DS:
1930 case R_PPC64_TOC16:
1931 case R_PPC64_TOC16_LO:
1932 case R_PPC64_TOC16_HI:
1933 case R_PPC64_TOC16_HA:
1934 case R_PPC64_TOC16_DS:
1935 case R_PPC64_TOC16_LO_DS:
1936 break;
1937
1938 /* This relocation describes the C++ object vtable hierarchy.
1939 Reconstruct it for later use during GC. */
1940 case R_PPC64_GNU_VTINHERIT:
1941 if (!_bfd_elf64_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
1942 return false;
1943 break;
1944
1945 /* This relocation describes which C++ vtable entries are actually
1946 used. Record for later use during GC. */
1947 case R_PPC64_GNU_VTENTRY:
1948 if (!_bfd_elf64_gc_record_vtentry (abfd, sec, h, rel->r_addend))
1949 return false;
1950 break;
1951
5bd4f169
AM
1952 case R_PPC64_REL64:
1953 case R_PPC64_REL32:
1954 case R_PPC64_REL24:
1955 case R_PPC64_REL14:
1956 case R_PPC64_REL14_BRTAKEN:
1957 case R_PPC64_REL14_BRNTAKEN:
65f38f15
AM
1958 case R_PPC64_ADDR14:
1959 case R_PPC64_ADDR14_BRNTAKEN:
1960 case R_PPC64_ADDR14_BRTAKEN:
1961 case R_PPC64_ADDR16:
1962 case R_PPC64_ADDR16_DS:
1963 case R_PPC64_ADDR16_HA:
1964 case R_PPC64_ADDR16_HI:
1965 case R_PPC64_ADDR16_HIGHER:
1966 case R_PPC64_ADDR16_HIGHERA:
1967 case R_PPC64_ADDR16_HIGHEST:
1968 case R_PPC64_ADDR16_HIGHESTA:
1969 case R_PPC64_ADDR16_LO:
1970 case R_PPC64_ADDR16_LO_DS:
1971 case R_PPC64_ADDR24:
1972 case R_PPC64_ADDR30:
1973 case R_PPC64_ADDR32:
1974 case R_PPC64_ADDR64:
1975 case R_PPC64_UADDR16:
1976 case R_PPC64_UADDR32:
1977 case R_PPC64_UADDR64:
5bd4f169 1978 case R_PPC64_TOC:
65f38f15
AM
1979 /* If we are creating a shared library, and this is a reloc
1980 against a global symbol, or a non PC relative reloc
1981 against a local symbol, then we need to copy the reloc
1982 into the shared library. However, if we are linking with
1983 -Bsymbolic, we do not need to copy a reloc against a
1984 global symbol which is defined in an object we are
1985 including in the link (i.e., DEF_REGULAR is set). At
1986 this point we have not seen all the input files, so it is
1987 possible that DEF_REGULAR is not set now but will be set
1988 later (it is never cleared). In case of a weak definition,
1989 DEF_REGULAR may be cleared later by a strong definition in
1990 a shared library. We account for that possibility below by
1991 storing information in the relocs_copied field of the hash
1992 table entry. A similar situation occurs when creating
1993 shared libraries and symbol visibility changes render the
1994 symbol local.
1995
1996 If on the other hand, we are creating an executable, we
1997 may need to keep relocations for symbols satisfied by a
1998 dynamic library if we manage to avoid copy relocs for the
1999 symbol. */
2000 if ((info->shared
2001 && (sec->flags & SEC_ALLOC) != 0
a33d1f77 2002 && (IS_ABSOLUTE_RELOC (r_type)
65f38f15
AM
2003 || (h != NULL
2004 && (! info->symbolic
2005 || h->root.type == bfd_link_hash_defweak
2006 || (h->elf_link_hash_flags
2007 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
2008 || (!info->shared
2009 && (sec->flags & SEC_ALLOC) != 0
2010 && h != NULL
2011 && (h->root.type == bfd_link_hash_defweak
2012 || (h->elf_link_hash_flags
2013 & ELF_LINK_HASH_DEF_REGULAR) == 0)))
5bd4f169 2014 {
65f38f15
AM
2015 /* We must copy these reloc types into the output file.
2016 Create a reloc section in dynobj and make room for
2017 this reloc. */
5bd4f169
AM
2018 if (sreloc == NULL)
2019 {
2020 const char *name;
65f38f15 2021 bfd *dynobj;
5bd4f169
AM
2022
2023 name = (bfd_elf_string_from_elf_section
2024 (abfd,
2025 elf_elfheader (abfd)->e_shstrndx,
2026 elf_section_data (sec)->rel_hdr.sh_name));
2027 if (name == NULL)
2028 return false;
2029
65f38f15
AM
2030 if (strncmp (name, ".rela", 5) != 0
2031 || strcmp (bfd_get_section_name (abfd, sec),
2032 name + 5) != 0)
2033 {
2034 (*_bfd_error_handler)
2035 (_("%s: bad relocation section name `%s\'"),
2036 bfd_archive_filename (abfd), name);
2037 }
2038
2039 if (htab->elf.dynobj == NULL)
2040 htab->elf.dynobj = abfd;
5bd4f169 2041
65f38f15 2042 dynobj = htab->elf.dynobj;
5bd4f169
AM
2043 sreloc = bfd_get_section_by_name (dynobj, name);
2044 if (sreloc == NULL)
2045 {
2046 flagword flags;
2047
2048 sreloc = bfd_make_section (dynobj, name);
2049 flags = (SEC_HAS_CONTENTS | SEC_READONLY
2050 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2051 if ((sec->flags & SEC_ALLOC) != 0)
2052 flags |= SEC_ALLOC | SEC_LOAD;
2053 if (sreloc == NULL
2054 || ! bfd_set_section_flags (dynobj, sreloc, flags)
65f38f15 2055 || ! bfd_set_section_alignment (dynobj, sreloc, 3))
5bd4f169
AM
2056 return false;
2057 }
65f38f15 2058 elf_section_data (sec)->sreloc = sreloc;
5bd4f169
AM
2059 }
2060
65f38f15
AM
2061 /* If this is a global symbol, we count the number of
2062 relocations we need for this symbol. */
2063 if (h != NULL)
2064 {
2065 struct ppc_link_hash_entry *eh;
2066 struct ppc_dyn_relocs *p;
2067
2068 eh = (struct ppc_link_hash_entry *) h;
2069 p = eh->dyn_relocs;
5bd4f169 2070
65f38f15
AM
2071 if (p == NULL || p->sec != sec)
2072 {
2073 p = ((struct ppc_dyn_relocs *)
2074 bfd_alloc (htab->elf.dynobj,
2075 (bfd_size_type) sizeof *p));
2076 if (p == NULL)
2077 return false;
2078 p->next = eh->dyn_relocs;
2079 eh->dyn_relocs = p;
2080 p->sec = sec;
2081 p->count = 0;
2082 p->pc_count = 0;
2083 }
5bd4f169 2084
65f38f15 2085 p->count += 1;
a33d1f77 2086 if (!IS_ABSOLUTE_RELOC (r_type))
65f38f15
AM
2087 p->pc_count += 1;
2088 }
2089 else
2090 {
2091 /* Track dynamic relocs needed for local syms too. */
2092 elf_section_data (sec)->local_dynrel += 1;
2093 }
2094 }
5bd4f169 2095 break;
65f38f15
AM
2096
2097 default:
5bd4f169
AM
2098 }
2099 }
2100
2101 return true;
2102}
2103
2104/* Return the section that should be marked against GC for a given
2105 relocation. */
2106
2107static asection *
2108ppc64_elf_gc_mark_hook (abfd, info, rel, h, sym)
2109 bfd *abfd;
2110 struct bfd_link_info *info ATTRIBUTE_UNUSED;
2111 Elf_Internal_Rela *rel;
2112 struct elf_link_hash_entry *h;
2113 Elf_Internal_Sym *sym;
2114{
2115 if (h != NULL)
2116 {
a33d1f77
AM
2117 enum elf_ppc_reloc_type r_type;
2118
2119 r_type = (enum elf_ppc_reloc_type) ELF64_R_TYPE (rel->r_info);
2120 switch (r_type)
5bd4f169
AM
2121 {
2122 case R_PPC64_GNU_VTINHERIT:
2123 case R_PPC64_GNU_VTENTRY:
2124 break;
2125
2126 default:
2127 switch (h->root.type)
2128 {
2129 case bfd_link_hash_defined:
2130 case bfd_link_hash_defweak:
2131 return h->root.u.def.section;
2132
2133 case bfd_link_hash_common:
2134 return h->root.u.c.p->section;
2135
2136 default:
2137 break;
2138 }
2139 }
2140 }
2141 else
2142 {
2143 if (! (elf_bad_symtab (abfd)
2144 && ELF_ST_BIND (sym->st_info) != STB_LOCAL)
2145 && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE)
2146 && sym->st_shndx != SHN_COMMON))
2147 {
2148 return bfd_section_from_elf_index (abfd, sym->st_shndx);
2149 }
2150 }
2151
2152 return NULL;
2153}
2154
65f38f15
AM
2155/* Update the .got, .plt. and dynamic reloc reference counts for the
2156 section being removed. */
5bd4f169
AM
2157
2158static boolean
2159ppc64_elf_gc_sweep_hook (abfd, info, sec, relocs)
2160 bfd *abfd;
2161 struct bfd_link_info *info ATTRIBUTE_UNUSED;
2162 asection *sec;
2163 const Elf_Internal_Rela *relocs;
2164{
2165 Elf_Internal_Shdr *symtab_hdr;
2166 struct elf_link_hash_entry **sym_hashes;
2167 bfd_signed_vma *local_got_refcounts;
2168 const Elf_Internal_Rela *rel, *relend;
5bd4f169
AM
2169
2170 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2171 sym_hashes = elf_sym_hashes (abfd);
2172 local_got_refcounts = elf_local_got_refcounts (abfd);
2173
2174 relend = relocs + sec->reloc_count;
2175 for (rel = relocs; rel < relend; rel++)
a33d1f77
AM
2176 {
2177 unsigned long r_symndx;
2178 enum elf_ppc_reloc_type r_type;
2179 struct elf_link_hash_entry *h;
5bd4f169 2180
a33d1f77
AM
2181 r_symndx = ELF64_R_SYM (rel->r_info);
2182 r_type = (enum elf_ppc_reloc_type) ELF64_R_TYPE (rel->r_info);
2183 switch (r_type)
2184 {
2185 case R_PPC64_GOT16:
2186 case R_PPC64_GOT16_DS:
2187 case R_PPC64_GOT16_HA:
2188 case R_PPC64_GOT16_HI:
2189 case R_PPC64_GOT16_LO:
2190 case R_PPC64_GOT16_LO_DS:
2191 if (r_symndx >= symtab_hdr->sh_info)
2192 {
2193 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2194 if (h->got.refcount > 0)
2195 h->got.refcount--;
2196 }
2197 else
2198 {
2199 if (local_got_refcounts[r_symndx] > 0)
2200 local_got_refcounts[r_symndx]--;
2201 }
2202 break;
65f38f15 2203
a33d1f77
AM
2204 case R_PPC64_PLT16_HA:
2205 case R_PPC64_PLT16_HI:
2206 case R_PPC64_PLT16_LO:
2207 case R_PPC64_PLT32:
2208 case R_PPC64_PLT64:
2209 if (r_symndx >= symtab_hdr->sh_info)
2210 {
2211 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2212 if (h->plt.refcount > 0)
2213 h->plt.refcount--;
2214 }
2215 break;
65f38f15 2216
a33d1f77
AM
2217 case R_PPC64_REL14:
2218 case R_PPC64_REL14_BRNTAKEN:
2219 case R_PPC64_REL14_BRTAKEN:
2220 case R_PPC64_REL24:
2221 case R_PPC64_REL32:
2222 case R_PPC64_REL64:
2223 if (r_symndx >= symtab_hdr->sh_info)
2224 {
2225 struct ppc_link_hash_entry *eh;
2226 struct ppc_dyn_relocs **pp;
2227 struct ppc_dyn_relocs *p;
65f38f15 2228
a33d1f77
AM
2229 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2230 eh = (struct ppc_link_hash_entry *) h;
65f38f15 2231
a33d1f77
AM
2232 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
2233 if (p->sec == sec)
2234 {
2235 p->pc_count -= 1;
2236 p->count -= 1;
2237 if (p->count == 0)
2238 *pp = p->next;
2239 break;
2240 }
2241 }
2242 break;
65f38f15 2243
a33d1f77
AM
2244 case R_PPC64_ADDR14:
2245 case R_PPC64_ADDR14_BRNTAKEN:
2246 case R_PPC64_ADDR14_BRTAKEN:
2247 case R_PPC64_ADDR16:
2248 case R_PPC64_ADDR16_DS:
2249 case R_PPC64_ADDR16_HA:
2250 case R_PPC64_ADDR16_HI:
2251 case R_PPC64_ADDR16_HIGHER:
2252 case R_PPC64_ADDR16_HIGHERA:
2253 case R_PPC64_ADDR16_HIGHEST:
2254 case R_PPC64_ADDR16_HIGHESTA:
2255 case R_PPC64_ADDR16_LO:
2256 case R_PPC64_ADDR16_LO_DS:
2257 case R_PPC64_ADDR24:
2258 case R_PPC64_ADDR30:
2259 case R_PPC64_ADDR32:
2260 case R_PPC64_ADDR64:
2261 case R_PPC64_UADDR16:
2262 case R_PPC64_UADDR32:
2263 case R_PPC64_UADDR64:
2264 case R_PPC64_TOC:
2265 if (r_symndx >= symtab_hdr->sh_info)
2266 {
2267 struct ppc_link_hash_entry *eh;
2268 struct ppc_dyn_relocs **pp;
2269 struct ppc_dyn_relocs *p;
65f38f15 2270
a33d1f77
AM
2271 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2272 eh = (struct ppc_link_hash_entry *) h;
2273
2274 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
2275 if (p->sec == sec)
2276 {
2277 p->count -= 1;
2278 if (p->count == 0)
2279 *pp = p->next;
2280 break;
2281 }
2282 }
2283 break;
5bd4f169 2284
a33d1f77
AM
2285 default:
2286 break;
2287 }
2288 }
5bd4f169
AM
2289 return true;
2290}
2291
2292/* Adjust a symbol defined by a dynamic object and referenced by a
2293 regular object. The current definition is in some section of the
2294 dynamic object, but we're not including those sections. We have to
2295 change the definition to something the rest of the link can
2296 understand. */
2297
2298static boolean
2299ppc64_elf_adjust_dynamic_symbol (info, h)
2300 struct bfd_link_info *info;
2301 struct elf_link_hash_entry *h;
2302{
65f38f15
AM
2303 struct ppc_link_hash_table *htab;
2304 struct ppc_link_hash_entry * eh;
2305 struct ppc_dyn_relocs *p;
5bd4f169
AM
2306 asection *s;
2307 unsigned int power_of_two;
5bd4f169 2308
65f38f15 2309 htab = ppc_hash_table (info);
5bd4f169 2310
65f38f15
AM
2311 /* If this is a function, put it in the procedure linkage table. We
2312 will fill in the contents of the procedure linkage table later. */
5bd4f169
AM
2313 if (h->type == STT_FUNC
2314 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
2315 {
65f38f15
AM
2316 struct elf_link_hash_entry *fdh;
2317
2318 /* If it's a function entry point, the name starts with a dot
2319 unless someone has written some poor assembly code. The ABI
2320 for .plt calls requires that there be a function descriptor
2321 sym which has the name of the function minus the dot. */
2322
2323 if (h->plt.refcount <= 0
2324 || h->root.root.string[0] != '.'
2325 || h->root.root.string[1] == '\0'
2326 || (! info->shared
2327 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
2328 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0))
5bd4f169 2329 {
65f38f15
AM
2330 /* This case can occur if we saw a PLT reloc in an input
2331 file, but the symbol was never referred to by a dynamic
2332 object, or if all references were garbage collected. In
2333 such a case, we don't actually need to build a procedure
2334 linkage table entry. */
5bd4f169
AM
2335 h->plt.offset = (bfd_vma) -1;
2336 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
2337 return true;
2338 }
2339
65f38f15
AM
2340 /* Find the corresponding function descriptor symbol. Create it
2341 as undefined if necessary. ppc_elf64_finish_dynamic_symbol
2342 will look it up again and create a JMP_SLOT reloc for it. */
5bd4f169 2343
65f38f15
AM
2344 fdh = elf_link_hash_lookup (elf_hash_table (info),
2345 h->root.root.string + 1,
2346 false, false, false);
5bd4f169 2347
65f38f15 2348 if (fdh == NULL)
5bd4f169 2349 {
65f38f15
AM
2350 asymbol *newsym;
2351
2352 /* Create it as undefined. */
2353 newsym = bfd_make_empty_symbol (htab->elf.dynobj);
2354 newsym->name = h->root.root.string + 1;
2355 newsym->section = bfd_und_section_ptr;
2356 newsym->value = 0;
2357 newsym->flags = BSF_DYNAMIC | BSF_OBJECT;
2358
2359 if ( !(_bfd_generic_link_add_one_symbol
2360 (info, htab->elf.dynobj, newsym->name, newsym->flags,
2361 newsym->section, newsym->value, NULL, false, false,
2362 (struct bfd_link_hash_entry **) &fdh)))
5bd4f169 2363 {
65f38f15 2364 return false;
5bd4f169 2365 }
5bd4f169 2366 }
5bd4f169
AM
2367 return true;
2368 }
2369
2370 /* If this is a weak symbol, and there is a real definition, the
2371 processor independent code will have arranged for us to see the
2372 real definition first, and we can just use the same value. */
2373 if (h->weakdef != NULL)
2374 {
2375 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
2376 || h->weakdef->root.type == bfd_link_hash_defweak);
2377 h->root.u.def.section = h->weakdef->root.u.def.section;
2378 h->root.u.def.value = h->weakdef->root.u.def.value;
5bd4f169
AM
2379 }
2380
2381 /* This is a reference to a symbol defined by a dynamic object which
2382 is not a function. */
2383
2384 /* If we are creating a shared library, we must presume that the
2385 only references to the symbol are via the global offset table.
2386 For such cases we need not do anything here; the relocations will
2387 be handled correctly by relocate_section. */
2388 if (info->shared)
2389 return true;
2390
65f38f15
AM
2391 /* If there are no references to this symbol that do not use the
2392 GOT, we don't need to generate a copy reloc. */
2393 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
2394 return true;
2395
2396 eh = (struct ppc_link_hash_entry *) h;
2397 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2398 {
2399 s = p->sec->output_section;
2400 if (s != NULL && (s->flags & SEC_READONLY) != 0)
2401 break;
2402 }
2403
2404 /* If we didn't find any dynamic relocs in read-only sections, then
2405 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
2406 if (p == NULL)
2407 {
2408 h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
2409 return true;
2410 }
2411
5bd4f169
AM
2412 /* We must allocate the symbol in our .dynbss section, which will
2413 become part of the .bss section of the executable. There will be
2414 an entry for this symbol in the .dynsym section. The dynamic
2415 object will contain position independent code, so all references
2416 from the dynamic object to this symbol will go through the global
2417 offset table. The dynamic linker will use the .dynsym entry to
2418 determine the address it must put in the global offset table, so
2419 both the dynamic object and the regular object will refer to the
2420 same memory location for the variable. */
5bd4f169
AM
2421
2422 /* We must generate a R_PPC_COPY reloc to tell the dynamic linker to
2423 copy the initial value out of the dynamic object and into the
2424 runtime process image. We need to remember the offset into the
2425 .rela.bss section we are going to use. */
2426 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
2427 {
65f38f15 2428 htab->srelbss->_raw_size += sizeof (Elf64_External_Rela);
5bd4f169
AM
2429 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
2430 }
2431
2432 /* We need to figure out the alignment required for this symbol. I
2433 have no idea how ELF linkers handle this. */
2434 power_of_two = bfd_log2 (h->size);
2435 if (power_of_two > 4)
2436 power_of_two = 4;
2437
2438 /* Apply the required alignment. */
65f38f15
AM
2439 s = htab->sdynbss;
2440 s->_raw_size = BFD_ALIGN (s->_raw_size, (bfd_size_type) (1 << power_of_two));
2441 if (power_of_two > bfd_get_section_alignment (htab->elf.dynobj, s))
5bd4f169 2442 {
65f38f15 2443 if (! bfd_set_section_alignment (htab->elf.dynobj, s, power_of_two))
5bd4f169
AM
2444 return false;
2445 }
2446
2447 /* Define the symbol as being at this point in the section. */
2448 h->root.u.def.section = s;
2449 h->root.u.def.value = s->_raw_size;
2450
2451 /* Increment the section size to make room for the symbol. */
2452 s->_raw_size += h->size;
2453
2454 return true;
2455}
2456
65f38f15
AM
2457/* This is the condition under which ppc64_elf_finish_dynamic_symbol
2458 will be called from elflink.h. If elflink.h doesn't call our
2459 finish_dynamic_symbol routine, we'll need to do something about
2460 initializing any .plt and .got entries in ppc64_elf_relocate_section. */
2461#define WILL_CALL_FINISH_DYNAMIC_SYMBOL(DYN, INFO, H) \
2462 ((DYN) \
2463 && ((INFO)->shared \
2464 || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0) \
2465 && ((H)->dynindx != -1 \
2466 || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0))
5bd4f169 2467
65f38f15
AM
2468/* Allocate space in .plt, .got and associated reloc sections for
2469 dynamic relocs. */
5bd4f169
AM
2470
2471static boolean
65f38f15
AM
2472allocate_dynrelocs (h, inf)
2473 struct elf_link_hash_entry *h;
2474 PTR inf;
5bd4f169 2475{
65f38f15
AM
2476 struct bfd_link_info *info;
2477 struct ppc_link_hash_table *htab;
5bd4f169 2478 asection *s;
65f38f15
AM
2479 struct ppc_link_hash_entry *eh;
2480 struct ppc_dyn_relocs *p;
5bd4f169 2481
65f38f15
AM
2482 if (h->root.type == bfd_link_hash_indirect
2483 || h->root.type == bfd_link_hash_warning)
2484 return true;
5bd4f169 2485
65f38f15
AM
2486 info = (struct bfd_link_info *) inf;
2487 htab = ppc_hash_table (info);
5bd4f169 2488
65f38f15
AM
2489 if (htab->elf.dynamic_sections_created
2490 && h->plt.refcount > 0)
5bd4f169 2491 {
65f38f15
AM
2492 /* Make sure this symbol is output as a dynamic symbol.
2493 Undefined weak syms won't yet be marked as dynamic. */
2494 if (h->dynindx == -1
2495 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
5bd4f169 2496 {
65f38f15
AM
2497 if (! bfd_elf64_link_record_dynamic_symbol (info, h))
2498 return false;
5bd4f169 2499 }
5bd4f169 2500
65f38f15
AM
2501 BFD_ASSERT (h->root.root.string[0] == '.'
2502 && h->root.root.string[1] != '\0');
5bd4f169 2503
65f38f15
AM
2504 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info, h))
2505 {
2506 /* Make sure the corresponding function descriptor symbol is
2507 dynamic too. */
2508
2509 if (h->dynindx != -1)
2510 {
2511 struct elf_link_hash_entry *fdh;
5bd4f169 2512
65f38f15
AM
2513 fdh = elf_link_hash_lookup (elf_hash_table (info),
2514 h->root.root.string + 1,
2515 false, false, false);
5bd4f169 2516
65f38f15
AM
2517 if (fdh == NULL)
2518 abort ();
5bd4f169 2519
65f38f15
AM
2520 if (fdh->dynindx == -1
2521 && (fdh->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
2522 {
2523 if (! bfd_elf64_link_record_dynamic_symbol (info, fdh))
2524 return false;
2525 }
2526 }
2527
2528 /* If this is the first .plt entry, make room for the special
2529 first entry. */
2530 s = htab->splt;
5bd4f169 2531 if (s->_raw_size == 0)
65f38f15
AM
2532 s->_raw_size += PLT_INITIAL_ENTRY_SIZE;
2533
2534 h->plt.offset = s->_raw_size;
2535
2536 /* Make room for this entry. */
2537 s->_raw_size += PLT_ENTRY_SIZE;
2538
2539 /* Point the function at the linkage stub. This works because
2540 the only references to the function code sym are calls.
2541 Function pointer comparisons use the function descriptor. */
2542 s = htab->sglink;
2543 h->root.type = bfd_link_hash_defined;
2544 h->root.u.def.section = s;
2545 h->root.u.def.value = s->_raw_size;
2546 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
2547
2548 /* Make room for global linkage code in .glink. */
2549 s->_raw_size += PPC64_ELF_GLINK_SIZE;
2550
2551 /* We also need to make an entry in the .rela.plt section. */
2552 s = htab->srelplt;
2553 s->_raw_size += sizeof (Elf64_External_Rela);
2554 }
2555 else
2556 {
2557 h->plt.offset = (bfd_vma) -1;
2558 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
2559 }
2560 }
2561 else
2562 {
2563 h->plt.offset = (bfd_vma) -1;
2564 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
2565 }
2566
2567 if (h->got.refcount > 0)
2568 {
2569 boolean dyn;
2570
2571 /* Make sure this symbol is output as a dynamic symbol.
2572 Undefined weak syms won't yet be marked as dynamic. */
2573 if (h->dynindx == -1
2574 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
2575 {
2576 if (! bfd_elf64_link_record_dynamic_symbol (info, h))
2577 return false;
2578 }
2579
2580 s = htab->sgot;
2581 h->got.offset = s->_raw_size;
2582 s->_raw_size += 8;
2583 dyn = htab->elf.dynamic_sections_created;
2584 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info, h))
2585 htab->srelgot->_raw_size += sizeof (Elf64_External_Rela);
2586 }
2587 else
2588 h->got.offset = (bfd_vma) -1;
2589
2590 eh = (struct ppc_link_hash_entry *) h;
2591 if (eh->dyn_relocs == NULL)
2592 return true;
2593
2594 /* In the shared -Bsymbolic case, discard space allocated for
2595 dynamic pc-relative relocs against symbols which turn out to be
2596 defined in regular objects. For the normal shared case, discard
2597 space for relocs that have become local due to symbol visibility
2598 changes. */
2599
2600 if (info->shared)
2601 {
2602 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
2603 && ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0
2604 || info->symbolic))
2605 {
2606 struct ppc_dyn_relocs **pp;
2607
2608 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
5bd4f169 2609 {
65f38f15
AM
2610 p->count -= p->pc_count;
2611 p->pc_count = 0;
2612 if (p->count == 0)
2613 *pp = p->next;
2614 else
2615 pp = &p->next;
5bd4f169 2616 }
65f38f15
AM
2617 }
2618 }
2619 else
2620 {
2621 /* For the non-shared case, discard space for relocs against
2622 symbols which turn out to need copy relocs or are not
2623 dynamic. */
2624
2625 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
2626 && (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2627 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2628 || (htab->elf.dynamic_sections_created
2629 && (h->root.type == bfd_link_hash_undefweak
2630 || h->root.type == bfd_link_hash_undefined))))
2631 {
2632 /* Make sure this symbol is output as a dynamic symbol.
2633 Undefined weak syms won't yet be marked as dynamic. */
2634 if (h->dynindx == -1
2635 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
2636 {
2637 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
2638 return false;
2639 }
2640
2641 /* If that succeeded, we know we'll be keeping all the
2642 relocs. */
2643 if (h->dynindx != -1)
2644 goto keep;
2645 }
2646
2647 eh->dyn_relocs = NULL;
2648
2649 keep:
2650 }
2651
2652 /* Finally, allocate space. */
2653 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2654 {
2655 asection *sreloc = elf_section_data (p->sec)->sreloc;
2656 sreloc->_raw_size += p->count * sizeof (Elf64_External_Rela);
2657 }
2658
2659 return true;
2660}
2661
2662/* Find any dynamic relocs that apply to read-only sections. */
2663
2664static boolean
2665readonly_dynrelocs (h, inf)
2666 struct elf_link_hash_entry *h;
2667 PTR inf;
2668{
2669 struct ppc_link_hash_entry *eh;
2670 struct ppc_dyn_relocs *p;
2671
2672 eh = (struct ppc_link_hash_entry *) h;
2673 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2674 {
2675 asection *s = p->sec->output_section;
2676
2677 if (s != NULL && (s->flags & SEC_READONLY) != 0)
2678 {
2679 struct bfd_link_info *info = (struct bfd_link_info *) inf;
2680
2681 info->flags |= DF_TEXTREL;
2682
2683 /* Not an error, just cut short the traversal. */
2684 return false;
2685 }
2686 }
2687 return true;
2688}
2689
2690/* Set the sizes of the dynamic sections. */
2691
2692static boolean
2693ppc64_elf_size_dynamic_sections (output_bfd, info)
2694 bfd *output_bfd ATTRIBUTE_UNUSED;
2695 struct bfd_link_info *info;
2696{
2697 struct ppc_link_hash_table *htab;
2698 bfd *dynobj;
2699 asection *s;
2700 boolean relocs;
2701 bfd *ibfd;
2702
2703 htab = ppc_hash_table (info);
2704 dynobj = htab->elf.dynobj;
2705 if (dynobj == NULL)
2706 abort ();
2707
2708 if (htab->elf.dynamic_sections_created)
2709 {
2710 /* Set the contents of the .interp section to the interpreter. */
2711 if (! info->shared)
2712 {
2713 s = bfd_get_section_by_name (dynobj, ".interp");
2714 if (s == NULL)
2715 abort ();
2716 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
2717 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2718 }
2719 }
2720
2721 /* Set up .got offsets for local syms, and space for local dynamic
2722 relocs. */
2723 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
2724 {
2725 bfd_signed_vma *local_got;
2726 bfd_signed_vma *end_local_got;
2727 bfd_size_type locsymcount;
2728 Elf_Internal_Shdr *symtab_hdr;
2729 asection *srel;
2730
2731 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
2732 continue;
2733
2734 for (s = ibfd->sections; s != NULL; s = s->next)
2735 {
2736 bfd_size_type count = elf_section_data (s)->local_dynrel;
2737
2738 if (count != 0)
2739 {
2740 srel = elf_section_data (s)->sreloc;
2741 srel->_raw_size += count * sizeof (Elf64_External_Rela);
2742 }
2743 }
2744
2745 local_got = elf_local_got_refcounts (ibfd);
2746 if (!local_got)
2747 continue;
2748
2749 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
2750 locsymcount = symtab_hdr->sh_info;
2751 end_local_got = local_got + locsymcount;
2752 s = htab->sgot;
2753 srel = htab->srelgot;
2754 for (; local_got < end_local_got; ++local_got)
2755 {
2756 if (*local_got > 0)
5bd4f169 2757 {
65f38f15
AM
2758 *local_got = s->_raw_size;
2759 s->_raw_size += 8;
2760 if (info->shared)
2761 srel->_raw_size += sizeof (Elf64_External_Rela);
5bd4f169 2762 }
65f38f15
AM
2763 else
2764 *local_got = (bfd_vma) -1;
2765 }
2766 }
2767
2768 /* Allocate global sym .plt and .got entries, and space for global
2769 sym dynamic relocs. */
2770 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, (PTR) info);
2771
2772 /* We now have determined the sizes of the various dynamic sections.
2773 Allocate memory for them. */
2774 relocs = false;
2775 for (s = dynobj->sections; s != NULL; s = s->next)
2776 {
2777 if ((s->flags & SEC_LINKER_CREATED) == 0)
2778 continue;
2779
2780 if (s == htab->splt
2781 || s == htab->sgot
2782 || s == htab->sglink)
2783 {
2784 /* Strip this section if we don't need it; see the
2785 comment below. */
5bd4f169 2786 }
65f38f15 2787 else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
5bd4f169
AM
2788 {
2789 if (s->_raw_size == 0)
2790 {
2791 /* If we don't need this section, strip it from the
2792 output file. This is mostly to handle .rela.bss and
2793 .rela.plt. We must create both sections in
2794 create_dynamic_sections, because they must be created
2795 before the linker maps input sections to output
2796 sections. The linker does that before
2797 adjust_dynamic_symbol is called, and it is that
2798 function which decides whether anything needs to go
2799 into these sections. */
5bd4f169
AM
2800 }
2801 else
2802 {
65f38f15
AM
2803 if (s != htab->srelplt)
2804 relocs = true;
5bd4f169
AM
2805
2806 /* We use the reloc_count field as a counter if we need
2807 to copy relocs into the output file. */
2808 s->reloc_count = 0;
2809 }
2810 }
65f38f15 2811 else
5bd4f169
AM
2812 {
2813 /* It's not one of our sections, so don't allocate space. */
2814 continue;
2815 }
2816
65f38f15 2817 if (s->_raw_size == 0)
5bd4f169
AM
2818 {
2819 _bfd_strip_section_from_output (info, s);
2820 continue;
2821 }
2822
65f38f15
AM
2823 /* Allocate memory for the section contents. We use bfd_zalloc
2824 here in case unused entries are not reclaimed before the
2825 section's contents are written out. This should not happen,
2826 but this way if it does, we get a R_PPC64_NONE reloc instead
2827 of garbage. */
5bd4f169 2828 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
65f38f15 2829 if (s->contents == NULL)
5bd4f169
AM
2830 return false;
2831 }
2832
2833 if (elf_hash_table (info)->dynamic_sections_created)
2834 {
2835 /* Add some entries to the .dynamic section. We fill in the
2836 values later, in ppc64_elf_finish_dynamic_sections, but we
2837 must add the entries now so that we get the correct size for
2838 the .dynamic section. The DT_DEBUG entry is filled in by the
2839 dynamic linker and used by the debugger. */
dc810e39
AM
2840#define add_dynamic_entry(TAG, VAL) \
2841 bfd_elf64_add_dynamic_entry (info, (bfd_vma) (TAG), (bfd_vma) (VAL))
2842
2843 if (!info->shared)
5bd4f169 2844 {
dc810e39 2845 if (!add_dynamic_entry (DT_DEBUG, 0))
5bd4f169
AM
2846 return false;
2847 }
2848
65f38f15 2849 if (htab->splt->_raw_size != 0)
5bd4f169 2850 {
dc810e39
AM
2851 if (!add_dynamic_entry (DT_PLTGOT, 0)
2852 || !add_dynamic_entry (DT_PLTRELSZ, 0)
2853 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
2854 || !add_dynamic_entry (DT_JMPREL, 0))
5bd4f169
AM
2855 return false;
2856 }
2857
2858 if (relocs)
2859 {
dc810e39
AM
2860 if (!add_dynamic_entry (DT_RELA, 0)
2861 || !add_dynamic_entry (DT_RELASZ, 0)
2862 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
5bd4f169 2863 return false;
5bd4f169 2864
65f38f15
AM
2865 /* If any dynamic relocs apply to a read-only section,
2866 then we need a DT_TEXTREL entry. */
2867 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs, (PTR) info);
5bd4f169 2868
65f38f15 2869 if ((info->flags & DF_TEXTREL) != 0)
5bd4f169 2870 {
65f38f15
AM
2871 if (!add_dynamic_entry (DT_TEXTREL, 0))
2872 return false;
5bd4f169 2873 }
5bd4f169 2874 }
5bd4f169 2875 }
65f38f15 2876#undef add_dynamic_entry
5bd4f169
AM
2877
2878 return true;
2879}
2880
2881/* Called after we have seen all the input files/sections, but before
2882 final symbol resolution and section placement has been determined.
2883
2884 We use this hook to provide a value for TOCstart, which we store in
2885 the output bfd elf_gp, then we call the generic ELF final link
2886 routine. */
2887
2888static boolean
2889ppc64_elf_final_link (abfd, info)
2890 bfd *abfd;
2891 struct bfd_link_info *info;
2892{
2893 if (! info->relocateable)
2894 {
2895 asection *s;
2896 bfd_vma TOCstart;
2897
2898 /* The TOC consists of sections .got, .toc, .tocbss, .plt in this
2899 order. The TOC starts where the first of these sections starts. */
2900 s = bfd_get_section_by_name (abfd, ".got");
2901 if (s == NULL)
2902 s = bfd_get_section_by_name (abfd, ".toc");
2903 if (s == NULL)
2904 s = bfd_get_section_by_name (abfd, ".tocbss");
2905 if (s == NULL)
2906 s = bfd_get_section_by_name (abfd, ".plt");
2907 if (s == NULL)
2908 {
2909 /* This may happen for
2910 o references to TOC base (SYM@toc / TOC[tc0]) without a
2911 .toc directive
2912 o bad linker script
2913 o --gc-sections and empty TOC sections
2914
2915 FIXME: Warn user? */
2916
2917 /* Look for a likely section. We probably won't even be
2918 using TOCstart. */
2919 for (s = abfd->sections; s != NULL; s = s->next)
2920 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY))
2921 == (SEC_ALLOC | SEC_SMALL_DATA))
2922 break;
2923 if (s == NULL)
2924 for (s = abfd->sections; s != NULL; s = s->next)
2925 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA))
2926 == (SEC_ALLOC | SEC_SMALL_DATA))
2927 break;
2928 if (s == NULL)
2929 for (s = abfd->sections; s != NULL; s = s->next)
2930 if ((s->flags & (SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
2931 break;
2932 if (s == NULL)
2933 for (s = abfd->sections; s != NULL; s = s->next)
2934 if ((s->flags & SEC_ALLOC) == SEC_ALLOC)
2935 break;
2936 }
2937
2938 TOCstart = 0;
2939 if (s != NULL)
2940 TOCstart = s->output_section->vma + s->output_offset;
2941
2942 elf_gp (abfd) = TOCstart;
2943 }
2944
65f38f15
AM
2945 /* Invoke the regular ELF backend linker to do all the work. */
2946 return bfd_elf64_bfd_final_link (abfd, info);
5bd4f169
AM
2947}
2948
2949/* Set up any other section flags and such that may be necessary. */
2950
2951static boolean
2952ppc64_elf_fake_sections (abfd, shdr, asect)
2953 bfd *abfd ATTRIBUTE_UNUSED;
2954 Elf64_Internal_Shdr *shdr;
2955 asection *asect;
2956{
2957 if ((asect->flags & SEC_EXCLUDE) != 0)
2958 shdr->sh_flags |= SHF_EXCLUDE;
2959
2960 if ((asect->flags & SEC_SORT_ENTRIES) != 0)
2961 shdr->sh_type = SHT_ORDERED;
2962
2963 return true;
2964}
2965
2966/* The RELOCATE_SECTION function is called by the ELF backend linker
2967 to handle the relocations for a section.
2968
2969 The relocs are always passed as Rela structures; if the section
2970 actually uses Rel structures, the r_addend field will always be
2971 zero.
2972
2973 This function is responsible for adjust the section contents as
2974 necessary, and (if using Rela relocs and generating a
2975 relocateable output file) adjusting the reloc addend as
2976 necessary.
2977
2978 This function does not have to worry about setting the reloc
2979 address or the reloc symbol index.
2980
2981 LOCAL_SYMS is a pointer to the swapped in local symbols.
2982
2983 LOCAL_SECTIONS is an array giving the section in the input file
2984 corresponding to the st_shndx field of each local symbol.
2985
2986 The global hash table entry for the global symbols can be found
2987 via elf_sym_hashes (input_bfd).
2988
2989 When generating relocateable output, this function must handle
2990 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
2991 going to be the section symbol corresponding to the output
2992 section, which means that the addend must be adjusted
2993 accordingly. */
2994
2995static boolean
2996ppc64_elf_relocate_section (output_bfd, info, input_bfd, input_section,
2997 contents, relocs, local_syms, local_sections)
2998 bfd *output_bfd;
2999 struct bfd_link_info *info;
3000 bfd *input_bfd;
3001 asection *input_section;
3002 bfd_byte *contents;
3003 Elf_Internal_Rela *relocs;
3004 Elf_Internal_Sym *local_syms;
3005 asection **local_sections;
3006{
65f38f15 3007 struct ppc_link_hash_table *htab;
5bd4f169
AM
3008 Elf_Internal_Shdr *symtab_hdr;
3009 struct elf_link_hash_entry **sym_hashes;
5bd4f169
AM
3010 Elf_Internal_Rela *rel;
3011 Elf_Internal_Rela *relend;
5bd4f169
AM
3012 bfd_vma *local_got_offsets;
3013 bfd_vma TOCstart;
3014 boolean ret = true;
5bd4f169 3015
65f38f15 3016 /* Initialize howto table if needed. */
5bd4f169 3017 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
3018 ppc_howto_init ();
3019
65f38f15 3020 htab = ppc_hash_table (info);
5bd4f169 3021 local_got_offsets = elf_local_got_offsets (input_bfd);
5bd4f169 3022 TOCstart = elf_gp (output_bfd);
5bd4f169
AM
3023 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3024 sym_hashes = elf_sym_hashes (input_bfd);
65f38f15 3025
5bd4f169
AM
3026 rel = relocs;
3027 relend = relocs + input_section->reloc_count;
3028 for (; rel < relend; rel++)
3029 {
3030 enum elf_ppc_reloc_type r_type;
3031 bfd_vma offset;
3032 bfd_vma addend;
3033 bfd_reloc_status_type r;
3034 Elf_Internal_Sym *sym;
3035 asection *sec;
3036 struct elf_link_hash_entry *h;
3037 const char *sym_name;
5bd4f169
AM
3038 unsigned long r_symndx;
3039 bfd_vma relocation;
65f38f15 3040 boolean unresolved_reloc;
dc810e39 3041 long insn;
5bd4f169
AM
3042
3043 r_type = (enum elf_ppc_reloc_type) ELF64_R_TYPE (rel->r_info);
5bd4f169
AM
3044 r_symndx = ELF64_R_SYM (rel->r_info);
3045
3046 if (info->relocateable)
3047 {
3048 /* This is a relocatable link. We don't have to change
3049 anything, unless the reloc is against a section symbol,
3050 in which case we have to adjust according to where the
3051 section symbol winds up in the output section. */
3052 if (r_symndx < symtab_hdr->sh_info)
3053 {
3054 sym = local_syms + r_symndx;
3055 if ((unsigned) ELF_ST_TYPE (sym->st_info) == STT_SECTION)
3056 {
3057 sec = local_sections[r_symndx];
65f38f15 3058 rel->r_addend += sec->output_offset + sym->st_value;
5bd4f169
AM
3059 }
3060 }
5bd4f169
AM
3061 continue;
3062 }
3063
3064 /* This is a final link. */
3065
65f38f15
AM
3066 offset = rel->r_offset;
3067 addend = rel->r_addend;
3068 r = bfd_reloc_other;
3069 sym = (Elf_Internal_Sym *) 0;
3070 sec = (asection *) 0;
3071 h = (struct elf_link_hash_entry *) 0;
3072 sym_name = (const char *) 0;
3073 unresolved_reloc = false;
3074
5bd4f169
AM
3075 if (r_type == R_PPC64_TOC)
3076 {
3077 /* Relocation value is TOC base. Symbol is ignored. */
3078 relocation = TOCstart + TOC_BASE_OFF;
3079 }
3080 else if (r_symndx < symtab_hdr->sh_info)
3081 {
3082 /* It's a local symbol. */
3083 sym = local_syms + r_symndx;
3084 sec = local_sections[r_symndx];
3085 sym_name = "<local symbol>";
3086
3087 relocation = (sec->output_section->vma
3088 + sec->output_offset
3089 + sym->st_value);
3090 }
3091 else
3092 {
3093 /* It's a global symbol. */
3094 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3095 while (h->root.type == bfd_link_hash_indirect
3096 || h->root.type == bfd_link_hash_warning)
3097 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3098 sym_name = h->root.root.string;
65f38f15 3099 relocation = 0;
5bd4f169
AM
3100 if (h->root.type == bfd_link_hash_defined
3101 || h->root.type == bfd_link_hash_defweak)
3102 {
3103 sec = h->root.u.def.section;
65f38f15
AM
3104 if (sec->output_section == NULL)
3105 /* Set a flag that will be cleared later if we find a
3106 relocation value for this symbol. output_section
3107 is typically NULL for symbols satisfied by a shared
3108 library. */
3109 unresolved_reloc = true;
5bd4f169
AM
3110 else
3111 relocation = (h->root.u.def.value
3112 + sec->output_section->vma
3113 + sec->output_offset);
3114 }
3115 else if (h->root.type == bfd_link_hash_undefweak)
65f38f15 3116 ;
671bae9c 3117 else if (info->shared
65f38f15
AM
3118 && (!info->symbolic || info->allow_shlib_undefined)
3119 && !info->no_undefined
3120 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3121 ;
5bd4f169
AM
3122 else
3123 {
3124 if (! ((*info->callbacks->undefined_symbol)
3125 (info, h->root.root.string, input_bfd, input_section,
65f38f15
AM
3126 offset, (!info->shared
3127 || info->no_undefined
3128 || ELF_ST_VISIBILITY (h->other)))))
5bd4f169
AM
3129 return false;
3130 relocation = 0;
3131 }
3132 }
3133
65f38f15
AM
3134 /* First handle relocations that tweak non-addend part of insn. */
3135 switch (r_type)
5bd4f169
AM
3136 {
3137 default:
65f38f15 3138 break;
5bd4f169 3139
65f38f15
AM
3140 /* Branch taken prediction relocations. */
3141 case R_PPC64_ADDR14_BRTAKEN:
3142 case R_PPC64_REL14_BRTAKEN:
3143 insn = bfd_get_32 (output_bfd, contents + offset);
3144 if ((relocation - offset) & 0x8000)
3145 insn &= ~BRANCH_PREDICT_BIT;
3146 else
3147 insn |= BRANCH_PREDICT_BIT;
3148 bfd_put_32 (output_bfd, (bfd_vma) insn, contents + offset);
3149 break;
3150
3151 /* Branch not taken predicition relocations. */
3152 case R_PPC64_ADDR14_BRNTAKEN:
3153 case R_PPC64_REL14_BRNTAKEN:
3154 insn = bfd_get_32 (output_bfd, contents + offset);
3155 if ((relocation - offset) & 0x8000)
3156 insn |= BRANCH_PREDICT_BIT;
3157 else
3158 insn &= ~BRANCH_PREDICT_BIT;
3159 bfd_put_32 (output_bfd, (bfd_vma) insn, contents + offset);
3160 break;
5bd4f169 3161
65f38f15
AM
3162 case R_PPC64_REL24:
3163 case R_PPC64_ADDR24:
5bd4f169 3164 /* An ADDR24 or REL24 branching to a linkage function may be
65f38f15 3165 followed by a nop that we have to replace with a ld in
5bd4f169
AM
3166 order to restore the TOC base pointer. Only calls to
3167 shared objects need to alter the TOC base. These are
3168 recognized by their need for a PLT entry. */
3169 if (h != NULL
3170 && (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0
3171 /* Make sure that there really is an instruction after
3172 the branch that we can decode. */
65f38f15 3173 && offset + 8 <= input_section->_cooked_size)
5bd4f169
AM
3174 {
3175 bfd_byte *pnext;
5bd4f169 3176
65f38f15 3177 pnext = contents + offset + 4;
5bd4f169
AM
3178 insn = bfd_get_32 (input_bfd, pnext);
3179
3180 if (insn == 0x60000000 /* nop (ori r0,r0,0) */
3181 || insn == 0x4def7b82 /* cror 15,15,15 */
dc810e39 3182 || insn == 0x4ffffb82) /* cror 31,31,31 */
5bd4f169 3183 {
dc810e39
AM
3184 bfd_put_32 (input_bfd,
3185 (bfd_vma) 0xe8410028, /* ld r2,40(r1) */
3186 pnext);
5bd4f169
AM
3187 }
3188 }
65f38f15
AM
3189 break;
3190 }
5bd4f169 3191
65f38f15
AM
3192 /* Set `addend'. */
3193 switch (r_type)
3194 {
3195 default:
3196 (*_bfd_error_handler)
3197 (_("%s: unknown relocation type %d for symbol %s"),
3198 bfd_archive_filename (input_bfd), (int) r_type, sym_name);
5bd4f169 3199
65f38f15
AM
3200 bfd_set_error (bfd_error_bad_value);
3201 ret = false;
3202 continue;
5bd4f169 3203
65f38f15
AM
3204 case R_PPC64_NONE:
3205 case R_PPC_GNU_VTINHERIT:
3206 case R_PPC_GNU_VTENTRY:
3207 continue;
5bd4f169
AM
3208
3209 /* GOT16 relocations. Like an ADDR16 using the symbol's
3210 address in the GOT as relocation value instead of the
3211 symbols value itself. Also, create a GOT entry for the
3212 symbol and put the symbol value there. */
65f38f15
AM
3213 case R_PPC64_GOT16:
3214 case R_PPC64_GOT16_LO:
3215 case R_PPC64_GOT16_HI:
3216 case R_PPC64_GOT16_HA:
3217 case R_PPC64_GOT16_DS:
3218 case R_PPC64_GOT16_LO_DS:
5bd4f169
AM
3219 {
3220 /* Relocation is to the entry for this symbol in the global
3221 offset table. */
3222 bfd_vma off;
65f38f15
AM
3223
3224 if (htab->sgot == NULL)
3225 abort ();
5bd4f169
AM
3226
3227 if (h != NULL)
3228 {
65f38f15 3229 boolean dyn;
5bd4f169 3230
65f38f15
AM
3231 off = h->got.offset;
3232 dyn = htab->elf.dynamic_sections_created;
3233 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info, h)
5bd4f169 3234 || (info->shared
65f38f15
AM
3235 && (info->symbolic
3236 || h->dynindx == -1
3237 || (h->elf_link_hash_flags
3238 & ELF_LINK_FORCED_LOCAL))
5bd4f169
AM
3239 && (h->elf_link_hash_flags
3240 & ELF_LINK_HASH_DEF_REGULAR)))
3241 {
3242 /* This is actually a static link, or it is a
3243 -Bsymbolic link and the symbol is defined
65f38f15
AM
3244 locally, or the symbol was forced to be local
3245 because of a version file. We must initialize
3246 this entry in the global offset table. Since the
3247 offset must always be a multiple of 8, we use the
3248 least significant bit to record whether we have
5bd4f169
AM
3249 initialized it already.
3250
65f38f15 3251 When doing a dynamic link, we create a .rel.got
5bd4f169
AM
3252 relocation entry to initialize the value. This
3253 is done in the finish_dynamic_symbol routine. */
3254 if ((off & 1) != 0)
3255 off &= ~1;
3256 else
3257 {
3258 bfd_put_64 (output_bfd, relocation,
65f38f15 3259 htab->sgot->contents + off);
5bd4f169
AM
3260 h->got.offset |= 1;
3261 }
3262 }
65f38f15
AM
3263 else
3264 unresolved_reloc = false;
5bd4f169
AM
3265 }
3266 else
3267 {
65f38f15
AM
3268 if (local_got_offsets == NULL)
3269 abort ();
5bd4f169
AM
3270
3271 off = local_got_offsets[r_symndx];
3272
65f38f15 3273 /* The offset must always be a multiple of 8. We use
5bd4f169
AM
3274 the least significant bit to record whether we have
3275 already processed this entry. */
3276 if ((off & 1) != 0)
3277 off &= ~1;
3278 else
3279 {
65f38f15
AM
3280 bfd_put_64 (output_bfd, relocation,
3281 htab->sgot->contents + off);
5bd4f169
AM
3282
3283 if (info->shared)
3284 {
5bd4f169 3285 Elf_Internal_Rela outrel;
65f38f15 3286 Elf64_External_Rela *loc;
5bd4f169
AM
3287
3288 /* We need to generate a R_PPC64_RELATIVE reloc
3289 for the dynamic linker. */
65f38f15
AM
3290 outrel.r_offset = (htab->sgot->output_section->vma
3291 + htab->sgot->output_offset
5bd4f169
AM
3292 + off);
3293 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
3294 outrel.r_addend = relocation;
65f38f15
AM
3295 loc = (Elf64_External_Rela *) htab->srelgot->contents;
3296 loc += htab->srelgot->reloc_count++;
3297 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
5bd4f169
AM
3298 }
3299
3300 local_got_offsets[r_symndx] |= 1;
3301 }
3302 }
3303
65f38f15
AM
3304 if (off >= (bfd_vma) -2)
3305 abort ();
3306
3307 relocation = htab->sgot->output_offset + off;
3308
5bd4f169
AM
3309 /* TOC base (r2) is TOC start plus 0x8000. */
3310 addend -= TOC_BASE_OFF;
5bd4f169 3311 }
65f38f15
AM
3312 break;
3313
3314 case R_PPC64_PLT16_HA:
3315 case R_PPC64_PLT16_HI:
3316 case R_PPC64_PLT16_LO:
3317 case R_PPC64_PLT32:
3318 case R_PPC64_PLT64:
3319 /* Relocation is to the entry for this symbol in the
3320 procedure linkage table. */
3321
3322 /* Resolve a PLT reloc against a local symbol directly,
3323 without using the procedure linkage table. */
3324 if (h == NULL)
3325 break;
3326
3327 if (h->plt.offset == (bfd_vma) -1
3328 || htab->splt == NULL)
3329 {
3330 /* We didn't make a PLT entry for this symbol. This
3331 happens when statically linking PIC code, or when
3332 using -Bsymbolic. */
3333 break;
3334 }
3335
3336 relocation = (htab->splt->output_section->vma
3337 + htab->splt->output_offset
3338 + h->plt.offset);
3339 unresolved_reloc = false;
3340 break;
5bd4f169
AM
3341
3342 /* TOC16 relocs. We want the offset relative to the TOC base,
3343 which is the address of the start of the TOC plus 0x8000.
3344 The TOC consists of sections .got, .toc, .tocbss, and .plt,
3345 in this order. */
3346
65f38f15
AM
3347 case R_PPC64_TOC16:
3348 case R_PPC64_TOC16_LO:
3349 case R_PPC64_TOC16_HI:
3350 case R_PPC64_TOC16_DS:
3351 case R_PPC64_TOC16_LO_DS:
3352 case R_PPC64_TOC16_HA:
5bd4f169 3353 /* Only .got, .toc and *UND* symbols are allowed. */
65f38f15
AM
3354 BFD_ASSERT (sec != (asection *) 0
3355 && (bfd_is_und_section (sec)
3356 || strcmp (bfd_get_section_name (abfd, sec),
3357 ".toc") == 0
3358 || strcmp (bfd_get_section_name (abfd, sec),
3359 ".got") == 0));
5bd4f169
AM
3360
3361 addend -= TOCstart + TOC_BASE_OFF;
5bd4f169
AM
3362 break;
3363
3364 /* Relocate against the beginning of the section. */
65f38f15
AM
3365 case R_PPC64_SECTOFF:
3366 case R_PPC64_SECTOFF_LO:
3367 case R_PPC64_SECTOFF_HI:
3368 case R_PPC64_SECTOFF_DS:
3369 case R_PPC64_SECTOFF_LO_DS:
3370 case R_PPC64_SECTOFF_HA:
3371 if (sec != (asection *) 0)
3372 addend -= sec->output_section->vma;
5bd4f169
AM
3373 break;
3374
65f38f15
AM
3375 /* Relocations that may need to be propagated if this is a
3376 dynamic object. */
3377 case R_PPC64_REL14:
3378 case R_PPC64_REL14_BRNTAKEN:
3379 case R_PPC64_REL14_BRTAKEN:
3380 case R_PPC64_REL24:
3381 case R_PPC64_REL32:
3382 case R_PPC64_REL64:
3383 case R_PPC64_ADDR14:
3384 case R_PPC64_ADDR14_BRNTAKEN:
3385 case R_PPC64_ADDR14_BRTAKEN:
3386 case R_PPC64_ADDR16:
3387 case R_PPC64_ADDR16_DS:
3388 case R_PPC64_ADDR16_HA:
3389 case R_PPC64_ADDR16_HI:
3390 case R_PPC64_ADDR16_HIGHER:
3391 case R_PPC64_ADDR16_HIGHERA:
3392 case R_PPC64_ADDR16_HIGHEST:
3393 case R_PPC64_ADDR16_HIGHESTA:
3394 case R_PPC64_ADDR16_LO:
3395 case R_PPC64_ADDR16_LO_DS:
3396 case R_PPC64_ADDR24:
3397 case R_PPC64_ADDR30:
3398 case R_PPC64_ADDR32:
3399 case R_PPC64_ADDR64:
3400 case R_PPC64_UADDR16:
3401 case R_PPC64_UADDR32:
3402 case R_PPC64_UADDR64:
3403 case R_PPC64_TOC:
3404 if ((info->shared
3405 && (input_section->flags & SEC_ALLOC) != 0
3406 && (IS_ABSOLUTE_RELOC (r_type)
3407 || (h != NULL
3408 && h->dynindx != -1
3409 && (! info->symbolic
3410 || (h->elf_link_hash_flags
3411 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
3412 || (!info->shared
3413 && (input_section->flags & SEC_ALLOC) != 0
3414 && h != NULL
3415 && h->dynindx != -1
3416 && (h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
3417 && (((h->elf_link_hash_flags
3418 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
3419 && (h->elf_link_hash_flags
3420 & ELF_LINK_HASH_DEF_REGULAR) == 0)
3421 || h->root.type == bfd_link_hash_undefweak
3422 || h->root.type == bfd_link_hash_undefined)))
3423 {
3424 Elf_Internal_Rela outrel;
3425 boolean skip, relocate;
3426 asection *sreloc;
3427 Elf64_External_Rela *loc;
3428
3429 /* When generating a dynamic object, these relocations
3430 are copied into the output file to be resolved at run
3431 time. */
3432
3433 skip = false;
3434
3435 if (elf_section_data (input_section)->stab_info == NULL)
3436 outrel.r_offset = offset;
3437 else
3438 {
3439 bfd_vma off;
3440
3441 off = (_bfd_stab_section_offset
3442 (output_bfd, htab->elf.stab_info, input_section,
3443 &elf_section_data (input_section)->stab_info,
3444 offset));
3445 if (off == (bfd_vma) -1)
3446 skip = true;
3447 outrel.r_offset = off;
3448 }
3449
3450 outrel.r_offset += (input_section->output_section->vma
3451 + input_section->output_offset);
3452 outrel.r_addend = addend;
3453
3454 if (skip)
3455 {
3456 relocate = false;
3457 memset (&outrel, 0, sizeof outrel);
3458 }
3459 else if (h != NULL
3460 && h->dynindx != -1
3461 && (!IS_ABSOLUTE_RELOC (r_type)
3462 || !info->shared
3463 || !info->symbolic
3464 || (h->elf_link_hash_flags
3465 & ELF_LINK_HASH_DEF_REGULAR) == 0))
3466 {
3467 relocate = false;
3468 outrel.r_info = ELF64_R_INFO (h->dynindx, r_type);
3469 }
3470 else
3471 {
3472 /* This symbol is local, or marked to become local. */
3473 outrel.r_addend += relocation;
3474 relocate = true;
3475 if (r_type == R_PPC64_ADDR64)
3476 {
3477 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
3478 }
3479 else
3480 {
3481 long indx = 0;
3482
3483 if (r_type == R_PPC64_TOC || bfd_is_abs_section (sec))
3484 ;
3485 else if (sec == NULL || sec->owner == NULL)
3486 {
3487 bfd_set_error (bfd_error_bad_value);
3488 return false;
3489 }
3490 else
3491 {
3492 asection *osec;
3493
3494 osec = sec->output_section;
3495 indx = elf_section_data (osec)->dynindx;
3496
3497 /* We are turning this relocation into one
3498 against a section symbol, so subtract out
3499 the output section's address but not the
3500 offset of the input section in the output
3501 section. */
3502 outrel.r_addend -= osec->vma;
3503 }
3504
3505 outrel.r_info = ELF64_R_INFO (indx, r_type);
3506 }
3507 }
3508
3509 sreloc = elf_section_data (input_section)->sreloc;
3510 if (sreloc == NULL)
3511 abort ();
3512
3513 loc = (Elf64_External_Rela *) sreloc->contents;
3514 loc += sreloc->reloc_count++;
3515 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
3516
3517 /* If this reloc is against an external symbol, it will
3518 be computed at runtime, so there's no need to do
3519 anything now. */
3520 if (! relocate)
3521 continue;
3522 }
5bd4f169
AM
3523 break;
3524
65f38f15
AM
3525 case R_PPC64_COPY:
3526 case R_PPC64_GLOB_DAT:
3527 case R_PPC64_JMP_SLOT:
3528 case R_PPC64_RELATIVE:
3529 /* We shouldn't ever see these dynamic relocs in relocatable
3530 files. */
3531 /* Fall thru */
3532
3533 case R_PPC64_PLTGOT16:
3534 case R_PPC64_PLTGOT16_DS:
3535 case R_PPC64_PLTGOT16_HA:
3536 case R_PPC64_PLTGOT16_HI:
3537 case R_PPC64_PLTGOT16_LO:
3538 case R_PPC64_PLTGOT16_LO_DS:
3539 case R_PPC64_PLTREL32:
3540 case R_PPC64_PLTREL64:
3541 /* These ones haven't been implemented yet. */
3542
3543 (*_bfd_error_handler)
3544 (_("%s: Relocation %s is not supported for symbol %s."),
3545 bfd_archive_filename (input_bfd),
3546 ppc64_elf_howto_table[(int) r_type]->name, sym_name);
5bd4f169
AM
3547
3548 bfd_set_error (bfd_error_invalid_operation);
3549 ret = false;
3550 continue;
65f38f15 3551 }
5bd4f169 3552
65f38f15
AM
3553 /* Do any further special processing. */
3554 switch (r_type)
3555 {
3556 default:
3557 break;
3558
3559 case R_PPC64_ADDR16_HA:
3560 case R_PPC64_ADDR16_HIGHERA:
3561 case R_PPC64_ADDR16_HIGHESTA:
3562 case R_PPC64_PLT16_HA:
3563 case R_PPC64_TOC16_HA:
3564 case R_PPC64_SECTOFF_HA:
3565 /* It's just possible that this symbol is a weak symbol
3566 that's not actually defined anywhere. In that case,
3567 'sec' would be NULL, and we should leave the symbol
3568 alone (it will be set to zero elsewhere in the link). */
3569 if (sec != NULL)
3570 /* Add 0x10000 if sign bit in 0:15 is set. */
3571 addend += ((relocation + addend) & 0x8000) << 1;
3572 break;
3573
3574 case R_PPC64_ADDR16_DS:
3575 case R_PPC64_ADDR16_LO_DS:
3576 case R_PPC64_GOT16_DS:
3577 case R_PPC64_GOT16_LO_DS:
3578 case R_PPC64_PLT16_LO_DS:
3579 case R_PPC64_SECTOFF_DS:
3580 case R_PPC64_SECTOFF_LO_DS:
3581 case R_PPC64_TOC16_DS:
3582 case R_PPC64_TOC16_LO_DS:
3583 case R_PPC64_PLTGOT16_DS:
3584 case R_PPC64_PLTGOT16_LO_DS:
3585 if (((relocation + addend) & 3) != 0)
3586 {
3587 (*_bfd_error_handler)
3588 (_("%s: error: relocation %s not a multiple of 4"),
3589 bfd_archive_filename (input_bfd),
3590 ppc64_elf_howto_table[(int) r_type]->name);
3591 bfd_set_error (bfd_error_bad_value);
3592 ret = false;
3593 continue;
3594 }
3595 break;
5bd4f169
AM
3596 }
3597
65f38f15
AM
3598 /* FIXME: Why do we allow debugging sections to escape this error?
3599 More importantly, why do we not emit dynamic relocs above in
3600 debugging sections (which are ! SEC_ALLOC)? If we had
3601 emitted the dynamic reloc, we could remove the fudge here. */
3602 if (unresolved_reloc
3603 && !(info->shared
3604 && (input_section->flags & SEC_DEBUGGING) != 0
3605 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0))
3606 (*_bfd_error_handler)
3607 (_("%s(%s+0x%lx): unresolvable relocation against symbol `%s'"),
3608 bfd_archive_filename (input_bfd),
3609 bfd_get_section_name (input_bfd, input_section),
3610 (long) rel->r_offset,
3611 h->root.root.string);
5bd4f169 3612
65f38f15 3613 r = _bfd_final_link_relocate (ppc64_elf_howto_table[(int) r_type],
5bd4f169
AM
3614 input_bfd,
3615 input_section,
3616 contents,
3617 offset,
3618 relocation,
3619 addend);
3620
3621 if (r == bfd_reloc_ok)
3622 ;
3623 else if (r == bfd_reloc_overflow)
3624 {
3625 const char *name;
3626
3627 if (h != NULL)
3628 {
3629 if (h->root.type == bfd_link_hash_undefweak
65f38f15 3630 && ppc64_elf_howto_table[(int) r_type]->pc_relative)
5bd4f169
AM
3631 {
3632 /* Assume this is a call protected by other code that
3633 detects the symbol is undefined. If this is the case,
3634 we can safely ignore the overflow. If not, the
3635 program is hosed anyway, and a little warning isn't
3636 going to help. */
3637
3638 continue;
3639 }
3640
3641 name = h->root.root.string;
3642 }
3643 else
3644 {
3645 name = bfd_elf_string_from_elf_section (input_bfd,
3646 symtab_hdr->sh_link,
3647 sym->st_name);
3648 if (name == NULL)
3649 continue;
3650 if (*name == '\0')
3651 name = bfd_section_name (input_bfd, sec);
3652 }
3653
65f38f15
AM
3654 if (! ((*info->callbacks->reloc_overflow)
3655 (info, name, ppc64_elf_howto_table[(int) r_type]->name,
3656 (bfd_vma) 0, input_bfd, input_section, offset)))
5bd4f169
AM
3657 return false;
3658 }
3659 else
3660 ret = false;
3661 }
3662
5bd4f169
AM
3663 return ret;
3664}
3665
3666/* Finish up dynamic symbol handling. We set the contents of various
3667 dynamic sections here. */
3668
3669static boolean
3670ppc64_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
3671 bfd *output_bfd;
3672 struct bfd_link_info *info;
3673 struct elf_link_hash_entry *h;
3674 Elf_Internal_Sym *sym;
3675{
65f38f15 3676 struct ppc_link_hash_table *htab;
5bd4f169
AM
3677 bfd *dynobj;
3678
65f38f15
AM
3679 htab = ppc_hash_table (info);
3680 dynobj = htab->elf.dynobj;
5bd4f169
AM
3681
3682 if (h->plt.offset != (bfd_vma) -1)
3683 {
5bd4f169 3684 struct elf_link_hash_entry *funcdesc_h;
65f38f15
AM
3685 Elf_Internal_Rela rela;
3686 Elf64_External_Rela *loc;
5bd4f169
AM
3687
3688 /* This symbol has an entry in the procedure linkage table. Set
3689 it up. */
3690
65f38f15
AM
3691 if (h->dynindx == -1
3692 || htab->splt == NULL
3693 || htab->srelplt == NULL
3694 || htab->sglink == NULL
3695 || h->root.root.string[0] != '.'
3696 || h->root.root.string[1] == '\0')
3697 abort ();
3698
3699 /* Find its corresponding function descriptor.
3700 ppc64_elf_adjust_dynamic_symbol has already set it up for us. */
5bd4f169 3701
65f38f15
AM
3702 funcdesc_h = elf_link_hash_lookup (elf_hash_table (info),
3703 h->root.root.string + 1,
3704 false, false, false);
3705
3706 if (funcdesc_h == NULL || funcdesc_h->dynindx == -1)
3707 abort ();
3708
3709 /* Create a JMP_SLOT reloc to inform the dynamic linker to
3710 fill in the PLT entry. */
5bd4f169 3711
65f38f15
AM
3712 rela.r_offset = (htab->splt->output_section->vma
3713 + htab->splt->output_offset
3714 + h->plt.offset);
3715 rela.r_info = ELF64_R_INFO (funcdesc_h->dynindx, R_PPC64_JMP_SLOT);
3716 rela.r_addend = 0;
3717
3718 loc = (Elf64_External_Rela *) htab->srelplt->contents;
3719 loc += (h->plt.offset - PLT_INITIAL_ENTRY_SIZE) / PLT_ENTRY_SIZE;
3720 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
5bd4f169 3721
65f38f15
AM
3722 /* We need to create a linkage function. */
3723 if (h->root.u.def.section == htab->sglink)
5bd4f169
AM
3724 {
3725 bfd_byte *p;
3726 bfd_vma pltoff;
3727 const unsigned int *stub;
3728
3729 /* Where to write it. */
3730 p = h->root.u.def.section->contents + h->root.u.def.value;
3731
3732 /* The function descriptor is in the PLT. */
65f38f15
AM
3733 pltoff = htab->splt->output_section->vma
3734 + htab->splt->output_section->output_offset
5bd4f169
AM
3735 + h->plt.offset
3736 - elf_gp (output_bfd) - TOC_BASE_OFF;
3737
3738 if (pltoff + 0x8000 > 0xffff)
3739 {
3740 (*_bfd_error_handler)
3741 (_("linkage table overflow against `%s'"),
3742 h->root.root.string);
3743 }
3744
3745 /* Write it out. */
3746 stub = ppc64_elf_glink_code;
3747 bfd_put_32 (output_bfd, *stub | (pltoff & 0xfffc), p);
3748 while (p += 4, ++stub < (ppc64_elf_glink_code
3749 + (sizeof (ppc64_elf_glink_code)
3750 / sizeof (*ppc64_elf_glink_code))));
3751 {
dc810e39 3752 bfd_put_32 (output_bfd, (bfd_vma) *stub, p);
5bd4f169 3753 }
5bd4f169 3754 }
65f38f15
AM
3755 else
3756 abort ();
5bd4f169
AM
3757 }
3758
3759 if (h->got.offset != (bfd_vma) -1)
3760 {
5bd4f169 3761 Elf_Internal_Rela rela;
65f38f15 3762 Elf64_External_Rela *loc;
5bd4f169
AM
3763
3764 /* This symbol has an entry in the global offset table. Set it
3765 up. */
3766
65f38f15
AM
3767 if (htab->sgot == NULL || htab->srelgot == NULL)
3768 abort ();
5bd4f169 3769
65f38f15
AM
3770 rela.r_offset = (htab->sgot->output_section->vma
3771 + htab->sgot->output_offset
dc810e39 3772 + (h->got.offset &~ (bfd_vma) 1));
5bd4f169 3773
65f38f15
AM
3774 /* If this is a static link, or it is a -Bsymbolic link and the
3775 symbol is defined locally or was forced to be local because
3776 of a version file, we just want to emit a RELATIVE reloc.
3777 The entry in the global offset table will already have been
3778 initialized in the relocate_section function. */
5bd4f169 3779 if (info->shared
65f38f15
AM
3780 && (info->symbolic
3781 || h->dynindx == -1
3782 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL))
5bd4f169
AM
3783 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
3784 {
65f38f15 3785 BFD_ASSERT((h->got.offset & 1) != 0);
5bd4f169
AM
3786 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
3787 rela.r_addend = (h->root.u.def.value
3788 + h->root.u.def.section->output_section->vma
3789 + h->root.u.def.section->output_offset);
3790 }
3791 else
3792 {
3793 BFD_ASSERT ((h->got.offset & 1) == 0);
65f38f15
AM
3794 bfd_put_64 (output_bfd, (bfd_vma) 0,
3795 htab->sgot->contents + h->got.offset);
5bd4f169
AM
3796 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_GLOB_DAT);
3797 rela.r_addend = 0;
3798 }
3799
65f38f15
AM
3800 loc = (Elf64_External_Rela *) htab->srelgot->contents;
3801 loc += htab->srelgot->reloc_count++;
3802 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
5bd4f169
AM
3803 }
3804
3805 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
3806 {
5bd4f169 3807 Elf_Internal_Rela rela;
65f38f15 3808 Elf64_External_Rela *loc;
5bd4f169 3809
65f38f15 3810 /* This symbol needs a copy reloc. Set it up. */
5bd4f169 3811
65f38f15
AM
3812 if (h->dynindx == -1
3813 || (h->root.type != bfd_link_hash_defined
3814 && h->root.type != bfd_link_hash_defweak)
3815 || htab->srelbss == NULL)
3816 abort ();
5bd4f169
AM
3817
3818 rela.r_offset = (h->root.u.def.value
3819 + h->root.u.def.section->output_section->vma
3820 + h->root.u.def.section->output_offset);
3821 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
3822 rela.r_addend = 0;
65f38f15
AM
3823 loc = (Elf64_External_Rela *) htab->srelbss->contents;
3824 loc += htab->srelbss->reloc_count++;
3825 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
5bd4f169
AM
3826 }
3827
5bd4f169
AM
3828 /* Mark some specially defined symbols as absolute. */
3829 if (strcmp (h->root.root.string, "_DYNAMIC") == 0)
3830 sym->st_shndx = SHN_ABS;
3831
3832 return true;
3833}
3834
65f38f15
AM
3835/* Used to decide how to sort relocs in an optimal manner for the
3836 dynamic linker, before writing them out. */
3837
3838static enum elf_reloc_type_class
3839ppc64_elf_reloc_type_class (rela)
3840 const Elf_Internal_Rela *rela;
3841{
a33d1f77
AM
3842 enum elf_ppc_reloc_type r_type;
3843
3844 r_type = (enum elf_ppc_reloc_type) ELF64_R_TYPE (rela->r_info);
3845 switch (r_type)
65f38f15
AM
3846 {
3847 case R_PPC64_RELATIVE:
3848 return reloc_class_relative;
3849 case R_PPC64_JMP_SLOT:
3850 return reloc_class_plt;
3851 case R_PPC64_COPY:
3852 return reloc_class_copy;
3853 default:
3854 return reloc_class_normal;
3855 }
3856}
3857
5bd4f169
AM
3858/* Finish up the dynamic sections. */
3859
3860static boolean
3861ppc64_elf_finish_dynamic_sections (output_bfd, info)
3862 bfd *output_bfd;
3863 struct bfd_link_info *info;
3864{
65f38f15
AM
3865 struct ppc_link_hash_table *htab;
3866 bfd *dynobj;
5bd4f169 3867 asection *sdyn;
5bd4f169 3868
65f38f15
AM
3869 htab = ppc_hash_table (info);
3870 dynobj = htab->elf.dynobj;
5bd4f169
AM
3871 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3872
65f38f15 3873 if (htab->elf.dynamic_sections_created)
5bd4f169 3874 {
5bd4f169
AM
3875 Elf64_External_Dyn *dyncon, *dynconend;
3876
65f38f15
AM
3877 if (sdyn == NULL || htab->sgot == NULL)
3878 abort ();
5bd4f169
AM
3879
3880 dyncon = (Elf64_External_Dyn *) sdyn->contents;
3881 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
3882 for (; dyncon < dynconend; dyncon++)
3883 {
3884 Elf_Internal_Dyn dyn;
5bd4f169
AM
3885
3886 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
3887
3888 switch (dyn.d_tag)
3889 {
65f38f15
AM
3890 default:
3891 continue;
5bd4f169 3892
65f38f15
AM
3893 case DT_PLTGOT:
3894 dyn.d_un.d_ptr = htab->splt->output_section->vma;
3895 break;
3896
3897 case DT_JMPREL:
3898 dyn.d_un.d_ptr = htab->srelplt->output_section->vma;
3899 break;
5bd4f169 3900
65f38f15
AM
3901 case DT_PLTRELSZ:
3902 if (htab->srelplt->output_section->_cooked_size != 0)
3903 dyn.d_un.d_val = htab->srelplt->output_section->_cooked_size;
5bd4f169 3904 else
65f38f15
AM
3905 dyn.d_un.d_val = htab->srelplt->output_section->_raw_size;
3906 break;
5bd4f169 3907 }
5bd4f169 3908
65f38f15 3909 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
5bd4f169 3910 }
5bd4f169
AM
3911 }
3912
3913 return true;
3914}
3915
3916#define TARGET_LITTLE_SYM bfd_elf64_powerpcle_vec
3917#define TARGET_LITTLE_NAME "elf64-powerpcle"
3918#define TARGET_BIG_SYM bfd_elf64_powerpc_vec
3919#define TARGET_BIG_NAME "elf64-powerpc"
3920#define ELF_ARCH bfd_arch_powerpc
3921#define ELF_MACHINE_CODE EM_PPC64
3922#define ELF_MAXPAGESIZE 0x10000
3923#define elf_info_to_howto ppc64_elf_info_to_howto
3924
3925#ifdef EM_CYGNUS_POWERPC
3926#define ELF_MACHINE_ALT1 EM_CYGNUS_POWERPC
3927#endif
3928
3929#ifdef EM_PPC_OLD
3930#define ELF_MACHINE_ALT2 EM_PPC_OLD
3931#endif
3932
3933#define elf_backend_want_got_sym 0
3934#define elf_backend_want_plt_sym 0
3935#define elf_backend_plt_alignment 3
3936#define elf_backend_plt_not_loaded 1
3937#define elf_backend_got_symbol_offset 0
3938#define elf_backend_got_header_size 0
3939#define elf_backend_can_gc_sections 1
65f38f15 3940#define elf_backend_can_refcount 1
5bd4f169
AM
3941
3942#define elf_backend_plt_header_size PLT_INITIAL_ENTRY_SIZE
3943
65f38f15
AM
3944#define bfd_elf64_bfd_reloc_type_lookup ppc64_elf_reloc_type_lookup
3945#define bfd_elf64_bfd_set_private_flags ppc64_elf_set_private_flags
3946#define bfd_elf64_bfd_copy_private_bfd_data ppc64_elf_copy_private_bfd_data
3947#define bfd_elf64_bfd_merge_private_bfd_data ppc64_elf_merge_private_bfd_data
3948#define bfd_elf64_bfd_link_hash_table_create ppc64_elf_link_hash_table_create
3949#define bfd_elf64_bfd_final_link ppc64_elf_final_link
3950
3951#define elf_backend_section_from_shdr ppc64_elf_section_from_shdr
3952#define elf_backend_create_dynamic_sections ppc64_elf_create_dynamic_sections
3953#define elf_backend_copy_indirect_symbol ppc64_elf_copy_indirect_symbol
3954#define elf_backend_check_relocs ppc64_elf_check_relocs
3955#define elf_backend_gc_mark_hook ppc64_elf_gc_mark_hook
3956#define elf_backend_gc_sweep_hook ppc64_elf_gc_sweep_hook
3957#define elf_backend_adjust_dynamic_symbol ppc64_elf_adjust_dynamic_symbol
3958#define elf_backend_size_dynamic_sections ppc64_elf_size_dynamic_sections
3959#define elf_backend_fake_sections ppc64_elf_fake_sections
3960#define elf_backend_relocate_section ppc64_elf_relocate_section
3961#define elf_backend_finish_dynamic_symbol ppc64_elf_finish_dynamic_symbol
3962#define elf_backend_reloc_type_class ppc64_elf_reloc_type_class
3963#define elf_backend_finish_dynamic_sections ppc64_elf_finish_dynamic_sections
5bd4f169
AM
3964
3965#include "elf64-target.h"
This page took 0.200886 seconds and 4 git commands to generate.