*** empty log message ***
[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
e86ce104
AM
2586 edir->is_func |= eind->is_func;
2587 edir->is_func_descriptor |= eind->is_func_descriptor;
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 {
2772 /* Ensure the function descriptor symbol string is
2773 part of the code symbol string. We aren't
2774 changing the name here, just allowing some tricks
2775 in ppc64_elf_hide_symbol. */
2776 fdh->root.root.string = h->root.root.string + 1;
2777 ((struct ppc_link_hash_entry *) fdh)->is_func_descriptor = 1;
721956f4 2778 ((struct ppc_link_hash_entry *) fdh)->oh = h;
e86ce104 2779 ((struct ppc_link_hash_entry *) h)->is_func = 1;
721956f4 2780 ((struct ppc_link_hash_entry *) h)->oh = fdh;
e86ce104
AM
2781 }
2782 }
2783 /* Fall through. */
2784
5bd4f169
AM
2785 case R_PPC64_REL64:
2786 case R_PPC64_REL32:
65f38f15
AM
2787 case R_PPC64_ADDR14:
2788 case R_PPC64_ADDR14_BRNTAKEN:
2789 case R_PPC64_ADDR14_BRTAKEN:
2790 case R_PPC64_ADDR16:
2791 case R_PPC64_ADDR16_DS:
2792 case R_PPC64_ADDR16_HA:
2793 case R_PPC64_ADDR16_HI:
2794 case R_PPC64_ADDR16_HIGHER:
2795 case R_PPC64_ADDR16_HIGHERA:
2796 case R_PPC64_ADDR16_HIGHEST:
2797 case R_PPC64_ADDR16_HIGHESTA:
2798 case R_PPC64_ADDR16_LO:
2799 case R_PPC64_ADDR16_LO_DS:
2800 case R_PPC64_ADDR24:
2801 case R_PPC64_ADDR30:
2802 case R_PPC64_ADDR32:
65f38f15
AM
2803 case R_PPC64_UADDR16:
2804 case R_PPC64_UADDR32:
2805 case R_PPC64_UADDR64:
5bd4f169 2806 case R_PPC64_TOC:
41bd81ab
AM
2807 /* Don't propagate .opd relocs. */
2808 if (NO_OPD_RELOCS && is_opd)
e86ce104 2809 break;
e86ce104 2810
65f38f15
AM
2811 /* If we are creating a shared library, and this is a reloc
2812 against a global symbol, or a non PC relative reloc
2813 against a local symbol, then we need to copy the reloc
2814 into the shared library. However, if we are linking with
2815 -Bsymbolic, we do not need to copy a reloc against a
2816 global symbol which is defined in an object we are
2817 including in the link (i.e., DEF_REGULAR is set). At
2818 this point we have not seen all the input files, so it is
2819 possible that DEF_REGULAR is not set now but will be set
2820 later (it is never cleared). In case of a weak definition,
2821 DEF_REGULAR may be cleared later by a strong definition in
2822 a shared library. We account for that possibility below by
2823 storing information in the relocs_copied field of the hash
2824 table entry. A similar situation occurs when creating
2825 shared libraries and symbol visibility changes render the
2826 symbol local.
2827
2828 If on the other hand, we are creating an executable, we
2829 may need to keep relocations for symbols satisfied by a
2830 dynamic library if we manage to avoid copy relocs for the
2831 symbol. */
2832 if ((info->shared
2833 && (sec->flags & SEC_ALLOC) != 0
a33d1f77 2834 && (IS_ABSOLUTE_RELOC (r_type)
65f38f15
AM
2835 || (h != NULL
2836 && (! info->symbolic
2837 || h->root.type == bfd_link_hash_defweak
2838 || (h->elf_link_hash_flags
2839 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
2840 || (!info->shared
2841 && (sec->flags & SEC_ALLOC) != 0
2842 && h != NULL
2843 && (h->root.type == bfd_link_hash_defweak
2844 || (h->elf_link_hash_flags
2845 & ELF_LINK_HASH_DEF_REGULAR) == 0)))
5bd4f169 2846 {
ec338859
AM
2847 struct ppc_dyn_relocs *p;
2848 struct ppc_dyn_relocs **head;
2849
65f38f15
AM
2850 /* We must copy these reloc types into the output file.
2851 Create a reloc section in dynobj and make room for
2852 this reloc. */
5bd4f169
AM
2853 if (sreloc == NULL)
2854 {
2855 const char *name;
65f38f15 2856 bfd *dynobj;
5bd4f169
AM
2857
2858 name = (bfd_elf_string_from_elf_section
2859 (abfd,
2860 elf_elfheader (abfd)->e_shstrndx,
2861 elf_section_data (sec)->rel_hdr.sh_name));
2862 if (name == NULL)
2863 return false;
2864
65f38f15
AM
2865 if (strncmp (name, ".rela", 5) != 0
2866 || strcmp (bfd_get_section_name (abfd, sec),
2867 name + 5) != 0)
2868 {
2869 (*_bfd_error_handler)
2870 (_("%s: bad relocation section name `%s\'"),
2871 bfd_archive_filename (abfd), name);
5d1634d7 2872 bfd_set_error (bfd_error_bad_value);
65f38f15
AM
2873 }
2874
65f38f15 2875 dynobj = htab->elf.dynobj;
5bd4f169
AM
2876 sreloc = bfd_get_section_by_name (dynobj, name);
2877 if (sreloc == NULL)
2878 {
2879 flagword flags;
2880
2881 sreloc = bfd_make_section (dynobj, name);
2882 flags = (SEC_HAS_CONTENTS | SEC_READONLY
2883 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2884 if ((sec->flags & SEC_ALLOC) != 0)
2885 flags |= SEC_ALLOC | SEC_LOAD;
2886 if (sreloc == NULL
2887 || ! bfd_set_section_flags (dynobj, sreloc, flags)
65f38f15 2888 || ! bfd_set_section_alignment (dynobj, sreloc, 3))
5bd4f169
AM
2889 return false;
2890 }
65f38f15 2891 elf_section_data (sec)->sreloc = sreloc;
5bd4f169
AM
2892 }
2893
65f38f15
AM
2894 /* If this is a global symbol, we count the number of
2895 relocations we need for this symbol. */
2896 if (h != NULL)
2897 {
ec338859 2898 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
65f38f15
AM
2899 }
2900 else
2901 {
ec338859
AM
2902 /* Track dynamic relocs needed for local syms too.
2903 We really need local syms available to do this
2904 easily. Oh well. */
2905
2906 asection *s;
2907 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
2908 sec, r_symndx);
2909 if (s == NULL)
2910 return false;
2911
2912 head = ((struct ppc_dyn_relocs **)
2913 &elf_section_data (s)->local_dynrel);
65f38f15 2914 }
ec338859
AM
2915
2916 p = *head;
2917 if (p == NULL || p->sec != sec)
2918 {
2919 p = ((struct ppc_dyn_relocs *)
2920 bfd_alloc (htab->elf.dynobj,
2921 (bfd_size_type) sizeof *p));
2922 if (p == NULL)
2923 return false;
2924 p->next = *head;
2925 *head = p;
2926 p->sec = sec;
2927 p->count = 0;
2928 p->pc_count = 0;
2929 }
2930
2931 p->count += 1;
2932 if (!IS_ABSOLUTE_RELOC (r_type))
2933 p->pc_count += 1;
65f38f15 2934 }
5bd4f169 2935 break;
65f38f15
AM
2936
2937 default:
96e0dda4 2938 break;
5bd4f169
AM
2939 }
2940 }
2941
2942 return true;
2943}
2944
2945/* Return the section that should be marked against GC for a given
2946 relocation. */
2947
2948static asection *
2949ppc64_elf_gc_mark_hook (abfd, info, rel, h, sym)
2950 bfd *abfd;
2951 struct bfd_link_info *info ATTRIBUTE_UNUSED;
2952 Elf_Internal_Rela *rel;
2953 struct elf_link_hash_entry *h;
2954 Elf_Internal_Sym *sym;
2955{
2956 if (h != NULL)
2957 {
a33d1f77
AM
2958 enum elf_ppc_reloc_type r_type;
2959
2960 r_type = (enum elf_ppc_reloc_type) ELF64_R_TYPE (rel->r_info);
2961 switch (r_type)
5bd4f169
AM
2962 {
2963 case R_PPC64_GNU_VTINHERIT:
2964 case R_PPC64_GNU_VTENTRY:
2965 break;
2966
2967 default:
2968 switch (h->root.type)
2969 {
2970 case bfd_link_hash_defined:
2971 case bfd_link_hash_defweak:
2972 return h->root.u.def.section;
2973
2974 case bfd_link_hash_common:
2975 return h->root.u.c.p->section;
2976
2977 default:
2978 break;
2979 }
2980 }
2981 }
2982 else
2983 {
9ad5cbcf 2984 return bfd_section_from_elf_index (abfd, sym->st_shndx);
5bd4f169
AM
2985 }
2986
2987 return NULL;
2988}
2989
65f38f15
AM
2990/* Update the .got, .plt. and dynamic reloc reference counts for the
2991 section being removed. */
5bd4f169
AM
2992
2993static boolean
2994ppc64_elf_gc_sweep_hook (abfd, info, sec, relocs)
2995 bfd *abfd;
2996 struct bfd_link_info *info ATTRIBUTE_UNUSED;
2997 asection *sec;
2998 const Elf_Internal_Rela *relocs;
2999{
3000 Elf_Internal_Shdr *symtab_hdr;
3001 struct elf_link_hash_entry **sym_hashes;
3002 bfd_signed_vma *local_got_refcounts;
3003 const Elf_Internal_Rela *rel, *relend;
5bd4f169 3004
ec338859
AM
3005 elf_section_data (sec)->local_dynrel = NULL;
3006
5bd4f169
AM
3007 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3008 sym_hashes = elf_sym_hashes (abfd);
3009 local_got_refcounts = elf_local_got_refcounts (abfd);
3010
3011 relend = relocs + sec->reloc_count;
3012 for (rel = relocs; rel < relend; rel++)
a33d1f77
AM
3013 {
3014 unsigned long r_symndx;
3015 enum elf_ppc_reloc_type r_type;
3016 struct elf_link_hash_entry *h;
5bd4f169 3017
a33d1f77
AM
3018 r_symndx = ELF64_R_SYM (rel->r_info);
3019 r_type = (enum elf_ppc_reloc_type) ELF64_R_TYPE (rel->r_info);
3020 switch (r_type)
3021 {
3022 case R_PPC64_GOT16:
3023 case R_PPC64_GOT16_DS:
3024 case R_PPC64_GOT16_HA:
3025 case R_PPC64_GOT16_HI:
3026 case R_PPC64_GOT16_LO:
3027 case R_PPC64_GOT16_LO_DS:
3028 if (r_symndx >= symtab_hdr->sh_info)
3029 {
3030 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3031 if (h->got.refcount > 0)
3032 h->got.refcount--;
3033 }
3034 else
3035 {
3036 if (local_got_refcounts[r_symndx] > 0)
3037 local_got_refcounts[r_symndx]--;
3038 }
3039 break;
65f38f15 3040
a33d1f77
AM
3041 case R_PPC64_PLT16_HA:
3042 case R_PPC64_PLT16_HI:
3043 case R_PPC64_PLT16_LO:
3044 case R_PPC64_PLT32:
3045 case R_PPC64_PLT64:
3046 if (r_symndx >= symtab_hdr->sh_info)
3047 {
3048 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3049 if (h->plt.refcount > 0)
3050 h->plt.refcount--;
3051 }
3052 break;
65f38f15 3053
721956f4
AM
3054 case R_PPC64_REL14:
3055 case R_PPC64_REL14_BRNTAKEN:
3056 case R_PPC64_REL14_BRTAKEN:
5d1634d7
AM
3057 case R_PPC64_REL24:
3058 if (r_symndx >= symtab_hdr->sh_info)
3059 {
3060 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3061 if (h->plt.refcount > 0)
3062 h->plt.refcount--;
3063 }
e86ce104 3064 break;
5d1634d7 3065
a33d1f77
AM
3066 case R_PPC64_REL32:
3067 case R_PPC64_REL64:
3068 if (r_symndx >= symtab_hdr->sh_info)
3069 {
3070 struct ppc_link_hash_entry *eh;
3071 struct ppc_dyn_relocs **pp;
3072 struct ppc_dyn_relocs *p;
65f38f15 3073
a33d1f77
AM
3074 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3075 eh = (struct ppc_link_hash_entry *) h;
65f38f15 3076
a33d1f77
AM
3077 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
3078 if (p->sec == sec)
3079 {
3080 p->pc_count -= 1;
3081 p->count -= 1;
3082 if (p->count == 0)
3083 *pp = p->next;
3084 break;
3085 }
3086 }
3087 break;
65f38f15 3088
a33d1f77
AM
3089 case R_PPC64_ADDR14:
3090 case R_PPC64_ADDR14_BRNTAKEN:
3091 case R_PPC64_ADDR14_BRTAKEN:
3092 case R_PPC64_ADDR16:
3093 case R_PPC64_ADDR16_DS:
3094 case R_PPC64_ADDR16_HA:
3095 case R_PPC64_ADDR16_HI:
3096 case R_PPC64_ADDR16_HIGHER:
3097 case R_PPC64_ADDR16_HIGHERA:
3098 case R_PPC64_ADDR16_HIGHEST:
3099 case R_PPC64_ADDR16_HIGHESTA:
3100 case R_PPC64_ADDR16_LO:
3101 case R_PPC64_ADDR16_LO_DS:
3102 case R_PPC64_ADDR24:
3103 case R_PPC64_ADDR30:
3104 case R_PPC64_ADDR32:
3105 case R_PPC64_ADDR64:
3106 case R_PPC64_UADDR16:
3107 case R_PPC64_UADDR32:
3108 case R_PPC64_UADDR64:
3109 case R_PPC64_TOC:
3110 if (r_symndx >= symtab_hdr->sh_info)
3111 {
3112 struct ppc_link_hash_entry *eh;
3113 struct ppc_dyn_relocs **pp;
3114 struct ppc_dyn_relocs *p;
65f38f15 3115
a33d1f77
AM
3116 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3117 eh = (struct ppc_link_hash_entry *) h;
3118
3119 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
3120 if (p->sec == sec)
3121 {
3122 p->count -= 1;
3123 if (p->count == 0)
3124 *pp = p->next;
3125 break;
3126 }
3127 }
3128 break;
5bd4f169 3129
a33d1f77
AM
3130 default:
3131 break;
3132 }
3133 }
5bd4f169
AM
3134 return true;
3135}
3136
e86ce104
AM
3137/* Called via elf_link_hash_traverse to transfer dynamic linking
3138 information on function code symbol entries to their corresponding
3139 function descriptor symbol entries. */
5bd4f169 3140static boolean
e86ce104 3141func_desc_adjust (h, inf)
5bd4f169 3142 struct elf_link_hash_entry *h;
e86ce104 3143 PTR inf;
5bd4f169 3144{
e86ce104 3145 struct bfd_link_info *info;
65f38f15 3146 struct ppc_link_hash_table *htab;
5bd4f169 3147
e92d460e 3148 if (h->root.type == bfd_link_hash_indirect)
e86ce104
AM
3149 return true;
3150
e92d460e
AM
3151 if (h->root.type == bfd_link_hash_warning)
3152 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3153
e86ce104 3154 info = (struct bfd_link_info *) inf;
65f38f15 3155 htab = ppc_hash_table (info);
5bd4f169 3156
e86ce104
AM
3157 /* If this is a function code symbol, transfer dynamic linking
3158 information to the function descriptor symbol. */
3159 if (!((struct ppc_link_hash_entry *) h)->is_func)
3160 return true;
3161
805fc799
AM
3162 if (h->root.type == bfd_link_hash_undefweak
3163 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR))
3164 htab->have_undefweak = true;
3165
e86ce104
AM
3166 if (h->plt.refcount > 0
3167 && h->root.root.string[0] == '.'
3168 && h->root.root.string[1] != '\0')
5bd4f169 3169 {
721956f4 3170 struct elf_link_hash_entry *fdh = ((struct ppc_link_hash_entry *) h)->oh;
e86ce104 3171 boolean force_local;
5bd4f169 3172
648cca2c
AM
3173 /* Find the corresponding function descriptor symbol. Create it
3174 as undefined if necessary. */
5bd4f169 3175
721956f4
AM
3176 if (fdh == NULL)
3177 fdh = elf_link_hash_lookup (&htab->elf, h->root.root.string + 1,
3178 false, false, true);
5bd4f169 3179
d926e03d
AM
3180 if (fdh == NULL
3181 && info->shared
3182 && (h->root.type == bfd_link_hash_undefined
3183 || h->root.type == bfd_link_hash_undefweak))
648cca2c
AM
3184 {
3185 bfd *abfd;
3186 asymbol *newsym;
3187
d926e03d 3188 abfd = h->root.u.undef.abfd;
648cca2c
AM
3189 newsym = bfd_make_empty_symbol (abfd);
3190 newsym->name = h->root.root.string + 1;
3191 newsym->section = bfd_und_section_ptr;
3192 newsym->value = 0;
3193 newsym->flags = BSF_OBJECT;
3194 if (h->root.type == bfd_link_hash_undefweak)
3195 newsym->flags |= BSF_WEAK;
3196
3197 if ( !(_bfd_generic_link_add_one_symbol
3198 (info, abfd, newsym->name, newsym->flags,
3199 newsym->section, newsym->value, NULL, false, false,
3200 (struct bfd_link_hash_entry **) &fdh)))
3201 {
3202 return false;
3203 }
d926e03d 3204 fdh->elf_link_hash_flags &= ~ELF_LINK_NON_ELF;
648cca2c
AM
3205 }
3206
e86ce104
AM
3207 if (fdh != NULL
3208 && (fdh->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0
3209 && (info->shared
3210 || (fdh->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
3211 || (fdh->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0))
3212 {
3213 if (fdh->dynindx == -1)
3214 if (! bfd_elf64_link_record_dynamic_symbol (info, fdh))
3215 return false;
e86ce104
AM
3216 fdh->elf_link_hash_flags |= (h->elf_link_hash_flags
3217 & (ELF_LINK_HASH_REF_REGULAR
3218 | ELF_LINK_HASH_REF_DYNAMIC
3219 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
3220 | ELF_LINK_NON_GOT_REF));
e1fa50e7
AM
3221 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3222 {
3223 fdh->plt.refcount = h->plt.refcount;
3224 fdh->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
3225 }
e86ce104 3226 ((struct ppc_link_hash_entry *) fdh)->is_func_descriptor = 1;
721956f4 3227 ((struct ppc_link_hash_entry *) fdh)->oh = h;
e86ce104 3228 fdh->root.root.string = h->root.root.string + 1;
721956f4 3229 ((struct ppc_link_hash_entry *) h)->oh = fdh;
e86ce104
AM
3230 }
3231
3232 /* Now that the info is on the function descriptor, clear the
3233 function code sym info. Any function code syms for which we
3234 don't have a definition in a regular file, we force local.
3235 This prevents a shared library from exporting syms that have
3236 been imported from another library. Function code syms that
3237 are really in the library we must leave global to prevent the
e1fa50e7 3238 linker dragging in a definition from a static library. */
82bd7b59
AM
3239 force_local = ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
3240 && info->shared);
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 {
3505 const char *name;
721956f4 3506 struct elf_link_hash_entry *fh = ((struct ppc_link_hash_entry *) h)->oh;
e86ce104
AM
3507 struct ppc_link_hash_table *htab;
3508
3509 name = h->root.root.string - 1;
3510 htab = ppc_hash_table (info);
721956f4
AM
3511 if (fh == NULL)
3512 fh = elf_link_hash_lookup (&htab->elf, name, false, false, false);
e86ce104
AM
3513 if (fh != NULL)
3514 _bfd_elf_link_hash_hide_symbol (info, fh, force_local);
3515 }
3516}
3517
65f38f15
AM
3518/* This is the condition under which ppc64_elf_finish_dynamic_symbol
3519 will be called from elflink.h. If elflink.h doesn't call our
3520 finish_dynamic_symbol routine, we'll need to do something about
3521 initializing any .plt and .got entries in ppc64_elf_relocate_section. */
3522#define WILL_CALL_FINISH_DYNAMIC_SYMBOL(DYN, INFO, H) \
3523 ((DYN) \
3524 && ((INFO)->shared \
3525 || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0) \
3526 && ((H)->dynindx != -1 \
3527 || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0))
5bd4f169 3528
65f38f15
AM
3529/* Allocate space in .plt, .got and associated reloc sections for
3530 dynamic relocs. */
5bd4f169
AM
3531
3532static boolean
65f38f15
AM
3533allocate_dynrelocs (h, inf)
3534 struct elf_link_hash_entry *h;
3535 PTR inf;
5bd4f169 3536{
65f38f15
AM
3537 struct bfd_link_info *info;
3538 struct ppc_link_hash_table *htab;
5bd4f169 3539 asection *s;
65f38f15
AM
3540 struct ppc_link_hash_entry *eh;
3541 struct ppc_dyn_relocs *p;
5bd4f169 3542
e92d460e 3543 if (h->root.type == bfd_link_hash_indirect)
65f38f15 3544 return true;
5bd4f169 3545
e92d460e
AM
3546 if (h->root.type == bfd_link_hash_warning)
3547 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3548
65f38f15
AM
3549 info = (struct bfd_link_info *) inf;
3550 htab = ppc_hash_table (info);
5bd4f169 3551
65f38f15 3552 if (htab->elf.dynamic_sections_created
e86ce104
AM
3553 && h->plt.refcount > 0
3554 && h->dynindx != -1)
5bd4f169 3555 {
e86ce104 3556 BFD_ASSERT (((struct ppc_link_hash_entry *) h)->is_func_descriptor);
5bd4f169 3557
65f38f15
AM
3558 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info, h))
3559 {
65f38f15
AM
3560 /* If this is the first .plt entry, make room for the special
3561 first entry. */
3562 s = htab->splt;
5bd4f169 3563 if (s->_raw_size == 0)
65f38f15
AM
3564 s->_raw_size += PLT_INITIAL_ENTRY_SIZE;
3565
3566 h->plt.offset = s->_raw_size;
3567
3568 /* Make room for this entry. */
3569 s->_raw_size += PLT_ENTRY_SIZE;
3570
721956f4 3571 /* Make room for the .glink code. */
5d1634d7
AM
3572 s = htab->sglink;
3573 if (s->_raw_size == 0)
3574 s->_raw_size += GLINK_CALL_STUB_SIZE;
3575 /* We need bigger stubs past index 32767. */
3576 if (s->_raw_size >= GLINK_CALL_STUB_SIZE + 32768*2*4)
3577 s->_raw_size += 4;
3578 s->_raw_size += 2*4;
65f38f15
AM
3579
3580 /* We also need to make an entry in the .rela.plt section. */
3581 s = htab->srelplt;
3582 s->_raw_size += sizeof (Elf64_External_Rela);
3583 }
3584 else
3585 {
3586 h->plt.offset = (bfd_vma) -1;
3587 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
3588 }
3589 }
3590 else
3591 {
3592 h->plt.offset = (bfd_vma) -1;
3593 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
3594 }
3595
3596 if (h->got.refcount > 0)
3597 {
3598 boolean dyn;
3599
3600 /* Make sure this symbol is output as a dynamic symbol.
3601 Undefined weak syms won't yet be marked as dynamic. */
3602 if (h->dynindx == -1
3603 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
3604 {
3605 if (! bfd_elf64_link_record_dynamic_symbol (info, h))
3606 return false;
3607 }
3608
3609 s = htab->sgot;
3610 h->got.offset = s->_raw_size;
3611 s->_raw_size += 8;
3612 dyn = htab->elf.dynamic_sections_created;
3613 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info, h))
3614 htab->srelgot->_raw_size += sizeof (Elf64_External_Rela);
3615 }
3616 else
3617 h->got.offset = (bfd_vma) -1;
3618
3619 eh = (struct ppc_link_hash_entry *) h;
3620 if (eh->dyn_relocs == NULL)
3621 return true;
3622
3623 /* In the shared -Bsymbolic case, discard space allocated for
3624 dynamic pc-relative relocs against symbols which turn out to be
3625 defined in regular objects. For the normal shared case, discard
3626 space for relocs that have become local due to symbol visibility
3627 changes. */
3628
3629 if (info->shared)
3630 {
3631 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
3632 && ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0
3633 || info->symbolic))
3634 {
3635 struct ppc_dyn_relocs **pp;
3636
3637 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
5bd4f169 3638 {
65f38f15
AM
3639 p->count -= p->pc_count;
3640 p->pc_count = 0;
3641 if (p->count == 0)
3642 *pp = p->next;
3643 else
3644 pp = &p->next;
5bd4f169 3645 }
65f38f15
AM
3646 }
3647 }
3648 else
3649 {
3650 /* For the non-shared case, discard space for relocs against
3651 symbols which turn out to need copy relocs or are not
3652 dynamic. */
3653
3654 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
3655 && (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
3656 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
3657 || (htab->elf.dynamic_sections_created
3658 && (h->root.type == bfd_link_hash_undefweak
3659 || h->root.type == bfd_link_hash_undefined))))
3660 {
3661 /* Make sure this symbol is output as a dynamic symbol.
3662 Undefined weak syms won't yet be marked as dynamic. */
3663 if (h->dynindx == -1
3664 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
3665 {
ec338859 3666 if (! bfd_elf64_link_record_dynamic_symbol (info, h))
65f38f15
AM
3667 return false;
3668 }
3669
3670 /* If that succeeded, we know we'll be keeping all the
3671 relocs. */
3672 if (h->dynindx != -1)
3673 goto keep;
3674 }
3675
3676 eh->dyn_relocs = NULL;
3677
ec338859 3678 keep: ;
65f38f15
AM
3679 }
3680
3681 /* Finally, allocate space. */
3682 for (p = eh->dyn_relocs; p != NULL; p = p->next)
3683 {
3684 asection *sreloc = elf_section_data (p->sec)->sreloc;
3685 sreloc->_raw_size += p->count * sizeof (Elf64_External_Rela);
3686 }
3687
3688 return true;
3689}
3690
3691/* Find any dynamic relocs that apply to read-only sections. */
3692
3693static boolean
3694readonly_dynrelocs (h, inf)
3695 struct elf_link_hash_entry *h;
3696 PTR inf;
3697{
3698 struct ppc_link_hash_entry *eh;
3699 struct ppc_dyn_relocs *p;
3700
e92d460e
AM
3701 if (h->root.type == bfd_link_hash_warning)
3702 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3703
65f38f15
AM
3704 eh = (struct ppc_link_hash_entry *) h;
3705 for (p = eh->dyn_relocs; p != NULL; p = p->next)
3706 {
3707 asection *s = p->sec->output_section;
3708
3709 if (s != NULL && (s->flags & SEC_READONLY) != 0)
3710 {
3711 struct bfd_link_info *info = (struct bfd_link_info *) inf;
3712
3713 info->flags |= DF_TEXTREL;
3714
3715 /* Not an error, just cut short the traversal. */
3716 return false;
3717 }
3718 }
3719 return true;
3720}
3721
3722/* Set the sizes of the dynamic sections. */
3723
3724static boolean
3725ppc64_elf_size_dynamic_sections (output_bfd, info)
3726 bfd *output_bfd ATTRIBUTE_UNUSED;
3727 struct bfd_link_info *info;
3728{
3729 struct ppc_link_hash_table *htab;
3730 bfd *dynobj;
3731 asection *s;
3732 boolean relocs;
3733 bfd *ibfd;
3734
3735 htab = ppc_hash_table (info);
3736 dynobj = htab->elf.dynobj;
3737 if (dynobj == NULL)
3738 abort ();
3739
3740 if (htab->elf.dynamic_sections_created)
3741 {
3742 /* Set the contents of the .interp section to the interpreter. */
3743 if (! info->shared)
3744 {
3745 s = bfd_get_section_by_name (dynobj, ".interp");
3746 if (s == NULL)
3747 abort ();
3748 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
3749 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
3750 }
3751 }
3752
3753 /* Set up .got offsets for local syms, and space for local dynamic
3754 relocs. */
3755 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
3756 {
3757 bfd_signed_vma *local_got;
3758 bfd_signed_vma *end_local_got;
3759 bfd_size_type locsymcount;
3760 Elf_Internal_Shdr *symtab_hdr;
3761 asection *srel;
3762
3763 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
3764 continue;
3765
3766 for (s = ibfd->sections; s != NULL; s = s->next)
3767 {
ec338859 3768 struct ppc_dyn_relocs *p;
65f38f15 3769
ec338859
AM
3770 for (p = *((struct ppc_dyn_relocs **)
3771 &elf_section_data (s)->local_dynrel);
3772 p != NULL;
3773 p = p->next)
65f38f15 3774 {
ec338859
AM
3775 if (!bfd_is_abs_section (p->sec)
3776 && bfd_is_abs_section (p->sec->output_section))
3777 {
3778 /* Input section has been discarded, either because
3779 it is a copy of a linkonce section or due to
3780 linker script /DISCARD/, so we'll be discarding
3781 the relocs too. */
3782 }
248866a8 3783 else if (p->count != 0)
ec338859
AM
3784 {
3785 srel = elf_section_data (p->sec)->sreloc;
3786 srel->_raw_size += p->count * sizeof (Elf64_External_Rela);
248866a8
AM
3787 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
3788 info->flags |= DF_TEXTREL;
ec338859 3789 }
65f38f15
AM
3790 }
3791 }
3792
3793 local_got = elf_local_got_refcounts (ibfd);
3794 if (!local_got)
3795 continue;
3796
3797 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
3798 locsymcount = symtab_hdr->sh_info;
3799 end_local_got = local_got + locsymcount;
3800 s = htab->sgot;
3801 srel = htab->srelgot;
3802 for (; local_got < end_local_got; ++local_got)
3803 {
3804 if (*local_got > 0)
5bd4f169 3805 {
65f38f15
AM
3806 *local_got = s->_raw_size;
3807 s->_raw_size += 8;
3808 if (info->shared)
3809 srel->_raw_size += sizeof (Elf64_External_Rela);
5bd4f169 3810 }
65f38f15
AM
3811 else
3812 *local_got = (bfd_vma) -1;
3813 }
3814 }
3815
3816 /* Allocate global sym .plt and .got entries, and space for global
3817 sym dynamic relocs. */
3818 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, (PTR) info);
3819
3820 /* We now have determined the sizes of the various dynamic sections.
3821 Allocate memory for them. */
3822 relocs = false;
3823 for (s = dynobj->sections; s != NULL; s = s->next)
3824 {
3825 if ((s->flags & SEC_LINKER_CREATED) == 0)
3826 continue;
3827
721956f4
AM
3828 if (s == htab->sbrlt || s == htab->srelbrlt)
3829 /* These haven't been allocated yet; don't strip. */
3830 continue;
3831 else if (s == htab->splt
3832 || s == htab->sgot
3833 || s == htab->sglink)
65f38f15
AM
3834 {
3835 /* Strip this section if we don't need it; see the
3836 comment below. */
5bd4f169 3837 }
65f38f15 3838 else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
5bd4f169
AM
3839 {
3840 if (s->_raw_size == 0)
3841 {
3842 /* If we don't need this section, strip it from the
3843 output file. This is mostly to handle .rela.bss and
3844 .rela.plt. We must create both sections in
3845 create_dynamic_sections, because they must be created
3846 before the linker maps input sections to output
3847 sections. The linker does that before
3848 adjust_dynamic_symbol is called, and it is that
3849 function which decides whether anything needs to go
3850 into these sections. */
5bd4f169
AM
3851 }
3852 else
3853 {
65f38f15
AM
3854 if (s != htab->srelplt)
3855 relocs = true;
5bd4f169
AM
3856
3857 /* We use the reloc_count field as a counter if we need
3858 to copy relocs into the output file. */
3859 s->reloc_count = 0;
3860 }
3861 }
65f38f15 3862 else
5bd4f169
AM
3863 {
3864 /* It's not one of our sections, so don't allocate space. */
3865 continue;
3866 }
3867
65f38f15 3868 if (s->_raw_size == 0)
5bd4f169
AM
3869 {
3870 _bfd_strip_section_from_output (info, s);
3871 continue;
3872 }
3873
65f38f15
AM
3874 /* Allocate memory for the section contents. We use bfd_zalloc
3875 here in case unused entries are not reclaimed before the
3876 section's contents are written out. This should not happen,
3877 but this way if it does, we get a R_PPC64_NONE reloc instead
3878 of garbage. */
721956f4 3879 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
65f38f15 3880 if (s->contents == NULL)
5bd4f169
AM
3881 return false;
3882 }
3883
e86ce104 3884 if (htab->elf.dynamic_sections_created)
5bd4f169
AM
3885 {
3886 /* Add some entries to the .dynamic section. We fill in the
3887 values later, in ppc64_elf_finish_dynamic_sections, but we
3888 must add the entries now so that we get the correct size for
3889 the .dynamic section. The DT_DEBUG entry is filled in by the
3890 dynamic linker and used by the debugger. */
dc810e39
AM
3891#define add_dynamic_entry(TAG, VAL) \
3892 bfd_elf64_add_dynamic_entry (info, (bfd_vma) (TAG), (bfd_vma) (VAL))
3893
3894 if (!info->shared)
5bd4f169 3895 {
dc810e39 3896 if (!add_dynamic_entry (DT_DEBUG, 0))
5bd4f169
AM
3897 return false;
3898 }
3899
65f38f15 3900 if (htab->splt->_raw_size != 0)
5bd4f169 3901 {
dc810e39
AM
3902 if (!add_dynamic_entry (DT_PLTGOT, 0)
3903 || !add_dynamic_entry (DT_PLTRELSZ, 0)
3904 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
5d1634d7
AM
3905 || !add_dynamic_entry (DT_JMPREL, 0)
3906 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
5bd4f169
AM
3907 return false;
3908 }
3909
19397422
AM
3910 if (NO_OPD_RELOCS)
3911 {
3912 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
3913 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
3914 return false;
3915 }
3916
5bd4f169
AM
3917 if (relocs)
3918 {
dc810e39
AM
3919 if (!add_dynamic_entry (DT_RELA, 0)
3920 || !add_dynamic_entry (DT_RELASZ, 0)
3921 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
5bd4f169 3922 return false;
5bd4f169 3923
65f38f15
AM
3924 /* If any dynamic relocs apply to a read-only section,
3925 then we need a DT_TEXTREL entry. */
248866a8
AM
3926 if ((info->flags & DF_TEXTREL) == 0)
3927 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs,
3928 (PTR) info);
5bd4f169 3929
65f38f15 3930 if ((info->flags & DF_TEXTREL) != 0)
5bd4f169 3931 {
65f38f15
AM
3932 if (!add_dynamic_entry (DT_TEXTREL, 0))
3933 return false;
5bd4f169 3934 }
5bd4f169 3935 }
5bd4f169 3936 }
65f38f15 3937#undef add_dynamic_entry
5bd4f169
AM
3938
3939 return true;
3940}
3941
721956f4 3942/* Determine the type of stub needed, if any, for a call. */
5bd4f169 3943
721956f4
AM
3944static INLINE enum ppc_stub_type
3945ppc_type_of_stub (input_sec, rel, hash, destination)
3946 asection *input_sec;
3947 const Elf_Internal_Rela *rel;
3948 struct ppc_link_hash_entry **hash;
3949 bfd_vma destination;
5bd4f169 3950{
721956f4
AM
3951 struct ppc_link_hash_entry *h = *hash;
3952 bfd_vma location;
3953 bfd_vma branch_offset;
3954 bfd_vma max_branch_offset;
3955 unsigned int r_type;
5bd4f169 3956
721956f4
AM
3957 if (h != NULL)
3958 {
3959 if (h->oh != NULL
3960 && h->oh->plt.offset != (bfd_vma) -1
3961 && h->oh->dynindx != -1)
5bd4f169 3962 {
721956f4
AM
3963 *hash = (struct ppc_link_hash_entry *) h->oh;
3964 return ppc_stub_plt_call;
5bd4f169
AM
3965 }
3966
721956f4
AM
3967 if (h->elf.root.type == bfd_link_hash_undefweak
3968 || h->elf.root.type == bfd_link_hash_undefined)
3969 return ppc_stub_none;
5d1634d7 3970 }
5d1634d7 3971
721956f4
AM
3972 /* Determine where the call point is. */
3973 location = (input_sec->output_offset
3974 + input_sec->output_section->vma
3975 + rel->r_offset);
5d1634d7 3976
721956f4
AM
3977 branch_offset = destination - location;
3978 r_type = ELF64_R_TYPE (rel->r_info);
5d1634d7 3979
721956f4
AM
3980 /* Determine if a long branch stub is needed. */
3981 max_branch_offset = 1 << 25;
3982 if (r_type != (unsigned int) R_PPC64_REL24)
3983 max_branch_offset = 1 << 15;
5d1634d7 3984
721956f4
AM
3985 if (branch_offset + max_branch_offset >= 2 * max_branch_offset)
3986 /* We need a stub. Figure out whether a long_branch or plt_branch
3987 is needed later. */
3988 return ppc_stub_long_branch;
5d1634d7 3989
721956f4 3990 return ppc_stub_none;
5d1634d7
AM
3991}
3992
3993/* Build a .plt call stub. */
3994
3995static bfd_byte *
3996build_plt_stub (obfd, p, offset, glink)
3997 bfd *obfd;
3998 bfd_byte *p;
3999 int offset;
4000 int glink;
4001{
4002#define PPC_LO(v) ((v) & 0xffff)
4003#define PPC_HI(v) (((v) >> 16) & 0xffff)
4004#define PPC_HA(v) PPC_HI ((v) + 0x8000)
4005
4006 if (glink)
4007 bfd_put_32 (obfd, LD_R2_40R1, p), p += 4;
4008 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
4009 if (!glink)
4010 bfd_put_32 (obfd, STD_R2_40R1, p), p += 4;
4011 bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset), p), p += 4;
4012 if (PPC_HA (offset + 8) != PPC_HA (offset))
4013 bfd_put_32 (obfd, ADDIS_R12_R12_1, p), p += 4;
4014 offset += 8;
4015 bfd_put_32 (obfd, LD_R2_0R12 | PPC_LO (offset), p), p += 4;
4016 if (PPC_HA (offset + 8) != PPC_HA (offset))
4017 bfd_put_32 (obfd, ADDIS_R12_R12_1, p), p += 4;
4018 offset += 8;
4019 bfd_put_32 (obfd, MTCTR_R11, p), p += 4;
4020 bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset), p), p += 4;
4021 bfd_put_32 (obfd, BCTR, p), p += 4;
4022 return p;
4023}
4024
5d1634d7 4025static boolean
721956f4
AM
4026ppc_build_one_stub (gen_entry, in_arg)
4027 struct bfd_hash_entry *gen_entry;
4028 PTR in_arg;
5d1634d7 4029{
721956f4
AM
4030 struct ppc_stub_hash_entry *stub_entry;
4031 struct ppc_branch_hash_entry *br_entry;
5d1634d7
AM
4032 struct bfd_link_info *info;
4033 struct ppc_link_hash_table *htab;
721956f4
AM
4034 asection *stub_sec;
4035 bfd *stub_bfd;
4036 bfd_byte *loc;
4037 bfd_byte *p;
4038 unsigned int indx;
4039 bfd_vma off;
4040 int size;
5d1634d7 4041
721956f4
AM
4042 /* Massage our args to the form they really have. */
4043 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
4044 info = (struct bfd_link_info *) in_arg;
5d1634d7 4045
5d1634d7 4046 htab = ppc_hash_table (info);
721956f4 4047 stub_sec = stub_entry->stub_sec;
5d1634d7 4048
721956f4
AM
4049 /* Make a note of the offset within the stubs for this entry. */
4050 stub_entry->stub_offset = stub_sec->_cooked_size;
4051 loc = stub_sec->contents + stub_entry->stub_offset;
4052
4053 stub_bfd = stub_sec->owner;
4054
4055 switch (stub_entry->stub_type)
5d1634d7 4056 {
721956f4
AM
4057 case ppc_stub_long_branch:
4058 /* Branches are relative. This is where we are going to. */
4059 off = (stub_entry->target_value
4060 + stub_entry->target_section->output_offset
4061 + stub_entry->target_section->output_section->vma);
5d1634d7 4062
721956f4
AM
4063 /* And this is where we are coming from. */
4064 off -= (stub_entry->stub_offset
4065 + stub_sec->output_offset
4066 + stub_sec->output_section->vma);
e86ce104 4067
721956f4 4068 BFD_ASSERT (off + (1 << 25) < (bfd_vma) (1 << 26));
e86ce104 4069
721956f4
AM
4070 bfd_put_32 (stub_bfd, (bfd_vma) B_DOT | (off & 0x3fffffc), loc);
4071 size = 4;
4072 break;
e86ce104 4073
721956f4
AM
4074 case ppc_stub_plt_branch:
4075 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
4076 stub_entry->root.string + 9,
4077 false, false);
4078 if (br_entry == NULL)
4079 {
4080 (*_bfd_error_handler) (_("can't find branch stub `%s'"),
4081 stub_entry->root.string + 9);
4082 htab->stub_error = true;
4083 return false;
4084 }
4085
4086 off = (stub_entry->target_value
4087 + stub_entry->target_section->output_offset
4088 + stub_entry->target_section->output_section->vma);
4089
4090 bfd_put_64 (htab->sbrlt->owner, off,
4091 htab->sbrlt->contents + br_entry->offset);
4092
4093 if (info->shared)
4094 {
4095 /* Create a reloc for the branch lookup table entry. */
4096 Elf_Internal_Rela rela;
4097 Elf64_External_Rela *r;
5d1634d7 4098
721956f4
AM
4099 rela.r_offset = (br_entry->offset
4100 + htab->sbrlt->output_offset
4101 + htab->sbrlt->output_section->vma);
4102 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
4103 rela.r_addend = off;
4104
4105 r = (Elf64_External_Rela *) htab->srelbrlt->contents;
4106 r += htab->srelbrlt->reloc_count++;
4107 bfd_elf64_swap_reloca_out (htab->srelbrlt->owner, &rela, r);
4108 }
4109
4110 off = (br_entry->offset
4111 + htab->sbrlt->output_offset
4112 + htab->sbrlt->output_section->vma
4113 - elf_gp (htab->sbrlt->output_section->owner)
4114 - TOC_BASE_OFF);
4115
4116 if (off + 0x80000000 > 0xffffffff || (off & 7) != 0)
5d1634d7
AM
4117 {
4118 (*_bfd_error_handler)
e86ce104 4119 (_("linkage table error against `%s'"),
721956f4 4120 stub_entry->root.string);
5d1634d7 4121 bfd_set_error (bfd_error_bad_value);
721956f4 4122 htab->stub_error = true;
5d1634d7
AM
4123 return false;
4124 }
41bd81ab 4125
721956f4
AM
4126 indx = off;
4127 bfd_put_32 (stub_bfd, (bfd_vma) ADDIS_R12_R2 | PPC_HA (indx), loc);
4128 bfd_put_32 (stub_bfd, (bfd_vma) LD_R11_0R12 | PPC_LO (indx), loc + 4);
4129 bfd_put_32 (stub_bfd, (bfd_vma) MTCTR_R11, loc + 8);
4130 bfd_put_32 (stub_bfd, (bfd_vma) BCTR, loc + 12);
4131 size = 16;
4132 break;
5d1634d7 4133
721956f4 4134 case ppc_stub_plt_call:
5d1634d7 4135 /* Build the .glink lazy link call stub. */
721956f4
AM
4136 p = htab->sglink->contents + htab->sglink->_cooked_size;
4137 indx = htab->sglink->reloc_count;
5d1634d7
AM
4138 if (indx < 0x8000)
4139 {
721956f4 4140 bfd_put_32 (htab->sglink->owner, LI_R0_0 | indx, p);
5d1634d7
AM
4141 p += 4;
4142 }
4143 else
4144 {
721956f4 4145 bfd_put_32 (htab->sglink->owner, LIS_R0_0 | PPC_HI (indx), p);
5d1634d7 4146 p += 4;
721956f4 4147 bfd_put_32 (htab->sglink->owner, ORI_R0_R0_0 | PPC_LO (indx), p);
5d1634d7
AM
4148 p += 4;
4149 }
721956f4
AM
4150 bfd_put_32 (htab->sglink->owner,
4151 B_DOT | ((htab->sglink->contents - p) & 0x3fffffc), p);
5d1634d7 4152 p += 4;
721956f4
AM
4153 htab->sglink->_cooked_size = p - htab->sglink->contents;
4154 htab->sglink->reloc_count += 1;
4155
c862ae31
AM
4156 /* Do the best we can for shared libraries built without
4157 exporting ".foo" for each "foo". This can happen when symbol
4158 versioning scripts strip all bar a subset of symbols. */
4159 if (stub_entry->h->oh->root.type != bfd_link_hash_defined
4160 && stub_entry->h->oh->root.type != bfd_link_hash_defweak)
4161 {
4162 /* Point the symbol at the stub. There may be multiple stubs,
4163 we don't really care; The main thing is to make this sym
4164 defined somewhere. */
4165 stub_entry->h->oh->root.type = bfd_link_hash_defined;
4166 stub_entry->h->oh->root.u.def.section = stub_entry->stub_sec;
4167 stub_entry->h->oh->root.u.def.value = stub_entry->stub_offset;
4168 }
4169
721956f4
AM
4170 /* Now build the stub. */
4171 off = stub_entry->h->elf.plt.offset;
4172 if (off >= (bfd_vma) -2)
4173 abort ();
4174
4175 off &= ~ (bfd_vma) 1;
4176 off += (htab->splt->output_offset
4177 + htab->splt->output_section->vma
4178 - elf_gp (htab->splt->output_section->owner)
4179 - TOC_BASE_OFF);
4180
4181 if (off + 0x80000000 > 0xffffffff || (off & 7) != 0)
4182 {
4183 (*_bfd_error_handler)
4184 (_("linkage table error against `%s'"),
4185 stub_entry->h->elf.root.root.string);
4186 bfd_set_error (bfd_error_bad_value);
4187 htab->stub_error = true;
4188 return false;
4189 }
4190
4191 p = build_plt_stub (stub_bfd, loc, (int) off, 0);
4192 size = p - loc;
4193 break;
4194
4195 default:
4196 BFD_FAIL ();
4197 return false;
4198 }
4199
4200 stub_sec->_cooked_size += size;
4201 return true;
4202}
4203
4204/* As above, but don't actually build the stub. Just bump offset so
4205 we know stub section sizes, and select plt_branch stubs where
4206 long_branch stubs won't do. */
4207
4208static boolean
4209ppc_size_one_stub (gen_entry, in_arg)
4210 struct bfd_hash_entry *gen_entry;
4211 PTR in_arg;
4212{
4213 struct ppc_stub_hash_entry *stub_entry;
4214 struct ppc_link_hash_table *htab;
4215 bfd_vma off;
4216 int size;
4217
4218 /* Massage our args to the form they really have. */
4219 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
4220 htab = (struct ppc_link_hash_table *) in_arg;
4221
4222 if (stub_entry->stub_type == ppc_stub_plt_call)
4223 {
4224 off = stub_entry->h->elf.plt.offset & ~(bfd_vma) 1;
4225 off += (htab->splt->output_offset
4226 + htab->splt->output_section->vma
4227 - elf_gp (htab->splt->output_section->owner)
4228 - TOC_BASE_OFF);
4229
4230 size = 28;
4231 if (PPC_HA ((int) off + 16) != PPC_HA ((int) off))
4232 size += 4;
4233 }
4234 else
4235 {
4236 /* ppc_stub_long_branch or ppc_stub_plt_branch. */
4237 stub_entry->stub_type = ppc_stub_long_branch;
4238 size = 4;
4239
4240 off = (stub_entry->target_value
4241 + stub_entry->target_section->output_offset
4242 + stub_entry->target_section->output_section->vma);
4243 off -= (stub_entry->stub_sec->_raw_size
4244 + stub_entry->stub_sec->output_offset
4245 + stub_entry->stub_sec->output_section->vma);
4246
4247 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
4248 {
4249 struct ppc_branch_hash_entry *br_entry;
4250
4251 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
4252 stub_entry->root.string + 9,
4253 true, false);
4254 if (br_entry == NULL)
4255 {
4256 (*_bfd_error_handler) (_("can't build branch stub `%s'"),
4257 stub_entry->root.string + 9);
4258 htab->stub_error = true;
4259 return false;
4260 }
4261
4262 if (br_entry->iter != htab->stub_iteration)
4263 {
4264 br_entry->iter = htab->stub_iteration;
4265 br_entry->offset = htab->sbrlt->_raw_size;
4266 htab->sbrlt->_raw_size += 8;
4267 }
4268 stub_entry->stub_type = ppc_stub_plt_branch;
4269 size = 16;
4270 }
4271 }
4272
4273 stub_entry->stub_sec->_raw_size += size;
4274 return true;
4275}
4276
4277/* Set up various things so that we can make a list of input sections
4278 for each output section included in the link. Returns -1 on error,
4279 0 when no stubs will be needed, and 1 on success. */
4280
4281int
4282ppc64_elf_setup_section_lists (output_bfd, info)
4283 bfd *output_bfd;
4284 struct bfd_link_info *info;
4285{
4286 bfd *input_bfd;
4287 unsigned int bfd_count;
4288 int top_id, top_index;
4289 asection *section;
4290 asection **input_list, **list;
4291 bfd_size_type amt;
4292 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4293
805fc799
AM
4294 if (htab->elf.root.creator->flavour != bfd_target_elf_flavour
4295 || htab->sbrlt == NULL)
721956f4
AM
4296 return 0;
4297
4298 /* Count the number of input BFDs and find the top input section id. */
4299 for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
4300 input_bfd != NULL;
4301 input_bfd = input_bfd->link_next)
4302 {
4303 bfd_count += 1;
4304 for (section = input_bfd->sections;
4305 section != NULL;
4306 section = section->next)
4307 {
4308 if (top_id < section->id)
4309 top_id = section->id;
4310 }
4311 }
4312 htab->bfd_count = bfd_count;
4313
4314 amt = sizeof (struct map_stub) * (top_id + 1);
4315 htab->stub_group = (struct map_stub *) bfd_zmalloc (amt);
4316 if (htab->stub_group == NULL)
4317 return -1;
4318
4319 /* We can't use output_bfd->section_count here to find the top output
4320 section index as some sections may have been removed, and
4321 _bfd_strip_section_from_output doesn't renumber the indices. */
4322 for (section = output_bfd->sections, top_index = 0;
4323 section != NULL;
4324 section = section->next)
4325 {
4326 if (top_index < section->index)
4327 top_index = section->index;
4328 }
4329
4330 htab->top_index = top_index;
4331 amt = sizeof (asection *) * (top_index + 1);
4332 input_list = (asection **) bfd_malloc (amt);
4333 htab->input_list = input_list;
4334 if (input_list == NULL)
4335 return -1;
4336
4337 /* For sections we aren't interested in, mark their entries with a
4338 value we can check later. */
4339 list = input_list + top_index;
4340 do
4341 *list = bfd_abs_section_ptr;
4342 while (list-- != input_list);
4343
4344 for (section = output_bfd->sections;
4345 section != NULL;
4346 section = section->next)
4347 {
4348 if ((section->flags & SEC_CODE) != 0)
4349 input_list[section->index] = NULL;
4350 }
4351
4352 return 1;
4353}
4354
4355/* The linker repeatedly calls this function for each input section,
4356 in the order that input sections are linked into output sections.
4357 Build lists of input sections to determine groupings between which
4358 we may insert linker stubs. */
4359
4360void
805fc799 4361ppc64_elf_next_input_section (info, isec)
721956f4
AM
4362 struct bfd_link_info *info;
4363 asection *isec;
4364{
4365 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4366
805fc799 4367 if (isec->output_section->index <= htab->top_index)
721956f4
AM
4368 {
4369 asection **list = htab->input_list + isec->output_section->index;
4370 if (*list != bfd_abs_section_ptr)
4371 {
4372 /* Steal the link_sec pointer for our list. */
4373#define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
4374 /* This happens to make the list in reverse order,
4375 which is what we want. */
4376 PREV_SEC (isec) = *list;
4377 *list = isec;
4378 }
4379 }
4380}
4381
4382/* See whether we can group stub sections together. Grouping stub
4383 sections may result in fewer stubs. More importantly, we need to
4384 put all .init* and .fini* stubs at the beginning of the .init or
4385 .fini output sections respectively, because glibc splits the
4386 _init and _fini functions into multiple parts. Putting a stub in
4387 the middle of a function is not a good idea. */
4388
4389static void
4390group_sections (htab, stub_group_size, stubs_always_before_branch)
4391 struct ppc_link_hash_table *htab;
4392 bfd_size_type stub_group_size;
4393 boolean stubs_always_before_branch;
4394{
4395 asection **list = htab->input_list + htab->top_index;
4396 do
4397 {
4398 asection *tail = *list;
4399 if (tail == bfd_abs_section_ptr)
4400 continue;
4401 while (tail != NULL)
4402 {
4403 asection *curr;
4404 asection *prev;
4405 bfd_size_type total;
4406
4407 curr = tail;
4408 if (tail->_cooked_size)
4409 total = tail->_cooked_size;
4410 else
4411 total = tail->_raw_size;
4412 while ((prev = PREV_SEC (curr)) != NULL
4413 && ((total += curr->output_offset - prev->output_offset)
4414 < stub_group_size))
4415 curr = prev;
4416
4417 /* OK, the size from the start of CURR to the end is less
4418 than stub_group_size and thus can be handled by one stub
4419 section. (or the tail section is itself larger than
4420 stub_group_size, in which case we may be toast.) We
4421 should really be keeping track of the total size of stubs
4422 added here, as stubs contribute to the final output
4423 section size. That's a little tricky, and this way will
4424 only break if stubs added make the total size more than
4425 2^25, ie. for the default stub_group_size, if stubs total
4426 more than 2834432 bytes, or over 100000 plt call stubs. */
4427 do
4428 {
4429 prev = PREV_SEC (tail);
4430 /* Set up this stub group. */
4431 htab->stub_group[tail->id].link_sec = curr;
4432 }
4433 while (tail != curr && (tail = prev) != NULL);
4434
4435 /* But wait, there's more! Input sections up to stub_group_size
4436 bytes before the stub section can be handled by it too. */
4437 if (!stubs_always_before_branch)
4438 {
4439 total = 0;
4440 while (prev != NULL
4441 && ((total += tail->output_offset - prev->output_offset)
4442 < stub_group_size))
4443 {
4444 tail = prev;
4445 prev = PREV_SEC (tail);
4446 htab->stub_group[tail->id].link_sec = curr;
4447 }
4448 }
4449 tail = prev;
4450 }
4451 }
4452 while (list-- != htab->input_list);
4453 free (htab->input_list);
4454#undef PREV_SEC
4455}
4456
4457/* Read in all local syms for all input bfds. */
4458
4459static boolean
4460get_local_syms (input_bfd, htab)
4461 bfd *input_bfd;
4462 struct ppc_link_hash_table *htab;
4463{
4464 unsigned int bfd_indx;
4465 Elf_Internal_Sym *local_syms, **all_local_syms;
4466
4467 /* We want to read in symbol extension records only once. To do this
4468 we need to read in the local symbols in parallel and save them for
4469 later use; so hold pointers to the local symbols in an array. */
4470 bfd_size_type amt = sizeof (Elf_Internal_Sym *) * htab->bfd_count;
4471 all_local_syms = (Elf_Internal_Sym **) bfd_zmalloc (amt);
4472 htab->all_local_syms = all_local_syms;
4473 if (all_local_syms == NULL)
4474 return false;
4475
4476 /* Walk over all the input BFDs, swapping in local symbols.
4477 If we are creating a shared library, create hash entries for the
4478 export stubs. */
4479 for (bfd_indx = 0;
4480 input_bfd != NULL;
4481 input_bfd = input_bfd->link_next, bfd_indx++)
4482 {
4483 Elf_Internal_Shdr *symtab_hdr;
4484 Elf_Internal_Shdr *shndx_hdr;
4485 Elf_Internal_Sym *isym;
4486 Elf64_External_Sym *ext_syms, *esym, *end_sy;
4487 Elf_External_Sym_Shndx *shndx_buf, *shndx;
4488 bfd_size_type sec_size;
4489
4490 /* We'll need the symbol table in a second. */
4491 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4492 if (symtab_hdr->sh_info == 0)
4493 continue;
4494
4495 /* We need an array of the local symbols attached to the input bfd.
4496 Unfortunately, we're going to have to read & swap them in. */
4497 sec_size = symtab_hdr->sh_info;
4498 sec_size *= sizeof (Elf_Internal_Sym);
4499 local_syms = (Elf_Internal_Sym *) bfd_malloc (sec_size);
4500 if (local_syms == NULL)
4501 return false;
4502
4503 all_local_syms[bfd_indx] = local_syms;
4504 sec_size = symtab_hdr->sh_info;
4505 sec_size *= sizeof (Elf64_External_Sym);
4506 ext_syms = (Elf64_External_Sym *) bfd_malloc (sec_size);
4507 if (ext_syms == NULL)
4508 return false;
4509
4510 if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
4511 || bfd_bread ((PTR) ext_syms, sec_size, input_bfd) != sec_size)
4512 {
4513 error_ret_free_ext_syms:
4514 free (ext_syms);
4515 return false;
4516 }
4517
4518 shndx_buf = NULL;
4519 shndx_hdr = &elf_tdata (input_bfd)->symtab_shndx_hdr;
4520 if (shndx_hdr->sh_size != 0)
4521 {
4522 sec_size = symtab_hdr->sh_info;
4523 sec_size *= sizeof (Elf_External_Sym_Shndx);
4524 shndx_buf = (Elf_External_Sym_Shndx *) bfd_malloc (sec_size);
4525 if (shndx_buf == NULL)
4526 goto error_ret_free_ext_syms;
4527
4528 if (bfd_seek (input_bfd, shndx_hdr->sh_offset, SEEK_SET) != 0
4529 || bfd_bread ((PTR) shndx_buf, sec_size, input_bfd) != sec_size)
4530 {
4531 free (shndx_buf);
4532 goto error_ret_free_ext_syms;
4533 }
4534 }
4535
4536 /* Swap the local symbols in. */
4537 for (esym = ext_syms, end_sy = esym + symtab_hdr->sh_info,
4538 isym = local_syms, shndx = shndx_buf;
4539 esym < end_sy;
4540 esym++, isym++, shndx = (shndx ? shndx + 1 : NULL))
4541 bfd_elf64_swap_symbol_in (input_bfd, esym, shndx, isym);
4542
4543 /* Now we can free the external symbols. */
4544 free (shndx_buf);
4545 free (ext_syms);
5bd4f169 4546 }
721956f4 4547
5d1634d7
AM
4548 return true;
4549}
5bd4f169 4550
721956f4
AM
4551/* Determine and set the size of the stub section for a final link.
4552
4553 The basic idea here is to examine all the relocations looking for
4554 PC-relative calls to a target that is unreachable with a "bl"
4555 instruction. */
4556
4557boolean
4558ppc64_elf_size_stubs (output_bfd, stub_bfd, info, group_size,
4559 add_stub_section, layout_sections_again)
4560 bfd *output_bfd;
4561 bfd *stub_bfd;
4562 struct bfd_link_info *info;
4563 bfd_signed_vma group_size;
4564 asection * (*add_stub_section) PARAMS ((const char *, asection *));
4565 void (*layout_sections_again) PARAMS ((void));
4566{
4567 bfd_size_type stub_group_size;
4568 boolean stubs_always_before_branch;
4569 boolean ret = false;
4570 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4571
4572 /* Stash our params away. */
4573 htab->stub_bfd = stub_bfd;
4574 htab->add_stub_section = add_stub_section;
4575 htab->layout_sections_again = layout_sections_again;
4576 stubs_always_before_branch = group_size < 0;
4577 if (group_size < 0)
4578 stub_group_size = -group_size;
4579 else
4580 stub_group_size = group_size;
4581 if (stub_group_size == 1)
4582 {
4583 /* Default values. */
4584 stub_group_size = 30720000;
4585 if (htab->has_14bit_branch)
4586 stub_group_size = 30000;
4587 }
4588
4589 group_sections (htab, stub_group_size, stubs_always_before_branch);
4590
4591 if (! get_local_syms (info->input_bfds, htab))
4592 {
4593 if (htab->all_local_syms)
4594 goto error_ret_free_local;
4595 return false;
4596 }
4597
4598 while (1)
4599 {
4600 bfd *input_bfd;
4601 unsigned int bfd_indx;
4602 asection *stub_sec;
4603 boolean stub_changed;
4604
4605 htab->stub_iteration += 1;
4606 stub_changed = false;
4607
4608 for (input_bfd = info->input_bfds, bfd_indx = 0;
4609 input_bfd != NULL;
4610 input_bfd = input_bfd->link_next, bfd_indx++)
4611 {
4612 Elf_Internal_Shdr *symtab_hdr;
4613 asection *section;
4614 Elf_Internal_Sym *local_syms;
4615
4616 /* We'll need the symbol table in a second. */
4617 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4618 if (symtab_hdr->sh_info == 0)
4619 continue;
4620
4621 local_syms = htab->all_local_syms[bfd_indx];
4622
4623 /* Walk over each section attached to the input bfd. */
4624 for (section = input_bfd->sections;
4625 section != NULL;
4626 section = section->next)
4627 {
4628 Elf_Internal_Shdr *input_rel_hdr;
4629 Elf64_External_Rela *external_relocs, *erelaend, *erela;
4630 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
4631 bfd_size_type amt;
4632
4633 /* If there aren't any relocs, then there's nothing more
4634 to do. */
4635 if ((section->flags & SEC_RELOC) == 0
4636 || section->reloc_count == 0)
4637 continue;
4638
4639 /* If this section is a link-once section that will be
4640 discarded, then don't create any stubs. */
4641 if (section->output_section == NULL
4642 || section->output_section->owner != output_bfd)
4643 continue;
4644
4645 /* Allocate space for the external relocations. */
4646 amt = section->reloc_count;
4647 amt *= sizeof (Elf64_External_Rela);
4648 external_relocs = (Elf64_External_Rela *) bfd_malloc (amt);
4649 if (external_relocs == NULL)
4650 {
4651 goto error_ret_free_local;
4652 }
4653
4654 /* Likewise for the internal relocations. */
4655 amt = section->reloc_count;
4656 amt *= sizeof (Elf_Internal_Rela);
4657 internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
4658 if (internal_relocs == NULL)
4659 {
4660 free (external_relocs);
4661 goto error_ret_free_local;
4662 }
4663
4664 /* Read in the external relocs. */
4665 input_rel_hdr = &elf_section_data (section)->rel_hdr;
4666 if (bfd_seek (input_bfd, input_rel_hdr->sh_offset, SEEK_SET) != 0
4667 || bfd_bread ((PTR) external_relocs,
4668 input_rel_hdr->sh_size,
4669 input_bfd) != input_rel_hdr->sh_size)
4670 {
4671 free (external_relocs);
4672 error_ret_free_internal:
4673 free (internal_relocs);
4674 goto error_ret_free_local;
4675 }
4676
4677 /* Swap in the relocs. */
4678 erela = external_relocs;
4679 erelaend = erela + section->reloc_count;
4680 irela = internal_relocs;
4681 for (; erela < erelaend; erela++, irela++)
4682 bfd_elf64_swap_reloca_in (input_bfd, erela, irela);
4683
4684 /* We're done with the external relocs, free them. */
4685 free (external_relocs);
4686
4687 /* Now examine each relocation. */
4688 irela = internal_relocs;
4689 irelaend = irela + section->reloc_count;
4690 for (; irela < irelaend; irela++)
4691 {
4692 unsigned int r_type, r_indx;
4693 enum ppc_stub_type stub_type;
4694 struct ppc_stub_hash_entry *stub_entry;
4695 asection *sym_sec;
4696 bfd_vma sym_value;
4697 bfd_vma destination;
4698 struct ppc_link_hash_entry *hash;
4699 char *stub_name;
4700 const asection *id_sec;
4701
4702 r_type = ELF64_R_TYPE (irela->r_info);
4703 r_indx = ELF64_R_SYM (irela->r_info);
4704
4705 if (r_type >= (unsigned int) R_PPC_max)
4706 {
4707 bfd_set_error (bfd_error_bad_value);
4708 goto error_ret_free_internal;
4709 }
4710
4711 /* Only look for stubs on branch instructions. */
4712 if (r_type != (unsigned int) R_PPC64_REL24
4713 && r_type != (unsigned int) R_PPC64_REL14
4714 && r_type != (unsigned int) R_PPC64_REL14_BRTAKEN
4715 && r_type != (unsigned int) R_PPC64_REL14_BRNTAKEN)
4716 continue;
4717
4718 /* Now determine the call target, its name, value,
4719 section. */
4720 sym_sec = NULL;
4721 sym_value = 0;
4722 destination = 0;
4723 hash = NULL;
4724 if (r_indx < symtab_hdr->sh_info)
4725 {
4726 /* It's a local symbol. */
4727 Elf_Internal_Sym *sym;
4728 Elf_Internal_Shdr *hdr;
4729
4730 sym = local_syms + r_indx;
4731 hdr = elf_elfsections (input_bfd)[sym->st_shndx];
4732 sym_sec = hdr->bfd_section;
4733 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
4734 sym_value = sym->st_value;
4735 destination = (sym_value + irela->r_addend
4736 + sym_sec->output_offset
4737 + sym_sec->output_section->vma);
4738 }
4739 else
4740 {
4741 /* It's an external symbol. */
4742 int e_indx;
4743
4744 e_indx = r_indx - symtab_hdr->sh_info;
4745 hash = ((struct ppc_link_hash_entry *)
4746 elf_sym_hashes (input_bfd)[e_indx]);
4747
4748 while (hash->elf.root.type == bfd_link_hash_indirect
4749 || hash->elf.root.type == bfd_link_hash_warning)
4750 hash = ((struct ppc_link_hash_entry *)
4751 hash->elf.root.u.i.link);
4752
4753 if (hash->elf.root.type == bfd_link_hash_defined
4754 || hash->elf.root.type == bfd_link_hash_defweak)
4755 {
4756 sym_sec = hash->elf.root.u.def.section;
4757 sym_value = hash->elf.root.u.def.value;
4758 if (sym_sec->output_section != NULL)
4759 destination = (sym_value + irela->r_addend
4760 + sym_sec->output_offset
4761 + sym_sec->output_section->vma);
4762 }
4763 else if (hash->elf.root.type == bfd_link_hash_undefweak)
4764 ;
4765 else if (hash->elf.root.type == bfd_link_hash_undefined)
4766 ;
4767 else
4768 {
4769 bfd_set_error (bfd_error_bad_value);
4770 goto error_ret_free_internal;
4771 }
4772 }
4773
4774 /* Determine what (if any) linker stub is needed. */
4775 stub_type = ppc_type_of_stub (section, irela, &hash,
4776 destination);
4777 if (stub_type == ppc_stub_none)
4778 continue;
4779
4780 /* Support for grouping stub sections. */
4781 id_sec = htab->stub_group[section->id].link_sec;
4782
4783 /* Get the name of this stub. */
4784 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
4785 if (!stub_name)
4786 goto error_ret_free_internal;
4787
4788 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
4789 stub_name, false, false);
4790 if (stub_entry != NULL)
4791 {
4792 /* The proper stub has already been created. */
4793 free (stub_name);
4794 continue;
4795 }
4796
4797 stub_entry = ppc_add_stub (stub_name, section, htab);
4798 if (stub_entry == NULL)
4799 {
4800 free (stub_name);
4801 goto error_ret_free_local;
4802 }
4803
4804 stub_entry->target_value = sym_value;
4805 stub_entry->target_section = sym_sec;
4806 stub_entry->stub_type = stub_type;
4807 stub_entry->h = hash;
4808 stub_changed = true;
4809 }
4810
4811 /* We're done with the internal relocs, free them. */
4812 free (internal_relocs);
4813 }
4814 }
4815
4816 if (!stub_changed)
4817 break;
4818
4819 /* OK, we've added some stubs. Find out the new size of the
4820 stub sections. */
4821 for (stub_sec = htab->stub_bfd->sections;
4822 stub_sec != NULL;
4823 stub_sec = stub_sec->next)
4824 {
4825 stub_sec->_raw_size = 0;
4826 stub_sec->_cooked_size = 0;
4827 }
4828 htab->sbrlt->_raw_size = 0;
4829 htab->sbrlt->_cooked_size = 0;
4830
4831 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, htab);
4832
4833 /* Ask the linker to do its stuff. */
4834 (*htab->layout_sections_again) ();
4835 }
4836
4837 if (htab->sbrlt->_raw_size == 0)
4838 {
4839 _bfd_strip_section_from_output (info, htab->sbrlt);
4840 if (htab->srelbrlt != NULL)
4841 _bfd_strip_section_from_output (info, htab->srelbrlt);
4842 }
4843
4844 ret = true;
4845
4846 error_ret_free_local:
4847 while (htab->bfd_count-- > 0)
4848 if (htab->all_local_syms[htab->bfd_count])
4849 free (htab->all_local_syms[htab->bfd_count]);
4850 free (htab->all_local_syms);
4851
4852 return ret;
4853}
4854
4855/* Called after we have determined section placement. If sections
805fc799 4856 move, we'll be called again. Provide a value for TOCstart. */
721956f4 4857
805fc799
AM
4858bfd_vma
4859ppc64_elf_toc (obfd)
5d1634d7 4860 bfd *obfd;
721956f4 4861{
805fc799
AM
4862 asection *s;
4863 bfd_vma TOCstart;
721956f4 4864
805fc799
AM
4865 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
4866 order. The TOC starts where the first of these sections starts. */
4867 s = bfd_get_section_by_name (obfd, ".got");
4868 if (s == NULL)
4869 s = bfd_get_section_by_name (obfd, ".toc");
4870 if (s == NULL)
4871 s = bfd_get_section_by_name (obfd, ".tocbss");
4872 if (s == NULL)
4873 s = bfd_get_section_by_name (obfd, ".plt");
4874 if (s == NULL)
4875 {
4876 /* This may happen for
4877 o references to TOC base (SYM@toc / TOC[tc0]) without a
4878 .toc directive
4879 o bad linker script
4880 o --gc-sections and empty TOC sections
4881
4882 FIXME: Warn user? */
4883
4884 /* Look for a likely section. We probably won't even be
4885 using TOCstart. */
4886 for (s = obfd->sections; s != NULL; s = s->next)
4887 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY))
4888 == (SEC_ALLOC | SEC_SMALL_DATA))
4889 break;
721956f4 4890 if (s == NULL)
805fc799
AM
4891 for (s = obfd->sections; s != NULL; s = s->next)
4892 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA))
4893 == (SEC_ALLOC | SEC_SMALL_DATA))
4894 break;
721956f4 4895 if (s == NULL)
805fc799
AM
4896 for (s = obfd->sections; s != NULL; s = s->next)
4897 if ((s->flags & (SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
4898 break;
721956f4 4899 if (s == NULL)
805fc799
AM
4900 for (s = obfd->sections; s != NULL; s = s->next)
4901 if ((s->flags & SEC_ALLOC) == SEC_ALLOC)
4902 break;
4903 }
721956f4 4904
805fc799
AM
4905 TOCstart = 0;
4906 if (s != NULL)
4907 TOCstart = s->output_section->vma + s->output_offset;
721956f4 4908
805fc799 4909 return TOCstart;
721956f4
AM
4910}
4911
4912/* Build all the stubs associated with the current output file.
4913 The stubs are kept in a hash table attached to the main linker
4914 hash table. This function is called via gldelf64ppc_finish. */
4915
4916boolean
4917ppc64_elf_build_stubs (info)
4918 struct bfd_link_info *info;
5d1634d7
AM
4919{
4920 struct ppc_link_hash_table *htab = ppc_hash_table (info);
721956f4 4921 asection *stub_sec;
5d1634d7
AM
4922 bfd_vma plt_r2;
4923 bfd_byte *p;
4924
721956f4
AM
4925 for (stub_sec = htab->stub_bfd->sections;
4926 stub_sec != NULL;
4927 stub_sec = stub_sec->next)
4928 {
4929 bfd_size_type size;
4930
4931 /* Allocate memory to hold the linker stubs. */
4932 size = stub_sec->_raw_size;
4933 if (size != 0)
4934 {
4935 stub_sec->contents = (bfd_byte *) bfd_zalloc (htab->stub_bfd, size);
4936 if (stub_sec->contents == NULL)
4937 return false;
4938 }
4939 stub_sec->_cooked_size = 0;
4940 }
5d1634d7 4941
721956f4 4942 if (htab->splt != NULL)
5d1634d7 4943 {
721956f4
AM
4944 /* Build the .glink plt call stub. */
4945 plt_r2 = (htab->splt->output_offset
4946 + htab->splt->output_section->vma
4947 - elf_gp (htab->splt->output_section->owner)
4948 - TOC_BASE_OFF);
4949 p = htab->sglink->contents;
4950 p = build_plt_stub (htab->sglink->owner, p, (int) plt_r2, 1);
4951 while (p - htab->sglink->contents < GLINK_CALL_STUB_SIZE)
4952 {
4953 bfd_put_32 (htab->sglink->owner, NOP, p);
4954 p += 4;
4955 }
4956 htab->sglink->_cooked_size = p - htab->sglink->contents;
4957
4958 /* Use reloc_count to count entries. */
4959 htab->sglink->reloc_count = 0;
5d1634d7 4960 }
5d1634d7 4961
721956f4
AM
4962 if (htab->sbrlt->_raw_size != 0)
4963 {
4964 htab->sbrlt->contents = (bfd_byte *) bfd_zalloc (htab->sbrlt->owner,
4965 htab->sbrlt->_raw_size);
4966 if (htab->sbrlt->contents == NULL)
4967 return false;
4968 }
5d1634d7 4969
721956f4
AM
4970 /* Build the stubs as directed by the stub hash table. */
4971 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
5d1634d7
AM
4972 htab->sglink->reloc_count = 0;
4973
721956f4
AM
4974 for (stub_sec = htab->stub_bfd->sections;
4975 stub_sec != NULL;
4976 stub_sec = stub_sec->next)
4977 {
4978 if (stub_sec->_raw_size != stub_sec->_cooked_size)
4979 break;
4980 }
5d1634d7 4981
721956f4 4982 if (stub_sec != NULL
5d1634d7
AM
4983 || htab->sglink->_raw_size != htab->sglink->_cooked_size)
4984 {
721956f4
AM
4985 htab->stub_error = true;
4986 (*_bfd_error_handler) (_("stubs don't match calculated size"));
5d1634d7 4987 }
721956f4
AM
4988
4989 return !htab->stub_error;
5bd4f169
AM
4990}
4991
4992/* Set up any other section flags and such that may be necessary. */
4993
4994static boolean
4995ppc64_elf_fake_sections (abfd, shdr, asect)
4996 bfd *abfd ATTRIBUTE_UNUSED;
4997 Elf64_Internal_Shdr *shdr;
4998 asection *asect;
4999{
5000 if ((asect->flags & SEC_EXCLUDE) != 0)
5001 shdr->sh_flags |= SHF_EXCLUDE;
5002
5003 if ((asect->flags & SEC_SORT_ENTRIES) != 0)
5004 shdr->sh_type = SHT_ORDERED;
5005
5006 return true;
5007}
5008
5009/* The RELOCATE_SECTION function is called by the ELF backend linker
5010 to handle the relocations for a section.
5011
5012 The relocs are always passed as Rela structures; if the section
5013 actually uses Rel structures, the r_addend field will always be
5014 zero.
5015
5016 This function is responsible for adjust the section contents as
5017 necessary, and (if using Rela relocs and generating a
5018 relocateable output file) adjusting the reloc addend as
5019 necessary.
5020
5021 This function does not have to worry about setting the reloc
5022 address or the reloc symbol index.
5023
5024 LOCAL_SYMS is a pointer to the swapped in local symbols.
5025
5026 LOCAL_SECTIONS is an array giving the section in the input file
5027 corresponding to the st_shndx field of each local symbol.
5028
5029 The global hash table entry for the global symbols can be found
5030 via elf_sym_hashes (input_bfd).
5031
5032 When generating relocateable output, this function must handle
5033 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
5034 going to be the section symbol corresponding to the output
5035 section, which means that the addend must be adjusted
5036 accordingly. */
5037
5038static boolean
5039ppc64_elf_relocate_section (output_bfd, info, input_bfd, input_section,
5040 contents, relocs, local_syms, local_sections)
5041 bfd *output_bfd;
5042 struct bfd_link_info *info;
5043 bfd *input_bfd;
5044 asection *input_section;
5045 bfd_byte *contents;
5046 Elf_Internal_Rela *relocs;
5047 Elf_Internal_Sym *local_syms;
5048 asection **local_sections;
5049{
65f38f15 5050 struct ppc_link_hash_table *htab;
5bd4f169
AM
5051 Elf_Internal_Shdr *symtab_hdr;
5052 struct elf_link_hash_entry **sym_hashes;
5bd4f169
AM
5053 Elf_Internal_Rela *rel;
5054 Elf_Internal_Rela *relend;
5bd4f169
AM
5055 bfd_vma *local_got_offsets;
5056 bfd_vma TOCstart;
5057 boolean ret = true;
41bd81ab 5058 boolean is_opd;
96e0dda4
AM
5059 /* Disabled until we sort out how ld should choose 'y' vs 'at'. */
5060 boolean is_power4 = false;
5bd4f169 5061
b491616a
AM
5062 if (info->relocateable)
5063 return true;
5064
65f38f15 5065 /* Initialize howto table if needed. */
5bd4f169 5066 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
5067 ppc_howto_init ();
5068
65f38f15 5069 htab = ppc_hash_table (info);
5bd4f169 5070 local_got_offsets = elf_local_got_offsets (input_bfd);
5bd4f169 5071 TOCstart = elf_gp (output_bfd);
5bd4f169
AM
5072 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5073 sym_hashes = elf_sym_hashes (input_bfd);
41bd81ab 5074 is_opd = strcmp (bfd_get_section_name (abfd, input_section), ".opd") == 0;
65f38f15 5075
5bd4f169
AM
5076 rel = relocs;
5077 relend = relocs + input_section->reloc_count;
5078 for (; rel < relend; rel++)
5079 {
5080 enum elf_ppc_reloc_type r_type;
5081 bfd_vma offset;
5082 bfd_vma addend;
5083 bfd_reloc_status_type r;
5084 Elf_Internal_Sym *sym;
5085 asection *sec;
5086 struct elf_link_hash_entry *h;
721956f4 5087 struct elf_link_hash_entry *fdh;
5bd4f169 5088 const char *sym_name;
5bd4f169
AM
5089 unsigned long r_symndx;
5090 bfd_vma relocation;
65f38f15 5091 boolean unresolved_reloc;
ef60b7ff 5092 boolean warned;
dc810e39 5093 long insn;
721956f4
AM
5094 struct ppc_stub_hash_entry *stub_entry;
5095 bfd_vma max_br_offset;
5096 bfd_vma from;
5bd4f169
AM
5097
5098 r_type = (enum elf_ppc_reloc_type) ELF64_R_TYPE (rel->r_info);
5bd4f169 5099 r_symndx = ELF64_R_SYM (rel->r_info);
65f38f15
AM
5100 offset = rel->r_offset;
5101 addend = rel->r_addend;
5102 r = bfd_reloc_other;
5103 sym = (Elf_Internal_Sym *) 0;
5104 sec = (asection *) 0;
5105 h = (struct elf_link_hash_entry *) 0;
5106 sym_name = (const char *) 0;
5107 unresolved_reloc = false;
ef60b7ff 5108 warned = false;
65f38f15 5109
5bd4f169
AM
5110 if (r_type == R_PPC64_TOC)
5111 {
5112 /* Relocation value is TOC base. Symbol is ignored. */
5113 relocation = TOCstart + TOC_BASE_OFF;
5114 }
5115 else if (r_symndx < symtab_hdr->sh_info)
5116 {
5117 /* It's a local symbol. */
5118 sym = local_syms + r_symndx;
5119 sec = local_sections[r_symndx];
5120 sym_name = "<local symbol>";
5121
f8df10f4 5122 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, sec, rel);
f46c2da6
AM
5123 /* rel may have changed, update our copy of addend. */
5124 addend = rel->r_addend;
5bd4f169
AM
5125 }
5126 else
5127 {
5128 /* It's a global symbol. */
5129 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5130 while (h->root.type == bfd_link_hash_indirect
5131 || h->root.type == bfd_link_hash_warning)
5132 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5133 sym_name = h->root.root.string;
65f38f15 5134 relocation = 0;
5bd4f169
AM
5135 if (h->root.type == bfd_link_hash_defined
5136 || h->root.type == bfd_link_hash_defweak)
5137 {
5138 sec = h->root.u.def.section;
65f38f15
AM
5139 if (sec->output_section == NULL)
5140 /* Set a flag that will be cleared later if we find a
5141 relocation value for this symbol. output_section
5142 is typically NULL for symbols satisfied by a shared
5143 library. */
5144 unresolved_reloc = true;
5bd4f169
AM
5145 else
5146 relocation = (h->root.u.def.value
5147 + sec->output_section->vma
5148 + sec->output_offset);
5149 }
5150 else if (h->root.type == bfd_link_hash_undefweak)
65f38f15 5151 ;
671bae9c 5152 else if (info->shared
65f38f15
AM
5153 && (!info->symbolic || info->allow_shlib_undefined)
5154 && !info->no_undefined
5155 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
5156 ;
5bd4f169
AM
5157 else
5158 {
5159 if (! ((*info->callbacks->undefined_symbol)
5160 (info, h->root.root.string, input_bfd, input_section,
65f38f15
AM
5161 offset, (!info->shared
5162 || info->no_undefined
5163 || ELF_ST_VISIBILITY (h->other)))))
5bd4f169 5164 return false;
ef60b7ff 5165 warned = true;
5bd4f169
AM
5166 }
5167 }
5168
65f38f15 5169 /* First handle relocations that tweak non-addend part of insn. */
86c76c7b 5170 insn = 0;
65f38f15 5171 switch (r_type)
5bd4f169
AM
5172 {
5173 default:
65f38f15 5174 break;
5bd4f169 5175
65f38f15
AM
5176 /* Branch taken prediction relocations. */
5177 case R_PPC64_ADDR14_BRTAKEN:
5178 case R_PPC64_REL14_BRTAKEN:
96e0dda4 5179 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
86c76c7b 5180 /* Fall thru. */
65f38f15 5181
86c76c7b 5182 /* Branch not taken prediction relocations. */
65f38f15
AM
5183 case R_PPC64_ADDR14_BRNTAKEN:
5184 case R_PPC64_REL14_BRNTAKEN:
86c76c7b 5185 insn |= bfd_get_32 (output_bfd, contents + offset) & ~(0x01 << 21);
96e0dda4
AM
5186 if (is_power4)
5187 {
5188 /* Set 'a' bit. This is 0b00010 in BO field for branch
5189 on CR(BI) insns (BO == 001at or 011at), and 0b01000
5190 for branch on CTR insns (BO == 1a00t or 1a01t). */
5191 if ((insn & (0x14 << 21)) == (0x04 << 21))
5192 insn |= 0x02 << 21;
5193 else if ((insn & (0x14 << 21)) == (0x10 << 21))
5194 insn |= 0x08 << 21;
5195 else
5196 break;
5197 }
65f38f15 5198 else
96e0dda4 5199 {
805fc799
AM
5200 from = (offset
5201 + input_section->output_offset
5202 + input_section->output_section->vma);
5203
96e0dda4 5204 /* Invert 'y' bit if not the default. */
805fc799 5205 if ((bfd_signed_vma) (relocation + addend - from) < 0)
96e0dda4
AM
5206 insn ^= 0x01 << 21;
5207 }
86c76c7b 5208
65f38f15
AM
5209 bfd_put_32 (output_bfd, (bfd_vma) insn, contents + offset);
5210 break;
5bd4f169 5211
65f38f15 5212 case R_PPC64_REL24:
721956f4
AM
5213 /* A REL24 branching to a linkage function is followed by a
5214 nop. We replace the nop with a ld in order to restore
5215 the TOC base pointer. Only calls to shared objects need
5216 to alter the TOC base. These are recognized by their
5217 need for a PLT entry. */
5bd4f169 5218 if (h != NULL
721956f4
AM
5219 && (fdh = ((struct ppc_link_hash_entry *) h)->oh) != NULL
5220 && fdh->plt.offset != (bfd_vma) -1
5221 && (stub_entry = ppc_get_stub_entry (input_section, sec, fdh,
5222 rel, htab)) != NULL)
41bd81ab 5223 {
721956f4
AM
5224 boolean can_plt_call = 0;
5225
41bd81ab
AM
5226 if (offset + 8 <= input_section->_cooked_size)
5227 {
721956f4
AM
5228 insn = bfd_get_32 (input_bfd, contents + offset + 4);
5229 if (insn == NOP
5230 || insn == CROR_151515 || insn == CROR_313131)
41bd81ab 5231 {
721956f4
AM
5232 bfd_put_32 (input_bfd, (bfd_vma) LD_R2_40R1,
5233 contents + offset + 4);
5234 can_plt_call = 1;
41bd81ab 5235 }
5bd4f169 5236 }
721956f4
AM
5237
5238 if (!can_plt_call)
5239 {
5240 /* If this is a plain branch rather than a branch
5241 and link, don't require a nop. */
5242 insn = bfd_get_32 (input_bfd, contents + offset);
5243 if ((insn & 1) == 0)
5244 can_plt_call = 1;
5245 }
5246
5247 if (can_plt_call)
5248 {
5249 relocation = (stub_entry->stub_offset
5250 + stub_entry->stub_sec->output_offset
5251 + stub_entry->stub_sec->output_section->vma);
5252 addend = 0;
5253 unresolved_reloc = false;
5254 }
5bd4f169 5255 }
e86ce104
AM
5256
5257 if (h != NULL
5258 && h->root.type == bfd_link_hash_undefweak
721956f4
AM
5259 && relocation == 0
5260 && addend == 0)
e86ce104 5261 {
721956f4
AM
5262 /* Tweak calls to undefined weak functions to point at a
5263 blr. We can thus call a weak function without first
5264 checking whether the function is defined. We have a
5265 blr at the end of .sfpr. */
805fc799 5266 BFD_ASSERT (htab->sfpr->_raw_size != 0);
721956f4
AM
5267 relocation = (htab->sfpr->_raw_size - 4
5268 + htab->sfpr->output_offset
5269 + htab->sfpr->output_section->vma);
5270 from = (offset
5271 + input_section->output_offset
5272 + input_section->output_section->vma);
805fc799 5273
721956f4
AM
5274 /* But let's not be silly about it. If the blr isn't in
5275 reach, just go to the next instruction. */
805fc799
AM
5276 if (relocation - from + (1 << 25) >= (1 << 26)
5277 || htab->sfpr->_raw_size == 0)
721956f4 5278 relocation = from + 4;
e86ce104 5279 }
65f38f15
AM
5280 break;
5281 }
5bd4f169 5282
65f38f15
AM
5283 /* Set `addend'. */
5284 switch (r_type)
5285 {
5286 default:
5287 (*_bfd_error_handler)
5288 (_("%s: unknown relocation type %d for symbol %s"),
5289 bfd_archive_filename (input_bfd), (int) r_type, sym_name);
5bd4f169 5290
65f38f15
AM
5291 bfd_set_error (bfd_error_bad_value);
5292 ret = false;
5293 continue;
5bd4f169 5294
65f38f15
AM
5295 case R_PPC64_NONE:
5296 case R_PPC_GNU_VTINHERIT:
5297 case R_PPC_GNU_VTENTRY:
5298 continue;
5bd4f169
AM
5299
5300 /* GOT16 relocations. Like an ADDR16 using the symbol's
5301 address in the GOT as relocation value instead of the
5302 symbols value itself. Also, create a GOT entry for the
5303 symbol and put the symbol value there. */
65f38f15
AM
5304 case R_PPC64_GOT16:
5305 case R_PPC64_GOT16_LO:
5306 case R_PPC64_GOT16_HI:
5307 case R_PPC64_GOT16_HA:
5308 case R_PPC64_GOT16_DS:
5309 case R_PPC64_GOT16_LO_DS:
5bd4f169
AM
5310 {
5311 /* Relocation is to the entry for this symbol in the global
5312 offset table. */
5313 bfd_vma off;
65f38f15
AM
5314
5315 if (htab->sgot == NULL)
5316 abort ();
5bd4f169
AM
5317
5318 if (h != NULL)
5319 {
65f38f15 5320 boolean dyn;
5bd4f169 5321
65f38f15
AM
5322 off = h->got.offset;
5323 dyn = htab->elf.dynamic_sections_created;
5324 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info, h)
5bd4f169 5325 || (info->shared
65f38f15
AM
5326 && (info->symbolic
5327 || h->dynindx == -1
5328 || (h->elf_link_hash_flags
5329 & ELF_LINK_FORCED_LOCAL))
5bd4f169
AM
5330 && (h->elf_link_hash_flags
5331 & ELF_LINK_HASH_DEF_REGULAR)))
5332 {
5333 /* This is actually a static link, or it is a
5334 -Bsymbolic link and the symbol is defined
65f38f15
AM
5335 locally, or the symbol was forced to be local
5336 because of a version file. We must initialize
5337 this entry in the global offset table. Since the
5338 offset must always be a multiple of 8, we use the
5339 least significant bit to record whether we have
5bd4f169
AM
5340 initialized it already.
5341
65f38f15 5342 When doing a dynamic link, we create a .rel.got
5bd4f169
AM
5343 relocation entry to initialize the value. This
5344 is done in the finish_dynamic_symbol routine. */
5345 if ((off & 1) != 0)
5346 off &= ~1;
5347 else
5348 {
5349 bfd_put_64 (output_bfd, relocation,
65f38f15 5350 htab->sgot->contents + off);
5bd4f169
AM
5351 h->got.offset |= 1;
5352 }
5353 }
65f38f15
AM
5354 else
5355 unresolved_reloc = false;
5bd4f169
AM
5356 }
5357 else
5358 {
65f38f15
AM
5359 if (local_got_offsets == NULL)
5360 abort ();
5bd4f169
AM
5361
5362 off = local_got_offsets[r_symndx];
5363
65f38f15 5364 /* The offset must always be a multiple of 8. We use
5bd4f169
AM
5365 the least significant bit to record whether we have
5366 already processed this entry. */
5367 if ((off & 1) != 0)
5368 off &= ~1;
5369 else
5370 {
65f38f15
AM
5371 bfd_put_64 (output_bfd, relocation,
5372 htab->sgot->contents + off);
5bd4f169
AM
5373
5374 if (info->shared)
5375 {
5bd4f169 5376 Elf_Internal_Rela outrel;
65f38f15 5377 Elf64_External_Rela *loc;
5bd4f169
AM
5378
5379 /* We need to generate a R_PPC64_RELATIVE reloc
5380 for the dynamic linker. */
65f38f15
AM
5381 outrel.r_offset = (htab->sgot->output_section->vma
5382 + htab->sgot->output_offset
5bd4f169
AM
5383 + off);
5384 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
5385 outrel.r_addend = relocation;
65f38f15
AM
5386 loc = (Elf64_External_Rela *) htab->srelgot->contents;
5387 loc += htab->srelgot->reloc_count++;
5388 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
5bd4f169
AM
5389 }
5390
5391 local_got_offsets[r_symndx] |= 1;
5392 }
5393 }
5394
65f38f15
AM
5395 if (off >= (bfd_vma) -2)
5396 abort ();
5397
5398 relocation = htab->sgot->output_offset + off;
5399
5bd4f169
AM
5400 /* TOC base (r2) is TOC start plus 0x8000. */
5401 addend -= TOC_BASE_OFF;
5bd4f169 5402 }
65f38f15
AM
5403 break;
5404
5405 case R_PPC64_PLT16_HA:
5406 case R_PPC64_PLT16_HI:
5407 case R_PPC64_PLT16_LO:
5408 case R_PPC64_PLT32:
5409 case R_PPC64_PLT64:
5410 /* Relocation is to the entry for this symbol in the
5411 procedure linkage table. */
5412
5413 /* Resolve a PLT reloc against a local symbol directly,
5414 without using the procedure linkage table. */
5415 if (h == NULL)
5416 break;
5417
5418 if (h->plt.offset == (bfd_vma) -1
5419 || htab->splt == NULL)
5420 {
5421 /* We didn't make a PLT entry for this symbol. This
5422 happens when statically linking PIC code, or when
5423 using -Bsymbolic. */
5424 break;
5425 }
5426
5427 relocation = (htab->splt->output_section->vma
5428 + htab->splt->output_offset
5429 + h->plt.offset);
5430 unresolved_reloc = false;
5431 break;
5bd4f169
AM
5432
5433 /* TOC16 relocs. We want the offset relative to the TOC base,
5434 which is the address of the start of the TOC plus 0x8000.
5435 The TOC consists of sections .got, .toc, .tocbss, and .plt,
5436 in this order. */
65f38f15
AM
5437 case R_PPC64_TOC16:
5438 case R_PPC64_TOC16_LO:
5439 case R_PPC64_TOC16_HI:
5440 case R_PPC64_TOC16_DS:
5441 case R_PPC64_TOC16_LO_DS:
5442 case R_PPC64_TOC16_HA:
5bd4f169 5443 addend -= TOCstart + TOC_BASE_OFF;
5bd4f169
AM
5444 break;
5445
5446 /* Relocate against the beginning of the section. */
65f38f15
AM
5447 case R_PPC64_SECTOFF:
5448 case R_PPC64_SECTOFF_LO:
5449 case R_PPC64_SECTOFF_HI:
5450 case R_PPC64_SECTOFF_DS:
5451 case R_PPC64_SECTOFF_LO_DS:
5452 case R_PPC64_SECTOFF_HA:
5453 if (sec != (asection *) 0)
5454 addend -= sec->output_section->vma;
5bd4f169
AM
5455 break;
5456
721956f4
AM
5457 case R_PPC64_REL14:
5458 case R_PPC64_REL14_BRNTAKEN:
5459 case R_PPC64_REL14_BRTAKEN:
5d1634d7
AM
5460 case R_PPC64_REL24:
5461 break;
5462
65f38f15
AM
5463 /* Relocations that may need to be propagated if this is a
5464 dynamic object. */
65f38f15
AM
5465 case R_PPC64_REL32:
5466 case R_PPC64_REL64:
5467 case R_PPC64_ADDR14:
5468 case R_PPC64_ADDR14_BRNTAKEN:
5469 case R_PPC64_ADDR14_BRTAKEN:
5470 case R_PPC64_ADDR16:
5471 case R_PPC64_ADDR16_DS:
5472 case R_PPC64_ADDR16_HA:
5473 case R_PPC64_ADDR16_HI:
5474 case R_PPC64_ADDR16_HIGHER:
5475 case R_PPC64_ADDR16_HIGHERA:
5476 case R_PPC64_ADDR16_HIGHEST:
5477 case R_PPC64_ADDR16_HIGHESTA:
5478 case R_PPC64_ADDR16_LO:
5479 case R_PPC64_ADDR16_LO_DS:
5480 case R_PPC64_ADDR24:
5481 case R_PPC64_ADDR30:
5482 case R_PPC64_ADDR32:
5483 case R_PPC64_ADDR64:
5484 case R_PPC64_UADDR16:
5485 case R_PPC64_UADDR32:
5486 case R_PPC64_UADDR64:
ec338859
AM
5487 /* r_symndx will be zero only for relocs against symbols
5488 from removed linkonce sections, or sections discarded by
5489 a linker script. */
5d1634d7
AM
5490 if (r_symndx == 0)
5491 break;
5492 /* Fall thru. */
5493
5494 case R_PPC64_TOC:
5495 if ((input_section->flags & SEC_ALLOC) == 0)
ec338859
AM
5496 break;
5497
41bd81ab
AM
5498 if (NO_OPD_RELOCS && is_opd)
5499 break;
5500
65f38f15 5501 if ((info->shared
65f38f15
AM
5502 && (IS_ABSOLUTE_RELOC (r_type)
5503 || (h != NULL
5504 && h->dynindx != -1
5505 && (! info->symbolic
5506 || (h->elf_link_hash_flags
5507 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
5508 || (!info->shared
65f38f15
AM
5509 && h != NULL
5510 && h->dynindx != -1
5511 && (h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
5512 && (((h->elf_link_hash_flags
5513 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
5514 && (h->elf_link_hash_flags
5515 & ELF_LINK_HASH_DEF_REGULAR) == 0)
5516 || h->root.type == bfd_link_hash_undefweak
5517 || h->root.type == bfd_link_hash_undefined)))
5518 {
5519 Elf_Internal_Rela outrel;
5520 boolean skip, relocate;
5521 asection *sreloc;
5522 Elf64_External_Rela *loc;
5523
5524 /* When generating a dynamic object, these relocations
5525 are copied into the output file to be resolved at run
5526 time. */
5527
5528 skip = false;
0bb2d96a 5529 relocate = false;
65f38f15 5530
c629eae0
JJ
5531 outrel.r_offset =
5532 _bfd_elf_section_offset (output_bfd, info, input_section,
5533 rel->r_offset);
5534 if (outrel.r_offset == (bfd_vma) -1)
5535 skip = true;
0bb2d96a
JJ
5536 else if (outrel.r_offset == (bfd_vma) -2)
5537 skip = true, relocate = true;
65f38f15
AM
5538 outrel.r_offset += (input_section->output_section->vma
5539 + input_section->output_offset);
5540 outrel.r_addend = addend;
5541
5542 if (skip)
0bb2d96a 5543 memset (&outrel, 0, sizeof outrel);
65f38f15
AM
5544 else if (h != NULL
5545 && h->dynindx != -1
41bd81ab 5546 && !is_opd
65f38f15
AM
5547 && (!IS_ABSOLUTE_RELOC (r_type)
5548 || !info->shared
5549 || !info->symbolic
5550 || (h->elf_link_hash_flags
5551 & ELF_LINK_HASH_DEF_REGULAR) == 0))
0bb2d96a 5552 outrel.r_info = ELF64_R_INFO (h->dynindx, r_type);
65f38f15
AM
5553 else
5554 {
41bd81ab
AM
5555 /* This symbol is local, or marked to become local,
5556 or this is an opd section reloc which must point
5557 at a local function. */
65f38f15
AM
5558 outrel.r_addend += relocation;
5559 relocate = true;
e86ce104 5560 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
65f38f15
AM
5561 {
5562 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
5563 }
5564 else
5565 {
5566 long indx = 0;
5567
41bd81ab 5568 if (bfd_is_abs_section (sec))
65f38f15
AM
5569 ;
5570 else if (sec == NULL || sec->owner == NULL)
5571 {
5572 bfd_set_error (bfd_error_bad_value);
5573 return false;
5574 }
5575 else
5576 {
5577 asection *osec;
5578
5579 osec = sec->output_section;
5580 indx = elf_section_data (osec)->dynindx;
5581
5582 /* We are turning this relocation into one
5583 against a section symbol, so subtract out
5584 the output section's address but not the
5585 offset of the input section in the output
5586 section. */
5587 outrel.r_addend -= osec->vma;
5588 }
5589
5590 outrel.r_info = ELF64_R_INFO (indx, r_type);
5591 }
5592 }
5593
5594 sreloc = elf_section_data (input_section)->sreloc;
5595 if (sreloc == NULL)
5596 abort ();
5597
5598 loc = (Elf64_External_Rela *) sreloc->contents;
5599 loc += sreloc->reloc_count++;
5600 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
5601
5602 /* If this reloc is against an external symbol, it will
5603 be computed at runtime, so there's no need to do
5604 anything now. */
5605 if (! relocate)
5606 continue;
5607 }
5bd4f169
AM
5608 break;
5609
65f38f15
AM
5610 case R_PPC64_COPY:
5611 case R_PPC64_GLOB_DAT:
5612 case R_PPC64_JMP_SLOT:
5613 case R_PPC64_RELATIVE:
5614 /* We shouldn't ever see these dynamic relocs in relocatable
5615 files. */
5616 /* Fall thru */
5617
5618 case R_PPC64_PLTGOT16:
5619 case R_PPC64_PLTGOT16_DS:
5620 case R_PPC64_PLTGOT16_HA:
5621 case R_PPC64_PLTGOT16_HI:
5622 case R_PPC64_PLTGOT16_LO:
5623 case R_PPC64_PLTGOT16_LO_DS:
5624 case R_PPC64_PLTREL32:
5625 case R_PPC64_PLTREL64:
5626 /* These ones haven't been implemented yet. */
5627
5628 (*_bfd_error_handler)
5629 (_("%s: Relocation %s is not supported for symbol %s."),
5630 bfd_archive_filename (input_bfd),
5631 ppc64_elf_howto_table[(int) r_type]->name, sym_name);
5bd4f169
AM
5632
5633 bfd_set_error (bfd_error_invalid_operation);
5634 ret = false;
5635 continue;
65f38f15 5636 }
5bd4f169 5637
65f38f15
AM
5638 /* Do any further special processing. */
5639 switch (r_type)
5640 {
5641 default:
5642 break;
5643
5644 case R_PPC64_ADDR16_HA:
5645 case R_PPC64_ADDR16_HIGHERA:
5646 case R_PPC64_ADDR16_HIGHESTA:
5647 case R_PPC64_PLT16_HA:
5648 case R_PPC64_TOC16_HA:
5649 case R_PPC64_SECTOFF_HA:
5650 /* It's just possible that this symbol is a weak symbol
5651 that's not actually defined anywhere. In that case,
5652 'sec' would be NULL, and we should leave the symbol
5653 alone (it will be set to zero elsewhere in the link). */
5654 if (sec != NULL)
5655 /* Add 0x10000 if sign bit in 0:15 is set. */
5656 addend += ((relocation + addend) & 0x8000) << 1;
5657 break;
5658
5659 case R_PPC64_ADDR16_DS:
5660 case R_PPC64_ADDR16_LO_DS:
5661 case R_PPC64_GOT16_DS:
5662 case R_PPC64_GOT16_LO_DS:
5663 case R_PPC64_PLT16_LO_DS:
5664 case R_PPC64_SECTOFF_DS:
5665 case R_PPC64_SECTOFF_LO_DS:
5666 case R_PPC64_TOC16_DS:
5667 case R_PPC64_TOC16_LO_DS:
5668 case R_PPC64_PLTGOT16_DS:
5669 case R_PPC64_PLTGOT16_LO_DS:
5670 if (((relocation + addend) & 3) != 0)
5671 {
5672 (*_bfd_error_handler)
5673 (_("%s: error: relocation %s not a multiple of 4"),
5674 bfd_archive_filename (input_bfd),
5675 ppc64_elf_howto_table[(int) r_type]->name);
5676 bfd_set_error (bfd_error_bad_value);
5677 ret = false;
5678 continue;
5679 }
5680 break;
721956f4
AM
5681
5682 case R_PPC64_REL14:
5683 case R_PPC64_REL14_BRNTAKEN:
5684 case R_PPC64_REL14_BRTAKEN:
5685 max_br_offset = 1 << 15;
5686 goto branch_check;
5687
5688 case R_PPC64_REL24:
5689 max_br_offset = 1 << 25;
5690
5691 branch_check:
5692 /* If the branch is out of reach, then redirect the
5693 call to the local stub for this function. */
5694 from = (offset
5695 + input_section->output_offset
5696 + input_section->output_section->vma);
5697 if (relocation + addend - from + max_br_offset >= 2 * max_br_offset
5698 && (stub_entry = ppc_get_stub_entry (input_section, sec, h,
5699 rel, htab)) != NULL)
5700 {
5701 /* Munge up the value and addend so that we call the stub
5702 rather than the procedure directly. */
5703 relocation = (stub_entry->stub_offset
5704 + stub_entry->stub_sec->output_offset
5705 + stub_entry->stub_sec->output_section->vma);
5706 addend = 0;
5707 }
5708 break;
5bd4f169
AM
5709 }
5710
65f38f15
AM
5711 /* FIXME: Why do we allow debugging sections to escape this error?
5712 More importantly, why do we not emit dynamic relocs above in
5713 debugging sections (which are ! SEC_ALLOC)? If we had
5714 emitted the dynamic reloc, we could remove the fudge here. */
5715 if (unresolved_reloc
5716 && !(info->shared
5717 && (input_section->flags & SEC_DEBUGGING) != 0
5718 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0))
5719 (*_bfd_error_handler)
5720 (_("%s(%s+0x%lx): unresolvable relocation against symbol `%s'"),
5721 bfd_archive_filename (input_bfd),
5722 bfd_get_section_name (input_bfd, input_section),
5723 (long) rel->r_offset,
5724 h->root.root.string);
5bd4f169 5725
65f38f15 5726 r = _bfd_final_link_relocate (ppc64_elf_howto_table[(int) r_type],
5bd4f169
AM
5727 input_bfd,
5728 input_section,
5729 contents,
5730 offset,
5731 relocation,
5732 addend);
5733
ef60b7ff 5734 if (r != bfd_reloc_ok)
5bd4f169
AM
5735 {
5736 const char *name;
5737
5738 if (h != NULL)
5739 {
5740 if (h->root.type == bfd_link_hash_undefweak
65f38f15 5741 && ppc64_elf_howto_table[(int) r_type]->pc_relative)
5bd4f169
AM
5742 {
5743 /* Assume this is a call protected by other code that
5744 detects the symbol is undefined. If this is the case,
5745 we can safely ignore the overflow. If not, the
5746 program is hosed anyway, and a little warning isn't
5747 going to help. */
5748
5749 continue;
5750 }
5751
5752 name = h->root.root.string;
5753 }
5754 else
5755 {
5756 name = bfd_elf_string_from_elf_section (input_bfd,
5757 symtab_hdr->sh_link,
5758 sym->st_name);
5759 if (name == NULL)
5760 continue;
5761 if (*name == '\0')
5762 name = bfd_section_name (input_bfd, sec);
5763 }
5764
ef60b7ff
AM
5765 if (r == bfd_reloc_overflow)
5766 {
5767 if (warned)
5768 continue;
5769 if (!((*info->callbacks->reloc_overflow)
5770 (info, name, ppc64_elf_howto_table[(int) r_type]->name,
5771 rel->r_addend, input_bfd, input_section, offset)))
5772 return false;
5773 }
5774 else
5775 {
5776 (*_bfd_error_handler)
5777 (_("%s(%s+0x%lx): reloc against `%s': error %d"),
5778 bfd_archive_filename (input_bfd),
5779 bfd_get_section_name (input_bfd, input_section),
5780 (long) rel->r_offset, name, (int) r);
5781 ret = false;
5782 }
5bd4f169 5783 }
5bd4f169
AM
5784 }
5785
5bd4f169
AM
5786 return ret;
5787}
5788
5789/* Finish up dynamic symbol handling. We set the contents of various
5790 dynamic sections here. */
5791
5792static boolean
5793ppc64_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
5794 bfd *output_bfd;
5795 struct bfd_link_info *info;
5796 struct elf_link_hash_entry *h;
5797 Elf_Internal_Sym *sym;
5798{
65f38f15 5799 struct ppc_link_hash_table *htab;
5bd4f169
AM
5800 bfd *dynobj;
5801
65f38f15
AM
5802 htab = ppc_hash_table (info);
5803 dynobj = htab->elf.dynobj;
5bd4f169 5804
41bd81ab
AM
5805 if (h->plt.offset != (bfd_vma) -1
5806 && ((struct ppc_link_hash_entry *) h)->is_func_descriptor)
5bd4f169 5807 {
65f38f15
AM
5808 Elf_Internal_Rela rela;
5809 Elf64_External_Rela *loc;
5bd4f169
AM
5810
5811 /* This symbol has an entry in the procedure linkage table. Set
5812 it up. */
5813
5d1634d7 5814 if (htab->splt == NULL
65f38f15 5815 || htab->srelplt == NULL
e86ce104 5816 || htab->sglink == NULL)
65f38f15
AM
5817 abort ();
5818
65f38f15
AM
5819 /* Create a JMP_SLOT reloc to inform the dynamic linker to
5820 fill in the PLT entry. */
5bd4f169 5821
65f38f15
AM
5822 rela.r_offset = (htab->splt->output_section->vma
5823 + htab->splt->output_offset
5824 + h->plt.offset);
e86ce104 5825 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
65f38f15
AM
5826 rela.r_addend = 0;
5827
5828 loc = (Elf64_External_Rela *) htab->srelplt->contents;
5829 loc += (h->plt.offset - PLT_INITIAL_ENTRY_SIZE) / PLT_ENTRY_SIZE;
5830 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
5bd4f169
AM
5831 }
5832
5833 if (h->got.offset != (bfd_vma) -1)
5834 {
5bd4f169 5835 Elf_Internal_Rela rela;
65f38f15 5836 Elf64_External_Rela *loc;
5bd4f169
AM
5837
5838 /* This symbol has an entry in the global offset table. Set it
5839 up. */
5840
65f38f15
AM
5841 if (htab->sgot == NULL || htab->srelgot == NULL)
5842 abort ();
5bd4f169 5843
65f38f15
AM
5844 rela.r_offset = (htab->sgot->output_section->vma
5845 + htab->sgot->output_offset
dc810e39 5846 + (h->got.offset &~ (bfd_vma) 1));
5bd4f169 5847
65f38f15
AM
5848 /* If this is a static link, or it is a -Bsymbolic link and the
5849 symbol is defined locally or was forced to be local because
5850 of a version file, we just want to emit a RELATIVE reloc.
5851 The entry in the global offset table will already have been
5852 initialized in the relocate_section function. */
5bd4f169 5853 if (info->shared
65f38f15
AM
5854 && (info->symbolic
5855 || h->dynindx == -1
5856 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL))
5bd4f169
AM
5857 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
5858 {
65f38f15 5859 BFD_ASSERT((h->got.offset & 1) != 0);
5bd4f169
AM
5860 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
5861 rela.r_addend = (h->root.u.def.value
5862 + h->root.u.def.section->output_section->vma
5863 + h->root.u.def.section->output_offset);
5864 }
5865 else
5866 {
5867 BFD_ASSERT ((h->got.offset & 1) == 0);
65f38f15
AM
5868 bfd_put_64 (output_bfd, (bfd_vma) 0,
5869 htab->sgot->contents + h->got.offset);
5bd4f169
AM
5870 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_GLOB_DAT);
5871 rela.r_addend = 0;
5872 }
5873
65f38f15
AM
5874 loc = (Elf64_External_Rela *) htab->srelgot->contents;
5875 loc += htab->srelgot->reloc_count++;
5876 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
5bd4f169
AM
5877 }
5878
5879 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
5880 {
5bd4f169 5881 Elf_Internal_Rela rela;
65f38f15 5882 Elf64_External_Rela *loc;
5bd4f169 5883
65f38f15 5884 /* This symbol needs a copy reloc. Set it up. */
5bd4f169 5885
65f38f15
AM
5886 if (h->dynindx == -1
5887 || (h->root.type != bfd_link_hash_defined
5888 && h->root.type != bfd_link_hash_defweak)
5889 || htab->srelbss == NULL)
5890 abort ();
5bd4f169
AM
5891
5892 rela.r_offset = (h->root.u.def.value
5893 + h->root.u.def.section->output_section->vma
5894 + h->root.u.def.section->output_offset);
5895 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
5896 rela.r_addend = 0;
65f38f15
AM
5897 loc = (Elf64_External_Rela *) htab->srelbss->contents;
5898 loc += htab->srelbss->reloc_count++;
5899 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
5bd4f169
AM
5900 }
5901
5bd4f169
AM
5902 /* Mark some specially defined symbols as absolute. */
5903 if (strcmp (h->root.root.string, "_DYNAMIC") == 0)
5904 sym->st_shndx = SHN_ABS;
5905
5906 return true;
5907}
5908
65f38f15
AM
5909/* Used to decide how to sort relocs in an optimal manner for the
5910 dynamic linker, before writing them out. */
5911
5912static enum elf_reloc_type_class
5913ppc64_elf_reloc_type_class (rela)
5914 const Elf_Internal_Rela *rela;
5915{
a33d1f77
AM
5916 enum elf_ppc_reloc_type r_type;
5917
5918 r_type = (enum elf_ppc_reloc_type) ELF64_R_TYPE (rela->r_info);
5919 switch (r_type)
65f38f15
AM
5920 {
5921 case R_PPC64_RELATIVE:
5922 return reloc_class_relative;
5923 case R_PPC64_JMP_SLOT:
5924 return reloc_class_plt;
5925 case R_PPC64_COPY:
5926 return reloc_class_copy;
5927 default:
5928 return reloc_class_normal;
5929 }
5930}
5931
5bd4f169
AM
5932/* Finish up the dynamic sections. */
5933
5934static boolean
5935ppc64_elf_finish_dynamic_sections (output_bfd, info)
5936 bfd *output_bfd;
5937 struct bfd_link_info *info;
5938{
65f38f15
AM
5939 struct ppc_link_hash_table *htab;
5940 bfd *dynobj;
5bd4f169 5941 asection *sdyn;
5bd4f169 5942
65f38f15
AM
5943 htab = ppc_hash_table (info);
5944 dynobj = htab->elf.dynobj;
5bd4f169
AM
5945 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
5946
65f38f15 5947 if (htab->elf.dynamic_sections_created)
5bd4f169 5948 {
5bd4f169
AM
5949 Elf64_External_Dyn *dyncon, *dynconend;
5950
65f38f15
AM
5951 if (sdyn == NULL || htab->sgot == NULL)
5952 abort ();
5bd4f169
AM
5953
5954 dyncon = (Elf64_External_Dyn *) sdyn->contents;
5955 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
5956 for (; dyncon < dynconend; dyncon++)
5957 {
5958 Elf_Internal_Dyn dyn;
19397422 5959 asection *s;
5bd4f169
AM
5960
5961 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
5962
5963 switch (dyn.d_tag)
5964 {
65f38f15
AM
5965 default:
5966 continue;
5bd4f169 5967
5d1634d7
AM
5968 case DT_PPC64_GLINK:
5969 dyn.d_un.d_ptr = (htab->sglink->output_section->vma
5970 + htab->sglink->output_offset);
5971 break;
5972
19397422
AM
5973 case DT_PPC64_OPD:
5974 s = bfd_get_section_by_name (output_bfd, ".opd");
5975 if (s != NULL)
5976 dyn.d_un.d_ptr = s->vma;
5977 break;
5978
5979 case DT_PPC64_OPDSZ:
5980 s = bfd_get_section_by_name (output_bfd, ".opd");
5981 if (s != NULL)
5982 dyn.d_un.d_val = s->_raw_size;
5983 break;
5984
65f38f15 5985 case DT_PLTGOT:
5d1634d7
AM
5986 dyn.d_un.d_ptr = (htab->splt->output_section->vma
5987 + htab->splt->output_offset);
65f38f15
AM
5988 break;
5989
5990 case DT_JMPREL:
5d1634d7
AM
5991 dyn.d_un.d_ptr = (htab->srelplt->output_section->vma
5992 + htab->srelplt->output_offset);
65f38f15 5993 break;
5bd4f169 5994
65f38f15 5995 case DT_PLTRELSZ:
5d1634d7
AM
5996 dyn.d_un.d_val = htab->srelplt->_raw_size;
5997 break;
5998
5999 case DT_RELASZ:
6000 /* Don't count procedure linkage table relocs in the
6001 overall reloc count. */
6002 if (htab->srelplt != NULL)
6003 dyn.d_un.d_val -= htab->srelplt->_raw_size;
65f38f15 6004 break;
5bd4f169 6005 }
5bd4f169 6006
65f38f15 6007 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
5bd4f169 6008 }
5bd4f169
AM
6009 }
6010
5d1634d7
AM
6011 if (htab->sgot != NULL && htab->sgot->_raw_size != 0)
6012 {
6013 /* Fill in the first entry in the global offset table.
6014 We use it to hold the link-time TOCbase. */
6015 bfd_put_64 (output_bfd,
60ee0d4a 6016 elf_gp (output_bfd) + TOC_BASE_OFF,
5d1634d7
AM
6017 htab->sgot->contents);
6018
6019 /* Set .got entry size. */
6020 elf_section_data (htab->sgot->output_section)->this_hdr.sh_entsize = 8;
6021 }
6022
6023 if (htab->splt != NULL && htab->splt->_raw_size != 0)
6024 {
6025 /* Set .plt entry size. */
6026 elf_section_data (htab->splt->output_section)->this_hdr.sh_entsize
6027 = PLT_ENTRY_SIZE;
6028 }
6029
5bd4f169
AM
6030 return true;
6031}
6032
6033#define TARGET_LITTLE_SYM bfd_elf64_powerpcle_vec
6034#define TARGET_LITTLE_NAME "elf64-powerpcle"
6035#define TARGET_BIG_SYM bfd_elf64_powerpc_vec
6036#define TARGET_BIG_NAME "elf64-powerpc"
6037#define ELF_ARCH bfd_arch_powerpc
6038#define ELF_MACHINE_CODE EM_PPC64
6039#define ELF_MAXPAGESIZE 0x10000
6040#define elf_info_to_howto ppc64_elf_info_to_howto
6041
6042#ifdef EM_CYGNUS_POWERPC
6043#define ELF_MACHINE_ALT1 EM_CYGNUS_POWERPC
6044#endif
6045
6046#ifdef EM_PPC_OLD
6047#define ELF_MACHINE_ALT2 EM_PPC_OLD
6048#endif
6049
6050#define elf_backend_want_got_sym 0
6051#define elf_backend_want_plt_sym 0
6052#define elf_backend_plt_alignment 3
6053#define elf_backend_plt_not_loaded 1
6054#define elf_backend_got_symbol_offset 0
5d1634d7
AM
6055#define elf_backend_got_header_size 8
6056#define elf_backend_plt_header_size PLT_INITIAL_ENTRY_SIZE
5bd4f169 6057#define elf_backend_can_gc_sections 1
65f38f15 6058#define elf_backend_can_refcount 1
b491616a 6059#define elf_backend_rela_normal 1
5bd4f169 6060
65f38f15
AM
6061#define bfd_elf64_bfd_reloc_type_lookup ppc64_elf_reloc_type_lookup
6062#define bfd_elf64_bfd_set_private_flags ppc64_elf_set_private_flags
65f38f15
AM
6063#define bfd_elf64_bfd_merge_private_bfd_data ppc64_elf_merge_private_bfd_data
6064#define bfd_elf64_bfd_link_hash_table_create ppc64_elf_link_hash_table_create
721956f4 6065#define bfd_elf64_bfd_link_hash_table_free ppc64_elf_link_hash_table_free
65f38f15
AM
6066
6067#define elf_backend_section_from_shdr ppc64_elf_section_from_shdr
6068#define elf_backend_create_dynamic_sections ppc64_elf_create_dynamic_sections
6069#define elf_backend_copy_indirect_symbol ppc64_elf_copy_indirect_symbol
6070#define elf_backend_check_relocs ppc64_elf_check_relocs
6071#define elf_backend_gc_mark_hook ppc64_elf_gc_mark_hook
6072#define elf_backend_gc_sweep_hook ppc64_elf_gc_sweep_hook
6073#define elf_backend_adjust_dynamic_symbol ppc64_elf_adjust_dynamic_symbol
e86ce104
AM
6074#define elf_backend_hide_symbol ppc64_elf_hide_symbol
6075#define elf_backend_always_size_sections ppc64_elf_func_desc_adjust
65f38f15
AM
6076#define elf_backend_size_dynamic_sections ppc64_elf_size_dynamic_sections
6077#define elf_backend_fake_sections ppc64_elf_fake_sections
6078#define elf_backend_relocate_section ppc64_elf_relocate_section
6079#define elf_backend_finish_dynamic_symbol ppc64_elf_finish_dynamic_symbol
6080#define elf_backend_reloc_type_class ppc64_elf_reloc_type_class
6081#define elf_backend_finish_dynamic_sections ppc64_elf_finish_dynamic_sections
5bd4f169
AM
6082
6083#include "elf64-target.h"
This page took 0.36761 seconds and 4 git commands to generate.