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