*** empty log message ***
[deliverable/binutils-gdb.git] / bfd / elf64-ppc.c
CommitLineData
5bd4f169 1/* PowerPC64-specific support for 64-bit ELF.
3a71aa26 2 Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
df22d223 3 2009, 2010, 2011 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.
32ca9640 6 Largely rewritten by Alan Modra.
5bd4f169 7
ae9a127f 8 This file is part of BFD, the Binary File Descriptor library.
5bd4f169 9
ae9a127f
NC
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
cd123cb7 12 the Free Software Foundation; either version 3 of the License, or
ae9a127f 13 (at your option) any later version.
5bd4f169 14
ae9a127f
NC
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
5bd4f169 19
4ce794b7
AM
20 You should have received a copy of the GNU General Public License along
21 with this program; if not, write to the Free Software Foundation, Inc.,
3e110533 22 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
5bd4f169 23
cd123cb7 24
4ce794b7
AM
25/* The 64-bit PowerPC ELF ABI may be found at
26 http://www.linuxbase.org/spec/ELF/ppc64/PPC-elf64abi.txt, and
27 http://www.linuxbase.org/spec/ELF/ppc64/spec/book1.html */
5bd4f169 28
3db64b00 29#include "sysdep.h"
183e98be 30#include <stdarg.h>
5bd4f169 31#include "bfd.h"
5bd4f169
AM
32#include "bfdlink.h"
33#include "libbfd.h"
34#include "elf-bfd.h"
04c9666a 35#include "elf/ppc64.h"
5d1634d7 36#include "elf64-ppc.h"
58d180e8 37#include "dwarf2.h"
5bd4f169 38
805fc799 39static bfd_reloc_status_type ppc64_elf_ha_reloc
4ce794b7 40 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
2441e016
AM
41static bfd_reloc_status_type ppc64_elf_branch_reloc
42 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 43static bfd_reloc_status_type ppc64_elf_brtaken_reloc
4ce794b7 44 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 45static bfd_reloc_status_type ppc64_elf_sectoff_reloc
4ce794b7 46 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 47static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
4ce794b7 48 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 49static bfd_reloc_status_type ppc64_elf_toc_reloc
4ce794b7 50 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 51static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
4ce794b7 52 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 53static bfd_reloc_status_type ppc64_elf_toc64_reloc
4ce794b7 54 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 55static bfd_reloc_status_type ppc64_elf_unhandled_reloc
4ce794b7 56 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
2441e016
AM
57static bfd_vma opd_entry_value
58 (asection *, bfd_vma, asection **, bfd_vma *);
5bd4f169 59
ad8e1ba5
AM
60#define TARGET_LITTLE_SYM bfd_elf64_powerpcle_vec
61#define TARGET_LITTLE_NAME "elf64-powerpcle"
62#define TARGET_BIG_SYM bfd_elf64_powerpc_vec
63#define TARGET_BIG_NAME "elf64-powerpc"
64#define ELF_ARCH bfd_arch_powerpc
ae95ffa6 65#define ELF_TARGET_ID PPC64_ELF_DATA
ad8e1ba5
AM
66#define ELF_MACHINE_CODE EM_PPC64
67#define ELF_MAXPAGESIZE 0x10000
24718e3b 68#define ELF_COMMONPAGESIZE 0x1000
ad8e1ba5
AM
69#define elf_info_to_howto ppc64_elf_info_to_howto
70
71#define elf_backend_want_got_sym 0
72#define elf_backend_want_plt_sym 0
73#define elf_backend_plt_alignment 3
74#define elf_backend_plt_not_loaded 1
ad8e1ba5 75#define elf_backend_got_header_size 8
ad8e1ba5
AM
76#define elf_backend_can_gc_sections 1
77#define elf_backend_can_refcount 1
78#define elf_backend_rela_normal 1
6bfdb61b 79#define elf_backend_default_execstack 0
ad8e1ba5 80
e717da7e 81#define bfd_elf64_mkobject ppc64_elf_mkobject
ad8e1ba5 82#define bfd_elf64_bfd_reloc_type_lookup ppc64_elf_reloc_type_lookup
aa374f67 83#define bfd_elf64_bfd_reloc_name_lookup ppc64_elf_reloc_name_lookup
c79d6685 84#define bfd_elf64_bfd_merge_private_bfd_data _bfd_generic_verify_endian_match
ad8e1ba5
AM
85#define bfd_elf64_new_section_hook ppc64_elf_new_section_hook
86#define bfd_elf64_bfd_link_hash_table_create ppc64_elf_link_hash_table_create
87#define bfd_elf64_bfd_link_hash_table_free ppc64_elf_link_hash_table_free
90e3cdf2 88#define bfd_elf64_get_synthetic_symtab ppc64_elf_get_synthetic_symtab
aa374f67 89#define bfd_elf64_bfd_link_just_syms ppc64_elf_link_just_syms
ad8e1ba5
AM
90
91#define elf_backend_object_p ppc64_elf_object_p
d37c89e5
AM
92#define elf_backend_grok_prstatus ppc64_elf_grok_prstatus
93#define elf_backend_grok_psinfo ppc64_elf_grok_psinfo
183e98be 94#define elf_backend_write_core_note ppc64_elf_write_core_note
ad8e1ba5
AM
95#define elf_backend_create_dynamic_sections ppc64_elf_create_dynamic_sections
96#define elf_backend_copy_indirect_symbol ppc64_elf_copy_indirect_symbol
555cd476 97#define elf_backend_add_symbol_hook ppc64_elf_add_symbol_hook
7d9616d7 98#define elf_backend_check_directives ppc64_elf_process_dot_syms
97fed1c9 99#define elf_backend_as_needed_cleanup ppc64_elf_as_needed_cleanup
8387904d 100#define elf_backend_archive_symbol_lookup ppc64_elf_archive_symbol_lookup
ad8e1ba5 101#define elf_backend_check_relocs ppc64_elf_check_relocs
74f0fb50 102#define elf_backend_gc_keep ppc64_elf_gc_keep
64d03ab5 103#define elf_backend_gc_mark_dynamic_ref ppc64_elf_gc_mark_dynamic_ref
ad8e1ba5
AM
104#define elf_backend_gc_mark_hook ppc64_elf_gc_mark_hook
105#define elf_backend_gc_sweep_hook ppc64_elf_gc_sweep_hook
106#define elf_backend_adjust_dynamic_symbol ppc64_elf_adjust_dynamic_symbol
107#define elf_backend_hide_symbol ppc64_elf_hide_symbol
108#define elf_backend_always_size_sections ppc64_elf_func_desc_adjust
109#define elf_backend_size_dynamic_sections ppc64_elf_size_dynamic_sections
74541ad4 110#define elf_backend_init_index_section _bfd_elf_init_2_index_sections
60124e18 111#define elf_backend_action_discarded ppc64_elf_action_discarded
ad8e1ba5
AM
112#define elf_backend_relocate_section ppc64_elf_relocate_section
113#define elf_backend_finish_dynamic_symbol ppc64_elf_finish_dynamic_symbol
114#define elf_backend_reloc_type_class ppc64_elf_reloc_type_class
115#define elf_backend_finish_dynamic_sections ppc64_elf_finish_dynamic_sections
754021d0 116#define elf_backend_link_output_symbol_hook ppc64_elf_output_symbol_hook
29ef7005 117#define elf_backend_special_sections ppc64_elf_special_sections
e054468f 118#define elf_backend_post_process_headers _bfd_elf_set_osabi
ad8e1ba5 119
5bd4f169
AM
120/* The name of the dynamic interpreter. This is put in the .interp
121 section. */
122#define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
123
124/* The size in bytes of an entry in the procedure linkage table. */
125#define PLT_ENTRY_SIZE 24
126
127/* The initial size of the plt reserved for the dynamic linker. */
5d1634d7 128#define PLT_INITIAL_ENTRY_SIZE PLT_ENTRY_SIZE
5bd4f169
AM
129
130/* TOC base pointers offset from start of TOC. */
411e1bfb
AM
131#define TOC_BASE_OFF 0x8000
132
133/* Offset of tp and dtp pointers from start of TLS block. */
134#define TP_OFFSET 0x7000
135#define DTP_OFFSET 0x8000
5bd4f169 136
ad8e1ba5
AM
137/* .plt call stub instructions. The normal stub is like this, but
138 sometimes the .plt entry crosses a 64k boundary and we need to
ac2df442 139 insert an addi to adjust r12. */
ad8e1ba5 140#define PLT_CALL_STUB_SIZE (7*4)
5d1634d7
AM
141#define ADDIS_R12_R2 0x3d820000 /* addis %r12,%r2,xxx@ha */
142#define STD_R2_40R1 0xf8410028 /* std %r2,40(%r1) */
143#define LD_R11_0R12 0xe96c0000 /* ld %r11,xxx+0@l(%r12) */
5d1634d7 144#define MTCTR_R11 0x7d6903a6 /* mtctr %r11 */
ac2df442 145#define LD_R2_0R12 0xe84c0000 /* ld %r2,xxx+8@l(%r12) */
5d1634d7
AM
146 /* ld %r11,xxx+16@l(%r12) */
147#define BCTR 0x4e800420 /* bctr */
148
5d1634d7 149
ee4bf8d2 150#define ADDIS_R12_R12 0x3d8c0000 /* addis %r12,%r12,off@ha */
ac2df442 151#define ADDI_R12_R12 0x398c0000 /* addi %r12,%r12,off@l */
ad8e1ba5
AM
152#define ADDIS_R2_R2 0x3c420000 /* addis %r2,%r2,off@ha */
153#define ADDI_R2_R2 0x38420000 /* addi %r2,%r2,off@l */
154
ac2df442
AM
155#define LD_R11_0R2 0xe9620000 /* ld %r11,xxx+0(%r2) */
156#define LD_R2_0R2 0xe8420000 /* ld %r2,xxx+0(%r2) */
157
ad8e1ba5
AM
158#define LD_R2_40R1 0xe8410028 /* ld %r2,40(%r1) */
159
ee4bf8d2 160/* glink call stub instructions. We enter with the index in R0. */
ad8e1ba5 161#define GLINK_CALL_STUB_SIZE (16*4)
ee4bf8d2
AM
162 /* 0: */
163 /* .quad plt0-1f */
164 /* __glink: */
165#define MFLR_R12 0x7d8802a6 /* mflr %12 */
166#define BCL_20_31 0x429f0005 /* bcl 20,31,1f */
167 /* 1: */
168#define MFLR_R11 0x7d6802a6 /* mflr %11 */
169#define LD_R2_M16R11 0xe84bfff0 /* ld %2,(0b-1b)(%11) */
170#define MTLR_R12 0x7d8803a6 /* mtlr %12 */
171#define ADD_R12_R2_R11 0x7d825a14 /* add %12,%2,%11 */
172 /* ld %11,0(%12) */
173 /* ld %2,8(%12) */
174 /* mtctr %11 */
175 /* ld %11,16(%12) */
176 /* bctr */
5d1634d7
AM
177
178/* Pad with this. */
179#define NOP 0x60000000
180
721956f4
AM
181/* Some other nops. */
182#define CROR_151515 0x4def7b82
183#define CROR_313131 0x4ffffb82
184
cedb70c5 185/* .glink entries for the first 32k functions are two instructions. */
5d1634d7
AM
186#define LI_R0_0 0x38000000 /* li %r0,0 */
187#define B_DOT 0x48000000 /* b . */
188
189/* After that, we need two instructions to load the index, followed by
190 a branch. */
191#define LIS_R0_0 0x3c000000 /* lis %r0,0 */
10ed1bba 192#define ORI_R0_R0_0 0x60000000 /* ori %r0,%r0,0 */
41bd81ab 193
deb0e272
AM
194/* Instructions used by the save and restore reg functions. */
195#define STD_R0_0R1 0xf8010000 /* std %r0,0(%r1) */
196#define STD_R0_0R12 0xf80c0000 /* std %r0,0(%r12) */
197#define LD_R0_0R1 0xe8010000 /* ld %r0,0(%r1) */
198#define LD_R0_0R12 0xe80c0000 /* ld %r0,0(%r12) */
82bd7b59
AM
199#define STFD_FR0_0R1 0xd8010000 /* stfd %fr0,0(%r1) */
200#define LFD_FR0_0R1 0xc8010000 /* lfd %fr0,0(%r1) */
deb0e272
AM
201#define LI_R12_0 0x39800000 /* li %r12,0 */
202#define STVX_VR0_R12_R0 0x7c0c01ce /* stvx %v0,%r12,%r0 */
203#define LVX_VR0_R12_R0 0x7c0c00ce /* lvx %v0,%r12,%r0 */
204#define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
82bd7b59
AM
205#define BLR 0x4e800020 /* blr */
206
41bd81ab
AM
207/* Since .opd is an array of descriptors and each entry will end up
208 with identical R_PPC64_RELATIVE relocs, there is really no need to
209 propagate .opd relocs; The dynamic linker should be taught to
1e2f5b6e 210 relocate .opd without reloc entries. */
41bd81ab
AM
211#ifndef NO_OPD_RELOCS
212#define NO_OPD_RELOCS 0
213#endif
5bd4f169 214\f
f5e87a1d 215#define ONES(n) (((bfd_vma) 1 << ((n) - 1) << 1) - 1)
b34976b6 216
5bd4f169 217/* Relocation HOWTO's. */
04c9666a 218static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
5bd4f169
AM
219
220static reloc_howto_type ppc64_elf_howto_raw[] = {
221 /* This reloc does nothing. */
222 HOWTO (R_PPC64_NONE, /* type */
223 0, /* rightshift */
411e1bfb
AM
224 2, /* size (0 = byte, 1 = short, 2 = long) */
225 32, /* bitsize */
b34976b6 226 FALSE, /* pc_relative */
5bd4f169 227 0, /* bitpos */
f5e87a1d 228 complain_overflow_dont, /* complain_on_overflow */
5bd4f169
AM
229 bfd_elf_generic_reloc, /* special_function */
230 "R_PPC64_NONE", /* name */
b34976b6 231 FALSE, /* partial_inplace */
d006db6c 232 0, /* src_mask */
5bd4f169 233 0, /* dst_mask */
b34976b6 234 FALSE), /* pcrel_offset */
5bd4f169
AM
235
236 /* A standard 32 bit relocation. */
237 HOWTO (R_PPC64_ADDR32, /* type */
238 0, /* rightshift */
239 2, /* size (0 = byte, 1 = short, 2 = long) */
240 32, /* bitsize */
b34976b6 241 FALSE, /* pc_relative */
5bd4f169
AM
242 0, /* bitpos */
243 complain_overflow_bitfield, /* complain_on_overflow */
244 bfd_elf_generic_reloc, /* special_function */
245 "R_PPC64_ADDR32", /* name */
b34976b6 246 FALSE, /* partial_inplace */
5bd4f169
AM
247 0, /* src_mask */
248 0xffffffff, /* dst_mask */
b34976b6 249 FALSE), /* pcrel_offset */
5bd4f169
AM
250
251 /* An absolute 26 bit branch; the lower two bits must be zero.
252 FIXME: we don't check that, we just clear them. */
253 HOWTO (R_PPC64_ADDR24, /* type */
254 0, /* rightshift */
255 2, /* size (0 = byte, 1 = short, 2 = long) */
256 26, /* bitsize */
b34976b6 257 FALSE, /* pc_relative */
5bd4f169
AM
258 0, /* bitpos */
259 complain_overflow_bitfield, /* complain_on_overflow */
260 bfd_elf_generic_reloc, /* special_function */
261 "R_PPC64_ADDR24", /* name */
b34976b6 262 FALSE, /* partial_inplace */
d006db6c 263 0, /* src_mask */
f5e87a1d 264 0x03fffffc, /* dst_mask */
b34976b6 265 FALSE), /* pcrel_offset */
5bd4f169
AM
266
267 /* A standard 16 bit relocation. */
268 HOWTO (R_PPC64_ADDR16, /* type */
269 0, /* rightshift */
270 1, /* size (0 = byte, 1 = short, 2 = long) */
271 16, /* bitsize */
b34976b6 272 FALSE, /* pc_relative */
5bd4f169
AM
273 0, /* bitpos */
274 complain_overflow_bitfield, /* complain_on_overflow */
275 bfd_elf_generic_reloc, /* special_function */
276 "R_PPC64_ADDR16", /* name */
b34976b6 277 FALSE, /* partial_inplace */
5bd4f169
AM
278 0, /* src_mask */
279 0xffff, /* dst_mask */
b34976b6 280 FALSE), /* pcrel_offset */
5bd4f169
AM
281
282 /* A 16 bit relocation without overflow. */
283 HOWTO (R_PPC64_ADDR16_LO, /* type */
284 0, /* rightshift */
285 1, /* size (0 = byte, 1 = short, 2 = long) */
286 16, /* bitsize */
b34976b6 287 FALSE, /* pc_relative */
5bd4f169
AM
288 0, /* bitpos */
289 complain_overflow_dont,/* complain_on_overflow */
290 bfd_elf_generic_reloc, /* special_function */
291 "R_PPC64_ADDR16_LO", /* name */
b34976b6 292 FALSE, /* partial_inplace */
5bd4f169
AM
293 0, /* src_mask */
294 0xffff, /* dst_mask */
b34976b6 295 FALSE), /* pcrel_offset */
5bd4f169
AM
296
297 /* Bits 16-31 of an address. */
298 HOWTO (R_PPC64_ADDR16_HI, /* type */
299 16, /* rightshift */
300 1, /* size (0 = byte, 1 = short, 2 = long) */
301 16, /* bitsize */
b34976b6 302 FALSE, /* pc_relative */
5bd4f169
AM
303 0, /* bitpos */
304 complain_overflow_dont, /* complain_on_overflow */
305 bfd_elf_generic_reloc, /* special_function */
306 "R_PPC64_ADDR16_HI", /* name */
b34976b6 307 FALSE, /* partial_inplace */
5bd4f169
AM
308 0, /* src_mask */
309 0xffff, /* dst_mask */
b34976b6 310 FALSE), /* pcrel_offset */
5bd4f169
AM
311
312 /* Bits 16-31 of an address, plus 1 if the contents of the low 16
313 bits, treated as a signed number, is negative. */
314 HOWTO (R_PPC64_ADDR16_HA, /* type */
315 16, /* rightshift */
316 1, /* size (0 = byte, 1 = short, 2 = long) */
317 16, /* bitsize */
b34976b6 318 FALSE, /* pc_relative */
5bd4f169
AM
319 0, /* bitpos */
320 complain_overflow_dont, /* complain_on_overflow */
805fc799 321 ppc64_elf_ha_reloc, /* special_function */
5bd4f169 322 "R_PPC64_ADDR16_HA", /* name */
b34976b6 323 FALSE, /* partial_inplace */
5bd4f169
AM
324 0, /* src_mask */
325 0xffff, /* dst_mask */
b34976b6 326 FALSE), /* pcrel_offset */
5bd4f169
AM
327
328 /* An absolute 16 bit branch; the lower two bits must be zero.
329 FIXME: we don't check that, we just clear them. */
330 HOWTO (R_PPC64_ADDR14, /* type */
331 0, /* rightshift */
332 2, /* size (0 = byte, 1 = short, 2 = long) */
333 16, /* bitsize */
b34976b6 334 FALSE, /* pc_relative */
5bd4f169
AM
335 0, /* bitpos */
336 complain_overflow_bitfield, /* complain_on_overflow */
2441e016 337 ppc64_elf_branch_reloc, /* special_function */
5bd4f169 338 "R_PPC64_ADDR14", /* name */
b34976b6 339 FALSE, /* partial_inplace */
d006db6c 340 0, /* src_mask */
f5e87a1d 341 0x0000fffc, /* dst_mask */
b34976b6 342 FALSE), /* pcrel_offset */
5bd4f169
AM
343
344 /* An absolute 16 bit branch, for which bit 10 should be set to
345 indicate that the branch is expected to be taken. The lower two
346 bits must be zero. */
347 HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
348 0, /* rightshift */
349 2, /* size (0 = byte, 1 = short, 2 = long) */
350 16, /* bitsize */
b34976b6 351 FALSE, /* pc_relative */
5bd4f169
AM
352 0, /* bitpos */
353 complain_overflow_bitfield, /* complain_on_overflow */
805fc799 354 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 355 "R_PPC64_ADDR14_BRTAKEN",/* name */
b34976b6 356 FALSE, /* partial_inplace */
d006db6c 357 0, /* src_mask */
f5e87a1d 358 0x0000fffc, /* dst_mask */
b34976b6 359 FALSE), /* pcrel_offset */
5bd4f169
AM
360
361 /* An absolute 16 bit branch, for which bit 10 should be set to
362 indicate that the branch is not expected to be taken. The lower
363 two bits must be zero. */
364 HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
365 0, /* rightshift */
366 2, /* size (0 = byte, 1 = short, 2 = long) */
367 16, /* bitsize */
b34976b6 368 FALSE, /* pc_relative */
5bd4f169
AM
369 0, /* bitpos */
370 complain_overflow_bitfield, /* complain_on_overflow */
805fc799 371 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 372 "R_PPC64_ADDR14_BRNTAKEN",/* name */
b34976b6 373 FALSE, /* partial_inplace */
d006db6c 374 0, /* src_mask */
f5e87a1d 375 0x0000fffc, /* dst_mask */
b34976b6 376 FALSE), /* pcrel_offset */
5bd4f169
AM
377
378 /* A relative 26 bit branch; the lower two bits must be zero. */
379 HOWTO (R_PPC64_REL24, /* type */
380 0, /* rightshift */
381 2, /* size (0 = byte, 1 = short, 2 = long) */
382 26, /* bitsize */
b34976b6 383 TRUE, /* pc_relative */
5bd4f169
AM
384 0, /* bitpos */
385 complain_overflow_signed, /* complain_on_overflow */
2441e016 386 ppc64_elf_branch_reloc, /* special_function */
5bd4f169 387 "R_PPC64_REL24", /* name */
b34976b6 388 FALSE, /* partial_inplace */
d006db6c 389 0, /* src_mask */
f5e87a1d 390 0x03fffffc, /* dst_mask */
b34976b6 391 TRUE), /* pcrel_offset */
5bd4f169
AM
392
393 /* A relative 16 bit branch; the lower two bits must be zero. */
394 HOWTO (R_PPC64_REL14, /* type */
395 0, /* rightshift */
396 2, /* size (0 = byte, 1 = short, 2 = long) */
397 16, /* bitsize */
b34976b6 398 TRUE, /* pc_relative */
5bd4f169
AM
399 0, /* bitpos */
400 complain_overflow_signed, /* complain_on_overflow */
2441e016 401 ppc64_elf_branch_reloc, /* special_function */
5bd4f169 402 "R_PPC64_REL14", /* name */
b34976b6 403 FALSE, /* partial_inplace */
d006db6c 404 0, /* src_mask */
f5e87a1d 405 0x0000fffc, /* dst_mask */
b34976b6 406 TRUE), /* pcrel_offset */
5bd4f169
AM
407
408 /* A relative 16 bit branch. Bit 10 should be set to indicate that
409 the branch is expected to be taken. The lower two bits must be
410 zero. */
411 HOWTO (R_PPC64_REL14_BRTAKEN, /* type */
412 0, /* rightshift */
413 2, /* size (0 = byte, 1 = short, 2 = long) */
414 16, /* bitsize */
b34976b6 415 TRUE, /* pc_relative */
5bd4f169
AM
416 0, /* bitpos */
417 complain_overflow_signed, /* complain_on_overflow */
805fc799 418 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 419 "R_PPC64_REL14_BRTAKEN", /* name */
b34976b6 420 FALSE, /* partial_inplace */
d006db6c 421 0, /* src_mask */
f5e87a1d 422 0x0000fffc, /* dst_mask */
b34976b6 423 TRUE), /* pcrel_offset */
5bd4f169
AM
424
425 /* A relative 16 bit branch. Bit 10 should be set to indicate that
426 the branch is not expected to be taken. The lower two bits must
427 be zero. */
428 HOWTO (R_PPC64_REL14_BRNTAKEN, /* type */
429 0, /* rightshift */
430 2, /* size (0 = byte, 1 = short, 2 = long) */
431 16, /* bitsize */
b34976b6 432 TRUE, /* pc_relative */
5bd4f169
AM
433 0, /* bitpos */
434 complain_overflow_signed, /* complain_on_overflow */
805fc799 435 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 436 "R_PPC64_REL14_BRNTAKEN",/* name */
b34976b6 437 FALSE, /* partial_inplace */
d006db6c 438 0, /* src_mask */
f5e87a1d 439 0x0000fffc, /* dst_mask */
b34976b6 440 TRUE), /* pcrel_offset */
5bd4f169
AM
441
442 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
443 symbol. */
444 HOWTO (R_PPC64_GOT16, /* type */
445 0, /* rightshift */
446 1, /* size (0 = byte, 1 = short, 2 = long) */
447 16, /* bitsize */
b34976b6 448 FALSE, /* pc_relative */
5bd4f169
AM
449 0, /* bitpos */
450 complain_overflow_signed, /* complain_on_overflow */
805fc799 451 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 452 "R_PPC64_GOT16", /* name */
b34976b6 453 FALSE, /* partial_inplace */
5bd4f169
AM
454 0, /* src_mask */
455 0xffff, /* dst_mask */
b34976b6 456 FALSE), /* pcrel_offset */
5bd4f169
AM
457
458 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
459 the symbol. */
460 HOWTO (R_PPC64_GOT16_LO, /* type */
461 0, /* rightshift */
462 1, /* size (0 = byte, 1 = short, 2 = long) */
463 16, /* bitsize */
b34976b6 464 FALSE, /* pc_relative */
5bd4f169
AM
465 0, /* bitpos */
466 complain_overflow_dont, /* complain_on_overflow */
805fc799 467 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 468 "R_PPC64_GOT16_LO", /* name */
b34976b6 469 FALSE, /* partial_inplace */
5bd4f169
AM
470 0, /* src_mask */
471 0xffff, /* dst_mask */
b34976b6 472 FALSE), /* pcrel_offset */
5bd4f169
AM
473
474 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
475 the symbol. */
476 HOWTO (R_PPC64_GOT16_HI, /* type */
477 16, /* rightshift */
478 1, /* size (0 = byte, 1 = short, 2 = long) */
479 16, /* bitsize */
b34976b6 480 FALSE, /* pc_relative */
5bd4f169
AM
481 0, /* bitpos */
482 complain_overflow_dont,/* complain_on_overflow */
805fc799 483 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 484 "R_PPC64_GOT16_HI", /* name */
b34976b6 485 FALSE, /* partial_inplace */
5bd4f169
AM
486 0, /* src_mask */
487 0xffff, /* dst_mask */
b34976b6 488 FALSE), /* pcrel_offset */
5bd4f169
AM
489
490 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
491 the symbol. */
492 HOWTO (R_PPC64_GOT16_HA, /* type */
493 16, /* rightshift */
494 1, /* size (0 = byte, 1 = short, 2 = long) */
495 16, /* bitsize */
b34976b6 496 FALSE, /* pc_relative */
5bd4f169
AM
497 0, /* bitpos */
498 complain_overflow_dont,/* complain_on_overflow */
805fc799 499 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 500 "R_PPC64_GOT16_HA", /* name */
b34976b6 501 FALSE, /* partial_inplace */
5bd4f169
AM
502 0, /* src_mask */
503 0xffff, /* dst_mask */
b34976b6 504 FALSE), /* pcrel_offset */
5bd4f169
AM
505
506 /* This is used only by the dynamic linker. The symbol should exist
507 both in the object being run and in some shared library. The
508 dynamic linker copies the data addressed by the symbol from the
509 shared library into the object, because the object being
510 run has to have the data at some particular address. */
511 HOWTO (R_PPC64_COPY, /* type */
512 0, /* rightshift */
f5e87a1d
AM
513 0, /* this one is variable size */
514 0, /* bitsize */
b34976b6 515 FALSE, /* pc_relative */
5bd4f169 516 0, /* bitpos */
f5e87a1d
AM
517 complain_overflow_dont, /* complain_on_overflow */
518 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 519 "R_PPC64_COPY", /* name */
b34976b6 520 FALSE, /* partial_inplace */
5bd4f169
AM
521 0, /* src_mask */
522 0, /* dst_mask */
b34976b6 523 FALSE), /* pcrel_offset */
5bd4f169
AM
524
525 /* Like R_PPC64_ADDR64, but used when setting global offset table
526 entries. */
527 HOWTO (R_PPC64_GLOB_DAT, /* type */
528 0, /* rightshift */
529 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
530 64, /* bitsize */
b34976b6 531 FALSE, /* pc_relative */
5bd4f169
AM
532 0, /* bitpos */
533 complain_overflow_dont, /* complain_on_overflow */
805fc799 534 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 535 "R_PPC64_GLOB_DAT", /* name */
b34976b6 536 FALSE, /* partial_inplace */
5bd4f169 537 0, /* src_mask */
f5e87a1d 538 ONES (64), /* dst_mask */
b34976b6 539 FALSE), /* pcrel_offset */
5bd4f169
AM
540
541 /* Created by the link editor. Marks a procedure linkage table
542 entry for a symbol. */
543 HOWTO (R_PPC64_JMP_SLOT, /* type */
544 0, /* rightshift */
545 0, /* size (0 = byte, 1 = short, 2 = long) */
546 0, /* bitsize */
b34976b6 547 FALSE, /* pc_relative */
5bd4f169
AM
548 0, /* bitpos */
549 complain_overflow_dont, /* complain_on_overflow */
805fc799 550 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 551 "R_PPC64_JMP_SLOT", /* name */
b34976b6 552 FALSE, /* partial_inplace */
5bd4f169
AM
553 0, /* src_mask */
554 0, /* dst_mask */
b34976b6 555 FALSE), /* pcrel_offset */
5bd4f169
AM
556
557 /* Used only by the dynamic linker. When the object is run, this
558 doubleword64 is set to the load address of the object, plus the
559 addend. */
560 HOWTO (R_PPC64_RELATIVE, /* type */
561 0, /* rightshift */
562 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
563 64, /* bitsize */
b34976b6 564 FALSE, /* pc_relative */
5bd4f169
AM
565 0, /* bitpos */
566 complain_overflow_dont, /* complain_on_overflow */
567 bfd_elf_generic_reloc, /* special_function */
568 "R_PPC64_RELATIVE", /* name */
b34976b6 569 FALSE, /* partial_inplace */
5bd4f169 570 0, /* src_mask */
f5e87a1d 571 ONES (64), /* dst_mask */
b34976b6 572 FALSE), /* pcrel_offset */
5bd4f169
AM
573
574 /* Like R_PPC64_ADDR32, but may be unaligned. */
575 HOWTO (R_PPC64_UADDR32, /* type */
576 0, /* rightshift */
577 2, /* size (0 = byte, 1 = short, 2 = long) */
578 32, /* bitsize */
b34976b6 579 FALSE, /* pc_relative */
5bd4f169
AM
580 0, /* bitpos */
581 complain_overflow_bitfield, /* complain_on_overflow */
582 bfd_elf_generic_reloc, /* special_function */
583 "R_PPC64_UADDR32", /* name */
b34976b6 584 FALSE, /* partial_inplace */
5bd4f169
AM
585 0, /* src_mask */
586 0xffffffff, /* dst_mask */
b34976b6 587 FALSE), /* pcrel_offset */
5bd4f169
AM
588
589 /* Like R_PPC64_ADDR16, but may be unaligned. */
590 HOWTO (R_PPC64_UADDR16, /* type */
591 0, /* rightshift */
592 1, /* size (0 = byte, 1 = short, 2 = long) */
593 16, /* bitsize */
b34976b6 594 FALSE, /* pc_relative */
5bd4f169
AM
595 0, /* bitpos */
596 complain_overflow_bitfield, /* complain_on_overflow */
597 bfd_elf_generic_reloc, /* special_function */
598 "R_PPC64_UADDR16", /* name */
b34976b6 599 FALSE, /* partial_inplace */
5bd4f169
AM
600 0, /* src_mask */
601 0xffff, /* dst_mask */
b34976b6 602 FALSE), /* pcrel_offset */
5bd4f169
AM
603
604 /* 32-bit PC relative. */
605 HOWTO (R_PPC64_REL32, /* type */
606 0, /* rightshift */
607 2, /* size (0 = byte, 1 = short, 2 = long) */
608 32, /* bitsize */
b34976b6 609 TRUE, /* pc_relative */
5bd4f169 610 0, /* bitpos */
cedb70c5 611 /* FIXME: Verify. Was complain_overflow_bitfield. */
5bd4f169
AM
612 complain_overflow_signed, /* complain_on_overflow */
613 bfd_elf_generic_reloc, /* special_function */
614 "R_PPC64_REL32", /* name */
b34976b6 615 FALSE, /* partial_inplace */
5bd4f169
AM
616 0, /* src_mask */
617 0xffffffff, /* dst_mask */
b34976b6 618 TRUE), /* pcrel_offset */
5bd4f169 619
10ed1bba 620 /* 32-bit relocation to the symbol's procedure linkage table. */
5bd4f169
AM
621 HOWTO (R_PPC64_PLT32, /* type */
622 0, /* rightshift */
623 2, /* size (0 = byte, 1 = short, 2 = long) */
624 32, /* bitsize */
b34976b6 625 FALSE, /* pc_relative */
5bd4f169
AM
626 0, /* bitpos */
627 complain_overflow_bitfield, /* complain_on_overflow */
805fc799 628 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 629 "R_PPC64_PLT32", /* name */
b34976b6 630 FALSE, /* partial_inplace */
5bd4f169 631 0, /* src_mask */
f5e87a1d 632 0xffffffff, /* dst_mask */
b34976b6 633 FALSE), /* pcrel_offset */
5bd4f169
AM
634
635 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
636 FIXME: R_PPC64_PLTREL32 not supported. */
637 HOWTO (R_PPC64_PLTREL32, /* type */
638 0, /* rightshift */
639 2, /* size (0 = byte, 1 = short, 2 = long) */
640 32, /* bitsize */
b34976b6 641 TRUE, /* pc_relative */
5bd4f169
AM
642 0, /* bitpos */
643 complain_overflow_signed, /* complain_on_overflow */
644 bfd_elf_generic_reloc, /* special_function */
645 "R_PPC64_PLTREL32", /* name */
b34976b6 646 FALSE, /* partial_inplace */
5bd4f169 647 0, /* src_mask */
f5e87a1d 648 0xffffffff, /* dst_mask */
b34976b6 649 TRUE), /* pcrel_offset */
5bd4f169
AM
650
651 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
652 the symbol. */
653 HOWTO (R_PPC64_PLT16_LO, /* type */
654 0, /* rightshift */
655 1, /* size (0 = byte, 1 = short, 2 = long) */
656 16, /* bitsize */
b34976b6 657 FALSE, /* pc_relative */
5bd4f169
AM
658 0, /* bitpos */
659 complain_overflow_dont, /* complain_on_overflow */
805fc799 660 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 661 "R_PPC64_PLT16_LO", /* name */
b34976b6 662 FALSE, /* partial_inplace */
5bd4f169
AM
663 0, /* src_mask */
664 0xffff, /* dst_mask */
b34976b6 665 FALSE), /* pcrel_offset */
5bd4f169
AM
666
667 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
668 the symbol. */
669 HOWTO (R_PPC64_PLT16_HI, /* type */
670 16, /* rightshift */
671 1, /* size (0 = byte, 1 = short, 2 = long) */
672 16, /* bitsize */
b34976b6 673 FALSE, /* pc_relative */
5bd4f169
AM
674 0, /* bitpos */
675 complain_overflow_dont, /* complain_on_overflow */
805fc799 676 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 677 "R_PPC64_PLT16_HI", /* name */
b34976b6 678 FALSE, /* partial_inplace */
5bd4f169
AM
679 0, /* src_mask */
680 0xffff, /* dst_mask */
b34976b6 681 FALSE), /* pcrel_offset */
5bd4f169
AM
682
683 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
684 the symbol. */
685 HOWTO (R_PPC64_PLT16_HA, /* type */
686 16, /* rightshift */
687 1, /* size (0 = byte, 1 = short, 2 = long) */
688 16, /* bitsize */
b34976b6 689 FALSE, /* pc_relative */
5bd4f169
AM
690 0, /* bitpos */
691 complain_overflow_dont, /* complain_on_overflow */
805fc799 692 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 693 "R_PPC64_PLT16_HA", /* name */
b34976b6 694 FALSE, /* partial_inplace */
5bd4f169
AM
695 0, /* src_mask */
696 0xffff, /* dst_mask */
b34976b6 697 FALSE), /* pcrel_offset */
5bd4f169 698
c061c2d8 699 /* 16-bit section relative relocation. */
5bd4f169
AM
700 HOWTO (R_PPC64_SECTOFF, /* type */
701 0, /* rightshift */
c061c2d8
AM
702 1, /* size (0 = byte, 1 = short, 2 = long) */
703 16, /* bitsize */
b34976b6 704 FALSE, /* pc_relative */
5bd4f169
AM
705 0, /* bitpos */
706 complain_overflow_bitfield, /* complain_on_overflow */
805fc799 707 ppc64_elf_sectoff_reloc, /* special_function */
5bd4f169 708 "R_PPC64_SECTOFF", /* name */
b34976b6 709 FALSE, /* partial_inplace */
5bd4f169 710 0, /* src_mask */
c061c2d8 711 0xffff, /* dst_mask */
b34976b6 712 FALSE), /* pcrel_offset */
5bd4f169 713
c061c2d8 714 /* Like R_PPC64_SECTOFF, but no overflow warning. */
5bd4f169
AM
715 HOWTO (R_PPC64_SECTOFF_LO, /* type */
716 0, /* rightshift */
717 1, /* size (0 = byte, 1 = short, 2 = long) */
718 16, /* bitsize */
b34976b6 719 FALSE, /* pc_relative */
5bd4f169
AM
720 0, /* bitpos */
721 complain_overflow_dont, /* complain_on_overflow */
805fc799 722 ppc64_elf_sectoff_reloc, /* special_function */
5bd4f169 723 "R_PPC64_SECTOFF_LO", /* name */
b34976b6 724 FALSE, /* partial_inplace */
5bd4f169
AM
725 0, /* src_mask */
726 0xffff, /* dst_mask */
b34976b6 727 FALSE), /* pcrel_offset */
5bd4f169
AM
728
729 /* 16-bit upper half section relative relocation. */
730 HOWTO (R_PPC64_SECTOFF_HI, /* type */
731 16, /* rightshift */
732 1, /* size (0 = byte, 1 = short, 2 = long) */
733 16, /* bitsize */
b34976b6 734 FALSE, /* pc_relative */
5bd4f169
AM
735 0, /* bitpos */
736 complain_overflow_dont, /* complain_on_overflow */
805fc799 737 ppc64_elf_sectoff_reloc, /* special_function */
5bd4f169 738 "R_PPC64_SECTOFF_HI", /* name */
b34976b6 739 FALSE, /* partial_inplace */
5bd4f169
AM
740 0, /* src_mask */
741 0xffff, /* dst_mask */
b34976b6 742 FALSE), /* pcrel_offset */
5bd4f169
AM
743
744 /* 16-bit upper half adjusted section relative relocation. */
745 HOWTO (R_PPC64_SECTOFF_HA, /* type */
746 16, /* rightshift */
747 1, /* size (0 = byte, 1 = short, 2 = long) */
748 16, /* bitsize */
b34976b6 749 FALSE, /* pc_relative */
5bd4f169
AM
750 0, /* bitpos */
751 complain_overflow_dont, /* complain_on_overflow */
805fc799 752 ppc64_elf_sectoff_ha_reloc, /* special_function */
5bd4f169 753 "R_PPC64_SECTOFF_HA", /* name */
b34976b6 754 FALSE, /* partial_inplace */
5bd4f169
AM
755 0, /* src_mask */
756 0xffff, /* dst_mask */
b34976b6 757 FALSE), /* pcrel_offset */
5bd4f169 758
04c9666a
AM
759 /* Like R_PPC64_REL24 without touching the two least significant bits. */
760 HOWTO (R_PPC64_REL30, /* type */
5bd4f169
AM
761 2, /* rightshift */
762 2, /* size (0 = byte, 1 = short, 2 = long) */
763 30, /* bitsize */
b34976b6 764 TRUE, /* pc_relative */
5bd4f169
AM
765 0, /* bitpos */
766 complain_overflow_dont, /* complain_on_overflow */
767 bfd_elf_generic_reloc, /* special_function */
04c9666a 768 "R_PPC64_REL30", /* name */
b34976b6 769 FALSE, /* partial_inplace */
d006db6c 770 0, /* src_mask */
5bd4f169 771 0xfffffffc, /* dst_mask */
b34976b6 772 TRUE), /* pcrel_offset */
5bd4f169
AM
773
774 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
775
776 /* A standard 64-bit relocation. */
777 HOWTO (R_PPC64_ADDR64, /* type */
778 0, /* rightshift */
779 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
780 64, /* bitsize */
b34976b6 781 FALSE, /* pc_relative */
5bd4f169
AM
782 0, /* bitpos */
783 complain_overflow_dont, /* complain_on_overflow */
784 bfd_elf_generic_reloc, /* special_function */
785 "R_PPC64_ADDR64", /* name */
b34976b6 786 FALSE, /* partial_inplace */
5bd4f169 787 0, /* src_mask */
f5e87a1d 788 ONES (64), /* dst_mask */
b34976b6 789 FALSE), /* pcrel_offset */
5bd4f169
AM
790
791 /* The bits 32-47 of an address. */
792 HOWTO (R_PPC64_ADDR16_HIGHER, /* type */
793 32, /* rightshift */
794 1, /* size (0 = byte, 1 = short, 2 = long) */
795 16, /* bitsize */
b34976b6 796 FALSE, /* pc_relative */
5bd4f169
AM
797 0, /* bitpos */
798 complain_overflow_dont, /* complain_on_overflow */
799 bfd_elf_generic_reloc, /* special_function */
800 "R_PPC64_ADDR16_HIGHER", /* name */
b34976b6 801 FALSE, /* partial_inplace */
5bd4f169
AM
802 0, /* src_mask */
803 0xffff, /* dst_mask */
b34976b6 804 FALSE), /* pcrel_offset */
5bd4f169
AM
805
806 /* The bits 32-47 of an address, plus 1 if the contents of the low
807 16 bits, treated as a signed number, is negative. */
808 HOWTO (R_PPC64_ADDR16_HIGHERA, /* type */
809 32, /* rightshift */
810 1, /* size (0 = byte, 1 = short, 2 = long) */
811 16, /* bitsize */
b34976b6 812 FALSE, /* pc_relative */
5bd4f169
AM
813 0, /* bitpos */
814 complain_overflow_dont, /* complain_on_overflow */
805fc799 815 ppc64_elf_ha_reloc, /* special_function */
5bd4f169 816 "R_PPC64_ADDR16_HIGHERA", /* name */
b34976b6 817 FALSE, /* partial_inplace */
5bd4f169
AM
818 0, /* src_mask */
819 0xffff, /* dst_mask */
b34976b6 820 FALSE), /* pcrel_offset */
5bd4f169
AM
821
822 /* The bits 48-63 of an address. */
823 HOWTO (R_PPC64_ADDR16_HIGHEST,/* type */
824 48, /* rightshift */
825 1, /* size (0 = byte, 1 = short, 2 = long) */
826 16, /* bitsize */
b34976b6 827 FALSE, /* pc_relative */
5bd4f169
AM
828 0, /* bitpos */
829 complain_overflow_dont, /* complain_on_overflow */
830 bfd_elf_generic_reloc, /* special_function */
831 "R_PPC64_ADDR16_HIGHEST", /* name */
b34976b6 832 FALSE, /* partial_inplace */
5bd4f169
AM
833 0, /* src_mask */
834 0xffff, /* dst_mask */
b34976b6 835 FALSE), /* pcrel_offset */
5bd4f169
AM
836
837 /* The bits 48-63 of an address, plus 1 if the contents of the low
838 16 bits, treated as a signed number, is negative. */
839 HOWTO (R_PPC64_ADDR16_HIGHESTA,/* type */
840 48, /* rightshift */
841 1, /* size (0 = byte, 1 = short, 2 = long) */
842 16, /* bitsize */
b34976b6 843 FALSE, /* pc_relative */
5bd4f169
AM
844 0, /* bitpos */
845 complain_overflow_dont, /* complain_on_overflow */
805fc799 846 ppc64_elf_ha_reloc, /* special_function */
5bd4f169 847 "R_PPC64_ADDR16_HIGHESTA", /* name */
b34976b6 848 FALSE, /* partial_inplace */
5bd4f169
AM
849 0, /* src_mask */
850 0xffff, /* dst_mask */
b34976b6 851 FALSE), /* pcrel_offset */
5bd4f169
AM
852
853 /* Like ADDR64, but may be unaligned. */
854 HOWTO (R_PPC64_UADDR64, /* type */
855 0, /* rightshift */
856 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
857 64, /* bitsize */
b34976b6 858 FALSE, /* pc_relative */
5bd4f169
AM
859 0, /* bitpos */
860 complain_overflow_dont, /* complain_on_overflow */
861 bfd_elf_generic_reloc, /* special_function */
862 "R_PPC64_UADDR64", /* name */
b34976b6 863 FALSE, /* partial_inplace */
5bd4f169 864 0, /* src_mask */
f5e87a1d 865 ONES (64), /* dst_mask */
b34976b6 866 FALSE), /* pcrel_offset */
5bd4f169
AM
867
868 /* 64-bit relative relocation. */
869 HOWTO (R_PPC64_REL64, /* type */
870 0, /* rightshift */
871 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
872 64, /* bitsize */
b34976b6 873 TRUE, /* pc_relative */
5bd4f169
AM
874 0, /* bitpos */
875 complain_overflow_dont, /* complain_on_overflow */
876 bfd_elf_generic_reloc, /* special_function */
877 "R_PPC64_REL64", /* name */
b34976b6 878 FALSE, /* partial_inplace */
5bd4f169 879 0, /* src_mask */
f5e87a1d 880 ONES (64), /* dst_mask */
b34976b6 881 TRUE), /* pcrel_offset */
5bd4f169 882
cedb70c5 883 /* 64-bit relocation to the symbol's procedure linkage table. */
5bd4f169
AM
884 HOWTO (R_PPC64_PLT64, /* type */
885 0, /* rightshift */
886 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
887 64, /* bitsize */
b34976b6 888 FALSE, /* pc_relative */
5bd4f169
AM
889 0, /* bitpos */
890 complain_overflow_dont, /* complain_on_overflow */
805fc799 891 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 892 "R_PPC64_PLT64", /* name */
b34976b6 893 FALSE, /* partial_inplace */
5bd4f169 894 0, /* src_mask */
f5e87a1d 895 ONES (64), /* dst_mask */
b34976b6 896 FALSE), /* pcrel_offset */
5bd4f169
AM
897
898 /* 64-bit PC relative relocation to the symbol's procedure linkage
899 table. */
900 /* FIXME: R_PPC64_PLTREL64 not supported. */
901 HOWTO (R_PPC64_PLTREL64, /* type */
902 0, /* rightshift */
903 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
904 64, /* bitsize */
b34976b6 905 TRUE, /* pc_relative */
5bd4f169
AM
906 0, /* bitpos */
907 complain_overflow_dont, /* complain_on_overflow */
805fc799 908 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 909 "R_PPC64_PLTREL64", /* name */
b34976b6 910 FALSE, /* partial_inplace */
5bd4f169 911 0, /* src_mask */
f5e87a1d 912 ONES (64), /* dst_mask */
b34976b6 913 TRUE), /* pcrel_offset */
5bd4f169
AM
914
915 /* 16 bit TOC-relative relocation. */
916
917 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
918 HOWTO (R_PPC64_TOC16, /* type */
919 0, /* rightshift */
920 1, /* size (0 = byte, 1 = short, 2 = long) */
921 16, /* bitsize */
b34976b6 922 FALSE, /* pc_relative */
5bd4f169
AM
923 0, /* bitpos */
924 complain_overflow_signed, /* complain_on_overflow */
805fc799 925 ppc64_elf_toc_reloc, /* special_function */
5bd4f169 926 "R_PPC64_TOC16", /* name */
b34976b6 927 FALSE, /* partial_inplace */
5bd4f169
AM
928 0, /* src_mask */
929 0xffff, /* dst_mask */
b34976b6 930 FALSE), /* pcrel_offset */
5bd4f169
AM
931
932 /* 16 bit TOC-relative relocation without overflow. */
933
934 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
935 HOWTO (R_PPC64_TOC16_LO, /* type */
936 0, /* rightshift */
937 1, /* size (0 = byte, 1 = short, 2 = long) */
938 16, /* bitsize */
b34976b6 939 FALSE, /* pc_relative */
5bd4f169
AM
940 0, /* bitpos */
941 complain_overflow_dont, /* complain_on_overflow */
805fc799 942 ppc64_elf_toc_reloc, /* special_function */
5bd4f169 943 "R_PPC64_TOC16_LO", /* name */
b34976b6 944 FALSE, /* partial_inplace */
5bd4f169
AM
945 0, /* src_mask */
946 0xffff, /* dst_mask */
b34976b6 947 FALSE), /* pcrel_offset */
5bd4f169
AM
948
949 /* 16 bit TOC-relative relocation, high 16 bits. */
950
951 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
952 HOWTO (R_PPC64_TOC16_HI, /* type */
953 16, /* rightshift */
954 1, /* size (0 = byte, 1 = short, 2 = long) */
955 16, /* bitsize */
b34976b6 956 FALSE, /* pc_relative */
5bd4f169
AM
957 0, /* bitpos */
958 complain_overflow_dont, /* complain_on_overflow */
805fc799 959 ppc64_elf_toc_reloc, /* special_function */
5bd4f169 960 "R_PPC64_TOC16_HI", /* name */
b34976b6 961 FALSE, /* partial_inplace */
5bd4f169
AM
962 0, /* src_mask */
963 0xffff, /* dst_mask */
b34976b6 964 FALSE), /* pcrel_offset */
5bd4f169
AM
965
966 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
967 contents of the low 16 bits, treated as a signed number, is
968 negative. */
969
970 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
971 HOWTO (R_PPC64_TOC16_HA, /* type */
972 16, /* rightshift */
973 1, /* size (0 = byte, 1 = short, 2 = long) */
974 16, /* bitsize */
b34976b6 975 FALSE, /* pc_relative */
5bd4f169
AM
976 0, /* bitpos */
977 complain_overflow_dont, /* complain_on_overflow */
805fc799 978 ppc64_elf_toc_ha_reloc, /* special_function */
5bd4f169 979 "R_PPC64_TOC16_HA", /* name */
b34976b6 980 FALSE, /* partial_inplace */
5bd4f169
AM
981 0, /* src_mask */
982 0xffff, /* dst_mask */
b34976b6 983 FALSE), /* pcrel_offset */
5bd4f169
AM
984
985 /* 64-bit relocation; insert value of TOC base (.TOC.). */
986
987 /* R_PPC64_TOC 51 doubleword64 .TOC. */
988 HOWTO (R_PPC64_TOC, /* type */
989 0, /* rightshift */
990 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
991 64, /* bitsize */
b34976b6 992 FALSE, /* pc_relative */
5bd4f169
AM
993 0, /* bitpos */
994 complain_overflow_bitfield, /* complain_on_overflow */
805fc799 995 ppc64_elf_toc64_reloc, /* special_function */
5bd4f169 996 "R_PPC64_TOC", /* name */
b34976b6 997 FALSE, /* partial_inplace */
5bd4f169 998 0, /* src_mask */
f5e87a1d 999 ONES (64), /* dst_mask */
b34976b6 1000 FALSE), /* pcrel_offset */
5bd4f169
AM
1001
1002 /* Like R_PPC64_GOT16, but also informs the link editor that the
1003 value to relocate may (!) refer to a PLT entry which the link
1004 editor (a) may replace with the symbol value. If the link editor
1005 is unable to fully resolve the symbol, it may (b) create a PLT
1006 entry and store the address to the new PLT entry in the GOT.
1007 This permits lazy resolution of function symbols at run time.
1008 The link editor may also skip all of this and just (c) emit a
1009 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
1010 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
1011 HOWTO (R_PPC64_PLTGOT16, /* type */
1012 0, /* rightshift */
1013 1, /* size (0 = byte, 1 = short, 2 = long) */
1014 16, /* bitsize */
b34976b6 1015 FALSE, /* pc_relative */
5bd4f169
AM
1016 0, /* bitpos */
1017 complain_overflow_signed, /* complain_on_overflow */
805fc799 1018 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb
AM
1019 "R_PPC64_PLTGOT16", /* name */
1020 FALSE, /* partial_inplace */
1021 0, /* src_mask */
1022 0xffff, /* dst_mask */
1023 FALSE), /* pcrel_offset */
1024
1025 /* Like R_PPC64_PLTGOT16, but without overflow. */
1026 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1027 HOWTO (R_PPC64_PLTGOT16_LO, /* type */
1028 0, /* rightshift */
1029 1, /* size (0 = byte, 1 = short, 2 = long) */
1030 16, /* bitsize */
1031 FALSE, /* pc_relative */
1032 0, /* bitpos */
1033 complain_overflow_dont, /* complain_on_overflow */
1034 ppc64_elf_unhandled_reloc, /* special_function */
1035 "R_PPC64_PLTGOT16_LO", /* name */
1036 FALSE, /* partial_inplace */
1037 0, /* src_mask */
1038 0xffff, /* dst_mask */
1039 FALSE), /* pcrel_offset */
1040
1041 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
1042 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
1043 HOWTO (R_PPC64_PLTGOT16_HI, /* type */
1044 16, /* rightshift */
1045 1, /* size (0 = byte, 1 = short, 2 = long) */
1046 16, /* bitsize */
1047 FALSE, /* pc_relative */
1048 0, /* bitpos */
1049 complain_overflow_dont, /* complain_on_overflow */
1050 ppc64_elf_unhandled_reloc, /* special_function */
1051 "R_PPC64_PLTGOT16_HI", /* name */
1052 FALSE, /* partial_inplace */
1053 0, /* src_mask */
1054 0xffff, /* dst_mask */
1055 FALSE), /* pcrel_offset */
1056
1057 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
1058 1 if the contents of the low 16 bits, treated as a signed number,
1059 is negative. */
1060 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
1061 HOWTO (R_PPC64_PLTGOT16_HA, /* type */
1062 16, /* rightshift */
1063 1, /* size (0 = byte, 1 = short, 2 = long) */
1064 16, /* bitsize */
1065 FALSE, /* pc_relative */
1066 0, /* bitpos */
1067 complain_overflow_dont,/* complain_on_overflow */
1068 ppc64_elf_unhandled_reloc, /* special_function */
1069 "R_PPC64_PLTGOT16_HA", /* name */
1070 FALSE, /* partial_inplace */
1071 0, /* src_mask */
1072 0xffff, /* dst_mask */
1073 FALSE), /* pcrel_offset */
1074
1075 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
1076 HOWTO (R_PPC64_ADDR16_DS, /* type */
1077 0, /* rightshift */
1078 1, /* size (0 = byte, 1 = short, 2 = long) */
1079 16, /* bitsize */
1080 FALSE, /* pc_relative */
1081 0, /* bitpos */
1082 complain_overflow_bitfield, /* complain_on_overflow */
1083 bfd_elf_generic_reloc, /* special_function */
1084 "R_PPC64_ADDR16_DS", /* name */
1085 FALSE, /* partial_inplace */
1086 0, /* src_mask */
1087 0xfffc, /* dst_mask */
1088 FALSE), /* pcrel_offset */
1089
1090 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
1091 HOWTO (R_PPC64_ADDR16_LO_DS, /* type */
1092 0, /* rightshift */
1093 1, /* size (0 = byte, 1 = short, 2 = long) */
1094 16, /* bitsize */
1095 FALSE, /* pc_relative */
1096 0, /* bitpos */
1097 complain_overflow_dont,/* complain_on_overflow */
1098 bfd_elf_generic_reloc, /* special_function */
1099 "R_PPC64_ADDR16_LO_DS",/* name */
1100 FALSE, /* partial_inplace */
1101 0, /* src_mask */
1102 0xfffc, /* dst_mask */
1103 FALSE), /* pcrel_offset */
1104
1105 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
1106 HOWTO (R_PPC64_GOT16_DS, /* type */
1107 0, /* rightshift */
1108 1, /* size (0 = byte, 1 = short, 2 = long) */
1109 16, /* bitsize */
1110 FALSE, /* pc_relative */
1111 0, /* bitpos */
1112 complain_overflow_signed, /* complain_on_overflow */
1113 ppc64_elf_unhandled_reloc, /* special_function */
1114 "R_PPC64_GOT16_DS", /* name */
1115 FALSE, /* partial_inplace */
1116 0, /* src_mask */
1117 0xfffc, /* dst_mask */
1118 FALSE), /* pcrel_offset */
1119
1120 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
1121 HOWTO (R_PPC64_GOT16_LO_DS, /* type */
1122 0, /* rightshift */
1123 1, /* size (0 = byte, 1 = short, 2 = long) */
1124 16, /* bitsize */
1125 FALSE, /* pc_relative */
1126 0, /* bitpos */
1127 complain_overflow_dont, /* complain_on_overflow */
1128 ppc64_elf_unhandled_reloc, /* special_function */
1129 "R_PPC64_GOT16_LO_DS", /* name */
1130 FALSE, /* partial_inplace */
1131 0, /* src_mask */
1132 0xfffc, /* dst_mask */
1133 FALSE), /* pcrel_offset */
1134
1135 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
1136 HOWTO (R_PPC64_PLT16_LO_DS, /* type */
1137 0, /* rightshift */
1138 1, /* size (0 = byte, 1 = short, 2 = long) */
1139 16, /* bitsize */
1140 FALSE, /* pc_relative */
1141 0, /* bitpos */
1142 complain_overflow_dont, /* complain_on_overflow */
1143 ppc64_elf_unhandled_reloc, /* special_function */
1144 "R_PPC64_PLT16_LO_DS", /* name */
1145 FALSE, /* partial_inplace */
1146 0, /* src_mask */
1147 0xfffc, /* dst_mask */
1148 FALSE), /* pcrel_offset */
1149
1150 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
1151 HOWTO (R_PPC64_SECTOFF_DS, /* type */
1152 0, /* rightshift */
1153 1, /* size (0 = byte, 1 = short, 2 = long) */
1154 16, /* bitsize */
1155 FALSE, /* pc_relative */
1156 0, /* bitpos */
1157 complain_overflow_bitfield, /* complain_on_overflow */
1158 ppc64_elf_sectoff_reloc, /* special_function */
1159 "R_PPC64_SECTOFF_DS", /* name */
1160 FALSE, /* partial_inplace */
1161 0, /* src_mask */
1162 0xfffc, /* dst_mask */
1163 FALSE), /* pcrel_offset */
1164
1165 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
1166 HOWTO (R_PPC64_SECTOFF_LO_DS, /* type */
1167 0, /* rightshift */
1168 1, /* size (0 = byte, 1 = short, 2 = long) */
1169 16, /* bitsize */
1170 FALSE, /* pc_relative */
1171 0, /* bitpos */
1172 complain_overflow_dont, /* complain_on_overflow */
1173 ppc64_elf_sectoff_reloc, /* special_function */
1174 "R_PPC64_SECTOFF_LO_DS",/* name */
1175 FALSE, /* partial_inplace */
1176 0, /* src_mask */
1177 0xfffc, /* dst_mask */
1178 FALSE), /* pcrel_offset */
1179
1180 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
1181 HOWTO (R_PPC64_TOC16_DS, /* type */
1182 0, /* rightshift */
1183 1, /* size (0 = byte, 1 = short, 2 = long) */
1184 16, /* bitsize */
1185 FALSE, /* pc_relative */
1186 0, /* bitpos */
1187 complain_overflow_signed, /* complain_on_overflow */
1188 ppc64_elf_toc_reloc, /* special_function */
1189 "R_PPC64_TOC16_DS", /* name */
1190 FALSE, /* partial_inplace */
1191 0, /* src_mask */
1192 0xfffc, /* dst_mask */
1193 FALSE), /* pcrel_offset */
1194
1195 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
1196 HOWTO (R_PPC64_TOC16_LO_DS, /* type */
1197 0, /* rightshift */
1198 1, /* size (0 = byte, 1 = short, 2 = long) */
1199 16, /* bitsize */
1200 FALSE, /* pc_relative */
1201 0, /* bitpos */
1202 complain_overflow_dont, /* complain_on_overflow */
1203 ppc64_elf_toc_reloc, /* special_function */
1204 "R_PPC64_TOC16_LO_DS", /* name */
1205 FALSE, /* partial_inplace */
1206 0, /* src_mask */
1207 0xfffc, /* dst_mask */
1208 FALSE), /* pcrel_offset */
1209
1210 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
1211 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
6bfdb61b 1212 HOWTO (R_PPC64_PLTGOT16_DS, /* type */
411e1bfb
AM
1213 0, /* rightshift */
1214 1, /* size (0 = byte, 1 = short, 2 = long) */
1215 16, /* bitsize */
1216 FALSE, /* pc_relative */
1217 0, /* bitpos */
1218 complain_overflow_signed, /* complain_on_overflow */
1219 ppc64_elf_unhandled_reloc, /* special_function */
1220 "R_PPC64_PLTGOT16_DS", /* name */
1221 FALSE, /* partial_inplace */
1222 0, /* src_mask */
1223 0xfffc, /* dst_mask */
1224 FALSE), /* pcrel_offset */
1225
1226 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
1227 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1228 HOWTO (R_PPC64_PLTGOT16_LO_DS,/* type */
1229 0, /* rightshift */
1230 1, /* size (0 = byte, 1 = short, 2 = long) */
1231 16, /* bitsize */
1232 FALSE, /* pc_relative */
1233 0, /* bitpos */
1234 complain_overflow_dont, /* complain_on_overflow */
1235 ppc64_elf_unhandled_reloc, /* special_function */
1236 "R_PPC64_PLTGOT16_LO_DS",/* name */
1237 FALSE, /* partial_inplace */
1238 0, /* src_mask */
1239 0xfffc, /* dst_mask */
1240 FALSE), /* pcrel_offset */
1241
727fc41e 1242 /* Marker relocs for TLS. */
411e1bfb
AM
1243 HOWTO (R_PPC64_TLS,
1244 0, /* rightshift */
1245 2, /* size (0 = byte, 1 = short, 2 = long) */
1246 32, /* bitsize */
1247 FALSE, /* pc_relative */
1248 0, /* bitpos */
1249 complain_overflow_dont, /* complain_on_overflow */
1250 bfd_elf_generic_reloc, /* special_function */
1251 "R_PPC64_TLS", /* name */
1252 FALSE, /* partial_inplace */
1253 0, /* src_mask */
1254 0, /* dst_mask */
1255 FALSE), /* pcrel_offset */
1256
727fc41e
AM
1257 HOWTO (R_PPC64_TLSGD,
1258 0, /* rightshift */
1259 2, /* size (0 = byte, 1 = short, 2 = long) */
1260 32, /* bitsize */
1261 FALSE, /* pc_relative */
1262 0, /* bitpos */
1263 complain_overflow_dont, /* complain_on_overflow */
1264 bfd_elf_generic_reloc, /* special_function */
1265 "R_PPC64_TLSGD", /* name */
1266 FALSE, /* partial_inplace */
1267 0, /* src_mask */
1268 0, /* dst_mask */
1269 FALSE), /* pcrel_offset */
1270
1271 HOWTO (R_PPC64_TLSLD,
1272 0, /* rightshift */
1273 2, /* size (0 = byte, 1 = short, 2 = long) */
1274 32, /* bitsize */
1275 FALSE, /* pc_relative */
1276 0, /* bitpos */
1277 complain_overflow_dont, /* complain_on_overflow */
1278 bfd_elf_generic_reloc, /* special_function */
1279 "R_PPC64_TLSLD", /* name */
1280 FALSE, /* partial_inplace */
1281 0, /* src_mask */
1282 0, /* dst_mask */
1283 FALSE), /* pcrel_offset */
1284
3b421ab3
AM
1285 HOWTO (R_PPC64_TOCSAVE,
1286 0, /* rightshift */
1287 2, /* size (0 = byte, 1 = short, 2 = long) */
1288 32, /* bitsize */
1289 FALSE, /* pc_relative */
1290 0, /* bitpos */
1291 complain_overflow_dont, /* complain_on_overflow */
1292 bfd_elf_generic_reloc, /* special_function */
1293 "R_PPC64_TOCSAVE", /* name */
1294 FALSE, /* partial_inplace */
1295 0, /* src_mask */
1296 0, /* dst_mask */
1297 FALSE), /* pcrel_offset */
1298
411e1bfb
AM
1299 /* Computes the load module index of the load module that contains the
1300 definition of its TLS sym. */
1301 HOWTO (R_PPC64_DTPMOD64,
1302 0, /* rightshift */
1303 4, /* size (0 = byte, 1 = short, 2 = long) */
1304 64, /* bitsize */
1305 FALSE, /* pc_relative */
1306 0, /* bitpos */
1307 complain_overflow_dont, /* complain_on_overflow */
1308 ppc64_elf_unhandled_reloc, /* special_function */
1309 "R_PPC64_DTPMOD64", /* name */
1310 FALSE, /* partial_inplace */
1311 0, /* src_mask */
1312 ONES (64), /* dst_mask */
1313 FALSE), /* pcrel_offset */
1314
1315 /* Computes a dtv-relative displacement, the difference between the value
1316 of sym+add and the base address of the thread-local storage block that
1317 contains the definition of sym, minus 0x8000. */
1318 HOWTO (R_PPC64_DTPREL64,
1319 0, /* rightshift */
1320 4, /* size (0 = byte, 1 = short, 2 = long) */
1321 64, /* bitsize */
1322 FALSE, /* pc_relative */
1323 0, /* bitpos */
1324 complain_overflow_dont, /* complain_on_overflow */
1325 ppc64_elf_unhandled_reloc, /* special_function */
1326 "R_PPC64_DTPREL64", /* name */
1327 FALSE, /* partial_inplace */
1328 0, /* src_mask */
1329 ONES (64), /* dst_mask */
1330 FALSE), /* pcrel_offset */
1331
1332 /* A 16 bit dtprel reloc. */
1333 HOWTO (R_PPC64_DTPREL16,
1334 0, /* rightshift */
1335 1, /* size (0 = byte, 1 = short, 2 = long) */
1336 16, /* bitsize */
1337 FALSE, /* pc_relative */
1338 0, /* bitpos */
1339 complain_overflow_signed, /* complain_on_overflow */
1340 ppc64_elf_unhandled_reloc, /* special_function */
1341 "R_PPC64_DTPREL16", /* name */
1342 FALSE, /* partial_inplace */
1343 0, /* src_mask */
1344 0xffff, /* dst_mask */
1345 FALSE), /* pcrel_offset */
1346
1347 /* Like DTPREL16, but no overflow. */
1348 HOWTO (R_PPC64_DTPREL16_LO,
1349 0, /* rightshift */
1350 1, /* size (0 = byte, 1 = short, 2 = long) */
1351 16, /* bitsize */
1352 FALSE, /* pc_relative */
1353 0, /* bitpos */
1354 complain_overflow_dont, /* complain_on_overflow */
1355 ppc64_elf_unhandled_reloc, /* special_function */
1356 "R_PPC64_DTPREL16_LO", /* name */
1357 FALSE, /* partial_inplace */
1358 0, /* src_mask */
1359 0xffff, /* dst_mask */
1360 FALSE), /* pcrel_offset */
1361
1362 /* Like DTPREL16_LO, but next higher group of 16 bits. */
1363 HOWTO (R_PPC64_DTPREL16_HI,
1364 16, /* rightshift */
1365 1, /* size (0 = byte, 1 = short, 2 = long) */
1366 16, /* bitsize */
1367 FALSE, /* pc_relative */
1368 0, /* bitpos */
1369 complain_overflow_dont, /* complain_on_overflow */
1370 ppc64_elf_unhandled_reloc, /* special_function */
1371 "R_PPC64_DTPREL16_HI", /* name */
1372 FALSE, /* partial_inplace */
1373 0, /* src_mask */
1374 0xffff, /* dst_mask */
1375 FALSE), /* pcrel_offset */
1376
1377 /* Like DTPREL16_HI, but adjust for low 16 bits. */
1378 HOWTO (R_PPC64_DTPREL16_HA,
1379 16, /* rightshift */
1380 1, /* size (0 = byte, 1 = short, 2 = long) */
1381 16, /* bitsize */
1382 FALSE, /* pc_relative */
1383 0, /* bitpos */
1384 complain_overflow_dont, /* complain_on_overflow */
1385 ppc64_elf_unhandled_reloc, /* special_function */
1386 "R_PPC64_DTPREL16_HA", /* name */
1387 FALSE, /* partial_inplace */
1388 0, /* src_mask */
1389 0xffff, /* dst_mask */
1390 FALSE), /* pcrel_offset */
1391
1392 /* Like DTPREL16_HI, but next higher group of 16 bits. */
1393 HOWTO (R_PPC64_DTPREL16_HIGHER,
1394 32, /* rightshift */
1395 1, /* size (0 = byte, 1 = short, 2 = long) */
1396 16, /* bitsize */
1397 FALSE, /* pc_relative */
1398 0, /* bitpos */
1399 complain_overflow_dont, /* complain_on_overflow */
1400 ppc64_elf_unhandled_reloc, /* special_function */
1401 "R_PPC64_DTPREL16_HIGHER", /* name */
1402 FALSE, /* partial_inplace */
1403 0, /* src_mask */
1404 0xffff, /* dst_mask */
1405 FALSE), /* pcrel_offset */
1406
1407 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
1408 HOWTO (R_PPC64_DTPREL16_HIGHERA,
1409 32, /* rightshift */
1410 1, /* size (0 = byte, 1 = short, 2 = long) */
1411 16, /* bitsize */
1412 FALSE, /* pc_relative */
1413 0, /* bitpos */
1414 complain_overflow_dont, /* complain_on_overflow */
1415 ppc64_elf_unhandled_reloc, /* special_function */
1416 "R_PPC64_DTPREL16_HIGHERA", /* name */
1417 FALSE, /* partial_inplace */
1418 0, /* src_mask */
1419 0xffff, /* dst_mask */
1420 FALSE), /* pcrel_offset */
1421
1422 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
1423 HOWTO (R_PPC64_DTPREL16_HIGHEST,
1424 48, /* rightshift */
1425 1, /* size (0 = byte, 1 = short, 2 = long) */
1426 16, /* bitsize */
1427 FALSE, /* pc_relative */
1428 0, /* bitpos */
1429 complain_overflow_dont, /* complain_on_overflow */
1430 ppc64_elf_unhandled_reloc, /* special_function */
1431 "R_PPC64_DTPREL16_HIGHEST", /* name */
1432 FALSE, /* partial_inplace */
1433 0, /* src_mask */
1434 0xffff, /* dst_mask */
1435 FALSE), /* pcrel_offset */
1436
1437 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
1438 HOWTO (R_PPC64_DTPREL16_HIGHESTA,
1439 48, /* rightshift */
1440 1, /* size (0 = byte, 1 = short, 2 = long) */
1441 16, /* bitsize */
1442 FALSE, /* pc_relative */
1443 0, /* bitpos */
1444 complain_overflow_dont, /* complain_on_overflow */
1445 ppc64_elf_unhandled_reloc, /* special_function */
1446 "R_PPC64_DTPREL16_HIGHESTA", /* name */
1447 FALSE, /* partial_inplace */
1448 0, /* src_mask */
1449 0xffff, /* dst_mask */
1450 FALSE), /* pcrel_offset */
1451
1452 /* Like DTPREL16, but for insns with a DS field. */
1453 HOWTO (R_PPC64_DTPREL16_DS,
1454 0, /* rightshift */
1455 1, /* size (0 = byte, 1 = short, 2 = long) */
1456 16, /* bitsize */
1457 FALSE, /* pc_relative */
1458 0, /* bitpos */
1459 complain_overflow_signed, /* complain_on_overflow */
1460 ppc64_elf_unhandled_reloc, /* special_function */
1461 "R_PPC64_DTPREL16_DS", /* name */
1462 FALSE, /* partial_inplace */
1463 0, /* src_mask */
1464 0xfffc, /* dst_mask */
1465 FALSE), /* pcrel_offset */
1466
1467 /* Like DTPREL16_DS, but no overflow. */
1468 HOWTO (R_PPC64_DTPREL16_LO_DS,
1469 0, /* rightshift */
1470 1, /* size (0 = byte, 1 = short, 2 = long) */
1471 16, /* bitsize */
1472 FALSE, /* pc_relative */
1473 0, /* bitpos */
1474 complain_overflow_dont, /* complain_on_overflow */
1475 ppc64_elf_unhandled_reloc, /* special_function */
1476 "R_PPC64_DTPREL16_LO_DS", /* name */
1477 FALSE, /* partial_inplace */
1478 0, /* src_mask */
1479 0xfffc, /* dst_mask */
1480 FALSE), /* pcrel_offset */
1481
1482 /* Computes a tp-relative displacement, the difference between the value of
1483 sym+add and the value of the thread pointer (r13). */
1484 HOWTO (R_PPC64_TPREL64,
1485 0, /* rightshift */
1486 4, /* size (0 = byte, 1 = short, 2 = long) */
1487 64, /* bitsize */
1488 FALSE, /* pc_relative */
1489 0, /* bitpos */
1490 complain_overflow_dont, /* complain_on_overflow */
1491 ppc64_elf_unhandled_reloc, /* special_function */
1492 "R_PPC64_TPREL64", /* name */
1493 FALSE, /* partial_inplace */
1494 0, /* src_mask */
1495 ONES (64), /* dst_mask */
1496 FALSE), /* pcrel_offset */
1497
1498 /* A 16 bit tprel reloc. */
1499 HOWTO (R_PPC64_TPREL16,
1500 0, /* rightshift */
1501 1, /* size (0 = byte, 1 = short, 2 = long) */
1502 16, /* bitsize */
1503 FALSE, /* pc_relative */
1504 0, /* bitpos */
1505 complain_overflow_signed, /* complain_on_overflow */
1506 ppc64_elf_unhandled_reloc, /* special_function */
1507 "R_PPC64_TPREL16", /* name */
1508 FALSE, /* partial_inplace */
1509 0, /* src_mask */
1510 0xffff, /* dst_mask */
1511 FALSE), /* pcrel_offset */
1512
1513 /* Like TPREL16, but no overflow. */
1514 HOWTO (R_PPC64_TPREL16_LO,
1515 0, /* rightshift */
1516 1, /* size (0 = byte, 1 = short, 2 = long) */
1517 16, /* bitsize */
1518 FALSE, /* pc_relative */
1519 0, /* bitpos */
1520 complain_overflow_dont, /* complain_on_overflow */
1521 ppc64_elf_unhandled_reloc, /* special_function */
1522 "R_PPC64_TPREL16_LO", /* name */
1523 FALSE, /* partial_inplace */
1524 0, /* src_mask */
1525 0xffff, /* dst_mask */
1526 FALSE), /* pcrel_offset */
1527
1528 /* Like TPREL16_LO, but next higher group of 16 bits. */
1529 HOWTO (R_PPC64_TPREL16_HI,
1530 16, /* rightshift */
1531 1, /* size (0 = byte, 1 = short, 2 = long) */
1532 16, /* bitsize */
1533 FALSE, /* pc_relative */
1534 0, /* bitpos */
1535 complain_overflow_dont, /* complain_on_overflow */
1536 ppc64_elf_unhandled_reloc, /* special_function */
1537 "R_PPC64_TPREL16_HI", /* name */
1538 FALSE, /* partial_inplace */
1539 0, /* src_mask */
1540 0xffff, /* dst_mask */
1541 FALSE), /* pcrel_offset */
1542
1543 /* Like TPREL16_HI, but adjust for low 16 bits. */
1544 HOWTO (R_PPC64_TPREL16_HA,
1545 16, /* rightshift */
1546 1, /* size (0 = byte, 1 = short, 2 = long) */
1547 16, /* bitsize */
1548 FALSE, /* pc_relative */
1549 0, /* bitpos */
1550 complain_overflow_dont, /* complain_on_overflow */
1551 ppc64_elf_unhandled_reloc, /* special_function */
1552 "R_PPC64_TPREL16_HA", /* name */
1553 FALSE, /* partial_inplace */
1554 0, /* src_mask */
1555 0xffff, /* dst_mask */
1556 FALSE), /* pcrel_offset */
1557
1558 /* Like TPREL16_HI, but next higher group of 16 bits. */
1559 HOWTO (R_PPC64_TPREL16_HIGHER,
1560 32, /* rightshift */
1561 1, /* size (0 = byte, 1 = short, 2 = long) */
1562 16, /* bitsize */
1563 FALSE, /* pc_relative */
1564 0, /* bitpos */
1565 complain_overflow_dont, /* complain_on_overflow */
1566 ppc64_elf_unhandled_reloc, /* special_function */
1567 "R_PPC64_TPREL16_HIGHER", /* name */
1568 FALSE, /* partial_inplace */
1569 0, /* src_mask */
1570 0xffff, /* dst_mask */
1571 FALSE), /* pcrel_offset */
1572
1573 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
1574 HOWTO (R_PPC64_TPREL16_HIGHERA,
1575 32, /* rightshift */
1576 1, /* size (0 = byte, 1 = short, 2 = long) */
1577 16, /* bitsize */
1578 FALSE, /* pc_relative */
1579 0, /* bitpos */
1580 complain_overflow_dont, /* complain_on_overflow */
1581 ppc64_elf_unhandled_reloc, /* special_function */
1582 "R_PPC64_TPREL16_HIGHERA", /* name */
1583 FALSE, /* partial_inplace */
1584 0, /* src_mask */
1585 0xffff, /* dst_mask */
1586 FALSE), /* pcrel_offset */
1587
1588 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
1589 HOWTO (R_PPC64_TPREL16_HIGHEST,
1590 48, /* rightshift */
1591 1, /* size (0 = byte, 1 = short, 2 = long) */
1592 16, /* bitsize */
1593 FALSE, /* pc_relative */
1594 0, /* bitpos */
1595 complain_overflow_dont, /* complain_on_overflow */
1596 ppc64_elf_unhandled_reloc, /* special_function */
1597 "R_PPC64_TPREL16_HIGHEST", /* name */
1598 FALSE, /* partial_inplace */
1599 0, /* src_mask */
1600 0xffff, /* dst_mask */
1601 FALSE), /* pcrel_offset */
1602
1603 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
1604 HOWTO (R_PPC64_TPREL16_HIGHESTA,
1605 48, /* rightshift */
1606 1, /* size (0 = byte, 1 = short, 2 = long) */
1607 16, /* bitsize */
1608 FALSE, /* pc_relative */
1609 0, /* bitpos */
1610 complain_overflow_dont, /* complain_on_overflow */
1611 ppc64_elf_unhandled_reloc, /* special_function */
1612 "R_PPC64_TPREL16_HIGHESTA", /* name */
1613 FALSE, /* partial_inplace */
1614 0, /* src_mask */
1615 0xffff, /* dst_mask */
1616 FALSE), /* pcrel_offset */
1617
1618 /* Like TPREL16, but for insns with a DS field. */
1619 HOWTO (R_PPC64_TPREL16_DS,
1620 0, /* rightshift */
1621 1, /* size (0 = byte, 1 = short, 2 = long) */
1622 16, /* bitsize */
1623 FALSE, /* pc_relative */
1624 0, /* bitpos */
1625 complain_overflow_signed, /* complain_on_overflow */
1626 ppc64_elf_unhandled_reloc, /* special_function */
1627 "R_PPC64_TPREL16_DS", /* name */
1628 FALSE, /* partial_inplace */
1629 0, /* src_mask */
1630 0xfffc, /* dst_mask */
1631 FALSE), /* pcrel_offset */
1632
1633 /* Like TPREL16_DS, but no overflow. */
1634 HOWTO (R_PPC64_TPREL16_LO_DS,
1635 0, /* rightshift */
1636 1, /* size (0 = byte, 1 = short, 2 = long) */
1637 16, /* bitsize */
1638 FALSE, /* pc_relative */
1639 0, /* bitpos */
1640 complain_overflow_dont, /* complain_on_overflow */
1641 ppc64_elf_unhandled_reloc, /* special_function */
1642 "R_PPC64_TPREL16_LO_DS", /* name */
1643 FALSE, /* partial_inplace */
1644 0, /* src_mask */
1645 0xfffc, /* dst_mask */
1646 FALSE), /* pcrel_offset */
1647
1648 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1649 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
1650 to the first entry relative to the TOC base (r2). */
1651 HOWTO (R_PPC64_GOT_TLSGD16,
1652 0, /* rightshift */
1653 1, /* size (0 = byte, 1 = short, 2 = long) */
1654 16, /* bitsize */
1655 FALSE, /* pc_relative */
1656 0, /* bitpos */
1657 complain_overflow_signed, /* complain_on_overflow */
1658 ppc64_elf_unhandled_reloc, /* special_function */
1659 "R_PPC64_GOT_TLSGD16", /* name */
b34976b6 1660 FALSE, /* partial_inplace */
5bd4f169
AM
1661 0, /* src_mask */
1662 0xffff, /* dst_mask */
b34976b6 1663 FALSE), /* pcrel_offset */
5bd4f169 1664
411e1bfb
AM
1665 /* Like GOT_TLSGD16, but no overflow. */
1666 HOWTO (R_PPC64_GOT_TLSGD16_LO,
5bd4f169
AM
1667 0, /* rightshift */
1668 1, /* size (0 = byte, 1 = short, 2 = long) */
1669 16, /* bitsize */
b34976b6 1670 FALSE, /* pc_relative */
5bd4f169
AM
1671 0, /* bitpos */
1672 complain_overflow_dont, /* complain_on_overflow */
805fc799 1673 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1674 "R_PPC64_GOT_TLSGD16_LO", /* name */
b34976b6 1675 FALSE, /* partial_inplace */
5bd4f169
AM
1676 0, /* src_mask */
1677 0xffff, /* dst_mask */
b34976b6 1678 FALSE), /* pcrel_offset */
5bd4f169 1679
411e1bfb
AM
1680 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
1681 HOWTO (R_PPC64_GOT_TLSGD16_HI,
5bd4f169
AM
1682 16, /* rightshift */
1683 1, /* size (0 = byte, 1 = short, 2 = long) */
1684 16, /* bitsize */
b34976b6 1685 FALSE, /* pc_relative */
5bd4f169
AM
1686 0, /* bitpos */
1687 complain_overflow_dont, /* complain_on_overflow */
805fc799 1688 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1689 "R_PPC64_GOT_TLSGD16_HI", /* name */
b34976b6 1690 FALSE, /* partial_inplace */
5bd4f169
AM
1691 0, /* src_mask */
1692 0xffff, /* dst_mask */
b34976b6 1693 FALSE), /* pcrel_offset */
5bd4f169 1694
411e1bfb
AM
1695 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
1696 HOWTO (R_PPC64_GOT_TLSGD16_HA,
5bd4f169
AM
1697 16, /* rightshift */
1698 1, /* size (0 = byte, 1 = short, 2 = long) */
1699 16, /* bitsize */
b34976b6 1700 FALSE, /* pc_relative */
5bd4f169 1701 0, /* bitpos */
411e1bfb 1702 complain_overflow_dont, /* complain_on_overflow */
805fc799 1703 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1704 "R_PPC64_GOT_TLSGD16_HA", /* name */
b34976b6 1705 FALSE, /* partial_inplace */
5bd4f169
AM
1706 0, /* src_mask */
1707 0xffff, /* dst_mask */
b34976b6 1708 FALSE), /* pcrel_offset */
5bd4f169 1709
411e1bfb
AM
1710 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1711 with values (sym+add)@dtpmod and zero, and computes the offset to the
1712 first entry relative to the TOC base (r2). */
1713 HOWTO (R_PPC64_GOT_TLSLD16,
5bd4f169
AM
1714 0, /* rightshift */
1715 1, /* size (0 = byte, 1 = short, 2 = long) */
1716 16, /* bitsize */
b34976b6 1717 FALSE, /* pc_relative */
5bd4f169 1718 0, /* bitpos */
411e1bfb
AM
1719 complain_overflow_signed, /* complain_on_overflow */
1720 ppc64_elf_unhandled_reloc, /* special_function */
1721 "R_PPC64_GOT_TLSLD16", /* name */
b34976b6 1722 FALSE, /* partial_inplace */
d006db6c 1723 0, /* src_mask */
411e1bfb 1724 0xffff, /* dst_mask */
b34976b6 1725 FALSE), /* pcrel_offset */
5bd4f169 1726
411e1bfb
AM
1727 /* Like GOT_TLSLD16, but no overflow. */
1728 HOWTO (R_PPC64_GOT_TLSLD16_LO,
5bd4f169
AM
1729 0, /* rightshift */
1730 1, /* size (0 = byte, 1 = short, 2 = long) */
1731 16, /* bitsize */
b34976b6 1732 FALSE, /* pc_relative */
5bd4f169 1733 0, /* bitpos */
411e1bfb
AM
1734 complain_overflow_dont, /* complain_on_overflow */
1735 ppc64_elf_unhandled_reloc, /* special_function */
1736 "R_PPC64_GOT_TLSLD16_LO", /* name */
b34976b6 1737 FALSE, /* partial_inplace */
d006db6c 1738 0, /* src_mask */
411e1bfb 1739 0xffff, /* dst_mask */
b34976b6 1740 FALSE), /* pcrel_offset */
5bd4f169 1741
411e1bfb
AM
1742 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
1743 HOWTO (R_PPC64_GOT_TLSLD16_HI,
1744 16, /* rightshift */
5bd4f169
AM
1745 1, /* size (0 = byte, 1 = short, 2 = long) */
1746 16, /* bitsize */
b34976b6 1747 FALSE, /* pc_relative */
5bd4f169 1748 0, /* bitpos */
411e1bfb 1749 complain_overflow_dont, /* complain_on_overflow */
805fc799 1750 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1751 "R_PPC64_GOT_TLSLD16_HI", /* name */
b34976b6 1752 FALSE, /* partial_inplace */
d006db6c 1753 0, /* src_mask */
411e1bfb 1754 0xffff, /* dst_mask */
b34976b6 1755 FALSE), /* pcrel_offset */
5bd4f169 1756
411e1bfb
AM
1757 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
1758 HOWTO (R_PPC64_GOT_TLSLD16_HA,
1759 16, /* rightshift */
5bd4f169
AM
1760 1, /* size (0 = byte, 1 = short, 2 = long) */
1761 16, /* bitsize */
b34976b6 1762 FALSE, /* pc_relative */
5bd4f169
AM
1763 0, /* bitpos */
1764 complain_overflow_dont, /* complain_on_overflow */
805fc799 1765 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1766 "R_PPC64_GOT_TLSLD16_HA", /* name */
b34976b6 1767 FALSE, /* partial_inplace */
d006db6c 1768 0, /* src_mask */
411e1bfb 1769 0xffff, /* dst_mask */
b34976b6 1770 FALSE), /* pcrel_offset */
5bd4f169 1771
411e1bfb
AM
1772 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1773 the offset to the entry relative to the TOC base (r2). */
1774 HOWTO (R_PPC64_GOT_DTPREL16_DS,
5bd4f169
AM
1775 0, /* rightshift */
1776 1, /* size (0 = byte, 1 = short, 2 = long) */
1777 16, /* bitsize */
b34976b6 1778 FALSE, /* pc_relative */
5bd4f169 1779 0, /* bitpos */
411e1bfb 1780 complain_overflow_signed, /* complain_on_overflow */
805fc799 1781 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1782 "R_PPC64_GOT_DTPREL16_DS", /* name */
b34976b6 1783 FALSE, /* partial_inplace */
d006db6c 1784 0, /* src_mask */
5bd4f169 1785 0xfffc, /* dst_mask */
b34976b6 1786 FALSE), /* pcrel_offset */
5bd4f169 1787
411e1bfb
AM
1788 /* Like GOT_DTPREL16_DS, but no overflow. */
1789 HOWTO (R_PPC64_GOT_DTPREL16_LO_DS,
5bd4f169 1790 0, /* rightshift */
c061c2d8
AM
1791 1, /* size (0 = byte, 1 = short, 2 = long) */
1792 16, /* bitsize */
b34976b6 1793 FALSE, /* pc_relative */
5bd4f169 1794 0, /* bitpos */
411e1bfb
AM
1795 complain_overflow_dont, /* complain_on_overflow */
1796 ppc64_elf_unhandled_reloc, /* special_function */
1797 "R_PPC64_GOT_DTPREL16_LO_DS", /* name */
b34976b6 1798 FALSE, /* partial_inplace */
d006db6c 1799 0, /* src_mask */
c061c2d8 1800 0xfffc, /* dst_mask */
b34976b6 1801 FALSE), /* pcrel_offset */
5bd4f169 1802
411e1bfb
AM
1803 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
1804 HOWTO (R_PPC64_GOT_DTPREL16_HI,
1805 16, /* rightshift */
5bd4f169
AM
1806 1, /* size (0 = byte, 1 = short, 2 = long) */
1807 16, /* bitsize */
b34976b6 1808 FALSE, /* pc_relative */
5bd4f169
AM
1809 0, /* bitpos */
1810 complain_overflow_dont, /* complain_on_overflow */
411e1bfb
AM
1811 ppc64_elf_unhandled_reloc, /* special_function */
1812 "R_PPC64_GOT_DTPREL16_HI", /* name */
b34976b6 1813 FALSE, /* partial_inplace */
d006db6c 1814 0, /* src_mask */
411e1bfb 1815 0xffff, /* dst_mask */
b34976b6 1816 FALSE), /* pcrel_offset */
5bd4f169 1817
411e1bfb
AM
1818 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
1819 HOWTO (R_PPC64_GOT_DTPREL16_HA,
1820 16, /* rightshift */
1821 1, /* size (0 = byte, 1 = short, 2 = long) */
1822 16, /* bitsize */
1823 FALSE, /* pc_relative */
1824 0, /* bitpos */
1825 complain_overflow_dont, /* complain_on_overflow */
1826 ppc64_elf_unhandled_reloc, /* special_function */
1827 "R_PPC64_GOT_DTPREL16_HA", /* name */
1828 FALSE, /* partial_inplace */
1829 0, /* src_mask */
1830 0xffff, /* dst_mask */
1831 FALSE), /* pcrel_offset */
1832
1833 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1834 offset to the entry relative to the TOC base (r2). */
1835 HOWTO (R_PPC64_GOT_TPREL16_DS,
5bd4f169
AM
1836 0, /* rightshift */
1837 1, /* size (0 = byte, 1 = short, 2 = long) */
1838 16, /* bitsize */
b34976b6 1839 FALSE, /* pc_relative */
5bd4f169
AM
1840 0, /* bitpos */
1841 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1842 ppc64_elf_unhandled_reloc, /* special_function */
1843 "R_PPC64_GOT_TPREL16_DS", /* name */
b34976b6 1844 FALSE, /* partial_inplace */
d006db6c 1845 0, /* src_mask */
ad8e1ba5 1846 0xfffc, /* dst_mask */
b34976b6 1847 FALSE), /* pcrel_offset */
5bd4f169 1848
411e1bfb
AM
1849 /* Like GOT_TPREL16_DS, but no overflow. */
1850 HOWTO (R_PPC64_GOT_TPREL16_LO_DS,
5bd4f169
AM
1851 0, /* rightshift */
1852 1, /* size (0 = byte, 1 = short, 2 = long) */
1853 16, /* bitsize */
b34976b6 1854 FALSE, /* pc_relative */
5bd4f169
AM
1855 0, /* bitpos */
1856 complain_overflow_dont, /* complain_on_overflow */
411e1bfb
AM
1857 ppc64_elf_unhandled_reloc, /* special_function */
1858 "R_PPC64_GOT_TPREL16_LO_DS", /* name */
b34976b6 1859 FALSE, /* partial_inplace */
d006db6c 1860 0, /* src_mask */
ad8e1ba5 1861 0xfffc, /* dst_mask */
b34976b6 1862 FALSE), /* pcrel_offset */
5bd4f169 1863
411e1bfb
AM
1864 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
1865 HOWTO (R_PPC64_GOT_TPREL16_HI,
1866 16, /* rightshift */
5bd4f169
AM
1867 1, /* size (0 = byte, 1 = short, 2 = long) */
1868 16, /* bitsize */
b34976b6 1869 FALSE, /* pc_relative */
5bd4f169 1870 0, /* bitpos */
411e1bfb 1871 complain_overflow_dont, /* complain_on_overflow */
805fc799 1872 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1873 "R_PPC64_GOT_TPREL16_HI", /* name */
b34976b6 1874 FALSE, /* partial_inplace */
d006db6c 1875 0, /* src_mask */
411e1bfb 1876 0xffff, /* dst_mask */
b34976b6 1877 FALSE), /* pcrel_offset */
5bd4f169 1878
411e1bfb
AM
1879 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
1880 HOWTO (R_PPC64_GOT_TPREL16_HA,
1881 16, /* rightshift */
5bd4f169
AM
1882 1, /* size (0 = byte, 1 = short, 2 = long) */
1883 16, /* bitsize */
b34976b6 1884 FALSE, /* pc_relative */
5bd4f169
AM
1885 0, /* bitpos */
1886 complain_overflow_dont, /* complain_on_overflow */
805fc799 1887 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1888 "R_PPC64_GOT_TPREL16_HA", /* name */
b34976b6 1889 FALSE, /* partial_inplace */
d006db6c 1890 0, /* src_mask */
411e1bfb 1891 0xffff, /* dst_mask */
b34976b6 1892 FALSE), /* pcrel_offset */
5bd4f169 1893
25f23106
AM
1894 HOWTO (R_PPC64_JMP_IREL, /* type */
1895 0, /* rightshift */
1896 0, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1897 0, /* bitsize */
1898 FALSE, /* pc_relative */
1899 0, /* bitpos */
1900 complain_overflow_dont, /* complain_on_overflow */
1901 ppc64_elf_unhandled_reloc, /* special_function */
1902 "R_PPC64_JMP_IREL", /* name */
1903 FALSE, /* partial_inplace */
1904 0, /* src_mask */
1905 0, /* dst_mask */
1906 FALSE), /* pcrel_offset */
1907
e054468f
AM
1908 HOWTO (R_PPC64_IRELATIVE, /* type */
1909 0, /* rightshift */
1910 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1911 64, /* bitsize */
1912 FALSE, /* pc_relative */
1913 0, /* bitpos */
1914 complain_overflow_dont, /* complain_on_overflow */
1915 bfd_elf_generic_reloc, /* special_function */
1916 "R_PPC64_IRELATIVE", /* name */
1917 FALSE, /* partial_inplace */
1918 0, /* src_mask */
1919 ONES (64), /* dst_mask */
1920 FALSE), /* pcrel_offset */
1921
25f23106
AM
1922 /* A 16 bit relative relocation. */
1923 HOWTO (R_PPC64_REL16, /* type */
1924 0, /* rightshift */
1925 1, /* size (0 = byte, 1 = short, 2 = long) */
1926 16, /* bitsize */
1927 TRUE, /* pc_relative */
1928 0, /* bitpos */
1929 complain_overflow_bitfield, /* complain_on_overflow */
1930 bfd_elf_generic_reloc, /* special_function */
1931 "R_PPC64_REL16", /* name */
1932 FALSE, /* partial_inplace */
1933 0, /* src_mask */
1934 0xffff, /* dst_mask */
1935 TRUE), /* pcrel_offset */
1936
1937 /* A 16 bit relative relocation without overflow. */
1938 HOWTO (R_PPC64_REL16_LO, /* type */
1939 0, /* rightshift */
1940 1, /* size (0 = byte, 1 = short, 2 = long) */
1941 16, /* bitsize */
1942 TRUE, /* pc_relative */
1943 0, /* bitpos */
1944 complain_overflow_dont,/* complain_on_overflow */
1945 bfd_elf_generic_reloc, /* special_function */
1946 "R_PPC64_REL16_LO", /* name */
1947 FALSE, /* partial_inplace */
1948 0, /* src_mask */
1949 0xffff, /* dst_mask */
1950 TRUE), /* pcrel_offset */
1951
1952 /* The high order 16 bits of a relative address. */
1953 HOWTO (R_PPC64_REL16_HI, /* type */
1954 16, /* rightshift */
1955 1, /* size (0 = byte, 1 = short, 2 = long) */
1956 16, /* bitsize */
1957 TRUE, /* pc_relative */
1958 0, /* bitpos */
1959 complain_overflow_dont, /* complain_on_overflow */
1960 bfd_elf_generic_reloc, /* special_function */
1961 "R_PPC64_REL16_HI", /* name */
1962 FALSE, /* partial_inplace */
1963 0, /* src_mask */
1964 0xffff, /* dst_mask */
1965 TRUE), /* pcrel_offset */
1966
1967 /* The high order 16 bits of a relative address, plus 1 if the contents of
1968 the low 16 bits, treated as a signed number, is negative. */
1969 HOWTO (R_PPC64_REL16_HA, /* type */
1970 16, /* rightshift */
1971 1, /* size (0 = byte, 1 = short, 2 = long) */
1972 16, /* bitsize */
1973 TRUE, /* pc_relative */
1974 0, /* bitpos */
1975 complain_overflow_dont, /* complain_on_overflow */
1976 ppc64_elf_ha_reloc, /* special_function */
1977 "R_PPC64_REL16_HA", /* name */
1978 FALSE, /* partial_inplace */
1979 0, /* src_mask */
1980 0xffff, /* dst_mask */
1981 TRUE), /* pcrel_offset */
1982
5bd4f169
AM
1983 /* GNU extension to record C++ vtable hierarchy. */
1984 HOWTO (R_PPC64_GNU_VTINHERIT, /* type */
1985 0, /* rightshift */
1986 0, /* size (0 = byte, 1 = short, 2 = long) */
1987 0, /* bitsize */
b34976b6 1988 FALSE, /* pc_relative */
5bd4f169
AM
1989 0, /* bitpos */
1990 complain_overflow_dont, /* complain_on_overflow */
1991 NULL, /* special_function */
1992 "R_PPC64_GNU_VTINHERIT", /* name */
b34976b6 1993 FALSE, /* partial_inplace */
5bd4f169
AM
1994 0, /* src_mask */
1995 0, /* dst_mask */
b34976b6 1996 FALSE), /* pcrel_offset */
5bd4f169
AM
1997
1998 /* GNU extension to record C++ vtable member usage. */
1999 HOWTO (R_PPC64_GNU_VTENTRY, /* type */
2000 0, /* rightshift */
2001 0, /* size (0 = byte, 1 = short, 2 = long) */
2002 0, /* bitsize */
b34976b6 2003 FALSE, /* pc_relative */
5bd4f169
AM
2004 0, /* bitpos */
2005 complain_overflow_dont, /* complain_on_overflow */
2006 NULL, /* special_function */
2007 "R_PPC64_GNU_VTENTRY", /* name */
b34976b6 2008 FALSE, /* partial_inplace */
5bd4f169
AM
2009 0, /* src_mask */
2010 0, /* dst_mask */
b34976b6 2011 FALSE), /* pcrel_offset */
5bd4f169
AM
2012};
2013
2014\f
2015/* Initialize the ppc64_elf_howto_table, so that linear accesses can
2016 be done. */
2017
2018static void
4ce794b7 2019ppc_howto_init (void)
5bd4f169
AM
2020{
2021 unsigned int i, type;
2022
2023 for (i = 0;
2024 i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
2025 i++)
2026 {
2027 type = ppc64_elf_howto_raw[i].type;
2028 BFD_ASSERT (type < (sizeof (ppc64_elf_howto_table)
2029 / sizeof (ppc64_elf_howto_table[0])));
2030 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
2031 }
2032}
2033
2034static reloc_howto_type *
4ce794b7
AM
2035ppc64_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2036 bfd_reloc_code_real_type code)
5bd4f169 2037{
411e1bfb 2038 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
5bd4f169
AM
2039
2040 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2041 /* Initialize howto table if needed. */
2042 ppc_howto_init ();
2043
4ce794b7 2044 switch (code)
5bd4f169
AM
2045 {
2046 default:
4ce794b7 2047 return NULL;
5bd4f169 2048
411e1bfb
AM
2049 case BFD_RELOC_NONE: r = R_PPC64_NONE;
2050 break;
2051 case BFD_RELOC_32: r = R_PPC64_ADDR32;
2052 break;
2053 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
2054 break;
2055 case BFD_RELOC_16: r = R_PPC64_ADDR16;
2056 break;
2057 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
2058 break;
2059 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
2060 break;
2061 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
5bd4f169 2062 break;
411e1bfb 2063 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
5bd4f169 2064 break;
411e1bfb 2065 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
5bd4f169 2066 break;
411e1bfb 2067 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
5bd4f169 2068 break;
411e1bfb 2069 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
5bd4f169 2070 break;
411e1bfb 2071 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
5bd4f169 2072 break;
411e1bfb 2073 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
5bd4f169 2074 break;
411e1bfb 2075 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
5bd4f169 2076 break;
411e1bfb 2077 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
5bd4f169 2078 break;
411e1bfb 2079 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
5bd4f169 2080 break;
411e1bfb 2081 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
5bd4f169 2082 break;
411e1bfb 2083 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
5bd4f169 2084 break;
411e1bfb 2085 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
5bd4f169 2086 break;
411e1bfb 2087 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
5bd4f169 2088 break;
411e1bfb 2089 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
5bd4f169 2090 break;
411e1bfb 2091 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
5bd4f169 2092 break;
411e1bfb 2093 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
5bd4f169 2094 break;
411e1bfb 2095 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
5bd4f169 2096 break;
411e1bfb 2097 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
5bd4f169 2098 break;
411e1bfb 2099 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
5bd4f169 2100 break;
411e1bfb 2101 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
5bd4f169 2102 break;
411e1bfb 2103 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
5bd4f169 2104 break;
411e1bfb 2105 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
5bd4f169 2106 break;
411e1bfb 2107 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
5bd4f169 2108 break;
411e1bfb 2109 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
5bd4f169 2110 break;
411e1bfb 2111 case BFD_RELOC_64: r = R_PPC64_ADDR64;
5bd4f169 2112 break;
411e1bfb 2113 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
5bd4f169 2114 break;
411e1bfb 2115 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
5bd4f169 2116 break;
411e1bfb 2117 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
5bd4f169 2118 break;
411e1bfb 2119 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
5bd4f169 2120 break;
411e1bfb 2121 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
5bd4f169 2122 break;
411e1bfb 2123 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
5bd4f169 2124 break;
411e1bfb 2125 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
5bd4f169 2126 break;
411e1bfb 2127 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
5bd4f169 2128 break;
411e1bfb 2129 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
5bd4f169 2130 break;
411e1bfb 2131 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
5bd4f169 2132 break;
411e1bfb 2133 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
5bd4f169 2134 break;
411e1bfb 2135 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
5bd4f169 2136 break;
411e1bfb 2137 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
5bd4f169 2138 break;
411e1bfb 2139 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
5bd4f169 2140 break;
411e1bfb 2141 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
5bd4f169 2142 break;
411e1bfb 2143 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
5bd4f169 2144 break;
411e1bfb 2145 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
5bd4f169 2146 break;
411e1bfb 2147 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
5bd4f169 2148 break;
411e1bfb 2149 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
5bd4f169 2150 break;
411e1bfb 2151 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
5bd4f169 2152 break;
411e1bfb 2153 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
5bd4f169 2154 break;
411e1bfb 2155 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
5bd4f169 2156 break;
411e1bfb 2157 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
5bd4f169 2158 break;
411e1bfb 2159 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
5bd4f169 2160 break;
411e1bfb 2161 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
5bd4f169 2162 break;
411e1bfb 2163 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
5bd4f169 2164 break;
411e1bfb 2165 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
5bd4f169 2166 break;
411e1bfb 2167 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
5bd4f169 2168 break;
727fc41e
AM
2169 case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
2170 break;
2171 case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
2172 break;
411e1bfb 2173 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
5bd4f169 2174 break;
411e1bfb 2175 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
5bd4f169 2176 break;
411e1bfb 2177 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
5bd4f169 2178 break;
411e1bfb 2179 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
5bd4f169 2180 break;
411e1bfb 2181 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
5bd4f169 2182 break;
411e1bfb 2183 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
5bd4f169 2184 break;
411e1bfb
AM
2185 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
2186 break;
2187 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
2188 break;
2189 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
2190 break;
2191 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
2192 break;
2193 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
2194 break;
2195 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
2196 break;
2197 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
2198 break;
2199 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
2200 break;
2201 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
2202 break;
2203 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
2204 break;
2205 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
2206 break;
2207 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
2208 break;
2209 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
2210 break;
2211 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
2212 break;
2213 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
2214 break;
2215 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
2216 break;
2217 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
2218 break;
2219 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
2220 break;
2221 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
2222 break;
2223 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
2224 break;
2225 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
2226 break;
2227 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
2228 break;
2229 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
2230 break;
2231 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
2232 break;
2233 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
2234 break;
2235 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
2236 break;
2237 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
2238 break;
2239 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
2240 break;
2241 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
2242 break;
2243 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
2244 break;
2245 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
2246 break;
2247 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
2248 break;
2249 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
2250 break;
25f23106
AM
2251 case BFD_RELOC_16_PCREL: r = R_PPC64_REL16;
2252 break;
2253 case BFD_RELOC_LO16_PCREL: r = R_PPC64_REL16_LO;
2254 break;
2255 case BFD_RELOC_HI16_PCREL: r = R_PPC64_REL16_HI;
2256 break;
2257 case BFD_RELOC_HI16_S_PCREL: r = R_PPC64_REL16_HA;
2258 break;
411e1bfb
AM
2259 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
2260 break;
2261 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
5bd4f169
AM
2262 break;
2263 }
2264
4ce794b7 2265 return ppc64_elf_howto_table[r];
5bd4f169
AM
2266};
2267
157090f7
AM
2268static reloc_howto_type *
2269ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2270 const char *r_name)
2271{
2272 unsigned int i;
2273
2274 for (i = 0;
2275 i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
2276 i++)
2277 if (ppc64_elf_howto_raw[i].name != NULL
2278 && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
2279 return &ppc64_elf_howto_raw[i];
2280
2281 return NULL;
2282}
2283
5bd4f169
AM
2284/* Set the howto pointer for a PowerPC ELF reloc. */
2285
2286static void
4ce794b7
AM
2287ppc64_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
2288 Elf_Internal_Rela *dst)
5bd4f169 2289{
65f38f15
AM
2290 unsigned int type;
2291
ef60b7ff 2292 /* Initialize howto table if needed. */
5bd4f169 2293 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
2294 ppc_howto_init ();
2295
65f38f15 2296 type = ELF64_R_TYPE (dst->r_info);
d0fb9a8d
JJ
2297 if (type >= (sizeof (ppc64_elf_howto_table)
2298 / sizeof (ppc64_elf_howto_table[0])))
2299 {
2300 (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
3ec2b351
NC
2301 abfd, (int) type);
2302 type = R_PPC64_NONE;
d0fb9a8d 2303 }
65f38f15 2304 cache_ptr->howto = ppc64_elf_howto_table[type];
5bd4f169
AM
2305}
2306
04c9666a 2307/* Handle the R_PPC64_ADDR16_HA and similar relocs. */
5bd4f169
AM
2308
2309static bfd_reloc_status_type
4ce794b7
AM
2310ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2311 void *data, asection *input_section,
2312 bfd *output_bfd, char **error_message)
5bd4f169 2313{
805fc799
AM
2314 /* If this is a relocatable link (output_bfd test tells us), just
2315 call the generic function. Any adjustment will be done at final
2316 link time. */
2317 if (output_bfd != NULL)
cedb70c5 2318 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2319 input_section, output_bfd, error_message);
2320
2321 /* Adjust the addend for sign extension of the low 16 bits.
2322 We won't actually be using the low 16 bits, so trashing them
2323 doesn't matter. */
2324 reloc_entry->addend += 0x8000;
2325 return bfd_reloc_continue;
2326}
5bd4f169 2327
2441e016
AM
2328static bfd_reloc_status_type
2329ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2330 void *data, asection *input_section,
2331 bfd *output_bfd, char **error_message)
2332{
2333 if (output_bfd != NULL)
2334 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2335 input_section, output_bfd, error_message);
2336
699733f6
AM
2337 if (strcmp (symbol->section->name, ".opd") == 0
2338 && (symbol->section->owner->flags & DYNAMIC) == 0)
2441e016
AM
2339 {
2340 bfd_vma dest = opd_entry_value (symbol->section,
2341 symbol->value + reloc_entry->addend,
2342 NULL, NULL);
2343 if (dest != (bfd_vma) -1)
2344 reloc_entry->addend = dest - (symbol->value
2345 + symbol->section->output_section->vma
2346 + symbol->section->output_offset);
2347 }
2348 return bfd_reloc_continue;
2349}
2350
805fc799 2351static bfd_reloc_status_type
4ce794b7
AM
2352ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2353 void *data, asection *input_section,
2354 bfd *output_bfd, char **error_message)
805fc799
AM
2355{
2356 long insn;
04c9666a 2357 enum elf_ppc64_reloc_type r_type;
805fc799
AM
2358 bfd_size_type octets;
2359 /* Disabled until we sort out how ld should choose 'y' vs 'at'. */
b34976b6 2360 bfd_boolean is_power4 = FALSE;
805fc799
AM
2361
2362 /* If this is a relocatable link (output_bfd test tells us), just
2363 call the generic function. Any adjustment will be done at final
2364 link time. */
5bd4f169 2365 if (output_bfd != NULL)
cedb70c5 2366 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2367 input_section, output_bfd, error_message);
2368
2369 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2370 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2371 insn &= ~(0x01 << 21);
4ce794b7 2372 r_type = reloc_entry->howto->type;
805fc799
AM
2373 if (r_type == R_PPC64_ADDR14_BRTAKEN
2374 || r_type == R_PPC64_REL14_BRTAKEN)
cedb70c5 2375 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
805fc799
AM
2376
2377 if (is_power4)
5bd4f169 2378 {
805fc799
AM
2379 /* Set 'a' bit. This is 0b00010 in BO field for branch
2380 on CR(BI) insns (BO == 001at or 011at), and 0b01000
2381 for branch on CTR insns (BO == 1a00t or 1a01t). */
2382 if ((insn & (0x14 << 21)) == (0x04 << 21))
2383 insn |= 0x02 << 21;
2384 else if ((insn & (0x14 << 21)) == (0x10 << 21))
2385 insn |= 0x08 << 21;
2386 else
2441e016 2387 goto out;
5bd4f169 2388 }
805fc799
AM
2389 else
2390 {
2391 bfd_vma target = 0;
2392 bfd_vma from;
5bd4f169 2393
805fc799
AM
2394 if (!bfd_is_com_section (symbol->section))
2395 target = symbol->value;
2396 target += symbol->section->output_section->vma;
2397 target += symbol->section->output_offset;
2398 target += reloc_entry->addend;
5bd4f169 2399
805fc799
AM
2400 from = (reloc_entry->address
2401 + input_section->output_offset
2402 + input_section->output_section->vma);
5bd4f169 2403
805fc799
AM
2404 /* Invert 'y' bit if not the default. */
2405 if ((bfd_signed_vma) (target - from) < 0)
2406 insn ^= 0x01 << 21;
2407 }
4ce794b7 2408 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2441e016
AM
2409 out:
2410 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
2411 input_section, output_bfd, error_message);
805fc799 2412}
5bd4f169 2413
805fc799 2414static bfd_reloc_status_type
4ce794b7
AM
2415ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2416 void *data, asection *input_section,
2417 bfd *output_bfd, char **error_message)
805fc799
AM
2418{
2419 /* If this is a relocatable link (output_bfd test tells us), just
2420 call the generic function. Any adjustment will be done at final
2421 link time. */
2422 if (output_bfd != NULL)
cedb70c5 2423 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799 2424 input_section, output_bfd, error_message);
5bd4f169 2425
805fc799
AM
2426 /* Subtract the symbol section base address. */
2427 reloc_entry->addend -= symbol->section->output_section->vma;
5bd4f169
AM
2428 return bfd_reloc_continue;
2429}
2430
805fc799 2431static bfd_reloc_status_type
4ce794b7
AM
2432ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2433 void *data, asection *input_section,
2434 bfd *output_bfd, char **error_message)
805fc799
AM
2435{
2436 /* If this is a relocatable link (output_bfd test tells us), just
2437 call the generic function. Any adjustment will be done at final
2438 link time. */
2439 if (output_bfd != NULL)
cedb70c5 2440 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2441 input_section, output_bfd, error_message);
2442
2443 /* Subtract the symbol section base address. */
2444 reloc_entry->addend -= symbol->section->output_section->vma;
2445
2446 /* Adjust the addend for sign extension of the low 16 bits. */
2447 reloc_entry->addend += 0x8000;
2448 return bfd_reloc_continue;
2449}
2450
2451static bfd_reloc_status_type
4ce794b7
AM
2452ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2453 void *data, asection *input_section,
2454 bfd *output_bfd, char **error_message)
805fc799
AM
2455{
2456 bfd_vma TOCstart;
2457
2458 /* If this is a relocatable link (output_bfd test tells us), just
2459 call the generic function. Any adjustment will be done at final
2460 link time. */
2461 if (output_bfd != NULL)
cedb70c5 2462 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2463 input_section, output_bfd, error_message);
2464
2465 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2466 if (TOCstart == 0)
2467 TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2468
2469 /* Subtract the TOC base address. */
2470 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2471 return bfd_reloc_continue;
2472}
2473
2474static bfd_reloc_status_type
4ce794b7
AM
2475ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2476 void *data, asection *input_section,
2477 bfd *output_bfd, char **error_message)
805fc799
AM
2478{
2479 bfd_vma TOCstart;
2480
2481 /* If this is a relocatable link (output_bfd test tells us), just
2482 call the generic function. Any adjustment will be done at final
2483 link time. */
2484 if (output_bfd != NULL)
cedb70c5 2485 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2486 input_section, output_bfd, error_message);
2487
2488 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2489 if (TOCstart == 0)
2490 TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2491
2492 /* Subtract the TOC base address. */
2493 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2494
2495 /* Adjust the addend for sign extension of the low 16 bits. */
2496 reloc_entry->addend += 0x8000;
2497 return bfd_reloc_continue;
2498}
2499
2500static bfd_reloc_status_type
4ce794b7
AM
2501ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2502 void *data, asection *input_section,
2503 bfd *output_bfd, char **error_message)
805fc799
AM
2504{
2505 bfd_vma TOCstart;
2506 bfd_size_type octets;
2507
2508 /* If this is a relocatable link (output_bfd test tells us), just
2509 call the generic function. Any adjustment will be done at final
2510 link time. */
2511 if (output_bfd != NULL)
cedb70c5 2512 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2513 input_section, output_bfd, error_message);
2514
2515 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2516 if (TOCstart == 0)
2517 TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2518
2519 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2520 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
2521 return bfd_reloc_ok;
2522}
2523
2524static bfd_reloc_status_type
4ce794b7
AM
2525ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2526 void *data, asection *input_section,
2527 bfd *output_bfd, char **error_message)
805fc799
AM
2528{
2529 /* If this is a relocatable link (output_bfd test tells us), just
2530 call the generic function. Any adjustment will be done at final
2531 link time. */
2532 if (output_bfd != NULL)
cedb70c5 2533 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2534 input_section, output_bfd, error_message);
2535
2536 if (error_message != NULL)
2537 {
2538 static char buf[60];
2539 sprintf (buf, "generic linker can't handle %s",
2540 reloc_entry->howto->name);
2541 *error_message = buf;
2542 }
2543 return bfd_reloc_dangerous;
2544}
2545
927be08e
AM
2546/* Track GOT entries needed for a given symbol. We might need more
2547 than one got entry per symbol. */
2548struct got_entry
2549{
2550 struct got_entry *next;
2551
2552 /* The symbol addend that we'll be placing in the GOT. */
2553 bfd_vma addend;
2554
2555 /* Unlike other ELF targets, we use separate GOT entries for the same
2556 symbol referenced from different input files. This is to support
2557 automatic multiple TOC/GOT sections, where the TOC base can vary
2558 from one input file to another. After partitioning into TOC groups
2559 we merge entries within the group.
2560
2561 Point to the BFD owning this GOT entry. */
2562 bfd *owner;
2563
2564 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
2565 TLS_TPREL or TLS_DTPREL for tls entries. */
f961d9dd 2566 unsigned char tls_type;
927be08e
AM
2567
2568 /* Non-zero if got.ent points to real entry. */
f961d9dd 2569 unsigned char is_indirect;
927be08e
AM
2570
2571 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
2572 union
2573 {
2574 bfd_signed_vma refcount;
2575 bfd_vma offset;
2576 struct got_entry *ent;
2577 } got;
2578};
2579
2580/* The same for PLT. */
2581struct plt_entry
2582{
2583 struct plt_entry *next;
2584
2585 bfd_vma addend;
2586
2587 union
2588 {
2589 bfd_signed_vma refcount;
2590 bfd_vma offset;
2591 } plt;
2592};
2593
e717da7e
AM
2594struct ppc64_elf_obj_tdata
2595{
2596 struct elf_obj_tdata elf;
2597
2598 /* Shortcuts to dynamic linker sections. */
2599 asection *got;
2600 asection *relgot;
2601
b3fac117
AM
2602 /* Used during garbage collection. We attach global symbols defined
2603 on removed .opd entries to this section so that the sym is removed. */
2604 asection *deleted_section;
81688140 2605
927be08e 2606 /* TLS local dynamic got entry handling. Support for multiple GOT
e717da7e 2607 sections means we potentially need one of these for each input bfd. */
927be08e 2608 struct got_entry tlsld_got;
8860955f
AM
2609
2610 /* A copy of relocs before they are modified for --emit-relocs. */
2611 Elf_Internal_Rela *opd_relocs;
d77c8a4b
AM
2612
2613 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
2614 the reloc to be in the range -32768 to 32767. */
98528052
AM
2615 unsigned int has_small_toc_reloc : 1;
2616
560c8763
AM
2617 /* Set if toc/got ha relocs detected not using r2, or lo reloc
2618 instruction not one we handle. */
2619 unsigned int unexpected_toc_insn : 1;
e717da7e
AM
2620};
2621
2622#define ppc64_elf_tdata(bfd) \
2623 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
2624
2625#define ppc64_tlsld_got(bfd) \
2626 (&ppc64_elf_tdata (bfd)->tlsld_got)
2627
0c8d6e5c
AM
2628#define is_ppc64_elf(bfd) \
2629 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
4dfe6ac6 2630 && elf_object_id (bfd) == PPC64_ELF_DATA)
0c8d6e5c 2631
e717da7e
AM
2632/* Override the generic function because we store some extras. */
2633
2634static bfd_boolean
2635ppc64_elf_mkobject (bfd *abfd)
2636{
0ffa91dd 2637 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
4dfe6ac6 2638 PPC64_ELF_DATA);
e717da7e
AM
2639}
2640
feee612b
AM
2641/* Fix bad default arch selected for a 64 bit input bfd when the
2642 default is 32 bit. */
2643
b34976b6 2644static bfd_boolean
4ce794b7 2645ppc64_elf_object_p (bfd *abfd)
feee612b
AM
2646{
2647 if (abfd->arch_info->the_default && abfd->arch_info->bits_per_word == 32)
2648 {
2649 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
2650
2651 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
2652 {
2653 /* Relies on arch after 32 bit default being 64 bit default. */
2654 abfd->arch_info = abfd->arch_info->next;
2655 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
2656 }
2657 }
b34976b6 2658 return TRUE;
feee612b
AM
2659}
2660
d37c89e5
AM
2661/* Support for core dump NOTE sections. */
2662
2663static bfd_boolean
2664ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2665{
eea6121a 2666 size_t offset, size;
d37c89e5
AM
2667
2668 if (note->descsz != 504)
2669 return FALSE;
2670
2671 /* pr_cursig */
2672 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
2673
2674 /* pr_pid */
261b8d08 2675 elf_tdata (abfd)->core_lwpid = bfd_get_32 (abfd, note->descdata + 32);
d37c89e5
AM
2676
2677 /* pr_reg */
2678 offset = 112;
eea6121a 2679 size = 384;
d37c89e5
AM
2680
2681 /* Make a ".reg/999" section. */
2682 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 2683 size, note->descpos + offset);
d37c89e5
AM
2684}
2685
2686static bfd_boolean
2687ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
2688{
2689 if (note->descsz != 136)
2690 return FALSE;
2691
bc989cdc
JK
2692 elf_tdata (abfd)->core_pid
2693 = bfd_get_32 (abfd, note->descdata + 24);
d37c89e5
AM
2694 elf_tdata (abfd)->core_program
2695 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
2696 elf_tdata (abfd)->core_command
2697 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
2698
2699 return TRUE;
2700}
2701
183e98be
AM
2702static char *
2703ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
2704 ...)
2705{
2706 switch (note_type)
2707 {
2708 default:
2709 return NULL;
2710
2711 case NT_PRPSINFO:
2712 {
2713 char data[136];
2714 va_list ap;
2715
2716 va_start (ap, note_type);
2717 memset (data, 0, 40);
2718 strncpy (data + 40, va_arg (ap, const char *), 16);
2719 strncpy (data + 56, va_arg (ap, const char *), 80);
2720 va_end (ap);
2721 return elfcore_write_note (abfd, buf, bufsiz,
2722 "CORE", note_type, data, sizeof (data));
2723 }
2724
2725 case NT_PRSTATUS:
2726 {
2727 char data[504];
2728 va_list ap;
2729 long pid;
2730 int cursig;
2731 const void *greg;
2732
2733 va_start (ap, note_type);
2734 memset (data, 0, 112);
2735 pid = va_arg (ap, long);
2736 bfd_put_32 (abfd, pid, data + 32);
2737 cursig = va_arg (ap, int);
2738 bfd_put_16 (abfd, cursig, data + 12);
2739 greg = va_arg (ap, const void *);
2740 memcpy (data + 112, greg, 384);
2741 memset (data + 496, 0, 8);
2742 va_end (ap);
2743 return elfcore_write_note (abfd, buf, bufsiz,
2744 "CORE", note_type, data, sizeof (data));
2745 }
2746 }
2747}
2748
5d35169e
AM
2749/* Add extra PPC sections. */
2750
b35d266b 2751static const struct bfd_elf_special_section ppc64_elf_special_sections[]=
7f4d3958 2752{
0112cd26
NC
2753 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
2754 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2755 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2756 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2757 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2758 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2759 { NULL, 0, 0, 0, 0 }
5d35169e
AM
2760};
2761
7c8fe5c4
AM
2762enum _ppc64_sec_type {
2763 sec_normal = 0,
2764 sec_opd = 1,
2765 sec_toc = 2
2766};
2767
f0abc2a1
AM
2768struct _ppc64_elf_section_data
2769{
2770 struct bfd_elf_section_data elf;
411e1bfb 2771
f0abc2a1
AM
2772 union
2773 {
74f0fb50
AM
2774 /* An array with one entry for each opd function descriptor. */
2775 struct _opd_sec_data
2776 {
2777 /* Points to the function code section for local opd entries. */
2778 asection **func_sec;
2779
2780 /* After editing .opd, adjust references to opd local syms. */
2781 long *adjust;
2782 } opd;
7c8fe5c4 2783
3a71aa26
AM
2784 /* An array for toc sections, indexed by offset/8. */
2785 struct _toc_sec_data
2786 {
2787 /* Specifies the relocation symbol index used at a given toc offset. */
2788 unsigned *symndx;
2789
2790 /* And the relocation addend. */
2791 bfd_vma *add;
2792 } toc;
7c8fe5c4
AM
2793 } u;
2794
2795 enum _ppc64_sec_type sec_type:2;
411e1bfb 2796
7c8fe5c4
AM
2797 /* Flag set when small branches are detected. Used to
2798 select suitable defaults for the stub group size. */
2799 unsigned int has_14bit_branch:1;
f0abc2a1
AM
2800};
2801
2802#define ppc64_elf_section_data(sec) \
411e1bfb 2803 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
f0abc2a1
AM
2804
2805static bfd_boolean
4ce794b7 2806ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
f0abc2a1 2807{
f592407e
AM
2808 if (!sec->used_by_bfd)
2809 {
2810 struct _ppc64_elf_section_data *sdata;
2811 bfd_size_type amt = sizeof (*sdata);
f0abc2a1 2812
f592407e
AM
2813 sdata = bfd_zalloc (abfd, amt);
2814 if (sdata == NULL)
2815 return FALSE;
2816 sec->used_by_bfd = sdata;
2817 }
f0abc2a1
AM
2818
2819 return _bfd_elf_new_section_hook (abfd, sec);
2820}
4025353c 2821
74f0fb50 2822static struct _opd_sec_data *
4025353c
AM
2823get_opd_info (asection * sec)
2824{
2825 if (sec != NULL
2826 && ppc64_elf_section_data (sec) != NULL
7c8fe5c4 2827 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
74f0fb50 2828 return &ppc64_elf_section_data (sec)->u.opd;
4025353c
AM
2829 return NULL;
2830}
90e3cdf2
JJ
2831\f
2832/* Parameters for the qsort hook. */
90e3cdf2
JJ
2833static bfd_boolean synthetic_relocatable;
2834
699733f6 2835/* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
90e3cdf2
JJ
2836
2837static int
2838compare_symbols (const void *ap, const void *bp)
2839{
2840 const asymbol *a = * (const asymbol **) ap;
2841 const asymbol *b = * (const asymbol **) bp;
2842
699733f6
AM
2843 /* Section symbols first. */
2844 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
90e3cdf2 2845 return -1;
699733f6 2846 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
90e3cdf2
JJ
2847 return 1;
2848
699733f6 2849 /* then .opd symbols. */
ffcfec52
AM
2850 if (strcmp (a->section->name, ".opd") == 0
2851 && strcmp (b->section->name, ".opd") != 0)
90e3cdf2 2852 return -1;
ffcfec52
AM
2853 if (strcmp (a->section->name, ".opd") != 0
2854 && strcmp (b->section->name, ".opd") == 0)
90e3cdf2
JJ
2855 return 1;
2856
699733f6 2857 /* then other code symbols. */
90e3cdf2
JJ
2858 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2859 == (SEC_CODE | SEC_ALLOC)
2860 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2861 != (SEC_CODE | SEC_ALLOC))
2862 return -1;
2863
2864 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2865 != (SEC_CODE | SEC_ALLOC)
2866 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2867 == (SEC_CODE | SEC_ALLOC))
2868 return 1;
2869
2870 if (synthetic_relocatable)
2871 {
2872 if (a->section->id < b->section->id)
2873 return -1;
2874
2875 if (a->section->id > b->section->id)
2876 return 1;
2877 }
2878
2879 if (a->value + a->section->vma < b->value + b->section->vma)
2880 return -1;
2881
2882 if (a->value + a->section->vma > b->value + b->section->vma)
2883 return 1;
2884
4d35a0aa
AM
2885 /* For syms with the same value, prefer strong dynamic global function
2886 syms over other syms. */
2887 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
2888 return -1;
2889
2890 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
2891 return 1;
2892
2893 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
2894 return -1;
2895
2896 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
2897 return 1;
2898
2899 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
2900 return -1;
2901
2902 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
2903 return 1;
2904
2905 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
2906 return -1;
2907
2908 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
2909 return 1;
2910
90e3cdf2
JJ
2911 return 0;
2912}
2913
699733f6 2914/* Search SYMS for a symbol of the given VALUE. */
90e3cdf2 2915
699733f6
AM
2916static asymbol *
2917sym_exists_at (asymbol **syms, long lo, long hi, int id, bfd_vma value)
90e3cdf2 2918{
699733f6 2919 long mid;
90e3cdf2 2920
699733f6
AM
2921 if (id == -1)
2922 {
2923 while (lo < hi)
2924 {
2925 mid = (lo + hi) >> 1;
2926 if (syms[mid]->value + syms[mid]->section->vma < value)
2927 lo = mid + 1;
2928 else if (syms[mid]->value + syms[mid]->section->vma > value)
2929 hi = mid;
2930 else
2931 return syms[mid];
2932 }
2933 }
2934 else
2935 {
2936 while (lo < hi)
2937 {
2938 mid = (lo + hi) >> 1;
2939 if (syms[mid]->section->id < id)
2940 lo = mid + 1;
2941 else if (syms[mid]->section->id > id)
2942 hi = mid;
2943 else if (syms[mid]->value < value)
2944 lo = mid + 1;
2945 else if (syms[mid]->value > value)
2946 hi = mid;
2947 else
2948 return syms[mid];
2949 }
2950 }
2951 return NULL;
90e3cdf2
JJ
2952}
2953
468392fb
AM
2954static bfd_boolean
2955section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
2956{
2957 bfd_vma vma = *(bfd_vma *) ptr;
2958 return ((section->flags & SEC_ALLOC) != 0
2959 && section->vma <= vma
2960 && vma < section->vma + section->size);
2961}
2962
699733f6 2963/* Create synthetic symbols, effectively restoring "dot-symbol" function
468392fb 2964 entry syms. Also generate @plt symbols for the glink branch table. */
90e3cdf2
JJ
2965
2966static long
a7535cf3
AM
2967ppc64_elf_get_synthetic_symtab (bfd *abfd,
2968 long static_count, asymbol **static_syms,
2969 long dyn_count, asymbol **dyn_syms,
c9727e01 2970 asymbol **ret)
90e3cdf2
JJ
2971{
2972 asymbol *s;
699733f6
AM
2973 long i;
2974 long count;
90e3cdf2 2975 char *names;
a7535cf3 2976 long symcount, codesecsym, codesecsymend, secsymend, opdsymend;
699733f6 2977 asection *opd;
90e3cdf2 2978 bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
a7535cf3 2979 asymbol **syms;
90e3cdf2
JJ
2980
2981 *ret = NULL;
2982
2983 opd = bfd_get_section_by_name (abfd, ".opd");
2984 if (opd == NULL)
2985 return 0;
2986
a7535cf3 2987 symcount = static_count;
c9727e01 2988 if (!relocatable)
a7535cf3 2989 symcount += dyn_count;
90e3cdf2 2990 if (symcount == 0)
c9727e01 2991 return 0;
90e3cdf2 2992
a7535cf3
AM
2993 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
2994 if (syms == NULL)
7356fed5 2995 return -1;
a7535cf3
AM
2996
2997 if (!relocatable && static_count != 0 && dyn_count != 0)
2998 {
2999 /* Use both symbol tables. */
3000 memcpy (syms, static_syms, static_count * sizeof (*syms));
3001 memcpy (syms + static_count, dyn_syms, (dyn_count + 1) * sizeof (*syms));
3002 }
3003 else if (!relocatable && static_count == 0)
3004 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
3005 else
3006 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
3007
90e3cdf2 3008 synthetic_relocatable = relocatable;
595da8c5 3009 qsort (syms, symcount, sizeof (*syms), compare_symbols);
90e3cdf2 3010
c9727e01
AM
3011 if (!relocatable && symcount > 1)
3012 {
3013 long j;
3014 /* Trim duplicate syms, since we may have merged the normal and
3015 dynamic symbols. Actually, we only care about syms that have
3b36f7e6 3016 different values, so trim any with the same value. */
c9727e01
AM
3017 for (i = 1, j = 1; i < symcount; ++i)
3018 if (syms[i - 1]->value + syms[i - 1]->section->vma
3019 != syms[i]->value + syms[i]->section->vma)
3020 syms[j++] = syms[i];
3021 symcount = j;
3022 }
3023
699733f6 3024 i = 0;
ffcfec52 3025 if (strcmp (syms[i]->section->name, ".opd") == 0)
699733f6
AM
3026 ++i;
3027 codesecsym = i;
90e3cdf2 3028
699733f6
AM
3029 for (; i < symcount; ++i)
3030 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3031 != (SEC_CODE | SEC_ALLOC))
3032 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
3033 break;
3034 codesecsymend = i;
90e3cdf2 3035
699733f6
AM
3036 for (; i < symcount; ++i)
3037 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
3038 break;
3039 secsymend = i;
90e3cdf2 3040
699733f6 3041 for (; i < symcount; ++i)
ffcfec52 3042 if (strcmp (syms[i]->section->name, ".opd") != 0)
699733f6
AM
3043 break;
3044 opdsymend = i;
90e3cdf2 3045
699733f6
AM
3046 for (; i < symcount; ++i)
3047 if ((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3048 != (SEC_CODE | SEC_ALLOC))
3049 break;
3050 symcount = i;
3051
c9727e01 3052 count = 0;
90e3cdf2 3053
699733f6 3054 if (relocatable)
90e3cdf2 3055 {
699733f6
AM
3056 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3057 arelent *r;
3058 size_t size;
3059 long relcount;
90e3cdf2 3060
468392fb
AM
3061 if (opdsymend == secsymend)
3062 goto done;
3063
699733f6 3064 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
90e3cdf2 3065 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
7356fed5 3066 if (relcount == 0)
c9727e01 3067 goto done;
90e3cdf2 3068
7356fed5
AM
3069 if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
3070 {
3071 count = -1;
3072 goto done;
3073 }
3074
699733f6 3075 size = 0;
595da8c5 3076 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
699733f6
AM
3077 {
3078 asymbol *sym;
90e3cdf2 3079
595da8c5 3080 while (r < opd->relocation + relcount
699733f6
AM
3081 && r->address < syms[i]->value + opd->vma)
3082 ++r;
90e3cdf2 3083
595da8c5 3084 if (r == opd->relocation + relcount)
699733f6 3085 break;
90e3cdf2 3086
699733f6
AM
3087 if (r->address != syms[i]->value + opd->vma)
3088 continue;
90e3cdf2 3089
699733f6
AM
3090 if (r->howto->type != R_PPC64_ADDR64)
3091 continue;
90e3cdf2 3092
699733f6
AM
3093 sym = *r->sym_ptr_ptr;
3094 if (!sym_exists_at (syms, opdsymend, symcount,
3095 sym->section->id, sym->value + r->addend))
3096 {
3097 ++count;
3098 size += sizeof (asymbol);
3099 size += strlen (syms[i]->name) + 2;
3100 }
3101 }
90e3cdf2 3102
699733f6
AM
3103 s = *ret = bfd_malloc (size);
3104 if (s == NULL)
3105 {
7356fed5 3106 count = -1;
c9727e01 3107 goto done;
699733f6 3108 }
90e3cdf2 3109
699733f6 3110 names = (char *) (s + count);
90e3cdf2 3111
595da8c5 3112 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
90e3cdf2 3113 {
699733f6 3114 asymbol *sym;
90e3cdf2 3115
595da8c5 3116 while (r < opd->relocation + relcount
699733f6
AM
3117 && r->address < syms[i]->value + opd->vma)
3118 ++r;
90e3cdf2 3119
595da8c5 3120 if (r == opd->relocation + relcount)
699733f6
AM
3121 break;
3122
3123 if (r->address != syms[i]->value + opd->vma)
3124 continue;
3125
3126 if (r->howto->type != R_PPC64_ADDR64)
3127 continue;
90e3cdf2 3128
699733f6
AM
3129 sym = *r->sym_ptr_ptr;
3130 if (!sym_exists_at (syms, opdsymend, symcount,
3131 sym->section->id, sym->value + r->addend))
3132 {
3133 size_t len;
3134
3135 *s = *syms[i];
6ba2a415 3136 s->flags |= BSF_SYNTHETIC;
699733f6
AM
3137 s->section = sym->section;
3138 s->value = sym->value + r->addend;
3139 s->name = names;
3140 *names++ = '.';
3141 len = strlen (syms[i]->name);
3142 memcpy (names, syms[i]->name, len + 1);
3143 names += len + 1;
6f610d07
UW
3144 /* Have udata.p point back to the original symbol this
3145 synthetic symbol was derived from. */
3146 s->udata.p = syms[i];
699733f6
AM
3147 s++;
3148 }
3149 }
3150 }
3151 else
90e3cdf2 3152 {
468392fb 3153 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
699733f6
AM
3154 bfd_byte *contents;
3155 size_t size;
468392fb
AM
3156 long plt_count = 0;
3157 bfd_vma glink_vma = 0, resolv_vma = 0;
3158 asection *dynamic, *glink = NULL, *relplt = NULL;
3159 arelent *p;
90e3cdf2 3160
699733f6
AM
3161 if (!bfd_malloc_and_get_section (abfd, opd, &contents))
3162 {
3163 if (contents)
c9727e01
AM
3164 {
3165 free_contents_and_exit:
3166 free (contents);
3167 }
7356fed5 3168 count = -1;
c9727e01 3169 goto done;
699733f6 3170 }
90e3cdf2 3171
699733f6
AM
3172 size = 0;
3173 for (i = secsymend; i < opdsymend; ++i)
3174 {
3175 bfd_vma ent;
90e3cdf2 3176
5ef11c02
AM
3177 /* Ignore bogus symbols. */
3178 if (syms[i]->value > opd->size - 8)
3179 continue;
3180
699733f6
AM
3181 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3182 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3183 {
3184 ++count;
3185 size += sizeof (asymbol);
3186 size += strlen (syms[i]->name) + 2;
3187 }
3188 }
90e3cdf2 3189
468392fb 3190 /* Get start of .glink stubs from DT_PPC64_GLINK. */
066ee829
AM
3191 if (dyn_count != 0
3192 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
468392fb
AM
3193 {
3194 bfd_byte *dynbuf, *extdyn, *extdynend;
3195 size_t extdynsize;
3196 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3197
3198 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
3199 goto free_contents_and_exit;
3200
3201 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
3202 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
3203
3204 extdyn = dynbuf;
3205 extdynend = extdyn + dynamic->size;
3206 for (; extdyn < extdynend; extdyn += extdynsize)
3207 {
3208 Elf_Internal_Dyn dyn;
3209 (*swap_dyn_in) (abfd, extdyn, &dyn);
3210
3211 if (dyn.d_tag == DT_NULL)
3212 break;
3213
3214 if (dyn.d_tag == DT_PPC64_GLINK)
3215 {
3216 /* The first glink stub starts at offset 32; see comment in
3217 ppc64_elf_finish_dynamic_sections. */
3218 glink_vma = dyn.d_un.d_val + 32;
3219 /* The .glink section usually does not survive the final
3220 link; search for the section (usually .text) where the
3221 glink stubs now reside. */
3222 glink = bfd_sections_find_if (abfd, section_covers_vma,
3223 &glink_vma);
3224 break;
3225 }
3226 }
3227
3228 free (dynbuf);
3229 }
3230
3231 if (glink != NULL)
3232 {
3233 /* Determine __glink trampoline by reading the relative branch
3234 from the first glink stub. */
3235 bfd_byte buf[4];
3236 if (bfd_get_section_contents (abfd, glink, buf,
3237 glink_vma + 4 - glink->vma, 4))
3238 {
3239 unsigned int insn = bfd_get_32 (abfd, buf);
3240 insn ^= B_DOT;
3241 if ((insn & ~0x3fffffc) == 0)
3242 resolv_vma = glink_vma + 4 + (insn ^ 0x2000000) - 0x2000000;
3243 }
3244
3245 if (resolv_vma)
3246 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
468392fb 3247
066ee829
AM
3248 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
3249 if (relplt != NULL)
3250 {
3251 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3252 if (! (*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
3253 goto free_contents_and_exit;
468392fb 3254
066ee829
AM
3255 plt_count = relplt->size / sizeof (Elf64_External_Rela);
3256 size += plt_count * sizeof (asymbol);
468392fb 3257
066ee829
AM
3258 p = relplt->relocation;
3259 for (i = 0; i < plt_count; i++, p++)
e054468f
AM
3260 {
3261 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
3262 if (p->addend != 0)
3263 size += sizeof ("+0x") - 1 + 16;
3264 }
066ee829 3265 }
468392fb
AM
3266 }
3267
699733f6
AM
3268 s = *ret = bfd_malloc (size);
3269 if (s == NULL)
7356fed5 3270 goto free_contents_and_exit;
90e3cdf2 3271
468392fb 3272 names = (char *) (s + count + plt_count + (resolv_vma != 0));
90e3cdf2 3273
699733f6 3274 for (i = secsymend; i < opdsymend; ++i)
90e3cdf2 3275 {
699733f6 3276 bfd_vma ent;
90e3cdf2 3277
5ef11c02
AM
3278 if (syms[i]->value > opd->size - 8)
3279 continue;
3280
699733f6
AM
3281 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3282 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
90e3cdf2 3283 {
c9727e01 3284 long lo, hi;
699733f6 3285 size_t len;
c9727e01 3286 asection *sec = abfd->sections;
90e3cdf2 3287
699733f6
AM
3288 *s = *syms[i];
3289 lo = codesecsym;
3290 hi = codesecsymend;
3291 while (lo < hi)
3292 {
c9727e01 3293 long mid = (lo + hi) >> 1;
699733f6
AM
3294 if (syms[mid]->section->vma < ent)
3295 lo = mid + 1;
3296 else if (syms[mid]->section->vma > ent)
3297 hi = mid;
3298 else
c9727e01
AM
3299 {
3300 sec = syms[mid]->section;
3301 break;
3302 }
699733f6
AM
3303 }
3304
c9727e01 3305 if (lo >= hi && lo > codesecsym)
699733f6 3306 sec = syms[lo - 1]->section;
699733f6
AM
3307
3308 for (; sec != NULL; sec = sec->next)
3309 {
3310 if (sec->vma > ent)
3311 break;
63524580
JK
3312 /* SEC_LOAD may not be set if SEC is from a separate debug
3313 info file. */
3314 if ((sec->flags & SEC_ALLOC) == 0)
699733f6
AM
3315 break;
3316 if ((sec->flags & SEC_CODE) != 0)
3317 s->section = sec;
3318 }
6ba2a415 3319 s->flags |= BSF_SYNTHETIC;
699733f6
AM
3320 s->value = ent - s->section->vma;
3321 s->name = names;
3322 *names++ = '.';
3323 len = strlen (syms[i]->name);
3324 memcpy (names, syms[i]->name, len + 1);
3325 names += len + 1;
6f610d07
UW
3326 /* Have udata.p point back to the original symbol this
3327 synthetic symbol was derived from. */
3328 s->udata.p = syms[i];
699733f6 3329 s++;
90e3cdf2 3330 }
90e3cdf2 3331 }
699733f6 3332 free (contents);
468392fb
AM
3333
3334 if (glink != NULL && relplt != NULL)
3335 {
3336 if (resolv_vma)
3337 {
3338 /* Add a symbol for the main glink trampoline. */
86a4952b 3339 memset (s, 0, sizeof *s);
468392fb 3340 s->the_bfd = abfd;
6ba2a415 3341 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
468392fb
AM
3342 s->section = glink;
3343 s->value = resolv_vma - glink->vma;
3344 s->name = names;
3345 memcpy (names, "__glink_PLTresolve", sizeof ("__glink_PLTresolve"));
3346 names += sizeof ("__glink_PLTresolve");
3347 s++;
3348 count++;
3349 }
3350
3351 /* FIXME: It would be very much nicer to put sym@plt on the
3352 stub rather than on the glink branch table entry. The
3353 objdump disassembler would then use a sensible symbol
3354 name on plt calls. The difficulty in doing so is
3355 a) finding the stubs, and,
3356 b) matching stubs against plt entries, and,
3357 c) there can be multiple stubs for a given plt entry.
3358
3359 Solving (a) could be done by code scanning, but older
3360 ppc64 binaries used different stubs to current code.
3361 (b) is the tricky one since you need to known the toc
3362 pointer for at least one function that uses a pic stub to
3363 be able to calculate the plt address referenced.
3364 (c) means gdb would need to set multiple breakpoints (or
3365 find the glink branch itself) when setting breakpoints
3366 for pending shared library loads. */
3367 p = relplt->relocation;
3368 for (i = 0; i < plt_count; i++, p++)
3369 {
3370 size_t len;
3371
3372 *s = **p->sym_ptr_ptr;
3373 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
3374 we are defining a symbol, ensure one of them is set. */
3375 if ((s->flags & BSF_LOCAL) == 0)
3376 s->flags |= BSF_GLOBAL;
6ba2a415 3377 s->flags |= BSF_SYNTHETIC;
468392fb
AM
3378 s->section = glink;
3379 s->value = glink_vma - glink->vma;
3380 s->name = names;
3381 s->udata.p = NULL;
3382 len = strlen ((*p->sym_ptr_ptr)->name);
3383 memcpy (names, (*p->sym_ptr_ptr)->name, len);
3384 names += len;
e054468f
AM
3385 if (p->addend != 0)
3386 {
3387 memcpy (names, "+0x", sizeof ("+0x") - 1);
3388 names += sizeof ("+0x") - 1;
3389 bfd_sprintf_vma (abfd, names, p->addend);
3390 names += strlen (names);
3391 }
468392fb
AM
3392 memcpy (names, "@plt", sizeof ("@plt"));
3393 names += sizeof ("@plt");
3394 s++;
3395 glink_vma += 8;
3396 if (i >= 0x8000)
3397 glink_vma += 4;
3398 }
3399 count += plt_count;
3400 }
90e3cdf2
JJ
3401 }
3402
c9727e01 3403 done:
a7535cf3 3404 free (syms);
90e3cdf2
JJ
3405 return count;
3406}
5bd4f169 3407\f
65f38f15
AM
3408/* The following functions are specific to the ELF linker, while
3409 functions above are used generally. Those named ppc64_elf_* are
3410 called by the main ELF linker code. They appear in this file more
3411 or less in the order in which they are called. eg.
3412 ppc64_elf_check_relocs is called early in the link process,
3413 ppc64_elf_finish_dynamic_sections is one of the last functions
e86ce104
AM
3414 called.
3415
3416 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
3417 functions have both a function code symbol and a function descriptor
3418 symbol. A call to foo in a relocatable object file looks like:
3419
3420 . .text
3421 . x:
3422 . bl .foo
3423 . nop
3424
3425 The function definition in another object file might be:
3426
3427 . .section .opd
3428 . foo: .quad .foo
3429 . .quad .TOC.@tocbase
3430 . .quad 0
3431 .
3432 . .text
3433 . .foo: blr
3434
3435 When the linker resolves the call during a static link, the branch
3436 unsurprisingly just goes to .foo and the .opd information is unused.
3437 If the function definition is in a shared library, things are a little
3438 different: The call goes via a plt call stub, the opd information gets
3439 copied to the plt, and the linker patches the nop.
3440
3441 . x:
3442 . bl .foo_stub
3443 . ld 2,40(1)
3444 .
3445 .
3446 . .foo_stub:
3447 . addis 12,2,Lfoo@toc@ha # in practice, the call stub
411e1bfb 3448 . addi 12,12,Lfoo@toc@l # is slightly optimized, but
e86ce104
AM
3449 . std 2,40(1) # this is the general idea
3450 . ld 11,0(12)
3451 . ld 2,8(12)
3452 . mtctr 11
3453 . ld 11,16(12)
3454 . bctr
3455 .
3456 . .section .plt
3457 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
3458
3459 The "reloc ()" notation is supposed to indicate that the linker emits
3460 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
3461 copying.
3462
3463 What are the difficulties here? Well, firstly, the relocations
3464 examined by the linker in check_relocs are against the function code
3465 sym .foo, while the dynamic relocation in the plt is emitted against
3466 the function descriptor symbol, foo. Somewhere along the line, we need
3467 to carefully copy dynamic link information from one symbol to the other.
3468 Secondly, the generic part of the elf linker will make .foo a dynamic
3469 symbol as is normal for most other backends. We need foo dynamic
3470 instead, at least for an application final link. However, when
3471 creating a shared library containing foo, we need to have both symbols
3472 dynamic so that references to .foo are satisfied during the early
3473 stages of linking. Otherwise the linker might decide to pull in a
8387904d
AM
3474 definition from some other object, eg. a static library.
3475
3476 Update: As of August 2004, we support a new convention. Function
3477 calls may use the function descriptor symbol, ie. "bl foo". This
3478 behaves exactly as "bl .foo". */
65f38f15 3479
1d483afe 3480/* Of those relocs that might be copied as dynamic relocs, this function
58ac9f71
AM
3481 selects those that must be copied when linking a shared library,
3482 even when the symbol is local. */
65f38f15 3483
1d483afe
AM
3484static int
3485must_be_dyn_reloc (struct bfd_link_info *info,
3486 enum elf_ppc64_reloc_type r_type)
3487{
3488 switch (r_type)
3489 {
3490 default:
3491 return 1;
3492
3493 case R_PPC64_REL32:
3494 case R_PPC64_REL64:
3495 case R_PPC64_REL30:
3496 return 0;
3497
3498 case R_PPC64_TPREL16:
3499 case R_PPC64_TPREL16_LO:
3500 case R_PPC64_TPREL16_HI:
3501 case R_PPC64_TPREL16_HA:
3502 case R_PPC64_TPREL16_DS:
3503 case R_PPC64_TPREL16_LO_DS:
3504 case R_PPC64_TPREL16_HIGHER:
3505 case R_PPC64_TPREL16_HIGHERA:
3506 case R_PPC64_TPREL16_HIGHEST:
3507 case R_PPC64_TPREL16_HIGHESTA:
3508 case R_PPC64_TPREL64:
3509 return !info->executable;
3510 }
3511}
65f38f15 3512
f4656909
AM
3513/* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
3514 copying dynamic variables from a shared lib into an app's dynbss
3515 section, and instead use a dynamic relocation to point into the
5d35169e
AM
3516 shared lib. With code that gcc generates, it's vital that this be
3517 enabled; In the PowerPC64 ABI, the address of a function is actually
3518 the address of a function descriptor, which resides in the .opd
3519 section. gcc uses the descriptor directly rather than going via the
3520 GOT as some other ABI's do, which means that initialized function
3521 pointers must reference the descriptor. Thus, a function pointer
3522 initialized to the address of a function in a shared library will
3523 either require a copy reloc, or a dynamic reloc. Using a copy reloc
4cc11e76 3524 redefines the function descriptor symbol to point to the copy. This
5d35169e
AM
3525 presents a problem as a plt entry for that function is also
3526 initialized from the function descriptor symbol and the copy reloc
3527 may not be initialized first. */
a23b6845 3528#define ELIMINATE_COPY_RELOCS 1
f4656909 3529
721956f4 3530/* Section name for stubs is the associated section name plus this
29942be8
NC
3531 string. */
3532#define STUB_SUFFIX ".stub"
721956f4
AM
3533
3534/* Linker stubs.
3535 ppc_stub_long_branch:
3536 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
3537 destination, but a 24 bit branch in a stub section will reach.
3538 . b dest
3539
3540 ppc_stub_plt_branch:
3541 Similar to the above, but a 24 bit branch in the stub section won't
3542 reach its destination.
87e226ce
AM
3543 . addis %r12,%r2,xxx@toc@ha
3544 . ld %r11,xxx@toc@l(%r12)
721956f4
AM
3545 . mtctr %r11
3546 . bctr
3547
3548 ppc_stub_plt_call:
2c66dc6c
AM
3549 Used to call a function in a shared library. If it so happens that
3550 the plt entry referenced crosses a 64k boundary, then an extra
ac2df442 3551 "addi %r12,%r12,xxx@toc@l" will be inserted before the "mtctr".
87e226ce 3552 . addis %r12,%r2,xxx@toc@ha
721956f4 3553 . std %r2,40(%r1)
87e226ce 3554 . ld %r11,xxx+0@toc@l(%r12)
721956f4 3555 . mtctr %r11
ac2df442 3556 . ld %r2,xxx+8@toc@l(%r12)
87e226ce 3557 . ld %r11,xxx+16@toc@l(%r12)
721956f4 3558 . bctr
ad8e1ba5
AM
3559
3560 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
3561 code to adjust the value and save r2 to support multiple toc sections.
3562 A ppc_stub_long_branch with an r2 offset looks like:
3563 . std %r2,40(%r1)
3564 . addis %r2,%r2,off@ha
3565 . addi %r2,%r2,off@l
3566 . b dest
3567
3568 A ppc_stub_plt_branch with an r2 offset looks like:
3569 . std %r2,40(%r1)
3570 . addis %r12,%r2,xxx@toc@ha
3571 . ld %r11,xxx@toc@l(%r12)
3572 . addis %r2,%r2,off@ha
3573 . addi %r2,%r2,off@l
3574 . mtctr %r11
3575 . bctr
ac2df442
AM
3576
3577 In cases where the "addis" instruction would add zero, the "addis" is
3578 omitted and following instructions modified slightly in some cases.
721956f4
AM
3579*/
3580
3581enum ppc_stub_type {
3582 ppc_stub_none,
3583 ppc_stub_long_branch,
ad8e1ba5 3584 ppc_stub_long_branch_r2off,
721956f4 3585 ppc_stub_plt_branch,
ad8e1ba5 3586 ppc_stub_plt_branch_r2off,
721956f4
AM
3587 ppc_stub_plt_call
3588};
3589
3590struct ppc_stub_hash_entry {
3591
3592 /* Base hash table entry structure. */
3593 struct bfd_hash_entry root;
3594
ad8e1ba5
AM
3595 enum ppc_stub_type stub_type;
3596
721956f4
AM
3597 /* The stub section. */
3598 asection *stub_sec;
3599
3600 /* Offset within stub_sec of the beginning of this stub. */
3601 bfd_vma stub_offset;
3602
3603 /* Given the symbol's value and its section we can determine its final
3604 value when building the stubs (so the stub knows where to jump. */
3605 bfd_vma target_value;
3606 asection *target_section;
3607
721956f4
AM
3608 /* The symbol table entry, if any, that this was derived from. */
3609 struct ppc_link_hash_entry *h;
e054468f 3610 struct plt_entry *plt_ent;
721956f4 3611
411e1bfb
AM
3612 /* And the reloc addend that this was derived from. */
3613 bfd_vma addend;
3614
721956f4
AM
3615 /* Where this stub is being called from, or, in the case of combined
3616 stub sections, the first input section in the group. */
3617 asection *id_sec;
3618};
3619
3620struct ppc_branch_hash_entry {
3621
3622 /* Base hash table entry structure. */
3623 struct bfd_hash_entry root;
3624
c456f082 3625 /* Offset within branch lookup table. */
721956f4
AM
3626 unsigned int offset;
3627
3628 /* Generation marker. */
3629 unsigned int iter;
3630};
65f38f15
AM
3631
3632struct ppc_link_hash_entry
3633{
3634 struct elf_link_hash_entry elf;
3635
b3fac117
AM
3636 union {
3637 /* A pointer to the most recently used stub hash entry against this
3638 symbol. */
3639 struct ppc_stub_hash_entry *stub_cache;
3640
3641 /* A pointer to the next symbol starting with a '.' */
3642 struct ppc_link_hash_entry *next_dot_sym;
3643 } u;
721956f4 3644
65f38f15 3645 /* Track dynamic relocs copied for this symbol. */
6061a67d 3646 struct elf_dyn_relocs *dyn_relocs;
e86ce104 3647
721956f4 3648 /* Link between function code and descriptor symbols. */
34814b9f 3649 struct ppc_link_hash_entry *oh;
721956f4 3650
e86ce104
AM
3651 /* Flag function code and descriptor symbols. */
3652 unsigned int is_func:1;
3653 unsigned int is_func_descriptor:1;
908b32fc 3654 unsigned int fake:1;
411e1bfb 3655
c5614fa4
AM
3656 /* Whether global opd/toc sym has been adjusted or not.
3657 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3658 should be set for all globals defined in any opd/toc section. */
754021d0
AM
3659 unsigned int adjust_done:1;
3660
99877b66
AM
3661 /* Set if we twiddled this symbol to weak at some stage. */
3662 unsigned int was_undefined:1;
3663
411e1bfb 3664 /* Contexts in which symbol is used in the GOT (or TOC).
e7b938ca
AM
3665 TLS_GD .. TLS_EXPLICIT bits are or'd into the mask as the
3666 corresponding relocs are encountered during check_relocs.
3667 tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
3668 indicate the corresponding GOT entry type is not needed.
3669 tls_optimize may also set TLS_TPRELGD when a GD reloc turns into
3670 a TPREL one. We use a separate flag rather than setting TPREL
3671 just for convenience in distinguishing the two cases. */
3672#define TLS_GD 1 /* GD reloc. */
3673#define TLS_LD 2 /* LD reloc. */
3674#define TLS_TPREL 4 /* TPREL reloc, => IE. */
3675#define TLS_DTPREL 8 /* DTPREL reloc, => LD. */
3676#define TLS_TLS 16 /* Any TLS reloc. */
3677#define TLS_EXPLICIT 32 /* Marks TOC section TLS relocs. */
3678#define TLS_TPRELGD 64 /* TPREL reloc resulting from GD->IE. */
e054468f 3679#define PLT_IFUNC 128 /* STT_GNU_IFUNC. */
f961d9dd 3680 unsigned char tls_mask;
65f38f15
AM
3681};
3682
3683/* ppc64 ELF linker hash table. */
3684
3685struct ppc_link_hash_table
3686{
3687 struct elf_link_hash_table elf;
3688
721956f4
AM
3689 /* The stub hash table. */
3690 struct bfd_hash_table stub_hash_table;
3691
3692 /* Another hash table for plt_branch stubs. */
3693 struct bfd_hash_table branch_hash_table;
3694
3b421ab3
AM
3695 /* Hash table for function prologue tocsave. */
3696 htab_t tocsave_htab;
3697
721956f4
AM
3698 /* Linker stub bfd. */
3699 bfd *stub_bfd;
3700
3701 /* Linker call-backs. */
4ce794b7
AM
3702 asection * (*add_stub_section) (const char *, asection *);
3703 void (*layout_sections_again) (void);
721956f4
AM
3704
3705 /* Array to keep track of which stub sections have been created, and
3706 information on stub grouping. */
3707 struct map_stub {
3708 /* This is the section to which stubs in the group will be attached. */
3709 asection *link_sec;
3710 /* The stub section. */
3711 asection *stub_sec;
ad8e1ba5
AM
3712 /* Along with elf_gp, specifies the TOC pointer used in this group. */
3713 bfd_vma toc_off;
721956f4
AM
3714 } *stub_group;
3715
ad8e1ba5
AM
3716 /* Temp used when calculating TOC pointers. */
3717 bfd_vma toc_curr;
bf102f86
AM
3718 bfd *toc_bfd;
3719 asection *toc_first_sec;
ad8e1ba5 3720
8f3bab57
AM
3721 /* Highest input section id. */
3722 int top_id;
3723
734b6cf9
AM
3724 /* Highest output section index. */
3725 int top_index;
3726
b3fac117
AM
3727 /* Used when adding symbols. */
3728 struct ppc_link_hash_entry *dot_syms;
3729
734b6cf9
AM
3730 /* List of input sections for each output section. */
3731 asection **input_list;
721956f4 3732
65f38f15 3733 /* Short-cuts to get to dynamic linker sections. */
4ce794b7 3734 asection *got;
4ce794b7
AM
3735 asection *plt;
3736 asection *relplt;
e054468f
AM
3737 asection *iplt;
3738 asection *reliplt;
4ce794b7
AM
3739 asection *dynbss;
3740 asection *relbss;
3741 asection *glink;
82bd7b59 3742 asection *sfpr;
4ce794b7
AM
3743 asection *brlt;
3744 asection *relbrlt;
58d180e8 3745 asection *glink_eh_frame;
ec338859 3746
8387904d
AM
3747 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
3748 struct ppc_link_hash_entry *tls_get_addr;
3749 struct ppc_link_hash_entry *tls_get_addr_fd;
411e1bfb 3750
927be08e
AM
3751 /* The size of reliplt used by got entry relocs. */
3752 bfd_size_type got_reli_size;
3753
9b5ecbd0
AM
3754 /* Statistics. */
3755 unsigned long stub_count[ppc_stub_plt_call];
3756
ee75fd95
AM
3757 /* Number of stubs against global syms. */
3758 unsigned long stub_globals;
3759
9df0ef5f
AM
3760 /* Set if PLT call stubs should load r11. */
3761 unsigned int plt_static_chain:1;
3762
ad8e1ba5 3763 /* Set if we should emit symbols for stubs. */
99877b66 3764 unsigned int emit_stub_syms:1;
ad8e1ba5 3765
a7f2871e
AM
3766 /* Set if __tls_get_addr optimization should not be done. */
3767 unsigned int no_tls_get_addr_opt:1;
3768
4c52953f 3769 /* Support for multiple toc sections. */
33c0ec9d 3770 unsigned int do_multi_toc:1;
4c52953f 3771 unsigned int multi_toc_needed:1;
927be08e 3772 unsigned int second_toc_pass:1;
67f0cbdb 3773 unsigned int do_toc_opt:1;
4c52953f 3774
5d1634d7 3775 /* Set on error. */
99877b66 3776 unsigned int stub_error:1;
721956f4 3777
7d9616d7 3778 /* Temp used by ppc64_elf_process_dot_syms. */
99877b66 3779 unsigned int twiddled_syms:1;
721956f4
AM
3780
3781 /* Incremented every time we size stubs. */
3782 unsigned int stub_iteration;
5d1634d7 3783
87d72d41
AM
3784 /* Small local sym cache. */
3785 struct sym_cache sym_cache;
65f38f15
AM
3786};
3787
4c52953f
AM
3788/* Rename some of the generic section flags to better document how they
3789 are used here. */
b0dddeec
AM
3790
3791/* Nonzero if this section has TLS related relocations. */
3792#define has_tls_reloc sec_flg0
3793
3794/* Nonzero if this section has a call to __tls_get_addr. */
3795#define has_tls_get_addr_call sec_flg1
3796
3797/* Nonzero if this section has any toc or got relocs. */
3798#define has_toc_reloc sec_flg2
3799
3800/* Nonzero if this section has a call to another section that uses
3801 the toc or got. */
d77c8a4b 3802#define makes_toc_func_call sec_flg3
b0dddeec
AM
3803
3804/* Recursion protection when determining above flag. */
d77c8a4b 3805#define call_check_in_progress sec_flg4
70cc837d 3806#define call_check_done sec_flg5
4c52953f 3807
65f38f15
AM
3808/* Get the ppc64 ELF linker hash table from a link_info structure. */
3809
3810#define ppc_hash_table(p) \
4dfe6ac6
NC
3811 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
3812 == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
65f38f15 3813
721956f4
AM
3814#define ppc_stub_hash_lookup(table, string, create, copy) \
3815 ((struct ppc_stub_hash_entry *) \
3816 bfd_hash_lookup ((table), (string), (create), (copy)))
3817
3818#define ppc_branch_hash_lookup(table, string, create, copy) \
3819 ((struct ppc_branch_hash_entry *) \
3820 bfd_hash_lookup ((table), (string), (create), (copy)))
3821
3822/* Create an entry in the stub hash table. */
3823
3824static struct bfd_hash_entry *
4ce794b7
AM
3825stub_hash_newfunc (struct bfd_hash_entry *entry,
3826 struct bfd_hash_table *table,
3827 const char *string)
721956f4
AM
3828{
3829 /* Allocate the structure if it has not already been allocated by a
3830 subclass. */
3831 if (entry == NULL)
3832 {
3833 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
3834 if (entry == NULL)
3835 return entry;
3836 }
3837
3838 /* Call the allocation method of the superclass. */
3839 entry = bfd_hash_newfunc (entry, table, string);
3840 if (entry != NULL)
3841 {
3842 struct ppc_stub_hash_entry *eh;
3843
3844 /* Initialize the local fields. */
3845 eh = (struct ppc_stub_hash_entry *) entry;
ad8e1ba5 3846 eh->stub_type = ppc_stub_none;
721956f4
AM
3847 eh->stub_sec = NULL;
3848 eh->stub_offset = 0;
3849 eh->target_value = 0;
3850 eh->target_section = NULL;
721956f4
AM
3851 eh->h = NULL;
3852 eh->id_sec = NULL;
3853 }
3854
3855 return entry;
3856}
3857
3858/* Create an entry in the branch hash table. */
3859
3860static struct bfd_hash_entry *
4ce794b7
AM
3861branch_hash_newfunc (struct bfd_hash_entry *entry,
3862 struct bfd_hash_table *table,
3863 const char *string)
721956f4
AM
3864{
3865 /* Allocate the structure if it has not already been allocated by a
3866 subclass. */
3867 if (entry == NULL)
3868 {
3869 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
3870 if (entry == NULL)
3871 return entry;
3872 }
3873
3874 /* Call the allocation method of the superclass. */
3875 entry = bfd_hash_newfunc (entry, table, string);
3876 if (entry != NULL)
3877 {
3878 struct ppc_branch_hash_entry *eh;
3879
3880 /* Initialize the local fields. */
3881 eh = (struct ppc_branch_hash_entry *) entry;
3882 eh->offset = 0;
3883 eh->iter = 0;
3884 }
3885
3886 return entry;
3887}
3888
65f38f15
AM
3889/* Create an entry in a ppc64 ELF linker hash table. */
3890
3891static struct bfd_hash_entry *
4ce794b7
AM
3892link_hash_newfunc (struct bfd_hash_entry *entry,
3893 struct bfd_hash_table *table,
3894 const char *string)
65f38f15
AM
3895{
3896 /* Allocate the structure if it has not already been allocated by a
3897 subclass. */
3898 if (entry == NULL)
3899 {
3900 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
3901 if (entry == NULL)
3902 return entry;
3903 }
3904
3905 /* Call the allocation method of the superclass. */
3906 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
3907 if (entry != NULL)
3908 {
3909 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
3910
b3fac117 3911 memset (&eh->u.stub_cache, 0,
908b32fc 3912 (sizeof (struct ppc_link_hash_entry)
b3fac117
AM
3913 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
3914
3915 /* When making function calls, old ABI code references function entry
3916 points (dot symbols), while new ABI code references the function
3917 descriptor symbol. We need to make any combination of reference and
3918 definition work together, without breaking archive linking.
3919
3920 For a defined function "foo" and an undefined call to "bar":
3921 An old object defines "foo" and ".foo", references ".bar" (possibly
3922 "bar" too).
3923 A new object defines "foo" and references "bar".
3924
3925 A new object thus has no problem with its undefined symbols being
3926 satisfied by definitions in an old object. On the other hand, the
3927 old object won't have ".bar" satisfied by a new object.
3928
3929 Keep a list of newly added dot-symbols. */
3930
3931 if (string[0] == '.')
3932 {
3933 struct ppc_link_hash_table *htab;
3934
3935 htab = (struct ppc_link_hash_table *) table;
3936 eh->u.next_dot_sym = htab->dot_syms;
3937 htab->dot_syms = eh;
3938 }
65f38f15
AM
3939 }
3940
3941 return entry;
3942}
3943
3b421ab3
AM
3944struct tocsave_entry {
3945 asection *sec;
3946 bfd_vma offset;
3947};
3948
3949static hashval_t
3950tocsave_htab_hash (const void *p)
3951{
3952 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
3953 return ((bfd_vma)(intptr_t) e->sec ^ e->offset) >> 3;
3954}
3955
3956static int
3957tocsave_htab_eq (const void *p1, const void *p2)
3958{
3959 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
3960 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
3961 return e1->sec == e2->sec && e1->offset == e2->offset;
3962}
3963
65f38f15
AM
3964/* Create a ppc64 ELF linker hash table. */
3965
3966static struct bfd_link_hash_table *
4ce794b7 3967ppc64_elf_link_hash_table_create (bfd *abfd)
65f38f15
AM
3968{
3969 struct ppc_link_hash_table *htab;
3970 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
3971
4ce794b7 3972 htab = bfd_zmalloc (amt);
65f38f15
AM
3973 if (htab == NULL)
3974 return NULL;
3975
66eb6687 3976 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
4dfe6ac6
NC
3977 sizeof (struct ppc_link_hash_entry),
3978 PPC64_ELF_DATA))
65f38f15 3979 {
e2d34d7d 3980 free (htab);
65f38f15
AM
3981 return NULL;
3982 }
3983
721956f4 3984 /* Init the stub hash table too. */
66eb6687
AM
3985 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
3986 sizeof (struct ppc_stub_hash_entry)))
721956f4
AM
3987 return NULL;
3988
3989 /* And the branch hash table. */
66eb6687
AM
3990 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
3991 sizeof (struct ppc_branch_hash_entry)))
721956f4
AM
3992 return NULL;
3993
3b421ab3
AM
3994 htab->tocsave_htab = htab_try_create (1024,
3995 tocsave_htab_hash,
3996 tocsave_htab_eq,
3997 NULL);
3998 if (htab->tocsave_htab == NULL)
3999 return NULL;
4000
3254fd24
AM
4001 /* Initializing two fields of the union is just cosmetic. We really
4002 only care about glist, but when compiled on a 32-bit host the
4003 bfd_vma fields are larger. Setting the bfd_vma to zero makes
4004 debugger inspection of these fields look nicer. */
a6aa5195
AM
4005 htab->elf.init_got_refcount.refcount = 0;
4006 htab->elf.init_got_refcount.glist = NULL;
4007 htab->elf.init_plt_refcount.refcount = 0;
4008 htab->elf.init_plt_refcount.glist = NULL;
4009 htab->elf.init_got_offset.offset = 0;
4010 htab->elf.init_got_offset.glist = NULL;
4011 htab->elf.init_plt_offset.offset = 0;
4012 htab->elf.init_plt_offset.glist = NULL;
3254fd24 4013
65f38f15
AM
4014 return &htab->elf.root;
4015}
4016
721956f4
AM
4017/* Free the derived linker hash table. */
4018
4019static void
4ce794b7 4020ppc64_elf_link_hash_table_free (struct bfd_link_hash_table *hash)
721956f4 4021{
3b421ab3 4022 struct ppc_link_hash_table *htab = (struct ppc_link_hash_table *) hash;
721956f4 4023
3b421ab3
AM
4024 bfd_hash_table_free (&htab->stub_hash_table);
4025 bfd_hash_table_free (&htab->branch_hash_table);
4026 if (htab->tocsave_htab)
4027 htab_delete (htab->tocsave_htab);
721956f4
AM
4028 _bfd_generic_link_hash_table_free (hash);
4029}
4030
e717da7e
AM
4031/* Satisfy the ELF linker by filling in some fields in our fake bfd. */
4032
4033void
4034ppc64_elf_init_stub_bfd (bfd *abfd, struct bfd_link_info *info)
4035{
4036 struct ppc_link_hash_table *htab;
4037
4038 elf_elfheader (abfd)->e_ident[EI_CLASS] = ELFCLASS64;
4039
4040/* Always hook our dynamic sections into the first bfd, which is the
4041 linker created stub bfd. This ensures that the GOT header is at
4042 the start of the output TOC section. */
4043 htab = ppc_hash_table (info);
4dfe6ac6
NC
4044 if (htab == NULL)
4045 return;
e717da7e
AM
4046 htab->stub_bfd = abfd;
4047 htab->elf.dynobj = abfd;
4048}
4049
721956f4
AM
4050/* Build a name for an entry in the stub hash table. */
4051
4052static char *
4ce794b7
AM
4053ppc_stub_name (const asection *input_section,
4054 const asection *sym_sec,
4055 const struct ppc_link_hash_entry *h,
4056 const Elf_Internal_Rela *rel)
721956f4
AM
4057{
4058 char *stub_name;
4059 bfd_size_type len;
4060
4061 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
4062 offsets from a sym as a branch target? In fact, we could
4063 probably assume the addend is always zero. */
4064 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
4065
4066 if (h)
4067 {
4068 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
4069 stub_name = bfd_malloc (len);
46de2a7c
AM
4070 if (stub_name == NULL)
4071 return stub_name;
4072
4073 sprintf (stub_name, "%08x.%s+%x",
4074 input_section->id & 0xffffffff,
4075 h->elf.root.root.string,
4076 (int) rel->r_addend & 0xffffffff);
721956f4
AM
4077 }
4078 else
4079 {
ad8e1ba5 4080 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
721956f4 4081 stub_name = bfd_malloc (len);
46de2a7c
AM
4082 if (stub_name == NULL)
4083 return stub_name;
4084
4085 sprintf (stub_name, "%08x.%x:%x+%x",
4086 input_section->id & 0xffffffff,
4087 sym_sec->id & 0xffffffff,
4088 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
4089 (int) rel->r_addend & 0xffffffff);
721956f4 4090 }
ee75fd95
AM
4091 if (stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
4092 stub_name[len - 2] = 0;
721956f4
AM
4093 return stub_name;
4094}
4095
4096/* Look up an entry in the stub hash. Stub entries are cached because
4097 creating the stub name takes a bit of time. */
4098
4099static struct ppc_stub_hash_entry *
4ce794b7
AM
4100ppc_get_stub_entry (const asection *input_section,
4101 const asection *sym_sec,
039b3fef 4102 struct ppc_link_hash_entry *h,
4ce794b7
AM
4103 const Elf_Internal_Rela *rel,
4104 struct ppc_link_hash_table *htab)
721956f4
AM
4105{
4106 struct ppc_stub_hash_entry *stub_entry;
721956f4
AM
4107 const asection *id_sec;
4108
4109 /* If this input section is part of a group of sections sharing one
4110 stub section, then use the id of the first section in the group.
4111 Stub names need to include a section id, as there may well be
4112 more than one stub used to reach say, printf, and we need to
4113 distinguish between them. */
4114 id_sec = htab->stub_group[input_section->id].link_sec;
4115
b3fac117
AM
4116 if (h != NULL && h->u.stub_cache != NULL
4117 && h->u.stub_cache->h == h
4118 && h->u.stub_cache->id_sec == id_sec)
721956f4 4119 {
b3fac117 4120 stub_entry = h->u.stub_cache;
721956f4
AM
4121 }
4122 else
4123 {
4124 char *stub_name;
4125
4126 stub_name = ppc_stub_name (id_sec, sym_sec, h, rel);
4127 if (stub_name == NULL)
4128 return NULL;
4129
4130 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 4131 stub_name, FALSE, FALSE);
721956f4 4132 if (h != NULL)
b3fac117 4133 h->u.stub_cache = stub_entry;
721956f4
AM
4134
4135 free (stub_name);
4136 }
4137
4138 return stub_entry;
4139}
4140
4141/* Add a new stub entry to the stub hash. Not all fields of the new
4142 stub entry are initialised. */
4143
4144static struct ppc_stub_hash_entry *
4ce794b7
AM
4145ppc_add_stub (const char *stub_name,
4146 asection *section,
25f53a85 4147 struct bfd_link_info *info)
721956f4 4148{
25f53a85 4149 struct ppc_link_hash_table *htab = ppc_hash_table (info);
721956f4
AM
4150 asection *link_sec;
4151 asection *stub_sec;
4152 struct ppc_stub_hash_entry *stub_entry;
4153
4154 link_sec = htab->stub_group[section->id].link_sec;
4155 stub_sec = htab->stub_group[section->id].stub_sec;
4156 if (stub_sec == NULL)
4157 {
4158 stub_sec = htab->stub_group[link_sec->id].stub_sec;
4159 if (stub_sec == NULL)
4160 {
d4c88bbb 4161 size_t namelen;
721956f4
AM
4162 bfd_size_type len;
4163 char *s_name;
4164
d4c88bbb
AM
4165 namelen = strlen (link_sec->name);
4166 len = namelen + sizeof (STUB_SUFFIX);
721956f4
AM
4167 s_name = bfd_alloc (htab->stub_bfd, len);
4168 if (s_name == NULL)
4169 return NULL;
4170
d4c88bbb
AM
4171 memcpy (s_name, link_sec->name, namelen);
4172 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
721956f4
AM
4173 stub_sec = (*htab->add_stub_section) (s_name, link_sec);
4174 if (stub_sec == NULL)
4175 return NULL;
4176 htab->stub_group[link_sec->id].stub_sec = stub_sec;
4177 }
4178 htab->stub_group[section->id].stub_sec = stub_sec;
4179 }
4180
4181 /* Enter this entry into the linker stub hash table. */
4182 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
b34976b6 4183 TRUE, FALSE);
721956f4
AM
4184 if (stub_entry == NULL)
4185 {
8de848d8 4186 info->callbacks->einfo (_("%P: %B: cannot create stub entry %s\n"),
25f53a85 4187 section->owner, stub_name);
721956f4
AM
4188 return NULL;
4189 }
4190
4191 stub_entry->stub_sec = stub_sec;
4192 stub_entry->stub_offset = 0;
4193 stub_entry->id_sec = link_sec;
4194 return stub_entry;
4195}
4196
82bd7b59
AM
4197/* Create sections for linker generated code. */
4198
b34976b6 4199static bfd_boolean
4ce794b7 4200create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
82bd7b59
AM
4201{
4202 struct ppc_link_hash_table *htab;
4203 flagword flags;
4204
4205 htab = ppc_hash_table (info);
4dfe6ac6
NC
4206 if (htab == NULL)
4207 return FALSE;
82bd7b59
AM
4208
4209 /* Create .sfpr for code to save and restore fp regs. */
4210 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
4211 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3496cb2a
L
4212 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
4213 flags);
82bd7b59 4214 if (htab->sfpr == NULL
82bd7b59 4215 || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
b34976b6 4216 return FALSE;
82bd7b59 4217
721956f4 4218 /* Create .glink for lazy dynamic linking support. */
3496cb2a
L
4219 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4220 flags);
4ce794b7 4221 if (htab->glink == NULL
ee4bf8d2 4222 || ! bfd_set_section_alignment (dynobj, htab->glink, 3))
b34976b6 4223 return FALSE;
82bd7b59 4224
58d180e8
AM
4225 if (!info->no_ld_generated_unwind_info)
4226 {
4227 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
4228 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4229 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
4230 ".eh_frame",
4231 flags);
4232 if (htab->glink_eh_frame == NULL
4233 || !bfd_set_section_alignment (abfd, htab->glink_eh_frame, 2))
4234 return FALSE;
4235 }
4236
e054468f
AM
4237 flags = SEC_ALLOC | SEC_LINKER_CREATED;
4238 htab->iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
4239 if (htab->iplt == NULL
4240 || ! bfd_set_section_alignment (dynobj, htab->iplt, 3))
4241 return FALSE;
4242
4243 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4244 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4245 htab->reliplt = bfd_make_section_anyway_with_flags (dynobj,
4246 ".rela.iplt",
4247 flags);
4248 if (htab->reliplt == NULL
4249 || ! bfd_set_section_alignment (dynobj, htab->reliplt, 3))
4250 return FALSE;
4251
c456f082 4252 /* Create branch lookup table for plt_branch stubs. */
e4e0193e
AM
4253 flags = (SEC_ALLOC | SEC_LOAD
4254 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4255 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4256 flags);
4ce794b7 4257 if (htab->brlt == NULL
4ce794b7 4258 || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
b34976b6 4259 return FALSE;
721956f4 4260
e4e0193e 4261 if (!info->shared)
c456f082
AM
4262 return TRUE;
4263
e4e0193e
AM
4264 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4265 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4266 htab->relbrlt = bfd_make_section_anyway_with_flags (dynobj,
4267 ".rela.branch_lt",
4268 flags);
e054468f 4269 if (htab->relbrlt == NULL
c456f082
AM
4270 || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
4271 return FALSE;
4272
b34976b6 4273 return TRUE;
82bd7b59
AM
4274}
4275
e717da7e
AM
4276/* Create .got and .rela.got sections in ABFD, and .got in dynobj if
4277 not already done. */
65f38f15 4278
b34976b6 4279static bfd_boolean
e717da7e 4280create_got_section (bfd *abfd, struct bfd_link_info *info)
65f38f15 4281{
e717da7e
AM
4282 asection *got, *relgot;
4283 flagword flags;
4284 struct ppc_link_hash_table *htab = ppc_hash_table (info);
65f38f15 4285
0c8d6e5c 4286 if (!is_ppc64_elf (abfd))
0ffa91dd 4287 return FALSE;
4dfe6ac6
NC
4288 if (htab == NULL)
4289 return FALSE;
0ffa91dd 4290
4ce794b7 4291 if (!htab->got)
e717da7e
AM
4292 {
4293 if (! _bfd_elf_create_got_section (htab->elf.dynobj, info))
4294 return FALSE;
4295
4296 htab->got = bfd_get_section_by_name (htab->elf.dynobj, ".got");
4297 if (!htab->got)
4298 abort ();
4299 }
4300
4301 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4302 | SEC_LINKER_CREATED);
4303
c456f082 4304 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
e717da7e 4305 if (!got
e717da7e
AM
4306 || !bfd_set_section_alignment (abfd, got, 3))
4307 return FALSE;
65f38f15 4308
c456f082
AM
4309 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
4310 flags | SEC_READONLY);
e717da7e 4311 if (!relgot
e717da7e 4312 || ! bfd_set_section_alignment (abfd, relgot, 3))
b34976b6 4313 return FALSE;
e717da7e
AM
4314
4315 ppc64_elf_tdata (abfd)->got = got;
4316 ppc64_elf_tdata (abfd)->relgot = relgot;
b34976b6 4317 return TRUE;
65f38f15 4318}
5bd4f169 4319
82bd7b59 4320/* Create the dynamic sections, and set up shortcuts. */
5bd4f169 4321
b34976b6 4322static bfd_boolean
4ce794b7 4323ppc64_elf_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
5bd4f169 4324{
65f38f15 4325 struct ppc_link_hash_table *htab;
5bd4f169 4326
65f38f15 4327 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
b34976b6 4328 return FALSE;
65f38f15 4329
e717da7e 4330 htab = ppc_hash_table (info);
4dfe6ac6
NC
4331 if (htab == NULL)
4332 return FALSE;
4333
e717da7e
AM
4334 if (!htab->got)
4335 htab->got = bfd_get_section_by_name (dynobj, ".got");
4ce794b7
AM
4336 htab->plt = bfd_get_section_by_name (dynobj, ".plt");
4337 htab->relplt = bfd_get_section_by_name (dynobj, ".rela.plt");
4338 htab->dynbss = bfd_get_section_by_name (dynobj, ".dynbss");
65f38f15 4339 if (!info->shared)
4ce794b7 4340 htab->relbss = bfd_get_section_by_name (dynobj, ".rela.bss");
65f38f15 4341
e717da7e 4342 if (!htab->got || !htab->plt || !htab->relplt || !htab->dynbss
4ce794b7 4343 || (!info->shared && !htab->relbss))
65f38f15
AM
4344 abort ();
4345
b34976b6 4346 return TRUE;
5bd4f169
AM
4347}
4348
b31867b6
AM
4349/* Follow indirect and warning symbol links. */
4350
4351static inline struct bfd_link_hash_entry *
4352follow_link (struct bfd_link_hash_entry *h)
4353{
4354 while (h->type == bfd_link_hash_indirect
4355 || h->type == bfd_link_hash_warning)
4356 h = h->u.i.link;
4357 return h;
4358}
4359
4360static inline struct elf_link_hash_entry *
4361elf_follow_link (struct elf_link_hash_entry *h)
4362{
4363 return (struct elf_link_hash_entry *) follow_link (&h->root);
4364}
4365
4366static inline struct ppc_link_hash_entry *
4367ppc_follow_link (struct ppc_link_hash_entry *h)
4368{
4369 return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
4370}
4371
40d16e0b
AM
4372/* Merge PLT info on FROM with that on TO. */
4373
4374static void
4375move_plt_plist (struct ppc_link_hash_entry *from,
4376 struct ppc_link_hash_entry *to)
4377{
4378 if (from->elf.plt.plist != NULL)
4379 {
4380 if (to->elf.plt.plist != NULL)
4381 {
4382 struct plt_entry **entp;
4383 struct plt_entry *ent;
4384
4385 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4386 {
4387 struct plt_entry *dent;
4388
4389 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4390 if (dent->addend == ent->addend)
4391 {
4392 dent->plt.refcount += ent->plt.refcount;
4393 *entp = ent->next;
4394 break;
4395 }
4396 if (dent == NULL)
4397 entp = &ent->next;
4398 }
4399 *entp = to->elf.plt.plist;
4400 }
4401
4402 to->elf.plt.plist = from->elf.plt.plist;
4403 from->elf.plt.plist = NULL;
4404 }
4405}
4406
65f38f15
AM
4407/* Copy the extra info we tack onto an elf_link_hash_entry. */
4408
4409static void
fcfa13d2
AM
4410ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4411 struct elf_link_hash_entry *dir,
4412 struct elf_link_hash_entry *ind)
65f38f15
AM
4413{
4414 struct ppc_link_hash_entry *edir, *eind;
4415
4416 edir = (struct ppc_link_hash_entry *) dir;
4417 eind = (struct ppc_link_hash_entry *) ind;
4418
c79d6685
AM
4419 edir->is_func |= eind->is_func;
4420 edir->is_func_descriptor |= eind->is_func_descriptor;
4421 edir->tls_mask |= eind->tls_mask;
4422 if (eind->oh != NULL)
4423 edir->oh = ppc_follow_link (eind->oh);
4424
4425 /* If called to transfer flags for a weakdef during processing
4426 of elf_adjust_dynamic_symbol, don't copy NON_GOT_REF.
4427 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
4428 if (!(ELIMINATE_COPY_RELOCS
4429 && eind->elf.root.type != bfd_link_hash_indirect
4430 && edir->elf.dynamic_adjusted))
4431 edir->elf.non_got_ref |= eind->elf.non_got_ref;
4432
4433 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4434 edir->elf.ref_regular |= eind->elf.ref_regular;
4435 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4436 edir->elf.needs_plt |= eind->elf.needs_plt;
4437
4438 /* If we were called to copy over info for a weak sym, that's all. */
4439 if (eind->elf.root.type != bfd_link_hash_indirect)
4440 return;
4441
411e1bfb 4442 /* Copy over any dynamic relocs we may have on the indirect sym. */
bbd7ec4a 4443 if (eind->dyn_relocs != NULL)
65f38f15 4444 {
bbd7ec4a
AM
4445 if (edir->dyn_relocs != NULL)
4446 {
6061a67d
AM
4447 struct elf_dyn_relocs **pp;
4448 struct elf_dyn_relocs *p;
bbd7ec4a 4449
fcfa13d2 4450 /* Add reloc counts against the indirect sym to the direct sym
bbd7ec4a
AM
4451 list. Merge any entries against the same section. */
4452 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
4453 {
6061a67d 4454 struct elf_dyn_relocs *q;
bbd7ec4a
AM
4455
4456 for (q = edir->dyn_relocs; q != NULL; q = q->next)
4457 if (q->sec == p->sec)
4458 {
4459 q->pc_count += p->pc_count;
4460 q->count += p->count;
4461 *pp = p->next;
4462 break;
4463 }
4464 if (q == NULL)
4465 pp = &p->next;
4466 }
4467 *pp = edir->dyn_relocs;
4468 }
4469
65f38f15
AM
4470 edir->dyn_relocs = eind->dyn_relocs;
4471 eind->dyn_relocs = NULL;
4472 }
65f38f15 4473
81848ca0
AM
4474 /* Copy over got entries that we may have already seen to the
4475 symbol which just became indirect. */
411e1bfb
AM
4476 if (eind->elf.got.glist != NULL)
4477 {
4478 if (edir->elf.got.glist != NULL)
4479 {
4480 struct got_entry **entp;
4481 struct got_entry *ent;
4482
4483 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4484 {
4485 struct got_entry *dent;
4486
4487 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4488 if (dent->addend == ent->addend
e717da7e 4489 && dent->owner == ent->owner
411e1bfb
AM
4490 && dent->tls_type == ent->tls_type)
4491 {
4492 dent->got.refcount += ent->got.refcount;
4493 *entp = ent->next;
4494 break;
4495 }
4496 if (dent == NULL)
4497 entp = &ent->next;
4498 }
4499 *entp = edir->elf.got.glist;
4500 }
4501
4502 edir->elf.got.glist = eind->elf.got.glist;
4503 eind->elf.got.glist = NULL;
4504 }
4505
4506 /* And plt entries. */
40d16e0b 4507 move_plt_plist (eind, edir);
411e1bfb 4508
fcfa13d2 4509 if (eind->elf.dynindx != -1)
411e1bfb 4510 {
fcfa13d2
AM
4511 if (edir->elf.dynindx != -1)
4512 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4513 edir->elf.dynstr_index);
411e1bfb
AM
4514 edir->elf.dynindx = eind->elf.dynindx;
4515 edir->elf.dynstr_index = eind->elf.dynstr_index;
4516 eind->elf.dynindx = -1;
4517 eind->elf.dynstr_index = 0;
4518 }
411e1bfb
AM
4519}
4520
8387904d
AM
4521/* Find the function descriptor hash entry from the given function code
4522 hash entry FH. Link the entries via their OH fields. */
4523
4524static struct ppc_link_hash_entry *
b31867b6 4525lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
8387904d
AM
4526{
4527 struct ppc_link_hash_entry *fdh = fh->oh;
4528
4529 if (fdh == NULL)
4530 {
4531 const char *fd_name = fh->elf.root.root.string + 1;
4532
4533 fdh = (struct ppc_link_hash_entry *)
4534 elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
b31867b6
AM
4535 if (fdh == NULL)
4536 return fdh;
4537
4538 fdh->is_func_descriptor = 1;
4539 fdh->oh = fh;
4540 fh->is_func = 1;
4541 fh->oh = fdh;
8387904d
AM
4542 }
4543
b31867b6 4544 return ppc_follow_link (fdh);
8387904d
AM
4545}
4546
bb700d78
AM
4547/* Make a fake function descriptor sym for the code sym FH. */
4548
4549static struct ppc_link_hash_entry *
4550make_fdh (struct bfd_link_info *info,
908b32fc 4551 struct ppc_link_hash_entry *fh)
bb700d78
AM
4552{
4553 bfd *abfd;
4554 asymbol *newsym;
4555 struct bfd_link_hash_entry *bh;
4556 struct ppc_link_hash_entry *fdh;
4557
4558 abfd = fh->elf.root.u.undef.abfd;
4559 newsym = bfd_make_empty_symbol (abfd);
4560 newsym->name = fh->elf.root.root.string + 1;
4561 newsym->section = bfd_und_section_ptr;
4562 newsym->value = 0;
908b32fc 4563 newsym->flags = BSF_WEAK;
bb700d78
AM
4564
4565 bh = NULL;
4566 if (!_bfd_generic_link_add_one_symbol (info, abfd, newsym->name,
4567 newsym->flags, newsym->section,
4568 newsym->value, NULL, FALSE, FALSE,
4569 &bh))
4570 return NULL;
4571
4572 fdh = (struct ppc_link_hash_entry *) bh;
4573 fdh->elf.non_elf = 0;
908b32fc
AM
4574 fdh->fake = 1;
4575 fdh->is_func_descriptor = 1;
4576 fdh->oh = fh;
4577 fh->is_func = 1;
4578 fh->oh = fdh;
bb700d78
AM
4579 return fdh;
4580}
4581
8387904d
AM
4582/* Fix function descriptor symbols defined in .opd sections to be
4583 function type. */
555cd476
AM
4584
4585static bfd_boolean
c16153ae 4586ppc64_elf_add_symbol_hook (bfd *ibfd,
e054468f 4587 struct bfd_link_info *info,
555cd476 4588 Elf_Internal_Sym *isym,
b3fac117 4589 const char **name ATTRIBUTE_UNUSED,
555cd476
AM
4590 flagword *flags ATTRIBUTE_UNUSED,
4591 asection **sec,
4592 bfd_vma *value ATTRIBUTE_UNUSED)
4593{
f64b2e8d
NC
4594 if ((ibfd->flags & DYNAMIC) == 0
4595 && ELF_ST_BIND (isym->st_info) == STB_GNU_UNIQUE)
4596 elf_tdata (info->output_bfd)->has_gnu_symbols = TRUE;
4597
e054468f 4598 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
c16153ae
L
4599 {
4600 if ((ibfd->flags & DYNAMIC) == 0)
f64b2e8d 4601 elf_tdata (info->output_bfd)->has_gnu_symbols = TRUE;
c16153ae 4602 }
e054468f
AM
4603 else if (ELF_ST_TYPE (isym->st_info) == STT_FUNC)
4604 ;
4605 else if (*sec != NULL
70cc837d 4606 && strcmp ((*sec)->name, ".opd") == 0)
555cd476 4607 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
433817dd 4608
555cd476
AM
4609 return TRUE;
4610}
4611
8387904d 4612/* This function makes an old ABI object reference to ".bar" cause the
908b32fc
AM
4613 inclusion of a new ABI object archive that defines "bar".
4614 NAME is a symbol defined in an archive. Return a symbol in the hash
4615 table that might be satisfied by the archive symbols. */
8387904d
AM
4616
4617static struct elf_link_hash_entry *
4618ppc64_elf_archive_symbol_lookup (bfd *abfd,
4619 struct bfd_link_info *info,
4620 const char *name)
4621{
4622 struct elf_link_hash_entry *h;
4623 char *dot_name;
4624 size_t len;
4625
4626 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
908b32fc
AM
4627 if (h != NULL
4628 /* Don't return this sym if it is a fake function descriptor
4629 created by add_symbol_adjust. */
4630 && !(h->root.type == bfd_link_hash_undefweak
4631 && ((struct ppc_link_hash_entry *) h)->fake))
8387904d
AM
4632 return h;
4633
4634 if (name[0] == '.')
4635 return h;
4636
4637 len = strlen (name);
4638 dot_name = bfd_alloc (abfd, len + 2);
4639 if (dot_name == NULL)
4640 return (struct elf_link_hash_entry *) 0 - 1;
4641 dot_name[0] = '.';
4642 memcpy (dot_name + 1, name, len + 1);
4643 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4644 bfd_release (abfd, dot_name);
4645 return h;
4646}
4647
4648/* This function satisfies all old ABI object references to ".bar" if a
99877b66
AM
4649 new ABI object defines "bar". Well, at least, undefined dot symbols
4650 are made weak. This stops later archive searches from including an
4651 object if we already have a function descriptor definition. It also
35b0ce59
AM
4652 prevents the linker complaining about undefined symbols.
4653 We also check and correct mismatched symbol visibility here. The
4654 most restrictive visibility of the function descriptor and the
4655 function entry symbol is used. */
8387904d
AM
4656
4657static bfd_boolean
b3fac117 4658add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
8387904d 4659{
8387904d
AM
4660 struct ppc_link_hash_table *htab;
4661 struct ppc_link_hash_entry *fdh;
4662
b3fac117 4663 if (eh->elf.root.type == bfd_link_hash_indirect)
8387904d
AM
4664 return TRUE;
4665
b3fac117
AM
4666 if (eh->elf.root.type == bfd_link_hash_warning)
4667 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
8387904d 4668
b3fac117
AM
4669 if (eh->elf.root.root.string[0] != '.')
4670 abort ();
8387904d 4671
b3fac117 4672 htab = ppc_hash_table (info);
4dfe6ac6
NC
4673 if (htab == NULL)
4674 return FALSE;
4675
b31867b6
AM
4676 fdh = lookup_fdh (eh, htab);
4677 if (fdh == NULL)
4678 {
4679 if (!info->relocatable
4680 && (eh->elf.root.type == bfd_link_hash_undefined
4681 || eh->elf.root.type == bfd_link_hash_undefweak)
4682 && eh->elf.ref_regular)
4683 {
4684 /* Make an undefweak function descriptor sym, which is enough to
4685 pull in an --as-needed shared lib, but won't cause link
4686 errors. Archives are handled elsewhere. */
4687 fdh = make_fdh (info, eh);
4688 if (fdh == NULL)
4689 return FALSE;
4690 fdh->elf.ref_regular = 1;
4691 }
bb700d78 4692 }
b31867b6 4693 else
8387904d 4694 {
35b0ce59
AM
4695 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4696 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
4697 if (entry_vis < descr_vis)
4698 fdh->elf.other += entry_vis - descr_vis;
4699 else if (entry_vis > descr_vis)
4700 eh->elf.other += descr_vis - entry_vis;
4701
e87d886e
AM
4702 if ((fdh->elf.root.type == bfd_link_hash_defined
4703 || fdh->elf.root.type == bfd_link_hash_defweak)
4704 && eh->elf.root.type == bfd_link_hash_undefined)
35b0ce59
AM
4705 {
4706 eh->elf.root.type = bfd_link_hash_undefweak;
4707 eh->was_undefined = 1;
4708 htab->twiddled_syms = 1;
4709 }
8387904d 4710 }
99877b66 4711
8387904d
AM
4712 return TRUE;
4713}
4714
b3fac117
AM
4715/* Process list of dot-symbols we made in link_hash_newfunc. */
4716
8387904d 4717static bfd_boolean
7d9616d7 4718ppc64_elf_process_dot_syms (bfd *ibfd, struct bfd_link_info *info)
8387904d 4719{
99877b66 4720 struct ppc_link_hash_table *htab;
b3fac117 4721 struct ppc_link_hash_entry **p, *eh;
433817dd 4722
0c8d6e5c 4723 if (!is_ppc64_elf (info->output_bfd))
35b0ce59 4724 return TRUE;
4dfe6ac6
NC
4725 htab = ppc_hash_table (info);
4726 if (htab == NULL)
4727 return FALSE;
35b0ce59 4728
0c8d6e5c 4729 if (is_ppc64_elf (ibfd))
b3fac117
AM
4730 {
4731 p = &htab->dot_syms;
4732 while ((eh = *p) != NULL)
4733 {
4734 *p = NULL;
4735 if (!add_symbol_adjust (eh, info))
4736 return FALSE;
4737 p = &eh->u.next_dot_sym;
4738 }
4739 }
4740
4741 /* Clear the list for non-ppc64 input files. */
4742 p = &htab->dot_syms;
4743 while ((eh = *p) != NULL)
4744 {
4745 *p = NULL;
4746 p = &eh->u.next_dot_sym;
4747 }
99877b66
AM
4748
4749 /* We need to fix the undefs list for any syms we have twiddled to
4750 undef_weak. */
4751 if (htab->twiddled_syms)
4752 {
77cfaee6 4753 bfd_link_repair_undef_list (&htab->elf.root);
99877b66
AM
4754 htab->twiddled_syms = 0;
4755 }
b3fac117 4756 return TRUE;
8387904d
AM
4757}
4758
97fed1c9
JJ
4759/* Undo hash table changes when an --as-needed input file is determined
4760 not to be needed. */
4761
4762static bfd_boolean
4763ppc64_elf_as_needed_cleanup (bfd *ibfd ATTRIBUTE_UNUSED,
4764 struct bfd_link_info *info)
4765{
4dfe6ac6
NC
4766 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4767
4768 if (htab == NULL)
4769 return FALSE;
4770
4771 htab->dot_syms = NULL;
97fed1c9
JJ
4772 return TRUE;
4773}
4774
aa374f67
AM
4775/* If --just-symbols against a final linked binary, then assume we need
4776 toc adjusting stubs when calling functions defined there. */
4777
4778static void
4779ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
4780{
4781 if ((sec->flags & SEC_CODE) != 0
4782 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
4783 && is_ppc64_elf (sec->owner))
4784 {
4785 asection *got = bfd_get_section_by_name (sec->owner, ".got");
4786 if (got != NULL
4787 && got->size >= elf_backend_got_header_size
4788 && bfd_get_section_by_name (sec->owner, ".opd") != NULL)
4789 sec->has_toc_reloc = 1;
4790 }
4791 _bfd_elf_link_just_syms (sec, info);
4792}
4793
e054468f 4794static struct plt_entry **
4ce794b7
AM
4795update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
4796 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
411e1bfb
AM
4797{
4798 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
e054468f 4799 struct plt_entry **local_plt;
f961d9dd 4800 unsigned char *local_got_tls_masks;
411e1bfb
AM
4801
4802 if (local_got_ents == NULL)
4803 {
4804 bfd_size_type size = symtab_hdr->sh_info;
4805
e054468f
AM
4806 size *= (sizeof (*local_got_ents)
4807 + sizeof (*local_plt)
4808 + sizeof (*local_got_tls_masks));
4ce794b7 4809 local_got_ents = bfd_zalloc (abfd, size);
411e1bfb 4810 if (local_got_ents == NULL)
e054468f 4811 return NULL;
411e1bfb
AM
4812 elf_local_got_ents (abfd) = local_got_ents;
4813 }
4814
e054468f 4815 if ((tls_type & (PLT_IFUNC | TLS_EXPLICIT)) == 0)
411e1bfb
AM
4816 {
4817 struct got_entry *ent;
4818
4819 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
e717da7e
AM
4820 if (ent->addend == r_addend
4821 && ent->owner == abfd
4822 && ent->tls_type == tls_type)
411e1bfb
AM
4823 break;
4824 if (ent == NULL)
4825 {
4826 bfd_size_type amt = sizeof (*ent);
4ce794b7 4827 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
4828 if (ent == NULL)
4829 return FALSE;
4830 ent->next = local_got_ents[r_symndx];
4831 ent->addend = r_addend;
e717da7e 4832 ent->owner = abfd;
411e1bfb 4833 ent->tls_type = tls_type;
927be08e 4834 ent->is_indirect = FALSE;
411e1bfb
AM
4835 ent->got.refcount = 0;
4836 local_got_ents[r_symndx] = ent;
4837 }
4838 ent->got.refcount += 1;
4839 }
4840
e054468f 4841 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
f961d9dd 4842 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
e7b938ca 4843 local_got_tls_masks[r_symndx] |= tls_type;
e054468f
AM
4844
4845 return local_plt + r_symndx;
65f38f15
AM
4846}
4847
411e1bfb 4848static bfd_boolean
e054468f 4849update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
1e2f5b6e 4850{
411e1bfb 4851 struct plt_entry *ent;
1e2f5b6e 4852
e054468f 4853 for (ent = *plist; ent != NULL; ent = ent->next)
411e1bfb
AM
4854 if (ent->addend == addend)
4855 break;
4856 if (ent == NULL)
1e2f5b6e 4857 {
411e1bfb 4858 bfd_size_type amt = sizeof (*ent);
4ce794b7 4859 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
4860 if (ent == NULL)
4861 return FALSE;
e054468f 4862 ent->next = *plist;
411e1bfb
AM
4863 ent->addend = addend;
4864 ent->plt.refcount = 0;
e054468f 4865 *plist = ent;
1e2f5b6e 4866 }
411e1bfb 4867 ent->plt.refcount += 1;
b34976b6 4868 return TRUE;
1e2f5b6e
AM
4869}
4870
e054468f
AM
4871static bfd_boolean
4872is_branch_reloc (enum elf_ppc64_reloc_type r_type)
4873{
4874 return (r_type == R_PPC64_REL24
4875 || r_type == R_PPC64_REL14
4876 || r_type == R_PPC64_REL14_BRTAKEN
4877 || r_type == R_PPC64_REL14_BRNTAKEN
4878 || r_type == R_PPC64_ADDR24
4879 || r_type == R_PPC64_ADDR14
4880 || r_type == R_PPC64_ADDR14_BRTAKEN
4881 || r_type == R_PPC64_ADDR14_BRNTAKEN);
4882}
4883
5bd4f169 4884/* Look through the relocs for a section during the first phase, and
65f38f15 4885 calculate needed space in the global offset table, procedure
5d1634d7 4886 linkage table, and dynamic reloc sections. */
5bd4f169 4887
b34976b6 4888static bfd_boolean
4ce794b7
AM
4889ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
4890 asection *sec, const Elf_Internal_Rela *relocs)
5bd4f169 4891{
65f38f15 4892 struct ppc_link_hash_table *htab;
5bd4f169 4893 Elf_Internal_Shdr *symtab_hdr;
c7e2358a 4894 struct elf_link_hash_entry **sym_hashes;
5bd4f169
AM
4895 const Elf_Internal_Rela *rel;
4896 const Elf_Internal_Rela *rel_end;
5bd4f169 4897 asection *sreloc;
1e2f5b6e 4898 asection **opd_sym_map;
3a71aa26 4899 struct elf_link_hash_entry *tga, *dottga;
5bd4f169 4900
1049f94e 4901 if (info->relocatable)
b34976b6 4902 return TRUE;
5bd4f169 4903
680a3378
AM
4904 /* Don't do anything special with non-loaded, non-alloced sections.
4905 In particular, any relocs in such sections should not affect GOT
4906 and PLT reference counting (ie. we don't allow them to create GOT
4907 or PLT entries), there's no possibility or desire to optimize TLS
4908 relocs, and there's not much point in propagating relocs to shared
4909 libs that the dynamic linker won't relocate. */
4910 if ((sec->flags & SEC_ALLOC) == 0)
4911 return TRUE;
4912
0c8d6e5c 4913 BFD_ASSERT (is_ppc64_elf (abfd));
0ffa91dd 4914
65f38f15 4915 htab = ppc_hash_table (info);
4dfe6ac6
NC
4916 if (htab == NULL)
4917 return FALSE;
4918
3a71aa26
AM
4919 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
4920 FALSE, FALSE, TRUE);
4921 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
4922 FALSE, FALSE, TRUE);
0ffa91dd 4923 symtab_hdr = &elf_symtab_hdr (abfd);
5bd4f169 4924 sym_hashes = elf_sym_hashes (abfd);
5bd4f169 4925 sreloc = NULL;
1e2f5b6e 4926 opd_sym_map = NULL;
70cc837d 4927 if (strcmp (sec->name, ".opd") == 0)
1e2f5b6e
AM
4928 {
4929 /* Garbage collection needs some extra help with .opd sections.
4930 We don't want to necessarily keep everything referenced by
4931 relocs in .opd, as that would keep all functions. Instead,
4932 if we reference an .opd symbol (a function descriptor), we
4933 want to keep the function code symbol's section. This is
4934 easy for global symbols, but for local syms we need to keep
74f0fb50 4935 information about the associated function section. */
1e2f5b6e
AM
4936 bfd_size_type amt;
4937
74f0fb50 4938 amt = sec->size * sizeof (*opd_sym_map) / 8;
4ce794b7 4939 opd_sym_map = bfd_zalloc (abfd, amt);
1e2f5b6e 4940 if (opd_sym_map == NULL)
b34976b6 4941 return FALSE;
74f0fb50 4942 ppc64_elf_section_data (sec)->u.opd.func_sec = opd_sym_map;
7c8fe5c4
AM
4943 BFD_ASSERT (ppc64_elf_section_data (sec)->sec_type == sec_normal);
4944 ppc64_elf_section_data (sec)->sec_type = sec_opd;
1e2f5b6e 4945 }
5bd4f169 4946
82bd7b59
AM
4947 if (htab->sfpr == NULL
4948 && !create_linkage_sections (htab->elf.dynobj, info))
b34976b6 4949 return FALSE;
82bd7b59 4950
5bd4f169
AM
4951 rel_end = relocs + sec->reloc_count;
4952 for (rel = relocs; rel < rel_end; rel++)
4953 {
4954 unsigned long r_symndx;
4955 struct elf_link_hash_entry *h;
04c9666a 4956 enum elf_ppc64_reloc_type r_type;
727fc41e 4957 int tls_type;
7c8fe5c4 4958 struct _ppc64_elf_section_data *ppc64_sec;
e054468f 4959 struct plt_entry **ifunc;
5bd4f169
AM
4960
4961 r_symndx = ELF64_R_SYM (rel->r_info);
4962 if (r_symndx < symtab_hdr->sh_info)
4963 h = NULL;
4964 else
973a3492
L
4965 {
4966 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 4967 h = elf_follow_link (h);
973a3492 4968 }
5bd4f169 4969
727fc41e 4970 tls_type = 0;
e054468f 4971 ifunc = NULL;
25f23106
AM
4972 if (h != NULL)
4973 {
4974 if (h->type == STT_GNU_IFUNC)
4975 {
4976 h->needs_plt = 1;
4977 ifunc = &h->plt.plist;
4978 }
4979 }
4980 else
4981 {
4982 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4983 abfd, r_symndx);
4984 if (isym == NULL)
4985 return FALSE;
4986
4987 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4988 {
4989 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
4990 rel->r_addend, PLT_IFUNC);
4991 if (ifunc == NULL)
4992 return FALSE;
4993 }
4994 }
4ce794b7 4995 r_type = ELF64_R_TYPE (rel->r_info);
e054468f
AM
4996 if (is_branch_reloc (r_type))
4997 {
4998 if (h != NULL && (h == tga || h == dottga))
4999 {
5000 if (rel != relocs
5001 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5002 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5003 /* We have a new-style __tls_get_addr call with a marker
5004 reloc. */
5005 ;
5006 else
5007 /* Mark this section as having an old-style call. */
5008 sec->has_tls_get_addr_call = 1;
5009 }
727fc41e 5010
e054468f 5011 /* STT_GNU_IFUNC symbols must have a PLT entry. */
e054468f
AM
5012 if (ifunc != NULL
5013 && !update_plt_info (abfd, ifunc, rel->r_addend))
5014 return FALSE;
5015 }
727fc41e 5016
a33d1f77 5017 switch (r_type)
5bd4f169 5018 {
727fc41e
AM
5019 case R_PPC64_TLSGD:
5020 case R_PPC64_TLSLD:
5021 /* These special tls relocs tie a call to __tls_get_addr with
5022 its parameter symbol. */
5023 break;
5024
411e1bfb
AM
5025 case R_PPC64_GOT_TLSLD16:
5026 case R_PPC64_GOT_TLSLD16_LO:
5027 case R_PPC64_GOT_TLSLD16_HI:
5028 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 5029 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
5030 goto dogottls;
5031
5032 case R_PPC64_GOT_TLSGD16:
5033 case R_PPC64_GOT_TLSGD16_LO:
5034 case R_PPC64_GOT_TLSGD16_HI:
5035 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 5036 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
5037 goto dogottls;
5038
5039 case R_PPC64_GOT_TPREL16_DS:
5040 case R_PPC64_GOT_TPREL16_LO_DS:
5041 case R_PPC64_GOT_TPREL16_HI:
5042 case R_PPC64_GOT_TPREL16_HA:
1d483afe 5043 if (!info->executable)
411e1bfb
AM
5044 info->flags |= DF_STATIC_TLS;
5045 tls_type = TLS_TLS | TLS_TPREL;
5046 goto dogottls;
5047
5048 case R_PPC64_GOT_DTPREL16_DS:
5049 case R_PPC64_GOT_DTPREL16_LO_DS:
5050 case R_PPC64_GOT_DTPREL16_HI:
5051 case R_PPC64_GOT_DTPREL16_HA:
5052 tls_type = TLS_TLS | TLS_DTPREL;
5053 dogottls:
5054 sec->has_tls_reloc = 1;
5055 /* Fall thru */
5056
5bd4f169 5057 case R_PPC64_GOT16:
5bd4f169 5058 case R_PPC64_GOT16_DS:
65f38f15
AM
5059 case R_PPC64_GOT16_HA:
5060 case R_PPC64_GOT16_HI:
5061 case R_PPC64_GOT16_LO:
5bd4f169 5062 case R_PPC64_GOT16_LO_DS:
65f38f15 5063 /* This symbol requires a global offset table entry. */
4c52953f 5064 sec->has_toc_reloc = 1;
33c0ec9d
AM
5065 if (r_type == R_PPC64_GOT_TLSLD16
5066 || r_type == R_PPC64_GOT_TLSGD16
5067 || r_type == R_PPC64_GOT_TPREL16_DS
5068 || r_type == R_PPC64_GOT_DTPREL16_DS
5069 || r_type == R_PPC64_GOT16
5070 || r_type == R_PPC64_GOT16_DS)
5071 {
5072 htab->do_multi_toc = 1;
d77c8a4b 5073 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
33c0ec9d
AM
5074 }
5075
e717da7e
AM
5076 if (ppc64_elf_tdata (abfd)->got == NULL
5077 && !create_got_section (abfd, info))
b34976b6 5078 return FALSE;
5bd4f169
AM
5079
5080 if (h != NULL)
5081 {
411e1bfb
AM
5082 struct ppc_link_hash_entry *eh;
5083 struct got_entry *ent;
65f38f15 5084
411e1bfb
AM
5085 eh = (struct ppc_link_hash_entry *) h;
5086 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
5087 if (ent->addend == rel->r_addend
e717da7e 5088 && ent->owner == abfd
411e1bfb
AM
5089 && ent->tls_type == tls_type)
5090 break;
5091 if (ent == NULL)
5bd4f169 5092 {
411e1bfb 5093 bfd_size_type amt = sizeof (*ent);
4ce794b7 5094 ent = bfd_alloc (abfd, amt);
411e1bfb 5095 if (ent == NULL)
b34976b6 5096 return FALSE;
411e1bfb
AM
5097 ent->next = eh->elf.got.glist;
5098 ent->addend = rel->r_addend;
e717da7e 5099 ent->owner = abfd;
411e1bfb 5100 ent->tls_type = tls_type;
927be08e 5101 ent->is_indirect = FALSE;
411e1bfb
AM
5102 ent->got.refcount = 0;
5103 eh->elf.got.glist = ent;
5bd4f169 5104 }
411e1bfb 5105 ent->got.refcount += 1;
e7b938ca 5106 eh->tls_mask |= tls_type;
5bd4f169 5107 }
411e1bfb
AM
5108 else
5109 /* This is a global offset table entry for a local symbol. */
5110 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5111 rel->r_addend, tls_type))
5112 return FALSE;
5bd4f169
AM
5113 break;
5114
5bd4f169 5115 case R_PPC64_PLT16_HA:
65f38f15
AM
5116 case R_PPC64_PLT16_HI:
5117 case R_PPC64_PLT16_LO:
5118 case R_PPC64_PLT32:
5119 case R_PPC64_PLT64:
5bd4f169 5120 /* This symbol requires a procedure linkage table entry. We
3fad3c7c
AM
5121 actually build the entry in adjust_dynamic_symbol,
5122 because this might be a case of linking PIC code without
5123 linking in any dynamic objects, in which case we don't
5124 need to generate a procedure linkage table after all. */
5bd4f169
AM
5125 if (h == NULL)
5126 {
5127 /* It does not make sense to have a procedure linkage
3fad3c7c 5128 table entry for a local symbol. */
5bd4f169 5129 bfd_set_error (bfd_error_bad_value);
b34976b6 5130 return FALSE;
5bd4f169 5131 }
411e1bfb 5132 else
e054468f
AM
5133 {
5134 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5135 return FALSE;
5136 h->needs_plt = 1;
5137 if (h->root.root.string[0] == '.'
5138 && h->root.root.string[1] != '\0')
5139 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5140 }
5bd4f169
AM
5141 break;
5142
5143 /* The following relocations don't need to propagate the
5144 relocation if linking a shared object since they are
5145 section relative. */
5146 case R_PPC64_SECTOFF:
5147 case R_PPC64_SECTOFF_LO:
5148 case R_PPC64_SECTOFF_HI:
5149 case R_PPC64_SECTOFF_HA:
5150 case R_PPC64_SECTOFF_DS:
5151 case R_PPC64_SECTOFF_LO_DS:
411e1bfb
AM
5152 case R_PPC64_DTPREL16:
5153 case R_PPC64_DTPREL16_LO:
5154 case R_PPC64_DTPREL16_HI:
5155 case R_PPC64_DTPREL16_HA:
5156 case R_PPC64_DTPREL16_DS:
5157 case R_PPC64_DTPREL16_LO_DS:
5158 case R_PPC64_DTPREL16_HIGHER:
5159 case R_PPC64_DTPREL16_HIGHERA:
5160 case R_PPC64_DTPREL16_HIGHEST:
5161 case R_PPC64_DTPREL16_HIGHESTA:
5bd4f169
AM
5162 break;
5163
ad8e1ba5 5164 /* Nor do these. */
25f23106
AM
5165 case R_PPC64_REL16:
5166 case R_PPC64_REL16_LO:
5167 case R_PPC64_REL16_HI:
5168 case R_PPC64_REL16_HA:
5169 break;
5170
ad8e1ba5 5171 case R_PPC64_TOC16:
33c0ec9d
AM
5172 case R_PPC64_TOC16_DS:
5173 htab->do_multi_toc = 1;
d77c8a4b 5174 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
ad8e1ba5
AM
5175 case R_PPC64_TOC16_LO:
5176 case R_PPC64_TOC16_HI:
5177 case R_PPC64_TOC16_HA:
ad8e1ba5 5178 case R_PPC64_TOC16_LO_DS:
4c52953f 5179 sec->has_toc_reloc = 1;
ad8e1ba5
AM
5180 break;
5181
5bd4f169
AM
5182 /* This relocation describes the C++ object vtable hierarchy.
5183 Reconstruct it for later use during GC. */
5184 case R_PPC64_GNU_VTINHERIT:
c152c796 5185 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 5186 return FALSE;
5bd4f169
AM
5187 break;
5188
5189 /* This relocation describes which C++ vtable entries are actually
5190 used. Record for later use during GC. */
5191 case R_PPC64_GNU_VTENTRY:
d17e0c6e
JB
5192 BFD_ASSERT (h != NULL);
5193 if (h != NULL
5194 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
b34976b6 5195 return FALSE;
5bd4f169
AM
5196 break;
5197
721956f4
AM
5198 case R_PPC64_REL14:
5199 case R_PPC64_REL14_BRTAKEN:
5200 case R_PPC64_REL14_BRNTAKEN:
220c76dd
AM
5201 {
5202 asection *dest = NULL;
5203
5204 /* Heuristic: If jumping outside our section, chances are
5205 we are going to need a stub. */
5206 if (h != NULL)
5207 {
5208 /* If the sym is weak it may be overridden later, so
5209 don't assume we know where a weak sym lives. */
5210 if (h->root.type == bfd_link_hash_defined)
5211 dest = h->root.u.def.section;
5212 }
5213 else
87d72d41
AM
5214 {
5215 Elf_Internal_Sym *isym;
5216
5217 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5218 abfd, r_symndx);
5219 if (isym == NULL)
5220 return FALSE;
5221
5222 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5223 }
5224
220c76dd 5225 if (dest != sec)
7c8fe5c4 5226 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
220c76dd 5227 }
721956f4
AM
5228 /* Fall through. */
5229
5d1634d7 5230 case R_PPC64_REL24:
e054468f 5231 if (h != NULL && ifunc == NULL)
5d1634d7
AM
5232 {
5233 /* We may need a .plt entry if the function this reloc
5234 refers to is in a shared lib. */
e054468f 5235 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
411e1bfb 5236 return FALSE;
e054468f
AM
5237 h->needs_plt = 1;
5238 if (h->root.root.string[0] == '.'
5239 && h->root.root.string[1] != '\0')
5240 ((struct ppc_link_hash_entry *) h)->is_func = 1;
3a71aa26 5241 if (h == tga || h == dottga)
411e1bfb 5242 sec->has_tls_reloc = 1;
411e1bfb
AM
5243 }
5244 break;
5245
5246 case R_PPC64_TPREL64:
5247 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
1d483afe 5248 if (!info->executable)
411e1bfb
AM
5249 info->flags |= DF_STATIC_TLS;
5250 goto dotlstoc;
5251
5252 case R_PPC64_DTPMOD64:
5253 if (rel + 1 < rel_end
5254 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5255 && rel[1].r_offset == rel->r_offset + 8)
951fd09b 5256 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
411e1bfb 5257 else
951fd09b 5258 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
411e1bfb
AM
5259 goto dotlstoc;
5260
5261 case R_PPC64_DTPREL64:
5262 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5263 if (rel != relocs
5264 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5265 && rel[-1].r_offset == rel->r_offset - 8)
5266 /* This is the second reloc of a dtpmod, dtprel pair.
5267 Don't mark with TLS_DTPREL. */
5268 goto dodyn;
5269
5270 dotlstoc:
5271 sec->has_tls_reloc = 1;
5272 if (h != NULL)
5273 {
5274 struct ppc_link_hash_entry *eh;
5275 eh = (struct ppc_link_hash_entry *) h;
e7b938ca 5276 eh->tls_mask |= tls_type;
411e1bfb
AM
5277 }
5278 else
5279 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5280 rel->r_addend, tls_type))
5281 return FALSE;
5282
7c8fe5c4
AM
5283 ppc64_sec = ppc64_elf_section_data (sec);
5284 if (ppc64_sec->sec_type != sec_toc)
411e1bfb 5285 {
3a71aa26
AM
5286 bfd_size_type amt;
5287
e7b938ca 5288 /* One extra to simplify get_tls_mask. */
3a71aa26
AM
5289 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5290 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5291 if (ppc64_sec->u.toc.symndx == NULL)
5292 return FALSE;
5293 amt = sec->size * sizeof (bfd_vma) / 8;
5294 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5295 if (ppc64_sec->u.toc.add == NULL)
411e1bfb 5296 return FALSE;
7c8fe5c4
AM
5297 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5298 ppc64_sec->sec_type = sec_toc;
411e1bfb
AM
5299 }
5300 BFD_ASSERT (rel->r_offset % 8 == 0);
3a71aa26
AM
5301 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5302 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
951fd09b
AM
5303
5304 /* Mark the second slot of a GD or LD entry.
5305 -1 to indicate GD and -2 to indicate LD. */
5306 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
3a71aa26 5307 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
951fd09b 5308 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
3a71aa26 5309 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
411e1bfb
AM
5310 goto dodyn;
5311
5312 case R_PPC64_TPREL16:
5313 case R_PPC64_TPREL16_LO:
5314 case R_PPC64_TPREL16_HI:
5315 case R_PPC64_TPREL16_HA:
5316 case R_PPC64_TPREL16_DS:
5317 case R_PPC64_TPREL16_LO_DS:
5318 case R_PPC64_TPREL16_HIGHER:
5319 case R_PPC64_TPREL16_HIGHERA:
5320 case R_PPC64_TPREL16_HIGHEST:
5321 case R_PPC64_TPREL16_HIGHESTA:
5322 if (info->shared)
5323 {
1d483afe
AM
5324 if (!info->executable)
5325 info->flags |= DF_STATIC_TLS;
411e1bfb 5326 goto dodyn;
5d1634d7
AM
5327 }
5328 break;
5329
e86ce104 5330 case R_PPC64_ADDR64:
1e2f5b6e 5331 if (opd_sym_map != NULL
1e2f5b6e 5332 && rel + 1 < rel_end
4ce794b7 5333 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
1e2f5b6e 5334 {
8387904d
AM
5335 if (h != NULL)
5336 {
5337 if (h->root.root.string[0] == '.'
5338 && h->root.root.string[1] != 0
b31867b6 5339 && lookup_fdh ((struct ppc_link_hash_entry *) h, htab))
8387904d
AM
5340 ;
5341 else
5342 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5343 }
5344 else
5345 {
5346 asection *s;
87d72d41 5347 Elf_Internal_Sym *isym;
1e2f5b6e 5348
87d72d41
AM
5349 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5350 abfd, r_symndx);
5351 if (isym == NULL)
8387904d 5352 return FALSE;
87d72d41
AM
5353
5354 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5355 if (s != NULL && s != sec)
3f764659 5356 opd_sym_map[rel->r_offset / 8] = s;
8387904d 5357 }
1e2f5b6e 5358 }
e86ce104
AM
5359 /* Fall through. */
5360
04c9666a 5361 case R_PPC64_REL30:
5bd4f169 5362 case R_PPC64_REL32:
04c9666a 5363 case R_PPC64_REL64:
65f38f15
AM
5364 case R_PPC64_ADDR14:
5365 case R_PPC64_ADDR14_BRNTAKEN:
5366 case R_PPC64_ADDR14_BRTAKEN:
5367 case R_PPC64_ADDR16:
5368 case R_PPC64_ADDR16_DS:
5369 case R_PPC64_ADDR16_HA:
5370 case R_PPC64_ADDR16_HI:
5371 case R_PPC64_ADDR16_HIGHER:
5372 case R_PPC64_ADDR16_HIGHERA:
5373 case R_PPC64_ADDR16_HIGHEST:
5374 case R_PPC64_ADDR16_HIGHESTA:
5375 case R_PPC64_ADDR16_LO:
5376 case R_PPC64_ADDR16_LO_DS:
5377 case R_PPC64_ADDR24:
65f38f15 5378 case R_PPC64_ADDR32:
65f38f15
AM
5379 case R_PPC64_UADDR16:
5380 case R_PPC64_UADDR32:
5381 case R_PPC64_UADDR64:
5bd4f169 5382 case R_PPC64_TOC:
81848ca0
AM
5383 if (h != NULL && !info->shared)
5384 /* We may need a copy reloc. */
f5385ebf 5385 h->non_got_ref = 1;
81848ca0 5386
41bd81ab 5387 /* Don't propagate .opd relocs. */
1e2f5b6e 5388 if (NO_OPD_RELOCS && opd_sym_map != NULL)
e86ce104 5389 break;
e86ce104 5390
65f38f15
AM
5391 /* If we are creating a shared library, and this is a reloc
5392 against a global symbol, or a non PC relative reloc
5393 against a local symbol, then we need to copy the reloc
5394 into the shared library. However, if we are linking with
5395 -Bsymbolic, we do not need to copy a reloc against a
5396 global symbol which is defined in an object we are
5397 including in the link (i.e., DEF_REGULAR is set). At
5398 this point we have not seen all the input files, so it is
5399 possible that DEF_REGULAR is not set now but will be set
5400 later (it is never cleared). In case of a weak definition,
5401 DEF_REGULAR may be cleared later by a strong definition in
5402 a shared library. We account for that possibility below by
f4656909 5403 storing information in the dyn_relocs field of the hash
65f38f15
AM
5404 table entry. A similar situation occurs when creating
5405 shared libraries and symbol visibility changes render the
5406 symbol local.
5407
5408 If on the other hand, we are creating an executable, we
5409 may need to keep relocations for symbols satisfied by a
5410 dynamic library if we manage to avoid copy relocs for the
5411 symbol. */
411e1bfb 5412 dodyn:
65f38f15 5413 if ((info->shared
1d483afe 5414 && (must_be_dyn_reloc (info, r_type)
65f38f15
AM
5415 || (h != NULL
5416 && (! info->symbolic
5417 || h->root.type == bfd_link_hash_defweak
f5385ebf 5418 || !h->def_regular))))
f4656909
AM
5419 || (ELIMINATE_COPY_RELOCS
5420 && !info->shared
65f38f15
AM
5421 && h != NULL
5422 && (h->root.type == bfd_link_hash_defweak
25f23106
AM
5423 || !h->def_regular))
5424 || (!info->shared
5425 && ifunc != NULL))
5bd4f169 5426 {
6061a67d
AM
5427 struct elf_dyn_relocs *p;
5428 struct elf_dyn_relocs **head;
ec338859 5429
65f38f15
AM
5430 /* We must copy these reloc types into the output file.
5431 Create a reloc section in dynobj and make room for
5432 this reloc. */
5bd4f169
AM
5433 if (sreloc == NULL)
5434 {
83bac4b0
NC
5435 sreloc = _bfd_elf_make_dynamic_reloc_section
5436 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
65f38f15 5437
5bd4f169 5438 if (sreloc == NULL)
83bac4b0 5439 return FALSE;
5bd4f169
AM
5440 }
5441
65f38f15
AM
5442 /* If this is a global symbol, we count the number of
5443 relocations we need for this symbol. */
5444 if (h != NULL)
5445 {
ec338859 5446 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
65f38f15
AM
5447 }
5448 else
5449 {
ec338859
AM
5450 /* Track dynamic relocs needed for local syms too.
5451 We really need local syms available to do this
5452 easily. Oh well. */
ec338859 5453 asection *s;
6edfbbad 5454 void *vpp;
87d72d41 5455 Elf_Internal_Sym *isym;
6edfbbad 5456
87d72d41
AM
5457 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5458 abfd, r_symndx);
5459 if (isym == NULL)
b34976b6 5460 return FALSE;
ec338859 5461
87d72d41
AM
5462 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5463 if (s == NULL)
5464 s = sec;
5465
6edfbbad 5466 vpp = &elf_section_data (s)->local_dynrel;
6061a67d 5467 head = (struct elf_dyn_relocs **) vpp;
65f38f15 5468 }
ec338859
AM
5469
5470 p = *head;
5471 if (p == NULL || p->sec != sec)
5472 {
4ce794b7 5473 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
ec338859 5474 if (p == NULL)
b34976b6 5475 return FALSE;
ec338859
AM
5476 p->next = *head;
5477 *head = p;
5478 p->sec = sec;
5479 p->count = 0;
5480 p->pc_count = 0;
5481 }
5482
5483 p->count += 1;
1d483afe 5484 if (!must_be_dyn_reloc (info, r_type))
ec338859 5485 p->pc_count += 1;
65f38f15 5486 }
5bd4f169 5487 break;
65f38f15
AM
5488
5489 default:
96e0dda4 5490 break;
5bd4f169
AM
5491 }
5492 }
5493
b34976b6 5494 return TRUE;
5bd4f169
AM
5495}
5496
8387904d
AM
5497/* OFFSET in OPD_SEC specifies a function descriptor. Return the address
5498 of the code entry point, and its section. */
5499
5500static bfd_vma
5501opd_entry_value (asection *opd_sec,
5502 bfd_vma offset,
5503 asection **code_sec,
5504 bfd_vma *code_off)
5505{
5506 bfd *opd_bfd = opd_sec->owner;
8860955f 5507 Elf_Internal_Rela *relocs;
8387904d 5508 Elf_Internal_Rela *lo, *hi, *look;
645ea6a9 5509 bfd_vma val;
8387904d 5510
4b85d634
AM
5511 /* No relocs implies we are linking a --just-symbols object. */
5512 if (opd_sec->reloc_count == 0)
5513 {
aa374f67
AM
5514 char buf[8];
5515
5516 if (!bfd_get_section_contents (opd_bfd, opd_sec, buf, offset, 8))
4b85d634 5517 return (bfd_vma) -1;
3b36f7e6 5518
aa374f67 5519 val = bfd_get_64 (opd_bfd, buf);
4b85d634
AM
5520 if (code_sec != NULL)
5521 {
5522 asection *sec, *likely = NULL;
5523 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
5524 if (sec->vma <= val
5525 && (sec->flags & SEC_LOAD) != 0
5526 && (sec->flags & SEC_ALLOC) != 0)
5527 likely = sec;
5528 if (likely != NULL)
5529 {
5530 *code_sec = likely;
5531 if (code_off != NULL)
5532 *code_off = val - likely->vma;
5533 }
5534 }
5535 return val;
5536 }
5537
0c8d6e5c 5538 BFD_ASSERT (is_ppc64_elf (opd_bfd));
0ffa91dd 5539
8860955f
AM
5540 relocs = ppc64_elf_tdata (opd_bfd)->opd_relocs;
5541 if (relocs == NULL)
5542 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
645ea6a9 5543
8387904d 5544 /* Go find the opd reloc at the sym address. */
8860955f 5545 lo = relocs;
8387904d
AM
5546 BFD_ASSERT (lo != NULL);
5547 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
645ea6a9 5548 val = (bfd_vma) -1;
8387904d
AM
5549 while (lo < hi)
5550 {
5551 look = lo + (hi - lo) / 2;
5552 if (look->r_offset < offset)
5553 lo = look + 1;
5554 else if (look->r_offset > offset)
5555 hi = look;
5556 else
5557 {
0ffa91dd
NC
5558 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
5559
8387904d
AM
5560 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
5561 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
5562 {
5563 unsigned long symndx = ELF64_R_SYM (look->r_info);
8387904d
AM
5564 asection *sec;
5565
5566 if (symndx < symtab_hdr->sh_info)
5567 {
5568 Elf_Internal_Sym *sym;
5569
5570 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
5571 if (sym == NULL)
5572 {
5573 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5574 symtab_hdr->sh_info,
5575 0, NULL, NULL, NULL);
5576 if (sym == NULL)
645ea6a9 5577 break;
8387904d
AM
5578 symtab_hdr->contents = (bfd_byte *) sym;
5579 }
5580
5581 sym += symndx;
5582 val = sym->st_value;
cb33740c 5583 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
8387904d
AM
5584 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
5585 }
5586 else
5587 {
5588 struct elf_link_hash_entry **sym_hashes;
5589 struct elf_link_hash_entry *rh;
5590
5591 sym_hashes = elf_sym_hashes (opd_bfd);
5592 rh = sym_hashes[symndx - symtab_hdr->sh_info];
b31867b6 5593 rh = elf_follow_link (rh);
8387904d
AM
5594 BFD_ASSERT (rh->root.type == bfd_link_hash_defined
5595 || rh->root.type == bfd_link_hash_defweak);
5596 val = rh->root.u.def.value;
5597 sec = rh->root.u.def.section;
5598 }
5599 val += look->r_addend;
5600 if (code_off != NULL)
5601 *code_off = val;
5602 if (code_sec != NULL)
5603 *code_sec = sec;
5604 if (sec != NULL && sec->output_section != NULL)
5605 val += sec->output_section->vma + sec->output_offset;
8387904d
AM
5606 }
5607 break;
5608 }
5609 }
645ea6a9 5610
645ea6a9 5611 return val;
8387904d
AM
5612}
5613
854b41e7
AM
5614/* Return true if symbol is defined in a regular object file. */
5615
5616static bfd_boolean
5617is_static_defined (struct elf_link_hash_entry *h)
5618{
5619 return ((h->root.type == bfd_link_hash_defined
5620 || h->root.type == bfd_link_hash_defweak)
5621 && h->root.u.def.section != NULL
5622 && h->root.u.def.section->output_section != NULL);
5623}
5624
b31867b6
AM
5625/* If FDH is a function descriptor symbol, return the associated code
5626 entry symbol if it is defined. Return NULL otherwise. */
5627
5628static struct ppc_link_hash_entry *
5629defined_code_entry (struct ppc_link_hash_entry *fdh)
5630{
5631 if (fdh->is_func_descriptor)
5632 {
5633 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
5634 if (fh->elf.root.type == bfd_link_hash_defined
5635 || fh->elf.root.type == bfd_link_hash_defweak)
5636 return fh;
5637 }
5638 return NULL;
5639}
5640
5641/* If FH is a function code entry symbol, return the associated
5642 function descriptor symbol if it is defined. Return NULL otherwise. */
5643
5644static struct ppc_link_hash_entry *
5645defined_func_desc (struct ppc_link_hash_entry *fh)
5646{
5647 if (fh->oh != NULL
5648 && fh->oh->is_func_descriptor)
5649 {
5650 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
5651 if (fdh->elf.root.type == bfd_link_hash_defined
5652 || fdh->elf.root.type == bfd_link_hash_defweak)
5653 return fdh;
5654 }
5655 return NULL;
5656}
5657
74f0fb50
AM
5658/* Mark all our entry sym sections, both opd and code section. */
5659
5660static void
5661ppc64_elf_gc_keep (struct bfd_link_info *info)
5662{
5663 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5664 struct bfd_sym_chain *sym;
5665
4dfe6ac6
NC
5666 if (htab == NULL)
5667 return;
5668
74f0fb50
AM
5669 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
5670 {
b31867b6 5671 struct ppc_link_hash_entry *eh, *fh;
74f0fb50
AM
5672 asection *sec;
5673
5674 eh = (struct ppc_link_hash_entry *)
b31867b6 5675 elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
74f0fb50
AM
5676 if (eh == NULL)
5677 continue;
5678 if (eh->elf.root.type != bfd_link_hash_defined
5679 && eh->elf.root.type != bfd_link_hash_defweak)
5680 continue;
5681
b31867b6
AM
5682 fh = defined_code_entry (eh);
5683 if (fh != NULL)
74f0fb50 5684 {
b31867b6 5685 sec = fh->elf.root.u.def.section;
74f0fb50
AM
5686 sec->flags |= SEC_KEEP;
5687 }
5688 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5689 && opd_entry_value (eh->elf.root.u.def.section,
5690 eh->elf.root.u.def.value,
5691 &sec, NULL) != (bfd_vma) -1)
5692 sec->flags |= SEC_KEEP;
5693
5694 sec = eh->elf.root.u.def.section;
5695 sec->flags |= SEC_KEEP;
5696 }
5697}
5698
64d03ab5
AM
5699/* Mark sections containing dynamically referenced symbols. When
5700 building shared libraries, we must assume that any visible symbol is
5701 referenced. */
5702
5703static bfd_boolean
5704ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
5705{
5706 struct bfd_link_info *info = (struct bfd_link_info *) inf;
5707 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
b31867b6 5708 struct ppc_link_hash_entry *fdh;
64d03ab5 5709
64d03ab5 5710 /* Dynamic linking info is on the func descriptor sym. */
b31867b6
AM
5711 fdh = defined_func_desc (eh);
5712 if (fdh != NULL)
5713 eh = fdh;
64d03ab5
AM
5714
5715 if ((eh->elf.root.type == bfd_link_hash_defined
5716 || eh->elf.root.type == bfd_link_hash_defweak)
5717 && (eh->elf.ref_dynamic
5718 || (!info->executable
5719 && eh->elf.def_regular
5720 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
4c58e0d8
AM
5721 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
5722 && (strchr (eh->elf.root.root.string, ELF_VER_CHR) != NULL
5723 || !bfd_hide_sym_by_version (info->version_info,
5724 eh->elf.root.root.string)))))
64d03ab5
AM
5725 {
5726 asection *code_sec;
b31867b6 5727 struct ppc_link_hash_entry *fh;
64d03ab5
AM
5728
5729 eh->elf.root.u.def.section->flags |= SEC_KEEP;
5730
5731 /* Function descriptor syms cause the associated
5732 function code sym section to be marked. */
b31867b6
AM
5733 fh = defined_code_entry (eh);
5734 if (fh != NULL)
5735 {
5736 code_sec = fh->elf.root.u.def.section;
5737 code_sec->flags |= SEC_KEEP;
5738 }
64d03ab5
AM
5739 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5740 && opd_entry_value (eh->elf.root.u.def.section,
5741 eh->elf.root.u.def.value,
5742 &code_sec, NULL) != (bfd_vma) -1)
5743 code_sec->flags |= SEC_KEEP;
5744 }
5745
5746 return TRUE;
5747}
5748
5bd4f169
AM
5749/* Return the section that should be marked against GC for a given
5750 relocation. */
5751
5752static asection *
4ce794b7 5753ppc64_elf_gc_mark_hook (asection *sec,
fb34365b 5754 struct bfd_link_info *info,
4ce794b7
AM
5755 Elf_Internal_Rela *rel,
5756 struct elf_link_hash_entry *h,
5757 Elf_Internal_Sym *sym)
5bd4f169 5758{
ccfa59ea
AM
5759 asection *rsec;
5760
ccfa59ea
AM
5761 /* Syms return NULL if we're marking .opd, so we avoid marking all
5762 function sections, as all functions are referenced in .opd. */
5763 rsec = NULL;
5764 if (get_opd_info (sec) != NULL)
5765 return rsec;
1e2f5b6e 5766
5bd4f169
AM
5767 if (h != NULL)
5768 {
04c9666a 5769 enum elf_ppc64_reloc_type r_type;
b31867b6 5770 struct ppc_link_hash_entry *eh, *fh, *fdh;
a33d1f77 5771
4ce794b7 5772 r_type = ELF64_R_TYPE (rel->r_info);
a33d1f77 5773 switch (r_type)
5bd4f169
AM
5774 {
5775 case R_PPC64_GNU_VTINHERIT:
5776 case R_PPC64_GNU_VTENTRY:
5777 break;
5778
5779 default:
5780 switch (h->root.type)
5781 {
5782 case bfd_link_hash_defined:
5783 case bfd_link_hash_defweak:
ccfa59ea 5784 eh = (struct ppc_link_hash_entry *) h;
b31867b6
AM
5785 fdh = defined_func_desc (eh);
5786 if (fdh != NULL)
5787 eh = fdh;
1e2f5b6e
AM
5788
5789 /* Function descriptor syms cause the associated
5790 function code sym section to be marked. */
b31867b6
AM
5791 fh = defined_code_entry (eh);
5792 if (fh != NULL)
ccfa59ea
AM
5793 {
5794 /* They also mark their opd section. */
74f0fb50 5795 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 5796
b31867b6 5797 rsec = fh->elf.root.u.def.section;
ccfa59ea 5798 }
8387904d
AM
5799 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5800 && opd_entry_value (eh->elf.root.u.def.section,
5801 eh->elf.root.u.def.value,
5802 &rsec, NULL) != (bfd_vma) -1)
74f0fb50 5803 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 5804 else
1e2f5b6e
AM
5805 rsec = h->root.u.def.section;
5806 break;
5bd4f169
AM
5807
5808 case bfd_link_hash_common:
1e2f5b6e
AM
5809 rsec = h->root.u.c.p->section;
5810 break;
5bd4f169
AM
5811
5812 default:
fb34365b 5813 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5bd4f169
AM
5814 }
5815 }
5816 }
5817 else
5818 {
74f0fb50 5819 struct _opd_sec_data *opd;
1e2f5b6e
AM
5820
5821 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
74f0fb50
AM
5822 opd = get_opd_info (rsec);
5823 if (opd != NULL && opd->func_sec != NULL)
ccfa59ea 5824 {
74f0fb50 5825 rsec->gc_mark = 1;
ccfa59ea 5826
74f0fb50 5827 rsec = opd->func_sec[(sym->st_value + rel->r_addend) / 8];
ccfa59ea 5828 }
5bd4f169
AM
5829 }
5830
1e2f5b6e 5831 return rsec;
5bd4f169
AM
5832}
5833
65f38f15
AM
5834/* Update the .got, .plt. and dynamic reloc reference counts for the
5835 section being removed. */
5bd4f169 5836
b34976b6 5837static bfd_boolean
4ce794b7
AM
5838ppc64_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
5839 asection *sec, const Elf_Internal_Rela *relocs)
5bd4f169 5840{
411e1bfb 5841 struct ppc_link_hash_table *htab;
5bd4f169
AM
5842 Elf_Internal_Shdr *symtab_hdr;
5843 struct elf_link_hash_entry **sym_hashes;
411e1bfb 5844 struct got_entry **local_got_ents;
5bd4f169 5845 const Elf_Internal_Rela *rel, *relend;
5bd4f169 5846
7dda2462
TG
5847 if (info->relocatable)
5848 return TRUE;
5849
680a3378
AM
5850 if ((sec->flags & SEC_ALLOC) == 0)
5851 return TRUE;
5852
ec338859
AM
5853 elf_section_data (sec)->local_dynrel = NULL;
5854
411e1bfb 5855 htab = ppc_hash_table (info);
4dfe6ac6
NC
5856 if (htab == NULL)
5857 return FALSE;
5858
0ffa91dd 5859 symtab_hdr = &elf_symtab_hdr (abfd);
5bd4f169 5860 sym_hashes = elf_sym_hashes (abfd);
411e1bfb 5861 local_got_ents = elf_local_got_ents (abfd);
5bd4f169
AM
5862
5863 relend = relocs + sec->reloc_count;
5864 for (rel = relocs; rel < relend; rel++)
a33d1f77
AM
5865 {
5866 unsigned long r_symndx;
04c9666a 5867 enum elf_ppc64_reloc_type r_type;
58ac9f71 5868 struct elf_link_hash_entry *h = NULL;
f961d9dd 5869 unsigned char tls_type = 0;
5bd4f169 5870
a33d1f77 5871 r_symndx = ELF64_R_SYM (rel->r_info);
4ce794b7 5872 r_type = ELF64_R_TYPE (rel->r_info);
58ac9f71
AM
5873 if (r_symndx >= symtab_hdr->sh_info)
5874 {
5875 struct ppc_link_hash_entry *eh;
6061a67d
AM
5876 struct elf_dyn_relocs **pp;
5877 struct elf_dyn_relocs *p;
58ac9f71
AM
5878
5879 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 5880 h = elf_follow_link (h);
58ac9f71
AM
5881 eh = (struct ppc_link_hash_entry *) h;
5882
5883 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
5884 if (p->sec == sec)
5885 {
5886 /* Everything must go for SEC. */
5887 *pp = p->next;
5888 break;
5889 }
5890 }
5891
e054468f
AM
5892 if (is_branch_reloc (r_type))
5893 {
5894 struct plt_entry **ifunc = NULL;
5895 if (h != NULL)
5896 {
5897 if (h->type == STT_GNU_IFUNC)
5898 ifunc = &h->plt.plist;
5899 }
5900 else if (local_got_ents != NULL)
5901 {
5902 struct plt_entry **local_plt = (struct plt_entry **)
5903 (local_got_ents + symtab_hdr->sh_info);
f961d9dd 5904 unsigned char *local_got_tls_masks = (unsigned char *)
e054468f
AM
5905 (local_plt + symtab_hdr->sh_info);
5906 if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
5907 ifunc = local_plt + r_symndx;
5908 }
5909 if (ifunc != NULL)
5910 {
5911 struct plt_entry *ent;
5912
5913 for (ent = *ifunc; ent != NULL; ent = ent->next)
5914 if (ent->addend == rel->r_addend)
5915 break;
5916 if (ent == NULL)
5917 abort ();
5918 if (ent->plt.refcount > 0)
5919 ent->plt.refcount -= 1;
5920 continue;
5921 }
5922 }
5923
a33d1f77
AM
5924 switch (r_type)
5925 {
411e1bfb
AM
5926 case R_PPC64_GOT_TLSLD16:
5927 case R_PPC64_GOT_TLSLD16_LO:
5928 case R_PPC64_GOT_TLSLD16_HI:
5929 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 5930 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
5931 goto dogot;
5932
5933 case R_PPC64_GOT_TLSGD16:
5934 case R_PPC64_GOT_TLSGD16_LO:
5935 case R_PPC64_GOT_TLSGD16_HI:
5936 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 5937 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
5938 goto dogot;
5939
5940 case R_PPC64_GOT_TPREL16_DS:
5941 case R_PPC64_GOT_TPREL16_LO_DS:
5942 case R_PPC64_GOT_TPREL16_HI:
5943 case R_PPC64_GOT_TPREL16_HA:
5944 tls_type = TLS_TLS | TLS_TPREL;
5945 goto dogot;
5946
5947 case R_PPC64_GOT_DTPREL16_DS:
5948 case R_PPC64_GOT_DTPREL16_LO_DS:
5949 case R_PPC64_GOT_DTPREL16_HI:
5950 case R_PPC64_GOT_DTPREL16_HA:
5951 tls_type = TLS_TLS | TLS_DTPREL;
5952 goto dogot;
5953
a33d1f77
AM
5954 case R_PPC64_GOT16:
5955 case R_PPC64_GOT16_DS:
5956 case R_PPC64_GOT16_HA:
5957 case R_PPC64_GOT16_HI:
5958 case R_PPC64_GOT16_LO:
5959 case R_PPC64_GOT16_LO_DS:
411e1bfb
AM
5960 dogot:
5961 {
5962 struct got_entry *ent;
5963
58ac9f71
AM
5964 if (h != NULL)
5965 ent = h->got.glist;
411e1bfb
AM
5966 else
5967 ent = local_got_ents[r_symndx];
5968
5969 for (; ent != NULL; ent = ent->next)
5970 if (ent->addend == rel->r_addend
e717da7e 5971 && ent->owner == abfd
411e1bfb
AM
5972 && ent->tls_type == tls_type)
5973 break;
5974 if (ent == NULL)
5975 abort ();
5976 if (ent->got.refcount > 0)
5977 ent->got.refcount -= 1;
5978 }
a33d1f77 5979 break;
65f38f15 5980
a33d1f77
AM
5981 case R_PPC64_PLT16_HA:
5982 case R_PPC64_PLT16_HI:
5983 case R_PPC64_PLT16_LO:
5984 case R_PPC64_PLT32:
5985 case R_PPC64_PLT64:
721956f4
AM
5986 case R_PPC64_REL14:
5987 case R_PPC64_REL14_BRNTAKEN:
5988 case R_PPC64_REL14_BRTAKEN:
5d1634d7 5989 case R_PPC64_REL24:
58ac9f71 5990 if (h != NULL)
5d1634d7 5991 {
411e1bfb
AM
5992 struct plt_entry *ent;
5993
411e1bfb
AM
5994 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
5995 if (ent->addend == rel->r_addend)
5996 break;
7c6c1722 5997 if (ent != NULL && ent->plt.refcount > 0)
411e1bfb 5998 ent->plt.refcount -= 1;
5d1634d7 5999 }
e86ce104 6000 break;
5d1634d7 6001
a33d1f77
AM
6002 default:
6003 break;
6004 }
6005 }
b34976b6 6006 return TRUE;
5bd4f169
AM
6007}
6008
deb0e272
AM
6009/* The maximum size of .sfpr. */
6010#define SFPR_MAX (218*4)
6011
6012struct sfpr_def_parms
6013{
699733f6
AM
6014 const char name[12];
6015 unsigned char lo, hi;
deb0e272
AM
6016 bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
6017 bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
6018};
6019
6020/* Auto-generate _save*, _rest* functions in .sfpr. */
6021
4dfe6ac6 6022static bfd_boolean
deb0e272
AM
6023sfpr_define (struct bfd_link_info *info, const struct sfpr_def_parms *parm)
6024{
6025 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6026 unsigned int i;
6027 size_t len = strlen (parm->name);
6028 bfd_boolean writing = FALSE;
699733f6 6029 char sym[16];
deb0e272 6030
4dfe6ac6
NC
6031 if (htab == NULL)
6032 return FALSE;
6033
deb0e272
AM
6034 memcpy (sym, parm->name, len);
6035 sym[len + 2] = 0;
6036
6037 for (i = parm->lo; i <= parm->hi; i++)
6038 {
6039 struct elf_link_hash_entry *h;
6040
6041 sym[len + 0] = i / 10 + '0';
6042 sym[len + 1] = i % 10 + '0';
6043 h = elf_link_hash_lookup (&htab->elf, sym, FALSE, FALSE, TRUE);
6044 if (h != NULL
f5385ebf 6045 && !h->def_regular)
deb0e272
AM
6046 {
6047 h->root.type = bfd_link_hash_defined;
6048 h->root.u.def.section = htab->sfpr;
6049 h->root.u.def.value = htab->sfpr->size;
6050 h->type = STT_FUNC;
f5385ebf 6051 h->def_regular = 1;
deb0e272
AM
6052 _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
6053 writing = TRUE;
6054 if (htab->sfpr->contents == NULL)
6055 {
6056 htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
6057 if (htab->sfpr->contents == NULL)
6058 return FALSE;
6059 }
6060 }
6061 if (writing)
6062 {
6063 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6064 if (i != parm->hi)
6065 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6066 else
6067 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6068 htab->sfpr->size = p - htab->sfpr->contents;
6069 }
6070 }
6071
6072 return TRUE;
6073}
6074
6075static bfd_byte *
6076savegpr0 (bfd *abfd, bfd_byte *p, int r)
6077{
6078 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6079 return p + 4;
6080}
6081
6082static bfd_byte *
6083savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6084{
6085 p = savegpr0 (abfd, p, r);
6086 bfd_put_32 (abfd, STD_R0_0R1 + 16, p);
6087 p = p + 4;
6088 bfd_put_32 (abfd, BLR, p);
6089 return p + 4;
6090}
6091
6092static bfd_byte *
6093restgpr0 (bfd *abfd, bfd_byte *p, int r)
6094{
6095 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6096 return p + 4;
6097}
6098
6099static bfd_byte *
6100restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6101{
6102 bfd_put_32 (abfd, LD_R0_0R1 + 16, p);
6103 p = p + 4;
6104 p = restgpr0 (abfd, p, r);
6105 bfd_put_32 (abfd, MTLR_R0, p);
6106 p = p + 4;
6107 if (r == 29)
6108 {
6109 p = restgpr0 (abfd, p, 30);
6110 p = restgpr0 (abfd, p, 31);
6111 }
6112 bfd_put_32 (abfd, BLR, p);
6113 return p + 4;
6114}
6115
6116static bfd_byte *
6117savegpr1 (bfd *abfd, bfd_byte *p, int r)
6118{
6119 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6120 return p + 4;
6121}
6122
6123static bfd_byte *
6124savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6125{
6126 p = savegpr1 (abfd, p, r);
6127 bfd_put_32 (abfd, BLR, p);
6128 return p + 4;
6129}
6130
6131static bfd_byte *
6132restgpr1 (bfd *abfd, bfd_byte *p, int r)
6133{
6134 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6135 return p + 4;
6136}
6137
6138static bfd_byte *
6139restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6140{
6141 p = restgpr1 (abfd, p, r);
6142 bfd_put_32 (abfd, BLR, p);
6143 return p + 4;
6144}
6145
6146static bfd_byte *
6147savefpr (bfd *abfd, bfd_byte *p, int r)
6148{
6149 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6150 return p + 4;
6151}
6152
6153static bfd_byte *
6154savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6155{
6156 p = savefpr (abfd, p, r);
6157 bfd_put_32 (abfd, STD_R0_0R1 + 16, p);
6158 p = p + 4;
6159 bfd_put_32 (abfd, BLR, p);
6160 return p + 4;
6161}
6162
6163static bfd_byte *
6164restfpr (bfd *abfd, bfd_byte *p, int r)
6165{
6166 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6167 return p + 4;
6168}
6169
6170static bfd_byte *
6171restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6172{
6173 bfd_put_32 (abfd, LD_R0_0R1 + 16, p);
6174 p = p + 4;
6175 p = restfpr (abfd, p, r);
6176 bfd_put_32 (abfd, MTLR_R0, p);
6177 p = p + 4;
6178 if (r == 29)
6179 {
6180 p = restfpr (abfd, p, 30);
6181 p = restfpr (abfd, p, 31);
6182 }
6183 bfd_put_32 (abfd, BLR, p);
6184 return p + 4;
6185}
6186
6187static bfd_byte *
6188savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6189{
6190 p = savefpr (abfd, p, r);
6191 bfd_put_32 (abfd, BLR, p);
6192 return p + 4;
6193}
6194
6195static bfd_byte *
6196restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6197{
6198 p = restfpr (abfd, p, r);
6199 bfd_put_32 (abfd, BLR, p);
6200 return p + 4;
6201}
6202
6203static bfd_byte *
6204savevr (bfd *abfd, bfd_byte *p, int r)
6205{
6206 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6207 p = p + 4;
6208 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6209 return p + 4;
6210}
6211
6212static bfd_byte *
6213savevr_tail (bfd *abfd, bfd_byte *p, int r)
6214{
6215 p = savevr (abfd, p, r);
6216 bfd_put_32 (abfd, BLR, p);
6217 return p + 4;
6218}
6219
6220static bfd_byte *
6221restvr (bfd *abfd, bfd_byte *p, int r)
6222{
6223 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6224 p = p + 4;
6225 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6226 return p + 4;
6227}
6228
6229static bfd_byte *
6230restvr_tail (bfd *abfd, bfd_byte *p, int r)
6231{
6232 p = restvr (abfd, p, r);
6233 bfd_put_32 (abfd, BLR, p);
6234 return p + 4;
6235}
6236
e86ce104
AM
6237/* Called via elf_link_hash_traverse to transfer dynamic linking
6238 information on function code symbol entries to their corresponding
6239 function descriptor symbol entries. */
deb0e272 6240
b34976b6 6241static bfd_boolean
4ce794b7 6242func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
5bd4f169 6243{
e86ce104 6244 struct bfd_link_info *info;
65f38f15 6245 struct ppc_link_hash_table *htab;
411e1bfb 6246 struct plt_entry *ent;
50bc7936
AM
6247 struct ppc_link_hash_entry *fh;
6248 struct ppc_link_hash_entry *fdh;
6249 bfd_boolean force_local;
5bd4f169 6250
50bc7936
AM
6251 fh = (struct ppc_link_hash_entry *) h;
6252 if (fh->elf.root.type == bfd_link_hash_indirect)
b34976b6 6253 return TRUE;
e86ce104 6254
4ce794b7 6255 info = inf;
65f38f15 6256 htab = ppc_hash_table (info);
4dfe6ac6
NC
6257 if (htab == NULL)
6258 return FALSE;
5bd4f169 6259
c09bdfe5
AM
6260 /* Resolve undefined references to dot-symbols as the value
6261 in the function descriptor, if we have one in a regular object.
6262 This is to satisfy cases like ".quad .foo". Calls to functions
6263 in dynamic objects are handled elsewhere. */
6264 if (fh->elf.root.type == bfd_link_hash_undefweak
6265 && fh->was_undefined
b31867b6
AM
6266 && (fdh = defined_func_desc (fh)) != NULL
6267 && get_opd_info (fdh->elf.root.u.def.section) != NULL
6268 && opd_entry_value (fdh->elf.root.u.def.section,
6269 fdh->elf.root.u.def.value,
c09bdfe5
AM
6270 &fh->elf.root.u.def.section,
6271 &fh->elf.root.u.def.value) != (bfd_vma) -1)
6272 {
b31867b6 6273 fh->elf.root.type = fdh->elf.root.type;
f5385ebf 6274 fh->elf.forced_local = 1;
b31867b6
AM
6275 fh->elf.def_regular = fdh->elf.def_regular;
6276 fh->elf.def_dynamic = fdh->elf.def_dynamic;
c09bdfe5
AM
6277 }
6278
e86ce104
AM
6279 /* If this is a function code symbol, transfer dynamic linking
6280 information to the function descriptor symbol. */
50bc7936 6281 if (!fh->is_func)
b34976b6 6282 return TRUE;
e86ce104 6283
50bc7936 6284 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
411e1bfb
AM
6285 if (ent->plt.refcount > 0)
6286 break;
50bc7936
AM
6287 if (ent == NULL
6288 || fh->elf.root.root.string[0] != '.'
6289 || fh->elf.root.root.string[1] == '\0')
6290 return TRUE;
5bd4f169 6291
50bc7936
AM
6292 /* Find the corresponding function descriptor symbol. Create it
6293 as undefined if necessary. */
5bd4f169 6294
b31867b6 6295 fdh = lookup_fdh (fh, htab);
50bc7936 6296 if (fdh == NULL
df131623 6297 && !info->executable
50bc7936
AM
6298 && (fh->elf.root.type == bfd_link_hash_undefined
6299 || fh->elf.root.type == bfd_link_hash_undefweak))
6300 {
908b32fc 6301 fdh = make_fdh (info, fh);
bb700d78
AM
6302 if (fdh == NULL)
6303 return FALSE;
50bc7936 6304 }
648cca2c 6305
908b32fc 6306 /* Fake function descriptors are made undefweak. If the function
433817dd
AM
6307 code symbol is strong undefined, make the fake sym the same.
6308 If the function code symbol is defined, then force the fake
6309 descriptor local; We can't support overriding of symbols in a
6310 shared library on a fake descriptor. */
908b32fc
AM
6311
6312 if (fdh != NULL
6313 && fdh->fake
433817dd 6314 && fdh->elf.root.type == bfd_link_hash_undefweak)
908b32fc 6315 {
433817dd
AM
6316 if (fh->elf.root.type == bfd_link_hash_undefined)
6317 {
6318 fdh->elf.root.type = bfd_link_hash_undefined;
6319 bfd_link_add_undef (&htab->elf.root, &fdh->elf.root);
6320 }
6321 else if (fh->elf.root.type == bfd_link_hash_defined
6322 || fh->elf.root.type == bfd_link_hash_defweak)
6323 {
6324 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
6325 }
908b32fc
AM
6326 }
6327
50bc7936 6328 if (fdh != NULL
f5385ebf 6329 && !fdh->elf.forced_local
df131623 6330 && (!info->executable
f5385ebf
AM
6331 || fdh->elf.def_dynamic
6332 || fdh->elf.ref_dynamic
50bc7936
AM
6333 || (fdh->elf.root.type == bfd_link_hash_undefweak
6334 && ELF_ST_VISIBILITY (fdh->elf.other) == STV_DEFAULT)))
6335 {
6336 if (fdh->elf.dynindx == -1)
c152c796 6337 if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
50bc7936 6338 return FALSE;
f5385ebf
AM
6339 fdh->elf.ref_regular |= fh->elf.ref_regular;
6340 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6341 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6342 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
50bc7936 6343 if (ELF_ST_VISIBILITY (fh->elf.other) == STV_DEFAULT)
e86ce104 6344 {
40d16e0b 6345 move_plt_plist (fh, fdh);
f5385ebf 6346 fdh->elf.needs_plt = 1;
e86ce104 6347 }
50bc7936 6348 fdh->is_func_descriptor = 1;
34814b9f
AM
6349 fdh->oh = fh;
6350 fh->oh = fdh;
e86ce104
AM
6351 }
6352
50bc7936
AM
6353 /* Now that the info is on the function descriptor, clear the
6354 function code sym info. Any function code syms for which we
6355 don't have a definition in a regular file, we force local.
6356 This prevents a shared library from exporting syms that have
6357 been imported from another library. Function code syms that
6358 are really in the library we must leave global to prevent the
6359 linker dragging in a definition from a static library. */
93f3fa99
AM
6360 force_local = (!fh->elf.def_regular
6361 || fdh == NULL
6362 || !fdh->elf.def_regular
6363 || fdh->elf.forced_local);
50bc7936
AM
6364 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6365
b34976b6 6366 return TRUE;
e86ce104 6367}
40b8271b 6368
e86ce104 6369/* Called near the start of bfd_elf_size_dynamic_sections. We use
82bd7b59
AM
6370 this hook to a) provide some gcc support functions, and b) transfer
6371 dynamic linking information gathered so far on function code symbol
6372 entries, to their corresponding function descriptor symbol entries. */
deb0e272 6373
b34976b6 6374static bfd_boolean
4ce794b7
AM
6375ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
6376 struct bfd_link_info *info)
e86ce104
AM
6377{
6378 struct ppc_link_hash_table *htab;
82bd7b59 6379 unsigned int i;
deb0e272
AM
6380 const struct sfpr_def_parms funcs[] =
6381 {
6382 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6383 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6384 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6385 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
6386 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
6387 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
6388 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
6389 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
6390 { "._savef", 14, 31, savefpr, savefpr1_tail },
6391 { "._restf", 14, 31, restfpr, restfpr1_tail },
6392 { "_savevr_", 20, 31, savevr, savevr_tail },
6393 { "_restvr_", 20, 31, restvr, restvr_tail }
6394 };
e86ce104
AM
6395
6396 htab = ppc_hash_table (info);
4dfe6ac6
NC
6397 if (htab == NULL)
6398 return FALSE;
6399
82bd7b59
AM
6400 if (htab->sfpr == NULL)
6401 /* We don't have any relocs. */
b34976b6 6402 return TRUE;
82bd7b59 6403
deb0e272
AM
6404 /* Provide any missing _save* and _rest* functions. */
6405 htab->sfpr->size = 0;
6406 for (i = 0; i < sizeof (funcs) / sizeof (funcs[0]); i++)
6407 if (!sfpr_define (info, &funcs[i]))
6408 return FALSE;
82bd7b59 6409
4ce794b7 6410 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
805fc799 6411
eea6121a 6412 if (htab->sfpr->size == 0)
8423293d 6413 htab->sfpr->flags |= SEC_EXCLUDE;
82bd7b59 6414
b34976b6 6415 return TRUE;
e86ce104
AM
6416}
6417
6418/* Adjust a symbol defined by a dynamic object and referenced by a
6419 regular object. The current definition is in some section of the
6420 dynamic object, but we're not including those sections. We have to
6421 change the definition to something the rest of the link can
6422 understand. */
6423
b34976b6 6424static bfd_boolean
4ce794b7
AM
6425ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
6426 struct elf_link_hash_entry *h)
e86ce104
AM
6427{
6428 struct ppc_link_hash_table *htab;
e86ce104 6429 asection *s;
e86ce104
AM
6430
6431 htab = ppc_hash_table (info);
4dfe6ac6
NC
6432 if (htab == NULL)
6433 return FALSE;
e86ce104
AM
6434
6435 /* Deal with function syms. */
6436 if (h->type == STT_FUNC
e054468f 6437 || h->type == STT_GNU_IFUNC
f5385ebf 6438 || h->needs_plt)
e86ce104
AM
6439 {
6440 /* Clear procedure linkage table information for any symbol that
6441 won't need a .plt entry. */
411e1bfb
AM
6442 struct plt_entry *ent;
6443 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6444 if (ent->plt.refcount > 0)
6445 break;
8387904d 6446 if (ent == NULL
e054468f
AM
6447 || (h->type != STT_GNU_IFUNC
6448 && (SYMBOL_CALLS_LOCAL (info, h)
6449 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
6450 && h->root.type == bfd_link_hash_undefweak))))
40b8271b 6451 {
411e1bfb 6452 h->plt.plist = NULL;
f5385ebf 6453 h->needs_plt = 0;
40b8271b 6454 }
5bd4f169 6455 }
bbd7ec4a 6456 else
411e1bfb 6457 h->plt.plist = NULL;
5bd4f169
AM
6458
6459 /* If this is a weak symbol, and there is a real definition, the
6460 processor independent code will have arranged for us to see the
6461 real definition first, and we can just use the same value. */
f6e332e6 6462 if (h->u.weakdef != NULL)
5bd4f169 6463 {
f6e332e6
AM
6464 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
6465 || h->u.weakdef->root.type == bfd_link_hash_defweak);
6466 h->root.u.def.section = h->u.weakdef->root.u.def.section;
6467 h->root.u.def.value = h->u.weakdef->root.u.def.value;
a23b6845 6468 if (ELIMINATE_COPY_RELOCS)
f6e332e6 6469 h->non_got_ref = h->u.weakdef->non_got_ref;
b34976b6 6470 return TRUE;
5bd4f169
AM
6471 }
6472
5bd4f169
AM
6473 /* If we are creating a shared library, we must presume that the
6474 only references to the symbol are via the global offset table.
6475 For such cases we need not do anything here; the relocations will
6476 be handled correctly by relocate_section. */
6477 if (info->shared)
b34976b6 6478 return TRUE;
5bd4f169 6479
65f38f15
AM
6480 /* If there are no references to this symbol that do not use the
6481 GOT, we don't need to generate a copy reloc. */
f5385ebf 6482 if (!h->non_got_ref)
b34976b6 6483 return TRUE;
65f38f15 6484
b186458a
JJ
6485 /* Don't generate a copy reloc for symbols defined in the executable. */
6486 if (!h->def_dynamic || !h->ref_regular || h->def_regular)
6487 return TRUE;
6488
f4656909 6489 if (ELIMINATE_COPY_RELOCS)
65f38f15 6490 {
f4656909 6491 struct ppc_link_hash_entry * eh;
6061a67d 6492 struct elf_dyn_relocs *p;
65f38f15 6493
f4656909
AM
6494 eh = (struct ppc_link_hash_entry *) h;
6495 for (p = eh->dyn_relocs; p != NULL; p = p->next)
6496 {
6497 s = p->sec->output_section;
6498 if (s != NULL && (s->flags & SEC_READONLY) != 0)
6499 break;
6500 }
6501
6502 /* If we didn't find any dynamic relocs in read-only sections, then
6503 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
6504 if (p == NULL)
6505 {
f5385ebf 6506 h->non_got_ref = 0;
f4656909
AM
6507 return TRUE;
6508 }
65f38f15
AM
6509 }
6510
5d35169e 6511 if (h->plt.plist != NULL)
97b639ba
AM
6512 {
6513 /* We should never get here, but unfortunately there are versions
6514 of gcc out there that improperly (for this ABI) put initialized
6515 function pointers, vtable refs and suchlike in read-only
6516 sections. Allow them to proceed, but warn that this might
6517 break at runtime. */
25f53a85 6518 info->callbacks->einfo
8de848d8 6519 (_("%P: copy reloc against `%s' requires lazy plt linking; "
25f53a85 6520 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
97b639ba
AM
6521 h->root.root.string);
6522 }
5d35169e
AM
6523
6524 /* This is a reference to a symbol defined by a dynamic object which
6525 is not a function. */
6526
909272ee
AM
6527 if (h->size == 0)
6528 {
8de848d8 6529 info->callbacks->einfo (_("%P: dynamic variable `%s' is zero size\n"),
25f53a85 6530 h->root.root.string);
909272ee
AM
6531 return TRUE;
6532 }
6533
5bd4f169
AM
6534 /* We must allocate the symbol in our .dynbss section, which will
6535 become part of the .bss section of the executable. There will be
6536 an entry for this symbol in the .dynsym section. The dynamic
6537 object will contain position independent code, so all references
6538 from the dynamic object to this symbol will go through the global
6539 offset table. The dynamic linker will use the .dynsym entry to
6540 determine the address it must put in the global offset table, so
6541 both the dynamic object and the regular object will refer to the
6542 same memory location for the variable. */
5bd4f169 6543
04c9666a
AM
6544 /* We must generate a R_PPC64_COPY reloc to tell the dynamic linker
6545 to copy the initial value out of the dynamic object and into the
5bd4f169
AM
6546 runtime process image. We need to remember the offset into the
6547 .rela.bss section we are going to use. */
6548 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
6549 {
eea6121a 6550 htab->relbss->size += sizeof (Elf64_External_Rela);
f5385ebf 6551 h->needs_copy = 1;
5bd4f169
AM
6552 }
6553
4ce794b7 6554 s = htab->dynbss;
5bd4f169 6555
027297b7 6556 return _bfd_elf_adjust_dynamic_copy (h, s);
5bd4f169
AM
6557}
6558
e86ce104
AM
6559/* If given a function descriptor symbol, hide both the function code
6560 sym and the descriptor. */
6561static void
4ce794b7
AM
6562ppc64_elf_hide_symbol (struct bfd_link_info *info,
6563 struct elf_link_hash_entry *h,
6564 bfd_boolean force_local)
e86ce104 6565{
34814b9f 6566 struct ppc_link_hash_entry *eh;
e86ce104
AM
6567 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
6568
34814b9f
AM
6569 eh = (struct ppc_link_hash_entry *) h;
6570 if (eh->is_func_descriptor)
e86ce104 6571 {
34814b9f 6572 struct ppc_link_hash_entry *fh = eh->oh;
e86ce104 6573
721956f4 6574 if (fh == NULL)
d1329ca3
AM
6575 {
6576 const char *p, *q;
6577 struct ppc_link_hash_table *htab;
6578 char save;
6579
6580 /* We aren't supposed to use alloca in BFD because on
6581 systems which do not have alloca the version in libiberty
6582 calls xmalloc, which might cause the program to crash
6583 when it runs out of memory. This function doesn't have a
6584 return status, so there's no way to gracefully return an
6585 error. So cheat. We know that string[-1] can be safely
34814b9f
AM
6586 accessed; It's either a string in an ELF string table,
6587 or allocated in an objalloc structure. */
d1329ca3 6588
34814b9f 6589 p = eh->elf.root.root.string - 1;
d1329ca3
AM
6590 save = *p;
6591 *(char *) p = '.';
6592 htab = ppc_hash_table (info);
4dfe6ac6
NC
6593 if (htab == NULL)
6594 return;
6595
34814b9f
AM
6596 fh = (struct ppc_link_hash_entry *)
6597 elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
d1329ca3
AM
6598 *(char *) p = save;
6599
6600 /* Unfortunately, if it so happens that the string we were
6601 looking for was allocated immediately before this string,
6602 then we overwrote the string terminator. That's the only
6603 reason the lookup should fail. */
6604 if (fh == NULL)
6605 {
34814b9f
AM
6606 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
6607 while (q >= eh->elf.root.root.string && *q == *p)
d1329ca3 6608 --q, --p;
34814b9f
AM
6609 if (q < eh->elf.root.root.string && *p == '.')
6610 fh = (struct ppc_link_hash_entry *)
6611 elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
d1329ca3
AM
6612 }
6613 if (fh != NULL)
6614 {
34814b9f
AM
6615 eh->oh = fh;
6616 fh->oh = eh;
d1329ca3
AM
6617 }
6618 }
e86ce104 6619 if (fh != NULL)
34814b9f 6620 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
e86ce104
AM
6621 }
6622}
6623
411e1bfb 6624static bfd_boolean
8843416a
AM
6625get_sym_h (struct elf_link_hash_entry **hp,
6626 Elf_Internal_Sym **symp,
6627 asection **symsecp,
f961d9dd 6628 unsigned char **tls_maskp,
8843416a
AM
6629 Elf_Internal_Sym **locsymsp,
6630 unsigned long r_symndx,
6631 bfd *ibfd)
411e1bfb 6632{
0ffa91dd 6633 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
411e1bfb
AM
6634
6635 if (r_symndx >= symtab_hdr->sh_info)
6636 {
6637 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
6638 struct elf_link_hash_entry *h;
6639
6640 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 6641 h = elf_follow_link (h);
411e1bfb
AM
6642
6643 if (hp != NULL)
6644 *hp = h;
6645
6646 if (symp != NULL)
6647 *symp = NULL;
6648
6649 if (symsecp != NULL)
6650 {
6651 asection *symsec = NULL;
6652 if (h->root.type == bfd_link_hash_defined
6653 || h->root.type == bfd_link_hash_defweak)
6654 symsec = h->root.u.def.section;
6655 *symsecp = symsec;
6656 }
6657
e7b938ca 6658 if (tls_maskp != NULL)
411e1bfb
AM
6659 {
6660 struct ppc_link_hash_entry *eh;
6661
6662 eh = (struct ppc_link_hash_entry *) h;
e7b938ca 6663 *tls_maskp = &eh->tls_mask;
411e1bfb
AM
6664 }
6665 }
6666 else
6667 {
6668 Elf_Internal_Sym *sym;
6669 Elf_Internal_Sym *locsyms = *locsymsp;
6670
6671 if (locsyms == NULL)
6672 {
6673 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
6674 if (locsyms == NULL)
6675 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
6676 symtab_hdr->sh_info,
6677 0, NULL, NULL, NULL);
6678 if (locsyms == NULL)
6679 return FALSE;
6680 *locsymsp = locsyms;
6681 }
6682 sym = locsyms + r_symndx;
6683
6684 if (hp != NULL)
6685 *hp = NULL;
6686
6687 if (symp != NULL)
6688 *symp = sym;
6689
6690 if (symsecp != NULL)
cb33740c 6691 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
411e1bfb 6692
e7b938ca 6693 if (tls_maskp != NULL)
411e1bfb
AM
6694 {
6695 struct got_entry **lgot_ents;
f961d9dd 6696 unsigned char *tls_mask;
411e1bfb 6697
e7b938ca 6698 tls_mask = NULL;
411e1bfb
AM
6699 lgot_ents = elf_local_got_ents (ibfd);
6700 if (lgot_ents != NULL)
6701 {
e054468f
AM
6702 struct plt_entry **local_plt = (struct plt_entry **)
6703 (lgot_ents + symtab_hdr->sh_info);
f961d9dd 6704 unsigned char *lgot_masks = (unsigned char *)
e054468f 6705 (local_plt + symtab_hdr->sh_info);
e7b938ca 6706 tls_mask = &lgot_masks[r_symndx];
411e1bfb 6707 }
e7b938ca 6708 *tls_maskp = tls_mask;
411e1bfb
AM
6709 }
6710 }
6711 return TRUE;
6712}
6713
e7b938ca 6714/* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
951fd09b 6715 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
ad8e1ba5 6716 type suitable for optimization, and 1 otherwise. */
951fd09b
AM
6717
6718static int
f961d9dd 6719get_tls_mask (unsigned char **tls_maskp,
3a71aa26
AM
6720 unsigned long *toc_symndx,
6721 bfd_vma *toc_addend,
0d4792f7 6722 Elf_Internal_Sym **locsymsp,
3a71aa26
AM
6723 const Elf_Internal_Rela *rel,
6724 bfd *ibfd)
411e1bfb
AM
6725{
6726 unsigned long r_symndx;
0d4792f7 6727 int next_r;
411e1bfb
AM
6728 struct elf_link_hash_entry *h;
6729 Elf_Internal_Sym *sym;
6730 asection *sec;
6731 bfd_vma off;
6732
6733 r_symndx = ELF64_R_SYM (rel->r_info);
e7b938ca 6734 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
951fd09b 6735 return 0;
411e1bfb 6736
e7b938ca 6737 if ((*tls_maskp != NULL && **tls_maskp != 0)
411e1bfb 6738 || sec == NULL
6bee8834 6739 || ppc64_elf_section_data (sec) == NULL
7c8fe5c4 6740 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
951fd09b 6741 return 1;
411e1bfb
AM
6742
6743 /* Look inside a TOC section too. */
6744 if (h != NULL)
6745 {
6746 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
6747 off = h->root.u.def.value;
6748 }
6749 else
6750 off = sym->st_value;
6751 off += rel->r_addend;
6752 BFD_ASSERT (off % 8 == 0);
3a71aa26
AM
6753 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
6754 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
0d4792f7
AM
6755 if (toc_symndx != NULL)
6756 *toc_symndx = r_symndx;
3a71aa26
AM
6757 if (toc_addend != NULL)
6758 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
6759 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6760 return 0;
854b41e7 6761 if ((h == NULL || is_static_defined (h))
0d4792f7
AM
6762 && (next_r == -1 || next_r == -2))
6763 return 1 - next_r;
951fd09b 6764 return 1;
411e1bfb
AM
6765}
6766
3b421ab3
AM
6767/* Find (or create) an entry in the tocsave hash table. */
6768
6769static struct tocsave_entry *
6770tocsave_find (struct ppc_link_hash_table *htab,
6771 enum insert_option insert,
6772 Elf_Internal_Sym **local_syms,
6773 const Elf_Internal_Rela *irela,
6774 bfd *ibfd)
6775{
6776 unsigned long r_indx;
6777 struct elf_link_hash_entry *h;
6778 Elf_Internal_Sym *sym;
6779 struct tocsave_entry ent, *p;
6780 hashval_t hash;
6781 struct tocsave_entry **slot;
6782
6783 r_indx = ELF64_R_SYM (irela->r_info);
6784 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
6785 return NULL;
6786 if (ent.sec == NULL || ent.sec->output_section == NULL)
6787 {
6788 (*_bfd_error_handler)
6789 (_("%B: undefined symbol on R_PPC64_TOCSAVE relocation"));
6790 return NULL;
6791 }
6792
6793 if (h != NULL)
6794 ent.offset = h->root.u.def.value;
6795 else
6796 ent.offset = sym->st_value;
6797 ent.offset += irela->r_addend;
6798
6799 hash = tocsave_htab_hash (&ent);
6800 slot = ((struct tocsave_entry **)
6801 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
6802 if (slot == NULL)
6803 return NULL;
6804
6805 if (*slot == NULL)
6806 {
6807 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
6808 if (p == NULL)
6809 return NULL;
6810 *p = ent;
6811 *slot = p;
6812 }
6813 return *slot;
6814}
6815
754021d0 6816/* Adjust all global syms defined in opd sections. In gcc generated
8387904d 6817 code for the old ABI, these will already have been done. */
754021d0
AM
6818
6819static bfd_boolean
6820adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
6821{
6822 struct ppc_link_hash_entry *eh;
6823 asection *sym_sec;
74f0fb50 6824 struct _opd_sec_data *opd;
754021d0
AM
6825
6826 if (h->root.type == bfd_link_hash_indirect)
6827 return TRUE;
6828
754021d0
AM
6829 if (h->root.type != bfd_link_hash_defined
6830 && h->root.type != bfd_link_hash_defweak)
6831 return TRUE;
6832
6833 eh = (struct ppc_link_hash_entry *) h;
6834 if (eh->adjust_done)
6835 return TRUE;
6836
6837 sym_sec = eh->elf.root.u.def.section;
74f0fb50
AM
6838 opd = get_opd_info (sym_sec);
6839 if (opd != NULL && opd->adjust != NULL)
754021d0 6840 {
74f0fb50 6841 long adjust = opd->adjust[eh->elf.root.u.def.value / 8];
4025353c
AM
6842 if (adjust == -1)
6843 {
6844 /* This entry has been deleted. */
b3fac117 6845 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
81688140
AM
6846 if (dsec == NULL)
6847 {
6848 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
6849 if (elf_discarded_section (dsec))
6850 {
b3fac117 6851 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
81688140
AM
6852 break;
6853 }
6854 }
4025353c 6855 eh->elf.root.u.def.value = 0;
81688140 6856 eh->elf.root.u.def.section = dsec;
4025353c
AM
6857 }
6858 else
6859 eh->elf.root.u.def.value += adjust;
754021d0
AM
6860 eh->adjust_done = 1;
6861 }
6862 return TRUE;
6863}
6864
8c1d1bb8
AM
6865/* Handles decrementing dynamic reloc counts for the reloc specified by
6866 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM_SEC
6867 have already been determined. */
6868
6869static bfd_boolean
6870dec_dynrel_count (bfd_vma r_info,
6871 asection *sec,
6872 struct bfd_link_info *info,
6873 Elf_Internal_Sym **local_syms,
6874 struct elf_link_hash_entry *h,
6875 asection *sym_sec)
6876{
6877 enum elf_ppc64_reloc_type r_type;
6061a67d
AM
6878 struct elf_dyn_relocs *p;
6879 struct elf_dyn_relocs **pp;
8c1d1bb8
AM
6880
6881 /* Can this reloc be dynamic? This switch, and later tests here
6882 should be kept in sync with the code in check_relocs. */
6883 r_type = ELF64_R_TYPE (r_info);
6884 switch (r_type)
6885 {
6886 default:
6887 return TRUE;
6888
6889 case R_PPC64_TPREL16:
6890 case R_PPC64_TPREL16_LO:
6891 case R_PPC64_TPREL16_HI:
6892 case R_PPC64_TPREL16_HA:
6893 case R_PPC64_TPREL16_DS:
6894 case R_PPC64_TPREL16_LO_DS:
6895 case R_PPC64_TPREL16_HIGHER:
6896 case R_PPC64_TPREL16_HIGHERA:
6897 case R_PPC64_TPREL16_HIGHEST:
6898 case R_PPC64_TPREL16_HIGHESTA:
6899 if (!info->shared)
6900 return TRUE;
6901
6902 case R_PPC64_TPREL64:
6903 case R_PPC64_DTPMOD64:
6904 case R_PPC64_DTPREL64:
6905 case R_PPC64_ADDR64:
6906 case R_PPC64_REL30:
6907 case R_PPC64_REL32:
6908 case R_PPC64_REL64:
6909 case R_PPC64_ADDR14:
6910 case R_PPC64_ADDR14_BRNTAKEN:
6911 case R_PPC64_ADDR14_BRTAKEN:
6912 case R_PPC64_ADDR16:
6913 case R_PPC64_ADDR16_DS:
6914 case R_PPC64_ADDR16_HA:
6915 case R_PPC64_ADDR16_HI:
6916 case R_PPC64_ADDR16_HIGHER:
6917 case R_PPC64_ADDR16_HIGHERA:
6918 case R_PPC64_ADDR16_HIGHEST:
6919 case R_PPC64_ADDR16_HIGHESTA:
6920 case R_PPC64_ADDR16_LO:
6921 case R_PPC64_ADDR16_LO_DS:
6922 case R_PPC64_ADDR24:
6923 case R_PPC64_ADDR32:
6924 case R_PPC64_UADDR16:
6925 case R_PPC64_UADDR32:
6926 case R_PPC64_UADDR64:
6927 case R_PPC64_TOC:
6928 break;
6929 }
6930
6931 if (local_syms != NULL)
6932 {
6933 unsigned long r_symndx;
6934 Elf_Internal_Sym *sym;
6935 bfd *ibfd = sec->owner;
6936
6937 r_symndx = ELF64_R_SYM (r_info);
6938 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
6939 return FALSE;
6940 }
6941
6942 if ((info->shared
1d483afe 6943 && (must_be_dyn_reloc (info, r_type)
8c1d1bb8
AM
6944 || (h != NULL
6945 && (!info->symbolic
6946 || h->root.type == bfd_link_hash_defweak
6947 || !h->def_regular))))
6948 || (ELIMINATE_COPY_RELOCS
6949 && !info->shared
6950 && h != NULL
6951 && (h->root.type == bfd_link_hash_defweak
6952 || !h->def_regular)))
6953 ;
6954 else
6955 return TRUE;
6956
6957 if (h != NULL)
6958 pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
8c1d1bb8 6959 else
6edfbbad 6960 {
60124e18
AM
6961 if (sym_sec != NULL)
6962 {
6963 void *vpp = &elf_section_data (sym_sec)->local_dynrel;
6061a67d 6964 pp = (struct elf_dyn_relocs **) vpp;
60124e18
AM
6965 }
6966 else
6967 {
6968 void *vpp = &elf_section_data (sec)->local_dynrel;
6061a67d 6969 pp = (struct elf_dyn_relocs **) vpp;
60124e18
AM
6970 }
6971
6972 /* elf_gc_sweep may have already removed all dyn relocs associated
6973 with local syms for a given section. Don't report a dynreloc
6974 miscount. */
6975 if (*pp == NULL)
6976 return TRUE;
6edfbbad 6977 }
8c1d1bb8
AM
6978
6979 while ((p = *pp) != NULL)
6980 {
6981 if (p->sec == sec)
6982 {
1d483afe 6983 if (!must_be_dyn_reloc (info, r_type))
8c1d1bb8
AM
6984 p->pc_count -= 1;
6985 p->count -= 1;
6986 if (p->count == 0)
6987 *pp = p->next;
6988 return TRUE;
6989 }
6990 pp = &p->next;
6991 }
6992
8de848d8 6993 info->callbacks->einfo (_("%P: dynreloc miscount for %B, section %A\n"),
25f53a85 6994 sec->owner, sec);
8c1d1bb8
AM
6995 bfd_set_error (bfd_error_bad_value);
6996 return FALSE;
6997}
6998
754021d0
AM
6999/* Remove unused Official Procedure Descriptor entries. Currently we
7000 only remove those associated with functions in discarded link-once
7001 sections, or weakly defined functions that have been overridden. It
7002 would be possible to remove many more entries for statically linked
7003 applications. */
7004
b34976b6 7005bfd_boolean
33c0ec9d 7006ppc64_elf_edit_opd (struct bfd_link_info *info, bfd_boolean non_overlapping)
1e2f5b6e
AM
7007{
7008 bfd *ibfd;
754021d0 7009 bfd_boolean some_edited = FALSE;
3f764659 7010 asection *need_pad = NULL;
1e2f5b6e 7011
411e1bfb 7012 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
1e2f5b6e
AM
7013 {
7014 asection *sec;
7015 Elf_Internal_Rela *relstart, *rel, *relend;
7016 Elf_Internal_Shdr *symtab_hdr;
6cdc0ccc 7017 Elf_Internal_Sym *local_syms;
1e2f5b6e 7018 bfd_vma offset;
74f0fb50 7019 struct _opd_sec_data *opd;
3f764659
JJ
7020 bfd_boolean need_edit, add_aux_fields;
7021 bfd_size_type cnt_16b = 0;
1e2f5b6e 7022
854b41e7
AM
7023 if (!is_ppc64_elf (ibfd))
7024 continue;
7025
1e2f5b6e 7026 sec = bfd_get_section_by_name (ibfd, ".opd");
46de2a7c 7027 if (sec == NULL || sec->size == 0)
1e2f5b6e
AM
7028 continue;
7029
4b85d634
AM
7030 if (sec->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
7031 continue;
7032
1e2f5b6e
AM
7033 if (sec->output_section == bfd_abs_section_ptr)
7034 continue;
7035
7036 /* Look through the section relocs. */
7037 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7038 continue;
7039
6cdc0ccc 7040 local_syms = NULL;
0ffa91dd 7041 symtab_hdr = &elf_symtab_hdr (ibfd);
1e2f5b6e
AM
7042
7043 /* Read the relocations. */
4ce794b7 7044 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
45d6a902 7045 info->keep_memory);
1e2f5b6e 7046 if (relstart == NULL)
b34976b6 7047 return FALSE;
1e2f5b6e
AM
7048
7049 /* First run through the relocs to check they are sane, and to
7050 determine whether we need to edit this opd section. */
b34976b6 7051 need_edit = FALSE;
3f764659 7052 need_pad = sec;
1e2f5b6e
AM
7053 offset = 0;
7054 relend = relstart + sec->reloc_count;
50bc7936 7055 for (rel = relstart; rel < relend; )
1e2f5b6e 7056 {
04c9666a 7057 enum elf_ppc64_reloc_type r_type;
1e2f5b6e
AM
7058 unsigned long r_symndx;
7059 asection *sym_sec;
7060 struct elf_link_hash_entry *h;
7061 Elf_Internal_Sym *sym;
7062
3f764659 7063 /* .opd contains a regular array of 16 or 24 byte entries. We're
1e2f5b6e
AM
7064 only interested in the reloc pointing to a function entry
7065 point. */
50bc7936
AM
7066 if (rel->r_offset != offset
7067 || rel + 1 >= relend
7068 || (rel + 1)->r_offset != offset + 8)
1e2f5b6e
AM
7069 {
7070 /* If someone messes with .opd alignment then after a
7071 "ld -r" we might have padding in the middle of .opd.
7072 Also, there's nothing to prevent someone putting
7073 something silly in .opd with the assembler. No .opd
b34976b6 7074 optimization for them! */
3f764659 7075 broken_opd:
1e2f5b6e 7076 (*_bfd_error_handler)
d003868e 7077 (_("%B: .opd is not a regular array of opd entries"), ibfd);
b34976b6 7078 need_edit = FALSE;
1e2f5b6e
AM
7079 break;
7080 }
7081
50bc7936
AM
7082 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7083 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7084 {
7085 (*_bfd_error_handler)
d003868e
AM
7086 (_("%B: unexpected reloc type %u in .opd section"),
7087 ibfd, r_type);
50bc7936
AM
7088 need_edit = FALSE;
7089 break;
7090 }
7091
1e2f5b6e 7092 r_symndx = ELF64_R_SYM (rel->r_info);
411e1bfb
AM
7093 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7094 r_symndx, ibfd))
50bc7936 7095 goto error_ret;
1e2f5b6e
AM
7096
7097 if (sym_sec == NULL || sym_sec->owner == NULL)
7098 {
411e1bfb
AM
7099 const char *sym_name;
7100 if (h != NULL)
7101 sym_name = h->root.root.string;
7102 else
26c61ae5
L
7103 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7104 sym_sec);
411e1bfb 7105
1e2f5b6e 7106 (*_bfd_error_handler)
d003868e
AM
7107 (_("%B: undefined sym `%s' in .opd section"),
7108 ibfd, sym_name);
b34976b6 7109 need_edit = FALSE;
1e2f5b6e
AM
7110 break;
7111 }
7112
51020317
AM
7113 /* opd entries are always for functions defined in the
7114 current input bfd. If the symbol isn't defined in the
7115 input bfd, then we won't be using the function in this
7116 bfd; It must be defined in a linkonce section in another
7117 bfd, or is weak. It's also possible that we are
7118 discarding the function due to a linker script /DISCARD/,
7119 which we test for via the output_section. */
7120 if (sym_sec->owner != ibfd
7121 || sym_sec->output_section == bfd_abs_section_ptr)
b34976b6 7122 need_edit = TRUE;
1e2f5b6e 7123
50bc7936 7124 rel += 2;
3f764659
JJ
7125 if (rel == relend
7126 || (rel + 1 == relend && rel->r_offset == offset + 16))
7127 {
7128 if (sec->size == offset + 24)
7129 {
7130 need_pad = NULL;
7131 break;
7132 }
7133 if (rel == relend && sec->size == offset + 16)
7134 {
7135 cnt_16b++;
7136 break;
7137 }
7138 goto broken_opd;
7139 }
7140
7141 if (rel->r_offset == offset + 24)
7142 offset += 24;
7143 else if (rel->r_offset != offset + 16)
7144 goto broken_opd;
7145 else if (rel + 1 < relend
7146 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7147 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7148 {
7149 offset += 16;
7150 cnt_16b++;
7151 }
7152 else if (rel + 2 < relend
7153 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_ADDR64
7154 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC)
7155 {
7156 offset += 24;
7157 rel += 1;
7158 }
7159 else
7160 goto broken_opd;
1e2f5b6e
AM
7161 }
7162
3f764659
JJ
7163 add_aux_fields = non_overlapping && cnt_16b > 0;
7164
7165 if (need_edit || add_aux_fields)
1e2f5b6e
AM
7166 {
7167 Elf_Internal_Rela *write_rel;
d4730f92 7168 Elf_Internal_Shdr *rel_hdr;
1e2f5b6e 7169 bfd_byte *rptr, *wptr;
983bddc8 7170 bfd_byte *new_contents;
b34976b6 7171 bfd_boolean skip;
3f764659 7172 long opd_ent_size;
74f0fb50
AM
7173 bfd_size_type amt;
7174
983bddc8 7175 new_contents = NULL;
74f0fb50
AM
7176 amt = sec->size * sizeof (long) / 8;
7177 opd = &ppc64_elf_section_data (sec)->u.opd;
33c0ec9d 7178 opd->adjust = bfd_zalloc (sec->owner, amt);
74f0fb50
AM
7179 if (opd->adjust == NULL)
7180 return FALSE;
7181 ppc64_elf_section_data (sec)->sec_type = sec_opd;
1e2f5b6e
AM
7182
7183 /* This seems a waste of time as input .opd sections are all
7184 zeros as generated by gcc, but I suppose there's no reason
7185 this will always be so. We might start putting something in
7186 the third word of .opd entries. */
7187 if ((sec->flags & SEC_IN_MEMORY) == 0)
7188 {
eea6121a
AM
7189 bfd_byte *loc;
7190 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
6cdc0ccc 7191 {
eea6121a
AM
7192 if (loc != NULL)
7193 free (loc);
50bc7936 7194 error_ret:
6cdc0ccc
AM
7195 if (local_syms != NULL
7196 && symtab_hdr->contents != (unsigned char *) local_syms)
7197 free (local_syms);
6cdc0ccc
AM
7198 if (elf_section_data (sec)->relocs != relstart)
7199 free (relstart);
b34976b6 7200 return FALSE;
6cdc0ccc 7201 }
1e2f5b6e
AM
7202 sec->contents = loc;
7203 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7204 }
7205
7206 elf_section_data (sec)->relocs = relstart;
7207
3f764659 7208 new_contents = sec->contents;
3f764659
JJ
7209 if (add_aux_fields)
7210 {
7211 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7212 if (new_contents == NULL)
7213 return FALSE;
7214 need_pad = FALSE;
3f764659 7215 }
b4f4e59f
AM
7216 wptr = new_contents;
7217 rptr = sec->contents;
3f764659 7218
1e2f5b6e 7219 write_rel = relstart;
b34976b6 7220 skip = FALSE;
1e2f5b6e 7221 offset = 0;
3f764659 7222 opd_ent_size = 0;
1e2f5b6e
AM
7223 for (rel = relstart; rel < relend; rel++)
7224 {
50bc7936
AM
7225 unsigned long r_symndx;
7226 asection *sym_sec;
7227 struct elf_link_hash_entry *h;
7228 Elf_Internal_Sym *sym;
7229
7230 r_symndx = ELF64_R_SYM (rel->r_info);
7231 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
d37c89e5 7232 r_symndx, ibfd))
50bc7936
AM
7233 goto error_ret;
7234
1e2f5b6e
AM
7235 if (rel->r_offset == offset)
7236 {
50bc7936 7237 struct ppc_link_hash_entry *fdh = NULL;
3f764659
JJ
7238
7239 /* See if the .opd entry is full 24 byte or
7240 16 byte (with fd_aux entry overlapped with next
7241 fd_func). */
7242 opd_ent_size = 24;
7243 if ((rel + 2 == relend && sec->size == offset + 16)
7244 || (rel + 3 < relend
7245 && rel[2].r_offset == offset + 16
7246 && rel[3].r_offset == offset + 24
7247 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_ADDR64
7248 && ELF64_R_TYPE (rel[3].r_info) == R_PPC64_TOC))
7249 opd_ent_size = 16;
7250
4025353c
AM
7251 if (h != NULL
7252 && h->root.root.string[0] == '.')
c4f68ce3 7253 {
4dfe6ac6
NC
7254 struct ppc_link_hash_table *htab;
7255
7256 htab = ppc_hash_table (info);
7257 if (htab != NULL)
7258 fdh = lookup_fdh ((struct ppc_link_hash_entry *) h,
7259 htab);
c4f68ce3
AM
7260 if (fdh != NULL
7261 && fdh->elf.root.type != bfd_link_hash_defined
7262 && fdh->elf.root.type != bfd_link_hash_defweak)
7263 fdh = NULL;
7264 }
1e2f5b6e 7265
51020317
AM
7266 skip = (sym_sec->owner != ibfd
7267 || sym_sec->output_section == bfd_abs_section_ptr);
a4aa0fb7
AM
7268 if (skip)
7269 {
4025353c 7270 if (fdh != NULL && sym_sec->owner == ibfd)
a4aa0fb7
AM
7271 {
7272 /* Arrange for the function descriptor sym
7273 to be dropped. */
d6fe2dc1
AM
7274 fdh->elf.root.u.def.value = 0;
7275 fdh->elf.root.u.def.section = sym_sec;
a4aa0fb7 7276 }
74f0fb50 7277 opd->adjust[rel->r_offset / 8] = -1;
a4aa0fb7
AM
7278 }
7279 else
1e2f5b6e
AM
7280 {
7281 /* We'll be keeping this opd entry. */
7282
4025353c 7283 if (fdh != NULL)
1e2f5b6e 7284 {
754021d0
AM
7285 /* Redefine the function descriptor symbol to
7286 this location in the opd section. It is
7287 necessary to update the value here rather
7288 than using an array of adjustments as we do
7289 for local symbols, because various places
7290 in the generic ELF code use the value
7291 stored in u.def.value. */
3f764659 7292 fdh->elf.root.u.def.value = wptr - new_contents;
754021d0 7293 fdh->adjust_done = 1;
1e2f5b6e 7294 }
754021d0
AM
7295
7296 /* Local syms are a bit tricky. We could
7297 tweak them as they can be cached, but
7298 we'd need to look through the local syms
7299 for the function descriptor sym which we
7300 don't have at the moment. So keep an
7301 array of adjustments. */
74f0fb50 7302 opd->adjust[rel->r_offset / 8]
3f764659 7303 = (wptr - new_contents) - (rptr - sec->contents);
1e2f5b6e
AM
7304
7305 if (wptr != rptr)
3f764659
JJ
7306 memcpy (wptr, rptr, opd_ent_size);
7307 wptr += opd_ent_size;
7308 if (add_aux_fields && opd_ent_size == 16)
7309 {
7310 memset (wptr, '\0', 8);
7311 wptr += 8;
7312 }
1e2f5b6e 7313 }
3f764659
JJ
7314 rptr += opd_ent_size;
7315 offset += opd_ent_size;
1e2f5b6e
AM
7316 }
7317
50bc7936
AM
7318 if (skip)
7319 {
60124e18
AM
7320 if (!NO_OPD_RELOCS
7321 && !info->relocatable
18d944df
AM
7322 && !dec_dynrel_count (rel->r_info, sec, info,
7323 NULL, h, sym_sec))
8c1d1bb8 7324 goto error_ret;
50bc7936
AM
7325 }
7326 else
1e2f5b6e 7327 {
50bc7936
AM
7328 /* We need to adjust any reloc offsets to point to the
7329 new opd entries. While we're at it, we may as well
7330 remove redundant relocs. */
74f0fb50 7331 rel->r_offset += opd->adjust[(offset - opd_ent_size) / 8];
1e2f5b6e
AM
7332 if (write_rel != rel)
7333 memcpy (write_rel, rel, sizeof (*rel));
7334 ++write_rel;
7335 }
7336 }
7337
3f764659 7338 sec->size = wptr - new_contents;
1e2f5b6e 7339 sec->reloc_count = write_rel - relstart;
3f764659
JJ
7340 if (add_aux_fields)
7341 {
7342 free (sec->contents);
7343 sec->contents = new_contents;
7344 }
7345
05bf9422 7346 /* Fudge the header size too, as this is used later in
cdcf6e38 7347 elf_bfd_final_link if we are emitting relocs. */
d4730f92
BS
7348 rel_hdr = _bfd_elf_single_rel_hdr (sec);
7349 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
754021d0 7350 some_edited = TRUE;
1e2f5b6e 7351 }
6cdc0ccc 7352 else if (elf_section_data (sec)->relocs != relstart)
1e2f5b6e 7353 free (relstart);
6cdc0ccc 7354
411e1bfb
AM
7355 if (local_syms != NULL
7356 && symtab_hdr->contents != (unsigned char *) local_syms)
7357 {
7358 if (!info->keep_memory)
7359 free (local_syms);
7360 else
7361 symtab_hdr->contents = (unsigned char *) local_syms;
7362 }
7363 }
7364
754021d0
AM
7365 if (some_edited)
7366 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
7367
3f764659
JJ
7368 /* If we are doing a final link and the last .opd entry is just 16 byte
7369 long, add a 8 byte padding after it. */
7370 if (need_pad != NULL && !info->relocatable)
7371 {
7372 bfd_byte *p;
7373
7374 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
7375 {
7376 BFD_ASSERT (need_pad->size > 0);
7377
7378 p = bfd_malloc (need_pad->size + 8);
7379 if (p == NULL)
7380 return FALSE;
699733f6 7381
3f764659
JJ
7382 if (! bfd_get_section_contents (need_pad->owner, need_pad,
7383 p, 0, need_pad->size))
7384 return FALSE;
7385
7386 need_pad->contents = p;
7387 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7388 }
7389 else
7390 {
7391 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
7392 if (p == NULL)
7393 return FALSE;
7394
7395 need_pad->contents = p;
7396 }
7397
7398 memset (need_pad->contents + need_pad->size, 0, 8);
7399 need_pad->size += 8;
7400 }
7401
411e1bfb
AM
7402 return TRUE;
7403}
7404
e1918d23 7405/* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
411e1bfb 7406
e1918d23 7407asection *
33c0ec9d
AM
7408ppc64_elf_tls_setup (struct bfd_link_info *info,
7409 int no_tls_get_addr_opt,
7410 int *no_multi_toc)
411e1bfb 7411{
411e1bfb
AM
7412 struct ppc_link_hash_table *htab;
7413
411e1bfb 7414 htab = ppc_hash_table (info);
4dfe6ac6
NC
7415 if (htab == NULL)
7416 return NULL;
7417
33c0ec9d
AM
7418 if (*no_multi_toc)
7419 htab->do_multi_toc = 0;
7420 else if (!htab->do_multi_toc)
7421 *no_multi_toc = 1;
7422
3a71aa26
AM
7423 htab->tls_get_addr = ((struct ppc_link_hash_entry *)
7424 elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
7425 FALSE, FALSE, TRUE));
a7f2871e
AM
7426 /* Move dynamic linking info to the function descriptor sym. */
7427 if (htab->tls_get_addr != NULL)
7428 func_desc_adjust (&htab->tls_get_addr->elf, info);
3a71aa26
AM
7429 htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
7430 elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
7431 FALSE, FALSE, TRUE));
a7f2871e
AM
7432 if (!no_tls_get_addr_opt)
7433 {
7434 struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
7435
7436 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
7437 FALSE, FALSE, TRUE);
7438 if (opt != NULL)
7439 func_desc_adjust (opt, info);
7440 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
7441 FALSE, FALSE, TRUE);
7442 if (opt_fd != NULL
7443 && (opt_fd->root.type == bfd_link_hash_defined
7444 || opt_fd->root.type == bfd_link_hash_defweak))
7445 {
7446 /* If glibc supports an optimized __tls_get_addr call stub,
7447 signalled by the presence of __tls_get_addr_opt, and we'll
7448 be calling __tls_get_addr via a plt call stub, then
7449 make __tls_get_addr point to __tls_get_addr_opt. */
7450 tga_fd = &htab->tls_get_addr_fd->elf;
7451 if (htab->elf.dynamic_sections_created
7452 && tga_fd != NULL
7453 && (tga_fd->type == STT_FUNC
7454 || tga_fd->needs_plt)
7455 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
7456 || (ELF_ST_VISIBILITY (tga_fd->other) != STV_DEFAULT
7457 && tga_fd->root.type == bfd_link_hash_undefweak)))
7458 {
7459 struct plt_entry *ent;
7460
7461 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
7462 if (ent->plt.refcount > 0)
7463 break;
7464 if (ent != NULL)
7465 {
7466 tga_fd->root.type = bfd_link_hash_indirect;
7467 tga_fd->root.u.i.link = &opt_fd->root;
7468 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
7469 if (opt_fd->dynindx != -1)
7470 {
7471 /* Use __tls_get_addr_opt in dynamic relocations. */
7472 opt_fd->dynindx = -1;
7473 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7474 opt_fd->dynstr_index);
7475 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
854b41e7 7476 return NULL;
a7f2871e
AM
7477 }
7478 htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) opt_fd;
7479 tga = &htab->tls_get_addr->elf;
7480 if (opt != NULL && tga != NULL)
7481 {
7482 tga->root.type = bfd_link_hash_indirect;
7483 tga->root.u.i.link = &opt->root;
7484 ppc64_elf_copy_indirect_symbol (info, opt, tga);
7485 _bfd_elf_link_hash_hide_symbol (info, opt,
7486 tga->forced_local);
7487 htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
7488 }
7489 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
7490 htab->tls_get_addr_fd->is_func_descriptor = 1;
7491 if (htab->tls_get_addr != NULL)
7492 {
7493 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
7494 htab->tls_get_addr->is_func = 1;
7495 }
7496 }
7497 }
7498 }
7499 else
7500 no_tls_get_addr_opt = TRUE;
7501 }
7502 htab->no_tls_get_addr_opt = no_tls_get_addr_opt;
33c0ec9d 7503 return _bfd_elf_tls_setup (info->output_bfd, info);
3a71aa26 7504}
8387904d 7505
3a71aa26
AM
7506/* Return TRUE iff REL is a branch reloc with a global symbol matching
7507 HASH1 or HASH2. */
8387904d 7508
3a71aa26
AM
7509static bfd_boolean
7510branch_reloc_hash_match (const bfd *ibfd,
7511 const Elf_Internal_Rela *rel,
7512 const struct ppc_link_hash_entry *hash1,
7513 const struct ppc_link_hash_entry *hash2)
7514{
7515 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7516 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
7517 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
7518
e054468f 7519 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8387904d 7520 {
3a71aa26
AM
7521 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7522 struct elf_link_hash_entry *h;
8387904d 7523
3a71aa26 7524 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 7525 h = elf_follow_link (h);
3a71aa26
AM
7526 if (h == &hash1->elf || h == &hash2->elf)
7527 return TRUE;
a48ebf4d 7528 }
3a71aa26 7529 return FALSE;
951fd09b 7530}
411e1bfb 7531
951fd09b
AM
7532/* Run through all the TLS relocs looking for optimization
7533 opportunities. The linker has been hacked (see ppc64elf.em) to do
7534 a preliminary section layout so that we know the TLS segment
7535 offsets. We can't optimize earlier because some optimizations need
7536 to know the tp offset, and we need to optimize before allocating
7537 dynamic relocations. */
7538
7539bfd_boolean
33c0ec9d 7540ppc64_elf_tls_optimize (struct bfd_link_info *info)
951fd09b
AM
7541{
7542 bfd *ibfd;
7543 asection *sec;
7544 struct ppc_link_hash_table *htab;
663a1470 7545 unsigned char *toc_ref;
102890f0 7546 int pass;
951fd09b 7547
1d483afe 7548 if (info->relocatable || !info->executable)
411e1bfb
AM
7549 return TRUE;
7550
951fd09b 7551 htab = ppc_hash_table (info);
4dfe6ac6
NC
7552 if (htab == NULL)
7553 return FALSE;
7554
663a1470
AM
7555 /* Make two passes over the relocs. On the first pass, mark toc
7556 entries involved with tls relocs, and check that tls relocs
7557 involved in setting up a tls_get_addr call are indeed followed by
7558 such a call. If they are not, we can't do any tls optimization.
7559 On the second pass twiddle tls_mask flags to notify
7560 relocate_section that optimization can be done, and adjust got
7561 and plt refcounts. */
7562 toc_ref = NULL;
7563 for (pass = 0; pass < 2; ++pass)
7564 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
7565 {
7566 Elf_Internal_Sym *locsyms = NULL;
7567 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
7568
102890f0
AM
7569 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7570 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
7571 {
7572 Elf_Internal_Rela *relstart, *rel, *relend;
663a1470 7573 bfd_boolean found_tls_get_addr_arg = 0;
411e1bfb 7574
102890f0
AM
7575 /* Read the relocations. */
7576 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7577 info->keep_memory);
7578 if (relstart == NULL)
7579 return FALSE;
411e1bfb 7580
102890f0
AM
7581 relend = relstart + sec->reloc_count;
7582 for (rel = relstart; rel < relend; rel++)
7583 {
7584 enum elf_ppc64_reloc_type r_type;
7585 unsigned long r_symndx;
7586 struct elf_link_hash_entry *h;
7587 Elf_Internal_Sym *sym;
7588 asection *sym_sec;
f961d9dd
AM
7589 unsigned char *tls_mask;
7590 unsigned char tls_set, tls_clear, tls_type = 0;
102890f0
AM
7591 bfd_vma value;
7592 bfd_boolean ok_tprel, is_local;
7593 long toc_ref_index = 0;
7594 int expecting_tls_get_addr = 0;
663a1470 7595 bfd_boolean ret = FALSE;
411e1bfb 7596
102890f0
AM
7597 r_symndx = ELF64_R_SYM (rel->r_info);
7598 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
7599 r_symndx, ibfd))
7600 {
7601 err_free_rel:
7602 if (elf_section_data (sec)->relocs != relstart)
7603 free (relstart);
7604 if (toc_ref != NULL)
7605 free (toc_ref);
7606 if (locsyms != NULL
0ffa91dd 7607 && (elf_symtab_hdr (ibfd).contents
102890f0
AM
7608 != (unsigned char *) locsyms))
7609 free (locsyms);
663a1470 7610 return ret;
102890f0 7611 }
411e1bfb 7612
102890f0
AM
7613 if (h != NULL)
7614 {
766bc656
AM
7615 if (h->root.type == bfd_link_hash_defined
7616 || h->root.type == bfd_link_hash_defweak)
7617 value = h->root.u.def.value;
7618 else if (h->root.type == bfd_link_hash_undefweak)
7619 value = 0;
7620 else
663a1470
AM
7621 {
7622 found_tls_get_addr_arg = 0;
7623 continue;
7624 }
102890f0
AM
7625 }
7626 else
7627 /* Symbols referenced by TLS relocs must be of type
7628 STT_TLS. So no need for .opd local sym adjust. */
7629 value = sym->st_value;
7630
7631 ok_tprel = FALSE;
7632 is_local = FALSE;
7633 if (h == NULL
7634 || !h->def_dynamic)
7635 {
7636 is_local = TRUE;
766bc656
AM
7637 if (h != NULL
7638 && h->root.type == bfd_link_hash_undefweak)
7639 ok_tprel = TRUE;
7640 else
7641 {
7642 value += sym_sec->output_offset;
7643 value += sym_sec->output_section->vma;
7644 value -= htab->elf.tls_sec->vma;
7645 ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
7646 < (bfd_vma) 1 << 32);
7647 }
102890f0 7648 }
951fd09b 7649
102890f0 7650 r_type = ELF64_R_TYPE (rel->r_info);
663a1470
AM
7651 /* If this section has old-style __tls_get_addr calls
7652 without marker relocs, then check that each
7653 __tls_get_addr call reloc is preceded by a reloc
7654 that conceivably belongs to the __tls_get_addr arg
7655 setup insn. If we don't find matching arg setup
7656 relocs, don't do any tls optimization. */
7657 if (pass == 0
7658 && sec->has_tls_get_addr_call
7659 && h != NULL
7660 && (h == &htab->tls_get_addr->elf
7661 || h == &htab->tls_get_addr_fd->elf)
7662 && !found_tls_get_addr_arg
7663 && is_branch_reloc (r_type))
7664 {
25f53a85 7665 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
663a1470
AM
7666 "TLS optimization disabled\n"),
7667 ibfd, sec, rel->r_offset);
7668 ret = TRUE;
7669 goto err_free_rel;
7670 }
7671
7672 found_tls_get_addr_arg = 0;
102890f0
AM
7673 switch (r_type)
7674 {
7675 case R_PPC64_GOT_TLSLD16:
7676 case R_PPC64_GOT_TLSLD16_LO:
7677 expecting_tls_get_addr = 1;
663a1470 7678 found_tls_get_addr_arg = 1;
102890f0
AM
7679 /* Fall thru */
7680
7681 case R_PPC64_GOT_TLSLD16_HI:
7682 case R_PPC64_GOT_TLSLD16_HA:
7683 /* These relocs should never be against a symbol
7684 defined in a shared lib. Leave them alone if
7685 that turns out to be the case. */
7686 if (!is_local)
7687 continue;
411e1bfb 7688
102890f0 7689 /* LD -> LE */
411e1bfb 7690 tls_set = 0;
102890f0
AM
7691 tls_clear = TLS_LD;
7692 tls_type = TLS_TLS | TLS_LD;
7693 break;
411e1bfb 7694
102890f0
AM
7695 case R_PPC64_GOT_TLSGD16:
7696 case R_PPC64_GOT_TLSGD16_LO:
7697 expecting_tls_get_addr = 1;
663a1470 7698 found_tls_get_addr_arg = 1;
102890f0
AM
7699 /* Fall thru */
7700
7701 case R_PPC64_GOT_TLSGD16_HI:
7702 case R_PPC64_GOT_TLSGD16_HA:
7703 if (ok_tprel)
7704 /* GD -> LE */
411e1bfb 7705 tls_set = 0;
102890f0
AM
7706 else
7707 /* GD -> IE */
7708 tls_set = TLS_TLS | TLS_TPRELGD;
7709 tls_clear = TLS_GD;
7710 tls_type = TLS_TLS | TLS_GD;
7711 break;
7712
7713 case R_PPC64_GOT_TPREL16_DS:
7714 case R_PPC64_GOT_TPREL16_LO_DS:
7715 case R_PPC64_GOT_TPREL16_HI:
7716 case R_PPC64_GOT_TPREL16_HA:
7717 if (ok_tprel)
7718 {
7719 /* IE -> LE */
7720 tls_set = 0;
7721 tls_clear = TLS_TPREL;
7722 tls_type = TLS_TLS | TLS_TPREL;
7723 break;
7724 }
411e1bfb
AM
7725 continue;
7726
727fc41e
AM
7727 case R_PPC64_TLSGD:
7728 case R_PPC64_TLSLD:
663a1470
AM
7729 found_tls_get_addr_arg = 1;
7730 /* Fall thru */
7731
7732 case R_PPC64_TLS:
7733 case R_PPC64_TOC16:
7734 case R_PPC64_TOC16_LO:
102890f0
AM
7735 if (sym_sec == NULL || sym_sec != toc)
7736 continue;
7737
7738 /* Mark this toc entry as referenced by a TLS
7739 code sequence. We can do that now in the
7740 case of R_PPC64_TLS, and after checking for
7741 tls_get_addr for the TOC16 relocs. */
7742 if (toc_ref == NULL)
663a1470
AM
7743 toc_ref = bfd_zmalloc (toc->output_section->rawsize / 8);
7744 if (toc_ref == NULL)
7745 goto err_free_rel;
7746
102890f0
AM
7747 if (h != NULL)
7748 value = h->root.u.def.value;
7749 else
7750 value = sym->st_value;
7751 value += rel->r_addend;
7752 BFD_ASSERT (value < toc->size && value % 8 == 0);
663a1470 7753 toc_ref_index = (value + toc->output_offset) / 8;
727fc41e
AM
7754 if (r_type == R_PPC64_TLS
7755 || r_type == R_PPC64_TLSGD
7756 || r_type == R_PPC64_TLSLD)
102890f0
AM
7757 {
7758 toc_ref[toc_ref_index] = 1;
7759 continue;
7760 }
7761
7762 if (pass != 0 && toc_ref[toc_ref_index] == 0)
7763 continue;
7764
7765 tls_set = 0;
7766 tls_clear = 0;
7767 expecting_tls_get_addr = 2;
7768 break;
7769
7770 case R_PPC64_TPREL64:
7771 if (pass == 0
7772 || sec != toc
7773 || toc_ref == NULL
663a1470 7774 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
7775 continue;
7776 if (ok_tprel)
7777 {
7778 /* IE -> LE */
7779 tls_set = TLS_EXPLICIT;
7780 tls_clear = TLS_TPREL;
7781 break;
7782 }
7783 continue;
7784
7785 case R_PPC64_DTPMOD64:
7786 if (pass == 0
7787 || sec != toc
7788 || toc_ref == NULL
663a1470 7789 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
7790 continue;
7791 if (rel + 1 < relend
7792 && (rel[1].r_info
7793 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
7794 && rel[1].r_offset == rel->r_offset + 8)
7795 {
7796 if (ok_tprel)
7797 /* GD -> LE */
7798 tls_set = TLS_EXPLICIT | TLS_GD;
7799 else
7800 /* GD -> IE */
7801 tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
7802 tls_clear = TLS_GD;
7803 }
7804 else
7805 {
7806 if (!is_local)
7807 continue;
7808
7809 /* LD -> LE */
7810 tls_set = TLS_EXPLICIT;
7811 tls_clear = TLS_LD;
7812 }
7813 break;
7814
7815 default:
7816 continue;
7817 }
7818
7819 if (pass == 0)
7820 {
727fc41e
AM
7821 if (!expecting_tls_get_addr
7822 || !sec->has_tls_get_addr_call)
102890f0
AM
7823 continue;
7824
3a71aa26
AM
7825 if (rel + 1 < relend
7826 && branch_reloc_hash_match (ibfd, rel + 1,
7827 htab->tls_get_addr,
7828 htab->tls_get_addr_fd))
102890f0 7829 {
3a71aa26 7830 if (expecting_tls_get_addr == 2)
102890f0 7831 {
3a71aa26 7832 /* Check for toc tls entries. */
f961d9dd 7833 unsigned char *toc_tls;
3a71aa26
AM
7834 int retval;
7835
7836 retval = get_tls_mask (&toc_tls, NULL, NULL,
7837 &locsyms,
7838 rel, ibfd);
7839 if (retval == 0)
7840 goto err_free_rel;
663a1470
AM
7841 if (toc_tls != NULL)
7842 {
7843 if ((*toc_tls & (TLS_GD | TLS_LD)) != 0)
7844 found_tls_get_addr_arg = 1;
7845 if (retval > 1)
7846 toc_ref[toc_ref_index] = 1;
7847 }
102890f0 7848 }
3a71aa26 7849 continue;
102890f0
AM
7850 }
7851
7852 if (expecting_tls_get_addr != 1)
7853 continue;
7854
7855 /* Uh oh, we didn't find the expected call. We
7856 could just mark this symbol to exclude it
7857 from tls optimization but it's safer to skip
663a1470 7858 the entire optimization. */
25f53a85 7859 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
663a1470
AM
7860 "TLS optimization disabled\n"),
7861 ibfd, sec, rel->r_offset);
7862 ret = TRUE;
7863 goto err_free_rel;
102890f0
AM
7864 }
7865
85f7a9cb 7866 if (expecting_tls_get_addr && htab->tls_get_addr != NULL)
102890f0
AM
7867 {
7868 struct plt_entry *ent;
7869 for (ent = htab->tls_get_addr->elf.plt.plist;
7870 ent != NULL;
7871 ent = ent->next)
7872 if (ent->addend == 0)
411e1bfb 7873 {
102890f0 7874 if (ent->plt.refcount > 0)
30038c59 7875 {
102890f0
AM
7876 ent->plt.refcount -= 1;
7877 expecting_tls_get_addr = 0;
30038c59 7878 }
102890f0 7879 break;
411e1bfb 7880 }
102890f0 7881 }
411e1bfb 7882
85f7a9cb 7883 if (expecting_tls_get_addr && htab->tls_get_addr_fd != NULL)
102890f0
AM
7884 {
7885 struct plt_entry *ent;
7886 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
7887 ent != NULL;
7888 ent = ent->next)
7889 if (ent->addend == 0)
411e1bfb 7890 {
102890f0
AM
7891 if (ent->plt.refcount > 0)
7892 ent->plt.refcount -= 1;
7893 break;
411e1bfb 7894 }
102890f0 7895 }
411e1bfb 7896
102890f0 7897 if (tls_clear == 0)
30038c59
AM
7898 continue;
7899
102890f0
AM
7900 if ((tls_set & TLS_EXPLICIT) == 0)
7901 {
7902 struct got_entry *ent;
411e1bfb 7903
102890f0
AM
7904 /* Adjust got entry for this reloc. */
7905 if (h != NULL)
7906 ent = h->got.glist;
7907 else
7908 ent = elf_local_got_ents (ibfd)[r_symndx];
411e1bfb 7909
102890f0
AM
7910 for (; ent != NULL; ent = ent->next)
7911 if (ent->addend == rel->r_addend
7912 && ent->owner == ibfd
7913 && ent->tls_type == tls_type)
7914 break;
7915 if (ent == NULL)
7916 abort ();
411e1bfb 7917
102890f0
AM
7918 if (tls_set == 0)
7919 {
7920 /* We managed to get rid of a got entry. */
7921 if (ent->got.refcount > 0)
7922 ent->got.refcount -= 1;
7923 }
7924 }
7925 else
7926 {
7927 /* If we got rid of a DTPMOD/DTPREL reloc pair then
7928 we'll lose one or two dyn relocs. */
7929 if (!dec_dynrel_count (rel->r_info, sec, info,
7930 NULL, h, sym_sec))
7931 return FALSE;
411e1bfb 7932
102890f0
AM
7933 if (tls_set == (TLS_EXPLICIT | TLS_GD))
7934 {
7935 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
7936 NULL, h, sym_sec))
7937 return FALSE;
7938 }
7939 }
411e1bfb 7940
102890f0
AM
7941 *tls_mask |= tls_set;
7942 *tls_mask &= ~tls_clear;
7943 }
8c1d1bb8 7944
102890f0
AM
7945 if (elf_section_data (sec)->relocs != relstart)
7946 free (relstart);
7947 }
411e1bfb 7948
663a1470
AM
7949 if (locsyms != NULL
7950 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
7951 {
7952 if (!info->keep_memory)
7953 free (locsyms);
7954 else
7955 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
7956 }
7957 }
411e1bfb 7958
663a1470
AM
7959 if (toc_ref != NULL)
7960 free (toc_ref);
b34976b6 7961 return TRUE;
1e2f5b6e 7962}
b34976b6 7963
c5614fa4
AM
7964/* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
7965 the values of any global symbols in a toc section that has been
7966 edited. Globals in toc sections should be a rarity, so this function
7967 sets a flag if any are found in toc sections other than the one just
7968 edited, so that futher hash table traversals can be avoided. */
7969
7970struct adjust_toc_info
7971{
7972 asection *toc;
7973 unsigned long *skip;
7974 bfd_boolean global_toc_syms;
7975};
7976
ba761f19
AM
7977enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
7978
c5614fa4
AM
7979static bfd_boolean
7980adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
7981{
7982 struct ppc_link_hash_entry *eh;
7983 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
854b41e7 7984 unsigned long i;
c5614fa4 7985
c5614fa4
AM
7986 if (h->root.type != bfd_link_hash_defined
7987 && h->root.type != bfd_link_hash_defweak)
7988 return TRUE;
7989
7990 eh = (struct ppc_link_hash_entry *) h;
7991 if (eh->adjust_done)
7992 return TRUE;
7993
7994 if (eh->elf.root.u.def.section == toc_inf->toc)
7995 {
854b41e7
AM
7996 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
7997 i = toc_inf->toc->rawsize >> 3;
c5614fa4 7998 else
854b41e7
AM
7999 i = eh->elf.root.u.def.value >> 3;
8000
ba761f19 8001 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
c5614fa4
AM
8002 {
8003 (*_bfd_error_handler)
854b41e7
AM
8004 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8005 do
8006 ++i;
ba761f19 8007 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
854b41e7 8008 eh->elf.root.u.def.value = (bfd_vma) i << 3;
c5614fa4 8009 }
854b41e7
AM
8010
8011 eh->elf.root.u.def.value -= toc_inf->skip[i];
c5614fa4
AM
8012 eh->adjust_done = 1;
8013 }
8014 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8015 toc_inf->global_toc_syms = TRUE;
8016
8017 return TRUE;
8018}
8019
560c8763
AM
8020/* Return TRUE iff INSN is one we expect on a _LO variety toc/got reloc. */
8021
8022static bfd_boolean
8023ok_lo_toc_insn (unsigned int insn)
8024{
8025 return ((insn & (0x3f << 26)) == 14u << 26 /* addi */
8026 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
8027 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
8028 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
8029 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
8030 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
8031 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
8032 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
8033 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
8034 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
8035 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
8036 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
8037 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
8038 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
8039 || ((insn & (0x3f << 26)) == 58u << 26 /* lwa,ld,lmd */
8040 && (insn & 3) != 1)
8041 || ((insn & (0x3f << 26)) == 62u << 26 /* std, stmd */
8042 && ((insn & 3) == 0 || (insn & 3) == 3))
8043 || (insn & (0x3f << 26)) == 12u << 26 /* addic */);
8044}
8045
c5614fa4
AM
8046/* Examine all relocs referencing .toc sections in order to remove
8047 unused .toc entries. */
8048
8049bfd_boolean
33c0ec9d 8050ppc64_elf_edit_toc (struct bfd_link_info *info)
c5614fa4
AM
8051{
8052 bfd *ibfd;
8053 struct adjust_toc_info toc_inf;
67f0cbdb 8054 struct ppc_link_hash_table *htab = ppc_hash_table (info);
c5614fa4 8055
67f0cbdb 8056 htab->do_toc_opt = 1;
c5614fa4
AM
8057 toc_inf.global_toc_syms = TRUE;
8058 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
8059 {
8060 asection *toc, *sec;
8061 Elf_Internal_Shdr *symtab_hdr;
8062 Elf_Internal_Sym *local_syms;
425b145b 8063 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
c5614fa4
AM
8064 unsigned long *skip, *drop;
8065 unsigned char *used;
8066 unsigned char *keep, last, some_unused;
8067
854b41e7
AM
8068 if (!is_ppc64_elf (ibfd))
8069 continue;
8070
c5614fa4
AM
8071 toc = bfd_get_section_by_name (ibfd, ".toc");
8072 if (toc == NULL
92b7a70f 8073 || toc->size == 0
c5614fa4
AM
8074 || toc->sec_info_type == ELF_INFO_TYPE_JUST_SYMS
8075 || elf_discarded_section (toc))
8076 continue;
8077
425b145b 8078 toc_relocs = NULL;
c5614fa4 8079 local_syms = NULL;
0ffa91dd 8080 symtab_hdr = &elf_symtab_hdr (ibfd);
c5614fa4
AM
8081
8082 /* Look at sections dropped from the final link. */
8083 skip = NULL;
8084 relstart = NULL;
8085 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8086 {
8087 if (sec->reloc_count == 0
8088 || !elf_discarded_section (sec)
8089 || get_opd_info (sec)
8090 || (sec->flags & SEC_ALLOC) == 0
8091 || (sec->flags & SEC_DEBUGGING) != 0)
8092 continue;
8093
8094 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
8095 if (relstart == NULL)
8096 goto error_ret;
8097
8098 /* Run through the relocs to see which toc entries might be
8099 unused. */
8100 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8101 {
8102 enum elf_ppc64_reloc_type r_type;
8103 unsigned long r_symndx;
8104 asection *sym_sec;
8105 struct elf_link_hash_entry *h;
8106 Elf_Internal_Sym *sym;
8107 bfd_vma val;
8108
8109 r_type = ELF64_R_TYPE (rel->r_info);
8110 switch (r_type)
8111 {
8112 default:
8113 continue;
8114
8115 case R_PPC64_TOC16:
8116 case R_PPC64_TOC16_LO:
8117 case R_PPC64_TOC16_HI:
8118 case R_PPC64_TOC16_HA:
8119 case R_PPC64_TOC16_DS:
8120 case R_PPC64_TOC16_LO_DS:
8121 break;
8122 }
8123
8124 r_symndx = ELF64_R_SYM (rel->r_info);
8125 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8126 r_symndx, ibfd))
8127 goto error_ret;
8128
8129 if (sym_sec != toc)
8130 continue;
8131
8132 if (h != NULL)
8133 val = h->root.u.def.value;
8134 else
8135 val = sym->st_value;
8136 val += rel->r_addend;
8137
8138 if (val >= toc->size)
8139 continue;
8140
8141 /* Anything in the toc ought to be aligned to 8 bytes.
8142 If not, don't mark as unused. */
8143 if (val & 7)
8144 continue;
8145
8146 if (skip == NULL)
8147 {
854b41e7 8148 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
c5614fa4
AM
8149 if (skip == NULL)
8150 goto error_ret;
8151 }
8152
ba761f19 8153 skip[val >> 3] = ref_from_discarded;
c5614fa4
AM
8154 }
8155
8156 if (elf_section_data (sec)->relocs != relstart)
8157 free (relstart);
8158 }
8159
ba761f19
AM
8160 /* For largetoc loads of address constants, we can convert
8161 . addis rx,2,addr@got@ha
8162 . ld ry,addr@got@l(rx)
8163 to
8164 . addis rx,2,addr@toc@ha
8165 . addi ry,rx,addr@toc@l
8166 when addr is within 2G of the toc pointer. This then means
8167 that the word storing "addr" in the toc is no longer needed. */
8168
8169 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
8170 && toc->output_section->rawsize < (bfd_vma) 1 << 31
8171 && toc->reloc_count != 0)
8172 {
8173 /* Read toc relocs. */
425b145b
AM
8174 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8175 info->keep_memory);
8176 if (toc_relocs == NULL)
ba761f19
AM
8177 goto error_ret;
8178
425b145b 8179 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
8180 {
8181 enum elf_ppc64_reloc_type r_type;
8182 unsigned long r_symndx;
8183 asection *sym_sec;
8184 struct elf_link_hash_entry *h;
8185 Elf_Internal_Sym *sym;
8186 bfd_vma val, addr;
8187
8188 r_type = ELF64_R_TYPE (rel->r_info);
8189 if (r_type != R_PPC64_ADDR64)
8190 continue;
8191
8192 r_symndx = ELF64_R_SYM (rel->r_info);
8193 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8194 r_symndx, ibfd))
8195 goto error_ret;
8196
425b145b
AM
8197 if (sym_sec == NULL
8198 || elf_discarded_section (sym_sec))
8199 continue;
8200
726d3ab0 8201 if (!SYMBOL_CALLS_LOCAL (info, h))
ba761f19
AM
8202 continue;
8203
8204 if (h != NULL)
bddc25c9
AM
8205 {
8206 if (h->type == STT_GNU_IFUNC)
8207 continue;
8208 val = h->root.u.def.value;
8209 }
ba761f19 8210 else
bddc25c9
AM
8211 {
8212 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
8213 continue;
8214 val = sym->st_value;
8215 }
ba761f19
AM
8216 val += rel->r_addend;
8217 val += sym_sec->output_section->vma + sym_sec->output_offset;
8218
8219 /* We don't yet know the exact toc pointer value, but we
8220 know it will be somewhere in the toc section. Don't
8221 optimize if the difference from any possible toc
8222 pointer is outside [ff..f80008000, 7fff7fff]. */
8223 addr = toc->output_section->vma + TOC_BASE_OFF;
8224 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8225 continue;
8226
8227 addr = toc->output_section->vma + toc->output_section->rawsize;
8228 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8229 continue;
8230
8231 if (skip == NULL)
8232 {
8233 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8234 if (skip == NULL)
8235 goto error_ret;
8236 }
8237
8238 skip[rel->r_offset >> 3]
425b145b 8239 |= can_optimize | ((rel - toc_relocs) << 2);
ba761f19 8240 }
ba761f19
AM
8241 }
8242
c5614fa4
AM
8243 if (skip == NULL)
8244 continue;
8245
8246 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
8247 if (used == NULL)
8248 {
8249 error_ret:
8250 if (local_syms != NULL
8251 && symtab_hdr->contents != (unsigned char *) local_syms)
8252 free (local_syms);
8253 if (sec != NULL
8254 && relstart != NULL
8255 && elf_section_data (sec)->relocs != relstart)
8256 free (relstart);
425b145b
AM
8257 if (toc_relocs != NULL
8258 && elf_section_data (toc)->relocs != toc_relocs)
8259 free (toc_relocs);
c5614fa4
AM
8260 if (skip != NULL)
8261 free (skip);
8262 return FALSE;
8263 }
8264
30038c59
AM
8265 /* Now check all kept sections that might reference the toc.
8266 Check the toc itself last. */
8267 for (sec = (ibfd->sections == toc && toc->next ? toc->next
8268 : ibfd->sections);
c5614fa4 8269 sec != NULL;
c5614fa4 8270 sec = (sec == toc ? NULL
c5614fa4 8271 : sec->next == NULL ? toc
30038c59 8272 : sec->next == toc && toc->next ? toc->next
c5614fa4
AM
8273 : sec->next))
8274 {
8275 int repeat;
8276
8277 if (sec->reloc_count == 0
8278 || elf_discarded_section (sec)
8279 || get_opd_info (sec)
8280 || (sec->flags & SEC_ALLOC) == 0
8281 || (sec->flags & SEC_DEBUGGING) != 0)
8282 continue;
8283
854b41e7
AM
8284 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8285 info->keep_memory);
c5614fa4
AM
8286 if (relstart == NULL)
8287 goto error_ret;
8288
8289 /* Mark toc entries referenced as used. */
8290 repeat = 0;
8291 do
8292 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8293 {
8294 enum elf_ppc64_reloc_type r_type;
8295 unsigned long r_symndx;
8296 asection *sym_sec;
8297 struct elf_link_hash_entry *h;
8298 Elf_Internal_Sym *sym;
8299 bfd_vma val;
560c8763 8300 enum {no_check, check_lo, check_ha} insn_check;
c5614fa4
AM
8301
8302 r_type = ELF64_R_TYPE (rel->r_info);
98528052
AM
8303 switch (r_type)
8304 {
8305 default:
560c8763 8306 insn_check = no_check;
98528052
AM
8307 break;
8308
8309 case R_PPC64_GOT_TLSLD16_HA:
8310 case R_PPC64_GOT_TLSGD16_HA:
8311 case R_PPC64_GOT_TPREL16_HA:
8312 case R_PPC64_GOT_DTPREL16_HA:
8313 case R_PPC64_GOT16_HA:
8314 case R_PPC64_TOC16_HA:
560c8763
AM
8315 insn_check = check_ha;
8316 break;
98528052 8317
560c8763
AM
8318 case R_PPC64_GOT_TLSLD16_LO:
8319 case R_PPC64_GOT_TLSGD16_LO:
8320 case R_PPC64_GOT_TPREL16_LO_DS:
8321 case R_PPC64_GOT_DTPREL16_LO_DS:
8322 case R_PPC64_GOT16_LO:
8323 case R_PPC64_GOT16_LO_DS:
8324 case R_PPC64_TOC16_LO:
8325 case R_PPC64_TOC16_LO_DS:
8326 insn_check = check_lo;
98528052
AM
8327 break;
8328 }
8329
560c8763
AM
8330 if (insn_check != no_check)
8331 {
8332 bfd_vma off = rel->r_offset & ~3;
8333 unsigned char buf[4];
8334 unsigned int insn;
8335
8336 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
8337 {
8338 free (used);
8339 goto error_ret;
8340 }
8341 insn = bfd_get_32 (ibfd, buf);
8342 if (insn_check == check_lo
8343 ? !ok_lo_toc_insn (insn)
8344 : ((insn & ((0x3f << 26) | 0x1f << 16))
8345 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
8346 {
8347 char str[12];
8348
8349 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
8350 sprintf (str, "%#08x", insn);
8351 info->callbacks->einfo
8352 (_("%P: %H: toc optimization is not supported for"
8353 " %s instruction.\n"),
8354 ibfd, sec, rel->r_offset & ~3, str);
8355 }
8356 }
8357
c5614fa4
AM
8358 switch (r_type)
8359 {
8360 case R_PPC64_TOC16:
8361 case R_PPC64_TOC16_LO:
8362 case R_PPC64_TOC16_HI:
8363 case R_PPC64_TOC16_HA:
8364 case R_PPC64_TOC16_DS:
8365 case R_PPC64_TOC16_LO_DS:
8366 /* In case we're taking addresses of toc entries. */
8367 case R_PPC64_ADDR64:
8368 break;
8369
8370 default:
8371 continue;
8372 }
8373
8374 r_symndx = ELF64_R_SYM (rel->r_info);
8375 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8376 r_symndx, ibfd))
8377 {
8378 free (used);
8379 goto error_ret;
8380 }
8381
8382 if (sym_sec != toc)
8383 continue;
8384
8385 if (h != NULL)
8386 val = h->root.u.def.value;
8387 else
8388 val = sym->st_value;
8389 val += rel->r_addend;
8390
8391 if (val >= toc->size)
8392 continue;
8393
ba761f19
AM
8394 if ((skip[val >> 3] & can_optimize) != 0)
8395 {
8396 bfd_vma off;
8397 unsigned char opc;
8398
8399 switch (r_type)
8400 {
8401 case R_PPC64_TOC16_HA:
8402 break;
8403
8404 case R_PPC64_TOC16_LO_DS:
8405 off = rel->r_offset + (bfd_big_endian (ibfd) ? -2 : 3);
8406 if (!bfd_get_section_contents (ibfd, sec, &opc, off, 1))
98528052
AM
8407 {
8408 free (used);
8409 goto error_ret;
8410 }
ba761f19
AM
8411 if ((opc & (0x3f << 2)) == (58u << 2))
8412 break;
8413 /* Fall thru */
8414
8415 default:
8416 /* Wrong sort of reloc, or not a ld. We may
8417 as well clear ref_from_discarded too. */
8418 skip[val >> 3] = 0;
8419 }
8420 }
8421
c5614fa4
AM
8422 /* For the toc section, we only mark as used if
8423 this entry itself isn't unused. */
8424 if (sec == toc
8425 && !used[val >> 3]
8426 && (used[rel->r_offset >> 3]
ba761f19 8427 || !(skip[rel->r_offset >> 3] & ref_from_discarded)))
c5614fa4
AM
8428 /* Do all the relocs again, to catch reference
8429 chains. */
8430 repeat = 1;
8431
8432 used[val >> 3] = 1;
8433 }
8434 while (repeat);
854b41e7
AM
8435
8436 if (elf_section_data (sec)->relocs != relstart)
8437 free (relstart);
c5614fa4
AM
8438 }
8439
8440 /* Merge the used and skip arrays. Assume that TOC
8441 doublewords not appearing as either used or unused belong
8442 to to an entry more than one doubleword in size. */
8443 for (drop = skip, keep = used, last = 0, some_unused = 0;
8444 drop < skip + (toc->size + 7) / 8;
8445 ++drop, ++keep)
8446 {
8447 if (*keep)
8448 {
ba761f19
AM
8449 *drop &= ~ref_from_discarded;
8450 if ((*drop & can_optimize) != 0)
8451 some_unused = 1;
c5614fa4
AM
8452 last = 0;
8453 }
b140b010 8454 else if ((*drop & ref_from_discarded) != 0)
c5614fa4
AM
8455 {
8456 some_unused = 1;
ba761f19 8457 last = ref_from_discarded;
c5614fa4
AM
8458 }
8459 else
8460 *drop = last;
8461 }
8462
8463 free (used);
8464
8465 if (some_unused)
8466 {
8467 bfd_byte *contents, *src;
8468 unsigned long off;
d62b3684 8469 Elf_Internal_Sym *sym;
ba761f19 8470 bfd_boolean local_toc_syms = FALSE;
c5614fa4
AM
8471
8472 /* Shuffle the toc contents, and at the same time convert the
8473 skip array from booleans into offsets. */
8474 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
8475 goto error_ret;
8476
8477 elf_section_data (toc)->this_hdr.contents = contents;
8478
8479 for (src = contents, off = 0, drop = skip;
8480 src < contents + toc->size;
8481 src += 8, ++drop)
8482 {
ba761f19
AM
8483 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
8484 off += 8;
c5614fa4
AM
8485 else if (off != 0)
8486 {
8487 *drop = off;
8488 memcpy (src - off, src, 8);
8489 }
8490 }
854b41e7 8491 *drop = off;
c5614fa4
AM
8492 toc->rawsize = toc->size;
8493 toc->size = src - contents - off;
8494
ba761f19
AM
8495 /* Adjust addends for relocs against the toc section sym,
8496 and optimize any accesses we can. */
c5614fa4
AM
8497 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8498 {
8499 if (sec->reloc_count == 0
8500 || elf_discarded_section (sec))
8501 continue;
8502
8503 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
854b41e7 8504 info->keep_memory);
c5614fa4
AM
8505 if (relstart == NULL)
8506 goto error_ret;
8507
8508 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8509 {
8510 enum elf_ppc64_reloc_type r_type;
8511 unsigned long r_symndx;
8512 asection *sym_sec;
8513 struct elf_link_hash_entry *h;
854b41e7 8514 bfd_vma val;
c5614fa4
AM
8515
8516 r_type = ELF64_R_TYPE (rel->r_info);
8517 switch (r_type)
8518 {
8519 default:
8520 continue;
8521
8522 case R_PPC64_TOC16:
8523 case R_PPC64_TOC16_LO:
8524 case R_PPC64_TOC16_HI:
8525 case R_PPC64_TOC16_HA:
8526 case R_PPC64_TOC16_DS:
8527 case R_PPC64_TOC16_LO_DS:
8528 case R_PPC64_ADDR64:
8529 break;
8530 }
8531
8532 r_symndx = ELF64_R_SYM (rel->r_info);
8533 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8534 r_symndx, ibfd))
8535 goto error_ret;
8536
ba761f19 8537 if (sym_sec != toc)
c5614fa4
AM
8538 continue;
8539
ba761f19
AM
8540 if (h != NULL)
8541 val = h->root.u.def.value;
8542 else
8543 {
8544 val = sym->st_value;
8545 if (val != 0)
8546 local_toc_syms = TRUE;
8547 }
8548
8549 val += rel->r_addend;
854b41e7
AM
8550
8551 if (val > toc->rawsize)
8552 val = toc->rawsize;
ba761f19
AM
8553 else if ((skip[val >> 3] & ref_from_discarded) != 0)
8554 continue;
8555 else if ((skip[val >> 3] & can_optimize) != 0)
8556 {
8557 Elf_Internal_Rela *tocrel
425b145b 8558 = toc_relocs + (skip[val >> 3] >> 2);
ba761f19
AM
8559 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
8560
8561 switch (r_type)
8562 {
8563 case R_PPC64_TOC16_HA:
8564 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
8565 break;
8566
8567 case R_PPC64_TOC16_LO_DS:
8568 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
8569 break;
8570
8571 default:
28942f62
AM
8572 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
8573 ppc_howto_init ();
b140b010
AM
8574 info->callbacks->einfo
8575 (_("%P: %H: %s relocation references "
8576 "optimized away TOC entry\n"),
8577 ibfd, sec, rel->r_offset,
8578 ppc64_elf_howto_table[r_type]->name);
8579 bfd_set_error (bfd_error_bad_value);
8580 goto error_ret;
ba761f19
AM
8581 }
8582 rel->r_addend = tocrel->r_addend;
8583 elf_section_data (sec)->relocs = relstart;
8584 continue;
8585 }
8586
8587 if (h != NULL || sym->st_value != 0)
8588 continue;
854b41e7
AM
8589
8590 rel->r_addend -= skip[val >> 3];
8591 elf_section_data (sec)->relocs = relstart;
c5614fa4 8592 }
854b41e7
AM
8593
8594 if (elf_section_data (sec)->relocs != relstart)
8595 free (relstart);
c5614fa4
AM
8596 }
8597
8598 /* We shouldn't have local or global symbols defined in the TOC,
8599 but handle them anyway. */
df22d223
AM
8600 if (local_syms != NULL)
8601 for (sym = local_syms;
8602 sym < local_syms + symtab_hdr->sh_info;
8603 ++sym)
8604 if (sym->st_value != 0
8605 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
8606 {
8607 unsigned long i;
854b41e7 8608
df22d223
AM
8609 if (sym->st_value > toc->rawsize)
8610 i = toc->rawsize >> 3;
8611 else
8612 i = sym->st_value >> 3;
854b41e7 8613
df22d223
AM
8614 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
8615 {
8616 if (local_toc_syms)
8617 (*_bfd_error_handler)
8618 (_("%s defined on removed toc entry"),
8619 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
8620 do
8621 ++i;
8622 while ((skip[i] & (ref_from_discarded | can_optimize)));
8623 sym->st_value = (bfd_vma) i << 3;
8624 }
d62b3684 8625
df22d223
AM
8626 sym->st_value -= skip[i];
8627 symtab_hdr->contents = (unsigned char *) local_syms;
8628 }
c5614fa4 8629
854b41e7 8630 /* Adjust any global syms defined in this toc input section. */
c5614fa4
AM
8631 if (toc_inf.global_toc_syms)
8632 {
8633 toc_inf.toc = toc;
8634 toc_inf.skip = skip;
8635 toc_inf.global_toc_syms = FALSE;
8636 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
8637 &toc_inf);
8638 }
854b41e7
AM
8639
8640 if (toc->reloc_count != 0)
8641 {
d4730f92 8642 Elf_Internal_Shdr *rel_hdr;
854b41e7
AM
8643 Elf_Internal_Rela *wrel;
8644 bfd_size_type sz;
8645
854b41e7 8646 /* Remove unused toc relocs, and adjust those we keep. */
28be611c
AM
8647 if (toc_relocs == NULL)
8648 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8649 info->keep_memory);
8650 if (toc_relocs == NULL)
8651 goto error_ret;
8652
425b145b
AM
8653 wrel = toc_relocs;
8654 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
8655 if ((skip[rel->r_offset >> 3]
8656 & (ref_from_discarded | can_optimize)) == 0)
854b41e7
AM
8657 {
8658 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
8659 wrel->r_info = rel->r_info;
8660 wrel->r_addend = rel->r_addend;
8661 ++wrel;
8662 }
8663 else if (!dec_dynrel_count (rel->r_info, toc, info,
8664 &local_syms, NULL, NULL))
8665 goto error_ret;
8666
425b145b
AM
8667 elf_section_data (toc)->relocs = toc_relocs;
8668 toc->reloc_count = wrel - toc_relocs;
d4730f92
BS
8669 rel_hdr = _bfd_elf_single_rel_hdr (toc);
8670 sz = rel_hdr->sh_entsize;
8671 rel_hdr->sh_size = toc->reloc_count * sz;
854b41e7 8672 }
c5614fa4 8673 }
28be611c
AM
8674 else if (toc_relocs != NULL
8675 && elf_section_data (toc)->relocs != toc_relocs)
425b145b 8676 free (toc_relocs);
c5614fa4
AM
8677
8678 if (local_syms != NULL
8679 && symtab_hdr->contents != (unsigned char *) local_syms)
8680 {
8681 if (!info->keep_memory)
8682 free (local_syms);
8683 else
8684 symtab_hdr->contents = (unsigned char *) local_syms;
8685 }
8686 free (skip);
8687 }
8688
8689 return TRUE;
8690}
8691
1bbe0902
AM
8692/* Return true iff input section I references the TOC using
8693 instructions limited to +/-32k offsets. */
8694
8695bfd_boolean
8696ppc64_elf_has_small_toc_reloc (asection *i)
8697{
8698 return (is_ppc64_elf (i->owner)
8699 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
8700}
8701
927be08e
AM
8702/* Allocate space for one GOT entry. */
8703
8704static void
8705allocate_got (struct elf_link_hash_entry *h,
8706 struct bfd_link_info *info,
8707 struct got_entry *gent)
8708{
8709 struct ppc_link_hash_table *htab = ppc_hash_table (info);
8710 bfd_boolean dyn;
8711 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
8712 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
8713 ? 16 : 8);
8714 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
8715 ? 2 : 1) * sizeof (Elf64_External_Rela);
8716 asection *got = ppc64_elf_tdata (gent->owner)->got;
8717
8718 gent->got.offset = got->size;
8719 got->size += entsize;
8720
8721 dyn = htab->elf.dynamic_sections_created;
8722 if ((info->shared
8723 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))
8724 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
8725 || h->root.type != bfd_link_hash_undefweak))
8726 {
8727 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
8728 relgot->size += rentsize;
8729 }
8730 else if (h->type == STT_GNU_IFUNC)
8731 {
8732 asection *relgot = htab->reliplt;
8733 relgot->size += rentsize;
8734 htab->got_reli_size += rentsize;
8735 }
8736}
8737
7865406b
AM
8738/* This function merges got entries in the same toc group. */
8739
8740static void
8741merge_got_entries (struct got_entry **pent)
8742{
8743 struct got_entry *ent, *ent2;
8744
8745 for (ent = *pent; ent != NULL; ent = ent->next)
8746 if (!ent->is_indirect)
8747 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
8748 if (!ent2->is_indirect
8749 && ent2->addend == ent->addend
8750 && ent2->tls_type == ent->tls_type
8751 && elf_gp (ent2->owner) == elf_gp (ent->owner))
8752 {
8753 ent2->is_indirect = TRUE;
8754 ent2->got.ent = ent;
8755 }
8756}
8757
65f38f15
AM
8758/* Allocate space in .plt, .got and associated reloc sections for
8759 dynamic relocs. */
5bd4f169 8760
b34976b6 8761static bfd_boolean
4ce794b7 8762allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
5bd4f169 8763{
65f38f15
AM
8764 struct bfd_link_info *info;
8765 struct ppc_link_hash_table *htab;
5bd4f169 8766 asection *s;
65f38f15 8767 struct ppc_link_hash_entry *eh;
6061a67d 8768 struct elf_dyn_relocs *p;
0b8bcf0d 8769 struct got_entry **pgent, *gent;
5bd4f169 8770
e92d460e 8771 if (h->root.type == bfd_link_hash_indirect)
b34976b6 8772 return TRUE;
5bd4f169 8773
65f38f15
AM
8774 info = (struct bfd_link_info *) inf;
8775 htab = ppc_hash_table (info);
4dfe6ac6
NC
8776 if (htab == NULL)
8777 return FALSE;
5bd4f169 8778
e054468f
AM
8779 if ((htab->elf.dynamic_sections_created
8780 && h->dynindx != -1
8781 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
8782 || h->type == STT_GNU_IFUNC)
5bd4f169 8783 {
411e1bfb
AM
8784 struct plt_entry *pent;
8785 bfd_boolean doneone = FALSE;
8786 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
8787 if (pent->plt.refcount > 0)
8788 {
25f23106
AM
8789 if (!htab->elf.dynamic_sections_created
8790 || h->dynindx == -1)
e054468f
AM
8791 {
8792 s = htab->iplt;
8793 pent->plt.offset = s->size;
8794 s->size += PLT_ENTRY_SIZE;
8795 s = htab->reliplt;
8796 }
8797 else
8798 {
8799 /* If this is the first .plt entry, make room for the special
8800 first entry. */
8801 s = htab->plt;
8802 if (s->size == 0)
8803 s->size += PLT_INITIAL_ENTRY_SIZE;
8804
8805 pent->plt.offset = s->size;
8806
8807 /* Make room for this entry. */
8808 s->size += PLT_ENTRY_SIZE;
8809
8810 /* Make room for the .glink code. */
8811 s = htab->glink;
8812 if (s->size == 0)
8813 s->size += GLINK_CALL_STUB_SIZE;
8814 /* We need bigger stubs past index 32767. */
8815 if (s->size >= GLINK_CALL_STUB_SIZE + 32768*2*4)
8816 s->size += 4;
8817 s->size += 2*4;
8818
8819 /* We also need to make an entry in the .rela.plt section. */
8820 s = htab->relplt;
8821 }
eea6121a 8822 s->size += sizeof (Elf64_External_Rela);
411e1bfb
AM
8823 doneone = TRUE;
8824 }
8825 else
8826 pent->plt.offset = (bfd_vma) -1;
8827 if (!doneone)
65f38f15 8828 {
411e1bfb 8829 h->plt.plist = NULL;
f5385ebf 8830 h->needs_plt = 0;
65f38f15
AM
8831 }
8832 }
8833 else
8834 {
411e1bfb 8835 h->plt.plist = NULL;
f5385ebf 8836 h->needs_plt = 0;
65f38f15
AM
8837 }
8838
951fd09b
AM
8839 eh = (struct ppc_link_hash_entry *) h;
8840 /* Run through the TLS GD got entries first if we're changing them
8841 to TPREL. */
e7b938ca 8842 if ((eh->tls_mask & TLS_TPRELGD) != 0)
951fd09b
AM
8843 for (gent = h->got.glist; gent != NULL; gent = gent->next)
8844 if (gent->got.refcount > 0
8845 && (gent->tls_type & TLS_GD) != 0)
8846 {
8847 /* This was a GD entry that has been converted to TPREL. If
8848 there happens to be a TPREL entry we can use that one. */
8849 struct got_entry *ent;
8850 for (ent = h->got.glist; ent != NULL; ent = ent->next)
8851 if (ent->got.refcount > 0
8852 && (ent->tls_type & TLS_TPREL) != 0
e717da7e
AM
8853 && ent->addend == gent->addend
8854 && ent->owner == gent->owner)
951fd09b
AM
8855 {
8856 gent->got.refcount = 0;
8857 break;
8858 }
8859
8860 /* If not, then we'll be using our own TPREL entry. */
8861 if (gent->got.refcount != 0)
8862 gent->tls_type = TLS_TLS | TLS_TPREL;
8863 }
8864
7865406b
AM
8865 /* Remove any list entry that won't generate a word in the GOT before
8866 we call merge_got_entries. Otherwise we risk merging to empty
8867 entries. */
0b8bcf0d
AM
8868 pgent = &h->got.glist;
8869 while ((gent = *pgent) != NULL)
411e1bfb 8870 if (gent->got.refcount > 0)
7865406b
AM
8871 {
8872 if ((gent->tls_type & TLS_LD) != 0
8873 && !h->def_dynamic)
8874 {
8875 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
8876 *pgent = gent->next;
8877 }
8878 else
8879 pgent = &gent->next;
8880 }
8881 else
8882 *pgent = gent->next;
8883
8884 if (!htab->do_multi_toc)
8885 merge_got_entries (&h->got.glist);
8886
8887 for (gent = h->got.glist; gent != NULL; gent = gent->next)
8888 if (!gent->is_indirect)
411e1bfb
AM
8889 {
8890 /* Make sure this symbol is output as a dynamic symbol.
951fd09b
AM
8891 Undefined weak syms won't yet be marked as dynamic,
8892 nor will all TLS symbols. */
411e1bfb 8893 if (h->dynindx == -1
b099ab9f 8894 && !h->forced_local
25f23106 8895 && h->type != STT_GNU_IFUNC
b099ab9f 8896 && htab->elf.dynamic_sections_created)
411e1bfb 8897 {
c152c796 8898 if (! bfd_elf_link_record_dynamic_symbol (info, h))
411e1bfb
AM
8899 return FALSE;
8900 }
65f38f15 8901
0c8d6e5c 8902 if (!is_ppc64_elf (gent->owner))
927be08e 8903 abort ();
0ffa91dd 8904
927be08e 8905 allocate_got (h, info, gent);
411e1bfb 8906 }
65f38f15 8907
b099ab9f 8908 if (eh->dyn_relocs == NULL
25f23106
AM
8909 || (!htab->elf.dynamic_sections_created
8910 && h->type != STT_GNU_IFUNC))
b34976b6 8911 return TRUE;
65f38f15
AM
8912
8913 /* In the shared -Bsymbolic case, discard space allocated for
8914 dynamic pc-relative relocs against symbols which turn out to be
8915 defined in regular objects. For the normal shared case, discard
8916 space for relocs that have become local due to symbol visibility
8917 changes. */
8918
8919 if (info->shared)
8920 {
9c7a29a3 8921 /* Relocs that use pc_count are those that appear on a call insn,
1d483afe 8922 or certain REL relocs (see must_be_dyn_reloc) that can be
9c7a29a3
AM
8923 generated via assembly. We want calls to protected symbols to
8924 resolve directly to the function rather than going via the plt.
8925 If people want function pointer comparisons to work as expected
8926 then they should avoid writing weird assembly. */
09695f56 8927 if (SYMBOL_CALLS_LOCAL (info, h))
65f38f15 8928 {
6061a67d 8929 struct elf_dyn_relocs **pp;
65f38f15
AM
8930
8931 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
5bd4f169 8932 {
65f38f15
AM
8933 p->count -= p->pc_count;
8934 p->pc_count = 0;
8935 if (p->count == 0)
8936 *pp = p->next;
8937 else
8938 pp = &p->next;
5bd4f169 8939 }
65f38f15 8940 }
4e795f50
AM
8941
8942 /* Also discard relocs on undefined weak syms with non-default
8943 visibility. */
cab87ef9
AM
8944 if (eh->dyn_relocs != NULL
8945 && h->root.type == bfd_link_hash_undefweak)
dfbb6ac9
AM
8946 {
8947 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
8948 eh->dyn_relocs = NULL;
8949
8950 /* Make sure this symbol is output as a dynamic symbol.
8951 Undefined weak syms won't yet be marked as dynamic. */
8952 else if (h->dynindx == -1
8953 && !h->forced_local)
8954 {
8955 if (! bfd_elf_link_record_dynamic_symbol (info, h))
8956 return FALSE;
8957 }
8958 }
65f38f15 8959 }
25f23106
AM
8960 else if (h->type == STT_GNU_IFUNC)
8961 {
8962 if (!h->non_got_ref)
8963 eh->dyn_relocs = NULL;
8964 }
f4656909 8965 else if (ELIMINATE_COPY_RELOCS)
65f38f15
AM
8966 {
8967 /* For the non-shared case, discard space for relocs against
8968 symbols which turn out to need copy relocs or are not
8969 dynamic. */
8970
f5385ebf 8971 if (!h->non_got_ref
f5385ebf 8972 && !h->def_regular)
65f38f15
AM
8973 {
8974 /* Make sure this symbol is output as a dynamic symbol.
8975 Undefined weak syms won't yet be marked as dynamic. */
8976 if (h->dynindx == -1
f5385ebf 8977 && !h->forced_local)
65f38f15 8978 {
c152c796 8979 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 8980 return FALSE;
65f38f15
AM
8981 }
8982
8983 /* If that succeeded, we know we'll be keeping all the
8984 relocs. */
8985 if (h->dynindx != -1)
8986 goto keep;
8987 }
8988
8989 eh->dyn_relocs = NULL;
8990
ec338859 8991 keep: ;
65f38f15
AM
8992 }
8993
8994 /* Finally, allocate space. */
8995 for (p = eh->dyn_relocs; p != NULL; p = p->next)
8996 {
8997 asection *sreloc = elf_section_data (p->sec)->sreloc;
25f23106
AM
8998 if (!htab->elf.dynamic_sections_created)
8999 sreloc = htab->reliplt;
eea6121a 9000 sreloc->size += p->count * sizeof (Elf64_External_Rela);
65f38f15
AM
9001 }
9002
b34976b6 9003 return TRUE;
65f38f15
AM
9004}
9005
9006/* Find any dynamic relocs that apply to read-only sections. */
9007
b34976b6 9008static bfd_boolean
4ce794b7 9009readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
65f38f15
AM
9010{
9011 struct ppc_link_hash_entry *eh;
6061a67d 9012 struct elf_dyn_relocs *p;
65f38f15
AM
9013
9014 eh = (struct ppc_link_hash_entry *) h;
9015 for (p = eh->dyn_relocs; p != NULL; p = p->next)
9016 {
9017 asection *s = p->sec->output_section;
9018
9019 if (s != NULL && (s->flags & SEC_READONLY) != 0)
9020 {
4ce794b7 9021 struct bfd_link_info *info = inf;
65f38f15
AM
9022
9023 info->flags |= DF_TEXTREL;
9024
9025 /* Not an error, just cut short the traversal. */
b34976b6 9026 return FALSE;
65f38f15
AM
9027 }
9028 }
b34976b6 9029 return TRUE;
65f38f15
AM
9030}
9031
9032/* Set the sizes of the dynamic sections. */
9033
b34976b6 9034static bfd_boolean
4ce794b7
AM
9035ppc64_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
9036 struct bfd_link_info *info)
65f38f15
AM
9037{
9038 struct ppc_link_hash_table *htab;
9039 bfd *dynobj;
9040 asection *s;
b34976b6 9041 bfd_boolean relocs;
65f38f15 9042 bfd *ibfd;
7865406b 9043 struct got_entry *first_tlsld;
65f38f15
AM
9044
9045 htab = ppc_hash_table (info);
4dfe6ac6
NC
9046 if (htab == NULL)
9047 return FALSE;
9048
65f38f15
AM
9049 dynobj = htab->elf.dynobj;
9050 if (dynobj == NULL)
9051 abort ();
9052
9053 if (htab->elf.dynamic_sections_created)
9054 {
9055 /* Set the contents of the .interp section to the interpreter. */
36af4a4e 9056 if (info->executable)
65f38f15
AM
9057 {
9058 s = bfd_get_section_by_name (dynobj, ".interp");
9059 if (s == NULL)
9060 abort ();
eea6121a 9061 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
65f38f15
AM
9062 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
9063 }
9064 }
9065
9066 /* Set up .got offsets for local syms, and space for local dynamic
9067 relocs. */
9068 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
9069 {
411e1bfb
AM
9070 struct got_entry **lgot_ents;
9071 struct got_entry **end_lgot_ents;
e054468f
AM
9072 struct plt_entry **local_plt;
9073 struct plt_entry **end_local_plt;
f961d9dd 9074 unsigned char *lgot_masks;
65f38f15
AM
9075 bfd_size_type locsymcount;
9076 Elf_Internal_Shdr *symtab_hdr;
9077 asection *srel;
9078
0c8d6e5c 9079 if (!is_ppc64_elf (ibfd))
65f38f15
AM
9080 continue;
9081
9082 for (s = ibfd->sections; s != NULL; s = s->next)
9083 {
6061a67d 9084 struct elf_dyn_relocs *p;
65f38f15 9085
6edfbbad 9086 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
65f38f15 9087 {
ec338859
AM
9088 if (!bfd_is_abs_section (p->sec)
9089 && bfd_is_abs_section (p->sec->output_section))
9090 {
9091 /* Input section has been discarded, either because
9092 it is a copy of a linkonce section or due to
9093 linker script /DISCARD/, so we'll be discarding
9094 the relocs too. */
9095 }
248866a8 9096 else if (p->count != 0)
ec338859
AM
9097 {
9098 srel = elf_section_data (p->sec)->sreloc;
25f23106
AM
9099 if (!htab->elf.dynamic_sections_created)
9100 srel = htab->reliplt;
eea6121a 9101 srel->size += p->count * sizeof (Elf64_External_Rela);
248866a8
AM
9102 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
9103 info->flags |= DF_TEXTREL;
ec338859 9104 }
65f38f15
AM
9105 }
9106 }
9107
411e1bfb
AM
9108 lgot_ents = elf_local_got_ents (ibfd);
9109 if (!lgot_ents)
65f38f15
AM
9110 continue;
9111
0ffa91dd 9112 symtab_hdr = &elf_symtab_hdr (ibfd);
65f38f15 9113 locsymcount = symtab_hdr->sh_info;
411e1bfb 9114 end_lgot_ents = lgot_ents + locsymcount;
e054468f
AM
9115 local_plt = (struct plt_entry **) end_lgot_ents;
9116 end_local_plt = local_plt + locsymcount;
f961d9dd 9117 lgot_masks = (unsigned char *) end_local_plt;
e717da7e
AM
9118 s = ppc64_elf_tdata (ibfd)->got;
9119 srel = ppc64_elf_tdata (ibfd)->relgot;
e7b938ca 9120 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
65f38f15 9121 {
0b8bcf0d 9122 struct got_entry **pent, *ent;
411e1bfb 9123
0b8bcf0d
AM
9124 pent = lgot_ents;
9125 while ((ent = *pent) != NULL)
411e1bfb
AM
9126 if (ent->got.refcount > 0)
9127 {
e7b938ca 9128 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
411e1bfb 9129 {
927be08e 9130 ppc64_tlsld_got (ibfd)->got.refcount += 1;
0b8bcf0d 9131 *pent = ent->next;
411e1bfb
AM
9132 }
9133 else
9134 {
25f23106 9135 unsigned int num = 1;
eea6121a 9136 ent->got.offset = s->size;
e7b938ca 9137 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
25f23106
AM
9138 num = 2;
9139 s->size += num * 8;
9140 if (info->shared)
9141 srel->size += num * sizeof (Elf64_External_Rela);
9142 else if ((*lgot_masks & PLT_IFUNC) != 0)
927be08e
AM
9143 {
9144 htab->reliplt->size
9145 += num * sizeof (Elf64_External_Rela);
9146 htab->got_reli_size
9147 += num * sizeof (Elf64_External_Rela);
9148 }
0b8bcf0d 9149 pent = &ent->next;
411e1bfb
AM
9150 }
9151 }
9152 else
0b8bcf0d 9153 *pent = ent->next;
65f38f15 9154 }
e054468f
AM
9155
9156 /* Allocate space for calls to local STT_GNU_IFUNC syms in .iplt. */
9157 for (; local_plt < end_local_plt; ++local_plt)
9158 {
9159 struct plt_entry *ent;
9160
9161 for (ent = *local_plt; ent != NULL; ent = ent->next)
9162 if (ent->plt.refcount > 0)
9163 {
91d6fa6a 9164 s = htab->iplt;
e054468f
AM
9165 ent->plt.offset = s->size;
9166 s->size += PLT_ENTRY_SIZE;
9167
25f23106 9168 htab->reliplt->size += sizeof (Elf64_External_Rela);
e054468f
AM
9169 }
9170 else
9171 ent->plt.offset = (bfd_vma) -1;
9172 }
65f38f15
AM
9173 }
9174
9175 /* Allocate global sym .plt and .got entries, and space for global
9176 sym dynamic relocs. */
4ce794b7 9177 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
65f38f15 9178
7865406b 9179 first_tlsld = NULL;
102890f0
AM
9180 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
9181 {
7865406b
AM
9182 struct got_entry *ent;
9183
0c8d6e5c 9184 if (!is_ppc64_elf (ibfd))
102890f0
AM
9185 continue;
9186
7865406b
AM
9187 ent = ppc64_tlsld_got (ibfd);
9188 if (ent->got.refcount > 0)
102890f0 9189 {
7865406b 9190 if (!htab->do_multi_toc && first_tlsld != NULL)
102890f0 9191 {
7865406b
AM
9192 ent->is_indirect = TRUE;
9193 ent->got.ent = first_tlsld;
9194 }
9195 else
9196 {
9197 if (first_tlsld == NULL)
9198 first_tlsld = ent;
9199 s = ppc64_elf_tdata (ibfd)->got;
9200 ent->got.offset = s->size;
9201 ent->owner = ibfd;
9202 s->size += 16;
9203 if (info->shared)
9204 {
9205 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9206 srel->size += sizeof (Elf64_External_Rela);
9207 }
102890f0
AM
9208 }
9209 }
9210 else
7865406b 9211 ent->got.offset = (bfd_vma) -1;
102890f0
AM
9212 }
9213
65f38f15
AM
9214 /* We now have determined the sizes of the various dynamic sections.
9215 Allocate memory for them. */
b34976b6 9216 relocs = FALSE;
65f38f15
AM
9217 for (s = dynobj->sections; s != NULL; s = s->next)
9218 {
9219 if ((s->flags & SEC_LINKER_CREATED) == 0)
9220 continue;
9221
4ce794b7 9222 if (s == htab->brlt || s == htab->relbrlt)
721956f4
AM
9223 /* These haven't been allocated yet; don't strip. */
9224 continue;
e717da7e
AM
9225 else if (s == htab->got
9226 || s == htab->plt
e054468f 9227 || s == htab->iplt
c456f082
AM
9228 || s == htab->glink
9229 || s == htab->dynbss)
65f38f15
AM
9230 {
9231 /* Strip this section if we don't need it; see the
9232 comment below. */
5bd4f169 9233 }
58d180e8
AM
9234 else if (s == htab->glink_eh_frame)
9235 {
9236 if (!bfd_is_abs_section (s->output_section))
9237 /* Not sized yet. */
9238 continue;
9239 }
70cc837d 9240 else if (CONST_STRNEQ (s->name, ".rela"))
5bd4f169 9241 {
c456f082 9242 if (s->size != 0)
5bd4f169 9243 {
4ce794b7 9244 if (s != htab->relplt)
b34976b6 9245 relocs = TRUE;
5bd4f169
AM
9246
9247 /* We use the reloc_count field as a counter if we need
9248 to copy relocs into the output file. */
9249 s->reloc_count = 0;
9250 }
9251 }
65f38f15 9252 else
5bd4f169
AM
9253 {
9254 /* It's not one of our sections, so don't allocate space. */
9255 continue;
9256 }
9257
eea6121a 9258 if (s->size == 0)
5bd4f169 9259 {
c456f082
AM
9260 /* If we don't need this section, strip it from the
9261 output file. This is mostly to handle .rela.bss and
9262 .rela.plt. We must create both sections in
9263 create_dynamic_sections, because they must be created
9264 before the linker maps input sections to output
9265 sections. The linker does that before
9266 adjust_dynamic_symbol is called, and it is that
9267 function which decides whether anything needs to go
9268 into these sections. */
8423293d 9269 s->flags |= SEC_EXCLUDE;
5bd4f169
AM
9270 continue;
9271 }
9272
c456f082 9273 if ((s->flags & SEC_HAS_CONTENTS) == 0)
5f333394
AM
9274 continue;
9275
65f38f15
AM
9276 /* Allocate memory for the section contents. We use bfd_zalloc
9277 here in case unused entries are not reclaimed before the
9278 section's contents are written out. This should not happen,
411e1bfb
AM
9279 but this way if it does we get a R_PPC64_NONE reloc in .rela
9280 sections instead of garbage.
9281 We also rely on the section contents being zero when writing
9282 the GOT. */
eea6121a 9283 s->contents = bfd_zalloc (dynobj, s->size);
65f38f15 9284 if (s->contents == NULL)
b34976b6 9285 return FALSE;
5bd4f169
AM
9286 }
9287
e717da7e
AM
9288 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
9289 {
0c8d6e5c 9290 if (!is_ppc64_elf (ibfd))
7b53ace3
AM
9291 continue;
9292
e717da7e
AM
9293 s = ppc64_elf_tdata (ibfd)->got;
9294 if (s != NULL && s != htab->got)
9295 {
eea6121a 9296 if (s->size == 0)
8423293d 9297 s->flags |= SEC_EXCLUDE;
e717da7e
AM
9298 else
9299 {
eea6121a 9300 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
9301 if (s->contents == NULL)
9302 return FALSE;
9303 }
9304 }
9305 s = ppc64_elf_tdata (ibfd)->relgot;
9306 if (s != NULL)
9307 {
eea6121a 9308 if (s->size == 0)
8423293d 9309 s->flags |= SEC_EXCLUDE;
e717da7e
AM
9310 else
9311 {
eea6121a 9312 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
9313 if (s->contents == NULL)
9314 return FALSE;
9315 relocs = TRUE;
9316 s->reloc_count = 0;
9317 }
9318 }
9319 }
9320
e86ce104 9321 if (htab->elf.dynamic_sections_created)
5bd4f169
AM
9322 {
9323 /* Add some entries to the .dynamic section. We fill in the
9324 values later, in ppc64_elf_finish_dynamic_sections, but we
9325 must add the entries now so that we get the correct size for
9326 the .dynamic section. The DT_DEBUG entry is filled in by the
9327 dynamic linker and used by the debugger. */
dc810e39 9328#define add_dynamic_entry(TAG, VAL) \
5a580b3a 9329 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
dc810e39 9330
36af4a4e 9331 if (info->executable)
5bd4f169 9332 {
dc810e39 9333 if (!add_dynamic_entry (DT_DEBUG, 0))
b34976b6 9334 return FALSE;
5bd4f169
AM
9335 }
9336
eea6121a 9337 if (htab->plt != NULL && htab->plt->size != 0)
5bd4f169 9338 {
dc810e39
AM
9339 if (!add_dynamic_entry (DT_PLTGOT, 0)
9340 || !add_dynamic_entry (DT_PLTRELSZ, 0)
9341 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
5d1634d7
AM
9342 || !add_dynamic_entry (DT_JMPREL, 0)
9343 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
b34976b6 9344 return FALSE;
5bd4f169
AM
9345 }
9346
19397422
AM
9347 if (NO_OPD_RELOCS)
9348 {
9349 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
9350 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
b34976b6 9351 return FALSE;
19397422
AM
9352 }
9353
a7f2871e
AM
9354 if (!htab->no_tls_get_addr_opt
9355 && htab->tls_get_addr_fd != NULL
9356 && htab->tls_get_addr_fd->elf.plt.plist != NULL
9357 && !add_dynamic_entry (DT_PPC64_TLSOPT, 0))
9358 return FALSE;
9359
5bd4f169
AM
9360 if (relocs)
9361 {
dc810e39
AM
9362 if (!add_dynamic_entry (DT_RELA, 0)
9363 || !add_dynamic_entry (DT_RELASZ, 0)
9364 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
b34976b6 9365 return FALSE;
5bd4f169 9366
65f38f15
AM
9367 /* If any dynamic relocs apply to a read-only section,
9368 then we need a DT_TEXTREL entry. */
248866a8 9369 if ((info->flags & DF_TEXTREL) == 0)
4ce794b7 9370 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs, info);
5bd4f169 9371
65f38f15 9372 if ((info->flags & DF_TEXTREL) != 0)
5bd4f169 9373 {
65f38f15 9374 if (!add_dynamic_entry (DT_TEXTREL, 0))
b34976b6 9375 return FALSE;
5bd4f169 9376 }
5bd4f169 9377 }
5bd4f169 9378 }
65f38f15 9379#undef add_dynamic_entry
5bd4f169 9380
b34976b6 9381 return TRUE;
5bd4f169
AM
9382}
9383
721956f4 9384/* Determine the type of stub needed, if any, for a call. */
5bd4f169 9385
4ce794b7
AM
9386static inline enum ppc_stub_type
9387ppc_type_of_stub (asection *input_sec,
9388 const Elf_Internal_Rela *rel,
9389 struct ppc_link_hash_entry **hash,
e054468f 9390 struct plt_entry **plt_ent,
4ce794b7 9391 bfd_vma destination)
5bd4f169 9392{
721956f4
AM
9393 struct ppc_link_hash_entry *h = *hash;
9394 bfd_vma location;
9395 bfd_vma branch_offset;
9396 bfd_vma max_branch_offset;
4ce794b7 9397 enum elf_ppc64_reloc_type r_type;
5bd4f169 9398
721956f4
AM
9399 if (h != NULL)
9400 {
e054468f 9401 struct plt_entry *ent;
7fe2b9a6 9402 struct ppc_link_hash_entry *fdh = h;
b31867b6
AM
9403 if (h->oh != NULL
9404 && h->oh->is_func_descriptor)
7b8f6675
AM
9405 {
9406 fdh = ppc_follow_link (h->oh);
9407 *hash = fdh;
9408 }
8387904d 9409
e054468f
AM
9410 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
9411 if (ent->addend == rel->r_addend
9412 && ent->plt.offset != (bfd_vma) -1)
9413 {
e054468f
AM
9414 *plt_ent = ent;
9415 return ppc_stub_plt_call;
9416 }
5bd4f169 9417
7fe2b9a6
AM
9418 /* Here, we know we don't have a plt entry. If we don't have a
9419 either a defined function descriptor or a defined entry symbol
9420 in a regular object file, then it is pointless trying to make
9421 any other type of stub. */
854b41e7
AM
9422 if (!is_static_defined (&fdh->elf)
9423 && !is_static_defined (&h->elf))
721956f4 9424 return ppc_stub_none;
5d1634d7 9425 }
e054468f
AM
9426 else if (elf_local_got_ents (input_sec->owner) != NULL)
9427 {
9428 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
9429 struct plt_entry **local_plt = (struct plt_entry **)
9430 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
9431 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
9432
9433 if (local_plt[r_symndx] != NULL)
9434 {
9435 struct plt_entry *ent;
9436
9437 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
9438 if (ent->addend == rel->r_addend
9439 && ent->plt.offset != (bfd_vma) -1)
9440 {
9441 *plt_ent = ent;
9442 return ppc_stub_plt_call;
9443 }
9444 }
9445 }
5d1634d7 9446
721956f4
AM
9447 /* Determine where the call point is. */
9448 location = (input_sec->output_offset
9449 + input_sec->output_section->vma
9450 + rel->r_offset);
5d1634d7 9451
721956f4
AM
9452 branch_offset = destination - location;
9453 r_type = ELF64_R_TYPE (rel->r_info);
5d1634d7 9454
721956f4
AM
9455 /* Determine if a long branch stub is needed. */
9456 max_branch_offset = 1 << 25;
4ce794b7 9457 if (r_type != R_PPC64_REL24)
721956f4 9458 max_branch_offset = 1 << 15;
5d1634d7 9459
721956f4
AM
9460 if (branch_offset + max_branch_offset >= 2 * max_branch_offset)
9461 /* We need a stub. Figure out whether a long_branch or plt_branch
9462 is needed later. */
9463 return ppc_stub_long_branch;
5d1634d7 9464
721956f4 9465 return ppc_stub_none;
5d1634d7
AM
9466}
9467
9468/* Build a .plt call stub. */
9469
4ce794b7 9470static inline bfd_byte *
9df0ef5f
AM
9471build_plt_stub (bfd *obfd, bfd_byte *p, int offset, Elf_Internal_Rela *r,
9472 bfd_boolean plt_static_chain)
5d1634d7
AM
9473{
9474#define PPC_LO(v) ((v) & 0xffff)
9475#define PPC_HI(v) (((v) >> 16) & 0xffff)
9476#define PPC_HA(v) PPC_HI ((v) + 0x8000)
9477
ac2df442
AM
9478 if (PPC_HA (offset) != 0)
9479 {
176a0d42
AM
9480 if (r != NULL)
9481 {
3b421ab3 9482 r[0].r_offset += 4;
176a0d42 9483 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
3b421ab3 9484 r[1].r_offset = r[0].r_offset + 4;
176a0d42
AM
9485 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9486 r[1].r_addend = r[0].r_addend;
c7131b65 9487 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
176a0d42
AM
9488 {
9489 r[2].r_offset = r[1].r_offset + 4;
9490 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
9491 r[2].r_addend = r[0].r_addend;
9492 }
9493 else
9494 {
9495 r[2].r_offset = r[1].r_offset + 8;
9496 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9497 r[2].r_addend = r[0].r_addend + 8;
c7131b65
AM
9498 if (plt_static_chain)
9499 {
9500 r[3].r_offset = r[2].r_offset + 4;
9501 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9502 r[3].r_addend = r[0].r_addend + 16;
9503 }
176a0d42
AM
9504 }
9505 }
ac2df442 9506 bfd_put_32 (obfd, STD_R2_40R1, p), p += 4;
3b421ab3 9507 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
ac2df442 9508 bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset), p), p += 4;
9df0ef5f 9509 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442
AM
9510 {
9511 bfd_put_32 (obfd, ADDI_R12_R12 | PPC_LO (offset), p), p += 4;
9512 offset = 0;
9513 }
9514 bfd_put_32 (obfd, MTCTR_R11, p), p += 4;
9515 bfd_put_32 (obfd, LD_R2_0R12 | PPC_LO (offset + 8), p), p += 4;
9df0ef5f
AM
9516 if (plt_static_chain)
9517 bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset + 16), p), p += 4;
ac2df442
AM
9518 bfd_put_32 (obfd, BCTR, p), p += 4;
9519 }
9520 else
9521 {
176a0d42
AM
9522 if (r != NULL)
9523 {
9524 r[0].r_offset += 4;
9525 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
c7131b65 9526 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
176a0d42
AM
9527 {
9528 r[1].r_offset = r[0].r_offset + 4;
9529 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
9530 r[1].r_addend = r[0].r_addend;
9531 }
9532 else
9533 {
9534 r[1].r_offset = r[0].r_offset + 8;
9535 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
c7131b65
AM
9536 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
9537 if (plt_static_chain)
9538 {
9539 r[2].r_offset = r[1].r_offset + 4;
9540 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
9541 r[2].r_addend = r[0].r_addend + 8;
9542 }
176a0d42
AM
9543 }
9544 }
ac2df442
AM
9545 bfd_put_32 (obfd, STD_R2_40R1, p), p += 4;
9546 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset), p), p += 4;
9df0ef5f 9547 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442
AM
9548 {
9549 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
9550 offset = 0;
9551 }
9552 bfd_put_32 (obfd, MTCTR_R11, p), p += 4;
9df0ef5f
AM
9553 if (plt_static_chain)
9554 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
ac2df442
AM
9555 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
9556 bfd_put_32 (obfd, BCTR, p), p += 4;
9557 }
5d1634d7
AM
9558 return p;
9559}
9560
a7f2871e
AM
9561/* Build a special .plt call stub for __tls_get_addr. */
9562
9563#define LD_R11_0R3 0xe9630000
9564#define LD_R12_0R3 0xe9830000
9565#define MR_R0_R3 0x7c601b78
9566#define CMPDI_R11_0 0x2c2b0000
9567#define ADD_R3_R12_R13 0x7c6c6a14
9568#define BEQLR 0x4d820020
9569#define MR_R3_R0 0x7c030378
9570#define MFLR_R11 0x7d6802a6
9571#define STD_R11_0R1 0xf9610000
9572#define BCTRL 0x4e800421
9573#define LD_R11_0R1 0xe9610000
9574#define LD_R2_0R1 0xe8410000
9575#define MTLR_R11 0x7d6803a6
9576
9577static inline bfd_byte *
9578build_tls_get_addr_stub (bfd *obfd, bfd_byte *p, int offset,
9df0ef5f 9579 Elf_Internal_Rela *r, bfd_boolean plt_static_chain)
a7f2871e
AM
9580{
9581 bfd_put_32 (obfd, LD_R11_0R3 + 0, p), p += 4;
9582 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
9583 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
9584 bfd_put_32 (obfd, CMPDI_R11_0, p), p += 4;
9585 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
9586 bfd_put_32 (obfd, BEQLR, p), p += 4;
9587 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
9588 bfd_put_32 (obfd, MFLR_R11, p), p += 4;
9589 bfd_put_32 (obfd, STD_R11_0R1 + 32, p), p += 4;
9590
9591 if (r != NULL)
9592 r[0].r_offset += 9 * 4;
9df0ef5f 9593 p = build_plt_stub (obfd, p, offset, r, plt_static_chain);
a7f2871e
AM
9594 bfd_put_32 (obfd, BCTRL, p - 4);
9595
9596 bfd_put_32 (obfd, LD_R11_0R1 + 32, p), p += 4;
9597 bfd_put_32 (obfd, LD_R2_0R1 + 40, p), p += 4;
9598 bfd_put_32 (obfd, MTLR_R11, p), p += 4;
9599 bfd_put_32 (obfd, BLR, p), p += 4;
9600
9601 return p;
9602}
9603
176a0d42
AM
9604static Elf_Internal_Rela *
9605get_relocs (asection *sec, int count)
9606{
9607 Elf_Internal_Rela *relocs;
9608 struct bfd_elf_section_data *elfsec_data;
9609
9610 elfsec_data = elf_section_data (sec);
9611 relocs = elfsec_data->relocs;
9612 if (relocs == NULL)
9613 {
9614 bfd_size_type relsize;
9615 relsize = sec->reloc_count * sizeof (*relocs);
9616 relocs = bfd_alloc (sec->owner, relsize);
9617 if (relocs == NULL)
9618 return NULL;
9619 elfsec_data->relocs = relocs;
d4730f92
BS
9620 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
9621 sizeof (Elf_Internal_Shdr));
9622 if (elfsec_data->rela.hdr == NULL)
9623 return NULL;
9624 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
9625 * sizeof (Elf64_External_Rela));
9626 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
176a0d42
AM
9627 sec->reloc_count = 0;
9628 }
9629 relocs += sec->reloc_count;
9630 sec->reloc_count += count;
9631 return relocs;
9632}
9633
aa374f67 9634static bfd_vma
25f53a85 9635get_r2off (struct bfd_link_info *info,
aa374f67
AM
9636 struct ppc_stub_hash_entry *stub_entry)
9637{
25f53a85 9638 struct ppc_link_hash_table *htab = ppc_hash_table (info);
aa374f67
AM
9639 bfd_vma r2off = htab->stub_group[stub_entry->target_section->id].toc_off;
9640
9641 if (r2off == 0)
9642 {
9643 /* Support linking -R objects. Get the toc pointer from the
9644 opd entry. */
9645 char buf[8];
9646 asection *opd = stub_entry->h->elf.root.u.def.section;
9647 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
9648
9649 if (strcmp (opd->name, ".opd") != 0
9650 || opd->reloc_count != 0)
9651 {
8de848d8 9652 info->callbacks->einfo (_("%P: cannot find opd entry toc for %s\n"),
25f53a85 9653 stub_entry->h->elf.root.root.string);
aa374f67
AM
9654 bfd_set_error (bfd_error_bad_value);
9655 return 0;
9656 }
9657 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
9658 return 0;
9659 r2off = bfd_get_64 (opd->owner, buf);
25f53a85 9660 r2off -= elf_gp (info->output_bfd);
aa374f67
AM
9661 }
9662 r2off -= htab->stub_group[stub_entry->id_sec->id].toc_off;
9663 return r2off;
9664}
9665
b34976b6 9666static bfd_boolean
4ce794b7 9667ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
5d1634d7 9668{
721956f4
AM
9669 struct ppc_stub_hash_entry *stub_entry;
9670 struct ppc_branch_hash_entry *br_entry;
5d1634d7
AM
9671 struct bfd_link_info *info;
9672 struct ppc_link_hash_table *htab;
721956f4
AM
9673 bfd_byte *loc;
9674 bfd_byte *p;
ee75fd95 9675 bfd_vma dest, off;
721956f4 9676 int size;
176a0d42 9677 Elf_Internal_Rela *r;
e054468f 9678 asection *plt;
5d1634d7 9679
721956f4
AM
9680 /* Massage our args to the form they really have. */
9681 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
4ce794b7 9682 info = in_arg;
5d1634d7 9683
5d1634d7 9684 htab = ppc_hash_table (info);
4dfe6ac6
NC
9685 if (htab == NULL)
9686 return FALSE;
5d1634d7 9687
721956f4 9688 /* Make a note of the offset within the stubs for this entry. */
eea6121a 9689 stub_entry->stub_offset = stub_entry->stub_sec->size;
97b639ba 9690 loc = stub_entry->stub_sec->contents + stub_entry->stub_offset;
721956f4 9691
4ce794b7 9692 htab->stub_count[stub_entry->stub_type - 1] += 1;
721956f4 9693 switch (stub_entry->stub_type)
5d1634d7 9694 {
721956f4 9695 case ppc_stub_long_branch:
ad8e1ba5 9696 case ppc_stub_long_branch_r2off:
721956f4 9697 /* Branches are relative. This is where we are going to. */
ee75fd95
AM
9698 off = dest = (stub_entry->target_value
9699 + stub_entry->target_section->output_offset
9700 + stub_entry->target_section->output_section->vma);
5d1634d7 9701
721956f4
AM
9702 /* And this is where we are coming from. */
9703 off -= (stub_entry->stub_offset
97b639ba
AM
9704 + stub_entry->stub_sec->output_offset
9705 + stub_entry->stub_sec->output_section->vma);
e86ce104 9706
ac2df442
AM
9707 size = 4;
9708 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
ad8e1ba5 9709 {
25f53a85 9710 bfd_vma r2off = get_r2off (info, stub_entry);
ad8e1ba5 9711
aa374f67
AM
9712 if (r2off == 0)
9713 {
9714 htab->stub_error = TRUE;
9715 return FALSE;
9716 }
97b639ba 9717 bfd_put_32 (htab->stub_bfd, STD_R2_40R1, loc);
ad8e1ba5 9718 loc += 4;
ac2df442
AM
9719 size = 12;
9720 if (PPC_HA (r2off) != 0)
9721 {
9722 size = 16;
9723 bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
9724 loc += 4;
9725 }
97b639ba 9726 bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
ad8e1ba5 9727 loc += 4;
ac2df442 9728 off -= size - 4;
ad8e1ba5 9729 }
97b639ba 9730 bfd_put_32 (htab->stub_bfd, B_DOT | (off & 0x3fffffc), loc);
ad8e1ba5 9731
5c3dead3
AM
9732 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
9733 {
8de848d8 9734 info->callbacks->einfo (_("%P: long branch stub `%s' offset overflow\n"),
25f53a85 9735 stub_entry->root.string);
5c3dead3
AM
9736 htab->stub_error = TRUE;
9737 return FALSE;
9738 }
ee75fd95
AM
9739
9740 if (info->emitrelocations)
9741 {
176a0d42
AM
9742 r = get_relocs (stub_entry->stub_sec, 1);
9743 if (r == NULL)
9744 return FALSE;
ee75fd95
AM
9745 r->r_offset = loc - stub_entry->stub_sec->contents;
9746 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
9747 r->r_addend = dest;
9748 if (stub_entry->h != NULL)
9749 {
9750 struct elf_link_hash_entry **hashes;
9751 unsigned long symndx;
9752 struct ppc_link_hash_entry *h;
9753
9754 hashes = elf_sym_hashes (htab->stub_bfd);
9755 if (hashes == NULL)
9756 {
9757 bfd_size_type hsize;
9758
9759 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
9760 hashes = bfd_zalloc (htab->stub_bfd, hsize);
9761 if (hashes == NULL)
9762 return FALSE;
9763 elf_sym_hashes (htab->stub_bfd) = hashes;
9764 htab->stub_globals = 1;
9765 }
9766 symndx = htab->stub_globals++;
9767 h = stub_entry->h;
9768 hashes[symndx] = &h->elf;
9769 r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
9770 if (h->oh != NULL && h->oh->is_func)
b31867b6 9771 h = ppc_follow_link (h->oh);
ee75fd95
AM
9772 if (h->elf.root.u.def.section != stub_entry->target_section)
9773 /* H is an opd symbol. The addend must be zero. */
9774 r->r_addend = 0;
9775 else
9776 {
9777 off = (h->elf.root.u.def.value
9778 + h->elf.root.u.def.section->output_offset
9779 + h->elf.root.u.def.section->output_section->vma);
9780 r->r_addend -= off;
9781 }
9782 }
9783 }
721956f4 9784 break;
e86ce104 9785
721956f4 9786 case ppc_stub_plt_branch:
ad8e1ba5 9787 case ppc_stub_plt_branch_r2off:
721956f4
AM
9788 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
9789 stub_entry->root.string + 9,
b34976b6 9790 FALSE, FALSE);
721956f4
AM
9791 if (br_entry == NULL)
9792 {
8de848d8 9793 info->callbacks->einfo (_("%P: can't find branch stub `%s'\n"),
25f53a85 9794 stub_entry->root.string);
b34976b6
AM
9795 htab->stub_error = TRUE;
9796 return FALSE;
721956f4
AM
9797 }
9798
176a0d42
AM
9799 dest = (stub_entry->target_value
9800 + stub_entry->target_section->output_offset
9801 + stub_entry->target_section->output_section->vma);
721956f4 9802
176a0d42 9803 bfd_put_64 (htab->brlt->owner, dest,
4ce794b7 9804 htab->brlt->contents + br_entry->offset);
721956f4 9805
f94498ff 9806 if (br_entry->iter == htab->stub_iteration)
721956f4 9807 {
f94498ff 9808 br_entry->iter = 0;
84f5d08e 9809
f94498ff 9810 if (htab->relbrlt != NULL)
84f5d08e 9811 {
f94498ff
AM
9812 /* Create a reloc for the branch lookup table entry. */
9813 Elf_Internal_Rela rela;
9814 bfd_byte *rl;
9815
9816 rela.r_offset = (br_entry->offset
9817 + htab->brlt->output_offset
9818 + htab->brlt->output_section->vma);
9819 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
176a0d42 9820 rela.r_addend = dest;
f94498ff
AM
9821
9822 rl = htab->relbrlt->contents;
9823 rl += (htab->relbrlt->reloc_count++
9824 * sizeof (Elf64_External_Rela));
9825 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
9826 }
9827 else if (info->emitrelocations)
9828 {
176a0d42
AM
9829 r = get_relocs (htab->brlt, 1);
9830 if (r == NULL)
9831 return FALSE;
9832 /* brlt, being SEC_LINKER_CREATED does not go through the
9833 normal reloc processing. Symbols and offsets are not
9834 translated from input file to output file form, so
9835 set up the offset per the output file. */
f94498ff
AM
9836 r->r_offset = (br_entry->offset
9837 + htab->brlt->output_offset
9838 + htab->brlt->output_section->vma);
9839 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
176a0d42 9840 r->r_addend = dest;
84f5d08e 9841 }
84f5d08e 9842 }
721956f4 9843
176a0d42
AM
9844 dest = (br_entry->offset
9845 + htab->brlt->output_offset
9846 + htab->brlt->output_section->vma);
9847
9848 off = (dest
4ce794b7 9849 - elf_gp (htab->brlt->output_section->owner)
ad8e1ba5 9850 - htab->stub_group[stub_entry->id_sec->id].toc_off);
721956f4 9851
ad8e1ba5 9852 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
5d1634d7 9853 {
25f53a85 9854 info->callbacks->einfo
8de848d8 9855 (_("%P: linkage table error against `%s'\n"),
721956f4 9856 stub_entry->root.string);
5d1634d7 9857 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
9858 htab->stub_error = TRUE;
9859 return FALSE;
5d1634d7 9860 }
41bd81ab 9861
176a0d42
AM
9862 if (info->emitrelocations)
9863 {
9864 r = get_relocs (stub_entry->stub_sec, 1 + (PPC_HA (off) != 0));
9865 if (r == NULL)
9866 return FALSE;
9867 r[0].r_offset = loc - stub_entry->stub_sec->contents;
7cfbafbc
AM
9868 if (bfd_big_endian (info->output_bfd))
9869 r[0].r_offset += 2;
176a0d42
AM
9870 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
9871 r[0].r_offset += 4;
9872 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
9873 r[0].r_addend = dest;
9874 if (PPC_HA (off) != 0)
9875 {
9876 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
9877 r[1].r_offset = r[0].r_offset + 4;
9878 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9879 r[1].r_addend = r[0].r_addend;
9880 }
9881 }
9882
ad8e1ba5
AM
9883 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
9884 {
176a0d42 9885 if (PPC_HA (off) != 0)
ac2df442
AM
9886 {
9887 size = 16;
176a0d42 9888 bfd_put_32 (htab->stub_bfd, ADDIS_R12_R2 | PPC_HA (off), loc);
ac2df442 9889 loc += 4;
176a0d42 9890 bfd_put_32 (htab->stub_bfd, LD_R11_0R12 | PPC_LO (off), loc);
ac2df442
AM
9891 }
9892 else
9893 {
9894 size = 12;
176a0d42 9895 bfd_put_32 (htab->stub_bfd, LD_R11_0R2 | PPC_LO (off), loc);
ac2df442 9896 }
ad8e1ba5
AM
9897 }
9898 else
9899 {
25f53a85 9900 bfd_vma r2off = get_r2off (info, stub_entry);
aa374f67
AM
9901
9902 if (r2off == 0)
9903 {
9904 htab->stub_error = TRUE;
9905 return FALSE;
9906 }
ad8e1ba5 9907
97b639ba 9908 bfd_put_32 (htab->stub_bfd, STD_R2_40R1, loc);
ad8e1ba5 9909 loc += 4;
ac2df442 9910 size = 20;
176a0d42 9911 if (PPC_HA (off) != 0)
ac2df442
AM
9912 {
9913 size += 4;
176a0d42 9914 bfd_put_32 (htab->stub_bfd, ADDIS_R12_R2 | PPC_HA (off), loc);
ac2df442 9915 loc += 4;
176a0d42 9916 bfd_put_32 (htab->stub_bfd, LD_R11_0R12 | PPC_LO (off), loc);
ac2df442
AM
9917 loc += 4;
9918 }
9919 else
9920 {
176a0d42 9921 bfd_put_32 (htab->stub_bfd, LD_R11_0R2 | PPC_LO (off), loc);
ac2df442
AM
9922 loc += 4;
9923 }
9924
9925 if (PPC_HA (r2off) != 0)
9926 {
9927 size += 4;
9928 bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
9929 loc += 4;
9930 }
97b639ba 9931 bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
ad8e1ba5
AM
9932 }
9933 loc += 4;
97b639ba 9934 bfd_put_32 (htab->stub_bfd, MTCTR_R11, loc);
ad8e1ba5 9935 loc += 4;
97b639ba 9936 bfd_put_32 (htab->stub_bfd, BCTR, loc);
721956f4 9937 break;
5d1634d7 9938
721956f4 9939 case ppc_stub_plt_call:
e054468f 9940 if (stub_entry->h != NULL
b31867b6
AM
9941 && stub_entry->h->is_func_descriptor
9942 && stub_entry->h->oh != NULL)
c862ae31 9943 {
b31867b6
AM
9944 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
9945
9946 /* If the old-ABI "dot-symbol" is undefined make it weak so
9947 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL.
9948 FIXME: We used to define the symbol on one of the call
9949 stubs instead, which is why we test symbol section id
9950 against htab->top_id in various places. Likely all
9951 these checks could now disappear. */
9952 if (fh->elf.root.type == bfd_link_hash_undefined)
9953 fh->elf.root.type = bfd_link_hash_undefweak;
9507a174
AM
9954 /* Stop undo_symbol_twiddle changing it back to undefined. */
9955 fh->was_undefined = 0;
c862ae31
AM
9956 }
9957
721956f4 9958 /* Now build the stub. */
e054468f 9959 dest = stub_entry->plt_ent->plt.offset & ~1;
176a0d42 9960 if (dest >= (bfd_vma) -2)
721956f4
AM
9961 abort ();
9962
e054468f 9963 plt = htab->plt;
25f23106
AM
9964 if (!htab->elf.dynamic_sections_created
9965 || stub_entry->h == NULL
9966 || stub_entry->h->elf.dynindx == -1)
e054468f
AM
9967 plt = htab->iplt;
9968
9969 dest += plt->output_offset + plt->output_section->vma;
9970
9971 if (stub_entry->h == NULL
9972 && (stub_entry->plt_ent->plt.offset & 1) == 0)
9973 {
9974 Elf_Internal_Rela rela;
9975 bfd_byte *rl;
9976
9977 rela.r_offset = dest;
25f23106 9978 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
e054468f
AM
9979 rela.r_addend = (stub_entry->target_value
9980 + stub_entry->target_section->output_offset
9981 + stub_entry->target_section->output_section->vma);
9982
25f23106
AM
9983 rl = (htab->reliplt->contents
9984 + (htab->reliplt->reloc_count++
9985 * sizeof (Elf64_External_Rela)));
9986 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, rl);
e054468f
AM
9987 stub_entry->plt_ent->plt.offset |= 1;
9988 }
176a0d42
AM
9989
9990 off = (dest
e054468f 9991 - elf_gp (plt->output_section->owner)
176a0d42 9992 - htab->stub_group[stub_entry->id_sec->id].toc_off);
721956f4 9993
ad8e1ba5 9994 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
721956f4 9995 {
25f53a85 9996 info->callbacks->einfo
8de848d8 9997 (_("%P: linkage table error against `%s'\n"),
e054468f
AM
9998 stub_entry->h != NULL
9999 ? stub_entry->h->elf.root.root.string
10000 : "<local sym>");
721956f4 10001 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
10002 htab->stub_error = TRUE;
10003 return FALSE;
721956f4
AM
10004 }
10005
176a0d42
AM
10006 r = NULL;
10007 if (info->emitrelocations)
10008 {
10009 r = get_relocs (stub_entry->stub_sec,
c7131b65
AM
10010 (2
10011 + (PPC_HA (off) != 0)
10012 + (htab->plt_static_chain
10013 && PPC_HA (off + 16) == PPC_HA (off))));
176a0d42
AM
10014 if (r == NULL)
10015 return FALSE;
10016 r[0].r_offset = loc - stub_entry->stub_sec->contents;
7cfbafbc
AM
10017 if (bfd_big_endian (info->output_bfd))
10018 r[0].r_offset += 2;
176a0d42
AM
10019 r[0].r_addend = dest;
10020 }
a7f2871e
AM
10021 if (stub_entry->h != NULL
10022 && (stub_entry->h == htab->tls_get_addr_fd
10023 || stub_entry->h == htab->tls_get_addr)
10024 && !htab->no_tls_get_addr_opt)
9df0ef5f
AM
10025 p = build_tls_get_addr_stub (htab->stub_bfd, loc, off, r,
10026 htab->plt_static_chain);
a7f2871e 10027 else
9df0ef5f
AM
10028 p = build_plt_stub (htab->stub_bfd, loc, off, r,
10029 htab->plt_static_chain);
721956f4
AM
10030 size = p - loc;
10031 break;
10032
10033 default:
10034 BFD_FAIL ();
b34976b6 10035 return FALSE;
721956f4
AM
10036 }
10037
eea6121a 10038 stub_entry->stub_sec->size += size;
97b639ba 10039
ee75fd95 10040 if (htab->emit_stub_syms)
97b639ba
AM
10041 {
10042 struct elf_link_hash_entry *h;
ee75fd95
AM
10043 size_t len1, len2;
10044 char *name;
10045 const char *const stub_str[] = { "long_branch",
10046 "long_branch_r2off",
10047 "plt_branch",
10048 "plt_branch_r2off",
10049 "plt_call" };
10050
10051 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
10052 len2 = strlen (stub_entry->root.string);
10053 name = bfd_malloc (len1 + len2 + 2);
10054 if (name == NULL)
10055 return FALSE;
10056 memcpy (name, stub_entry->root.string, 9);
10057 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
10058 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
10059 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
97b639ba
AM
10060 if (h == NULL)
10061 return FALSE;
10062 if (h->root.type == bfd_link_hash_new)
10063 {
10064 h->root.type = bfd_link_hash_defined;
10065 h->root.u.def.section = stub_entry->stub_sec;
10066 h->root.u.def.value = stub_entry->stub_offset;
f5385ebf
AM
10067 h->ref_regular = 1;
10068 h->def_regular = 1;
10069 h->ref_regular_nonweak = 1;
10070 h->forced_local = 1;
10071 h->non_elf = 0;
97b639ba
AM
10072 }
10073 }
10074
b34976b6 10075 return TRUE;
721956f4
AM
10076}
10077
10078/* As above, but don't actually build the stub. Just bump offset so
10079 we know stub section sizes, and select plt_branch stubs where
10080 long_branch stubs won't do. */
10081
b34976b6 10082static bfd_boolean
4ce794b7 10083ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
721956f4
AM
10084{
10085 struct ppc_stub_hash_entry *stub_entry;
63bc6f6c 10086 struct bfd_link_info *info;
721956f4
AM
10087 struct ppc_link_hash_table *htab;
10088 bfd_vma off;
10089 int size;
10090
10091 /* Massage our args to the form they really have. */
10092 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
63bc6f6c
AM
10093 info = in_arg;
10094
10095 htab = ppc_hash_table (info);
4dfe6ac6
NC
10096 if (htab == NULL)
10097 return FALSE;
721956f4
AM
10098
10099 if (stub_entry->stub_type == ppc_stub_plt_call)
10100 {
e054468f
AM
10101 asection *plt;
10102 off = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
58ac9f71 10103 if (off >= (bfd_vma) -2)
411e1bfb 10104 abort ();
e054468f 10105 plt = htab->plt;
25f23106
AM
10106 if (!htab->elf.dynamic_sections_created
10107 || stub_entry->h == NULL
10108 || stub_entry->h->elf.dynindx == -1)
e054468f
AM
10109 plt = htab->iplt;
10110 off += (plt->output_offset
10111 + plt->output_section->vma
10112 - elf_gp (plt->output_section->owner)
ad8e1ba5 10113 - htab->stub_group[stub_entry->id_sec->id].toc_off);
721956f4 10114
ad8e1ba5 10115 size = PLT_CALL_STUB_SIZE;
9df0ef5f
AM
10116 if (!htab->plt_static_chain)
10117 size -= 4;
ac2df442
AM
10118 if (PPC_HA (off) == 0)
10119 size -= 4;
c7131b65 10120 if (PPC_HA (off + 8 + 8 * htab->plt_static_chain) != PPC_HA (off))
721956f4 10121 size += 4;
a7f2871e
AM
10122 if (stub_entry->h != NULL
10123 && (stub_entry->h == htab->tls_get_addr_fd
10124 || stub_entry->h == htab->tls_get_addr)
10125 && !htab->no_tls_get_addr_opt)
10126 size += 13 * 4;
176a0d42
AM
10127 if (info->emitrelocations)
10128 {
10129 stub_entry->stub_sec->reloc_count
c7131b65
AM
10130 += (2
10131 + (PPC_HA (off) != 0)
10132 + (htab->plt_static_chain
10133 && PPC_HA (off + 16) == PPC_HA (off)));
176a0d42
AM
10134 stub_entry->stub_sec->flags |= SEC_RELOC;
10135 }
721956f4
AM
10136 }
10137 else
10138 {
ad8e1ba5
AM
10139 /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
10140 variants. */
ac2df442
AM
10141 bfd_vma r2off = 0;
10142
721956f4
AM
10143 off = (stub_entry->target_value
10144 + stub_entry->target_section->output_offset
10145 + stub_entry->target_section->output_section->vma);
eea6121a 10146 off -= (stub_entry->stub_sec->size
721956f4
AM
10147 + stub_entry->stub_sec->output_offset
10148 + stub_entry->stub_sec->output_section->vma);
10149
ad8e1ba5
AM
10150 /* Reset the stub type from the plt variant in case we now
10151 can reach with a shorter stub. */
10152 if (stub_entry->stub_type >= ppc_stub_plt_branch)
10153 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
10154
10155 size = 4;
10156 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
10157 {
25f53a85 10158 r2off = get_r2off (info, stub_entry);
aa374f67
AM
10159 if (r2off == 0)
10160 {
10161 htab->stub_error = TRUE;
10162 return FALSE;
10163 }
ac2df442
AM
10164 size = 12;
10165 if (PPC_HA (r2off) != 0)
10166 size = 16;
10167 off -= size - 4;
ad8e1ba5
AM
10168 }
10169
10170 /* If the branch offset if too big, use a ppc_stub_plt_branch. */
721956f4
AM
10171 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
10172 {
10173 struct ppc_branch_hash_entry *br_entry;
10174
10175 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
10176 stub_entry->root.string + 9,
b34976b6 10177 TRUE, FALSE);
721956f4
AM
10178 if (br_entry == NULL)
10179 {
8de848d8 10180 info->callbacks->einfo (_("%P: can't build branch stub `%s'\n"),
25f53a85 10181 stub_entry->root.string);
b34976b6
AM
10182 htab->stub_error = TRUE;
10183 return FALSE;
721956f4
AM
10184 }
10185
10186 if (br_entry->iter != htab->stub_iteration)
10187 {
10188 br_entry->iter = htab->stub_iteration;
eea6121a
AM
10189 br_entry->offset = htab->brlt->size;
10190 htab->brlt->size += 8;
63bc6f6c 10191
ee75fd95 10192 if (htab->relbrlt != NULL)
eea6121a 10193 htab->relbrlt->size += sizeof (Elf64_External_Rela);
84f5d08e
AM
10194 else if (info->emitrelocations)
10195 {
10196 htab->brlt->reloc_count += 1;
10197 htab->brlt->flags |= SEC_RELOC;
10198 }
721956f4 10199 }
ad8e1ba5
AM
10200
10201 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
ac2df442
AM
10202 off = (br_entry->offset
10203 + htab->brlt->output_offset
10204 + htab->brlt->output_section->vma
10205 - elf_gp (htab->brlt->output_section->owner)
10206 - htab->stub_group[stub_entry->id_sec->id].toc_off);
10207
176a0d42
AM
10208 if (info->emitrelocations)
10209 {
10210 stub_entry->stub_sec->reloc_count += 1 + (PPC_HA (off) != 0);
10211 stub_entry->stub_sec->flags |= SEC_RELOC;
10212 }
10213
ac2df442
AM
10214 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10215 {
10216 size = 12;
176a0d42 10217 if (PPC_HA (off) != 0)
ac2df442
AM
10218 size = 16;
10219 }
10220 else
10221 {
10222 size = 20;
176a0d42 10223 if (PPC_HA (off) != 0)
ac2df442
AM
10224 size += 4;
10225
10226 if (PPC_HA (r2off) != 0)
10227 size += 4;
10228 }
721956f4 10229 }
84f5d08e
AM
10230 else if (info->emitrelocations)
10231 {
10232 stub_entry->stub_sec->reloc_count += 1;
10233 stub_entry->stub_sec->flags |= SEC_RELOC;
10234 }
721956f4
AM
10235 }
10236
eea6121a 10237 stub_entry->stub_sec->size += size;
b34976b6 10238 return TRUE;
721956f4
AM
10239}
10240
10241/* Set up various things so that we can make a list of input sections
10242 for each output section included in the link. Returns -1 on error,
cedb70c5 10243 0 when no stubs will be needed, and 1 on success. */
721956f4
AM
10244
10245int
927be08e
AM
10246ppc64_elf_setup_section_lists
10247 (struct bfd_link_info *info,
10248 asection *(*add_stub_section) (const char *, asection *),
10249 void (*layout_sections_again) (void))
721956f4
AM
10250{
10251 bfd *input_bfd;
734b6cf9 10252 int top_id, top_index, id;
721956f4 10253 asection *section;
734b6cf9 10254 asection **input_list;
721956f4
AM
10255 bfd_size_type amt;
10256 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10257
4dfe6ac6
NC
10258 if (htab == NULL)
10259 return -1;
927be08e
AM
10260 /* Stash our params away. */
10261 htab->add_stub_section = add_stub_section;
10262 htab->layout_sections_again = layout_sections_again;
4c52953f 10263
4ce794b7 10264 if (htab->brlt == NULL)
721956f4
AM
10265 return 0;
10266
1e2f5b6e 10267 /* Find the top input section id. */
3d6f9012 10268 for (input_bfd = info->input_bfds, top_id = 3;
721956f4
AM
10269 input_bfd != NULL;
10270 input_bfd = input_bfd->link_next)
10271 {
721956f4
AM
10272 for (section = input_bfd->sections;
10273 section != NULL;
10274 section = section->next)
10275 {
10276 if (top_id < section->id)
10277 top_id = section->id;
10278 }
10279 }
721956f4 10280
8f3bab57 10281 htab->top_id = top_id;
721956f4 10282 amt = sizeof (struct map_stub) * (top_id + 1);
4ce794b7 10283 htab->stub_group = bfd_zmalloc (amt);
721956f4
AM
10284 if (htab->stub_group == NULL)
10285 return -1;
10286
3d6f9012
AM
10287 /* Set toc_off for com, und, abs and ind sections. */
10288 for (id = 0; id < 3; id++)
10289 htab->stub_group[id].toc_off = TOC_BASE_OFF;
721956f4 10290
734b6cf9
AM
10291 /* We can't use output_bfd->section_count here to find the top output
10292 section index as some sections may have been removed, and
8423293d 10293 strip_excluded_output_sections doesn't renumber the indices. */
927be08e 10294 for (section = info->output_bfd->sections, top_index = 0;
734b6cf9
AM
10295 section != NULL;
10296 section = section->next)
10297 {
10298 if (top_index < section->index)
10299 top_index = section->index;
10300 }
10301
10302 htab->top_index = top_index;
10303 amt = sizeof (asection *) * (top_index + 1);
4ce794b7 10304 input_list = bfd_zmalloc (amt);
734b6cf9
AM
10305 htab->input_list = input_list;
10306 if (input_list == NULL)
10307 return -1;
10308
721956f4
AM
10309 return 1;
10310}
10311
927be08e
AM
10312/* Set up for first pass at multitoc partitioning. */
10313
10314void
10315ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
10316{
10317 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10318
10319 elf_gp (info->output_bfd) = ppc64_elf_toc (info->output_bfd);
10320 htab->toc_curr = elf_gp (info->output_bfd);
10321 htab->toc_bfd = NULL;
10322 htab->toc_first_sec = NULL;
10323}
10324
e717da7e
AM
10325/* The linker repeatedly calls this function for each TOC input section
10326 and linker generated GOT section. Group input bfds such that the toc
927be08e 10327 within a group is less than 64k in size. */
ad8e1ba5 10328
927be08e 10329bfd_boolean
4ce794b7 10330ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
ad8e1ba5
AM
10331{
10332 struct ppc_link_hash_table *htab = ppc_hash_table (info);
d77c8a4b 10333 bfd_vma addr, off, limit;
ad8e1ba5 10334
4dfe6ac6
NC
10335 if (htab == NULL)
10336 return FALSE;
10337
927be08e 10338 if (!htab->second_toc_pass)
4c52953f 10339 {
927be08e 10340 /* Keep track of the first .toc or .got section for this input bfd. */
bf102f86
AM
10341 if (htab->toc_bfd != isec->owner)
10342 {
10343 htab->toc_bfd = isec->owner;
10344 htab->toc_first_sec = isec;
10345 }
927be08e 10346
bf102f86
AM
10347 addr = isec->output_offset + isec->output_section->vma;
10348 off = addr - htab->toc_curr;
d77c8a4b
AM
10349 limit = 0x80008000;
10350 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
10351 limit = 0x10000;
10352 if (off + isec->size > limit)
bf102f86
AM
10353 {
10354 addr = (htab->toc_first_sec->output_offset
10355 + htab->toc_first_sec->output_section->vma);
10356 htab->toc_curr = addr;
10357 }
99877b66 10358
927be08e
AM
10359 /* toc_curr is the base address of this toc group. Set elf_gp
10360 for the input section to be the offset relative to the
10361 output toc base plus 0x8000. Making the input elf_gp an
10362 offset allows us to move the toc as a whole without
10363 recalculating input elf_gp. */
10364 off = htab->toc_curr - elf_gp (isec->output_section->owner);
10365 off += TOC_BASE_OFF;
10366
10367 /* Die if someone uses a linker script that doesn't keep input
10368 file .toc and .got together. */
10369 if (elf_gp (isec->owner) != 0
10370 && elf_gp (isec->owner) != off)
10371 return FALSE;
10372
10373 elf_gp (isec->owner) = off;
10374 return TRUE;
4c52953f 10375 }
927be08e
AM
10376
10377 /* During the second pass toc_first_sec points to the start of
10378 a toc group, and toc_curr is used to track the old elf_gp.
10379 We use toc_bfd to ensure we only look at each bfd once. */
10380 if (htab->toc_bfd == isec->owner)
10381 return TRUE;
10382 htab->toc_bfd = isec->owner;
10383
10384 if (htab->toc_first_sec == NULL
10385 || htab->toc_curr != elf_gp (isec->owner))
10386 {
10387 htab->toc_curr = elf_gp (isec->owner);
10388 htab->toc_first_sec = isec;
10389 }
10390 addr = (htab->toc_first_sec->output_offset
10391 + htab->toc_first_sec->output_section->vma);
10392 off = addr - elf_gp (isec->output_section->owner) + TOC_BASE_OFF;
10393 elf_gp (isec->owner) = off;
10394
10395 return TRUE;
ad8e1ba5
AM
10396}
10397
927be08e
AM
10398/* Called via elf_link_hash_traverse to merge GOT entries for global
10399 symbol H. */
10400
10401static bfd_boolean
10402merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
10403{
10404 if (h->root.type == bfd_link_hash_indirect)
10405 return TRUE;
10406
927be08e
AM
10407 merge_got_entries (&h->got.glist);
10408
10409 return TRUE;
10410}
10411
10412/* Called via elf_link_hash_traverse to allocate GOT entries for global
10413 symbol H. */
10414
10415static bfd_boolean
10416reallocate_got (struct elf_link_hash_entry *h, void *inf)
10417{
10418 struct got_entry *gent;
10419
10420 if (h->root.type == bfd_link_hash_indirect)
10421 return TRUE;
10422
927be08e
AM
10423 for (gent = h->got.glist; gent != NULL; gent = gent->next)
10424 if (!gent->is_indirect)
10425 allocate_got (h, (struct bfd_link_info *) inf, gent);
10426 return TRUE;
10427}
10428
10429/* Called on the first multitoc pass after the last call to
10430 ppc64_elf_next_toc_section. This function removes duplicate GOT
10431 entries. */
10432
10433bfd_boolean
10434ppc64_elf_layout_multitoc (struct bfd_link_info *info)
ad8e1ba5
AM
10435{
10436 struct ppc_link_hash_table *htab = ppc_hash_table (info);
927be08e
AM
10437 struct bfd *ibfd, *ibfd2;
10438 bfd_boolean done_something;
10439
10440 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
ad8e1ba5 10441
7865406b
AM
10442 if (!htab->do_multi_toc)
10443 return FALSE;
10444
d0fae19d 10445 /* Merge global sym got entries within a toc group. */
927be08e
AM
10446 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
10447
10448 /* And tlsld_got. */
10449 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10450 {
10451 struct got_entry *ent, *ent2;
10452
10453 if (!is_ppc64_elf (ibfd))
10454 continue;
10455
10456 ent = ppc64_tlsld_got (ibfd);
10457 if (!ent->is_indirect
10458 && ent->got.offset != (bfd_vma) -1)
10459 {
10460 for (ibfd2 = ibfd->link_next; ibfd2 != NULL; ibfd2 = ibfd2->link_next)
10461 {
10462 if (!is_ppc64_elf (ibfd2))
10463 continue;
10464
10465 ent2 = ppc64_tlsld_got (ibfd2);
10466 if (!ent2->is_indirect
10467 && ent2->got.offset != (bfd_vma) -1
10468 && elf_gp (ibfd2) == elf_gp (ibfd))
10469 {
10470 ent2->is_indirect = TRUE;
10471 ent2->got.ent = ent;
10472 }
10473 }
10474 }
10475 }
10476
10477 /* Zap sizes of got sections. */
10478 htab->reliplt->rawsize = htab->reliplt->size;
10479 htab->reliplt->size -= htab->got_reli_size;
10480 htab->got_reli_size = 0;
10481
10482 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10483 {
10484 asection *got, *relgot;
10485
10486 if (!is_ppc64_elf (ibfd))
10487 continue;
10488
10489 got = ppc64_elf_tdata (ibfd)->got;
10490 if (got != NULL)
10491 {
10492 got->rawsize = got->size;
10493 got->size = 0;
10494 relgot = ppc64_elf_tdata (ibfd)->relgot;
10495 relgot->rawsize = relgot->size;
10496 relgot->size = 0;
10497 }
10498 }
10499
10500 /* Now reallocate the got, local syms first. We don't need to
10501 allocate section contents again since we never increase size. */
10502 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10503 {
10504 struct got_entry **lgot_ents;
10505 struct got_entry **end_lgot_ents;
10506 struct plt_entry **local_plt;
10507 struct plt_entry **end_local_plt;
f961d9dd 10508 unsigned char *lgot_masks;
927be08e
AM
10509 bfd_size_type locsymcount;
10510 Elf_Internal_Shdr *symtab_hdr;
10511 asection *s, *srel;
10512
10513 if (!is_ppc64_elf (ibfd))
10514 continue;
10515
10516 lgot_ents = elf_local_got_ents (ibfd);
10517 if (!lgot_ents)
10518 continue;
10519
10520 symtab_hdr = &elf_symtab_hdr (ibfd);
10521 locsymcount = symtab_hdr->sh_info;
10522 end_lgot_ents = lgot_ents + locsymcount;
10523 local_plt = (struct plt_entry **) end_lgot_ents;
10524 end_local_plt = local_plt + locsymcount;
f961d9dd 10525 lgot_masks = (unsigned char *) end_local_plt;
927be08e
AM
10526 s = ppc64_elf_tdata (ibfd)->got;
10527 srel = ppc64_elf_tdata (ibfd)->relgot;
10528 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
10529 {
10530 struct got_entry *ent;
10531
10532 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
d0fae19d
AM
10533 {
10534 unsigned int num = 1;
10535 ent->got.offset = s->size;
10536 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
10537 num = 2;
10538 s->size += num * 8;
10539 if (info->shared)
10540 srel->size += num * sizeof (Elf64_External_Rela);
10541 else if ((*lgot_masks & PLT_IFUNC) != 0)
10542 {
10543 htab->reliplt->size
10544 += num * sizeof (Elf64_External_Rela);
10545 htab->got_reli_size
10546 += num * sizeof (Elf64_External_Rela);
10547 }
10548 }
927be08e
AM
10549 }
10550 }
10551
10552 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
10553
10554 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10555 {
10556 struct got_entry *ent;
10557
10558 if (!is_ppc64_elf (ibfd))
10559 continue;
10560
10561 ent = ppc64_tlsld_got (ibfd);
10562 if (!ent->is_indirect
10563 && ent->got.offset != (bfd_vma) -1)
10564 {
10565 asection *s = ppc64_elf_tdata (ibfd)->got;
10566 ent->got.offset = s->size;
10567 s->size += 16;
10568 if (info->shared)
10569 {
10570 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10571 srel->size += sizeof (Elf64_External_Rela);
10572 }
10573 }
10574 }
10575
10576 done_something = htab->reliplt->rawsize != htab->reliplt->size;
10577 if (!done_something)
10578 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10579 {
10580 asection *got;
10581
10582 if (!is_ppc64_elf (ibfd))
10583 continue;
10584
10585 got = ppc64_elf_tdata (ibfd)->got;
10586 if (got != NULL)
10587 {
10588 done_something = got->rawsize != got->size;
10589 if (done_something)
10590 break;
10591 }
10592 }
10593
10594 if (done_something)
10595 (*htab->layout_sections_again) ();
10596
10597 /* Set up for second pass over toc sections to recalculate elf_gp
10598 on input sections. */
10599 htab->toc_bfd = NULL;
10600 htab->toc_first_sec = NULL;
10601 htab->second_toc_pass = TRUE;
10602 return done_something;
10603}
10604
10605/* Called after second pass of multitoc partitioning. */
10606
10607void
10608ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
10609{
10610 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10611
10612 /* After the second pass, toc_curr tracks the TOC offset used
10613 for code sections below in ppc64_elf_next_input_section. */
3d6f9012 10614 htab->toc_curr = TOC_BASE_OFF;
ad8e1ba5
AM
10615}
10616
9b5ecbd0
AM
10617/* No toc references were found in ISEC. If the code in ISEC makes no
10618 calls, then there's no need to use toc adjusting stubs when branching
10619 into ISEC. Actually, indirect calls from ISEC are OK as they will
4c52953f
AM
10620 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
10621 needed, and 2 if a cyclical call-graph was found but no other reason
10622 for a stub was detected. If called from the top level, a return of
10623 2 means the same as a return of 0. */
9b5ecbd0
AM
10624
10625static int
4ce794b7 10626toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
9b5ecbd0 10627{
9b5ecbd0 10628 int ret;
70cc837d
AM
10629
10630 /* Mark this section as checked. */
10631 isec->call_check_done = 1;
9b5ecbd0 10632
772119ce
AM
10633 /* We know none of our code bearing sections will need toc stubs. */
10634 if ((isec->flags & SEC_LINKER_CREATED) != 0)
10635 return 0;
10636
eea6121a 10637 if (isec->size == 0)
082c50f8
AM
10638 return 0;
10639
4c52953f
AM
10640 if (isec->output_section == NULL)
10641 return 0;
10642
4c52953f 10643 ret = 0;
70cc837d 10644 if (isec->reloc_count != 0)
9b5ecbd0 10645 {
70cc837d
AM
10646 Elf_Internal_Rela *relstart, *rel;
10647 Elf_Internal_Sym *local_syms;
10648 struct ppc_link_hash_table *htab;
2917689a 10649
70cc837d
AM
10650 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
10651 info->keep_memory);
10652 if (relstart == NULL)
10653 return -1;
90aecf7a 10654
70cc837d
AM
10655 /* Look for branches to outside of this section. */
10656 local_syms = NULL;
10657 htab = ppc_hash_table (info);
10658 if (htab == NULL)
10659 return -1;
4c52953f 10660
70cc837d 10661 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
4c52953f 10662 {
70cc837d
AM
10663 enum elf_ppc64_reloc_type r_type;
10664 unsigned long r_symndx;
10665 struct elf_link_hash_entry *h;
10666 struct ppc_link_hash_entry *eh;
10667 Elf_Internal_Sym *sym;
10668 asection *sym_sec;
10669 struct _opd_sec_data *opd;
10670 bfd_vma sym_value;
10671 bfd_vma dest;
10672
10673 r_type = ELF64_R_TYPE (rel->r_info);
10674 if (r_type != R_PPC64_REL24
10675 && r_type != R_PPC64_REL14
10676 && r_type != R_PPC64_REL14_BRTAKEN
10677 && r_type != R_PPC64_REL14_BRNTAKEN)
10678 continue;
4c52953f 10679
70cc837d
AM
10680 r_symndx = ELF64_R_SYM (rel->r_info);
10681 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
10682 isec->owner))
4c52953f 10683 {
70cc837d
AM
10684 ret = -1;
10685 break;
10686 }
4c52953f 10687
70cc837d
AM
10688 /* Calls to dynamic lib functions go through a plt call stub
10689 that uses r2. */
10690 eh = (struct ppc_link_hash_entry *) h;
10691 if (eh != NULL
10692 && (eh->elf.plt.plist != NULL
10693 || (eh->oh != NULL
10694 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
10695 {
10696 ret = 1;
10697 break;
4c52953f
AM
10698 }
10699
70cc837d
AM
10700 if (sym_sec == NULL)
10701 /* Ignore other undefined symbols. */
4c52953f 10702 continue;
4c52953f 10703
70cc837d
AM
10704 /* Assume branches to other sections not included in the
10705 link need stubs too, to cover -R and absolute syms. */
10706 if (sym_sec->output_section == NULL)
10707 {
10708 ret = 1;
10709 break;
10710 }
4c52953f 10711
70cc837d
AM
10712 if (h == NULL)
10713 sym_value = sym->st_value;
10714 else
10715 {
10716 if (h->root.type != bfd_link_hash_defined
10717 && h->root.type != bfd_link_hash_defweak)
10718 abort ();
10719 sym_value = h->root.u.def.value;
10720 }
10721 sym_value += rel->r_addend;
4c52953f 10722
70cc837d
AM
10723 /* If this branch reloc uses an opd sym, find the code section. */
10724 opd = get_opd_info (sym_sec);
10725 if (opd != NULL)
10726 {
10727 if (h == NULL && opd->adjust != NULL)
10728 {
10729 long adjust;
4c52953f 10730
70cc837d
AM
10731 adjust = opd->adjust[sym->st_value / 8];
10732 if (adjust == -1)
10733 /* Assume deleted functions won't ever be called. */
10734 continue;
10735 sym_value += adjust;
10736 }
4c52953f 10737
70cc837d
AM
10738 dest = opd_entry_value (sym_sec, sym_value, &sym_sec, NULL);
10739 if (dest == (bfd_vma) -1)
10740 continue;
10741 }
10742 else
10743 dest = (sym_value
10744 + sym_sec->output_offset
10745 + sym_sec->output_section->vma);
4c52953f 10746
70cc837d
AM
10747 /* Ignore branch to self. */
10748 if (sym_sec == isec)
10749 continue;
4c52953f 10750
70cc837d
AM
10751 /* If the called function uses the toc, we need a stub. */
10752 if (sym_sec->has_toc_reloc
10753 || sym_sec->makes_toc_func_call)
4c52953f 10754 {
70cc837d 10755 ret = 1;
4c52953f
AM
10756 break;
10757 }
70cc837d
AM
10758
10759 /* Assume any branch that needs a long branch stub might in fact
10760 need a plt_branch stub. A plt_branch stub uses r2. */
10761 else if (dest - (isec->output_offset
10762 + isec->output_section->vma
10763 + rel->r_offset) + (1 << 25) >= (2 << 25))
4c52953f 10764 {
70cc837d
AM
10765 ret = 1;
10766 break;
10767 }
10768
10769 /* If calling back to a section in the process of being
10770 tested, we can't say for sure that no toc adjusting stubs
10771 are needed, so don't return zero. */
10772 else if (sym_sec->call_check_in_progress)
10773 ret = 2;
10774
10775 /* Branches to another section that itself doesn't have any TOC
10776 references are OK. Recursively call ourselves to check. */
10777 else if (!sym_sec->call_check_done)
10778 {
10779 int recur;
10780
10781 /* Mark current section as indeterminate, so that other
10782 sections that call back to current won't be marked as
10783 known. */
10784 isec->call_check_in_progress = 1;
10785 recur = toc_adjusting_stub_needed (info, sym_sec);
10786 isec->call_check_in_progress = 0;
10787
4c52953f
AM
10788 if (recur != 0)
10789 {
70cc837d
AM
10790 ret = recur;
10791 if (recur != 2)
10792 break;
4c52953f
AM
10793 }
10794 }
4c52953f 10795 }
70cc837d
AM
10796
10797 if (local_syms != NULL
10798 && (elf_symtab_hdr (isec->owner).contents
10799 != (unsigned char *) local_syms))
10800 free (local_syms);
10801 if (elf_section_data (isec)->relocs != relstart)
10802 free (relstart);
9b5ecbd0
AM
10803 }
10804
70cc837d
AM
10805 if ((ret & 1) == 0
10806 && isec->map_head.s != NULL
10807 && (strcmp (isec->output_section->name, ".init") == 0
10808 || strcmp (isec->output_section->name, ".fini") == 0))
10809 {
10810 if (isec->map_head.s->has_toc_reloc
10811 || isec->map_head.s->makes_toc_func_call)
10812 ret = 1;
10813 else if (!isec->map_head.s->call_check_done)
10814 {
10815 int recur;
10816 isec->call_check_in_progress = 1;
10817 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
10818 isec->call_check_in_progress = 0;
10819 if (recur != 0)
10820 ret = recur;
10821 }
10822 }
10823
10824 if (ret == 1)
10825 isec->makes_toc_func_call = 1;
4c52953f 10826
9b5ecbd0
AM
10827 return ret;
10828}
10829
721956f4
AM
10830/* The linker repeatedly calls this function for each input section,
10831 in the order that input sections are linked into output sections.
10832 Build lists of input sections to determine groupings between which
10833 we may insert linker stubs. */
10834
9b5ecbd0 10835bfd_boolean
4ce794b7 10836ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
721956f4
AM
10837{
10838 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10839
4dfe6ac6
NC
10840 if (htab == NULL)
10841 return FALSE;
10842
734b6cf9
AM
10843 if ((isec->output_section->flags & SEC_CODE) != 0
10844 && isec->output_section->index <= htab->top_index)
721956f4 10845 {
734b6cf9 10846 asection **list = htab->input_list + isec->output_section->index;
3d6f9012 10847 /* Steal the link_sec pointer for our list. */
721956f4 10848#define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
3d6f9012
AM
10849 /* This happens to make the list in reverse order,
10850 which is what we want. */
734b6cf9
AM
10851 PREV_SEC (isec) = *list;
10852 *list = isec;
721956f4 10853 }
ad8e1ba5 10854
4c52953f 10855 if (htab->multi_toc_needed)
9b5ecbd0 10856 {
4c52953f
AM
10857 /* If a code section has a function that uses the TOC then we need
10858 to use the right TOC (obviously). Also, make sure that .opd gets
10859 the correct TOC value for R_PPC64_TOC relocs that don't have or
f94498ff
AM
10860 can't find their function symbol (shouldn't ever happen now).
10861 Also specially treat .fixup for the linux kernel. .fixup
10862 contains branches, but only back to the function that hit an
10863 exception. */
10864 if (isec->has_toc_reloc
10865 || (isec->flags & SEC_CODE) == 0
10866 || strcmp (isec->name, ".fixup") == 0)
4c52953f
AM
10867 {
10868 if (elf_gp (isec->owner) != 0)
10869 htab->toc_curr = elf_gp (isec->owner);
10870 }
6683a28d
AM
10871 else
10872 {
10873 if (!isec->call_check_done
10874 && toc_adjusting_stub_needed (info, isec) < 0)
10875 return FALSE;
10876 /* If we make a local call from this section, ie. a branch
10877 without a following nop, then we have no place to put a
10878 toc restoring insn. We must use the same toc group as
10879 the callee.
10880 Testing makes_toc_func_call actually tests for *any*
10881 calls to functions that need a good toc pointer. A more
10882 precise test would be better, as this one will set
10883 incorrect values for pasted .init/.fini fragments.
10884 (Fixed later in check_pasted_section.) */
10885 if (isec->makes_toc_func_call
10886 && elf_gp (isec->owner) != 0)
10887 htab->toc_curr = elf_gp (isec->owner);
10888 }
9b5ecbd0 10889 }
ad8e1ba5
AM
10890
10891 /* Functions that don't use the TOC can belong in any TOC group.
6683a28d 10892 Use the last TOC base. */
ad8e1ba5 10893 htab->stub_group[isec->id].toc_off = htab->toc_curr;
9b5ecbd0 10894 return TRUE;
721956f4
AM
10895}
10896
70cc837d
AM
10897/* Check that all .init and .fini sections use the same toc, if they
10898 have toc relocs. */
10899
10900static bfd_boolean
10901check_pasted_section (struct bfd_link_info *info, const char *name)
10902{
10903 asection *o = bfd_get_section_by_name (info->output_bfd, name);
10904
10905 if (o != NULL)
10906 {
10907 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10908 bfd_vma toc_off = 0;
10909 asection *i;
10910
10911 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
10912 if (i->has_toc_reloc)
10913 {
10914 if (toc_off == 0)
10915 toc_off = htab->stub_group[i->id].toc_off;
10916 else if (toc_off != htab->stub_group[i->id].toc_off)
10917 return FALSE;
10918 }
6683a28d
AM
10919
10920 if (toc_off == 0)
10921 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
10922 if (i->makes_toc_func_call)
10923 {
10924 toc_off = htab->stub_group[i->id].toc_off;
10925 break;
10926 }
10927
70cc837d
AM
10928 /* Make sure the whole pasted function uses the same toc offset. */
10929 if (toc_off != 0)
10930 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
10931 htab->stub_group[i->id].toc_off = toc_off;
10932 }
10933 return TRUE;
10934}
10935
10936bfd_boolean
10937ppc64_elf_check_init_fini (struct bfd_link_info *info)
10938{
10939 return (check_pasted_section (info, ".init")
10940 & check_pasted_section (info, ".fini"));
10941}
10942
721956f4
AM
10943/* See whether we can group stub sections together. Grouping stub
10944 sections may result in fewer stubs. More importantly, we need to
10945 put all .init* and .fini* stubs at the beginning of the .init or
10946 .fini output sections respectively, because glibc splits the
10947 _init and _fini functions into multiple parts. Putting a stub in
10948 the middle of a function is not a good idea. */
10949
10950static void
4ce794b7
AM
10951group_sections (struct ppc_link_hash_table *htab,
10952 bfd_size_type stub_group_size,
10953 bfd_boolean stubs_always_before_branch)
721956f4 10954{
7c8fe5c4
AM
10955 asection **list;
10956 bfd_size_type stub14_group_size;
10957 bfd_boolean suppress_size_errors;
10958
10959 suppress_size_errors = FALSE;
10960 stub14_group_size = stub_group_size;
10961 if (stub_group_size == 1)
10962 {
10963 /* Default values. */
10964 if (stubs_always_before_branch)
10965 {
10966 stub_group_size = 0x1e00000;
10967 stub14_group_size = 0x7800;
10968 }
10969 else
10970 {
10971 stub_group_size = 0x1c00000;
10972 stub14_group_size = 0x7000;
10973 }
10974 suppress_size_errors = TRUE;
10975 }
10976
10977 list = htab->input_list + htab->top_index;
734b6cf9 10978 do
721956f4 10979 {
734b6cf9
AM
10980 asection *tail = *list;
10981 while (tail != NULL)
721956f4 10982 {
734b6cf9
AM
10983 asection *curr;
10984 asection *prev;
10985 bfd_size_type total;
10986 bfd_boolean big_sec;
10987 bfd_vma curr_toc;
10988
10989 curr = tail;
eea6121a 10990 total = tail->size;
6bee8834
AM
10991 big_sec = total > (ppc64_elf_section_data (tail) != NULL
10992 && ppc64_elf_section_data (tail)->has_14bit_branch
7c8fe5c4
AM
10993 ? stub14_group_size : stub_group_size);
10994 if (big_sec && !suppress_size_errors)
5c3dead3
AM
10995 (*_bfd_error_handler) (_("%B section %A exceeds stub group size"),
10996 tail->owner, tail);
734b6cf9
AM
10997 curr_toc = htab->stub_group[tail->id].toc_off;
10998
10999 while ((prev = PREV_SEC (curr)) != NULL
11000 && ((total += curr->output_offset - prev->output_offset)
6bee8834
AM
11001 < (ppc64_elf_section_data (prev) != NULL
11002 && ppc64_elf_section_data (prev)->has_14bit_branch
7c8fe5c4 11003 ? stub14_group_size : stub_group_size))
ad8e1ba5 11004 && htab->stub_group[prev->id].toc_off == curr_toc)
734b6cf9
AM
11005 curr = prev;
11006
11007 /* OK, the size from the start of CURR to the end is less
11008 than stub_group_size and thus can be handled by one stub
11009 section. (or the tail section is itself larger than
11010 stub_group_size, in which case we may be toast.) We
11011 should really be keeping track of the total size of stubs
11012 added here, as stubs contribute to the final output
11013 section size. That's a little tricky, and this way will
11014 only break if stubs added make the total size more than
11015 2^25, ie. for the default stub_group_size, if stubs total
11016 more than 2097152 bytes, or nearly 75000 plt call stubs. */
11017 do
721956f4
AM
11018 {
11019 prev = PREV_SEC (tail);
734b6cf9 11020 /* Set up this stub group. */
721956f4
AM
11021 htab->stub_group[tail->id].link_sec = curr;
11022 }
734b6cf9
AM
11023 while (tail != curr && (tail = prev) != NULL);
11024
11025 /* But wait, there's more! Input sections up to stub_group_size
11026 bytes before the stub section can be handled by it too.
11027 Don't do this if we have a really large section after the
11028 stubs, as adding more stubs increases the chance that
11029 branches may not reach into the stub section. */
11030 if (!stubs_always_before_branch && !big_sec)
11031 {
11032 total = 0;
11033 while (prev != NULL
11034 && ((total += tail->output_offset - prev->output_offset)
6bee8834
AM
11035 < (ppc64_elf_section_data (prev) != NULL
11036 && ppc64_elf_section_data (prev)->has_14bit_branch
7c8fe5c4 11037 ? stub14_group_size : stub_group_size))
734b6cf9
AM
11038 && htab->stub_group[prev->id].toc_off == curr_toc)
11039 {
11040 tail = prev;
11041 prev = PREV_SEC (tail);
11042 htab->stub_group[tail->id].link_sec = curr;
11043 }
11044 }
11045 tail = prev;
721956f4
AM
11046 }
11047 }
734b6cf9
AM
11048 while (list-- != htab->input_list);
11049 free (htab->input_list);
721956f4
AM
11050#undef PREV_SEC
11051}
11052
58d180e8
AM
11053static const unsigned char glink_eh_frame_cie[] =
11054{
11055 0, 0, 0, 16, /* length. */
11056 0, 0, 0, 0, /* id. */
11057 1, /* CIE version. */
11058 'z', 'R', 0, /* Augmentation string. */
11059 4, /* Code alignment. */
11060 0x78, /* Data alignment. */
11061 65, /* RA reg. */
11062 1, /* Augmentation size. */
11063 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
11064 DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
11065};
11066
d969d15f
AM
11067/* Stripping output sections is normally done before dynamic section
11068 symbols have been allocated. This function is called later, and
11069 handles cases like htab->brlt which is mapped to its own output
11070 section. */
11071
11072static void
11073maybe_strip_output (struct bfd_link_info *info, asection *isec)
11074{
11075 if (isec->size == 0
11076 && isec->output_section->size == 0
11077 && !bfd_section_removed_from_list (info->output_bfd,
11078 isec->output_section)
11079 && elf_section_data (isec->output_section)->dynindx == 0)
11080 {
11081 isec->output_section->flags |= SEC_EXCLUDE;
11082 bfd_section_list_remove (info->output_bfd, isec->output_section);
11083 info->output_bfd->section_count--;
11084 }
11085}
11086
721956f4
AM
11087/* Determine and set the size of the stub section for a final link.
11088
11089 The basic idea here is to examine all the relocations looking for
11090 PC-relative calls to a target that is unreachable with a "bl"
11091 instruction. */
11092
b34976b6 11093bfd_boolean
9df0ef5f
AM
11094ppc64_elf_size_stubs (struct bfd_link_info *info, bfd_signed_vma group_size,
11095 bfd_boolean plt_static_chain)
721956f4
AM
11096{
11097 bfd_size_type stub_group_size;
b34976b6 11098 bfd_boolean stubs_always_before_branch;
721956f4
AM
11099 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11100
4dfe6ac6
NC
11101 if (htab == NULL)
11102 return FALSE;
11103
9df0ef5f 11104 htab->plt_static_chain = plt_static_chain;
721956f4
AM
11105 stubs_always_before_branch = group_size < 0;
11106 if (group_size < 0)
11107 stub_group_size = -group_size;
11108 else
11109 stub_group_size = group_size;
721956f4
AM
11110
11111 group_sections (htab, stub_group_size, stubs_always_before_branch);
11112
721956f4
AM
11113 while (1)
11114 {
11115 bfd *input_bfd;
11116 unsigned int bfd_indx;
11117 asection *stub_sec;
721956f4
AM
11118
11119 htab->stub_iteration += 1;
721956f4
AM
11120
11121 for (input_bfd = info->input_bfds, bfd_indx = 0;
11122 input_bfd != NULL;
11123 input_bfd = input_bfd->link_next, bfd_indx++)
11124 {
11125 Elf_Internal_Shdr *symtab_hdr;
11126 asection *section;
6cdc0ccc 11127 Elf_Internal_Sym *local_syms = NULL;
721956f4 11128
0c8d6e5c 11129 if (!is_ppc64_elf (input_bfd))
67f93c31
AM
11130 continue;
11131
721956f4 11132 /* We'll need the symbol table in a second. */
0ffa91dd 11133 symtab_hdr = &elf_symtab_hdr (input_bfd);
721956f4
AM
11134 if (symtab_hdr->sh_info == 0)
11135 continue;
11136
721956f4
AM
11137 /* Walk over each section attached to the input bfd. */
11138 for (section = input_bfd->sections;
11139 section != NULL;
11140 section = section->next)
11141 {
721956f4 11142 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
721956f4
AM
11143
11144 /* If there aren't any relocs, then there's nothing more
11145 to do. */
11146 if ((section->flags & SEC_RELOC) == 0
12c0f757
AM
11147 || (section->flags & SEC_ALLOC) == 0
11148 || (section->flags & SEC_LOAD) == 0
11149 || (section->flags & SEC_CODE) == 0
721956f4
AM
11150 || section->reloc_count == 0)
11151 continue;
11152
11153 /* If this section is a link-once section that will be
11154 discarded, then don't create any stubs. */
11155 if (section->output_section == NULL
927be08e 11156 || section->output_section->owner != info->output_bfd)
721956f4
AM
11157 continue;
11158
1e2f5b6e
AM
11159 /* Get the relocs. */
11160 internal_relocs
4ce794b7 11161 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
45d6a902 11162 info->keep_memory);
721956f4 11163 if (internal_relocs == NULL)
1e2f5b6e 11164 goto error_ret_free_local;
721956f4
AM
11165
11166 /* Now examine each relocation. */
11167 irela = internal_relocs;
11168 irelaend = irela + section->reloc_count;
11169 for (; irela < irelaend; irela++)
11170 {
4ce794b7
AM
11171 enum elf_ppc64_reloc_type r_type;
11172 unsigned int r_indx;
721956f4
AM
11173 enum ppc_stub_type stub_type;
11174 struct ppc_stub_hash_entry *stub_entry;
8387904d 11175 asection *sym_sec, *code_sec;
e054468f 11176 bfd_vma sym_value, code_value;
721956f4 11177 bfd_vma destination;
8843416a 11178 bfd_boolean ok_dest;
721956f4 11179 struct ppc_link_hash_entry *hash;
8387904d 11180 struct ppc_link_hash_entry *fdh;
411e1bfb
AM
11181 struct elf_link_hash_entry *h;
11182 Elf_Internal_Sym *sym;
721956f4
AM
11183 char *stub_name;
11184 const asection *id_sec;
74f0fb50 11185 struct _opd_sec_data *opd;
e054468f 11186 struct plt_entry *plt_ent;
721956f4
AM
11187
11188 r_type = ELF64_R_TYPE (irela->r_info);
11189 r_indx = ELF64_R_SYM (irela->r_info);
11190
4ce794b7 11191 if (r_type >= R_PPC64_max)
721956f4
AM
11192 {
11193 bfd_set_error (bfd_error_bad_value);
6cdc0ccc 11194 goto error_ret_free_internal;
721956f4
AM
11195 }
11196
11197 /* Only look for stubs on branch instructions. */
4ce794b7
AM
11198 if (r_type != R_PPC64_REL24
11199 && r_type != R_PPC64_REL14
11200 && r_type != R_PPC64_REL14_BRTAKEN
11201 && r_type != R_PPC64_REL14_BRNTAKEN)
721956f4
AM
11202 continue;
11203
11204 /* Now determine the call target, its name, value,
11205 section. */
411e1bfb
AM
11206 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
11207 r_indx, input_bfd))
11208 goto error_ret_free_internal;
11209 hash = (struct ppc_link_hash_entry *) h;
11210
8843416a 11211 ok_dest = FALSE;
8387904d 11212 fdh = NULL;
7fe2b9a6 11213 sym_value = 0;
411e1bfb 11214 if (hash == NULL)
721956f4 11215 {
411e1bfb 11216 sym_value = sym->st_value;
8843416a 11217 ok_dest = TRUE;
721956f4 11218 }
7fe2b9a6
AM
11219 else if (hash->elf.root.type == bfd_link_hash_defined
11220 || hash->elf.root.type == bfd_link_hash_defweak)
11221 {
11222 sym_value = hash->elf.root.u.def.value;
11223 if (sym_sec->output_section != NULL)
11224 ok_dest = TRUE;
11225 }
11226 else if (hash->elf.root.type == bfd_link_hash_undefweak
11227 || hash->elf.root.type == bfd_link_hash_undefined)
721956f4 11228 {
99877b66 11229 /* Recognise an old ABI func code entry sym, and
7fe2b9a6
AM
11230 use the func descriptor sym instead if it is
11231 defined. */
ceb1f1ef 11232 if (hash->elf.root.root.string[0] == '.'
b31867b6 11233 && (fdh = lookup_fdh (hash, htab)) != NULL)
8387904d 11234 {
8387904d
AM
11235 if (fdh->elf.root.type == bfd_link_hash_defined
11236 || fdh->elf.root.type == bfd_link_hash_defweak)
11237 {
11238 sym_sec = fdh->elf.root.u.def.section;
11239 sym_value = fdh->elf.root.u.def.value;
11240 if (sym_sec->output_section != NULL)
11241 ok_dest = TRUE;
11242 }
99877b66
AM
11243 else
11244 fdh = NULL;
8387904d 11245 }
7fe2b9a6
AM
11246 }
11247 else
11248 {
11249 bfd_set_error (bfd_error_bad_value);
11250 goto error_ret_free_internal;
721956f4
AM
11251 }
11252
8843416a
AM
11253 destination = 0;
11254 if (ok_dest)
11255 {
11256 sym_value += irela->r_addend;
11257 destination = (sym_value
11258 + sym_sec->output_offset
11259 + sym_sec->output_section->vma);
11260 }
11261
8387904d 11262 code_sec = sym_sec;
e054468f 11263 code_value = sym_value;
74f0fb50
AM
11264 opd = get_opd_info (sym_sec);
11265 if (opd != NULL)
8387904d
AM
11266 {
11267 bfd_vma dest;
11268
74f0fb50 11269 if (hash == NULL && opd->adjust != NULL)
8387904d 11270 {
74f0fb50 11271 long adjust = opd->adjust[sym_value / 8];
8387904d
AM
11272 if (adjust == -1)
11273 continue;
e054468f 11274 code_value += adjust;
8387904d
AM
11275 sym_value += adjust;
11276 }
11277 dest = opd_entry_value (sym_sec, sym_value,
e054468f 11278 &code_sec, &code_value);
8387904d
AM
11279 if (dest != (bfd_vma) -1)
11280 {
11281 destination = dest;
11282 if (fdh != NULL)
11283 {
11284 /* Fixup old ABI sym to point at code
11285 entry. */
99877b66 11286 hash->elf.root.type = bfd_link_hash_defweak;
8387904d 11287 hash->elf.root.u.def.section = code_sec;
e054468f 11288 hash->elf.root.u.def.value = code_value;
8387904d
AM
11289 }
11290 }
11291 }
11292
721956f4 11293 /* Determine what (if any) linker stub is needed. */
e054468f 11294 plt_ent = NULL;
721956f4 11295 stub_type = ppc_type_of_stub (section, irela, &hash,
e054468f 11296 &plt_ent, destination);
ad8e1ba5
AM
11297
11298 if (stub_type != ppc_stub_plt_call)
11299 {
11300 /* Check whether we need a TOC adjusting stub.
11301 Since the linker pastes together pieces from
11302 different object files when creating the
11303 _init and _fini functions, it may be that a
11304 call to what looks like a local sym is in
11305 fact a call needing a TOC adjustment. */
8387904d
AM
11306 if (code_sec != NULL
11307 && code_sec->output_section != NULL
11308 && (htab->stub_group[code_sec->id].toc_off
9b5ecbd0 11309 != htab->stub_group[section->id].toc_off)
4c52953f
AM
11310 && (code_sec->has_toc_reloc
11311 || code_sec->makes_toc_func_call))
ad8e1ba5
AM
11312 stub_type = ppc_stub_long_branch_r2off;
11313 }
11314
721956f4
AM
11315 if (stub_type == ppc_stub_none)
11316 continue;
11317
411e1bfb
AM
11318 /* __tls_get_addr calls might be eliminated. */
11319 if (stub_type != ppc_stub_plt_call
11320 && hash != NULL
8387904d
AM
11321 && (hash == htab->tls_get_addr
11322 || hash == htab->tls_get_addr_fd)
411e1bfb
AM
11323 && section->has_tls_reloc
11324 && irela != internal_relocs)
11325 {
11326 /* Get tls info. */
f961d9dd 11327 unsigned char *tls_mask;
411e1bfb 11328
3a71aa26 11329 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
411e1bfb
AM
11330 irela - 1, input_bfd))
11331 goto error_ret_free_internal;
e7b938ca 11332 if (*tls_mask != 0)
411e1bfb
AM
11333 continue;
11334 }
11335
3b421ab3
AM
11336 if (stub_type == ppc_stub_plt_call
11337 && irela + 1 < irelaend
11338 && irela[1].r_offset == irela->r_offset + 4
11339 && ELF64_R_TYPE (irela[1].r_info) == R_PPC64_TOCSAVE
11340 && !tocsave_find (htab, INSERT,
11341 &local_syms, irela + 1, input_bfd))
11342 goto error_ret_free_internal;
11343
721956f4
AM
11344 /* Support for grouping stub sections. */
11345 id_sec = htab->stub_group[section->id].link_sec;
11346
11347 /* Get the name of this stub. */
11348 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
11349 if (!stub_name)
11350 goto error_ret_free_internal;
11351
11352 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 11353 stub_name, FALSE, FALSE);
721956f4
AM
11354 if (stub_entry != NULL)
11355 {
11356 /* The proper stub has already been created. */
11357 free (stub_name);
11358 continue;
11359 }
11360
25f53a85 11361 stub_entry = ppc_add_stub (stub_name, section, info);
721956f4
AM
11362 if (stub_entry == NULL)
11363 {
11364 free (stub_name);
6cdc0ccc
AM
11365 error_ret_free_internal:
11366 if (elf_section_data (section)->relocs == NULL)
11367 free (internal_relocs);
11368 error_ret_free_local:
11369 if (local_syms != NULL
11370 && (symtab_hdr->contents
11371 != (unsigned char *) local_syms))
11372 free (local_syms);
b34976b6 11373 return FALSE;
721956f4
AM
11374 }
11375
ad8e1ba5 11376 stub_entry->stub_type = stub_type;
e054468f
AM
11377 if (stub_type != ppc_stub_plt_call)
11378 {
11379 stub_entry->target_value = code_value;
11380 stub_entry->target_section = code_sec;
11381 }
11382 else
11383 {
11384 stub_entry->target_value = sym_value;
11385 stub_entry->target_section = sym_sec;
11386 }
721956f4 11387 stub_entry->h = hash;
e054468f 11388 stub_entry->plt_ent = plt_ent;
411e1bfb 11389 stub_entry->addend = irela->r_addend;
ee75fd95
AM
11390
11391 if (stub_entry->h != NULL)
11392 htab->stub_globals += 1;
721956f4
AM
11393 }
11394
11395 /* We're done with the internal relocs, free them. */
6cdc0ccc 11396 if (elf_section_data (section)->relocs != internal_relocs)
1e2f5b6e 11397 free (internal_relocs);
721956f4 11398 }
6cdc0ccc
AM
11399
11400 if (local_syms != NULL
11401 && symtab_hdr->contents != (unsigned char *) local_syms)
11402 {
11403 if (!info->keep_memory)
11404 free (local_syms);
11405 else
11406 symtab_hdr->contents = (unsigned char *) local_syms;
11407 }
721956f4
AM
11408 }
11409
5c3dead3 11410 /* We may have added some stubs. Find out the new size of the
721956f4
AM
11411 stub sections. */
11412 for (stub_sec = htab->stub_bfd->sections;
11413 stub_sec != NULL;
11414 stub_sec = stub_sec->next)
e717da7e 11415 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
ee75fd95 11416 {
5c3dead3 11417 stub_sec->rawsize = stub_sec->size;
ee75fd95
AM
11418 stub_sec->size = 0;
11419 stub_sec->reloc_count = 0;
84f5d08e 11420 stub_sec->flags &= ~SEC_RELOC;
ee75fd95 11421 }
eea6121a
AM
11422
11423 htab->brlt->size = 0;
84f5d08e
AM
11424 htab->brlt->reloc_count = 0;
11425 htab->brlt->flags &= ~SEC_RELOC;
ee75fd95 11426 if (htab->relbrlt != NULL)
eea6121a 11427 htab->relbrlt->size = 0;
721956f4 11428
63bc6f6c 11429 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
721956f4 11430
176a0d42
AM
11431 if (info->emitrelocations
11432 && htab->glink != NULL && htab->glink->size != 0)
11433 {
11434 htab->glink->reloc_count = 1;
11435 htab->glink->flags |= SEC_RELOC;
11436 }
11437
58d180e8
AM
11438 if (htab->glink_eh_frame != NULL
11439 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
11440 && (htab->glink_eh_frame->flags & SEC_EXCLUDE) == 0)
11441 {
11442 bfd_size_type size = 0;
11443
11444 for (stub_sec = htab->stub_bfd->sections;
11445 stub_sec != NULL;
11446 stub_sec = stub_sec->next)
11447 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
11448 size += 20;
11449 if (htab->glink != NULL && htab->glink->size != 0)
11450 size += 24;
11451 if (size != 0)
11452 size += sizeof (glink_eh_frame_cie);
11453 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
11454 htab->glink_eh_frame->size = size;
11455 }
11456
5c3dead3
AM
11457 for (stub_sec = htab->stub_bfd->sections;
11458 stub_sec != NULL;
11459 stub_sec = stub_sec->next)
11460 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
11461 && stub_sec->rawsize != stub_sec->size)
11462 break;
11463
11464 /* Exit from this loop when no stubs have been added, and no stubs
11465 have changed size. */
58d180e8
AM
11466 if (stub_sec == NULL
11467 && (htab->glink_eh_frame == NULL
11468 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
5c3dead3
AM
11469 break;
11470
721956f4
AM
11471 /* Ask the linker to do its stuff. */
11472 (*htab->layout_sections_again) ();
11473 }
11474
d969d15f
AM
11475 maybe_strip_output (info, htab->brlt);
11476 if (htab->glink_eh_frame != NULL)
11477 maybe_strip_output (info, htab->glink_eh_frame);
721956f4 11478
b34976b6 11479 return TRUE;
721956f4
AM
11480}
11481
11482/* Called after we have determined section placement. If sections
805fc799 11483 move, we'll be called again. Provide a value for TOCstart. */
721956f4 11484
805fc799 11485bfd_vma
4ce794b7 11486ppc64_elf_toc (bfd *obfd)
721956f4 11487{
805fc799
AM
11488 asection *s;
11489 bfd_vma TOCstart;
721956f4 11490
805fc799
AM
11491 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
11492 order. The TOC starts where the first of these sections starts. */
11493 s = bfd_get_section_by_name (obfd, ".got");
e054468f 11494 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 11495 s = bfd_get_section_by_name (obfd, ".toc");
e054468f 11496 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 11497 s = bfd_get_section_by_name (obfd, ".tocbss");
e054468f 11498 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 11499 s = bfd_get_section_by_name (obfd, ".plt");
e054468f 11500 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799
AM
11501 {
11502 /* This may happen for
11503 o references to TOC base (SYM@toc / TOC[tc0]) without a
11504 .toc directive
11505 o bad linker script
11506 o --gc-sections and empty TOC sections
11507
11508 FIXME: Warn user? */
11509
11510 /* Look for a likely section. We probably won't even be
11511 using TOCstart. */
11512 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
11513 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
11514 | SEC_EXCLUDE))
805fc799
AM
11515 == (SEC_ALLOC | SEC_SMALL_DATA))
11516 break;
721956f4 11517 if (s == NULL)
805fc799 11518 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 11519 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
805fc799
AM
11520 == (SEC_ALLOC | SEC_SMALL_DATA))
11521 break;
721956f4 11522 if (s == NULL)
805fc799 11523 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
11524 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
11525 == SEC_ALLOC)
805fc799 11526 break;
721956f4 11527 if (s == NULL)
805fc799 11528 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 11529 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
805fc799
AM
11530 break;
11531 }
721956f4 11532
805fc799
AM
11533 TOCstart = 0;
11534 if (s != NULL)
11535 TOCstart = s->output_section->vma + s->output_offset;
721956f4 11536
805fc799 11537 return TOCstart;
721956f4
AM
11538}
11539
11540/* Build all the stubs associated with the current output file.
11541 The stubs are kept in a hash table attached to the main linker
11542 hash table. This function is called via gldelf64ppc_finish. */
11543
b34976b6 11544bfd_boolean
4ce794b7
AM
11545ppc64_elf_build_stubs (bfd_boolean emit_stub_syms,
11546 struct bfd_link_info *info,
11547 char **stats)
5d1634d7
AM
11548{
11549 struct ppc_link_hash_table *htab = ppc_hash_table (info);
721956f4 11550 asection *stub_sec;
5d1634d7 11551 bfd_byte *p;
e717da7e 11552 int stub_sec_count = 0;
5d1634d7 11553
4dfe6ac6
NC
11554 if (htab == NULL)
11555 return FALSE;
11556
ad8e1ba5 11557 htab->emit_stub_syms = emit_stub_syms;
eea6121a
AM
11558
11559 /* Allocate memory to hold the linker stubs. */
721956f4
AM
11560 for (stub_sec = htab->stub_bfd->sections;
11561 stub_sec != NULL;
11562 stub_sec = stub_sec->next)
eea6121a
AM
11563 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
11564 && stub_sec->size != 0)
e717da7e 11565 {
eea6121a
AM
11566 stub_sec->contents = bfd_zalloc (htab->stub_bfd, stub_sec->size);
11567 if (stub_sec->contents == NULL)
11568 return FALSE;
11569 /* We want to check that built size is the same as calculated
11570 size. rawsize is a convenient location to use. */
11571 stub_sec->rawsize = stub_sec->size;
11572 stub_sec->size = 0;
e717da7e 11573 }
5d1634d7 11574
23eb7e01 11575 if (htab->glink != NULL && htab->glink->size != 0)
5d1634d7 11576 {
9f951329 11577 unsigned int indx;
ad8e1ba5 11578 bfd_vma plt0;
9f951329 11579
721956f4 11580 /* Build the .glink plt call stub. */
97b639ba
AM
11581 if (htab->emit_stub_syms)
11582 {
11583 struct elf_link_hash_entry *h;
468392fb
AM
11584 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
11585 TRUE, FALSE, FALSE);
97b639ba
AM
11586 if (h == NULL)
11587 return FALSE;
11588 if (h->root.type == bfd_link_hash_new)
11589 {
11590 h->root.type = bfd_link_hash_defined;
11591 h->root.u.def.section = htab->glink;
ee4bf8d2 11592 h->root.u.def.value = 8;
f5385ebf
AM
11593 h->ref_regular = 1;
11594 h->def_regular = 1;
11595 h->ref_regular_nonweak = 1;
11596 h->forced_local = 1;
11597 h->non_elf = 0;
97b639ba
AM
11598 }
11599 }
176a0d42
AM
11600 plt0 = htab->plt->output_section->vma + htab->plt->output_offset - 16;
11601 if (info->emitrelocations)
11602 {
11603 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
11604 if (r == NULL)
11605 return FALSE;
11606 r->r_offset = (htab->glink->output_offset
11607 + htab->glink->output_section->vma);
11608 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
11609 r->r_addend = plt0;
11610 }
4ce794b7 11611 p = htab->glink->contents;
176a0d42 11612 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
ee4bf8d2
AM
11613 bfd_put_64 (htab->glink->owner, plt0, p);
11614 p += 8;
11615 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
ad8e1ba5 11616 p += 4;
ee4bf8d2 11617 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
ad8e1ba5 11618 p += 4;
ee4bf8d2 11619 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
ad8e1ba5 11620 p += 4;
ee4bf8d2 11621 bfd_put_32 (htab->glink->owner, LD_R2_M16R11, p);
ad8e1ba5 11622 p += 4;
ee4bf8d2 11623 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
ad8e1ba5 11624 p += 4;
ee4bf8d2 11625 bfd_put_32 (htab->glink->owner, ADD_R12_R2_R11, p);
ad8e1ba5 11626 p += 4;
ee4bf8d2 11627 bfd_put_32 (htab->glink->owner, LD_R11_0R12, p);
ad8e1ba5 11628 p += 4;
4ce794b7 11629 bfd_put_32 (htab->glink->owner, LD_R2_0R12 | 8, p);
ad8e1ba5 11630 p += 4;
4ce794b7 11631 bfd_put_32 (htab->glink->owner, MTCTR_R11, p);
ad8e1ba5 11632 p += 4;
4ce794b7 11633 bfd_put_32 (htab->glink->owner, LD_R11_0R12 | 16, p);
ad8e1ba5 11634 p += 4;
4ce794b7 11635 bfd_put_32 (htab->glink->owner, BCTR, p);
ad8e1ba5 11636 p += 4;
ee4bf8d2
AM
11637 while (p - htab->glink->contents < GLINK_CALL_STUB_SIZE)
11638 {
11639 bfd_put_32 (htab->glink->owner, NOP, p);
11640 p += 4;
11641 }
ad8e1ba5 11642
9f951329
AM
11643 /* Build the .glink lazy link call stubs. */
11644 indx = 0;
eea6121a 11645 while (p < htab->glink->contents + htab->glink->size)
9f951329
AM
11646 {
11647 if (indx < 0x8000)
11648 {
4ce794b7 11649 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
9f951329
AM
11650 p += 4;
11651 }
11652 else
11653 {
4ce794b7 11654 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
9f951329 11655 p += 4;
4ce794b7 11656 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx), p);
9f951329
AM
11657 p += 4;
11658 }
4ce794b7 11659 bfd_put_32 (htab->glink->owner,
ee4bf8d2 11660 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
a16d5acb 11661 indx++;
9f951329
AM
11662 p += 4;
11663 }
eea6121a 11664 htab->glink->rawsize = p - htab->glink->contents;
5d1634d7 11665 }
5d1634d7 11666
eea6121a 11667 if (htab->brlt->size != 0)
721956f4 11668 {
4ce794b7 11669 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
eea6121a 11670 htab->brlt->size);
4ce794b7 11671 if (htab->brlt->contents == NULL)
b34976b6 11672 return FALSE;
721956f4 11673 }
ee75fd95 11674 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
63bc6f6c
AM
11675 {
11676 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
eea6121a 11677 htab->relbrlt->size);
63bc6f6c
AM
11678 if (htab->relbrlt->contents == NULL)
11679 return FALSE;
11680 }
5d1634d7 11681
58d180e8
AM
11682 if (htab->glink_eh_frame != NULL
11683 && htab->glink_eh_frame->size != 0)
11684 {
11685 bfd_vma val;
11686
11687 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
11688 if (p == NULL)
11689 return FALSE;
11690 htab->glink_eh_frame->contents = p;
11691
11692 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
11693
11694 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
11695 /* CIE length (rewrite in case little-endian). */
11696 bfd_put_32 (htab->elf.dynobj, sizeof (glink_eh_frame_cie) - 4, p);
11697 p += sizeof (glink_eh_frame_cie);
11698
11699 for (stub_sec = htab->stub_bfd->sections;
11700 stub_sec != NULL;
11701 stub_sec = stub_sec->next)
11702 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
11703 {
11704 /* FDE length. */
11705 bfd_put_32 (htab->elf.dynobj, 16, p);
11706 p += 4;
11707 /* CIE pointer. */
11708 val = p - htab->glink_eh_frame->contents;
11709 bfd_put_32 (htab->elf.dynobj, val, p);
11710 p += 4;
11711 /* Offset to stub section. */
11712 val = (stub_sec->output_section->vma
11713 + stub_sec->output_offset);
11714 val -= (htab->glink_eh_frame->output_section->vma
11715 + htab->glink_eh_frame->output_offset);
11716 val -= p - htab->glink_eh_frame->contents;
11717 if (val + 0x80000000 > 0xffffffff)
11718 {
11719 info->callbacks->einfo
8de848d8 11720 (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
58d180e8
AM
11721 stub_sec->name);
11722 return FALSE;
11723 }
11724 bfd_put_32 (htab->elf.dynobj, val, p);
11725 p += 4;
11726 /* stub section size. */
11727 bfd_put_32 (htab->elf.dynobj, stub_sec->rawsize, p);
11728 p += 4;
11729 /* Augmentation. */
11730 p += 1;
11731 /* Pad. */
11732 p += 3;
11733 }
11734 if (htab->glink != NULL && htab->glink->size != 0)
11735 {
11736 /* FDE length. */
11737 bfd_put_32 (htab->elf.dynobj, 20, p);
11738 p += 4;
11739 /* CIE pointer. */
11740 val = p - htab->glink_eh_frame->contents;
11741 bfd_put_32 (htab->elf.dynobj, val, p);
11742 p += 4;
11743 /* Offset to .glink. */
11744 val = (htab->glink->output_section->vma
11745 + htab->glink->output_offset
11746 + 8);
11747 val -= (htab->glink_eh_frame->output_section->vma
11748 + htab->glink_eh_frame->output_offset);
11749 val -= p - htab->glink_eh_frame->contents;
11750 if (val + 0x80000000 > 0xffffffff)
11751 {
11752 info->callbacks->einfo
8de848d8 11753 (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
58d180e8
AM
11754 htab->glink->name);
11755 return FALSE;
11756 }
11757 bfd_put_32 (htab->elf.dynobj, val, p);
11758 p += 4;
11759 /* .glink size. */
11760 bfd_put_32 (htab->elf.dynobj, htab->glink->rawsize - 8, p);
11761 p += 4;
11762 /* Augmentation. */
11763 p += 1;
11764
11765 *p++ = DW_CFA_advance_loc + 1;
11766 *p++ = DW_CFA_register;
11767 *p++ = 65;
11768 *p++ = 12;
11769 *p++ = DW_CFA_advance_loc + 4;
11770 *p++ = DW_CFA_restore_extended;
11771 *p++ = 65;
11772 }
11773 htab->glink_eh_frame->size = p - htab->glink_eh_frame->contents;
11774 }
11775
721956f4
AM
11776 /* Build the stubs as directed by the stub hash table. */
11777 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
5d1634d7 11778
aa8a7074
AM
11779 if (htab->relbrlt != NULL)
11780 htab->relbrlt->reloc_count = 0;
11781
721956f4
AM
11782 for (stub_sec = htab->stub_bfd->sections;
11783 stub_sec != NULL;
11784 stub_sec = stub_sec->next)
e717da7e
AM
11785 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
11786 {
11787 stub_sec_count += 1;
eea6121a 11788 if (stub_sec->rawsize != stub_sec->size)
e717da7e
AM
11789 break;
11790 }
5d1634d7 11791
721956f4 11792 if (stub_sec != NULL
58d180e8
AM
11793 || htab->glink->rawsize != htab->glink->size
11794 || (htab->glink_eh_frame != NULL
11795 && htab->glink_eh_frame->rawsize != htab->glink_eh_frame->size))
5d1634d7 11796 {
b34976b6 11797 htab->stub_error = TRUE;
8de848d8 11798 info->callbacks->einfo (_("%P: stubs don't match calculated size\n"));
5d1634d7 11799 }
721956f4 11800
d2a300cf
AM
11801 if (htab->stub_error)
11802 return FALSE;
11803
11804 if (stats != NULL)
11805 {
11806 *stats = bfd_malloc (500);
11807 if (*stats == NULL)
11808 return FALSE;
11809
ee75fd95 11810 sprintf (*stats, _("linker stubs in %u group%s\n"
d2a300cf
AM
11811 " branch %lu\n"
11812 " toc adjust %lu\n"
11813 " long branch %lu\n"
11814 " long toc adj %lu\n"
11815 " plt call %lu"),
e717da7e 11816 stub_sec_count,
ee75fd95 11817 stub_sec_count == 1 ? "" : "s",
4ce794b7
AM
11818 htab->stub_count[ppc_stub_long_branch - 1],
11819 htab->stub_count[ppc_stub_long_branch_r2off - 1],
11820 htab->stub_count[ppc_stub_plt_branch - 1],
11821 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
11822 htab->stub_count[ppc_stub_plt_call - 1]);
d2a300cf
AM
11823 }
11824 return TRUE;
5bd4f169
AM
11825}
11826
99877b66
AM
11827/* This function undoes the changes made by add_symbol_adjust. */
11828
11829static bfd_boolean
11830undo_symbol_twiddle (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
11831{
11832 struct ppc_link_hash_entry *eh;
11833
11834 if (h->root.type == bfd_link_hash_indirect)
11835 return TRUE;
11836
99877b66
AM
11837 eh = (struct ppc_link_hash_entry *) h;
11838 if (eh->elf.root.type != bfd_link_hash_undefweak || !eh->was_undefined)
11839 return TRUE;
11840
11841 eh->elf.root.type = bfd_link_hash_undefined;
11842 return TRUE;
11843}
11844
11845void
11846ppc64_elf_restore_symbols (struct bfd_link_info *info)
11847{
11848 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4dfe6ac6
NC
11849
11850 if (htab != NULL)
11851 elf_link_hash_traverse (&htab->elf, undo_symbol_twiddle, info);
99877b66
AM
11852}
11853
60124e18
AM
11854/* What to do when ld finds relocations against symbols defined in
11855 discarded sections. */
11856
11857static unsigned int
11858ppc64_elf_action_discarded (asection *sec)
11859{
11860 if (strcmp (".opd", sec->name) == 0)
11861 return 0;
11862
11863 if (strcmp (".toc", sec->name) == 0)
11864 return 0;
11865
bce50a28
JJ
11866 if (strcmp (".toc1", sec->name) == 0)
11867 return 0;
11868
60124e18
AM
11869 return _bfd_elf_default_action_discarded (sec);
11870}
11871
5bd4f169
AM
11872/* The RELOCATE_SECTION function is called by the ELF backend linker
11873 to handle the relocations for a section.
11874
11875 The relocs are always passed as Rela structures; if the section
11876 actually uses Rel structures, the r_addend field will always be
11877 zero.
11878
11879 This function is responsible for adjust the section contents as
11880 necessary, and (if using Rela relocs and generating a
1049f94e 11881 relocatable output file) adjusting the reloc addend as
5bd4f169
AM
11882 necessary.
11883
11884 This function does not have to worry about setting the reloc
11885 address or the reloc symbol index.
11886
11887 LOCAL_SYMS is a pointer to the swapped in local symbols.
11888
11889 LOCAL_SECTIONS is an array giving the section in the input file
11890 corresponding to the st_shndx field of each local symbol.
11891
11892 The global hash table entry for the global symbols can be found
11893 via elf_sym_hashes (input_bfd).
11894
1049f94e 11895 When generating relocatable output, this function must handle
5bd4f169
AM
11896 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
11897 going to be the section symbol corresponding to the output
11898 section, which means that the addend must be adjusted
11899 accordingly. */
11900
b34976b6 11901static bfd_boolean
4ce794b7
AM
11902ppc64_elf_relocate_section (bfd *output_bfd,
11903 struct bfd_link_info *info,
11904 bfd *input_bfd,
11905 asection *input_section,
11906 bfd_byte *contents,
11907 Elf_Internal_Rela *relocs,
11908 Elf_Internal_Sym *local_syms,
11909 asection **local_sections)
5bd4f169 11910{
65f38f15 11911 struct ppc_link_hash_table *htab;
5bd4f169
AM
11912 Elf_Internal_Shdr *symtab_hdr;
11913 struct elf_link_hash_entry **sym_hashes;
5bd4f169
AM
11914 Elf_Internal_Rela *rel;
11915 Elf_Internal_Rela *relend;
411e1bfb
AM
11916 Elf_Internal_Rela outrel;
11917 bfd_byte *loc;
411e1bfb 11918 struct got_entry **local_got_ents;
5bd4f169 11919 bfd_vma TOCstart;
b34976b6
AM
11920 bfd_boolean ret = TRUE;
11921 bfd_boolean is_opd;
96e0dda4 11922 /* Disabled until we sort out how ld should choose 'y' vs 'at'. */
b34976b6 11923 bfd_boolean is_power4 = FALSE;
4fe5ca5b 11924 bfd_vma d_offset = (bfd_big_endian (output_bfd) ? 2 : 0);
5bd4f169 11925
65f38f15 11926 /* Initialize howto table if needed. */
5bd4f169 11927 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
11928 ppc_howto_init ();
11929
65f38f15 11930 htab = ppc_hash_table (info);
4dfe6ac6
NC
11931 if (htab == NULL)
11932 return FALSE;
ee75fd95
AM
11933
11934 /* Don't relocate stub sections. */
11935 if (input_section->owner == htab->stub_bfd)
11936 return TRUE;
11937
0c8d6e5c 11938 BFD_ASSERT (is_ppc64_elf (input_bfd));
0ffa91dd 11939
411e1bfb 11940 local_got_ents = elf_local_got_ents (input_bfd);
5bd4f169 11941 TOCstart = elf_gp (output_bfd);
0ffa91dd 11942 symtab_hdr = &elf_symtab_hdr (input_bfd);
5bd4f169 11943 sym_hashes = elf_sym_hashes (input_bfd);
7c8fe5c4 11944 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
65f38f15 11945
5bd4f169
AM
11946 rel = relocs;
11947 relend = relocs + input_section->reloc_count;
11948 for (; rel < relend; rel++)
11949 {
04c9666a 11950 enum elf_ppc64_reloc_type r_type;
4cc603a5 11951 bfd_vma addend, orig_addend;
5bd4f169
AM
11952 bfd_reloc_status_type r;
11953 Elf_Internal_Sym *sym;
11954 asection *sec;
039b3fef
AM
11955 struct elf_link_hash_entry *h_elf;
11956 struct ppc_link_hash_entry *h;
11957 struct ppc_link_hash_entry *fdh;
5bd4f169 11958 const char *sym_name;
0d4792f7 11959 unsigned long r_symndx, toc_symndx;
3a71aa26 11960 bfd_vma toc_addend;
f961d9dd
AM
11961 unsigned char tls_mask, tls_gd, tls_type;
11962 unsigned char sym_type;
5bd4f169 11963 bfd_vma relocation;
b34976b6
AM
11964 bfd_boolean unresolved_reloc;
11965 bfd_boolean warned;
67f0cbdb 11966 unsigned int insn;
e11840f9 11967 unsigned int mask;
721956f4
AM
11968 struct ppc_stub_hash_entry *stub_entry;
11969 bfd_vma max_br_offset;
11970 bfd_vma from;
5bd4f169 11971
4ce794b7 11972 r_type = ELF64_R_TYPE (rel->r_info);
5bd4f169 11973 r_symndx = ELF64_R_SYM (rel->r_info);
ee87f2da
AM
11974
11975 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
11976 symbol of the previous ADDR64 reloc. The symbol gives us the
11977 proper TOC base to use. */
11978 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
11979 && rel != relocs
11980 && ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_ADDR64
11981 && is_opd)
11982 r_symndx = ELF64_R_SYM (rel[-1].r_info);
11983
4ce794b7
AM
11984 sym = NULL;
11985 sec = NULL;
039b3fef 11986 h_elf = NULL;
4ce794b7 11987 sym_name = NULL;
b34976b6
AM
11988 unresolved_reloc = FALSE;
11989 warned = FALSE;
4cc603a5 11990 orig_addend = rel->r_addend;
65f38f15 11991
0b13192e 11992 if (r_symndx < symtab_hdr->sh_info)
5bd4f169
AM
11993 {
11994 /* It's a local symbol. */
74f0fb50 11995 struct _opd_sec_data *opd;
4025353c 11996
5bd4f169
AM
11997 sym = local_syms + r_symndx;
11998 sec = local_sections[r_symndx];
26c61ae5 11999 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
0d4792f7 12000 sym_type = ELF64_ST_TYPE (sym->st_info);
8517fae7 12001 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
74f0fb50
AM
12002 opd = get_opd_info (sec);
12003 if (opd != NULL && opd->adjust != NULL)
1e2f5b6e 12004 {
74f0fb50 12005 long adjust = opd->adjust[(sym->st_value + rel->r_addend) / 8];
4025353c
AM
12006 if (adjust == -1)
12007 relocation = 0;
12008 else
4cc603a5
AM
12009 {
12010 /* If this is a relocation against the opd section sym
12011 and we have edited .opd, adjust the reloc addend so
12012 that ld -r and ld --emit-relocs output is correct.
12013 If it is a reloc against some other .opd symbol,
12014 then the symbol value will be adjusted later. */
12015 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
12016 rel->r_addend += adjust;
12017 else
12018 relocation += adjust;
12019 }
1e2f5b6e 12020 }
5bd4f169
AM
12021 }
12022 else
12023 {
b2a8e766
AM
12024 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
12025 r_symndx, symtab_hdr, sym_hashes,
039b3fef 12026 h_elf, sec, relocation,
b2a8e766 12027 unresolved_reloc, warned);
039b3fef
AM
12028 sym_name = h_elf->root.root.string;
12029 sym_type = h_elf->type;
5bd4f169 12030 }
039b3fef 12031 h = (struct ppc_link_hash_entry *) h_elf;
5bd4f169 12032
ab96bf03 12033 if (sec != NULL && elf_discarded_section (sec))
e4067dbb
DJ
12034 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
12035 rel, relend,
12036 ppc64_elf_howto_table[r_type],
12037 contents);
ab96bf03
AM
12038
12039 if (info->relocatable)
12040 continue;
12041
951fd09b
AM
12042 /* TLS optimizations. Replace instruction sequences and relocs
12043 based on information we collected in tls_optimize. We edit
12044 RELOCS so that --emit-relocs will output something sensible
12045 for the final instruction stream. */
12046 tls_mask = 0;
12047 tls_gd = 0;
0d4792f7 12048 toc_symndx = 0;
727fc41e
AM
12049 if (h != NULL)
12050 tls_mask = h->tls_mask;
12051 else if (local_got_ents != NULL)
411e1bfb 12052 {
e054468f
AM
12053 struct plt_entry **local_plt = (struct plt_entry **)
12054 (local_got_ents + symtab_hdr->sh_info);
f961d9dd 12055 unsigned char *lgot_masks = (unsigned char *)
e054468f 12056 (local_plt + symtab_hdr->sh_info);
727fc41e
AM
12057 tls_mask = lgot_masks[r_symndx];
12058 }
12059 if (tls_mask == 0
12060 && (r_type == R_PPC64_TLS
12061 || r_type == R_PPC64_TLSGD
12062 || r_type == R_PPC64_TLSLD))
12063 {
12064 /* Check for toc tls entries. */
f961d9dd 12065 unsigned char *toc_tls;
0d4792f7 12066
727fc41e
AM
12067 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
12068 &local_syms, rel, input_bfd))
12069 return FALSE;
0d4792f7 12070
727fc41e
AM
12071 if (toc_tls)
12072 tls_mask = *toc_tls;
0d4792f7
AM
12073 }
12074
12075 /* Check that tls relocs are used with tls syms, and non-tls
12076 relocs are used with non-tls syms. */
cf35638d 12077 if (r_symndx != STN_UNDEF
0d4792f7
AM
12078 && r_type != R_PPC64_NONE
12079 && (h == NULL
039b3fef
AM
12080 || h->elf.root.type == bfd_link_hash_defined
12081 || h->elf.root.type == bfd_link_hash_defweak)
1d483afe
AM
12082 && (IS_PPC64_TLS_RELOC (r_type)
12083 != (sym_type == STT_TLS
12084 || (sym_type == STT_SECTION
12085 && (sec->flags & SEC_THREAD_LOCAL) != 0))))
0d4792f7 12086 {
727fc41e
AM
12087 if (tls_mask != 0
12088 && (r_type == R_PPC64_TLS
12089 || r_type == R_PPC64_TLSGD
12090 || r_type == R_PPC64_TLSLD))
0d4792f7
AM
12091 /* R_PPC64_TLS is OK against a symbol in the TOC. */
12092 ;
12093 else
25f53a85 12094 info->callbacks->einfo
1d483afe 12095 (!IS_PPC64_TLS_RELOC (r_type)
8de848d8
AM
12096 ? _("%P: %H: %s used with TLS symbol %s\n")
12097 : _("%P: %H: %s used with non-TLS symbol %s\n"),
25f53a85 12098 input_bfd, input_section, rel->r_offset,
0d4792f7
AM
12099 ppc64_elf_howto_table[r_type]->name,
12100 sym_name);
411e1bfb
AM
12101 }
12102
12103 /* Ensure reloc mapping code below stays sane. */
12104 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
12105 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
12106 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
12107 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
12108 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
12109 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
12110 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
12111 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
12112 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
12113 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
12114 abort ();
0d4792f7 12115
411e1bfb
AM
12116 switch (r_type)
12117 {
12118 default:
411e1bfb
AM
12119 break;
12120
ba761f19
AM
12121 case R_PPC64_LO_DS_OPT:
12122 insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
12123 if ((insn & (0x3f << 26)) != 58u << 26)
12124 abort ();
12125 insn += (14u << 26) - (58u << 26);
12126 bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
12127 r_type = R_PPC64_TOC16_LO;
12128 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12129 break;
12130
411e1bfb
AM
12131 case R_PPC64_TOC16:
12132 case R_PPC64_TOC16_LO:
12133 case R_PPC64_TOC16_DS:
12134 case R_PPC64_TOC16_LO_DS:
411e1bfb
AM
12135 {
12136 /* Check for toc tls entries. */
f961d9dd 12137 unsigned char *toc_tls;
951fd09b 12138 int retval;
411e1bfb 12139
3a71aa26
AM
12140 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
12141 &local_syms, rel, input_bfd);
951fd09b 12142 if (retval == 0)
411e1bfb
AM
12143 return FALSE;
12144
12145 if (toc_tls)
12146 {
951fd09b 12147 tls_mask = *toc_tls;
411e1bfb
AM
12148 if (r_type == R_PPC64_TOC16_DS
12149 || r_type == R_PPC64_TOC16_LO_DS)
81407a69
AM
12150 {
12151 if (tls_mask != 0
12152 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
12153 goto toctprel;
12154 }
411e1bfb 12155 else
951fd09b
AM
12156 {
12157 /* If we found a GD reloc pair, then we might be
12158 doing a GD->IE transition. */
12159 if (retval == 2)
12160 {
12161 tls_gd = TLS_TPRELGD;
12162 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
102890f0 12163 goto tls_ldgd_opt;
951fd09b
AM
12164 }
12165 else if (retval == 3)
12166 {
12167 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
102890f0 12168 goto tls_ldgd_opt;
951fd09b
AM
12169 }
12170 }
411e1bfb
AM
12171 }
12172 }
12173 break;
12174
9d6ded02
AM
12175 case R_PPC64_GOT_TPREL16_HI:
12176 case R_PPC64_GOT_TPREL16_HA:
12177 if (tls_mask != 0
12178 && (tls_mask & TLS_TPREL) == 0)
12179 {
12180 rel->r_offset -= d_offset;
12181 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
12182 r_type = R_PPC64_NONE;
12183 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12184 }
12185 break;
12186
411e1bfb
AM
12187 case R_PPC64_GOT_TPREL16_DS:
12188 case R_PPC64_GOT_TPREL16_LO_DS:
951fd09b
AM
12189 if (tls_mask != 0
12190 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 12191 {
81407a69 12192 toctprel:
4fe5ca5b 12193 insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
411e1bfb
AM
12194 insn &= 31 << 21;
12195 insn |= 0x3c0d0000; /* addis 0,13,0 */
4fe5ca5b 12196 bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
411e1bfb 12197 r_type = R_PPC64_TPREL16_HA;
0d4792f7
AM
12198 if (toc_symndx != 0)
12199 {
12200 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 12201 rel->r_addend = toc_addend;
0d4792f7
AM
12202 /* We changed the symbol. Start over in order to
12203 get h, sym, sec etc. right. */
12204 rel--;
12205 continue;
12206 }
12207 else
12208 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
12209 }
12210 break;
12211
12212 case R_PPC64_TLS:
951fd09b
AM
12213 if (tls_mask != 0
12214 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 12215 {
411e1bfb 12216 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
2d0f3896
AM
12217 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
12218 if (insn == 0)
411e1bfb 12219 abort ();
411e1bfb 12220 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
411e1bfb 12221 /* Was PPC64_TLS which sits on insn boundary, now
4fe5ca5b
GM
12222 PPC64_TPREL16_LO which is at low-order half-word. */
12223 rel->r_offset += d_offset;
0d4792f7
AM
12224 r_type = R_PPC64_TPREL16_LO;
12225 if (toc_symndx != 0)
12226 {
12227 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 12228 rel->r_addend = toc_addend;
0d4792f7
AM
12229 /* We changed the symbol. Start over in order to
12230 get h, sym, sec etc. right. */
12231 rel--;
12232 continue;
12233 }
12234 else
12235 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
12236 }
12237 break;
12238
411e1bfb
AM
12239 case R_PPC64_GOT_TLSGD16_HI:
12240 case R_PPC64_GOT_TLSGD16_HA:
951fd09b
AM
12241 tls_gd = TLS_TPRELGD;
12242 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
12243 goto tls_gdld_hi;
12244 break;
12245
411e1bfb
AM
12246 case R_PPC64_GOT_TLSLD16_HI:
12247 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 12248 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
411e1bfb 12249 {
951fd09b
AM
12250 tls_gdld_hi:
12251 if ((tls_mask & tls_gd) != 0)
12252 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
12253 + R_PPC64_GOT_TPREL16_DS);
12254 else
411e1bfb 12255 {
4fe5ca5b 12256 rel->r_offset -= d_offset;
727ac201 12257 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
951fd09b 12258 r_type = R_PPC64_NONE;
411e1bfb 12259 }
951fd09b 12260 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
12261 }
12262 break;
12263
951fd09b
AM
12264 case R_PPC64_GOT_TLSGD16:
12265 case R_PPC64_GOT_TLSGD16_LO:
12266 tls_gd = TLS_TPRELGD;
12267 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
102890f0 12268 goto tls_ldgd_opt;
951fd09b 12269 break;
411e1bfb 12270
951fd09b
AM
12271 case R_PPC64_GOT_TLSLD16:
12272 case R_PPC64_GOT_TLSLD16_LO:
12273 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
12274 {
3a71aa26 12275 unsigned int insn1, insn2, insn3;
102890f0
AM
12276 bfd_vma offset;
12277
12278 tls_ldgd_opt:
727fc41e
AM
12279 offset = (bfd_vma) -1;
12280 /* If not using the newer R_PPC64_TLSGD/LD to mark
12281 __tls_get_addr calls, we must trust that the call
12282 stays with its arg setup insns, ie. that the next
12283 reloc is the __tls_get_addr call associated with
12284 the current reloc. Edit both insns. */
12285 if (input_section->has_tls_get_addr_call
12286 && rel + 1 < relend
12287 && branch_reloc_hash_match (input_bfd, rel + 1,
12288 htab->tls_get_addr,
12289 htab->tls_get_addr_fd))
12290 offset = rel[1].r_offset;
102890f0 12291 if ((tls_mask & tls_gd) != 0)
411e1bfb 12292 {
102890f0 12293 /* IE */
3a71aa26
AM
12294 insn1 = bfd_get_32 (output_bfd,
12295 contents + rel->r_offset - d_offset);
102890f0
AM
12296 insn1 &= (1 << 26) - (1 << 2);
12297 insn1 |= 58 << 26; /* ld */
12298 insn2 = 0x7c636a14; /* add 3,3,13 */
727fc41e 12299 if (offset != (bfd_vma) -1)
f58d5a2d 12300 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
102890f0
AM
12301 if ((tls_mask & TLS_EXPLICIT) == 0)
12302 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
12303 + R_PPC64_GOT_TPREL16_DS);
411e1bfb 12304 else
102890f0
AM
12305 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
12306 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12307 }
12308 else
12309 {
12310 /* LE */
12311 insn1 = 0x3c6d0000; /* addis 3,13,0 */
12312 insn2 = 0x38630000; /* addi 3,3,0 */
12313 if (tls_gd == 0)
951fd09b 12314 {
102890f0 12315 /* Was an LD reloc. */
1d483afe
AM
12316 if (toc_symndx)
12317 sec = local_sections[toc_symndx];
12318 for (r_symndx = 0;
12319 r_symndx < symtab_hdr->sh_info;
12320 r_symndx++)
12321 if (local_sections[r_symndx] == sec)
12322 break;
12323 if (r_symndx >= symtab_hdr->sh_info)
cf35638d 12324 r_symndx = STN_UNDEF;
102890f0 12325 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
cf35638d 12326 if (r_symndx != STN_UNDEF)
1d483afe
AM
12327 rel->r_addend -= (local_syms[r_symndx].st_value
12328 + sec->output_offset
12329 + sec->output_section->vma);
951fd09b 12330 }
102890f0 12331 else if (toc_symndx != 0)
3a71aa26
AM
12332 {
12333 r_symndx = toc_symndx;
12334 rel->r_addend = toc_addend;
12335 }
102890f0
AM
12336 r_type = R_PPC64_TPREL16_HA;
12337 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
727fc41e
AM
12338 if (offset != (bfd_vma) -1)
12339 {
12340 rel[1].r_info = ELF64_R_INFO (r_symndx,
12341 R_PPC64_TPREL16_LO);
12342 rel[1].r_offset = offset + d_offset;
12343 rel[1].r_addend = rel->r_addend;
12344 }
102890f0 12345 }
3a71aa26
AM
12346 bfd_put_32 (output_bfd, insn1,
12347 contents + rel->r_offset - d_offset);
727fc41e
AM
12348 if (offset != (bfd_vma) -1)
12349 {
12350 insn3 = bfd_get_32 (output_bfd,
12351 contents + offset + 4);
12352 if (insn3 == NOP
12353 || insn3 == CROR_151515 || insn3 == CROR_313131)
12354 {
12355 rel[1].r_offset += 4;
12356 bfd_put_32 (output_bfd, insn2, contents + offset + 4);
12357 insn2 = NOP;
12358 }
12359 bfd_put_32 (output_bfd, insn2, contents + offset);
12360 }
12361 if ((tls_mask & tls_gd) == 0
12362 && (tls_gd == 0 || toc_symndx != 0))
12363 {
12364 /* We changed the symbol. Start over in order
12365 to get h, sym, sec etc. right. */
12366 rel--;
12367 continue;
12368 }
12369 }
12370 break;
12371
12372 case R_PPC64_TLSGD:
12373 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
12374 {
12375 unsigned int insn2, insn3;
12376 bfd_vma offset = rel->r_offset;
12377
12378 if ((tls_mask & TLS_TPRELGD) != 0)
12379 {
12380 /* IE */
12381 r_type = R_PPC64_NONE;
12382 insn2 = 0x7c636a14; /* add 3,3,13 */
12383 }
12384 else
12385 {
12386 /* LE */
12387 if (toc_symndx != 0)
12388 {
12389 r_symndx = toc_symndx;
12390 rel->r_addend = toc_addend;
12391 }
12392 r_type = R_PPC64_TPREL16_LO;
12393 rel->r_offset = offset + d_offset;
12394 insn2 = 0x38630000; /* addi 3,3,0 */
12395 }
12396 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12397 /* Zap the reloc on the _tls_get_addr call too. */
12398 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 12399 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
3a71aa26
AM
12400 insn3 = bfd_get_32 (output_bfd,
12401 contents + offset + 4);
102890f0
AM
12402 if (insn3 == NOP
12403 || insn3 == CROR_151515 || insn3 == CROR_313131)
12404 {
727fc41e 12405 rel->r_offset += 4;
3a71aa26
AM
12406 bfd_put_32 (output_bfd, insn2, contents + offset + 4);
12407 insn2 = NOP;
102890f0 12408 }
102890f0 12409 bfd_put_32 (output_bfd, insn2, contents + offset);
727fc41e 12410 if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
102890f0 12411 {
102890f0
AM
12412 rel--;
12413 continue;
411e1bfb 12414 }
411e1bfb 12415 }
411e1bfb
AM
12416 break;
12417
727fc41e
AM
12418 case R_PPC64_TLSLD:
12419 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
12420 {
12421 unsigned int insn2, insn3;
12422 bfd_vma offset = rel->r_offset;
12423
12424 if (toc_symndx)
12425 sec = local_sections[toc_symndx];
12426 for (r_symndx = 0;
12427 r_symndx < symtab_hdr->sh_info;
12428 r_symndx++)
12429 if (local_sections[r_symndx] == sec)
12430 break;
12431 if (r_symndx >= symtab_hdr->sh_info)
cf35638d 12432 r_symndx = STN_UNDEF;
727fc41e 12433 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
cf35638d 12434 if (r_symndx != STN_UNDEF)
727fc41e
AM
12435 rel->r_addend -= (local_syms[r_symndx].st_value
12436 + sec->output_offset
12437 + sec->output_section->vma);
12438
12439 r_type = R_PPC64_TPREL16_LO;
12440 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12441 rel->r_offset = offset + d_offset;
12442 /* Zap the reloc on the _tls_get_addr call too. */
12443 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 12444 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
727fc41e
AM
12445 insn2 = 0x38630000; /* addi 3,3,0 */
12446 insn3 = bfd_get_32 (output_bfd,
12447 contents + offset + 4);
12448 if (insn3 == NOP
12449 || insn3 == CROR_151515 || insn3 == CROR_313131)
12450 {
12451 rel->r_offset += 4;
12452 bfd_put_32 (output_bfd, insn2, contents + offset + 4);
12453 insn2 = NOP;
12454 }
12455 bfd_put_32 (output_bfd, insn2, contents + offset);
12456 rel--;
12457 continue;
12458 }
12459 break;
12460
411e1bfb 12461 case R_PPC64_DTPMOD64:
951fd09b
AM
12462 if (rel + 1 < relend
12463 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
12464 && rel[1].r_offset == rel->r_offset + 8)
411e1bfb 12465 {
951fd09b
AM
12466 if ((tls_mask & TLS_GD) == 0)
12467 {
12468 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
12469 if ((tls_mask & TLS_TPRELGD) != 0)
12470 r_type = R_PPC64_TPREL64;
12471 else
12472 {
4ce794b7 12473 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
951fd09b
AM
12474 r_type = R_PPC64_NONE;
12475 }
12476 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12477 }
12478 }
12479 else
12480 {
12481 if ((tls_mask & TLS_LD) == 0)
411e1bfb 12482 {
4ce794b7 12483 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
411e1bfb 12484 r_type = R_PPC64_NONE;
951fd09b 12485 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb 12486 }
411e1bfb
AM
12487 }
12488 break;
12489
12490 case R_PPC64_TPREL64:
951fd09b 12491 if ((tls_mask & TLS_TPREL) == 0)
411e1bfb
AM
12492 {
12493 r_type = R_PPC64_NONE;
12494 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12495 }
12496 break;
12497 }
12498
12499 /* Handle other relocations that tweak non-addend part of insn. */
86c76c7b 12500 insn = 0;
b25116a9
AM
12501 max_br_offset = 1 << 25;
12502 addend = rel->r_addend;
65f38f15 12503 switch (r_type)
5bd4f169
AM
12504 {
12505 default:
65f38f15 12506 break;
5bd4f169 12507
3b421ab3
AM
12508 case R_PPC64_TOCSAVE:
12509 if (relocation + addend == (rel->r_offset
12510 + input_section->output_offset
12511 + input_section->output_section->vma)
12512 && tocsave_find (htab, NO_INSERT,
12513 &local_syms, rel, input_bfd))
12514 {
12515 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
12516 if (insn == NOP
12517 || insn == CROR_151515 || insn == CROR_313131)
12518 bfd_put_32 (input_bfd, STD_R2_40R1,
12519 contents + rel->r_offset);
12520 }
12521 break;
12522
65f38f15
AM
12523 /* Branch taken prediction relocations. */
12524 case R_PPC64_ADDR14_BRTAKEN:
12525 case R_PPC64_REL14_BRTAKEN:
cedb70c5
KH
12526 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
12527 /* Fall thru. */
65f38f15 12528
86c76c7b 12529 /* Branch not taken prediction relocations. */
65f38f15
AM
12530 case R_PPC64_ADDR14_BRNTAKEN:
12531 case R_PPC64_REL14_BRNTAKEN:
411e1bfb
AM
12532 insn |= bfd_get_32 (output_bfd,
12533 contents + rel->r_offset) & ~(0x01 << 21);
b25116a9 12534 /* Fall thru. */
86c76c7b 12535
b25116a9
AM
12536 case R_PPC64_REL14:
12537 max_br_offset = 1 << 15;
12538 /* Fall thru. */
5bd4f169 12539
65f38f15 12540 case R_PPC64_REL24:
ad8e1ba5
AM
12541 /* Calls to functions with a different TOC, such as calls to
12542 shared objects, need to alter the TOC pointer. This is
12543 done using a linkage stub. A REL24 branching to these
12544 linkage stubs needs to be followed by a nop, as the nop
12545 will be replaced with an instruction to restore the TOC
12546 base pointer. */
8387904d 12547 fdh = h;
b31867b6
AM
12548 if (h != NULL
12549 && h->oh != NULL
12550 && h->oh->is_func_descriptor)
12551 fdh = ppc_follow_link (h->oh);
6abec6bc
AM
12552 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, rel, htab);
12553 if (stub_entry != NULL
ad8e1ba5
AM
12554 && (stub_entry->stub_type == ppc_stub_plt_call
12555 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
12556 || stub_entry->stub_type == ppc_stub_long_branch_r2off))
41bd81ab 12557 {
b25116a9 12558 bfd_boolean can_plt_call = FALSE;
721956f4 12559
eea6121a 12560 if (rel->r_offset + 8 <= input_section->size)
41bd81ab 12561 {
b25116a9
AM
12562 unsigned long nop;
12563 nop = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
12564 if (nop == NOP
12565 || nop == CROR_151515 || nop == CROR_313131)
41bd81ab 12566 {
a7f2871e
AM
12567 if (h != NULL
12568 && (h == htab->tls_get_addr_fd
12569 || h == htab->tls_get_addr)
12570 && !htab->no_tls_get_addr_opt)
12571 {
12572 /* Special stub used, leave nop alone. */
12573 }
12574 else
12575 bfd_put_32 (input_bfd, LD_R2_40R1,
12576 contents + rel->r_offset + 4);
b25116a9 12577 can_plt_call = TRUE;
41bd81ab 12578 }
5bd4f169 12579 }
721956f4
AM
12580
12581 if (!can_plt_call)
12582 {
ad8e1ba5
AM
12583 if (stub_entry->stub_type == ppc_stub_plt_call)
12584 {
12585 /* If this is a plain branch rather than a branch
4fa1c237
AM
12586 and link, don't require a nop. However, don't
12587 allow tail calls in a shared library as they
12588 will result in r2 being corrupted. */
b25116a9
AM
12589 unsigned long br;
12590 br = bfd_get_32 (input_bfd, contents + rel->r_offset);
4fa1c237 12591 if (info->executable && (br & 1) == 0)
b25116a9 12592 can_plt_call = TRUE;
15b552e9
AM
12593 else
12594 stub_entry = NULL;
ad8e1ba5 12595 }
6ab189d5 12596 else if (h != NULL
039b3fef 12597 && strcmp (h->elf.root.root.string,
6ab189d5
AM
12598 ".__libc_start_main") == 0)
12599 {
12600 /* Allow crt1 branch to go via a toc adjusting stub. */
b25116a9 12601 can_plt_call = TRUE;
6ab189d5 12602 }
ad8e1ba5
AM
12603 else
12604 {
12605 if (strcmp (input_section->output_section->name,
12606 ".init") == 0
12607 || strcmp (input_section->output_section->name,
12608 ".fini") == 0)
25f53a85 12609 info->callbacks->einfo
8de848d8 12610 (_("%P: %H: automatic multiple TOCs "
ad8e1ba5 12611 "not supported using your crt files; "
25f53a85
AM
12612 "recompile with -mminimal-toc or upgrade gcc\n"),
12613 input_bfd, input_section, rel->r_offset);
ad8e1ba5 12614 else
25f53a85 12615 info->callbacks->einfo
8de848d8 12616 (_("%P: %H: sibling call optimization to `%s' "
ad8e1ba5
AM
12617 "does not allow automatic multiple TOCs; "
12618 "recompile with -mminimal-toc or "
12619 "-fno-optimize-sibling-calls, "
25f53a85
AM
12620 "or make `%s' extern\n"),
12621 input_bfd, input_section, rel->r_offset,
ad8e1ba5
AM
12622 sym_name,
12623 sym_name);
12624 bfd_set_error (bfd_error_bad_value);
12625 ret = FALSE;
12626 }
721956f4
AM
12627 }
12628
b25116a9
AM
12629 if (can_plt_call
12630 && stub_entry->stub_type == ppc_stub_plt_call)
12631 unresolved_reloc = FALSE;
12632 }
12633
6abec6bc
AM
12634 if ((stub_entry == NULL
12635 || stub_entry->stub_type == ppc_stub_long_branch
12636 || stub_entry->stub_type == ppc_stub_plt_branch)
8387904d
AM
12637 && get_opd_info (sec) != NULL)
12638 {
12639 /* The branch destination is the value of the opd entry. */
4cc603a5
AM
12640 bfd_vma off = (relocation + addend
12641 - sec->output_section->vma
12642 - sec->output_offset);
8387904d
AM
12643 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL);
12644 if (dest != (bfd_vma) -1)
12645 {
12646 relocation = dest;
12647 addend = 0;
12648 }
12649 }
12650
b25116a9
AM
12651 /* If the branch is out of reach we ought to have a long
12652 branch stub. */
12653 from = (rel->r_offset
12654 + input_section->output_offset
12655 + input_section->output_section->vma);
12656
6abec6bc
AM
12657 if (stub_entry != NULL
12658 && (stub_entry->stub_type == ppc_stub_long_branch
12659 || stub_entry->stub_type == ppc_stub_plt_branch)
12660 && (r_type == R_PPC64_ADDR14_BRTAKEN
12661 || r_type == R_PPC64_ADDR14_BRNTAKEN
12662 || (relocation + addend - from + max_br_offset
12663 < 2 * max_br_offset)))
12664 /* Don't use the stub if this branch is in range. */
12665 stub_entry = NULL;
b25116a9
AM
12666
12667 if (stub_entry != NULL)
12668 {
12669 /* Munge up the value and addend so that we call the stub
12670 rather than the procedure directly. */
12671 relocation = (stub_entry->stub_offset
12672 + stub_entry->stub_sec->output_offset
12673 + stub_entry->stub_sec->output_section->vma);
12674 addend = 0;
3b421ab3
AM
12675
12676 if (stub_entry->stub_type == ppc_stub_plt_call
12677 && rel + 1 < relend
12678 && rel[1].r_offset == rel->r_offset + 4
12679 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
12680 relocation += 4;
b25116a9
AM
12681 }
12682
12683 if (insn != 0)
12684 {
12685 if (is_power4)
721956f4 12686 {
b25116a9
AM
12687 /* Set 'a' bit. This is 0b00010 in BO field for branch
12688 on CR(BI) insns (BO == 001at or 011at), and 0b01000
12689 for branch on CTR insns (BO == 1a00t or 1a01t). */
12690 if ((insn & (0x14 << 21)) == (0x04 << 21))
12691 insn |= 0x02 << 21;
12692 else if ((insn & (0x14 << 21)) == (0x10 << 21))
12693 insn |= 0x08 << 21;
12694 else
12695 break;
12696 }
12697 else
12698 {
12699 /* Invert 'y' bit if not the default. */
4cc603a5 12700 if ((bfd_signed_vma) (relocation + addend - from) < 0)
b25116a9 12701 insn ^= 0x01 << 21;
721956f4 12702 }
b25116a9
AM
12703
12704 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
5bd4f169 12705 }
e86ce104 12706
06da1e8e
AM
12707 /* NOP out calls to undefined weak functions.
12708 We can thus call a weak function without first
12709 checking whether the function is defined. */
b25116a9 12710 else if (h != NULL
039b3fef 12711 && h->elf.root.type == bfd_link_hash_undefweak
766bc656 12712 && h->elf.dynindx == -1
b25116a9
AM
12713 && r_type == R_PPC64_REL24
12714 && relocation == 0
4cc603a5 12715 && addend == 0)
e86ce104 12716 {
06da1e8e
AM
12717 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
12718 continue;
e86ce104 12719 }
65f38f15
AM
12720 break;
12721 }
5bd4f169 12722
65f38f15 12723 /* Set `addend'. */
411e1bfb 12724 tls_type = 0;
65f38f15
AM
12725 switch (r_type)
12726 {
12727 default:
25f53a85 12728 info->callbacks->einfo
8de848d8 12729 (_("%P: %B: unknown relocation type %d for symbol %s\n"),
d003868e 12730 input_bfd, (int) r_type, sym_name);
5bd4f169 12731
65f38f15 12732 bfd_set_error (bfd_error_bad_value);
b34976b6 12733 ret = FALSE;
65f38f15 12734 continue;
5bd4f169 12735
65f38f15 12736 case R_PPC64_NONE:
411e1bfb 12737 case R_PPC64_TLS:
727fc41e
AM
12738 case R_PPC64_TLSGD:
12739 case R_PPC64_TLSLD:
3b421ab3 12740 case R_PPC64_TOCSAVE:
04c9666a
AM
12741 case R_PPC64_GNU_VTINHERIT:
12742 case R_PPC64_GNU_VTENTRY:
65f38f15 12743 continue;
5bd4f169
AM
12744
12745 /* GOT16 relocations. Like an ADDR16 using the symbol's
12746 address in the GOT as relocation value instead of the
411e1bfb 12747 symbol's value itself. Also, create a GOT entry for the
5bd4f169 12748 symbol and put the symbol value there. */
411e1bfb
AM
12749 case R_PPC64_GOT_TLSGD16:
12750 case R_PPC64_GOT_TLSGD16_LO:
12751 case R_PPC64_GOT_TLSGD16_HI:
12752 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 12753 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
12754 goto dogot;
12755
12756 case R_PPC64_GOT_TLSLD16:
12757 case R_PPC64_GOT_TLSLD16_LO:
12758 case R_PPC64_GOT_TLSLD16_HI:
12759 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 12760 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
12761 goto dogot;
12762
12763 case R_PPC64_GOT_TPREL16_DS:
12764 case R_PPC64_GOT_TPREL16_LO_DS:
12765 case R_PPC64_GOT_TPREL16_HI:
12766 case R_PPC64_GOT_TPREL16_HA:
12767 tls_type = TLS_TLS | TLS_TPREL;
12768 goto dogot;
12769
12770 case R_PPC64_GOT_DTPREL16_DS:
12771 case R_PPC64_GOT_DTPREL16_LO_DS:
12772 case R_PPC64_GOT_DTPREL16_HI:
12773 case R_PPC64_GOT_DTPREL16_HA:
12774 tls_type = TLS_TLS | TLS_DTPREL;
12775 goto dogot;
12776
65f38f15
AM
12777 case R_PPC64_GOT16:
12778 case R_PPC64_GOT16_LO:
12779 case R_PPC64_GOT16_HI:
12780 case R_PPC64_GOT16_HA:
12781 case R_PPC64_GOT16_DS:
12782 case R_PPC64_GOT16_LO_DS:
411e1bfb 12783 dogot:
5bd4f169
AM
12784 {
12785 /* Relocation is to the entry for this symbol in the global
12786 offset table. */
e717da7e 12787 asection *got;
d881513a 12788 bfd_vma *offp;
5bd4f169 12789 bfd_vma off;
d881513a 12790 unsigned long indx = 0;
927be08e 12791 struct got_entry *ent;
65f38f15 12792
d881513a
AM
12793 if (tls_type == (TLS_TLS | TLS_LD)
12794 && (h == NULL
f5385ebf 12795 || !h->elf.def_dynamic))
927be08e 12796 ent = ppc64_tlsld_got (input_bfd);
411e1bfb 12797 else
5bd4f169 12798 {
5bd4f169 12799
d881513a
AM
12800 if (h != NULL)
12801 {
12802 bfd_boolean dyn = htab->elf.dynamic_sections_created;
039b3fef
AM
12803 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared,
12804 &h->elf)
d881513a 12805 || (info->shared
726d3ab0 12806 && SYMBOL_CALLS_LOCAL (info, &h->elf)))
d881513a
AM
12807 /* This is actually a static link, or it is a
12808 -Bsymbolic link and the symbol is defined
12809 locally, or the symbol was forced to be local
12810 because of a version file. */
12811 ;
12812 else
12813 {
039b3fef 12814 indx = h->elf.dynindx;
d881513a
AM
12815 unresolved_reloc = FALSE;
12816 }
039b3fef 12817 ent = h->elf.got.glist;
d881513a 12818 }
411e1bfb 12819 else
5bd4f169 12820 {
d881513a
AM
12821 if (local_got_ents == NULL)
12822 abort ();
12823 ent = local_got_ents[r_symndx];
5bd4f169 12824 }
d881513a
AM
12825
12826 for (; ent != NULL; ent = ent->next)
4cc603a5 12827 if (ent->addend == orig_addend
e717da7e 12828 && ent->owner == input_bfd
d881513a
AM
12829 && ent->tls_type == tls_type)
12830 break;
5bd4f169 12831 }
411e1bfb 12832
927be08e
AM
12833 if (ent == NULL)
12834 abort ();
12835 if (ent->is_indirect)
12836 ent = ent->got.ent;
12837 offp = &ent->got.offset;
12838 got = ppc64_elf_tdata (ent->owner)->got;
e717da7e
AM
12839 if (got == NULL)
12840 abort ();
12841
411e1bfb
AM
12842 /* The offset must always be a multiple of 8. We use the
12843 least significant bit to record whether we have already
12844 processed this entry. */
d881513a 12845 off = *offp;
411e1bfb
AM
12846 if ((off & 1) != 0)
12847 off &= ~1;
5bd4f169
AM
12848 else
12849 {
411e1bfb
AM
12850 /* Generate relocs for the dynamic linker, except in
12851 the case of TLSLD where we'll use one entry per
12852 module. */
25f23106
AM
12853 asection *relgot;
12854 bfd_boolean ifunc;
e717da7e 12855
d881513a 12856 *offp = off | 1;
25f23106
AM
12857 relgot = NULL;
12858 ifunc = (h != NULL
12859 ? h->elf.type == STT_GNU_IFUNC
12860 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
4e795f50 12861 if ((info->shared || indx != 0)
927be08e
AM
12862 && (h == NULL
12863 || (tls_type == (TLS_TLS | TLS_LD)
12864 && !h->elf.def_dynamic)
039b3fef
AM
12865 || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
12866 || h->elf.root.type != bfd_link_hash_undefweak))
927be08e 12867 relgot = ppc64_elf_tdata (ent->owner)->relgot;
25f23106
AM
12868 else if (ifunc)
12869 relgot = htab->reliplt;
12870 if (relgot != NULL)
5bd4f169 12871 {
e717da7e
AM
12872 outrel.r_offset = (got->output_section->vma
12873 + got->output_offset
411e1bfb 12874 + off);
4cc603a5 12875 outrel.r_addend = addend;
d881513a 12876 if (tls_type & (TLS_LD | TLS_GD))
5bd4f169 12877 {
411e1bfb 12878 outrel.r_addend = 0;
e515b051 12879 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
d881513a
AM
12880 if (tls_type == (TLS_TLS | TLS_GD))
12881 {
e717da7e
AM
12882 loc = relgot->contents;
12883 loc += (relgot->reloc_count++
d881513a
AM
12884 * sizeof (Elf64_External_Rela));
12885 bfd_elf64_swap_reloca_out (output_bfd,
12886 &outrel, loc);
e515b051 12887 outrel.r_offset += 8;
4cc603a5 12888 outrel.r_addend = addend;
d881513a
AM
12889 outrel.r_info
12890 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
d881513a 12891 }
411e1bfb 12892 }
951fd09b 12893 else if (tls_type == (TLS_TLS | TLS_DTPREL))
411e1bfb 12894 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
951fd09b 12895 else if (tls_type == (TLS_TLS | TLS_TPREL))
411e1bfb 12896 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
25f23106
AM
12897 else if (indx != 0)
12898 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
12899 else
81407a69 12900 {
25f23106
AM
12901 if (ifunc)
12902 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
12903 else
12904 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69
AM
12905
12906 /* Write the .got section contents for the sake
12907 of prelink. */
e717da7e 12908 loc = got->contents + off;
23fbd6fa
JJ
12909 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
12910 loc);
81407a69 12911 }
81407a69
AM
12912
12913 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
e515b051
AM
12914 {
12915 outrel.r_addend += relocation;
12916 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
e1918d23 12917 outrel.r_addend -= htab->elf.tls_sec->vma;
e515b051 12918 }
e717da7e
AM
12919 loc = relgot->contents;
12920 loc += (relgot->reloc_count++
411e1bfb
AM
12921 * sizeof (Elf64_External_Rela));
12922 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
12923 }
12924
ad8e1ba5 12925 /* Init the .got section contents here if we're not
81407a69 12926 emitting a reloc. */
d881513a 12927 else
411e1bfb 12928 {
4cc603a5 12929 relocation += addend;
7b609f53
AM
12930 if (tls_type == (TLS_TLS | TLS_LD))
12931 relocation = 1;
12932 else if (tls_type != 0)
411e1bfb 12933 {
e1918d23 12934 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
7b609f53 12935 if (tls_type == (TLS_TLS | TLS_TPREL))
411e1bfb 12936 relocation += DTP_OFFSET - TP_OFFSET;
5bd4f169 12937
7b609f53
AM
12938 if (tls_type == (TLS_TLS | TLS_GD))
12939 {
12940 bfd_put_64 (output_bfd, relocation,
e717da7e 12941 got->contents + off + 8);
7b609f53
AM
12942 relocation = 1;
12943 }
411e1bfb 12944 }
7b609f53 12945
411e1bfb 12946 bfd_put_64 (output_bfd, relocation,
e717da7e 12947 got->contents + off);
5bd4f169
AM
12948 }
12949 }
12950
65f38f15
AM
12951 if (off >= (bfd_vma) -2)
12952 abort ();
12953
bf102f86
AM
12954 relocation = got->output_section->vma + got->output_offset + off;
12955 addend = -(TOCstart + htab->stub_group[input_section->id].toc_off);
5bd4f169 12956 }
65f38f15
AM
12957 break;
12958
12959 case R_PPC64_PLT16_HA:
12960 case R_PPC64_PLT16_HI:
12961 case R_PPC64_PLT16_LO:
12962 case R_PPC64_PLT32:
12963 case R_PPC64_PLT64:
12964 /* Relocation is to the entry for this symbol in the
12965 procedure linkage table. */
12966
12967 /* Resolve a PLT reloc against a local symbol directly,
12968 without using the procedure linkage table. */
12969 if (h == NULL)
12970 break;
12971
411e1bfb
AM
12972 /* It's possible that we didn't make a PLT entry for this
12973 symbol. This happens when statically linking PIC code,
12974 or when using -Bsymbolic. Go find a match if there is a
12975 PLT entry. */
4ce794b7 12976 if (htab->plt != NULL)
65f38f15 12977 {
411e1bfb 12978 struct plt_entry *ent;
039b3fef 12979 for (ent = h->elf.plt.plist; ent != NULL; ent = ent->next)
4cc603a5 12980 if (ent->addend == orig_addend
411e1bfb
AM
12981 && ent->plt.offset != (bfd_vma) -1)
12982 {
4ce794b7
AM
12983 relocation = (htab->plt->output_section->vma
12984 + htab->plt->output_offset
411e1bfb
AM
12985 + ent->plt.offset);
12986 unresolved_reloc = FALSE;
12987 }
65f38f15 12988 }
65f38f15 12989 break;
5bd4f169 12990
0b13192e
AM
12991 case R_PPC64_TOC:
12992 /* Relocation value is TOC base. */
12993 relocation = TOCstart;
cf35638d 12994 if (r_symndx == STN_UNDEF)
0b13192e 12995 relocation += htab->stub_group[input_section->id].toc_off;
8517fae7
AM
12996 else if (unresolved_reloc)
12997 ;
12998 else if (sec != NULL && sec->id <= htab->top_id)
0b13192e
AM
12999 relocation += htab->stub_group[sec->id].toc_off;
13000 else
13001 unresolved_reloc = TRUE;
ab96bf03 13002 goto dodyn;
0b13192e 13003
5bd4f169
AM
13004 /* TOC16 relocs. We want the offset relative to the TOC base,
13005 which is the address of the start of the TOC plus 0x8000.
13006 The TOC consists of sections .got, .toc, .tocbss, and .plt,
13007 in this order. */
65f38f15
AM
13008 case R_PPC64_TOC16:
13009 case R_PPC64_TOC16_LO:
13010 case R_PPC64_TOC16_HI:
13011 case R_PPC64_TOC16_DS:
13012 case R_PPC64_TOC16_LO_DS:
13013 case R_PPC64_TOC16_HA:
ad8e1ba5 13014 addend -= TOCstart + htab->stub_group[input_section->id].toc_off;
5bd4f169
AM
13015 break;
13016
13017 /* Relocate against the beginning of the section. */
65f38f15
AM
13018 case R_PPC64_SECTOFF:
13019 case R_PPC64_SECTOFF_LO:
13020 case R_PPC64_SECTOFF_HI:
13021 case R_PPC64_SECTOFF_DS:
13022 case R_PPC64_SECTOFF_LO_DS:
13023 case R_PPC64_SECTOFF_HA:
4ce794b7 13024 if (sec != NULL)
65f38f15 13025 addend -= sec->output_section->vma;
5bd4f169
AM
13026 break;
13027
25f23106
AM
13028 case R_PPC64_REL16:
13029 case R_PPC64_REL16_LO:
13030 case R_PPC64_REL16_HI:
13031 case R_PPC64_REL16_HA:
13032 break;
13033
721956f4
AM
13034 case R_PPC64_REL14:
13035 case R_PPC64_REL14_BRNTAKEN:
13036 case R_PPC64_REL14_BRTAKEN:
5d1634d7
AM
13037 case R_PPC64_REL24:
13038 break;
13039
411e1bfb
AM
13040 case R_PPC64_TPREL16:
13041 case R_PPC64_TPREL16_LO:
13042 case R_PPC64_TPREL16_HI:
13043 case R_PPC64_TPREL16_HA:
13044 case R_PPC64_TPREL16_DS:
13045 case R_PPC64_TPREL16_LO_DS:
13046 case R_PPC64_TPREL16_HIGHER:
13047 case R_PPC64_TPREL16_HIGHERA:
13048 case R_PPC64_TPREL16_HIGHEST:
13049 case R_PPC64_TPREL16_HIGHESTA:
766bc656
AM
13050 if (h != NULL
13051 && h->elf.root.type == bfd_link_hash_undefweak
13052 && h->elf.dynindx == -1)
13053 {
13054 /* Make this relocation against an undefined weak symbol
13055 resolve to zero. This is really just a tweak, since
13056 code using weak externs ought to check that they are
13057 defined before using them. */
13058 bfd_byte *p = contents + rel->r_offset - d_offset;
13059
13060 insn = bfd_get_32 (output_bfd, p);
13061 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
13062 if (insn != 0)
13063 bfd_put_32 (output_bfd, insn, p);
13064 break;
13065 }
e1918d23 13066 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
411e1bfb
AM
13067 if (info->shared)
13068 /* The TPREL16 relocs shouldn't really be used in shared
13069 libs as they will result in DT_TEXTREL being set, but
13070 support them anyway. */
13071 goto dodyn;
13072 break;
13073
13074 case R_PPC64_DTPREL16:
13075 case R_PPC64_DTPREL16_LO:
13076 case R_PPC64_DTPREL16_HI:
13077 case R_PPC64_DTPREL16_HA:
13078 case R_PPC64_DTPREL16_DS:
13079 case R_PPC64_DTPREL16_LO_DS:
13080 case R_PPC64_DTPREL16_HIGHER:
13081 case R_PPC64_DTPREL16_HIGHERA:
13082 case R_PPC64_DTPREL16_HIGHEST:
13083 case R_PPC64_DTPREL16_HIGHESTA:
e1918d23 13084 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
411e1bfb
AM
13085 break;
13086
e515b051
AM
13087 case R_PPC64_DTPMOD64:
13088 relocation = 1;
13089 addend = 0;
13090 goto dodyn;
13091
411e1bfb 13092 case R_PPC64_TPREL64:
e1918d23 13093 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
411e1bfb
AM
13094 goto dodyn;
13095
13096 case R_PPC64_DTPREL64:
e1918d23 13097 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
411e1bfb
AM
13098 /* Fall thru */
13099
65f38f15
AM
13100 /* Relocations that may need to be propagated if this is a
13101 dynamic object. */
04c9666a 13102 case R_PPC64_REL30:
65f38f15
AM
13103 case R_PPC64_REL32:
13104 case R_PPC64_REL64:
13105 case R_PPC64_ADDR14:
13106 case R_PPC64_ADDR14_BRNTAKEN:
13107 case R_PPC64_ADDR14_BRTAKEN:
13108 case R_PPC64_ADDR16:
13109 case R_PPC64_ADDR16_DS:
13110 case R_PPC64_ADDR16_HA:
13111 case R_PPC64_ADDR16_HI:
13112 case R_PPC64_ADDR16_HIGHER:
13113 case R_PPC64_ADDR16_HIGHERA:
13114 case R_PPC64_ADDR16_HIGHEST:
13115 case R_PPC64_ADDR16_HIGHESTA:
13116 case R_PPC64_ADDR16_LO:
13117 case R_PPC64_ADDR16_LO_DS:
13118 case R_PPC64_ADDR24:
65f38f15
AM
13119 case R_PPC64_ADDR32:
13120 case R_PPC64_ADDR64:
13121 case R_PPC64_UADDR16:
13122 case R_PPC64_UADDR32:
13123 case R_PPC64_UADDR64:
411e1bfb 13124 dodyn:
5d1634d7 13125 if ((input_section->flags & SEC_ALLOC) == 0)
ec338859
AM
13126 break;
13127
41bd81ab
AM
13128 if (NO_OPD_RELOCS && is_opd)
13129 break;
13130
65f38f15 13131 if ((info->shared
4e795f50 13132 && (h == NULL
039b3fef
AM
13133 || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
13134 || h->elf.root.type != bfd_link_hash_undefweak)
1d483afe 13135 && (must_be_dyn_reloc (info, r_type)
039b3fef 13136 || !SYMBOL_CALLS_LOCAL (info, &h->elf)))
f4656909
AM
13137 || (ELIMINATE_COPY_RELOCS
13138 && !info->shared
65f38f15 13139 && h != NULL
039b3fef 13140 && h->elf.dynindx != -1
f5385ebf 13141 && !h->elf.non_got_ref
25f23106
AM
13142 && !h->elf.def_regular)
13143 || (!info->shared
13144 && (h != NULL
13145 ? h->elf.type == STT_GNU_IFUNC
13146 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))
65f38f15 13147 {
b34976b6 13148 bfd_boolean skip, relocate;
65f38f15 13149 asection *sreloc;
1cf1f670 13150 bfd_vma out_off;
65f38f15
AM
13151
13152 /* When generating a dynamic object, these relocations
13153 are copied into the output file to be resolved at run
13154 time. */
13155
b34976b6
AM
13156 skip = FALSE;
13157 relocate = FALSE;
65f38f15 13158
1cf1f670
AM
13159 out_off = _bfd_elf_section_offset (output_bfd, info,
13160 input_section, rel->r_offset);
13161 if (out_off == (bfd_vma) -1)
b34976b6 13162 skip = TRUE;
1cf1f670 13163 else if (out_off == (bfd_vma) -2)
b34976b6 13164 skip = TRUE, relocate = TRUE;
1cf1f670
AM
13165 out_off += (input_section->output_section->vma
13166 + input_section->output_offset);
13167 outrel.r_offset = out_off;
411e1bfb 13168 outrel.r_addend = rel->r_addend;
65f38f15 13169
1cf1f670
AM
13170 /* Optimize unaligned reloc use. */
13171 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
13172 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
13173 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
13174 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
13175 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
13176 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
13177 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
13178 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
13179 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
13180
65f38f15 13181 if (skip)
0bb2d96a 13182 memset (&outrel, 0, sizeof outrel);
726d3ab0 13183 else if (!SYMBOL_CALLS_LOCAL (info, &h->elf)
0b13192e
AM
13184 && !is_opd
13185 && r_type != R_PPC64_TOC)
039b3fef 13186 outrel.r_info = ELF64_R_INFO (h->elf.dynindx, r_type);
65f38f15
AM
13187 else
13188 {
41bd81ab
AM
13189 /* This symbol is local, or marked to become local,
13190 or this is an opd section reloc which must point
13191 at a local function. */
65f38f15 13192 outrel.r_addend += relocation;
e86ce104 13193 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
65f38f15 13194 {
3fad3c7c 13195 if (is_opd && h != NULL)
afbe61cf
AM
13196 {
13197 /* Lie about opd entries. This case occurs
13198 when building shared libraries and we
13199 reference a function in another shared
3fad3c7c
AM
13200 lib. The same thing happens for a weak
13201 definition in an application that's
13202 overridden by a strong definition in a
13203 shared lib. (I believe this is a generic
13204 bug in binutils handling of weak syms.)
13205 In these cases we won't use the opd
1e2f5b6e 13206 entry in this lib. */
b34976b6 13207 unresolved_reloc = FALSE;
afbe61cf 13208 }
25f23106
AM
13209 if (!is_opd
13210 && r_type == R_PPC64_ADDR64
13211 && (h != NULL
13212 ? h->elf.type == STT_GNU_IFUNC
13213 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
13214 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13215 else
13216 {
13217 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69 13218
25f23106
AM
13219 /* We need to relocate .opd contents for ld.so.
13220 Prelink also wants simple and consistent rules
13221 for relocs. This make all RELATIVE relocs have
13222 *r_offset equal to r_addend. */
13223 relocate = TRUE;
13224 }
65f38f15
AM
13225 }
13226 else
13227 {
13228 long indx = 0;
13229
25f23106
AM
13230 if (h != NULL
13231 ? h->elf.type == STT_GNU_IFUNC
13232 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13233 {
25f53a85 13234 info->callbacks->einfo
8de848d8 13235 (_("%P: %H: relocation %s for indirect "
25f53a85
AM
13236 "function %s unsupported\n"),
13237 input_bfd, input_section, rel->r_offset,
25f23106
AM
13238 ppc64_elf_howto_table[r_type]->name,
13239 sym_name);
13240 ret = FALSE;
13241 }
cf35638d 13242 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
65f38f15
AM
13243 ;
13244 else if (sec == NULL || sec->owner == NULL)
13245 {
13246 bfd_set_error (bfd_error_bad_value);
b34976b6 13247 return FALSE;
65f38f15
AM
13248 }
13249 else
13250 {
13251 asection *osec;
13252
13253 osec = sec->output_section;
13254 indx = elf_section_data (osec)->dynindx;
13255
74541ad4
AM
13256 if (indx == 0)
13257 {
13258 if ((osec->flags & SEC_READONLY) == 0
13259 && htab->elf.data_index_section != NULL)
13260 osec = htab->elf.data_index_section;
13261 else
13262 osec = htab->elf.text_index_section;
13263 indx = elf_section_data (osec)->dynindx;
13264 }
13265 BFD_ASSERT (indx != 0);
13266
65f38f15
AM
13267 /* We are turning this relocation into one
13268 against a section symbol, so subtract out
13269 the output section's address but not the
13270 offset of the input section in the output
13271 section. */
13272 outrel.r_addend -= osec->vma;
13273 }
13274
13275 outrel.r_info = ELF64_R_INFO (indx, r_type);
13276 }
13277 }
13278
13279 sreloc = elf_section_data (input_section)->sreloc;
25f23106
AM
13280 if (!htab->elf.dynamic_sections_created)
13281 sreloc = htab->reliplt;
65f38f15
AM
13282 if (sreloc == NULL)
13283 abort ();
13284
dfbb6ac9
AM
13285 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
13286 >= sreloc->size)
13287 abort ();
947216bf
AM
13288 loc = sreloc->contents;
13289 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15
AM
13290 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
13291
13292 /* If this reloc is against an external symbol, it will
13293 be computed at runtime, so there's no need to do
81407a69
AM
13294 anything now. However, for the sake of prelink ensure
13295 that the section contents are a known value. */
65f38f15 13296 if (! relocate)
81407a69
AM
13297 {
13298 unresolved_reloc = FALSE;
13299 /* The value chosen here is quite arbitrary as ld.so
13300 ignores section contents except for the special
13301 case of .opd where the contents might be accessed
13302 before relocation. Choose zero, as that won't
13303 cause reloc overflow. */
13304 relocation = 0;
13305 addend = 0;
13306 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
13307 to improve backward compatibility with older
13308 versions of ld. */
13309 if (r_type == R_PPC64_ADDR64)
13310 addend = outrel.r_addend;
13311 /* Adjust pc_relative relocs to have zero in *r_offset. */
4ce794b7 13312 else if (ppc64_elf_howto_table[r_type]->pc_relative)
000732f7
AM
13313 addend = (input_section->output_section->vma
13314 + input_section->output_offset
13315 + rel->r_offset);
81407a69 13316 }
65f38f15 13317 }
5bd4f169
AM
13318 break;
13319
65f38f15
AM
13320 case R_PPC64_COPY:
13321 case R_PPC64_GLOB_DAT:
13322 case R_PPC64_JMP_SLOT:
25f23106 13323 case R_PPC64_JMP_IREL:
65f38f15
AM
13324 case R_PPC64_RELATIVE:
13325 /* We shouldn't ever see these dynamic relocs in relocatable
13326 files. */
ae9a127f 13327 /* Fall through. */
65f38f15
AM
13328
13329 case R_PPC64_PLTGOT16:
13330 case R_PPC64_PLTGOT16_DS:
13331 case R_PPC64_PLTGOT16_HA:
13332 case R_PPC64_PLTGOT16_HI:
13333 case R_PPC64_PLTGOT16_LO:
13334 case R_PPC64_PLTGOT16_LO_DS:
13335 case R_PPC64_PLTREL32:
13336 case R_PPC64_PLTREL64:
13337 /* These ones haven't been implemented yet. */
13338
25f53a85 13339 info->callbacks->einfo
8de848d8 13340 (_("%P: %B: relocation %s is not supported for symbol %s\n"),
d003868e 13341 input_bfd,
4ce794b7 13342 ppc64_elf_howto_table[r_type]->name, sym_name);
5bd4f169
AM
13343
13344 bfd_set_error (bfd_error_invalid_operation);
b34976b6 13345 ret = FALSE;
5bd4f169 13346 continue;
65f38f15 13347 }
5bd4f169 13348
67f0cbdb
AM
13349 /* Multi-instruction sequences that access the TOC can be
13350 optimized, eg. addis ra,r2,0; addi rb,ra,x;
13351 to nop; addi rb,r2,x; */
13352 switch (r_type)
13353 {
13354 default:
13355 break;
13356
13357 case R_PPC64_GOT_TLSLD16_HI:
13358 case R_PPC64_GOT_TLSGD16_HI:
13359 case R_PPC64_GOT_TPREL16_HI:
13360 case R_PPC64_GOT_DTPREL16_HI:
13361 case R_PPC64_GOT16_HI:
13362 case R_PPC64_TOC16_HI:
13363 /* These relocs would only be useful if building up an
13364 offset to later add to r2, perhaps in an indexed
13365 addressing mode instruction. Don't try to optimize.
13366 Unfortunately, the possibility of someone building up an
13367 offset like this or even with the HA relocs, means that
13368 we need to check the high insn when optimizing the low
13369 insn. */
13370 break;
13371
13372 case R_PPC64_GOT_TLSLD16_HA:
13373 case R_PPC64_GOT_TLSGD16_HA:
13374 case R_PPC64_GOT_TPREL16_HA:
13375 case R_PPC64_GOT_DTPREL16_HA:
13376 case R_PPC64_GOT16_HA:
13377 case R_PPC64_TOC16_HA:
98528052 13378 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
560c8763 13379 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
98528052
AM
13380 {
13381 bfd_byte *p = contents + (rel->r_offset & ~3);
13382 bfd_put_32 (input_bfd, NOP, p);
13383 }
67f0cbdb
AM
13384 break;
13385
13386 case R_PPC64_GOT_TLSLD16_LO:
13387 case R_PPC64_GOT_TLSGD16_LO:
13388 case R_PPC64_GOT_TPREL16_LO_DS:
13389 case R_PPC64_GOT_DTPREL16_LO_DS:
13390 case R_PPC64_GOT16_LO:
13391 case R_PPC64_GOT16_LO_DS:
13392 case R_PPC64_TOC16_LO:
13393 case R_PPC64_TOC16_LO_DS:
98528052 13394 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
560c8763 13395 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
67f0cbdb
AM
13396 {
13397 bfd_byte *p = contents + (rel->r_offset & ~3);
13398 insn = bfd_get_32 (input_bfd, p);
560c8763
AM
13399 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
13400 {
13401 /* Transform addic to addi when we change reg. */
13402 insn &= ~((0x3f << 26) | (0x1f << 16));
13403 insn |= (14u << 26) | (2 << 16);
13404 }
13405 else
67f0cbdb 13406 {
98528052
AM
13407 insn &= ~(0x1f << 16);
13408 insn |= 2 << 16;
67f0cbdb 13409 }
560c8763 13410 bfd_put_32 (input_bfd, insn, p);
67f0cbdb
AM
13411 }
13412 break;
13413 }
13414
65f38f15
AM
13415 /* Do any further special processing. */
13416 switch (r_type)
13417 {
13418 default:
13419 break;
13420
13421 case R_PPC64_ADDR16_HA:
25f23106 13422 case R_PPC64_REL16_HA:
65f38f15
AM
13423 case R_PPC64_ADDR16_HIGHERA:
13424 case R_PPC64_ADDR16_HIGHESTA:
65f38f15
AM
13425 case R_PPC64_TOC16_HA:
13426 case R_PPC64_SECTOFF_HA:
411e1bfb
AM
13427 case R_PPC64_TPREL16_HA:
13428 case R_PPC64_DTPREL16_HA:
411e1bfb
AM
13429 case R_PPC64_TPREL16_HIGHER:
13430 case R_PPC64_TPREL16_HIGHERA:
13431 case R_PPC64_TPREL16_HIGHEST:
13432 case R_PPC64_TPREL16_HIGHESTA:
13433 case R_PPC64_DTPREL16_HIGHER:
13434 case R_PPC64_DTPREL16_HIGHERA:
13435 case R_PPC64_DTPREL16_HIGHEST:
13436 case R_PPC64_DTPREL16_HIGHESTA:
65f38f15
AM
13437 /* It's just possible that this symbol is a weak symbol
13438 that's not actually defined anywhere. In that case,
13439 'sec' would be NULL, and we should leave the symbol
13440 alone (it will be set to zero elsewhere in the link). */
5c5f6e17
AM
13441 if (sec == NULL)
13442 break;
13443 /* Fall thru */
13444
13445 case R_PPC64_GOT16_HA:
13446 case R_PPC64_PLTGOT16_HA:
13447 case R_PPC64_PLT16_HA:
13448 case R_PPC64_GOT_TLSGD16_HA:
13449 case R_PPC64_GOT_TLSLD16_HA:
13450 case R_PPC64_GOT_TPREL16_HA:
13451 case R_PPC64_GOT_DTPREL16_HA:
13452 /* Add 0x10000 if sign bit in 0:15 is set.
13453 Bits 0:15 are not used. */
13454 addend += 0x8000;
65f38f15
AM
13455 break;
13456
13457 case R_PPC64_ADDR16_DS:
13458 case R_PPC64_ADDR16_LO_DS:
13459 case R_PPC64_GOT16_DS:
13460 case R_PPC64_GOT16_LO_DS:
13461 case R_PPC64_PLT16_LO_DS:
13462 case R_PPC64_SECTOFF_DS:
13463 case R_PPC64_SECTOFF_LO_DS:
13464 case R_PPC64_TOC16_DS:
13465 case R_PPC64_TOC16_LO_DS:
13466 case R_PPC64_PLTGOT16_DS:
13467 case R_PPC64_PLTGOT16_LO_DS:
411e1bfb
AM
13468 case R_PPC64_GOT_TPREL16_DS:
13469 case R_PPC64_GOT_TPREL16_LO_DS:
13470 case R_PPC64_GOT_DTPREL16_DS:
13471 case R_PPC64_GOT_DTPREL16_LO_DS:
13472 case R_PPC64_TPREL16_DS:
13473 case R_PPC64_TPREL16_LO_DS:
13474 case R_PPC64_DTPREL16_DS:
13475 case R_PPC64_DTPREL16_LO_DS:
adadcc0c
AM
13476 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
13477 mask = 3;
13478 /* If this reloc is against an lq insn, then the value must be
13479 a multiple of 16. This is somewhat of a hack, but the
13480 "correct" way to do this by defining _DQ forms of all the
13481 _DS relocs bloats all reloc switches in this file. It
13482 doesn't seem to make much sense to use any of these relocs
13483 in data, so testing the insn should be safe. */
494dac0c 13484 if ((insn & (0x3f << 26)) == (56u << 26))
adadcc0c
AM
13485 mask = 15;
13486 if (((relocation + addend) & mask) != 0)
65f38f15 13487 {
25f53a85 13488 info->callbacks->einfo
8de848d8 13489 (_("%P: %H: error: %s not a multiple of %u\n"),
25f53a85 13490 input_bfd, input_section, rel->r_offset,
4ce794b7 13491 ppc64_elf_howto_table[r_type]->name,
adadcc0c 13492 mask + 1);
65f38f15 13493 bfd_set_error (bfd_error_bad_value);
b34976b6 13494 ret = FALSE;
65f38f15
AM
13495 continue;
13496 }
13497 break;
5bd4f169
AM
13498 }
13499
239e1f3a
AM
13500 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
13501 because such sections are not SEC_ALLOC and thus ld.so will
13502 not process them. */
65f38f15 13503 if (unresolved_reloc
239e1f3a 13504 && !((input_section->flags & SEC_DEBUGGING) != 0
1d5316ab
AM
13505 && h->elf.def_dynamic)
13506 && _bfd_elf_section_offset (output_bfd, info, input_section,
13507 rel->r_offset) != (bfd_vma) -1)
9c07fe7c 13508 {
25f53a85 13509 info->callbacks->einfo
8de848d8 13510 (_("%P: %H: unresolvable %s relocation against symbol `%s'\n"),
25f53a85 13511 input_bfd, input_section, rel->r_offset,
7b609f53 13512 ppc64_elf_howto_table[(int) r_type]->name,
039b3fef 13513 h->elf.root.root.string);
b34976b6 13514 ret = FALSE;
9c07fe7c 13515 }
5bd4f169 13516
65f38f15 13517 r = _bfd_final_link_relocate (ppc64_elf_howto_table[(int) r_type],
5bd4f169
AM
13518 input_bfd,
13519 input_section,
13520 contents,
411e1bfb 13521 rel->r_offset,
5bd4f169
AM
13522 relocation,
13523 addend);
13524
ef60b7ff 13525 if (r != bfd_reloc_ok)
5bd4f169 13526 {
cd27b276
AM
13527 if (sym_name == NULL)
13528 sym_name = "(null)";
13529 if (r == bfd_reloc_overflow)
5bd4f169 13530 {
cd27b276
AM
13531 if (warned)
13532 continue;
13533 if (h != NULL
039b3fef 13534 && h->elf.root.type == bfd_link_hash_undefweak
4ce794b7 13535 && ppc64_elf_howto_table[r_type]->pc_relative)
5bd4f169
AM
13536 {
13537 /* Assume this is a call protected by other code that
13538 detects the symbol is undefined. If this is the case,
13539 we can safely ignore the overflow. If not, the
13540 program is hosed anyway, and a little warning isn't
13541 going to help. */
13542
13543 continue;
13544 }
13545
ef60b7ff 13546 if (!((*info->callbacks->reloc_overflow)
dfeffb9f
L
13547 (info, (h ? &h->elf.root : NULL), sym_name,
13548 ppc64_elf_howto_table[r_type]->name,
4cc603a5 13549 orig_addend, input_bfd, input_section, rel->r_offset)))
b34976b6 13550 return FALSE;
ef60b7ff
AM
13551 }
13552 else
13553 {
25f53a85 13554 info->callbacks->einfo
8de848d8 13555 (_("%P: %H: %s reloc against `%s': error %d\n"),
25f53a85 13556 input_bfd, input_section, rel->r_offset,
4ce794b7 13557 ppc64_elf_howto_table[r_type]->name,
7b609f53
AM
13558 sym_name,
13559 (int) r);
b34976b6 13560 ret = FALSE;
ef60b7ff 13561 }
5bd4f169 13562 }
5bd4f169
AM
13563 }
13564
645ea6a9
AM
13565 /* If we're emitting relocations, then shortly after this function
13566 returns, reloc offsets and addends for this section will be
13567 adjusted. Worse, reloc symbol indices will be for the output
8860955f
AM
13568 file rather than the input. Save a copy of the relocs for
13569 opd_entry_value. */
4cc603a5 13570 if (is_opd && (info->emitrelocations || info->relocatable))
8860955f
AM
13571 {
13572 bfd_size_type amt;
13573 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
13574 rel = bfd_alloc (input_bfd, amt);
13575 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd_relocs == NULL);
13576 ppc64_elf_tdata (input_bfd)->opd_relocs = rel;
13577 if (rel == NULL)
13578 return FALSE;
13579 memcpy (rel, relocs, amt);
13580 }
5bd4f169
AM
13581 return ret;
13582}
13583
754021d0
AM
13584/* Adjust the value of any local symbols in opd sections. */
13585
6e0b88f1 13586static int
754021d0
AM
13587ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
13588 const char *name ATTRIBUTE_UNUSED,
13589 Elf_Internal_Sym *elfsym,
13590 asection *input_sec,
13591 struct elf_link_hash_entry *h)
13592{
74f0fb50
AM
13593 struct _opd_sec_data *opd;
13594 long adjust;
754021d0
AM
13595 bfd_vma value;
13596
4025353c 13597 if (h != NULL)
6e0b88f1 13598 return 1;
4025353c 13599
74f0fb50
AM
13600 opd = get_opd_info (input_sec);
13601 if (opd == NULL || opd->adjust == NULL)
6e0b88f1 13602 return 1;
754021d0
AM
13603
13604 value = elfsym->st_value - input_sec->output_offset;
13605 if (!info->relocatable)
13606 value -= input_sec->output_section->vma;
13607
74f0fb50 13608 adjust = opd->adjust[value / 8];
4025353c 13609 if (adjust == -1)
6e0b88f1
AM
13610 return 2;
13611
13612 elfsym->st_value += adjust;
13613 return 1;
754021d0
AM
13614}
13615
5bd4f169
AM
13616/* Finish up dynamic symbol handling. We set the contents of various
13617 dynamic sections here. */
13618
b34976b6 13619static bfd_boolean
4ce794b7
AM
13620ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
13621 struct bfd_link_info *info,
13622 struct elf_link_hash_entry *h,
13623 Elf_Internal_Sym *sym)
5bd4f169 13624{
65f38f15 13625 struct ppc_link_hash_table *htab;
8387904d
AM
13626 struct plt_entry *ent;
13627 Elf_Internal_Rela rela;
13628 bfd_byte *loc;
5bd4f169 13629
65f38f15 13630 htab = ppc_hash_table (info);
4dfe6ac6
NC
13631 if (htab == NULL)
13632 return FALSE;
5bd4f169 13633
8387904d
AM
13634 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13635 if (ent->plt.offset != (bfd_vma) -1)
13636 {
13637 /* This symbol has an entry in the procedure linkage
13638 table. Set it up. */
e054468f
AM
13639 if (!htab->elf.dynamic_sections_created
13640 || h->dynindx == -1)
13641 {
13642 BFD_ASSERT (h->type == STT_GNU_IFUNC
13643 && h->def_regular
13644 && (h->root.type == bfd_link_hash_defined
13645 || h->root.type == bfd_link_hash_defweak));
25f23106
AM
13646 rela.r_offset = (htab->iplt->output_section->vma
13647 + htab->iplt->output_offset
13648 + ent->plt.offset);
13649 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
e054468f
AM
13650 rela.r_addend = (h->root.u.def.value
13651 + h->root.u.def.section->output_offset
13652 + h->root.u.def.section->output_section->vma
13653 + ent->addend);
25f23106
AM
13654 loc = (htab->reliplt->contents
13655 + (htab->reliplt->reloc_count++
13656 * sizeof (Elf64_External_Rela)));
e054468f
AM
13657 }
13658 else
13659 {
25f23106
AM
13660 rela.r_offset = (htab->plt->output_section->vma
13661 + htab->plt->output_offset
13662 + ent->plt.offset);
e054468f
AM
13663 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
13664 rela.r_addend = ent->addend;
25f23106
AM
13665 loc = (htab->relplt->contents
13666 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE)
13667 / (PLT_ENTRY_SIZE / sizeof (Elf64_External_Rela))));
e054468f 13668 }
8387904d
AM
13669 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
13670 }
5bd4f169 13671
f5385ebf 13672 if (h->needs_copy)
5bd4f169 13673 {
65f38f15 13674 /* This symbol needs a copy reloc. Set it up. */
5bd4f169 13675
65f38f15
AM
13676 if (h->dynindx == -1
13677 || (h->root.type != bfd_link_hash_defined
13678 && h->root.type != bfd_link_hash_defweak)
4ce794b7 13679 || htab->relbss == NULL)
65f38f15 13680 abort ();
5bd4f169
AM
13681
13682 rela.r_offset = (h->root.u.def.value
13683 + h->root.u.def.section->output_section->vma
13684 + h->root.u.def.section->output_offset);
13685 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
13686 rela.r_addend = 0;
4ce794b7
AM
13687 loc = htab->relbss->contents;
13688 loc += htab->relbss->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15 13689 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
5bd4f169
AM
13690 }
13691
5bd4f169
AM
13692 /* Mark some specially defined symbols as absolute. */
13693 if (strcmp (h->root.root.string, "_DYNAMIC") == 0)
13694 sym->st_shndx = SHN_ABS;
13695
b34976b6 13696 return TRUE;
5bd4f169
AM
13697}
13698
65f38f15
AM
13699/* Used to decide how to sort relocs in an optimal manner for the
13700 dynamic linker, before writing them out. */
13701
13702static enum elf_reloc_type_class
4ce794b7 13703ppc64_elf_reloc_type_class (const Elf_Internal_Rela *rela)
65f38f15 13704{
04c9666a 13705 enum elf_ppc64_reloc_type r_type;
a33d1f77 13706
4ce794b7 13707 r_type = ELF64_R_TYPE (rela->r_info);
a33d1f77 13708 switch (r_type)
65f38f15
AM
13709 {
13710 case R_PPC64_RELATIVE:
13711 return reloc_class_relative;
13712 case R_PPC64_JMP_SLOT:
13713 return reloc_class_plt;
13714 case R_PPC64_COPY:
13715 return reloc_class_copy;
13716 default:
13717 return reloc_class_normal;
13718 }
13719}
13720
5bd4f169
AM
13721/* Finish up the dynamic sections. */
13722
b34976b6 13723static bfd_boolean
4ce794b7
AM
13724ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
13725 struct bfd_link_info *info)
5bd4f169 13726{
65f38f15
AM
13727 struct ppc_link_hash_table *htab;
13728 bfd *dynobj;
5bd4f169 13729 asection *sdyn;
5bd4f169 13730
65f38f15 13731 htab = ppc_hash_table (info);
4dfe6ac6
NC
13732 if (htab == NULL)
13733 return FALSE;
13734
65f38f15 13735 dynobj = htab->elf.dynobj;
5bd4f169
AM
13736 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
13737
65f38f15 13738 if (htab->elf.dynamic_sections_created)
5bd4f169 13739 {
5bd4f169
AM
13740 Elf64_External_Dyn *dyncon, *dynconend;
13741
4ce794b7 13742 if (sdyn == NULL || htab->got == NULL)
65f38f15 13743 abort ();
5bd4f169
AM
13744
13745 dyncon = (Elf64_External_Dyn *) sdyn->contents;
eea6121a 13746 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
5bd4f169
AM
13747 for (; dyncon < dynconend; dyncon++)
13748 {
13749 Elf_Internal_Dyn dyn;
19397422 13750 asection *s;
5bd4f169
AM
13751
13752 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
13753
13754 switch (dyn.d_tag)
13755 {
65f38f15
AM
13756 default:
13757 continue;
5bd4f169 13758
5d1634d7 13759 case DT_PPC64_GLINK:
4ce794b7 13760 s = htab->glink;
6348e046 13761 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
ad8e1ba5
AM
13762 /* We stupidly defined DT_PPC64_GLINK to be the start
13763 of glink rather than the first entry point, which is
13764 what ld.so needs, and now have a bigger stub to
13765 support automatic multiple TOCs. */
13766 dyn.d_un.d_ptr += GLINK_CALL_STUB_SIZE - 32;
5d1634d7
AM
13767 break;
13768
19397422
AM
13769 case DT_PPC64_OPD:
13770 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
13771 if (s == NULL)
13772 continue;
13773 dyn.d_un.d_ptr = s->vma;
19397422
AM
13774 break;
13775
13776 case DT_PPC64_OPDSZ:
13777 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
13778 if (s == NULL)
13779 continue;
eea6121a 13780 dyn.d_un.d_val = s->size;
19397422
AM
13781 break;
13782
65f38f15 13783 case DT_PLTGOT:
4ce794b7 13784 s = htab->plt;
6348e046 13785 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15
AM
13786 break;
13787
13788 case DT_JMPREL:
4ce794b7 13789 s = htab->relplt;
6348e046 13790 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15 13791 break;
5bd4f169 13792
65f38f15 13793 case DT_PLTRELSZ:
eea6121a 13794 dyn.d_un.d_val = htab->relplt->size;
5d1634d7
AM
13795 break;
13796
13797 case DT_RELASZ:
13798 /* Don't count procedure linkage table relocs in the
13799 overall reloc count. */
4ce794b7 13800 s = htab->relplt;
6348e046
AM
13801 if (s == NULL)
13802 continue;
eea6121a 13803 dyn.d_un.d_val -= s->size;
6348e046
AM
13804 break;
13805
13806 case DT_RELA:
13807 /* We may not be using the standard ELF linker script.
13808 If .rela.plt is the first .rela section, we adjust
13809 DT_RELA to not include it. */
4ce794b7 13810 s = htab->relplt;
6348e046
AM
13811 if (s == NULL)
13812 continue;
13813 if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
13814 continue;
eea6121a 13815 dyn.d_un.d_ptr += s->size;
65f38f15 13816 break;
5bd4f169 13817 }
5bd4f169 13818
65f38f15 13819 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
5bd4f169 13820 }
5bd4f169
AM
13821 }
13822
eea6121a 13823 if (htab->got != NULL && htab->got->size != 0)
5d1634d7
AM
13824 {
13825 /* Fill in the first entry in the global offset table.
13826 We use it to hold the link-time TOCbase. */
13827 bfd_put_64 (output_bfd,
60ee0d4a 13828 elf_gp (output_bfd) + TOC_BASE_OFF,
4ce794b7 13829 htab->got->contents);
5d1634d7
AM
13830
13831 /* Set .got entry size. */
4ce794b7 13832 elf_section_data (htab->got->output_section)->this_hdr.sh_entsize = 8;
5d1634d7
AM
13833 }
13834
eea6121a 13835 if (htab->plt != NULL && htab->plt->size != 0)
5d1634d7
AM
13836 {
13837 /* Set .plt entry size. */
4ce794b7 13838 elf_section_data (htab->plt->output_section)->this_hdr.sh_entsize
5d1634d7
AM
13839 = PLT_ENTRY_SIZE;
13840 }
13841
84f5d08e
AM
13842 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
13843 brlt ourselves if emitrelocations. */
13844 if (htab->brlt != NULL
13845 && htab->brlt->reloc_count != 0
13846 && !_bfd_elf_link_output_relocs (output_bfd,
13847 htab->brlt,
d4730f92 13848 elf_section_data (htab->brlt)->rela.hdr,
84f5d08e
AM
13849 elf_section_data (htab->brlt)->relocs,
13850 NULL))
13851 return FALSE;
13852
176a0d42
AM
13853 if (htab->glink != NULL
13854 && htab->glink->reloc_count != 0
13855 && !_bfd_elf_link_output_relocs (output_bfd,
13856 htab->glink,
d4730f92 13857 elf_section_data (htab->glink)->rela.hdr,
176a0d42
AM
13858 elf_section_data (htab->glink)->relocs,
13859 NULL))
13860 return FALSE;
13861
58d180e8
AM
13862
13863 if (htab->glink_eh_frame != NULL
13864 && htab->glink_eh_frame->sec_info_type == ELF_INFO_TYPE_EH_FRAME
13865 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
13866 htab->glink_eh_frame,
13867 htab->glink_eh_frame->contents))
13868 return FALSE;
13869
e717da7e 13870 /* We need to handle writing out multiple GOT sections ourselves,
7b53ace3
AM
13871 since we didn't add them to DYNOBJ. We know dynobj is the first
13872 bfd. */
e717da7e
AM
13873 while ((dynobj = dynobj->link_next) != NULL)
13874 {
13875 asection *s;
7b53ace3 13876
0c8d6e5c 13877 if (!is_ppc64_elf (dynobj))
7b53ace3
AM
13878 continue;
13879
e717da7e
AM
13880 s = ppc64_elf_tdata (dynobj)->got;
13881 if (s != NULL
eea6121a 13882 && s->size != 0
e717da7e
AM
13883 && s->output_section != bfd_abs_section_ptr
13884 && !bfd_set_section_contents (output_bfd, s->output_section,
13885 s->contents, s->output_offset,
eea6121a 13886 s->size))
e717da7e
AM
13887 return FALSE;
13888 s = ppc64_elf_tdata (dynobj)->relgot;
13889 if (s != NULL
eea6121a 13890 && s->size != 0
e717da7e
AM
13891 && s->output_section != bfd_abs_section_ptr
13892 && !bfd_set_section_contents (output_bfd, s->output_section,
13893 s->contents, s->output_offset,
eea6121a 13894 s->size))
e717da7e
AM
13895 return FALSE;
13896 }
f6c52c13 13897
b34976b6 13898 return TRUE;
5bd4f169
AM
13899}
13900
5bd4f169 13901#include "elf64-target.h"
7b8e7dad
AM
13902
13903/* FreeBSD support */
13904
13905#undef TARGET_LITTLE_SYM
13906#undef TARGET_LITTLE_NAME
13907
13908#undef TARGET_BIG_SYM
13909#define TARGET_BIG_SYM bfd_elf64_powerpc_freebsd_vec
13910#undef TARGET_BIG_NAME
13911#define TARGET_BIG_NAME "elf64-powerpc-freebsd"
13912
13913#undef ELF_OSABI
13914#define ELF_OSABI ELFOSABI_FREEBSD
13915
13916#undef elf64_bed
13917#define elf64_bed elf64_powerpc_fbsd_bed
13918
13919#include "elf64-target.h"
13920
This page took 1.964948 seconds and 4 git commands to generate.