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