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