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