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