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