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