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