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