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