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