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