]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elf64-ppc.c
*** empty log message ***
[thirdparty/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. */
411e1bfb
AM
74#define TOC_BASE_OFF 0x8000
75
76/* Offset of tp and dtp pointers from start of TLS block. */
77#define TP_OFFSET 0x7000
78#define DTP_OFFSET 0x8000
5bd4f169 79
5d1634d7
AM
80/* .plt call stub instructions. */
81#define ADDIS_R12_R2 0x3d820000 /* addis %r12,%r2,xxx@ha */
82#define STD_R2_40R1 0xf8410028 /* std %r2,40(%r1) */
83#define LD_R11_0R12 0xe96c0000 /* ld %r11,xxx+0@l(%r12) */
84#define LD_R2_0R12 0xe84c0000 /* ld %r2,xxx+8@l(%r12) */
85#define MTCTR_R11 0x7d6903a6 /* mtctr %r11 */
86 /* ld %r11,xxx+16@l(%r12) */
87#define BCTR 0x4e800420 /* bctr */
88
89/* The normal stub is this size. */
90#define PLT_CALL_STUB_SIZE (7*4)
91
92/* But sometimes the .plt entry crosses a 64k boundary, and we need
93 to adjust the high word with this insn. */
94#define ADDIS_R12_R12_1 0x3d8c0001 /* addis %r12,%r12,1 */
5bd4f169 95
5d1634d7
AM
96/* The .glink fixup call stub is the same as the .plt call stub, but
97 the first instruction restores r2, and the std is omitted. */
98#define LD_R2_40R1 0xe8410028 /* ld %r2,40(%r1) */
99
100/* Always allow this much space. */
101#define GLINK_CALL_STUB_SIZE (8*4)
102
103/* Pad with this. */
104#define NOP 0x60000000
105
721956f4
AM
106/* Some other nops. */
107#define CROR_151515 0x4def7b82
108#define CROR_313131 0x4ffffb82
109
cedb70c5 110/* .glink entries for the first 32k functions are two instructions. */
5d1634d7
AM
111#define LI_R0_0 0x38000000 /* li %r0,0 */
112#define B_DOT 0x48000000 /* b . */
113
114/* After that, we need two instructions to load the index, followed by
115 a branch. */
116#define LIS_R0_0 0x3c000000 /* lis %r0,0 */
10ed1bba 117#define ORI_R0_R0_0 0x60000000 /* ori %r0,%r0,0 */
41bd81ab 118
82bd7b59
AM
119/* Instructions to save and restore floating point regs. */
120#define STFD_FR0_0R1 0xd8010000 /* stfd %fr0,0(%r1) */
121#define LFD_FR0_0R1 0xc8010000 /* lfd %fr0,0(%r1) */
122#define BLR 0x4e800020 /* blr */
123
41bd81ab
AM
124/* Since .opd is an array of descriptors and each entry will end up
125 with identical R_PPC64_RELATIVE relocs, there is really no need to
126 propagate .opd relocs; The dynamic linker should be taught to
1e2f5b6e 127 relocate .opd without reloc entries. */
41bd81ab
AM
128#ifndef NO_OPD_RELOCS
129#define NO_OPD_RELOCS 0
130#endif
5bd4f169 131\f
f5e87a1d 132#define ONES(n) (((bfd_vma) 1 << ((n) - 1) << 1) - 1)
b34976b6 133
5bd4f169 134/* Relocation HOWTO's. */
04c9666a 135static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
5bd4f169
AM
136
137static reloc_howto_type ppc64_elf_howto_raw[] = {
138 /* This reloc does nothing. */
139 HOWTO (R_PPC64_NONE, /* type */
140 0, /* rightshift */
411e1bfb
AM
141 2, /* size (0 = byte, 1 = short, 2 = long) */
142 32, /* bitsize */
b34976b6 143 FALSE, /* pc_relative */
5bd4f169 144 0, /* bitpos */
f5e87a1d 145 complain_overflow_dont, /* complain_on_overflow */
5bd4f169
AM
146 bfd_elf_generic_reloc, /* special_function */
147 "R_PPC64_NONE", /* name */
b34976b6 148 FALSE, /* partial_inplace */
d006db6c 149 0, /* src_mask */
5bd4f169 150 0, /* dst_mask */
b34976b6 151 FALSE), /* pcrel_offset */
5bd4f169
AM
152
153 /* A standard 32 bit relocation. */
154 HOWTO (R_PPC64_ADDR32, /* type */
155 0, /* rightshift */
156 2, /* size (0 = byte, 1 = short, 2 = long) */
157 32, /* bitsize */
b34976b6 158 FALSE, /* pc_relative */
5bd4f169
AM
159 0, /* bitpos */
160 complain_overflow_bitfield, /* complain_on_overflow */
161 bfd_elf_generic_reloc, /* special_function */
162 "R_PPC64_ADDR32", /* name */
b34976b6 163 FALSE, /* partial_inplace */
5bd4f169
AM
164 0, /* src_mask */
165 0xffffffff, /* dst_mask */
b34976b6 166 FALSE), /* pcrel_offset */
5bd4f169
AM
167
168 /* An absolute 26 bit branch; the lower two bits must be zero.
169 FIXME: we don't check that, we just clear them. */
170 HOWTO (R_PPC64_ADDR24, /* type */
171 0, /* rightshift */
172 2, /* size (0 = byte, 1 = short, 2 = long) */
173 26, /* bitsize */
b34976b6 174 FALSE, /* pc_relative */
5bd4f169
AM
175 0, /* bitpos */
176 complain_overflow_bitfield, /* complain_on_overflow */
177 bfd_elf_generic_reloc, /* special_function */
178 "R_PPC64_ADDR24", /* name */
b34976b6 179 FALSE, /* partial_inplace */
d006db6c 180 0, /* src_mask */
f5e87a1d 181 0x03fffffc, /* dst_mask */
b34976b6 182 FALSE), /* pcrel_offset */
5bd4f169
AM
183
184 /* A standard 16 bit relocation. */
185 HOWTO (R_PPC64_ADDR16, /* type */
186 0, /* rightshift */
187 1, /* size (0 = byte, 1 = short, 2 = long) */
188 16, /* bitsize */
b34976b6 189 FALSE, /* pc_relative */
5bd4f169
AM
190 0, /* bitpos */
191 complain_overflow_bitfield, /* complain_on_overflow */
192 bfd_elf_generic_reloc, /* special_function */
193 "R_PPC64_ADDR16", /* name */
b34976b6 194 FALSE, /* partial_inplace */
5bd4f169
AM
195 0, /* src_mask */
196 0xffff, /* dst_mask */
b34976b6 197 FALSE), /* pcrel_offset */
5bd4f169
AM
198
199 /* A 16 bit relocation without overflow. */
200 HOWTO (R_PPC64_ADDR16_LO, /* type */
201 0, /* rightshift */
202 1, /* size (0 = byte, 1 = short, 2 = long) */
203 16, /* bitsize */
b34976b6 204 FALSE, /* pc_relative */
5bd4f169
AM
205 0, /* bitpos */
206 complain_overflow_dont,/* complain_on_overflow */
207 bfd_elf_generic_reloc, /* special_function */
208 "R_PPC64_ADDR16_LO", /* name */
b34976b6 209 FALSE, /* partial_inplace */
5bd4f169
AM
210 0, /* src_mask */
211 0xffff, /* dst_mask */
b34976b6 212 FALSE), /* pcrel_offset */
5bd4f169
AM
213
214 /* Bits 16-31 of an address. */
215 HOWTO (R_PPC64_ADDR16_HI, /* type */
216 16, /* rightshift */
217 1, /* size (0 = byte, 1 = short, 2 = long) */
218 16, /* bitsize */
b34976b6 219 FALSE, /* pc_relative */
5bd4f169
AM
220 0, /* bitpos */
221 complain_overflow_dont, /* complain_on_overflow */
222 bfd_elf_generic_reloc, /* special_function */
223 "R_PPC64_ADDR16_HI", /* name */
b34976b6 224 FALSE, /* partial_inplace */
5bd4f169
AM
225 0, /* src_mask */
226 0xffff, /* dst_mask */
b34976b6 227 FALSE), /* pcrel_offset */
5bd4f169
AM
228
229 /* Bits 16-31 of an address, plus 1 if the contents of the low 16
230 bits, treated as a signed number, is negative. */
231 HOWTO (R_PPC64_ADDR16_HA, /* type */
232 16, /* rightshift */
233 1, /* size (0 = byte, 1 = short, 2 = long) */
234 16, /* bitsize */
b34976b6 235 FALSE, /* pc_relative */
5bd4f169
AM
236 0, /* bitpos */
237 complain_overflow_dont, /* complain_on_overflow */
805fc799 238 ppc64_elf_ha_reloc, /* special_function */
5bd4f169 239 "R_PPC64_ADDR16_HA", /* name */
b34976b6 240 FALSE, /* partial_inplace */
5bd4f169
AM
241 0, /* src_mask */
242 0xffff, /* dst_mask */
b34976b6 243 FALSE), /* pcrel_offset */
5bd4f169
AM
244
245 /* An absolute 16 bit branch; the lower two bits must be zero.
246 FIXME: we don't check that, we just clear them. */
247 HOWTO (R_PPC64_ADDR14, /* type */
248 0, /* rightshift */
249 2, /* size (0 = byte, 1 = short, 2 = long) */
250 16, /* bitsize */
b34976b6 251 FALSE, /* pc_relative */
5bd4f169
AM
252 0, /* bitpos */
253 complain_overflow_bitfield, /* complain_on_overflow */
254 bfd_elf_generic_reloc, /* special_function */
255 "R_PPC64_ADDR14", /* name */
b34976b6 256 FALSE, /* partial_inplace */
d006db6c 257 0, /* src_mask */
f5e87a1d 258 0x0000fffc, /* dst_mask */
b34976b6 259 FALSE), /* pcrel_offset */
5bd4f169
AM
260
261 /* An absolute 16 bit branch, for which bit 10 should be set to
262 indicate that the branch is expected to be taken. The lower two
263 bits must be zero. */
264 HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
265 0, /* rightshift */
266 2, /* size (0 = byte, 1 = short, 2 = long) */
267 16, /* bitsize */
b34976b6 268 FALSE, /* pc_relative */
5bd4f169
AM
269 0, /* bitpos */
270 complain_overflow_bitfield, /* complain_on_overflow */
805fc799 271 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 272 "R_PPC64_ADDR14_BRTAKEN",/* name */
b34976b6 273 FALSE, /* partial_inplace */
d006db6c 274 0, /* src_mask */
f5e87a1d 275 0x0000fffc, /* dst_mask */
b34976b6 276 FALSE), /* pcrel_offset */
5bd4f169
AM
277
278 /* An absolute 16 bit branch, for which bit 10 should be set to
279 indicate that the branch is not expected to be taken. The lower
280 two bits must be zero. */
281 HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
282 0, /* rightshift */
283 2, /* size (0 = byte, 1 = short, 2 = long) */
284 16, /* bitsize */
b34976b6 285 FALSE, /* pc_relative */
5bd4f169
AM
286 0, /* bitpos */
287 complain_overflow_bitfield, /* complain_on_overflow */
805fc799 288 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 289 "R_PPC64_ADDR14_BRNTAKEN",/* name */
b34976b6 290 FALSE, /* partial_inplace */
d006db6c 291 0, /* src_mask */
f5e87a1d 292 0x0000fffc, /* dst_mask */
b34976b6 293 FALSE), /* pcrel_offset */
5bd4f169
AM
294
295 /* A relative 26 bit branch; the lower two bits must be zero. */
296 HOWTO (R_PPC64_REL24, /* type */
297 0, /* rightshift */
298 2, /* size (0 = byte, 1 = short, 2 = long) */
299 26, /* bitsize */
b34976b6 300 TRUE, /* pc_relative */
5bd4f169
AM
301 0, /* bitpos */
302 complain_overflow_signed, /* complain_on_overflow */
303 bfd_elf_generic_reloc, /* special_function */
304 "R_PPC64_REL24", /* name */
b34976b6 305 FALSE, /* partial_inplace */
d006db6c 306 0, /* src_mask */
f5e87a1d 307 0x03fffffc, /* dst_mask */
b34976b6 308 TRUE), /* pcrel_offset */
5bd4f169
AM
309
310 /* A relative 16 bit branch; the lower two bits must be zero. */
311 HOWTO (R_PPC64_REL14, /* type */
312 0, /* rightshift */
313 2, /* size (0 = byte, 1 = short, 2 = long) */
314 16, /* bitsize */
b34976b6 315 TRUE, /* pc_relative */
5bd4f169
AM
316 0, /* bitpos */
317 complain_overflow_signed, /* complain_on_overflow */
318 bfd_elf_generic_reloc, /* special_function */
319 "R_PPC64_REL14", /* name */
b34976b6 320 FALSE, /* partial_inplace */
d006db6c 321 0, /* src_mask */
f5e87a1d 322 0x0000fffc, /* dst_mask */
b34976b6 323 TRUE), /* pcrel_offset */
5bd4f169
AM
324
325 /* A relative 16 bit branch. Bit 10 should be set to indicate that
326 the branch is expected to be taken. The lower two bits must be
327 zero. */
328 HOWTO (R_PPC64_REL14_BRTAKEN, /* type */
329 0, /* rightshift */
330 2, /* size (0 = byte, 1 = short, 2 = long) */
331 16, /* bitsize */
b34976b6 332 TRUE, /* pc_relative */
5bd4f169
AM
333 0, /* bitpos */
334 complain_overflow_signed, /* complain_on_overflow */
805fc799 335 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 336 "R_PPC64_REL14_BRTAKEN", /* name */
b34976b6 337 FALSE, /* partial_inplace */
d006db6c 338 0, /* src_mask */
f5e87a1d 339 0x0000fffc, /* dst_mask */
b34976b6 340 TRUE), /* pcrel_offset */
5bd4f169
AM
341
342 /* A relative 16 bit branch. Bit 10 should be set to indicate that
343 the branch is not expected to be taken. The lower two bits must
344 be zero. */
345 HOWTO (R_PPC64_REL14_BRNTAKEN, /* type */
346 0, /* rightshift */
347 2, /* size (0 = byte, 1 = short, 2 = long) */
348 16, /* bitsize */
b34976b6 349 TRUE, /* pc_relative */
5bd4f169
AM
350 0, /* bitpos */
351 complain_overflow_signed, /* complain_on_overflow */
805fc799 352 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 353 "R_PPC64_REL14_BRNTAKEN",/* name */
b34976b6 354 FALSE, /* partial_inplace */
d006db6c 355 0, /* src_mask */
f5e87a1d 356 0x0000fffc, /* dst_mask */
b34976b6 357 TRUE), /* pcrel_offset */
5bd4f169
AM
358
359 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
360 symbol. */
361 HOWTO (R_PPC64_GOT16, /* type */
362 0, /* rightshift */
363 1, /* size (0 = byte, 1 = short, 2 = long) */
364 16, /* bitsize */
b34976b6 365 FALSE, /* pc_relative */
5bd4f169
AM
366 0, /* bitpos */
367 complain_overflow_signed, /* complain_on_overflow */
805fc799 368 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 369 "R_PPC64_GOT16", /* name */
b34976b6 370 FALSE, /* partial_inplace */
5bd4f169
AM
371 0, /* src_mask */
372 0xffff, /* dst_mask */
b34976b6 373 FALSE), /* pcrel_offset */
5bd4f169
AM
374
375 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
376 the symbol. */
377 HOWTO (R_PPC64_GOT16_LO, /* type */
378 0, /* rightshift */
379 1, /* size (0 = byte, 1 = short, 2 = long) */
380 16, /* bitsize */
b34976b6 381 FALSE, /* pc_relative */
5bd4f169
AM
382 0, /* bitpos */
383 complain_overflow_dont, /* complain_on_overflow */
805fc799 384 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 385 "R_PPC64_GOT16_LO", /* name */
b34976b6 386 FALSE, /* partial_inplace */
5bd4f169
AM
387 0, /* src_mask */
388 0xffff, /* dst_mask */
b34976b6 389 FALSE), /* pcrel_offset */
5bd4f169
AM
390
391 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
392 the symbol. */
393 HOWTO (R_PPC64_GOT16_HI, /* type */
394 16, /* rightshift */
395 1, /* size (0 = byte, 1 = short, 2 = long) */
396 16, /* bitsize */
b34976b6 397 FALSE, /* pc_relative */
5bd4f169
AM
398 0, /* bitpos */
399 complain_overflow_dont,/* complain_on_overflow */
805fc799 400 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 401 "R_PPC64_GOT16_HI", /* name */
b34976b6 402 FALSE, /* partial_inplace */
5bd4f169
AM
403 0, /* src_mask */
404 0xffff, /* dst_mask */
b34976b6 405 FALSE), /* pcrel_offset */
5bd4f169
AM
406
407 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
408 the symbol. */
409 HOWTO (R_PPC64_GOT16_HA, /* type */
410 16, /* rightshift */
411 1, /* size (0 = byte, 1 = short, 2 = long) */
412 16, /* bitsize */
b34976b6 413 FALSE, /* pc_relative */
5bd4f169
AM
414 0, /* bitpos */
415 complain_overflow_dont,/* complain_on_overflow */
805fc799 416 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 417 "R_PPC64_GOT16_HA", /* name */
b34976b6 418 FALSE, /* partial_inplace */
5bd4f169
AM
419 0, /* src_mask */
420 0xffff, /* dst_mask */
b34976b6 421 FALSE), /* pcrel_offset */
5bd4f169
AM
422
423 /* This is used only by the dynamic linker. The symbol should exist
424 both in the object being run and in some shared library. The
425 dynamic linker copies the data addressed by the symbol from the
426 shared library into the object, because the object being
427 run has to have the data at some particular address. */
428 HOWTO (R_PPC64_COPY, /* type */
429 0, /* rightshift */
f5e87a1d
AM
430 0, /* this one is variable size */
431 0, /* bitsize */
b34976b6 432 FALSE, /* pc_relative */
5bd4f169 433 0, /* bitpos */
f5e87a1d
AM
434 complain_overflow_dont, /* complain_on_overflow */
435 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 436 "R_PPC64_COPY", /* name */
b34976b6 437 FALSE, /* partial_inplace */
5bd4f169
AM
438 0, /* src_mask */
439 0, /* dst_mask */
b34976b6 440 FALSE), /* pcrel_offset */
5bd4f169
AM
441
442 /* Like R_PPC64_ADDR64, but used when setting global offset table
443 entries. */
444 HOWTO (R_PPC64_GLOB_DAT, /* type */
445 0, /* rightshift */
446 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
447 64, /* bitsize */
b34976b6 448 FALSE, /* pc_relative */
5bd4f169
AM
449 0, /* bitpos */
450 complain_overflow_dont, /* complain_on_overflow */
805fc799 451 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 452 "R_PPC64_GLOB_DAT", /* name */
b34976b6 453 FALSE, /* partial_inplace */
5bd4f169 454 0, /* src_mask */
f5e87a1d 455 ONES (64), /* dst_mask */
b34976b6 456 FALSE), /* pcrel_offset */
5bd4f169
AM
457
458 /* Created by the link editor. Marks a procedure linkage table
459 entry for a symbol. */
460 HOWTO (R_PPC64_JMP_SLOT, /* type */
461 0, /* rightshift */
462 0, /* size (0 = byte, 1 = short, 2 = long) */
463 0, /* bitsize */
b34976b6 464 FALSE, /* pc_relative */
5bd4f169
AM
465 0, /* bitpos */
466 complain_overflow_dont, /* complain_on_overflow */
805fc799 467 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 468 "R_PPC64_JMP_SLOT", /* name */
b34976b6 469 FALSE, /* partial_inplace */
5bd4f169
AM
470 0, /* src_mask */
471 0, /* dst_mask */
b34976b6 472 FALSE), /* pcrel_offset */
5bd4f169
AM
473
474 /* Used only by the dynamic linker. When the object is run, this
475 doubleword64 is set to the load address of the object, plus the
476 addend. */
477 HOWTO (R_PPC64_RELATIVE, /* type */
478 0, /* rightshift */
479 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
480 64, /* bitsize */
b34976b6 481 FALSE, /* pc_relative */
5bd4f169
AM
482 0, /* bitpos */
483 complain_overflow_dont, /* complain_on_overflow */
484 bfd_elf_generic_reloc, /* special_function */
485 "R_PPC64_RELATIVE", /* name */
b34976b6 486 FALSE, /* partial_inplace */
5bd4f169 487 0, /* src_mask */
f5e87a1d 488 ONES (64), /* dst_mask */
b34976b6 489 FALSE), /* pcrel_offset */
5bd4f169
AM
490
491 /* Like R_PPC64_ADDR32, but may be unaligned. */
492 HOWTO (R_PPC64_UADDR32, /* type */
493 0, /* rightshift */
494 2, /* size (0 = byte, 1 = short, 2 = long) */
495 32, /* bitsize */
b34976b6 496 FALSE, /* pc_relative */
5bd4f169
AM
497 0, /* bitpos */
498 complain_overflow_bitfield, /* complain_on_overflow */
499 bfd_elf_generic_reloc, /* special_function */
500 "R_PPC64_UADDR32", /* name */
b34976b6 501 FALSE, /* partial_inplace */
5bd4f169
AM
502 0, /* src_mask */
503 0xffffffff, /* dst_mask */
b34976b6 504 FALSE), /* pcrel_offset */
5bd4f169
AM
505
506 /* Like R_PPC64_ADDR16, but may be unaligned. */
507 HOWTO (R_PPC64_UADDR16, /* type */
508 0, /* rightshift */
509 1, /* size (0 = byte, 1 = short, 2 = long) */
510 16, /* bitsize */
b34976b6 511 FALSE, /* pc_relative */
5bd4f169
AM
512 0, /* bitpos */
513 complain_overflow_bitfield, /* complain_on_overflow */
514 bfd_elf_generic_reloc, /* special_function */
515 "R_PPC64_UADDR16", /* name */
b34976b6 516 FALSE, /* partial_inplace */
5bd4f169
AM
517 0, /* src_mask */
518 0xffff, /* dst_mask */
b34976b6 519 FALSE), /* pcrel_offset */
5bd4f169
AM
520
521 /* 32-bit PC relative. */
522 HOWTO (R_PPC64_REL32, /* type */
523 0, /* rightshift */
524 2, /* size (0 = byte, 1 = short, 2 = long) */
525 32, /* bitsize */
b34976b6 526 TRUE, /* pc_relative */
5bd4f169 527 0, /* bitpos */
cedb70c5 528 /* FIXME: Verify. Was complain_overflow_bitfield. */
5bd4f169
AM
529 complain_overflow_signed, /* complain_on_overflow */
530 bfd_elf_generic_reloc, /* special_function */
531 "R_PPC64_REL32", /* name */
b34976b6 532 FALSE, /* partial_inplace */
5bd4f169
AM
533 0, /* src_mask */
534 0xffffffff, /* dst_mask */
b34976b6 535 TRUE), /* pcrel_offset */
5bd4f169 536
10ed1bba 537 /* 32-bit relocation to the symbol's procedure linkage table. */
5bd4f169
AM
538 HOWTO (R_PPC64_PLT32, /* type */
539 0, /* rightshift */
540 2, /* size (0 = byte, 1 = short, 2 = long) */
541 32, /* bitsize */
b34976b6 542 FALSE, /* pc_relative */
5bd4f169
AM
543 0, /* bitpos */
544 complain_overflow_bitfield, /* complain_on_overflow */
805fc799 545 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 546 "R_PPC64_PLT32", /* name */
b34976b6 547 FALSE, /* partial_inplace */
5bd4f169 548 0, /* src_mask */
f5e87a1d 549 0xffffffff, /* dst_mask */
b34976b6 550 FALSE), /* pcrel_offset */
5bd4f169
AM
551
552 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
553 FIXME: R_PPC64_PLTREL32 not supported. */
554 HOWTO (R_PPC64_PLTREL32, /* type */
555 0, /* rightshift */
556 2, /* size (0 = byte, 1 = short, 2 = long) */
557 32, /* bitsize */
b34976b6 558 TRUE, /* pc_relative */
5bd4f169
AM
559 0, /* bitpos */
560 complain_overflow_signed, /* complain_on_overflow */
561 bfd_elf_generic_reloc, /* special_function */
562 "R_PPC64_PLTREL32", /* name */
b34976b6 563 FALSE, /* partial_inplace */
5bd4f169 564 0, /* src_mask */
f5e87a1d 565 0xffffffff, /* dst_mask */
b34976b6 566 TRUE), /* pcrel_offset */
5bd4f169
AM
567
568 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
569 the symbol. */
570 HOWTO (R_PPC64_PLT16_LO, /* type */
571 0, /* rightshift */
572 1, /* size (0 = byte, 1 = short, 2 = long) */
573 16, /* bitsize */
b34976b6 574 FALSE, /* pc_relative */
5bd4f169
AM
575 0, /* bitpos */
576 complain_overflow_dont, /* complain_on_overflow */
805fc799 577 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 578 "R_PPC64_PLT16_LO", /* name */
b34976b6 579 FALSE, /* partial_inplace */
5bd4f169
AM
580 0, /* src_mask */
581 0xffff, /* dst_mask */
b34976b6 582 FALSE), /* pcrel_offset */
5bd4f169
AM
583
584 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
585 the symbol. */
586 HOWTO (R_PPC64_PLT16_HI, /* type */
587 16, /* rightshift */
588 1, /* size (0 = byte, 1 = short, 2 = long) */
589 16, /* bitsize */
b34976b6 590 FALSE, /* pc_relative */
5bd4f169
AM
591 0, /* bitpos */
592 complain_overflow_dont, /* complain_on_overflow */
805fc799 593 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 594 "R_PPC64_PLT16_HI", /* name */
b34976b6 595 FALSE, /* partial_inplace */
5bd4f169
AM
596 0, /* src_mask */
597 0xffff, /* dst_mask */
b34976b6 598 FALSE), /* pcrel_offset */
5bd4f169
AM
599
600 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
601 the symbol. */
602 HOWTO (R_PPC64_PLT16_HA, /* type */
603 16, /* rightshift */
604 1, /* size (0 = byte, 1 = short, 2 = long) */
605 16, /* bitsize */
b34976b6 606 FALSE, /* pc_relative */
5bd4f169
AM
607 0, /* bitpos */
608 complain_overflow_dont, /* complain_on_overflow */
805fc799 609 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 610 "R_PPC64_PLT16_HA", /* name */
b34976b6 611 FALSE, /* partial_inplace */
5bd4f169
AM
612 0, /* src_mask */
613 0xffff, /* dst_mask */
b34976b6 614 FALSE), /* pcrel_offset */
5bd4f169 615
c061c2d8 616 /* 16-bit section relative relocation. */
5bd4f169
AM
617 HOWTO (R_PPC64_SECTOFF, /* type */
618 0, /* rightshift */
c061c2d8
AM
619 1, /* size (0 = byte, 1 = short, 2 = long) */
620 16, /* bitsize */
b34976b6 621 FALSE, /* pc_relative */
5bd4f169
AM
622 0, /* bitpos */
623 complain_overflow_bitfield, /* complain_on_overflow */
805fc799 624 ppc64_elf_sectoff_reloc, /* special_function */
5bd4f169 625 "R_PPC64_SECTOFF", /* name */
b34976b6 626 FALSE, /* partial_inplace */
5bd4f169 627 0, /* src_mask */
c061c2d8 628 0xffff, /* dst_mask */
b34976b6 629 FALSE), /* pcrel_offset */
5bd4f169 630
c061c2d8 631 /* Like R_PPC64_SECTOFF, but no overflow warning. */
5bd4f169
AM
632 HOWTO (R_PPC64_SECTOFF_LO, /* type */
633 0, /* rightshift */
634 1, /* size (0 = byte, 1 = short, 2 = long) */
635 16, /* bitsize */
b34976b6 636 FALSE, /* pc_relative */
5bd4f169
AM
637 0, /* bitpos */
638 complain_overflow_dont, /* complain_on_overflow */
805fc799 639 ppc64_elf_sectoff_reloc, /* special_function */
5bd4f169 640 "R_PPC64_SECTOFF_LO", /* name */
b34976b6 641 FALSE, /* partial_inplace */
5bd4f169
AM
642 0, /* src_mask */
643 0xffff, /* dst_mask */
b34976b6 644 FALSE), /* pcrel_offset */
5bd4f169
AM
645
646 /* 16-bit upper half section relative relocation. */
647 HOWTO (R_PPC64_SECTOFF_HI, /* type */
648 16, /* rightshift */
649 1, /* size (0 = byte, 1 = short, 2 = long) */
650 16, /* bitsize */
b34976b6 651 FALSE, /* pc_relative */
5bd4f169
AM
652 0, /* bitpos */
653 complain_overflow_dont, /* complain_on_overflow */
805fc799 654 ppc64_elf_sectoff_reloc, /* special_function */
5bd4f169 655 "R_PPC64_SECTOFF_HI", /* name */
b34976b6 656 FALSE, /* partial_inplace */
5bd4f169
AM
657 0, /* src_mask */
658 0xffff, /* dst_mask */
b34976b6 659 FALSE), /* pcrel_offset */
5bd4f169
AM
660
661 /* 16-bit upper half adjusted section relative relocation. */
662 HOWTO (R_PPC64_SECTOFF_HA, /* type */
663 16, /* rightshift */
664 1, /* size (0 = byte, 1 = short, 2 = long) */
665 16, /* bitsize */
b34976b6 666 FALSE, /* pc_relative */
5bd4f169
AM
667 0, /* bitpos */
668 complain_overflow_dont, /* complain_on_overflow */
805fc799 669 ppc64_elf_sectoff_ha_reloc, /* special_function */
5bd4f169 670 "R_PPC64_SECTOFF_HA", /* name */
b34976b6 671 FALSE, /* partial_inplace */
5bd4f169
AM
672 0, /* src_mask */
673 0xffff, /* dst_mask */
b34976b6 674 FALSE), /* pcrel_offset */
5bd4f169 675
04c9666a
AM
676 /* Like R_PPC64_REL24 without touching the two least significant bits. */
677 HOWTO (R_PPC64_REL30, /* type */
5bd4f169
AM
678 2, /* rightshift */
679 2, /* size (0 = byte, 1 = short, 2 = long) */
680 30, /* bitsize */
b34976b6 681 TRUE, /* pc_relative */
5bd4f169
AM
682 0, /* bitpos */
683 complain_overflow_dont, /* complain_on_overflow */
684 bfd_elf_generic_reloc, /* special_function */
04c9666a 685 "R_PPC64_REL30", /* name */
b34976b6 686 FALSE, /* partial_inplace */
d006db6c 687 0, /* src_mask */
5bd4f169 688 0xfffffffc, /* dst_mask */
b34976b6 689 TRUE), /* pcrel_offset */
5bd4f169
AM
690
691 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
692
693 /* A standard 64-bit relocation. */
694 HOWTO (R_PPC64_ADDR64, /* type */
695 0, /* rightshift */
696 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
697 64, /* bitsize */
b34976b6 698 FALSE, /* pc_relative */
5bd4f169
AM
699 0, /* bitpos */
700 complain_overflow_dont, /* complain_on_overflow */
701 bfd_elf_generic_reloc, /* special_function */
702 "R_PPC64_ADDR64", /* name */
b34976b6 703 FALSE, /* partial_inplace */
5bd4f169 704 0, /* src_mask */
f5e87a1d 705 ONES (64), /* dst_mask */
b34976b6 706 FALSE), /* pcrel_offset */
5bd4f169
AM
707
708 /* The bits 32-47 of an address. */
709 HOWTO (R_PPC64_ADDR16_HIGHER, /* type */
710 32, /* rightshift */
711 1, /* size (0 = byte, 1 = short, 2 = long) */
712 16, /* bitsize */
b34976b6 713 FALSE, /* pc_relative */
5bd4f169
AM
714 0, /* bitpos */
715 complain_overflow_dont, /* complain_on_overflow */
716 bfd_elf_generic_reloc, /* special_function */
717 "R_PPC64_ADDR16_HIGHER", /* name */
b34976b6 718 FALSE, /* partial_inplace */
5bd4f169
AM
719 0, /* src_mask */
720 0xffff, /* dst_mask */
b34976b6 721 FALSE), /* pcrel_offset */
5bd4f169
AM
722
723 /* The bits 32-47 of an address, plus 1 if the contents of the low
724 16 bits, treated as a signed number, is negative. */
725 HOWTO (R_PPC64_ADDR16_HIGHERA, /* type */
726 32, /* rightshift */
727 1, /* size (0 = byte, 1 = short, 2 = long) */
728 16, /* bitsize */
b34976b6 729 FALSE, /* pc_relative */
5bd4f169
AM
730 0, /* bitpos */
731 complain_overflow_dont, /* complain_on_overflow */
805fc799 732 ppc64_elf_ha_reloc, /* special_function */
5bd4f169 733 "R_PPC64_ADDR16_HIGHERA", /* name */
b34976b6 734 FALSE, /* partial_inplace */
5bd4f169
AM
735 0, /* src_mask */
736 0xffff, /* dst_mask */
b34976b6 737 FALSE), /* pcrel_offset */
5bd4f169
AM
738
739 /* The bits 48-63 of an address. */
740 HOWTO (R_PPC64_ADDR16_HIGHEST,/* type */
741 48, /* rightshift */
742 1, /* size (0 = byte, 1 = short, 2 = long) */
743 16, /* bitsize */
b34976b6 744 FALSE, /* pc_relative */
5bd4f169
AM
745 0, /* bitpos */
746 complain_overflow_dont, /* complain_on_overflow */
747 bfd_elf_generic_reloc, /* special_function */
748 "R_PPC64_ADDR16_HIGHEST", /* name */
b34976b6 749 FALSE, /* partial_inplace */
5bd4f169
AM
750 0, /* src_mask */
751 0xffff, /* dst_mask */
b34976b6 752 FALSE), /* pcrel_offset */
5bd4f169
AM
753
754 /* The bits 48-63 of an address, plus 1 if the contents of the low
755 16 bits, treated as a signed number, is negative. */
756 HOWTO (R_PPC64_ADDR16_HIGHESTA,/* type */
757 48, /* rightshift */
758 1, /* size (0 = byte, 1 = short, 2 = long) */
759 16, /* bitsize */
b34976b6 760 FALSE, /* pc_relative */
5bd4f169
AM
761 0, /* bitpos */
762 complain_overflow_dont, /* complain_on_overflow */
805fc799 763 ppc64_elf_ha_reloc, /* special_function */
5bd4f169 764 "R_PPC64_ADDR16_HIGHESTA", /* name */
b34976b6 765 FALSE, /* partial_inplace */
5bd4f169
AM
766 0, /* src_mask */
767 0xffff, /* dst_mask */
b34976b6 768 FALSE), /* pcrel_offset */
5bd4f169
AM
769
770 /* Like ADDR64, but may be unaligned. */
771 HOWTO (R_PPC64_UADDR64, /* type */
772 0, /* rightshift */
773 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
774 64, /* bitsize */
b34976b6 775 FALSE, /* pc_relative */
5bd4f169
AM
776 0, /* bitpos */
777 complain_overflow_dont, /* complain_on_overflow */
778 bfd_elf_generic_reloc, /* special_function */
779 "R_PPC64_UADDR64", /* name */
b34976b6 780 FALSE, /* partial_inplace */
5bd4f169 781 0, /* src_mask */
f5e87a1d 782 ONES (64), /* dst_mask */
b34976b6 783 FALSE), /* pcrel_offset */
5bd4f169
AM
784
785 /* 64-bit relative relocation. */
786 HOWTO (R_PPC64_REL64, /* type */
787 0, /* rightshift */
788 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
789 64, /* bitsize */
b34976b6 790 TRUE, /* pc_relative */
5bd4f169
AM
791 0, /* bitpos */
792 complain_overflow_dont, /* complain_on_overflow */
793 bfd_elf_generic_reloc, /* special_function */
794 "R_PPC64_REL64", /* name */
b34976b6 795 FALSE, /* partial_inplace */
5bd4f169 796 0, /* src_mask */
f5e87a1d 797 ONES (64), /* dst_mask */
b34976b6 798 TRUE), /* pcrel_offset */
5bd4f169 799
cedb70c5 800 /* 64-bit relocation to the symbol's procedure linkage table. */
5bd4f169
AM
801 HOWTO (R_PPC64_PLT64, /* type */
802 0, /* rightshift */
803 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
804 64, /* bitsize */
b34976b6 805 FALSE, /* pc_relative */
5bd4f169
AM
806 0, /* bitpos */
807 complain_overflow_dont, /* complain_on_overflow */
805fc799 808 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 809 "R_PPC64_PLT64", /* name */
b34976b6 810 FALSE, /* partial_inplace */
5bd4f169 811 0, /* src_mask */
f5e87a1d 812 ONES (64), /* dst_mask */
b34976b6 813 FALSE), /* pcrel_offset */
5bd4f169
AM
814
815 /* 64-bit PC relative relocation to the symbol's procedure linkage
816 table. */
817 /* FIXME: R_PPC64_PLTREL64 not supported. */
818 HOWTO (R_PPC64_PLTREL64, /* type */
819 0, /* rightshift */
820 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
821 64, /* bitsize */
b34976b6 822 TRUE, /* pc_relative */
5bd4f169
AM
823 0, /* bitpos */
824 complain_overflow_dont, /* complain_on_overflow */
805fc799 825 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 826 "R_PPC64_PLTREL64", /* name */
b34976b6 827 FALSE, /* partial_inplace */
5bd4f169 828 0, /* src_mask */
f5e87a1d 829 ONES (64), /* dst_mask */
b34976b6 830 TRUE), /* pcrel_offset */
5bd4f169
AM
831
832 /* 16 bit TOC-relative relocation. */
833
834 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
835 HOWTO (R_PPC64_TOC16, /* type */
836 0, /* rightshift */
837 1, /* size (0 = byte, 1 = short, 2 = long) */
838 16, /* bitsize */
b34976b6 839 FALSE, /* pc_relative */
5bd4f169
AM
840 0, /* bitpos */
841 complain_overflow_signed, /* complain_on_overflow */
805fc799 842 ppc64_elf_toc_reloc, /* special_function */
5bd4f169 843 "R_PPC64_TOC16", /* name */
b34976b6 844 FALSE, /* partial_inplace */
5bd4f169
AM
845 0, /* src_mask */
846 0xffff, /* dst_mask */
b34976b6 847 FALSE), /* pcrel_offset */
5bd4f169
AM
848
849 /* 16 bit TOC-relative relocation without overflow. */
850
851 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
852 HOWTO (R_PPC64_TOC16_LO, /* type */
853 0, /* rightshift */
854 1, /* size (0 = byte, 1 = short, 2 = long) */
855 16, /* bitsize */
b34976b6 856 FALSE, /* pc_relative */
5bd4f169
AM
857 0, /* bitpos */
858 complain_overflow_dont, /* complain_on_overflow */
805fc799 859 ppc64_elf_toc_reloc, /* special_function */
5bd4f169 860 "R_PPC64_TOC16_LO", /* name */
b34976b6 861 FALSE, /* partial_inplace */
5bd4f169
AM
862 0, /* src_mask */
863 0xffff, /* dst_mask */
b34976b6 864 FALSE), /* pcrel_offset */
5bd4f169
AM
865
866 /* 16 bit TOC-relative relocation, high 16 bits. */
867
868 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
869 HOWTO (R_PPC64_TOC16_HI, /* type */
870 16, /* rightshift */
871 1, /* size (0 = byte, 1 = short, 2 = long) */
872 16, /* bitsize */
b34976b6 873 FALSE, /* pc_relative */
5bd4f169
AM
874 0, /* bitpos */
875 complain_overflow_dont, /* complain_on_overflow */
805fc799 876 ppc64_elf_toc_reloc, /* special_function */
5bd4f169 877 "R_PPC64_TOC16_HI", /* name */
b34976b6 878 FALSE, /* partial_inplace */
5bd4f169
AM
879 0, /* src_mask */
880 0xffff, /* dst_mask */
b34976b6 881 FALSE), /* pcrel_offset */
5bd4f169
AM
882
883 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
884 contents of the low 16 bits, treated as a signed number, is
885 negative. */
886
887 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
888 HOWTO (R_PPC64_TOC16_HA, /* type */
889 16, /* rightshift */
890 1, /* size (0 = byte, 1 = short, 2 = long) */
891 16, /* bitsize */
b34976b6 892 FALSE, /* pc_relative */
5bd4f169
AM
893 0, /* bitpos */
894 complain_overflow_dont, /* complain_on_overflow */
805fc799 895 ppc64_elf_toc_ha_reloc, /* special_function */
5bd4f169 896 "R_PPC64_TOC16_HA", /* name */
b34976b6 897 FALSE, /* partial_inplace */
5bd4f169
AM
898 0, /* src_mask */
899 0xffff, /* dst_mask */
b34976b6 900 FALSE), /* pcrel_offset */
5bd4f169
AM
901
902 /* 64-bit relocation; insert value of TOC base (.TOC.). */
903
904 /* R_PPC64_TOC 51 doubleword64 .TOC. */
905 HOWTO (R_PPC64_TOC, /* type */
906 0, /* rightshift */
907 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
908 64, /* bitsize */
b34976b6 909 FALSE, /* pc_relative */
5bd4f169
AM
910 0, /* bitpos */
911 complain_overflow_bitfield, /* complain_on_overflow */
805fc799 912 ppc64_elf_toc64_reloc, /* special_function */
5bd4f169 913 "R_PPC64_TOC", /* name */
b34976b6 914 FALSE, /* partial_inplace */
5bd4f169 915 0, /* src_mask */
f5e87a1d 916 ONES (64), /* dst_mask */
b34976b6 917 FALSE), /* pcrel_offset */
5bd4f169
AM
918
919 /* Like R_PPC64_GOT16, but also informs the link editor that the
920 value to relocate may (!) refer to a PLT entry which the link
921 editor (a) may replace with the symbol value. If the link editor
922 is unable to fully resolve the symbol, it may (b) create a PLT
923 entry and store the address to the new PLT entry in the GOT.
924 This permits lazy resolution of function symbols at run time.
925 The link editor may also skip all of this and just (c) emit a
926 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
927 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
928 HOWTO (R_PPC64_PLTGOT16, /* type */
929 0, /* rightshift */
930 1, /* size (0 = byte, 1 = short, 2 = long) */
931 16, /* bitsize */
b34976b6 932 FALSE, /* pc_relative */
5bd4f169
AM
933 0, /* bitpos */
934 complain_overflow_signed, /* complain_on_overflow */
805fc799 935 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb
AM
936 "R_PPC64_PLTGOT16", /* name */
937 FALSE, /* partial_inplace */
938 0, /* src_mask */
939 0xffff, /* dst_mask */
940 FALSE), /* pcrel_offset */
941
942 /* Like R_PPC64_PLTGOT16, but without overflow. */
943 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
944 HOWTO (R_PPC64_PLTGOT16_LO, /* type */
945 0, /* rightshift */
946 1, /* size (0 = byte, 1 = short, 2 = long) */
947 16, /* bitsize */
948 FALSE, /* pc_relative */
949 0, /* bitpos */
950 complain_overflow_dont, /* complain_on_overflow */
951 ppc64_elf_unhandled_reloc, /* special_function */
952 "R_PPC64_PLTGOT16_LO", /* name */
953 FALSE, /* partial_inplace */
954 0, /* src_mask */
955 0xffff, /* dst_mask */
956 FALSE), /* pcrel_offset */
957
958 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
959 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
960 HOWTO (R_PPC64_PLTGOT16_HI, /* type */
961 16, /* rightshift */
962 1, /* size (0 = byte, 1 = short, 2 = long) */
963 16, /* bitsize */
964 FALSE, /* pc_relative */
965 0, /* bitpos */
966 complain_overflow_dont, /* complain_on_overflow */
967 ppc64_elf_unhandled_reloc, /* special_function */
968 "R_PPC64_PLTGOT16_HI", /* name */
969 FALSE, /* partial_inplace */
970 0, /* src_mask */
971 0xffff, /* dst_mask */
972 FALSE), /* pcrel_offset */
973
974 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
975 1 if the contents of the low 16 bits, treated as a signed number,
976 is negative. */
977 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
978 HOWTO (R_PPC64_PLTGOT16_HA, /* type */
979 16, /* rightshift */
980 1, /* size (0 = byte, 1 = short, 2 = long) */
981 16, /* bitsize */
982 FALSE, /* pc_relative */
983 0, /* bitpos */
984 complain_overflow_dont,/* complain_on_overflow */
985 ppc64_elf_unhandled_reloc, /* special_function */
986 "R_PPC64_PLTGOT16_HA", /* name */
987 FALSE, /* partial_inplace */
988 0, /* src_mask */
989 0xffff, /* dst_mask */
990 FALSE), /* pcrel_offset */
991
992 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
993 HOWTO (R_PPC64_ADDR16_DS, /* type */
994 0, /* rightshift */
995 1, /* size (0 = byte, 1 = short, 2 = long) */
996 16, /* bitsize */
997 FALSE, /* pc_relative */
998 0, /* bitpos */
999 complain_overflow_bitfield, /* complain_on_overflow */
1000 bfd_elf_generic_reloc, /* special_function */
1001 "R_PPC64_ADDR16_DS", /* name */
1002 FALSE, /* partial_inplace */
1003 0, /* src_mask */
1004 0xfffc, /* dst_mask */
1005 FALSE), /* pcrel_offset */
1006
1007 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
1008 HOWTO (R_PPC64_ADDR16_LO_DS, /* type */
1009 0, /* rightshift */
1010 1, /* size (0 = byte, 1 = short, 2 = long) */
1011 16, /* bitsize */
1012 FALSE, /* pc_relative */
1013 0, /* bitpos */
1014 complain_overflow_dont,/* complain_on_overflow */
1015 bfd_elf_generic_reloc, /* special_function */
1016 "R_PPC64_ADDR16_LO_DS",/* name */
1017 FALSE, /* partial_inplace */
1018 0, /* src_mask */
1019 0xfffc, /* dst_mask */
1020 FALSE), /* pcrel_offset */
1021
1022 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
1023 HOWTO (R_PPC64_GOT16_DS, /* type */
1024 0, /* rightshift */
1025 1, /* size (0 = byte, 1 = short, 2 = long) */
1026 16, /* bitsize */
1027 FALSE, /* pc_relative */
1028 0, /* bitpos */
1029 complain_overflow_signed, /* complain_on_overflow */
1030 ppc64_elf_unhandled_reloc, /* special_function */
1031 "R_PPC64_GOT16_DS", /* name */
1032 FALSE, /* partial_inplace */
1033 0, /* src_mask */
1034 0xfffc, /* dst_mask */
1035 FALSE), /* pcrel_offset */
1036
1037 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
1038 HOWTO (R_PPC64_GOT16_LO_DS, /* type */
1039 0, /* rightshift */
1040 1, /* size (0 = byte, 1 = short, 2 = long) */
1041 16, /* bitsize */
1042 FALSE, /* pc_relative */
1043 0, /* bitpos */
1044 complain_overflow_dont, /* complain_on_overflow */
1045 ppc64_elf_unhandled_reloc, /* special_function */
1046 "R_PPC64_GOT16_LO_DS", /* name */
1047 FALSE, /* partial_inplace */
1048 0, /* src_mask */
1049 0xfffc, /* dst_mask */
1050 FALSE), /* pcrel_offset */
1051
1052 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
1053 HOWTO (R_PPC64_PLT16_LO_DS, /* type */
1054 0, /* rightshift */
1055 1, /* size (0 = byte, 1 = short, 2 = long) */
1056 16, /* bitsize */
1057 FALSE, /* pc_relative */
1058 0, /* bitpos */
1059 complain_overflow_dont, /* complain_on_overflow */
1060 ppc64_elf_unhandled_reloc, /* special_function */
1061 "R_PPC64_PLT16_LO_DS", /* name */
1062 FALSE, /* partial_inplace */
1063 0, /* src_mask */
1064 0xfffc, /* dst_mask */
1065 FALSE), /* pcrel_offset */
1066
1067 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
1068 HOWTO (R_PPC64_SECTOFF_DS, /* type */
1069 0, /* rightshift */
1070 1, /* size (0 = byte, 1 = short, 2 = long) */
1071 16, /* bitsize */
1072 FALSE, /* pc_relative */
1073 0, /* bitpos */
1074 complain_overflow_bitfield, /* complain_on_overflow */
1075 ppc64_elf_sectoff_reloc, /* special_function */
1076 "R_PPC64_SECTOFF_DS", /* name */
1077 FALSE, /* partial_inplace */
1078 0, /* src_mask */
1079 0xfffc, /* dst_mask */
1080 FALSE), /* pcrel_offset */
1081
1082 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
1083 HOWTO (R_PPC64_SECTOFF_LO_DS, /* type */
1084 0, /* rightshift */
1085 1, /* size (0 = byte, 1 = short, 2 = long) */
1086 16, /* bitsize */
1087 FALSE, /* pc_relative */
1088 0, /* bitpos */
1089 complain_overflow_dont, /* complain_on_overflow */
1090 ppc64_elf_sectoff_reloc, /* special_function */
1091 "R_PPC64_SECTOFF_LO_DS",/* name */
1092 FALSE, /* partial_inplace */
1093 0, /* src_mask */
1094 0xfffc, /* dst_mask */
1095 FALSE), /* pcrel_offset */
1096
1097 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
1098 HOWTO (R_PPC64_TOC16_DS, /* type */
1099 0, /* rightshift */
1100 1, /* size (0 = byte, 1 = short, 2 = long) */
1101 16, /* bitsize */
1102 FALSE, /* pc_relative */
1103 0, /* bitpos */
1104 complain_overflow_signed, /* complain_on_overflow */
1105 ppc64_elf_toc_reloc, /* special_function */
1106 "R_PPC64_TOC16_DS", /* name */
1107 FALSE, /* partial_inplace */
1108 0, /* src_mask */
1109 0xfffc, /* dst_mask */
1110 FALSE), /* pcrel_offset */
1111
1112 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
1113 HOWTO (R_PPC64_TOC16_LO_DS, /* type */
1114 0, /* rightshift */
1115 1, /* size (0 = byte, 1 = short, 2 = long) */
1116 16, /* bitsize */
1117 FALSE, /* pc_relative */
1118 0, /* bitpos */
1119 complain_overflow_dont, /* complain_on_overflow */
1120 ppc64_elf_toc_reloc, /* special_function */
1121 "R_PPC64_TOC16_LO_DS", /* name */
1122 FALSE, /* partial_inplace */
1123 0, /* src_mask */
1124 0xfffc, /* dst_mask */
1125 FALSE), /* pcrel_offset */
1126
1127 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
1128 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
1129 HOWTO (R_PPC64_PLTGOT16_DS, /* type */
1130 0, /* rightshift */
1131 1, /* size (0 = byte, 1 = short, 2 = long) */
1132 16, /* bitsize */
1133 FALSE, /* pc_relative */
1134 0, /* bitpos */
1135 complain_overflow_signed, /* complain_on_overflow */
1136 ppc64_elf_unhandled_reloc, /* special_function */
1137 "R_PPC64_PLTGOT16_DS", /* name */
1138 FALSE, /* partial_inplace */
1139 0, /* src_mask */
1140 0xfffc, /* dst_mask */
1141 FALSE), /* pcrel_offset */
1142
1143 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
1144 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1145 HOWTO (R_PPC64_PLTGOT16_LO_DS,/* type */
1146 0, /* rightshift */
1147 1, /* size (0 = byte, 1 = short, 2 = long) */
1148 16, /* bitsize */
1149 FALSE, /* pc_relative */
1150 0, /* bitpos */
1151 complain_overflow_dont, /* complain_on_overflow */
1152 ppc64_elf_unhandled_reloc, /* special_function */
1153 "R_PPC64_PLTGOT16_LO_DS",/* name */
1154 FALSE, /* partial_inplace */
1155 0, /* src_mask */
1156 0xfffc, /* dst_mask */
1157 FALSE), /* pcrel_offset */
1158
1159 /* Marker reloc for TLS. */
1160 HOWTO (R_PPC64_TLS,
1161 0, /* rightshift */
1162 2, /* size (0 = byte, 1 = short, 2 = long) */
1163 32, /* bitsize */
1164 FALSE, /* pc_relative */
1165 0, /* bitpos */
1166 complain_overflow_dont, /* complain_on_overflow */
1167 bfd_elf_generic_reloc, /* special_function */
1168 "R_PPC64_TLS", /* name */
1169 FALSE, /* partial_inplace */
1170 0, /* src_mask */
1171 0, /* dst_mask */
1172 FALSE), /* pcrel_offset */
1173
1174 /* Computes the load module index of the load module that contains the
1175 definition of its TLS sym. */
1176 HOWTO (R_PPC64_DTPMOD64,
1177 0, /* rightshift */
1178 4, /* size (0 = byte, 1 = short, 2 = long) */
1179 64, /* bitsize */
1180 FALSE, /* pc_relative */
1181 0, /* bitpos */
1182 complain_overflow_dont, /* complain_on_overflow */
1183 ppc64_elf_unhandled_reloc, /* special_function */
1184 "R_PPC64_DTPMOD64", /* name */
1185 FALSE, /* partial_inplace */
1186 0, /* src_mask */
1187 ONES (64), /* dst_mask */
1188 FALSE), /* pcrel_offset */
1189
1190 /* Computes a dtv-relative displacement, the difference between the value
1191 of sym+add and the base address of the thread-local storage block that
1192 contains the definition of sym, minus 0x8000. */
1193 HOWTO (R_PPC64_DTPREL64,
1194 0, /* rightshift */
1195 4, /* size (0 = byte, 1 = short, 2 = long) */
1196 64, /* bitsize */
1197 FALSE, /* pc_relative */
1198 0, /* bitpos */
1199 complain_overflow_dont, /* complain_on_overflow */
1200 ppc64_elf_unhandled_reloc, /* special_function */
1201 "R_PPC64_DTPREL64", /* name */
1202 FALSE, /* partial_inplace */
1203 0, /* src_mask */
1204 ONES (64), /* dst_mask */
1205 FALSE), /* pcrel_offset */
1206
1207 /* A 16 bit dtprel reloc. */
1208 HOWTO (R_PPC64_DTPREL16,
1209 0, /* rightshift */
1210 1, /* size (0 = byte, 1 = short, 2 = long) */
1211 16, /* bitsize */
1212 FALSE, /* pc_relative */
1213 0, /* bitpos */
1214 complain_overflow_signed, /* complain_on_overflow */
1215 ppc64_elf_unhandled_reloc, /* special_function */
1216 "R_PPC64_DTPREL16", /* name */
1217 FALSE, /* partial_inplace */
1218 0, /* src_mask */
1219 0xffff, /* dst_mask */
1220 FALSE), /* pcrel_offset */
1221
1222 /* Like DTPREL16, but no overflow. */
1223 HOWTO (R_PPC64_DTPREL16_LO,
1224 0, /* rightshift */
1225 1, /* size (0 = byte, 1 = short, 2 = long) */
1226 16, /* bitsize */
1227 FALSE, /* pc_relative */
1228 0, /* bitpos */
1229 complain_overflow_dont, /* complain_on_overflow */
1230 ppc64_elf_unhandled_reloc, /* special_function */
1231 "R_PPC64_DTPREL16_LO", /* name */
1232 FALSE, /* partial_inplace */
1233 0, /* src_mask */
1234 0xffff, /* dst_mask */
1235 FALSE), /* pcrel_offset */
1236
1237 /* Like DTPREL16_LO, but next higher group of 16 bits. */
1238 HOWTO (R_PPC64_DTPREL16_HI,
1239 16, /* rightshift */
1240 1, /* size (0 = byte, 1 = short, 2 = long) */
1241 16, /* bitsize */
1242 FALSE, /* pc_relative */
1243 0, /* bitpos */
1244 complain_overflow_dont, /* complain_on_overflow */
1245 ppc64_elf_unhandled_reloc, /* special_function */
1246 "R_PPC64_DTPREL16_HI", /* name */
1247 FALSE, /* partial_inplace */
1248 0, /* src_mask */
1249 0xffff, /* dst_mask */
1250 FALSE), /* pcrel_offset */
1251
1252 /* Like DTPREL16_HI, but adjust for low 16 bits. */
1253 HOWTO (R_PPC64_DTPREL16_HA,
1254 16, /* rightshift */
1255 1, /* size (0 = byte, 1 = short, 2 = long) */
1256 16, /* bitsize */
1257 FALSE, /* pc_relative */
1258 0, /* bitpos */
1259 complain_overflow_dont, /* complain_on_overflow */
1260 ppc64_elf_unhandled_reloc, /* special_function */
1261 "R_PPC64_DTPREL16_HA", /* name */
1262 FALSE, /* partial_inplace */
1263 0, /* src_mask */
1264 0xffff, /* dst_mask */
1265 FALSE), /* pcrel_offset */
1266
1267 /* Like DTPREL16_HI, but next higher group of 16 bits. */
1268 HOWTO (R_PPC64_DTPREL16_HIGHER,
1269 32, /* rightshift */
1270 1, /* size (0 = byte, 1 = short, 2 = long) */
1271 16, /* bitsize */
1272 FALSE, /* pc_relative */
1273 0, /* bitpos */
1274 complain_overflow_dont, /* complain_on_overflow */
1275 ppc64_elf_unhandled_reloc, /* special_function */
1276 "R_PPC64_DTPREL16_HIGHER", /* name */
1277 FALSE, /* partial_inplace */
1278 0, /* src_mask */
1279 0xffff, /* dst_mask */
1280 FALSE), /* pcrel_offset */
1281
1282 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
1283 HOWTO (R_PPC64_DTPREL16_HIGHERA,
1284 32, /* rightshift */
1285 1, /* size (0 = byte, 1 = short, 2 = long) */
1286 16, /* bitsize */
1287 FALSE, /* pc_relative */
1288 0, /* bitpos */
1289 complain_overflow_dont, /* complain_on_overflow */
1290 ppc64_elf_unhandled_reloc, /* special_function */
1291 "R_PPC64_DTPREL16_HIGHERA", /* name */
1292 FALSE, /* partial_inplace */
1293 0, /* src_mask */
1294 0xffff, /* dst_mask */
1295 FALSE), /* pcrel_offset */
1296
1297 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
1298 HOWTO (R_PPC64_DTPREL16_HIGHEST,
1299 48, /* rightshift */
1300 1, /* size (0 = byte, 1 = short, 2 = long) */
1301 16, /* bitsize */
1302 FALSE, /* pc_relative */
1303 0, /* bitpos */
1304 complain_overflow_dont, /* complain_on_overflow */
1305 ppc64_elf_unhandled_reloc, /* special_function */
1306 "R_PPC64_DTPREL16_HIGHEST", /* name */
1307 FALSE, /* partial_inplace */
1308 0, /* src_mask */
1309 0xffff, /* dst_mask */
1310 FALSE), /* pcrel_offset */
1311
1312 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
1313 HOWTO (R_PPC64_DTPREL16_HIGHESTA,
1314 48, /* rightshift */
1315 1, /* size (0 = byte, 1 = short, 2 = long) */
1316 16, /* bitsize */
1317 FALSE, /* pc_relative */
1318 0, /* bitpos */
1319 complain_overflow_dont, /* complain_on_overflow */
1320 ppc64_elf_unhandled_reloc, /* special_function */
1321 "R_PPC64_DTPREL16_HIGHESTA", /* name */
1322 FALSE, /* partial_inplace */
1323 0, /* src_mask */
1324 0xffff, /* dst_mask */
1325 FALSE), /* pcrel_offset */
1326
1327 /* Like DTPREL16, but for insns with a DS field. */
1328 HOWTO (R_PPC64_DTPREL16_DS,
1329 0, /* rightshift */
1330 1, /* size (0 = byte, 1 = short, 2 = long) */
1331 16, /* bitsize */
1332 FALSE, /* pc_relative */
1333 0, /* bitpos */
1334 complain_overflow_signed, /* complain_on_overflow */
1335 ppc64_elf_unhandled_reloc, /* special_function */
1336 "R_PPC64_DTPREL16_DS", /* name */
1337 FALSE, /* partial_inplace */
1338 0, /* src_mask */
1339 0xfffc, /* dst_mask */
1340 FALSE), /* pcrel_offset */
1341
1342 /* Like DTPREL16_DS, but no overflow. */
1343 HOWTO (R_PPC64_DTPREL16_LO_DS,
1344 0, /* rightshift */
1345 1, /* size (0 = byte, 1 = short, 2 = long) */
1346 16, /* bitsize */
1347 FALSE, /* pc_relative */
1348 0, /* bitpos */
1349 complain_overflow_dont, /* complain_on_overflow */
1350 ppc64_elf_unhandled_reloc, /* special_function */
1351 "R_PPC64_DTPREL16_LO_DS", /* name */
1352 FALSE, /* partial_inplace */
1353 0, /* src_mask */
1354 0xfffc, /* dst_mask */
1355 FALSE), /* pcrel_offset */
1356
1357 /* Computes a tp-relative displacement, the difference between the value of
1358 sym+add and the value of the thread pointer (r13). */
1359 HOWTO (R_PPC64_TPREL64,
1360 0, /* rightshift */
1361 4, /* size (0 = byte, 1 = short, 2 = long) */
1362 64, /* bitsize */
1363 FALSE, /* pc_relative */
1364 0, /* bitpos */
1365 complain_overflow_dont, /* complain_on_overflow */
1366 ppc64_elf_unhandled_reloc, /* special_function */
1367 "R_PPC64_TPREL64", /* name */
1368 FALSE, /* partial_inplace */
1369 0, /* src_mask */
1370 ONES (64), /* dst_mask */
1371 FALSE), /* pcrel_offset */
1372
1373 /* A 16 bit tprel reloc. */
1374 HOWTO (R_PPC64_TPREL16,
1375 0, /* rightshift */
1376 1, /* size (0 = byte, 1 = short, 2 = long) */
1377 16, /* bitsize */
1378 FALSE, /* pc_relative */
1379 0, /* bitpos */
1380 complain_overflow_signed, /* complain_on_overflow */
1381 ppc64_elf_unhandled_reloc, /* special_function */
1382 "R_PPC64_TPREL16", /* name */
1383 FALSE, /* partial_inplace */
1384 0, /* src_mask */
1385 0xffff, /* dst_mask */
1386 FALSE), /* pcrel_offset */
1387
1388 /* Like TPREL16, but no overflow. */
1389 HOWTO (R_PPC64_TPREL16_LO,
1390 0, /* rightshift */
1391 1, /* size (0 = byte, 1 = short, 2 = long) */
1392 16, /* bitsize */
1393 FALSE, /* pc_relative */
1394 0, /* bitpos */
1395 complain_overflow_dont, /* complain_on_overflow */
1396 ppc64_elf_unhandled_reloc, /* special_function */
1397 "R_PPC64_TPREL16_LO", /* name */
1398 FALSE, /* partial_inplace */
1399 0, /* src_mask */
1400 0xffff, /* dst_mask */
1401 FALSE), /* pcrel_offset */
1402
1403 /* Like TPREL16_LO, but next higher group of 16 bits. */
1404 HOWTO (R_PPC64_TPREL16_HI,
1405 16, /* rightshift */
1406 1, /* size (0 = byte, 1 = short, 2 = long) */
1407 16, /* bitsize */
1408 FALSE, /* pc_relative */
1409 0, /* bitpos */
1410 complain_overflow_dont, /* complain_on_overflow */
1411 ppc64_elf_unhandled_reloc, /* special_function */
1412 "R_PPC64_TPREL16_HI", /* name */
1413 FALSE, /* partial_inplace */
1414 0, /* src_mask */
1415 0xffff, /* dst_mask */
1416 FALSE), /* pcrel_offset */
1417
1418 /* Like TPREL16_HI, but adjust for low 16 bits. */
1419 HOWTO (R_PPC64_TPREL16_HA,
1420 16, /* rightshift */
1421 1, /* size (0 = byte, 1 = short, 2 = long) */
1422 16, /* bitsize */
1423 FALSE, /* pc_relative */
1424 0, /* bitpos */
1425 complain_overflow_dont, /* complain_on_overflow */
1426 ppc64_elf_unhandled_reloc, /* special_function */
1427 "R_PPC64_TPREL16_HA", /* name */
1428 FALSE, /* partial_inplace */
1429 0, /* src_mask */
1430 0xffff, /* dst_mask */
1431 FALSE), /* pcrel_offset */
1432
1433 /* Like TPREL16_HI, but next higher group of 16 bits. */
1434 HOWTO (R_PPC64_TPREL16_HIGHER,
1435 32, /* rightshift */
1436 1, /* size (0 = byte, 1 = short, 2 = long) */
1437 16, /* bitsize */
1438 FALSE, /* pc_relative */
1439 0, /* bitpos */
1440 complain_overflow_dont, /* complain_on_overflow */
1441 ppc64_elf_unhandled_reloc, /* special_function */
1442 "R_PPC64_TPREL16_HIGHER", /* name */
1443 FALSE, /* partial_inplace */
1444 0, /* src_mask */
1445 0xffff, /* dst_mask */
1446 FALSE), /* pcrel_offset */
1447
1448 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
1449 HOWTO (R_PPC64_TPREL16_HIGHERA,
1450 32, /* rightshift */
1451 1, /* size (0 = byte, 1 = short, 2 = long) */
1452 16, /* bitsize */
1453 FALSE, /* pc_relative */
1454 0, /* bitpos */
1455 complain_overflow_dont, /* complain_on_overflow */
1456 ppc64_elf_unhandled_reloc, /* special_function */
1457 "R_PPC64_TPREL16_HIGHERA", /* name */
1458 FALSE, /* partial_inplace */
1459 0, /* src_mask */
1460 0xffff, /* dst_mask */
1461 FALSE), /* pcrel_offset */
1462
1463 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
1464 HOWTO (R_PPC64_TPREL16_HIGHEST,
1465 48, /* rightshift */
1466 1, /* size (0 = byte, 1 = short, 2 = long) */
1467 16, /* bitsize */
1468 FALSE, /* pc_relative */
1469 0, /* bitpos */
1470 complain_overflow_dont, /* complain_on_overflow */
1471 ppc64_elf_unhandled_reloc, /* special_function */
1472 "R_PPC64_TPREL16_HIGHEST", /* name */
1473 FALSE, /* partial_inplace */
1474 0, /* src_mask */
1475 0xffff, /* dst_mask */
1476 FALSE), /* pcrel_offset */
1477
1478 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
1479 HOWTO (R_PPC64_TPREL16_HIGHESTA,
1480 48, /* rightshift */
1481 1, /* size (0 = byte, 1 = short, 2 = long) */
1482 16, /* bitsize */
1483 FALSE, /* pc_relative */
1484 0, /* bitpos */
1485 complain_overflow_dont, /* complain_on_overflow */
1486 ppc64_elf_unhandled_reloc, /* special_function */
1487 "R_PPC64_TPREL16_HIGHESTA", /* name */
1488 FALSE, /* partial_inplace */
1489 0, /* src_mask */
1490 0xffff, /* dst_mask */
1491 FALSE), /* pcrel_offset */
1492
1493 /* Like TPREL16, but for insns with a DS field. */
1494 HOWTO (R_PPC64_TPREL16_DS,
1495 0, /* rightshift */
1496 1, /* size (0 = byte, 1 = short, 2 = long) */
1497 16, /* bitsize */
1498 FALSE, /* pc_relative */
1499 0, /* bitpos */
1500 complain_overflow_signed, /* complain_on_overflow */
1501 ppc64_elf_unhandled_reloc, /* special_function */
1502 "R_PPC64_TPREL16_DS", /* name */
1503 FALSE, /* partial_inplace */
1504 0, /* src_mask */
1505 0xfffc, /* dst_mask */
1506 FALSE), /* pcrel_offset */
1507
1508 /* Like TPREL16_DS, but no overflow. */
1509 HOWTO (R_PPC64_TPREL16_LO_DS,
1510 0, /* rightshift */
1511 1, /* size (0 = byte, 1 = short, 2 = long) */
1512 16, /* bitsize */
1513 FALSE, /* pc_relative */
1514 0, /* bitpos */
1515 complain_overflow_dont, /* complain_on_overflow */
1516 ppc64_elf_unhandled_reloc, /* special_function */
1517 "R_PPC64_TPREL16_LO_DS", /* name */
1518 FALSE, /* partial_inplace */
1519 0, /* src_mask */
1520 0xfffc, /* dst_mask */
1521 FALSE), /* pcrel_offset */
1522
1523 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1524 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
1525 to the first entry relative to the TOC base (r2). */
1526 HOWTO (R_PPC64_GOT_TLSGD16,
1527 0, /* rightshift */
1528 1, /* size (0 = byte, 1 = short, 2 = long) */
1529 16, /* bitsize */
1530 FALSE, /* pc_relative */
1531 0, /* bitpos */
1532 complain_overflow_signed, /* complain_on_overflow */
1533 ppc64_elf_unhandled_reloc, /* special_function */
1534 "R_PPC64_GOT_TLSGD16", /* name */
b34976b6 1535 FALSE, /* partial_inplace */
5bd4f169
AM
1536 0, /* src_mask */
1537 0xffff, /* dst_mask */
b34976b6 1538 FALSE), /* pcrel_offset */
5bd4f169 1539
411e1bfb
AM
1540 /* Like GOT_TLSGD16, but no overflow. */
1541 HOWTO (R_PPC64_GOT_TLSGD16_LO,
5bd4f169
AM
1542 0, /* rightshift */
1543 1, /* size (0 = byte, 1 = short, 2 = long) */
1544 16, /* bitsize */
b34976b6 1545 FALSE, /* pc_relative */
5bd4f169
AM
1546 0, /* bitpos */
1547 complain_overflow_dont, /* complain_on_overflow */
805fc799 1548 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1549 "R_PPC64_GOT_TLSGD16_LO", /* name */
b34976b6 1550 FALSE, /* partial_inplace */
5bd4f169
AM
1551 0, /* src_mask */
1552 0xffff, /* dst_mask */
b34976b6 1553 FALSE), /* pcrel_offset */
5bd4f169 1554
411e1bfb
AM
1555 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
1556 HOWTO (R_PPC64_GOT_TLSGD16_HI,
5bd4f169
AM
1557 16, /* rightshift */
1558 1, /* size (0 = byte, 1 = short, 2 = long) */
1559 16, /* bitsize */
b34976b6 1560 FALSE, /* pc_relative */
5bd4f169
AM
1561 0, /* bitpos */
1562 complain_overflow_dont, /* complain_on_overflow */
805fc799 1563 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1564 "R_PPC64_GOT_TLSGD16_HI", /* name */
b34976b6 1565 FALSE, /* partial_inplace */
5bd4f169
AM
1566 0, /* src_mask */
1567 0xffff, /* dst_mask */
b34976b6 1568 FALSE), /* pcrel_offset */
5bd4f169 1569
411e1bfb
AM
1570 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
1571 HOWTO (R_PPC64_GOT_TLSGD16_HA,
5bd4f169
AM
1572 16, /* rightshift */
1573 1, /* size (0 = byte, 1 = short, 2 = long) */
1574 16, /* bitsize */
b34976b6 1575 FALSE, /* pc_relative */
5bd4f169 1576 0, /* bitpos */
411e1bfb 1577 complain_overflow_dont, /* complain_on_overflow */
805fc799 1578 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1579 "R_PPC64_GOT_TLSGD16_HA", /* name */
b34976b6 1580 FALSE, /* partial_inplace */
5bd4f169
AM
1581 0, /* src_mask */
1582 0xffff, /* dst_mask */
b34976b6 1583 FALSE), /* pcrel_offset */
5bd4f169 1584
411e1bfb
AM
1585 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1586 with values (sym+add)@dtpmod and zero, and computes the offset to the
1587 first entry relative to the TOC base (r2). */
1588 HOWTO (R_PPC64_GOT_TLSLD16,
5bd4f169
AM
1589 0, /* rightshift */
1590 1, /* size (0 = byte, 1 = short, 2 = long) */
1591 16, /* bitsize */
b34976b6 1592 FALSE, /* pc_relative */
5bd4f169 1593 0, /* bitpos */
411e1bfb
AM
1594 complain_overflow_signed, /* complain_on_overflow */
1595 ppc64_elf_unhandled_reloc, /* special_function */
1596 "R_PPC64_GOT_TLSLD16", /* name */
b34976b6 1597 FALSE, /* partial_inplace */
d006db6c 1598 0, /* src_mask */
411e1bfb 1599 0xffff, /* dst_mask */
b34976b6 1600 FALSE), /* pcrel_offset */
5bd4f169 1601
411e1bfb
AM
1602 /* Like GOT_TLSLD16, but no overflow. */
1603 HOWTO (R_PPC64_GOT_TLSLD16_LO,
5bd4f169
AM
1604 0, /* rightshift */
1605 1, /* size (0 = byte, 1 = short, 2 = long) */
1606 16, /* bitsize */
b34976b6 1607 FALSE, /* pc_relative */
5bd4f169 1608 0, /* bitpos */
411e1bfb
AM
1609 complain_overflow_dont, /* complain_on_overflow */
1610 ppc64_elf_unhandled_reloc, /* special_function */
1611 "R_PPC64_GOT_TLSLD16_LO", /* name */
b34976b6 1612 FALSE, /* partial_inplace */
d006db6c 1613 0, /* src_mask */
411e1bfb 1614 0xffff, /* dst_mask */
b34976b6 1615 FALSE), /* pcrel_offset */
5bd4f169 1616
411e1bfb
AM
1617 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
1618 HOWTO (R_PPC64_GOT_TLSLD16_HI,
1619 16, /* rightshift */
5bd4f169
AM
1620 1, /* size (0 = byte, 1 = short, 2 = long) */
1621 16, /* bitsize */
b34976b6 1622 FALSE, /* pc_relative */
5bd4f169 1623 0, /* bitpos */
411e1bfb 1624 complain_overflow_dont, /* complain_on_overflow */
805fc799 1625 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1626 "R_PPC64_GOT_TLSLD16_HI", /* name */
b34976b6 1627 FALSE, /* partial_inplace */
d006db6c 1628 0, /* src_mask */
411e1bfb 1629 0xffff, /* dst_mask */
b34976b6 1630 FALSE), /* pcrel_offset */
5bd4f169 1631
411e1bfb
AM
1632 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
1633 HOWTO (R_PPC64_GOT_TLSLD16_HA,
1634 16, /* rightshift */
5bd4f169
AM
1635 1, /* size (0 = byte, 1 = short, 2 = long) */
1636 16, /* bitsize */
b34976b6 1637 FALSE, /* pc_relative */
5bd4f169
AM
1638 0, /* bitpos */
1639 complain_overflow_dont, /* complain_on_overflow */
805fc799 1640 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1641 "R_PPC64_GOT_TLSLD16_HA", /* name */
b34976b6 1642 FALSE, /* partial_inplace */
d006db6c 1643 0, /* src_mask */
411e1bfb 1644 0xffff, /* dst_mask */
b34976b6 1645 FALSE), /* pcrel_offset */
5bd4f169 1646
411e1bfb
AM
1647 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1648 the offset to the entry relative to the TOC base (r2). */
1649 HOWTO (R_PPC64_GOT_DTPREL16_DS,
5bd4f169
AM
1650 0, /* rightshift */
1651 1, /* size (0 = byte, 1 = short, 2 = long) */
1652 16, /* bitsize */
b34976b6 1653 FALSE, /* pc_relative */
5bd4f169 1654 0, /* bitpos */
411e1bfb 1655 complain_overflow_signed, /* complain_on_overflow */
805fc799 1656 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1657 "R_PPC64_GOT_DTPREL16_DS", /* name */
b34976b6 1658 FALSE, /* partial_inplace */
d006db6c 1659 0, /* src_mask */
5bd4f169 1660 0xfffc, /* dst_mask */
b34976b6 1661 FALSE), /* pcrel_offset */
5bd4f169 1662
411e1bfb
AM
1663 /* Like GOT_DTPREL16_DS, but no overflow. */
1664 HOWTO (R_PPC64_GOT_DTPREL16_LO_DS,
5bd4f169 1665 0, /* rightshift */
c061c2d8
AM
1666 1, /* size (0 = byte, 1 = short, 2 = long) */
1667 16, /* bitsize */
b34976b6 1668 FALSE, /* pc_relative */
5bd4f169 1669 0, /* bitpos */
411e1bfb
AM
1670 complain_overflow_dont, /* complain_on_overflow */
1671 ppc64_elf_unhandled_reloc, /* special_function */
1672 "R_PPC64_GOT_DTPREL16_LO_DS", /* name */
b34976b6 1673 FALSE, /* partial_inplace */
d006db6c 1674 0, /* src_mask */
c061c2d8 1675 0xfffc, /* dst_mask */
b34976b6 1676 FALSE), /* pcrel_offset */
5bd4f169 1677
411e1bfb
AM
1678 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
1679 HOWTO (R_PPC64_GOT_DTPREL16_HI,
1680 16, /* rightshift */
5bd4f169
AM
1681 1, /* size (0 = byte, 1 = short, 2 = long) */
1682 16, /* bitsize */
b34976b6 1683 FALSE, /* pc_relative */
5bd4f169
AM
1684 0, /* bitpos */
1685 complain_overflow_dont, /* complain_on_overflow */
411e1bfb
AM
1686 ppc64_elf_unhandled_reloc, /* special_function */
1687 "R_PPC64_GOT_DTPREL16_HI", /* name */
b34976b6 1688 FALSE, /* partial_inplace */
d006db6c 1689 0, /* src_mask */
411e1bfb 1690 0xffff, /* dst_mask */
b34976b6 1691 FALSE), /* pcrel_offset */
5bd4f169 1692
411e1bfb
AM
1693 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
1694 HOWTO (R_PPC64_GOT_DTPREL16_HA,
1695 16, /* rightshift */
1696 1, /* size (0 = byte, 1 = short, 2 = long) */
1697 16, /* bitsize */
1698 FALSE, /* pc_relative */
1699 0, /* bitpos */
1700 complain_overflow_dont, /* complain_on_overflow */
1701 ppc64_elf_unhandled_reloc, /* special_function */
1702 "R_PPC64_GOT_DTPREL16_HA", /* name */
1703 FALSE, /* partial_inplace */
1704 0, /* src_mask */
1705 0xffff, /* dst_mask */
1706 FALSE), /* pcrel_offset */
1707
1708 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1709 offset to the entry relative to the TOC base (r2). */
1710 HOWTO (R_PPC64_GOT_TPREL16_DS,
5bd4f169
AM
1711 0, /* rightshift */
1712 1, /* size (0 = byte, 1 = short, 2 = long) */
1713 16, /* bitsize */
b34976b6 1714 FALSE, /* pc_relative */
5bd4f169
AM
1715 0, /* bitpos */
1716 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1717 ppc64_elf_unhandled_reloc, /* special_function */
1718 "R_PPC64_GOT_TPREL16_DS", /* name */
b34976b6 1719 FALSE, /* partial_inplace */
d006db6c 1720 0, /* src_mask */
411e1bfb 1721 0xffff, /* dst_mask */
b34976b6 1722 FALSE), /* pcrel_offset */
5bd4f169 1723
411e1bfb
AM
1724 /* Like GOT_TPREL16_DS, but no overflow. */
1725 HOWTO (R_PPC64_GOT_TPREL16_LO_DS,
5bd4f169
AM
1726 0, /* rightshift */
1727 1, /* size (0 = byte, 1 = short, 2 = long) */
1728 16, /* bitsize */
b34976b6 1729 FALSE, /* pc_relative */
5bd4f169
AM
1730 0, /* bitpos */
1731 complain_overflow_dont, /* complain_on_overflow */
411e1bfb
AM
1732 ppc64_elf_unhandled_reloc, /* special_function */
1733 "R_PPC64_GOT_TPREL16_LO_DS", /* name */
b34976b6 1734 FALSE, /* partial_inplace */
d006db6c 1735 0, /* src_mask */
411e1bfb 1736 0xffff, /* dst_mask */
b34976b6 1737 FALSE), /* pcrel_offset */
5bd4f169 1738
411e1bfb
AM
1739 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
1740 HOWTO (R_PPC64_GOT_TPREL16_HI,
1741 16, /* rightshift */
5bd4f169
AM
1742 1, /* size (0 = byte, 1 = short, 2 = long) */
1743 16, /* bitsize */
b34976b6 1744 FALSE, /* pc_relative */
5bd4f169 1745 0, /* bitpos */
411e1bfb 1746 complain_overflow_dont, /* complain_on_overflow */
805fc799 1747 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1748 "R_PPC64_GOT_TPREL16_HI", /* name */
b34976b6 1749 FALSE, /* partial_inplace */
d006db6c 1750 0, /* src_mask */
411e1bfb 1751 0xffff, /* dst_mask */
b34976b6 1752 FALSE), /* pcrel_offset */
5bd4f169 1753
411e1bfb
AM
1754 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
1755 HOWTO (R_PPC64_GOT_TPREL16_HA,
1756 16, /* rightshift */
5bd4f169
AM
1757 1, /* size (0 = byte, 1 = short, 2 = long) */
1758 16, /* bitsize */
b34976b6 1759 FALSE, /* pc_relative */
5bd4f169
AM
1760 0, /* bitpos */
1761 complain_overflow_dont, /* complain_on_overflow */
805fc799 1762 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1763 "R_PPC64_GOT_TPREL16_HA", /* name */
b34976b6 1764 FALSE, /* partial_inplace */
d006db6c 1765 0, /* src_mask */
411e1bfb 1766 0xffff, /* dst_mask */
b34976b6 1767 FALSE), /* pcrel_offset */
5bd4f169
AM
1768
1769 /* GNU extension to record C++ vtable hierarchy. */
1770 HOWTO (R_PPC64_GNU_VTINHERIT, /* type */
1771 0, /* rightshift */
1772 0, /* size (0 = byte, 1 = short, 2 = long) */
1773 0, /* bitsize */
b34976b6 1774 FALSE, /* pc_relative */
5bd4f169
AM
1775 0, /* bitpos */
1776 complain_overflow_dont, /* complain_on_overflow */
1777 NULL, /* special_function */
1778 "R_PPC64_GNU_VTINHERIT", /* name */
b34976b6 1779 FALSE, /* partial_inplace */
5bd4f169
AM
1780 0, /* src_mask */
1781 0, /* dst_mask */
b34976b6 1782 FALSE), /* pcrel_offset */
5bd4f169
AM
1783
1784 /* GNU extension to record C++ vtable member usage. */
1785 HOWTO (R_PPC64_GNU_VTENTRY, /* type */
1786 0, /* rightshift */
1787 0, /* size (0 = byte, 1 = short, 2 = long) */
1788 0, /* bitsize */
b34976b6 1789 FALSE, /* pc_relative */
5bd4f169
AM
1790 0, /* bitpos */
1791 complain_overflow_dont, /* complain_on_overflow */
1792 NULL, /* special_function */
1793 "R_PPC64_GNU_VTENTRY", /* name */
b34976b6 1794 FALSE, /* partial_inplace */
5bd4f169
AM
1795 0, /* src_mask */
1796 0, /* dst_mask */
b34976b6 1797 FALSE), /* pcrel_offset */
5bd4f169
AM
1798};
1799
1800\f
1801/* Initialize the ppc64_elf_howto_table, so that linear accesses can
1802 be done. */
1803
1804static void
1805ppc_howto_init ()
1806{
1807 unsigned int i, type;
1808
1809 for (i = 0;
1810 i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
1811 i++)
1812 {
1813 type = ppc64_elf_howto_raw[i].type;
1814 BFD_ASSERT (type < (sizeof (ppc64_elf_howto_table)
1815 / sizeof (ppc64_elf_howto_table[0])));
1816 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
1817 }
1818}
1819
1820static reloc_howto_type *
1821ppc64_elf_reloc_type_lookup (abfd, code)
1822 bfd *abfd ATTRIBUTE_UNUSED;
1823 bfd_reloc_code_real_type code;
1824{
411e1bfb 1825 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
5bd4f169
AM
1826
1827 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
1828 /* Initialize howto table if needed. */
1829 ppc_howto_init ();
1830
1831 switch ((int) code)
1832 {
1833 default:
1834 return (reloc_howto_type *) NULL;
1835
411e1bfb
AM
1836 case BFD_RELOC_NONE: r = R_PPC64_NONE;
1837 break;
1838 case BFD_RELOC_32: r = R_PPC64_ADDR32;
1839 break;
1840 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
1841 break;
1842 case BFD_RELOC_16: r = R_PPC64_ADDR16;
1843 break;
1844 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
1845 break;
1846 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
1847 break;
1848 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
5bd4f169 1849 break;
411e1bfb 1850 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
5bd4f169 1851 break;
411e1bfb 1852 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
5bd4f169 1853 break;
411e1bfb 1854 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
5bd4f169 1855 break;
411e1bfb 1856 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
5bd4f169 1857 break;
411e1bfb 1858 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
5bd4f169 1859 break;
411e1bfb 1860 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
5bd4f169 1861 break;
411e1bfb 1862 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
5bd4f169 1863 break;
411e1bfb 1864 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
5bd4f169 1865 break;
411e1bfb 1866 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
5bd4f169 1867 break;
411e1bfb 1868 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
5bd4f169 1869 break;
411e1bfb 1870 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
5bd4f169 1871 break;
411e1bfb 1872 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
5bd4f169 1873 break;
411e1bfb 1874 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
5bd4f169 1875 break;
411e1bfb 1876 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
5bd4f169 1877 break;
411e1bfb 1878 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
5bd4f169 1879 break;
411e1bfb 1880 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
5bd4f169 1881 break;
411e1bfb 1882 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
5bd4f169 1883 break;
411e1bfb 1884 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
5bd4f169 1885 break;
411e1bfb 1886 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
5bd4f169 1887 break;
411e1bfb 1888 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
5bd4f169 1889 break;
411e1bfb 1890 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
5bd4f169 1891 break;
411e1bfb 1892 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
5bd4f169 1893 break;
411e1bfb 1894 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
5bd4f169 1895 break;
411e1bfb 1896 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
5bd4f169 1897 break;
411e1bfb 1898 case BFD_RELOC_64: r = R_PPC64_ADDR64;
5bd4f169 1899 break;
411e1bfb 1900 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
5bd4f169 1901 break;
411e1bfb 1902 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
5bd4f169 1903 break;
411e1bfb 1904 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
5bd4f169 1905 break;
411e1bfb 1906 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
5bd4f169 1907 break;
411e1bfb 1908 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
5bd4f169 1909 break;
411e1bfb 1910 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
5bd4f169 1911 break;
411e1bfb 1912 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
5bd4f169 1913 break;
411e1bfb 1914 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
5bd4f169 1915 break;
411e1bfb 1916 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
5bd4f169 1917 break;
411e1bfb 1918 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
5bd4f169 1919 break;
411e1bfb 1920 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
5bd4f169 1921 break;
411e1bfb 1922 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
5bd4f169 1923 break;
411e1bfb 1924 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
5bd4f169 1925 break;
411e1bfb 1926 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
5bd4f169 1927 break;
411e1bfb 1928 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
5bd4f169 1929 break;
411e1bfb 1930 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
5bd4f169 1931 break;
411e1bfb 1932 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
5bd4f169 1933 break;
411e1bfb 1934 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
5bd4f169 1935 break;
411e1bfb 1936 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
5bd4f169 1937 break;
411e1bfb 1938 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
5bd4f169 1939 break;
411e1bfb 1940 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
5bd4f169 1941 break;
411e1bfb 1942 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
5bd4f169 1943 break;
411e1bfb 1944 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
5bd4f169 1945 break;
411e1bfb 1946 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
5bd4f169 1947 break;
411e1bfb 1948 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
5bd4f169 1949 break;
411e1bfb 1950 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
5bd4f169 1951 break;
411e1bfb 1952 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
5bd4f169 1953 break;
411e1bfb 1954 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
5bd4f169 1955 break;
411e1bfb 1956 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
5bd4f169 1957 break;
411e1bfb 1958 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
5bd4f169 1959 break;
411e1bfb 1960 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
5bd4f169 1961 break;
411e1bfb 1962 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
5bd4f169 1963 break;
411e1bfb 1964 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
5bd4f169 1965 break;
411e1bfb 1966 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
5bd4f169 1967 break;
411e1bfb
AM
1968 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
1969 break;
1970 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
1971 break;
1972 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
1973 break;
1974 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
1975 break;
1976 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
1977 break;
1978 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
1979 break;
1980 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
1981 break;
1982 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
1983 break;
1984 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
1985 break;
1986 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
1987 break;
1988 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
1989 break;
1990 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
1991 break;
1992 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
1993 break;
1994 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
1995 break;
1996 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
1997 break;
1998 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
1999 break;
2000 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
2001 break;
2002 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
2003 break;
2004 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
2005 break;
2006 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
2007 break;
2008 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
2009 break;
2010 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
2011 break;
2012 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
2013 break;
2014 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
2015 break;
2016 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
2017 break;
2018 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
2019 break;
2020 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
2021 break;
2022 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
2023 break;
2024 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
2025 break;
2026 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
2027 break;
2028 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
2029 break;
2030 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
2031 break;
2032 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
2033 break;
2034 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
2035 break;
2036 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
5bd4f169
AM
2037 break;
2038 }
2039
411e1bfb 2040 return ppc64_elf_howto_table[(int) r];
5bd4f169
AM
2041};
2042
2043/* Set the howto pointer for a PowerPC ELF reloc. */
2044
2045static void
2046ppc64_elf_info_to_howto (abfd, cache_ptr, dst)
2047 bfd *abfd ATTRIBUTE_UNUSED;
2048 arelent *cache_ptr;
947216bf 2049 Elf_Internal_Rela *dst;
5bd4f169 2050{
65f38f15
AM
2051 unsigned int type;
2052
ef60b7ff 2053 /* Initialize howto table if needed. */
5bd4f169 2054 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
2055 ppc_howto_init ();
2056
65f38f15
AM
2057 type = ELF64_R_TYPE (dst->r_info);
2058 BFD_ASSERT (type < (sizeof (ppc64_elf_howto_table)
2059 / sizeof (ppc64_elf_howto_table[0])));
2060 cache_ptr->howto = ppc64_elf_howto_table[type];
5bd4f169
AM
2061}
2062
04c9666a 2063/* Handle the R_PPC64_ADDR16_HA and similar relocs. */
5bd4f169
AM
2064
2065static bfd_reloc_status_type
805fc799
AM
2066ppc64_elf_ha_reloc (abfd, reloc_entry, symbol, data,
2067 input_section, output_bfd, error_message)
2068 bfd *abfd;
5bd4f169
AM
2069 arelent *reloc_entry;
2070 asymbol *symbol;
805fc799 2071 PTR data;
5bd4f169
AM
2072 asection *input_section;
2073 bfd *output_bfd;
805fc799 2074 char **error_message;
5bd4f169 2075{
805fc799
AM
2076 /* If this is a relocatable link (output_bfd test tells us), just
2077 call the generic function. Any adjustment will be done at final
2078 link time. */
2079 if (output_bfd != NULL)
cedb70c5 2080 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2081 input_section, output_bfd, error_message);
2082
2083 /* Adjust the addend for sign extension of the low 16 bits.
2084 We won't actually be using the low 16 bits, so trashing them
2085 doesn't matter. */
2086 reloc_entry->addend += 0x8000;
2087 return bfd_reloc_continue;
2088}
5bd4f169 2089
805fc799
AM
2090static bfd_reloc_status_type
2091ppc64_elf_brtaken_reloc (abfd, reloc_entry, symbol, data,
2092 input_section, output_bfd, error_message)
2093 bfd *abfd;
2094 arelent *reloc_entry;
2095 asymbol *symbol;
2096 PTR data;
2097 asection *input_section;
2098 bfd *output_bfd;
2099 char **error_message;
2100{
2101 long insn;
04c9666a 2102 enum elf_ppc64_reloc_type r_type;
805fc799
AM
2103 bfd_size_type octets;
2104 /* Disabled until we sort out how ld should choose 'y' vs 'at'. */
b34976b6 2105 bfd_boolean is_power4 = FALSE;
805fc799
AM
2106
2107 /* If this is a relocatable link (output_bfd test tells us), just
2108 call the generic function. Any adjustment will be done at final
2109 link time. */
5bd4f169 2110 if (output_bfd != NULL)
cedb70c5 2111 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2112 input_section, output_bfd, error_message);
2113
2114 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2115 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2116 insn &= ~(0x01 << 21);
04c9666a 2117 r_type = (enum elf_ppc64_reloc_type) reloc_entry->howto->type;
805fc799
AM
2118 if (r_type == R_PPC64_ADDR14_BRTAKEN
2119 || r_type == R_PPC64_REL14_BRTAKEN)
cedb70c5 2120 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
805fc799
AM
2121
2122 if (is_power4)
5bd4f169 2123 {
805fc799
AM
2124 /* Set 'a' bit. This is 0b00010 in BO field for branch
2125 on CR(BI) insns (BO == 001at or 011at), and 0b01000
2126 for branch on CTR insns (BO == 1a00t or 1a01t). */
2127 if ((insn & (0x14 << 21)) == (0x04 << 21))
2128 insn |= 0x02 << 21;
2129 else if ((insn & (0x14 << 21)) == (0x10 << 21))
2130 insn |= 0x08 << 21;
2131 else
2132 return bfd_reloc_continue;
5bd4f169 2133 }
805fc799
AM
2134 else
2135 {
2136 bfd_vma target = 0;
2137 bfd_vma from;
5bd4f169 2138
805fc799
AM
2139 if (!bfd_is_com_section (symbol->section))
2140 target = symbol->value;
2141 target += symbol->section->output_section->vma;
2142 target += symbol->section->output_offset;
2143 target += reloc_entry->addend;
5bd4f169 2144
805fc799
AM
2145 from = (reloc_entry->address
2146 + input_section->output_offset
2147 + input_section->output_section->vma);
5bd4f169 2148
805fc799
AM
2149 /* Invert 'y' bit if not the default. */
2150 if ((bfd_signed_vma) (target - from) < 0)
2151 insn ^= 0x01 << 21;
2152 }
2153 bfd_put_32 (abfd, (bfd_vma) insn, (bfd_byte *) data + octets);
2154 return bfd_reloc_continue;
2155}
5bd4f169 2156
805fc799
AM
2157static bfd_reloc_status_type
2158ppc64_elf_sectoff_reloc (abfd, reloc_entry, symbol, data,
2159 input_section, output_bfd, error_message)
2160 bfd *abfd;
2161 arelent *reloc_entry;
2162 asymbol *symbol;
2163 PTR data;
2164 asection *input_section;
2165 bfd *output_bfd;
2166 char **error_message;
2167{
2168 /* If this is a relocatable link (output_bfd test tells us), just
2169 call the generic function. Any adjustment will be done at final
2170 link time. */
2171 if (output_bfd != NULL)
cedb70c5 2172 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799 2173 input_section, output_bfd, error_message);
5bd4f169 2174
805fc799
AM
2175 /* Subtract the symbol section base address. */
2176 reloc_entry->addend -= symbol->section->output_section->vma;
5bd4f169
AM
2177 return bfd_reloc_continue;
2178}
2179
805fc799
AM
2180static bfd_reloc_status_type
2181ppc64_elf_sectoff_ha_reloc (abfd, reloc_entry, symbol, data,
2182 input_section, output_bfd, error_message)
2183 bfd *abfd;
2184 arelent *reloc_entry;
2185 asymbol *symbol;
2186 PTR data;
2187 asection *input_section;
2188 bfd *output_bfd;
2189 char **error_message;
2190{
2191 /* If this is a relocatable link (output_bfd test tells us), just
2192 call the generic function. Any adjustment will be done at final
2193 link time. */
2194 if (output_bfd != NULL)
cedb70c5 2195 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2196 input_section, output_bfd, error_message);
2197
2198 /* Subtract the symbol section base address. */
2199 reloc_entry->addend -= symbol->section->output_section->vma;
2200
2201 /* Adjust the addend for sign extension of the low 16 bits. */
2202 reloc_entry->addend += 0x8000;
2203 return bfd_reloc_continue;
2204}
2205
2206static bfd_reloc_status_type
2207ppc64_elf_toc_reloc (abfd, reloc_entry, symbol, data,
2208 input_section, output_bfd, error_message)
2209 bfd *abfd;
2210 arelent *reloc_entry;
2211 asymbol *symbol;
2212 PTR data;
2213 asection *input_section;
2214 bfd *output_bfd;
2215 char **error_message;
2216{
2217 bfd_vma TOCstart;
2218
2219 /* If this is a relocatable link (output_bfd test tells us), just
2220 call the generic function. Any adjustment will be done at final
2221 link time. */
2222 if (output_bfd != NULL)
cedb70c5 2223 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2224 input_section, output_bfd, error_message);
2225
2226 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2227 if (TOCstart == 0)
2228 TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2229
2230 /* Subtract the TOC base address. */
2231 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2232 return bfd_reloc_continue;
2233}
2234
2235static bfd_reloc_status_type
2236ppc64_elf_toc_ha_reloc (abfd, reloc_entry, symbol, data,
2237 input_section, output_bfd, error_message)
2238 bfd *abfd;
2239 arelent *reloc_entry;
2240 asymbol *symbol;
2241 PTR data;
2242 asection *input_section;
2243 bfd *output_bfd;
2244 char **error_message;
2245{
2246 bfd_vma TOCstart;
2247
2248 /* If this is a relocatable link (output_bfd test tells us), just
2249 call the generic function. Any adjustment will be done at final
2250 link time. */
2251 if (output_bfd != NULL)
cedb70c5 2252 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2253 input_section, output_bfd, error_message);
2254
2255 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2256 if (TOCstart == 0)
2257 TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2258
2259 /* Subtract the TOC base address. */
2260 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2261
2262 /* Adjust the addend for sign extension of the low 16 bits. */
2263 reloc_entry->addend += 0x8000;
2264 return bfd_reloc_continue;
2265}
2266
2267static bfd_reloc_status_type
2268ppc64_elf_toc64_reloc (abfd, reloc_entry, symbol, data,
2269 input_section, output_bfd, error_message)
2270 bfd *abfd;
2271 arelent *reloc_entry;
2272 asymbol *symbol;
2273 PTR data;
2274 asection *input_section;
2275 bfd *output_bfd;
2276 char **error_message;
2277{
2278 bfd_vma TOCstart;
2279 bfd_size_type octets;
2280
2281 /* If this is a relocatable link (output_bfd test tells us), just
2282 call the generic function. Any adjustment will be done at final
2283 link time. */
2284 if (output_bfd != NULL)
cedb70c5 2285 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2286 input_section, output_bfd, error_message);
2287
2288 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2289 if (TOCstart == 0)
2290 TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2291
2292 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2293 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
2294 return bfd_reloc_ok;
2295}
2296
2297static bfd_reloc_status_type
2298ppc64_elf_unhandled_reloc (abfd, reloc_entry, symbol, data,
2299 input_section, output_bfd, error_message)
2300 bfd *abfd;
2301 arelent *reloc_entry;
2302 asymbol *symbol;
2303 PTR data;
2304 asection *input_section;
2305 bfd *output_bfd;
2306 char **error_message;
2307{
2308 /* If this is a relocatable link (output_bfd test tells us), just
2309 call the generic function. Any adjustment will be done at final
2310 link time. */
2311 if (output_bfd != NULL)
cedb70c5 2312 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2313 input_section, output_bfd, error_message);
2314
2315 if (error_message != NULL)
2316 {
2317 static char buf[60];
2318 sprintf (buf, "generic linker can't handle %s",
2319 reloc_entry->howto->name);
2320 *error_message = buf;
2321 }
2322 return bfd_reloc_dangerous;
2323}
2324
feee612b
AM
2325/* Fix bad default arch selected for a 64 bit input bfd when the
2326 default is 32 bit. */
2327
b34976b6 2328static bfd_boolean
feee612b
AM
2329ppc64_elf_object_p (abfd)
2330 bfd *abfd;
2331{
2332 if (abfd->arch_info->the_default && abfd->arch_info->bits_per_word == 32)
2333 {
2334 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
2335
2336 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
2337 {
2338 /* Relies on arch after 32 bit default being 64 bit default. */
2339 abfd->arch_info = abfd->arch_info->next;
2340 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
2341 }
2342 }
b34976b6 2343 return TRUE;
feee612b
AM
2344}
2345
5bd4f169
AM
2346/* Merge backend specific data from an object file to the output
2347 object file when linking. */
2f6d9989 2348
b34976b6 2349static bfd_boolean
5bd4f169
AM
2350ppc64_elf_merge_private_bfd_data (ibfd, obfd)
2351 bfd *ibfd;
2352 bfd *obfd;
2353{
5bd4f169
AM
2354 /* Check if we have the same endianess. */
2355 if (ibfd->xvec->byteorder != obfd->xvec->byteorder
87e226ce 2356 && ibfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN
5bd4f169
AM
2357 && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
2358 {
2359 const char *msg;
2360
2361 if (bfd_big_endian (ibfd))
2362 msg = _("%s: compiled for a big endian system and target is little endian");
2363 else
2364 msg = _("%s: compiled for a little endian system and target is big endian");
2365
8f615d07 2366 (*_bfd_error_handler) (msg, bfd_archive_filename (ibfd));
5bd4f169
AM
2367
2368 bfd_set_error (bfd_error_wrong_format);
b34976b6 2369 return FALSE;
5bd4f169
AM
2370 }
2371
b34976b6 2372 return TRUE;
5bd4f169 2373}
f0abc2a1
AM
2374
2375struct _ppc64_elf_section_data
2376{
2377 struct bfd_elf_section_data elf;
411e1bfb
AM
2378
2379 /* An array with one entry for each opd function descriptor. */
f0abc2a1
AM
2380 union
2381 {
411e1bfb 2382 /* Points to the function code section for local opd entries. */
f0abc2a1 2383 asection **func_sec;
411e1bfb 2384 /* After editing .opd, adjust references to opd local syms. */
f0abc2a1
AM
2385 long *adjust;
2386 } opd;
411e1bfb
AM
2387
2388 /* An array for toc sections, indexed by offset/8.
2389 Specifies the relocation symbol index used at a given toc offset. */
2390 unsigned *t_symndx;
f0abc2a1
AM
2391};
2392
2393#define ppc64_elf_section_data(sec) \
411e1bfb 2394 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
f0abc2a1
AM
2395
2396static bfd_boolean
2397ppc64_elf_new_section_hook (abfd, sec)
2398 bfd *abfd;
2399 asection *sec;
2400{
2401 struct _ppc64_elf_section_data *sdata;
2402 bfd_size_type amt = sizeof (*sdata);
2403
2404 sdata = (struct _ppc64_elf_section_data *) bfd_zalloc (abfd, amt);
2405 if (sdata == NULL)
2406 return FALSE;
2407 sec->used_by_bfd = (PTR) sdata;
2408
2409 return _bfd_elf_new_section_hook (abfd, sec);
2410}
5bd4f169 2411\f
65f38f15
AM
2412/* The following functions are specific to the ELF linker, while
2413 functions above are used generally. Those named ppc64_elf_* are
2414 called by the main ELF linker code. They appear in this file more
2415 or less in the order in which they are called. eg.
2416 ppc64_elf_check_relocs is called early in the link process,
2417 ppc64_elf_finish_dynamic_sections is one of the last functions
e86ce104
AM
2418 called.
2419
2420 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
2421 functions have both a function code symbol and a function descriptor
2422 symbol. A call to foo in a relocatable object file looks like:
2423
2424 . .text
2425 . x:
2426 . bl .foo
2427 . nop
2428
2429 The function definition in another object file might be:
2430
2431 . .section .opd
2432 . foo: .quad .foo
2433 . .quad .TOC.@tocbase
2434 . .quad 0
2435 .
2436 . .text
2437 . .foo: blr
2438
2439 When the linker resolves the call during a static link, the branch
2440 unsurprisingly just goes to .foo and the .opd information is unused.
2441 If the function definition is in a shared library, things are a little
2442 different: The call goes via a plt call stub, the opd information gets
2443 copied to the plt, and the linker patches the nop.
2444
2445 . x:
2446 . bl .foo_stub
2447 . ld 2,40(1)
2448 .
2449 .
2450 . .foo_stub:
2451 . addis 12,2,Lfoo@toc@ha # in practice, the call stub
411e1bfb 2452 . addi 12,12,Lfoo@toc@l # is slightly optimized, but
e86ce104
AM
2453 . std 2,40(1) # this is the general idea
2454 . ld 11,0(12)
2455 . ld 2,8(12)
2456 . mtctr 11
2457 . ld 11,16(12)
2458 . bctr
2459 .
2460 . .section .plt
2461 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
2462
2463 The "reloc ()" notation is supposed to indicate that the linker emits
2464 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
2465 copying.
2466
2467 What are the difficulties here? Well, firstly, the relocations
2468 examined by the linker in check_relocs are against the function code
2469 sym .foo, while the dynamic relocation in the plt is emitted against
2470 the function descriptor symbol, foo. Somewhere along the line, we need
2471 to carefully copy dynamic link information from one symbol to the other.
2472 Secondly, the generic part of the elf linker will make .foo a dynamic
2473 symbol as is normal for most other backends. We need foo dynamic
2474 instead, at least for an application final link. However, when
2475 creating a shared library containing foo, we need to have both symbols
2476 dynamic so that references to .foo are satisfied during the early
2477 stages of linking. Otherwise the linker might decide to pull in a
41bd81ab 2478 definition from some other object, eg. a static library. */
65f38f15
AM
2479
2480/* The linker needs to keep track of the number of relocs that it
2481 decides to copy as dynamic relocs in check_relocs for each symbol.
2482 This is so that it can later discard them if they are found to be
2483 unnecessary. We store the information in a field extending the
2484 regular ELF linker hash table. */
2485
2486struct ppc_dyn_relocs
2487{
2488 struct ppc_dyn_relocs *next;
2489
2490 /* The input section of the reloc. */
2491 asection *sec;
2492
2493 /* Total number of relocs copied for the input section. */
2494 bfd_size_type count;
2495
2496 /* Number of pc-relative relocs copied for the input section. */
2497 bfd_size_type pc_count;
2498};
2499
411e1bfb
AM
2500/* Track GOT entries needed for a given symbol. We might need more
2501 than one got entry per symbol. */
2502struct got_entry
2503{
2504 struct got_entry *next;
2505
2506 bfd_vma addend;
2507
2508 union
2509 {
2510 bfd_signed_vma refcount;
2511 bfd_vma offset;
2512 } got;
2513
2514 char tls_type;
2515};
2516
2517/* The same for PLT. */
2518struct plt_entry
2519{
2520 struct plt_entry *next;
2521
2522 bfd_vma addend;
2523
2524 union
2525 {
2526 bfd_signed_vma refcount;
2527 bfd_vma offset;
2528 } plt;
2529};
2530
65f38f15 2531/* Of those relocs that might be copied as dynamic relocs, this macro
411e1bfb 2532 selects those that must be copied when linking a shared library. */
65f38f15 2533
411e1bfb 2534#define MUST_BE_DYN_RELOC(RTYPE) \
805fc799
AM
2535 ((RTYPE) != R_PPC64_REL32 \
2536 && (RTYPE) != R_PPC64_REL64 \
04c9666a 2537 && (RTYPE) != R_PPC64_REL30)
65f38f15 2538
721956f4
AM
2539/* Section name for stubs is the associated section name plus this
2540 string. */
2541#define STUB_SUFFIX ".stub"
2542
2543/* Linker stubs.
2544 ppc_stub_long_branch:
2545 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
2546 destination, but a 24 bit branch in a stub section will reach.
2547 . b dest
2548
2549 ppc_stub_plt_branch:
2550 Similar to the above, but a 24 bit branch in the stub section won't
2551 reach its destination.
87e226ce
AM
2552 . addis %r12,%r2,xxx@toc@ha
2553 . ld %r11,xxx@toc@l(%r12)
721956f4
AM
2554 . mtctr %r11
2555 . bctr
2556
2557 ppc_stub_plt_call:
2558 Used to call a function in a shared library.
87e226ce 2559 . addis %r12,%r2,xxx@toc@ha
721956f4 2560 . std %r2,40(%r1)
87e226ce
AM
2561 . ld %r11,xxx+0@toc@l(%r12)
2562 . ld %r2,xxx+8@toc@l(%r12)
721956f4 2563 . mtctr %r11
87e226ce 2564 . ld %r11,xxx+16@toc@l(%r12)
721956f4
AM
2565 . bctr
2566*/
2567
2568enum ppc_stub_type {
2569 ppc_stub_none,
2570 ppc_stub_long_branch,
2571 ppc_stub_plt_branch,
2572 ppc_stub_plt_call
2573};
2574
2575struct ppc_stub_hash_entry {
2576
2577 /* Base hash table entry structure. */
2578 struct bfd_hash_entry root;
2579
2580 /* The stub section. */
2581 asection *stub_sec;
2582
2583 /* Offset within stub_sec of the beginning of this stub. */
2584 bfd_vma stub_offset;
2585
2586 /* Given the symbol's value and its section we can determine its final
2587 value when building the stubs (so the stub knows where to jump. */
2588 bfd_vma target_value;
2589 asection *target_section;
2590
2591 enum ppc_stub_type stub_type;
2592
2593 /* The symbol table entry, if any, that this was derived from. */
2594 struct ppc_link_hash_entry *h;
2595
411e1bfb
AM
2596 /* And the reloc addend that this was derived from. */
2597 bfd_vma addend;
2598
721956f4
AM
2599 /* Where this stub is being called from, or, in the case of combined
2600 stub sections, the first input section in the group. */
2601 asection *id_sec;
2602};
2603
2604struct ppc_branch_hash_entry {
2605
2606 /* Base hash table entry structure. */
2607 struct bfd_hash_entry root;
2608
2609 /* Offset within .branch_lt. */
2610 unsigned int offset;
2611
2612 /* Generation marker. */
2613 unsigned int iter;
2614};
65f38f15
AM
2615
2616struct ppc_link_hash_entry
2617{
2618 struct elf_link_hash_entry elf;
2619
721956f4
AM
2620 /* A pointer to the most recently used stub hash entry against this
2621 symbol. */
2622 struct ppc_stub_hash_entry *stub_cache;
2623
65f38f15
AM
2624 /* Track dynamic relocs copied for this symbol. */
2625 struct ppc_dyn_relocs *dyn_relocs;
e86ce104 2626
721956f4
AM
2627 /* Link between function code and descriptor symbols. */
2628 struct elf_link_hash_entry *oh;
2629
e86ce104
AM
2630 /* Flag function code and descriptor symbols. */
2631 unsigned int is_func:1;
2632 unsigned int is_func_descriptor:1;
1e2f5b6e 2633 unsigned int is_entry:1;
411e1bfb
AM
2634
2635 /* Contexts in which symbol is used in the GOT (or TOC).
2636 Linker optimization will result in various transformations. */
2637#define TLS_TLS 1 /* Any TLS reloc. */
2638#define TLS_GD_LD 2 /* GD or LD reloc requiring 2 got slots. */
2639#define TLS_LD 4 /* LD reloc. */
2640#define TLS_TPREL 8 /* TPREL reloc, => IE. */
2641#define TLS_DTPREL 16 /* DTPREL reloc, => LD. */
2642#define TLS_EXPLICIT 32 /* Marks TOC section relocs. */
2643 char tls_type;
65f38f15
AM
2644};
2645
2646/* ppc64 ELF linker hash table. */
2647
2648struct ppc_link_hash_table
2649{
2650 struct elf_link_hash_table elf;
2651
721956f4
AM
2652 /* The stub hash table. */
2653 struct bfd_hash_table stub_hash_table;
2654
2655 /* Another hash table for plt_branch stubs. */
2656 struct bfd_hash_table branch_hash_table;
2657
2658 /* Linker stub bfd. */
2659 bfd *stub_bfd;
2660
2661 /* Linker call-backs. */
2662 asection * (*add_stub_section) PARAMS ((const char *, asection *));
2663 void (*layout_sections_again) PARAMS ((void));
2664
2665 /* Array to keep track of which stub sections have been created, and
2666 information on stub grouping. */
2667 struct map_stub {
2668 /* This is the section to which stubs in the group will be attached. */
2669 asection *link_sec;
2670 /* The stub section. */
2671 asection *stub_sec;
2672 } *stub_group;
2673
2674 /* Assorted information used by ppc64_elf_size_stubs. */
721956f4
AM
2675 int top_index;
2676 asection **input_list;
721956f4 2677
65f38f15
AM
2678 /* Short-cuts to get to dynamic linker sections. */
2679 asection *sgot;
2680 asection *srelgot;
2681 asection *splt;
2682 asection *srelplt;
2683 asection *sdynbss;
2684 asection *srelbss;
2685 asection *sglink;
82bd7b59 2686 asection *sfpr;
721956f4
AM
2687 asection *sbrlt;
2688 asection *srelbrlt;
ec338859 2689
411e1bfb
AM
2690 /* Short-cut to first output tls section. */
2691 asection *tls_sec;
2692
2693 /* Shortcut to .__tls_get_addr. */
2694 struct elf_link_hash_entry *tls_get_addr;
2695
2696 /* TLS local dynamic got entry handling. */
2697 union {
2698 bfd_signed_vma refcount;
2699 bfd_vma offset;
2700 } tlsld_got;
2701
5d1634d7 2702 /* Set on error. */
721956f4
AM
2703 unsigned int stub_error;
2704
2705 /* Flag set when small branches are detected. Used to
2706 select suitable defaults for the stub group size. */
2707 unsigned int has_14bit_branch;
2708
805fc799
AM
2709 /* Set if we detect a reference undefined weak symbol. */
2710 unsigned int have_undefweak;
2711
721956f4
AM
2712 /* Incremented every time we size stubs. */
2713 unsigned int stub_iteration;
5d1634d7 2714
ec338859
AM
2715 /* Small local sym to section mapping cache. */
2716 struct sym_sec_cache sym_sec;
65f38f15
AM
2717};
2718
721956f4
AM
2719static struct bfd_hash_entry *stub_hash_newfunc
2720 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
2721static struct bfd_hash_entry *branch_hash_newfunc
2722 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
2723static struct bfd_hash_entry *link_hash_newfunc
2724 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
2725static struct bfd_link_hash_table *ppc64_elf_link_hash_table_create
2726 PARAMS ((bfd *));
2727static void ppc64_elf_link_hash_table_free
2728 PARAMS ((struct bfd_link_hash_table *));
2729static char *ppc_stub_name
2730 PARAMS ((const asection *, const asection *,
2731 const struct ppc_link_hash_entry *, const Elf_Internal_Rela *));
2732static struct ppc_stub_hash_entry *ppc_get_stub_entry
2733 PARAMS ((const asection *, const asection *, struct elf_link_hash_entry *,
2734 const Elf_Internal_Rela *, struct ppc_link_hash_table *));
2735static struct ppc_stub_hash_entry *ppc_add_stub
2736 PARAMS ((const char *, asection *, struct ppc_link_hash_table *));
b34976b6 2737static bfd_boolean create_linkage_sections
721956f4 2738 PARAMS ((bfd *, struct bfd_link_info *));
b34976b6 2739static bfd_boolean create_got_section
721956f4 2740 PARAMS ((bfd *, struct bfd_link_info *));
b34976b6 2741static bfd_boolean ppc64_elf_create_dynamic_sections
721956f4
AM
2742 PARAMS ((bfd *, struct bfd_link_info *));
2743static void ppc64_elf_copy_indirect_symbol
b48fa14c
AM
2744 PARAMS ((struct elf_backend_data *, struct elf_link_hash_entry *,
2745 struct elf_link_hash_entry *));
411e1bfb
AM
2746static bfd_boolean update_local_sym_info
2747 PARAMS ((bfd *, Elf_Internal_Shdr *, unsigned long, bfd_vma, int));
2748static bfd_boolean update_plt_info
2749 PARAMS ((bfd *, struct ppc_link_hash_entry *, bfd_vma));
b34976b6 2750static bfd_boolean ppc64_elf_check_relocs
721956f4
AM
2751 PARAMS ((bfd *, struct bfd_link_info *, asection *,
2752 const Elf_Internal_Rela *));
2753static asection * ppc64_elf_gc_mark_hook
1e2f5b6e
AM
2754 PARAMS ((asection *, struct bfd_link_info *, Elf_Internal_Rela *,
2755 struct elf_link_hash_entry *, Elf_Internal_Sym *));
b34976b6 2756static bfd_boolean ppc64_elf_gc_sweep_hook
1e2f5b6e
AM
2757 PARAMS ((bfd *, struct bfd_link_info *, asection *,
2758 const Elf_Internal_Rela *));
b34976b6 2759static bfd_boolean func_desc_adjust
721956f4 2760 PARAMS ((struct elf_link_hash_entry *, PTR));
b34976b6 2761static bfd_boolean ppc64_elf_func_desc_adjust
721956f4 2762 PARAMS ((bfd *, struct bfd_link_info *));
b34976b6 2763static bfd_boolean ppc64_elf_adjust_dynamic_symbol
721956f4
AM
2764 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
2765static void ppc64_elf_hide_symbol
b34976b6 2766 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *, bfd_boolean));
411e1bfb
AM
2767static bfd_boolean get_sym_h
2768 PARAMS ((struct elf_link_hash_entry **, Elf_Internal_Sym **, asection **,
2769 char **, Elf_Internal_Sym **, unsigned long, bfd *));
2770static bfd_boolean get_tls_type
2771 PARAMS ((char **, Elf_Internal_Sym **, const Elf_Internal_Rela *, bfd *));
b34976b6 2772static bfd_boolean allocate_dynrelocs
721956f4 2773 PARAMS ((struct elf_link_hash_entry *, PTR));
b34976b6 2774static bfd_boolean readonly_dynrelocs
721956f4
AM
2775 PARAMS ((struct elf_link_hash_entry *, PTR));
2776static enum elf_reloc_type_class ppc64_elf_reloc_type_class
2777 PARAMS ((const Elf_Internal_Rela *));
b34976b6 2778static bfd_boolean ppc64_elf_size_dynamic_sections
721956f4 2779 PARAMS ((bfd *, struct bfd_link_info *));
411e1bfb 2780static enum ppc_stub_type ppc_type_of_stub
721956f4
AM
2781 PARAMS ((asection *, const Elf_Internal_Rela *,
2782 struct ppc_link_hash_entry **, bfd_vma));
2783static bfd_byte *build_plt_stub
2784 PARAMS ((bfd *, bfd_byte *, int, int));
b34976b6 2785static bfd_boolean ppc_build_one_stub
721956f4 2786 PARAMS ((struct bfd_hash_entry *, PTR));
b34976b6 2787static bfd_boolean ppc_size_one_stub
721956f4
AM
2788 PARAMS ((struct bfd_hash_entry *, PTR));
2789static void group_sections
b34976b6
AM
2790 PARAMS ((struct ppc_link_hash_table *, bfd_size_type, bfd_boolean));
2791static bfd_boolean ppc64_elf_relocate_section
721956f4
AM
2792 PARAMS ((bfd *, struct bfd_link_info *info, bfd *, asection *, bfd_byte *,
2793 Elf_Internal_Rela *relocs, Elf_Internal_Sym *local_syms,
2794 asection **));
b34976b6 2795static bfd_boolean ppc64_elf_finish_dynamic_symbol
721956f4
AM
2796 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
2797 Elf_Internal_Sym *));
b34976b6 2798static bfd_boolean ppc64_elf_finish_dynamic_sections
721956f4
AM
2799 PARAMS ((bfd *, struct bfd_link_info *));
2800
65f38f15
AM
2801/* Get the ppc64 ELF linker hash table from a link_info structure. */
2802
2803#define ppc_hash_table(p) \
2804 ((struct ppc_link_hash_table *) ((p)->hash))
2805
721956f4
AM
2806#define ppc_stub_hash_lookup(table, string, create, copy) \
2807 ((struct ppc_stub_hash_entry *) \
2808 bfd_hash_lookup ((table), (string), (create), (copy)))
2809
2810#define ppc_branch_hash_lookup(table, string, create, copy) \
2811 ((struct ppc_branch_hash_entry *) \
2812 bfd_hash_lookup ((table), (string), (create), (copy)))
2813
2814/* Create an entry in the stub hash table. */
2815
2816static struct bfd_hash_entry *
2817stub_hash_newfunc (entry, table, string)
2818 struct bfd_hash_entry *entry;
2819 struct bfd_hash_table *table;
2820 const char *string;
2821{
2822 /* Allocate the structure if it has not already been allocated by a
2823 subclass. */
2824 if (entry == NULL)
2825 {
2826 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
2827 if (entry == NULL)
2828 return entry;
2829 }
2830
2831 /* Call the allocation method of the superclass. */
2832 entry = bfd_hash_newfunc (entry, table, string);
2833 if (entry != NULL)
2834 {
2835 struct ppc_stub_hash_entry *eh;
2836
2837 /* Initialize the local fields. */
2838 eh = (struct ppc_stub_hash_entry *) entry;
2839 eh->stub_sec = NULL;
2840 eh->stub_offset = 0;
2841 eh->target_value = 0;
2842 eh->target_section = NULL;
2843 eh->stub_type = ppc_stub_none;
2844 eh->h = NULL;
2845 eh->id_sec = NULL;
2846 }
2847
2848 return entry;
2849}
2850
2851/* Create an entry in the branch hash table. */
2852
2853static struct bfd_hash_entry *
2854branch_hash_newfunc (entry, table, string)
2855 struct bfd_hash_entry *entry;
2856 struct bfd_hash_table *table;
2857 const char *string;
2858{
2859 /* Allocate the structure if it has not already been allocated by a
2860 subclass. */
2861 if (entry == NULL)
2862 {
2863 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
2864 if (entry == NULL)
2865 return entry;
2866 }
2867
2868 /* Call the allocation method of the superclass. */
2869 entry = bfd_hash_newfunc (entry, table, string);
2870 if (entry != NULL)
2871 {
2872 struct ppc_branch_hash_entry *eh;
2873
2874 /* Initialize the local fields. */
2875 eh = (struct ppc_branch_hash_entry *) entry;
2876 eh->offset = 0;
2877 eh->iter = 0;
2878 }
2879
2880 return entry;
2881}
2882
65f38f15
AM
2883/* Create an entry in a ppc64 ELF linker hash table. */
2884
2885static struct bfd_hash_entry *
2886link_hash_newfunc (entry, table, string)
2887 struct bfd_hash_entry *entry;
2888 struct bfd_hash_table *table;
2889 const char *string;
2890{
2891 /* Allocate the structure if it has not already been allocated by a
2892 subclass. */
2893 if (entry == NULL)
2894 {
2895 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
2896 if (entry == NULL)
2897 return entry;
2898 }
2899
2900 /* Call the allocation method of the superclass. */
2901 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
2902 if (entry != NULL)
2903 {
2904 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
2905
721956f4 2906 eh->stub_cache = NULL;
65f38f15 2907 eh->dyn_relocs = NULL;
721956f4 2908 eh->oh = NULL;
e86ce104
AM
2909 eh->is_func = 0;
2910 eh->is_func_descriptor = 0;
1e2f5b6e 2911 eh->is_entry = 0;
411e1bfb 2912 eh->tls_type = 0;
65f38f15
AM
2913 }
2914
2915 return entry;
2916}
2917
2918/* Create a ppc64 ELF linker hash table. */
2919
2920static struct bfd_link_hash_table *
2921ppc64_elf_link_hash_table_create (abfd)
2922 bfd *abfd;
2923{
2924 struct ppc_link_hash_table *htab;
2925 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
2926
e2d34d7d 2927 htab = (struct ppc_link_hash_table *) bfd_malloc (amt);
65f38f15
AM
2928 if (htab == NULL)
2929 return NULL;
2930
2931 if (! _bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc))
2932 {
e2d34d7d 2933 free (htab);
65f38f15
AM
2934 return NULL;
2935 }
2936
721956f4
AM
2937 /* Init the stub hash table too. */
2938 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc))
2939 return NULL;
2940
2941 /* And the branch hash table. */
2942 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc))
2943 return NULL;
2944
2945 htab->stub_bfd = NULL;
2946 htab->add_stub_section = NULL;
2947 htab->layout_sections_again = NULL;
2948 htab->stub_group = NULL;
65f38f15
AM
2949 htab->sgot = NULL;
2950 htab->srelgot = NULL;
2951 htab->splt = NULL;
2952 htab->srelplt = NULL;
2953 htab->sdynbss = NULL;
2954 htab->srelbss = NULL;
2955 htab->sglink = NULL;
82bd7b59 2956 htab->sfpr = NULL;
721956f4
AM
2957 htab->sbrlt = NULL;
2958 htab->srelbrlt = NULL;
411e1bfb
AM
2959 htab->tls_sec = NULL;
2960 htab->tlsld_got.refcount = 0;
721956f4
AM
2961 htab->stub_error = 0;
2962 htab->has_14bit_branch = 0;
805fc799 2963 htab->have_undefweak = 0;
721956f4 2964 htab->stub_iteration = 0;
ec338859 2965 htab->sym_sec.abfd = NULL;
411e1bfb
AM
2966 htab->elf.init_refcount.glist = NULL;
2967 htab->elf.init_offset.glist = NULL;
65f38f15
AM
2968
2969 return &htab->elf.root;
2970}
2971
721956f4
AM
2972/* Free the derived linker hash table. */
2973
2974static void
2975ppc64_elf_link_hash_table_free (hash)
2976 struct bfd_link_hash_table *hash;
2977{
2978 struct ppc_link_hash_table *ret = (struct ppc_link_hash_table *) hash;
2979
2980 bfd_hash_table_free (&ret->stub_hash_table);
2981 bfd_hash_table_free (&ret->branch_hash_table);
2982 _bfd_generic_link_hash_table_free (hash);
2983}
2984
2985/* Build a name for an entry in the stub hash table. */
2986
2987static char *
2988ppc_stub_name (input_section, sym_sec, h, rel)
2989 const asection *input_section;
2990 const asection *sym_sec;
2991 const struct ppc_link_hash_entry *h;
2992 const Elf_Internal_Rela *rel;
2993{
2994 char *stub_name;
2995 bfd_size_type len;
2996
2997 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
2998 offsets from a sym as a branch target? In fact, we could
2999 probably assume the addend is always zero. */
3000 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
3001
3002 if (h)
3003 {
3004 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
3005 stub_name = bfd_malloc (len);
3006 if (stub_name != NULL)
3007 {
3008 sprintf (stub_name, "%08x_%s+%x",
3009 input_section->id & 0xffffffff,
3010 h->elf.root.root.string,
3011 (int) rel->r_addend & 0xffffffff);
3012 }
3013 }
3014 else
3015 {
3016 len = 8 + 1 + 8 + 1 + 8 + 1 + 16 + 1;
3017 stub_name = bfd_malloc (len);
3018 if (stub_name != NULL)
3019 {
3020 sprintf (stub_name, "%08x_%x:%x+%x",
3021 input_section->id & 0xffffffff,
3022 sym_sec->id & 0xffffffff,
3023 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
3024 (int) rel->r_addend & 0xffffffff);
3025 }
3026 }
3027 return stub_name;
3028}
3029
3030/* Look up an entry in the stub hash. Stub entries are cached because
3031 creating the stub name takes a bit of time. */
3032
3033static struct ppc_stub_hash_entry *
3034ppc_get_stub_entry (input_section, sym_sec, hash, rel, htab)
3035 const asection *input_section;
3036 const asection *sym_sec;
3037 struct elf_link_hash_entry *hash;
3038 const Elf_Internal_Rela *rel;
3039 struct ppc_link_hash_table *htab;
3040{
3041 struct ppc_stub_hash_entry *stub_entry;
3042 struct ppc_link_hash_entry *h = (struct ppc_link_hash_entry *) hash;
3043 const asection *id_sec;
3044
3045 /* If this input section is part of a group of sections sharing one
3046 stub section, then use the id of the first section in the group.
3047 Stub names need to include a section id, as there may well be
3048 more than one stub used to reach say, printf, and we need to
3049 distinguish between them. */
3050 id_sec = htab->stub_group[input_section->id].link_sec;
3051
3052 if (h != NULL && h->stub_cache != NULL
3053 && h->stub_cache->h == h
3054 && h->stub_cache->id_sec == id_sec)
3055 {
3056 stub_entry = h->stub_cache;
3057 }
3058 else
3059 {
3060 char *stub_name;
3061
3062 stub_name = ppc_stub_name (id_sec, sym_sec, h, rel);
3063 if (stub_name == NULL)
3064 return NULL;
3065
3066 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 3067 stub_name, FALSE, FALSE);
721956f4
AM
3068 if (h != NULL)
3069 h->stub_cache = stub_entry;
3070
3071 free (stub_name);
3072 }
3073
3074 return stub_entry;
3075}
3076
3077/* Add a new stub entry to the stub hash. Not all fields of the new
3078 stub entry are initialised. */
3079
3080static struct ppc_stub_hash_entry *
3081ppc_add_stub (stub_name, section, htab)
3082 const char *stub_name;
3083 asection *section;
3084 struct ppc_link_hash_table *htab;
3085{
3086 asection *link_sec;
3087 asection *stub_sec;
3088 struct ppc_stub_hash_entry *stub_entry;
3089
3090 link_sec = htab->stub_group[section->id].link_sec;
3091 stub_sec = htab->stub_group[section->id].stub_sec;
3092 if (stub_sec == NULL)
3093 {
3094 stub_sec = htab->stub_group[link_sec->id].stub_sec;
3095 if (stub_sec == NULL)
3096 {
d4c88bbb 3097 size_t namelen;
721956f4
AM
3098 bfd_size_type len;
3099 char *s_name;
3100
d4c88bbb
AM
3101 namelen = strlen (link_sec->name);
3102 len = namelen + sizeof (STUB_SUFFIX);
721956f4
AM
3103 s_name = bfd_alloc (htab->stub_bfd, len);
3104 if (s_name == NULL)
3105 return NULL;
3106
d4c88bbb
AM
3107 memcpy (s_name, link_sec->name, namelen);
3108 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
721956f4
AM
3109 stub_sec = (*htab->add_stub_section) (s_name, link_sec);
3110 if (stub_sec == NULL)
3111 return NULL;
3112 htab->stub_group[link_sec->id].stub_sec = stub_sec;
3113 }
3114 htab->stub_group[section->id].stub_sec = stub_sec;
3115 }
3116
3117 /* Enter this entry into the linker stub hash table. */
3118 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
b34976b6 3119 TRUE, FALSE);
721956f4
AM
3120 if (stub_entry == NULL)
3121 {
3122 (*_bfd_error_handler) (_("%s: cannot create stub entry %s"),
3123 bfd_archive_filename (section->owner),
3124 stub_name);
3125 return NULL;
3126 }
3127
3128 stub_entry->stub_sec = stub_sec;
3129 stub_entry->stub_offset = 0;
3130 stub_entry->id_sec = link_sec;
3131 return stub_entry;
3132}
3133
82bd7b59
AM
3134/* Create sections for linker generated code. */
3135
b34976b6 3136static bfd_boolean
82bd7b59
AM
3137create_linkage_sections (dynobj, info)
3138 bfd *dynobj;
3139 struct bfd_link_info *info;
3140{
3141 struct ppc_link_hash_table *htab;
3142 flagword flags;
3143
3144 htab = ppc_hash_table (info);
3145
3146 /* Create .sfpr for code to save and restore fp regs. */
3147 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
3148 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
721956f4 3149 htab->sfpr = bfd_make_section_anyway (dynobj, ".sfpr");
82bd7b59
AM
3150 if (htab->sfpr == NULL
3151 || ! bfd_set_section_flags (dynobj, htab->sfpr, flags)
3152 || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
b34976b6 3153 return FALSE;
82bd7b59 3154
721956f4
AM
3155 /* Create .glink for lazy dynamic linking support. */
3156 htab->sglink = bfd_make_section_anyway (dynobj, ".glink");
82bd7b59
AM
3157 if (htab->sglink == NULL
3158 || ! bfd_set_section_flags (dynobj, htab->sglink, flags)
3159 || ! bfd_set_section_alignment (dynobj, htab->sglink, 2))
b34976b6 3160 return FALSE;
82bd7b59 3161
721956f4
AM
3162 /* Create .branch_lt for plt_branch stubs. */
3163 flags = (SEC_ALLOC | SEC_LOAD
3164 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3165 htab->sbrlt = bfd_make_section_anyway (dynobj, ".branch_lt");
3166 if (htab->sbrlt == NULL
3167 || ! bfd_set_section_flags (dynobj, htab->sbrlt, flags)
3168 || ! bfd_set_section_alignment (dynobj, htab->sbrlt, 3))
b34976b6 3169 return FALSE;
721956f4
AM
3170
3171 if (info->shared)
3172 {
3173 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3174 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
1e2f5b6e 3175 htab->srelbrlt = bfd_make_section_anyway (dynobj, ".rela.branch_lt");
721956f4
AM
3176 if (!htab->srelbrlt
3177 || ! bfd_set_section_flags (dynobj, htab->srelbrlt, flags)
3178 || ! bfd_set_section_alignment (dynobj, htab->srelbrlt, 3))
b34976b6 3179 return FALSE;
721956f4 3180 }
b34976b6 3181 return TRUE;
82bd7b59
AM
3182}
3183
65f38f15
AM
3184/* Create .got and .rela.got sections in DYNOBJ, and set up
3185 shortcuts to them in our hash table. */
3186
b34976b6 3187static bfd_boolean
65f38f15
AM
3188create_got_section (dynobj, info)
3189 bfd *dynobj;
3190 struct bfd_link_info *info;
3191{
3192 struct ppc_link_hash_table *htab;
3193
3194 if (! _bfd_elf_create_got_section (dynobj, info))
b34976b6 3195 return FALSE;
65f38f15
AM
3196
3197 htab = ppc_hash_table (info);
3198 htab->sgot = bfd_get_section_by_name (dynobj, ".got");
3199 if (!htab->sgot)
3200 abort ();
3201
3202 htab->srelgot = bfd_make_section (dynobj, ".rela.got");
3203 if (!htab->srelgot
3204 || ! bfd_set_section_flags (dynobj, htab->srelgot,
3205 (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
3206 | SEC_IN_MEMORY | SEC_LINKER_CREATED
3207 | SEC_READONLY))
3208 || ! bfd_set_section_alignment (dynobj, htab->srelgot, 3))
b34976b6
AM
3209 return FALSE;
3210 return TRUE;
65f38f15 3211}
5bd4f169 3212
82bd7b59 3213/* Create the dynamic sections, and set up shortcuts. */
5bd4f169 3214
b34976b6 3215static bfd_boolean
65f38f15
AM
3216ppc64_elf_create_dynamic_sections (dynobj, info)
3217 bfd *dynobj;
5bd4f169
AM
3218 struct bfd_link_info *info;
3219{
65f38f15 3220 struct ppc_link_hash_table *htab;
5bd4f169 3221
65f38f15
AM
3222 htab = ppc_hash_table (info);
3223 if (!htab->sgot && !create_got_section (dynobj, info))
b34976b6 3224 return FALSE;
5bd4f169 3225
65f38f15 3226 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
b34976b6 3227 return FALSE;
65f38f15
AM
3228
3229 htab->splt = bfd_get_section_by_name (dynobj, ".plt");
3230 htab->srelplt = bfd_get_section_by_name (dynobj, ".rela.plt");
3231 htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
3232 if (!info->shared)
3233 htab->srelbss = bfd_get_section_by_name (dynobj, ".rela.bss");
3234
3235 if (!htab->splt || !htab->srelplt || !htab->sdynbss
3236 || (!info->shared && !htab->srelbss))
3237 abort ();
3238
b34976b6 3239 return TRUE;
5bd4f169
AM
3240}
3241
65f38f15
AM
3242/* Copy the extra info we tack onto an elf_link_hash_entry. */
3243
3244static void
b48fa14c 3245ppc64_elf_copy_indirect_symbol (bed, dir, ind)
411e1bfb 3246 struct elf_backend_data *bed ATTRIBUTE_UNUSED;
65f38f15
AM
3247 struct elf_link_hash_entry *dir, *ind;
3248{
3249 struct ppc_link_hash_entry *edir, *eind;
3250
3251 edir = (struct ppc_link_hash_entry *) dir;
3252 eind = (struct ppc_link_hash_entry *) ind;
3253
411e1bfb 3254 /* Copy over any dynamic relocs we may have on the indirect sym. */
bbd7ec4a 3255 if (eind->dyn_relocs != NULL)
65f38f15 3256 {
bbd7ec4a
AM
3257 if (edir->dyn_relocs != NULL)
3258 {
3259 struct ppc_dyn_relocs **pp;
3260 struct ppc_dyn_relocs *p;
3261
411e1bfb 3262 if (eind->elf.root.type == bfd_link_hash_indirect)
bbd7ec4a
AM
3263 abort ();
3264
3265 /* Add reloc counts against the weak sym to the strong sym
3266 list. Merge any entries against the same section. */
3267 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
3268 {
3269 struct ppc_dyn_relocs *q;
3270
3271 for (q = edir->dyn_relocs; q != NULL; q = q->next)
3272 if (q->sec == p->sec)
3273 {
3274 q->pc_count += p->pc_count;
3275 q->count += p->count;
3276 *pp = p->next;
3277 break;
3278 }
3279 if (q == NULL)
3280 pp = &p->next;
3281 }
3282 *pp = edir->dyn_relocs;
3283 }
3284
65f38f15
AM
3285 edir->dyn_relocs = eind->dyn_relocs;
3286 eind->dyn_relocs = NULL;
3287 }
65f38f15 3288
6349e628
AM
3289 edir->is_func |= eind->is_func;
3290 edir->is_func_descriptor |= eind->is_func_descriptor;
3291 edir->is_entry |= eind->is_entry;
3292
3293 /* Copy down any references that we may have already seen to the
3294 symbol which just became indirect. */
3295 edir->elf.elf_link_hash_flags |=
3296 (eind->elf.elf_link_hash_flags
3297 & (ELF_LINK_HASH_REF_DYNAMIC
3298 | ELF_LINK_HASH_REF_REGULAR
3299 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
3300 | ELF_LINK_NON_GOT_REF));
3301
3302 /* If we were called to copy over info for a weak sym, that's all. */
3303 if (eind->elf.root.type != bfd_link_hash_indirect)
3304 return;
3305
3306 /* Copy over got entries. */
411e1bfb
AM
3307 if (eind->elf.got.glist != NULL)
3308 {
3309 if (edir->elf.got.glist != NULL)
3310 {
3311 struct got_entry **entp;
3312 struct got_entry *ent;
3313
3314 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
3315 {
3316 struct got_entry *dent;
3317
3318 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
3319 if (dent->addend == ent->addend
3320 && dent->tls_type == ent->tls_type)
3321 {
3322 dent->got.refcount += ent->got.refcount;
3323 *entp = ent->next;
3324 break;
3325 }
3326 if (dent == NULL)
3327 entp = &ent->next;
3328 }
3329 *entp = edir->elf.got.glist;
3330 }
3331
3332 edir->elf.got.glist = eind->elf.got.glist;
3333 eind->elf.got.glist = NULL;
3334 }
3335
3336 /* And plt entries. */
3337 if (eind->elf.plt.plist != NULL)
3338 {
3339 if (edir->elf.plt.plist != NULL)
3340 {
3341 struct plt_entry **entp;
3342 struct plt_entry *ent;
3343
3344 for (entp = &eind->elf.plt.plist; (ent = *entp) != NULL; )
3345 {
3346 struct plt_entry *dent;
3347
3348 for (dent = edir->elf.plt.plist; dent != NULL; dent = dent->next)
3349 if (dent->addend == ent->addend)
3350 {
3351 dent->plt.refcount += ent->plt.refcount;
3352 *entp = ent->next;
3353 break;
3354 }
3355 if (dent == NULL)
3356 entp = &ent->next;
3357 }
3358 *entp = edir->elf.plt.plist;
3359 }
3360
3361 edir->elf.plt.plist = eind->elf.plt.plist;
3362 eind->elf.plt.plist = NULL;
3363 }
3364
411e1bfb
AM
3365 if (edir->elf.dynindx == -1)
3366 {
3367 edir->elf.dynindx = eind->elf.dynindx;
3368 edir->elf.dynstr_index = eind->elf.dynstr_index;
3369 eind->elf.dynindx = -1;
3370 eind->elf.dynstr_index = 0;
3371 }
3372 else
3373 BFD_ASSERT (eind->elf.dynindx == -1);
3374}
3375
3376/* Set a flag, used by ppc64_elf_gc_mark_hook, on the entry symbol and
3377 symbols undefined on the command-line. */
3378
3379bfd_boolean
3380ppc64_elf_mark_entry_syms (info)
3381 struct bfd_link_info *info;
3382{
3383 struct ppc_link_hash_table *htab;
3384 struct bfd_sym_chain *sym;
3385
3386 htab = ppc_hash_table (info);
3387 for (sym = info->gc_sym_list; sym; sym = sym->next)
3388 {
3389 struct elf_link_hash_entry *h;
3390
3391 h = elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, FALSE);
3392 if (h != NULL)
3393 ((struct ppc_link_hash_entry *) h)->is_entry = 1;
3394 }
3395 return TRUE;
3396}
3397
3398static bfd_boolean
3399update_local_sym_info (abfd, symtab_hdr, r_symndx, r_addend, tls_type)
3400 bfd *abfd;
3401 Elf_Internal_Shdr *symtab_hdr;
3402 unsigned long r_symndx;
3403 bfd_vma r_addend;
3404 int tls_type;
3405{
3406 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
3407 char *local_got_tls_types;
3408
3409 if (local_got_ents == NULL)
3410 {
3411 bfd_size_type size = symtab_hdr->sh_info;
3412
3413 size *= sizeof (*local_got_ents) + sizeof (char);
3414 local_got_ents = (struct got_entry **) bfd_zalloc (abfd, size);
3415 if (local_got_ents == NULL)
3416 return FALSE;
3417 elf_local_got_ents (abfd) = local_got_ents;
3418 }
3419
3420 if ((tls_type & TLS_EXPLICIT) == 0)
3421 {
3422 struct got_entry *ent;
3423
3424 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
3425 if (ent->addend == r_addend && ent->tls_type == tls_type)
3426 break;
3427 if (ent == NULL)
3428 {
3429 bfd_size_type amt = sizeof (*ent);
3430 ent = (struct got_entry *) bfd_alloc (abfd, amt);
3431 if (ent == NULL)
3432 return FALSE;
3433 ent->next = local_got_ents[r_symndx];
3434 ent->addend = r_addend;
3435 ent->tls_type = tls_type;
3436 ent->got.refcount = 0;
3437 local_got_ents[r_symndx] = ent;
3438 }
3439 ent->got.refcount += 1;
3440 }
3441
3442 local_got_tls_types = (char *) (local_got_ents + symtab_hdr->sh_info);
3443 local_got_tls_types[r_symndx] |= tls_type;
3444 return TRUE;
65f38f15
AM
3445}
3446
411e1bfb
AM
3447static bfd_boolean
3448update_plt_info (abfd, eh, addend)
3449 bfd *abfd;
3450 struct ppc_link_hash_entry *eh;
3451 bfd_vma addend;
1e2f5b6e 3452{
411e1bfb 3453 struct plt_entry *ent;
1e2f5b6e 3454
411e1bfb
AM
3455 for (ent = eh->elf.plt.plist; ent != NULL; ent = ent->next)
3456 if (ent->addend == addend)
3457 break;
3458 if (ent == NULL)
1e2f5b6e 3459 {
411e1bfb
AM
3460 bfd_size_type amt = sizeof (*ent);
3461 ent = (struct plt_entry *) bfd_alloc (abfd, amt);
3462 if (ent == NULL)
3463 return FALSE;
3464 ent->next = eh->elf.plt.plist;
3465 ent->addend = addend;
3466 ent->plt.refcount = 0;
3467 eh->elf.plt.plist = ent;
1e2f5b6e 3468 }
411e1bfb
AM
3469 ent->plt.refcount += 1;
3470 eh->elf.elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
3471 eh->is_func = 1;
b34976b6 3472 return TRUE;
1e2f5b6e
AM
3473}
3474
5bd4f169 3475/* Look through the relocs for a section during the first phase, and
65f38f15 3476 calculate needed space in the global offset table, procedure
5d1634d7 3477 linkage table, and dynamic reloc sections. */
5bd4f169 3478
b34976b6 3479static bfd_boolean
5bd4f169
AM
3480ppc64_elf_check_relocs (abfd, info, sec, relocs)
3481 bfd *abfd;
3482 struct bfd_link_info *info;
3483 asection *sec;
3484 const Elf_Internal_Rela *relocs;
3485{
65f38f15 3486 struct ppc_link_hash_table *htab;
5bd4f169
AM
3487 Elf_Internal_Shdr *symtab_hdr;
3488 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
3489 const Elf_Internal_Rela *rel;
3490 const Elf_Internal_Rela *rel_end;
5bd4f169 3491 asection *sreloc;
1e2f5b6e 3492 asection **opd_sym_map;
5bd4f169
AM
3493
3494 if (info->relocateable)
b34976b6 3495 return TRUE;
5bd4f169 3496
65f38f15 3497 htab = ppc_hash_table (info);
5bd4f169 3498 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5bd4f169
AM
3499
3500 sym_hashes = elf_sym_hashes (abfd);
3501 sym_hashes_end = (sym_hashes
1e2f5b6e
AM
3502 + symtab_hdr->sh_size / sizeof (Elf64_External_Sym)
3503 - symtab_hdr->sh_info);
5bd4f169
AM
3504
3505 sreloc = NULL;
1e2f5b6e
AM
3506 opd_sym_map = NULL;
3507 if (strcmp (bfd_get_section_name (abfd, sec), ".opd") == 0)
3508 {
3509 /* Garbage collection needs some extra help with .opd sections.
3510 We don't want to necessarily keep everything referenced by
3511 relocs in .opd, as that would keep all functions. Instead,
3512 if we reference an .opd symbol (a function descriptor), we
3513 want to keep the function code symbol's section. This is
3514 easy for global symbols, but for local syms we need to keep
3515 information about the associated function section. Later, if
3516 edit_opd deletes entries, we'll use this array to adjust
3517 local syms in .opd. */
3518 union opd_info {
3519 asection *func_section;
3520 long entry_adjust;
3521 };
3522 bfd_size_type amt;
3523
3524 amt = sec->_raw_size * sizeof (union opd_info) / 24;
3525 opd_sym_map = (asection **) bfd_zalloc (abfd, amt);
3526 if (opd_sym_map == NULL)
b34976b6 3527 return FALSE;
f0abc2a1 3528 ppc64_elf_section_data (sec)->opd.func_sec = opd_sym_map;
1e2f5b6e 3529 }
5bd4f169 3530
82bd7b59
AM
3531 if (htab->elf.dynobj == NULL)
3532 htab->elf.dynobj = abfd;
3533 if (htab->sfpr == NULL
3534 && !create_linkage_sections (htab->elf.dynobj, info))
b34976b6 3535 return FALSE;
82bd7b59 3536
5bd4f169
AM
3537 rel_end = relocs + sec->reloc_count;
3538 for (rel = relocs; rel < rel_end; rel++)
3539 {
3540 unsigned long r_symndx;
3541 struct elf_link_hash_entry *h;
04c9666a 3542 enum elf_ppc64_reloc_type r_type;
411e1bfb 3543 int tls_type = 0;
5bd4f169
AM
3544
3545 r_symndx = ELF64_R_SYM (rel->r_info);
3546 if (r_symndx < symtab_hdr->sh_info)
3547 h = NULL;
3548 else
3549 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3550
04c9666a 3551 r_type = (enum elf_ppc64_reloc_type) ELF64_R_TYPE (rel->r_info);
a33d1f77 3552 switch (r_type)
5bd4f169 3553 {
411e1bfb
AM
3554 case R_PPC64_GOT_TLSLD16:
3555 case R_PPC64_GOT_TLSLD16_LO:
3556 case R_PPC64_GOT_TLSLD16_HI:
3557 case R_PPC64_GOT_TLSLD16_HA:
3558 htab->tlsld_got.refcount += 1;
3559 tls_type = TLS_TLS | TLS_GD_LD | TLS_LD;
3560 goto dogottls;
3561
3562 case R_PPC64_GOT_TLSGD16:
3563 case R_PPC64_GOT_TLSGD16_LO:
3564 case R_PPC64_GOT_TLSGD16_HI:
3565 case R_PPC64_GOT_TLSGD16_HA:
3566 tls_type = TLS_TLS | TLS_GD_LD;
3567 goto dogottls;
3568
3569 case R_PPC64_GOT_TPREL16_DS:
3570 case R_PPC64_GOT_TPREL16_LO_DS:
3571 case R_PPC64_GOT_TPREL16_HI:
3572 case R_PPC64_GOT_TPREL16_HA:
3573 if (info->shared)
3574 info->flags |= DF_STATIC_TLS;
3575 tls_type = TLS_TLS | TLS_TPREL;
3576 goto dogottls;
3577
3578 case R_PPC64_GOT_DTPREL16_DS:
3579 case R_PPC64_GOT_DTPREL16_LO_DS:
3580 case R_PPC64_GOT_DTPREL16_HI:
3581 case R_PPC64_GOT_DTPREL16_HA:
3582 tls_type = TLS_TLS | TLS_DTPREL;
3583 dogottls:
3584 sec->has_tls_reloc = 1;
3585 /* Fall thru */
3586
5bd4f169 3587 case R_PPC64_GOT16:
5bd4f169 3588 case R_PPC64_GOT16_DS:
65f38f15
AM
3589 case R_PPC64_GOT16_HA:
3590 case R_PPC64_GOT16_HI:
3591 case R_PPC64_GOT16_LO:
5bd4f169 3592 case R_PPC64_GOT16_LO_DS:
65f38f15 3593 /* This symbol requires a global offset table entry. */
82bd7b59
AM
3594 if (htab->sgot == NULL
3595 && !create_got_section (htab->elf.dynobj, info))
b34976b6 3596 return FALSE;
5bd4f169
AM
3597
3598 if (h != NULL)
3599 {
411e1bfb
AM
3600 struct ppc_link_hash_entry *eh;
3601 struct got_entry *ent;
65f38f15 3602
411e1bfb
AM
3603 eh = (struct ppc_link_hash_entry *) h;
3604 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
3605 if (ent->addend == rel->r_addend
3606 && ent->tls_type == tls_type)
3607 break;
3608 if (ent == NULL)
5bd4f169 3609 {
411e1bfb
AM
3610 bfd_size_type amt = sizeof (*ent);
3611 ent = (struct got_entry *) bfd_alloc (abfd, amt);
3612 if (ent == NULL)
b34976b6 3613 return FALSE;
411e1bfb
AM
3614 ent->next = eh->elf.got.glist;
3615 ent->addend = rel->r_addend;
3616 ent->tls_type = tls_type;
3617 ent->got.refcount = 0;
3618 eh->elf.got.glist = ent;
5bd4f169 3619 }
411e1bfb
AM
3620 ent->got.refcount += 1;
3621 eh->tls_type |= tls_type;
5bd4f169 3622 }
411e1bfb
AM
3623 else
3624 /* This is a global offset table entry for a local symbol. */
3625 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
3626 rel->r_addend, tls_type))
3627 return FALSE;
5bd4f169
AM
3628 break;
3629
5bd4f169 3630 case R_PPC64_PLT16_HA:
65f38f15
AM
3631 case R_PPC64_PLT16_HI:
3632 case R_PPC64_PLT16_LO:
3633 case R_PPC64_PLT32:
3634 case R_PPC64_PLT64:
5bd4f169 3635 /* This symbol requires a procedure linkage table entry. We
3fad3c7c
AM
3636 actually build the entry in adjust_dynamic_symbol,
3637 because this might be a case of linking PIC code without
3638 linking in any dynamic objects, in which case we don't
3639 need to generate a procedure linkage table after all. */
5bd4f169
AM
3640 if (h == NULL)
3641 {
3642 /* It does not make sense to have a procedure linkage
3fad3c7c 3643 table entry for a local symbol. */
5bd4f169 3644 bfd_set_error (bfd_error_bad_value);
b34976b6 3645 return FALSE;
5bd4f169 3646 }
411e1bfb
AM
3647 else
3648 if (!update_plt_info (abfd, (struct ppc_link_hash_entry *) h,
3649 rel->r_addend))
3650 return FALSE;
5bd4f169
AM
3651 break;
3652
3653 /* The following relocations don't need to propagate the
3654 relocation if linking a shared object since they are
3655 section relative. */
3656 case R_PPC64_SECTOFF:
3657 case R_PPC64_SECTOFF_LO:
3658 case R_PPC64_SECTOFF_HI:
3659 case R_PPC64_SECTOFF_HA:
3660 case R_PPC64_SECTOFF_DS:
3661 case R_PPC64_SECTOFF_LO_DS:
3662 case R_PPC64_TOC16:
3663 case R_PPC64_TOC16_LO:
3664 case R_PPC64_TOC16_HI:
3665 case R_PPC64_TOC16_HA:
3666 case R_PPC64_TOC16_DS:
3667 case R_PPC64_TOC16_LO_DS:
411e1bfb
AM
3668 case R_PPC64_DTPREL16:
3669 case R_PPC64_DTPREL16_LO:
3670 case R_PPC64_DTPREL16_HI:
3671 case R_PPC64_DTPREL16_HA:
3672 case R_PPC64_DTPREL16_DS:
3673 case R_PPC64_DTPREL16_LO_DS:
3674 case R_PPC64_DTPREL16_HIGHER:
3675 case R_PPC64_DTPREL16_HIGHERA:
3676 case R_PPC64_DTPREL16_HIGHEST:
3677 case R_PPC64_DTPREL16_HIGHESTA:
5bd4f169
AM
3678 break;
3679
3680 /* This relocation describes the C++ object vtable hierarchy.
3681 Reconstruct it for later use during GC. */
3682 case R_PPC64_GNU_VTINHERIT:
3683 if (!_bfd_elf64_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 3684 return FALSE;
5bd4f169
AM
3685 break;
3686
3687 /* This relocation describes which C++ vtable entries are actually
3688 used. Record for later use during GC. */
3689 case R_PPC64_GNU_VTENTRY:
3690 if (!_bfd_elf64_gc_record_vtentry (abfd, sec, h, rel->r_addend))
b34976b6 3691 return FALSE;
5bd4f169
AM
3692 break;
3693
721956f4
AM
3694 case R_PPC64_REL14:
3695 case R_PPC64_REL14_BRTAKEN:
3696 case R_PPC64_REL14_BRNTAKEN:
3697 htab->has_14bit_branch = 1;
3698 /* Fall through. */
3699
5d1634d7 3700 case R_PPC64_REL24:
e86ce104
AM
3701 if (h != NULL
3702 && h->root.root.string[0] == '.'
3703 && h->root.root.string[1] != 0)
5d1634d7
AM
3704 {
3705 /* We may need a .plt entry if the function this reloc
3706 refers to is in a shared lib. */
411e1bfb
AM
3707 if (!update_plt_info (abfd, (struct ppc_link_hash_entry *) h,
3708 rel->r_addend))
3709 return FALSE;
3710 if (h == htab->tls_get_addr)
3711 sec->has_tls_reloc = 1;
3712 else if (strcmp (h->root.root.string, ".__tls_get_addr") == 0)
3713 {
3714 htab->tls_get_addr = h;
3715 sec->has_tls_reloc = 1;
3716 }
3717 }
3718 break;
3719
3720 case R_PPC64_TPREL64:
3721 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
3722 if (info->shared)
3723 info->flags |= DF_STATIC_TLS;
3724 goto dotlstoc;
3725
3726 case R_PPC64_DTPMOD64:
3727 if (rel + 1 < rel_end
3728 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
3729 && rel[1].r_offset == rel->r_offset + 8)
3730 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD_LD;
3731 else
3732 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD_LD | TLS_LD;
3733 goto dotlstoc;
3734
3735 case R_PPC64_DTPREL64:
3736 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
3737 if (rel != relocs
3738 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
3739 && rel[-1].r_offset == rel->r_offset - 8)
3740 /* This is the second reloc of a dtpmod, dtprel pair.
3741 Don't mark with TLS_DTPREL. */
3742 goto dodyn;
3743
3744 dotlstoc:
3745 sec->has_tls_reloc = 1;
3746 if (h != NULL)
3747 {
3748 struct ppc_link_hash_entry *eh;
3749 eh = (struct ppc_link_hash_entry *) h;
3750 eh->tls_type |= tls_type;
3751 }
3752 else
3753 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
3754 rel->r_addend, tls_type))
3755 return FALSE;
3756
3757 if (ppc64_elf_section_data (sec)->t_symndx == NULL)
3758 {
3759 bfd_size_type amt = sec->_raw_size * sizeof (unsigned) / 8;
3760 ppc64_elf_section_data (sec)->t_symndx
3761 = (unsigned *) bfd_zalloc (abfd, amt);
3762 if (ppc64_elf_section_data (sec)->t_symndx == NULL)
3763 return FALSE;
3764 }
3765 BFD_ASSERT (rel->r_offset % 8 == 0);
3766 ppc64_elf_section_data (sec)->t_symndx[rel->r_offset / 8] = r_symndx;
3767 goto dodyn;
3768
3769 case R_PPC64_TPREL16:
3770 case R_PPC64_TPREL16_LO:
3771 case R_PPC64_TPREL16_HI:
3772 case R_PPC64_TPREL16_HA:
3773 case R_PPC64_TPREL16_DS:
3774 case R_PPC64_TPREL16_LO_DS:
3775 case R_PPC64_TPREL16_HIGHER:
3776 case R_PPC64_TPREL16_HIGHERA:
3777 case R_PPC64_TPREL16_HIGHEST:
3778 case R_PPC64_TPREL16_HIGHESTA:
3779 if (info->shared)
3780 {
3781 info->flags |= DF_STATIC_TLS;
3782 goto dodyn;
5d1634d7
AM
3783 }
3784 break;
3785
e86ce104 3786 case R_PPC64_ADDR64:
1e2f5b6e 3787 if (opd_sym_map != NULL
e86ce104
AM
3788 && h != NULL
3789 && h->root.root.string[0] == '.'
3790 && h->root.root.string[1] != 0)
3791 {
3792 struct elf_link_hash_entry *fdh;
3793
3794 fdh = elf_link_hash_lookup (&htab->elf, h->root.root.string + 1,
b34976b6 3795 FALSE, FALSE, FALSE);
e86ce104
AM
3796 if (fdh != NULL)
3797 {
e86ce104 3798 ((struct ppc_link_hash_entry *) fdh)->is_func_descriptor = 1;
721956f4 3799 ((struct ppc_link_hash_entry *) fdh)->oh = h;
e86ce104 3800 ((struct ppc_link_hash_entry *) h)->is_func = 1;
721956f4 3801 ((struct ppc_link_hash_entry *) h)->oh = fdh;
e86ce104
AM
3802 }
3803 }
1e2f5b6e
AM
3804 if (opd_sym_map != NULL
3805 && h == NULL
3806 && rel + 1 < rel_end
04c9666a 3807 && ((enum elf_ppc64_reloc_type) ELF64_R_TYPE ((rel + 1)->r_info)
1e2f5b6e
AM
3808 == R_PPC64_TOC))
3809 {
3810 asection *s;
3811
3812 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec, sec,
3813 r_symndx);
3814 if (s == NULL)
b34976b6 3815 return FALSE;
1e2f5b6e
AM
3816 else if (s != sec)
3817 opd_sym_map[rel->r_offset / 24] = s;
3818 }
e86ce104
AM
3819 /* Fall through. */
3820
04c9666a 3821 case R_PPC64_REL30:
5bd4f169 3822 case R_PPC64_REL32:
04c9666a 3823 case R_PPC64_REL64:
65f38f15
AM
3824 case R_PPC64_ADDR14:
3825 case R_PPC64_ADDR14_BRNTAKEN:
3826 case R_PPC64_ADDR14_BRTAKEN:
3827 case R_PPC64_ADDR16:
3828 case R_PPC64_ADDR16_DS:
3829 case R_PPC64_ADDR16_HA:
3830 case R_PPC64_ADDR16_HI:
3831 case R_PPC64_ADDR16_HIGHER:
3832 case R_PPC64_ADDR16_HIGHERA:
3833 case R_PPC64_ADDR16_HIGHEST:
3834 case R_PPC64_ADDR16_HIGHESTA:
3835 case R_PPC64_ADDR16_LO:
3836 case R_PPC64_ADDR16_LO_DS:
3837 case R_PPC64_ADDR24:
65f38f15 3838 case R_PPC64_ADDR32:
65f38f15
AM
3839 case R_PPC64_UADDR16:
3840 case R_PPC64_UADDR32:
3841 case R_PPC64_UADDR64:
5bd4f169 3842 case R_PPC64_TOC:
41bd81ab 3843 /* Don't propagate .opd relocs. */
1e2f5b6e 3844 if (NO_OPD_RELOCS && opd_sym_map != NULL)
e86ce104 3845 break;
e86ce104 3846
65f38f15
AM
3847 /* If we are creating a shared library, and this is a reloc
3848 against a global symbol, or a non PC relative reloc
3849 against a local symbol, then we need to copy the reloc
3850 into the shared library. However, if we are linking with
3851 -Bsymbolic, we do not need to copy a reloc against a
3852 global symbol which is defined in an object we are
3853 including in the link (i.e., DEF_REGULAR is set). At
3854 this point we have not seen all the input files, so it is
3855 possible that DEF_REGULAR is not set now but will be set
3856 later (it is never cleared). In case of a weak definition,
3857 DEF_REGULAR may be cleared later by a strong definition in
3858 a shared library. We account for that possibility below by
3859 storing information in the relocs_copied field of the hash
3860 table entry. A similar situation occurs when creating
3861 shared libraries and symbol visibility changes render the
3862 symbol local.
3863
3864 If on the other hand, we are creating an executable, we
3865 may need to keep relocations for symbols satisfied by a
3866 dynamic library if we manage to avoid copy relocs for the
3867 symbol. */
411e1bfb 3868 dodyn:
65f38f15
AM
3869 if ((info->shared
3870 && (sec->flags & SEC_ALLOC) != 0
411e1bfb 3871 && (MUST_BE_DYN_RELOC (r_type)
65f38f15
AM
3872 || (h != NULL
3873 && (! info->symbolic
3874 || h->root.type == bfd_link_hash_defweak
3875 || (h->elf_link_hash_flags
3876 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
3877 || (!info->shared
3878 && (sec->flags & SEC_ALLOC) != 0
3879 && h != NULL
3880 && (h->root.type == bfd_link_hash_defweak
3881 || (h->elf_link_hash_flags
3882 & ELF_LINK_HASH_DEF_REGULAR) == 0)))
5bd4f169 3883 {
ec338859
AM
3884 struct ppc_dyn_relocs *p;
3885 struct ppc_dyn_relocs **head;
3886
65f38f15
AM
3887 /* We must copy these reloc types into the output file.
3888 Create a reloc section in dynobj and make room for
3889 this reloc. */
5bd4f169
AM
3890 if (sreloc == NULL)
3891 {
3892 const char *name;
65f38f15 3893 bfd *dynobj;
5bd4f169
AM
3894
3895 name = (bfd_elf_string_from_elf_section
3896 (abfd,
3897 elf_elfheader (abfd)->e_shstrndx,
3898 elf_section_data (sec)->rel_hdr.sh_name));
3899 if (name == NULL)
b34976b6 3900 return FALSE;
5bd4f169 3901
65f38f15
AM
3902 if (strncmp (name, ".rela", 5) != 0
3903 || strcmp (bfd_get_section_name (abfd, sec),
3904 name + 5) != 0)
3905 {
3906 (*_bfd_error_handler)
3907 (_("%s: bad relocation section name `%s\'"),
3908 bfd_archive_filename (abfd), name);
5d1634d7 3909 bfd_set_error (bfd_error_bad_value);
65f38f15
AM
3910 }
3911
65f38f15 3912 dynobj = htab->elf.dynobj;
5bd4f169
AM
3913 sreloc = bfd_get_section_by_name (dynobj, name);
3914 if (sreloc == NULL)
3915 {
3916 flagword flags;
3917
3918 sreloc = bfd_make_section (dynobj, name);
3919 flags = (SEC_HAS_CONTENTS | SEC_READONLY
3920 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3921 if ((sec->flags & SEC_ALLOC) != 0)
3922 flags |= SEC_ALLOC | SEC_LOAD;
3923 if (sreloc == NULL
3924 || ! bfd_set_section_flags (dynobj, sreloc, flags)
65f38f15 3925 || ! bfd_set_section_alignment (dynobj, sreloc, 3))
b34976b6 3926 return FALSE;
5bd4f169 3927 }
65f38f15 3928 elf_section_data (sec)->sreloc = sreloc;
5bd4f169
AM
3929 }
3930
65f38f15
AM
3931 /* If this is a global symbol, we count the number of
3932 relocations we need for this symbol. */
3933 if (h != NULL)
3934 {
ec338859 3935 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
65f38f15
AM
3936 }
3937 else
3938 {
ec338859
AM
3939 /* Track dynamic relocs needed for local syms too.
3940 We really need local syms available to do this
3941 easily. Oh well. */
3942
3943 asection *s;
3944 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
3945 sec, r_symndx);
3946 if (s == NULL)
b34976b6 3947 return FALSE;
ec338859
AM
3948
3949 head = ((struct ppc_dyn_relocs **)
3950 &elf_section_data (s)->local_dynrel);
65f38f15 3951 }
ec338859
AM
3952
3953 p = *head;
3954 if (p == NULL || p->sec != sec)
3955 {
3956 p = ((struct ppc_dyn_relocs *)
3957 bfd_alloc (htab->elf.dynobj,
3958 (bfd_size_type) sizeof *p));
3959 if (p == NULL)
b34976b6 3960 return FALSE;
ec338859
AM
3961 p->next = *head;
3962 *head = p;
3963 p->sec = sec;
3964 p->count = 0;
3965 p->pc_count = 0;
3966 }
3967
3968 p->count += 1;
411e1bfb 3969 if (!MUST_BE_DYN_RELOC (r_type))
ec338859 3970 p->pc_count += 1;
65f38f15 3971 }
5bd4f169 3972 break;
65f38f15
AM
3973
3974 default:
96e0dda4 3975 break;
5bd4f169
AM
3976 }
3977 }
3978
b34976b6 3979 return TRUE;
5bd4f169
AM
3980}
3981
3982/* Return the section that should be marked against GC for a given
3983 relocation. */
3984
3985static asection *
1e2f5b6e
AM
3986ppc64_elf_gc_mark_hook (sec, info, rel, h, sym)
3987 asection *sec;
5bd4f169
AM
3988 struct bfd_link_info *info ATTRIBUTE_UNUSED;
3989 Elf_Internal_Rela *rel;
3990 struct elf_link_hash_entry *h;
3991 Elf_Internal_Sym *sym;
3992{
1e2f5b6e
AM
3993 asection *rsec = NULL;
3994
5bd4f169
AM
3995 if (h != NULL)
3996 {
04c9666a 3997 enum elf_ppc64_reloc_type r_type;
1e2f5b6e 3998 struct ppc_link_hash_entry *fdh;
a33d1f77 3999
04c9666a 4000 r_type = (enum elf_ppc64_reloc_type) ELF64_R_TYPE (rel->r_info);
a33d1f77 4001 switch (r_type)
5bd4f169
AM
4002 {
4003 case R_PPC64_GNU_VTINHERIT:
4004 case R_PPC64_GNU_VTENTRY:
4005 break;
4006
4007 default:
4008 switch (h->root.type)
4009 {
4010 case bfd_link_hash_defined:
4011 case bfd_link_hash_defweak:
1e2f5b6e
AM
4012 fdh = (struct ppc_link_hash_entry *) h;
4013
4014 /* Function descriptor syms cause the associated
4015 function code sym section to be marked. */
4016 if (fdh->is_func_descriptor)
4017 rsec = fdh->oh->root.u.def.section;
4018
4019 /* Function entry syms return NULL if they are in .opd
4020 and are not ._start (or others undefined on the ld
4021 command line). Thus we avoid marking all function
b34976b6 4022 sections, as all functions are referenced in .opd. */
1e2f5b6e
AM
4023 else if ((fdh->oh != NULL
4024 && ((struct ppc_link_hash_entry *) fdh->oh)->is_entry)
f0abc2a1 4025 || ppc64_elf_section_data (sec)->opd.func_sec == NULL)
1e2f5b6e
AM
4026 rsec = h->root.u.def.section;
4027 break;
5bd4f169
AM
4028
4029 case bfd_link_hash_common:
1e2f5b6e
AM
4030 rsec = h->root.u.c.p->section;
4031 break;
5bd4f169
AM
4032
4033 default:
4034 break;
4035 }
4036 }
4037 }
4038 else
4039 {
1e2f5b6e
AM
4040 asection **opd_sym_section;
4041
4042 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
f0abc2a1 4043 opd_sym_section = ppc64_elf_section_data (rsec)->opd.func_sec;
1e2f5b6e
AM
4044 if (opd_sym_section != NULL)
4045 rsec = opd_sym_section[sym->st_value / 24];
f0abc2a1 4046 else if (ppc64_elf_section_data (sec)->opd.func_sec != NULL)
1e2f5b6e 4047 rsec = NULL;
5bd4f169
AM
4048 }
4049
1e2f5b6e 4050 return rsec;
5bd4f169
AM
4051}
4052
65f38f15
AM
4053/* Update the .got, .plt. and dynamic reloc reference counts for the
4054 section being removed. */
5bd4f169 4055
b34976b6 4056static bfd_boolean
5bd4f169
AM
4057ppc64_elf_gc_sweep_hook (abfd, info, sec, relocs)
4058 bfd *abfd;
411e1bfb 4059 struct bfd_link_info *info;
5bd4f169
AM
4060 asection *sec;
4061 const Elf_Internal_Rela *relocs;
4062{
411e1bfb 4063 struct ppc_link_hash_table *htab;
5bd4f169
AM
4064 Elf_Internal_Shdr *symtab_hdr;
4065 struct elf_link_hash_entry **sym_hashes;
411e1bfb 4066 struct got_entry **local_got_ents;
5bd4f169 4067 const Elf_Internal_Rela *rel, *relend;
5bd4f169 4068
ec338859
AM
4069 elf_section_data (sec)->local_dynrel = NULL;
4070
411e1bfb 4071 htab = ppc_hash_table (info);
5bd4f169
AM
4072 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4073 sym_hashes = elf_sym_hashes (abfd);
411e1bfb 4074 local_got_ents = elf_local_got_ents (abfd);
5bd4f169
AM
4075
4076 relend = relocs + sec->reloc_count;
4077 for (rel = relocs; rel < relend; rel++)
a33d1f77
AM
4078 {
4079 unsigned long r_symndx;
04c9666a 4080 enum elf_ppc64_reloc_type r_type;
a33d1f77 4081 struct elf_link_hash_entry *h;
411e1bfb 4082 char tls_type = 0;
5bd4f169 4083
a33d1f77 4084 r_symndx = ELF64_R_SYM (rel->r_info);
04c9666a 4085 r_type = (enum elf_ppc64_reloc_type) ELF64_R_TYPE (rel->r_info);
a33d1f77
AM
4086 switch (r_type)
4087 {
411e1bfb
AM
4088 case R_PPC64_GOT_TLSLD16:
4089 case R_PPC64_GOT_TLSLD16_LO:
4090 case R_PPC64_GOT_TLSLD16_HI:
4091 case R_PPC64_GOT_TLSLD16_HA:
4092 htab->tlsld_got.refcount -= 1;
4093 tls_type = TLS_TLS | TLS_GD_LD | TLS_LD;
4094 goto dogot;
4095
4096 case R_PPC64_GOT_TLSGD16:
4097 case R_PPC64_GOT_TLSGD16_LO:
4098 case R_PPC64_GOT_TLSGD16_HI:
4099 case R_PPC64_GOT_TLSGD16_HA:
4100 tls_type = TLS_TLS | TLS_GD_LD;
4101 goto dogot;
4102
4103 case R_PPC64_GOT_TPREL16_DS:
4104 case R_PPC64_GOT_TPREL16_LO_DS:
4105 case R_PPC64_GOT_TPREL16_HI:
4106 case R_PPC64_GOT_TPREL16_HA:
4107 tls_type = TLS_TLS | TLS_TPREL;
4108 goto dogot;
4109
4110 case R_PPC64_GOT_DTPREL16_DS:
4111 case R_PPC64_GOT_DTPREL16_LO_DS:
4112 case R_PPC64_GOT_DTPREL16_HI:
4113 case R_PPC64_GOT_DTPREL16_HA:
4114 tls_type = TLS_TLS | TLS_DTPREL;
4115 goto dogot;
4116
a33d1f77
AM
4117 case R_PPC64_GOT16:
4118 case R_PPC64_GOT16_DS:
4119 case R_PPC64_GOT16_HA:
4120 case R_PPC64_GOT16_HI:
4121 case R_PPC64_GOT16_LO:
4122 case R_PPC64_GOT16_LO_DS:
411e1bfb
AM
4123 dogot:
4124 {
4125 struct got_entry *ent;
4126
4127 if (r_symndx >= symtab_hdr->sh_info)
4128 {
4129 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4130 ent = h->got.glist;
4131 }
4132 else
4133 ent = local_got_ents[r_symndx];
4134
4135 for (; ent != NULL; ent = ent->next)
4136 if (ent->addend == rel->r_addend
4137 && ent->tls_type == tls_type)
4138 break;
4139 if (ent == NULL)
4140 abort ();
4141 if (ent->got.refcount > 0)
4142 ent->got.refcount -= 1;
4143 }
a33d1f77 4144 break;
65f38f15 4145
a33d1f77
AM
4146 case R_PPC64_PLT16_HA:
4147 case R_PPC64_PLT16_HI:
4148 case R_PPC64_PLT16_LO:
4149 case R_PPC64_PLT32:
4150 case R_PPC64_PLT64:
721956f4
AM
4151 case R_PPC64_REL14:
4152 case R_PPC64_REL14_BRNTAKEN:
4153 case R_PPC64_REL14_BRTAKEN:
5d1634d7
AM
4154 case R_PPC64_REL24:
4155 if (r_symndx >= symtab_hdr->sh_info)
4156 {
411e1bfb
AM
4157 struct plt_entry *ent;
4158
5d1634d7 4159 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
411e1bfb
AM
4160 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
4161 if (ent->addend == rel->r_addend)
4162 break;
4163 if (ent == NULL)
4164 abort ();
4165 if (ent->plt.refcount > 0)
4166 ent->plt.refcount -= 1;
5d1634d7 4167 }
e86ce104 4168 break;
5d1634d7 4169
04c9666a 4170 case R_PPC64_REL30:
a33d1f77
AM
4171 case R_PPC64_REL32:
4172 case R_PPC64_REL64:
4173 if (r_symndx >= symtab_hdr->sh_info)
4174 {
4175 struct ppc_link_hash_entry *eh;
4176 struct ppc_dyn_relocs **pp;
4177 struct ppc_dyn_relocs *p;
65f38f15 4178
a33d1f77
AM
4179 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4180 eh = (struct ppc_link_hash_entry *) h;
65f38f15 4181
a33d1f77
AM
4182 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
4183 if (p->sec == sec)
4184 {
4185 p->pc_count -= 1;
4186 p->count -= 1;
4187 if (p->count == 0)
4188 *pp = p->next;
4189 break;
4190 }
4191 }
4192 break;
65f38f15 4193
411e1bfb
AM
4194 case R_PPC64_DTPMOD64:
4195 case R_PPC64_DTPREL64:
4196 case R_PPC64_TPREL64:
a33d1f77
AM
4197 case R_PPC64_ADDR14:
4198 case R_PPC64_ADDR14_BRNTAKEN:
4199 case R_PPC64_ADDR14_BRTAKEN:
4200 case R_PPC64_ADDR16:
4201 case R_PPC64_ADDR16_DS:
4202 case R_PPC64_ADDR16_HA:
4203 case R_PPC64_ADDR16_HI:
4204 case R_PPC64_ADDR16_HIGHER:
4205 case R_PPC64_ADDR16_HIGHERA:
4206 case R_PPC64_ADDR16_HIGHEST:
4207 case R_PPC64_ADDR16_HIGHESTA:
4208 case R_PPC64_ADDR16_LO:
4209 case R_PPC64_ADDR16_LO_DS:
4210 case R_PPC64_ADDR24:
a33d1f77
AM
4211 case R_PPC64_ADDR32:
4212 case R_PPC64_ADDR64:
4213 case R_PPC64_UADDR16:
4214 case R_PPC64_UADDR32:
4215 case R_PPC64_UADDR64:
4216 case R_PPC64_TOC:
4217 if (r_symndx >= symtab_hdr->sh_info)
4218 {
4219 struct ppc_link_hash_entry *eh;
4220 struct ppc_dyn_relocs **pp;
4221 struct ppc_dyn_relocs *p;
65f38f15 4222
a33d1f77
AM
4223 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4224 eh = (struct ppc_link_hash_entry *) h;
4225
4226 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
4227 if (p->sec == sec)
4228 {
4229 p->count -= 1;
4230 if (p->count == 0)
4231 *pp = p->next;
4232 break;
4233 }
4234 }
4235 break;
5bd4f169 4236
a33d1f77
AM
4237 default:
4238 break;
4239 }
4240 }
b34976b6 4241 return TRUE;
5bd4f169
AM
4242}
4243
e86ce104
AM
4244/* Called via elf_link_hash_traverse to transfer dynamic linking
4245 information on function code symbol entries to their corresponding
4246 function descriptor symbol entries. */
b34976b6 4247static bfd_boolean
e86ce104 4248func_desc_adjust (h, inf)
5bd4f169 4249 struct elf_link_hash_entry *h;
e86ce104 4250 PTR inf;
5bd4f169 4251{
e86ce104 4252 struct bfd_link_info *info;
65f38f15 4253 struct ppc_link_hash_table *htab;
411e1bfb 4254 struct plt_entry *ent;
5bd4f169 4255
e92d460e 4256 if (h->root.type == bfd_link_hash_indirect)
b34976b6 4257 return TRUE;
e86ce104 4258
e92d460e
AM
4259 if (h->root.type == bfd_link_hash_warning)
4260 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4261
e86ce104 4262 info = (struct bfd_link_info *) inf;
65f38f15 4263 htab = ppc_hash_table (info);
5bd4f169 4264
e86ce104
AM
4265 /* If this is a function code symbol, transfer dynamic linking
4266 information to the function descriptor symbol. */
4267 if (!((struct ppc_link_hash_entry *) h)->is_func)
b34976b6 4268 return TRUE;
e86ce104 4269
805fc799
AM
4270 if (h->root.type == bfd_link_hash_undefweak
4271 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR))
b34976b6 4272 htab->have_undefweak = TRUE;
805fc799 4273
411e1bfb
AM
4274 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
4275 if (ent->plt.refcount > 0)
4276 break;
4277 if (ent != NULL
e86ce104
AM
4278 && h->root.root.string[0] == '.'
4279 && h->root.root.string[1] != '\0')
5bd4f169 4280 {
721956f4 4281 struct elf_link_hash_entry *fdh = ((struct ppc_link_hash_entry *) h)->oh;
b34976b6 4282 bfd_boolean force_local;
5bd4f169 4283
648cca2c
AM
4284 /* Find the corresponding function descriptor symbol. Create it
4285 as undefined if necessary. */
5bd4f169 4286
721956f4
AM
4287 if (fdh == NULL)
4288 fdh = elf_link_hash_lookup (&htab->elf, h->root.root.string + 1,
b34976b6 4289 FALSE, FALSE, TRUE);
5bd4f169 4290
d926e03d
AM
4291 if (fdh == NULL
4292 && info->shared
4293 && (h->root.type == bfd_link_hash_undefined
4294 || h->root.type == bfd_link_hash_undefweak))
648cca2c
AM
4295 {
4296 bfd *abfd;
4297 asymbol *newsym;
14a793b2 4298 struct bfd_link_hash_entry *bh;
648cca2c 4299
d926e03d 4300 abfd = h->root.u.undef.abfd;
648cca2c
AM
4301 newsym = bfd_make_empty_symbol (abfd);
4302 newsym->name = h->root.root.string + 1;
4303 newsym->section = bfd_und_section_ptr;
4304 newsym->value = 0;
4305 newsym->flags = BSF_OBJECT;
4306 if (h->root.type == bfd_link_hash_undefweak)
4307 newsym->flags |= BSF_WEAK;
4308
14a793b2 4309 bh = &fdh->root;
648cca2c
AM
4310 if ( !(_bfd_generic_link_add_one_symbol
4311 (info, abfd, newsym->name, newsym->flags,
b34976b6 4312 newsym->section, newsym->value, NULL, FALSE, FALSE, &bh)))
648cca2c 4313 {
b34976b6 4314 return FALSE;
648cca2c 4315 }
14a793b2 4316 fdh = (struct elf_link_hash_entry *) bh;
d926e03d 4317 fdh->elf_link_hash_flags &= ~ELF_LINK_NON_ELF;
648cca2c
AM
4318 }
4319
e86ce104
AM
4320 if (fdh != NULL
4321 && (fdh->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0
4322 && (info->shared
4323 || (fdh->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
4324 || (fdh->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0))
4325 {
4326 if (fdh->dynindx == -1)
4327 if (! bfd_elf64_link_record_dynamic_symbol (info, fdh))
b34976b6 4328 return FALSE;
e86ce104
AM
4329 fdh->elf_link_hash_flags |= (h->elf_link_hash_flags
4330 & (ELF_LINK_HASH_REF_REGULAR
4331 | ELF_LINK_HASH_REF_DYNAMIC
4332 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
4333 | ELF_LINK_NON_GOT_REF));
e1fa50e7
AM
4334 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
4335 {
411e1bfb 4336 fdh->plt.plist = h->plt.plist;
e1fa50e7
AM
4337 fdh->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
4338 }
e86ce104 4339 ((struct ppc_link_hash_entry *) fdh)->is_func_descriptor = 1;
721956f4 4340 ((struct ppc_link_hash_entry *) fdh)->oh = h;
721956f4 4341 ((struct ppc_link_hash_entry *) h)->oh = fdh;
e86ce104
AM
4342 }
4343
4344 /* Now that the info is on the function descriptor, clear the
4345 function code sym info. Any function code syms for which we
4346 don't have a definition in a regular file, we force local.
4347 This prevents a shared library from exporting syms that have
4348 been imported from another library. Function code syms that
4349 are really in the library we must leave global to prevent the
e1fa50e7 4350 linker dragging in a definition from a static library. */
ea1e4b0d
AM
4351 force_local = (info->shared
4352 && ((h->elf_link_hash_flags
4353 & ELF_LINK_HASH_DEF_REGULAR) == 0
4354 || fdh == NULL
4355 || (fdh->elf_link_hash_flags
4356 & ELF_LINK_HASH_DEF_REGULAR) == 0
4357 || (fdh->elf_link_hash_flags
4358 & ELF_LINK_FORCED_LOCAL) != 0));
e86ce104
AM
4359 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
4360 }
4361
b34976b6 4362 return TRUE;
e86ce104 4363}
40b8271b 4364
82bd7b59
AM
4365#define MIN_SAVE_FPR 14
4366#define MAX_SAVE_FPR 31
4367
e86ce104 4368/* Called near the start of bfd_elf_size_dynamic_sections. We use
82bd7b59
AM
4369 this hook to a) provide some gcc support functions, and b) transfer
4370 dynamic linking information gathered so far on function code symbol
4371 entries, to their corresponding function descriptor symbol entries. */
b34976b6 4372static bfd_boolean
e86ce104
AM
4373ppc64_elf_func_desc_adjust (obfd, info)
4374 bfd *obfd ATTRIBUTE_UNUSED;
4375 struct bfd_link_info *info;
4376{
4377 struct ppc_link_hash_table *htab;
82bd7b59
AM
4378 unsigned int lowest_savef = MAX_SAVE_FPR + 2;
4379 unsigned int lowest_restf = MAX_SAVE_FPR + 2;
4380 unsigned int i;
4381 struct elf_link_hash_entry *h;
721956f4 4382 bfd_byte *p;
82bd7b59 4383 char sym[10];
e86ce104
AM
4384
4385 htab = ppc_hash_table (info);
82bd7b59
AM
4386
4387 if (htab->sfpr == NULL)
4388 /* We don't have any relocs. */
b34976b6 4389 return TRUE;
82bd7b59
AM
4390
4391 /* First provide any missing ._savef* and ._restf* functions. */
4392 memcpy (sym, "._savef14", 10);
4393 for (i = MIN_SAVE_FPR; i <= MAX_SAVE_FPR; i++)
4394 {
4395 sym[7] = i / 10 + '0';
4396 sym[8] = i % 10 + '0';
b34976b6 4397 h = elf_link_hash_lookup (&htab->elf, sym, FALSE, FALSE, TRUE);
82bd7b59
AM
4398 if (h != NULL
4399 && h->root.type == bfd_link_hash_undefined)
4400 {
4401 if (lowest_savef > i)
4402 lowest_savef = i;
4403 h->root.type = bfd_link_hash_defined;
4404 h->root.u.def.section = htab->sfpr;
4405 h->root.u.def.value = (i - lowest_savef) * 4;
4406 h->type = STT_FUNC;
4407 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
4408 _bfd_elf_link_hash_hide_symbol (info, h, info->shared);
4409 }
4410 }
4411
4412 memcpy (sym, "._restf14", 10);
4413 for (i = MIN_SAVE_FPR; i <= MAX_SAVE_FPR; i++)
4414 {
4415 sym[7] = i / 10 + '0';
4416 sym[8] = i % 10 + '0';
b34976b6 4417 h = elf_link_hash_lookup (&htab->elf, sym, FALSE, FALSE, TRUE);
82bd7b59
AM
4418 if (h != NULL
4419 && h->root.type == bfd_link_hash_undefined)
4420 {
4421 if (lowest_restf > i)
4422 lowest_restf = i;
4423 h->root.type = bfd_link_hash_defined;
4424 h->root.u.def.section = htab->sfpr;
4425 h->root.u.def.value = ((MAX_SAVE_FPR + 2 - lowest_savef) * 4
4426 + (i - lowest_restf) * 4);
4427 h->type = STT_FUNC;
4428 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
4429 _bfd_elf_link_hash_hide_symbol (info, h, info->shared);
4430 }
4431 }
4432
805fc799
AM
4433 elf_link_hash_traverse (&htab->elf, func_desc_adjust, (PTR) info);
4434
82bd7b59
AM
4435 htab->sfpr->_raw_size = ((MAX_SAVE_FPR + 2 - lowest_savef) * 4
4436 + (MAX_SAVE_FPR + 2 - lowest_restf) * 4);
4437
4438 if (htab->sfpr->_raw_size == 0)
805fc799
AM
4439 {
4440 if (!htab->have_undefweak)
4441 {
4442 _bfd_strip_section_from_output (info, htab->sfpr);
b34976b6 4443 return TRUE;
805fc799
AM
4444 }
4445
4446 htab->sfpr->_raw_size = 4;
4447 }
82bd7b59 4448
721956f4
AM
4449 p = (bfd_byte *) bfd_alloc (htab->elf.dynobj, htab->sfpr->_raw_size);
4450 if (p == NULL)
b34976b6 4451 return FALSE;
721956f4
AM
4452 htab->sfpr->contents = p;
4453
4454 for (i = lowest_savef; i <= MAX_SAVE_FPR; i++)
4455 {
4456 unsigned int fpr = i << 21;
4457 unsigned int stackoff = (1 << 16) - (MAX_SAVE_FPR + 1 - i) * 8;
4458 bfd_put_32 (htab->elf.dynobj, STFD_FR0_0R1 + fpr + stackoff, p);
4459 p += 4;
4460 }
4461 if (lowest_savef <= MAX_SAVE_FPR)
4462 {
82bd7b59
AM
4463 bfd_put_32 (htab->elf.dynobj, BLR, p);
4464 p += 4;
721956f4 4465 }
82bd7b59 4466
721956f4
AM
4467 for (i = lowest_restf; i <= MAX_SAVE_FPR; i++)
4468 {
4469 unsigned int fpr = i << 21;
4470 unsigned int stackoff = (1 << 16) - (MAX_SAVE_FPR + 1 - i) * 8;
4471 bfd_put_32 (htab->elf.dynobj, LFD_FR0_0R1 + fpr + stackoff, p);
82bd7b59
AM
4472 p += 4;
4473 }
721956f4
AM
4474 if (lowest_restf <= MAX_SAVE_FPR
4475 || htab->sfpr->_raw_size == 4)
4476 {
4477 bfd_put_32 (htab->elf.dynobj, BLR, p);
4478 }
82bd7b59 4479
b34976b6 4480 return TRUE;
e86ce104
AM
4481}
4482
4483/* Adjust a symbol defined by a dynamic object and referenced by a
4484 regular object. The current definition is in some section of the
4485 dynamic object, but we're not including those sections. We have to
4486 change the definition to something the rest of the link can
4487 understand. */
4488
b34976b6 4489static bfd_boolean
e86ce104
AM
4490ppc64_elf_adjust_dynamic_symbol (info, h)
4491 struct bfd_link_info *info;
4492 struct elf_link_hash_entry *h;
4493{
4494 struct ppc_link_hash_table *htab;
4495 struct ppc_link_hash_entry * eh;
4496 struct ppc_dyn_relocs *p;
4497 asection *s;
4498 unsigned int power_of_two;
4499
4500 htab = ppc_hash_table (info);
4501
4502 /* Deal with function syms. */
4503 if (h->type == STT_FUNC
4504 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
4505 {
4506 /* Clear procedure linkage table information for any symbol that
4507 won't need a .plt entry. */
411e1bfb
AM
4508 struct plt_entry *ent;
4509 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
4510 if (ent->plt.refcount > 0)
4511 break;
e86ce104 4512 if (!((struct ppc_link_hash_entry *) h)->is_func_descriptor
411e1bfb 4513 || ent == NULL
e86ce104
AM
4514 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0
4515 || (! info->shared
4516 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
4517 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0))
40b8271b 4518 {
411e1bfb 4519 h->plt.plist = NULL;
40b8271b 4520 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
40b8271b 4521 }
b34976b6 4522 return TRUE;
5bd4f169 4523 }
bbd7ec4a 4524 else
411e1bfb 4525 h->plt.plist = NULL;
5bd4f169
AM
4526
4527 /* If this is a weak symbol, and there is a real definition, the
4528 processor independent code will have arranged for us to see the
4529 real definition first, and we can just use the same value. */
4530 if (h->weakdef != NULL)
4531 {
4532 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
4533 || h->weakdef->root.type == bfd_link_hash_defweak);
4534 h->root.u.def.section = h->weakdef->root.u.def.section;
4535 h->root.u.def.value = h->weakdef->root.u.def.value;
b34976b6 4536 return TRUE;
5bd4f169
AM
4537 }
4538
4539 /* This is a reference to a symbol defined by a dynamic object which
4540 is not a function. */
4541
4542 /* If we are creating a shared library, we must presume that the
4543 only references to the symbol are via the global offset table.
4544 For such cases we need not do anything here; the relocations will
4545 be handled correctly by relocate_section. */
4546 if (info->shared)
b34976b6 4547 return TRUE;
5bd4f169 4548
65f38f15
AM
4549 /* If there are no references to this symbol that do not use the
4550 GOT, we don't need to generate a copy reloc. */
4551 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
b34976b6 4552 return TRUE;
65f38f15
AM
4553
4554 eh = (struct ppc_link_hash_entry *) h;
4555 for (p = eh->dyn_relocs; p != NULL; p = p->next)
4556 {
4557 s = p->sec->output_section;
4558 if (s != NULL && (s->flags & SEC_READONLY) != 0)
4559 break;
4560 }
4561
4562 /* If we didn't find any dynamic relocs in read-only sections, then
5d1634d7 4563 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
65f38f15
AM
4564 if (p == NULL)
4565 {
4566 h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
b34976b6 4567 return TRUE;
65f38f15
AM
4568 }
4569
5bd4f169
AM
4570 /* We must allocate the symbol in our .dynbss section, which will
4571 become part of the .bss section of the executable. There will be
4572 an entry for this symbol in the .dynsym section. The dynamic
4573 object will contain position independent code, so all references
4574 from the dynamic object to this symbol will go through the global
4575 offset table. The dynamic linker will use the .dynsym entry to
4576 determine the address it must put in the global offset table, so
4577 both the dynamic object and the regular object will refer to the
4578 same memory location for the variable. */
5bd4f169 4579
04c9666a
AM
4580 /* We must generate a R_PPC64_COPY reloc to tell the dynamic linker
4581 to copy the initial value out of the dynamic object and into the
5bd4f169
AM
4582 runtime process image. We need to remember the offset into the
4583 .rela.bss section we are going to use. */
4584 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
4585 {
65f38f15 4586 htab->srelbss->_raw_size += sizeof (Elf64_External_Rela);
5bd4f169
AM
4587 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
4588 }
4589
4590 /* We need to figure out the alignment required for this symbol. I
4591 have no idea how ELF linkers handle this. */
4592 power_of_two = bfd_log2 (h->size);
4593 if (power_of_two > 4)
4594 power_of_two = 4;
4595
4596 /* Apply the required alignment. */
65f38f15
AM
4597 s = htab->sdynbss;
4598 s->_raw_size = BFD_ALIGN (s->_raw_size, (bfd_size_type) (1 << power_of_two));
4599 if (power_of_two > bfd_get_section_alignment (htab->elf.dynobj, s))
5bd4f169 4600 {
65f38f15 4601 if (! bfd_set_section_alignment (htab->elf.dynobj, s, power_of_two))
b34976b6 4602 return FALSE;
5bd4f169
AM
4603 }
4604
4605 /* Define the symbol as being at this point in the section. */
4606 h->root.u.def.section = s;
4607 h->root.u.def.value = s->_raw_size;
4608
4609 /* Increment the section size to make room for the symbol. */
4610 s->_raw_size += h->size;
4611
b34976b6 4612 return TRUE;
5bd4f169
AM
4613}
4614
e86ce104
AM
4615/* If given a function descriptor symbol, hide both the function code
4616 sym and the descriptor. */
4617static void
4618ppc64_elf_hide_symbol (info, h, force_local)
4619 struct bfd_link_info *info;
4620 struct elf_link_hash_entry *h;
b34976b6 4621 bfd_boolean force_local;
e86ce104
AM
4622{
4623 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
4624
4625 if (((struct ppc_link_hash_entry *) h)->is_func_descriptor)
4626 {
721956f4 4627 struct elf_link_hash_entry *fh = ((struct ppc_link_hash_entry *) h)->oh;
e86ce104 4628
721956f4 4629 if (fh == NULL)
d1329ca3
AM
4630 {
4631 const char *p, *q;
4632 struct ppc_link_hash_table *htab;
4633 char save;
4634
4635 /* We aren't supposed to use alloca in BFD because on
4636 systems which do not have alloca the version in libiberty
4637 calls xmalloc, which might cause the program to crash
4638 when it runs out of memory. This function doesn't have a
4639 return status, so there's no way to gracefully return an
4640 error. So cheat. We know that string[-1] can be safely
4641 dereferenced; It's either a string in an ELF string
4642 table, or allocated in an objalloc structure. */
4643
4644 p = h->root.root.string - 1;
4645 save = *p;
4646 *(char *) p = '.';
4647 htab = ppc_hash_table (info);
b34976b6 4648 fh = elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
d1329ca3
AM
4649 *(char *) p = save;
4650
4651 /* Unfortunately, if it so happens that the string we were
4652 looking for was allocated immediately before this string,
4653 then we overwrote the string terminator. That's the only
4654 reason the lookup should fail. */
4655 if (fh == NULL)
4656 {
4657 q = h->root.root.string + strlen (h->root.root.string);
4658 while (q >= h->root.root.string && *q == *p)
4659 --q, --p;
4660 if (q < h->root.root.string && *p == '.')
b34976b6 4661 fh = elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
d1329ca3
AM
4662 }
4663 if (fh != NULL)
4664 {
4665 ((struct ppc_link_hash_entry *) h)->oh = fh;
4666 ((struct ppc_link_hash_entry *) fh)->oh = h;
4667 }
4668 }
e86ce104
AM
4669 if (fh != NULL)
4670 _bfd_elf_link_hash_hide_symbol (info, fh, force_local);
4671 }
4672}
4673
411e1bfb
AM
4674static bfd_boolean
4675get_sym_h (hp, symp, symsecp, tlstypep, locsymsp, r_symndx, ibfd)
4676 struct elf_link_hash_entry **hp;
4677 Elf_Internal_Sym **symp;
4678 asection **symsecp;
4679 char **tlstypep;
4680 Elf_Internal_Sym **locsymsp;
4681 unsigned long r_symndx;
4682 bfd *ibfd;
4683{
4684 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
4685
4686 if (r_symndx >= symtab_hdr->sh_info)
4687 {
4688 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
4689 struct elf_link_hash_entry *h;
4690
4691 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4692 while (h->root.type == bfd_link_hash_indirect
4693 || h->root.type == bfd_link_hash_warning)
4694 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4695
4696 if (hp != NULL)
4697 *hp = h;
4698
4699 if (symp != NULL)
4700 *symp = NULL;
4701
4702 if (symsecp != NULL)
4703 {
4704 asection *symsec = NULL;
4705 if (h->root.type == bfd_link_hash_defined
4706 || h->root.type == bfd_link_hash_defweak)
4707 symsec = h->root.u.def.section;
4708 *symsecp = symsec;
4709 }
4710
4711 if (tlstypep != NULL)
4712 {
4713 struct ppc_link_hash_entry *eh;
4714
4715 eh = (struct ppc_link_hash_entry *) h;
4716 *tlstypep = &eh->tls_type;
4717 }
4718 }
4719 else
4720 {
4721 Elf_Internal_Sym *sym;
4722 Elf_Internal_Sym *locsyms = *locsymsp;
4723
4724 if (locsyms == NULL)
4725 {
4726 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
4727 if (locsyms == NULL)
4728 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
4729 symtab_hdr->sh_info,
4730 0, NULL, NULL, NULL);
4731 if (locsyms == NULL)
4732 return FALSE;
4733 *locsymsp = locsyms;
4734 }
4735 sym = locsyms + r_symndx;
4736
4737 if (hp != NULL)
4738 *hp = NULL;
4739
4740 if (symp != NULL)
4741 *symp = sym;
4742
4743 if (symsecp != NULL)
4744 {
4745 asection *symsec = NULL;
4746 if ((sym->st_shndx != SHN_UNDEF
4747 && sym->st_shndx < SHN_LORESERVE)
4748 || sym->st_shndx > SHN_HIRESERVE)
4749 symsec = bfd_section_from_elf_index (ibfd, sym->st_shndx);
4750 *symsecp = symsec;
4751 }
4752
4753 if (tlstypep != NULL)
4754 {
4755 struct got_entry **lgot_ents;
4756 char *tlstype;
4757
4758 tlstype = NULL;
4759 lgot_ents = elf_local_got_ents (ibfd);
4760 if (lgot_ents != NULL)
4761 {
4762 char *lgot_types = (char *) (lgot_ents + symtab_hdr->sh_info);
4763 tlstype = &lgot_types[r_symndx];
4764 }
4765 *tlstypep = tlstype;
4766 }
4767 }
4768 return TRUE;
4769}
4770
4771static bfd_boolean
4772get_tls_type (tls_type, locsymsp, rel, ibfd)
4773 char **tls_type;
4774 Elf_Internal_Sym **locsymsp;
4775 const Elf_Internal_Rela *rel;
4776 bfd *ibfd;
4777{
4778 unsigned long r_symndx;
4779 struct elf_link_hash_entry *h;
4780 Elf_Internal_Sym *sym;
4781 asection *sec;
4782 bfd_vma off;
4783
4784 r_symndx = ELF64_R_SYM (rel->r_info);
4785 if (!get_sym_h (&h, &sym, &sec, tls_type, locsymsp, r_symndx, ibfd))
4786 return FALSE;
4787
4788 if ((*tls_type != NULL && **tls_type != 0)
4789 || sec == NULL
4790 || ppc64_elf_section_data (sec)->t_symndx == NULL)
4791 return TRUE;
4792
4793 /* Look inside a TOC section too. */
4794 if (h != NULL)
4795 {
4796 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
4797 off = h->root.u.def.value;
4798 }
4799 else
4800 off = sym->st_value;
4801 off += rel->r_addend;
4802 BFD_ASSERT (off % 8 == 0);
4803 r_symndx = ppc64_elf_section_data (sec)->t_symndx[off / 8];
4804 return get_sym_h (&h, &sym, NULL, tls_type, locsymsp, r_symndx, ibfd);
4805}
4806
b34976b6 4807bfd_boolean
d6fe2dc1 4808ppc64_elf_edit_opd (obfd, info)
1e2f5b6e
AM
4809 bfd *obfd;
4810 struct bfd_link_info *info;
4811{
4812 bfd *ibfd;
1e2f5b6e 4813
411e1bfb 4814 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
1e2f5b6e
AM
4815 {
4816 asection *sec;
4817 Elf_Internal_Rela *relstart, *rel, *relend;
4818 Elf_Internal_Shdr *symtab_hdr;
6cdc0ccc 4819 Elf_Internal_Sym *local_syms;
1e2f5b6e
AM
4820 struct elf_link_hash_entry **sym_hashes;
4821 bfd_vma offset;
d6fe2dc1 4822 bfd_size_type amt;
1e2f5b6e 4823 long *adjust;
b34976b6 4824 bfd_boolean need_edit;
1e2f5b6e
AM
4825
4826 sec = bfd_get_section_by_name (ibfd, ".opd");
4827 if (sec == NULL)
4828 continue;
4829
d6fe2dc1 4830 amt = sec->_raw_size * sizeof (long) / 24;
f0abc2a1 4831 adjust = ppc64_elf_section_data (sec)->opd.adjust;
d6fe2dc1
AM
4832 if (adjust == NULL)
4833 {
4834 /* Must be a ld -r link. ie. check_relocs hasn't been
4835 called. */
4836 adjust = (long *) bfd_zalloc (obfd, amt);
f0abc2a1 4837 ppc64_elf_section_data (sec)->opd.adjust = adjust;
d6fe2dc1
AM
4838 }
4839 memset (adjust, 0, (size_t) amt);
1e2f5b6e
AM
4840
4841 if (sec->output_section == bfd_abs_section_ptr)
4842 continue;
4843
4844 /* Look through the section relocs. */
4845 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
4846 continue;
4847
6cdc0ccc 4848 local_syms = NULL;
1e2f5b6e
AM
4849 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
4850 sym_hashes = elf_sym_hashes (ibfd);
4851
4852 /* Read the relocations. */
73374ef1 4853 relstart = _bfd_elf64_link_read_relocs (ibfd, sec, (PTR) NULL,
1e2f5b6e
AM
4854 (Elf_Internal_Rela *) NULL,
4855 info->keep_memory);
4856 if (relstart == NULL)
b34976b6 4857 return FALSE;
1e2f5b6e
AM
4858
4859 /* First run through the relocs to check they are sane, and to
4860 determine whether we need to edit this opd section. */
b34976b6 4861 need_edit = FALSE;
1e2f5b6e
AM
4862 offset = 0;
4863 relend = relstart + sec->reloc_count;
4864 for (rel = relstart; rel < relend; rel++)
4865 {
04c9666a 4866 enum elf_ppc64_reloc_type r_type;
1e2f5b6e
AM
4867 unsigned long r_symndx;
4868 asection *sym_sec;
4869 struct elf_link_hash_entry *h;
4870 Elf_Internal_Sym *sym;
4871
4872 /* .opd contains a regular array of 24 byte entries. We're
4873 only interested in the reloc pointing to a function entry
4874 point. */
04c9666a 4875 r_type = (enum elf_ppc64_reloc_type) ELF64_R_TYPE (rel->r_info);
1e2f5b6e
AM
4876 if (r_type == R_PPC64_TOC)
4877 continue;
4878
4879 if (r_type != R_PPC64_ADDR64)
4880 {
4881 (*_bfd_error_handler)
4882 (_("%s: unexpected reloc type %u in .opd section"),
4883 bfd_archive_filename (ibfd), r_type);
b34976b6 4884 need_edit = FALSE;
1e2f5b6e
AM
4885 break;
4886 }
4887
4888 if (rel + 1 >= relend)
4889 continue;
04c9666a 4890 r_type = (enum elf_ppc64_reloc_type) ELF64_R_TYPE ((rel + 1)->r_info);
1e2f5b6e
AM
4891 if (r_type != R_PPC64_TOC)
4892 continue;
4893
4894 if (rel->r_offset != offset)
4895 {
4896 /* If someone messes with .opd alignment then after a
4897 "ld -r" we might have padding in the middle of .opd.
4898 Also, there's nothing to prevent someone putting
4899 something silly in .opd with the assembler. No .opd
b34976b6 4900 optimization for them! */
1e2f5b6e
AM
4901 (*_bfd_error_handler)
4902 (_("%s: .opd is not a regular array of opd entries"),
4903 bfd_archive_filename (ibfd));
b34976b6 4904 need_edit = FALSE;
1e2f5b6e
AM
4905 break;
4906 }
4907
4908 r_symndx = ELF64_R_SYM (rel->r_info);
411e1bfb
AM
4909 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
4910 r_symndx, ibfd))
4911 goto error_free_rel;
1e2f5b6e
AM
4912
4913 if (sym_sec == NULL || sym_sec->owner == NULL)
4914 {
411e1bfb
AM
4915 const char *sym_name;
4916 if (h != NULL)
4917 sym_name = h->root.root.string;
4918 else
4919 sym_name = bfd_elf_local_sym_name (ibfd, sym);
4920
1e2f5b6e
AM
4921 (*_bfd_error_handler)
4922 (_("%s: undefined sym `%s' in .opd section"),
4923 bfd_archive_filename (ibfd),
411e1bfb 4924 sym_name);
b34976b6 4925 need_edit = FALSE;
1e2f5b6e
AM
4926 break;
4927 }
4928
51020317
AM
4929 /* opd entries are always for functions defined in the
4930 current input bfd. If the symbol isn't defined in the
4931 input bfd, then we won't be using the function in this
4932 bfd; It must be defined in a linkonce section in another
4933 bfd, or is weak. It's also possible that we are
4934 discarding the function due to a linker script /DISCARD/,
4935 which we test for via the output_section. */
4936 if (sym_sec->owner != ibfd
4937 || sym_sec->output_section == bfd_abs_section_ptr)
b34976b6 4938 need_edit = TRUE;
1e2f5b6e
AM
4939
4940 offset += 24;
4941 }
4942
4943 if (need_edit)
4944 {
4945 Elf_Internal_Rela *write_rel;
4946 bfd_byte *rptr, *wptr;
b34976b6 4947 bfd_boolean skip;
1e2f5b6e
AM
4948
4949 /* This seems a waste of time as input .opd sections are all
4950 zeros as generated by gcc, but I suppose there's no reason
4951 this will always be so. We might start putting something in
4952 the third word of .opd entries. */
4953 if ((sec->flags & SEC_IN_MEMORY) == 0)
4954 {
4955 bfd_byte *loc = bfd_alloc (ibfd, sec->_raw_size);
6cdc0ccc
AM
4956 if (loc == NULL
4957 || !bfd_get_section_contents (ibfd, sec, loc, (bfd_vma) 0,
4958 sec->_raw_size))
4959 {
4960 if (local_syms != NULL
4961 && symtab_hdr->contents != (unsigned char *) local_syms)
4962 free (local_syms);
4963 error_free_rel:
4964 if (elf_section_data (sec)->relocs != relstart)
4965 free (relstart);
b34976b6 4966 return FALSE;
6cdc0ccc 4967 }
1e2f5b6e
AM
4968 sec->contents = loc;
4969 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
4970 }
4971
4972 elf_section_data (sec)->relocs = relstart;
4973
4974 wptr = sec->contents;
4975 rptr = sec->contents;
4976 write_rel = relstart;
b34976b6 4977 skip = FALSE;
1e2f5b6e
AM
4978 offset = 0;
4979 for (rel = relstart; rel < relend; rel++)
4980 {
4981 if (rel->r_offset == offset)
4982 {
4983 unsigned long r_symndx;
4984 asection *sym_sec;
4985 struct elf_link_hash_entry *h;
4986 Elf_Internal_Sym *sym;
4987
4988 r_symndx = ELF64_R_SYM (rel->r_info);
411e1bfb
AM
4989 get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
4990 r_symndx, ibfd);
1e2f5b6e 4991
51020317
AM
4992 skip = (sym_sec->owner != ibfd
4993 || sym_sec->output_section == bfd_abs_section_ptr);
a4aa0fb7
AM
4994 if (skip)
4995 {
0be617ce 4996 if (h != NULL && sym_sec->owner == ibfd)
a4aa0fb7
AM
4997 {
4998 /* Arrange for the function descriptor sym
4999 to be dropped. */
d6fe2dc1 5000 struct ppc_link_hash_entry *fdh;
a4aa0fb7
AM
5001 struct ppc_link_hash_entry *fh;
5002
5003 fh = (struct ppc_link_hash_entry *) h;
d6fe2dc1
AM
5004 fdh = (struct ppc_link_hash_entry *) fh->oh;
5005 if (fdh == NULL)
5006 {
5007 const char *fd_name;
5008 struct ppc_link_hash_table *htab;
5009
5010 fd_name = h->root.root.string + 1;
5011 htab = ppc_hash_table (info);
5012 fdh = (struct ppc_link_hash_entry *)
5013 elf_link_hash_lookup (&htab->elf, fd_name,
b34976b6 5014 FALSE, FALSE, FALSE);
d6fe2dc1
AM
5015 fdh->is_func_descriptor = 1;
5016 fdh->oh = &fh->elf;
5017 fh->is_func = 1;
5018 fh->oh = &fdh->elf;
5019 }
5020
5021 fdh->elf.root.u.def.value = 0;
5022 fdh->elf.root.u.def.section = sym_sec;
a4aa0fb7
AM
5023 }
5024 }
5025 else
1e2f5b6e
AM
5026 {
5027 /* We'll be keeping this opd entry. */
5028
5029 if (h != NULL)
5030 {
5031 /* Redefine the function descriptor symbol
5032 to this location in the opd section.
5033 We've checked above that opd relocs are
5034 ordered. */
d6fe2dc1 5035 struct ppc_link_hash_entry *fdh;
1e2f5b6e
AM
5036 struct ppc_link_hash_entry *fh;
5037
5038 fh = (struct ppc_link_hash_entry *) h;
d6fe2dc1
AM
5039 fdh = (struct ppc_link_hash_entry *) fh->oh;
5040 if (fdh == NULL)
5041 {
5042 const char *fd_name;
5043 struct ppc_link_hash_table *htab;
5044
5045 fd_name = h->root.root.string + 1;
5046 htab = ppc_hash_table (info);
5047 fdh = (struct ppc_link_hash_entry *)
5048 elf_link_hash_lookup (&htab->elf, fd_name,
b34976b6 5049 FALSE, FALSE, FALSE);
d6fe2dc1
AM
5050 fdh->is_func_descriptor = 1;
5051 fdh->oh = &fh->elf;
5052 fh->is_func = 1;
5053 fh->oh = &fdh->elf;
5054 }
5055
5056 fdh->elf.root.u.def.value = wptr - sec->contents;
1e2f5b6e
AM
5057 }
5058 else
5059 {
6cdc0ccc
AM
5060 /* Local syms are a bit tricky. We could
5061 tweak them as they can be cached, but
5062 we'd need to look through the local syms
5063 for the function descriptor sym which we
5064 don't have at the moment. So keep an
b34976b6 5065 array of adjustments. */
7f6a7663 5066 adjust[rel->r_offset / 24] = wptr - rptr;
1e2f5b6e
AM
5067 }
5068
5069 if (wptr != rptr)
5070 memcpy (wptr, rptr, 24);
5071 wptr += 24;
5072 }
5073 rptr += 24;
5074 offset += 24;
5075 }
5076
5077 /* We need to adjust any reloc offsets to point to the
5078 new opd entries. While we're at it, we may as well
5079 remove redundant relocs. */
5080 if (!skip)
5081 {
5082 rel->r_offset += wptr - rptr;
5083 if (write_rel != rel)
5084 memcpy (write_rel, rel, sizeof (*rel));
5085 ++write_rel;
5086 }
5087 }
5088
5089 sec->_cooked_size = wptr - sec->contents;
5090 sec->reloc_count = write_rel - relstart;
cdcf6e38
AM
5091 /* Fudge the size too, as this is used later in
5092 elf_bfd_final_link if we are emitting relocs. */
5093 elf_section_data (sec)->rel_hdr.sh_size
5094 = sec->reloc_count * elf_section_data (sec)->rel_hdr.sh_entsize;
5095 BFD_ASSERT (elf_section_data (sec)->rel_hdr2 == NULL);
1e2f5b6e 5096 }
6cdc0ccc 5097 else if (elf_section_data (sec)->relocs != relstart)
1e2f5b6e 5098 free (relstart);
6cdc0ccc 5099
411e1bfb
AM
5100 if (local_syms != NULL
5101 && symtab_hdr->contents != (unsigned char *) local_syms)
5102 {
5103 if (!info->keep_memory)
5104 free (local_syms);
5105 else
5106 symtab_hdr->contents = (unsigned char *) local_syms;
5107 }
5108 }
5109
5110 return TRUE;
5111}
5112
5113/* Run through all the TLS relocs looking for optimization
5114 opportunities. The linker has been hacked (see ppc64elf.em) to do
5115 a preliminary section layout so that we know the TLS segment
5116 offsets. We can't optimize earlier because some optimizations need
5117 to know the tp offset, and we need to optimize before allocating
5118 dynamic relocations. */
5119
5120bfd_boolean
5121ppc64_elf_tls_optimize (obfd, info)
5122 bfd *obfd;
5123 struct bfd_link_info *info;
5124{
5125 asection *tls;
5126 bfd *ibfd;
5127 asection *sec;
5128 struct ppc_link_hash_table *htab;
5129
5130 if (info->relocateable)
5131 return TRUE;
5132
5133 for (tls = obfd->sections; tls != NULL; tls = tls->next)
5134 if ((tls->flags & (SEC_THREAD_LOCAL | SEC_LOAD))
5135 == (SEC_THREAD_LOCAL | SEC_LOAD))
5136 break;
5137 if (tls == NULL)
5138 return TRUE;
5139
5140 htab = ppc_hash_table (info);
5141 htab->tls_sec = tls;
5142
5143 if (info->shared)
5144 return TRUE;
5145
5146 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
5147 {
5148 Elf_Internal_Sym *locsyms = NULL;
5149
5150 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
5151 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
5152 {
5153 Elf_Internal_Rela *relstart, *rel, *relend;
5154 int expecting_tls_get_addr;
5155
5156 /* Read the relocations. */
5157 relstart = _bfd_elf64_link_read_relocs (ibfd, sec, (PTR) NULL,
5158 (Elf_Internal_Rela *) NULL,
5159 info->keep_memory);
5160 if (relstart == NULL)
5161 return FALSE;
5162
5163 expecting_tls_get_addr = 0;
5164 relend = relstart + sec->reloc_count;
5165 for (rel = relstart; rel < relend; rel++)
5166 {
5167 enum elf_ppc64_reloc_type r_type;
5168 unsigned long r_symndx;
5169 struct elf_link_hash_entry *h;
5170 Elf_Internal_Sym *sym;
5171 asection *sym_sec;
5172 char *tls_type;
5173 char tls_set, tls_clear, got_tls_type = 0;
5174 bfd_vma value;
5175 bfd_boolean ok_tprel;
5176
5177 r_symndx = ELF64_R_SYM (rel->r_info);
5178 if (!get_sym_h (&h, &sym, &sym_sec, &tls_type, &locsyms,
5179 r_symndx, ibfd))
5180 {
5181 err_free_rel:
5182 if (elf_section_data (sec)->relocs != relstart)
5183 free (relstart);
5184 if (locsyms != NULL
5185 && (elf_tdata (ibfd)->symtab_hdr.contents
5186 != (unsigned char *) locsyms))
5187 free (locsyms);
5188 return FALSE;
5189 }
5190
5191 if (h != NULL)
5192 {
5193 if (h->root.type != bfd_link_hash_defined
5194 && h->root.type != bfd_link_hash_defweak)
5195 continue;
5196 value = h->root.u.def.value;
5197 }
5198 else
5199 value = sym->st_value;
5200 ok_tprel = FALSE;
5201 if (sym_sec != NULL && sym_sec->output_section != NULL)
5202 {
5203 value += sym_sec->output_offset;
5204 value += sym_sec->output_section->vma;
5205 value -= htab->tls_sec->vma;
5206 ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
5207 < (bfd_vma) 1 << 32);
5208 }
5209
5210 r_type
5211 = (enum elf_ppc64_reloc_type) ELF64_R_TYPE (rel->r_info);
5212 switch (r_type)
5213 {
5214 case R_PPC64_GOT_TLSLD16:
5215 case R_PPC64_GOT_TLSLD16_LO:
5216 case R_PPC64_GOT_TLSLD16_HI:
5217 case R_PPC64_GOT_TLSLD16_HA:
5218 htab->tlsld_got.refcount -= 1;
5219 if (ok_tprel)
5220 /* LD -> LE */
5221 tls_set = 0;
5222 else
5223 /* We still need a GOT entry as the offset is
5224 too big. ie. LD -> IE. */
5225 tls_set = TLS_TLS | TLS_TPREL;
5226 tls_clear = TLS_GD_LD;
5227 got_tls_type = TLS_TLS | TLS_GD_LD | TLS_LD;
5228 expecting_tls_get_addr = 1;
5229 break;
5230
5231 case R_PPC64_GOT_TLSGD16:
5232 case R_PPC64_GOT_TLSGD16_LO:
5233 case R_PPC64_GOT_TLSGD16_HI:
5234 case R_PPC64_GOT_TLSGD16_HA:
5235 if (ok_tprel
5236 && (h == NULL
5237 || ((h->elf_link_hash_flags
5238 & ELF_LINK_HASH_DEF_REGULAR) != 0
5239 && ((h->elf_link_hash_flags
5240 & ELF_LINK_FORCED_LOCAL) != 0
5241 || !info->shared
5242 || info->symbolic))))
5243 /* GD -> LE */
5244 tls_set = 0;
5245 else
5246 /* GD -> IE */
5247 tls_set = TLS_TLS | TLS_TPREL;
5248 tls_clear = TLS_GD_LD;
5249 got_tls_type = TLS_TLS | TLS_GD_LD;
5250 expecting_tls_get_addr = 1;
5251 break;
5252
5253 case R_PPC64_GOT_TPREL16_DS:
5254 case R_PPC64_GOT_TPREL16_LO_DS:
5255 case R_PPC64_GOT_TPREL16_HI:
5256 case R_PPC64_GOT_TPREL16_HA:
5257 expecting_tls_get_addr = 0;
5258 if (ok_tprel)
5259 {
5260 /* IE -> LE */
5261 tls_set = 0;
5262 tls_clear = TLS_TPREL;
5263 got_tls_type = TLS_TLS | TLS_TPREL;
5264 break;
5265 }
5266 else
5267 continue;
5268
5269 case R_PPC64_REL14:
5270 case R_PPC64_REL14_BRTAKEN:
5271 case R_PPC64_REL14_BRNTAKEN:
5272 case R_PPC64_REL24:
5273 if (h != NULL
5274 && h == htab->tls_get_addr)
5275 {
5276 if (!expecting_tls_get_addr
5277 && rel != relstart
5278 && ((ELF64_R_TYPE (rel[-1].r_info)
5279 == R_PPC64_TOC16)
5280 || (ELF64_R_TYPE (rel[-1].r_info)
5281 == R_PPC64_TOC16_LO)))
5282 {
5283 /* Check for toc tls entries. */
5284 char *toc_tls;
5285
5286 if (!get_tls_type (&toc_tls, &locsyms,
5287 rel - 1, ibfd))
5288 goto err_free_rel;
5289 if (toc_tls != NULL)
5290 expecting_tls_get_addr = *toc_tls != 0;
5291 }
5292
5293 if (expecting_tls_get_addr)
5294 {
5295 struct plt_entry *ent;
5296 for (ent = h->plt.plist; ent; ent = ent->next)
5297 if (ent->addend == 0)
5298 {
5299 if (ent->plt.refcount > 0)
5300 ent->plt.refcount -= 1;
5301 break;
5302 }
5303 }
5304 }
5305 expecting_tls_get_addr = 0;
5306 continue;
5307
5308 case R_PPC64_TPREL64:
5309 expecting_tls_get_addr = 0;
5310 if (ok_tprel)
5311 {
5312 /* IE -> LE */
5313 tls_set = TLS_EXPLICIT;
5314 tls_clear = TLS_TPREL;
5315 break;
5316 }
5317 else
5318 continue;
5319
5320 case R_PPC64_DTPMOD64:
5321 expecting_tls_get_addr = 0;
5322 if ((*tls_type & TLS_LD) == 0)
5323 {
5324 if ((h == NULL
5325 || ((h->elf_link_hash_flags
5326 & ELF_LINK_HASH_DEF_REGULAR) != 0
5327 && ((h->elf_link_hash_flags
5328 & ELF_LINK_FORCED_LOCAL) != 0
5329 || !info->shared
5330 || info->symbolic)))
5331 && ok_tprel)
5332 /* GD -> LE */
5333 tls_set = TLS_EXPLICIT;
5334 else
5335 /* GD -> IE */
5336 tls_set = TLS_EXPLICIT | TLS_TPREL;
5337 tls_clear = TLS_GD_LD;
5338 }
5339 else
5340 {
5341 if (ok_tprel)
5342 /* LD -> LE */
5343 tls_set = TLS_EXPLICIT;
5344 else
5345 /* LD -> IE */
5346 tls_set = TLS_EXPLICIT | TLS_TPREL;
5347 tls_clear = TLS_GD_LD;
5348 }
5349 break;
5350
5351 default:
5352 expecting_tls_get_addr = 0;
5353 continue;
5354 }
5355
5356 if ((tls_set & TLS_EXPLICIT) == 0)
5357 {
5358 struct got_entry *ent;
5359
5360 /* Adjust got entry for this reloc. */
5361 if (h != NULL)
5362 ent = h->got.glist;
5363 else
5364 ent = elf_local_got_ents (ibfd)[r_symndx];
5365
5366 for (; ent != NULL; ent = ent->next)
5367 if (ent->addend == rel->r_addend
5368 && ent->tls_type == got_tls_type)
5369 break;
5370 if (ent == NULL)
5371 abort ();
5372
5373 if (tls_set == 0)
5374 {
5375 /* We managed to get rid of a got entry. */
5376 if (ent->got.refcount > 0)
5377 ent->got.refcount -= 1;
5378 }
5379 else
5380 ent->tls_type = tls_set;
5381 }
5382 else if (h != NULL)
5383 {
5384 struct ppc_link_hash_entry * eh;
5385 struct ppc_dyn_relocs **pp;
5386 struct ppc_dyn_relocs *p;
5387
5388 /* Adjust dynamic relocs. */
5389 eh = (struct ppc_link_hash_entry *) h;
5390 for (pp = &eh->dyn_relocs;
5391 (p = *pp) != NULL;
5392 pp = &p->next)
5393 if (p->sec == sec)
5394 {
5395 /* If we got rid of a DTPMOD/DTPREL reloc
5396 pair then we'll lose one or two dyn
5397 relocs. */
5398 if (tls_clear == TLS_GD_LD
5399 && rel + 1 < relend
5400 && (rel[1].r_info
5401 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
5402 && rel[1].r_offset == rel->r_offset + 8)
5403 p->count -= 1;
5404 if (tls_set == TLS_EXPLICIT)
5405 p->count -= 1;
5406 if (p->count == 0)
5407 *pp = p->next;
5408 break;
5409 }
5410 }
5411
5412 *tls_type |= tls_set;
5413 *tls_type &= ~tls_clear;
5414 }
5415
5416 if (elf_section_data (sec)->relocs != relstart)
5417 free (relstart);
5418 }
5419
5420 if (locsyms != NULL
5421 && (elf_tdata (ibfd)->symtab_hdr.contents
5422 != (unsigned char *) locsyms))
6cdc0ccc
AM
5423 {
5424 if (!info->keep_memory)
411e1bfb 5425 free (locsyms);
6cdc0ccc 5426 else
411e1bfb 5427 elf_tdata (ibfd)->symtab_hdr.contents = (unsigned char *) locsyms;
6cdc0ccc 5428 }
1e2f5b6e 5429 }
b34976b6 5430 return TRUE;
1e2f5b6e 5431}
b34976b6 5432
65f38f15
AM
5433/* This is the condition under which ppc64_elf_finish_dynamic_symbol
5434 will be called from elflink.h. If elflink.h doesn't call our
5435 finish_dynamic_symbol routine, we'll need to do something about
5436 initializing any .plt and .got entries in ppc64_elf_relocate_section. */
5437#define WILL_CALL_FINISH_DYNAMIC_SYMBOL(DYN, INFO, H) \
5438 ((DYN) \
5439 && ((INFO)->shared \
5440 || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0) \
5441 && ((H)->dynindx != -1 \
5442 || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0))
5bd4f169 5443
65f38f15
AM
5444/* Allocate space in .plt, .got and associated reloc sections for
5445 dynamic relocs. */
5bd4f169 5446
b34976b6 5447static bfd_boolean
65f38f15
AM
5448allocate_dynrelocs (h, inf)
5449 struct elf_link_hash_entry *h;
5450 PTR inf;
5bd4f169 5451{
65f38f15
AM
5452 struct bfd_link_info *info;
5453 struct ppc_link_hash_table *htab;
5bd4f169 5454 asection *s;
65f38f15
AM
5455 struct ppc_link_hash_entry *eh;
5456 struct ppc_dyn_relocs *p;
411e1bfb 5457 struct got_entry *gent;
5bd4f169 5458
e92d460e 5459 if (h->root.type == bfd_link_hash_indirect)
b34976b6 5460 return TRUE;
5bd4f169 5461
e92d460e
AM
5462 if (h->root.type == bfd_link_hash_warning)
5463 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5464
65f38f15
AM
5465 info = (struct bfd_link_info *) inf;
5466 htab = ppc_hash_table (info);
5bd4f169 5467
65f38f15 5468 if (htab->elf.dynamic_sections_created
411e1bfb
AM
5469 && h->dynindx != -1
5470 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info, h))
5bd4f169 5471 {
411e1bfb
AM
5472 struct plt_entry *pent;
5473 bfd_boolean doneone = FALSE;
5474 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
5475 if (pent->plt.refcount > 0)
5476 {
5477 BFD_ASSERT (((struct ppc_link_hash_entry *) h)->is_func_descriptor);
5478
5479 /* If this is the first .plt entry, make room for the special
5480 first entry. */
5481 s = htab->splt;
5482 if (s->_raw_size == 0)
5483 s->_raw_size += PLT_INITIAL_ENTRY_SIZE;
5484
5485 pent->plt.offset = s->_raw_size;
5486
5487 /* Make room for this entry. */
5488 s->_raw_size += PLT_ENTRY_SIZE;
5489
5490 /* Make room for the .glink code. */
5491 s = htab->sglink;
5492 if (s->_raw_size == 0)
5493 s->_raw_size += GLINK_CALL_STUB_SIZE;
5494 /* We need bigger stubs past index 32767. */
5495 if (s->_raw_size >= GLINK_CALL_STUB_SIZE + 32768*2*4)
5496 s->_raw_size += 4;
5497 s->_raw_size += 2*4;
5498
5499 /* We also need to make an entry in the .rela.plt section. */
5500 s = htab->srelplt;
5501 s->_raw_size += sizeof (Elf64_External_Rela);
5502 doneone = TRUE;
5503 }
5504 else
5505 pent->plt.offset = (bfd_vma) -1;
5506 if (!doneone)
65f38f15 5507 {
411e1bfb 5508 h->plt.plist = NULL;
65f38f15
AM
5509 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
5510 }
5511 }
5512 else
5513 {
411e1bfb 5514 h->plt.plist = NULL;
65f38f15
AM
5515 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
5516 }
5517
411e1bfb
AM
5518 for (gent = h->got.glist; gent != NULL; gent = gent->next)
5519 if (gent->got.refcount > 0)
5520 {
5521 /* Make sure this symbol is output as a dynamic symbol.
5522 Undefined weak syms won't yet be marked as dynamic. */
5523 if (h->dynindx == -1
5524 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
5525 {
5526 if (! bfd_elf64_link_record_dynamic_symbol (info, h))
5527 return FALSE;
5528 }
65f38f15 5529
411e1bfb
AM
5530 if ((gent->tls_type & TLS_LD) != 0)
5531 gent->got.offset = htab->tlsld_got.offset;
5532 else
5533 {
5534 bfd_boolean dyn;
5535
5536 s = htab->sgot;
5537 gent->got.offset = s->_raw_size;
5538 s->_raw_size += (gent->tls_type & TLS_GD_LD) != 0 ? 16 : 8;
5539 dyn = htab->elf.dynamic_sections_created;
5540 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info, h))
5541 htab->srelgot->_raw_size
5542 += ((gent->tls_type & TLS_GD_LD) != 0
5543 ? 2 * sizeof (Elf64_External_Rela)
5544 : sizeof (Elf64_External_Rela));
5545 }
5546 }
5547 else
5548 gent->got.offset = (bfd_vma) -1;
65f38f15
AM
5549
5550 eh = (struct ppc_link_hash_entry *) h;
5551 if (eh->dyn_relocs == NULL)
b34976b6 5552 return TRUE;
65f38f15
AM
5553
5554 /* In the shared -Bsymbolic case, discard space allocated for
5555 dynamic pc-relative relocs against symbols which turn out to be
5556 defined in regular objects. For the normal shared case, discard
5557 space for relocs that have become local due to symbol visibility
5558 changes. */
5559
5560 if (info->shared)
5561 {
5562 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
5563 && ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0
5564 || info->symbolic))
5565 {
5566 struct ppc_dyn_relocs **pp;
5567
5568 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
5bd4f169 5569 {
65f38f15
AM
5570 p->count -= p->pc_count;
5571 p->pc_count = 0;
5572 if (p->count == 0)
5573 *pp = p->next;
5574 else
5575 pp = &p->next;
5bd4f169 5576 }
65f38f15
AM
5577 }
5578 }
5579 else
5580 {
5581 /* For the non-shared case, discard space for relocs against
5582 symbols which turn out to need copy relocs or are not
5583 dynamic. */
5584
5585 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
5586 && (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
5587 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
5588 || (htab->elf.dynamic_sections_created
5589 && (h->root.type == bfd_link_hash_undefweak
5590 || h->root.type == bfd_link_hash_undefined))))
5591 {
5592 /* Make sure this symbol is output as a dynamic symbol.
5593 Undefined weak syms won't yet be marked as dynamic. */
5594 if (h->dynindx == -1
5595 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
5596 {
ec338859 5597 if (! bfd_elf64_link_record_dynamic_symbol (info, h))
b34976b6 5598 return FALSE;
65f38f15
AM
5599 }
5600
5601 /* If that succeeded, we know we'll be keeping all the
5602 relocs. */
5603 if (h->dynindx != -1)
5604 goto keep;
5605 }
5606
5607 eh->dyn_relocs = NULL;
5608
ec338859 5609 keep: ;
65f38f15
AM
5610 }
5611
5612 /* Finally, allocate space. */
5613 for (p = eh->dyn_relocs; p != NULL; p = p->next)
5614 {
5615 asection *sreloc = elf_section_data (p->sec)->sreloc;
5616 sreloc->_raw_size += p->count * sizeof (Elf64_External_Rela);
5617 }
5618
b34976b6 5619 return TRUE;
65f38f15
AM
5620}
5621
5622/* Find any dynamic relocs that apply to read-only sections. */
5623
b34976b6 5624static bfd_boolean
65f38f15
AM
5625readonly_dynrelocs (h, inf)
5626 struct elf_link_hash_entry *h;
5627 PTR inf;
5628{
5629 struct ppc_link_hash_entry *eh;
5630 struct ppc_dyn_relocs *p;
5631
e92d460e
AM
5632 if (h->root.type == bfd_link_hash_warning)
5633 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5634
65f38f15
AM
5635 eh = (struct ppc_link_hash_entry *) h;
5636 for (p = eh->dyn_relocs; p != NULL; p = p->next)
5637 {
5638 asection *s = p->sec->output_section;
5639
5640 if (s != NULL && (s->flags & SEC_READONLY) != 0)
5641 {
5642 struct bfd_link_info *info = (struct bfd_link_info *) inf;
5643
5644 info->flags |= DF_TEXTREL;
5645
5646 /* Not an error, just cut short the traversal. */
b34976b6 5647 return FALSE;
65f38f15
AM
5648 }
5649 }
b34976b6 5650 return TRUE;
65f38f15
AM
5651}
5652
5653/* Set the sizes of the dynamic sections. */
5654
b34976b6 5655static bfd_boolean
65f38f15
AM
5656ppc64_elf_size_dynamic_sections (output_bfd, info)
5657 bfd *output_bfd ATTRIBUTE_UNUSED;
5658 struct bfd_link_info *info;
5659{
5660 struct ppc_link_hash_table *htab;
5661 bfd *dynobj;
5662 asection *s;
b34976b6 5663 bfd_boolean relocs;
65f38f15
AM
5664 bfd *ibfd;
5665
5666 htab = ppc_hash_table (info);
5667 dynobj = htab->elf.dynobj;
5668 if (dynobj == NULL)
5669 abort ();
5670
5671 if (htab->elf.dynamic_sections_created)
5672 {
5673 /* Set the contents of the .interp section to the interpreter. */
5674 if (! info->shared)
5675 {
5676 s = bfd_get_section_by_name (dynobj, ".interp");
5677 if (s == NULL)
5678 abort ();
5679 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
5680 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
5681 }
5682 }
5683
411e1bfb
AM
5684 if (htab->tlsld_got.refcount > 0)
5685 {
5686 htab->tlsld_got.offset = htab->sgot->_raw_size;
5687 htab->sgot->_raw_size += 16;
5688 if (info->shared)
5689 htab->srelgot->_raw_size += sizeof (Elf64_External_Rela);
5690 }
5691 else
5692 htab->tlsld_got.offset = (bfd_vma) -1;
5693
65f38f15
AM
5694 /* Set up .got offsets for local syms, and space for local dynamic
5695 relocs. */
5696 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
5697 {
411e1bfb
AM
5698 struct got_entry **lgot_ents;
5699 struct got_entry **end_lgot_ents;
65f38f15
AM
5700 bfd_size_type locsymcount;
5701 Elf_Internal_Shdr *symtab_hdr;
5702 asection *srel;
5703
5704 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
5705 continue;
5706
5707 for (s = ibfd->sections; s != NULL; s = s->next)
5708 {
ec338859 5709 struct ppc_dyn_relocs *p;
65f38f15 5710
ec338859
AM
5711 for (p = *((struct ppc_dyn_relocs **)
5712 &elf_section_data (s)->local_dynrel);
5713 p != NULL;
5714 p = p->next)
65f38f15 5715 {
ec338859
AM
5716 if (!bfd_is_abs_section (p->sec)
5717 && bfd_is_abs_section (p->sec->output_section))
5718 {
5719 /* Input section has been discarded, either because
5720 it is a copy of a linkonce section or due to
5721 linker script /DISCARD/, so we'll be discarding
5722 the relocs too. */
5723 }
248866a8 5724 else if (p->count != 0)
ec338859
AM
5725 {
5726 srel = elf_section_data (p->sec)->sreloc;
5727 srel->_raw_size += p->count * sizeof (Elf64_External_Rela);
248866a8
AM
5728 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
5729 info->flags |= DF_TEXTREL;
ec338859 5730 }
65f38f15
AM
5731 }
5732 }
5733
411e1bfb
AM
5734 lgot_ents = elf_local_got_ents (ibfd);
5735 if (!lgot_ents)
65f38f15
AM
5736 continue;
5737
5738 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
5739 locsymcount = symtab_hdr->sh_info;
411e1bfb 5740 end_lgot_ents = lgot_ents + locsymcount;
65f38f15
AM
5741 s = htab->sgot;
5742 srel = htab->srelgot;
411e1bfb 5743 for (; lgot_ents < end_lgot_ents; ++lgot_ents)
65f38f15 5744 {
411e1bfb
AM
5745 struct got_entry *ent;
5746
5747 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
5748 if (ent->got.refcount > 0)
5749 {
5750 if ((ent->tls_type & TLS_LD) != 0)
5751 {
5752 if (htab->tlsld_got.offset == (bfd_vma) -1)
5753 {
5754 htab->tlsld_got.offset = s->_raw_size;
5755 s->_raw_size += 16;
5756 if (info->shared)
5757 srel->_raw_size += sizeof (Elf64_External_Rela);
5758 }
5759 ent->got.offset = htab->tlsld_got.offset;
5760 }
5761 else
5762 {
5763 ent->got.offset = s->_raw_size;
5764 if ((ent->tls_type & TLS_GD_LD) != 0)
5765 {
5766 s->_raw_size += 16;
5767 if (info->shared)
5768 srel->_raw_size += 2 * sizeof (Elf64_External_Rela);
5769 }
5770 else
5771 {
5772 s->_raw_size += 8;
5773 if (info->shared)
5774 srel->_raw_size += sizeof (Elf64_External_Rela);
5775 }
5776 }
5777 }
5778 else
5779 ent->got.offset = (bfd_vma) -1;
65f38f15
AM
5780 }
5781 }
5782
5783 /* Allocate global sym .plt and .got entries, and space for global
5784 sym dynamic relocs. */
5785 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, (PTR) info);
5786
5787 /* We now have determined the sizes of the various dynamic sections.
5788 Allocate memory for them. */
b34976b6 5789 relocs = FALSE;
65f38f15
AM
5790 for (s = dynobj->sections; s != NULL; s = s->next)
5791 {
5792 if ((s->flags & SEC_LINKER_CREATED) == 0)
5793 continue;
5794
411e1bfb
AM
5795 /* Reset _cooked_size since prelim layout will set it wrongly,
5796 and a non-zero _cooked_size sticks. */
5797 s->_cooked_size = 0;
5798
721956f4
AM
5799 if (s == htab->sbrlt || s == htab->srelbrlt)
5800 /* These haven't been allocated yet; don't strip. */
5801 continue;
5802 else if (s == htab->splt
5803 || s == htab->sgot
5804 || s == htab->sglink)
65f38f15
AM
5805 {
5806 /* Strip this section if we don't need it; see the
5807 comment below. */
5bd4f169 5808 }
65f38f15 5809 else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
5bd4f169
AM
5810 {
5811 if (s->_raw_size == 0)
5812 {
5813 /* If we don't need this section, strip it from the
5814 output file. This is mostly to handle .rela.bss and
5815 .rela.plt. We must create both sections in
5816 create_dynamic_sections, because they must be created
5817 before the linker maps input sections to output
5818 sections. The linker does that before
5819 adjust_dynamic_symbol is called, and it is that
5820 function which decides whether anything needs to go
5821 into these sections. */
5bd4f169
AM
5822 }
5823 else
5824 {
65f38f15 5825 if (s != htab->srelplt)
b34976b6 5826 relocs = TRUE;
5bd4f169
AM
5827
5828 /* We use the reloc_count field as a counter if we need
5829 to copy relocs into the output file. */
5830 s->reloc_count = 0;
5831 }
5832 }
65f38f15 5833 else
5bd4f169
AM
5834 {
5835 /* It's not one of our sections, so don't allocate space. */
5836 continue;
5837 }
5838
65f38f15 5839 if (s->_raw_size == 0)
5bd4f169
AM
5840 {
5841 _bfd_strip_section_from_output (info, s);
5842 continue;
5843 }
5844
5f333394
AM
5845 /* .plt is in the bss section. We don't initialise it. */
5846 if ((s->flags & SEC_LOAD) == 0)
5847 continue;
5848
65f38f15
AM
5849 /* Allocate memory for the section contents. We use bfd_zalloc
5850 here in case unused entries are not reclaimed before the
5851 section's contents are written out. This should not happen,
411e1bfb
AM
5852 but this way if it does we get a R_PPC64_NONE reloc in .rela
5853 sections instead of garbage.
5854 We also rely on the section contents being zero when writing
5855 the GOT. */
721956f4 5856 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
65f38f15 5857 if (s->contents == NULL)
b34976b6 5858 return FALSE;
5bd4f169
AM
5859 }
5860
e86ce104 5861 if (htab->elf.dynamic_sections_created)
5bd4f169
AM
5862 {
5863 /* Add some entries to the .dynamic section. We fill in the
5864 values later, in ppc64_elf_finish_dynamic_sections, but we
5865 must add the entries now so that we get the correct size for
5866 the .dynamic section. The DT_DEBUG entry is filled in by the
5867 dynamic linker and used by the debugger. */
dc810e39
AM
5868#define add_dynamic_entry(TAG, VAL) \
5869 bfd_elf64_add_dynamic_entry (info, (bfd_vma) (TAG), (bfd_vma) (VAL))
5870
5871 if (!info->shared)
5bd4f169 5872 {
dc810e39 5873 if (!add_dynamic_entry (DT_DEBUG, 0))
b34976b6 5874 return FALSE;
5bd4f169
AM
5875 }
5876
9c07fe7c 5877 if (htab->splt != NULL && htab->splt->_raw_size != 0)
5bd4f169 5878 {
dc810e39
AM
5879 if (!add_dynamic_entry (DT_PLTGOT, 0)
5880 || !add_dynamic_entry (DT_PLTRELSZ, 0)
5881 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
5d1634d7
AM
5882 || !add_dynamic_entry (DT_JMPREL, 0)
5883 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
b34976b6 5884 return FALSE;
5bd4f169
AM
5885 }
5886
19397422
AM
5887 if (NO_OPD_RELOCS)
5888 {
5889 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
5890 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
b34976b6 5891 return FALSE;
19397422
AM
5892 }
5893
5bd4f169
AM
5894 if (relocs)
5895 {
dc810e39
AM
5896 if (!add_dynamic_entry (DT_RELA, 0)
5897 || !add_dynamic_entry (DT_RELASZ, 0)
5898 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
b34976b6 5899 return FALSE;
5bd4f169 5900
65f38f15
AM
5901 /* If any dynamic relocs apply to a read-only section,
5902 then we need a DT_TEXTREL entry. */
248866a8
AM
5903 if ((info->flags & DF_TEXTREL) == 0)
5904 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs,
5905 (PTR) info);
5bd4f169 5906
65f38f15 5907 if ((info->flags & DF_TEXTREL) != 0)
5bd4f169 5908 {
65f38f15 5909 if (!add_dynamic_entry (DT_TEXTREL, 0))
b34976b6 5910 return FALSE;
5bd4f169 5911 }
5bd4f169 5912 }
5bd4f169 5913 }
65f38f15 5914#undef add_dynamic_entry
5bd4f169 5915
b34976b6 5916 return TRUE;
5bd4f169
AM
5917}
5918
721956f4 5919/* Determine the type of stub needed, if any, for a call. */
5bd4f169 5920
721956f4
AM
5921static INLINE enum ppc_stub_type
5922ppc_type_of_stub (input_sec, rel, hash, destination)
5923 asection *input_sec;
5924 const Elf_Internal_Rela *rel;
5925 struct ppc_link_hash_entry **hash;
5926 bfd_vma destination;
5bd4f169 5927{
721956f4
AM
5928 struct ppc_link_hash_entry *h = *hash;
5929 bfd_vma location;
5930 bfd_vma branch_offset;
5931 bfd_vma max_branch_offset;
5932 unsigned int r_type;
5bd4f169 5933
721956f4
AM
5934 if (h != NULL)
5935 {
5936 if (h->oh != NULL
721956f4 5937 && h->oh->dynindx != -1)
5bd4f169 5938 {
411e1bfb
AM
5939 struct plt_entry *ent;
5940 for (ent = h->oh->plt.plist; ent != NULL; ent = ent->next)
5941 if (ent->addend == rel->r_addend
5942 && ent->plt.offset != (bfd_vma) -1)
5943 {
5944 *hash = (struct ppc_link_hash_entry *) h->oh;
5945 return ppc_stub_plt_call;
5946 }
5bd4f169
AM
5947 }
5948
721956f4
AM
5949 if (h->elf.root.type == bfd_link_hash_undefweak
5950 || h->elf.root.type == bfd_link_hash_undefined)
5951 return ppc_stub_none;
5d1634d7 5952 }
5d1634d7 5953
721956f4
AM
5954 /* Determine where the call point is. */
5955 location = (input_sec->output_offset
5956 + input_sec->output_section->vma
5957 + rel->r_offset);
5d1634d7 5958
721956f4
AM
5959 branch_offset = destination - location;
5960 r_type = ELF64_R_TYPE (rel->r_info);
5d1634d7 5961
721956f4
AM
5962 /* Determine if a long branch stub is needed. */
5963 max_branch_offset = 1 << 25;
5964 if (r_type != (unsigned int) R_PPC64_REL24)
5965 max_branch_offset = 1 << 15;
5d1634d7 5966
721956f4
AM
5967 if (branch_offset + max_branch_offset >= 2 * max_branch_offset)
5968 /* We need a stub. Figure out whether a long_branch or plt_branch
5969 is needed later. */
5970 return ppc_stub_long_branch;
5d1634d7 5971
721956f4 5972 return ppc_stub_none;
5d1634d7
AM
5973}
5974
5975/* Build a .plt call stub. */
5976
5977static bfd_byte *
5978build_plt_stub (obfd, p, offset, glink)
5979 bfd *obfd;
5980 bfd_byte *p;
5981 int offset;
5982 int glink;
5983{
5984#define PPC_LO(v) ((v) & 0xffff)
5985#define PPC_HI(v) (((v) >> 16) & 0xffff)
5986#define PPC_HA(v) PPC_HI ((v) + 0x8000)
5987
5988 if (glink)
5989 bfd_put_32 (obfd, LD_R2_40R1, p), p += 4;
5990 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
5991 if (!glink)
5992 bfd_put_32 (obfd, STD_R2_40R1, p), p += 4;
5993 bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset), p), p += 4;
5994 if (PPC_HA (offset + 8) != PPC_HA (offset))
5995 bfd_put_32 (obfd, ADDIS_R12_R12_1, p), p += 4;
5996 offset += 8;
5997 bfd_put_32 (obfd, LD_R2_0R12 | PPC_LO (offset), p), p += 4;
5998 if (PPC_HA (offset + 8) != PPC_HA (offset))
5999 bfd_put_32 (obfd, ADDIS_R12_R12_1, p), p += 4;
6000 offset += 8;
6001 bfd_put_32 (obfd, MTCTR_R11, p), p += 4;
6002 bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset), p), p += 4;
6003 bfd_put_32 (obfd, BCTR, p), p += 4;
6004 return p;
6005}
6006
b34976b6 6007static bfd_boolean
721956f4
AM
6008ppc_build_one_stub (gen_entry, in_arg)
6009 struct bfd_hash_entry *gen_entry;
6010 PTR in_arg;
5d1634d7 6011{
721956f4
AM
6012 struct ppc_stub_hash_entry *stub_entry;
6013 struct ppc_branch_hash_entry *br_entry;
5d1634d7
AM
6014 struct bfd_link_info *info;
6015 struct ppc_link_hash_table *htab;
721956f4
AM
6016 asection *stub_sec;
6017 bfd *stub_bfd;
6018 bfd_byte *loc;
6019 bfd_byte *p;
6020 unsigned int indx;
411e1bfb 6021 struct plt_entry *ent;
721956f4
AM
6022 bfd_vma off;
6023 int size;
5d1634d7 6024
721956f4
AM
6025 /* Massage our args to the form they really have. */
6026 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
6027 info = (struct bfd_link_info *) in_arg;
5d1634d7 6028
5d1634d7 6029 htab = ppc_hash_table (info);
721956f4 6030 stub_sec = stub_entry->stub_sec;
5d1634d7 6031
721956f4
AM
6032 /* Make a note of the offset within the stubs for this entry. */
6033 stub_entry->stub_offset = stub_sec->_cooked_size;
6034 loc = stub_sec->contents + stub_entry->stub_offset;
6035
6036 stub_bfd = stub_sec->owner;
6037
6038 switch (stub_entry->stub_type)
5d1634d7 6039 {
721956f4
AM
6040 case ppc_stub_long_branch:
6041 /* Branches are relative. This is where we are going to. */
6042 off = (stub_entry->target_value
6043 + stub_entry->target_section->output_offset
6044 + stub_entry->target_section->output_section->vma);
5d1634d7 6045
721956f4
AM
6046 /* And this is where we are coming from. */
6047 off -= (stub_entry->stub_offset
6048 + stub_sec->output_offset
6049 + stub_sec->output_section->vma);
e86ce104 6050
721956f4 6051 BFD_ASSERT (off + (1 << 25) < (bfd_vma) (1 << 26));
e86ce104 6052
721956f4
AM
6053 bfd_put_32 (stub_bfd, (bfd_vma) B_DOT | (off & 0x3fffffc), loc);
6054 size = 4;
6055 break;
e86ce104 6056
721956f4
AM
6057 case ppc_stub_plt_branch:
6058 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
6059 stub_entry->root.string + 9,
b34976b6 6060 FALSE, FALSE);
721956f4
AM
6061 if (br_entry == NULL)
6062 {
6063 (*_bfd_error_handler) (_("can't find branch stub `%s'"),
6064 stub_entry->root.string + 9);
b34976b6
AM
6065 htab->stub_error = TRUE;
6066 return FALSE;
721956f4
AM
6067 }
6068
6069 off = (stub_entry->target_value
6070 + stub_entry->target_section->output_offset
6071 + stub_entry->target_section->output_section->vma);
6072
6073 bfd_put_64 (htab->sbrlt->owner, off,
6074 htab->sbrlt->contents + br_entry->offset);
6075
6076 if (info->shared)
6077 {
6078 /* Create a reloc for the branch lookup table entry. */
6079 Elf_Internal_Rela rela;
947216bf 6080 bfd_byte *loc;
5d1634d7 6081
721956f4
AM
6082 rela.r_offset = (br_entry->offset
6083 + htab->sbrlt->output_offset
6084 + htab->sbrlt->output_section->vma);
6085 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
6086 rela.r_addend = off;
6087
947216bf
AM
6088 loc = htab->srelbrlt->contents;
6089 loc += htab->srelbrlt->reloc_count++ * sizeof (Elf64_External_Rela);
6090 bfd_elf64_swap_reloca_out (htab->srelbrlt->owner, &rela, loc);
721956f4
AM
6091 }
6092
6093 off = (br_entry->offset
6094 + htab->sbrlt->output_offset
6095 + htab->sbrlt->output_section->vma
6096 - elf_gp (htab->sbrlt->output_section->owner)
6097 - TOC_BASE_OFF);
6098
6099 if (off + 0x80000000 > 0xffffffff || (off & 7) != 0)
5d1634d7
AM
6100 {
6101 (*_bfd_error_handler)
e86ce104 6102 (_("linkage table error against `%s'"),
721956f4 6103 stub_entry->root.string);
5d1634d7 6104 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
6105 htab->stub_error = TRUE;
6106 return FALSE;
5d1634d7 6107 }
41bd81ab 6108
721956f4
AM
6109 indx = off;
6110 bfd_put_32 (stub_bfd, (bfd_vma) ADDIS_R12_R2 | PPC_HA (indx), loc);
6111 bfd_put_32 (stub_bfd, (bfd_vma) LD_R11_0R12 | PPC_LO (indx), loc + 4);
6112 bfd_put_32 (stub_bfd, (bfd_vma) MTCTR_R11, loc + 8);
6113 bfd_put_32 (stub_bfd, (bfd_vma) BCTR, loc + 12);
6114 size = 16;
6115 break;
5d1634d7 6116
721956f4 6117 case ppc_stub_plt_call:
c862ae31
AM
6118 /* Do the best we can for shared libraries built without
6119 exporting ".foo" for each "foo". This can happen when symbol
6120 versioning scripts strip all bar a subset of symbols. */
6121 if (stub_entry->h->oh->root.type != bfd_link_hash_defined
6122 && stub_entry->h->oh->root.type != bfd_link_hash_defweak)
6123 {
6124 /* Point the symbol at the stub. There may be multiple stubs,
6125 we don't really care; The main thing is to make this sym
6126 defined somewhere. */
6127 stub_entry->h->oh->root.type = bfd_link_hash_defined;
6128 stub_entry->h->oh->root.u.def.section = stub_entry->stub_sec;
6129 stub_entry->h->oh->root.u.def.value = stub_entry->stub_offset;
6130 }
6131
721956f4 6132 /* Now build the stub. */
411e1bfb
AM
6133 off = (bfd_vma) -1;
6134 for (ent = stub_entry->h->elf.plt.plist; ent != NULL; ent = ent->next)
6135 if (ent->addend == stub_entry->addend)
6136 {
6137 off = ent->plt.offset;
6138 break;
6139 }
721956f4
AM
6140 if (off >= (bfd_vma) -2)
6141 abort ();
6142
6143 off &= ~ (bfd_vma) 1;
6144 off += (htab->splt->output_offset
6145 + htab->splt->output_section->vma
6146 - elf_gp (htab->splt->output_section->owner)
6147 - TOC_BASE_OFF);
6148
6149 if (off + 0x80000000 > 0xffffffff || (off & 7) != 0)
6150 {
6151 (*_bfd_error_handler)
6152 (_("linkage table error against `%s'"),
6153 stub_entry->h->elf.root.root.string);
6154 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
6155 htab->stub_error = TRUE;
6156 return FALSE;
721956f4
AM
6157 }
6158
6159 p = build_plt_stub (stub_bfd, loc, (int) off, 0);
6160 size = p - loc;
6161 break;
6162
6163 default:
6164 BFD_FAIL ();
b34976b6 6165 return FALSE;
721956f4
AM
6166 }
6167
6168 stub_sec->_cooked_size += size;
b34976b6 6169 return TRUE;
721956f4
AM
6170}
6171
6172/* As above, but don't actually build the stub. Just bump offset so
6173 we know stub section sizes, and select plt_branch stubs where
6174 long_branch stubs won't do. */
6175
b34976b6 6176static bfd_boolean
721956f4
AM
6177ppc_size_one_stub (gen_entry, in_arg)
6178 struct bfd_hash_entry *gen_entry;
6179 PTR in_arg;
6180{
6181 struct ppc_stub_hash_entry *stub_entry;
6182 struct ppc_link_hash_table *htab;
6183 bfd_vma off;
6184 int size;
6185
6186 /* Massage our args to the form they really have. */
6187 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
6188 htab = (struct ppc_link_hash_table *) in_arg;
6189
6190 if (stub_entry->stub_type == ppc_stub_plt_call)
6191 {
411e1bfb
AM
6192 struct plt_entry *ent;
6193 for (ent = stub_entry->h->elf.plt.plist; ent != NULL; ent = ent->next)
6194 if (ent->addend == stub_entry->addend)
6195 {
6196 off = ent->plt.offset & ~(bfd_vma) 1;
6197 break;
6198 }
6199 if (ent == NULL)
6200 abort ();
721956f4
AM
6201 off += (htab->splt->output_offset
6202 + htab->splt->output_section->vma
6203 - elf_gp (htab->splt->output_section->owner)
6204 - TOC_BASE_OFF);
6205
6206 size = 28;
6207 if (PPC_HA ((int) off + 16) != PPC_HA ((int) off))
6208 size += 4;
6209 }
6210 else
6211 {
6212 /* ppc_stub_long_branch or ppc_stub_plt_branch. */
6213 stub_entry->stub_type = ppc_stub_long_branch;
6214 size = 4;
6215
6216 off = (stub_entry->target_value
6217 + stub_entry->target_section->output_offset
6218 + stub_entry->target_section->output_section->vma);
6219 off -= (stub_entry->stub_sec->_raw_size
6220 + stub_entry->stub_sec->output_offset
6221 + stub_entry->stub_sec->output_section->vma);
6222
6223 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
6224 {
6225 struct ppc_branch_hash_entry *br_entry;
6226
6227 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
6228 stub_entry->root.string + 9,
b34976b6 6229 TRUE, FALSE);
721956f4
AM
6230 if (br_entry == NULL)
6231 {
6232 (*_bfd_error_handler) (_("can't build branch stub `%s'"),
6233 stub_entry->root.string + 9);
b34976b6
AM
6234 htab->stub_error = TRUE;
6235 return FALSE;
721956f4
AM
6236 }
6237
6238 if (br_entry->iter != htab->stub_iteration)
6239 {
6240 br_entry->iter = htab->stub_iteration;
6241 br_entry->offset = htab->sbrlt->_raw_size;
6242 htab->sbrlt->_raw_size += 8;
6243 }
6244 stub_entry->stub_type = ppc_stub_plt_branch;
6245 size = 16;
6246 }
6247 }
6248
6249 stub_entry->stub_sec->_raw_size += size;
b34976b6 6250 return TRUE;
721956f4
AM
6251}
6252
6253/* Set up various things so that we can make a list of input sections
6254 for each output section included in the link. Returns -1 on error,
cedb70c5 6255 0 when no stubs will be needed, and 1 on success. */
721956f4
AM
6256
6257int
6258ppc64_elf_setup_section_lists (output_bfd, info)
6259 bfd *output_bfd;
6260 struct bfd_link_info *info;
6261{
6262 bfd *input_bfd;
721956f4
AM
6263 int top_id, top_index;
6264 asection *section;
6265 asection **input_list, **list;
6266 bfd_size_type amt;
6267 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6268
805fc799
AM
6269 if (htab->elf.root.creator->flavour != bfd_target_elf_flavour
6270 || htab->sbrlt == NULL)
721956f4
AM
6271 return 0;
6272
1e2f5b6e
AM
6273 /* Find the top input section id. */
6274 for (input_bfd = info->input_bfds, top_id = 0;
721956f4
AM
6275 input_bfd != NULL;
6276 input_bfd = input_bfd->link_next)
6277 {
721956f4
AM
6278 for (section = input_bfd->sections;
6279 section != NULL;
6280 section = section->next)
6281 {
6282 if (top_id < section->id)
6283 top_id = section->id;
6284 }
6285 }
721956f4
AM
6286
6287 amt = sizeof (struct map_stub) * (top_id + 1);
6288 htab->stub_group = (struct map_stub *) bfd_zmalloc (amt);
6289 if (htab->stub_group == NULL)
6290 return -1;
6291
6292 /* We can't use output_bfd->section_count here to find the top output
6293 section index as some sections may have been removed, and
6294 _bfd_strip_section_from_output doesn't renumber the indices. */
6295 for (section = output_bfd->sections, top_index = 0;
6296 section != NULL;
6297 section = section->next)
6298 {
6299 if (top_index < section->index)
6300 top_index = section->index;
6301 }
6302
6303 htab->top_index = top_index;
6304 amt = sizeof (asection *) * (top_index + 1);
6305 input_list = (asection **) bfd_malloc (amt);
6306 htab->input_list = input_list;
6307 if (input_list == NULL)
6308 return -1;
6309
6310 /* For sections we aren't interested in, mark their entries with a
6311 value we can check later. */
6312 list = input_list + top_index;
6313 do
6314 *list = bfd_abs_section_ptr;
6315 while (list-- != input_list);
6316
6317 for (section = output_bfd->sections;
6318 section != NULL;
6319 section = section->next)
6320 {
6321 if ((section->flags & SEC_CODE) != 0)
6322 input_list[section->index] = NULL;
6323 }
6324
6325 return 1;
6326}
6327
6328/* The linker repeatedly calls this function for each input section,
6329 in the order that input sections are linked into output sections.
6330 Build lists of input sections to determine groupings between which
6331 we may insert linker stubs. */
6332
6333void
805fc799 6334ppc64_elf_next_input_section (info, isec)
721956f4
AM
6335 struct bfd_link_info *info;
6336 asection *isec;
6337{
6338 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6339
805fc799 6340 if (isec->output_section->index <= htab->top_index)
721956f4
AM
6341 {
6342 asection **list = htab->input_list + isec->output_section->index;
6343 if (*list != bfd_abs_section_ptr)
6344 {
6345 /* Steal the link_sec pointer for our list. */
6346#define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
6347 /* This happens to make the list in reverse order,
6348 which is what we want. */
6349 PREV_SEC (isec) = *list;
6350 *list = isec;
6351 }
6352 }
6353}
6354
6355/* See whether we can group stub sections together. Grouping stub
6356 sections may result in fewer stubs. More importantly, we need to
6357 put all .init* and .fini* stubs at the beginning of the .init or
6358 .fini output sections respectively, because glibc splits the
6359 _init and _fini functions into multiple parts. Putting a stub in
6360 the middle of a function is not a good idea. */
6361
6362static void
6363group_sections (htab, stub_group_size, stubs_always_before_branch)
6364 struct ppc_link_hash_table *htab;
6365 bfd_size_type stub_group_size;
b34976b6 6366 bfd_boolean stubs_always_before_branch;
721956f4
AM
6367{
6368 asection **list = htab->input_list + htab->top_index;
6369 do
6370 {
6371 asection *tail = *list;
6372 if (tail == bfd_abs_section_ptr)
6373 continue;
6374 while (tail != NULL)
6375 {
6376 asection *curr;
6377 asection *prev;
6378 bfd_size_type total;
6379
6380 curr = tail;
6381 if (tail->_cooked_size)
6382 total = tail->_cooked_size;
6383 else
6384 total = tail->_raw_size;
6385 while ((prev = PREV_SEC (curr)) != NULL
6386 && ((total += curr->output_offset - prev->output_offset)
6387 < stub_group_size))
6388 curr = prev;
6389
6390 /* OK, the size from the start of CURR to the end is less
6391 than stub_group_size and thus can be handled by one stub
6392 section. (or the tail section is itself larger than
6393 stub_group_size, in which case we may be toast.) We
6394 should really be keeping track of the total size of stubs
6395 added here, as stubs contribute to the final output
6396 section size. That's a little tricky, and this way will
6397 only break if stubs added make the total size more than
6398 2^25, ie. for the default stub_group_size, if stubs total
6399 more than 2834432 bytes, or over 100000 plt call stubs. */
6400 do
6401 {
6402 prev = PREV_SEC (tail);
6403 /* Set up this stub group. */
6404 htab->stub_group[tail->id].link_sec = curr;
6405 }
6406 while (tail != curr && (tail = prev) != NULL);
6407
6408 /* But wait, there's more! Input sections up to stub_group_size
6409 bytes before the stub section can be handled by it too. */
6410 if (!stubs_always_before_branch)
6411 {
6412 total = 0;
6413 while (prev != NULL
6414 && ((total += tail->output_offset - prev->output_offset)
6415 < stub_group_size))
6416 {
6417 tail = prev;
6418 prev = PREV_SEC (tail);
6419 htab->stub_group[tail->id].link_sec = curr;
6420 }
6421 }
6422 tail = prev;
6423 }
6424 }
6425 while (list-- != htab->input_list);
6426 free (htab->input_list);
6427#undef PREV_SEC
6428}
6429
721956f4
AM
6430/* Determine and set the size of the stub section for a final link.
6431
6432 The basic idea here is to examine all the relocations looking for
6433 PC-relative calls to a target that is unreachable with a "bl"
6434 instruction. */
6435
b34976b6 6436bfd_boolean
721956f4
AM
6437ppc64_elf_size_stubs (output_bfd, stub_bfd, info, group_size,
6438 add_stub_section, layout_sections_again)
6439 bfd *output_bfd;
6440 bfd *stub_bfd;
6441 struct bfd_link_info *info;
6442 bfd_signed_vma group_size;
6443 asection * (*add_stub_section) PARAMS ((const char *, asection *));
6444 void (*layout_sections_again) PARAMS ((void));
6445{
6446 bfd_size_type stub_group_size;
b34976b6 6447 bfd_boolean stubs_always_before_branch;
721956f4
AM
6448 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6449
6450 /* Stash our params away. */
6451 htab->stub_bfd = stub_bfd;
6452 htab->add_stub_section = add_stub_section;
6453 htab->layout_sections_again = layout_sections_again;
6454 stubs_always_before_branch = group_size < 0;
6455 if (group_size < 0)
6456 stub_group_size = -group_size;
6457 else
6458 stub_group_size = group_size;
6459 if (stub_group_size == 1)
6460 {
6461 /* Default values. */
6462 stub_group_size = 30720000;
6463 if (htab->has_14bit_branch)
6464 stub_group_size = 30000;
6465 }
6466
6467 group_sections (htab, stub_group_size, stubs_always_before_branch);
6468
721956f4
AM
6469 while (1)
6470 {
6471 bfd *input_bfd;
6472 unsigned int bfd_indx;
6473 asection *stub_sec;
b34976b6 6474 bfd_boolean stub_changed;
721956f4
AM
6475
6476 htab->stub_iteration += 1;
b34976b6 6477 stub_changed = FALSE;
721956f4
AM
6478
6479 for (input_bfd = info->input_bfds, bfd_indx = 0;
6480 input_bfd != NULL;
6481 input_bfd = input_bfd->link_next, bfd_indx++)
6482 {
6483 Elf_Internal_Shdr *symtab_hdr;
6484 asection *section;
6cdc0ccc 6485 Elf_Internal_Sym *local_syms = NULL;
721956f4
AM
6486
6487 /* We'll need the symbol table in a second. */
6488 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
6489 if (symtab_hdr->sh_info == 0)
6490 continue;
6491
721956f4
AM
6492 /* Walk over each section attached to the input bfd. */
6493 for (section = input_bfd->sections;
6494 section != NULL;
6495 section = section->next)
6496 {
721956f4 6497 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
721956f4
AM
6498
6499 /* If there aren't any relocs, then there's nothing more
6500 to do. */
6501 if ((section->flags & SEC_RELOC) == 0
6502 || section->reloc_count == 0)
6503 continue;
6504
6505 /* If this section is a link-once section that will be
6506 discarded, then don't create any stubs. */
6507 if (section->output_section == NULL
6508 || section->output_section->owner != output_bfd)
6509 continue;
6510
1e2f5b6e
AM
6511 /* Get the relocs. */
6512 internal_relocs
6513 = _bfd_elf64_link_read_relocs (input_bfd, section, NULL,
6514 (Elf_Internal_Rela *) NULL,
6515 info->keep_memory);
721956f4 6516 if (internal_relocs == NULL)
1e2f5b6e 6517 goto error_ret_free_local;
721956f4
AM
6518
6519 /* Now examine each relocation. */
6520 irela = internal_relocs;
6521 irelaend = irela + section->reloc_count;
6522 for (; irela < irelaend; irela++)
6523 {
6524 unsigned int r_type, r_indx;
6525 enum ppc_stub_type stub_type;
6526 struct ppc_stub_hash_entry *stub_entry;
6527 asection *sym_sec;
6528 bfd_vma sym_value;
6529 bfd_vma destination;
6530 struct ppc_link_hash_entry *hash;
411e1bfb
AM
6531 struct elf_link_hash_entry *h;
6532 Elf_Internal_Sym *sym;
721956f4
AM
6533 char *stub_name;
6534 const asection *id_sec;
6535
6536 r_type = ELF64_R_TYPE (irela->r_info);
6537 r_indx = ELF64_R_SYM (irela->r_info);
6538
04c9666a 6539 if (r_type >= (unsigned int) R_PPC64_max)
721956f4
AM
6540 {
6541 bfd_set_error (bfd_error_bad_value);
6cdc0ccc 6542 goto error_ret_free_internal;
721956f4
AM
6543 }
6544
6545 /* Only look for stubs on branch instructions. */
6546 if (r_type != (unsigned int) R_PPC64_REL24
6547 && r_type != (unsigned int) R_PPC64_REL14
6548 && r_type != (unsigned int) R_PPC64_REL14_BRTAKEN
6549 && r_type != (unsigned int) R_PPC64_REL14_BRNTAKEN)
6550 continue;
6551
6552 /* Now determine the call target, its name, value,
6553 section. */
721956f4 6554 destination = 0;
411e1bfb
AM
6555 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
6556 r_indx, input_bfd))
6557 goto error_ret_free_internal;
6558 hash = (struct ppc_link_hash_entry *) h;
6559
6560 if (hash == NULL)
721956f4
AM
6561 {
6562 /* It's a local symbol. */
411e1bfb 6563 sym_value = sym->st_value;
721956f4
AM
6564 destination = (sym_value + irela->r_addend
6565 + sym_sec->output_offset
6566 + sym_sec->output_section->vma);
6567 }
6568 else
6569 {
6570 /* It's an external symbol. */
411e1bfb 6571 sym_value = 0;
721956f4
AM
6572 if (hash->elf.root.type == bfd_link_hash_defined
6573 || hash->elf.root.type == bfd_link_hash_defweak)
6574 {
721956f4
AM
6575 sym_value = hash->elf.root.u.def.value;
6576 if (sym_sec->output_section != NULL)
6577 destination = (sym_value + irela->r_addend
6578 + sym_sec->output_offset
6579 + sym_sec->output_section->vma);
6580 }
6581 else if (hash->elf.root.type == bfd_link_hash_undefweak)
6582 ;
6583 else if (hash->elf.root.type == bfd_link_hash_undefined)
6584 ;
6585 else
6586 {
6587 bfd_set_error (bfd_error_bad_value);
6588 goto error_ret_free_internal;
6589 }
6590 }
6591
6592 /* Determine what (if any) linker stub is needed. */
6593 stub_type = ppc_type_of_stub (section, irela, &hash,
6594 destination);
6595 if (stub_type == ppc_stub_none)
6596 continue;
6597
411e1bfb
AM
6598 /* __tls_get_addr calls might be eliminated. */
6599 if (stub_type != ppc_stub_plt_call
6600 && hash != NULL
6601 && &hash->elf == htab->tls_get_addr
6602 && section->has_tls_reloc
6603 && irela != internal_relocs)
6604 {
6605 /* Get tls info. */
6606 char *tls_type;
6607
6608 if (!get_tls_type (&tls_type, &local_syms,
6609 irela - 1, input_bfd))
6610 goto error_ret_free_internal;
6611 if (tls_type != NULL
6612 && (*tls_type & (TLS_TLS | TLS_GD_LD)) == TLS_TLS)
6613 continue;
6614 }
6615
721956f4
AM
6616 /* Support for grouping stub sections. */
6617 id_sec = htab->stub_group[section->id].link_sec;
6618
6619 /* Get the name of this stub. */
6620 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
6621 if (!stub_name)
6622 goto error_ret_free_internal;
6623
6624 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 6625 stub_name, FALSE, FALSE);
721956f4
AM
6626 if (stub_entry != NULL)
6627 {
6628 /* The proper stub has already been created. */
6629 free (stub_name);
6630 continue;
6631 }
6632
6633 stub_entry = ppc_add_stub (stub_name, section, htab);
6634 if (stub_entry == NULL)
6635 {
6636 free (stub_name);
6cdc0ccc
AM
6637 error_ret_free_internal:
6638 if (elf_section_data (section)->relocs == NULL)
6639 free (internal_relocs);
6640 error_ret_free_local:
6641 if (local_syms != NULL
6642 && (symtab_hdr->contents
6643 != (unsigned char *) local_syms))
6644 free (local_syms);
b34976b6 6645 return FALSE;
721956f4
AM
6646 }
6647
6648 stub_entry->target_value = sym_value;
6649 stub_entry->target_section = sym_sec;
6650 stub_entry->stub_type = stub_type;
6651 stub_entry->h = hash;
411e1bfb 6652 stub_entry->addend = irela->r_addend;
b34976b6 6653 stub_changed = TRUE;
721956f4
AM
6654 }
6655
6656 /* We're done with the internal relocs, free them. */
6cdc0ccc 6657 if (elf_section_data (section)->relocs != internal_relocs)
1e2f5b6e 6658 free (internal_relocs);
721956f4 6659 }
6cdc0ccc
AM
6660
6661 if (local_syms != NULL
6662 && symtab_hdr->contents != (unsigned char *) local_syms)
6663 {
6664 if (!info->keep_memory)
6665 free (local_syms);
6666 else
6667 symtab_hdr->contents = (unsigned char *) local_syms;
6668 }
721956f4
AM
6669 }
6670
6671 if (!stub_changed)
6672 break;
6673
6674 /* OK, we've added some stubs. Find out the new size of the
6675 stub sections. */
6676 for (stub_sec = htab->stub_bfd->sections;
6677 stub_sec != NULL;
6678 stub_sec = stub_sec->next)
6679 {
6680 stub_sec->_raw_size = 0;
6681 stub_sec->_cooked_size = 0;
6682 }
6683 htab->sbrlt->_raw_size = 0;
6684 htab->sbrlt->_cooked_size = 0;
6685
6686 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, htab);
6687
6688 /* Ask the linker to do its stuff. */
6689 (*htab->layout_sections_again) ();
6690 }
6691
afbe61cf
AM
6692 /* It would be nice to strip .branch_lt from the output if the
6693 section is empty, but it's too late. If we strip sections here,
6694 the dynamic symbol table is corrupted since the section symbol
6695 for the stripped section isn't written. */
721956f4 6696
b34976b6 6697 return TRUE;
721956f4
AM
6698}
6699
6700/* Called after we have determined section placement. If sections
805fc799 6701 move, we'll be called again. Provide a value for TOCstart. */
721956f4 6702
805fc799
AM
6703bfd_vma
6704ppc64_elf_toc (obfd)
5d1634d7 6705 bfd *obfd;
721956f4 6706{
805fc799
AM
6707 asection *s;
6708 bfd_vma TOCstart;
721956f4 6709
805fc799
AM
6710 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
6711 order. The TOC starts where the first of these sections starts. */
6712 s = bfd_get_section_by_name (obfd, ".got");
6713 if (s == NULL)
6714 s = bfd_get_section_by_name (obfd, ".toc");
6715 if (s == NULL)
6716 s = bfd_get_section_by_name (obfd, ".tocbss");
6717 if (s == NULL)
6718 s = bfd_get_section_by_name (obfd, ".plt");
6719 if (s == NULL)
6720 {
6721 /* This may happen for
6722 o references to TOC base (SYM@toc / TOC[tc0]) without a
6723 .toc directive
6724 o bad linker script
6725 o --gc-sections and empty TOC sections
6726
6727 FIXME: Warn user? */
6728
6729 /* Look for a likely section. We probably won't even be
6730 using TOCstart. */
6731 for (s = obfd->sections; s != NULL; s = s->next)
6732 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY))
6733 == (SEC_ALLOC | SEC_SMALL_DATA))
6734 break;
721956f4 6735 if (s == NULL)
805fc799
AM
6736 for (s = obfd->sections; s != NULL; s = s->next)
6737 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA))
6738 == (SEC_ALLOC | SEC_SMALL_DATA))
6739 break;
721956f4 6740 if (s == NULL)
805fc799
AM
6741 for (s = obfd->sections; s != NULL; s = s->next)
6742 if ((s->flags & (SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
6743 break;
721956f4 6744 if (s == NULL)
805fc799
AM
6745 for (s = obfd->sections; s != NULL; s = s->next)
6746 if ((s->flags & SEC_ALLOC) == SEC_ALLOC)
6747 break;
6748 }
721956f4 6749
805fc799
AM
6750 TOCstart = 0;
6751 if (s != NULL)
6752 TOCstart = s->output_section->vma + s->output_offset;
721956f4 6753
805fc799 6754 return TOCstart;
721956f4
AM
6755}
6756
6757/* Build all the stubs associated with the current output file.
6758 The stubs are kept in a hash table attached to the main linker
6759 hash table. This function is called via gldelf64ppc_finish. */
6760
b34976b6 6761bfd_boolean
721956f4
AM
6762ppc64_elf_build_stubs (info)
6763 struct bfd_link_info *info;
5d1634d7
AM
6764{
6765 struct ppc_link_hash_table *htab = ppc_hash_table (info);
721956f4 6766 asection *stub_sec;
5d1634d7
AM
6767 bfd_vma plt_r2;
6768 bfd_byte *p;
6769
721956f4
AM
6770 for (stub_sec = htab->stub_bfd->sections;
6771 stub_sec != NULL;
6772 stub_sec = stub_sec->next)
6773 {
6774 bfd_size_type size;
6775
6776 /* Allocate memory to hold the linker stubs. */
6777 size = stub_sec->_raw_size;
6778 if (size != 0)
6779 {
6780 stub_sec->contents = (bfd_byte *) bfd_zalloc (htab->stub_bfd, size);
6781 if (stub_sec->contents == NULL)
b34976b6 6782 return FALSE;
721956f4
AM
6783 }
6784 stub_sec->_cooked_size = 0;
6785 }
5d1634d7 6786
721956f4 6787 if (htab->splt != NULL)
5d1634d7 6788 {
9f951329
AM
6789 unsigned int indx;
6790
721956f4
AM
6791 /* Build the .glink plt call stub. */
6792 plt_r2 = (htab->splt->output_offset
6793 + htab->splt->output_section->vma
6794 - elf_gp (htab->splt->output_section->owner)
6795 - TOC_BASE_OFF);
6796 p = htab->sglink->contents;
6797 p = build_plt_stub (htab->sglink->owner, p, (int) plt_r2, 1);
9f951329 6798 while (p < htab->sglink->contents + GLINK_CALL_STUB_SIZE)
721956f4
AM
6799 {
6800 bfd_put_32 (htab->sglink->owner, NOP, p);
6801 p += 4;
6802 }
721956f4 6803
9f951329
AM
6804 /* Build the .glink lazy link call stubs. */
6805 indx = 0;
6806 while (p < htab->sglink->contents + htab->sglink->_raw_size)
6807 {
6808 if (indx < 0x8000)
6809 {
6810 bfd_put_32 (htab->sglink->owner, LI_R0_0 | indx, p);
6811 p += 4;
6812 }
6813 else
6814 {
6815 bfd_put_32 (htab->sglink->owner, LIS_R0_0 | PPC_HI (indx), p);
6816 p += 4;
6817 bfd_put_32 (htab->sglink->owner, ORI_R0_R0_0 | PPC_LO (indx), p);
6818 p += 4;
6819 }
6820 bfd_put_32 (htab->sglink->owner,
6821 B_DOT | ((htab->sglink->contents - p) & 0x3fffffc), p);
a16d5acb 6822 indx++;
9f951329
AM
6823 p += 4;
6824 }
6825 htab->sglink->_cooked_size = p - htab->sglink->contents;
5d1634d7 6826 }
5d1634d7 6827
721956f4
AM
6828 if (htab->sbrlt->_raw_size != 0)
6829 {
6830 htab->sbrlt->contents = (bfd_byte *) bfd_zalloc (htab->sbrlt->owner,
6831 htab->sbrlt->_raw_size);
6832 if (htab->sbrlt->contents == NULL)
b34976b6 6833 return FALSE;
721956f4 6834 }
5d1634d7 6835
721956f4
AM
6836 /* Build the stubs as directed by the stub hash table. */
6837 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
5d1634d7 6838
721956f4
AM
6839 for (stub_sec = htab->stub_bfd->sections;
6840 stub_sec != NULL;
6841 stub_sec = stub_sec->next)
6842 {
6843 if (stub_sec->_raw_size != stub_sec->_cooked_size)
6844 break;
6845 }
5d1634d7 6846
721956f4 6847 if (stub_sec != NULL
5d1634d7
AM
6848 || htab->sglink->_raw_size != htab->sglink->_cooked_size)
6849 {
b34976b6 6850 htab->stub_error = TRUE;
721956f4 6851 (*_bfd_error_handler) (_("stubs don't match calculated size"));
5d1634d7 6852 }
721956f4
AM
6853
6854 return !htab->stub_error;
5bd4f169
AM
6855}
6856
5bd4f169
AM
6857/* The RELOCATE_SECTION function is called by the ELF backend linker
6858 to handle the relocations for a section.
6859
6860 The relocs are always passed as Rela structures; if the section
6861 actually uses Rel structures, the r_addend field will always be
6862 zero.
6863
6864 This function is responsible for adjust the section contents as
6865 necessary, and (if using Rela relocs and generating a
6866 relocateable output file) adjusting the reloc addend as
6867 necessary.
6868
6869 This function does not have to worry about setting the reloc
6870 address or the reloc symbol index.
6871
6872 LOCAL_SYMS is a pointer to the swapped in local symbols.
6873
6874 LOCAL_SECTIONS is an array giving the section in the input file
6875 corresponding to the st_shndx field of each local symbol.
6876
6877 The global hash table entry for the global symbols can be found
6878 via elf_sym_hashes (input_bfd).
6879
6880 When generating relocateable output, this function must handle
6881 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
6882 going to be the section symbol corresponding to the output
6883 section, which means that the addend must be adjusted
6884 accordingly. */
6885
b34976b6 6886static bfd_boolean
5bd4f169
AM
6887ppc64_elf_relocate_section (output_bfd, info, input_bfd, input_section,
6888 contents, relocs, local_syms, local_sections)
6889 bfd *output_bfd;
6890 struct bfd_link_info *info;
6891 bfd *input_bfd;
6892 asection *input_section;
6893 bfd_byte *contents;
6894 Elf_Internal_Rela *relocs;
6895 Elf_Internal_Sym *local_syms;
6896 asection **local_sections;
6897{
65f38f15 6898 struct ppc_link_hash_table *htab;
5bd4f169
AM
6899 Elf_Internal_Shdr *symtab_hdr;
6900 struct elf_link_hash_entry **sym_hashes;
5bd4f169
AM
6901 Elf_Internal_Rela *rel;
6902 Elf_Internal_Rela *relend;
411e1bfb
AM
6903 Elf_Internal_Rela outrel;
6904 bfd_byte *loc;
6905 unsigned int tls_get_addr_type;
6906 struct got_entry **local_got_ents;
5bd4f169 6907 bfd_vma TOCstart;
b34976b6
AM
6908 bfd_boolean ret = TRUE;
6909 bfd_boolean is_opd;
96e0dda4 6910 /* Disabled until we sort out how ld should choose 'y' vs 'at'. */
b34976b6 6911 bfd_boolean is_power4 = FALSE;
5bd4f169 6912
b491616a 6913 if (info->relocateable)
b34976b6 6914 return TRUE;
b491616a 6915
65f38f15 6916 /* Initialize howto table if needed. */
5bd4f169 6917 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
6918 ppc_howto_init ();
6919
65f38f15 6920 htab = ppc_hash_table (info);
411e1bfb
AM
6921 if (info->shared && (htab->tlsld_got.offset & 1) == 0)
6922 {
6923 outrel.r_offset = (htab->sgot->output_section->vma
6924 + htab->sgot->output_offset
6925 + htab->tlsld_got.offset);
6926 outrel.r_info = ELF64_R_INFO (0, R_PPC64_DTPMOD64);
6927 outrel.r_addend = 0;
6928
6929 loc = htab->srelgot->contents;
6930 loc += htab->srelgot->reloc_count++ * sizeof (Elf64_External_Rela);
6931 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
6932 htab->tlsld_got.offset |= 1;
6933 }
6934
6935 local_got_ents = elf_local_got_ents (input_bfd);
5bd4f169 6936 TOCstart = elf_gp (output_bfd);
5bd4f169
AM
6937 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
6938 sym_hashes = elf_sym_hashes (input_bfd);
f0abc2a1 6939 is_opd = ppc64_elf_section_data (input_section)->opd.adjust != NULL;
65f38f15 6940
411e1bfb 6941 tls_get_addr_type = 0;
5bd4f169
AM
6942 rel = relocs;
6943 relend = relocs + input_section->reloc_count;
6944 for (; rel < relend; rel++)
6945 {
04c9666a 6946 enum elf_ppc64_reloc_type r_type;
5bd4f169
AM
6947 bfd_vma addend;
6948 bfd_reloc_status_type r;
6949 Elf_Internal_Sym *sym;
6950 asection *sec;
6951 struct elf_link_hash_entry *h;
721956f4 6952 struct elf_link_hash_entry *fdh;
5bd4f169 6953 const char *sym_name;
5bd4f169 6954 unsigned long r_symndx;
411e1bfb 6955 char tls_type;
5bd4f169 6956 bfd_vma relocation;
b34976b6
AM
6957 bfd_boolean unresolved_reloc;
6958 bfd_boolean warned;
dc810e39 6959 long insn;
721956f4
AM
6960 struct ppc_stub_hash_entry *stub_entry;
6961 bfd_vma max_br_offset;
6962 bfd_vma from;
5bd4f169 6963
04c9666a 6964 r_type = (enum elf_ppc64_reloc_type) ELF64_R_TYPE (rel->r_info);
5bd4f169 6965 r_symndx = ELF64_R_SYM (rel->r_info);
65f38f15
AM
6966 r = bfd_reloc_other;
6967 sym = (Elf_Internal_Sym *) 0;
6968 sec = (asection *) 0;
6969 h = (struct elf_link_hash_entry *) 0;
6970 sym_name = (const char *) 0;
b34976b6
AM
6971 unresolved_reloc = FALSE;
6972 warned = FALSE;
65f38f15 6973
5bd4f169
AM
6974 if (r_type == R_PPC64_TOC)
6975 {
6976 /* Relocation value is TOC base. Symbol is ignored. */
6977 relocation = TOCstart + TOC_BASE_OFF;
6978 }
6979 else if (r_symndx < symtab_hdr->sh_info)
6980 {
6981 /* It's a local symbol. */
6982 sym = local_syms + r_symndx;
6983 sec = local_sections[r_symndx];
411e1bfb 6984 sym_name = bfd_elf_local_sym_name (input_bfd, sym);
f8df10f4 6985 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, sec, rel);
1e2f5b6e
AM
6986 if (elf_section_data (sec) != NULL)
6987 {
6988 long *opd_sym_adjust;
6989
f0abc2a1 6990 opd_sym_adjust = ppc64_elf_section_data (sec)->opd.adjust;
1e2f5b6e
AM
6991 if (opd_sym_adjust != NULL && sym->st_value % 24 == 0)
6992 relocation += opd_sym_adjust[sym->st_value / 24];
6993 }
5bd4f169
AM
6994 }
6995 else
6996 {
6997 /* It's a global symbol. */
6998 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6999 while (h->root.type == bfd_link_hash_indirect
7000 || h->root.type == bfd_link_hash_warning)
7001 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7002 sym_name = h->root.root.string;
65f38f15 7003 relocation = 0;
5bd4f169
AM
7004 if (h->root.type == bfd_link_hash_defined
7005 || h->root.type == bfd_link_hash_defweak)
7006 {
7007 sec = h->root.u.def.section;
65f38f15
AM
7008 if (sec->output_section == NULL)
7009 /* Set a flag that will be cleared later if we find a
7010 relocation value for this symbol. output_section
7011 is typically NULL for symbols satisfied by a shared
7012 library. */
b34976b6 7013 unresolved_reloc = TRUE;
5bd4f169
AM
7014 else
7015 relocation = (h->root.u.def.value
7016 + sec->output_section->vma
7017 + sec->output_offset);
7018 }
7019 else if (h->root.type == bfd_link_hash_undefweak)
65f38f15 7020 ;
671bae9c 7021 else if (info->shared
65f38f15
AM
7022 && (!info->symbolic || info->allow_shlib_undefined)
7023 && !info->no_undefined
7024 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
7025 ;
5bd4f169
AM
7026 else
7027 {
7028 if (! ((*info->callbacks->undefined_symbol)
7029 (info, h->root.root.string, input_bfd, input_section,
411e1bfb
AM
7030 rel->r_offset, (!info->shared
7031 || info->no_undefined
7032 || ELF_ST_VISIBILITY (h->other)))))
b34976b6
AM
7033 return FALSE;
7034 warned = TRUE;
5bd4f169
AM
7035 }
7036 }
7037
411e1bfb
AM
7038 /* TLS optimizations. */
7039 tls_type = 0;
7040 if (IS_TLS_RELOC (r_type))
7041 {
7042 if (h != NULL)
7043 tls_type = ((struct ppc_link_hash_entry *) h)->tls_type;
7044 else if (local_got_ents != NULL)
7045 {
7046 char *lgot_types;
7047 lgot_types = (char *) (local_got_ents + symtab_hdr->sh_info);
7048 tls_type = lgot_types[r_symndx];
7049 }
7050 }
7051
7052 /* Ensure reloc mapping code below stays sane. */
7053 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
7054 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
7055 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
7056 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
7057 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
7058 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
7059 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
7060 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
7061 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
7062 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
7063 abort ();
7064 switch (r_type)
7065 {
7066 default:
7067 tls_get_addr_type = 0;
7068 break;
7069
7070 case R_PPC64_TOC16:
7071 case R_PPC64_TOC16_LO:
7072 case R_PPC64_TOC16_DS:
7073 case R_PPC64_TOC16_LO_DS:
7074 tls_get_addr_type = 0;
7075 {
7076 /* Check for toc tls entries. */
7077 char *toc_tls;
7078
7079 if (!get_tls_type (&toc_tls, &local_syms, rel, input_bfd))
7080 return FALSE;
7081
7082 if (toc_tls)
7083 {
7084 tls_type = *toc_tls;
7085 if (r_type == R_PPC64_TOC16_DS
7086 || r_type == R_PPC64_TOC16_LO_DS)
7087 goto toctprel;
7088 else
7089 tls_get_addr_type = tls_type;
7090 }
7091 }
7092 break;
7093
7094 case R_PPC64_GOT_TPREL16_DS:
7095 case R_PPC64_GOT_TPREL16_LO_DS:
7096 tls_get_addr_type = 0;
7097 toctprel:
7098 if (tls_type != 0
7099 && (tls_type & TLS_TPREL) == 0)
7100 {
7101 bfd_vma insn;
7102 insn = bfd_get_32 (output_bfd, contents + rel->r_offset - 2);
7103 insn &= 31 << 21;
7104 insn |= 0x3c0d0000; /* addis 0,13,0 */
7105 bfd_put_32 (output_bfd, insn, contents + rel->r_offset - 2);
7106 r_type = R_PPC64_TPREL16_HA;
7107 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
7108 }
7109 break;
7110
7111 case R_PPC64_TLS:
7112 tls_get_addr_type = 0;
7113 if (tls_type == 0)
7114 {
7115 /* Check for toc tls entries. */
7116 char *toc_tls;
7117
7118 if (!get_tls_type (&toc_tls, &local_syms, rel, input_bfd))
7119 return FALSE;
7120
7121 if (toc_tls)
7122 tls_type = *toc_tls;
7123 }
7124 if (tls_type != 0
7125 && (tls_type & TLS_TPREL) == 0)
7126 {
7127 bfd_vma insn, rtra;
7128 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
7129 if ((insn & ((31 << 26) | (31 << 11)))
7130 == ((31 << 26) | (13 << 11)))
7131 rtra = insn & ((1 << 26) - (1 << 16));
7132 else if ((insn & ((31 << 26) | (31 << 16)))
7133 == ((31 << 26) | (13 << 16)))
7134 rtra = (insn & (31 << 21)) | ((insn & (31 << 11)) << 5);
7135 else
7136 abort ();
7137 if ((insn & ((1 << 11) - (1 << 1))) == 266 << 1)
7138 /* add -> addi. */
7139 insn = 14 << 26;
7140 else if ((insn & (31 << 1)) == 23 << 1
7141 && ((insn & (31 << 6)) < 14 << 6
7142 || ((insn & (31 << 6)) >= 16 << 6
7143 && (insn & (31 << 6)) < 24 << 6)))
7144 /* load and store indexed -> dform. */
7145 insn = (32 | ((insn >> 6) & 31)) << 26;
7146 else if ((insn & (31 << 1)) == 21 << 1
7147 && (insn & (0x1a << 6)) == 0)
7148 /* ldx, ldux, stdx, stdux -> ld, ldu, std, stdu. */
7149 insn = (((58 | ((insn >> 6) & 4)) << 26)
7150 | ((insn >> 6) & 1));
7151 else if ((insn & (31 << 1)) == 21 << 1
7152 && (insn & ((1 << 11) - (1 << 1))) == 341 << 1)
7153 /* lwax -> lwa. */
7154 insn = (58 << 26) | 2;
7155 else
7156 abort ();
7157 insn |= rtra;
7158 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
7159 r_type = R_PPC64_TPREL16_LO;
7160 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
7161 /* Was PPC64_TLS which sits on insn boundary, now
7162 PPC64_TPREL16_LO which is at insn+2. */
7163 rel->r_offset += 2;
7164 }
7165 break;
7166
7167 case R_PPC64_GOT_TLSGD16:
7168 case R_PPC64_GOT_TLSGD16_LO:
7169 case R_PPC64_GOT_TLSGD16_HI:
7170 case R_PPC64_GOT_TLSGD16_HA:
7171 case R_PPC64_GOT_TLSLD16:
7172 case R_PPC64_GOT_TLSLD16_LO:
7173 case R_PPC64_GOT_TLSLD16_HI:
7174 case R_PPC64_GOT_TLSLD16_HA:
7175 tls_get_addr_type = 0;
7176 if (tls_type != 0 && (tls_type & TLS_GD_LD) == 0)
7177 {
7178 if (r_type == R_PPC64_GOT_TLSGD16_HI
7179 || r_type == R_PPC64_GOT_TLSGD16_HA
7180 || r_type == R_PPC64_GOT_TLSLD16_HI
7181 || r_type == R_PPC64_GOT_TLSLD16_HA)
7182 {
7183 if ((tls_type & TLS_LD) != 0)
7184 {
7185 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
7186 r_type = R_PPC64_NONE;
7187 rel->r_offset -= 2;
7188 }
7189 else
7190 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
7191 + R_PPC64_GOT_TPREL16_DS);
7192 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
7193 break;
7194 }
7195
7196 /* This reloc and its instruction will be replaced so there's
7197 not much point in doing anything more here. We know the
7198 next reloc will be a REL24 to __tls_get_addr, and will
7199 be handled below. */
7200 tls_get_addr_type = tls_type;
7201 continue;
7202 }
7203 break;
7204
7205 case R_PPC64_REL14:
7206 case R_PPC64_REL14_BRTAKEN:
7207 case R_PPC64_REL14_BRNTAKEN:
7208 case R_PPC64_REL24:
7209 if (h != NULL
7210 && h == htab->tls_get_addr
7211 && tls_get_addr_type != 0
7212 && (tls_get_addr_type & TLS_GD_LD) == 0)
7213 {
7214 /* Replace the call. */
7215 bfd_vma insn1, insn2, insn3;
7216 bfd_vma offset = rel->r_offset;
7217
7218 insn1 = bfd_get_32 (output_bfd, contents + rel[-1].r_offset - 2);
7219 insn3 = bfd_get_32 (output_bfd, contents + offset + 4);
7220 if (tls_get_addr_type & TLS_TPREL)
7221 {
7222 /* IE */
7223 insn1 &= (1 << 26) - (1 << 2);
7224 insn1 |= 58 << 26; /* ld */
7225 insn2 = 0x7c636a14; /* add 3,3,13 */
7226 /* Fix the relocs for --emit-relocs. */
7227 r_type = ELF64_R_TYPE (rel[-1].r_info);
7228 if (tls_get_addr_type & TLS_EXPLICIT)
7229 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
7230 else
7231 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
7232 + R_PPC64_GOT_TPREL16_DS);
7233 rel[-1].r_info
7234 = ELF64_R_INFO (ELF64_R_SYM (rel[-1].r_info), r_type);
7235 rel->r_info = 0;
7236 }
7237 else
7238 {
7239 /* LE */
7240 insn1 = 0x3c6d0000; /* addis 3,13,0 */
7241 insn2 = 0x38630000; /* addi 3,3,0 */
7242 if (tls_get_addr_type & TLS_LD)
7243 {
7244 r_symndx = 0;
7245 rel[-1].r_addend = htab->tls_sec->vma + DTP_OFFSET;
7246 rel->r_addend = htab->tls_sec->vma + DTP_OFFSET;
7247 }
7248 else
7249 r_symndx = ELF64_R_SYM (rel[-1].r_info);
7250 rel[-1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_TPREL16_HA);
7251 rel->r_info = ELF64_R_INFO (r_symndx, R_PPC64_TPREL16_LO);
7252 rel->r_offset += 2;
7253 }
7254 if (insn3 == NOP
7255 || insn3 == CROR_151515 || insn3 == CROR_313131)
7256 {
7257 insn3 = insn2;
7258 insn2 = NOP;
7259 rel->r_offset += 4;
7260 }
7261 bfd_put_32 (output_bfd, insn1, contents + rel[-1].r_offset - 2);
7262 bfd_put_32 (output_bfd, insn2, contents + offset);
7263 bfd_put_32 (output_bfd, insn3, contents + offset + 4);
7264 /* Do the relocs again. */
7265 rel -= 2;
7266 tls_get_addr_type = 0;
7267 continue;
7268 }
7269 tls_get_addr_type = 0;
7270 break;
7271
7272 case R_PPC64_DTPMOD64:
7273 if ((tls_type & TLS_GD_LD) == 0)
7274 {
7275 if ((tls_type & TLS_TPREL) != 0)
7276 r_type = R_PPC64_TPREL64;
7277 else
7278 {
7279 bfd_put_64 (output_bfd, (bfd_vma) 1,
7280 contents + rel->r_offset);
7281 r_type = R_PPC64_NONE;
7282 }
7283 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
7284 if ((tls_type & TLS_LD) == 0)
7285 rel[1].r_info = 0;
7286 }
7287 break;
7288
7289 case R_PPC64_TPREL64:
7290 if ((tls_type & TLS_TPREL) == 0)
7291 {
7292 r_type = R_PPC64_NONE;
7293 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
7294 }
7295 break;
7296 }
7297
7298 /* Handle other relocations that tweak non-addend part of insn. */
86c76c7b 7299 insn = 0;
65f38f15 7300 switch (r_type)
5bd4f169
AM
7301 {
7302 default:
65f38f15 7303 break;
5bd4f169 7304
65f38f15
AM
7305 /* Branch taken prediction relocations. */
7306 case R_PPC64_ADDR14_BRTAKEN:
7307 case R_PPC64_REL14_BRTAKEN:
cedb70c5
KH
7308 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
7309 /* Fall thru. */
65f38f15 7310
86c76c7b 7311 /* Branch not taken prediction relocations. */
65f38f15
AM
7312 case R_PPC64_ADDR14_BRNTAKEN:
7313 case R_PPC64_REL14_BRNTAKEN:
411e1bfb
AM
7314 insn |= bfd_get_32 (output_bfd,
7315 contents + rel->r_offset) & ~(0x01 << 21);
96e0dda4
AM
7316 if (is_power4)
7317 {
7318 /* Set 'a' bit. This is 0b00010 in BO field for branch
7319 on CR(BI) insns (BO == 001at or 011at), and 0b01000
7320 for branch on CTR insns (BO == 1a00t or 1a01t). */
7321 if ((insn & (0x14 << 21)) == (0x04 << 21))
7322 insn |= 0x02 << 21;
7323 else if ((insn & (0x14 << 21)) == (0x10 << 21))
7324 insn |= 0x08 << 21;
7325 else
7326 break;
7327 }
65f38f15 7328 else
96e0dda4 7329 {
411e1bfb 7330 from = (rel->r_offset
805fc799
AM
7331 + input_section->output_offset
7332 + input_section->output_section->vma);
7333
96e0dda4 7334 /* Invert 'y' bit if not the default. */
411e1bfb 7335 if ((bfd_signed_vma) (relocation + rel->r_addend - from) < 0)
96e0dda4
AM
7336 insn ^= 0x01 << 21;
7337 }
86c76c7b 7338
411e1bfb 7339 bfd_put_32 (output_bfd, (bfd_vma) insn, contents + rel->r_offset);
65f38f15 7340 break;
5bd4f169 7341
65f38f15 7342 case R_PPC64_REL24:
721956f4
AM
7343 /* A REL24 branching to a linkage function is followed by a
7344 nop. We replace the nop with a ld in order to restore
7345 the TOC base pointer. Only calls to shared objects need
7346 to alter the TOC base. These are recognized by their
7347 need for a PLT entry. */
5bd4f169 7348 if (h != NULL
721956f4 7349 && (fdh = ((struct ppc_link_hash_entry *) h)->oh) != NULL
411e1bfb 7350 && fdh->plt.plist != NULL
721956f4
AM
7351 && (stub_entry = ppc_get_stub_entry (input_section, sec, fdh,
7352 rel, htab)) != NULL)
41bd81ab 7353 {
b34976b6 7354 bfd_boolean can_plt_call = 0;
721956f4 7355
411e1bfb 7356 if (rel->r_offset + 8 <= input_section->_cooked_size)
41bd81ab 7357 {
411e1bfb 7358 insn = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
721956f4
AM
7359 if (insn == NOP
7360 || insn == CROR_151515 || insn == CROR_313131)
41bd81ab 7361 {
721956f4 7362 bfd_put_32 (input_bfd, (bfd_vma) LD_R2_40R1,
411e1bfb 7363 contents + rel->r_offset + 4);
721956f4 7364 can_plt_call = 1;
41bd81ab 7365 }
5bd4f169 7366 }
721956f4
AM
7367
7368 if (!can_plt_call)
7369 {
7370 /* If this is a plain branch rather than a branch
7371 and link, don't require a nop. */
411e1bfb 7372 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
721956f4
AM
7373 if ((insn & 1) == 0)
7374 can_plt_call = 1;
7375 }
7376
7377 if (can_plt_call)
7378 {
7379 relocation = (stub_entry->stub_offset
7380 + stub_entry->stub_sec->output_offset
7381 + stub_entry->stub_sec->output_section->vma);
b34976b6 7382 unresolved_reloc = FALSE;
721956f4 7383 }
5bd4f169 7384 }
e86ce104
AM
7385
7386 if (h != NULL
7387 && h->root.type == bfd_link_hash_undefweak
721956f4 7388 && relocation == 0
411e1bfb 7389 && rel->r_addend == 0)
e86ce104 7390 {
721956f4
AM
7391 /* Tweak calls to undefined weak functions to point at a
7392 blr. We can thus call a weak function without first
7393 checking whether the function is defined. We have a
7394 blr at the end of .sfpr. */
805fc799 7395 BFD_ASSERT (htab->sfpr->_raw_size != 0);
721956f4
AM
7396 relocation = (htab->sfpr->_raw_size - 4
7397 + htab->sfpr->output_offset
7398 + htab->sfpr->output_section->vma);
411e1bfb 7399 from = (rel->r_offset
721956f4
AM
7400 + input_section->output_offset
7401 + input_section->output_section->vma);
805fc799 7402
721956f4
AM
7403 /* But let's not be silly about it. If the blr isn't in
7404 reach, just go to the next instruction. */
805fc799
AM
7405 if (relocation - from + (1 << 25) >= (1 << 26)
7406 || htab->sfpr->_raw_size == 0)
721956f4 7407 relocation = from + 4;
e86ce104 7408 }
65f38f15
AM
7409 break;
7410 }
5bd4f169 7411
65f38f15 7412 /* Set `addend'. */
411e1bfb
AM
7413 tls_type = 0;
7414 addend = rel->r_addend;
65f38f15
AM
7415 switch (r_type)
7416 {
7417 default:
7418 (*_bfd_error_handler)
7419 (_("%s: unknown relocation type %d for symbol %s"),
7420 bfd_archive_filename (input_bfd), (int) r_type, sym_name);
5bd4f169 7421
65f38f15 7422 bfd_set_error (bfd_error_bad_value);
b34976b6 7423 ret = FALSE;
65f38f15 7424 continue;
5bd4f169 7425
65f38f15 7426 case R_PPC64_NONE:
411e1bfb 7427 case R_PPC64_TLS:
04c9666a
AM
7428 case R_PPC64_GNU_VTINHERIT:
7429 case R_PPC64_GNU_VTENTRY:
65f38f15 7430 continue;
5bd4f169
AM
7431
7432 /* GOT16 relocations. Like an ADDR16 using the symbol's
7433 address in the GOT as relocation value instead of the
411e1bfb 7434 symbol's value itself. Also, create a GOT entry for the
5bd4f169 7435 symbol and put the symbol value there. */
411e1bfb
AM
7436 case R_PPC64_GOT_TLSGD16:
7437 case R_PPC64_GOT_TLSGD16_LO:
7438 case R_PPC64_GOT_TLSGD16_HI:
7439 case R_PPC64_GOT_TLSGD16_HA:
7440 tls_type = TLS_TLS | TLS_GD_LD;
7441 goto dogot;
7442
7443 case R_PPC64_GOT_TLSLD16:
7444 case R_PPC64_GOT_TLSLD16_LO:
7445 case R_PPC64_GOT_TLSLD16_HI:
7446 case R_PPC64_GOT_TLSLD16_HA:
7447 tls_type = TLS_TLS | TLS_GD_LD | TLS_LD;
7448 goto dogot;
7449
7450 case R_PPC64_GOT_TPREL16_DS:
7451 case R_PPC64_GOT_TPREL16_LO_DS:
7452 case R_PPC64_GOT_TPREL16_HI:
7453 case R_PPC64_GOT_TPREL16_HA:
7454 tls_type = TLS_TLS | TLS_TPREL;
7455 goto dogot;
7456
7457 case R_PPC64_GOT_DTPREL16_DS:
7458 case R_PPC64_GOT_DTPREL16_LO_DS:
7459 case R_PPC64_GOT_DTPREL16_HI:
7460 case R_PPC64_GOT_DTPREL16_HA:
7461 tls_type = TLS_TLS | TLS_DTPREL;
7462 goto dogot;
7463
65f38f15
AM
7464 case R_PPC64_GOT16:
7465 case R_PPC64_GOT16_LO:
7466 case R_PPC64_GOT16_HI:
7467 case R_PPC64_GOT16_HA:
7468 case R_PPC64_GOT16_DS:
7469 case R_PPC64_GOT16_LO_DS:
411e1bfb 7470 dogot:
5bd4f169
AM
7471 {
7472 /* Relocation is to the entry for this symbol in the global
7473 offset table. */
411e1bfb 7474 struct got_entry *ent;
5bd4f169 7475 bfd_vma off;
411e1bfb 7476 unsigned long indx;
65f38f15
AM
7477
7478 if (htab->sgot == NULL)
7479 abort ();
5bd4f169
AM
7480
7481 if (h != NULL)
411e1bfb
AM
7482 ent = h->got.glist;
7483 else
5bd4f169 7484 {
411e1bfb
AM
7485 if (local_got_ents == NULL)
7486 abort ();
7487 ent = local_got_ents[r_symndx];
7488 }
7489
7490 for (; ent != NULL; ent = ent->next)
7491 if (ent->addend == rel->r_addend
7492 && ent->tls_type == tls_type)
7493 break;
7494 if (ent == NULL)
7495 abort ();
5bd4f169 7496
411e1bfb
AM
7497 off = ent->got.offset;
7498 indx = 0;
7499 if (h != NULL)
7500 {
7501 bfd_boolean dyn = htab->elf.dynamic_sections_created;
65f38f15 7502 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info, h)
5bd4f169 7503 || (info->shared
65f38f15
AM
7504 && (info->symbolic
7505 || h->dynindx == -1
7506 || (h->elf_link_hash_flags
7507 & ELF_LINK_FORCED_LOCAL))
5bd4f169
AM
7508 && (h->elf_link_hash_flags
7509 & ELF_LINK_HASH_DEF_REGULAR)))
411e1bfb
AM
7510 /* This is actually a static link, or it is a
7511 -Bsymbolic link and the symbol is defined
7512 locally, or the symbol was forced to be local
7513 because of a version file. */
7514 ;
7515 else
5bd4f169 7516 {
411e1bfb
AM
7517 indx = h->dynindx;
7518 unresolved_reloc = FALSE;
5bd4f169
AM
7519 }
7520 }
411e1bfb
AM
7521
7522 /* The offset must always be a multiple of 8. We use the
7523 least significant bit to record whether we have already
7524 processed this entry. */
7525 if ((off & 1) != 0)
7526 off &= ~1;
5bd4f169
AM
7527 else
7528 {
411e1bfb
AM
7529 /* Generate relocs for the dynamic linker, except in
7530 the case of TLSLD where we'll use one entry per
7531 module. */
7532 if ((info->shared || indx != 0)
7533 && ent->tls_type != (TLS_TLS | TLS_GD_LD | TLS_LD))
5bd4f169 7534 {
411e1bfb
AM
7535 outrel.r_offset = (htab->sgot->output_section->vma
7536 + htab->sgot->output_offset
7537 + off);
7538 if (ent->tls_type == (TLS_TLS | TLS_GD_LD))
5bd4f169 7539 {
411e1bfb
AM
7540 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
7541 outrel.r_addend = 0;
947216bf
AM
7542 loc = htab->srelgot->contents;
7543 loc += (htab->srelgot->reloc_count++
7544 * sizeof (Elf64_External_Rela));
65f38f15 7545 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
411e1bfb
AM
7546 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
7547 outrel.r_offset += 8;
7548 }
7549 else if (ent->tls_type == (TLS_TLS | TLS_DTPREL))
7550 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
7551 else if (ent->tls_type == (TLS_TLS | TLS_TPREL))
7552 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
7553 else if (indx == 0)
7554 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_RELATIVE);
7555 else
7556 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
7557 outrel.r_addend = ent->addend;
7558 if (indx == 0)
7559 outrel.r_addend += relocation;
7560 loc = htab->srelgot->contents;
7561 loc += (htab->srelgot->reloc_count++
7562 * sizeof (Elf64_External_Rela));
7563 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
7564 }
7565
7566 /* Init the .got section contents if we're not
7567 emitting a reloc. */
7568 if (!(info->shared || indx != 0))
7569 {
7570
7571 relocation += ent->addend;
7572 if ((tls_type & TLS_TLS) != 0)
7573 {
7574 relocation -= htab->tls_sec->vma + DTP_OFFSET;
7575 if ((tls_type & TLS_TPREL) != 0)
7576 relocation += DTP_OFFSET - TP_OFFSET;
5bd4f169
AM
7577 }
7578
411e1bfb
AM
7579 if ((tls_type & TLS_GD_LD) != 0)
7580 {
7581 if ((tls_type & TLS_LD) != 0)
7582 relocation = - DTP_OFFSET;
7583 bfd_put_64 (output_bfd, relocation,
7584 htab->sgot->contents + off + 8);
7585 relocation = 1;
7586 }
7587 bfd_put_64 (output_bfd, relocation,
7588 htab->sgot->contents + off);
5bd4f169 7589 }
411e1bfb 7590 ent->got.offset |= 1;
5bd4f169
AM
7591 }
7592
65f38f15
AM
7593 if (off >= (bfd_vma) -2)
7594 abort ();
7595
7596 relocation = htab->sgot->output_offset + off;
7597
5bd4f169 7598 /* TOC base (r2) is TOC start plus 0x8000. */
411e1bfb 7599 addend = - TOC_BASE_OFF;
5bd4f169 7600 }
65f38f15
AM
7601 break;
7602
7603 case R_PPC64_PLT16_HA:
7604 case R_PPC64_PLT16_HI:
7605 case R_PPC64_PLT16_LO:
7606 case R_PPC64_PLT32:
7607 case R_PPC64_PLT64:
7608 /* Relocation is to the entry for this symbol in the
7609 procedure linkage table. */
7610
7611 /* Resolve a PLT reloc against a local symbol directly,
7612 without using the procedure linkage table. */
7613 if (h == NULL)
7614 break;
7615
411e1bfb
AM
7616 /* It's possible that we didn't make a PLT entry for this
7617 symbol. This happens when statically linking PIC code,
7618 or when using -Bsymbolic. Go find a match if there is a
7619 PLT entry. */
7620 if (htab->splt != NULL)
65f38f15 7621 {
411e1bfb
AM
7622 struct plt_entry *ent;
7623 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
7624 if (ent->addend == rel->r_addend
7625 && ent->plt.offset != (bfd_vma) -1)
7626 {
7627 relocation = (htab->splt->output_section->vma
7628 + htab->splt->output_offset
7629 + ent->plt.offset);
7630 unresolved_reloc = FALSE;
7631 }
65f38f15 7632 }
65f38f15 7633 break;
5bd4f169
AM
7634
7635 /* TOC16 relocs. We want the offset relative to the TOC base,
7636 which is the address of the start of the TOC plus 0x8000.
7637 The TOC consists of sections .got, .toc, .tocbss, and .plt,
7638 in this order. */
65f38f15
AM
7639 case R_PPC64_TOC16:
7640 case R_PPC64_TOC16_LO:
7641 case R_PPC64_TOC16_HI:
7642 case R_PPC64_TOC16_DS:
7643 case R_PPC64_TOC16_LO_DS:
7644 case R_PPC64_TOC16_HA:
5bd4f169 7645 addend -= TOCstart + TOC_BASE_OFF;
5bd4f169
AM
7646 break;
7647
7648 /* Relocate against the beginning of the section. */
65f38f15
AM
7649 case R_PPC64_SECTOFF:
7650 case R_PPC64_SECTOFF_LO:
7651 case R_PPC64_SECTOFF_HI:
7652 case R_PPC64_SECTOFF_DS:
7653 case R_PPC64_SECTOFF_LO_DS:
7654 case R_PPC64_SECTOFF_HA:
7655 if (sec != (asection *) 0)
7656 addend -= sec->output_section->vma;
5bd4f169
AM
7657 break;
7658
721956f4
AM
7659 case R_PPC64_REL14:
7660 case R_PPC64_REL14_BRNTAKEN:
7661 case R_PPC64_REL14_BRTAKEN:
5d1634d7
AM
7662 case R_PPC64_REL24:
7663 break;
7664
411e1bfb
AM
7665 case R_PPC64_TPREL16:
7666 case R_PPC64_TPREL16_LO:
7667 case R_PPC64_TPREL16_HI:
7668 case R_PPC64_TPREL16_HA:
7669 case R_PPC64_TPREL16_DS:
7670 case R_PPC64_TPREL16_LO_DS:
7671 case R_PPC64_TPREL16_HIGHER:
7672 case R_PPC64_TPREL16_HIGHERA:
7673 case R_PPC64_TPREL16_HIGHEST:
7674 case R_PPC64_TPREL16_HIGHESTA:
7675 addend -= htab->tls_sec->vma + TP_OFFSET;
7676 if (info->shared)
7677 /* The TPREL16 relocs shouldn't really be used in shared
7678 libs as they will result in DT_TEXTREL being set, but
7679 support them anyway. */
7680 goto dodyn;
7681 break;
7682
7683 case R_PPC64_DTPREL16:
7684 case R_PPC64_DTPREL16_LO:
7685 case R_PPC64_DTPREL16_HI:
7686 case R_PPC64_DTPREL16_HA:
7687 case R_PPC64_DTPREL16_DS:
7688 case R_PPC64_DTPREL16_LO_DS:
7689 case R_PPC64_DTPREL16_HIGHER:
7690 case R_PPC64_DTPREL16_HIGHERA:
7691 case R_PPC64_DTPREL16_HIGHEST:
7692 case R_PPC64_DTPREL16_HIGHESTA:
7693 addend -= htab->tls_sec->vma + DTP_OFFSET;
7694 break;
7695
7696 case R_PPC64_TPREL64:
7697 addend -= htab->tls_sec->vma + TP_OFFSET;
7698 goto dodyn;
7699
7700 case R_PPC64_DTPREL64:
7701 addend -= htab->tls_sec->vma + DTP_OFFSET;
7702 /* Fall thru */
7703
65f38f15
AM
7704 /* Relocations that may need to be propagated if this is a
7705 dynamic object. */
411e1bfb 7706 case R_PPC64_DTPMOD64:
04c9666a 7707 case R_PPC64_REL30:
65f38f15
AM
7708 case R_PPC64_REL32:
7709 case R_PPC64_REL64:
7710 case R_PPC64_ADDR14:
7711 case R_PPC64_ADDR14_BRNTAKEN:
7712 case R_PPC64_ADDR14_BRTAKEN:
7713 case R_PPC64_ADDR16:
7714 case R_PPC64_ADDR16_DS:
7715 case R_PPC64_ADDR16_HA:
7716 case R_PPC64_ADDR16_HI:
7717 case R_PPC64_ADDR16_HIGHER:
7718 case R_PPC64_ADDR16_HIGHERA:
7719 case R_PPC64_ADDR16_HIGHEST:
7720 case R_PPC64_ADDR16_HIGHESTA:
7721 case R_PPC64_ADDR16_LO:
7722 case R_PPC64_ADDR16_LO_DS:
7723 case R_PPC64_ADDR24:
65f38f15
AM
7724 case R_PPC64_ADDR32:
7725 case R_PPC64_ADDR64:
7726 case R_PPC64_UADDR16:
7727 case R_PPC64_UADDR32:
7728 case R_PPC64_UADDR64:
ec338859
AM
7729 /* r_symndx will be zero only for relocs against symbols
7730 from removed linkonce sections, or sections discarded by
7731 a linker script. */
411e1bfb 7732 dodyn:
5d1634d7
AM
7733 if (r_symndx == 0)
7734 break;
7735 /* Fall thru. */
7736
7737 case R_PPC64_TOC:
7738 if ((input_section->flags & SEC_ALLOC) == 0)
ec338859
AM
7739 break;
7740
41bd81ab
AM
7741 if (NO_OPD_RELOCS && is_opd)
7742 break;
7743
65f38f15 7744 if ((info->shared
411e1bfb 7745 && (MUST_BE_DYN_RELOC (r_type)
65f38f15
AM
7746 || (h != NULL
7747 && h->dynindx != -1
7748 && (! info->symbolic
7749 || (h->elf_link_hash_flags
7750 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
7751 || (!info->shared
65f38f15
AM
7752 && h != NULL
7753 && h->dynindx != -1
7754 && (h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
7755 && (((h->elf_link_hash_flags
7756 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
7757 && (h->elf_link_hash_flags
7758 & ELF_LINK_HASH_DEF_REGULAR) == 0)
7759 || h->root.type == bfd_link_hash_undefweak
7760 || h->root.type == bfd_link_hash_undefined)))
7761 {
7762 Elf_Internal_Rela outrel;
b34976b6 7763 bfd_boolean skip, relocate;
65f38f15 7764 asection *sreloc;
947216bf 7765 bfd_byte *loc;
65f38f15
AM
7766
7767 /* When generating a dynamic object, these relocations
7768 are copied into the output file to be resolved at run
7769 time. */
7770
b34976b6
AM
7771 skip = FALSE;
7772 relocate = FALSE;
65f38f15 7773
c629eae0
JJ
7774 outrel.r_offset =
7775 _bfd_elf_section_offset (output_bfd, info, input_section,
7776 rel->r_offset);
7777 if (outrel.r_offset == (bfd_vma) -1)
b34976b6 7778 skip = TRUE;
0bb2d96a 7779 else if (outrel.r_offset == (bfd_vma) -2)
b34976b6 7780 skip = TRUE, relocate = TRUE;
65f38f15
AM
7781 outrel.r_offset += (input_section->output_section->vma
7782 + input_section->output_offset);
411e1bfb 7783 outrel.r_addend = rel->r_addend;
65f38f15
AM
7784
7785 if (skip)
0bb2d96a 7786 memset (&outrel, 0, sizeof outrel);
65f38f15
AM
7787 else if (h != NULL
7788 && h->dynindx != -1
41bd81ab 7789 && !is_opd
411e1bfb 7790 && (!MUST_BE_DYN_RELOC (r_type)
65f38f15
AM
7791 || !info->shared
7792 || !info->symbolic
7793 || (h->elf_link_hash_flags
7794 & ELF_LINK_HASH_DEF_REGULAR) == 0))
0bb2d96a 7795 outrel.r_info = ELF64_R_INFO (h->dynindx, r_type);
65f38f15
AM
7796 else
7797 {
41bd81ab
AM
7798 /* This symbol is local, or marked to become local,
7799 or this is an opd section reloc which must point
7800 at a local function. */
65f38f15 7801 outrel.r_addend += relocation;
411e1bfb 7802 /* ??? why? */
b34976b6 7803 relocate = TRUE;
e86ce104 7804 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
65f38f15 7805 {
3fad3c7c 7806 if (is_opd && h != NULL)
afbe61cf
AM
7807 {
7808 /* Lie about opd entries. This case occurs
7809 when building shared libraries and we
7810 reference a function in another shared
3fad3c7c
AM
7811 lib. The same thing happens for a weak
7812 definition in an application that's
7813 overridden by a strong definition in a
7814 shared lib. (I believe this is a generic
7815 bug in binutils handling of weak syms.)
7816 In these cases we won't use the opd
1e2f5b6e 7817 entry in this lib. */
b34976b6 7818 unresolved_reloc = FALSE;
afbe61cf 7819 }
65f38f15
AM
7820 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
7821 }
7822 else
7823 {
7824 long indx = 0;
7825
41bd81ab 7826 if (bfd_is_abs_section (sec))
65f38f15
AM
7827 ;
7828 else if (sec == NULL || sec->owner == NULL)
7829 {
7830 bfd_set_error (bfd_error_bad_value);
b34976b6 7831 return FALSE;
65f38f15
AM
7832 }
7833 else
7834 {
7835 asection *osec;
7836
7837 osec = sec->output_section;
7838 indx = elf_section_data (osec)->dynindx;
7839
7840 /* We are turning this relocation into one
7841 against a section symbol, so subtract out
7842 the output section's address but not the
7843 offset of the input section in the output
7844 section. */
7845 outrel.r_addend -= osec->vma;
7846 }
7847
7848 outrel.r_info = ELF64_R_INFO (indx, r_type);
7849 }
7850 }
7851
7852 sreloc = elf_section_data (input_section)->sreloc;
7853 if (sreloc == NULL)
7854 abort ();
7855
947216bf
AM
7856 loc = sreloc->contents;
7857 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15
AM
7858 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
7859
7860 /* If this reloc is against an external symbol, it will
7861 be computed at runtime, so there's no need to do
7862 anything now. */
7863 if (! relocate)
7864 continue;
7865 }
5bd4f169
AM
7866 break;
7867
65f38f15
AM
7868 case R_PPC64_COPY:
7869 case R_PPC64_GLOB_DAT:
7870 case R_PPC64_JMP_SLOT:
7871 case R_PPC64_RELATIVE:
7872 /* We shouldn't ever see these dynamic relocs in relocatable
7873 files. */
7874 /* Fall thru */
7875
7876 case R_PPC64_PLTGOT16:
7877 case R_PPC64_PLTGOT16_DS:
7878 case R_PPC64_PLTGOT16_HA:
7879 case R_PPC64_PLTGOT16_HI:
7880 case R_PPC64_PLTGOT16_LO:
7881 case R_PPC64_PLTGOT16_LO_DS:
7882 case R_PPC64_PLTREL32:
7883 case R_PPC64_PLTREL64:
7884 /* These ones haven't been implemented yet. */
7885
7886 (*_bfd_error_handler)
7887 (_("%s: Relocation %s is not supported for symbol %s."),
7888 bfd_archive_filename (input_bfd),
7889 ppc64_elf_howto_table[(int) r_type]->name, sym_name);
5bd4f169
AM
7890
7891 bfd_set_error (bfd_error_invalid_operation);
b34976b6 7892 ret = FALSE;
5bd4f169 7893 continue;
65f38f15 7894 }
5bd4f169 7895
65f38f15
AM
7896 /* Do any further special processing. */
7897 switch (r_type)
7898 {
7899 default:
7900 break;
7901
7902 case R_PPC64_ADDR16_HA:
7903 case R_PPC64_ADDR16_HIGHERA:
7904 case R_PPC64_ADDR16_HIGHESTA:
86bbe32f
AM
7905 case R_PPC64_GOT16_HA:
7906 case R_PPC64_PLTGOT16_HA:
65f38f15
AM
7907 case R_PPC64_PLT16_HA:
7908 case R_PPC64_TOC16_HA:
7909 case R_PPC64_SECTOFF_HA:
411e1bfb
AM
7910 case R_PPC64_TPREL16_HA:
7911 case R_PPC64_DTPREL16_HA:
7912 case R_PPC64_GOT_TLSGD16_HA:
7913 case R_PPC64_GOT_TLSLD16_HA:
7914 case R_PPC64_GOT_TPREL16_HA:
7915 case R_PPC64_GOT_DTPREL16_HA:
7916 case R_PPC64_TPREL16_HIGHER:
7917 case R_PPC64_TPREL16_HIGHERA:
7918 case R_PPC64_TPREL16_HIGHEST:
7919 case R_PPC64_TPREL16_HIGHESTA:
7920 case R_PPC64_DTPREL16_HIGHER:
7921 case R_PPC64_DTPREL16_HIGHERA:
7922 case R_PPC64_DTPREL16_HIGHEST:
7923 case R_PPC64_DTPREL16_HIGHESTA:
65f38f15
AM
7924 /* It's just possible that this symbol is a weak symbol
7925 that's not actually defined anywhere. In that case,
7926 'sec' would be NULL, and we should leave the symbol
7927 alone (it will be set to zero elsewhere in the link). */
7928 if (sec != NULL)
7929 /* Add 0x10000 if sign bit in 0:15 is set. */
7930 addend += ((relocation + addend) & 0x8000) << 1;
7931 break;
7932
7933 case R_PPC64_ADDR16_DS:
7934 case R_PPC64_ADDR16_LO_DS:
7935 case R_PPC64_GOT16_DS:
7936 case R_PPC64_GOT16_LO_DS:
7937 case R_PPC64_PLT16_LO_DS:
7938 case R_PPC64_SECTOFF_DS:
7939 case R_PPC64_SECTOFF_LO_DS:
7940 case R_PPC64_TOC16_DS:
7941 case R_PPC64_TOC16_LO_DS:
7942 case R_PPC64_PLTGOT16_DS:
7943 case R_PPC64_PLTGOT16_LO_DS:
411e1bfb
AM
7944 case R_PPC64_GOT_TPREL16_DS:
7945 case R_PPC64_GOT_TPREL16_LO_DS:
7946 case R_PPC64_GOT_DTPREL16_DS:
7947 case R_PPC64_GOT_DTPREL16_LO_DS:
7948 case R_PPC64_TPREL16_DS:
7949 case R_PPC64_TPREL16_LO_DS:
7950 case R_PPC64_DTPREL16_DS:
7951 case R_PPC64_DTPREL16_LO_DS:
65f38f15
AM
7952 if (((relocation + addend) & 3) != 0)
7953 {
7954 (*_bfd_error_handler)
7955 (_("%s: error: relocation %s not a multiple of 4"),
7956 bfd_archive_filename (input_bfd),
7957 ppc64_elf_howto_table[(int) r_type]->name);
7958 bfd_set_error (bfd_error_bad_value);
b34976b6 7959 ret = FALSE;
65f38f15
AM
7960 continue;
7961 }
7962 break;
721956f4
AM
7963
7964 case R_PPC64_REL14:
7965 case R_PPC64_REL14_BRNTAKEN:
7966 case R_PPC64_REL14_BRTAKEN:
7967 max_br_offset = 1 << 15;
7968 goto branch_check;
7969
7970 case R_PPC64_REL24:
7971 max_br_offset = 1 << 25;
7972
7973 branch_check:
7974 /* If the branch is out of reach, then redirect the
7975 call to the local stub for this function. */
411e1bfb 7976 from = (rel->r_offset
721956f4
AM
7977 + input_section->output_offset
7978 + input_section->output_section->vma);
7979 if (relocation + addend - from + max_br_offset >= 2 * max_br_offset
7980 && (stub_entry = ppc_get_stub_entry (input_section, sec, h,
7981 rel, htab)) != NULL)
7982 {
7983 /* Munge up the value and addend so that we call the stub
7984 rather than the procedure directly. */
7985 relocation = (stub_entry->stub_offset
7986 + stub_entry->stub_sec->output_offset
7987 + stub_entry->stub_sec->output_section->vma);
7988 addend = 0;
7989 }
7990 break;
5bd4f169
AM
7991 }
7992
239e1f3a
AM
7993 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
7994 because such sections are not SEC_ALLOC and thus ld.so will
7995 not process them. */
65f38f15 7996 if (unresolved_reloc
239e1f3a 7997 && !((input_section->flags & SEC_DEBUGGING) != 0
65f38f15 7998 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0))
9c07fe7c
AM
7999 {
8000 (*_bfd_error_handler)
8001 (_("%s(%s+0x%lx): unresolvable relocation against symbol `%s'"),
8002 bfd_archive_filename (input_bfd),
8003 bfd_get_section_name (input_bfd, input_section),
8004 (long) rel->r_offset,
8005 h->root.root.string);
b34976b6 8006 ret = FALSE;
9c07fe7c 8007 }
5bd4f169 8008
65f38f15 8009 r = _bfd_final_link_relocate (ppc64_elf_howto_table[(int) r_type],
5bd4f169
AM
8010 input_bfd,
8011 input_section,
8012 contents,
411e1bfb 8013 rel->r_offset,
5bd4f169
AM
8014 relocation,
8015 addend);
8016
ef60b7ff 8017 if (r != bfd_reloc_ok)
5bd4f169
AM
8018 {
8019 const char *name;
8020
8021 if (h != NULL)
8022 {
8023 if (h->root.type == bfd_link_hash_undefweak
65f38f15 8024 && ppc64_elf_howto_table[(int) r_type]->pc_relative)
5bd4f169
AM
8025 {
8026 /* Assume this is a call protected by other code that
8027 detects the symbol is undefined. If this is the case,
8028 we can safely ignore the overflow. If not, the
8029 program is hosed anyway, and a little warning isn't
8030 going to help. */
8031
8032 continue;
8033 }
8034
8035 name = h->root.root.string;
8036 }
8037 else
8038 {
411e1bfb 8039 name = bfd_elf_local_sym_name (input_bfd, sym);
5bd4f169
AM
8040 if (name == NULL)
8041 continue;
5bd4f169
AM
8042 }
8043
ef60b7ff
AM
8044 if (r == bfd_reloc_overflow)
8045 {
8046 if (warned)
8047 continue;
8048 if (!((*info->callbacks->reloc_overflow)
8049 (info, name, ppc64_elf_howto_table[(int) r_type]->name,
411e1bfb 8050 rel->r_addend, input_bfd, input_section, rel->r_offset)))
b34976b6 8051 return FALSE;
ef60b7ff
AM
8052 }
8053 else
8054 {
8055 (*_bfd_error_handler)
8056 (_("%s(%s+0x%lx): reloc against `%s': error %d"),
8057 bfd_archive_filename (input_bfd),
8058 bfd_get_section_name (input_bfd, input_section),
8059 (long) rel->r_offset, name, (int) r);
b34976b6 8060 ret = FALSE;
ef60b7ff 8061 }
5bd4f169 8062 }
5bd4f169
AM
8063 }
8064
5bd4f169
AM
8065 return ret;
8066}
8067
8068/* Finish up dynamic symbol handling. We set the contents of various
8069 dynamic sections here. */
8070
b34976b6 8071static bfd_boolean
5bd4f169
AM
8072ppc64_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
8073 bfd *output_bfd;
8074 struct bfd_link_info *info;
8075 struct elf_link_hash_entry *h;
8076 Elf_Internal_Sym *sym;
8077{
65f38f15 8078 struct ppc_link_hash_table *htab;
5bd4f169
AM
8079 bfd *dynobj;
8080
65f38f15
AM
8081 htab = ppc_hash_table (info);
8082 dynobj = htab->elf.dynobj;
5bd4f169 8083
411e1bfb 8084 if (((struct ppc_link_hash_entry *) h)->is_func_descriptor)
5bd4f169 8085 {
411e1bfb 8086 struct plt_entry *ent;
65f38f15 8087 Elf_Internal_Rela rela;
947216bf 8088 bfd_byte *loc;
5bd4f169 8089
411e1bfb
AM
8090 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
8091 if (ent->plt.offset != (bfd_vma) -1)
8092 {
8093 /* This symbol has an entry in the procedure linkage
8094 table. Set it up. */
5bd4f169 8095
411e1bfb
AM
8096 if (htab->splt == NULL
8097 || htab->srelplt == NULL
8098 || htab->sglink == NULL)
8099 abort ();
5bd4f169 8100
411e1bfb
AM
8101 /* Create a JMP_SLOT reloc to inform the dynamic linker to
8102 fill in the PLT entry. */
5bd4f169 8103
411e1bfb
AM
8104 rela.r_offset = (htab->splt->output_section->vma
8105 + htab->splt->output_offset
8106 + ent->plt.offset);
8107 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
8108 rela.r_addend = ent->addend;
5bd4f169 8109
411e1bfb
AM
8110 loc = htab->srelplt->contents;
8111 loc += ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE) / PLT_ENTRY_SIZE
8112 * sizeof (Elf64_External_Rela));
8113 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
8114 }
5bd4f169
AM
8115 }
8116
8117 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
8118 {
5bd4f169 8119 Elf_Internal_Rela rela;
947216bf 8120 bfd_byte *loc;
5bd4f169 8121
65f38f15 8122 /* This symbol needs a copy reloc. Set it up. */
5bd4f169 8123
65f38f15
AM
8124 if (h->dynindx == -1
8125 || (h->root.type != bfd_link_hash_defined
8126 && h->root.type != bfd_link_hash_defweak)
8127 || htab->srelbss == NULL)
8128 abort ();
5bd4f169
AM
8129
8130 rela.r_offset = (h->root.u.def.value
8131 + h->root.u.def.section->output_section->vma
8132 + h->root.u.def.section->output_offset);
8133 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
8134 rela.r_addend = 0;
947216bf
AM
8135 loc = htab->srelbss->contents;
8136 loc += htab->srelbss->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15 8137 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
5bd4f169
AM
8138 }
8139
5bd4f169
AM
8140 /* Mark some specially defined symbols as absolute. */
8141 if (strcmp (h->root.root.string, "_DYNAMIC") == 0)
8142 sym->st_shndx = SHN_ABS;
8143
b34976b6 8144 return TRUE;
5bd4f169
AM
8145}
8146
65f38f15
AM
8147/* Used to decide how to sort relocs in an optimal manner for the
8148 dynamic linker, before writing them out. */
8149
8150static enum elf_reloc_type_class
8151ppc64_elf_reloc_type_class (rela)
8152 const Elf_Internal_Rela *rela;
8153{
04c9666a 8154 enum elf_ppc64_reloc_type r_type;
a33d1f77 8155
04c9666a 8156 r_type = (enum elf_ppc64_reloc_type) ELF64_R_TYPE (rela->r_info);
a33d1f77 8157 switch (r_type)
65f38f15
AM
8158 {
8159 case R_PPC64_RELATIVE:
8160 return reloc_class_relative;
8161 case R_PPC64_JMP_SLOT:
8162 return reloc_class_plt;
8163 case R_PPC64_COPY:
8164 return reloc_class_copy;
8165 default:
8166 return reloc_class_normal;
8167 }
8168}
8169
5bd4f169
AM
8170/* Finish up the dynamic sections. */
8171
b34976b6 8172static bfd_boolean
5bd4f169
AM
8173ppc64_elf_finish_dynamic_sections (output_bfd, info)
8174 bfd *output_bfd;
8175 struct bfd_link_info *info;
8176{
65f38f15
AM
8177 struct ppc_link_hash_table *htab;
8178 bfd *dynobj;
5bd4f169 8179 asection *sdyn;
5bd4f169 8180
65f38f15
AM
8181 htab = ppc_hash_table (info);
8182 dynobj = htab->elf.dynobj;
5bd4f169
AM
8183 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
8184
65f38f15 8185 if (htab->elf.dynamic_sections_created)
5bd4f169 8186 {
5bd4f169
AM
8187 Elf64_External_Dyn *dyncon, *dynconend;
8188
65f38f15
AM
8189 if (sdyn == NULL || htab->sgot == NULL)
8190 abort ();
5bd4f169
AM
8191
8192 dyncon = (Elf64_External_Dyn *) sdyn->contents;
8193 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
8194 for (; dyncon < dynconend; dyncon++)
8195 {
8196 Elf_Internal_Dyn dyn;
19397422 8197 asection *s;
5bd4f169
AM
8198
8199 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
8200
8201 switch (dyn.d_tag)
8202 {
65f38f15
AM
8203 default:
8204 continue;
5bd4f169 8205
5d1634d7 8206 case DT_PPC64_GLINK:
6348e046
AM
8207 s = htab->sglink;
8208 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
5d1634d7
AM
8209 break;
8210
19397422
AM
8211 case DT_PPC64_OPD:
8212 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
8213 if (s == NULL)
8214 continue;
8215 dyn.d_un.d_ptr = s->vma;
19397422
AM
8216 break;
8217
8218 case DT_PPC64_OPDSZ:
8219 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
8220 if (s == NULL)
8221 continue;
8222 dyn.d_un.d_val = s->_raw_size;
19397422
AM
8223 break;
8224
65f38f15 8225 case DT_PLTGOT:
6348e046
AM
8226 s = htab->splt;
8227 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15
AM
8228 break;
8229
8230 case DT_JMPREL:
6348e046
AM
8231 s = htab->srelplt;
8232 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15 8233 break;
5bd4f169 8234
65f38f15 8235 case DT_PLTRELSZ:
5d1634d7
AM
8236 dyn.d_un.d_val = htab->srelplt->_raw_size;
8237 break;
8238
8239 case DT_RELASZ:
8240 /* Don't count procedure linkage table relocs in the
8241 overall reloc count. */
6348e046
AM
8242 s = htab->srelplt;
8243 if (s == NULL)
8244 continue;
8245 dyn.d_un.d_val -= s->_raw_size;
8246 break;
8247
8248 case DT_RELA:
8249 /* We may not be using the standard ELF linker script.
8250 If .rela.plt is the first .rela section, we adjust
8251 DT_RELA to not include it. */
8252 s = htab->srelplt;
8253 if (s == NULL)
8254 continue;
8255 if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
8256 continue;
8257 dyn.d_un.d_ptr += s->_raw_size;
65f38f15 8258 break;
5bd4f169 8259 }
5bd4f169 8260
65f38f15 8261 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
5bd4f169 8262 }
5bd4f169
AM
8263 }
8264
5d1634d7
AM
8265 if (htab->sgot != NULL && htab->sgot->_raw_size != 0)
8266 {
8267 /* Fill in the first entry in the global offset table.
8268 We use it to hold the link-time TOCbase. */
8269 bfd_put_64 (output_bfd,
60ee0d4a 8270 elf_gp (output_bfd) + TOC_BASE_OFF,
5d1634d7
AM
8271 htab->sgot->contents);
8272
8273 /* Set .got entry size. */
8274 elf_section_data (htab->sgot->output_section)->this_hdr.sh_entsize = 8;
8275 }
8276
8277 if (htab->splt != NULL && htab->splt->_raw_size != 0)
8278 {
8279 /* Set .plt entry size. */
8280 elf_section_data (htab->splt->output_section)->this_hdr.sh_entsize
8281 = PLT_ENTRY_SIZE;
8282 }
8283
b34976b6 8284 return TRUE;
5bd4f169
AM
8285}
8286
8287#define TARGET_LITTLE_SYM bfd_elf64_powerpcle_vec
8288#define TARGET_LITTLE_NAME "elf64-powerpcle"
8289#define TARGET_BIG_SYM bfd_elf64_powerpc_vec
8290#define TARGET_BIG_NAME "elf64-powerpc"
8291#define ELF_ARCH bfd_arch_powerpc
8292#define ELF_MACHINE_CODE EM_PPC64
8293#define ELF_MAXPAGESIZE 0x10000
8294#define elf_info_to_howto ppc64_elf_info_to_howto
8295
8296#ifdef EM_CYGNUS_POWERPC
8297#define ELF_MACHINE_ALT1 EM_CYGNUS_POWERPC
8298#endif
8299
8300#ifdef EM_PPC_OLD
8301#define ELF_MACHINE_ALT2 EM_PPC_OLD
8302#endif
8303
8304#define elf_backend_want_got_sym 0
8305#define elf_backend_want_plt_sym 0
8306#define elf_backend_plt_alignment 3
8307#define elf_backend_plt_not_loaded 1
8308#define elf_backend_got_symbol_offset 0
5d1634d7
AM
8309#define elf_backend_got_header_size 8
8310#define elf_backend_plt_header_size PLT_INITIAL_ENTRY_SIZE
5bd4f169 8311#define elf_backend_can_gc_sections 1
65f38f15 8312#define elf_backend_can_refcount 1
b491616a 8313#define elf_backend_rela_normal 1
5bd4f169 8314
65f38f15 8315#define bfd_elf64_bfd_reloc_type_lookup ppc64_elf_reloc_type_lookup
65f38f15 8316#define bfd_elf64_bfd_merge_private_bfd_data ppc64_elf_merge_private_bfd_data
f0abc2a1 8317#define bfd_elf64_new_section_hook ppc64_elf_new_section_hook
65f38f15 8318#define bfd_elf64_bfd_link_hash_table_create ppc64_elf_link_hash_table_create
721956f4 8319#define bfd_elf64_bfd_link_hash_table_free ppc64_elf_link_hash_table_free
65f38f15 8320
feee612b 8321#define elf_backend_object_p ppc64_elf_object_p
65f38f15
AM
8322#define elf_backend_create_dynamic_sections ppc64_elf_create_dynamic_sections
8323#define elf_backend_copy_indirect_symbol ppc64_elf_copy_indirect_symbol
8324#define elf_backend_check_relocs ppc64_elf_check_relocs
8325#define elf_backend_gc_mark_hook ppc64_elf_gc_mark_hook
8326#define elf_backend_gc_sweep_hook ppc64_elf_gc_sweep_hook
8327#define elf_backend_adjust_dynamic_symbol ppc64_elf_adjust_dynamic_symbol
e86ce104
AM
8328#define elf_backend_hide_symbol ppc64_elf_hide_symbol
8329#define elf_backend_always_size_sections ppc64_elf_func_desc_adjust
65f38f15 8330#define elf_backend_size_dynamic_sections ppc64_elf_size_dynamic_sections
65f38f15
AM
8331#define elf_backend_relocate_section ppc64_elf_relocate_section
8332#define elf_backend_finish_dynamic_symbol ppc64_elf_finish_dynamic_symbol
8333#define elf_backend_reloc_type_class ppc64_elf_reloc_type_class
8334#define elf_backend_finish_dynamic_sections ppc64_elf_finish_dynamic_sections
5bd4f169
AM
8335
8336#include "elf64-target.h"