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