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