daily update
[deliverable/binutils-gdb.git] / bfd / elf64-ppc.c
CommitLineData
5bd4f169 1/* PowerPC64-specific support for 64-bit ELF.
86bbe32f 2 Copyright 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
5bd4f169
AM
3 Written by Linus Nordberg, Swox AB <info@swox.com>,
4 based on elf32-ppc.c by Ian Lance Taylor.
5
6This file is part of BFD, the Binary File Descriptor library.
7
8This program is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 2 of the License, or
11(at your option) any later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with this program; if not, write to the Free Software
20Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21
22/* This file is based on the 64-bit PowerPC ELF ABI. It is also based
23 on the file elf32-ppc.c. */
24
25#include "bfd.h"
26#include "sysdep.h"
27#include "bfdlink.h"
28#include "libbfd.h"
29#include "elf-bfd.h"
04c9666a 30#include "elf/ppc64.h"
5d1634d7 31#include "elf64-ppc.h"
5bd4f169 32
5bd4f169
AM
33static void ppc_howto_init
34 PARAMS ((void));
35static reloc_howto_type *ppc64_elf_reloc_type_lookup
36 PARAMS ((bfd *abfd, bfd_reloc_code_real_type code));
37static void ppc64_elf_info_to_howto
947216bf 38 PARAMS ((bfd *abfd, arelent *cache_ptr, Elf_Internal_Rela *dst));
805fc799
AM
39static bfd_reloc_status_type ppc64_elf_ha_reloc
40 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
41static bfd_reloc_status_type ppc64_elf_brtaken_reloc
42 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
43static bfd_reloc_status_type ppc64_elf_sectoff_reloc
44 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
45static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
46 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
47static bfd_reloc_status_type ppc64_elf_toc_reloc
48 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
49static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
50 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
51static bfd_reloc_status_type ppc64_elf_toc64_reloc
52 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
53static bfd_reloc_status_type ppc64_elf_unhandled_reloc
5bd4f169 54 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
b34976b6 55static bfd_boolean ppc64_elf_object_p
feee612b 56 PARAMS ((bfd *));
b34976b6 57static bfd_boolean ppc64_elf_merge_private_bfd_data
5bd4f169 58 PARAMS ((bfd *, bfd *));
f0abc2a1
AM
59static bfd_boolean ppc64_elf_new_section_hook
60 PARAMS ((bfd *, asection *));
5bd4f169
AM
61
62
5bd4f169
AM
63/* The name of the dynamic interpreter. This is put in the .interp
64 section. */
65#define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
66
67/* The size in bytes of an entry in the procedure linkage table. */
68#define PLT_ENTRY_SIZE 24
69
70/* The initial size of the plt reserved for the dynamic linker. */
5d1634d7 71#define PLT_INITIAL_ENTRY_SIZE PLT_ENTRY_SIZE
5bd4f169
AM
72
73/* TOC base pointers offset from start of TOC. */
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
e7b938ca 2506 /* The symbol addend that we'll be placing in the GOT. */
411e1bfb
AM
2507 bfd_vma addend;
2508
e7b938ca 2509 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
411e1bfb
AM
2510 union
2511 {
2512 bfd_signed_vma refcount;
2513 bfd_vma offset;
2514 } got;
2515
e7b938ca
AM
2516 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
2517 TLS_TPREL or TLS_DTPREL for tls entries. */
411e1bfb
AM
2518 char tls_type;
2519};
2520
2521/* The same for PLT. */
2522struct plt_entry
2523{
2524 struct plt_entry *next;
2525
2526 bfd_vma addend;
2527
2528 union
2529 {
2530 bfd_signed_vma refcount;
2531 bfd_vma offset;
2532 } plt;
2533};
2534
65f38f15 2535/* Of those relocs that might be copied as dynamic relocs, this macro
411e1bfb 2536 selects those that must be copied when linking a shared library. */
65f38f15 2537
411e1bfb 2538#define MUST_BE_DYN_RELOC(RTYPE) \
805fc799
AM
2539 ((RTYPE) != R_PPC64_REL32 \
2540 && (RTYPE) != R_PPC64_REL64 \
04c9666a 2541 && (RTYPE) != R_PPC64_REL30)
65f38f15 2542
721956f4
AM
2543/* Section name for stubs is the associated section name plus this
2544 string. */
2545#define STUB_SUFFIX ".stub"
2546
2547/* Linker stubs.
2548 ppc_stub_long_branch:
2549 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
2550 destination, but a 24 bit branch in a stub section will reach.
2551 . b dest
2552
2553 ppc_stub_plt_branch:
2554 Similar to the above, but a 24 bit branch in the stub section won't
2555 reach its destination.
87e226ce
AM
2556 . addis %r12,%r2,xxx@toc@ha
2557 . ld %r11,xxx@toc@l(%r12)
721956f4
AM
2558 . mtctr %r11
2559 . bctr
2560
2561 ppc_stub_plt_call:
2562 Used to call a function in a shared library.
87e226ce 2563 . addis %r12,%r2,xxx@toc@ha
721956f4 2564 . std %r2,40(%r1)
87e226ce
AM
2565 . ld %r11,xxx+0@toc@l(%r12)
2566 . ld %r2,xxx+8@toc@l(%r12)
721956f4 2567 . mtctr %r11
87e226ce 2568 . ld %r11,xxx+16@toc@l(%r12)
721956f4
AM
2569 . bctr
2570*/
2571
2572enum ppc_stub_type {
2573 ppc_stub_none,
2574 ppc_stub_long_branch,
2575 ppc_stub_plt_branch,
2576 ppc_stub_plt_call
2577};
2578
2579struct ppc_stub_hash_entry {
2580
2581 /* Base hash table entry structure. */
2582 struct bfd_hash_entry root;
2583
2584 /* The stub section. */
2585 asection *stub_sec;
2586
2587 /* Offset within stub_sec of the beginning of this stub. */
2588 bfd_vma stub_offset;
2589
2590 /* Given the symbol's value and its section we can determine its final
2591 value when building the stubs (so the stub knows where to jump. */
2592 bfd_vma target_value;
2593 asection *target_section;
2594
2595 enum ppc_stub_type stub_type;
2596
2597 /* The symbol table entry, if any, that this was derived from. */
2598 struct ppc_link_hash_entry *h;
2599
411e1bfb
AM
2600 /* And the reloc addend that this was derived from. */
2601 bfd_vma addend;
2602
721956f4
AM
2603 /* Where this stub is being called from, or, in the case of combined
2604 stub sections, the first input section in the group. */
2605 asection *id_sec;
2606};
2607
2608struct ppc_branch_hash_entry {
2609
2610 /* Base hash table entry structure. */
2611 struct bfd_hash_entry root;
2612
2613 /* Offset within .branch_lt. */
2614 unsigned int offset;
2615
2616 /* Generation marker. */
2617 unsigned int iter;
2618};
65f38f15
AM
2619
2620struct ppc_link_hash_entry
2621{
2622 struct elf_link_hash_entry elf;
2623
721956f4
AM
2624 /* A pointer to the most recently used stub hash entry against this
2625 symbol. */
2626 struct ppc_stub_hash_entry *stub_cache;
2627
65f38f15
AM
2628 /* Track dynamic relocs copied for this symbol. */
2629 struct ppc_dyn_relocs *dyn_relocs;
e86ce104 2630
721956f4
AM
2631 /* Link between function code and descriptor symbols. */
2632 struct elf_link_hash_entry *oh;
2633
e86ce104
AM
2634 /* Flag function code and descriptor symbols. */
2635 unsigned int is_func:1;
2636 unsigned int is_func_descriptor:1;
1e2f5b6e 2637 unsigned int is_entry:1;
411e1bfb
AM
2638
2639 /* Contexts in which symbol is used in the GOT (or TOC).
e7b938ca
AM
2640 TLS_GD .. TLS_EXPLICIT bits are or'd into the mask as the
2641 corresponding relocs are encountered during check_relocs.
2642 tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
2643 indicate the corresponding GOT entry type is not needed.
2644 tls_optimize may also set TLS_TPRELGD when a GD reloc turns into
2645 a TPREL one. We use a separate flag rather than setting TPREL
2646 just for convenience in distinguishing the two cases. */
2647#define TLS_GD 1 /* GD reloc. */
2648#define TLS_LD 2 /* LD reloc. */
2649#define TLS_TPREL 4 /* TPREL reloc, => IE. */
2650#define TLS_DTPREL 8 /* DTPREL reloc, => LD. */
2651#define TLS_TLS 16 /* Any TLS reloc. */
2652#define TLS_EXPLICIT 32 /* Marks TOC section TLS relocs. */
2653#define TLS_TPRELGD 64 /* TPREL reloc resulting from GD->IE. */
2654 char tls_mask;
65f38f15
AM
2655};
2656
2657/* ppc64 ELF linker hash table. */
2658
2659struct ppc_link_hash_table
2660{
2661 struct elf_link_hash_table elf;
2662
721956f4
AM
2663 /* The stub hash table. */
2664 struct bfd_hash_table stub_hash_table;
2665
2666 /* Another hash table for plt_branch stubs. */
2667 struct bfd_hash_table branch_hash_table;
2668
2669 /* Linker stub bfd. */
2670 bfd *stub_bfd;
2671
2672 /* Linker call-backs. */
2673 asection * (*add_stub_section) PARAMS ((const char *, asection *));
2674 void (*layout_sections_again) PARAMS ((void));
2675
2676 /* Array to keep track of which stub sections have been created, and
2677 information on stub grouping. */
2678 struct map_stub {
2679 /* This is the section to which stubs in the group will be attached. */
2680 asection *link_sec;
2681 /* The stub section. */
2682 asection *stub_sec;
2683 } *stub_group;
2684
2685 /* Assorted information used by ppc64_elf_size_stubs. */
721956f4
AM
2686 int top_index;
2687 asection **input_list;
721956f4 2688
65f38f15
AM
2689 /* Short-cuts to get to dynamic linker sections. */
2690 asection *sgot;
2691 asection *srelgot;
2692 asection *splt;
2693 asection *srelplt;
2694 asection *sdynbss;
2695 asection *srelbss;
2696 asection *sglink;
82bd7b59 2697 asection *sfpr;
721956f4
AM
2698 asection *sbrlt;
2699 asection *srelbrlt;
ec338859 2700
411e1bfb
AM
2701 /* Short-cut to first output tls section. */
2702 asection *tls_sec;
2703
2704 /* Shortcut to .__tls_get_addr. */
2705 struct elf_link_hash_entry *tls_get_addr;
2706
2707 /* TLS local dynamic got entry handling. */
2708 union {
2709 bfd_signed_vma refcount;
2710 bfd_vma offset;
2711 } tlsld_got;
2712
5d1634d7 2713 /* Set on error. */
721956f4
AM
2714 unsigned int stub_error;
2715
2716 /* Flag set when small branches are detected. Used to
2717 select suitable defaults for the stub group size. */
2718 unsigned int has_14bit_branch;
2719
805fc799
AM
2720 /* Set if we detect a reference undefined weak symbol. */
2721 unsigned int have_undefweak;
2722
721956f4
AM
2723 /* Incremented every time we size stubs. */
2724 unsigned int stub_iteration;
5d1634d7 2725
ec338859
AM
2726 /* Small local sym to section mapping cache. */
2727 struct sym_sec_cache sym_sec;
65f38f15
AM
2728};
2729
721956f4
AM
2730static struct bfd_hash_entry *stub_hash_newfunc
2731 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
2732static struct bfd_hash_entry *branch_hash_newfunc
2733 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
2734static struct bfd_hash_entry *link_hash_newfunc
2735 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
2736static struct bfd_link_hash_table *ppc64_elf_link_hash_table_create
2737 PARAMS ((bfd *));
2738static void ppc64_elf_link_hash_table_free
2739 PARAMS ((struct bfd_link_hash_table *));
2740static char *ppc_stub_name
2741 PARAMS ((const asection *, const asection *,
2742 const struct ppc_link_hash_entry *, const Elf_Internal_Rela *));
2743static struct ppc_stub_hash_entry *ppc_get_stub_entry
2744 PARAMS ((const asection *, const asection *, struct elf_link_hash_entry *,
2745 const Elf_Internal_Rela *, struct ppc_link_hash_table *));
2746static struct ppc_stub_hash_entry *ppc_add_stub
2747 PARAMS ((const char *, asection *, struct ppc_link_hash_table *));
b34976b6 2748static bfd_boolean create_linkage_sections
721956f4 2749 PARAMS ((bfd *, struct bfd_link_info *));
b34976b6 2750static bfd_boolean create_got_section
721956f4 2751 PARAMS ((bfd *, struct bfd_link_info *));
b34976b6 2752static bfd_boolean ppc64_elf_create_dynamic_sections
721956f4
AM
2753 PARAMS ((bfd *, struct bfd_link_info *));
2754static void ppc64_elf_copy_indirect_symbol
b48fa14c
AM
2755 PARAMS ((struct elf_backend_data *, struct elf_link_hash_entry *,
2756 struct elf_link_hash_entry *));
411e1bfb
AM
2757static bfd_boolean update_local_sym_info
2758 PARAMS ((bfd *, Elf_Internal_Shdr *, unsigned long, bfd_vma, int));
2759static bfd_boolean update_plt_info
2760 PARAMS ((bfd *, struct ppc_link_hash_entry *, bfd_vma));
b34976b6 2761static bfd_boolean ppc64_elf_check_relocs
721956f4
AM
2762 PARAMS ((bfd *, struct bfd_link_info *, asection *,
2763 const Elf_Internal_Rela *));
2764static asection * ppc64_elf_gc_mark_hook
1e2f5b6e
AM
2765 PARAMS ((asection *, struct bfd_link_info *, Elf_Internal_Rela *,
2766 struct elf_link_hash_entry *, Elf_Internal_Sym *));
b34976b6 2767static bfd_boolean ppc64_elf_gc_sweep_hook
1e2f5b6e
AM
2768 PARAMS ((bfd *, struct bfd_link_info *, asection *,
2769 const Elf_Internal_Rela *));
b34976b6 2770static bfd_boolean func_desc_adjust
721956f4 2771 PARAMS ((struct elf_link_hash_entry *, PTR));
b34976b6 2772static bfd_boolean ppc64_elf_func_desc_adjust
721956f4 2773 PARAMS ((bfd *, struct bfd_link_info *));
b34976b6 2774static bfd_boolean ppc64_elf_adjust_dynamic_symbol
721956f4
AM
2775 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
2776static void ppc64_elf_hide_symbol
b34976b6 2777 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *, bfd_boolean));
411e1bfb
AM
2778static bfd_boolean get_sym_h
2779 PARAMS ((struct elf_link_hash_entry **, Elf_Internal_Sym **, asection **,
2780 char **, Elf_Internal_Sym **, unsigned long, bfd *));
e7b938ca 2781static int get_tls_mask
411e1bfb 2782 PARAMS ((char **, Elf_Internal_Sym **, const Elf_Internal_Rela *, bfd *));
b34976b6 2783static bfd_boolean allocate_dynrelocs
721956f4 2784 PARAMS ((struct elf_link_hash_entry *, PTR));
b34976b6 2785static bfd_boolean readonly_dynrelocs
721956f4
AM
2786 PARAMS ((struct elf_link_hash_entry *, PTR));
2787static enum elf_reloc_type_class ppc64_elf_reloc_type_class
2788 PARAMS ((const Elf_Internal_Rela *));
b34976b6 2789static bfd_boolean ppc64_elf_size_dynamic_sections
721956f4 2790 PARAMS ((bfd *, struct bfd_link_info *));
411e1bfb 2791static enum ppc_stub_type ppc_type_of_stub
721956f4
AM
2792 PARAMS ((asection *, const Elf_Internal_Rela *,
2793 struct ppc_link_hash_entry **, bfd_vma));
2794static bfd_byte *build_plt_stub
2795 PARAMS ((bfd *, bfd_byte *, int, int));
b34976b6 2796static bfd_boolean ppc_build_one_stub
721956f4 2797 PARAMS ((struct bfd_hash_entry *, PTR));
b34976b6 2798static bfd_boolean ppc_size_one_stub
721956f4
AM
2799 PARAMS ((struct bfd_hash_entry *, PTR));
2800static void group_sections
b34976b6
AM
2801 PARAMS ((struct ppc_link_hash_table *, bfd_size_type, bfd_boolean));
2802static bfd_boolean ppc64_elf_relocate_section
721956f4
AM
2803 PARAMS ((bfd *, struct bfd_link_info *info, bfd *, asection *, bfd_byte *,
2804 Elf_Internal_Rela *relocs, Elf_Internal_Sym *local_syms,
2805 asection **));
b34976b6 2806static bfd_boolean ppc64_elf_finish_dynamic_symbol
721956f4
AM
2807 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
2808 Elf_Internal_Sym *));
b34976b6 2809static bfd_boolean ppc64_elf_finish_dynamic_sections
721956f4
AM
2810 PARAMS ((bfd *, struct bfd_link_info *));
2811
65f38f15
AM
2812/* Get the ppc64 ELF linker hash table from a link_info structure. */
2813
2814#define ppc_hash_table(p) \
2815 ((struct ppc_link_hash_table *) ((p)->hash))
2816
721956f4
AM
2817#define ppc_stub_hash_lookup(table, string, create, copy) \
2818 ((struct ppc_stub_hash_entry *) \
2819 bfd_hash_lookup ((table), (string), (create), (copy)))
2820
2821#define ppc_branch_hash_lookup(table, string, create, copy) \
2822 ((struct ppc_branch_hash_entry *) \
2823 bfd_hash_lookup ((table), (string), (create), (copy)))
2824
2825/* Create an entry in the stub hash table. */
2826
2827static struct bfd_hash_entry *
2828stub_hash_newfunc (entry, table, string)
2829 struct bfd_hash_entry *entry;
2830 struct bfd_hash_table *table;
2831 const char *string;
2832{
2833 /* Allocate the structure if it has not already been allocated by a
2834 subclass. */
2835 if (entry == NULL)
2836 {
2837 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
2838 if (entry == NULL)
2839 return entry;
2840 }
2841
2842 /* Call the allocation method of the superclass. */
2843 entry = bfd_hash_newfunc (entry, table, string);
2844 if (entry != NULL)
2845 {
2846 struct ppc_stub_hash_entry *eh;
2847
2848 /* Initialize the local fields. */
2849 eh = (struct ppc_stub_hash_entry *) entry;
2850 eh->stub_sec = NULL;
2851 eh->stub_offset = 0;
2852 eh->target_value = 0;
2853 eh->target_section = NULL;
2854 eh->stub_type = ppc_stub_none;
2855 eh->h = NULL;
2856 eh->id_sec = NULL;
2857 }
2858
2859 return entry;
2860}
2861
2862/* Create an entry in the branch hash table. */
2863
2864static struct bfd_hash_entry *
2865branch_hash_newfunc (entry, table, string)
2866 struct bfd_hash_entry *entry;
2867 struct bfd_hash_table *table;
2868 const char *string;
2869{
2870 /* Allocate the structure if it has not already been allocated by a
2871 subclass. */
2872 if (entry == NULL)
2873 {
2874 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
2875 if (entry == NULL)
2876 return entry;
2877 }
2878
2879 /* Call the allocation method of the superclass. */
2880 entry = bfd_hash_newfunc (entry, table, string);
2881 if (entry != NULL)
2882 {
2883 struct ppc_branch_hash_entry *eh;
2884
2885 /* Initialize the local fields. */
2886 eh = (struct ppc_branch_hash_entry *) entry;
2887 eh->offset = 0;
2888 eh->iter = 0;
2889 }
2890
2891 return entry;
2892}
2893
65f38f15
AM
2894/* Create an entry in a ppc64 ELF linker hash table. */
2895
2896static struct bfd_hash_entry *
2897link_hash_newfunc (entry, table, string)
2898 struct bfd_hash_entry *entry;
2899 struct bfd_hash_table *table;
2900 const char *string;
2901{
2902 /* Allocate the structure if it has not already been allocated by a
2903 subclass. */
2904 if (entry == NULL)
2905 {
2906 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
2907 if (entry == NULL)
2908 return entry;
2909 }
2910
2911 /* Call the allocation method of the superclass. */
2912 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
2913 if (entry != NULL)
2914 {
2915 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
2916
721956f4 2917 eh->stub_cache = NULL;
65f38f15 2918 eh->dyn_relocs = NULL;
721956f4 2919 eh->oh = NULL;
e86ce104
AM
2920 eh->is_func = 0;
2921 eh->is_func_descriptor = 0;
1e2f5b6e 2922 eh->is_entry = 0;
e7b938ca 2923 eh->tls_mask = 0;
65f38f15
AM
2924 }
2925
2926 return entry;
2927}
2928
2929/* Create a ppc64 ELF linker hash table. */
2930
2931static struct bfd_link_hash_table *
2932ppc64_elf_link_hash_table_create (abfd)
2933 bfd *abfd;
2934{
2935 struct ppc_link_hash_table *htab;
2936 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
2937
e2d34d7d 2938 htab = (struct ppc_link_hash_table *) bfd_malloc (amt);
65f38f15
AM
2939 if (htab == NULL)
2940 return NULL;
2941
2942 if (! _bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc))
2943 {
e2d34d7d 2944 free (htab);
65f38f15
AM
2945 return NULL;
2946 }
2947
721956f4
AM
2948 /* Init the stub hash table too. */
2949 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc))
2950 return NULL;
2951
2952 /* And the branch hash table. */
2953 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc))
2954 return NULL;
2955
2956 htab->stub_bfd = NULL;
2957 htab->add_stub_section = NULL;
2958 htab->layout_sections_again = NULL;
2959 htab->stub_group = NULL;
65f38f15
AM
2960 htab->sgot = NULL;
2961 htab->srelgot = NULL;
2962 htab->splt = NULL;
2963 htab->srelplt = NULL;
2964 htab->sdynbss = NULL;
2965 htab->srelbss = NULL;
2966 htab->sglink = NULL;
82bd7b59 2967 htab->sfpr = NULL;
721956f4
AM
2968 htab->sbrlt = NULL;
2969 htab->srelbrlt = NULL;
411e1bfb
AM
2970 htab->tls_sec = NULL;
2971 htab->tlsld_got.refcount = 0;
721956f4
AM
2972 htab->stub_error = 0;
2973 htab->has_14bit_branch = 0;
805fc799 2974 htab->have_undefweak = 0;
721956f4 2975 htab->stub_iteration = 0;
ec338859 2976 htab->sym_sec.abfd = NULL;
951fd09b
AM
2977 /* Initializing two fields of the union is just cosmetic. We really
2978 only care about glist, but when compiled on a 32-bit host the
2979 bfd_vma fields are larger. Setting the bfd_vma to zero makes
2980 debugger inspection of these fields look nicer. */
2981 htab->elf.init_refcount.refcount = 0;
411e1bfb 2982 htab->elf.init_refcount.glist = NULL;
951fd09b 2983 htab->elf.init_offset.offset = 0;
411e1bfb 2984 htab->elf.init_offset.glist = NULL;
65f38f15
AM
2985
2986 return &htab->elf.root;
2987}
2988
721956f4
AM
2989/* Free the derived linker hash table. */
2990
2991static void
2992ppc64_elf_link_hash_table_free (hash)
2993 struct bfd_link_hash_table *hash;
2994{
2995 struct ppc_link_hash_table *ret = (struct ppc_link_hash_table *) hash;
2996
2997 bfd_hash_table_free (&ret->stub_hash_table);
2998 bfd_hash_table_free (&ret->branch_hash_table);
2999 _bfd_generic_link_hash_table_free (hash);
3000}
3001
3002/* Build a name for an entry in the stub hash table. */
3003
3004static char *
3005ppc_stub_name (input_section, sym_sec, h, rel)
3006 const asection *input_section;
3007 const asection *sym_sec;
3008 const struct ppc_link_hash_entry *h;
3009 const Elf_Internal_Rela *rel;
3010{
3011 char *stub_name;
3012 bfd_size_type len;
3013
3014 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
3015 offsets from a sym as a branch target? In fact, we could
3016 probably assume the addend is always zero. */
3017 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
3018
3019 if (h)
3020 {
3021 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
3022 stub_name = bfd_malloc (len);
3023 if (stub_name != NULL)
3024 {
3025 sprintf (stub_name, "%08x_%s+%x",
3026 input_section->id & 0xffffffff,
3027 h->elf.root.root.string,
3028 (int) rel->r_addend & 0xffffffff);
3029 }
3030 }
3031 else
3032 {
3033 len = 8 + 1 + 8 + 1 + 8 + 1 + 16 + 1;
3034 stub_name = bfd_malloc (len);
3035 if (stub_name != NULL)
3036 {
3037 sprintf (stub_name, "%08x_%x:%x+%x",
3038 input_section->id & 0xffffffff,
3039 sym_sec->id & 0xffffffff,
3040 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
3041 (int) rel->r_addend & 0xffffffff);
3042 }
3043 }
3044 return stub_name;
3045}
3046
3047/* Look up an entry in the stub hash. Stub entries are cached because
3048 creating the stub name takes a bit of time. */
3049
3050static struct ppc_stub_hash_entry *
3051ppc_get_stub_entry (input_section, sym_sec, hash, rel, htab)
3052 const asection *input_section;
3053 const asection *sym_sec;
3054 struct elf_link_hash_entry *hash;
3055 const Elf_Internal_Rela *rel;
3056 struct ppc_link_hash_table *htab;
3057{
3058 struct ppc_stub_hash_entry *stub_entry;
3059 struct ppc_link_hash_entry *h = (struct ppc_link_hash_entry *) hash;
3060 const asection *id_sec;
3061
3062 /* If this input section is part of a group of sections sharing one
3063 stub section, then use the id of the first section in the group.
3064 Stub names need to include a section id, as there may well be
3065 more than one stub used to reach say, printf, and we need to
3066 distinguish between them. */
3067 id_sec = htab->stub_group[input_section->id].link_sec;
3068
3069 if (h != NULL && h->stub_cache != NULL
3070 && h->stub_cache->h == h
3071 && h->stub_cache->id_sec == id_sec)
3072 {
3073 stub_entry = h->stub_cache;
3074 }
3075 else
3076 {
3077 char *stub_name;
3078
3079 stub_name = ppc_stub_name (id_sec, sym_sec, h, rel);
3080 if (stub_name == NULL)
3081 return NULL;
3082
3083 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 3084 stub_name, FALSE, FALSE);
721956f4
AM
3085 if (h != NULL)
3086 h->stub_cache = stub_entry;
3087
3088 free (stub_name);
3089 }
3090
3091 return stub_entry;
3092}
3093
3094/* Add a new stub entry to the stub hash. Not all fields of the new
3095 stub entry are initialised. */
3096
3097static struct ppc_stub_hash_entry *
3098ppc_add_stub (stub_name, section, htab)
3099 const char *stub_name;
3100 asection *section;
3101 struct ppc_link_hash_table *htab;
3102{
3103 asection *link_sec;
3104 asection *stub_sec;
3105 struct ppc_stub_hash_entry *stub_entry;
3106
3107 link_sec = htab->stub_group[section->id].link_sec;
3108 stub_sec = htab->stub_group[section->id].stub_sec;
3109 if (stub_sec == NULL)
3110 {
3111 stub_sec = htab->stub_group[link_sec->id].stub_sec;
3112 if (stub_sec == NULL)
3113 {
d4c88bbb 3114 size_t namelen;
721956f4
AM
3115 bfd_size_type len;
3116 char *s_name;
3117
d4c88bbb
AM
3118 namelen = strlen (link_sec->name);
3119 len = namelen + sizeof (STUB_SUFFIX);
721956f4
AM
3120 s_name = bfd_alloc (htab->stub_bfd, len);
3121 if (s_name == NULL)
3122 return NULL;
3123
d4c88bbb
AM
3124 memcpy (s_name, link_sec->name, namelen);
3125 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
721956f4
AM
3126 stub_sec = (*htab->add_stub_section) (s_name, link_sec);
3127 if (stub_sec == NULL)
3128 return NULL;
3129 htab->stub_group[link_sec->id].stub_sec = stub_sec;
3130 }
3131 htab->stub_group[section->id].stub_sec = stub_sec;
3132 }
3133
3134 /* Enter this entry into the linker stub hash table. */
3135 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
b34976b6 3136 TRUE, FALSE);
721956f4
AM
3137 if (stub_entry == NULL)
3138 {
3139 (*_bfd_error_handler) (_("%s: cannot create stub entry %s"),
3140 bfd_archive_filename (section->owner),
3141 stub_name);
3142 return NULL;
3143 }
3144
3145 stub_entry->stub_sec = stub_sec;
3146 stub_entry->stub_offset = 0;
3147 stub_entry->id_sec = link_sec;
3148 return stub_entry;
3149}
3150
82bd7b59
AM
3151/* Create sections for linker generated code. */
3152
b34976b6 3153static bfd_boolean
82bd7b59
AM
3154create_linkage_sections (dynobj, info)
3155 bfd *dynobj;
3156 struct bfd_link_info *info;
3157{
3158 struct ppc_link_hash_table *htab;
3159 flagword flags;
3160
3161 htab = ppc_hash_table (info);
3162
3163 /* Create .sfpr for code to save and restore fp regs. */
3164 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
3165 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
721956f4 3166 htab->sfpr = bfd_make_section_anyway (dynobj, ".sfpr");
82bd7b59
AM
3167 if (htab->sfpr == NULL
3168 || ! bfd_set_section_flags (dynobj, htab->sfpr, flags)
3169 || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
b34976b6 3170 return FALSE;
82bd7b59 3171
721956f4
AM
3172 /* Create .glink for lazy dynamic linking support. */
3173 htab->sglink = bfd_make_section_anyway (dynobj, ".glink");
82bd7b59
AM
3174 if (htab->sglink == NULL
3175 || ! bfd_set_section_flags (dynobj, htab->sglink, flags)
3176 || ! bfd_set_section_alignment (dynobj, htab->sglink, 2))
b34976b6 3177 return FALSE;
82bd7b59 3178
721956f4
AM
3179 /* Create .branch_lt for plt_branch stubs. */
3180 flags = (SEC_ALLOC | SEC_LOAD
3181 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3182 htab->sbrlt = bfd_make_section_anyway (dynobj, ".branch_lt");
3183 if (htab->sbrlt == NULL
3184 || ! bfd_set_section_flags (dynobj, htab->sbrlt, flags)
3185 || ! bfd_set_section_alignment (dynobj, htab->sbrlt, 3))
b34976b6 3186 return FALSE;
721956f4
AM
3187
3188 if (info->shared)
3189 {
3190 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3191 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
1e2f5b6e 3192 htab->srelbrlt = bfd_make_section_anyway (dynobj, ".rela.branch_lt");
721956f4
AM
3193 if (!htab->srelbrlt
3194 || ! bfd_set_section_flags (dynobj, htab->srelbrlt, flags)
3195 || ! bfd_set_section_alignment (dynobj, htab->srelbrlt, 3))
b34976b6 3196 return FALSE;
721956f4 3197 }
b34976b6 3198 return TRUE;
82bd7b59
AM
3199}
3200
65f38f15
AM
3201/* Create .got and .rela.got sections in DYNOBJ, and set up
3202 shortcuts to them in our hash table. */
3203
b34976b6 3204static bfd_boolean
65f38f15
AM
3205create_got_section (dynobj, info)
3206 bfd *dynobj;
3207 struct bfd_link_info *info;
3208{
3209 struct ppc_link_hash_table *htab;
3210
3211 if (! _bfd_elf_create_got_section (dynobj, info))
b34976b6 3212 return FALSE;
65f38f15
AM
3213
3214 htab = ppc_hash_table (info);
3215 htab->sgot = bfd_get_section_by_name (dynobj, ".got");
3216 if (!htab->sgot)
3217 abort ();
3218
3219 htab->srelgot = bfd_make_section (dynobj, ".rela.got");
3220 if (!htab->srelgot
3221 || ! bfd_set_section_flags (dynobj, htab->srelgot,
3222 (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
3223 | SEC_IN_MEMORY | SEC_LINKER_CREATED
3224 | SEC_READONLY))
3225 || ! bfd_set_section_alignment (dynobj, htab->srelgot, 3))
b34976b6
AM
3226 return FALSE;
3227 return TRUE;
65f38f15 3228}
5bd4f169 3229
82bd7b59 3230/* Create the dynamic sections, and set up shortcuts. */
5bd4f169 3231
b34976b6 3232static bfd_boolean
65f38f15
AM
3233ppc64_elf_create_dynamic_sections (dynobj, info)
3234 bfd *dynobj;
5bd4f169
AM
3235 struct bfd_link_info *info;
3236{
65f38f15 3237 struct ppc_link_hash_table *htab;
5bd4f169 3238
65f38f15
AM
3239 htab = ppc_hash_table (info);
3240 if (!htab->sgot && !create_got_section (dynobj, info))
b34976b6 3241 return FALSE;
5bd4f169 3242
65f38f15 3243 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
b34976b6 3244 return FALSE;
65f38f15
AM
3245
3246 htab->splt = bfd_get_section_by_name (dynobj, ".plt");
3247 htab->srelplt = bfd_get_section_by_name (dynobj, ".rela.plt");
3248 htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
3249 if (!info->shared)
3250 htab->srelbss = bfd_get_section_by_name (dynobj, ".rela.bss");
3251
3252 if (!htab->splt || !htab->srelplt || !htab->sdynbss
3253 || (!info->shared && !htab->srelbss))
3254 abort ();
3255
b34976b6 3256 return TRUE;
5bd4f169
AM
3257}
3258
65f38f15
AM
3259/* Copy the extra info we tack onto an elf_link_hash_entry. */
3260
3261static void
b48fa14c 3262ppc64_elf_copy_indirect_symbol (bed, dir, ind)
411e1bfb 3263 struct elf_backend_data *bed ATTRIBUTE_UNUSED;
65f38f15
AM
3264 struct elf_link_hash_entry *dir, *ind;
3265{
3266 struct ppc_link_hash_entry *edir, *eind;
3267
3268 edir = (struct ppc_link_hash_entry *) dir;
3269 eind = (struct ppc_link_hash_entry *) ind;
3270
411e1bfb 3271 /* Copy over any dynamic relocs we may have on the indirect sym. */
bbd7ec4a 3272 if (eind->dyn_relocs != NULL)
65f38f15 3273 {
bbd7ec4a
AM
3274 if (edir->dyn_relocs != NULL)
3275 {
3276 struct ppc_dyn_relocs **pp;
3277 struct ppc_dyn_relocs *p;
3278
411e1bfb 3279 if (eind->elf.root.type == bfd_link_hash_indirect)
bbd7ec4a
AM
3280 abort ();
3281
3282 /* Add reloc counts against the weak sym to the strong sym
3283 list. Merge any entries against the same section. */
3284 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
3285 {
3286 struct ppc_dyn_relocs *q;
3287
3288 for (q = edir->dyn_relocs; q != NULL; q = q->next)
3289 if (q->sec == p->sec)
3290 {
3291 q->pc_count += p->pc_count;
3292 q->count += p->count;
3293 *pp = p->next;
3294 break;
3295 }
3296 if (q == NULL)
3297 pp = &p->next;
3298 }
3299 *pp = edir->dyn_relocs;
3300 }
3301
65f38f15
AM
3302 edir->dyn_relocs = eind->dyn_relocs;
3303 eind->dyn_relocs = NULL;
3304 }
65f38f15 3305
6349e628
AM
3306 edir->is_func |= eind->is_func;
3307 edir->is_func_descriptor |= eind->is_func_descriptor;
3308 edir->is_entry |= eind->is_entry;
3309
3310 /* Copy down any references that we may have already seen to the
3311 symbol which just became indirect. */
3312 edir->elf.elf_link_hash_flags |=
3313 (eind->elf.elf_link_hash_flags
3314 & (ELF_LINK_HASH_REF_DYNAMIC
3315 | ELF_LINK_HASH_REF_REGULAR
3316 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
3317 | ELF_LINK_NON_GOT_REF));
3318
3319 /* If we were called to copy over info for a weak sym, that's all. */
3320 if (eind->elf.root.type != bfd_link_hash_indirect)
3321 return;
3322
3323 /* Copy over got entries. */
411e1bfb
AM
3324 if (eind->elf.got.glist != NULL)
3325 {
3326 if (edir->elf.got.glist != NULL)
3327 {
3328 struct got_entry **entp;
3329 struct got_entry *ent;
3330
3331 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
3332 {
3333 struct got_entry *dent;
3334
3335 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
3336 if (dent->addend == ent->addend
3337 && dent->tls_type == ent->tls_type)
3338 {
3339 dent->got.refcount += ent->got.refcount;
3340 *entp = ent->next;
3341 break;
3342 }
3343 if (dent == NULL)
3344 entp = &ent->next;
3345 }
3346 *entp = edir->elf.got.glist;
3347 }
3348
3349 edir->elf.got.glist = eind->elf.got.glist;
3350 eind->elf.got.glist = NULL;
3351 }
3352
3353 /* And plt entries. */
3354 if (eind->elf.plt.plist != NULL)
3355 {
3356 if (edir->elf.plt.plist != NULL)
3357 {
3358 struct plt_entry **entp;
3359 struct plt_entry *ent;
3360
3361 for (entp = &eind->elf.plt.plist; (ent = *entp) != NULL; )
3362 {
3363 struct plt_entry *dent;
3364
3365 for (dent = edir->elf.plt.plist; dent != NULL; dent = dent->next)
3366 if (dent->addend == ent->addend)
3367 {
3368 dent->plt.refcount += ent->plt.refcount;
3369 *entp = ent->next;
3370 break;
3371 }
3372 if (dent == NULL)
3373 entp = &ent->next;
3374 }
3375 *entp = edir->elf.plt.plist;
3376 }
3377
3378 edir->elf.plt.plist = eind->elf.plt.plist;
3379 eind->elf.plt.plist = NULL;
3380 }
3381
411e1bfb
AM
3382 if (edir->elf.dynindx == -1)
3383 {
3384 edir->elf.dynindx = eind->elf.dynindx;
3385 edir->elf.dynstr_index = eind->elf.dynstr_index;
3386 eind->elf.dynindx = -1;
3387 eind->elf.dynstr_index = 0;
3388 }
3389 else
3390 BFD_ASSERT (eind->elf.dynindx == -1);
3391}
3392
3393/* Set a flag, used by ppc64_elf_gc_mark_hook, on the entry symbol and
3394 symbols undefined on the command-line. */
3395
3396bfd_boolean
3397ppc64_elf_mark_entry_syms (info)
3398 struct bfd_link_info *info;
3399{
3400 struct ppc_link_hash_table *htab;
3401 struct bfd_sym_chain *sym;
3402
3403 htab = ppc_hash_table (info);
3404 for (sym = info->gc_sym_list; sym; sym = sym->next)
3405 {
3406 struct elf_link_hash_entry *h;
3407
3408 h = elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, FALSE);
3409 if (h != NULL)
3410 ((struct ppc_link_hash_entry *) h)->is_entry = 1;
3411 }
3412 return TRUE;
3413}
3414
3415static bfd_boolean
3416update_local_sym_info (abfd, symtab_hdr, r_symndx, r_addend, tls_type)
3417 bfd *abfd;
3418 Elf_Internal_Shdr *symtab_hdr;
3419 unsigned long r_symndx;
3420 bfd_vma r_addend;
3421 int tls_type;
3422{
3423 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
e7b938ca 3424 char *local_got_tls_masks;
411e1bfb
AM
3425
3426 if (local_got_ents == NULL)
3427 {
3428 bfd_size_type size = symtab_hdr->sh_info;
3429
e7b938ca 3430 size *= sizeof (*local_got_ents) + sizeof (*local_got_tls_masks);
411e1bfb
AM
3431 local_got_ents = (struct got_entry **) bfd_zalloc (abfd, size);
3432 if (local_got_ents == NULL)
3433 return FALSE;
3434 elf_local_got_ents (abfd) = local_got_ents;
3435 }
3436
3437 if ((tls_type & TLS_EXPLICIT) == 0)
3438 {
3439 struct got_entry *ent;
3440
3441 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
3442 if (ent->addend == r_addend && ent->tls_type == tls_type)
3443 break;
3444 if (ent == NULL)
3445 {
3446 bfd_size_type amt = sizeof (*ent);
3447 ent = (struct got_entry *) bfd_alloc (abfd, amt);
3448 if (ent == NULL)
3449 return FALSE;
3450 ent->next = local_got_ents[r_symndx];
3451 ent->addend = r_addend;
3452 ent->tls_type = tls_type;
3453 ent->got.refcount = 0;
3454 local_got_ents[r_symndx] = ent;
3455 }
3456 ent->got.refcount += 1;
3457 }
3458
e7b938ca
AM
3459 local_got_tls_masks = (char *) (local_got_ents + symtab_hdr->sh_info);
3460 local_got_tls_masks[r_symndx] |= tls_type;
411e1bfb 3461 return TRUE;
65f38f15
AM
3462}
3463
411e1bfb
AM
3464static bfd_boolean
3465update_plt_info (abfd, eh, addend)
3466 bfd *abfd;
3467 struct ppc_link_hash_entry *eh;
3468 bfd_vma addend;
1e2f5b6e 3469{
411e1bfb 3470 struct plt_entry *ent;
1e2f5b6e 3471
411e1bfb
AM
3472 for (ent = eh->elf.plt.plist; ent != NULL; ent = ent->next)
3473 if (ent->addend == addend)
3474 break;
3475 if (ent == NULL)
1e2f5b6e 3476 {
411e1bfb
AM
3477 bfd_size_type amt = sizeof (*ent);
3478 ent = (struct plt_entry *) bfd_alloc (abfd, amt);
3479 if (ent == NULL)
3480 return FALSE;
3481 ent->next = eh->elf.plt.plist;
3482 ent->addend = addend;
3483 ent->plt.refcount = 0;
3484 eh->elf.plt.plist = ent;
1e2f5b6e 3485 }
411e1bfb
AM
3486 ent->plt.refcount += 1;
3487 eh->elf.elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
3488 eh->is_func = 1;
b34976b6 3489 return TRUE;
1e2f5b6e
AM
3490}
3491
5bd4f169 3492/* Look through the relocs for a section during the first phase, and
65f38f15 3493 calculate needed space in the global offset table, procedure
5d1634d7 3494 linkage table, and dynamic reloc sections. */
5bd4f169 3495
b34976b6 3496static bfd_boolean
5bd4f169
AM
3497ppc64_elf_check_relocs (abfd, info, sec, relocs)
3498 bfd *abfd;
3499 struct bfd_link_info *info;
3500 asection *sec;
3501 const Elf_Internal_Rela *relocs;
3502{
65f38f15 3503 struct ppc_link_hash_table *htab;
5bd4f169
AM
3504 Elf_Internal_Shdr *symtab_hdr;
3505 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
3506 const Elf_Internal_Rela *rel;
3507 const Elf_Internal_Rela *rel_end;
5bd4f169 3508 asection *sreloc;
1e2f5b6e 3509 asection **opd_sym_map;
5bd4f169
AM
3510
3511 if (info->relocateable)
b34976b6 3512 return TRUE;
5bd4f169 3513
65f38f15 3514 htab = ppc_hash_table (info);
5bd4f169 3515 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5bd4f169
AM
3516
3517 sym_hashes = elf_sym_hashes (abfd);
3518 sym_hashes_end = (sym_hashes
1e2f5b6e
AM
3519 + symtab_hdr->sh_size / sizeof (Elf64_External_Sym)
3520 - symtab_hdr->sh_info);
5bd4f169
AM
3521
3522 sreloc = NULL;
1e2f5b6e
AM
3523 opd_sym_map = NULL;
3524 if (strcmp (bfd_get_section_name (abfd, sec), ".opd") == 0)
3525 {
3526 /* Garbage collection needs some extra help with .opd sections.
3527 We don't want to necessarily keep everything referenced by
3528 relocs in .opd, as that would keep all functions. Instead,
3529 if we reference an .opd symbol (a function descriptor), we
3530 want to keep the function code symbol's section. This is
3531 easy for global symbols, but for local syms we need to keep
3532 information about the associated function section. Later, if
3533 edit_opd deletes entries, we'll use this array to adjust
3534 local syms in .opd. */
3535 union opd_info {
3536 asection *func_section;
3537 long entry_adjust;
3538 };
3539 bfd_size_type amt;
3540
3541 amt = sec->_raw_size * sizeof (union opd_info) / 24;
3542 opd_sym_map = (asection **) bfd_zalloc (abfd, amt);
3543 if (opd_sym_map == NULL)
b34976b6 3544 return FALSE;
f0abc2a1 3545 ppc64_elf_section_data (sec)->opd.func_sec = opd_sym_map;
1e2f5b6e 3546 }
5bd4f169 3547
82bd7b59
AM
3548 if (htab->elf.dynobj == NULL)
3549 htab->elf.dynobj = abfd;
3550 if (htab->sfpr == NULL
3551 && !create_linkage_sections (htab->elf.dynobj, info))
b34976b6 3552 return FALSE;
82bd7b59 3553
5bd4f169
AM
3554 rel_end = relocs + sec->reloc_count;
3555 for (rel = relocs; rel < rel_end; rel++)
3556 {
3557 unsigned long r_symndx;
3558 struct elf_link_hash_entry *h;
04c9666a 3559 enum elf_ppc64_reloc_type r_type;
411e1bfb 3560 int tls_type = 0;
5bd4f169
AM
3561
3562 r_symndx = ELF64_R_SYM (rel->r_info);
3563 if (r_symndx < symtab_hdr->sh_info)
3564 h = NULL;
3565 else
3566 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3567
04c9666a 3568 r_type = (enum elf_ppc64_reloc_type) ELF64_R_TYPE (rel->r_info);
a33d1f77 3569 switch (r_type)
5bd4f169 3570 {
411e1bfb
AM
3571 case R_PPC64_GOT_TLSLD16:
3572 case R_PPC64_GOT_TLSLD16_LO:
3573 case R_PPC64_GOT_TLSLD16_HI:
3574 case R_PPC64_GOT_TLSLD16_HA:
3575 htab->tlsld_got.refcount += 1;
951fd09b 3576 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
3577 goto dogottls;
3578
3579 case R_PPC64_GOT_TLSGD16:
3580 case R_PPC64_GOT_TLSGD16_LO:
3581 case R_PPC64_GOT_TLSGD16_HI:
3582 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 3583 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
3584 goto dogottls;
3585
3586 case R_PPC64_GOT_TPREL16_DS:
3587 case R_PPC64_GOT_TPREL16_LO_DS:
3588 case R_PPC64_GOT_TPREL16_HI:
3589 case R_PPC64_GOT_TPREL16_HA:
3590 if (info->shared)
3591 info->flags |= DF_STATIC_TLS;
3592 tls_type = TLS_TLS | TLS_TPREL;
3593 goto dogottls;
3594
3595 case R_PPC64_GOT_DTPREL16_DS:
3596 case R_PPC64_GOT_DTPREL16_LO_DS:
3597 case R_PPC64_GOT_DTPREL16_HI:
3598 case R_PPC64_GOT_DTPREL16_HA:
3599 tls_type = TLS_TLS | TLS_DTPREL;
3600 dogottls:
3601 sec->has_tls_reloc = 1;
3602 /* Fall thru */
3603
5bd4f169 3604 case R_PPC64_GOT16:
5bd4f169 3605 case R_PPC64_GOT16_DS:
65f38f15
AM
3606 case R_PPC64_GOT16_HA:
3607 case R_PPC64_GOT16_HI:
3608 case R_PPC64_GOT16_LO:
5bd4f169 3609 case R_PPC64_GOT16_LO_DS:
65f38f15 3610 /* This symbol requires a global offset table entry. */
82bd7b59
AM
3611 if (htab->sgot == NULL
3612 && !create_got_section (htab->elf.dynobj, info))
b34976b6 3613 return FALSE;
5bd4f169
AM
3614
3615 if (h != NULL)
3616 {
411e1bfb
AM
3617 struct ppc_link_hash_entry *eh;
3618 struct got_entry *ent;
65f38f15 3619
411e1bfb
AM
3620 eh = (struct ppc_link_hash_entry *) h;
3621 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
3622 if (ent->addend == rel->r_addend
3623 && ent->tls_type == tls_type)
3624 break;
3625 if (ent == NULL)
5bd4f169 3626 {
411e1bfb
AM
3627 bfd_size_type amt = sizeof (*ent);
3628 ent = (struct got_entry *) bfd_alloc (abfd, amt);
3629 if (ent == NULL)
b34976b6 3630 return FALSE;
411e1bfb
AM
3631 ent->next = eh->elf.got.glist;
3632 ent->addend = rel->r_addend;
3633 ent->tls_type = tls_type;
3634 ent->got.refcount = 0;
3635 eh->elf.got.glist = ent;
5bd4f169 3636 }
411e1bfb 3637 ent->got.refcount += 1;
e7b938ca 3638 eh->tls_mask |= tls_type;
5bd4f169 3639 }
411e1bfb
AM
3640 else
3641 /* This is a global offset table entry for a local symbol. */
3642 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
3643 rel->r_addend, tls_type))
3644 return FALSE;
5bd4f169
AM
3645 break;
3646
5bd4f169 3647 case R_PPC64_PLT16_HA:
65f38f15
AM
3648 case R_PPC64_PLT16_HI:
3649 case R_PPC64_PLT16_LO:
3650 case R_PPC64_PLT32:
3651 case R_PPC64_PLT64:
5bd4f169 3652 /* This symbol requires a procedure linkage table entry. We
3fad3c7c
AM
3653 actually build the entry in adjust_dynamic_symbol,
3654 because this might be a case of linking PIC code without
3655 linking in any dynamic objects, in which case we don't
3656 need to generate a procedure linkage table after all. */
5bd4f169
AM
3657 if (h == NULL)
3658 {
3659 /* It does not make sense to have a procedure linkage
3fad3c7c 3660 table entry for a local symbol. */
5bd4f169 3661 bfd_set_error (bfd_error_bad_value);
b34976b6 3662 return FALSE;
5bd4f169 3663 }
411e1bfb
AM
3664 else
3665 if (!update_plt_info (abfd, (struct ppc_link_hash_entry *) h,
3666 rel->r_addend))
3667 return FALSE;
5bd4f169
AM
3668 break;
3669
3670 /* The following relocations don't need to propagate the
3671 relocation if linking a shared object since they are
3672 section relative. */
3673 case R_PPC64_SECTOFF:
3674 case R_PPC64_SECTOFF_LO:
3675 case R_PPC64_SECTOFF_HI:
3676 case R_PPC64_SECTOFF_HA:
3677 case R_PPC64_SECTOFF_DS:
3678 case R_PPC64_SECTOFF_LO_DS:
3679 case R_PPC64_TOC16:
3680 case R_PPC64_TOC16_LO:
3681 case R_PPC64_TOC16_HI:
3682 case R_PPC64_TOC16_HA:
3683 case R_PPC64_TOC16_DS:
3684 case R_PPC64_TOC16_LO_DS:
411e1bfb
AM
3685 case R_PPC64_DTPREL16:
3686 case R_PPC64_DTPREL16_LO:
3687 case R_PPC64_DTPREL16_HI:
3688 case R_PPC64_DTPREL16_HA:
3689 case R_PPC64_DTPREL16_DS:
3690 case R_PPC64_DTPREL16_LO_DS:
3691 case R_PPC64_DTPREL16_HIGHER:
3692 case R_PPC64_DTPREL16_HIGHERA:
3693 case R_PPC64_DTPREL16_HIGHEST:
3694 case R_PPC64_DTPREL16_HIGHESTA:
5bd4f169
AM
3695 break;
3696
3697 /* This relocation describes the C++ object vtable hierarchy.
3698 Reconstruct it for later use during GC. */
3699 case R_PPC64_GNU_VTINHERIT:
3700 if (!_bfd_elf64_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 3701 return FALSE;
5bd4f169
AM
3702 break;
3703
3704 /* This relocation describes which C++ vtable entries are actually
3705 used. Record for later use during GC. */
3706 case R_PPC64_GNU_VTENTRY:
3707 if (!_bfd_elf64_gc_record_vtentry (abfd, sec, h, rel->r_addend))
b34976b6 3708 return FALSE;
5bd4f169
AM
3709 break;
3710
721956f4
AM
3711 case R_PPC64_REL14:
3712 case R_PPC64_REL14_BRTAKEN:
3713 case R_PPC64_REL14_BRNTAKEN:
3714 htab->has_14bit_branch = 1;
3715 /* Fall through. */
3716
5d1634d7 3717 case R_PPC64_REL24:
e86ce104
AM
3718 if (h != NULL
3719 && h->root.root.string[0] == '.'
3720 && h->root.root.string[1] != 0)
5d1634d7
AM
3721 {
3722 /* We may need a .plt entry if the function this reloc
3723 refers to is in a shared lib. */
411e1bfb
AM
3724 if (!update_plt_info (abfd, (struct ppc_link_hash_entry *) h,
3725 rel->r_addend))
3726 return FALSE;
3727 if (h == htab->tls_get_addr)
3728 sec->has_tls_reloc = 1;
a48ebf4d
AM
3729 else if ((strncmp (h->root.root.string, ".__tls_get_addr", 15)
3730 == 0)
3731 && (h->root.root.string[15] == 0
3732 || h->root.root.string[15] == '@'))
411e1bfb
AM
3733 {
3734 htab->tls_get_addr = h;
3735 sec->has_tls_reloc = 1;
3736 }
3737 }
3738 break;
3739
3740 case R_PPC64_TPREL64:
3741 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
3742 if (info->shared)
3743 info->flags |= DF_STATIC_TLS;
3744 goto dotlstoc;
3745
3746 case R_PPC64_DTPMOD64:
3747 if (rel + 1 < rel_end
3748 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
3749 && rel[1].r_offset == rel->r_offset + 8)
951fd09b 3750 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
411e1bfb 3751 else
951fd09b 3752 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
411e1bfb
AM
3753 goto dotlstoc;
3754
3755 case R_PPC64_DTPREL64:
3756 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
3757 if (rel != relocs
3758 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
3759 && rel[-1].r_offset == rel->r_offset - 8)
3760 /* This is the second reloc of a dtpmod, dtprel pair.
3761 Don't mark with TLS_DTPREL. */
3762 goto dodyn;
3763
3764 dotlstoc:
3765 sec->has_tls_reloc = 1;
3766 if (h != NULL)
3767 {
3768 struct ppc_link_hash_entry *eh;
3769 eh = (struct ppc_link_hash_entry *) h;
e7b938ca 3770 eh->tls_mask |= tls_type;
411e1bfb
AM
3771 }
3772 else
3773 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
3774 rel->r_addend, tls_type))
3775 return FALSE;
3776
3777 if (ppc64_elf_section_data (sec)->t_symndx == NULL)
3778 {
e7b938ca 3779 /* One extra to simplify get_tls_mask. */
951fd09b 3780 bfd_size_type amt = sec->_raw_size * sizeof (unsigned) / 8 + 1;
411e1bfb
AM
3781 ppc64_elf_section_data (sec)->t_symndx
3782 = (unsigned *) bfd_zalloc (abfd, amt);
3783 if (ppc64_elf_section_data (sec)->t_symndx == NULL)
3784 return FALSE;
3785 }
3786 BFD_ASSERT (rel->r_offset % 8 == 0);
3787 ppc64_elf_section_data (sec)->t_symndx[rel->r_offset / 8] = r_symndx;
951fd09b
AM
3788
3789 /* Mark the second slot of a GD or LD entry.
3790 -1 to indicate GD and -2 to indicate LD. */
3791 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
3792 ppc64_elf_section_data (sec)->t_symndx[rel->r_offset / 8 + 1] = -1;
3793 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
3794 ppc64_elf_section_data (sec)->t_symndx[rel->r_offset / 8 + 1] = -2;
411e1bfb
AM
3795 goto dodyn;
3796
3797 case R_PPC64_TPREL16:
3798 case R_PPC64_TPREL16_LO:
3799 case R_PPC64_TPREL16_HI:
3800 case R_PPC64_TPREL16_HA:
3801 case R_PPC64_TPREL16_DS:
3802 case R_PPC64_TPREL16_LO_DS:
3803 case R_PPC64_TPREL16_HIGHER:
3804 case R_PPC64_TPREL16_HIGHERA:
3805 case R_PPC64_TPREL16_HIGHEST:
3806 case R_PPC64_TPREL16_HIGHESTA:
3807 if (info->shared)
3808 {
3809 info->flags |= DF_STATIC_TLS;
3810 goto dodyn;
5d1634d7
AM
3811 }
3812 break;
3813
e86ce104 3814 case R_PPC64_ADDR64:
1e2f5b6e 3815 if (opd_sym_map != NULL
e86ce104
AM
3816 && h != NULL
3817 && h->root.root.string[0] == '.'
3818 && h->root.root.string[1] != 0)
3819 {
3820 struct elf_link_hash_entry *fdh;
3821
3822 fdh = elf_link_hash_lookup (&htab->elf, h->root.root.string + 1,
b34976b6 3823 FALSE, FALSE, FALSE);
e86ce104
AM
3824 if (fdh != NULL)
3825 {
e86ce104 3826 ((struct ppc_link_hash_entry *) fdh)->is_func_descriptor = 1;
721956f4 3827 ((struct ppc_link_hash_entry *) fdh)->oh = h;
e86ce104 3828 ((struct ppc_link_hash_entry *) h)->is_func = 1;
721956f4 3829 ((struct ppc_link_hash_entry *) h)->oh = fdh;
e86ce104
AM
3830 }
3831 }
1e2f5b6e
AM
3832 if (opd_sym_map != NULL
3833 && h == NULL
3834 && rel + 1 < rel_end
04c9666a 3835 && ((enum elf_ppc64_reloc_type) ELF64_R_TYPE ((rel + 1)->r_info)
1e2f5b6e
AM
3836 == R_PPC64_TOC))
3837 {
3838 asection *s;
3839
3840 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec, sec,
3841 r_symndx);
3842 if (s == NULL)
b34976b6 3843 return FALSE;
1e2f5b6e
AM
3844 else if (s != sec)
3845 opd_sym_map[rel->r_offset / 24] = s;
3846 }
e86ce104
AM
3847 /* Fall through. */
3848
04c9666a 3849 case R_PPC64_REL30:
5bd4f169 3850 case R_PPC64_REL32:
04c9666a 3851 case R_PPC64_REL64:
65f38f15
AM
3852 case R_PPC64_ADDR14:
3853 case R_PPC64_ADDR14_BRNTAKEN:
3854 case R_PPC64_ADDR14_BRTAKEN:
3855 case R_PPC64_ADDR16:
3856 case R_PPC64_ADDR16_DS:
3857 case R_PPC64_ADDR16_HA:
3858 case R_PPC64_ADDR16_HI:
3859 case R_PPC64_ADDR16_HIGHER:
3860 case R_PPC64_ADDR16_HIGHERA:
3861 case R_PPC64_ADDR16_HIGHEST:
3862 case R_PPC64_ADDR16_HIGHESTA:
3863 case R_PPC64_ADDR16_LO:
3864 case R_PPC64_ADDR16_LO_DS:
3865 case R_PPC64_ADDR24:
65f38f15 3866 case R_PPC64_ADDR32:
65f38f15
AM
3867 case R_PPC64_UADDR16:
3868 case R_PPC64_UADDR32:
3869 case R_PPC64_UADDR64:
5bd4f169 3870 case R_PPC64_TOC:
41bd81ab 3871 /* Don't propagate .opd relocs. */
1e2f5b6e 3872 if (NO_OPD_RELOCS && opd_sym_map != NULL)
e86ce104 3873 break;
e86ce104 3874
65f38f15
AM
3875 /* If we are creating a shared library, and this is a reloc
3876 against a global symbol, or a non PC relative reloc
3877 against a local symbol, then we need to copy the reloc
3878 into the shared library. However, if we are linking with
3879 -Bsymbolic, we do not need to copy a reloc against a
3880 global symbol which is defined in an object we are
3881 including in the link (i.e., DEF_REGULAR is set). At
3882 this point we have not seen all the input files, so it is
3883 possible that DEF_REGULAR is not set now but will be set
3884 later (it is never cleared). In case of a weak definition,
3885 DEF_REGULAR may be cleared later by a strong definition in
3886 a shared library. We account for that possibility below by
3887 storing information in the relocs_copied field of the hash
3888 table entry. A similar situation occurs when creating
3889 shared libraries and symbol visibility changes render the
3890 symbol local.
3891
3892 If on the other hand, we are creating an executable, we
3893 may need to keep relocations for symbols satisfied by a
3894 dynamic library if we manage to avoid copy relocs for the
3895 symbol. */
411e1bfb 3896 dodyn:
65f38f15
AM
3897 if ((info->shared
3898 && (sec->flags & SEC_ALLOC) != 0
411e1bfb 3899 && (MUST_BE_DYN_RELOC (r_type)
65f38f15
AM
3900 || (h != NULL
3901 && (! info->symbolic
3902 || h->root.type == bfd_link_hash_defweak
3903 || (h->elf_link_hash_flags
3904 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
3905 || (!info->shared
3906 && (sec->flags & SEC_ALLOC) != 0
3907 && h != NULL
3908 && (h->root.type == bfd_link_hash_defweak
3909 || (h->elf_link_hash_flags
3910 & ELF_LINK_HASH_DEF_REGULAR) == 0)))
5bd4f169 3911 {
ec338859
AM
3912 struct ppc_dyn_relocs *p;
3913 struct ppc_dyn_relocs **head;
3914
65f38f15
AM
3915 /* We must copy these reloc types into the output file.
3916 Create a reloc section in dynobj and make room for
3917 this reloc. */
5bd4f169
AM
3918 if (sreloc == NULL)
3919 {
3920 const char *name;
65f38f15 3921 bfd *dynobj;
5bd4f169
AM
3922
3923 name = (bfd_elf_string_from_elf_section
3924 (abfd,
3925 elf_elfheader (abfd)->e_shstrndx,
3926 elf_section_data (sec)->rel_hdr.sh_name));
3927 if (name == NULL)
b34976b6 3928 return FALSE;
5bd4f169 3929
65f38f15
AM
3930 if (strncmp (name, ".rela", 5) != 0
3931 || strcmp (bfd_get_section_name (abfd, sec),
3932 name + 5) != 0)
3933 {
3934 (*_bfd_error_handler)
3935 (_("%s: bad relocation section name `%s\'"),
3936 bfd_archive_filename (abfd), name);
5d1634d7 3937 bfd_set_error (bfd_error_bad_value);
65f38f15
AM
3938 }
3939
65f38f15 3940 dynobj = htab->elf.dynobj;
5bd4f169
AM
3941 sreloc = bfd_get_section_by_name (dynobj, name);
3942 if (sreloc == NULL)
3943 {
3944 flagword flags;
3945
3946 sreloc = bfd_make_section (dynobj, name);
3947 flags = (SEC_HAS_CONTENTS | SEC_READONLY
3948 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3949 if ((sec->flags & SEC_ALLOC) != 0)
3950 flags |= SEC_ALLOC | SEC_LOAD;
3951 if (sreloc == NULL
3952 || ! bfd_set_section_flags (dynobj, sreloc, flags)
65f38f15 3953 || ! bfd_set_section_alignment (dynobj, sreloc, 3))
b34976b6 3954 return FALSE;
5bd4f169 3955 }
65f38f15 3956 elf_section_data (sec)->sreloc = sreloc;
5bd4f169
AM
3957 }
3958
65f38f15
AM
3959 /* If this is a global symbol, we count the number of
3960 relocations we need for this symbol. */
3961 if (h != NULL)
3962 {
ec338859 3963 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
65f38f15
AM
3964 }
3965 else
3966 {
ec338859
AM
3967 /* Track dynamic relocs needed for local syms too.
3968 We really need local syms available to do this
3969 easily. Oh well. */
3970
3971 asection *s;
3972 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
3973 sec, r_symndx);
3974 if (s == NULL)
b34976b6 3975 return FALSE;
ec338859
AM
3976
3977 head = ((struct ppc_dyn_relocs **)
3978 &elf_section_data (s)->local_dynrel);
65f38f15 3979 }
ec338859
AM
3980
3981 p = *head;
3982 if (p == NULL || p->sec != sec)
3983 {
3984 p = ((struct ppc_dyn_relocs *)
3985 bfd_alloc (htab->elf.dynobj,
3986 (bfd_size_type) sizeof *p));
3987 if (p == NULL)
b34976b6 3988 return FALSE;
ec338859
AM
3989 p->next = *head;
3990 *head = p;
3991 p->sec = sec;
3992 p->count = 0;
3993 p->pc_count = 0;
3994 }
3995
3996 p->count += 1;
411e1bfb 3997 if (!MUST_BE_DYN_RELOC (r_type))
ec338859 3998 p->pc_count += 1;
65f38f15 3999 }
5bd4f169 4000 break;
65f38f15
AM
4001
4002 default:
96e0dda4 4003 break;
5bd4f169
AM
4004 }
4005 }
4006
b34976b6 4007 return TRUE;
5bd4f169
AM
4008}
4009
4010/* Return the section that should be marked against GC for a given
4011 relocation. */
4012
4013static asection *
1e2f5b6e
AM
4014ppc64_elf_gc_mark_hook (sec, info, rel, h, sym)
4015 asection *sec;
5bd4f169
AM
4016 struct bfd_link_info *info ATTRIBUTE_UNUSED;
4017 Elf_Internal_Rela *rel;
4018 struct elf_link_hash_entry *h;
4019 Elf_Internal_Sym *sym;
4020{
1e2f5b6e
AM
4021 asection *rsec = NULL;
4022
5bd4f169
AM
4023 if (h != NULL)
4024 {
04c9666a 4025 enum elf_ppc64_reloc_type r_type;
1e2f5b6e 4026 struct ppc_link_hash_entry *fdh;
a33d1f77 4027
04c9666a 4028 r_type = (enum elf_ppc64_reloc_type) ELF64_R_TYPE (rel->r_info);
a33d1f77 4029 switch (r_type)
5bd4f169
AM
4030 {
4031 case R_PPC64_GNU_VTINHERIT:
4032 case R_PPC64_GNU_VTENTRY:
4033 break;
4034
4035 default:
4036 switch (h->root.type)
4037 {
4038 case bfd_link_hash_defined:
4039 case bfd_link_hash_defweak:
1e2f5b6e
AM
4040 fdh = (struct ppc_link_hash_entry *) h;
4041
4042 /* Function descriptor syms cause the associated
4043 function code sym section to be marked. */
4044 if (fdh->is_func_descriptor)
4045 rsec = fdh->oh->root.u.def.section;
4046
4047 /* Function entry syms return NULL if they are in .opd
4048 and are not ._start (or others undefined on the ld
4049 command line). Thus we avoid marking all function
b34976b6 4050 sections, as all functions are referenced in .opd. */
1e2f5b6e
AM
4051 else if ((fdh->oh != NULL
4052 && ((struct ppc_link_hash_entry *) fdh->oh)->is_entry)
f0abc2a1 4053 || ppc64_elf_section_data (sec)->opd.func_sec == NULL)
1e2f5b6e
AM
4054 rsec = h->root.u.def.section;
4055 break;
5bd4f169
AM
4056
4057 case bfd_link_hash_common:
1e2f5b6e
AM
4058 rsec = h->root.u.c.p->section;
4059 break;
5bd4f169
AM
4060
4061 default:
4062 break;
4063 }
4064 }
4065 }
4066 else
4067 {
1e2f5b6e
AM
4068 asection **opd_sym_section;
4069
4070 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
f0abc2a1 4071 opd_sym_section = ppc64_elf_section_data (rsec)->opd.func_sec;
1e2f5b6e
AM
4072 if (opd_sym_section != NULL)
4073 rsec = opd_sym_section[sym->st_value / 24];
f0abc2a1 4074 else if (ppc64_elf_section_data (sec)->opd.func_sec != NULL)
1e2f5b6e 4075 rsec = NULL;
5bd4f169
AM
4076 }
4077
1e2f5b6e 4078 return rsec;
5bd4f169
AM
4079}
4080
65f38f15
AM
4081/* Update the .got, .plt. and dynamic reloc reference counts for the
4082 section being removed. */
5bd4f169 4083
b34976b6 4084static bfd_boolean
5bd4f169
AM
4085ppc64_elf_gc_sweep_hook (abfd, info, sec, relocs)
4086 bfd *abfd;
411e1bfb 4087 struct bfd_link_info *info;
5bd4f169
AM
4088 asection *sec;
4089 const Elf_Internal_Rela *relocs;
4090{
411e1bfb 4091 struct ppc_link_hash_table *htab;
5bd4f169
AM
4092 Elf_Internal_Shdr *symtab_hdr;
4093 struct elf_link_hash_entry **sym_hashes;
411e1bfb 4094 struct got_entry **local_got_ents;
5bd4f169 4095 const Elf_Internal_Rela *rel, *relend;
5bd4f169 4096
ec338859
AM
4097 elf_section_data (sec)->local_dynrel = NULL;
4098
411e1bfb 4099 htab = ppc_hash_table (info);
5bd4f169
AM
4100 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4101 sym_hashes = elf_sym_hashes (abfd);
411e1bfb 4102 local_got_ents = elf_local_got_ents (abfd);
5bd4f169
AM
4103
4104 relend = relocs + sec->reloc_count;
4105 for (rel = relocs; rel < relend; rel++)
a33d1f77
AM
4106 {
4107 unsigned long r_symndx;
04c9666a 4108 enum elf_ppc64_reloc_type r_type;
a33d1f77 4109 struct elf_link_hash_entry *h;
411e1bfb 4110 char tls_type = 0;
5bd4f169 4111
a33d1f77 4112 r_symndx = ELF64_R_SYM (rel->r_info);
04c9666a 4113 r_type = (enum elf_ppc64_reloc_type) ELF64_R_TYPE (rel->r_info);
a33d1f77
AM
4114 switch (r_type)
4115 {
411e1bfb
AM
4116 case R_PPC64_GOT_TLSLD16:
4117 case R_PPC64_GOT_TLSLD16_LO:
4118 case R_PPC64_GOT_TLSLD16_HI:
4119 case R_PPC64_GOT_TLSLD16_HA:
4120 htab->tlsld_got.refcount -= 1;
951fd09b 4121 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
4122 goto dogot;
4123
4124 case R_PPC64_GOT_TLSGD16:
4125 case R_PPC64_GOT_TLSGD16_LO:
4126 case R_PPC64_GOT_TLSGD16_HI:
4127 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 4128 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
4129 goto dogot;
4130
4131 case R_PPC64_GOT_TPREL16_DS:
4132 case R_PPC64_GOT_TPREL16_LO_DS:
4133 case R_PPC64_GOT_TPREL16_HI:
4134 case R_PPC64_GOT_TPREL16_HA:
4135 tls_type = TLS_TLS | TLS_TPREL;
4136 goto dogot;
4137
4138 case R_PPC64_GOT_DTPREL16_DS:
4139 case R_PPC64_GOT_DTPREL16_LO_DS:
4140 case R_PPC64_GOT_DTPREL16_HI:
4141 case R_PPC64_GOT_DTPREL16_HA:
4142 tls_type = TLS_TLS | TLS_DTPREL;
4143 goto dogot;
4144
a33d1f77
AM
4145 case R_PPC64_GOT16:
4146 case R_PPC64_GOT16_DS:
4147 case R_PPC64_GOT16_HA:
4148 case R_PPC64_GOT16_HI:
4149 case R_PPC64_GOT16_LO:
4150 case R_PPC64_GOT16_LO_DS:
411e1bfb
AM
4151 dogot:
4152 {
4153 struct got_entry *ent;
4154
4155 if (r_symndx >= symtab_hdr->sh_info)
4156 {
4157 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4158 ent = h->got.glist;
4159 }
4160 else
4161 ent = local_got_ents[r_symndx];
4162
4163 for (; ent != NULL; ent = ent->next)
4164 if (ent->addend == rel->r_addend
4165 && ent->tls_type == tls_type)
4166 break;
4167 if (ent == NULL)
4168 abort ();
4169 if (ent->got.refcount > 0)
4170 ent->got.refcount -= 1;
4171 }
a33d1f77 4172 break;
65f38f15 4173
a33d1f77
AM
4174 case R_PPC64_PLT16_HA:
4175 case R_PPC64_PLT16_HI:
4176 case R_PPC64_PLT16_LO:
4177 case R_PPC64_PLT32:
4178 case R_PPC64_PLT64:
721956f4
AM
4179 case R_PPC64_REL14:
4180 case R_PPC64_REL14_BRNTAKEN:
4181 case R_PPC64_REL14_BRTAKEN:
5d1634d7
AM
4182 case R_PPC64_REL24:
4183 if (r_symndx >= symtab_hdr->sh_info)
4184 {
411e1bfb
AM
4185 struct plt_entry *ent;
4186
5d1634d7 4187 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
411e1bfb
AM
4188 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
4189 if (ent->addend == rel->r_addend)
4190 break;
4191 if (ent == NULL)
4192 abort ();
4193 if (ent->plt.refcount > 0)
4194 ent->plt.refcount -= 1;
5d1634d7 4195 }
e86ce104 4196 break;
5d1634d7 4197
04c9666a 4198 case R_PPC64_REL30:
a33d1f77
AM
4199 case R_PPC64_REL32:
4200 case R_PPC64_REL64:
4201 if (r_symndx >= symtab_hdr->sh_info)
4202 {
4203 struct ppc_link_hash_entry *eh;
4204 struct ppc_dyn_relocs **pp;
4205 struct ppc_dyn_relocs *p;
65f38f15 4206
a33d1f77
AM
4207 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4208 eh = (struct ppc_link_hash_entry *) h;
65f38f15 4209
a33d1f77
AM
4210 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
4211 if (p->sec == sec)
4212 {
4213 p->pc_count -= 1;
4214 p->count -= 1;
4215 if (p->count == 0)
4216 *pp = p->next;
4217 break;
4218 }
4219 }
4220 break;
65f38f15 4221
411e1bfb
AM
4222 case R_PPC64_DTPMOD64:
4223 case R_PPC64_DTPREL64:
4224 case R_PPC64_TPREL64:
a33d1f77
AM
4225 case R_PPC64_ADDR14:
4226 case R_PPC64_ADDR14_BRNTAKEN:
4227 case R_PPC64_ADDR14_BRTAKEN:
4228 case R_PPC64_ADDR16:
4229 case R_PPC64_ADDR16_DS:
4230 case R_PPC64_ADDR16_HA:
4231 case R_PPC64_ADDR16_HI:
4232 case R_PPC64_ADDR16_HIGHER:
4233 case R_PPC64_ADDR16_HIGHERA:
4234 case R_PPC64_ADDR16_HIGHEST:
4235 case R_PPC64_ADDR16_HIGHESTA:
4236 case R_PPC64_ADDR16_LO:
4237 case R_PPC64_ADDR16_LO_DS:
4238 case R_PPC64_ADDR24:
a33d1f77
AM
4239 case R_PPC64_ADDR32:
4240 case R_PPC64_ADDR64:
4241 case R_PPC64_UADDR16:
4242 case R_PPC64_UADDR32:
4243 case R_PPC64_UADDR64:
4244 case R_PPC64_TOC:
4245 if (r_symndx >= symtab_hdr->sh_info)
4246 {
4247 struct ppc_link_hash_entry *eh;
4248 struct ppc_dyn_relocs **pp;
4249 struct ppc_dyn_relocs *p;
65f38f15 4250
a33d1f77
AM
4251 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4252 eh = (struct ppc_link_hash_entry *) h;
4253
4254 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
4255 if (p->sec == sec)
4256 {
4257 p->count -= 1;
4258 if (p->count == 0)
4259 *pp = p->next;
4260 break;
4261 }
4262 }
4263 break;
5bd4f169 4264
a33d1f77
AM
4265 default:
4266 break;
4267 }
4268 }
b34976b6 4269 return TRUE;
5bd4f169
AM
4270}
4271
e86ce104
AM
4272/* Called via elf_link_hash_traverse to transfer dynamic linking
4273 information on function code symbol entries to their corresponding
4274 function descriptor symbol entries. */
b34976b6 4275static bfd_boolean
e86ce104 4276func_desc_adjust (h, inf)
5bd4f169 4277 struct elf_link_hash_entry *h;
e86ce104 4278 PTR inf;
5bd4f169 4279{
e86ce104 4280 struct bfd_link_info *info;
65f38f15 4281 struct ppc_link_hash_table *htab;
411e1bfb 4282 struct plt_entry *ent;
5bd4f169 4283
e92d460e 4284 if (h->root.type == bfd_link_hash_indirect)
b34976b6 4285 return TRUE;
e86ce104 4286
e92d460e
AM
4287 if (h->root.type == bfd_link_hash_warning)
4288 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4289
e86ce104 4290 info = (struct bfd_link_info *) inf;
65f38f15 4291 htab = ppc_hash_table (info);
5bd4f169 4292
e86ce104
AM
4293 /* If this is a function code symbol, transfer dynamic linking
4294 information to the function descriptor symbol. */
4295 if (!((struct ppc_link_hash_entry *) h)->is_func)
b34976b6 4296 return TRUE;
e86ce104 4297
805fc799
AM
4298 if (h->root.type == bfd_link_hash_undefweak
4299 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR))
b34976b6 4300 htab->have_undefweak = TRUE;
805fc799 4301
411e1bfb
AM
4302 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
4303 if (ent->plt.refcount > 0)
4304 break;
4305 if (ent != NULL
e86ce104
AM
4306 && h->root.root.string[0] == '.'
4307 && h->root.root.string[1] != '\0')
5bd4f169 4308 {
721956f4 4309 struct elf_link_hash_entry *fdh = ((struct ppc_link_hash_entry *) h)->oh;
b34976b6 4310 bfd_boolean force_local;
5bd4f169 4311
648cca2c
AM
4312 /* Find the corresponding function descriptor symbol. Create it
4313 as undefined if necessary. */
5bd4f169 4314
721956f4
AM
4315 if (fdh == NULL)
4316 fdh = elf_link_hash_lookup (&htab->elf, h->root.root.string + 1,
b34976b6 4317 FALSE, FALSE, TRUE);
5bd4f169 4318
d926e03d
AM
4319 if (fdh == NULL
4320 && info->shared
4321 && (h->root.type == bfd_link_hash_undefined
4322 || h->root.type == bfd_link_hash_undefweak))
648cca2c
AM
4323 {
4324 bfd *abfd;
4325 asymbol *newsym;
14a793b2 4326 struct bfd_link_hash_entry *bh;
648cca2c 4327
d926e03d 4328 abfd = h->root.u.undef.abfd;
648cca2c
AM
4329 newsym = bfd_make_empty_symbol (abfd);
4330 newsym->name = h->root.root.string + 1;
4331 newsym->section = bfd_und_section_ptr;
4332 newsym->value = 0;
4333 newsym->flags = BSF_OBJECT;
4334 if (h->root.type == bfd_link_hash_undefweak)
4335 newsym->flags |= BSF_WEAK;
4336
14a793b2 4337 bh = &fdh->root;
648cca2c
AM
4338 if ( !(_bfd_generic_link_add_one_symbol
4339 (info, abfd, newsym->name, newsym->flags,
b34976b6 4340 newsym->section, newsym->value, NULL, FALSE, FALSE, &bh)))
648cca2c 4341 {
b34976b6 4342 return FALSE;
648cca2c 4343 }
14a793b2 4344 fdh = (struct elf_link_hash_entry *) bh;
d926e03d 4345 fdh->elf_link_hash_flags &= ~ELF_LINK_NON_ELF;
648cca2c
AM
4346 }
4347
e86ce104
AM
4348 if (fdh != NULL
4349 && (fdh->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0
4350 && (info->shared
4351 || (fdh->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
4352 || (fdh->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0))
4353 {
4354 if (fdh->dynindx == -1)
4355 if (! bfd_elf64_link_record_dynamic_symbol (info, fdh))
b34976b6 4356 return FALSE;
e86ce104
AM
4357 fdh->elf_link_hash_flags |= (h->elf_link_hash_flags
4358 & (ELF_LINK_HASH_REF_REGULAR
4359 | ELF_LINK_HASH_REF_DYNAMIC
4360 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
4361 | ELF_LINK_NON_GOT_REF));
e1fa50e7
AM
4362 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
4363 {
411e1bfb 4364 fdh->plt.plist = h->plt.plist;
e1fa50e7
AM
4365 fdh->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
4366 }
e86ce104 4367 ((struct ppc_link_hash_entry *) fdh)->is_func_descriptor = 1;
721956f4 4368 ((struct ppc_link_hash_entry *) fdh)->oh = h;
721956f4 4369 ((struct ppc_link_hash_entry *) h)->oh = fdh;
e86ce104
AM
4370 }
4371
4372 /* Now that the info is on the function descriptor, clear the
4373 function code sym info. Any function code syms for which we
4374 don't have a definition in a regular file, we force local.
4375 This prevents a shared library from exporting syms that have
4376 been imported from another library. Function code syms that
4377 are really in the library we must leave global to prevent the
e1fa50e7 4378 linker dragging in a definition from a static library. */
ea1e4b0d
AM
4379 force_local = (info->shared
4380 && ((h->elf_link_hash_flags
4381 & ELF_LINK_HASH_DEF_REGULAR) == 0
4382 || fdh == NULL
4383 || (fdh->elf_link_hash_flags
4384 & ELF_LINK_HASH_DEF_REGULAR) == 0
4385 || (fdh->elf_link_hash_flags
4386 & ELF_LINK_FORCED_LOCAL) != 0));
e86ce104
AM
4387 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
4388 }
4389
b34976b6 4390 return TRUE;
e86ce104 4391}
40b8271b 4392
82bd7b59
AM
4393#define MIN_SAVE_FPR 14
4394#define MAX_SAVE_FPR 31
4395
e86ce104 4396/* Called near the start of bfd_elf_size_dynamic_sections. We use
82bd7b59
AM
4397 this hook to a) provide some gcc support functions, and b) transfer
4398 dynamic linking information gathered so far on function code symbol
4399 entries, to their corresponding function descriptor symbol entries. */
b34976b6 4400static bfd_boolean
e86ce104
AM
4401ppc64_elf_func_desc_adjust (obfd, info)
4402 bfd *obfd ATTRIBUTE_UNUSED;
4403 struct bfd_link_info *info;
4404{
4405 struct ppc_link_hash_table *htab;
82bd7b59
AM
4406 unsigned int lowest_savef = MAX_SAVE_FPR + 2;
4407 unsigned int lowest_restf = MAX_SAVE_FPR + 2;
4408 unsigned int i;
4409 struct elf_link_hash_entry *h;
721956f4 4410 bfd_byte *p;
82bd7b59 4411 char sym[10];
e86ce104
AM
4412
4413 htab = ppc_hash_table (info);
82bd7b59
AM
4414
4415 if (htab->sfpr == NULL)
4416 /* We don't have any relocs. */
b34976b6 4417 return TRUE;
82bd7b59
AM
4418
4419 /* First provide any missing ._savef* and ._restf* functions. */
4420 memcpy (sym, "._savef14", 10);
4421 for (i = MIN_SAVE_FPR; i <= MAX_SAVE_FPR; i++)
4422 {
4423 sym[7] = i / 10 + '0';
4424 sym[8] = i % 10 + '0';
b34976b6 4425 h = elf_link_hash_lookup (&htab->elf, sym, FALSE, FALSE, TRUE);
82bd7b59
AM
4426 if (h != NULL
4427 && h->root.type == bfd_link_hash_undefined)
4428 {
4429 if (lowest_savef > i)
4430 lowest_savef = i;
4431 h->root.type = bfd_link_hash_defined;
4432 h->root.u.def.section = htab->sfpr;
4433 h->root.u.def.value = (i - lowest_savef) * 4;
4434 h->type = STT_FUNC;
4435 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
4436 _bfd_elf_link_hash_hide_symbol (info, h, info->shared);
4437 }
4438 }
4439
4440 memcpy (sym, "._restf14", 10);
4441 for (i = MIN_SAVE_FPR; i <= MAX_SAVE_FPR; i++)
4442 {
4443 sym[7] = i / 10 + '0';
4444 sym[8] = i % 10 + '0';
b34976b6 4445 h = elf_link_hash_lookup (&htab->elf, sym, FALSE, FALSE, TRUE);
82bd7b59
AM
4446 if (h != NULL
4447 && h->root.type == bfd_link_hash_undefined)
4448 {
4449 if (lowest_restf > i)
4450 lowest_restf = i;
4451 h->root.type = bfd_link_hash_defined;
4452 h->root.u.def.section = htab->sfpr;
4453 h->root.u.def.value = ((MAX_SAVE_FPR + 2 - lowest_savef) * 4
4454 + (i - lowest_restf) * 4);
4455 h->type = STT_FUNC;
4456 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
4457 _bfd_elf_link_hash_hide_symbol (info, h, info->shared);
4458 }
4459 }
4460
805fc799
AM
4461 elf_link_hash_traverse (&htab->elf, func_desc_adjust, (PTR) info);
4462
82bd7b59
AM
4463 htab->sfpr->_raw_size = ((MAX_SAVE_FPR + 2 - lowest_savef) * 4
4464 + (MAX_SAVE_FPR + 2 - lowest_restf) * 4);
4465
4466 if (htab->sfpr->_raw_size == 0)
805fc799
AM
4467 {
4468 if (!htab->have_undefweak)
4469 {
4470 _bfd_strip_section_from_output (info, htab->sfpr);
b34976b6 4471 return TRUE;
805fc799
AM
4472 }
4473
4474 htab->sfpr->_raw_size = 4;
4475 }
82bd7b59 4476
721956f4
AM
4477 p = (bfd_byte *) bfd_alloc (htab->elf.dynobj, htab->sfpr->_raw_size);
4478 if (p == NULL)
b34976b6 4479 return FALSE;
721956f4
AM
4480 htab->sfpr->contents = p;
4481
4482 for (i = lowest_savef; i <= MAX_SAVE_FPR; i++)
4483 {
4484 unsigned int fpr = i << 21;
4485 unsigned int stackoff = (1 << 16) - (MAX_SAVE_FPR + 1 - i) * 8;
4486 bfd_put_32 (htab->elf.dynobj, STFD_FR0_0R1 + fpr + stackoff, p);
4487 p += 4;
4488 }
4489 if (lowest_savef <= MAX_SAVE_FPR)
4490 {
82bd7b59
AM
4491 bfd_put_32 (htab->elf.dynobj, BLR, p);
4492 p += 4;
721956f4 4493 }
82bd7b59 4494
721956f4
AM
4495 for (i = lowest_restf; i <= MAX_SAVE_FPR; i++)
4496 {
4497 unsigned int fpr = i << 21;
4498 unsigned int stackoff = (1 << 16) - (MAX_SAVE_FPR + 1 - i) * 8;
4499 bfd_put_32 (htab->elf.dynobj, LFD_FR0_0R1 + fpr + stackoff, p);
82bd7b59
AM
4500 p += 4;
4501 }
721956f4
AM
4502 if (lowest_restf <= MAX_SAVE_FPR
4503 || htab->sfpr->_raw_size == 4)
4504 {
4505 bfd_put_32 (htab->elf.dynobj, BLR, p);
4506 }
82bd7b59 4507
b34976b6 4508 return TRUE;
e86ce104
AM
4509}
4510
4511/* Adjust a symbol defined by a dynamic object and referenced by a
4512 regular object. The current definition is in some section of the
4513 dynamic object, but we're not including those sections. We have to
4514 change the definition to something the rest of the link can
4515 understand. */
4516
b34976b6 4517static bfd_boolean
e86ce104
AM
4518ppc64_elf_adjust_dynamic_symbol (info, h)
4519 struct bfd_link_info *info;
4520 struct elf_link_hash_entry *h;
4521{
4522 struct ppc_link_hash_table *htab;
4523 struct ppc_link_hash_entry * eh;
4524 struct ppc_dyn_relocs *p;
4525 asection *s;
4526 unsigned int power_of_two;
4527
4528 htab = ppc_hash_table (info);
4529
4530 /* Deal with function syms. */
4531 if (h->type == STT_FUNC
4532 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
4533 {
4534 /* Clear procedure linkage table information for any symbol that
4535 won't need a .plt entry. */
411e1bfb
AM
4536 struct plt_entry *ent;
4537 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
4538 if (ent->plt.refcount > 0)
4539 break;
e86ce104 4540 if (!((struct ppc_link_hash_entry *) h)->is_func_descriptor
411e1bfb 4541 || ent == NULL
e86ce104
AM
4542 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0
4543 || (! info->shared
4544 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
4545 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0))
40b8271b 4546 {
411e1bfb 4547 h->plt.plist = NULL;
40b8271b 4548 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
40b8271b 4549 }
b34976b6 4550 return TRUE;
5bd4f169 4551 }
bbd7ec4a 4552 else
411e1bfb 4553 h->plt.plist = NULL;
5bd4f169
AM
4554
4555 /* If this is a weak symbol, and there is a real definition, the
4556 processor independent code will have arranged for us to see the
4557 real definition first, and we can just use the same value. */
4558 if (h->weakdef != NULL)
4559 {
4560 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
4561 || h->weakdef->root.type == bfd_link_hash_defweak);
4562 h->root.u.def.section = h->weakdef->root.u.def.section;
4563 h->root.u.def.value = h->weakdef->root.u.def.value;
b34976b6 4564 return TRUE;
5bd4f169
AM
4565 }
4566
4567 /* This is a reference to a symbol defined by a dynamic object which
4568 is not a function. */
4569
4570 /* If we are creating a shared library, we must presume that the
4571 only references to the symbol are via the global offset table.
4572 For such cases we need not do anything here; the relocations will
4573 be handled correctly by relocate_section. */
4574 if (info->shared)
b34976b6 4575 return TRUE;
5bd4f169 4576
65f38f15
AM
4577 /* If there are no references to this symbol that do not use the
4578 GOT, we don't need to generate a copy reloc. */
4579 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
b34976b6 4580 return TRUE;
65f38f15
AM
4581
4582 eh = (struct ppc_link_hash_entry *) h;
4583 for (p = eh->dyn_relocs; p != NULL; p = p->next)
4584 {
4585 s = p->sec->output_section;
4586 if (s != NULL && (s->flags & SEC_READONLY) != 0)
4587 break;
4588 }
4589
4590 /* If we didn't find any dynamic relocs in read-only sections, then
5d1634d7 4591 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
65f38f15
AM
4592 if (p == NULL)
4593 {
4594 h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
b34976b6 4595 return TRUE;
65f38f15
AM
4596 }
4597
5bd4f169
AM
4598 /* We must allocate the symbol in our .dynbss section, which will
4599 become part of the .bss section of the executable. There will be
4600 an entry for this symbol in the .dynsym section. The dynamic
4601 object will contain position independent code, so all references
4602 from the dynamic object to this symbol will go through the global
4603 offset table. The dynamic linker will use the .dynsym entry to
4604 determine the address it must put in the global offset table, so
4605 both the dynamic object and the regular object will refer to the
4606 same memory location for the variable. */
5bd4f169 4607
04c9666a
AM
4608 /* We must generate a R_PPC64_COPY reloc to tell the dynamic linker
4609 to copy the initial value out of the dynamic object and into the
5bd4f169
AM
4610 runtime process image. We need to remember the offset into the
4611 .rela.bss section we are going to use. */
4612 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
4613 {
65f38f15 4614 htab->srelbss->_raw_size += sizeof (Elf64_External_Rela);
5bd4f169
AM
4615 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
4616 }
4617
4618 /* We need to figure out the alignment required for this symbol. I
4619 have no idea how ELF linkers handle this. */
4620 power_of_two = bfd_log2 (h->size);
4621 if (power_of_two > 4)
4622 power_of_two = 4;
4623
4624 /* Apply the required alignment. */
65f38f15
AM
4625 s = htab->sdynbss;
4626 s->_raw_size = BFD_ALIGN (s->_raw_size, (bfd_size_type) (1 << power_of_two));
4627 if (power_of_two > bfd_get_section_alignment (htab->elf.dynobj, s))
5bd4f169 4628 {
65f38f15 4629 if (! bfd_set_section_alignment (htab->elf.dynobj, s, power_of_two))
b34976b6 4630 return FALSE;
5bd4f169
AM
4631 }
4632
4633 /* Define the symbol as being at this point in the section. */
4634 h->root.u.def.section = s;
4635 h->root.u.def.value = s->_raw_size;
4636
4637 /* Increment the section size to make room for the symbol. */
4638 s->_raw_size += h->size;
4639
b34976b6 4640 return TRUE;
5bd4f169
AM
4641}
4642
e86ce104
AM
4643/* If given a function descriptor symbol, hide both the function code
4644 sym and the descriptor. */
4645static void
4646ppc64_elf_hide_symbol (info, h, force_local)
4647 struct bfd_link_info *info;
4648 struct elf_link_hash_entry *h;
b34976b6 4649 bfd_boolean force_local;
e86ce104
AM
4650{
4651 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
4652
4653 if (((struct ppc_link_hash_entry *) h)->is_func_descriptor)
4654 {
721956f4 4655 struct elf_link_hash_entry *fh = ((struct ppc_link_hash_entry *) h)->oh;
e86ce104 4656
721956f4 4657 if (fh == NULL)
d1329ca3
AM
4658 {
4659 const char *p, *q;
4660 struct ppc_link_hash_table *htab;
4661 char save;
4662
4663 /* We aren't supposed to use alloca in BFD because on
4664 systems which do not have alloca the version in libiberty
4665 calls xmalloc, which might cause the program to crash
4666 when it runs out of memory. This function doesn't have a
4667 return status, so there's no way to gracefully return an
4668 error. So cheat. We know that string[-1] can be safely
4669 dereferenced; It's either a string in an ELF string
4670 table, or allocated in an objalloc structure. */
4671
4672 p = h->root.root.string - 1;
4673 save = *p;
4674 *(char *) p = '.';
4675 htab = ppc_hash_table (info);
b34976b6 4676 fh = elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
d1329ca3
AM
4677 *(char *) p = save;
4678
4679 /* Unfortunately, if it so happens that the string we were
4680 looking for was allocated immediately before this string,
4681 then we overwrote the string terminator. That's the only
4682 reason the lookup should fail. */
4683 if (fh == NULL)
4684 {
4685 q = h->root.root.string + strlen (h->root.root.string);
4686 while (q >= h->root.root.string && *q == *p)
4687 --q, --p;
4688 if (q < h->root.root.string && *p == '.')
b34976b6 4689 fh = elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
d1329ca3
AM
4690 }
4691 if (fh != NULL)
4692 {
4693 ((struct ppc_link_hash_entry *) h)->oh = fh;
4694 ((struct ppc_link_hash_entry *) fh)->oh = h;
4695 }
4696 }
e86ce104
AM
4697 if (fh != NULL)
4698 _bfd_elf_link_hash_hide_symbol (info, fh, force_local);
4699 }
4700}
4701
411e1bfb 4702static bfd_boolean
e7b938ca 4703get_sym_h (hp, symp, symsecp, tls_maskp, locsymsp, r_symndx, ibfd)
411e1bfb
AM
4704 struct elf_link_hash_entry **hp;
4705 Elf_Internal_Sym **symp;
4706 asection **symsecp;
e7b938ca 4707 char **tls_maskp;
411e1bfb
AM
4708 Elf_Internal_Sym **locsymsp;
4709 unsigned long r_symndx;
4710 bfd *ibfd;
4711{
4712 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
4713
4714 if (r_symndx >= symtab_hdr->sh_info)
4715 {
4716 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
4717 struct elf_link_hash_entry *h;
4718
4719 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4720 while (h->root.type == bfd_link_hash_indirect
4721 || h->root.type == bfd_link_hash_warning)
4722 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4723
4724 if (hp != NULL)
4725 *hp = h;
4726
4727 if (symp != NULL)
4728 *symp = NULL;
4729
4730 if (symsecp != NULL)
4731 {
4732 asection *symsec = NULL;
4733 if (h->root.type == bfd_link_hash_defined
4734 || h->root.type == bfd_link_hash_defweak)
4735 symsec = h->root.u.def.section;
4736 *symsecp = symsec;
4737 }
4738
e7b938ca 4739 if (tls_maskp != NULL)
411e1bfb
AM
4740 {
4741 struct ppc_link_hash_entry *eh;
4742
4743 eh = (struct ppc_link_hash_entry *) h;
e7b938ca 4744 *tls_maskp = &eh->tls_mask;
411e1bfb
AM
4745 }
4746 }
4747 else
4748 {
4749 Elf_Internal_Sym *sym;
4750 Elf_Internal_Sym *locsyms = *locsymsp;
4751
4752 if (locsyms == NULL)
4753 {
4754 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
4755 if (locsyms == NULL)
4756 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
4757 symtab_hdr->sh_info,
4758 0, NULL, NULL, NULL);
4759 if (locsyms == NULL)
4760 return FALSE;
4761 *locsymsp = locsyms;
4762 }
4763 sym = locsyms + r_symndx;
4764
4765 if (hp != NULL)
4766 *hp = NULL;
4767
4768 if (symp != NULL)
4769 *symp = sym;
4770
4771 if (symsecp != NULL)
4772 {
4773 asection *symsec = NULL;
4774 if ((sym->st_shndx != SHN_UNDEF
4775 && sym->st_shndx < SHN_LORESERVE)
4776 || sym->st_shndx > SHN_HIRESERVE)
4777 symsec = bfd_section_from_elf_index (ibfd, sym->st_shndx);
4778 *symsecp = symsec;
4779 }
4780
e7b938ca 4781 if (tls_maskp != NULL)
411e1bfb
AM
4782 {
4783 struct got_entry **lgot_ents;
e7b938ca 4784 char *tls_mask;
411e1bfb 4785
e7b938ca 4786 tls_mask = NULL;
411e1bfb
AM
4787 lgot_ents = elf_local_got_ents (ibfd);
4788 if (lgot_ents != NULL)
4789 {
e7b938ca
AM
4790 char *lgot_masks = (char *) (lgot_ents + symtab_hdr->sh_info);
4791 tls_mask = &lgot_masks[r_symndx];
411e1bfb 4792 }
e7b938ca 4793 *tls_maskp = tls_mask;
411e1bfb
AM
4794 }
4795 }
4796 return TRUE;
4797}
4798
e7b938ca 4799/* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
951fd09b
AM
4800 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
4801 type suitable for optimization, and 1 otherwise. */
4802
4803static int
e7b938ca
AM
4804get_tls_mask (tls_maskp, locsymsp, rel, ibfd)
4805 char **tls_maskp;
411e1bfb
AM
4806 Elf_Internal_Sym **locsymsp;
4807 const Elf_Internal_Rela *rel;
4808 bfd *ibfd;
4809{
4810 unsigned long r_symndx;
951fd09b 4811 unsigned int next_r;
411e1bfb
AM
4812 struct elf_link_hash_entry *h;
4813 Elf_Internal_Sym *sym;
4814 asection *sec;
4815 bfd_vma off;
4816
4817 r_symndx = ELF64_R_SYM (rel->r_info);
e7b938ca 4818 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
951fd09b 4819 return 0;
411e1bfb 4820
e7b938ca 4821 if ((*tls_maskp != NULL && **tls_maskp != 0)
411e1bfb
AM
4822 || sec == NULL
4823 || ppc64_elf_section_data (sec)->t_symndx == NULL)
951fd09b 4824 return 1;
411e1bfb
AM
4825
4826 /* Look inside a TOC section too. */
4827 if (h != NULL)
4828 {
4829 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
4830 off = h->root.u.def.value;
4831 }
4832 else
4833 off = sym->st_value;
4834 off += rel->r_addend;
4835 BFD_ASSERT (off % 8 == 0);
4836 r_symndx = ppc64_elf_section_data (sec)->t_symndx[off / 8];
951fd09b 4837 next_r = ppc64_elf_section_data (sec)->t_symndx[off / 8 + 1];
e7b938ca 4838 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
951fd09b
AM
4839 return 0;
4840 if (h == NULL
4841 || h->root.type == bfd_link_hash_defined
4842 || h->root.type == bfd_link_hash_defweak)
4843 {
4844 if (next_r == (unsigned) -1)
4845 return 2;
4846 if (next_r == (unsigned) -2
4847 && (h == NULL
4848 || !(h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)))
4849 return 3;
4850 }
4851 return 1;
411e1bfb
AM
4852}
4853
b34976b6 4854bfd_boolean
d6fe2dc1 4855ppc64_elf_edit_opd (obfd, info)
1e2f5b6e
AM
4856 bfd *obfd;
4857 struct bfd_link_info *info;
4858{
4859 bfd *ibfd;
1e2f5b6e 4860
411e1bfb 4861 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
1e2f5b6e
AM
4862 {
4863 asection *sec;
4864 Elf_Internal_Rela *relstart, *rel, *relend;
4865 Elf_Internal_Shdr *symtab_hdr;
6cdc0ccc 4866 Elf_Internal_Sym *local_syms;
1e2f5b6e
AM
4867 struct elf_link_hash_entry **sym_hashes;
4868 bfd_vma offset;
d6fe2dc1 4869 bfd_size_type amt;
1e2f5b6e 4870 long *adjust;
b34976b6 4871 bfd_boolean need_edit;
1e2f5b6e
AM
4872
4873 sec = bfd_get_section_by_name (ibfd, ".opd");
4874 if (sec == NULL)
4875 continue;
4876
d6fe2dc1 4877 amt = sec->_raw_size * sizeof (long) / 24;
f0abc2a1 4878 adjust = ppc64_elf_section_data (sec)->opd.adjust;
d6fe2dc1
AM
4879 if (adjust == NULL)
4880 {
4881 /* Must be a ld -r link. ie. check_relocs hasn't been
4882 called. */
4883 adjust = (long *) bfd_zalloc (obfd, amt);
f0abc2a1 4884 ppc64_elf_section_data (sec)->opd.adjust = adjust;
d6fe2dc1
AM
4885 }
4886 memset (adjust, 0, (size_t) amt);
1e2f5b6e
AM
4887
4888 if (sec->output_section == bfd_abs_section_ptr)
4889 continue;
4890
4891 /* Look through the section relocs. */
4892 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
4893 continue;
4894
6cdc0ccc 4895 local_syms = NULL;
1e2f5b6e
AM
4896 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
4897 sym_hashes = elf_sym_hashes (ibfd);
4898
4899 /* Read the relocations. */
73374ef1 4900 relstart = _bfd_elf64_link_read_relocs (ibfd, sec, (PTR) NULL,
1e2f5b6e
AM
4901 (Elf_Internal_Rela *) NULL,
4902 info->keep_memory);
4903 if (relstart == NULL)
b34976b6 4904 return FALSE;
1e2f5b6e
AM
4905
4906 /* First run through the relocs to check they are sane, and to
4907 determine whether we need to edit this opd section. */
b34976b6 4908 need_edit = FALSE;
1e2f5b6e
AM
4909 offset = 0;
4910 relend = relstart + sec->reloc_count;
4911 for (rel = relstart; rel < relend; rel++)
4912 {
04c9666a 4913 enum elf_ppc64_reloc_type r_type;
1e2f5b6e
AM
4914 unsigned long r_symndx;
4915 asection *sym_sec;
4916 struct elf_link_hash_entry *h;
4917 Elf_Internal_Sym *sym;
4918
4919 /* .opd contains a regular array of 24 byte entries. We're
4920 only interested in the reloc pointing to a function entry
4921 point. */
04c9666a 4922 r_type = (enum elf_ppc64_reloc_type) ELF64_R_TYPE (rel->r_info);
1e2f5b6e
AM
4923 if (r_type == R_PPC64_TOC)
4924 continue;
4925
4926 if (r_type != R_PPC64_ADDR64)
4927 {
4928 (*_bfd_error_handler)
4929 (_("%s: unexpected reloc type %u in .opd section"),
4930 bfd_archive_filename (ibfd), r_type);
b34976b6 4931 need_edit = FALSE;
1e2f5b6e
AM
4932 break;
4933 }
4934
4935 if (rel + 1 >= relend)
4936 continue;
04c9666a 4937 r_type = (enum elf_ppc64_reloc_type) ELF64_R_TYPE ((rel + 1)->r_info);
1e2f5b6e
AM
4938 if (r_type != R_PPC64_TOC)
4939 continue;
4940
4941 if (rel->r_offset != offset)
4942 {
4943 /* If someone messes with .opd alignment then after a
4944 "ld -r" we might have padding in the middle of .opd.
4945 Also, there's nothing to prevent someone putting
4946 something silly in .opd with the assembler. No .opd
b34976b6 4947 optimization for them! */
1e2f5b6e
AM
4948 (*_bfd_error_handler)
4949 (_("%s: .opd is not a regular array of opd entries"),
4950 bfd_archive_filename (ibfd));
b34976b6 4951 need_edit = FALSE;
1e2f5b6e
AM
4952 break;
4953 }
4954
4955 r_symndx = ELF64_R_SYM (rel->r_info);
411e1bfb
AM
4956 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
4957 r_symndx, ibfd))
4958 goto error_free_rel;
1e2f5b6e
AM
4959
4960 if (sym_sec == NULL || sym_sec->owner == NULL)
4961 {
411e1bfb
AM
4962 const char *sym_name;
4963 if (h != NULL)
4964 sym_name = h->root.root.string;
4965 else
4966 sym_name = bfd_elf_local_sym_name (ibfd, sym);
4967
1e2f5b6e
AM
4968 (*_bfd_error_handler)
4969 (_("%s: undefined sym `%s' in .opd section"),
4970 bfd_archive_filename (ibfd),
411e1bfb 4971 sym_name);
b34976b6 4972 need_edit = FALSE;
1e2f5b6e
AM
4973 break;
4974 }
4975
51020317
AM
4976 /* opd entries are always for functions defined in the
4977 current input bfd. If the symbol isn't defined in the
4978 input bfd, then we won't be using the function in this
4979 bfd; It must be defined in a linkonce section in another
4980 bfd, or is weak. It's also possible that we are
4981 discarding the function due to a linker script /DISCARD/,
4982 which we test for via the output_section. */
4983 if (sym_sec->owner != ibfd
4984 || sym_sec->output_section == bfd_abs_section_ptr)
b34976b6 4985 need_edit = TRUE;
1e2f5b6e
AM
4986
4987 offset += 24;
4988 }
4989
4990 if (need_edit)
4991 {
4992 Elf_Internal_Rela *write_rel;
4993 bfd_byte *rptr, *wptr;
b34976b6 4994 bfd_boolean skip;
1e2f5b6e
AM
4995
4996 /* This seems a waste of time as input .opd sections are all
4997 zeros as generated by gcc, but I suppose there's no reason
4998 this will always be so. We might start putting something in
4999 the third word of .opd entries. */
5000 if ((sec->flags & SEC_IN_MEMORY) == 0)
5001 {
5002 bfd_byte *loc = bfd_alloc (ibfd, sec->_raw_size);
6cdc0ccc
AM
5003 if (loc == NULL
5004 || !bfd_get_section_contents (ibfd, sec, loc, (bfd_vma) 0,
5005 sec->_raw_size))
5006 {
5007 if (local_syms != NULL
5008 && symtab_hdr->contents != (unsigned char *) local_syms)
5009 free (local_syms);
5010 error_free_rel:
5011 if (elf_section_data (sec)->relocs != relstart)
5012 free (relstart);
b34976b6 5013 return FALSE;
6cdc0ccc 5014 }
1e2f5b6e
AM
5015 sec->contents = loc;
5016 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
5017 }
5018
5019 elf_section_data (sec)->relocs = relstart;
5020
5021 wptr = sec->contents;
5022 rptr = sec->contents;
5023 write_rel = relstart;
b34976b6 5024 skip = FALSE;
1e2f5b6e
AM
5025 offset = 0;
5026 for (rel = relstart; rel < relend; rel++)
5027 {
5028 if (rel->r_offset == offset)
5029 {
5030 unsigned long r_symndx;
5031 asection *sym_sec;
5032 struct elf_link_hash_entry *h;
5033 Elf_Internal_Sym *sym;
5034
5035 r_symndx = ELF64_R_SYM (rel->r_info);
411e1bfb
AM
5036 get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
5037 r_symndx, ibfd);
1e2f5b6e 5038
51020317
AM
5039 skip = (sym_sec->owner != ibfd
5040 || sym_sec->output_section == bfd_abs_section_ptr);
a4aa0fb7
AM
5041 if (skip)
5042 {
0be617ce 5043 if (h != NULL && sym_sec->owner == ibfd)
a4aa0fb7
AM
5044 {
5045 /* Arrange for the function descriptor sym
5046 to be dropped. */
d6fe2dc1 5047 struct ppc_link_hash_entry *fdh;
a4aa0fb7
AM
5048 struct ppc_link_hash_entry *fh;
5049
5050 fh = (struct ppc_link_hash_entry *) h;
d6fe2dc1
AM
5051 fdh = (struct ppc_link_hash_entry *) fh->oh;
5052 if (fdh == NULL)
5053 {
5054 const char *fd_name;
5055 struct ppc_link_hash_table *htab;
5056
5057 fd_name = h->root.root.string + 1;
5058 htab = ppc_hash_table (info);
5059 fdh = (struct ppc_link_hash_entry *)
5060 elf_link_hash_lookup (&htab->elf, fd_name,
b34976b6 5061 FALSE, FALSE, FALSE);
d6fe2dc1
AM
5062 fdh->is_func_descriptor = 1;
5063 fdh->oh = &fh->elf;
5064 fh->is_func = 1;
5065 fh->oh = &fdh->elf;
5066 }
5067
5068 fdh->elf.root.u.def.value = 0;
5069 fdh->elf.root.u.def.section = sym_sec;
a4aa0fb7
AM
5070 }
5071 }
5072 else
1e2f5b6e
AM
5073 {
5074 /* We'll be keeping this opd entry. */
5075
5076 if (h != NULL)
5077 {
5078 /* Redefine the function descriptor symbol
5079 to this location in the opd section.
5080 We've checked above that opd relocs are
5081 ordered. */
d6fe2dc1 5082 struct ppc_link_hash_entry *fdh;
1e2f5b6e
AM
5083 struct ppc_link_hash_entry *fh;
5084
5085 fh = (struct ppc_link_hash_entry *) h;
d6fe2dc1
AM
5086 fdh = (struct ppc_link_hash_entry *) fh->oh;
5087 if (fdh == NULL)
5088 {
5089 const char *fd_name;
5090 struct ppc_link_hash_table *htab;
5091
5092 fd_name = h->root.root.string + 1;
5093 htab = ppc_hash_table (info);
5094 fdh = (struct ppc_link_hash_entry *)
5095 elf_link_hash_lookup (&htab->elf, fd_name,
b34976b6 5096 FALSE, FALSE, FALSE);
d6fe2dc1
AM
5097 fdh->is_func_descriptor = 1;
5098 fdh->oh = &fh->elf;
5099 fh->is_func = 1;
5100 fh->oh = &fdh->elf;
5101 }
5102
5103 fdh->elf.root.u.def.value = wptr - sec->contents;
1e2f5b6e
AM
5104 }
5105 else
5106 {
6cdc0ccc
AM
5107 /* Local syms are a bit tricky. We could
5108 tweak them as they can be cached, but
5109 we'd need to look through the local syms
5110 for the function descriptor sym which we
5111 don't have at the moment. So keep an
b34976b6 5112 array of adjustments. */
7f6a7663 5113 adjust[rel->r_offset / 24] = wptr - rptr;
1e2f5b6e
AM
5114 }
5115
5116 if (wptr != rptr)
5117 memcpy (wptr, rptr, 24);
5118 wptr += 24;
5119 }
5120 rptr += 24;
5121 offset += 24;
5122 }
5123
5124 /* We need to adjust any reloc offsets to point to the
5125 new opd entries. While we're at it, we may as well
5126 remove redundant relocs. */
5127 if (!skip)
5128 {
5129 rel->r_offset += wptr - rptr;
5130 if (write_rel != rel)
5131 memcpy (write_rel, rel, sizeof (*rel));
5132 ++write_rel;
5133 }
5134 }
5135
5136 sec->_cooked_size = wptr - sec->contents;
5137 sec->reloc_count = write_rel - relstart;
cdcf6e38
AM
5138 /* Fudge the size too, as this is used later in
5139 elf_bfd_final_link if we are emitting relocs. */
5140 elf_section_data (sec)->rel_hdr.sh_size
5141 = sec->reloc_count * elf_section_data (sec)->rel_hdr.sh_entsize;
5142 BFD_ASSERT (elf_section_data (sec)->rel_hdr2 == NULL);
1e2f5b6e 5143 }
6cdc0ccc 5144 else if (elf_section_data (sec)->relocs != relstart)
1e2f5b6e 5145 free (relstart);
6cdc0ccc 5146
411e1bfb
AM
5147 if (local_syms != NULL
5148 && symtab_hdr->contents != (unsigned char *) local_syms)
5149 {
5150 if (!info->keep_memory)
5151 free (local_syms);
5152 else
5153 symtab_hdr->contents = (unsigned char *) local_syms;
5154 }
5155 }
5156
5157 return TRUE;
5158}
5159
951fd09b 5160/* Set htab->tls_sec. */
411e1bfb
AM
5161
5162bfd_boolean
951fd09b 5163ppc64_elf_tls_setup (obfd, info)
411e1bfb
AM
5164 bfd *obfd;
5165 struct bfd_link_info *info;
5166{
5167 asection *tls;
411e1bfb
AM
5168 struct ppc_link_hash_table *htab;
5169
411e1bfb
AM
5170 for (tls = obfd->sections; tls != NULL; tls = tls->next)
5171 if ((tls->flags & (SEC_THREAD_LOCAL | SEC_LOAD))
5172 == (SEC_THREAD_LOCAL | SEC_LOAD))
5173 break;
411e1bfb
AM
5174
5175 htab = ppc_hash_table (info);
5176 htab->tls_sec = tls;
a48ebf4d
AM
5177
5178 if (htab->tls_get_addr != NULL)
5179 {
5180 struct elf_link_hash_entry *h = htab->tls_get_addr;
5181
5182 while (h->root.type == bfd_link_hash_indirect
5183 || h->root.type == bfd_link_hash_warning)
5184 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5185
5186 htab->tls_get_addr = h;
5187 }
5188
951fd09b
AM
5189 return tls != NULL;
5190}
411e1bfb 5191
951fd09b
AM
5192/* Run through all the TLS relocs looking for optimization
5193 opportunities. The linker has been hacked (see ppc64elf.em) to do
5194 a preliminary section layout so that we know the TLS segment
5195 offsets. We can't optimize earlier because some optimizations need
5196 to know the tp offset, and we need to optimize before allocating
5197 dynamic relocations. */
5198
5199bfd_boolean
5200ppc64_elf_tls_optimize (obfd, info)
5201 bfd *obfd ATTRIBUTE_UNUSED;
5202 struct bfd_link_info *info;
5203{
5204 bfd *ibfd;
5205 asection *sec;
5206 struct ppc_link_hash_table *htab;
5207
5208 if (info->relocateable || info->shared)
411e1bfb
AM
5209 return TRUE;
5210
951fd09b 5211 htab = ppc_hash_table (info);
411e1bfb
AM
5212 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
5213 {
5214 Elf_Internal_Sym *locsyms = NULL;
5215
5216 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
5217 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
5218 {
5219 Elf_Internal_Rela *relstart, *rel, *relend;
5220 int expecting_tls_get_addr;
5221
5222 /* Read the relocations. */
5223 relstart = _bfd_elf64_link_read_relocs (ibfd, sec, (PTR) NULL,
5224 (Elf_Internal_Rela *) NULL,
5225 info->keep_memory);
5226 if (relstart == NULL)
5227 return FALSE;
5228
5229 expecting_tls_get_addr = 0;
5230 relend = relstart + sec->reloc_count;
5231 for (rel = relstart; rel < relend; rel++)
5232 {
5233 enum elf_ppc64_reloc_type r_type;
5234 unsigned long r_symndx;
5235 struct elf_link_hash_entry *h;
5236 Elf_Internal_Sym *sym;
5237 asection *sym_sec;
e7b938ca
AM
5238 char *tls_mask;
5239 char tls_set, tls_clear, tls_type = 0;
411e1bfb 5240 bfd_vma value;
951fd09b 5241 bfd_boolean ok_tprel, is_local;
411e1bfb
AM
5242
5243 r_symndx = ELF64_R_SYM (rel->r_info);
e7b938ca 5244 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
411e1bfb
AM
5245 r_symndx, ibfd))
5246 {
5247 err_free_rel:
5248 if (elf_section_data (sec)->relocs != relstart)
5249 free (relstart);
5250 if (locsyms != NULL
5251 && (elf_tdata (ibfd)->symtab_hdr.contents
5252 != (unsigned char *) locsyms))
5253 free (locsyms);
5254 return FALSE;
5255 }
5256
5257 if (h != NULL)
5258 {
5259 if (h->root.type != bfd_link_hash_defined
5260 && h->root.type != bfd_link_hash_defweak)
5261 continue;
5262 value = h->root.u.def.value;
5263 }
5264 else
5265 value = sym->st_value;
951fd09b 5266
411e1bfb 5267 ok_tprel = FALSE;
951fd09b
AM
5268 is_local = FALSE;
5269 if (h == NULL
5270 || !(h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC))
411e1bfb 5271 {
951fd09b 5272 is_local = TRUE;
411e1bfb
AM
5273 value += sym_sec->output_offset;
5274 value += sym_sec->output_section->vma;
5275 value -= htab->tls_sec->vma;
5276 ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
5277 < (bfd_vma) 1 << 32);
5278 }
5279
5280 r_type
5281 = (enum elf_ppc64_reloc_type) ELF64_R_TYPE (rel->r_info);
5282 switch (r_type)
5283 {
5284 case R_PPC64_GOT_TLSLD16:
5285 case R_PPC64_GOT_TLSLD16_LO:
5286 case R_PPC64_GOT_TLSLD16_HI:
5287 case R_PPC64_GOT_TLSLD16_HA:
951fd09b
AM
5288 /* These relocs should never be against a symbol
5289 defined in a shared lib. Leave them alone if
5290 that turns out to be the case. */
5291 if (!is_local)
5292 continue;
5293
411e1bfb 5294 htab->tlsld_got.refcount -= 1;
951fd09b
AM
5295 /* LD -> LE */
5296 tls_set = 0;
5297 tls_clear = TLS_LD;
e7b938ca 5298 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
5299 expecting_tls_get_addr = 1;
5300 break;
5301
5302 case R_PPC64_GOT_TLSGD16:
5303 case R_PPC64_GOT_TLSGD16_LO:
5304 case R_PPC64_GOT_TLSGD16_HI:
5305 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 5306 if (ok_tprel)
411e1bfb
AM
5307 /* GD -> LE */
5308 tls_set = 0;
5309 else
5310 /* GD -> IE */
951fd09b
AM
5311 tls_set = TLS_TLS | TLS_TPRELGD;
5312 tls_clear = TLS_GD;
e7b938ca 5313 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
5314 expecting_tls_get_addr = 1;
5315 break;
5316
5317 case R_PPC64_GOT_TPREL16_DS:
5318 case R_PPC64_GOT_TPREL16_LO_DS:
5319 case R_PPC64_GOT_TPREL16_HI:
5320 case R_PPC64_GOT_TPREL16_HA:
5321 expecting_tls_get_addr = 0;
5322 if (ok_tprel)
5323 {
5324 /* IE -> LE */
5325 tls_set = 0;
5326 tls_clear = TLS_TPREL;
e7b938ca 5327 tls_type = TLS_TLS | TLS_TPREL;
411e1bfb
AM
5328 break;
5329 }
5330 else
5331 continue;
5332
5333 case R_PPC64_REL14:
5334 case R_PPC64_REL14_BRTAKEN:
5335 case R_PPC64_REL14_BRNTAKEN:
5336 case R_PPC64_REL24:
5337 if (h != NULL
5338 && h == htab->tls_get_addr)
5339 {
5340 if (!expecting_tls_get_addr
5341 && rel != relstart
5342 && ((ELF64_R_TYPE (rel[-1].r_info)
5343 == R_PPC64_TOC16)
5344 || (ELF64_R_TYPE (rel[-1].r_info)
5345 == R_PPC64_TOC16_LO)))
5346 {
5347 /* Check for toc tls entries. */
5348 char *toc_tls;
951fd09b 5349 int retval;
411e1bfb 5350
e7b938ca 5351 retval = get_tls_mask (&toc_tls, &locsyms,
951fd09b
AM
5352 rel - 1, ibfd);
5353 if (retval == 0)
411e1bfb
AM
5354 goto err_free_rel;
5355 if (toc_tls != NULL)
951fd09b 5356 expecting_tls_get_addr = retval > 1;
411e1bfb
AM
5357 }
5358
5359 if (expecting_tls_get_addr)
5360 {
5361 struct plt_entry *ent;
5362 for (ent = h->plt.plist; ent; ent = ent->next)
5363 if (ent->addend == 0)
5364 {
5365 if (ent->plt.refcount > 0)
5366 ent->plt.refcount -= 1;
5367 break;
5368 }
5369 }
5370 }
5371 expecting_tls_get_addr = 0;
5372 continue;
5373
5374 case R_PPC64_TPREL64:
5375 expecting_tls_get_addr = 0;
5376 if (ok_tprel)
5377 {
5378 /* IE -> LE */
5379 tls_set = TLS_EXPLICIT;
5380 tls_clear = TLS_TPREL;
5381 break;
5382 }
5383 else
5384 continue;
5385
5386 case R_PPC64_DTPMOD64:
5387 expecting_tls_get_addr = 0;
951fd09b
AM
5388 if (rel + 1 < relend
5389 && (rel[1].r_info
5390 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
5391 && rel[1].r_offset == rel->r_offset + 8)
411e1bfb 5392 {
951fd09b 5393 if (ok_tprel)
411e1bfb 5394 /* GD -> LE */
951fd09b 5395 tls_set = TLS_EXPLICIT | TLS_GD;
411e1bfb
AM
5396 else
5397 /* GD -> IE */
951fd09b
AM
5398 tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
5399 tls_clear = TLS_GD;
411e1bfb
AM
5400 }
5401 else
5402 {
951fd09b
AM
5403 if (!is_local)
5404 continue;
5405
5406 /* LD -> LE */
5407 tls_set = TLS_EXPLICIT;
5408 tls_clear = TLS_LD;
411e1bfb
AM
5409 }
5410 break;
5411
5412 default:
5413 expecting_tls_get_addr = 0;
5414 continue;
5415 }
5416
5417 if ((tls_set & TLS_EXPLICIT) == 0)
5418 {
5419 struct got_entry *ent;
5420
5421 /* Adjust got entry for this reloc. */
5422 if (h != NULL)
5423 ent = h->got.glist;
5424 else
5425 ent = elf_local_got_ents (ibfd)[r_symndx];
5426
5427 for (; ent != NULL; ent = ent->next)
5428 if (ent->addend == rel->r_addend
e7b938ca 5429 && ent->tls_type == tls_type)
411e1bfb
AM
5430 break;
5431 if (ent == NULL)
5432 abort ();
5433
5434 if (tls_set == 0)
5435 {
5436 /* We managed to get rid of a got entry. */
5437 if (ent->got.refcount > 0)
5438 ent->got.refcount -= 1;
5439 }
411e1bfb
AM
5440 }
5441 else if (h != NULL)
5442 {
5443 struct ppc_link_hash_entry * eh;
5444 struct ppc_dyn_relocs **pp;
5445 struct ppc_dyn_relocs *p;
5446
5447 /* Adjust dynamic relocs. */
5448 eh = (struct ppc_link_hash_entry *) h;
5449 for (pp = &eh->dyn_relocs;
5450 (p = *pp) != NULL;
5451 pp = &p->next)
5452 if (p->sec == sec)
5453 {
5454 /* If we got rid of a DTPMOD/DTPREL reloc
5455 pair then we'll lose one or two dyn
5456 relocs. */
951fd09b 5457 if (tls_set == (TLS_EXPLICIT | TLS_GD))
411e1bfb 5458 p->count -= 1;
951fd09b 5459 p->count -= 1;
411e1bfb
AM
5460 if (p->count == 0)
5461 *pp = p->next;
5462 break;
5463 }
5464 }
5465
e7b938ca
AM
5466 *tls_mask |= tls_set;
5467 *tls_mask &= ~tls_clear;
411e1bfb
AM
5468 }
5469
5470 if (elf_section_data (sec)->relocs != relstart)
5471 free (relstart);
5472 }
5473
5474 if (locsyms != NULL
5475 && (elf_tdata (ibfd)->symtab_hdr.contents
5476 != (unsigned char *) locsyms))
6cdc0ccc
AM
5477 {
5478 if (!info->keep_memory)
411e1bfb 5479 free (locsyms);
6cdc0ccc 5480 else
411e1bfb 5481 elf_tdata (ibfd)->symtab_hdr.contents = (unsigned char *) locsyms;
6cdc0ccc 5482 }
1e2f5b6e 5483 }
b34976b6 5484 return TRUE;
1e2f5b6e 5485}
b34976b6 5486
65f38f15
AM
5487/* This is the condition under which ppc64_elf_finish_dynamic_symbol
5488 will be called from elflink.h. If elflink.h doesn't call our
5489 finish_dynamic_symbol routine, we'll need to do something about
5490 initializing any .plt and .got entries in ppc64_elf_relocate_section. */
5491#define WILL_CALL_FINISH_DYNAMIC_SYMBOL(DYN, INFO, H) \
5492 ((DYN) \
5493 && ((INFO)->shared \
5494 || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0) \
5495 && ((H)->dynindx != -1 \
5496 || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0))
5bd4f169 5497
65f38f15
AM
5498/* Allocate space in .plt, .got and associated reloc sections for
5499 dynamic relocs. */
5bd4f169 5500
b34976b6 5501static bfd_boolean
65f38f15
AM
5502allocate_dynrelocs (h, inf)
5503 struct elf_link_hash_entry *h;
5504 PTR inf;
5bd4f169 5505{
65f38f15
AM
5506 struct bfd_link_info *info;
5507 struct ppc_link_hash_table *htab;
5bd4f169 5508 asection *s;
65f38f15
AM
5509 struct ppc_link_hash_entry *eh;
5510 struct ppc_dyn_relocs *p;
411e1bfb 5511 struct got_entry *gent;
5bd4f169 5512
e92d460e 5513 if (h->root.type == bfd_link_hash_indirect)
b34976b6 5514 return TRUE;
5bd4f169 5515
e92d460e
AM
5516 if (h->root.type == bfd_link_hash_warning)
5517 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5518
65f38f15
AM
5519 info = (struct bfd_link_info *) inf;
5520 htab = ppc_hash_table (info);
5bd4f169 5521
65f38f15 5522 if (htab->elf.dynamic_sections_created
411e1bfb
AM
5523 && h->dynindx != -1
5524 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info, h))
5bd4f169 5525 {
411e1bfb
AM
5526 struct plt_entry *pent;
5527 bfd_boolean doneone = FALSE;
5528 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
5529 if (pent->plt.refcount > 0)
5530 {
5531 BFD_ASSERT (((struct ppc_link_hash_entry *) h)->is_func_descriptor);
5532
5533 /* If this is the first .plt entry, make room for the special
5534 first entry. */
5535 s = htab->splt;
5536 if (s->_raw_size == 0)
5537 s->_raw_size += PLT_INITIAL_ENTRY_SIZE;
5538
5539 pent->plt.offset = s->_raw_size;
5540
5541 /* Make room for this entry. */
5542 s->_raw_size += PLT_ENTRY_SIZE;
5543
5544 /* Make room for the .glink code. */
5545 s = htab->sglink;
5546 if (s->_raw_size == 0)
5547 s->_raw_size += GLINK_CALL_STUB_SIZE;
5548 /* We need bigger stubs past index 32767. */
5549 if (s->_raw_size >= GLINK_CALL_STUB_SIZE + 32768*2*4)
5550 s->_raw_size += 4;
5551 s->_raw_size += 2*4;
5552
5553 /* We also need to make an entry in the .rela.plt section. */
5554 s = htab->srelplt;
5555 s->_raw_size += sizeof (Elf64_External_Rela);
5556 doneone = TRUE;
5557 }
5558 else
5559 pent->plt.offset = (bfd_vma) -1;
5560 if (!doneone)
65f38f15 5561 {
411e1bfb 5562 h->plt.plist = NULL;
65f38f15
AM
5563 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
5564 }
5565 }
5566 else
5567 {
411e1bfb 5568 h->plt.plist = NULL;
65f38f15
AM
5569 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
5570 }
5571
951fd09b
AM
5572 eh = (struct ppc_link_hash_entry *) h;
5573 /* Run through the TLS GD got entries first if we're changing them
5574 to TPREL. */
e7b938ca 5575 if ((eh->tls_mask & TLS_TPRELGD) != 0)
951fd09b
AM
5576 for (gent = h->got.glist; gent != NULL; gent = gent->next)
5577 if (gent->got.refcount > 0
5578 && (gent->tls_type & TLS_GD) != 0)
5579 {
5580 /* This was a GD entry that has been converted to TPREL. If
5581 there happens to be a TPREL entry we can use that one. */
5582 struct got_entry *ent;
5583 for (ent = h->got.glist; ent != NULL; ent = ent->next)
5584 if (ent->got.refcount > 0
5585 && (ent->tls_type & TLS_TPREL) != 0
5586 && ent->addend == gent->addend)
5587 {
5588 gent->got.refcount = 0;
5589 break;
5590 }
5591
5592 /* If not, then we'll be using our own TPREL entry. */
5593 if (gent->got.refcount != 0)
5594 gent->tls_type = TLS_TLS | TLS_TPREL;
5595 }
5596
411e1bfb
AM
5597 for (gent = h->got.glist; gent != NULL; gent = gent->next)
5598 if (gent->got.refcount > 0)
5599 {
951fd09b
AM
5600 bfd_boolean dyn;
5601
411e1bfb 5602 /* Make sure this symbol is output as a dynamic symbol.
951fd09b
AM
5603 Undefined weak syms won't yet be marked as dynamic,
5604 nor will all TLS symbols. */
411e1bfb
AM
5605 if (h->dynindx == -1
5606 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
5607 {
5608 if (! bfd_elf64_link_record_dynamic_symbol (info, h))
5609 return FALSE;
5610 }
65f38f15 5611
411e1bfb 5612 if ((gent->tls_type & TLS_LD) != 0)
411e1bfb 5613 {
951fd09b
AM
5614 gent->got.offset = htab->tlsld_got.offset;
5615 continue;
411e1bfb 5616 }
951fd09b
AM
5617
5618 s = htab->sgot;
5619 gent->got.offset = s->_raw_size;
e7b938ca 5620 s->_raw_size += (gent->tls_type & eh->tls_mask & TLS_GD) ? 16 : 8;
951fd09b
AM
5621 dyn = htab->elf.dynamic_sections_created;
5622 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info, h))
5623 htab->srelgot->_raw_size
e7b938ca 5624 += (gent->tls_type & eh->tls_mask & TLS_GD
951fd09b
AM
5625 ? 2 * sizeof (Elf64_External_Rela)
5626 : sizeof (Elf64_External_Rela));
411e1bfb
AM
5627 }
5628 else
5629 gent->got.offset = (bfd_vma) -1;
65f38f15 5630
65f38f15 5631 if (eh->dyn_relocs == NULL)
b34976b6 5632 return TRUE;
65f38f15
AM
5633
5634 /* In the shared -Bsymbolic case, discard space allocated for
5635 dynamic pc-relative relocs against symbols which turn out to be
5636 defined in regular objects. For the normal shared case, discard
5637 space for relocs that have become local due to symbol visibility
5638 changes. */
5639
5640 if (info->shared)
5641 {
5642 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
5643 && ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0
5644 || info->symbolic))
5645 {
5646 struct ppc_dyn_relocs **pp;
5647
5648 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
5bd4f169 5649 {
65f38f15
AM
5650 p->count -= p->pc_count;
5651 p->pc_count = 0;
5652 if (p->count == 0)
5653 *pp = p->next;
5654 else
5655 pp = &p->next;
5bd4f169 5656 }
65f38f15
AM
5657 }
5658 }
5659 else
5660 {
5661 /* For the non-shared case, discard space for relocs against
5662 symbols which turn out to need copy relocs or are not
5663 dynamic. */
5664
5665 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
5666 && (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
5667 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
5668 || (htab->elf.dynamic_sections_created
5669 && (h->root.type == bfd_link_hash_undefweak
5670 || h->root.type == bfd_link_hash_undefined))))
5671 {
5672 /* Make sure this symbol is output as a dynamic symbol.
5673 Undefined weak syms won't yet be marked as dynamic. */
5674 if (h->dynindx == -1
5675 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
5676 {
ec338859 5677 if (! bfd_elf64_link_record_dynamic_symbol (info, h))
b34976b6 5678 return FALSE;
65f38f15
AM
5679 }
5680
5681 /* If that succeeded, we know we'll be keeping all the
5682 relocs. */
5683 if (h->dynindx != -1)
5684 goto keep;
5685 }
5686
5687 eh->dyn_relocs = NULL;
5688
ec338859 5689 keep: ;
65f38f15
AM
5690 }
5691
5692 /* Finally, allocate space. */
5693 for (p = eh->dyn_relocs; p != NULL; p = p->next)
5694 {
5695 asection *sreloc = elf_section_data (p->sec)->sreloc;
5696 sreloc->_raw_size += p->count * sizeof (Elf64_External_Rela);
5697 }
5698
b34976b6 5699 return TRUE;
65f38f15
AM
5700}
5701
5702/* Find any dynamic relocs that apply to read-only sections. */
5703
b34976b6 5704static bfd_boolean
65f38f15
AM
5705readonly_dynrelocs (h, inf)
5706 struct elf_link_hash_entry *h;
5707 PTR inf;
5708{
5709 struct ppc_link_hash_entry *eh;
5710 struct ppc_dyn_relocs *p;
5711
e92d460e
AM
5712 if (h->root.type == bfd_link_hash_warning)
5713 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5714
65f38f15
AM
5715 eh = (struct ppc_link_hash_entry *) h;
5716 for (p = eh->dyn_relocs; p != NULL; p = p->next)
5717 {
5718 asection *s = p->sec->output_section;
5719
5720 if (s != NULL && (s->flags & SEC_READONLY) != 0)
5721 {
5722 struct bfd_link_info *info = (struct bfd_link_info *) inf;
5723
5724 info->flags |= DF_TEXTREL;
5725
5726 /* Not an error, just cut short the traversal. */
b34976b6 5727 return FALSE;
65f38f15
AM
5728 }
5729 }
b34976b6 5730 return TRUE;
65f38f15
AM
5731}
5732
5733/* Set the sizes of the dynamic sections. */
5734
b34976b6 5735static bfd_boolean
65f38f15
AM
5736ppc64_elf_size_dynamic_sections (output_bfd, info)
5737 bfd *output_bfd ATTRIBUTE_UNUSED;
5738 struct bfd_link_info *info;
5739{
5740 struct ppc_link_hash_table *htab;
5741 bfd *dynobj;
5742 asection *s;
b34976b6 5743 bfd_boolean relocs;
65f38f15
AM
5744 bfd *ibfd;
5745
5746 htab = ppc_hash_table (info);
5747 dynobj = htab->elf.dynobj;
5748 if (dynobj == NULL)
5749 abort ();
5750
5751 if (htab->elf.dynamic_sections_created)
5752 {
5753 /* Set the contents of the .interp section to the interpreter. */
5754 if (! info->shared)
5755 {
5756 s = bfd_get_section_by_name (dynobj, ".interp");
5757 if (s == NULL)
5758 abort ();
5759 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
5760 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
5761 }
5762 }
5763
411e1bfb
AM
5764 if (htab->tlsld_got.refcount > 0)
5765 {
5766 htab->tlsld_got.offset = htab->sgot->_raw_size;
5767 htab->sgot->_raw_size += 16;
5768 if (info->shared)
5769 htab->srelgot->_raw_size += sizeof (Elf64_External_Rela);
5770 }
5771 else
5772 htab->tlsld_got.offset = (bfd_vma) -1;
5773
65f38f15
AM
5774 /* Set up .got offsets for local syms, and space for local dynamic
5775 relocs. */
5776 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
5777 {
411e1bfb
AM
5778 struct got_entry **lgot_ents;
5779 struct got_entry **end_lgot_ents;
e7b938ca 5780 char *lgot_masks;
65f38f15
AM
5781 bfd_size_type locsymcount;
5782 Elf_Internal_Shdr *symtab_hdr;
5783 asection *srel;
5784
5785 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
5786 continue;
5787
5788 for (s = ibfd->sections; s != NULL; s = s->next)
5789 {
ec338859 5790 struct ppc_dyn_relocs *p;
65f38f15 5791
ec338859
AM
5792 for (p = *((struct ppc_dyn_relocs **)
5793 &elf_section_data (s)->local_dynrel);
5794 p != NULL;
5795 p = p->next)
65f38f15 5796 {
ec338859
AM
5797 if (!bfd_is_abs_section (p->sec)
5798 && bfd_is_abs_section (p->sec->output_section))
5799 {
5800 /* Input section has been discarded, either because
5801 it is a copy of a linkonce section or due to
5802 linker script /DISCARD/, so we'll be discarding
5803 the relocs too. */
5804 }
248866a8 5805 else if (p->count != 0)
ec338859
AM
5806 {
5807 srel = elf_section_data (p->sec)->sreloc;
5808 srel->_raw_size += p->count * sizeof (Elf64_External_Rela);
248866a8
AM
5809 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
5810 info->flags |= DF_TEXTREL;
ec338859 5811 }
65f38f15
AM
5812 }
5813 }
5814
411e1bfb
AM
5815 lgot_ents = elf_local_got_ents (ibfd);
5816 if (!lgot_ents)
65f38f15
AM
5817 continue;
5818
5819 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
5820 locsymcount = symtab_hdr->sh_info;
411e1bfb 5821 end_lgot_ents = lgot_ents + locsymcount;
e7b938ca 5822 lgot_masks = (char *) end_lgot_ents;
65f38f15
AM
5823 s = htab->sgot;
5824 srel = htab->srelgot;
e7b938ca 5825 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
65f38f15 5826 {
411e1bfb
AM
5827 struct got_entry *ent;
5828
5829 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
5830 if (ent->got.refcount > 0)
5831 {
e7b938ca 5832 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
411e1bfb
AM
5833 {
5834 if (htab->tlsld_got.offset == (bfd_vma) -1)
5835 {
5836 htab->tlsld_got.offset = s->_raw_size;
5837 s->_raw_size += 16;
5838 if (info->shared)
5839 srel->_raw_size += sizeof (Elf64_External_Rela);
5840 }
5841 ent->got.offset = htab->tlsld_got.offset;
5842 }
5843 else
5844 {
5845 ent->got.offset = s->_raw_size;
e7b938ca 5846 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
411e1bfb
AM
5847 {
5848 s->_raw_size += 16;
5849 if (info->shared)
5850 srel->_raw_size += 2 * sizeof (Elf64_External_Rela);
5851 }
5852 else
5853 {
5854 s->_raw_size += 8;
5855 if (info->shared)
5856 srel->_raw_size += sizeof (Elf64_External_Rela);
5857 }
5858 }
5859 }
5860 else
5861 ent->got.offset = (bfd_vma) -1;
65f38f15
AM
5862 }
5863 }
5864
5865 /* Allocate global sym .plt and .got entries, and space for global
5866 sym dynamic relocs. */
5867 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, (PTR) info);
5868
5869 /* We now have determined the sizes of the various dynamic sections.
5870 Allocate memory for them. */
b34976b6 5871 relocs = FALSE;
65f38f15
AM
5872 for (s = dynobj->sections; s != NULL; s = s->next)
5873 {
5874 if ((s->flags & SEC_LINKER_CREATED) == 0)
5875 continue;
5876
411e1bfb
AM
5877 /* Reset _cooked_size since prelim layout will set it wrongly,
5878 and a non-zero _cooked_size sticks. */
5879 s->_cooked_size = 0;
5880
721956f4
AM
5881 if (s == htab->sbrlt || s == htab->srelbrlt)
5882 /* These haven't been allocated yet; don't strip. */
5883 continue;
5884 else if (s == htab->splt
5885 || s == htab->sgot
5886 || s == htab->sglink)
65f38f15
AM
5887 {
5888 /* Strip this section if we don't need it; see the
5889 comment below. */
5bd4f169 5890 }
65f38f15 5891 else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
5bd4f169
AM
5892 {
5893 if (s->_raw_size == 0)
5894 {
5895 /* If we don't need this section, strip it from the
5896 output file. This is mostly to handle .rela.bss and
5897 .rela.plt. We must create both sections in
5898 create_dynamic_sections, because they must be created
5899 before the linker maps input sections to output
5900 sections. The linker does that before
5901 adjust_dynamic_symbol is called, and it is that
5902 function which decides whether anything needs to go
5903 into these sections. */
5bd4f169
AM
5904 }
5905 else
5906 {
65f38f15 5907 if (s != htab->srelplt)
b34976b6 5908 relocs = TRUE;
5bd4f169
AM
5909
5910 /* We use the reloc_count field as a counter if we need
5911 to copy relocs into the output file. */
5912 s->reloc_count = 0;
5913 }
5914 }
65f38f15 5915 else
5bd4f169
AM
5916 {
5917 /* It's not one of our sections, so don't allocate space. */
5918 continue;
5919 }
5920
65f38f15 5921 if (s->_raw_size == 0)
5bd4f169
AM
5922 {
5923 _bfd_strip_section_from_output (info, s);
5924 continue;
5925 }
5926
5f333394
AM
5927 /* .plt is in the bss section. We don't initialise it. */
5928 if ((s->flags & SEC_LOAD) == 0)
5929 continue;
5930
65f38f15
AM
5931 /* Allocate memory for the section contents. We use bfd_zalloc
5932 here in case unused entries are not reclaimed before the
5933 section's contents are written out. This should not happen,
411e1bfb
AM
5934 but this way if it does we get a R_PPC64_NONE reloc in .rela
5935 sections instead of garbage.
5936 We also rely on the section contents being zero when writing
5937 the GOT. */
721956f4 5938 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
65f38f15 5939 if (s->contents == NULL)
b34976b6 5940 return FALSE;
5bd4f169
AM
5941 }
5942
e86ce104 5943 if (htab->elf.dynamic_sections_created)
5bd4f169
AM
5944 {
5945 /* Add some entries to the .dynamic section. We fill in the
5946 values later, in ppc64_elf_finish_dynamic_sections, but we
5947 must add the entries now so that we get the correct size for
5948 the .dynamic section. The DT_DEBUG entry is filled in by the
5949 dynamic linker and used by the debugger. */
dc810e39
AM
5950#define add_dynamic_entry(TAG, VAL) \
5951 bfd_elf64_add_dynamic_entry (info, (bfd_vma) (TAG), (bfd_vma) (VAL))
5952
5953 if (!info->shared)
5bd4f169 5954 {
dc810e39 5955 if (!add_dynamic_entry (DT_DEBUG, 0))
b34976b6 5956 return FALSE;
5bd4f169
AM
5957 }
5958
9c07fe7c 5959 if (htab->splt != NULL && htab->splt->_raw_size != 0)
5bd4f169 5960 {
dc810e39
AM
5961 if (!add_dynamic_entry (DT_PLTGOT, 0)
5962 || !add_dynamic_entry (DT_PLTRELSZ, 0)
5963 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
5d1634d7
AM
5964 || !add_dynamic_entry (DT_JMPREL, 0)
5965 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
b34976b6 5966 return FALSE;
5bd4f169
AM
5967 }
5968
19397422
AM
5969 if (NO_OPD_RELOCS)
5970 {
5971 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
5972 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
b34976b6 5973 return FALSE;
19397422
AM
5974 }
5975
5bd4f169
AM
5976 if (relocs)
5977 {
dc810e39
AM
5978 if (!add_dynamic_entry (DT_RELA, 0)
5979 || !add_dynamic_entry (DT_RELASZ, 0)
5980 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
b34976b6 5981 return FALSE;
5bd4f169 5982
65f38f15
AM
5983 /* If any dynamic relocs apply to a read-only section,
5984 then we need a DT_TEXTREL entry. */
248866a8
AM
5985 if ((info->flags & DF_TEXTREL) == 0)
5986 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs,
5987 (PTR) info);
5bd4f169 5988
65f38f15 5989 if ((info->flags & DF_TEXTREL) != 0)
5bd4f169 5990 {
65f38f15 5991 if (!add_dynamic_entry (DT_TEXTREL, 0))
b34976b6 5992 return FALSE;
5bd4f169 5993 }
5bd4f169 5994 }
5bd4f169 5995 }
65f38f15 5996#undef add_dynamic_entry
5bd4f169 5997
b34976b6 5998 return TRUE;
5bd4f169
AM
5999}
6000
721956f4 6001/* Determine the type of stub needed, if any, for a call. */
5bd4f169 6002
721956f4
AM
6003static INLINE enum ppc_stub_type
6004ppc_type_of_stub (input_sec, rel, hash, destination)
6005 asection *input_sec;
6006 const Elf_Internal_Rela *rel;
6007 struct ppc_link_hash_entry **hash;
6008 bfd_vma destination;
5bd4f169 6009{
721956f4
AM
6010 struct ppc_link_hash_entry *h = *hash;
6011 bfd_vma location;
6012 bfd_vma branch_offset;
6013 bfd_vma max_branch_offset;
6014 unsigned int r_type;
5bd4f169 6015
721956f4
AM
6016 if (h != NULL)
6017 {
6018 if (h->oh != NULL
721956f4 6019 && h->oh->dynindx != -1)
5bd4f169 6020 {
411e1bfb
AM
6021 struct plt_entry *ent;
6022 for (ent = h->oh->plt.plist; ent != NULL; ent = ent->next)
6023 if (ent->addend == rel->r_addend
6024 && ent->plt.offset != (bfd_vma) -1)
6025 {
6026 *hash = (struct ppc_link_hash_entry *) h->oh;
6027 return ppc_stub_plt_call;
6028 }
5bd4f169
AM
6029 }
6030
721956f4
AM
6031 if (h->elf.root.type == bfd_link_hash_undefweak
6032 || h->elf.root.type == bfd_link_hash_undefined)
6033 return ppc_stub_none;
5d1634d7 6034 }
5d1634d7 6035
721956f4
AM
6036 /* Determine where the call point is. */
6037 location = (input_sec->output_offset
6038 + input_sec->output_section->vma
6039 + rel->r_offset);
5d1634d7 6040
721956f4
AM
6041 branch_offset = destination - location;
6042 r_type = ELF64_R_TYPE (rel->r_info);
5d1634d7 6043
721956f4
AM
6044 /* Determine if a long branch stub is needed. */
6045 max_branch_offset = 1 << 25;
6046 if (r_type != (unsigned int) R_PPC64_REL24)
6047 max_branch_offset = 1 << 15;
5d1634d7 6048
721956f4
AM
6049 if (branch_offset + max_branch_offset >= 2 * max_branch_offset)
6050 /* We need a stub. Figure out whether a long_branch or plt_branch
6051 is needed later. */
6052 return ppc_stub_long_branch;
5d1634d7 6053
721956f4 6054 return ppc_stub_none;
5d1634d7
AM
6055}
6056
6057/* Build a .plt call stub. */
6058
6059static bfd_byte *
6060build_plt_stub (obfd, p, offset, glink)
6061 bfd *obfd;
6062 bfd_byte *p;
6063 int offset;
6064 int glink;
6065{
6066#define PPC_LO(v) ((v) & 0xffff)
6067#define PPC_HI(v) (((v) >> 16) & 0xffff)
6068#define PPC_HA(v) PPC_HI ((v) + 0x8000)
6069
6070 if (glink)
6071 bfd_put_32 (obfd, LD_R2_40R1, p), p += 4;
6072 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
6073 if (!glink)
6074 bfd_put_32 (obfd, STD_R2_40R1, p), p += 4;
6075 bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset), p), p += 4;
6076 if (PPC_HA (offset + 8) != PPC_HA (offset))
6077 bfd_put_32 (obfd, ADDIS_R12_R12_1, p), p += 4;
6078 offset += 8;
6079 bfd_put_32 (obfd, LD_R2_0R12 | PPC_LO (offset), p), p += 4;
6080 if (PPC_HA (offset + 8) != PPC_HA (offset))
6081 bfd_put_32 (obfd, ADDIS_R12_R12_1, p), p += 4;
6082 offset += 8;
6083 bfd_put_32 (obfd, MTCTR_R11, p), p += 4;
6084 bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset), p), p += 4;
6085 bfd_put_32 (obfd, BCTR, p), p += 4;
6086 return p;
6087}
6088
b34976b6 6089static bfd_boolean
721956f4
AM
6090ppc_build_one_stub (gen_entry, in_arg)
6091 struct bfd_hash_entry *gen_entry;
6092 PTR in_arg;
5d1634d7 6093{
721956f4
AM
6094 struct ppc_stub_hash_entry *stub_entry;
6095 struct ppc_branch_hash_entry *br_entry;
5d1634d7
AM
6096 struct bfd_link_info *info;
6097 struct ppc_link_hash_table *htab;
721956f4
AM
6098 asection *stub_sec;
6099 bfd *stub_bfd;
6100 bfd_byte *loc;
6101 bfd_byte *p;
6102 unsigned int indx;
411e1bfb 6103 struct plt_entry *ent;
721956f4
AM
6104 bfd_vma off;
6105 int size;
5d1634d7 6106
721956f4
AM
6107 /* Massage our args to the form they really have. */
6108 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
6109 info = (struct bfd_link_info *) in_arg;
5d1634d7 6110
5d1634d7 6111 htab = ppc_hash_table (info);
721956f4 6112 stub_sec = stub_entry->stub_sec;
5d1634d7 6113
721956f4
AM
6114 /* Make a note of the offset within the stubs for this entry. */
6115 stub_entry->stub_offset = stub_sec->_cooked_size;
6116 loc = stub_sec->contents + stub_entry->stub_offset;
6117
6118 stub_bfd = stub_sec->owner;
6119
6120 switch (stub_entry->stub_type)
5d1634d7 6121 {
721956f4
AM
6122 case ppc_stub_long_branch:
6123 /* Branches are relative. This is where we are going to. */
6124 off = (stub_entry->target_value
6125 + stub_entry->target_section->output_offset
6126 + stub_entry->target_section->output_section->vma);
5d1634d7 6127
721956f4
AM
6128 /* And this is where we are coming from. */
6129 off -= (stub_entry->stub_offset
6130 + stub_sec->output_offset
6131 + stub_sec->output_section->vma);
e86ce104 6132
721956f4 6133 BFD_ASSERT (off + (1 << 25) < (bfd_vma) (1 << 26));
e86ce104 6134
721956f4
AM
6135 bfd_put_32 (stub_bfd, (bfd_vma) B_DOT | (off & 0x3fffffc), loc);
6136 size = 4;
6137 break;
e86ce104 6138
721956f4
AM
6139 case ppc_stub_plt_branch:
6140 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
6141 stub_entry->root.string + 9,
b34976b6 6142 FALSE, FALSE);
721956f4
AM
6143 if (br_entry == NULL)
6144 {
6145 (*_bfd_error_handler) (_("can't find branch stub `%s'"),
6146 stub_entry->root.string + 9);
b34976b6
AM
6147 htab->stub_error = TRUE;
6148 return FALSE;
721956f4
AM
6149 }
6150
6151 off = (stub_entry->target_value
6152 + stub_entry->target_section->output_offset
6153 + stub_entry->target_section->output_section->vma);
6154
6155 bfd_put_64 (htab->sbrlt->owner, off,
6156 htab->sbrlt->contents + br_entry->offset);
6157
6158 if (info->shared)
6159 {
6160 /* Create a reloc for the branch lookup table entry. */
6161 Elf_Internal_Rela rela;
947216bf 6162 bfd_byte *loc;
5d1634d7 6163
721956f4
AM
6164 rela.r_offset = (br_entry->offset
6165 + htab->sbrlt->output_offset
6166 + htab->sbrlt->output_section->vma);
6167 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
6168 rela.r_addend = off;
6169
947216bf
AM
6170 loc = htab->srelbrlt->contents;
6171 loc += htab->srelbrlt->reloc_count++ * sizeof (Elf64_External_Rela);
6172 bfd_elf64_swap_reloca_out (htab->srelbrlt->owner, &rela, loc);
721956f4
AM
6173 }
6174
6175 off = (br_entry->offset
6176 + htab->sbrlt->output_offset
6177 + htab->sbrlt->output_section->vma
6178 - elf_gp (htab->sbrlt->output_section->owner)
6179 - TOC_BASE_OFF);
6180
6181 if (off + 0x80000000 > 0xffffffff || (off & 7) != 0)
5d1634d7
AM
6182 {
6183 (*_bfd_error_handler)
e86ce104 6184 (_("linkage table error against `%s'"),
721956f4 6185 stub_entry->root.string);
5d1634d7 6186 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
6187 htab->stub_error = TRUE;
6188 return FALSE;
5d1634d7 6189 }
41bd81ab 6190
721956f4
AM
6191 indx = off;
6192 bfd_put_32 (stub_bfd, (bfd_vma) ADDIS_R12_R2 | PPC_HA (indx), loc);
6193 bfd_put_32 (stub_bfd, (bfd_vma) LD_R11_0R12 | PPC_LO (indx), loc + 4);
6194 bfd_put_32 (stub_bfd, (bfd_vma) MTCTR_R11, loc + 8);
6195 bfd_put_32 (stub_bfd, (bfd_vma) BCTR, loc + 12);
6196 size = 16;
6197 break;
5d1634d7 6198
721956f4 6199 case ppc_stub_plt_call:
c862ae31
AM
6200 /* Do the best we can for shared libraries built without
6201 exporting ".foo" for each "foo". This can happen when symbol
6202 versioning scripts strip all bar a subset of symbols. */
6203 if (stub_entry->h->oh->root.type != bfd_link_hash_defined
6204 && stub_entry->h->oh->root.type != bfd_link_hash_defweak)
6205 {
6206 /* Point the symbol at the stub. There may be multiple stubs,
6207 we don't really care; The main thing is to make this sym
6208 defined somewhere. */
6209 stub_entry->h->oh->root.type = bfd_link_hash_defined;
6210 stub_entry->h->oh->root.u.def.section = stub_entry->stub_sec;
6211 stub_entry->h->oh->root.u.def.value = stub_entry->stub_offset;
6212 }
6213
721956f4 6214 /* Now build the stub. */
411e1bfb
AM
6215 off = (bfd_vma) -1;
6216 for (ent = stub_entry->h->elf.plt.plist; ent != NULL; ent = ent->next)
6217 if (ent->addend == stub_entry->addend)
6218 {
6219 off = ent->plt.offset;
6220 break;
6221 }
721956f4
AM
6222 if (off >= (bfd_vma) -2)
6223 abort ();
6224
6225 off &= ~ (bfd_vma) 1;
6226 off += (htab->splt->output_offset
6227 + htab->splt->output_section->vma
6228 - elf_gp (htab->splt->output_section->owner)
6229 - TOC_BASE_OFF);
6230
6231 if (off + 0x80000000 > 0xffffffff || (off & 7) != 0)
6232 {
6233 (*_bfd_error_handler)
6234 (_("linkage table error against `%s'"),
6235 stub_entry->h->elf.root.root.string);
6236 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
6237 htab->stub_error = TRUE;
6238 return FALSE;
721956f4
AM
6239 }
6240
6241 p = build_plt_stub (stub_bfd, loc, (int) off, 0);
6242 size = p - loc;
6243 break;
6244
6245 default:
6246 BFD_FAIL ();
b34976b6 6247 return FALSE;
721956f4
AM
6248 }
6249
6250 stub_sec->_cooked_size += size;
b34976b6 6251 return TRUE;
721956f4
AM
6252}
6253
6254/* As above, but don't actually build the stub. Just bump offset so
6255 we know stub section sizes, and select plt_branch stubs where
6256 long_branch stubs won't do. */
6257
b34976b6 6258static bfd_boolean
721956f4
AM
6259ppc_size_one_stub (gen_entry, in_arg)
6260 struct bfd_hash_entry *gen_entry;
6261 PTR in_arg;
6262{
6263 struct ppc_stub_hash_entry *stub_entry;
6264 struct ppc_link_hash_table *htab;
6265 bfd_vma off;
6266 int size;
6267
6268 /* Massage our args to the form they really have. */
6269 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
6270 htab = (struct ppc_link_hash_table *) in_arg;
6271
6272 if (stub_entry->stub_type == ppc_stub_plt_call)
6273 {
411e1bfb
AM
6274 struct plt_entry *ent;
6275 for (ent = stub_entry->h->elf.plt.plist; ent != NULL; ent = ent->next)
6276 if (ent->addend == stub_entry->addend)
6277 {
6278 off = ent->plt.offset & ~(bfd_vma) 1;
6279 break;
6280 }
6281 if (ent == NULL)
6282 abort ();
721956f4
AM
6283 off += (htab->splt->output_offset
6284 + htab->splt->output_section->vma
6285 - elf_gp (htab->splt->output_section->owner)
6286 - TOC_BASE_OFF);
6287
6288 size = 28;
6289 if (PPC_HA ((int) off + 16) != PPC_HA ((int) off))
6290 size += 4;
6291 }
6292 else
6293 {
6294 /* ppc_stub_long_branch or ppc_stub_plt_branch. */
6295 stub_entry->stub_type = ppc_stub_long_branch;
6296 size = 4;
6297
6298 off = (stub_entry->target_value
6299 + stub_entry->target_section->output_offset
6300 + stub_entry->target_section->output_section->vma);
6301 off -= (stub_entry->stub_sec->_raw_size
6302 + stub_entry->stub_sec->output_offset
6303 + stub_entry->stub_sec->output_section->vma);
6304
6305 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
6306 {
6307 struct ppc_branch_hash_entry *br_entry;
6308
6309 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
6310 stub_entry->root.string + 9,
b34976b6 6311 TRUE, FALSE);
721956f4
AM
6312 if (br_entry == NULL)
6313 {
6314 (*_bfd_error_handler) (_("can't build branch stub `%s'"),
6315 stub_entry->root.string + 9);
b34976b6
AM
6316 htab->stub_error = TRUE;
6317 return FALSE;
721956f4
AM
6318 }
6319
6320 if (br_entry->iter != htab->stub_iteration)
6321 {
6322 br_entry->iter = htab->stub_iteration;
6323 br_entry->offset = htab->sbrlt->_raw_size;
6324 htab->sbrlt->_raw_size += 8;
6325 }
6326 stub_entry->stub_type = ppc_stub_plt_branch;
6327 size = 16;
6328 }
6329 }
6330
6331 stub_entry->stub_sec->_raw_size += size;
b34976b6 6332 return TRUE;
721956f4
AM
6333}
6334
6335/* Set up various things so that we can make a list of input sections
6336 for each output section included in the link. Returns -1 on error,
cedb70c5 6337 0 when no stubs will be needed, and 1 on success. */
721956f4
AM
6338
6339int
6340ppc64_elf_setup_section_lists (output_bfd, info)
6341 bfd *output_bfd;
6342 struct bfd_link_info *info;
6343{
6344 bfd *input_bfd;
721956f4
AM
6345 int top_id, top_index;
6346 asection *section;
6347 asection **input_list, **list;
6348 bfd_size_type amt;
6349 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6350
805fc799
AM
6351 if (htab->elf.root.creator->flavour != bfd_target_elf_flavour
6352 || htab->sbrlt == NULL)
721956f4
AM
6353 return 0;
6354
1e2f5b6e
AM
6355 /* Find the top input section id. */
6356 for (input_bfd = info->input_bfds, top_id = 0;
721956f4
AM
6357 input_bfd != NULL;
6358 input_bfd = input_bfd->link_next)
6359 {
721956f4
AM
6360 for (section = input_bfd->sections;
6361 section != NULL;
6362 section = section->next)
6363 {
6364 if (top_id < section->id)
6365 top_id = section->id;
6366 }
6367 }
721956f4
AM
6368
6369 amt = sizeof (struct map_stub) * (top_id + 1);
6370 htab->stub_group = (struct map_stub *) bfd_zmalloc (amt);
6371 if (htab->stub_group == NULL)
6372 return -1;
6373
6374 /* We can't use output_bfd->section_count here to find the top output
6375 section index as some sections may have been removed, and
6376 _bfd_strip_section_from_output doesn't renumber the indices. */
6377 for (section = output_bfd->sections, top_index = 0;
6378 section != NULL;
6379 section = section->next)
6380 {
6381 if (top_index < section->index)
6382 top_index = section->index;
6383 }
6384
6385 htab->top_index = top_index;
6386 amt = sizeof (asection *) * (top_index + 1);
6387 input_list = (asection **) bfd_malloc (amt);
6388 htab->input_list = input_list;
6389 if (input_list == NULL)
6390 return -1;
6391
6392 /* For sections we aren't interested in, mark their entries with a
6393 value we can check later. */
6394 list = input_list + top_index;
6395 do
6396 *list = bfd_abs_section_ptr;
6397 while (list-- != input_list);
6398
6399 for (section = output_bfd->sections;
6400 section != NULL;
6401 section = section->next)
6402 {
6403 if ((section->flags & SEC_CODE) != 0)
6404 input_list[section->index] = NULL;
6405 }
6406
6407 return 1;
6408}
6409
6410/* The linker repeatedly calls this function for each input section,
6411 in the order that input sections are linked into output sections.
6412 Build lists of input sections to determine groupings between which
6413 we may insert linker stubs. */
6414
6415void
805fc799 6416ppc64_elf_next_input_section (info, isec)
721956f4
AM
6417 struct bfd_link_info *info;
6418 asection *isec;
6419{
6420 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6421
805fc799 6422 if (isec->output_section->index <= htab->top_index)
721956f4
AM
6423 {
6424 asection **list = htab->input_list + isec->output_section->index;
6425 if (*list != bfd_abs_section_ptr)
6426 {
6427 /* Steal the link_sec pointer for our list. */
6428#define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
6429 /* This happens to make the list in reverse order,
6430 which is what we want. */
6431 PREV_SEC (isec) = *list;
6432 *list = isec;
6433 }
6434 }
6435}
6436
6437/* See whether we can group stub sections together. Grouping stub
6438 sections may result in fewer stubs. More importantly, we need to
6439 put all .init* and .fini* stubs at the beginning of the .init or
6440 .fini output sections respectively, because glibc splits the
6441 _init and _fini functions into multiple parts. Putting a stub in
6442 the middle of a function is not a good idea. */
6443
6444static void
6445group_sections (htab, stub_group_size, stubs_always_before_branch)
6446 struct ppc_link_hash_table *htab;
6447 bfd_size_type stub_group_size;
b34976b6 6448 bfd_boolean stubs_always_before_branch;
721956f4
AM
6449{
6450 asection **list = htab->input_list + htab->top_index;
6451 do
6452 {
6453 asection *tail = *list;
6454 if (tail == bfd_abs_section_ptr)
6455 continue;
6456 while (tail != NULL)
6457 {
6458 asection *curr;
6459 asection *prev;
6460 bfd_size_type total;
6461
6462 curr = tail;
6463 if (tail->_cooked_size)
6464 total = tail->_cooked_size;
6465 else
6466 total = tail->_raw_size;
6467 while ((prev = PREV_SEC (curr)) != NULL
6468 && ((total += curr->output_offset - prev->output_offset)
6469 < stub_group_size))
6470 curr = prev;
6471
6472 /* OK, the size from the start of CURR to the end is less
6473 than stub_group_size and thus can be handled by one stub
6474 section. (or the tail section is itself larger than
6475 stub_group_size, in which case we may be toast.) We
6476 should really be keeping track of the total size of stubs
6477 added here, as stubs contribute to the final output
6478 section size. That's a little tricky, and this way will
6479 only break if stubs added make the total size more than
6480 2^25, ie. for the default stub_group_size, if stubs total
6481 more than 2834432 bytes, or over 100000 plt call stubs. */
6482 do
6483 {
6484 prev = PREV_SEC (tail);
6485 /* Set up this stub group. */
6486 htab->stub_group[tail->id].link_sec = curr;
6487 }
6488 while (tail != curr && (tail = prev) != NULL);
6489
6490 /* But wait, there's more! Input sections up to stub_group_size
6491 bytes before the stub section can be handled by it too. */
6492 if (!stubs_always_before_branch)
6493 {
6494 total = 0;
6495 while (prev != NULL
6496 && ((total += tail->output_offset - prev->output_offset)
6497 < stub_group_size))
6498 {
6499 tail = prev;
6500 prev = PREV_SEC (tail);
6501 htab->stub_group[tail->id].link_sec = curr;
6502 }
6503 }
6504 tail = prev;
6505 }
6506 }
6507 while (list-- != htab->input_list);
6508 free (htab->input_list);
6509#undef PREV_SEC
6510}
6511
721956f4
AM
6512/* Determine and set the size of the stub section for a final link.
6513
6514 The basic idea here is to examine all the relocations looking for
6515 PC-relative calls to a target that is unreachable with a "bl"
6516 instruction. */
6517
b34976b6 6518bfd_boolean
721956f4
AM
6519ppc64_elf_size_stubs (output_bfd, stub_bfd, info, group_size,
6520 add_stub_section, layout_sections_again)
6521 bfd *output_bfd;
6522 bfd *stub_bfd;
6523 struct bfd_link_info *info;
6524 bfd_signed_vma group_size;
6525 asection * (*add_stub_section) PARAMS ((const char *, asection *));
6526 void (*layout_sections_again) PARAMS ((void));
6527{
6528 bfd_size_type stub_group_size;
b34976b6 6529 bfd_boolean stubs_always_before_branch;
721956f4
AM
6530 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6531
6532 /* Stash our params away. */
6533 htab->stub_bfd = stub_bfd;
6534 htab->add_stub_section = add_stub_section;
6535 htab->layout_sections_again = layout_sections_again;
6536 stubs_always_before_branch = group_size < 0;
6537 if (group_size < 0)
6538 stub_group_size = -group_size;
6539 else
6540 stub_group_size = group_size;
6541 if (stub_group_size == 1)
6542 {
6543 /* Default values. */
6544 stub_group_size = 30720000;
6545 if (htab->has_14bit_branch)
6546 stub_group_size = 30000;
6547 }
6548
6549 group_sections (htab, stub_group_size, stubs_always_before_branch);
6550
721956f4
AM
6551 while (1)
6552 {
6553 bfd *input_bfd;
6554 unsigned int bfd_indx;
6555 asection *stub_sec;
b34976b6 6556 bfd_boolean stub_changed;
721956f4
AM
6557
6558 htab->stub_iteration += 1;
b34976b6 6559 stub_changed = FALSE;
721956f4
AM
6560
6561 for (input_bfd = info->input_bfds, bfd_indx = 0;
6562 input_bfd != NULL;
6563 input_bfd = input_bfd->link_next, bfd_indx++)
6564 {
6565 Elf_Internal_Shdr *symtab_hdr;
6566 asection *section;
6cdc0ccc 6567 Elf_Internal_Sym *local_syms = NULL;
721956f4
AM
6568
6569 /* We'll need the symbol table in a second. */
6570 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
6571 if (symtab_hdr->sh_info == 0)
6572 continue;
6573
721956f4
AM
6574 /* Walk over each section attached to the input bfd. */
6575 for (section = input_bfd->sections;
6576 section != NULL;
6577 section = section->next)
6578 {
721956f4 6579 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
721956f4
AM
6580
6581 /* If there aren't any relocs, then there's nothing more
6582 to do. */
6583 if ((section->flags & SEC_RELOC) == 0
6584 || section->reloc_count == 0)
6585 continue;
6586
6587 /* If this section is a link-once section that will be
6588 discarded, then don't create any stubs. */
6589 if (section->output_section == NULL
6590 || section->output_section->owner != output_bfd)
6591 continue;
6592
1e2f5b6e
AM
6593 /* Get the relocs. */
6594 internal_relocs
6595 = _bfd_elf64_link_read_relocs (input_bfd, section, NULL,
6596 (Elf_Internal_Rela *) NULL,
6597 info->keep_memory);
721956f4 6598 if (internal_relocs == NULL)
1e2f5b6e 6599 goto error_ret_free_local;
721956f4
AM
6600
6601 /* Now examine each relocation. */
6602 irela = internal_relocs;
6603 irelaend = irela + section->reloc_count;
6604 for (; irela < irelaend; irela++)
6605 {
6606 unsigned int r_type, r_indx;
6607 enum ppc_stub_type stub_type;
6608 struct ppc_stub_hash_entry *stub_entry;
6609 asection *sym_sec;
6610 bfd_vma sym_value;
6611 bfd_vma destination;
6612 struct ppc_link_hash_entry *hash;
411e1bfb
AM
6613 struct elf_link_hash_entry *h;
6614 Elf_Internal_Sym *sym;
721956f4
AM
6615 char *stub_name;
6616 const asection *id_sec;
6617
6618 r_type = ELF64_R_TYPE (irela->r_info);
6619 r_indx = ELF64_R_SYM (irela->r_info);
6620
04c9666a 6621 if (r_type >= (unsigned int) R_PPC64_max)
721956f4
AM
6622 {
6623 bfd_set_error (bfd_error_bad_value);
6cdc0ccc 6624 goto error_ret_free_internal;
721956f4
AM
6625 }
6626
6627 /* Only look for stubs on branch instructions. */
6628 if (r_type != (unsigned int) R_PPC64_REL24
6629 && r_type != (unsigned int) R_PPC64_REL14
6630 && r_type != (unsigned int) R_PPC64_REL14_BRTAKEN
6631 && r_type != (unsigned int) R_PPC64_REL14_BRNTAKEN)
6632 continue;
6633
6634 /* Now determine the call target, its name, value,
6635 section. */
721956f4 6636 destination = 0;
411e1bfb
AM
6637 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
6638 r_indx, input_bfd))
6639 goto error_ret_free_internal;
6640 hash = (struct ppc_link_hash_entry *) h;
6641
6642 if (hash == NULL)
721956f4
AM
6643 {
6644 /* It's a local symbol. */
411e1bfb 6645 sym_value = sym->st_value;
721956f4
AM
6646 destination = (sym_value + irela->r_addend
6647 + sym_sec->output_offset
6648 + sym_sec->output_section->vma);
6649 }
6650 else
6651 {
6652 /* It's an external symbol. */
411e1bfb 6653 sym_value = 0;
721956f4
AM
6654 if (hash->elf.root.type == bfd_link_hash_defined
6655 || hash->elf.root.type == bfd_link_hash_defweak)
6656 {
721956f4
AM
6657 sym_value = hash->elf.root.u.def.value;
6658 if (sym_sec->output_section != NULL)
6659 destination = (sym_value + irela->r_addend
6660 + sym_sec->output_offset
6661 + sym_sec->output_section->vma);
6662 }
6663 else if (hash->elf.root.type == bfd_link_hash_undefweak)
6664 ;
6665 else if (hash->elf.root.type == bfd_link_hash_undefined)
6666 ;
6667 else
6668 {
6669 bfd_set_error (bfd_error_bad_value);
6670 goto error_ret_free_internal;
6671 }
6672 }
6673
6674 /* Determine what (if any) linker stub is needed. */
6675 stub_type = ppc_type_of_stub (section, irela, &hash,
6676 destination);
6677 if (stub_type == ppc_stub_none)
6678 continue;
6679
411e1bfb
AM
6680 /* __tls_get_addr calls might be eliminated. */
6681 if (stub_type != ppc_stub_plt_call
6682 && hash != NULL
6683 && &hash->elf == htab->tls_get_addr
6684 && section->has_tls_reloc
6685 && irela != internal_relocs)
6686 {
6687 /* Get tls info. */
e7b938ca 6688 char *tls_mask;
411e1bfb 6689
e7b938ca 6690 if (!get_tls_mask (&tls_mask, &local_syms,
411e1bfb
AM
6691 irela - 1, input_bfd))
6692 goto error_ret_free_internal;
e7b938ca 6693 if (*tls_mask != 0)
411e1bfb
AM
6694 continue;
6695 }
6696
721956f4
AM
6697 /* Support for grouping stub sections. */
6698 id_sec = htab->stub_group[section->id].link_sec;
6699
6700 /* Get the name of this stub. */
6701 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
6702 if (!stub_name)
6703 goto error_ret_free_internal;
6704
6705 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 6706 stub_name, FALSE, FALSE);
721956f4
AM
6707 if (stub_entry != NULL)
6708 {
6709 /* The proper stub has already been created. */
6710 free (stub_name);
6711 continue;
6712 }
6713
6714 stub_entry = ppc_add_stub (stub_name, section, htab);
6715 if (stub_entry == NULL)
6716 {
6717 free (stub_name);
6cdc0ccc
AM
6718 error_ret_free_internal:
6719 if (elf_section_data (section)->relocs == NULL)
6720 free (internal_relocs);
6721 error_ret_free_local:
6722 if (local_syms != NULL
6723 && (symtab_hdr->contents
6724 != (unsigned char *) local_syms))
6725 free (local_syms);
b34976b6 6726 return FALSE;
721956f4
AM
6727 }
6728
6729 stub_entry->target_value = sym_value;
6730 stub_entry->target_section = sym_sec;
6731 stub_entry->stub_type = stub_type;
6732 stub_entry->h = hash;
411e1bfb 6733 stub_entry->addend = irela->r_addend;
b34976b6 6734 stub_changed = TRUE;
721956f4
AM
6735 }
6736
6737 /* We're done with the internal relocs, free them. */
6cdc0ccc 6738 if (elf_section_data (section)->relocs != internal_relocs)
1e2f5b6e 6739 free (internal_relocs);
721956f4 6740 }
6cdc0ccc
AM
6741
6742 if (local_syms != NULL
6743 && symtab_hdr->contents != (unsigned char *) local_syms)
6744 {
6745 if (!info->keep_memory)
6746 free (local_syms);
6747 else
6748 symtab_hdr->contents = (unsigned char *) local_syms;
6749 }
721956f4
AM
6750 }
6751
6752 if (!stub_changed)
6753 break;
6754
6755 /* OK, we've added some stubs. Find out the new size of the
6756 stub sections. */
6757 for (stub_sec = htab->stub_bfd->sections;
6758 stub_sec != NULL;
6759 stub_sec = stub_sec->next)
6760 {
6761 stub_sec->_raw_size = 0;
6762 stub_sec->_cooked_size = 0;
6763 }
6764 htab->sbrlt->_raw_size = 0;
6765 htab->sbrlt->_cooked_size = 0;
6766
6767 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, htab);
6768
6769 /* Ask the linker to do its stuff. */
6770 (*htab->layout_sections_again) ();
6771 }
6772
afbe61cf
AM
6773 /* It would be nice to strip .branch_lt from the output if the
6774 section is empty, but it's too late. If we strip sections here,
6775 the dynamic symbol table is corrupted since the section symbol
6776 for the stripped section isn't written. */
721956f4 6777
b34976b6 6778 return TRUE;
721956f4
AM
6779}
6780
6781/* Called after we have determined section placement. If sections
805fc799 6782 move, we'll be called again. Provide a value for TOCstart. */
721956f4 6783
805fc799
AM
6784bfd_vma
6785ppc64_elf_toc (obfd)
5d1634d7 6786 bfd *obfd;
721956f4 6787{
805fc799
AM
6788 asection *s;
6789 bfd_vma TOCstart;
721956f4 6790
805fc799
AM
6791 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
6792 order. The TOC starts where the first of these sections starts. */
6793 s = bfd_get_section_by_name (obfd, ".got");
6794 if (s == NULL)
6795 s = bfd_get_section_by_name (obfd, ".toc");
6796 if (s == NULL)
6797 s = bfd_get_section_by_name (obfd, ".tocbss");
6798 if (s == NULL)
6799 s = bfd_get_section_by_name (obfd, ".plt");
6800 if (s == NULL)
6801 {
6802 /* This may happen for
6803 o references to TOC base (SYM@toc / TOC[tc0]) without a
6804 .toc directive
6805 o bad linker script
6806 o --gc-sections and empty TOC sections
6807
6808 FIXME: Warn user? */
6809
6810 /* Look for a likely section. We probably won't even be
6811 using TOCstart. */
6812 for (s = obfd->sections; s != NULL; s = s->next)
6813 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY))
6814 == (SEC_ALLOC | SEC_SMALL_DATA))
6815 break;
721956f4 6816 if (s == NULL)
805fc799
AM
6817 for (s = obfd->sections; s != NULL; s = s->next)
6818 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA))
6819 == (SEC_ALLOC | SEC_SMALL_DATA))
6820 break;
721956f4 6821 if (s == NULL)
805fc799
AM
6822 for (s = obfd->sections; s != NULL; s = s->next)
6823 if ((s->flags & (SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
6824 break;
721956f4 6825 if (s == NULL)
805fc799
AM
6826 for (s = obfd->sections; s != NULL; s = s->next)
6827 if ((s->flags & SEC_ALLOC) == SEC_ALLOC)
6828 break;
6829 }
721956f4 6830
805fc799
AM
6831 TOCstart = 0;
6832 if (s != NULL)
6833 TOCstart = s->output_section->vma + s->output_offset;
721956f4 6834
805fc799 6835 return TOCstart;
721956f4
AM
6836}
6837
6838/* Build all the stubs associated with the current output file.
6839 The stubs are kept in a hash table attached to the main linker
6840 hash table. This function is called via gldelf64ppc_finish. */
6841
b34976b6 6842bfd_boolean
721956f4
AM
6843ppc64_elf_build_stubs (info)
6844 struct bfd_link_info *info;
5d1634d7
AM
6845{
6846 struct ppc_link_hash_table *htab = ppc_hash_table (info);
721956f4 6847 asection *stub_sec;
5d1634d7
AM
6848 bfd_vma plt_r2;
6849 bfd_byte *p;
6850
721956f4
AM
6851 for (stub_sec = htab->stub_bfd->sections;
6852 stub_sec != NULL;
6853 stub_sec = stub_sec->next)
6854 {
6855 bfd_size_type size;
6856
6857 /* Allocate memory to hold the linker stubs. */
6858 size = stub_sec->_raw_size;
6859 if (size != 0)
6860 {
6861 stub_sec->contents = (bfd_byte *) bfd_zalloc (htab->stub_bfd, size);
6862 if (stub_sec->contents == NULL)
b34976b6 6863 return FALSE;
721956f4
AM
6864 }
6865 stub_sec->_cooked_size = 0;
6866 }
5d1634d7 6867
721956f4 6868 if (htab->splt != NULL)
5d1634d7 6869 {
9f951329
AM
6870 unsigned int indx;
6871
721956f4
AM
6872 /* Build the .glink plt call stub. */
6873 plt_r2 = (htab->splt->output_offset
6874 + htab->splt->output_section->vma
6875 - elf_gp (htab->splt->output_section->owner)
6876 - TOC_BASE_OFF);
6877 p = htab->sglink->contents;
6878 p = build_plt_stub (htab->sglink->owner, p, (int) plt_r2, 1);
9f951329 6879 while (p < htab->sglink->contents + GLINK_CALL_STUB_SIZE)
721956f4
AM
6880 {
6881 bfd_put_32 (htab->sglink->owner, NOP, p);
6882 p += 4;
6883 }
721956f4 6884
9f951329
AM
6885 /* Build the .glink lazy link call stubs. */
6886 indx = 0;
6887 while (p < htab->sglink->contents + htab->sglink->_raw_size)
6888 {
6889 if (indx < 0x8000)
6890 {
6891 bfd_put_32 (htab->sglink->owner, LI_R0_0 | indx, p);
6892 p += 4;
6893 }
6894 else
6895 {
6896 bfd_put_32 (htab->sglink->owner, LIS_R0_0 | PPC_HI (indx), p);
6897 p += 4;
6898 bfd_put_32 (htab->sglink->owner, ORI_R0_R0_0 | PPC_LO (indx), p);
6899 p += 4;
6900 }
6901 bfd_put_32 (htab->sglink->owner,
6902 B_DOT | ((htab->sglink->contents - p) & 0x3fffffc), p);
a16d5acb 6903 indx++;
9f951329
AM
6904 p += 4;
6905 }
6906 htab->sglink->_cooked_size = p - htab->sglink->contents;
5d1634d7 6907 }
5d1634d7 6908
721956f4
AM
6909 if (htab->sbrlt->_raw_size != 0)
6910 {
6911 htab->sbrlt->contents = (bfd_byte *) bfd_zalloc (htab->sbrlt->owner,
6912 htab->sbrlt->_raw_size);
6913 if (htab->sbrlt->contents == NULL)
b34976b6 6914 return FALSE;
721956f4 6915 }
5d1634d7 6916
721956f4
AM
6917 /* Build the stubs as directed by the stub hash table. */
6918 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
5d1634d7 6919
721956f4
AM
6920 for (stub_sec = htab->stub_bfd->sections;
6921 stub_sec != NULL;
6922 stub_sec = stub_sec->next)
6923 {
6924 if (stub_sec->_raw_size != stub_sec->_cooked_size)
6925 break;
6926 }
5d1634d7 6927
721956f4 6928 if (stub_sec != NULL
5d1634d7
AM
6929 || htab->sglink->_raw_size != htab->sglink->_cooked_size)
6930 {
b34976b6 6931 htab->stub_error = TRUE;
721956f4 6932 (*_bfd_error_handler) (_("stubs don't match calculated size"));
5d1634d7 6933 }
721956f4
AM
6934
6935 return !htab->stub_error;
5bd4f169
AM
6936}
6937
5bd4f169
AM
6938/* The RELOCATE_SECTION function is called by the ELF backend linker
6939 to handle the relocations for a section.
6940
6941 The relocs are always passed as Rela structures; if the section
6942 actually uses Rel structures, the r_addend field will always be
6943 zero.
6944
6945 This function is responsible for adjust the section contents as
6946 necessary, and (if using Rela relocs and generating a
6947 relocateable output file) adjusting the reloc addend as
6948 necessary.
6949
6950 This function does not have to worry about setting the reloc
6951 address or the reloc symbol index.
6952
6953 LOCAL_SYMS is a pointer to the swapped in local symbols.
6954
6955 LOCAL_SECTIONS is an array giving the section in the input file
6956 corresponding to the st_shndx field of each local symbol.
6957
6958 The global hash table entry for the global symbols can be found
6959 via elf_sym_hashes (input_bfd).
6960
6961 When generating relocateable output, this function must handle
6962 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
6963 going to be the section symbol corresponding to the output
6964 section, which means that the addend must be adjusted
6965 accordingly. */
6966
b34976b6 6967static bfd_boolean
5bd4f169
AM
6968ppc64_elf_relocate_section (output_bfd, info, input_bfd, input_section,
6969 contents, relocs, local_syms, local_sections)
6970 bfd *output_bfd;
6971 struct bfd_link_info *info;
6972 bfd *input_bfd;
6973 asection *input_section;
6974 bfd_byte *contents;
6975 Elf_Internal_Rela *relocs;
6976 Elf_Internal_Sym *local_syms;
6977 asection **local_sections;
6978{
65f38f15 6979 struct ppc_link_hash_table *htab;
5bd4f169
AM
6980 Elf_Internal_Shdr *symtab_hdr;
6981 struct elf_link_hash_entry **sym_hashes;
5bd4f169
AM
6982 Elf_Internal_Rela *rel;
6983 Elf_Internal_Rela *relend;
411e1bfb
AM
6984 Elf_Internal_Rela outrel;
6985 bfd_byte *loc;
411e1bfb 6986 struct got_entry **local_got_ents;
5bd4f169 6987 bfd_vma TOCstart;
b34976b6
AM
6988 bfd_boolean ret = TRUE;
6989 bfd_boolean is_opd;
96e0dda4 6990 /* Disabled until we sort out how ld should choose 'y' vs 'at'. */
b34976b6 6991 bfd_boolean is_power4 = FALSE;
5bd4f169 6992
b491616a 6993 if (info->relocateable)
b34976b6 6994 return TRUE;
b491616a 6995
65f38f15 6996 /* Initialize howto table if needed. */
5bd4f169 6997 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
6998 ppc_howto_init ();
6999
65f38f15 7000 htab = ppc_hash_table (info);
411e1bfb
AM
7001 if (info->shared && (htab->tlsld_got.offset & 1) == 0)
7002 {
7003 outrel.r_offset = (htab->sgot->output_section->vma
7004 + htab->sgot->output_offset
7005 + htab->tlsld_got.offset);
7006 outrel.r_info = ELF64_R_INFO (0, R_PPC64_DTPMOD64);
7007 outrel.r_addend = 0;
7008
7009 loc = htab->srelgot->contents;
7010 loc += htab->srelgot->reloc_count++ * sizeof (Elf64_External_Rela);
7011 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
7012 htab->tlsld_got.offset |= 1;
7013 }
7014
7015 local_got_ents = elf_local_got_ents (input_bfd);
5bd4f169 7016 TOCstart = elf_gp (output_bfd);
5bd4f169
AM
7017 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
7018 sym_hashes = elf_sym_hashes (input_bfd);
f0abc2a1 7019 is_opd = ppc64_elf_section_data (input_section)->opd.adjust != NULL;
65f38f15 7020
5bd4f169
AM
7021 rel = relocs;
7022 relend = relocs + input_section->reloc_count;
7023 for (; rel < relend; rel++)
7024 {
04c9666a 7025 enum elf_ppc64_reloc_type r_type;
5bd4f169
AM
7026 bfd_vma addend;
7027 bfd_reloc_status_type r;
7028 Elf_Internal_Sym *sym;
7029 asection *sec;
7030 struct elf_link_hash_entry *h;
721956f4 7031 struct elf_link_hash_entry *fdh;
5bd4f169 7032 const char *sym_name;
5bd4f169 7033 unsigned long r_symndx;
951fd09b 7034 char tls_mask, tls_gd, tls_type;
5bd4f169 7035 bfd_vma relocation;
b34976b6
AM
7036 bfd_boolean unresolved_reloc;
7037 bfd_boolean warned;
dc810e39 7038 long insn;
721956f4
AM
7039 struct ppc_stub_hash_entry *stub_entry;
7040 bfd_vma max_br_offset;
7041 bfd_vma from;
5bd4f169 7042
04c9666a 7043 r_type = (enum elf_ppc64_reloc_type) ELF64_R_TYPE (rel->r_info);
5bd4f169 7044 r_symndx = ELF64_R_SYM (rel->r_info);
65f38f15
AM
7045 r = bfd_reloc_other;
7046 sym = (Elf_Internal_Sym *) 0;
7047 sec = (asection *) 0;
7048 h = (struct elf_link_hash_entry *) 0;
7049 sym_name = (const char *) 0;
b34976b6
AM
7050 unresolved_reloc = FALSE;
7051 warned = FALSE;
65f38f15 7052
5bd4f169
AM
7053 if (r_type == R_PPC64_TOC)
7054 {
7055 /* Relocation value is TOC base. Symbol is ignored. */
7056 relocation = TOCstart + TOC_BASE_OFF;
7057 }
7058 else if (r_symndx < symtab_hdr->sh_info)
7059 {
7060 /* It's a local symbol. */
7061 sym = local_syms + r_symndx;
7062 sec = local_sections[r_symndx];
411e1bfb 7063 sym_name = bfd_elf_local_sym_name (input_bfd, sym);
f8df10f4 7064 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, sec, rel);
1e2f5b6e
AM
7065 if (elf_section_data (sec) != NULL)
7066 {
7067 long *opd_sym_adjust;
7068
f0abc2a1 7069 opd_sym_adjust = ppc64_elf_section_data (sec)->opd.adjust;
1e2f5b6e
AM
7070 if (opd_sym_adjust != NULL && sym->st_value % 24 == 0)
7071 relocation += opd_sym_adjust[sym->st_value / 24];
7072 }
5bd4f169
AM
7073 }
7074 else
7075 {
7076 /* It's a global symbol. */
7077 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7078 while (h->root.type == bfd_link_hash_indirect
7079 || h->root.type == bfd_link_hash_warning)
7080 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7081 sym_name = h->root.root.string;
65f38f15 7082 relocation = 0;
5bd4f169
AM
7083 if (h->root.type == bfd_link_hash_defined
7084 || h->root.type == bfd_link_hash_defweak)
7085 {
7086 sec = h->root.u.def.section;
65f38f15
AM
7087 if (sec->output_section == NULL)
7088 /* Set a flag that will be cleared later if we find a
7089 relocation value for this symbol. output_section
7090 is typically NULL for symbols satisfied by a shared
7091 library. */
b34976b6 7092 unresolved_reloc = TRUE;
5bd4f169
AM
7093 else
7094 relocation = (h->root.u.def.value
7095 + sec->output_section->vma
7096 + sec->output_offset);
7097 }
7098 else if (h->root.type == bfd_link_hash_undefweak)
65f38f15 7099 ;
671bae9c 7100 else if (info->shared
65f38f15
AM
7101 && (!info->symbolic || info->allow_shlib_undefined)
7102 && !info->no_undefined
7103 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
7104 ;
5bd4f169
AM
7105 else
7106 {
7107 if (! ((*info->callbacks->undefined_symbol)
7108 (info, h->root.root.string, input_bfd, input_section,
411e1bfb
AM
7109 rel->r_offset, (!info->shared
7110 || info->no_undefined
7111 || ELF_ST_VISIBILITY (h->other)))))
b34976b6
AM
7112 return FALSE;
7113 warned = TRUE;
5bd4f169
AM
7114 }
7115 }
7116
951fd09b
AM
7117 /* TLS optimizations. Replace instruction sequences and relocs
7118 based on information we collected in tls_optimize. We edit
7119 RELOCS so that --emit-relocs will output something sensible
7120 for the final instruction stream. */
7121 tls_mask = 0;
7122 tls_gd = 0;
411e1bfb
AM
7123 if (IS_TLS_RELOC (r_type))
7124 {
7125 if (h != NULL)
e7b938ca 7126 tls_mask = ((struct ppc_link_hash_entry *) h)->tls_mask;
411e1bfb
AM
7127 else if (local_got_ents != NULL)
7128 {
e7b938ca
AM
7129 char *lgot_masks;
7130 lgot_masks = (char *) (local_got_ents + symtab_hdr->sh_info);
7131 tls_mask = lgot_masks[r_symndx];
411e1bfb
AM
7132 }
7133 }
7134
7135 /* Ensure reloc mapping code below stays sane. */
7136 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
7137 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
7138 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
7139 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
7140 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
7141 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
7142 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
7143 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
7144 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
7145 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
7146 abort ();
7147 switch (r_type)
7148 {
7149 default:
411e1bfb
AM
7150 break;
7151
7152 case R_PPC64_TOC16:
7153 case R_PPC64_TOC16_LO:
7154 case R_PPC64_TOC16_DS:
7155 case R_PPC64_TOC16_LO_DS:
411e1bfb
AM
7156 {
7157 /* Check for toc tls entries. */
7158 char *toc_tls;
951fd09b 7159 int retval;
411e1bfb 7160
e7b938ca 7161 retval = get_tls_mask (&toc_tls, &local_syms, rel, input_bfd);
951fd09b 7162 if (retval == 0)
411e1bfb
AM
7163 return FALSE;
7164
7165 if (toc_tls)
7166 {
951fd09b 7167 tls_mask = *toc_tls;
411e1bfb
AM
7168 if (r_type == R_PPC64_TOC16_DS
7169 || r_type == R_PPC64_TOC16_LO_DS)
7170 goto toctprel;
7171 else
951fd09b
AM
7172 {
7173 /* If we found a GD reloc pair, then we might be
7174 doing a GD->IE transition. */
7175 if (retval == 2)
7176 {
7177 tls_gd = TLS_TPRELGD;
7178 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
7179 goto tls_get_addr_check;
7180 }
7181 else if (retval == 3)
7182 {
7183 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
7184 goto tls_get_addr_check;
7185 }
7186 }
411e1bfb
AM
7187 }
7188 }
7189 break;
7190
7191 case R_PPC64_GOT_TPREL16_DS:
7192 case R_PPC64_GOT_TPREL16_LO_DS:
411e1bfb 7193 toctprel:
951fd09b
AM
7194 if (tls_mask != 0
7195 && (tls_mask & TLS_TPREL) == 0)
411e1bfb
AM
7196 {
7197 bfd_vma insn;
7198 insn = bfd_get_32 (output_bfd, contents + rel->r_offset - 2);
7199 insn &= 31 << 21;
7200 insn |= 0x3c0d0000; /* addis 0,13,0 */
7201 bfd_put_32 (output_bfd, insn, contents + rel->r_offset - 2);
7202 r_type = R_PPC64_TPREL16_HA;
7203 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
7204 }
7205 break;
7206
7207 case R_PPC64_TLS:
951fd09b 7208 if (tls_mask == 0)
411e1bfb
AM
7209 {
7210 /* Check for toc tls entries. */
7211 char *toc_tls;
7212
e7b938ca 7213 if (!get_tls_mask (&toc_tls, &local_syms, rel, input_bfd))
411e1bfb
AM
7214 return FALSE;
7215
7216 if (toc_tls)
951fd09b 7217 tls_mask = *toc_tls;
411e1bfb 7218 }
951fd09b
AM
7219 if (tls_mask != 0
7220 && (tls_mask & TLS_TPREL) == 0)
411e1bfb
AM
7221 {
7222 bfd_vma insn, rtra;
7223 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
7224 if ((insn & ((31 << 26) | (31 << 11)))
7225 == ((31 << 26) | (13 << 11)))
7226 rtra = insn & ((1 << 26) - (1 << 16));
7227 else if ((insn & ((31 << 26) | (31 << 16)))
7228 == ((31 << 26) | (13 << 16)))
7229 rtra = (insn & (31 << 21)) | ((insn & (31 << 11)) << 5);
7230 else
7231 abort ();
7232 if ((insn & ((1 << 11) - (1 << 1))) == 266 << 1)
7233 /* add -> addi. */
7234 insn = 14 << 26;
7235 else if ((insn & (31 << 1)) == 23 << 1
7236 && ((insn & (31 << 6)) < 14 << 6
7237 || ((insn & (31 << 6)) >= 16 << 6
7238 && (insn & (31 << 6)) < 24 << 6)))
7239 /* load and store indexed -> dform. */
7240 insn = (32 | ((insn >> 6) & 31)) << 26;
7241 else if ((insn & (31 << 1)) == 21 << 1
7242 && (insn & (0x1a << 6)) == 0)
7243 /* ldx, ldux, stdx, stdux -> ld, ldu, std, stdu. */
7244 insn = (((58 | ((insn >> 6) & 4)) << 26)
7245 | ((insn >> 6) & 1));
7246 else if ((insn & (31 << 1)) == 21 << 1
7247 && (insn & ((1 << 11) - (1 << 1))) == 341 << 1)
7248 /* lwax -> lwa. */
7249 insn = (58 << 26) | 2;
7250 else
7251 abort ();
7252 insn |= rtra;
7253 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
7254 r_type = R_PPC64_TPREL16_LO;
7255 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
7256 /* Was PPC64_TLS which sits on insn boundary, now
7257 PPC64_TPREL16_LO which is at insn+2. */
7258 rel->r_offset += 2;
7259 }
7260 break;
7261
411e1bfb
AM
7262 case R_PPC64_GOT_TLSGD16_HI:
7263 case R_PPC64_GOT_TLSGD16_HA:
951fd09b
AM
7264 tls_gd = TLS_TPRELGD;
7265 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
7266 goto tls_gdld_hi;
7267 break;
7268
411e1bfb
AM
7269 case R_PPC64_GOT_TLSLD16_HI:
7270 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 7271 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
411e1bfb 7272 {
951fd09b
AM
7273 tls_gdld_hi:
7274 if ((tls_mask & tls_gd) != 0)
7275 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
7276 + R_PPC64_GOT_TPREL16_DS);
7277 else
411e1bfb 7278 {
951fd09b
AM
7279 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
7280 rel->r_offset -= 2;
7281 r_type = R_PPC64_NONE;
411e1bfb 7282 }
951fd09b 7283 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
7284 }
7285 break;
7286
951fd09b
AM
7287 case R_PPC64_GOT_TLSGD16:
7288 case R_PPC64_GOT_TLSGD16_LO:
7289 tls_gd = TLS_TPRELGD;
7290 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
7291 goto tls_get_addr_check;
7292 break;
411e1bfb 7293
951fd09b
AM
7294 case R_PPC64_GOT_TLSLD16:
7295 case R_PPC64_GOT_TLSLD16_LO:
7296 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
7297 {
7298 tls_get_addr_check:
7299 if (rel + 1 < relend)
411e1bfb 7300 {
951fd09b
AM
7301 enum elf_ppc64_reloc_type r_type2;
7302 unsigned long r_symndx2;
7303 struct elf_link_hash_entry *h2;
7304 bfd_vma insn1, insn2, insn3;
7305 bfd_vma offset;
7306
7307 /* The next instruction should be a call to
7308 __tls_get_addr. Peek at the reloc to be sure. */
7309 r_type2
7310 = (enum elf_ppc64_reloc_type) ELF64_R_TYPE (rel[1].r_info);
7311 r_symndx2 = ELF64_R_SYM (rel[1].r_info);
7312 if (r_symndx2 < symtab_hdr->sh_info
7313 || (r_type2 != R_PPC64_REL14
7314 && r_type2 != R_PPC64_REL14_BRTAKEN
7315 && r_type2 != R_PPC64_REL14_BRNTAKEN
7316 && r_type2 != R_PPC64_REL24))
7317 break;
7318
7319 h2 = sym_hashes[r_symndx2 - symtab_hdr->sh_info];
7320 while (h2->root.type == bfd_link_hash_indirect
7321 || h2->root.type == bfd_link_hash_warning)
7322 h2 = (struct elf_link_hash_entry *) h2->root.u.i.link;
7323 if (h2 == NULL || h2 != htab->tls_get_addr)
7324 break;
7325
7326 /* OK, it checks out. Replace the call. */
7327 offset = rel[1].r_offset;
7328 insn1 = bfd_get_32 (output_bfd,
7329 contents + rel->r_offset - 2);
7330 insn3 = bfd_get_32 (output_bfd,
7331 contents + offset + 4);
7332 if ((tls_mask & tls_gd) != 0)
411e1bfb 7333 {
951fd09b
AM
7334 /* IE */
7335 insn1 &= (1 << 26) - (1 << 2);
7336 insn1 |= 58 << 26; /* ld */
7337 insn2 = 0x7c636a14; /* add 3,3,13 */
7338 rel[1].r_info = ELF64_R_INFO (r_symndx2, R_PPC64_NONE);
7339 if ((tls_mask & TLS_EXPLICIT) == 0)
7340 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
7341 + R_PPC64_GOT_TPREL16_DS);
7342 else
7343 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
7344 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
7345 }
7346 else
951fd09b
AM
7347 {
7348 /* LE */
7349 insn1 = 0x3c6d0000; /* addis 3,13,0 */
7350 insn2 = 0x38630000; /* addi 3,3,0 */
7351 if (tls_gd == 0)
7352 {
7353 /* Was an LD reloc. */
7354 r_symndx = 0;
7355 rel->r_addend = htab->tls_sec->vma + DTP_OFFSET;
7356 rel[1].r_addend = htab->tls_sec->vma + DTP_OFFSET;
7357 }
7358 r_type = R_PPC64_TPREL16_HA;
7359 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
7360 rel[1].r_info = ELF64_R_INFO (r_symndx,
7361 R_PPC64_TPREL16_LO);
7362 rel[1].r_offset += 2;
7363 }
7364 if (insn3 == NOP
7365 || insn3 == CROR_151515 || insn3 == CROR_313131)
7366 {
7367 insn3 = insn2;
7368 insn2 = NOP;
7369 rel[1].r_offset += 4;
7370 }
7371 bfd_put_32 (output_bfd, insn1, contents + rel->r_offset - 2);
7372 bfd_put_32 (output_bfd, insn2, contents + offset);
7373 bfd_put_32 (output_bfd, insn3, contents + offset + 4);
7374 if (tls_gd == 0)
7375 {
7376 /* We changed the symbol on an LD reloc. Start over
7377 in order to get h, sym, sec etc. right. */
7378 rel--;
7379 continue;
7380 }
411e1bfb 7381 }
411e1bfb 7382 }
411e1bfb
AM
7383 break;
7384
7385 case R_PPC64_DTPMOD64:
951fd09b
AM
7386 if (rel + 1 < relend
7387 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
7388 && rel[1].r_offset == rel->r_offset + 8)
411e1bfb 7389 {
951fd09b
AM
7390 if ((tls_mask & TLS_GD) == 0)
7391 {
7392 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
7393 if ((tls_mask & TLS_TPRELGD) != 0)
7394 r_type = R_PPC64_TPREL64;
7395 else
7396 {
7397 bfd_put_64 (output_bfd, (bfd_vma) 1,
7398 contents + rel->r_offset);
7399 r_type = R_PPC64_NONE;
7400 }
7401 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
7402 }
7403 }
7404 else
7405 {
7406 if ((tls_mask & TLS_LD) == 0)
411e1bfb
AM
7407 {
7408 bfd_put_64 (output_bfd, (bfd_vma) 1,
7409 contents + rel->r_offset);
7410 r_type = R_PPC64_NONE;
951fd09b 7411 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb 7412 }
411e1bfb
AM
7413 }
7414 break;
7415
7416 case R_PPC64_TPREL64:
951fd09b 7417 if ((tls_mask & TLS_TPREL) == 0)
411e1bfb
AM
7418 {
7419 r_type = R_PPC64_NONE;
7420 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
7421 }
7422 break;
7423 }
7424
7425 /* Handle other relocations that tweak non-addend part of insn. */
86c76c7b 7426 insn = 0;
65f38f15 7427 switch (r_type)
5bd4f169
AM
7428 {
7429 default:
65f38f15 7430 break;
5bd4f169 7431
65f38f15
AM
7432 /* Branch taken prediction relocations. */
7433 case R_PPC64_ADDR14_BRTAKEN:
7434 case R_PPC64_REL14_BRTAKEN:
cedb70c5
KH
7435 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
7436 /* Fall thru. */
65f38f15 7437
86c76c7b 7438 /* Branch not taken prediction relocations. */
65f38f15
AM
7439 case R_PPC64_ADDR14_BRNTAKEN:
7440 case R_PPC64_REL14_BRNTAKEN:
411e1bfb
AM
7441 insn |= bfd_get_32 (output_bfd,
7442 contents + rel->r_offset) & ~(0x01 << 21);
96e0dda4
AM
7443 if (is_power4)
7444 {
7445 /* Set 'a' bit. This is 0b00010 in BO field for branch
7446 on CR(BI) insns (BO == 001at or 011at), and 0b01000
7447 for branch on CTR insns (BO == 1a00t or 1a01t). */
7448 if ((insn & (0x14 << 21)) == (0x04 << 21))
7449 insn |= 0x02 << 21;
7450 else if ((insn & (0x14 << 21)) == (0x10 << 21))
7451 insn |= 0x08 << 21;
7452 else
7453 break;
7454 }
65f38f15 7455 else
96e0dda4 7456 {
411e1bfb 7457 from = (rel->r_offset
805fc799
AM
7458 + input_section->output_offset
7459 + input_section->output_section->vma);
7460
96e0dda4 7461 /* Invert 'y' bit if not the default. */
411e1bfb 7462 if ((bfd_signed_vma) (relocation + rel->r_addend - from) < 0)
96e0dda4
AM
7463 insn ^= 0x01 << 21;
7464 }
86c76c7b 7465
411e1bfb 7466 bfd_put_32 (output_bfd, (bfd_vma) insn, contents + rel->r_offset);
65f38f15 7467 break;
5bd4f169 7468
65f38f15 7469 case R_PPC64_REL24:
721956f4
AM
7470 /* A REL24 branching to a linkage function is followed by a
7471 nop. We replace the nop with a ld in order to restore
7472 the TOC base pointer. Only calls to shared objects need
7473 to alter the TOC base. These are recognized by their
7474 need for a PLT entry. */
5bd4f169 7475 if (h != NULL
721956f4 7476 && (fdh = ((struct ppc_link_hash_entry *) h)->oh) != NULL
411e1bfb 7477 && fdh->plt.plist != NULL
721956f4
AM
7478 && (stub_entry = ppc_get_stub_entry (input_section, sec, fdh,
7479 rel, htab)) != NULL)
41bd81ab 7480 {
b34976b6 7481 bfd_boolean can_plt_call = 0;
721956f4 7482
411e1bfb 7483 if (rel->r_offset + 8 <= input_section->_cooked_size)
41bd81ab 7484 {
411e1bfb 7485 insn = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
721956f4
AM
7486 if (insn == NOP
7487 || insn == CROR_151515 || insn == CROR_313131)
41bd81ab 7488 {
721956f4 7489 bfd_put_32 (input_bfd, (bfd_vma) LD_R2_40R1,
411e1bfb 7490 contents + rel->r_offset + 4);
721956f4 7491 can_plt_call = 1;
41bd81ab 7492 }
5bd4f169 7493 }
721956f4
AM
7494
7495 if (!can_plt_call)
7496 {
7497 /* If this is a plain branch rather than a branch
7498 and link, don't require a nop. */
411e1bfb 7499 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
721956f4
AM
7500 if ((insn & 1) == 0)
7501 can_plt_call = 1;
7502 }
7503
7504 if (can_plt_call)
7505 {
7506 relocation = (stub_entry->stub_offset
7507 + stub_entry->stub_sec->output_offset
7508 + stub_entry->stub_sec->output_section->vma);
b34976b6 7509 unresolved_reloc = FALSE;
721956f4 7510 }
5bd4f169 7511 }
e86ce104
AM
7512
7513 if (h != NULL
7514 && h->root.type == bfd_link_hash_undefweak
721956f4 7515 && relocation == 0
411e1bfb 7516 && rel->r_addend == 0)
e86ce104 7517 {
721956f4
AM
7518 /* Tweak calls to undefined weak functions to point at a
7519 blr. We can thus call a weak function without first
7520 checking whether the function is defined. We have a
7521 blr at the end of .sfpr. */
805fc799 7522 BFD_ASSERT (htab->sfpr->_raw_size != 0);
721956f4
AM
7523 relocation = (htab->sfpr->_raw_size - 4
7524 + htab->sfpr->output_offset
7525 + htab->sfpr->output_section->vma);
411e1bfb 7526 from = (rel->r_offset
721956f4
AM
7527 + input_section->output_offset
7528 + input_section->output_section->vma);
805fc799 7529
721956f4
AM
7530 /* But let's not be silly about it. If the blr isn't in
7531 reach, just go to the next instruction. */
805fc799
AM
7532 if (relocation - from + (1 << 25) >= (1 << 26)
7533 || htab->sfpr->_raw_size == 0)
721956f4 7534 relocation = from + 4;
e86ce104 7535 }
65f38f15
AM
7536 break;
7537 }
5bd4f169 7538
65f38f15 7539 /* Set `addend'. */
411e1bfb
AM
7540 tls_type = 0;
7541 addend = rel->r_addend;
65f38f15
AM
7542 switch (r_type)
7543 {
7544 default:
7545 (*_bfd_error_handler)
7546 (_("%s: unknown relocation type %d for symbol %s"),
7547 bfd_archive_filename (input_bfd), (int) r_type, sym_name);
5bd4f169 7548
65f38f15 7549 bfd_set_error (bfd_error_bad_value);
b34976b6 7550 ret = FALSE;
65f38f15 7551 continue;
5bd4f169 7552
65f38f15 7553 case R_PPC64_NONE:
411e1bfb 7554 case R_PPC64_TLS:
04c9666a
AM
7555 case R_PPC64_GNU_VTINHERIT:
7556 case R_PPC64_GNU_VTENTRY:
65f38f15 7557 continue;
5bd4f169
AM
7558
7559 /* GOT16 relocations. Like an ADDR16 using the symbol's
7560 address in the GOT as relocation value instead of the
411e1bfb 7561 symbol's value itself. Also, create a GOT entry for the
5bd4f169 7562 symbol and put the symbol value there. */
411e1bfb
AM
7563 case R_PPC64_GOT_TLSGD16:
7564 case R_PPC64_GOT_TLSGD16_LO:
7565 case R_PPC64_GOT_TLSGD16_HI:
7566 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 7567 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
7568 goto dogot;
7569
7570 case R_PPC64_GOT_TLSLD16:
7571 case R_PPC64_GOT_TLSLD16_LO:
7572 case R_PPC64_GOT_TLSLD16_HI:
7573 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 7574 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
7575 goto dogot;
7576
7577 case R_PPC64_GOT_TPREL16_DS:
7578 case R_PPC64_GOT_TPREL16_LO_DS:
7579 case R_PPC64_GOT_TPREL16_HI:
7580 case R_PPC64_GOT_TPREL16_HA:
7581 tls_type = TLS_TLS | TLS_TPREL;
7582 goto dogot;
7583
7584 case R_PPC64_GOT_DTPREL16_DS:
7585 case R_PPC64_GOT_DTPREL16_LO_DS:
7586 case R_PPC64_GOT_DTPREL16_HI:
7587 case R_PPC64_GOT_DTPREL16_HA:
7588 tls_type = TLS_TLS | TLS_DTPREL;
7589 goto dogot;
7590
65f38f15
AM
7591 case R_PPC64_GOT16:
7592 case R_PPC64_GOT16_LO:
7593 case R_PPC64_GOT16_HI:
7594 case R_PPC64_GOT16_HA:
7595 case R_PPC64_GOT16_DS:
7596 case R_PPC64_GOT16_LO_DS:
411e1bfb 7597 dogot:
5bd4f169
AM
7598 {
7599 /* Relocation is to the entry for this symbol in the global
7600 offset table. */
411e1bfb 7601 struct got_entry *ent;
5bd4f169 7602 bfd_vma off;
411e1bfb 7603 unsigned long indx;
65f38f15
AM
7604
7605 if (htab->sgot == NULL)
7606 abort ();
5bd4f169
AM
7607
7608 if (h != NULL)
411e1bfb
AM
7609 ent = h->got.glist;
7610 else
5bd4f169 7611 {
411e1bfb
AM
7612 if (local_got_ents == NULL)
7613 abort ();
7614 ent = local_got_ents[r_symndx];
7615 }
7616
7617 for (; ent != NULL; ent = ent->next)
7618 if (ent->addend == rel->r_addend
7619 && ent->tls_type == tls_type)
7620 break;
7621 if (ent == NULL)
7622 abort ();
5bd4f169 7623
411e1bfb
AM
7624 off = ent->got.offset;
7625 indx = 0;
7626 if (h != NULL)
7627 {
7628 bfd_boolean dyn = htab->elf.dynamic_sections_created;
65f38f15 7629 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info, h)
5bd4f169 7630 || (info->shared
65f38f15
AM
7631 && (info->symbolic
7632 || h->dynindx == -1
7633 || (h->elf_link_hash_flags
7634 & ELF_LINK_FORCED_LOCAL))
5bd4f169
AM
7635 && (h->elf_link_hash_flags
7636 & ELF_LINK_HASH_DEF_REGULAR)))
411e1bfb
AM
7637 /* This is actually a static link, or it is a
7638 -Bsymbolic link and the symbol is defined
7639 locally, or the symbol was forced to be local
7640 because of a version file. */
7641 ;
7642 else
5bd4f169 7643 {
411e1bfb
AM
7644 indx = h->dynindx;
7645 unresolved_reloc = FALSE;
5bd4f169
AM
7646 }
7647 }
411e1bfb
AM
7648
7649 /* The offset must always be a multiple of 8. We use the
7650 least significant bit to record whether we have already
7651 processed this entry. */
7652 if ((off & 1) != 0)
7653 off &= ~1;
5bd4f169
AM
7654 else
7655 {
411e1bfb
AM
7656 /* Generate relocs for the dynamic linker, except in
7657 the case of TLSLD where we'll use one entry per
7658 module. */
7659 if ((info->shared || indx != 0)
951fd09b 7660 && tls_type != (TLS_TLS | TLS_LD))
5bd4f169 7661 {
411e1bfb
AM
7662 outrel.r_offset = (htab->sgot->output_section->vma
7663 + htab->sgot->output_offset
7664 + off);
951fd09b 7665 if (tls_type == (TLS_TLS | TLS_GD))
5bd4f169 7666 {
411e1bfb
AM
7667 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
7668 outrel.r_addend = 0;
947216bf
AM
7669 loc = htab->srelgot->contents;
7670 loc += (htab->srelgot->reloc_count++
7671 * sizeof (Elf64_External_Rela));
65f38f15 7672 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
411e1bfb
AM
7673 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
7674 outrel.r_offset += 8;
7675 }
951fd09b 7676 else if (tls_type == (TLS_TLS | TLS_DTPREL))
411e1bfb 7677 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
951fd09b 7678 else if (tls_type == (TLS_TLS | TLS_TPREL))
411e1bfb
AM
7679 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
7680 else if (indx == 0)
7681 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_RELATIVE);
7682 else
7683 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
951fd09b 7684 outrel.r_addend = rel->r_addend;
411e1bfb
AM
7685 if (indx == 0)
7686 outrel.r_addend += relocation;
7687 loc = htab->srelgot->contents;
7688 loc += (htab->srelgot->reloc_count++
7689 * sizeof (Elf64_External_Rela));
7690 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
7691 }
7692
7693 /* Init the .got section contents if we're not
7694 emitting a reloc. */
7695 if (!(info->shared || indx != 0))
7696 {
7697
7698 relocation += ent->addend;
e7b938ca 7699 if (tls_type != 0)
411e1bfb
AM
7700 {
7701 relocation -= htab->tls_sec->vma + DTP_OFFSET;
7702 if ((tls_type & TLS_TPREL) != 0)
7703 relocation += DTP_OFFSET - TP_OFFSET;
5bd4f169
AM
7704 }
7705
951fd09b 7706 if ((tls_type & (TLS_GD | TLS_LD)) != 0)
411e1bfb
AM
7707 {
7708 if ((tls_type & TLS_LD) != 0)
7709 relocation = - DTP_OFFSET;
7710 bfd_put_64 (output_bfd, relocation,
7711 htab->sgot->contents + off + 8);
7712 relocation = 1;
7713 }
7714 bfd_put_64 (output_bfd, relocation,
7715 htab->sgot->contents + off);
5bd4f169 7716 }
411e1bfb 7717 ent->got.offset |= 1;
5bd4f169
AM
7718 }
7719
65f38f15
AM
7720 if (off >= (bfd_vma) -2)
7721 abort ();
7722
7723 relocation = htab->sgot->output_offset + off;
7724
5bd4f169 7725 /* TOC base (r2) is TOC start plus 0x8000. */
411e1bfb 7726 addend = - TOC_BASE_OFF;
5bd4f169 7727 }
65f38f15
AM
7728 break;
7729
7730 case R_PPC64_PLT16_HA:
7731 case R_PPC64_PLT16_HI:
7732 case R_PPC64_PLT16_LO:
7733 case R_PPC64_PLT32:
7734 case R_PPC64_PLT64:
7735 /* Relocation is to the entry for this symbol in the
7736 procedure linkage table. */
7737
7738 /* Resolve a PLT reloc against a local symbol directly,
7739 without using the procedure linkage table. */
7740 if (h == NULL)
7741 break;
7742
411e1bfb
AM
7743 /* It's possible that we didn't make a PLT entry for this
7744 symbol. This happens when statically linking PIC code,
7745 or when using -Bsymbolic. Go find a match if there is a
7746 PLT entry. */
7747 if (htab->splt != NULL)
65f38f15 7748 {
411e1bfb
AM
7749 struct plt_entry *ent;
7750 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
7751 if (ent->addend == rel->r_addend
7752 && ent->plt.offset != (bfd_vma) -1)
7753 {
7754 relocation = (htab->splt->output_section->vma
7755 + htab->splt->output_offset
7756 + ent->plt.offset);
7757 unresolved_reloc = FALSE;
7758 }
65f38f15 7759 }
65f38f15 7760 break;
5bd4f169
AM
7761
7762 /* TOC16 relocs. We want the offset relative to the TOC base,
7763 which is the address of the start of the TOC plus 0x8000.
7764 The TOC consists of sections .got, .toc, .tocbss, and .plt,
7765 in this order. */
65f38f15
AM
7766 case R_PPC64_TOC16:
7767 case R_PPC64_TOC16_LO:
7768 case R_PPC64_TOC16_HI:
7769 case R_PPC64_TOC16_DS:
7770 case R_PPC64_TOC16_LO_DS:
7771 case R_PPC64_TOC16_HA:
5bd4f169 7772 addend -= TOCstart + TOC_BASE_OFF;
5bd4f169
AM
7773 break;
7774
7775 /* Relocate against the beginning of the section. */
65f38f15
AM
7776 case R_PPC64_SECTOFF:
7777 case R_PPC64_SECTOFF_LO:
7778 case R_PPC64_SECTOFF_HI:
7779 case R_PPC64_SECTOFF_DS:
7780 case R_PPC64_SECTOFF_LO_DS:
7781 case R_PPC64_SECTOFF_HA:
7782 if (sec != (asection *) 0)
7783 addend -= sec->output_section->vma;
5bd4f169
AM
7784 break;
7785
721956f4
AM
7786 case R_PPC64_REL14:
7787 case R_PPC64_REL14_BRNTAKEN:
7788 case R_PPC64_REL14_BRTAKEN:
5d1634d7
AM
7789 case R_PPC64_REL24:
7790 break;
7791
411e1bfb
AM
7792 case R_PPC64_TPREL16:
7793 case R_PPC64_TPREL16_LO:
7794 case R_PPC64_TPREL16_HI:
7795 case R_PPC64_TPREL16_HA:
7796 case R_PPC64_TPREL16_DS:
7797 case R_PPC64_TPREL16_LO_DS:
7798 case R_PPC64_TPREL16_HIGHER:
7799 case R_PPC64_TPREL16_HIGHERA:
7800 case R_PPC64_TPREL16_HIGHEST:
7801 case R_PPC64_TPREL16_HIGHESTA:
7802 addend -= htab->tls_sec->vma + TP_OFFSET;
7803 if (info->shared)
7804 /* The TPREL16 relocs shouldn't really be used in shared
7805 libs as they will result in DT_TEXTREL being set, but
7806 support them anyway. */
7807 goto dodyn;
7808 break;
7809
7810 case R_PPC64_DTPREL16:
7811 case R_PPC64_DTPREL16_LO:
7812 case R_PPC64_DTPREL16_HI:
7813 case R_PPC64_DTPREL16_HA:
7814 case R_PPC64_DTPREL16_DS:
7815 case R_PPC64_DTPREL16_LO_DS:
7816 case R_PPC64_DTPREL16_HIGHER:
7817 case R_PPC64_DTPREL16_HIGHERA:
7818 case R_PPC64_DTPREL16_HIGHEST:
7819 case R_PPC64_DTPREL16_HIGHESTA:
7820 addend -= htab->tls_sec->vma + DTP_OFFSET;
7821 break;
7822
7823 case R_PPC64_TPREL64:
7824 addend -= htab->tls_sec->vma + TP_OFFSET;
7825 goto dodyn;
7826
7827 case R_PPC64_DTPREL64:
7828 addend -= htab->tls_sec->vma + DTP_OFFSET;
7829 /* Fall thru */
7830
65f38f15
AM
7831 /* Relocations that may need to be propagated if this is a
7832 dynamic object. */
411e1bfb 7833 case R_PPC64_DTPMOD64:
04c9666a 7834 case R_PPC64_REL30:
65f38f15
AM
7835 case R_PPC64_REL32:
7836 case R_PPC64_REL64:
7837 case R_PPC64_ADDR14:
7838 case R_PPC64_ADDR14_BRNTAKEN:
7839 case R_PPC64_ADDR14_BRTAKEN:
7840 case R_PPC64_ADDR16:
7841 case R_PPC64_ADDR16_DS:
7842 case R_PPC64_ADDR16_HA:
7843 case R_PPC64_ADDR16_HI:
7844 case R_PPC64_ADDR16_HIGHER:
7845 case R_PPC64_ADDR16_HIGHERA:
7846 case R_PPC64_ADDR16_HIGHEST:
7847 case R_PPC64_ADDR16_HIGHESTA:
7848 case R_PPC64_ADDR16_LO:
7849 case R_PPC64_ADDR16_LO_DS:
7850 case R_PPC64_ADDR24:
65f38f15
AM
7851 case R_PPC64_ADDR32:
7852 case R_PPC64_ADDR64:
7853 case R_PPC64_UADDR16:
7854 case R_PPC64_UADDR32:
7855 case R_PPC64_UADDR64:
ec338859
AM
7856 /* r_symndx will be zero only for relocs against symbols
7857 from removed linkonce sections, or sections discarded by
7858 a linker script. */
411e1bfb 7859 dodyn:
5d1634d7
AM
7860 if (r_symndx == 0)
7861 break;
7862 /* Fall thru. */
7863
7864 case R_PPC64_TOC:
7865 if ((input_section->flags & SEC_ALLOC) == 0)
ec338859
AM
7866 break;
7867
41bd81ab
AM
7868 if (NO_OPD_RELOCS && is_opd)
7869 break;
7870
65f38f15 7871 if ((info->shared
411e1bfb 7872 && (MUST_BE_DYN_RELOC (r_type)
65f38f15
AM
7873 || (h != NULL
7874 && h->dynindx != -1
7875 && (! info->symbolic
7876 || (h->elf_link_hash_flags
7877 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
7878 || (!info->shared
65f38f15
AM
7879 && h != NULL
7880 && h->dynindx != -1
7881 && (h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
7882 && (((h->elf_link_hash_flags
7883 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
7884 && (h->elf_link_hash_flags
7885 & ELF_LINK_HASH_DEF_REGULAR) == 0)
7886 || h->root.type == bfd_link_hash_undefweak
7887 || h->root.type == bfd_link_hash_undefined)))
7888 {
7889 Elf_Internal_Rela outrel;
b34976b6 7890 bfd_boolean skip, relocate;
65f38f15 7891 asection *sreloc;
947216bf 7892 bfd_byte *loc;
65f38f15
AM
7893
7894 /* When generating a dynamic object, these relocations
7895 are copied into the output file to be resolved at run
7896 time. */
7897
b34976b6
AM
7898 skip = FALSE;
7899 relocate = FALSE;
65f38f15 7900
c629eae0
JJ
7901 outrel.r_offset =
7902 _bfd_elf_section_offset (output_bfd, info, input_section,
7903 rel->r_offset);
7904 if (outrel.r_offset == (bfd_vma) -1)
b34976b6 7905 skip = TRUE;
0bb2d96a 7906 else if (outrel.r_offset == (bfd_vma) -2)
b34976b6 7907 skip = TRUE, relocate = TRUE;
65f38f15
AM
7908 outrel.r_offset += (input_section->output_section->vma
7909 + input_section->output_offset);
411e1bfb 7910 outrel.r_addend = rel->r_addend;
65f38f15
AM
7911
7912 if (skip)
0bb2d96a 7913 memset (&outrel, 0, sizeof outrel);
65f38f15
AM
7914 else if (h != NULL
7915 && h->dynindx != -1
41bd81ab 7916 && !is_opd
411e1bfb 7917 && (!MUST_BE_DYN_RELOC (r_type)
65f38f15
AM
7918 || !info->shared
7919 || !info->symbolic
7920 || (h->elf_link_hash_flags
7921 & ELF_LINK_HASH_DEF_REGULAR) == 0))
0bb2d96a 7922 outrel.r_info = ELF64_R_INFO (h->dynindx, r_type);
65f38f15
AM
7923 else
7924 {
41bd81ab
AM
7925 /* This symbol is local, or marked to become local,
7926 or this is an opd section reloc which must point
7927 at a local function. */
65f38f15 7928 outrel.r_addend += relocation;
411e1bfb 7929 /* ??? why? */
b34976b6 7930 relocate = TRUE;
e86ce104 7931 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
65f38f15 7932 {
3fad3c7c 7933 if (is_opd && h != NULL)
afbe61cf
AM
7934 {
7935 /* Lie about opd entries. This case occurs
7936 when building shared libraries and we
7937 reference a function in another shared
3fad3c7c
AM
7938 lib. The same thing happens for a weak
7939 definition in an application that's
7940 overridden by a strong definition in a
7941 shared lib. (I believe this is a generic
7942 bug in binutils handling of weak syms.)
7943 In these cases we won't use the opd
1e2f5b6e 7944 entry in this lib. */
b34976b6 7945 unresolved_reloc = FALSE;
afbe61cf 7946 }
65f38f15
AM
7947 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
7948 }
7949 else
7950 {
7951 long indx = 0;
7952
41bd81ab 7953 if (bfd_is_abs_section (sec))
65f38f15
AM
7954 ;
7955 else if (sec == NULL || sec->owner == NULL)
7956 {
7957 bfd_set_error (bfd_error_bad_value);
b34976b6 7958 return FALSE;
65f38f15
AM
7959 }
7960 else
7961 {
7962 asection *osec;
7963
7964 osec = sec->output_section;
7965 indx = elf_section_data (osec)->dynindx;
7966
7967 /* We are turning this relocation into one
7968 against a section symbol, so subtract out
7969 the output section's address but not the
7970 offset of the input section in the output
7971 section. */
7972 outrel.r_addend -= osec->vma;
7973 }
7974
7975 outrel.r_info = ELF64_R_INFO (indx, r_type);
7976 }
7977 }
7978
7979 sreloc = elf_section_data (input_section)->sreloc;
7980 if (sreloc == NULL)
7981 abort ();
7982
947216bf
AM
7983 loc = sreloc->contents;
7984 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15
AM
7985 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
7986
7987 /* If this reloc is against an external symbol, it will
7988 be computed at runtime, so there's no need to do
7989 anything now. */
7990 if (! relocate)
7991 continue;
7992 }
5bd4f169
AM
7993 break;
7994
65f38f15
AM
7995 case R_PPC64_COPY:
7996 case R_PPC64_GLOB_DAT:
7997 case R_PPC64_JMP_SLOT:
7998 case R_PPC64_RELATIVE:
7999 /* We shouldn't ever see these dynamic relocs in relocatable
8000 files. */
8001 /* Fall thru */
8002
8003 case R_PPC64_PLTGOT16:
8004 case R_PPC64_PLTGOT16_DS:
8005 case R_PPC64_PLTGOT16_HA:
8006 case R_PPC64_PLTGOT16_HI:
8007 case R_PPC64_PLTGOT16_LO:
8008 case R_PPC64_PLTGOT16_LO_DS:
8009 case R_PPC64_PLTREL32:
8010 case R_PPC64_PLTREL64:
8011 /* These ones haven't been implemented yet. */
8012
8013 (*_bfd_error_handler)
8014 (_("%s: Relocation %s is not supported for symbol %s."),
8015 bfd_archive_filename (input_bfd),
8016 ppc64_elf_howto_table[(int) r_type]->name, sym_name);
5bd4f169
AM
8017
8018 bfd_set_error (bfd_error_invalid_operation);
b34976b6 8019 ret = FALSE;
5bd4f169 8020 continue;
65f38f15 8021 }
5bd4f169 8022
65f38f15
AM
8023 /* Do any further special processing. */
8024 switch (r_type)
8025 {
8026 default:
8027 break;
8028
8029 case R_PPC64_ADDR16_HA:
8030 case R_PPC64_ADDR16_HIGHERA:
8031 case R_PPC64_ADDR16_HIGHESTA:
86bbe32f
AM
8032 case R_PPC64_GOT16_HA:
8033 case R_PPC64_PLTGOT16_HA:
65f38f15
AM
8034 case R_PPC64_PLT16_HA:
8035 case R_PPC64_TOC16_HA:
8036 case R_PPC64_SECTOFF_HA:
411e1bfb
AM
8037 case R_PPC64_TPREL16_HA:
8038 case R_PPC64_DTPREL16_HA:
8039 case R_PPC64_GOT_TLSGD16_HA:
8040 case R_PPC64_GOT_TLSLD16_HA:
8041 case R_PPC64_GOT_TPREL16_HA:
8042 case R_PPC64_GOT_DTPREL16_HA:
8043 case R_PPC64_TPREL16_HIGHER:
8044 case R_PPC64_TPREL16_HIGHERA:
8045 case R_PPC64_TPREL16_HIGHEST:
8046 case R_PPC64_TPREL16_HIGHESTA:
8047 case R_PPC64_DTPREL16_HIGHER:
8048 case R_PPC64_DTPREL16_HIGHERA:
8049 case R_PPC64_DTPREL16_HIGHEST:
8050 case R_PPC64_DTPREL16_HIGHESTA:
65f38f15
AM
8051 /* It's just possible that this symbol is a weak symbol
8052 that's not actually defined anywhere. In that case,
8053 'sec' would be NULL, and we should leave the symbol
8054 alone (it will be set to zero elsewhere in the link). */
8055 if (sec != NULL)
8056 /* Add 0x10000 if sign bit in 0:15 is set. */
8057 addend += ((relocation + addend) & 0x8000) << 1;
8058 break;
8059
8060 case R_PPC64_ADDR16_DS:
8061 case R_PPC64_ADDR16_LO_DS:
8062 case R_PPC64_GOT16_DS:
8063 case R_PPC64_GOT16_LO_DS:
8064 case R_PPC64_PLT16_LO_DS:
8065 case R_PPC64_SECTOFF_DS:
8066 case R_PPC64_SECTOFF_LO_DS:
8067 case R_PPC64_TOC16_DS:
8068 case R_PPC64_TOC16_LO_DS:
8069 case R_PPC64_PLTGOT16_DS:
8070 case R_PPC64_PLTGOT16_LO_DS:
411e1bfb
AM
8071 case R_PPC64_GOT_TPREL16_DS:
8072 case R_PPC64_GOT_TPREL16_LO_DS:
8073 case R_PPC64_GOT_DTPREL16_DS:
8074 case R_PPC64_GOT_DTPREL16_LO_DS:
8075 case R_PPC64_TPREL16_DS:
8076 case R_PPC64_TPREL16_LO_DS:
8077 case R_PPC64_DTPREL16_DS:
8078 case R_PPC64_DTPREL16_LO_DS:
65f38f15
AM
8079 if (((relocation + addend) & 3) != 0)
8080 {
8081 (*_bfd_error_handler)
8082 (_("%s: error: relocation %s not a multiple of 4"),
8083 bfd_archive_filename (input_bfd),
8084 ppc64_elf_howto_table[(int) r_type]->name);
8085 bfd_set_error (bfd_error_bad_value);
b34976b6 8086 ret = FALSE;
65f38f15
AM
8087 continue;
8088 }
8089 break;
721956f4
AM
8090
8091 case R_PPC64_REL14:
8092 case R_PPC64_REL14_BRNTAKEN:
8093 case R_PPC64_REL14_BRTAKEN:
8094 max_br_offset = 1 << 15;
8095 goto branch_check;
8096
8097 case R_PPC64_REL24:
8098 max_br_offset = 1 << 25;
8099
8100 branch_check:
8101 /* If the branch is out of reach, then redirect the
8102 call to the local stub for this function. */
411e1bfb 8103 from = (rel->r_offset
721956f4
AM
8104 + input_section->output_offset
8105 + input_section->output_section->vma);
8106 if (relocation + addend - from + max_br_offset >= 2 * max_br_offset
8107 && (stub_entry = ppc_get_stub_entry (input_section, sec, h,
8108 rel, htab)) != NULL)
8109 {
8110 /* Munge up the value and addend so that we call the stub
8111 rather than the procedure directly. */
8112 relocation = (stub_entry->stub_offset
8113 + stub_entry->stub_sec->output_offset
8114 + stub_entry->stub_sec->output_section->vma);
8115 addend = 0;
8116 }
8117 break;
5bd4f169
AM
8118 }
8119
239e1f3a
AM
8120 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
8121 because such sections are not SEC_ALLOC and thus ld.so will
8122 not process them. */
65f38f15 8123 if (unresolved_reloc
239e1f3a 8124 && !((input_section->flags & SEC_DEBUGGING) != 0
65f38f15 8125 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0))
9c07fe7c
AM
8126 {
8127 (*_bfd_error_handler)
8128 (_("%s(%s+0x%lx): unresolvable relocation against symbol `%s'"),
8129 bfd_archive_filename (input_bfd),
8130 bfd_get_section_name (input_bfd, input_section),
8131 (long) rel->r_offset,
8132 h->root.root.string);
b34976b6 8133 ret = FALSE;
9c07fe7c 8134 }
5bd4f169 8135
65f38f15 8136 r = _bfd_final_link_relocate (ppc64_elf_howto_table[(int) r_type],
5bd4f169
AM
8137 input_bfd,
8138 input_section,
8139 contents,
411e1bfb 8140 rel->r_offset,
5bd4f169
AM
8141 relocation,
8142 addend);
8143
ef60b7ff 8144 if (r != bfd_reloc_ok)
5bd4f169
AM
8145 {
8146 const char *name;
8147
8148 if (h != NULL)
8149 {
8150 if (h->root.type == bfd_link_hash_undefweak
65f38f15 8151 && ppc64_elf_howto_table[(int) r_type]->pc_relative)
5bd4f169
AM
8152 {
8153 /* Assume this is a call protected by other code that
8154 detects the symbol is undefined. If this is the case,
8155 we can safely ignore the overflow. If not, the
8156 program is hosed anyway, and a little warning isn't
8157 going to help. */
8158
8159 continue;
8160 }
8161
8162 name = h->root.root.string;
8163 }
8164 else
8165 {
411e1bfb 8166 name = bfd_elf_local_sym_name (input_bfd, sym);
5bd4f169
AM
8167 if (name == NULL)
8168 continue;
5bd4f169
AM
8169 }
8170
ef60b7ff
AM
8171 if (r == bfd_reloc_overflow)
8172 {
8173 if (warned)
8174 continue;
8175 if (!((*info->callbacks->reloc_overflow)
8176 (info, name, ppc64_elf_howto_table[(int) r_type]->name,
411e1bfb 8177 rel->r_addend, input_bfd, input_section, rel->r_offset)))
b34976b6 8178 return FALSE;
ef60b7ff
AM
8179 }
8180 else
8181 {
8182 (*_bfd_error_handler)
8183 (_("%s(%s+0x%lx): reloc against `%s': error %d"),
8184 bfd_archive_filename (input_bfd),
8185 bfd_get_section_name (input_bfd, input_section),
8186 (long) rel->r_offset, name, (int) r);
b34976b6 8187 ret = FALSE;
ef60b7ff 8188 }
5bd4f169 8189 }
5bd4f169
AM
8190 }
8191
5bd4f169
AM
8192 return ret;
8193}
8194
8195/* Finish up dynamic symbol handling. We set the contents of various
8196 dynamic sections here. */
8197
b34976b6 8198static bfd_boolean
5bd4f169
AM
8199ppc64_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
8200 bfd *output_bfd;
8201 struct bfd_link_info *info;
8202 struct elf_link_hash_entry *h;
8203 Elf_Internal_Sym *sym;
8204{
65f38f15 8205 struct ppc_link_hash_table *htab;
5bd4f169
AM
8206 bfd *dynobj;
8207
65f38f15
AM
8208 htab = ppc_hash_table (info);
8209 dynobj = htab->elf.dynobj;
5bd4f169 8210
411e1bfb 8211 if (((struct ppc_link_hash_entry *) h)->is_func_descriptor)
5bd4f169 8212 {
411e1bfb 8213 struct plt_entry *ent;
65f38f15 8214 Elf_Internal_Rela rela;
947216bf 8215 bfd_byte *loc;
5bd4f169 8216
411e1bfb
AM
8217 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
8218 if (ent->plt.offset != (bfd_vma) -1)
8219 {
8220 /* This symbol has an entry in the procedure linkage
8221 table. Set it up. */
5bd4f169 8222
411e1bfb
AM
8223 if (htab->splt == NULL
8224 || htab->srelplt == NULL
8225 || htab->sglink == NULL)
8226 abort ();
5bd4f169 8227
411e1bfb
AM
8228 /* Create a JMP_SLOT reloc to inform the dynamic linker to
8229 fill in the PLT entry. */
5bd4f169 8230
411e1bfb
AM
8231 rela.r_offset = (htab->splt->output_section->vma
8232 + htab->splt->output_offset
8233 + ent->plt.offset);
8234 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
8235 rela.r_addend = ent->addend;
5bd4f169 8236
411e1bfb
AM
8237 loc = htab->srelplt->contents;
8238 loc += ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE) / PLT_ENTRY_SIZE
8239 * sizeof (Elf64_External_Rela));
8240 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
8241 }
5bd4f169
AM
8242 }
8243
8244 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
8245 {
5bd4f169 8246 Elf_Internal_Rela rela;
947216bf 8247 bfd_byte *loc;
5bd4f169 8248
65f38f15 8249 /* This symbol needs a copy reloc. Set it up. */
5bd4f169 8250
65f38f15
AM
8251 if (h->dynindx == -1
8252 || (h->root.type != bfd_link_hash_defined
8253 && h->root.type != bfd_link_hash_defweak)
8254 || htab->srelbss == NULL)
8255 abort ();
5bd4f169
AM
8256
8257 rela.r_offset = (h->root.u.def.value
8258 + h->root.u.def.section->output_section->vma
8259 + h->root.u.def.section->output_offset);
8260 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
8261 rela.r_addend = 0;
947216bf
AM
8262 loc = htab->srelbss->contents;
8263 loc += htab->srelbss->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15 8264 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
5bd4f169
AM
8265 }
8266
5bd4f169
AM
8267 /* Mark some specially defined symbols as absolute. */
8268 if (strcmp (h->root.root.string, "_DYNAMIC") == 0)
8269 sym->st_shndx = SHN_ABS;
8270
b34976b6 8271 return TRUE;
5bd4f169
AM
8272}
8273
65f38f15
AM
8274/* Used to decide how to sort relocs in an optimal manner for the
8275 dynamic linker, before writing them out. */
8276
8277static enum elf_reloc_type_class
8278ppc64_elf_reloc_type_class (rela)
8279 const Elf_Internal_Rela *rela;
8280{
04c9666a 8281 enum elf_ppc64_reloc_type r_type;
a33d1f77 8282
04c9666a 8283 r_type = (enum elf_ppc64_reloc_type) ELF64_R_TYPE (rela->r_info);
a33d1f77 8284 switch (r_type)
65f38f15
AM
8285 {
8286 case R_PPC64_RELATIVE:
8287 return reloc_class_relative;
8288 case R_PPC64_JMP_SLOT:
8289 return reloc_class_plt;
8290 case R_PPC64_COPY:
8291 return reloc_class_copy;
8292 default:
8293 return reloc_class_normal;
8294 }
8295}
8296
5bd4f169
AM
8297/* Finish up the dynamic sections. */
8298
b34976b6 8299static bfd_boolean
5bd4f169
AM
8300ppc64_elf_finish_dynamic_sections (output_bfd, info)
8301 bfd *output_bfd;
8302 struct bfd_link_info *info;
8303{
65f38f15
AM
8304 struct ppc_link_hash_table *htab;
8305 bfd *dynobj;
5bd4f169 8306 asection *sdyn;
5bd4f169 8307
65f38f15
AM
8308 htab = ppc_hash_table (info);
8309 dynobj = htab->elf.dynobj;
5bd4f169
AM
8310 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
8311
65f38f15 8312 if (htab->elf.dynamic_sections_created)
5bd4f169 8313 {
5bd4f169
AM
8314 Elf64_External_Dyn *dyncon, *dynconend;
8315
65f38f15
AM
8316 if (sdyn == NULL || htab->sgot == NULL)
8317 abort ();
5bd4f169
AM
8318
8319 dyncon = (Elf64_External_Dyn *) sdyn->contents;
8320 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
8321 for (; dyncon < dynconend; dyncon++)
8322 {
8323 Elf_Internal_Dyn dyn;
19397422 8324 asection *s;
5bd4f169
AM
8325
8326 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
8327
8328 switch (dyn.d_tag)
8329 {
65f38f15
AM
8330 default:
8331 continue;
5bd4f169 8332
5d1634d7 8333 case DT_PPC64_GLINK:
6348e046
AM
8334 s = htab->sglink;
8335 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
5d1634d7
AM
8336 break;
8337
19397422
AM
8338 case DT_PPC64_OPD:
8339 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
8340 if (s == NULL)
8341 continue;
8342 dyn.d_un.d_ptr = s->vma;
19397422
AM
8343 break;
8344
8345 case DT_PPC64_OPDSZ:
8346 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
8347 if (s == NULL)
8348 continue;
8349 dyn.d_un.d_val = s->_raw_size;
19397422
AM
8350 break;
8351
65f38f15 8352 case DT_PLTGOT:
6348e046
AM
8353 s = htab->splt;
8354 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15
AM
8355 break;
8356
8357 case DT_JMPREL:
6348e046
AM
8358 s = htab->srelplt;
8359 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15 8360 break;
5bd4f169 8361
65f38f15 8362 case DT_PLTRELSZ:
5d1634d7
AM
8363 dyn.d_un.d_val = htab->srelplt->_raw_size;
8364 break;
8365
8366 case DT_RELASZ:
8367 /* Don't count procedure linkage table relocs in the
8368 overall reloc count. */
6348e046
AM
8369 s = htab->srelplt;
8370 if (s == NULL)
8371 continue;
8372 dyn.d_un.d_val -= s->_raw_size;
8373 break;
8374
8375 case DT_RELA:
8376 /* We may not be using the standard ELF linker script.
8377 If .rela.plt is the first .rela section, we adjust
8378 DT_RELA to not include it. */
8379 s = htab->srelplt;
8380 if (s == NULL)
8381 continue;
8382 if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
8383 continue;
8384 dyn.d_un.d_ptr += s->_raw_size;
65f38f15 8385 break;
5bd4f169 8386 }
5bd4f169 8387
65f38f15 8388 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
5bd4f169 8389 }
5bd4f169
AM
8390 }
8391
5d1634d7
AM
8392 if (htab->sgot != NULL && htab->sgot->_raw_size != 0)
8393 {
8394 /* Fill in the first entry in the global offset table.
8395 We use it to hold the link-time TOCbase. */
8396 bfd_put_64 (output_bfd,
60ee0d4a 8397 elf_gp (output_bfd) + TOC_BASE_OFF,
5d1634d7
AM
8398 htab->sgot->contents);
8399
8400 /* Set .got entry size. */
8401 elf_section_data (htab->sgot->output_section)->this_hdr.sh_entsize = 8;
8402 }
8403
8404 if (htab->splt != NULL && htab->splt->_raw_size != 0)
8405 {
8406 /* Set .plt entry size. */
8407 elf_section_data (htab->splt->output_section)->this_hdr.sh_entsize
8408 = PLT_ENTRY_SIZE;
8409 }
8410
b34976b6 8411 return TRUE;
5bd4f169
AM
8412}
8413
8414#define TARGET_LITTLE_SYM bfd_elf64_powerpcle_vec
8415#define TARGET_LITTLE_NAME "elf64-powerpcle"
8416#define TARGET_BIG_SYM bfd_elf64_powerpc_vec
8417#define TARGET_BIG_NAME "elf64-powerpc"
8418#define ELF_ARCH bfd_arch_powerpc
8419#define ELF_MACHINE_CODE EM_PPC64
8420#define ELF_MAXPAGESIZE 0x10000
8421#define elf_info_to_howto ppc64_elf_info_to_howto
8422
8423#ifdef EM_CYGNUS_POWERPC
8424#define ELF_MACHINE_ALT1 EM_CYGNUS_POWERPC
8425#endif
8426
8427#ifdef EM_PPC_OLD
8428#define ELF_MACHINE_ALT2 EM_PPC_OLD
8429#endif
8430
8431#define elf_backend_want_got_sym 0
8432#define elf_backend_want_plt_sym 0
8433#define elf_backend_plt_alignment 3
8434#define elf_backend_plt_not_loaded 1
8435#define elf_backend_got_symbol_offset 0
5d1634d7
AM
8436#define elf_backend_got_header_size 8
8437#define elf_backend_plt_header_size PLT_INITIAL_ENTRY_SIZE
5bd4f169 8438#define elf_backend_can_gc_sections 1
65f38f15 8439#define elf_backend_can_refcount 1
b491616a 8440#define elf_backend_rela_normal 1
5bd4f169 8441
65f38f15 8442#define bfd_elf64_bfd_reloc_type_lookup ppc64_elf_reloc_type_lookup
65f38f15 8443#define bfd_elf64_bfd_merge_private_bfd_data ppc64_elf_merge_private_bfd_data
f0abc2a1 8444#define bfd_elf64_new_section_hook ppc64_elf_new_section_hook
65f38f15 8445#define bfd_elf64_bfd_link_hash_table_create ppc64_elf_link_hash_table_create
721956f4 8446#define bfd_elf64_bfd_link_hash_table_free ppc64_elf_link_hash_table_free
65f38f15 8447
feee612b 8448#define elf_backend_object_p ppc64_elf_object_p
65f38f15
AM
8449#define elf_backend_create_dynamic_sections ppc64_elf_create_dynamic_sections
8450#define elf_backend_copy_indirect_symbol ppc64_elf_copy_indirect_symbol
8451#define elf_backend_check_relocs ppc64_elf_check_relocs
8452#define elf_backend_gc_mark_hook ppc64_elf_gc_mark_hook
8453#define elf_backend_gc_sweep_hook ppc64_elf_gc_sweep_hook
8454#define elf_backend_adjust_dynamic_symbol ppc64_elf_adjust_dynamic_symbol
e86ce104
AM
8455#define elf_backend_hide_symbol ppc64_elf_hide_symbol
8456#define elf_backend_always_size_sections ppc64_elf_func_desc_adjust
65f38f15 8457#define elf_backend_size_dynamic_sections ppc64_elf_size_dynamic_sections
65f38f15
AM
8458#define elf_backend_relocate_section ppc64_elf_relocate_section
8459#define elf_backend_finish_dynamic_symbol ppc64_elf_finish_dynamic_symbol
8460#define elf_backend_reloc_type_class ppc64_elf_reloc_type_class
8461#define elf_backend_finish_dynamic_sections ppc64_elf_finish_dynamic_sections
5bd4f169
AM
8462
8463#include "elf64-target.h"
This page took 0.712959 seconds and 4 git commands to generate.