*** empty log message ***
[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
2629 return 0;
2630}
2631
699733f6 2632/* Search SYMS for a symbol of the given VALUE. */
90e3cdf2 2633
699733f6
AM
2634static asymbol *
2635sym_exists_at (asymbol **syms, long lo, long hi, int id, bfd_vma value)
90e3cdf2 2636{
699733f6 2637 long mid;
90e3cdf2 2638
699733f6
AM
2639 if (id == -1)
2640 {
2641 while (lo < hi)
2642 {
2643 mid = (lo + hi) >> 1;
2644 if (syms[mid]->value + syms[mid]->section->vma < value)
2645 lo = mid + 1;
2646 else if (syms[mid]->value + syms[mid]->section->vma > value)
2647 hi = mid;
2648 else
2649 return syms[mid];
2650 }
2651 }
2652 else
2653 {
2654 while (lo < hi)
2655 {
2656 mid = (lo + hi) >> 1;
2657 if (syms[mid]->section->id < id)
2658 lo = mid + 1;
2659 else if (syms[mid]->section->id > id)
2660 hi = mid;
2661 else if (syms[mid]->value < value)
2662 lo = mid + 1;
2663 else if (syms[mid]->value > value)
2664 hi = mid;
2665 else
2666 return syms[mid];
2667 }
2668 }
2669 return NULL;
90e3cdf2
JJ
2670}
2671
699733f6
AM
2672/* Create synthetic symbols, effectively restoring "dot-symbol" function
2673 entry syms. */
90e3cdf2
JJ
2674
2675static long
a7535cf3
AM
2676ppc64_elf_get_synthetic_symtab (bfd *abfd,
2677 long static_count, asymbol **static_syms,
2678 long dyn_count, asymbol **dyn_syms,
c9727e01 2679 asymbol **ret)
90e3cdf2
JJ
2680{
2681 asymbol *s;
699733f6
AM
2682 long i;
2683 long count;
90e3cdf2 2684 char *names;
a7535cf3 2685 long symcount, codesecsym, codesecsymend, secsymend, opdsymend;
699733f6 2686 asection *opd;
90e3cdf2 2687 bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
a7535cf3 2688 asymbol **syms;
90e3cdf2
JJ
2689
2690 *ret = NULL;
2691
2692 opd = bfd_get_section_by_name (abfd, ".opd");
2693 if (opd == NULL)
2694 return 0;
2695
a7535cf3 2696 symcount = static_count;
c9727e01 2697 if (!relocatable)
a7535cf3 2698 symcount += dyn_count;
90e3cdf2 2699 if (symcount == 0)
c9727e01 2700 return 0;
90e3cdf2 2701
a7535cf3
AM
2702 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
2703 if (syms == NULL)
7356fed5 2704 return -1;
a7535cf3
AM
2705
2706 if (!relocatable && static_count != 0 && dyn_count != 0)
2707 {
2708 /* Use both symbol tables. */
2709 memcpy (syms, static_syms, static_count * sizeof (*syms));
2710 memcpy (syms + static_count, dyn_syms, (dyn_count + 1) * sizeof (*syms));
2711 }
2712 else if (!relocatable && static_count == 0)
2713 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
2714 else
2715 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
2716
90e3cdf2
JJ
2717 synthetic_opd = opd;
2718 synthetic_relocatable = relocatable;
595da8c5 2719 qsort (syms, symcount, sizeof (*syms), compare_symbols);
90e3cdf2 2720
c9727e01
AM
2721 if (!relocatable && symcount > 1)
2722 {
2723 long j;
2724 /* Trim duplicate syms, since we may have merged the normal and
2725 dynamic symbols. Actually, we only care about syms that have
3b36f7e6 2726 different values, so trim any with the same value. */
c9727e01
AM
2727 for (i = 1, j = 1; i < symcount; ++i)
2728 if (syms[i - 1]->value + syms[i - 1]->section->vma
2729 != syms[i]->value + syms[i]->section->vma)
2730 syms[j++] = syms[i];
2731 symcount = j;
2732 }
2733
699733f6
AM
2734 i = 0;
2735 if (syms[i]->section == opd)
2736 ++i;
2737 codesecsym = i;
90e3cdf2 2738
699733f6
AM
2739 for (; i < symcount; ++i)
2740 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2741 != (SEC_CODE | SEC_ALLOC))
2742 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
2743 break;
2744 codesecsymend = i;
90e3cdf2 2745
699733f6
AM
2746 for (; i < symcount; ++i)
2747 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
2748 break;
2749 secsymend = i;
90e3cdf2 2750
699733f6
AM
2751 for (; i < symcount; ++i)
2752 if (syms[i]->section != opd)
2753 break;
2754 opdsymend = i;
90e3cdf2 2755
699733f6
AM
2756 for (; i < symcount; ++i)
2757 if ((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2758 != (SEC_CODE | SEC_ALLOC))
2759 break;
2760 symcount = i;
2761
c9727e01 2762 count = 0;
699733f6 2763 if (opdsymend == secsymend)
c9727e01 2764 goto done;
90e3cdf2 2765
699733f6 2766 if (relocatable)
90e3cdf2 2767 {
699733f6
AM
2768 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
2769 arelent *r;
2770 size_t size;
2771 long relcount;
90e3cdf2 2772
699733f6 2773 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
90e3cdf2 2774 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
7356fed5 2775 if (relcount == 0)
c9727e01 2776 goto done;
90e3cdf2 2777
7356fed5
AM
2778 if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
2779 {
2780 count = -1;
2781 goto done;
2782 }
2783
699733f6 2784 size = 0;
595da8c5 2785 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
699733f6
AM
2786 {
2787 asymbol *sym;
90e3cdf2 2788
595da8c5 2789 while (r < opd->relocation + relcount
699733f6
AM
2790 && r->address < syms[i]->value + opd->vma)
2791 ++r;
90e3cdf2 2792
595da8c5 2793 if (r == opd->relocation + relcount)
699733f6 2794 break;
90e3cdf2 2795
699733f6
AM
2796 if (r->address != syms[i]->value + opd->vma)
2797 continue;
90e3cdf2 2798
699733f6
AM
2799 if (r->howto->type != R_PPC64_ADDR64)
2800 continue;
90e3cdf2 2801
699733f6
AM
2802 sym = *r->sym_ptr_ptr;
2803 if (!sym_exists_at (syms, opdsymend, symcount,
2804 sym->section->id, sym->value + r->addend))
2805 {
2806 ++count;
2807 size += sizeof (asymbol);
2808 size += strlen (syms[i]->name) + 2;
2809 }
2810 }
90e3cdf2 2811
699733f6
AM
2812 s = *ret = bfd_malloc (size);
2813 if (s == NULL)
2814 {
7356fed5 2815 count = -1;
c9727e01 2816 goto done;
699733f6 2817 }
90e3cdf2 2818
699733f6 2819 names = (char *) (s + count);
90e3cdf2 2820
595da8c5 2821 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
90e3cdf2 2822 {
699733f6 2823 asymbol *sym;
90e3cdf2 2824
595da8c5 2825 while (r < opd->relocation + relcount
699733f6
AM
2826 && r->address < syms[i]->value + opd->vma)
2827 ++r;
90e3cdf2 2828
595da8c5 2829 if (r == opd->relocation + relcount)
699733f6
AM
2830 break;
2831
2832 if (r->address != syms[i]->value + opd->vma)
2833 continue;
2834
2835 if (r->howto->type != R_PPC64_ADDR64)
2836 continue;
90e3cdf2 2837
699733f6
AM
2838 sym = *r->sym_ptr_ptr;
2839 if (!sym_exists_at (syms, opdsymend, symcount,
2840 sym->section->id, sym->value + r->addend))
2841 {
2842 size_t len;
2843
2844 *s = *syms[i];
2845 s->section = sym->section;
2846 s->value = sym->value + r->addend;
2847 s->name = names;
2848 *names++ = '.';
2849 len = strlen (syms[i]->name);
2850 memcpy (names, syms[i]->name, len + 1);
2851 names += len + 1;
2852 s++;
2853 }
2854 }
2855 }
2856 else
90e3cdf2 2857 {
699733f6
AM
2858 bfd_byte *contents;
2859 size_t size;
90e3cdf2 2860
699733f6
AM
2861 if (!bfd_malloc_and_get_section (abfd, opd, &contents))
2862 {
2863 if (contents)
c9727e01
AM
2864 {
2865 free_contents_and_exit:
2866 free (contents);
2867 }
7356fed5 2868 count = -1;
c9727e01 2869 goto done;
699733f6 2870 }
90e3cdf2 2871
699733f6
AM
2872 size = 0;
2873 for (i = secsymend; i < opdsymend; ++i)
2874 {
2875 bfd_vma ent;
90e3cdf2 2876
699733f6
AM
2877 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2878 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2879 {
2880 ++count;
2881 size += sizeof (asymbol);
2882 size += strlen (syms[i]->name) + 2;
2883 }
2884 }
90e3cdf2 2885
699733f6
AM
2886 s = *ret = bfd_malloc (size);
2887 if (s == NULL)
7356fed5 2888 goto free_contents_and_exit;
90e3cdf2 2889
699733f6 2890 names = (char *) (s + count);
90e3cdf2 2891
699733f6 2892 for (i = secsymend; i < opdsymend; ++i)
90e3cdf2 2893 {
699733f6 2894 bfd_vma ent;
90e3cdf2 2895
699733f6
AM
2896 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2897 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
90e3cdf2 2898 {
c9727e01 2899 long lo, hi;
699733f6 2900 size_t len;
c9727e01 2901 asection *sec = abfd->sections;
90e3cdf2 2902
699733f6
AM
2903 *s = *syms[i];
2904 lo = codesecsym;
2905 hi = codesecsymend;
2906 while (lo < hi)
2907 {
c9727e01 2908 long mid = (lo + hi) >> 1;
699733f6
AM
2909 if (syms[mid]->section->vma < ent)
2910 lo = mid + 1;
2911 else if (syms[mid]->section->vma > ent)
2912 hi = mid;
2913 else
c9727e01
AM
2914 {
2915 sec = syms[mid]->section;
2916 break;
2917 }
699733f6
AM
2918 }
2919
c9727e01 2920 if (lo >= hi && lo > codesecsym)
699733f6 2921 sec = syms[lo - 1]->section;
699733f6
AM
2922
2923 for (; sec != NULL; sec = sec->next)
2924 {
2925 if (sec->vma > ent)
2926 break;
2927 if ((sec->flags & SEC_ALLOC) == 0
2928 || (sec->flags & SEC_LOAD) == 0)
2929 break;
2930 if ((sec->flags & SEC_CODE) != 0)
2931 s->section = sec;
2932 }
2933 s->value = ent - s->section->vma;
2934 s->name = names;
2935 *names++ = '.';
2936 len = strlen (syms[i]->name);
2937 memcpy (names, syms[i]->name, len + 1);
2938 names += len + 1;
2939 s++;
90e3cdf2 2940 }
90e3cdf2 2941 }
699733f6 2942 free (contents);
90e3cdf2
JJ
2943 }
2944
c9727e01 2945 done:
a7535cf3 2946 free (syms);
90e3cdf2
JJ
2947 return count;
2948}
5bd4f169 2949\f
65f38f15
AM
2950/* The following functions are specific to the ELF linker, while
2951 functions above are used generally. Those named ppc64_elf_* are
2952 called by the main ELF linker code. They appear in this file more
2953 or less in the order in which they are called. eg.
2954 ppc64_elf_check_relocs is called early in the link process,
2955 ppc64_elf_finish_dynamic_sections is one of the last functions
e86ce104
AM
2956 called.
2957
2958 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
2959 functions have both a function code symbol and a function descriptor
2960 symbol. A call to foo in a relocatable object file looks like:
2961
2962 . .text
2963 . x:
2964 . bl .foo
2965 . nop
2966
2967 The function definition in another object file might be:
2968
2969 . .section .opd
2970 . foo: .quad .foo
2971 . .quad .TOC.@tocbase
2972 . .quad 0
2973 .
2974 . .text
2975 . .foo: blr
2976
2977 When the linker resolves the call during a static link, the branch
2978 unsurprisingly just goes to .foo and the .opd information is unused.
2979 If the function definition is in a shared library, things are a little
2980 different: The call goes via a plt call stub, the opd information gets
2981 copied to the plt, and the linker patches the nop.
2982
2983 . x:
2984 . bl .foo_stub
2985 . ld 2,40(1)
2986 .
2987 .
2988 . .foo_stub:
2989 . addis 12,2,Lfoo@toc@ha # in practice, the call stub
411e1bfb 2990 . addi 12,12,Lfoo@toc@l # is slightly optimized, but
e86ce104
AM
2991 . std 2,40(1) # this is the general idea
2992 . ld 11,0(12)
2993 . ld 2,8(12)
2994 . mtctr 11
2995 . ld 11,16(12)
2996 . bctr
2997 .
2998 . .section .plt
2999 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
3000
3001 The "reloc ()" notation is supposed to indicate that the linker emits
3002 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
3003 copying.
3004
3005 What are the difficulties here? Well, firstly, the relocations
3006 examined by the linker in check_relocs are against the function code
3007 sym .foo, while the dynamic relocation in the plt is emitted against
3008 the function descriptor symbol, foo. Somewhere along the line, we need
3009 to carefully copy dynamic link information from one symbol to the other.
3010 Secondly, the generic part of the elf linker will make .foo a dynamic
3011 symbol as is normal for most other backends. We need foo dynamic
3012 instead, at least for an application final link. However, when
3013 creating a shared library containing foo, we need to have both symbols
3014 dynamic so that references to .foo are satisfied during the early
3015 stages of linking. Otherwise the linker might decide to pull in a
8387904d
AM
3016 definition from some other object, eg. a static library.
3017
3018 Update: As of August 2004, we support a new convention. Function
3019 calls may use the function descriptor symbol, ie. "bl foo". This
3020 behaves exactly as "bl .foo". */
65f38f15
AM
3021
3022/* The linker needs to keep track of the number of relocs that it
3023 decides to copy as dynamic relocs in check_relocs for each symbol.
3024 This is so that it can later discard them if they are found to be
3025 unnecessary. We store the information in a field extending the
3026 regular ELF linker hash table. */
3027
3028struct ppc_dyn_relocs
3029{
3030 struct ppc_dyn_relocs *next;
3031
3032 /* The input section of the reloc. */
3033 asection *sec;
3034
3035 /* Total number of relocs copied for the input section. */
3036 bfd_size_type count;
3037
3038 /* Number of pc-relative relocs copied for the input section. */
3039 bfd_size_type pc_count;
3040};
3041
411e1bfb
AM
3042/* Track GOT entries needed for a given symbol. We might need more
3043 than one got entry per symbol. */
3044struct got_entry
3045{
3046 struct got_entry *next;
3047
e7b938ca 3048 /* The symbol addend that we'll be placing in the GOT. */
411e1bfb
AM
3049 bfd_vma addend;
3050
e717da7e
AM
3051 /* Unlike other ELF targets, we use separate GOT entries for the same
3052 symbol referenced from different input files. This is to support
3053 automatic multiple TOC/GOT sections, where the TOC base can vary
3054 from one input file to another.
3055
3056 Point to the BFD owning this GOT entry. */
3057 bfd *owner;
3058
3059 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
3060 TLS_TPREL or TLS_DTPREL for tls entries. */
3061 char tls_type;
3062
e7b938ca 3063 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
411e1bfb
AM
3064 union
3065 {
3066 bfd_signed_vma refcount;
3067 bfd_vma offset;
3068 } got;
411e1bfb
AM
3069};
3070
3071/* The same for PLT. */
3072struct plt_entry
3073{
3074 struct plt_entry *next;
3075
3076 bfd_vma addend;
3077
3078 union
3079 {
3080 bfd_signed_vma refcount;
3081 bfd_vma offset;
3082 } plt;
3083};
3084
65f38f15 3085/* Of those relocs that might be copied as dynamic relocs, this macro
58ac9f71
AM
3086 selects those that must be copied when linking a shared library,
3087 even when the symbol is local. */
65f38f15 3088
411e1bfb 3089#define MUST_BE_DYN_RELOC(RTYPE) \
805fc799
AM
3090 ((RTYPE) != R_PPC64_REL32 \
3091 && (RTYPE) != R_PPC64_REL64 \
04c9666a 3092 && (RTYPE) != R_PPC64_REL30)
65f38f15 3093
f4656909
AM
3094/* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
3095 copying dynamic variables from a shared lib into an app's dynbss
3096 section, and instead use a dynamic relocation to point into the
5d35169e
AM
3097 shared lib. With code that gcc generates, it's vital that this be
3098 enabled; In the PowerPC64 ABI, the address of a function is actually
3099 the address of a function descriptor, which resides in the .opd
3100 section. gcc uses the descriptor directly rather than going via the
3101 GOT as some other ABI's do, which means that initialized function
3102 pointers must reference the descriptor. Thus, a function pointer
3103 initialized to the address of a function in a shared library will
3104 either require a copy reloc, or a dynamic reloc. Using a copy reloc
4cc11e76 3105 redefines the function descriptor symbol to point to the copy. This
5d35169e
AM
3106 presents a problem as a plt entry for that function is also
3107 initialized from the function descriptor symbol and the copy reloc
3108 may not be initialized first. */
a23b6845 3109#define ELIMINATE_COPY_RELOCS 1
f4656909 3110
721956f4
AM
3111/* Section name for stubs is the associated section name plus this
3112 string. */
3113#define STUB_SUFFIX ".stub"
3114
3115/* Linker stubs.
3116 ppc_stub_long_branch:
3117 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
3118 destination, but a 24 bit branch in a stub section will reach.
3119 . b dest
3120
3121 ppc_stub_plt_branch:
3122 Similar to the above, but a 24 bit branch in the stub section won't
3123 reach its destination.
87e226ce
AM
3124 . addis %r12,%r2,xxx@toc@ha
3125 . ld %r11,xxx@toc@l(%r12)
721956f4
AM
3126 . mtctr %r11
3127 . bctr
3128
3129 ppc_stub_plt_call:
2c66dc6c
AM
3130 Used to call a function in a shared library. If it so happens that
3131 the plt entry referenced crosses a 64k boundary, then an extra
3132 "addis %r12,%r12,1" will be inserted before the load at xxx+8 or
3133 xxx+16 as appropriate.
87e226ce 3134 . addis %r12,%r2,xxx@toc@ha
721956f4 3135 . std %r2,40(%r1)
87e226ce
AM
3136 . ld %r11,xxx+0@toc@l(%r12)
3137 . ld %r2,xxx+8@toc@l(%r12)
721956f4 3138 . mtctr %r11
87e226ce 3139 . ld %r11,xxx+16@toc@l(%r12)
721956f4 3140 . bctr
ad8e1ba5
AM
3141
3142 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
3143 code to adjust the value and save r2 to support multiple toc sections.
3144 A ppc_stub_long_branch with an r2 offset looks like:
3145 . std %r2,40(%r1)
3146 . addis %r2,%r2,off@ha
3147 . addi %r2,%r2,off@l
3148 . b dest
3149
3150 A ppc_stub_plt_branch with an r2 offset looks like:
3151 . std %r2,40(%r1)
3152 . addis %r12,%r2,xxx@toc@ha
3153 . ld %r11,xxx@toc@l(%r12)
3154 . addis %r2,%r2,off@ha
3155 . addi %r2,%r2,off@l
3156 . mtctr %r11
3157 . bctr
721956f4
AM
3158*/
3159
3160enum ppc_stub_type {
3161 ppc_stub_none,
3162 ppc_stub_long_branch,
ad8e1ba5 3163 ppc_stub_long_branch_r2off,
721956f4 3164 ppc_stub_plt_branch,
ad8e1ba5 3165 ppc_stub_plt_branch_r2off,
721956f4
AM
3166 ppc_stub_plt_call
3167};
3168
3169struct ppc_stub_hash_entry {
3170
3171 /* Base hash table entry structure. */
3172 struct bfd_hash_entry root;
3173
ad8e1ba5
AM
3174 enum ppc_stub_type stub_type;
3175
721956f4
AM
3176 /* The stub section. */
3177 asection *stub_sec;
3178
3179 /* Offset within stub_sec of the beginning of this stub. */
3180 bfd_vma stub_offset;
3181
3182 /* Given the symbol's value and its section we can determine its final
3183 value when building the stubs (so the stub knows where to jump. */
3184 bfd_vma target_value;
3185 asection *target_section;
3186
721956f4
AM
3187 /* The symbol table entry, if any, that this was derived from. */
3188 struct ppc_link_hash_entry *h;
3189
411e1bfb
AM
3190 /* And the reloc addend that this was derived from. */
3191 bfd_vma addend;
3192
721956f4
AM
3193 /* Where this stub is being called from, or, in the case of combined
3194 stub sections, the first input section in the group. */
3195 asection *id_sec;
3196};
3197
3198struct ppc_branch_hash_entry {
3199
3200 /* Base hash table entry structure. */
3201 struct bfd_hash_entry root;
3202
c456f082 3203 /* Offset within branch lookup table. */
721956f4
AM
3204 unsigned int offset;
3205
3206 /* Generation marker. */
3207 unsigned int iter;
3208};
65f38f15
AM
3209
3210struct ppc_link_hash_entry
3211{
3212 struct elf_link_hash_entry elf;
3213
721956f4
AM
3214 /* A pointer to the most recently used stub hash entry against this
3215 symbol. */
3216 struct ppc_stub_hash_entry *stub_cache;
3217
65f38f15
AM
3218 /* Track dynamic relocs copied for this symbol. */
3219 struct ppc_dyn_relocs *dyn_relocs;
e86ce104 3220
721956f4 3221 /* Link between function code and descriptor symbols. */
34814b9f 3222 struct ppc_link_hash_entry *oh;
721956f4 3223
e86ce104
AM
3224 /* Flag function code and descriptor symbols. */
3225 unsigned int is_func:1;
3226 unsigned int is_func_descriptor:1;
908b32fc 3227 unsigned int fake:1;
411e1bfb 3228
c5614fa4
AM
3229 /* Whether global opd/toc sym has been adjusted or not.
3230 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3231 should be set for all globals defined in any opd/toc section. */
754021d0
AM
3232 unsigned int adjust_done:1;
3233
99877b66
AM
3234 /* Set if we twiddled this symbol to weak at some stage. */
3235 unsigned int was_undefined:1;
3236
411e1bfb 3237 /* Contexts in which symbol is used in the GOT (or TOC).
e7b938ca
AM
3238 TLS_GD .. TLS_EXPLICIT bits are or'd into the mask as the
3239 corresponding relocs are encountered during check_relocs.
3240 tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
3241 indicate the corresponding GOT entry type is not needed.
3242 tls_optimize may also set TLS_TPRELGD when a GD reloc turns into
3243 a TPREL one. We use a separate flag rather than setting TPREL
3244 just for convenience in distinguishing the two cases. */
3245#define TLS_GD 1 /* GD reloc. */
3246#define TLS_LD 2 /* LD reloc. */
3247#define TLS_TPREL 4 /* TPREL reloc, => IE. */
3248#define TLS_DTPREL 8 /* DTPREL reloc, => LD. */
3249#define TLS_TLS 16 /* Any TLS reloc. */
3250#define TLS_EXPLICIT 32 /* Marks TOC section TLS relocs. */
3251#define TLS_TPRELGD 64 /* TPREL reloc resulting from GD->IE. */
3252 char tls_mask;
65f38f15
AM
3253};
3254
3255/* ppc64 ELF linker hash table. */
3256
3257struct ppc_link_hash_table
3258{
3259 struct elf_link_hash_table elf;
3260
721956f4
AM
3261 /* The stub hash table. */
3262 struct bfd_hash_table stub_hash_table;
3263
3264 /* Another hash table for plt_branch stubs. */
3265 struct bfd_hash_table branch_hash_table;
3266
3267 /* Linker stub bfd. */
3268 bfd *stub_bfd;
3269
3270 /* Linker call-backs. */
4ce794b7
AM
3271 asection * (*add_stub_section) (const char *, asection *);
3272 void (*layout_sections_again) (void);
721956f4
AM
3273
3274 /* Array to keep track of which stub sections have been created, and
3275 information on stub grouping. */
3276 struct map_stub {
3277 /* This is the section to which stubs in the group will be attached. */
3278 asection *link_sec;
3279 /* The stub section. */
3280 asection *stub_sec;
ad8e1ba5
AM
3281 /* Along with elf_gp, specifies the TOC pointer used in this group. */
3282 bfd_vma toc_off;
721956f4
AM
3283 } *stub_group;
3284
ad8e1ba5
AM
3285 /* Temp used when calculating TOC pointers. */
3286 bfd_vma toc_curr;
3287
8f3bab57
AM
3288 /* Highest input section id. */
3289 int top_id;
3290
734b6cf9
AM
3291 /* Highest output section index. */
3292 int top_index;
3293
3294 /* List of input sections for each output section. */
3295 asection **input_list;
721956f4 3296
65f38f15 3297 /* Short-cuts to get to dynamic linker sections. */
4ce794b7 3298 asection *got;
4ce794b7
AM
3299 asection *plt;
3300 asection *relplt;
3301 asection *dynbss;
3302 asection *relbss;
3303 asection *glink;
82bd7b59 3304 asection *sfpr;
4ce794b7
AM
3305 asection *brlt;
3306 asection *relbrlt;
ec338859 3307
8387904d
AM
3308 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
3309 struct ppc_link_hash_entry *tls_get_addr;
3310 struct ppc_link_hash_entry *tls_get_addr_fd;
411e1bfb 3311
9b5ecbd0
AM
3312 /* Statistics. */
3313 unsigned long stub_count[ppc_stub_plt_call];
3314
ee75fd95
AM
3315 /* Number of stubs against global syms. */
3316 unsigned long stub_globals;
3317
ad8e1ba5 3318 /* Set if we should emit symbols for stubs. */
99877b66 3319 unsigned int emit_stub_syms:1;
ad8e1ba5 3320
4c52953f
AM
3321 /* Support for multiple toc sections. */
3322 unsigned int no_multi_toc:1;
3323 unsigned int multi_toc_needed:1;
3324
5d1634d7 3325 /* Set on error. */
99877b66 3326 unsigned int stub_error:1;
721956f4
AM
3327
3328 /* Flag set when small branches are detected. Used to
3329 select suitable defaults for the stub group size. */
99877b66
AM
3330 unsigned int has_14bit_branch:1;
3331
3332 /* Temp used by ppc64_elf_check_directives. */
3333 unsigned int twiddled_syms:1;
721956f4
AM
3334
3335 /* Incremented every time we size stubs. */
3336 unsigned int stub_iteration;
5d1634d7 3337
ec338859
AM
3338 /* Small local sym to section mapping cache. */
3339 struct sym_sec_cache sym_sec;
65f38f15
AM
3340};
3341
4c52953f
AM
3342/* Rename some of the generic section flags to better document how they
3343 are used here. */
3344#define has_toc_reloc has_gp_reloc
3345#define makes_toc_func_call need_finalize_relax
3346#define call_check_in_progress reloc_done
3347
65f38f15
AM
3348/* Get the ppc64 ELF linker hash table from a link_info structure. */
3349
3350#define ppc_hash_table(p) \
3351 ((struct ppc_link_hash_table *) ((p)->hash))
3352
721956f4
AM
3353#define ppc_stub_hash_lookup(table, string, create, copy) \
3354 ((struct ppc_stub_hash_entry *) \
3355 bfd_hash_lookup ((table), (string), (create), (copy)))
3356
3357#define ppc_branch_hash_lookup(table, string, create, copy) \
3358 ((struct ppc_branch_hash_entry *) \
3359 bfd_hash_lookup ((table), (string), (create), (copy)))
3360
3361/* Create an entry in the stub hash table. */
3362
3363static struct bfd_hash_entry *
4ce794b7
AM
3364stub_hash_newfunc (struct bfd_hash_entry *entry,
3365 struct bfd_hash_table *table,
3366 const char *string)
721956f4
AM
3367{
3368 /* Allocate the structure if it has not already been allocated by a
3369 subclass. */
3370 if (entry == NULL)
3371 {
3372 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
3373 if (entry == NULL)
3374 return entry;
3375 }
3376
3377 /* Call the allocation method of the superclass. */
3378 entry = bfd_hash_newfunc (entry, table, string);
3379 if (entry != NULL)
3380 {
3381 struct ppc_stub_hash_entry *eh;
3382
3383 /* Initialize the local fields. */
3384 eh = (struct ppc_stub_hash_entry *) entry;
ad8e1ba5 3385 eh->stub_type = ppc_stub_none;
721956f4
AM
3386 eh->stub_sec = NULL;
3387 eh->stub_offset = 0;
3388 eh->target_value = 0;
3389 eh->target_section = NULL;
721956f4
AM
3390 eh->h = NULL;
3391 eh->id_sec = NULL;
3392 }
3393
3394 return entry;
3395}
3396
3397/* Create an entry in the branch hash table. */
3398
3399static struct bfd_hash_entry *
4ce794b7
AM
3400branch_hash_newfunc (struct bfd_hash_entry *entry,
3401 struct bfd_hash_table *table,
3402 const char *string)
721956f4
AM
3403{
3404 /* Allocate the structure if it has not already been allocated by a
3405 subclass. */
3406 if (entry == NULL)
3407 {
3408 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
3409 if (entry == NULL)
3410 return entry;
3411 }
3412
3413 /* Call the allocation method of the superclass. */
3414 entry = bfd_hash_newfunc (entry, table, string);
3415 if (entry != NULL)
3416 {
3417 struct ppc_branch_hash_entry *eh;
3418
3419 /* Initialize the local fields. */
3420 eh = (struct ppc_branch_hash_entry *) entry;
3421 eh->offset = 0;
3422 eh->iter = 0;
3423 }
3424
3425 return entry;
3426}
3427
65f38f15
AM
3428/* Create an entry in a ppc64 ELF linker hash table. */
3429
3430static struct bfd_hash_entry *
4ce794b7
AM
3431link_hash_newfunc (struct bfd_hash_entry *entry,
3432 struct bfd_hash_table *table,
3433 const char *string)
65f38f15
AM
3434{
3435 /* Allocate the structure if it has not already been allocated by a
3436 subclass. */
3437 if (entry == NULL)
3438 {
3439 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
3440 if (entry == NULL)
3441 return entry;
3442 }
3443
3444 /* Call the allocation method of the superclass. */
3445 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
3446 if (entry != NULL)
3447 {
3448 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
3449
908b32fc
AM
3450 memset (&eh->stub_cache, 0,
3451 (sizeof (struct ppc_link_hash_entry)
3452 - offsetof (struct ppc_link_hash_entry, stub_cache)));
65f38f15
AM
3453 }
3454
3455 return entry;
3456}
3457
3458/* Create a ppc64 ELF linker hash table. */
3459
3460static struct bfd_link_hash_table *
4ce794b7 3461ppc64_elf_link_hash_table_create (bfd *abfd)
65f38f15
AM
3462{
3463 struct ppc_link_hash_table *htab;
3464 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
3465
4ce794b7 3466 htab = bfd_zmalloc (amt);
65f38f15
AM
3467 if (htab == NULL)
3468 return NULL;
3469
66eb6687
AM
3470 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
3471 sizeof (struct ppc_link_hash_entry)))
65f38f15 3472 {
e2d34d7d 3473 free (htab);
65f38f15
AM
3474 return NULL;
3475 }
3476
721956f4 3477 /* Init the stub hash table too. */
66eb6687
AM
3478 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
3479 sizeof (struct ppc_stub_hash_entry)))
721956f4
AM
3480 return NULL;
3481
3482 /* And the branch hash table. */
66eb6687
AM
3483 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
3484 sizeof (struct ppc_branch_hash_entry)))
721956f4
AM
3485 return NULL;
3486
3254fd24
AM
3487 /* Initializing two fields of the union is just cosmetic. We really
3488 only care about glist, but when compiled on a 32-bit host the
3489 bfd_vma fields are larger. Setting the bfd_vma to zero makes
3490 debugger inspection of these fields look nicer. */
a6aa5195
AM
3491 htab->elf.init_got_refcount.refcount = 0;
3492 htab->elf.init_got_refcount.glist = NULL;
3493 htab->elf.init_plt_refcount.refcount = 0;
3494 htab->elf.init_plt_refcount.glist = NULL;
3495 htab->elf.init_got_offset.offset = 0;
3496 htab->elf.init_got_offset.glist = NULL;
3497 htab->elf.init_plt_offset.offset = 0;
3498 htab->elf.init_plt_offset.glist = NULL;
3254fd24 3499
65f38f15
AM
3500 return &htab->elf.root;
3501}
3502
721956f4
AM
3503/* Free the derived linker hash table. */
3504
3505static void
4ce794b7 3506ppc64_elf_link_hash_table_free (struct bfd_link_hash_table *hash)
721956f4
AM
3507{
3508 struct ppc_link_hash_table *ret = (struct ppc_link_hash_table *) hash;
3509
3510 bfd_hash_table_free (&ret->stub_hash_table);
3511 bfd_hash_table_free (&ret->branch_hash_table);
3512 _bfd_generic_link_hash_table_free (hash);
3513}
3514
e717da7e
AM
3515/* Satisfy the ELF linker by filling in some fields in our fake bfd. */
3516
3517void
3518ppc64_elf_init_stub_bfd (bfd *abfd, struct bfd_link_info *info)
3519{
3520 struct ppc_link_hash_table *htab;
3521
3522 elf_elfheader (abfd)->e_ident[EI_CLASS] = ELFCLASS64;
3523
3524/* Always hook our dynamic sections into the first bfd, which is the
3525 linker created stub bfd. This ensures that the GOT header is at
3526 the start of the output TOC section. */
3527 htab = ppc_hash_table (info);
3528 htab->stub_bfd = abfd;
3529 htab->elf.dynobj = abfd;
3530}
3531
721956f4
AM
3532/* Build a name for an entry in the stub hash table. */
3533
3534static char *
4ce794b7
AM
3535ppc_stub_name (const asection *input_section,
3536 const asection *sym_sec,
3537 const struct ppc_link_hash_entry *h,
3538 const Elf_Internal_Rela *rel)
721956f4
AM
3539{
3540 char *stub_name;
3541 bfd_size_type len;
3542
3543 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
3544 offsets from a sym as a branch target? In fact, we could
3545 probably assume the addend is always zero. */
3546 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
3547
3548 if (h)
3549 {
3550 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
3551 stub_name = bfd_malloc (len);
46de2a7c
AM
3552 if (stub_name == NULL)
3553 return stub_name;
3554
3555 sprintf (stub_name, "%08x.%s+%x",
3556 input_section->id & 0xffffffff,
3557 h->elf.root.root.string,
3558 (int) rel->r_addend & 0xffffffff);
721956f4
AM
3559 }
3560 else
3561 {
ad8e1ba5 3562 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
721956f4 3563 stub_name = bfd_malloc (len);
46de2a7c
AM
3564 if (stub_name == NULL)
3565 return stub_name;
3566
3567 sprintf (stub_name, "%08x.%x:%x+%x",
3568 input_section->id & 0xffffffff,
3569 sym_sec->id & 0xffffffff,
3570 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
3571 (int) rel->r_addend & 0xffffffff);
721956f4 3572 }
ee75fd95
AM
3573 if (stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
3574 stub_name[len - 2] = 0;
721956f4
AM
3575 return stub_name;
3576}
3577
3578/* Look up an entry in the stub hash. Stub entries are cached because
3579 creating the stub name takes a bit of time. */
3580
3581static struct ppc_stub_hash_entry *
4ce794b7
AM
3582ppc_get_stub_entry (const asection *input_section,
3583 const asection *sym_sec,
039b3fef 3584 struct ppc_link_hash_entry *h,
4ce794b7
AM
3585 const Elf_Internal_Rela *rel,
3586 struct ppc_link_hash_table *htab)
721956f4
AM
3587{
3588 struct ppc_stub_hash_entry *stub_entry;
721956f4
AM
3589 const asection *id_sec;
3590
3591 /* If this input section is part of a group of sections sharing one
3592 stub section, then use the id of the first section in the group.
3593 Stub names need to include a section id, as there may well be
3594 more than one stub used to reach say, printf, and we need to
3595 distinguish between them. */
3596 id_sec = htab->stub_group[input_section->id].link_sec;
3597
3598 if (h != NULL && h->stub_cache != NULL
3599 && h->stub_cache->h == h
3600 && h->stub_cache->id_sec == id_sec)
3601 {
3602 stub_entry = h->stub_cache;
3603 }
3604 else
3605 {
3606 char *stub_name;
3607
3608 stub_name = ppc_stub_name (id_sec, sym_sec, h, rel);
3609 if (stub_name == NULL)
3610 return NULL;
3611
3612 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 3613 stub_name, FALSE, FALSE);
721956f4
AM
3614 if (h != NULL)
3615 h->stub_cache = stub_entry;
3616
3617 free (stub_name);
3618 }
3619
3620 return stub_entry;
3621}
3622
3623/* Add a new stub entry to the stub hash. Not all fields of the new
3624 stub entry are initialised. */
3625
3626static struct ppc_stub_hash_entry *
4ce794b7
AM
3627ppc_add_stub (const char *stub_name,
3628 asection *section,
3629 struct ppc_link_hash_table *htab)
721956f4
AM
3630{
3631 asection *link_sec;
3632 asection *stub_sec;
3633 struct ppc_stub_hash_entry *stub_entry;
3634
3635 link_sec = htab->stub_group[section->id].link_sec;
3636 stub_sec = htab->stub_group[section->id].stub_sec;
3637 if (stub_sec == NULL)
3638 {
3639 stub_sec = htab->stub_group[link_sec->id].stub_sec;
3640 if (stub_sec == NULL)
3641 {
d4c88bbb 3642 size_t namelen;
721956f4
AM
3643 bfd_size_type len;
3644 char *s_name;
3645
d4c88bbb
AM
3646 namelen = strlen (link_sec->name);
3647 len = namelen + sizeof (STUB_SUFFIX);
721956f4
AM
3648 s_name = bfd_alloc (htab->stub_bfd, len);
3649 if (s_name == NULL)
3650 return NULL;
3651
d4c88bbb
AM
3652 memcpy (s_name, link_sec->name, namelen);
3653 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
721956f4
AM
3654 stub_sec = (*htab->add_stub_section) (s_name, link_sec);
3655 if (stub_sec == NULL)
3656 return NULL;
3657 htab->stub_group[link_sec->id].stub_sec = stub_sec;
3658 }
3659 htab->stub_group[section->id].stub_sec = stub_sec;
3660 }
3661
3662 /* Enter this entry into the linker stub hash table. */
3663 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
b34976b6 3664 TRUE, FALSE);
721956f4
AM
3665 if (stub_entry == NULL)
3666 {
d003868e
AM
3667 (*_bfd_error_handler) (_("%B: cannot create stub entry %s"),
3668 section->owner, stub_name);
721956f4
AM
3669 return NULL;
3670 }
3671
3672 stub_entry->stub_sec = stub_sec;
3673 stub_entry->stub_offset = 0;
3674 stub_entry->id_sec = link_sec;
3675 return stub_entry;
3676}
3677
82bd7b59
AM
3678/* Create sections for linker generated code. */
3679
b34976b6 3680static bfd_boolean
4ce794b7 3681create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
82bd7b59
AM
3682{
3683 struct ppc_link_hash_table *htab;
3684 flagword flags;
3685
3686 htab = ppc_hash_table (info);
3687
3688 /* Create .sfpr for code to save and restore fp regs. */
3689 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
3690 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3496cb2a
L
3691 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
3692 flags);
82bd7b59 3693 if (htab->sfpr == NULL
82bd7b59 3694 || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
b34976b6 3695 return FALSE;
82bd7b59 3696
721956f4 3697 /* Create .glink for lazy dynamic linking support. */
3496cb2a
L
3698 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3699 flags);
4ce794b7 3700 if (htab->glink == NULL
4ce794b7 3701 || ! bfd_set_section_alignment (dynobj, htab->glink, 2))
b34976b6 3702 return FALSE;
82bd7b59 3703
c456f082
AM
3704 /* Create branch lookup table for plt_branch stubs. */
3705 if (info->shared)
3706 {
3707 flags = (SEC_ALLOC | SEC_LOAD
3708 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3709 htab->brlt
3710 = bfd_make_section_anyway_with_flags (dynobj, ".data.rel.ro.brlt",
3711 flags);
3712 }
3713 else
3714 {
3715 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3716 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3717 htab->brlt
3718 = bfd_make_section_anyway_with_flags (dynobj, ".rodata.brlt", flags);
3719 }
3720
4ce794b7 3721 if (htab->brlt == NULL
4ce794b7 3722 || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
b34976b6 3723 return FALSE;
721956f4 3724
c456f082 3725 if (info->shared)
721956f4
AM
3726 {
3727 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3728 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
c456f082
AM
3729 htab->relbrlt
3730 = bfd_make_section_anyway_with_flags (dynobj, ".rela.data.rel.ro.brlt",
3731 flags);
721956f4 3732 }
c456f082
AM
3733 else if (info->emitrelocations)
3734 {
3735 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3736 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3737 htab->relbrlt
3738 = bfd_make_section_anyway_with_flags (dynobj, ".rela.rodata.brlt",
3739 flags);
3740 }
3741 else
3742 return TRUE;
3743
3744 if (!htab->relbrlt
3745 || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
3746 return FALSE;
3747
b34976b6 3748 return TRUE;
82bd7b59
AM
3749}
3750
e717da7e
AM
3751/* Create .got and .rela.got sections in ABFD, and .got in dynobj if
3752 not already done. */
65f38f15 3753
b34976b6 3754static bfd_boolean
e717da7e 3755create_got_section (bfd *abfd, struct bfd_link_info *info)
65f38f15 3756{
e717da7e
AM
3757 asection *got, *relgot;
3758 flagword flags;
3759 struct ppc_link_hash_table *htab = ppc_hash_table (info);
65f38f15 3760
4ce794b7 3761 if (!htab->got)
e717da7e
AM
3762 {
3763 if (! _bfd_elf_create_got_section (htab->elf.dynobj, info))
3764 return FALSE;
3765
3766 htab->got = bfd_get_section_by_name (htab->elf.dynobj, ".got");
3767 if (!htab->got)
3768 abort ();
3769 }
3770
3771 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3772 | SEC_LINKER_CREATED);
3773
c456f082 3774 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
e717da7e 3775 if (!got
e717da7e
AM
3776 || !bfd_set_section_alignment (abfd, got, 3))
3777 return FALSE;
65f38f15 3778
c456f082
AM
3779 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
3780 flags | SEC_READONLY);
e717da7e 3781 if (!relgot
e717da7e 3782 || ! bfd_set_section_alignment (abfd, relgot, 3))
b34976b6 3783 return FALSE;
e717da7e
AM
3784
3785 ppc64_elf_tdata (abfd)->got = got;
3786 ppc64_elf_tdata (abfd)->relgot = relgot;
b34976b6 3787 return TRUE;
65f38f15 3788}
5bd4f169 3789
82bd7b59 3790/* Create the dynamic sections, and set up shortcuts. */
5bd4f169 3791
b34976b6 3792static bfd_boolean
4ce794b7 3793ppc64_elf_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
5bd4f169 3794{
65f38f15 3795 struct ppc_link_hash_table *htab;
5bd4f169 3796
65f38f15 3797 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
b34976b6 3798 return FALSE;
65f38f15 3799
e717da7e
AM
3800 htab = ppc_hash_table (info);
3801 if (!htab->got)
3802 htab->got = bfd_get_section_by_name (dynobj, ".got");
4ce794b7
AM
3803 htab->plt = bfd_get_section_by_name (dynobj, ".plt");
3804 htab->relplt = bfd_get_section_by_name (dynobj, ".rela.plt");
3805 htab->dynbss = bfd_get_section_by_name (dynobj, ".dynbss");
65f38f15 3806 if (!info->shared)
4ce794b7 3807 htab->relbss = bfd_get_section_by_name (dynobj, ".rela.bss");
65f38f15 3808
e717da7e 3809 if (!htab->got || !htab->plt || !htab->relplt || !htab->dynbss
4ce794b7 3810 || (!info->shared && !htab->relbss))
65f38f15
AM
3811 abort ();
3812
b34976b6 3813 return TRUE;
5bd4f169
AM
3814}
3815
40d16e0b
AM
3816/* Merge PLT info on FROM with that on TO. */
3817
3818static void
3819move_plt_plist (struct ppc_link_hash_entry *from,
3820 struct ppc_link_hash_entry *to)
3821{
3822 if (from->elf.plt.plist != NULL)
3823 {
3824 if (to->elf.plt.plist != NULL)
3825 {
3826 struct plt_entry **entp;
3827 struct plt_entry *ent;
3828
3829 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
3830 {
3831 struct plt_entry *dent;
3832
3833 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
3834 if (dent->addend == ent->addend)
3835 {
3836 dent->plt.refcount += ent->plt.refcount;
3837 *entp = ent->next;
3838 break;
3839 }
3840 if (dent == NULL)
3841 entp = &ent->next;
3842 }
3843 *entp = to->elf.plt.plist;
3844 }
3845
3846 to->elf.plt.plist = from->elf.plt.plist;
3847 from->elf.plt.plist = NULL;
3848 }
3849}
3850
65f38f15
AM
3851/* Copy the extra info we tack onto an elf_link_hash_entry. */
3852
3853static void
fcfa13d2
AM
3854ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
3855 struct elf_link_hash_entry *dir,
3856 struct elf_link_hash_entry *ind)
65f38f15
AM
3857{
3858 struct ppc_link_hash_entry *edir, *eind;
3859
3860 edir = (struct ppc_link_hash_entry *) dir;
3861 eind = (struct ppc_link_hash_entry *) ind;
3862
411e1bfb 3863 /* Copy over any dynamic relocs we may have on the indirect sym. */
bbd7ec4a 3864 if (eind->dyn_relocs != NULL)
65f38f15 3865 {
bbd7ec4a
AM
3866 if (edir->dyn_relocs != NULL)
3867 {
3868 struct ppc_dyn_relocs **pp;
3869 struct ppc_dyn_relocs *p;
3870
fcfa13d2 3871 /* Add reloc counts against the indirect sym to the direct sym
bbd7ec4a
AM
3872 list. Merge any entries against the same section. */
3873 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
3874 {
3875 struct ppc_dyn_relocs *q;
3876
3877 for (q = edir->dyn_relocs; q != NULL; q = q->next)
3878 if (q->sec == p->sec)
3879 {
3880 q->pc_count += p->pc_count;
3881 q->count += p->count;
3882 *pp = p->next;
3883 break;
3884 }
3885 if (q == NULL)
3886 pp = &p->next;
3887 }
3888 *pp = edir->dyn_relocs;
3889 }
3890
65f38f15
AM
3891 edir->dyn_relocs = eind->dyn_relocs;
3892 eind->dyn_relocs = NULL;
3893 }
65f38f15 3894
6349e628
AM
3895 edir->is_func |= eind->is_func;
3896 edir->is_func_descriptor |= eind->is_func_descriptor;
58ac9f71 3897 edir->tls_mask |= eind->tls_mask;
6349e628 3898
81848ca0 3899 /* If called to transfer flags for a weakdef during processing
f5385ebf 3900 of elf_adjust_dynamic_symbol, don't copy NON_GOT_REF.
81848ca0 3901 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
f5385ebf
AM
3902 if (!(ELIMINATE_COPY_RELOCS
3903 && eind->elf.root.type != bfd_link_hash_indirect
3904 && edir->elf.dynamic_adjusted))
3905 edir->elf.non_got_ref |= eind->elf.non_got_ref;
81848ca0 3906
f5385ebf
AM
3907 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
3908 edir->elf.ref_regular |= eind->elf.ref_regular;
3909 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
3910 edir->elf.needs_plt |= eind->elf.needs_plt;
6349e628
AM
3911
3912 /* If we were called to copy over info for a weak sym, that's all. */
3913 if (eind->elf.root.type != bfd_link_hash_indirect)
3914 return;
3915
81848ca0
AM
3916 /* Copy over got entries that we may have already seen to the
3917 symbol which just became indirect. */
411e1bfb
AM
3918 if (eind->elf.got.glist != NULL)
3919 {
3920 if (edir->elf.got.glist != NULL)
3921 {
3922 struct got_entry **entp;
3923 struct got_entry *ent;
3924
3925 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
3926 {
3927 struct got_entry *dent;
3928
3929 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
3930 if (dent->addend == ent->addend
e717da7e 3931 && dent->owner == ent->owner
411e1bfb
AM
3932 && dent->tls_type == ent->tls_type)
3933 {
3934 dent->got.refcount += ent->got.refcount;
3935 *entp = ent->next;
3936 break;
3937 }
3938 if (dent == NULL)
3939 entp = &ent->next;
3940 }
3941 *entp = edir->elf.got.glist;
3942 }
3943
3944 edir->elf.got.glist = eind->elf.got.glist;
3945 eind->elf.got.glist = NULL;
3946 }
3947
3948 /* And plt entries. */
40d16e0b 3949 move_plt_plist (eind, edir);
411e1bfb 3950
fcfa13d2 3951 if (eind->elf.dynindx != -1)
411e1bfb 3952 {
fcfa13d2
AM
3953 if (edir->elf.dynindx != -1)
3954 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
3955 edir->elf.dynstr_index);
411e1bfb
AM
3956 edir->elf.dynindx = eind->elf.dynindx;
3957 edir->elf.dynstr_index = eind->elf.dynstr_index;
3958 eind->elf.dynindx = -1;
3959 eind->elf.dynstr_index = 0;
3960 }
411e1bfb
AM
3961}
3962
8387904d
AM
3963/* Find the function descriptor hash entry from the given function code
3964 hash entry FH. Link the entries via their OH fields. */
3965
3966static struct ppc_link_hash_entry *
3967get_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
3968{
3969 struct ppc_link_hash_entry *fdh = fh->oh;
3970
3971 if (fdh == NULL)
3972 {
3973 const char *fd_name = fh->elf.root.root.string + 1;
3974
3975 fdh = (struct ppc_link_hash_entry *)
3976 elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
3977 if (fdh != NULL)
3978 {
3979 fdh->is_func_descriptor = 1;
3980 fdh->oh = fh;
3981 fh->is_func = 1;
3982 fh->oh = fdh;
3983 }
3984 }
3985
3986 return fdh;
3987}
3988
bb700d78
AM
3989/* Make a fake function descriptor sym for the code sym FH. */
3990
3991static struct ppc_link_hash_entry *
3992make_fdh (struct bfd_link_info *info,
908b32fc 3993 struct ppc_link_hash_entry *fh)
bb700d78
AM
3994{
3995 bfd *abfd;
3996 asymbol *newsym;
3997 struct bfd_link_hash_entry *bh;
3998 struct ppc_link_hash_entry *fdh;
3999
4000 abfd = fh->elf.root.u.undef.abfd;
4001 newsym = bfd_make_empty_symbol (abfd);
4002 newsym->name = fh->elf.root.root.string + 1;
4003 newsym->section = bfd_und_section_ptr;
4004 newsym->value = 0;
908b32fc 4005 newsym->flags = BSF_WEAK;
bb700d78
AM
4006
4007 bh = NULL;
4008 if (!_bfd_generic_link_add_one_symbol (info, abfd, newsym->name,
4009 newsym->flags, newsym->section,
4010 newsym->value, NULL, FALSE, FALSE,
4011 &bh))
4012 return NULL;
4013
4014 fdh = (struct ppc_link_hash_entry *) bh;
4015 fdh->elf.non_elf = 0;
908b32fc
AM
4016 fdh->fake = 1;
4017 fdh->is_func_descriptor = 1;
4018 fdh->oh = fh;
4019 fh->is_func = 1;
4020 fh->oh = fdh;
bb700d78
AM
4021 return fdh;
4022}
4023
8387904d
AM
4024/* Hacks to support old ABI code.
4025 When making function calls, old ABI code references function entry
4026 points (dot symbols), while new ABI code references the function
4027 descriptor symbol. We need to make any combination of reference and
4028 definition work together, without breaking archive linking.
4029
4030 For a defined function "foo" and an undefined call to "bar":
4031 An old object defines "foo" and ".foo", references ".bar" (possibly
4032 "bar" too).
4033 A new object defines "foo" and references "bar".
4034
4035 A new object thus has no problem with its undefined symbols being
4036 satisfied by definitions in an old object. On the other hand, the
4037 old object won't have ".bar" satisfied by a new object. */
4038
4039/* Fix function descriptor symbols defined in .opd sections to be
4040 function type. */
555cd476
AM
4041
4042static bfd_boolean
433817dd 4043ppc64_elf_add_symbol_hook (bfd *ibfd,
555cd476
AM
4044 struct bfd_link_info *info ATTRIBUTE_UNUSED,
4045 Elf_Internal_Sym *isym,
433817dd 4046 const char **name,
555cd476
AM
4047 flagword *flags ATTRIBUTE_UNUSED,
4048 asection **sec,
4049 bfd_vma *value ATTRIBUTE_UNUSED)
4050{
8843416a
AM
4051 if (*sec != NULL
4052 && strcmp (bfd_get_section_name (ibfd, *sec), ".opd") == 0)
555cd476 4053 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
433817dd
AM
4054
4055 if ((*name)[0] == '.'
4056 && ELF_ST_BIND (isym->st_info) == STB_GLOBAL
4057 && ELF_ST_TYPE (isym->st_info) < STT_SECTION
4058 && is_ppc64_elf_target (ibfd->xvec))
4059 ppc64_elf_tdata (ibfd)->u.has_dotsym = 1;
4060
555cd476
AM
4061 return TRUE;
4062}
4063
8387904d 4064/* This function makes an old ABI object reference to ".bar" cause the
908b32fc
AM
4065 inclusion of a new ABI object archive that defines "bar".
4066 NAME is a symbol defined in an archive. Return a symbol in the hash
4067 table that might be satisfied by the archive symbols. */
8387904d
AM
4068
4069static struct elf_link_hash_entry *
4070ppc64_elf_archive_symbol_lookup (bfd *abfd,
4071 struct bfd_link_info *info,
4072 const char *name)
4073{
4074 struct elf_link_hash_entry *h;
4075 char *dot_name;
4076 size_t len;
4077
4078 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
908b32fc
AM
4079 if (h != NULL
4080 /* Don't return this sym if it is a fake function descriptor
4081 created by add_symbol_adjust. */
4082 && !(h->root.type == bfd_link_hash_undefweak
4083 && ((struct ppc_link_hash_entry *) h)->fake))
8387904d
AM
4084 return h;
4085
4086 if (name[0] == '.')
4087 return h;
4088
4089 len = strlen (name);
4090 dot_name = bfd_alloc (abfd, len + 2);
4091 if (dot_name == NULL)
4092 return (struct elf_link_hash_entry *) 0 - 1;
4093 dot_name[0] = '.';
4094 memcpy (dot_name + 1, name, len + 1);
4095 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4096 bfd_release (abfd, dot_name);
4097 return h;
4098}
4099
4100/* This function satisfies all old ABI object references to ".bar" if a
99877b66
AM
4101 new ABI object defines "bar". Well, at least, undefined dot symbols
4102 are made weak. This stops later archive searches from including an
4103 object if we already have a function descriptor definition. It also
35b0ce59
AM
4104 prevents the linker complaining about undefined symbols.
4105 We also check and correct mismatched symbol visibility here. The
4106 most restrictive visibility of the function descriptor and the
4107 function entry symbol is used. */
8387904d 4108
bb700d78
AM
4109struct add_symbol_adjust_data
4110{
4111 struct bfd_link_info *info;
4112 bfd_boolean ok;
4113};
4114
8387904d
AM
4115static bfd_boolean
4116add_symbol_adjust (struct elf_link_hash_entry *h, void *inf)
4117{
bb700d78 4118 struct add_symbol_adjust_data *data;
8387904d 4119 struct ppc_link_hash_table *htab;
99877b66 4120 struct ppc_link_hash_entry *eh;
8387904d
AM
4121 struct ppc_link_hash_entry *fdh;
4122
4123 if (h->root.type == bfd_link_hash_indirect)
4124 return TRUE;
4125
4126 if (h->root.type == bfd_link_hash_warning)
4127 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4128
35b0ce59 4129 if (h->root.root.string[0] != '.')
8387904d
AM
4130 return TRUE;
4131
bb700d78
AM
4132 data = inf;
4133 htab = ppc_hash_table (data->info);
99877b66
AM
4134 eh = (struct ppc_link_hash_entry *) h;
4135 fdh = get_fdh (eh, htab);
bb700d78 4136 if (fdh == NULL
c4f68ce3 4137 && !data->info->relocatable
bb700d78
AM
4138 && (eh->elf.root.type == bfd_link_hash_undefined
4139 || eh->elf.root.type == bfd_link_hash_undefweak)
4140 && eh->elf.ref_regular)
4141 {
4142 /* Make an undefweak function descriptor sym, which is enough to
4143 pull in an --as-needed shared lib, but won't cause link
4144 errors. Archives are handled elsewhere. */
908b32fc 4145 fdh = make_fdh (data->info, eh);
bb700d78
AM
4146 if (fdh == NULL)
4147 data->ok = FALSE;
4148 else
4149 fdh->elf.ref_regular = 1;
4150 }
e87d886e 4151 else if (fdh != NULL)
8387904d 4152 {
35b0ce59
AM
4153 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4154 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
4155 if (entry_vis < descr_vis)
4156 fdh->elf.other += entry_vis - descr_vis;
4157 else if (entry_vis > descr_vis)
4158 eh->elf.other += descr_vis - entry_vis;
4159
e87d886e
AM
4160 if ((fdh->elf.root.type == bfd_link_hash_defined
4161 || fdh->elf.root.type == bfd_link_hash_defweak)
4162 && eh->elf.root.type == bfd_link_hash_undefined)
35b0ce59
AM
4163 {
4164 eh->elf.root.type = bfd_link_hash_undefweak;
4165 eh->was_undefined = 1;
4166 htab->twiddled_syms = 1;
4167 }
8387904d 4168 }
99877b66 4169
8387904d
AM
4170 return TRUE;
4171}
4172
4173static bfd_boolean
433817dd 4174ppc64_elf_check_directives (bfd *abfd, struct bfd_link_info *info)
8387904d 4175{
99877b66 4176 struct ppc_link_hash_table *htab;
bb700d78 4177 struct add_symbol_adjust_data data;
99877b66 4178
433817dd
AM
4179 if (!is_ppc64_elf_target (abfd->xvec))
4180 return TRUE;
4181
4182 if (!ppc64_elf_tdata (abfd)->u.has_dotsym)
4183 return TRUE;
4184 ppc64_elf_tdata (abfd)->u.deleted_section = NULL;
4185
99877b66 4186 htab = ppc_hash_table (info);
ee75fd95 4187 if (!is_ppc64_elf_target (htab->elf.root.creator))
35b0ce59
AM
4188 return TRUE;
4189
bb700d78
AM
4190 data.info = info;
4191 data.ok = TRUE;
4192 elf_link_hash_traverse (&htab->elf, add_symbol_adjust, &data);
99877b66
AM
4193
4194 /* We need to fix the undefs list for any syms we have twiddled to
4195 undef_weak. */
4196 if (htab->twiddled_syms)
4197 {
77cfaee6 4198 bfd_link_repair_undef_list (&htab->elf.root);
99877b66
AM
4199 htab->twiddled_syms = 0;
4200 }
bb700d78 4201 return data.ok;
8387904d
AM
4202}
4203
411e1bfb 4204static bfd_boolean
4ce794b7
AM
4205update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
4206 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
411e1bfb
AM
4207{
4208 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
e7b938ca 4209 char *local_got_tls_masks;
411e1bfb
AM
4210
4211 if (local_got_ents == NULL)
4212 {
4213 bfd_size_type size = symtab_hdr->sh_info;
4214
e7b938ca 4215 size *= sizeof (*local_got_ents) + sizeof (*local_got_tls_masks);
4ce794b7 4216 local_got_ents = bfd_zalloc (abfd, size);
411e1bfb
AM
4217 if (local_got_ents == NULL)
4218 return FALSE;
4219 elf_local_got_ents (abfd) = local_got_ents;
4220 }
4221
4222 if ((tls_type & TLS_EXPLICIT) == 0)
4223 {
4224 struct got_entry *ent;
4225
4226 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
e717da7e
AM
4227 if (ent->addend == r_addend
4228 && ent->owner == abfd
4229 && ent->tls_type == tls_type)
411e1bfb
AM
4230 break;
4231 if (ent == NULL)
4232 {
4233 bfd_size_type amt = sizeof (*ent);
4ce794b7 4234 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
4235 if (ent == NULL)
4236 return FALSE;
4237 ent->next = local_got_ents[r_symndx];
4238 ent->addend = r_addend;
e717da7e 4239 ent->owner = abfd;
411e1bfb
AM
4240 ent->tls_type = tls_type;
4241 ent->got.refcount = 0;
4242 local_got_ents[r_symndx] = ent;
4243 }
4244 ent->got.refcount += 1;
4245 }
4246
e7b938ca
AM
4247 local_got_tls_masks = (char *) (local_got_ents + symtab_hdr->sh_info);
4248 local_got_tls_masks[r_symndx] |= tls_type;
411e1bfb 4249 return TRUE;
65f38f15
AM
4250}
4251
411e1bfb 4252static bfd_boolean
4ce794b7 4253update_plt_info (bfd *abfd, struct ppc_link_hash_entry *eh, bfd_vma addend)
1e2f5b6e 4254{
411e1bfb 4255 struct plt_entry *ent;
1e2f5b6e 4256
411e1bfb
AM
4257 for (ent = eh->elf.plt.plist; ent != NULL; ent = ent->next)
4258 if (ent->addend == addend)
4259 break;
4260 if (ent == NULL)
1e2f5b6e 4261 {
411e1bfb 4262 bfd_size_type amt = sizeof (*ent);
4ce794b7 4263 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
4264 if (ent == NULL)
4265 return FALSE;
4266 ent->next = eh->elf.plt.plist;
4267 ent->addend = addend;
4268 ent->plt.refcount = 0;
4269 eh->elf.plt.plist = ent;
1e2f5b6e 4270 }
411e1bfb 4271 ent->plt.refcount += 1;
f5385ebf 4272 eh->elf.needs_plt = 1;
93f3fa99
AM
4273 if (eh->elf.root.root.string[0] == '.'
4274 && eh->elf.root.root.string[1] != '\0')
4275 eh->is_func = 1;
b34976b6 4276 return TRUE;
1e2f5b6e
AM
4277}
4278
5bd4f169 4279/* Look through the relocs for a section during the first phase, and
65f38f15 4280 calculate needed space in the global offset table, procedure
5d1634d7 4281 linkage table, and dynamic reloc sections. */
5bd4f169 4282
b34976b6 4283static bfd_boolean
4ce794b7
AM
4284ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
4285 asection *sec, const Elf_Internal_Rela *relocs)
5bd4f169 4286{
65f38f15 4287 struct ppc_link_hash_table *htab;
5bd4f169
AM
4288 Elf_Internal_Shdr *symtab_hdr;
4289 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
4290 const Elf_Internal_Rela *rel;
4291 const Elf_Internal_Rela *rel_end;
5bd4f169 4292 asection *sreloc;
1e2f5b6e 4293 asection **opd_sym_map;
5bd4f169 4294
1049f94e 4295 if (info->relocatable)
b34976b6 4296 return TRUE;
5bd4f169 4297
680a3378
AM
4298 /* Don't do anything special with non-loaded, non-alloced sections.
4299 In particular, any relocs in such sections should not affect GOT
4300 and PLT reference counting (ie. we don't allow them to create GOT
4301 or PLT entries), there's no possibility or desire to optimize TLS
4302 relocs, and there's not much point in propagating relocs to shared
4303 libs that the dynamic linker won't relocate. */
4304 if ((sec->flags & SEC_ALLOC) == 0)
4305 return TRUE;
4306
65f38f15 4307 htab = ppc_hash_table (info);
5bd4f169 4308 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5bd4f169
AM
4309
4310 sym_hashes = elf_sym_hashes (abfd);
4311 sym_hashes_end = (sym_hashes
1e2f5b6e
AM
4312 + symtab_hdr->sh_size / sizeof (Elf64_External_Sym)
4313 - symtab_hdr->sh_info);
5bd4f169
AM
4314
4315 sreloc = NULL;
1e2f5b6e
AM
4316 opd_sym_map = NULL;
4317 if (strcmp (bfd_get_section_name (abfd, sec), ".opd") == 0)
4318 {
4319 /* Garbage collection needs some extra help with .opd sections.
4320 We don't want to necessarily keep everything referenced by
4321 relocs in .opd, as that would keep all functions. Instead,
4322 if we reference an .opd symbol (a function descriptor), we
4323 want to keep the function code symbol's section. This is
4324 easy for global symbols, but for local syms we need to keep
4325 information about the associated function section. Later, if
4326 edit_opd deletes entries, we'll use this array to adjust
4327 local syms in .opd. */
4328 union opd_info {
4329 asection *func_section;
4330 long entry_adjust;
4331 };
4332 bfd_size_type amt;
4333
3f764659 4334 amt = sec->size * sizeof (union opd_info) / 8;
4ce794b7 4335 opd_sym_map = bfd_zalloc (abfd, amt);
1e2f5b6e 4336 if (opd_sym_map == NULL)
b34976b6 4337 return FALSE;
f0abc2a1 4338 ppc64_elf_section_data (sec)->opd.func_sec = opd_sym_map;
1e2f5b6e 4339 }
5bd4f169 4340
82bd7b59
AM
4341 if (htab->sfpr == NULL
4342 && !create_linkage_sections (htab->elf.dynobj, info))
b34976b6 4343 return FALSE;
82bd7b59 4344
5bd4f169
AM
4345 rel_end = relocs + sec->reloc_count;
4346 for (rel = relocs; rel < rel_end; rel++)
4347 {
4348 unsigned long r_symndx;
4349 struct elf_link_hash_entry *h;
04c9666a 4350 enum elf_ppc64_reloc_type r_type;
411e1bfb 4351 int tls_type = 0;
5bd4f169
AM
4352
4353 r_symndx = ELF64_R_SYM (rel->r_info);
4354 if (r_symndx < symtab_hdr->sh_info)
4355 h = NULL;
4356 else
973a3492
L
4357 {
4358 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4359 while (h->root.type == bfd_link_hash_indirect
4360 || h->root.type == bfd_link_hash_warning)
4361 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4362 }
5bd4f169 4363
4ce794b7 4364 r_type = ELF64_R_TYPE (rel->r_info);
a33d1f77 4365 switch (r_type)
5bd4f169 4366 {
411e1bfb
AM
4367 case R_PPC64_GOT_TLSLD16:
4368 case R_PPC64_GOT_TLSLD16_LO:
4369 case R_PPC64_GOT_TLSLD16_HI:
4370 case R_PPC64_GOT_TLSLD16_HA:
e717da7e 4371 ppc64_tlsld_got (abfd)->refcount += 1;
951fd09b 4372 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
4373 goto dogottls;
4374
4375 case R_PPC64_GOT_TLSGD16:
4376 case R_PPC64_GOT_TLSGD16_LO:
4377 case R_PPC64_GOT_TLSGD16_HI:
4378 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 4379 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
4380 goto dogottls;
4381
4382 case R_PPC64_GOT_TPREL16_DS:
4383 case R_PPC64_GOT_TPREL16_LO_DS:
4384 case R_PPC64_GOT_TPREL16_HI:
4385 case R_PPC64_GOT_TPREL16_HA:
4386 if (info->shared)
4387 info->flags |= DF_STATIC_TLS;
4388 tls_type = TLS_TLS | TLS_TPREL;
4389 goto dogottls;
4390
4391 case R_PPC64_GOT_DTPREL16_DS:
4392 case R_PPC64_GOT_DTPREL16_LO_DS:
4393 case R_PPC64_GOT_DTPREL16_HI:
4394 case R_PPC64_GOT_DTPREL16_HA:
4395 tls_type = TLS_TLS | TLS_DTPREL;
4396 dogottls:
4397 sec->has_tls_reloc = 1;
4398 /* Fall thru */
4399
5bd4f169 4400 case R_PPC64_GOT16:
5bd4f169 4401 case R_PPC64_GOT16_DS:
65f38f15
AM
4402 case R_PPC64_GOT16_HA:
4403 case R_PPC64_GOT16_HI:
4404 case R_PPC64_GOT16_LO:
5bd4f169 4405 case R_PPC64_GOT16_LO_DS:
65f38f15 4406 /* This symbol requires a global offset table entry. */
4c52953f 4407 sec->has_toc_reloc = 1;
e717da7e
AM
4408 if (ppc64_elf_tdata (abfd)->got == NULL
4409 && !create_got_section (abfd, info))
b34976b6 4410 return FALSE;
5bd4f169
AM
4411
4412 if (h != NULL)
4413 {
411e1bfb
AM
4414 struct ppc_link_hash_entry *eh;
4415 struct got_entry *ent;
65f38f15 4416
411e1bfb
AM
4417 eh = (struct ppc_link_hash_entry *) h;
4418 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
4419 if (ent->addend == rel->r_addend
e717da7e 4420 && ent->owner == abfd
411e1bfb
AM
4421 && ent->tls_type == tls_type)
4422 break;
4423 if (ent == NULL)
5bd4f169 4424 {
411e1bfb 4425 bfd_size_type amt = sizeof (*ent);
4ce794b7 4426 ent = bfd_alloc (abfd, amt);
411e1bfb 4427 if (ent == NULL)
b34976b6 4428 return FALSE;
411e1bfb
AM
4429 ent->next = eh->elf.got.glist;
4430 ent->addend = rel->r_addend;
e717da7e 4431 ent->owner = abfd;
411e1bfb
AM
4432 ent->tls_type = tls_type;
4433 ent->got.refcount = 0;
4434 eh->elf.got.glist = ent;
5bd4f169 4435 }
411e1bfb 4436 ent->got.refcount += 1;
e7b938ca 4437 eh->tls_mask |= tls_type;
5bd4f169 4438 }
411e1bfb
AM
4439 else
4440 /* This is a global offset table entry for a local symbol. */
4441 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4442 rel->r_addend, tls_type))
4443 return FALSE;
5bd4f169
AM
4444 break;
4445
5bd4f169 4446 case R_PPC64_PLT16_HA:
65f38f15
AM
4447 case R_PPC64_PLT16_HI:
4448 case R_PPC64_PLT16_LO:
4449 case R_PPC64_PLT32:
4450 case R_PPC64_PLT64:
5bd4f169 4451 /* This symbol requires a procedure linkage table entry. We
3fad3c7c
AM
4452 actually build the entry in adjust_dynamic_symbol,
4453 because this might be a case of linking PIC code without
4454 linking in any dynamic objects, in which case we don't
4455 need to generate a procedure linkage table after all. */
5bd4f169
AM
4456 if (h == NULL)
4457 {
4458 /* It does not make sense to have a procedure linkage
3fad3c7c 4459 table entry for a local symbol. */
5bd4f169 4460 bfd_set_error (bfd_error_bad_value);
b34976b6 4461 return FALSE;
5bd4f169 4462 }
411e1bfb
AM
4463 else
4464 if (!update_plt_info (abfd, (struct ppc_link_hash_entry *) h,
4465 rel->r_addend))
4466 return FALSE;
5bd4f169
AM
4467 break;
4468
4469 /* The following relocations don't need to propagate the
4470 relocation if linking a shared object since they are
4471 section relative. */
4472 case R_PPC64_SECTOFF:
4473 case R_PPC64_SECTOFF_LO:
4474 case R_PPC64_SECTOFF_HI:
4475 case R_PPC64_SECTOFF_HA:
4476 case R_PPC64_SECTOFF_DS:
4477 case R_PPC64_SECTOFF_LO_DS:
411e1bfb
AM
4478 case R_PPC64_DTPREL16:
4479 case R_PPC64_DTPREL16_LO:
4480 case R_PPC64_DTPREL16_HI:
4481 case R_PPC64_DTPREL16_HA:
4482 case R_PPC64_DTPREL16_DS:
4483 case R_PPC64_DTPREL16_LO_DS:
4484 case R_PPC64_DTPREL16_HIGHER:
4485 case R_PPC64_DTPREL16_HIGHERA:
4486 case R_PPC64_DTPREL16_HIGHEST:
4487 case R_PPC64_DTPREL16_HIGHESTA:
5bd4f169
AM
4488 break;
4489
ad8e1ba5
AM
4490 /* Nor do these. */
4491 case R_PPC64_TOC16:
4492 case R_PPC64_TOC16_LO:
4493 case R_PPC64_TOC16_HI:
4494 case R_PPC64_TOC16_HA:
4495 case R_PPC64_TOC16_DS:
4496 case R_PPC64_TOC16_LO_DS:
4c52953f 4497 sec->has_toc_reloc = 1;
ad8e1ba5
AM
4498 break;
4499
5bd4f169
AM
4500 /* This relocation describes the C++ object vtable hierarchy.
4501 Reconstruct it for later use during GC. */
4502 case R_PPC64_GNU_VTINHERIT:
c152c796 4503 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 4504 return FALSE;
5bd4f169
AM
4505 break;
4506
4507 /* This relocation describes which C++ vtable entries are actually
4508 used. Record for later use during GC. */
4509 case R_PPC64_GNU_VTENTRY:
c152c796 4510 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
b34976b6 4511 return FALSE;
5bd4f169
AM
4512 break;
4513
721956f4
AM
4514 case R_PPC64_REL14:
4515 case R_PPC64_REL14_BRTAKEN:
4516 case R_PPC64_REL14_BRNTAKEN:
220c76dd
AM
4517 {
4518 asection *dest = NULL;
4519
4520 /* Heuristic: If jumping outside our section, chances are
4521 we are going to need a stub. */
4522 if (h != NULL)
4523 {
4524 /* If the sym is weak it may be overridden later, so
4525 don't assume we know where a weak sym lives. */
4526 if (h->root.type == bfd_link_hash_defined)
4527 dest = h->root.u.def.section;
4528 }
4529 else
4530 dest = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
4531 sec, r_symndx);
4532 if (dest != sec)
4533 htab->has_14bit_branch = 1;
4534 }
721956f4
AM
4535 /* Fall through. */
4536
5d1634d7 4537 case R_PPC64_REL24:
8387904d 4538 if (h != NULL)
5d1634d7
AM
4539 {
4540 /* We may need a .plt entry if the function this reloc
4541 refers to is in a shared lib. */
411e1bfb
AM
4542 if (!update_plt_info (abfd, (struct ppc_link_hash_entry *) h,
4543 rel->r_addend))
4544 return FALSE;
8387904d
AM
4545 if (h == &htab->tls_get_addr->elf
4546 || h == &htab->tls_get_addr_fd->elf)
411e1bfb 4547 sec->has_tls_reloc = 1;
8387904d
AM
4548 else if (htab->tls_get_addr == NULL
4549 && !strncmp (h->root.root.string, ".__tls_get_addr", 15)
a48ebf4d
AM
4550 && (h->root.root.string[15] == 0
4551 || h->root.root.string[15] == '@'))
411e1bfb 4552 {
8387904d
AM
4553 htab->tls_get_addr = (struct ppc_link_hash_entry *) h;
4554 sec->has_tls_reloc = 1;
4555 }
4556 else if (htab->tls_get_addr_fd == NULL
4557 && !strncmp (h->root.root.string, "__tls_get_addr", 14)
4558 && (h->root.root.string[14] == 0
4559 || h->root.root.string[14] == '@'))
4560 {
4561 htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) h;
411e1bfb
AM
4562 sec->has_tls_reloc = 1;
4563 }
4564 }
4565 break;
4566
4567 case R_PPC64_TPREL64:
4568 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
4569 if (info->shared)
4570 info->flags |= DF_STATIC_TLS;
4571 goto dotlstoc;
4572
4573 case R_PPC64_DTPMOD64:
4574 if (rel + 1 < rel_end
4575 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
4576 && rel[1].r_offset == rel->r_offset + 8)
951fd09b 4577 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
411e1bfb 4578 else
951fd09b 4579 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
411e1bfb
AM
4580 goto dotlstoc;
4581
4582 case R_PPC64_DTPREL64:
4583 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
4584 if (rel != relocs
4585 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
4586 && rel[-1].r_offset == rel->r_offset - 8)
4587 /* This is the second reloc of a dtpmod, dtprel pair.
4588 Don't mark with TLS_DTPREL. */
4589 goto dodyn;
4590
4591 dotlstoc:
4592 sec->has_tls_reloc = 1;
4593 if (h != NULL)
4594 {
4595 struct ppc_link_hash_entry *eh;
4596 eh = (struct ppc_link_hash_entry *) h;
e7b938ca 4597 eh->tls_mask |= tls_type;
411e1bfb
AM
4598 }
4599 else
4600 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4601 rel->r_addend, tls_type))
4602 return FALSE;
4603
4604 if (ppc64_elf_section_data (sec)->t_symndx == NULL)
4605 {
e7b938ca 4606 /* One extra to simplify get_tls_mask. */
eea6121a 4607 bfd_size_type amt = sec->size * sizeof (unsigned) / 8 + 1;
4ce794b7 4608 ppc64_elf_section_data (sec)->t_symndx = bfd_zalloc (abfd, amt);
411e1bfb
AM
4609 if (ppc64_elf_section_data (sec)->t_symndx == NULL)
4610 return FALSE;
4611 }
4612 BFD_ASSERT (rel->r_offset % 8 == 0);
4613 ppc64_elf_section_data (sec)->t_symndx[rel->r_offset / 8] = r_symndx;
951fd09b
AM
4614
4615 /* Mark the second slot of a GD or LD entry.
4616 -1 to indicate GD and -2 to indicate LD. */
4617 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
4618 ppc64_elf_section_data (sec)->t_symndx[rel->r_offset / 8 + 1] = -1;
4619 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
4620 ppc64_elf_section_data (sec)->t_symndx[rel->r_offset / 8 + 1] = -2;
411e1bfb
AM
4621 goto dodyn;
4622
4623 case R_PPC64_TPREL16:
4624 case R_PPC64_TPREL16_LO:
4625 case R_PPC64_TPREL16_HI:
4626 case R_PPC64_TPREL16_HA:
4627 case R_PPC64_TPREL16_DS:
4628 case R_PPC64_TPREL16_LO_DS:
4629 case R_PPC64_TPREL16_HIGHER:
4630 case R_PPC64_TPREL16_HIGHERA:
4631 case R_PPC64_TPREL16_HIGHEST:
4632 case R_PPC64_TPREL16_HIGHESTA:
4633 if (info->shared)
4634 {
4635 info->flags |= DF_STATIC_TLS;
4636 goto dodyn;
5d1634d7
AM
4637 }
4638 break;
4639
e86ce104 4640 case R_PPC64_ADDR64:
1e2f5b6e 4641 if (opd_sym_map != NULL
1e2f5b6e 4642 && rel + 1 < rel_end
4ce794b7 4643 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
1e2f5b6e 4644 {
8387904d
AM
4645 if (h != NULL)
4646 {
4647 if (h->root.root.string[0] == '.'
4648 && h->root.root.string[1] != 0
4649 && get_fdh ((struct ppc_link_hash_entry *) h, htab))
4650 ;
4651 else
4652 ((struct ppc_link_hash_entry *) h)->is_func = 1;
4653 }
4654 else
4655 {
4656 asection *s;
1e2f5b6e 4657
8387904d
AM
4658 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec, sec,
4659 r_symndx);
4660 if (s == NULL)
4661 return FALSE;
4662 else if (s != sec)
3f764659 4663 opd_sym_map[rel->r_offset / 8] = s;
8387904d 4664 }
1e2f5b6e 4665 }
e86ce104
AM
4666 /* Fall through. */
4667
04c9666a 4668 case R_PPC64_REL30:
5bd4f169 4669 case R_PPC64_REL32:
04c9666a 4670 case R_PPC64_REL64:
65f38f15
AM
4671 case R_PPC64_ADDR14:
4672 case R_PPC64_ADDR14_BRNTAKEN:
4673 case R_PPC64_ADDR14_BRTAKEN:
4674 case R_PPC64_ADDR16:
4675 case R_PPC64_ADDR16_DS:
4676 case R_PPC64_ADDR16_HA:
4677 case R_PPC64_ADDR16_HI:
4678 case R_PPC64_ADDR16_HIGHER:
4679 case R_PPC64_ADDR16_HIGHERA:
4680 case R_PPC64_ADDR16_HIGHEST:
4681 case R_PPC64_ADDR16_HIGHESTA:
4682 case R_PPC64_ADDR16_LO:
4683 case R_PPC64_ADDR16_LO_DS:
4684 case R_PPC64_ADDR24:
65f38f15 4685 case R_PPC64_ADDR32:
65f38f15
AM
4686 case R_PPC64_UADDR16:
4687 case R_PPC64_UADDR32:
4688 case R_PPC64_UADDR64:
5bd4f169 4689 case R_PPC64_TOC:
81848ca0
AM
4690 if (h != NULL && !info->shared)
4691 /* We may need a copy reloc. */
f5385ebf 4692 h->non_got_ref = 1;
81848ca0 4693
41bd81ab 4694 /* Don't propagate .opd relocs. */
1e2f5b6e 4695 if (NO_OPD_RELOCS && opd_sym_map != NULL)
e86ce104 4696 break;
e86ce104 4697
65f38f15
AM
4698 /* If we are creating a shared library, and this is a reloc
4699 against a global symbol, or a non PC relative reloc
4700 against a local symbol, then we need to copy the reloc
4701 into the shared library. However, if we are linking with
4702 -Bsymbolic, we do not need to copy a reloc against a
4703 global symbol which is defined in an object we are
4704 including in the link (i.e., DEF_REGULAR is set). At
4705 this point we have not seen all the input files, so it is
4706 possible that DEF_REGULAR is not set now but will be set
4707 later (it is never cleared). In case of a weak definition,
4708 DEF_REGULAR may be cleared later by a strong definition in
4709 a shared library. We account for that possibility below by
f4656909 4710 storing information in the dyn_relocs field of the hash
65f38f15
AM
4711 table entry. A similar situation occurs when creating
4712 shared libraries and symbol visibility changes render the
4713 symbol local.
4714
4715 If on the other hand, we are creating an executable, we
4716 may need to keep relocations for symbols satisfied by a
4717 dynamic library if we manage to avoid copy relocs for the
4718 symbol. */
411e1bfb 4719 dodyn:
65f38f15 4720 if ((info->shared
411e1bfb 4721 && (MUST_BE_DYN_RELOC (r_type)
65f38f15
AM
4722 || (h != NULL
4723 && (! info->symbolic
4724 || h->root.type == bfd_link_hash_defweak
f5385ebf 4725 || !h->def_regular))))
f4656909
AM
4726 || (ELIMINATE_COPY_RELOCS
4727 && !info->shared
65f38f15
AM
4728 && h != NULL
4729 && (h->root.type == bfd_link_hash_defweak
f5385ebf 4730 || !h->def_regular)))
5bd4f169 4731 {
ec338859
AM
4732 struct ppc_dyn_relocs *p;
4733 struct ppc_dyn_relocs **head;
4734
65f38f15
AM
4735 /* We must copy these reloc types into the output file.
4736 Create a reloc section in dynobj and make room for
4737 this reloc. */
5bd4f169
AM
4738 if (sreloc == NULL)
4739 {
4740 const char *name;
65f38f15 4741 bfd *dynobj;
5bd4f169
AM
4742
4743 name = (bfd_elf_string_from_elf_section
4744 (abfd,
4745 elf_elfheader (abfd)->e_shstrndx,
4746 elf_section_data (sec)->rel_hdr.sh_name));
4747 if (name == NULL)
b34976b6 4748 return FALSE;
5bd4f169 4749
65f38f15
AM
4750 if (strncmp (name, ".rela", 5) != 0
4751 || strcmp (bfd_get_section_name (abfd, sec),
4752 name + 5) != 0)
4753 {
4754 (*_bfd_error_handler)
d003868e
AM
4755 (_("%B: bad relocation section name `%s\'"),
4756 abfd, name);
5d1634d7 4757 bfd_set_error (bfd_error_bad_value);
65f38f15
AM
4758 }
4759
65f38f15 4760 dynobj = htab->elf.dynobj;
5bd4f169
AM
4761 sreloc = bfd_get_section_by_name (dynobj, name);
4762 if (sreloc == NULL)
4763 {
4764 flagword flags;
4765
5bd4f169 4766 flags = (SEC_HAS_CONTENTS | SEC_READONLY
77623a34
AM
4767 | SEC_IN_MEMORY | SEC_LINKER_CREATED
4768 | SEC_ALLOC | SEC_LOAD);
3496cb2a
L
4769 sreloc = bfd_make_section_with_flags (dynobj,
4770 name,
4771 flags);
5bd4f169 4772 if (sreloc == NULL
65f38f15 4773 || ! bfd_set_section_alignment (dynobj, sreloc, 3))
b34976b6 4774 return FALSE;
5bd4f169 4775 }
65f38f15 4776 elf_section_data (sec)->sreloc = sreloc;
5bd4f169
AM
4777 }
4778
65f38f15
AM
4779 /* If this is a global symbol, we count the number of
4780 relocations we need for this symbol. */
4781 if (h != NULL)
4782 {
ec338859 4783 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
65f38f15
AM
4784 }
4785 else
4786 {
ec338859
AM
4787 /* Track dynamic relocs needed for local syms too.
4788 We really need local syms available to do this
4789 easily. Oh well. */
4790
4791 asection *s;
6edfbbad
DJ
4792 void *vpp;
4793
ec338859
AM
4794 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
4795 sec, r_symndx);
4796 if (s == NULL)
b34976b6 4797 return FALSE;
ec338859 4798
6edfbbad
DJ
4799 vpp = &elf_section_data (s)->local_dynrel;
4800 head = (struct ppc_dyn_relocs **) vpp;
65f38f15 4801 }
ec338859
AM
4802
4803 p = *head;
4804 if (p == NULL || p->sec != sec)
4805 {
4ce794b7 4806 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
ec338859 4807 if (p == NULL)
b34976b6 4808 return FALSE;
ec338859
AM
4809 p->next = *head;
4810 *head = p;
4811 p->sec = sec;
4812 p->count = 0;
4813 p->pc_count = 0;
4814 }
4815
4816 p->count += 1;
411e1bfb 4817 if (!MUST_BE_DYN_RELOC (r_type))
ec338859 4818 p->pc_count += 1;
65f38f15 4819 }
5bd4f169 4820 break;
65f38f15
AM
4821
4822 default:
96e0dda4 4823 break;
5bd4f169
AM
4824 }
4825 }
4826
b34976b6 4827 return TRUE;
5bd4f169
AM
4828}
4829
8387904d
AM
4830/* OFFSET in OPD_SEC specifies a function descriptor. Return the address
4831 of the code entry point, and its section. */
4832
4833static bfd_vma
4834opd_entry_value (asection *opd_sec,
4835 bfd_vma offset,
4836 asection **code_sec,
4837 bfd_vma *code_off)
4838{
4839 bfd *opd_bfd = opd_sec->owner;
8860955f 4840 Elf_Internal_Rela *relocs;
8387904d 4841 Elf_Internal_Rela *lo, *hi, *look;
645ea6a9 4842 bfd_vma val;
8387904d 4843
4b85d634
AM
4844 /* No relocs implies we are linking a --just-symbols object. */
4845 if (opd_sec->reloc_count == 0)
4846 {
4847 bfd_vma val;
4848
4849 if (!bfd_get_section_contents (opd_bfd, opd_sec, &val, offset, 8))
4850 return (bfd_vma) -1;
3b36f7e6 4851
4b85d634
AM
4852 if (code_sec != NULL)
4853 {
4854 asection *sec, *likely = NULL;
4855 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
4856 if (sec->vma <= val
4857 && (sec->flags & SEC_LOAD) != 0
4858 && (sec->flags & SEC_ALLOC) != 0)
4859 likely = sec;
4860 if (likely != NULL)
4861 {
4862 *code_sec = likely;
4863 if (code_off != NULL)
4864 *code_off = val - likely->vma;
4865 }
4866 }
4867 return val;
4868 }
4869
8860955f
AM
4870 relocs = ppc64_elf_tdata (opd_bfd)->opd_relocs;
4871 if (relocs == NULL)
4872 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
645ea6a9 4873
8387904d 4874 /* Go find the opd reloc at the sym address. */
8860955f 4875 lo = relocs;
8387904d
AM
4876 BFD_ASSERT (lo != NULL);
4877 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
645ea6a9 4878 val = (bfd_vma) -1;
8387904d
AM
4879 while (lo < hi)
4880 {
4881 look = lo + (hi - lo) / 2;
4882 if (look->r_offset < offset)
4883 lo = look + 1;
4884 else if (look->r_offset > offset)
4885 hi = look;
4886 else
4887 {
4888 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (opd_bfd)->symtab_hdr;
4889 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
4890 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
4891 {
4892 unsigned long symndx = ELF64_R_SYM (look->r_info);
8387904d
AM
4893 asection *sec;
4894
4895 if (symndx < symtab_hdr->sh_info)
4896 {
4897 Elf_Internal_Sym *sym;
4898
4899 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
4900 if (sym == NULL)
4901 {
4902 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
4903 symtab_hdr->sh_info,
4904 0, NULL, NULL, NULL);
4905 if (sym == NULL)
645ea6a9 4906 break;
8387904d
AM
4907 symtab_hdr->contents = (bfd_byte *) sym;
4908 }
4909
4910 sym += symndx;
4911 val = sym->st_value;
4912 sec = NULL;
4913 if ((sym->st_shndx != SHN_UNDEF
4914 && sym->st_shndx < SHN_LORESERVE)
4915 || sym->st_shndx > SHN_HIRESERVE)
4916 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
4917 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
4918 }
4919 else
4920 {
4921 struct elf_link_hash_entry **sym_hashes;
4922 struct elf_link_hash_entry *rh;
4923
4924 sym_hashes = elf_sym_hashes (opd_bfd);
4925 rh = sym_hashes[symndx - symtab_hdr->sh_info];
4926 while (rh->root.type == bfd_link_hash_indirect
4927 || rh->root.type == bfd_link_hash_warning)
4928 rh = ((struct elf_link_hash_entry *) rh->root.u.i.link);
4929 BFD_ASSERT (rh->root.type == bfd_link_hash_defined
4930 || rh->root.type == bfd_link_hash_defweak);
4931 val = rh->root.u.def.value;
4932 sec = rh->root.u.def.section;
4933 }
4934 val += look->r_addend;
4935 if (code_off != NULL)
4936 *code_off = val;
4937 if (code_sec != NULL)
4938 *code_sec = sec;
4939 if (sec != NULL && sec->output_section != NULL)
4940 val += sec->output_section->vma + sec->output_offset;
8387904d
AM
4941 }
4942 break;
4943 }
4944 }
645ea6a9 4945
645ea6a9 4946 return val;
8387904d
AM
4947}
4948
64d03ab5
AM
4949/* Mark sections containing dynamically referenced symbols. When
4950 building shared libraries, we must assume that any visible symbol is
4951 referenced. */
4952
4953static bfd_boolean
4954ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
4955{
4956 struct bfd_link_info *info = (struct bfd_link_info *) inf;
4957 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
4958
4959 if (eh->elf.root.type == bfd_link_hash_warning)
4960 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
4961
4962 /* Dynamic linking info is on the func descriptor sym. */
4963 if (eh->oh != NULL
4964 && eh->oh->is_func_descriptor
4965 && (eh->oh->elf.root.type == bfd_link_hash_defined
4966 || eh->oh->elf.root.type == bfd_link_hash_defweak))
4967 eh = eh->oh;
4968
4969 if ((eh->elf.root.type == bfd_link_hash_defined
4970 || eh->elf.root.type == bfd_link_hash_defweak)
4971 && (eh->elf.ref_dynamic
4972 || (!info->executable
4973 && eh->elf.def_regular
4974 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
4975 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN)))
4976 {
4977 asection *code_sec;
4978
4979 eh->elf.root.u.def.section->flags |= SEC_KEEP;
4980
4981 /* Function descriptor syms cause the associated
4982 function code sym section to be marked. */
4983 if (eh->is_func_descriptor
4984 && (eh->oh->elf.root.type == bfd_link_hash_defined
4985 || eh->oh->elf.root.type == bfd_link_hash_defweak))
4986 eh->oh->elf.root.u.def.section->flags |= SEC_KEEP;
4987 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
4988 && opd_entry_value (eh->elf.root.u.def.section,
4989 eh->elf.root.u.def.value,
4990 &code_sec, NULL) != (bfd_vma) -1)
4991 code_sec->flags |= SEC_KEEP;
4992 }
4993
4994 return TRUE;
4995}
4996
5bd4f169
AM
4997/* Return the section that should be marked against GC for a given
4998 relocation. */
4999
5000static asection *
4ce794b7 5001ppc64_elf_gc_mark_hook (asection *sec,
ccfa59ea 5002 struct bfd_link_info *info,
4ce794b7
AM
5003 Elf_Internal_Rela *rel,
5004 struct elf_link_hash_entry *h,
5005 Elf_Internal_Sym *sym)
5bd4f169 5006{
ccfa59ea
AM
5007 asection *rsec;
5008
5009 /* First mark all our entry sym sections. */
5010 if (info->gc_sym_list != NULL)
5011 {
5012 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5013 struct bfd_sym_chain *sym = info->gc_sym_list;
5014
5015 info->gc_sym_list = NULL;
b56cbdb9 5016 for (; sym != NULL; sym = sym->next)
ccfa59ea
AM
5017 {
5018 struct ppc_link_hash_entry *eh;
5019
5020 eh = (struct ppc_link_hash_entry *)
5021 elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, FALSE);
5022 if (eh == NULL)
5023 continue;
5024 if (eh->elf.root.type != bfd_link_hash_defined
5025 && eh->elf.root.type != bfd_link_hash_defweak)
5026 continue;
5027
c4f68ce3
AM
5028 if (eh->is_func_descriptor
5029 && (eh->oh->elf.root.type == bfd_link_hash_defined
5030 || eh->oh->elf.root.type == bfd_link_hash_defweak))
ccfa59ea 5031 rsec = eh->oh->elf.root.u.def.section;
8387904d
AM
5032 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5033 && opd_entry_value (eh->elf.root.u.def.section,
5034 eh->elf.root.u.def.value,
5035 &rsec, NULL) != (bfd_vma) -1)
5036 ;
ccfa59ea
AM
5037 else
5038 continue;
5039
5040 if (!rsec->gc_mark)
5041 _bfd_elf_gc_mark (info, rsec, ppc64_elf_gc_mark_hook);
5042
5043 rsec = eh->elf.root.u.def.section;
5044 if (!rsec->gc_mark)
5045 _bfd_elf_gc_mark (info, rsec, ppc64_elf_gc_mark_hook);
ccfa59ea 5046 }
ccfa59ea
AM
5047 }
5048
5049 /* Syms return NULL if we're marking .opd, so we avoid marking all
5050 function sections, as all functions are referenced in .opd. */
5051 rsec = NULL;
5052 if (get_opd_info (sec) != NULL)
5053 return rsec;
1e2f5b6e 5054
5bd4f169
AM
5055 if (h != NULL)
5056 {
04c9666a 5057 enum elf_ppc64_reloc_type r_type;
ccfa59ea 5058 struct ppc_link_hash_entry *eh;
a33d1f77 5059
4ce794b7 5060 r_type = ELF64_R_TYPE (rel->r_info);
a33d1f77 5061 switch (r_type)
5bd4f169
AM
5062 {
5063 case R_PPC64_GNU_VTINHERIT:
5064 case R_PPC64_GNU_VTENTRY:
5065 break;
5066
5067 default:
5068 switch (h->root.type)
5069 {
5070 case bfd_link_hash_defined:
5071 case bfd_link_hash_defweak:
ccfa59ea 5072 eh = (struct ppc_link_hash_entry *) h;
c4f68ce3
AM
5073 if (eh->oh != NULL
5074 && eh->oh->is_func_descriptor
5075 && (eh->oh->elf.root.type == bfd_link_hash_defined
5076 || eh->oh->elf.root.type == bfd_link_hash_defweak))
ccfa59ea 5077 eh = eh->oh;
1e2f5b6e
AM
5078
5079 /* Function descriptor syms cause the associated
5080 function code sym section to be marked. */
c4f68ce3
AM
5081 if (eh->is_func_descriptor
5082 && (eh->oh->elf.root.type == bfd_link_hash_defined
5083 || eh->oh->elf.root.type == bfd_link_hash_defweak))
ccfa59ea
AM
5084 {
5085 /* They also mark their opd section. */
5086 if (!eh->elf.root.u.def.section->gc_mark)
5087 _bfd_elf_gc_mark (info, eh->elf.root.u.def.section,
5088 ppc64_elf_gc_mark_hook);
5089
5090 rsec = eh->oh->elf.root.u.def.section;
5091 }
8387904d
AM
5092 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5093 && opd_entry_value (eh->elf.root.u.def.section,
5094 eh->elf.root.u.def.value,
5095 &rsec, NULL) != (bfd_vma) -1)
5096 {
5097 if (!eh->elf.root.u.def.section->gc_mark)
5098 _bfd_elf_gc_mark (info, eh->elf.root.u.def.section,
5099 ppc64_elf_gc_mark_hook);
5100 }
ccfa59ea 5101 else
1e2f5b6e
AM
5102 rsec = h->root.u.def.section;
5103 break;
5bd4f169
AM
5104
5105 case bfd_link_hash_common:
1e2f5b6e
AM
5106 rsec = h->root.u.c.p->section;
5107 break;
5bd4f169
AM
5108
5109 default:
5110 break;
5111 }
5112 }
5113 }
5114 else
5115 {
1e2f5b6e
AM
5116 asection **opd_sym_section;
5117
5118 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
ccfa59ea 5119 opd_sym_section = get_opd_info (rsec);
1e2f5b6e 5120 if (opd_sym_section != NULL)
ccfa59ea
AM
5121 {
5122 if (!rsec->gc_mark)
5123 _bfd_elf_gc_mark (info, rsec, ppc64_elf_gc_mark_hook);
5124
b4f4e59f 5125 rsec = opd_sym_section[(sym->st_value + rel->r_addend) / 8];
ccfa59ea 5126 }
5bd4f169
AM
5127 }
5128
1e2f5b6e 5129 return rsec;
5bd4f169
AM
5130}
5131
65f38f15
AM
5132/* Update the .got, .plt. and dynamic reloc reference counts for the
5133 section being removed. */
5bd4f169 5134
b34976b6 5135static bfd_boolean
4ce794b7
AM
5136ppc64_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
5137 asection *sec, const Elf_Internal_Rela *relocs)
5bd4f169 5138{
411e1bfb 5139 struct ppc_link_hash_table *htab;
5bd4f169
AM
5140 Elf_Internal_Shdr *symtab_hdr;
5141 struct elf_link_hash_entry **sym_hashes;
411e1bfb 5142 struct got_entry **local_got_ents;
5bd4f169 5143 const Elf_Internal_Rela *rel, *relend;
5bd4f169 5144
680a3378
AM
5145 if ((sec->flags & SEC_ALLOC) == 0)
5146 return TRUE;
5147
ec338859
AM
5148 elf_section_data (sec)->local_dynrel = NULL;
5149
411e1bfb 5150 htab = ppc_hash_table (info);
5bd4f169
AM
5151 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5152 sym_hashes = elf_sym_hashes (abfd);
411e1bfb 5153 local_got_ents = elf_local_got_ents (abfd);
5bd4f169
AM
5154
5155 relend = relocs + sec->reloc_count;
5156 for (rel = relocs; rel < relend; rel++)
a33d1f77
AM
5157 {
5158 unsigned long r_symndx;
04c9666a 5159 enum elf_ppc64_reloc_type r_type;
58ac9f71 5160 struct elf_link_hash_entry *h = NULL;
411e1bfb 5161 char tls_type = 0;
5bd4f169 5162
a33d1f77 5163 r_symndx = ELF64_R_SYM (rel->r_info);
4ce794b7 5164 r_type = ELF64_R_TYPE (rel->r_info);
58ac9f71
AM
5165 if (r_symndx >= symtab_hdr->sh_info)
5166 {
5167 struct ppc_link_hash_entry *eh;
5168 struct ppc_dyn_relocs **pp;
5169 struct ppc_dyn_relocs *p;
5170
5171 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3eb128b2
AM
5172 while (h->root.type == bfd_link_hash_indirect
5173 || h->root.type == bfd_link_hash_warning)
5174 h = (struct elf_link_hash_entry *) h->root.u.i.link;
58ac9f71
AM
5175 eh = (struct ppc_link_hash_entry *) h;
5176
5177 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
5178 if (p->sec == sec)
5179 {
5180 /* Everything must go for SEC. */
5181 *pp = p->next;
5182 break;
5183 }
5184 }
5185
a33d1f77
AM
5186 switch (r_type)
5187 {
411e1bfb
AM
5188 case R_PPC64_GOT_TLSLD16:
5189 case R_PPC64_GOT_TLSLD16_LO:
5190 case R_PPC64_GOT_TLSLD16_HI:
5191 case R_PPC64_GOT_TLSLD16_HA:
e717da7e 5192 ppc64_tlsld_got (abfd)->refcount -= 1;
951fd09b 5193 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
5194 goto dogot;
5195
5196 case R_PPC64_GOT_TLSGD16:
5197 case R_PPC64_GOT_TLSGD16_LO:
5198 case R_PPC64_GOT_TLSGD16_HI:
5199 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 5200 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
5201 goto dogot;
5202
5203 case R_PPC64_GOT_TPREL16_DS:
5204 case R_PPC64_GOT_TPREL16_LO_DS:
5205 case R_PPC64_GOT_TPREL16_HI:
5206 case R_PPC64_GOT_TPREL16_HA:
5207 tls_type = TLS_TLS | TLS_TPREL;
5208 goto dogot;
5209
5210 case R_PPC64_GOT_DTPREL16_DS:
5211 case R_PPC64_GOT_DTPREL16_LO_DS:
5212 case R_PPC64_GOT_DTPREL16_HI:
5213 case R_PPC64_GOT_DTPREL16_HA:
5214 tls_type = TLS_TLS | TLS_DTPREL;
5215 goto dogot;
5216
a33d1f77
AM
5217 case R_PPC64_GOT16:
5218 case R_PPC64_GOT16_DS:
5219 case R_PPC64_GOT16_HA:
5220 case R_PPC64_GOT16_HI:
5221 case R_PPC64_GOT16_LO:
5222 case R_PPC64_GOT16_LO_DS:
411e1bfb
AM
5223 dogot:
5224 {
5225 struct got_entry *ent;
5226
58ac9f71
AM
5227 if (h != NULL)
5228 ent = h->got.glist;
411e1bfb
AM
5229 else
5230 ent = local_got_ents[r_symndx];
5231
5232 for (; ent != NULL; ent = ent->next)
5233 if (ent->addend == rel->r_addend
e717da7e 5234 && ent->owner == abfd
411e1bfb
AM
5235 && ent->tls_type == tls_type)
5236 break;
5237 if (ent == NULL)
5238 abort ();
5239 if (ent->got.refcount > 0)
5240 ent->got.refcount -= 1;
5241 }
a33d1f77 5242 break;
65f38f15 5243
a33d1f77
AM
5244 case R_PPC64_PLT16_HA:
5245 case R_PPC64_PLT16_HI:
5246 case R_PPC64_PLT16_LO:
5247 case R_PPC64_PLT32:
5248 case R_PPC64_PLT64:
721956f4
AM
5249 case R_PPC64_REL14:
5250 case R_PPC64_REL14_BRNTAKEN:
5251 case R_PPC64_REL14_BRTAKEN:
5d1634d7 5252 case R_PPC64_REL24:
58ac9f71 5253 if (h != NULL)
5d1634d7 5254 {
411e1bfb
AM
5255 struct plt_entry *ent;
5256
411e1bfb
AM
5257 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
5258 if (ent->addend == rel->r_addend)
5259 break;
5260 if (ent == NULL)
5261 abort ();
5262 if (ent->plt.refcount > 0)
5263 ent->plt.refcount -= 1;
5d1634d7 5264 }
e86ce104 5265 break;
5d1634d7 5266
a33d1f77
AM
5267 default:
5268 break;
5269 }
5270 }
b34976b6 5271 return TRUE;
5bd4f169
AM
5272}
5273
deb0e272
AM
5274/* The maximum size of .sfpr. */
5275#define SFPR_MAX (218*4)
5276
5277struct sfpr_def_parms
5278{
699733f6
AM
5279 const char name[12];
5280 unsigned char lo, hi;
deb0e272
AM
5281 bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
5282 bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
5283};
5284
5285/* Auto-generate _save*, _rest* functions in .sfpr. */
5286
5287static unsigned int
5288sfpr_define (struct bfd_link_info *info, const struct sfpr_def_parms *parm)
5289{
5290 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5291 unsigned int i;
5292 size_t len = strlen (parm->name);
5293 bfd_boolean writing = FALSE;
699733f6 5294 char sym[16];
deb0e272
AM
5295
5296 memcpy (sym, parm->name, len);
5297 sym[len + 2] = 0;
5298
5299 for (i = parm->lo; i <= parm->hi; i++)
5300 {
5301 struct elf_link_hash_entry *h;
5302
5303 sym[len + 0] = i / 10 + '0';
5304 sym[len + 1] = i % 10 + '0';
5305 h = elf_link_hash_lookup (&htab->elf, sym, FALSE, FALSE, TRUE);
5306 if (h != NULL
f5385ebf 5307 && !h->def_regular)
deb0e272
AM
5308 {
5309 h->root.type = bfd_link_hash_defined;
5310 h->root.u.def.section = htab->sfpr;
5311 h->root.u.def.value = htab->sfpr->size;
5312 h->type = STT_FUNC;
f5385ebf 5313 h->def_regular = 1;
deb0e272
AM
5314 _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
5315 writing = TRUE;
5316 if (htab->sfpr->contents == NULL)
5317 {
5318 htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
5319 if (htab->sfpr->contents == NULL)
5320 return FALSE;
5321 }
5322 }
5323 if (writing)
5324 {
5325 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
5326 if (i != parm->hi)
5327 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
5328 else
5329 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
5330 htab->sfpr->size = p - htab->sfpr->contents;
5331 }
5332 }
5333
5334 return TRUE;
5335}
5336
5337static bfd_byte *
5338savegpr0 (bfd *abfd, bfd_byte *p, int r)
5339{
5340 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5341 return p + 4;
5342}
5343
5344static bfd_byte *
5345savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
5346{
5347 p = savegpr0 (abfd, p, r);
5348 bfd_put_32 (abfd, STD_R0_0R1 + 16, p);
5349 p = p + 4;
5350 bfd_put_32 (abfd, BLR, p);
5351 return p + 4;
5352}
5353
5354static bfd_byte *
5355restgpr0 (bfd *abfd, bfd_byte *p, int r)
5356{
5357 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5358 return p + 4;
5359}
5360
5361static bfd_byte *
5362restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
5363{
5364 bfd_put_32 (abfd, LD_R0_0R1 + 16, p);
5365 p = p + 4;
5366 p = restgpr0 (abfd, p, r);
5367 bfd_put_32 (abfd, MTLR_R0, p);
5368 p = p + 4;
5369 if (r == 29)
5370 {
5371 p = restgpr0 (abfd, p, 30);
5372 p = restgpr0 (abfd, p, 31);
5373 }
5374 bfd_put_32 (abfd, BLR, p);
5375 return p + 4;
5376}
5377
5378static bfd_byte *
5379savegpr1 (bfd *abfd, bfd_byte *p, int r)
5380{
5381 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5382 return p + 4;
5383}
5384
5385static bfd_byte *
5386savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
5387{
5388 p = savegpr1 (abfd, p, r);
5389 bfd_put_32 (abfd, BLR, p);
5390 return p + 4;
5391}
5392
5393static bfd_byte *
5394restgpr1 (bfd *abfd, bfd_byte *p, int r)
5395{
5396 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5397 return p + 4;
5398}
5399
5400static bfd_byte *
5401restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
5402{
5403 p = restgpr1 (abfd, p, r);
5404 bfd_put_32 (abfd, BLR, p);
5405 return p + 4;
5406}
5407
5408static bfd_byte *
5409savefpr (bfd *abfd, bfd_byte *p, int r)
5410{
5411 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5412 return p + 4;
5413}
5414
5415static bfd_byte *
5416savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
5417{
5418 p = savefpr (abfd, p, r);
5419 bfd_put_32 (abfd, STD_R0_0R1 + 16, p);
5420 p = p + 4;
5421 bfd_put_32 (abfd, BLR, p);
5422 return p + 4;
5423}
5424
5425static bfd_byte *
5426restfpr (bfd *abfd, bfd_byte *p, int r)
5427{
5428 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5429 return p + 4;
5430}
5431
5432static bfd_byte *
5433restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
5434{
5435 bfd_put_32 (abfd, LD_R0_0R1 + 16, p);
5436 p = p + 4;
5437 p = restfpr (abfd, p, r);
5438 bfd_put_32 (abfd, MTLR_R0, p);
5439 p = p + 4;
5440 if (r == 29)
5441 {
5442 p = restfpr (abfd, p, 30);
5443 p = restfpr (abfd, p, 31);
5444 }
5445 bfd_put_32 (abfd, BLR, p);
5446 return p + 4;
5447}
5448
5449static bfd_byte *
5450savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
5451{
5452 p = savefpr (abfd, p, r);
5453 bfd_put_32 (abfd, BLR, p);
5454 return p + 4;
5455}
5456
5457static bfd_byte *
5458restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
5459{
5460 p = restfpr (abfd, p, r);
5461 bfd_put_32 (abfd, BLR, p);
5462 return p + 4;
5463}
5464
5465static bfd_byte *
5466savevr (bfd *abfd, bfd_byte *p, int r)
5467{
5468 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
5469 p = p + 4;
5470 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
5471 return p + 4;
5472}
5473
5474static bfd_byte *
5475savevr_tail (bfd *abfd, bfd_byte *p, int r)
5476{
5477 p = savevr (abfd, p, r);
5478 bfd_put_32 (abfd, BLR, p);
5479 return p + 4;
5480}
5481
5482static bfd_byte *
5483restvr (bfd *abfd, bfd_byte *p, int r)
5484{
5485 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
5486 p = p + 4;
5487 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
5488 return p + 4;
5489}
5490
5491static bfd_byte *
5492restvr_tail (bfd *abfd, bfd_byte *p, int r)
5493{
5494 p = restvr (abfd, p, r);
5495 bfd_put_32 (abfd, BLR, p);
5496 return p + 4;
5497}
5498
e86ce104
AM
5499/* Called via elf_link_hash_traverse to transfer dynamic linking
5500 information on function code symbol entries to their corresponding
5501 function descriptor symbol entries. */
deb0e272 5502
b34976b6 5503static bfd_boolean
4ce794b7 5504func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
5bd4f169 5505{
e86ce104 5506 struct bfd_link_info *info;
65f38f15 5507 struct ppc_link_hash_table *htab;
411e1bfb 5508 struct plt_entry *ent;
50bc7936
AM
5509 struct ppc_link_hash_entry *fh;
5510 struct ppc_link_hash_entry *fdh;
5511 bfd_boolean force_local;
5bd4f169 5512
50bc7936
AM
5513 fh = (struct ppc_link_hash_entry *) h;
5514 if (fh->elf.root.type == bfd_link_hash_indirect)
b34976b6 5515 return TRUE;
e86ce104 5516
50bc7936
AM
5517 if (fh->elf.root.type == bfd_link_hash_warning)
5518 fh = (struct ppc_link_hash_entry *) fh->elf.root.u.i.link;
e92d460e 5519
4ce794b7 5520 info = inf;
65f38f15 5521 htab = ppc_hash_table (info);
5bd4f169 5522
c09bdfe5
AM
5523 /* Resolve undefined references to dot-symbols as the value
5524 in the function descriptor, if we have one in a regular object.
5525 This is to satisfy cases like ".quad .foo". Calls to functions
5526 in dynamic objects are handled elsewhere. */
5527 if (fh->elf.root.type == bfd_link_hash_undefweak
5528 && fh->was_undefined
5529 && (fh->oh->elf.root.type == bfd_link_hash_defined
5530 || fh->oh->elf.root.type == bfd_link_hash_defweak)
5531 && get_opd_info (fh->oh->elf.root.u.def.section) != NULL
5532 && opd_entry_value (fh->oh->elf.root.u.def.section,
5533 fh->oh->elf.root.u.def.value,
5534 &fh->elf.root.u.def.section,
5535 &fh->elf.root.u.def.value) != (bfd_vma) -1)
5536 {
5537 fh->elf.root.type = fh->oh->elf.root.type;
f5385ebf 5538 fh->elf.forced_local = 1;
c09bdfe5
AM
5539 }
5540
e86ce104
AM
5541 /* If this is a function code symbol, transfer dynamic linking
5542 information to the function descriptor symbol. */
50bc7936 5543 if (!fh->is_func)
b34976b6 5544 return TRUE;
e86ce104 5545
50bc7936 5546 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
411e1bfb
AM
5547 if (ent->plt.refcount > 0)
5548 break;
50bc7936
AM
5549 if (ent == NULL
5550 || fh->elf.root.root.string[0] != '.'
5551 || fh->elf.root.root.string[1] == '\0')
5552 return TRUE;
5bd4f169 5553
50bc7936
AM
5554 /* Find the corresponding function descriptor symbol. Create it
5555 as undefined if necessary. */
5bd4f169 5556
50bc7936
AM
5557 fdh = get_fdh (fh, htab);
5558 if (fdh != NULL)
5559 while (fdh->elf.root.type == bfd_link_hash_indirect
5560 || fdh->elf.root.type == bfd_link_hash_warning)
5561 fdh = (struct ppc_link_hash_entry *) fdh->elf.root.u.i.link;
5bd4f169 5562
50bc7936
AM
5563 if (fdh == NULL
5564 && info->shared
5565 && (fh->elf.root.type == bfd_link_hash_undefined
5566 || fh->elf.root.type == bfd_link_hash_undefweak))
5567 {
908b32fc 5568 fdh = make_fdh (info, fh);
bb700d78
AM
5569 if (fdh == NULL)
5570 return FALSE;
50bc7936 5571 }
648cca2c 5572
908b32fc 5573 /* Fake function descriptors are made undefweak. If the function
433817dd
AM
5574 code symbol is strong undefined, make the fake sym the same.
5575 If the function code symbol is defined, then force the fake
5576 descriptor local; We can't support overriding of symbols in a
5577 shared library on a fake descriptor. */
908b32fc
AM
5578
5579 if (fdh != NULL
5580 && fdh->fake
433817dd 5581 && fdh->elf.root.type == bfd_link_hash_undefweak)
908b32fc 5582 {
433817dd
AM
5583 if (fh->elf.root.type == bfd_link_hash_undefined)
5584 {
5585 fdh->elf.root.type = bfd_link_hash_undefined;
5586 bfd_link_add_undef (&htab->elf.root, &fdh->elf.root);
5587 }
5588 else if (fh->elf.root.type == bfd_link_hash_defined
5589 || fh->elf.root.type == bfd_link_hash_defweak)
5590 {
5591 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
5592 }
908b32fc
AM
5593 }
5594
50bc7936 5595 if (fdh != NULL
f5385ebf 5596 && !fdh->elf.forced_local
50bc7936 5597 && (info->shared
f5385ebf
AM
5598 || fdh->elf.def_dynamic
5599 || fdh->elf.ref_dynamic
50bc7936
AM
5600 || (fdh->elf.root.type == bfd_link_hash_undefweak
5601 && ELF_ST_VISIBILITY (fdh->elf.other) == STV_DEFAULT)))
5602 {
5603 if (fdh->elf.dynindx == -1)
c152c796 5604 if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
50bc7936 5605 return FALSE;
f5385ebf
AM
5606 fdh->elf.ref_regular |= fh->elf.ref_regular;
5607 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
5608 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
5609 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
50bc7936 5610 if (ELF_ST_VISIBILITY (fh->elf.other) == STV_DEFAULT)
e86ce104 5611 {
40d16e0b 5612 move_plt_plist (fh, fdh);
f5385ebf 5613 fdh->elf.needs_plt = 1;
e86ce104 5614 }
50bc7936 5615 fdh->is_func_descriptor = 1;
34814b9f
AM
5616 fdh->oh = fh;
5617 fh->oh = fdh;
e86ce104
AM
5618 }
5619
50bc7936
AM
5620 /* Now that the info is on the function descriptor, clear the
5621 function code sym info. Any function code syms for which we
5622 don't have a definition in a regular file, we force local.
5623 This prevents a shared library from exporting syms that have
5624 been imported from another library. Function code syms that
5625 are really in the library we must leave global to prevent the
5626 linker dragging in a definition from a static library. */
93f3fa99
AM
5627 force_local = (!fh->elf.def_regular
5628 || fdh == NULL
5629 || !fdh->elf.def_regular
5630 || fdh->elf.forced_local);
50bc7936
AM
5631 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
5632
b34976b6 5633 return TRUE;
e86ce104 5634}
40b8271b 5635
e86ce104 5636/* Called near the start of bfd_elf_size_dynamic_sections. We use
82bd7b59
AM
5637 this hook to a) provide some gcc support functions, and b) transfer
5638 dynamic linking information gathered so far on function code symbol
5639 entries, to their corresponding function descriptor symbol entries. */
deb0e272 5640
b34976b6 5641static bfd_boolean
4ce794b7
AM
5642ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
5643 struct bfd_link_info *info)
e86ce104
AM
5644{
5645 struct ppc_link_hash_table *htab;
82bd7b59 5646 unsigned int i;
deb0e272
AM
5647 const struct sfpr_def_parms funcs[] =
5648 {
5649 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
5650 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
5651 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
5652 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
5653 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
5654 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
5655 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
5656 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
5657 { "._savef", 14, 31, savefpr, savefpr1_tail },
5658 { "._restf", 14, 31, restfpr, restfpr1_tail },
5659 { "_savevr_", 20, 31, savevr, savevr_tail },
5660 { "_restvr_", 20, 31, restvr, restvr_tail }
5661 };
e86ce104
AM
5662
5663 htab = ppc_hash_table (info);
82bd7b59
AM
5664 if (htab->sfpr == NULL)
5665 /* We don't have any relocs. */
b34976b6 5666 return TRUE;
82bd7b59 5667
deb0e272
AM
5668 /* Provide any missing _save* and _rest* functions. */
5669 htab->sfpr->size = 0;
5670 for (i = 0; i < sizeof (funcs) / sizeof (funcs[0]); i++)
5671 if (!sfpr_define (info, &funcs[i]))
5672 return FALSE;
82bd7b59 5673
4ce794b7 5674 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
805fc799 5675
eea6121a 5676 if (htab->sfpr->size == 0)
8423293d 5677 htab->sfpr->flags |= SEC_EXCLUDE;
82bd7b59 5678
b34976b6 5679 return TRUE;
e86ce104
AM
5680}
5681
5682/* Adjust a symbol defined by a dynamic object and referenced by a
5683 regular object. The current definition is in some section of the
5684 dynamic object, but we're not including those sections. We have to
5685 change the definition to something the rest of the link can
5686 understand. */
5687
b34976b6 5688static bfd_boolean
4ce794b7
AM
5689ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
5690 struct elf_link_hash_entry *h)
e86ce104
AM
5691{
5692 struct ppc_link_hash_table *htab;
e86ce104
AM
5693 asection *s;
5694 unsigned int power_of_two;
5695
5696 htab = ppc_hash_table (info);
5697
5698 /* Deal with function syms. */
5699 if (h->type == STT_FUNC
f5385ebf 5700 || h->needs_plt)
e86ce104
AM
5701 {
5702 /* Clear procedure linkage table information for any symbol that
5703 won't need a .plt entry. */
411e1bfb
AM
5704 struct plt_entry *ent;
5705 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
5706 if (ent->plt.refcount > 0)
5707 break;
8387904d 5708 if (ent == NULL
9c7a29a3
AM
5709 || SYMBOL_CALLS_LOCAL (info, h)
5710 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
5711 && h->root.type == bfd_link_hash_undefweak))
40b8271b 5712 {
411e1bfb 5713 h->plt.plist = NULL;
f5385ebf 5714 h->needs_plt = 0;
40b8271b 5715 }
5bd4f169 5716 }
bbd7ec4a 5717 else
411e1bfb 5718 h->plt.plist = NULL;
5bd4f169
AM
5719
5720 /* If this is a weak symbol, and there is a real definition, the
5721 processor independent code will have arranged for us to see the
5722 real definition first, and we can just use the same value. */
f6e332e6 5723 if (h->u.weakdef != NULL)
5bd4f169 5724 {
f6e332e6
AM
5725 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
5726 || h->u.weakdef->root.type == bfd_link_hash_defweak);
5727 h->root.u.def.section = h->u.weakdef->root.u.def.section;
5728 h->root.u.def.value = h->u.weakdef->root.u.def.value;
a23b6845 5729 if (ELIMINATE_COPY_RELOCS)
f6e332e6 5730 h->non_got_ref = h->u.weakdef->non_got_ref;
b34976b6 5731 return TRUE;
5bd4f169
AM
5732 }
5733
5bd4f169
AM
5734 /* If we are creating a shared library, we must presume that the
5735 only references to the symbol are via the global offset table.
5736 For such cases we need not do anything here; the relocations will
5737 be handled correctly by relocate_section. */
5738 if (info->shared)
b34976b6 5739 return TRUE;
5bd4f169 5740
65f38f15
AM
5741 /* If there are no references to this symbol that do not use the
5742 GOT, we don't need to generate a copy reloc. */
f5385ebf 5743 if (!h->non_got_ref)
b34976b6 5744 return TRUE;
65f38f15 5745
f4656909 5746 if (ELIMINATE_COPY_RELOCS)
65f38f15 5747 {
f4656909
AM
5748 struct ppc_link_hash_entry * eh;
5749 struct ppc_dyn_relocs *p;
65f38f15 5750
f4656909
AM
5751 eh = (struct ppc_link_hash_entry *) h;
5752 for (p = eh->dyn_relocs; p != NULL; p = p->next)
5753 {
5754 s = p->sec->output_section;
5755 if (s != NULL && (s->flags & SEC_READONLY) != 0)
5756 break;
5757 }
5758
5759 /* If we didn't find any dynamic relocs in read-only sections, then
5760 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
5761 if (p == NULL)
5762 {
f5385ebf 5763 h->non_got_ref = 0;
f4656909
AM
5764 return TRUE;
5765 }
65f38f15
AM
5766 }
5767
5d35169e 5768 if (h->plt.plist != NULL)
97b639ba
AM
5769 {
5770 /* We should never get here, but unfortunately there are versions
5771 of gcc out there that improperly (for this ABI) put initialized
5772 function pointers, vtable refs and suchlike in read-only
5773 sections. Allow them to proceed, but warn that this might
5774 break at runtime. */
5775 (*_bfd_error_handler)
5776 (_("copy reloc against `%s' requires lazy plt linking; "
5777 "avoid setting LD_BIND_NOW=1 or upgrade gcc"),
5778 h->root.root.string);
5779 }
5d35169e
AM
5780
5781 /* This is a reference to a symbol defined by a dynamic object which
5782 is not a function. */
5783
909272ee
AM
5784 if (h->size == 0)
5785 {
5786 (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
5787 h->root.root.string);
5788 return TRUE;
5789 }
5790
5bd4f169
AM
5791 /* We must allocate the symbol in our .dynbss section, which will
5792 become part of the .bss section of the executable. There will be
5793 an entry for this symbol in the .dynsym section. The dynamic
5794 object will contain position independent code, so all references
5795 from the dynamic object to this symbol will go through the global
5796 offset table. The dynamic linker will use the .dynsym entry to
5797 determine the address it must put in the global offset table, so
5798 both the dynamic object and the regular object will refer to the
5799 same memory location for the variable. */
5bd4f169 5800
04c9666a
AM
5801 /* We must generate a R_PPC64_COPY reloc to tell the dynamic linker
5802 to copy the initial value out of the dynamic object and into the
5bd4f169
AM
5803 runtime process image. We need to remember the offset into the
5804 .rela.bss section we are going to use. */
5805 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
5806 {
eea6121a 5807 htab->relbss->size += sizeof (Elf64_External_Rela);
f5385ebf 5808 h->needs_copy = 1;
5bd4f169
AM
5809 }
5810
5811 /* We need to figure out the alignment required for this symbol. I
5812 have no idea how ELF linkers handle this. */
5813 power_of_two = bfd_log2 (h->size);
5814 if (power_of_two > 4)
5815 power_of_two = 4;
5816
5817 /* Apply the required alignment. */
4ce794b7 5818 s = htab->dynbss;
eea6121a 5819 s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
65f38f15 5820 if (power_of_two > bfd_get_section_alignment (htab->elf.dynobj, s))
5bd4f169 5821 {
65f38f15 5822 if (! bfd_set_section_alignment (htab->elf.dynobj, s, power_of_two))
b34976b6 5823 return FALSE;
5bd4f169
AM
5824 }
5825
5826 /* Define the symbol as being at this point in the section. */
5827 h->root.u.def.section = s;
eea6121a 5828 h->root.u.def.value = s->size;
5bd4f169
AM
5829
5830 /* Increment the section size to make room for the symbol. */
eea6121a 5831 s->size += h->size;
5bd4f169 5832
b34976b6 5833 return TRUE;
5bd4f169
AM
5834}
5835
e86ce104
AM
5836/* If given a function descriptor symbol, hide both the function code
5837 sym and the descriptor. */
5838static void
4ce794b7
AM
5839ppc64_elf_hide_symbol (struct bfd_link_info *info,
5840 struct elf_link_hash_entry *h,
5841 bfd_boolean force_local)
e86ce104 5842{
34814b9f 5843 struct ppc_link_hash_entry *eh;
e86ce104
AM
5844 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
5845
34814b9f
AM
5846 eh = (struct ppc_link_hash_entry *) h;
5847 if (eh->is_func_descriptor)
e86ce104 5848 {
34814b9f 5849 struct ppc_link_hash_entry *fh = eh->oh;
e86ce104 5850
721956f4 5851 if (fh == NULL)
d1329ca3
AM
5852 {
5853 const char *p, *q;
5854 struct ppc_link_hash_table *htab;
5855 char save;
5856
5857 /* We aren't supposed to use alloca in BFD because on
5858 systems which do not have alloca the version in libiberty
5859 calls xmalloc, which might cause the program to crash
5860 when it runs out of memory. This function doesn't have a
5861 return status, so there's no way to gracefully return an
5862 error. So cheat. We know that string[-1] can be safely
34814b9f
AM
5863 accessed; It's either a string in an ELF string table,
5864 or allocated in an objalloc structure. */
d1329ca3 5865
34814b9f 5866 p = eh->elf.root.root.string - 1;
d1329ca3
AM
5867 save = *p;
5868 *(char *) p = '.';
5869 htab = ppc_hash_table (info);
34814b9f
AM
5870 fh = (struct ppc_link_hash_entry *)
5871 elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
d1329ca3
AM
5872 *(char *) p = save;
5873
5874 /* Unfortunately, if it so happens that the string we were
5875 looking for was allocated immediately before this string,
5876 then we overwrote the string terminator. That's the only
5877 reason the lookup should fail. */
5878 if (fh == NULL)
5879 {
34814b9f
AM
5880 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
5881 while (q >= eh->elf.root.root.string && *q == *p)
d1329ca3 5882 --q, --p;
34814b9f
AM
5883 if (q < eh->elf.root.root.string && *p == '.')
5884 fh = (struct ppc_link_hash_entry *)
5885 elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
d1329ca3
AM
5886 }
5887 if (fh != NULL)
5888 {
34814b9f
AM
5889 eh->oh = fh;
5890 fh->oh = eh;
d1329ca3
AM
5891 }
5892 }
e86ce104 5893 if (fh != NULL)
34814b9f 5894 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
e86ce104
AM
5895 }
5896}
5897
411e1bfb 5898static bfd_boolean
8843416a
AM
5899get_sym_h (struct elf_link_hash_entry **hp,
5900 Elf_Internal_Sym **symp,
5901 asection **symsecp,
5902 char **tls_maskp,
5903 Elf_Internal_Sym **locsymsp,
5904 unsigned long r_symndx,
5905 bfd *ibfd)
411e1bfb
AM
5906{
5907 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
5908
5909 if (r_symndx >= symtab_hdr->sh_info)
5910 {
5911 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
5912 struct elf_link_hash_entry *h;
5913
5914 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5915 while (h->root.type == bfd_link_hash_indirect
5916 || h->root.type == bfd_link_hash_warning)
5917 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5918
5919 if (hp != NULL)
5920 *hp = h;
5921
5922 if (symp != NULL)
5923 *symp = NULL;
5924
5925 if (symsecp != NULL)
5926 {
5927 asection *symsec = NULL;
5928 if (h->root.type == bfd_link_hash_defined
5929 || h->root.type == bfd_link_hash_defweak)
5930 symsec = h->root.u.def.section;
5931 *symsecp = symsec;
5932 }
5933
e7b938ca 5934 if (tls_maskp != NULL)
411e1bfb
AM
5935 {
5936 struct ppc_link_hash_entry *eh;
5937
5938 eh = (struct ppc_link_hash_entry *) h;
e7b938ca 5939 *tls_maskp = &eh->tls_mask;
411e1bfb
AM
5940 }
5941 }
5942 else
5943 {
5944 Elf_Internal_Sym *sym;
5945 Elf_Internal_Sym *locsyms = *locsymsp;
5946
5947 if (locsyms == NULL)
5948 {
5949 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
5950 if (locsyms == NULL)
5951 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
5952 symtab_hdr->sh_info,
5953 0, NULL, NULL, NULL);
5954 if (locsyms == NULL)
5955 return FALSE;
5956 *locsymsp = locsyms;
5957 }
5958 sym = locsyms + r_symndx;
5959
5960 if (hp != NULL)
5961 *hp = NULL;
5962
5963 if (symp != NULL)
5964 *symp = sym;
5965
5966 if (symsecp != NULL)
5967 {
5968 asection *symsec = NULL;
5969 if ((sym->st_shndx != SHN_UNDEF
5970 && sym->st_shndx < SHN_LORESERVE)
5971 || sym->st_shndx > SHN_HIRESERVE)
5972 symsec = bfd_section_from_elf_index (ibfd, sym->st_shndx);
5973 *symsecp = symsec;
5974 }
5975
e7b938ca 5976 if (tls_maskp != NULL)
411e1bfb
AM
5977 {
5978 struct got_entry **lgot_ents;
e7b938ca 5979 char *tls_mask;
411e1bfb 5980
e7b938ca 5981 tls_mask = NULL;
411e1bfb
AM
5982 lgot_ents = elf_local_got_ents (ibfd);
5983 if (lgot_ents != NULL)
5984 {
e7b938ca
AM
5985 char *lgot_masks = (char *) (lgot_ents + symtab_hdr->sh_info);
5986 tls_mask = &lgot_masks[r_symndx];
411e1bfb 5987 }
e7b938ca 5988 *tls_maskp = tls_mask;
411e1bfb
AM
5989 }
5990 }
5991 return TRUE;
5992}
5993
e7b938ca 5994/* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
951fd09b 5995 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
ad8e1ba5 5996 type suitable for optimization, and 1 otherwise. */
951fd09b
AM
5997
5998static int
0d4792f7
AM
5999get_tls_mask (char **tls_maskp, unsigned long *toc_symndx,
6000 Elf_Internal_Sym **locsymsp,
4ce794b7 6001 const Elf_Internal_Rela *rel, bfd *ibfd)
411e1bfb
AM
6002{
6003 unsigned long r_symndx;
0d4792f7 6004 int next_r;
411e1bfb
AM
6005 struct elf_link_hash_entry *h;
6006 Elf_Internal_Sym *sym;
6007 asection *sec;
6008 bfd_vma off;
6009
6010 r_symndx = ELF64_R_SYM (rel->r_info);
e7b938ca 6011 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
951fd09b 6012 return 0;
411e1bfb 6013
e7b938ca 6014 if ((*tls_maskp != NULL && **tls_maskp != 0)
411e1bfb
AM
6015 || sec == NULL
6016 || ppc64_elf_section_data (sec)->t_symndx == NULL)
951fd09b 6017 return 1;
411e1bfb
AM
6018
6019 /* Look inside a TOC section too. */
6020 if (h != NULL)
6021 {
6022 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
6023 off = h->root.u.def.value;
6024 }
6025 else
6026 off = sym->st_value;
6027 off += rel->r_addend;
6028 BFD_ASSERT (off % 8 == 0);
6029 r_symndx = ppc64_elf_section_data (sec)->t_symndx[off / 8];
951fd09b 6030 next_r = ppc64_elf_section_data (sec)->t_symndx[off / 8 + 1];
e7b938ca 6031 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
951fd09b 6032 return 0;
0d4792f7
AM
6033 if (toc_symndx != NULL)
6034 *toc_symndx = r_symndx;
6035 if ((h == NULL
6036 || ((h->root.type == bfd_link_hash_defined
6037 || h->root.type == bfd_link_hash_defweak)
f5385ebf 6038 && !h->def_dynamic))
0d4792f7
AM
6039 && (next_r == -1 || next_r == -2))
6040 return 1 - next_r;
951fd09b 6041 return 1;
411e1bfb
AM
6042}
6043
754021d0 6044/* Adjust all global syms defined in opd sections. In gcc generated
8387904d 6045 code for the old ABI, these will already have been done. */
754021d0
AM
6046
6047static bfd_boolean
6048adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
6049{
6050 struct ppc_link_hash_entry *eh;
6051 asection *sym_sec;
6052 long *opd_adjust;
6053
6054 if (h->root.type == bfd_link_hash_indirect)
6055 return TRUE;
6056
6057 if (h->root.type == bfd_link_hash_warning)
6058 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6059
6060 if (h->root.type != bfd_link_hash_defined
6061 && h->root.type != bfd_link_hash_defweak)
6062 return TRUE;
6063
6064 eh = (struct ppc_link_hash_entry *) h;
6065 if (eh->adjust_done)
6066 return TRUE;
6067
6068 sym_sec = eh->elf.root.u.def.section;
4025353c
AM
6069 opd_adjust = get_opd_info (sym_sec);
6070 if (opd_adjust != NULL)
754021d0 6071 {
3f764659 6072 long adjust = opd_adjust[eh->elf.root.u.def.value / 8];
4025353c
AM
6073 if (adjust == -1)
6074 {
6075 /* This entry has been deleted. */
433817dd 6076 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->u.deleted_section;
81688140
AM
6077 if (dsec == NULL)
6078 {
6079 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
6080 if (elf_discarded_section (dsec))
6081 {
433817dd 6082 ppc64_elf_tdata (sym_sec->owner)->u.deleted_section = dsec;
81688140
AM
6083 break;
6084 }
6085 }
4025353c 6086 eh->elf.root.u.def.value = 0;
81688140 6087 eh->elf.root.u.def.section = dsec;
4025353c
AM
6088 }
6089 else
6090 eh->elf.root.u.def.value += adjust;
754021d0
AM
6091 eh->adjust_done = 1;
6092 }
6093 return TRUE;
6094}
6095
8c1d1bb8
AM
6096/* Handles decrementing dynamic reloc counts for the reloc specified by
6097 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM_SEC
6098 have already been determined. */
6099
6100static bfd_boolean
6101dec_dynrel_count (bfd_vma r_info,
6102 asection *sec,
6103 struct bfd_link_info *info,
6104 Elf_Internal_Sym **local_syms,
6105 struct elf_link_hash_entry *h,
6106 asection *sym_sec)
6107{
6108 enum elf_ppc64_reloc_type r_type;
6109 struct ppc_dyn_relocs *p;
6110 struct ppc_dyn_relocs **pp;
6111
6112 /* Can this reloc be dynamic? This switch, and later tests here
6113 should be kept in sync with the code in check_relocs. */
6114 r_type = ELF64_R_TYPE (r_info);
6115 switch (r_type)
6116 {
6117 default:
6118 return TRUE;
6119
6120 case R_PPC64_TPREL16:
6121 case R_PPC64_TPREL16_LO:
6122 case R_PPC64_TPREL16_HI:
6123 case R_PPC64_TPREL16_HA:
6124 case R_PPC64_TPREL16_DS:
6125 case R_PPC64_TPREL16_LO_DS:
6126 case R_PPC64_TPREL16_HIGHER:
6127 case R_PPC64_TPREL16_HIGHERA:
6128 case R_PPC64_TPREL16_HIGHEST:
6129 case R_PPC64_TPREL16_HIGHESTA:
6130 if (!info->shared)
6131 return TRUE;
6132
6133 case R_PPC64_TPREL64:
6134 case R_PPC64_DTPMOD64:
6135 case R_PPC64_DTPREL64:
6136 case R_PPC64_ADDR64:
6137 case R_PPC64_REL30:
6138 case R_PPC64_REL32:
6139 case R_PPC64_REL64:
6140 case R_PPC64_ADDR14:
6141 case R_PPC64_ADDR14_BRNTAKEN:
6142 case R_PPC64_ADDR14_BRTAKEN:
6143 case R_PPC64_ADDR16:
6144 case R_PPC64_ADDR16_DS:
6145 case R_PPC64_ADDR16_HA:
6146 case R_PPC64_ADDR16_HI:
6147 case R_PPC64_ADDR16_HIGHER:
6148 case R_PPC64_ADDR16_HIGHERA:
6149 case R_PPC64_ADDR16_HIGHEST:
6150 case R_PPC64_ADDR16_HIGHESTA:
6151 case R_PPC64_ADDR16_LO:
6152 case R_PPC64_ADDR16_LO_DS:
6153 case R_PPC64_ADDR24:
6154 case R_PPC64_ADDR32:
6155 case R_PPC64_UADDR16:
6156 case R_PPC64_UADDR32:
6157 case R_PPC64_UADDR64:
6158 case R_PPC64_TOC:
6159 break;
6160 }
6161
6162 if (local_syms != NULL)
6163 {
6164 unsigned long r_symndx;
6165 Elf_Internal_Sym *sym;
6166 bfd *ibfd = sec->owner;
6167
6168 r_symndx = ELF64_R_SYM (r_info);
6169 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
6170 return FALSE;
6171 }
6172
6173 if ((info->shared
6174 && (MUST_BE_DYN_RELOC (r_type)
6175 || (h != NULL
6176 && (!info->symbolic
6177 || h->root.type == bfd_link_hash_defweak
6178 || !h->def_regular))))
6179 || (ELIMINATE_COPY_RELOCS
6180 && !info->shared
6181 && h != NULL
6182 && (h->root.type == bfd_link_hash_defweak
6183 || !h->def_regular)))
6184 ;
6185 else
6186 return TRUE;
6187
6188 if (h != NULL)
6189 pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
8c1d1bb8 6190 else
6edfbbad 6191 {
60124e18
AM
6192 if (sym_sec != NULL)
6193 {
6194 void *vpp = &elf_section_data (sym_sec)->local_dynrel;
6195 pp = (struct ppc_dyn_relocs **) vpp;
6196 }
6197 else
6198 {
6199 void *vpp = &elf_section_data (sec)->local_dynrel;
6200 pp = (struct ppc_dyn_relocs **) vpp;
6201 }
6202
6203 /* elf_gc_sweep may have already removed all dyn relocs associated
6204 with local syms for a given section. Don't report a dynreloc
6205 miscount. */
6206 if (*pp == NULL)
6207 return TRUE;
6edfbbad 6208 }
8c1d1bb8
AM
6209
6210 while ((p = *pp) != NULL)
6211 {
6212 if (p->sec == sec)
6213 {
6214 if (!MUST_BE_DYN_RELOC (r_type))
6215 p->pc_count -= 1;
6216 p->count -= 1;
6217 if (p->count == 0)
6218 *pp = p->next;
6219 return TRUE;
6220 }
6221 pp = &p->next;
6222 }
6223
6224 (*_bfd_error_handler) (_("dynreloc miscount for %B, section %A"),
6225 sec->owner, sec);
6226 bfd_set_error (bfd_error_bad_value);
6227 return FALSE;
6228}
6229
754021d0
AM
6230/* Remove unused Official Procedure Descriptor entries. Currently we
6231 only remove those associated with functions in discarded link-once
6232 sections, or weakly defined functions that have been overridden. It
6233 would be possible to remove many more entries for statically linked
6234 applications. */
6235
b34976b6 6236bfd_boolean
3f764659 6237ppc64_elf_edit_opd (bfd *obfd, struct bfd_link_info *info,
b4f4e59f 6238 bfd_boolean no_opd_opt,
3f764659 6239 bfd_boolean non_overlapping)
1e2f5b6e
AM
6240{
6241 bfd *ibfd;
754021d0 6242 bfd_boolean some_edited = FALSE;
3f764659 6243 asection *need_pad = NULL;
1e2f5b6e 6244
411e1bfb 6245 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
1e2f5b6e
AM
6246 {
6247 asection *sec;
6248 Elf_Internal_Rela *relstart, *rel, *relend;
6249 Elf_Internal_Shdr *symtab_hdr;
6cdc0ccc 6250 Elf_Internal_Sym *local_syms;
1e2f5b6e
AM
6251 struct elf_link_hash_entry **sym_hashes;
6252 bfd_vma offset;
d6fe2dc1 6253 bfd_size_type amt;
4025353c 6254 long *opd_adjust;
3f764659
JJ
6255 bfd_boolean need_edit, add_aux_fields;
6256 bfd_size_type cnt_16b = 0;
1e2f5b6e
AM
6257
6258 sec = bfd_get_section_by_name (ibfd, ".opd");
46de2a7c 6259 if (sec == NULL || sec->size == 0)
1e2f5b6e
AM
6260 continue;
6261
3f764659 6262 amt = sec->size * sizeof (long) / 8;
4025353c
AM
6263 opd_adjust = get_opd_info (sec);
6264 if (opd_adjust == NULL)
d6fe2dc1 6265 {
4b85d634
AM
6266 /* check_relocs hasn't been called. Must be a ld -r link
6267 or --just-symbols object. */
b4f4e59f 6268 opd_adjust = bfd_alloc (obfd, amt);
46de2a7c
AM
6269 if (opd_adjust == NULL)
6270 return FALSE;
4025353c 6271 ppc64_elf_section_data (sec)->opd.adjust = opd_adjust;
d6fe2dc1 6272 }
4025353c 6273 memset (opd_adjust, 0, amt);
1e2f5b6e 6274
b4f4e59f
AM
6275 if (no_opd_opt)
6276 continue;
6277
4b85d634
AM
6278 if (sec->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
6279 continue;
6280
1e2f5b6e
AM
6281 if (sec->output_section == bfd_abs_section_ptr)
6282 continue;
6283
6284 /* Look through the section relocs. */
6285 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
6286 continue;
6287
6cdc0ccc 6288 local_syms = NULL;
1e2f5b6e
AM
6289 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
6290 sym_hashes = elf_sym_hashes (ibfd);
6291
6292 /* Read the relocations. */
4ce794b7 6293 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
45d6a902 6294 info->keep_memory);
1e2f5b6e 6295 if (relstart == NULL)
b34976b6 6296 return FALSE;
1e2f5b6e
AM
6297
6298 /* First run through the relocs to check they are sane, and to
6299 determine whether we need to edit this opd section. */
b34976b6 6300 need_edit = FALSE;
3f764659 6301 need_pad = sec;
1e2f5b6e
AM
6302 offset = 0;
6303 relend = relstart + sec->reloc_count;
50bc7936 6304 for (rel = relstart; rel < relend; )
1e2f5b6e 6305 {
04c9666a 6306 enum elf_ppc64_reloc_type r_type;
1e2f5b6e
AM
6307 unsigned long r_symndx;
6308 asection *sym_sec;
6309 struct elf_link_hash_entry *h;
6310 Elf_Internal_Sym *sym;
6311
3f764659 6312 /* .opd contains a regular array of 16 or 24 byte entries. We're
1e2f5b6e
AM
6313 only interested in the reloc pointing to a function entry
6314 point. */
50bc7936
AM
6315 if (rel->r_offset != offset
6316 || rel + 1 >= relend
6317 || (rel + 1)->r_offset != offset + 8)
1e2f5b6e
AM
6318 {
6319 /* If someone messes with .opd alignment then after a
6320 "ld -r" we might have padding in the middle of .opd.
6321 Also, there's nothing to prevent someone putting
6322 something silly in .opd with the assembler. No .opd
b34976b6 6323 optimization for them! */
3f764659 6324 broken_opd:
1e2f5b6e 6325 (*_bfd_error_handler)
d003868e 6326 (_("%B: .opd is not a regular array of opd entries"), ibfd);
b34976b6 6327 need_edit = FALSE;
1e2f5b6e
AM
6328 break;
6329 }
6330
50bc7936
AM
6331 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
6332 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
6333 {
6334 (*_bfd_error_handler)
d003868e
AM
6335 (_("%B: unexpected reloc type %u in .opd section"),
6336 ibfd, r_type);
50bc7936
AM
6337 need_edit = FALSE;
6338 break;
6339 }
6340
1e2f5b6e 6341 r_symndx = ELF64_R_SYM (rel->r_info);
411e1bfb
AM
6342 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
6343 r_symndx, ibfd))
50bc7936 6344 goto error_ret;
1e2f5b6e
AM
6345
6346 if (sym_sec == NULL || sym_sec->owner == NULL)
6347 {
411e1bfb
AM
6348 const char *sym_name;
6349 if (h != NULL)
6350 sym_name = h->root.root.string;
6351 else
26c61ae5
L
6352 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
6353 sym_sec);
411e1bfb 6354
1e2f5b6e 6355 (*_bfd_error_handler)
d003868e
AM
6356 (_("%B: undefined sym `%s' in .opd section"),
6357 ibfd, sym_name);
b34976b6 6358 need_edit = FALSE;
1e2f5b6e
AM
6359 break;
6360 }
6361
51020317
AM
6362 /* opd entries are always for functions defined in the
6363 current input bfd. If the symbol isn't defined in the
6364 input bfd, then we won't be using the function in this
6365 bfd; It must be defined in a linkonce section in another
6366 bfd, or is weak. It's also possible that we are
6367 discarding the function due to a linker script /DISCARD/,
6368 which we test for via the output_section. */
6369 if (sym_sec->owner != ibfd
6370 || sym_sec->output_section == bfd_abs_section_ptr)
b34976b6 6371 need_edit = TRUE;
1e2f5b6e 6372
50bc7936 6373 rel += 2;
3f764659
JJ
6374 if (rel == relend
6375 || (rel + 1 == relend && rel->r_offset == offset + 16))
6376 {
6377 if (sec->size == offset + 24)
6378 {
6379 need_pad = NULL;
6380 break;
6381 }
6382 if (rel == relend && sec->size == offset + 16)
6383 {
6384 cnt_16b++;
6385 break;
6386 }
6387 goto broken_opd;
6388 }
6389
6390 if (rel->r_offset == offset + 24)
6391 offset += 24;
6392 else if (rel->r_offset != offset + 16)
6393 goto broken_opd;
6394 else if (rel + 1 < relend
6395 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
6396 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
6397 {
6398 offset += 16;
6399 cnt_16b++;
6400 }
6401 else if (rel + 2 < relend
6402 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_ADDR64
6403 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC)
6404 {
6405 offset += 24;
6406 rel += 1;
6407 }
6408 else
6409 goto broken_opd;
1e2f5b6e
AM
6410 }
6411
3f764659
JJ
6412 add_aux_fields = non_overlapping && cnt_16b > 0;
6413
6414 if (need_edit || add_aux_fields)
1e2f5b6e
AM
6415 {
6416 Elf_Internal_Rela *write_rel;
6417 bfd_byte *rptr, *wptr;
3f764659 6418 bfd_byte *new_contents = NULL;
b34976b6 6419 bfd_boolean skip;
3f764659 6420 long opd_ent_size;
1e2f5b6e
AM
6421
6422 /* This seems a waste of time as input .opd sections are all
6423 zeros as generated by gcc, but I suppose there's no reason
6424 this will always be so. We might start putting something in
6425 the third word of .opd entries. */
6426 if ((sec->flags & SEC_IN_MEMORY) == 0)
6427 {
eea6121a
AM
6428 bfd_byte *loc;
6429 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
6cdc0ccc 6430 {
eea6121a
AM
6431 if (loc != NULL)
6432 free (loc);
50bc7936 6433 error_ret:
6cdc0ccc
AM
6434 if (local_syms != NULL
6435 && symtab_hdr->contents != (unsigned char *) local_syms)
6436 free (local_syms);
6cdc0ccc
AM
6437 if (elf_section_data (sec)->relocs != relstart)
6438 free (relstart);
b34976b6 6439 return FALSE;
6cdc0ccc 6440 }
1e2f5b6e
AM
6441 sec->contents = loc;
6442 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
6443 }
6444
6445 elf_section_data (sec)->relocs = relstart;
6446
3f764659 6447 new_contents = sec->contents;
3f764659
JJ
6448 if (add_aux_fields)
6449 {
6450 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
6451 if (new_contents == NULL)
6452 return FALSE;
6453 need_pad = FALSE;
3f764659 6454 }
b4f4e59f
AM
6455 wptr = new_contents;
6456 rptr = sec->contents;
3f764659 6457
1e2f5b6e 6458 write_rel = relstart;
b34976b6 6459 skip = FALSE;
1e2f5b6e 6460 offset = 0;
3f764659 6461 opd_ent_size = 0;
1e2f5b6e
AM
6462 for (rel = relstart; rel < relend; rel++)
6463 {
50bc7936
AM
6464 unsigned long r_symndx;
6465 asection *sym_sec;
6466 struct elf_link_hash_entry *h;
6467 Elf_Internal_Sym *sym;
6468
6469 r_symndx = ELF64_R_SYM (rel->r_info);
6470 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
d37c89e5 6471 r_symndx, ibfd))
50bc7936
AM
6472 goto error_ret;
6473
1e2f5b6e
AM
6474 if (rel->r_offset == offset)
6475 {
50bc7936 6476 struct ppc_link_hash_entry *fdh = NULL;
3f764659
JJ
6477
6478 /* See if the .opd entry is full 24 byte or
6479 16 byte (with fd_aux entry overlapped with next
6480 fd_func). */
6481 opd_ent_size = 24;
6482 if ((rel + 2 == relend && sec->size == offset + 16)
6483 || (rel + 3 < relend
6484 && rel[2].r_offset == offset + 16
6485 && rel[3].r_offset == offset + 24
6486 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_ADDR64
6487 && ELF64_R_TYPE (rel[3].r_info) == R_PPC64_TOC))
6488 opd_ent_size = 16;
6489
4025353c
AM
6490 if (h != NULL
6491 && h->root.root.string[0] == '.')
c4f68ce3
AM
6492 {
6493 fdh = get_fdh ((struct ppc_link_hash_entry *) h,
6494 ppc_hash_table (info));
6495 if (fdh != NULL
6496 && fdh->elf.root.type != bfd_link_hash_defined
6497 && fdh->elf.root.type != bfd_link_hash_defweak)
6498 fdh = NULL;
6499 }
1e2f5b6e 6500
51020317
AM
6501 skip = (sym_sec->owner != ibfd
6502 || sym_sec->output_section == bfd_abs_section_ptr);
a4aa0fb7
AM
6503 if (skip)
6504 {
4025353c 6505 if (fdh != NULL && sym_sec->owner == ibfd)
a4aa0fb7
AM
6506 {
6507 /* Arrange for the function descriptor sym
6508 to be dropped. */
d6fe2dc1
AM
6509 fdh->elf.root.u.def.value = 0;
6510 fdh->elf.root.u.def.section = sym_sec;
a4aa0fb7 6511 }
3f764659 6512 opd_adjust[rel->r_offset / 8] = -1;
a4aa0fb7
AM
6513 }
6514 else
1e2f5b6e
AM
6515 {
6516 /* We'll be keeping this opd entry. */
6517
4025353c 6518 if (fdh != NULL)
1e2f5b6e 6519 {
754021d0
AM
6520 /* Redefine the function descriptor symbol to
6521 this location in the opd section. It is
6522 necessary to update the value here rather
6523 than using an array of adjustments as we do
6524 for local symbols, because various places
6525 in the generic ELF code use the value
6526 stored in u.def.value. */
3f764659 6527 fdh->elf.root.u.def.value = wptr - new_contents;
754021d0 6528 fdh->adjust_done = 1;
1e2f5b6e 6529 }
754021d0
AM
6530
6531 /* Local syms are a bit tricky. We could
6532 tweak them as they can be cached, but
6533 we'd need to look through the local syms
6534 for the function descriptor sym which we
6535 don't have at the moment. So keep an
6536 array of adjustments. */
3f764659
JJ
6537 opd_adjust[rel->r_offset / 8]
6538 = (wptr - new_contents) - (rptr - sec->contents);
1e2f5b6e
AM
6539
6540 if (wptr != rptr)
3f764659
JJ
6541 memcpy (wptr, rptr, opd_ent_size);
6542 wptr += opd_ent_size;
6543 if (add_aux_fields && opd_ent_size == 16)
6544 {
6545 memset (wptr, '\0', 8);
6546 wptr += 8;
6547 }
1e2f5b6e 6548 }
3f764659
JJ
6549 rptr += opd_ent_size;
6550 offset += opd_ent_size;
1e2f5b6e
AM
6551 }
6552
50bc7936
AM
6553 if (skip)
6554 {
60124e18
AM
6555 if (!NO_OPD_RELOCS
6556 && !info->relocatable
18d944df
AM
6557 && !dec_dynrel_count (rel->r_info, sec, info,
6558 NULL, h, sym_sec))
8c1d1bb8 6559 goto error_ret;
50bc7936
AM
6560 }
6561 else
1e2f5b6e 6562 {
50bc7936
AM
6563 /* We need to adjust any reloc offsets to point to the
6564 new opd entries. While we're at it, we may as well
6565 remove redundant relocs. */
3f764659 6566 rel->r_offset += opd_adjust[(offset - opd_ent_size) / 8];
1e2f5b6e
AM
6567 if (write_rel != rel)
6568 memcpy (write_rel, rel, sizeof (*rel));
6569 ++write_rel;
6570 }
6571 }
6572
3f764659 6573 sec->size = wptr - new_contents;
1e2f5b6e 6574 sec->reloc_count = write_rel - relstart;
3f764659
JJ
6575 if (add_aux_fields)
6576 {
6577 free (sec->contents);
6578 sec->contents = new_contents;
6579 }
6580
cdcf6e38
AM
6581 /* Fudge the size too, as this is used later in
6582 elf_bfd_final_link if we are emitting relocs. */
6583 elf_section_data (sec)->rel_hdr.sh_size
6584 = sec->reloc_count * elf_section_data (sec)->rel_hdr.sh_entsize;
6585 BFD_ASSERT (elf_section_data (sec)->rel_hdr2 == NULL);
754021d0 6586 some_edited = TRUE;
1e2f5b6e 6587 }
6cdc0ccc 6588 else if (elf_section_data (sec)->relocs != relstart)
1e2f5b6e 6589 free (relstart);
6cdc0ccc 6590
411e1bfb
AM
6591 if (local_syms != NULL
6592 && symtab_hdr->contents != (unsigned char *) local_syms)
6593 {
6594 if (!info->keep_memory)
6595 free (local_syms);
6596 else
6597 symtab_hdr->contents = (unsigned char *) local_syms;
6598 }
6599 }
6600
754021d0
AM
6601 if (some_edited)
6602 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
6603
3f764659
JJ
6604 /* If we are doing a final link and the last .opd entry is just 16 byte
6605 long, add a 8 byte padding after it. */
6606 if (need_pad != NULL && !info->relocatable)
6607 {
6608 bfd_byte *p;
6609
6610 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
6611 {
6612 BFD_ASSERT (need_pad->size > 0);
6613
6614 p = bfd_malloc (need_pad->size + 8);
6615 if (p == NULL)
6616 return FALSE;
699733f6 6617
3f764659
JJ
6618 if (! bfd_get_section_contents (need_pad->owner, need_pad,
6619 p, 0, need_pad->size))
6620 return FALSE;
6621
6622 need_pad->contents = p;
6623 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
6624 }
6625 else
6626 {
6627 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
6628 if (p == NULL)
6629 return FALSE;
6630
6631 need_pad->contents = p;
6632 }
6633
6634 memset (need_pad->contents + need_pad->size, 0, 8);
6635 need_pad->size += 8;
6636 }
6637
411e1bfb
AM
6638 return TRUE;
6639}
6640
e1918d23 6641/* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
411e1bfb 6642
e1918d23 6643asection *
4ce794b7 6644ppc64_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
411e1bfb 6645{
411e1bfb
AM
6646 struct ppc_link_hash_table *htab;
6647
411e1bfb 6648 htab = ppc_hash_table (info);
a48ebf4d
AM
6649 if (htab->tls_get_addr != NULL)
6650 {
8387904d 6651 struct ppc_link_hash_entry *h = htab->tls_get_addr;
a48ebf4d 6652
8387904d
AM
6653 while (h->elf.root.type == bfd_link_hash_indirect
6654 || h->elf.root.type == bfd_link_hash_warning)
6655 h = (struct ppc_link_hash_entry *) h->elf.root.u.i.link;
a48ebf4d
AM
6656
6657 htab->tls_get_addr = h;
8387904d
AM
6658
6659 if (htab->tls_get_addr_fd == NULL
6660 && h->oh != NULL
c4f68ce3
AM
6661 && h->oh->is_func_descriptor
6662 && (h->oh->elf.root.type == bfd_link_hash_defined
6663 || h->oh->elf.root.type == bfd_link_hash_defweak))
8387904d
AM
6664 htab->tls_get_addr_fd = h->oh;
6665 }
6666
6667 if (htab->tls_get_addr_fd != NULL)
6668 {
6669 struct ppc_link_hash_entry *h = htab->tls_get_addr_fd;
6670
6671 while (h->elf.root.type == bfd_link_hash_indirect
6672 || h->elf.root.type == bfd_link_hash_warning)
6673 h = (struct ppc_link_hash_entry *) h->elf.root.u.i.link;
6674
6675 htab->tls_get_addr_fd = h;
a48ebf4d
AM
6676 }
6677
e1918d23 6678 return _bfd_elf_tls_setup (obfd, info);
951fd09b 6679}
411e1bfb 6680
951fd09b
AM
6681/* Run through all the TLS relocs looking for optimization
6682 opportunities. The linker has been hacked (see ppc64elf.em) to do
6683 a preliminary section layout so that we know the TLS segment
6684 offsets. We can't optimize earlier because some optimizations need
6685 to know the tp offset, and we need to optimize before allocating
6686 dynamic relocations. */
6687
6688bfd_boolean
4ce794b7 6689ppc64_elf_tls_optimize (bfd *obfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
951fd09b
AM
6690{
6691 bfd *ibfd;
6692 asection *sec;
6693 struct ppc_link_hash_table *htab;
6694
1049f94e 6695 if (info->relocatable || info->shared)
411e1bfb
AM
6696 return TRUE;
6697
951fd09b 6698 htab = ppc_hash_table (info);
411e1bfb
AM
6699 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
6700 {
6701 Elf_Internal_Sym *locsyms = NULL;
30038c59
AM
6702 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
6703 unsigned char *toc_ref = NULL;
411e1bfb 6704
30038c59
AM
6705 /* Look at all the sections for this file, with TOC last. */
6706 for (sec = (ibfd->sections == toc && toc && toc->next ? toc->next
6707 : ibfd->sections);
6708 sec != NULL;
6709 sec = (sec == toc ? NULL
6710 : sec->next == NULL ? toc
6711 : sec->next == toc && toc->next ? toc->next
6712 : sec->next))
411e1bfb
AM
6713 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
6714 {
6715 Elf_Internal_Rela *relstart, *rel, *relend;
6716 int expecting_tls_get_addr;
30038c59 6717 long toc_ref_index = 0;
411e1bfb
AM
6718
6719 /* Read the relocations. */
4ce794b7 6720 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
45d6a902 6721 info->keep_memory);
411e1bfb
AM
6722 if (relstart == NULL)
6723 return FALSE;
6724
6725 expecting_tls_get_addr = 0;
6726 relend = relstart + sec->reloc_count;
6727 for (rel = relstart; rel < relend; rel++)
6728 {
6729 enum elf_ppc64_reloc_type r_type;
6730 unsigned long r_symndx;
6731 struct elf_link_hash_entry *h;
6732 Elf_Internal_Sym *sym;
6733 asection *sym_sec;
e7b938ca
AM
6734 char *tls_mask;
6735 char tls_set, tls_clear, tls_type = 0;
411e1bfb 6736 bfd_vma value;
951fd09b 6737 bfd_boolean ok_tprel, is_local;
411e1bfb
AM
6738
6739 r_symndx = ELF64_R_SYM (rel->r_info);
e7b938ca 6740 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
411e1bfb
AM
6741 r_symndx, ibfd))
6742 {
6743 err_free_rel:
6744 if (elf_section_data (sec)->relocs != relstart)
6745 free (relstart);
30038c59
AM
6746 if (toc_ref != NULL)
6747 free (toc_ref);
411e1bfb
AM
6748 if (locsyms != NULL
6749 && (elf_tdata (ibfd)->symtab_hdr.contents
6750 != (unsigned char *) locsyms))
6751 free (locsyms);
6752 return FALSE;
6753 }
6754
6755 if (h != NULL)
6756 {
6757 if (h->root.type != bfd_link_hash_defined
6758 && h->root.type != bfd_link_hash_defweak)
6759 continue;
6760 value = h->root.u.def.value;
6761 }
6762 else
4025353c
AM
6763 /* Symbols referenced by TLS relocs must be of type
6764 STT_TLS. So no need for .opd local sym adjust. */
6765 value = sym->st_value;
951fd09b 6766
411e1bfb 6767 ok_tprel = FALSE;
951fd09b
AM
6768 is_local = FALSE;
6769 if (h == NULL
f5385ebf 6770 || !h->def_dynamic)
411e1bfb 6771 {
951fd09b 6772 is_local = TRUE;
411e1bfb
AM
6773 value += sym_sec->output_offset;
6774 value += sym_sec->output_section->vma;
e1918d23 6775 value -= htab->elf.tls_sec->vma;
411e1bfb
AM
6776 ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
6777 < (bfd_vma) 1 << 32);
6778 }
6779
4ce794b7 6780 r_type = ELF64_R_TYPE (rel->r_info);
411e1bfb
AM
6781 switch (r_type)
6782 {
6783 case R_PPC64_GOT_TLSLD16:
6784 case R_PPC64_GOT_TLSLD16_LO:
6785 case R_PPC64_GOT_TLSLD16_HI:
6786 case R_PPC64_GOT_TLSLD16_HA:
951fd09b
AM
6787 /* These relocs should never be against a symbol
6788 defined in a shared lib. Leave them alone if
6789 that turns out to be the case. */
e717da7e 6790 ppc64_tlsld_got (ibfd)->refcount -= 1;
951fd09b
AM
6791 if (!is_local)
6792 continue;
6793
951fd09b
AM
6794 /* LD -> LE */
6795 tls_set = 0;
6796 tls_clear = TLS_LD;
e7b938ca 6797 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
6798 expecting_tls_get_addr = 1;
6799 break;
6800
6801 case R_PPC64_GOT_TLSGD16:
6802 case R_PPC64_GOT_TLSGD16_LO:
6803 case R_PPC64_GOT_TLSGD16_HI:
6804 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 6805 if (ok_tprel)
411e1bfb
AM
6806 /* GD -> LE */
6807 tls_set = 0;
6808 else
6809 /* GD -> IE */
951fd09b
AM
6810 tls_set = TLS_TLS | TLS_TPRELGD;
6811 tls_clear = TLS_GD;
e7b938ca 6812 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
6813 expecting_tls_get_addr = 1;
6814 break;
6815
6816 case R_PPC64_GOT_TPREL16_DS:
6817 case R_PPC64_GOT_TPREL16_LO_DS:
6818 case R_PPC64_GOT_TPREL16_HI:
6819 case R_PPC64_GOT_TPREL16_HA:
6820 expecting_tls_get_addr = 0;
6821 if (ok_tprel)
6822 {
6823 /* IE -> LE */
6824 tls_set = 0;
6825 tls_clear = TLS_TPREL;
e7b938ca 6826 tls_type = TLS_TLS | TLS_TPREL;
411e1bfb
AM
6827 break;
6828 }
6829 else
6830 continue;
6831
6832 case R_PPC64_REL14:
6833 case R_PPC64_REL14_BRTAKEN:
6834 case R_PPC64_REL14_BRNTAKEN:
6835 case R_PPC64_REL24:
6836 if (h != NULL
8387904d
AM
6837 && (h == &htab->tls_get_addr->elf
6838 || h == &htab->tls_get_addr_fd->elf))
411e1bfb
AM
6839 {
6840 if (!expecting_tls_get_addr
6841 && rel != relstart
6842 && ((ELF64_R_TYPE (rel[-1].r_info)
6843 == R_PPC64_TOC16)
6844 || (ELF64_R_TYPE (rel[-1].r_info)
6845 == R_PPC64_TOC16_LO)))
6846 {
6847 /* Check for toc tls entries. */
6848 char *toc_tls;
951fd09b 6849 int retval;
411e1bfb 6850
0d4792f7 6851 retval = get_tls_mask (&toc_tls, NULL, &locsyms,
951fd09b
AM
6852 rel - 1, ibfd);
6853 if (retval == 0)
411e1bfb 6854 goto err_free_rel;
30038c59
AM
6855 if (retval > 1 && toc_tls != NULL)
6856 {
6857 expecting_tls_get_addr = 1;
6858 if (toc_ref != NULL)
6859 toc_ref[toc_ref_index] = 1;
6860 }
411e1bfb
AM
6861 }
6862
6863 if (expecting_tls_get_addr)
6864 {
6865 struct plt_entry *ent;
6866 for (ent = h->plt.plist; ent; ent = ent->next)
6867 if (ent->addend == 0)
6868 {
6869 if (ent->plt.refcount > 0)
6870 ent->plt.refcount -= 1;
6871 break;
6872 }
6873 }
6874 }
6875 expecting_tls_get_addr = 0;
6876 continue;
6877
30038c59
AM
6878 case R_PPC64_TOC16:
6879 case R_PPC64_TOC16_LO:
6880 case R_PPC64_TLS:
6881 expecting_tls_get_addr = 0;
6882 if (sym_sec == toc && toc != NULL)
6883 {
6884 /* Mark this toc entry as referenced by a TLS
6885 code sequence. We can do that now in the
6886 case of R_PPC64_TLS, and after checking for
6887 tls_get_addr for the TOC16 relocs. */
6888 if (toc_ref == NULL)
6889 {
6890 toc_ref = bfd_zmalloc (toc->size / 8);
6891 if (toc_ref == NULL)
6892 goto err_free_rel;
6893 }
6894 if (h != NULL)
6895 value = h->root.u.def.value;
6896 else
6897 value = sym->st_value;
6898 value += rel->r_addend;
6899 BFD_ASSERT (value < toc->size && value % 8 == 0);
6900 toc_ref_index = value / 8;
6901 if (r_type == R_PPC64_TLS)
6902 toc_ref[toc_ref_index] = 1;
6903 }
6904 continue;
6905
411e1bfb
AM
6906 case R_PPC64_TPREL64:
6907 expecting_tls_get_addr = 0;
30038c59
AM
6908 if (sec != toc
6909 || toc_ref == NULL
6910 || !toc_ref[rel->r_offset / 8])
6911 continue;
411e1bfb
AM
6912 if (ok_tprel)
6913 {
6914 /* IE -> LE */
6915 tls_set = TLS_EXPLICIT;
6916 tls_clear = TLS_TPREL;
6917 break;
6918 }
6919 else
6920 continue;
6921
6922 case R_PPC64_DTPMOD64:
6923 expecting_tls_get_addr = 0;
30038c59
AM
6924 if (sec != toc
6925 || toc_ref == NULL
6926 || !toc_ref[rel->r_offset / 8])
6927 continue;
951fd09b
AM
6928 if (rel + 1 < relend
6929 && (rel[1].r_info
6930 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
6931 && rel[1].r_offset == rel->r_offset + 8)
411e1bfb 6932 {
951fd09b 6933 if (ok_tprel)
411e1bfb 6934 /* GD -> LE */
951fd09b 6935 tls_set = TLS_EXPLICIT | TLS_GD;
411e1bfb
AM
6936 else
6937 /* GD -> IE */
951fd09b
AM
6938 tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
6939 tls_clear = TLS_GD;
411e1bfb
AM
6940 }
6941 else
6942 {
951fd09b
AM
6943 if (!is_local)
6944 continue;
6945
6946 /* LD -> LE */
6947 tls_set = TLS_EXPLICIT;
6948 tls_clear = TLS_LD;
411e1bfb
AM
6949 }
6950 break;
6951
6952 default:
6953 expecting_tls_get_addr = 0;
6954 continue;
6955 }
6956
6957 if ((tls_set & TLS_EXPLICIT) == 0)
6958 {
6959 struct got_entry *ent;
6960
6961 /* Adjust got entry for this reloc. */
6962 if (h != NULL)
6963 ent = h->got.glist;
6964 else
6965 ent = elf_local_got_ents (ibfd)[r_symndx];
6966
6967 for (; ent != NULL; ent = ent->next)
6968 if (ent->addend == rel->r_addend
e717da7e 6969 && ent->owner == ibfd
e7b938ca 6970 && ent->tls_type == tls_type)
411e1bfb
AM
6971 break;
6972 if (ent == NULL)
6973 abort ();
6974
6975 if (tls_set == 0)
6976 {
6977 /* We managed to get rid of a got entry. */
6978 if (ent->got.refcount > 0)
6979 ent->got.refcount -= 1;
6980 }
411e1bfb 6981 }
8c1d1bb8 6982 else
411e1bfb 6983 {
8c1d1bb8
AM
6984 /* If we got rid of a DTPMOD/DTPREL reloc pair then
6985 we'll lose one or two dyn relocs. */
6986 if (!dec_dynrel_count (rel->r_info, sec, info,
6987 NULL, h, sym_sec))
6988 return FALSE;
6989
6990 if (tls_set == (TLS_EXPLICIT | TLS_GD))
6991 {
6992 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
6993 NULL, h, sym_sec))
6994 return FALSE;
6995 }
411e1bfb
AM
6996 }
6997
e7b938ca
AM
6998 *tls_mask |= tls_set;
6999 *tls_mask &= ~tls_clear;
411e1bfb
AM
7000 }
7001
7002 if (elf_section_data (sec)->relocs != relstart)
7003 free (relstart);
7004 }
7005
30038c59
AM
7006 if (toc_ref != NULL)
7007 free (toc_ref);
7008
411e1bfb
AM
7009 if (locsyms != NULL
7010 && (elf_tdata (ibfd)->symtab_hdr.contents
7011 != (unsigned char *) locsyms))
6cdc0ccc
AM
7012 {
7013 if (!info->keep_memory)
411e1bfb 7014 free (locsyms);
6cdc0ccc 7015 else
411e1bfb 7016 elf_tdata (ibfd)->symtab_hdr.contents = (unsigned char *) locsyms;
6cdc0ccc 7017 }
1e2f5b6e 7018 }
b34976b6 7019 return TRUE;
1e2f5b6e 7020}
b34976b6 7021
c5614fa4
AM
7022/* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
7023 the values of any global symbols in a toc section that has been
7024 edited. Globals in toc sections should be a rarity, so this function
7025 sets a flag if any are found in toc sections other than the one just
7026 edited, so that futher hash table traversals can be avoided. */
7027
7028struct adjust_toc_info
7029{
7030 asection *toc;
7031 unsigned long *skip;
7032 bfd_boolean global_toc_syms;
7033};
7034
7035static bfd_boolean
7036adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
7037{
7038 struct ppc_link_hash_entry *eh;
7039 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
7040
7041 if (h->root.type == bfd_link_hash_indirect)
7042 return TRUE;
7043
7044 if (h->root.type == bfd_link_hash_warning)
7045 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7046
7047 if (h->root.type != bfd_link_hash_defined
7048 && h->root.type != bfd_link_hash_defweak)
7049 return TRUE;
7050
7051 eh = (struct ppc_link_hash_entry *) h;
7052 if (eh->adjust_done)
7053 return TRUE;
7054
7055 if (eh->elf.root.u.def.section == toc_inf->toc)
7056 {
7057 unsigned long skip = toc_inf->skip[eh->elf.root.u.def.value >> 3];
7058 if (skip != (unsigned long) -1)
7059 eh->elf.root.u.def.value -= skip;
7060 else
7061 {
7062 (*_bfd_error_handler)
7063 (_("%s defined in removed toc entry"), eh->elf.root.root.string);
7064 eh->elf.root.u.def.section = &bfd_abs_section;
7065 eh->elf.root.u.def.value = 0;
7066 }
7067 eh->adjust_done = 1;
7068 }
7069 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
7070 toc_inf->global_toc_syms = TRUE;
7071
7072 return TRUE;
7073}
7074
7075/* Examine all relocs referencing .toc sections in order to remove
7076 unused .toc entries. */
7077
7078bfd_boolean
7079ppc64_elf_edit_toc (bfd *obfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
7080{
7081 bfd *ibfd;
7082 struct adjust_toc_info toc_inf;
7083
7084 toc_inf.global_toc_syms = TRUE;
7085 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
7086 {
7087 asection *toc, *sec;
7088 Elf_Internal_Shdr *symtab_hdr;
7089 Elf_Internal_Sym *local_syms;
7090 struct elf_link_hash_entry **sym_hashes;
92b7a70f 7091 Elf_Internal_Rela *relstart, *rel;
c5614fa4
AM
7092 unsigned long *skip, *drop;
7093 unsigned char *used;
7094 unsigned char *keep, last, some_unused;
7095
7096 toc = bfd_get_section_by_name (ibfd, ".toc");
7097 if (toc == NULL
92b7a70f 7098 || toc->size == 0
c5614fa4
AM
7099 || toc->sec_info_type == ELF_INFO_TYPE_JUST_SYMS
7100 || elf_discarded_section (toc))
7101 continue;
7102
7103 local_syms = NULL;
7104 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
7105 sym_hashes = elf_sym_hashes (ibfd);
7106
7107 /* Look at sections dropped from the final link. */
7108 skip = NULL;
7109 relstart = NULL;
7110 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7111 {
7112 if (sec->reloc_count == 0
7113 || !elf_discarded_section (sec)
7114 || get_opd_info (sec)
7115 || (sec->flags & SEC_ALLOC) == 0
7116 || (sec->flags & SEC_DEBUGGING) != 0)
7117 continue;
7118
7119 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
7120 if (relstart == NULL)
7121 goto error_ret;
7122
7123 /* Run through the relocs to see which toc entries might be
7124 unused. */
7125 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
7126 {
7127 enum elf_ppc64_reloc_type r_type;
7128 unsigned long r_symndx;
7129 asection *sym_sec;
7130 struct elf_link_hash_entry *h;
7131 Elf_Internal_Sym *sym;
7132 bfd_vma val;
7133
7134 r_type = ELF64_R_TYPE (rel->r_info);
7135 switch (r_type)
7136 {
7137 default:
7138 continue;
7139
7140 case R_PPC64_TOC16:
7141 case R_PPC64_TOC16_LO:
7142 case R_PPC64_TOC16_HI:
7143 case R_PPC64_TOC16_HA:
7144 case R_PPC64_TOC16_DS:
7145 case R_PPC64_TOC16_LO_DS:
7146 break;
7147 }
7148
7149 r_symndx = ELF64_R_SYM (rel->r_info);
7150 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7151 r_symndx, ibfd))
7152 goto error_ret;
7153
7154 if (sym_sec != toc)
7155 continue;
7156
7157 if (h != NULL)
7158 val = h->root.u.def.value;
7159 else
7160 val = sym->st_value;
7161 val += rel->r_addend;
7162
7163 if (val >= toc->size)
7164 continue;
7165
7166 /* Anything in the toc ought to be aligned to 8 bytes.
7167 If not, don't mark as unused. */
7168 if (val & 7)
7169 continue;
7170
7171 if (skip == NULL)
7172 {
7173 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 7) / 8);
7174 if (skip == NULL)
7175 goto error_ret;
7176 }
7177
7178 skip[val >> 3] = 1;
7179 }
7180
7181 if (elf_section_data (sec)->relocs != relstart)
7182 free (relstart);
7183 }
7184
7185 if (skip == NULL)
7186 continue;
7187
7188 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
7189 if (used == NULL)
7190 {
7191 error_ret:
7192 if (local_syms != NULL
7193 && symtab_hdr->contents != (unsigned char *) local_syms)
7194 free (local_syms);
7195 if (sec != NULL
7196 && relstart != NULL
7197 && elf_section_data (sec)->relocs != relstart)
7198 free (relstart);
7199 if (skip != NULL)
7200 free (skip);
7201 return FALSE;
7202 }
7203
30038c59
AM
7204 /* Now check all kept sections that might reference the toc.
7205 Check the toc itself last. */
7206 for (sec = (ibfd->sections == toc && toc->next ? toc->next
7207 : ibfd->sections);
c5614fa4 7208 sec != NULL;
c5614fa4 7209 sec = (sec == toc ? NULL
c5614fa4 7210 : sec->next == NULL ? toc
30038c59 7211 : sec->next == toc && toc->next ? toc->next
c5614fa4
AM
7212 : sec->next))
7213 {
7214 int repeat;
7215
7216 if (sec->reloc_count == 0
7217 || elf_discarded_section (sec)
7218 || get_opd_info (sec)
7219 || (sec->flags & SEC_ALLOC) == 0
7220 || (sec->flags & SEC_DEBUGGING) != 0)
7221 continue;
7222
7223 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, TRUE);
7224 if (relstart == NULL)
7225 goto error_ret;
7226
7227 /* Mark toc entries referenced as used. */
7228 repeat = 0;
7229 do
7230 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
7231 {
7232 enum elf_ppc64_reloc_type r_type;
7233 unsigned long r_symndx;
7234 asection *sym_sec;
7235 struct elf_link_hash_entry *h;
7236 Elf_Internal_Sym *sym;
7237 bfd_vma val;
7238
7239 r_type = ELF64_R_TYPE (rel->r_info);
7240 switch (r_type)
7241 {
7242 case R_PPC64_TOC16:
7243 case R_PPC64_TOC16_LO:
7244 case R_PPC64_TOC16_HI:
7245 case R_PPC64_TOC16_HA:
7246 case R_PPC64_TOC16_DS:
7247 case R_PPC64_TOC16_LO_DS:
7248 /* In case we're taking addresses of toc entries. */
7249 case R_PPC64_ADDR64:
7250 break;
7251
7252 default:
7253 continue;
7254 }
7255
7256 r_symndx = ELF64_R_SYM (rel->r_info);
7257 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7258 r_symndx, ibfd))
7259 {
7260 free (used);
7261 goto error_ret;
7262 }
7263
7264 if (sym_sec != toc)
7265 continue;
7266
7267 if (h != NULL)
7268 val = h->root.u.def.value;
7269 else
7270 val = sym->st_value;
7271 val += rel->r_addend;
7272
7273 if (val >= toc->size)
7274 continue;
7275
7276 /* For the toc section, we only mark as used if
7277 this entry itself isn't unused. */
7278 if (sec == toc
7279 && !used[val >> 3]
7280 && (used[rel->r_offset >> 3]
7281 || !skip[rel->r_offset >> 3]))
7282 /* Do all the relocs again, to catch reference
7283 chains. */
7284 repeat = 1;
7285
7286 used[val >> 3] = 1;
7287 }
7288 while (repeat);
7289 }
7290
7291 /* Merge the used and skip arrays. Assume that TOC
7292 doublewords not appearing as either used or unused belong
7293 to to an entry more than one doubleword in size. */
7294 for (drop = skip, keep = used, last = 0, some_unused = 0;
7295 drop < skip + (toc->size + 7) / 8;
7296 ++drop, ++keep)
7297 {
7298 if (*keep)
7299 {
7300 *drop = 0;
7301 last = 0;
7302 }
7303 else if (*drop)
7304 {
7305 some_unused = 1;
7306 last = 1;
7307 }
7308 else
7309 *drop = last;
7310 }
7311
7312 free (used);
7313
7314 if (some_unused)
7315 {
7316 bfd_byte *contents, *src;
7317 unsigned long off;
7318
7319 /* Shuffle the toc contents, and at the same time convert the
7320 skip array from booleans into offsets. */
7321 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
7322 goto error_ret;
7323
7324 elf_section_data (toc)->this_hdr.contents = contents;
7325
7326 for (src = contents, off = 0, drop = skip;
7327 src < contents + toc->size;
7328 src += 8, ++drop)
7329 {
7330 if (*drop)
7331 {
7332 *drop = (unsigned long) -1;
7333 off += 8;
7334 }
7335 else if (off != 0)
7336 {
7337 *drop = off;
7338 memcpy (src - off, src, 8);
7339 }
7340 }
7341 toc->rawsize = toc->size;
7342 toc->size = src - contents - off;
7343
92b7a70f
AM
7344 if (toc->reloc_count != 0)
7345 {
7346 Elf_Internal_Rela *wrel;
7347 bfd_size_type sz;
c5614fa4 7348
92b7a70f
AM
7349 /* Read toc relocs. */
7350 relstart = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
7351 TRUE);
7352 if (relstart == NULL)
7353 goto error_ret;
7354
7355 /* Remove unused toc relocs, and adjust those we keep. */
7356 wrel = relstart;
7357 for (rel = relstart; rel < relstart + toc->reloc_count; ++rel)
7358 if (skip[rel->r_offset >> 3] != (unsigned long) -1)
7359 {
7360 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
7361 wrel->r_info = rel->r_info;
7362 wrel->r_addend = rel->r_addend;
7363 ++wrel;
7364 }
8c1d1bb8
AM
7365 else if (!dec_dynrel_count (rel->r_info, toc, info,
7366 &local_syms, NULL, NULL))
7367 goto error_ret;
35090471 7368
92b7a70f
AM
7369 toc->reloc_count = wrel - relstart;
7370 sz = elf_section_data (toc)->rel_hdr.sh_entsize;
7371 elf_section_data (toc)->rel_hdr.sh_size = toc->reloc_count * sz;
7372 BFD_ASSERT (elf_section_data (toc)->rel_hdr2 == NULL);
7373 }
c5614fa4
AM
7374
7375 /* Adjust addends for relocs against the toc section sym. */
7376 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7377 {
7378 if (sec->reloc_count == 0
7379 || elf_discarded_section (sec))
7380 continue;
7381
7382 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7383 TRUE);
7384 if (relstart == NULL)
7385 goto error_ret;
7386
7387 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
7388 {
7389 enum elf_ppc64_reloc_type r_type;
7390 unsigned long r_symndx;
7391 asection *sym_sec;
7392 struct elf_link_hash_entry *h;
7393 Elf_Internal_Sym *sym;
7394
7395 r_type = ELF64_R_TYPE (rel->r_info);
7396 switch (r_type)
7397 {
7398 default:
7399 continue;
7400
7401 case R_PPC64_TOC16:
7402 case R_PPC64_TOC16_LO:
7403 case R_PPC64_TOC16_HI:
7404 case R_PPC64_TOC16_HA:
7405 case R_PPC64_TOC16_DS:
7406 case R_PPC64_TOC16_LO_DS:
7407 case R_PPC64_ADDR64:
7408 break;
7409 }
7410
7411 r_symndx = ELF64_R_SYM (rel->r_info);
7412 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7413 r_symndx, ibfd))
7414 goto error_ret;
7415
7416 if (sym_sec != toc || h != NULL || sym->st_value != 0)
7417 continue;
7418
7419 rel->r_addend -= skip[rel->r_addend >> 3];
7420 }
7421 }
7422
7423 /* We shouldn't have local or global symbols defined in the TOC,
7424 but handle them anyway. */
7425 if (local_syms != NULL)
7426 {
7427 Elf_Internal_Sym *sym;
7428
7429 for (sym = local_syms;
7430 sym < local_syms + symtab_hdr->sh_info;
7431 ++sym)
7432 if (sym->st_shndx != SHN_UNDEF
7433 && (sym->st_shndx < SHN_LORESERVE
7434 || sym->st_shndx > SHN_HIRESERVE)
7435 && sym->st_value != 0
7436 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
7437 {
7438 if (skip[sym->st_value >> 3] != (unsigned long) -1)
7439 sym->st_value -= skip[sym->st_value >> 3];
7440 else
7441 {
7442 (*_bfd_error_handler)
7443 (_("%s defined in removed toc entry"),
26c61ae5
L
7444 bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7445 NULL));
c5614fa4
AM
7446 sym->st_value = 0;
7447 sym->st_shndx = SHN_ABS;
7448 }
7449 symtab_hdr->contents = (unsigned char *) local_syms;
7450 }
7451 }
7452
7453 /* Finally, adjust any global syms defined in the toc. */
7454 if (toc_inf.global_toc_syms)
7455 {
7456 toc_inf.toc = toc;
7457 toc_inf.skip = skip;
7458 toc_inf.global_toc_syms = FALSE;
7459 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
7460 &toc_inf);
7461 }
7462 }
7463
7464 if (local_syms != NULL
7465 && symtab_hdr->contents != (unsigned char *) local_syms)
7466 {
7467 if (!info->keep_memory)
7468 free (local_syms);
7469 else
7470 symtab_hdr->contents = (unsigned char *) local_syms;
7471 }
7472 free (skip);
7473 }
7474
7475 return TRUE;
7476}
7477
65f38f15
AM
7478/* Allocate space in .plt, .got and associated reloc sections for
7479 dynamic relocs. */
5bd4f169 7480
b34976b6 7481static bfd_boolean
4ce794b7 7482allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
5bd4f169 7483{
65f38f15
AM
7484 struct bfd_link_info *info;
7485 struct ppc_link_hash_table *htab;
5bd4f169 7486 asection *s;
65f38f15
AM
7487 struct ppc_link_hash_entry *eh;
7488 struct ppc_dyn_relocs *p;
411e1bfb 7489 struct got_entry *gent;
5bd4f169 7490
e92d460e 7491 if (h->root.type == bfd_link_hash_indirect)
b34976b6 7492 return TRUE;
5bd4f169 7493
e92d460e
AM
7494 if (h->root.type == bfd_link_hash_warning)
7495 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7496
65f38f15
AM
7497 info = (struct bfd_link_info *) inf;
7498 htab = ppc_hash_table (info);
5bd4f169 7499
65f38f15 7500 if (htab->elf.dynamic_sections_created
411e1bfb 7501 && h->dynindx != -1
9c7a29a3 7502 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
5bd4f169 7503 {
411e1bfb
AM
7504 struct plt_entry *pent;
7505 bfd_boolean doneone = FALSE;
7506 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
7507 if (pent->plt.refcount > 0)
7508 {
411e1bfb
AM
7509 /* If this is the first .plt entry, make room for the special
7510 first entry. */
4ce794b7 7511 s = htab->plt;
eea6121a
AM
7512 if (s->size == 0)
7513 s->size += PLT_INITIAL_ENTRY_SIZE;
411e1bfb 7514
eea6121a 7515 pent->plt.offset = s->size;
411e1bfb
AM
7516
7517 /* Make room for this entry. */
eea6121a 7518 s->size += PLT_ENTRY_SIZE;
411e1bfb
AM
7519
7520 /* Make room for the .glink code. */
4ce794b7 7521 s = htab->glink;
eea6121a
AM
7522 if (s->size == 0)
7523 s->size += GLINK_CALL_STUB_SIZE;
411e1bfb 7524 /* We need bigger stubs past index 32767. */
eea6121a
AM
7525 if (s->size >= GLINK_CALL_STUB_SIZE + 32768*2*4)
7526 s->size += 4;
7527 s->size += 2*4;
411e1bfb
AM
7528
7529 /* We also need to make an entry in the .rela.plt section. */
4ce794b7 7530 s = htab->relplt;
eea6121a 7531 s->size += sizeof (Elf64_External_Rela);
411e1bfb
AM
7532 doneone = TRUE;
7533 }
7534 else
7535 pent->plt.offset = (bfd_vma) -1;
7536 if (!doneone)
65f38f15 7537 {
411e1bfb 7538 h->plt.plist = NULL;
f5385ebf 7539 h->needs_plt = 0;
65f38f15
AM
7540 }
7541 }
7542 else
7543 {
411e1bfb 7544 h->plt.plist = NULL;
f5385ebf 7545 h->needs_plt = 0;
65f38f15
AM
7546 }
7547
951fd09b
AM
7548 eh = (struct ppc_link_hash_entry *) h;
7549 /* Run through the TLS GD got entries first if we're changing them
7550 to TPREL. */
e7b938ca 7551 if ((eh->tls_mask & TLS_TPRELGD) != 0)
951fd09b
AM
7552 for (gent = h->got.glist; gent != NULL; gent = gent->next)
7553 if (gent->got.refcount > 0
7554 && (gent->tls_type & TLS_GD) != 0)
7555 {
7556 /* This was a GD entry that has been converted to TPREL. If
7557 there happens to be a TPREL entry we can use that one. */
7558 struct got_entry *ent;
7559 for (ent = h->got.glist; ent != NULL; ent = ent->next)
7560 if (ent->got.refcount > 0
7561 && (ent->tls_type & TLS_TPREL) != 0
e717da7e
AM
7562 && ent->addend == gent->addend
7563 && ent->owner == gent->owner)
951fd09b
AM
7564 {
7565 gent->got.refcount = 0;
7566 break;
7567 }
7568
7569 /* If not, then we'll be using our own TPREL entry. */
7570 if (gent->got.refcount != 0)
7571 gent->tls_type = TLS_TLS | TLS_TPREL;
7572 }
7573
411e1bfb
AM
7574 for (gent = h->got.glist; gent != NULL; gent = gent->next)
7575 if (gent->got.refcount > 0)
7576 {
951fd09b
AM
7577 bfd_boolean dyn;
7578
411e1bfb 7579 /* Make sure this symbol is output as a dynamic symbol.
951fd09b
AM
7580 Undefined weak syms won't yet be marked as dynamic,
7581 nor will all TLS symbols. */
411e1bfb 7582 if (h->dynindx == -1
f5385ebf 7583 && !h->forced_local)
411e1bfb 7584 {
c152c796 7585 if (! bfd_elf_link_record_dynamic_symbol (info, h))
411e1bfb
AM
7586 return FALSE;
7587 }
65f38f15 7588
d881513a 7589 if ((gent->tls_type & TLS_LD) != 0
f5385ebf 7590 && !h->def_dynamic)
411e1bfb 7591 {
e717da7e 7592 gent->got.offset = ppc64_tlsld_got (gent->owner)->offset;
951fd09b 7593 continue;
411e1bfb 7594 }
951fd09b 7595
e717da7e 7596 s = ppc64_elf_tdata (gent->owner)->got;
eea6121a
AM
7597 gent->got.offset = s->size;
7598 s->size
d881513a 7599 += (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)) ? 16 : 8;
951fd09b 7600 dyn = htab->elf.dynamic_sections_created;
4e795f50
AM
7601 if ((info->shared
7602 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))
7603 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
7604 || h->root.type != bfd_link_hash_undefweak))
eea6121a 7605 ppc64_elf_tdata (gent->owner)->relgot->size
e7b938ca 7606 += (gent->tls_type & eh->tls_mask & TLS_GD
951fd09b
AM
7607 ? 2 * sizeof (Elf64_External_Rela)
7608 : sizeof (Elf64_External_Rela));
411e1bfb
AM
7609 }
7610 else
7611 gent->got.offset = (bfd_vma) -1;
65f38f15 7612
65f38f15 7613 if (eh->dyn_relocs == NULL)
b34976b6 7614 return TRUE;
65f38f15
AM
7615
7616 /* In the shared -Bsymbolic case, discard space allocated for
7617 dynamic pc-relative relocs against symbols which turn out to be
7618 defined in regular objects. For the normal shared case, discard
7619 space for relocs that have become local due to symbol visibility
7620 changes. */
7621
7622 if (info->shared)
7623 {
9c7a29a3
AM
7624 /* Relocs that use pc_count are those that appear on a call insn,
7625 or certain REL relocs (see MUST_BE_DYN_RELOC) that can be
7626 generated via assembly. We want calls to protected symbols to
7627 resolve directly to the function rather than going via the plt.
7628 If people want function pointer comparisons to work as expected
7629 then they should avoid writing weird assembly. */
09695f56 7630 if (SYMBOL_CALLS_LOCAL (info, h))
65f38f15
AM
7631 {
7632 struct ppc_dyn_relocs **pp;
7633
7634 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
5bd4f169 7635 {
65f38f15
AM
7636 p->count -= p->pc_count;
7637 p->pc_count = 0;
7638 if (p->count == 0)
7639 *pp = p->next;
7640 else
7641 pp = &p->next;
5bd4f169 7642 }
65f38f15 7643 }
4e795f50
AM
7644
7645 /* Also discard relocs on undefined weak syms with non-default
7646 visibility. */
cab87ef9
AM
7647 if (eh->dyn_relocs != NULL
7648 && h->root.type == bfd_link_hash_undefweak)
dfbb6ac9
AM
7649 {
7650 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
7651 eh->dyn_relocs = NULL;
7652
7653 /* Make sure this symbol is output as a dynamic symbol.
7654 Undefined weak syms won't yet be marked as dynamic. */
7655 else if (h->dynindx == -1
7656 && !h->forced_local)
7657 {
7658 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7659 return FALSE;
7660 }
7661 }
65f38f15 7662 }
f4656909 7663 else if (ELIMINATE_COPY_RELOCS)
65f38f15
AM
7664 {
7665 /* For the non-shared case, discard space for relocs against
7666 symbols which turn out to need copy relocs or are not
7667 dynamic. */
7668
f5385ebf
AM
7669 if (!h->non_got_ref
7670 && h->def_dynamic
7671 && !h->def_regular)
65f38f15
AM
7672 {
7673 /* Make sure this symbol is output as a dynamic symbol.
7674 Undefined weak syms won't yet be marked as dynamic. */
7675 if (h->dynindx == -1
f5385ebf 7676 && !h->forced_local)
65f38f15 7677 {
c152c796 7678 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 7679 return FALSE;
65f38f15
AM
7680 }
7681
7682 /* If that succeeded, we know we'll be keeping all the
7683 relocs. */
7684 if (h->dynindx != -1)
7685 goto keep;
7686 }
7687
7688 eh->dyn_relocs = NULL;
7689
ec338859 7690 keep: ;
65f38f15
AM
7691 }
7692
7693 /* Finally, allocate space. */
7694 for (p = eh->dyn_relocs; p != NULL; p = p->next)
7695 {
7696 asection *sreloc = elf_section_data (p->sec)->sreloc;
eea6121a 7697 sreloc->size += p->count * sizeof (Elf64_External_Rela);
65f38f15
AM
7698 }
7699
b34976b6 7700 return TRUE;
65f38f15
AM
7701}
7702
7703/* Find any dynamic relocs that apply to read-only sections. */
7704
b34976b6 7705static bfd_boolean
4ce794b7 7706readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
65f38f15
AM
7707{
7708 struct ppc_link_hash_entry *eh;
7709 struct ppc_dyn_relocs *p;
7710
e92d460e
AM
7711 if (h->root.type == bfd_link_hash_warning)
7712 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7713
65f38f15
AM
7714 eh = (struct ppc_link_hash_entry *) h;
7715 for (p = eh->dyn_relocs; p != NULL; p = p->next)
7716 {
7717 asection *s = p->sec->output_section;
7718
7719 if (s != NULL && (s->flags & SEC_READONLY) != 0)
7720 {
4ce794b7 7721 struct bfd_link_info *info = inf;
65f38f15
AM
7722
7723 info->flags |= DF_TEXTREL;
7724
7725 /* Not an error, just cut short the traversal. */
b34976b6 7726 return FALSE;
65f38f15
AM
7727 }
7728 }
b34976b6 7729 return TRUE;
65f38f15
AM
7730}
7731
7732/* Set the sizes of the dynamic sections. */
7733
b34976b6 7734static bfd_boolean
4ce794b7
AM
7735ppc64_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
7736 struct bfd_link_info *info)
65f38f15
AM
7737{
7738 struct ppc_link_hash_table *htab;
7739 bfd *dynobj;
7740 asection *s;
b34976b6 7741 bfd_boolean relocs;
65f38f15
AM
7742 bfd *ibfd;
7743
7744 htab = ppc_hash_table (info);
7745 dynobj = htab->elf.dynobj;
7746 if (dynobj == NULL)
7747 abort ();
7748
7749 if (htab->elf.dynamic_sections_created)
7750 {
7751 /* Set the contents of the .interp section to the interpreter. */
36af4a4e 7752 if (info->executable)
65f38f15
AM
7753 {
7754 s = bfd_get_section_by_name (dynobj, ".interp");
7755 if (s == NULL)
7756 abort ();
eea6121a 7757 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
65f38f15
AM
7758 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
7759 }
7760 }
7761
7762 /* Set up .got offsets for local syms, and space for local dynamic
7763 relocs. */
7764 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
7765 {
411e1bfb
AM
7766 struct got_entry **lgot_ents;
7767 struct got_entry **end_lgot_ents;
e7b938ca 7768 char *lgot_masks;
65f38f15
AM
7769 bfd_size_type locsymcount;
7770 Elf_Internal_Shdr *symtab_hdr;
7771 asection *srel;
7772
ee75fd95 7773 if (!is_ppc64_elf_target (ibfd->xvec))
65f38f15
AM
7774 continue;
7775
e717da7e
AM
7776 if (ppc64_tlsld_got (ibfd)->refcount > 0)
7777 {
7778 s = ppc64_elf_tdata (ibfd)->got;
eea6121a
AM
7779 ppc64_tlsld_got (ibfd)->offset = s->size;
7780 s->size += 16;
e717da7e
AM
7781 if (info->shared)
7782 {
7783 srel = ppc64_elf_tdata (ibfd)->relgot;
eea6121a 7784 srel->size += sizeof (Elf64_External_Rela);
e717da7e
AM
7785 }
7786 }
7787 else
7788 ppc64_tlsld_got (ibfd)->offset = (bfd_vma) -1;
7789
65f38f15
AM
7790 for (s = ibfd->sections; s != NULL; s = s->next)
7791 {
ec338859 7792 struct ppc_dyn_relocs *p;
65f38f15 7793
6edfbbad 7794 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
65f38f15 7795 {
ec338859
AM
7796 if (!bfd_is_abs_section (p->sec)
7797 && bfd_is_abs_section (p->sec->output_section))
7798 {
7799 /* Input section has been discarded, either because
7800 it is a copy of a linkonce section or due to
7801 linker script /DISCARD/, so we'll be discarding
7802 the relocs too. */
7803 }
248866a8 7804 else if (p->count != 0)
ec338859
AM
7805 {
7806 srel = elf_section_data (p->sec)->sreloc;
eea6121a 7807 srel->size += p->count * sizeof (Elf64_External_Rela);
248866a8
AM
7808 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
7809 info->flags |= DF_TEXTREL;
ec338859 7810 }
65f38f15
AM
7811 }
7812 }
7813
411e1bfb
AM
7814 lgot_ents = elf_local_got_ents (ibfd);
7815 if (!lgot_ents)
65f38f15
AM
7816 continue;
7817
7818 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
7819 locsymcount = symtab_hdr->sh_info;
411e1bfb 7820 end_lgot_ents = lgot_ents + locsymcount;
e7b938ca 7821 lgot_masks = (char *) end_lgot_ents;
e717da7e
AM
7822 s = ppc64_elf_tdata (ibfd)->got;
7823 srel = ppc64_elf_tdata (ibfd)->relgot;
e7b938ca 7824 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
65f38f15 7825 {
411e1bfb
AM
7826 struct got_entry *ent;
7827
7828 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
7829 if (ent->got.refcount > 0)
7830 {
e7b938ca 7831 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
411e1bfb 7832 {
e717da7e 7833 if (ppc64_tlsld_got (ibfd)->offset == (bfd_vma) -1)
411e1bfb 7834 {
eea6121a
AM
7835 ppc64_tlsld_got (ibfd)->offset = s->size;
7836 s->size += 16;
411e1bfb 7837 if (info->shared)
eea6121a 7838 srel->size += sizeof (Elf64_External_Rela);
411e1bfb 7839 }
e717da7e 7840 ent->got.offset = ppc64_tlsld_got (ibfd)->offset;
411e1bfb
AM
7841 }
7842 else
7843 {
eea6121a 7844 ent->got.offset = s->size;
e7b938ca 7845 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
411e1bfb 7846 {
eea6121a 7847 s->size += 16;
411e1bfb 7848 if (info->shared)
eea6121a 7849 srel->size += 2 * sizeof (Elf64_External_Rela);
411e1bfb
AM
7850 }
7851 else
7852 {
eea6121a 7853 s->size += 8;
411e1bfb 7854 if (info->shared)
eea6121a 7855 srel->size += sizeof (Elf64_External_Rela);
411e1bfb
AM
7856 }
7857 }
7858 }
7859 else
7860 ent->got.offset = (bfd_vma) -1;
65f38f15
AM
7861 }
7862 }
7863
7864 /* Allocate global sym .plt and .got entries, and space for global
7865 sym dynamic relocs. */
4ce794b7 7866 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
65f38f15
AM
7867
7868 /* We now have determined the sizes of the various dynamic sections.
7869 Allocate memory for them. */
b34976b6 7870 relocs = FALSE;
65f38f15
AM
7871 for (s = dynobj->sections; s != NULL; s = s->next)
7872 {
7873 if ((s->flags & SEC_LINKER_CREATED) == 0)
7874 continue;
7875
4ce794b7 7876 if (s == htab->brlt || s == htab->relbrlt)
721956f4
AM
7877 /* These haven't been allocated yet; don't strip. */
7878 continue;
e717da7e
AM
7879 else if (s == htab->got
7880 || s == htab->plt
c456f082
AM
7881 || s == htab->glink
7882 || s == htab->dynbss)
65f38f15
AM
7883 {
7884 /* Strip this section if we don't need it; see the
7885 comment below. */
5bd4f169 7886 }
65f38f15 7887 else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
5bd4f169 7888 {
c456f082 7889 if (s->size != 0)
5bd4f169 7890 {
4ce794b7 7891 if (s != htab->relplt)
b34976b6 7892 relocs = TRUE;
5bd4f169
AM
7893
7894 /* We use the reloc_count field as a counter if we need
7895 to copy relocs into the output file. */
7896 s->reloc_count = 0;
7897 }
7898 }
65f38f15 7899 else
5bd4f169
AM
7900 {
7901 /* It's not one of our sections, so don't allocate space. */
7902 continue;
7903 }
7904
eea6121a 7905 if (s->size == 0)
5bd4f169 7906 {
c456f082
AM
7907 /* If we don't need this section, strip it from the
7908 output file. This is mostly to handle .rela.bss and
7909 .rela.plt. We must create both sections in
7910 create_dynamic_sections, because they must be created
7911 before the linker maps input sections to output
7912 sections. The linker does that before
7913 adjust_dynamic_symbol is called, and it is that
7914 function which decides whether anything needs to go
7915 into these sections. */
8423293d 7916 s->flags |= SEC_EXCLUDE;
5bd4f169
AM
7917 continue;
7918 }
7919
c456f082 7920 if ((s->flags & SEC_HAS_CONTENTS) == 0)
5f333394
AM
7921 continue;
7922
65f38f15
AM
7923 /* Allocate memory for the section contents. We use bfd_zalloc
7924 here in case unused entries are not reclaimed before the
7925 section's contents are written out. This should not happen,
411e1bfb
AM
7926 but this way if it does we get a R_PPC64_NONE reloc in .rela
7927 sections instead of garbage.
7928 We also rely on the section contents being zero when writing
7929 the GOT. */
eea6121a 7930 s->contents = bfd_zalloc (dynobj, s->size);
65f38f15 7931 if (s->contents == NULL)
b34976b6 7932 return FALSE;
5bd4f169
AM
7933 }
7934
e717da7e
AM
7935 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
7936 {
ee75fd95 7937 if (!is_ppc64_elf_target (ibfd->xvec))
7b53ace3
AM
7938 continue;
7939
e717da7e
AM
7940 s = ppc64_elf_tdata (ibfd)->got;
7941 if (s != NULL && s != htab->got)
7942 {
eea6121a 7943 if (s->size == 0)
8423293d 7944 s->flags |= SEC_EXCLUDE;
e717da7e
AM
7945 else
7946 {
eea6121a 7947 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
7948 if (s->contents == NULL)
7949 return FALSE;
7950 }
7951 }
7952 s = ppc64_elf_tdata (ibfd)->relgot;
7953 if (s != NULL)
7954 {
eea6121a 7955 if (s->size == 0)
8423293d 7956 s->flags |= SEC_EXCLUDE;
e717da7e
AM
7957 else
7958 {
eea6121a 7959 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
7960 if (s->contents == NULL)
7961 return FALSE;
7962 relocs = TRUE;
7963 s->reloc_count = 0;
7964 }
7965 }
7966 }
7967
e86ce104 7968 if (htab->elf.dynamic_sections_created)
5bd4f169
AM
7969 {
7970 /* Add some entries to the .dynamic section. We fill in the
7971 values later, in ppc64_elf_finish_dynamic_sections, but we
7972 must add the entries now so that we get the correct size for
7973 the .dynamic section. The DT_DEBUG entry is filled in by the
7974 dynamic linker and used by the debugger. */
dc810e39 7975#define add_dynamic_entry(TAG, VAL) \
5a580b3a 7976 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
dc810e39 7977
36af4a4e 7978 if (info->executable)
5bd4f169 7979 {
dc810e39 7980 if (!add_dynamic_entry (DT_DEBUG, 0))
b34976b6 7981 return FALSE;
5bd4f169
AM
7982 }
7983
eea6121a 7984 if (htab->plt != NULL && htab->plt->size != 0)
5bd4f169 7985 {
dc810e39
AM
7986 if (!add_dynamic_entry (DT_PLTGOT, 0)
7987 || !add_dynamic_entry (DT_PLTRELSZ, 0)
7988 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
5d1634d7
AM
7989 || !add_dynamic_entry (DT_JMPREL, 0)
7990 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
b34976b6 7991 return FALSE;
5bd4f169
AM
7992 }
7993
19397422
AM
7994 if (NO_OPD_RELOCS)
7995 {
7996 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
7997 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
b34976b6 7998 return FALSE;
19397422
AM
7999 }
8000
5bd4f169
AM
8001 if (relocs)
8002 {
dc810e39
AM
8003 if (!add_dynamic_entry (DT_RELA, 0)
8004 || !add_dynamic_entry (DT_RELASZ, 0)
8005 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
b34976b6 8006 return FALSE;
5bd4f169 8007
65f38f15
AM
8008 /* If any dynamic relocs apply to a read-only section,
8009 then we need a DT_TEXTREL entry. */
248866a8 8010 if ((info->flags & DF_TEXTREL) == 0)
4ce794b7 8011 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs, info);
5bd4f169 8012
65f38f15 8013 if ((info->flags & DF_TEXTREL) != 0)
5bd4f169 8014 {
65f38f15 8015 if (!add_dynamic_entry (DT_TEXTREL, 0))
b34976b6 8016 return FALSE;
5bd4f169 8017 }
5bd4f169 8018 }
5bd4f169 8019 }
65f38f15 8020#undef add_dynamic_entry
5bd4f169 8021
b34976b6 8022 return TRUE;
5bd4f169
AM
8023}
8024
721956f4 8025/* Determine the type of stub needed, if any, for a call. */
5bd4f169 8026
4ce794b7
AM
8027static inline enum ppc_stub_type
8028ppc_type_of_stub (asection *input_sec,
8029 const Elf_Internal_Rela *rel,
8030 struct ppc_link_hash_entry **hash,
8031 bfd_vma destination)
5bd4f169 8032{
721956f4
AM
8033 struct ppc_link_hash_entry *h = *hash;
8034 bfd_vma location;
8035 bfd_vma branch_offset;
8036 bfd_vma max_branch_offset;
4ce794b7 8037 enum elf_ppc64_reloc_type r_type;
5bd4f169 8038
721956f4
AM
8039 if (h != NULL)
8040 {
7fe2b9a6
AM
8041 struct ppc_link_hash_entry *fdh = h;
8042 if (fdh->oh != NULL
8043 && fdh->oh->is_func_descriptor)
8044 fdh = fdh->oh;
8387904d 8045
7fe2b9a6 8046 if (fdh->elf.dynindx != -1)
5bd4f169 8047 {
411e1bfb 8048 struct plt_entry *ent;
8387904d 8049
7fe2b9a6 8050 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
411e1bfb
AM
8051 if (ent->addend == rel->r_addend
8052 && ent->plt.offset != (bfd_vma) -1)
8053 {
7fe2b9a6 8054 *hash = fdh;
411e1bfb
AM
8055 return ppc_stub_plt_call;
8056 }
5bd4f169
AM
8057 }
8058
7fe2b9a6
AM
8059 /* Here, we know we don't have a plt entry. If we don't have a
8060 either a defined function descriptor or a defined entry symbol
8061 in a regular object file, then it is pointless trying to make
8062 any other type of stub. */
8063 if (!((fdh->elf.root.type == bfd_link_hash_defined
8064 || fdh->elf.root.type == bfd_link_hash_defweak)
8065 && fdh->elf.root.u.def.section->output_section != NULL)
8066 && !((h->elf.root.type == bfd_link_hash_defined
8067 || h->elf.root.type == bfd_link_hash_defweak)
8068 && h->elf.root.u.def.section->output_section != NULL))
721956f4 8069 return ppc_stub_none;
5d1634d7 8070 }
5d1634d7 8071
721956f4
AM
8072 /* Determine where the call point is. */
8073 location = (input_sec->output_offset
8074 + input_sec->output_section->vma
8075 + rel->r_offset);
5d1634d7 8076
721956f4
AM
8077 branch_offset = destination - location;
8078 r_type = ELF64_R_TYPE (rel->r_info);
5d1634d7 8079
721956f4
AM
8080 /* Determine if a long branch stub is needed. */
8081 max_branch_offset = 1 << 25;
4ce794b7 8082 if (r_type != R_PPC64_REL24)
721956f4 8083 max_branch_offset = 1 << 15;
5d1634d7 8084
721956f4
AM
8085 if (branch_offset + max_branch_offset >= 2 * max_branch_offset)
8086 /* We need a stub. Figure out whether a long_branch or plt_branch
8087 is needed later. */
8088 return ppc_stub_long_branch;
5d1634d7 8089
721956f4 8090 return ppc_stub_none;
5d1634d7
AM
8091}
8092
8093/* Build a .plt call stub. */
8094
4ce794b7
AM
8095static inline bfd_byte *
8096build_plt_stub (bfd *obfd, bfd_byte *p, int offset)
5d1634d7
AM
8097{
8098#define PPC_LO(v) ((v) & 0xffff)
8099#define PPC_HI(v) (((v) >> 16) & 0xffff)
8100#define PPC_HA(v) PPC_HI ((v) + 0x8000)
8101
5d1634d7 8102 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
ad8e1ba5 8103 bfd_put_32 (obfd, STD_R2_40R1, p), p += 4;
5d1634d7
AM
8104 bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset), p), p += 4;
8105 if (PPC_HA (offset + 8) != PPC_HA (offset))
ad8e1ba5 8106 bfd_put_32 (obfd, ADDIS_R12_R12 | 1, p), p += 4;
5d1634d7
AM
8107 offset += 8;
8108 bfd_put_32 (obfd, LD_R2_0R12 | PPC_LO (offset), p), p += 4;
8109 if (PPC_HA (offset + 8) != PPC_HA (offset))
ad8e1ba5 8110 bfd_put_32 (obfd, ADDIS_R12_R12 | 1, p), p += 4;
5d1634d7
AM
8111 offset += 8;
8112 bfd_put_32 (obfd, MTCTR_R11, p), p += 4;
8113 bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset), p), p += 4;
8114 bfd_put_32 (obfd, BCTR, p), p += 4;
8115 return p;
8116}
8117
b34976b6 8118static bfd_boolean
4ce794b7 8119ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
5d1634d7 8120{
721956f4
AM
8121 struct ppc_stub_hash_entry *stub_entry;
8122 struct ppc_branch_hash_entry *br_entry;
5d1634d7
AM
8123 struct bfd_link_info *info;
8124 struct ppc_link_hash_table *htab;
721956f4
AM
8125 bfd_byte *loc;
8126 bfd_byte *p;
8127 unsigned int indx;
411e1bfb 8128 struct plt_entry *ent;
ee75fd95 8129 bfd_vma dest, off;
721956f4 8130 int size;
5d1634d7 8131
721956f4
AM
8132 /* Massage our args to the form they really have. */
8133 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
4ce794b7 8134 info = in_arg;
5d1634d7 8135
5d1634d7
AM
8136 htab = ppc_hash_table (info);
8137
721956f4 8138 /* Make a note of the offset within the stubs for this entry. */
eea6121a 8139 stub_entry->stub_offset = stub_entry->stub_sec->size;
97b639ba 8140 loc = stub_entry->stub_sec->contents + stub_entry->stub_offset;
721956f4 8141
4ce794b7 8142 htab->stub_count[stub_entry->stub_type - 1] += 1;
721956f4 8143 switch (stub_entry->stub_type)
5d1634d7 8144 {
721956f4 8145 case ppc_stub_long_branch:
ad8e1ba5 8146 case ppc_stub_long_branch_r2off:
721956f4 8147 /* Branches are relative. This is where we are going to. */
ee75fd95
AM
8148 off = dest = (stub_entry->target_value
8149 + stub_entry->target_section->output_offset
8150 + stub_entry->target_section->output_section->vma);
5d1634d7 8151
721956f4
AM
8152 /* And this is where we are coming from. */
8153 off -= (stub_entry->stub_offset
97b639ba
AM
8154 + stub_entry->stub_sec->output_offset
8155 + stub_entry->stub_sec->output_section->vma);
e86ce104 8156
ad8e1ba5
AM
8157 if (stub_entry->stub_type != ppc_stub_long_branch_r2off)
8158 size = 4;
8159 else
8160 {
8161 bfd_vma r2off;
8162
8163 r2off = (htab->stub_group[stub_entry->target_section->id].toc_off
8164 - htab->stub_group[stub_entry->id_sec->id].toc_off);
97b639ba 8165 bfd_put_32 (htab->stub_bfd, STD_R2_40R1, loc);
ad8e1ba5 8166 loc += 4;
97b639ba 8167 bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
ad8e1ba5 8168 loc += 4;
97b639ba 8169 bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
ad8e1ba5
AM
8170 loc += 4;
8171 off -= 12;
8172 size = 16;
8173 }
97b639ba 8174 bfd_put_32 (htab->stub_bfd, B_DOT | (off & 0x3fffffc), loc);
ad8e1ba5 8175
5c3dead3
AM
8176 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
8177 {
8178 (*_bfd_error_handler) (_("long branch stub `%s' offset overflow"),
8179 stub_entry->root.string);
8180 htab->stub_error = TRUE;
8181 return FALSE;
8182 }
ee75fd95
AM
8183
8184 if (info->emitrelocations)
8185 {
8186 Elf_Internal_Rela *relocs, *r;
8187 struct bfd_elf_section_data *elfsec_data;
8188
8189 elfsec_data = elf_section_data (stub_entry->stub_sec);
8190 relocs = elfsec_data->relocs;
8191 if (relocs == NULL)
8192 {
8193 bfd_size_type relsize;
8194 relsize = stub_entry->stub_sec->reloc_count * sizeof (*relocs);
8195 relocs = bfd_alloc (htab->stub_bfd, relsize);
8196 if (relocs == NULL)
8197 return FALSE;
8198 elfsec_data->relocs = relocs;
8199 elfsec_data->rel_hdr.sh_size = relsize;
8200 elfsec_data->rel_hdr.sh_entsize = 24;
8201 stub_entry->stub_sec->reloc_count = 0;
8202 }
8203 r = relocs + stub_entry->stub_sec->reloc_count;
8204 stub_entry->stub_sec->reloc_count += 1;
8205 r->r_offset = loc - stub_entry->stub_sec->contents;
8206 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
8207 r->r_addend = dest;
8208 if (stub_entry->h != NULL)
8209 {
8210 struct elf_link_hash_entry **hashes;
8211 unsigned long symndx;
8212 struct ppc_link_hash_entry *h;
8213
8214 hashes = elf_sym_hashes (htab->stub_bfd);
8215 if (hashes == NULL)
8216 {
8217 bfd_size_type hsize;
8218
8219 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
8220 hashes = bfd_zalloc (htab->stub_bfd, hsize);
8221 if (hashes == NULL)
8222 return FALSE;
8223 elf_sym_hashes (htab->stub_bfd) = hashes;
8224 htab->stub_globals = 1;
8225 }
8226 symndx = htab->stub_globals++;
8227 h = stub_entry->h;
8228 hashes[symndx] = &h->elf;
8229 r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
8230 if (h->oh != NULL && h->oh->is_func)
8231 h = h->oh;
8232 if (h->elf.root.u.def.section != stub_entry->target_section)
8233 /* H is an opd symbol. The addend must be zero. */
8234 r->r_addend = 0;
8235 else
8236 {
8237 off = (h->elf.root.u.def.value
8238 + h->elf.root.u.def.section->output_offset
8239 + h->elf.root.u.def.section->output_section->vma);
8240 r->r_addend -= off;
8241 }
8242 }
8243 }
721956f4 8244 break;
e86ce104 8245
721956f4 8246 case ppc_stub_plt_branch:
ad8e1ba5 8247 case ppc_stub_plt_branch_r2off:
721956f4
AM
8248 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
8249 stub_entry->root.string + 9,
b34976b6 8250 FALSE, FALSE);
721956f4
AM
8251 if (br_entry == NULL)
8252 {
8253 (*_bfd_error_handler) (_("can't find branch stub `%s'"),
5c3dead3 8254 stub_entry->root.string);
b34976b6
AM
8255 htab->stub_error = TRUE;
8256 return FALSE;
721956f4
AM
8257 }
8258
8259 off = (stub_entry->target_value
8260 + stub_entry->target_section->output_offset
8261 + stub_entry->target_section->output_section->vma);
8262
4ce794b7
AM
8263 bfd_put_64 (htab->brlt->owner, off,
8264 htab->brlt->contents + br_entry->offset);
721956f4 8265
ee75fd95 8266 if (htab->relbrlt != NULL)
721956f4
AM
8267 {
8268 /* Create a reloc for the branch lookup table entry. */
8269 Elf_Internal_Rela rela;
ad8e1ba5 8270 bfd_byte *rl;
5d1634d7 8271
721956f4 8272 rela.r_offset = (br_entry->offset
4ce794b7
AM
8273 + htab->brlt->output_offset
8274 + htab->brlt->output_section->vma);
721956f4
AM
8275 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
8276 rela.r_addend = off;
8277
4ce794b7
AM
8278 rl = htab->relbrlt->contents;
8279 rl += htab->relbrlt->reloc_count++ * sizeof (Elf64_External_Rela);
8280 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
721956f4
AM
8281 }
8282
8283 off = (br_entry->offset
4ce794b7
AM
8284 + htab->brlt->output_offset
8285 + htab->brlt->output_section->vma
8286 - elf_gp (htab->brlt->output_section->owner)
ad8e1ba5 8287 - htab->stub_group[stub_entry->id_sec->id].toc_off);
721956f4 8288
ad8e1ba5 8289 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
5d1634d7
AM
8290 {
8291 (*_bfd_error_handler)
e86ce104 8292 (_("linkage table error against `%s'"),
721956f4 8293 stub_entry->root.string);
5d1634d7 8294 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
8295 htab->stub_error = TRUE;
8296 return FALSE;
5d1634d7 8297 }
41bd81ab 8298
721956f4 8299 indx = off;
ad8e1ba5
AM
8300 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
8301 {
97b639ba 8302 bfd_put_32 (htab->stub_bfd, ADDIS_R12_R2 | PPC_HA (indx), loc);
ad8e1ba5 8303 loc += 4;
97b639ba 8304 bfd_put_32 (htab->stub_bfd, LD_R11_0R12 | PPC_LO (indx), loc);
ad8e1ba5
AM
8305 size = 16;
8306 }
8307 else
8308 {
8309 bfd_vma r2off;
8310
8311 r2off = (htab->stub_group[stub_entry->target_section->id].toc_off
8312 - htab->stub_group[stub_entry->id_sec->id].toc_off);
97b639ba 8313 bfd_put_32 (htab->stub_bfd, STD_R2_40R1, loc);
ad8e1ba5 8314 loc += 4;
97b639ba 8315 bfd_put_32 (htab->stub_bfd, ADDIS_R12_R2 | PPC_HA (indx), loc);
ad8e1ba5 8316 loc += 4;
97b639ba 8317 bfd_put_32 (htab->stub_bfd, LD_R11_0R12 | PPC_LO (indx), loc);
ad8e1ba5 8318 loc += 4;
97b639ba 8319 bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
ad8e1ba5 8320 loc += 4;
97b639ba 8321 bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
ad8e1ba5
AM
8322 size = 28;
8323 }
8324 loc += 4;
97b639ba 8325 bfd_put_32 (htab->stub_bfd, MTCTR_R11, loc);
ad8e1ba5 8326 loc += 4;
97b639ba 8327 bfd_put_32 (htab->stub_bfd, BCTR, loc);
721956f4 8328 break;
5d1634d7 8329
721956f4 8330 case ppc_stub_plt_call:
c862ae31
AM
8331 /* Do the best we can for shared libraries built without
8332 exporting ".foo" for each "foo". This can happen when symbol
8333 versioning scripts strip all bar a subset of symbols. */
8387904d
AM
8334 if (stub_entry->h->oh != NULL
8335 && stub_entry->h->oh->elf.root.type != bfd_link_hash_defined
34814b9f 8336 && stub_entry->h->oh->elf.root.type != bfd_link_hash_defweak)
c862ae31
AM
8337 {
8338 /* Point the symbol at the stub. There may be multiple stubs,
8339 we don't really care; The main thing is to make this sym
8f3bab57
AM
8340 defined somewhere. Maybe defining the symbol in the stub
8341 section is a silly idea. If we didn't do this, htab->top_id
8342 could disappear. */
34814b9f
AM
8343 stub_entry->h->oh->elf.root.type = bfd_link_hash_defined;
8344 stub_entry->h->oh->elf.root.u.def.section = stub_entry->stub_sec;
8345 stub_entry->h->oh->elf.root.u.def.value = stub_entry->stub_offset;
c862ae31
AM
8346 }
8347
721956f4 8348 /* Now build the stub. */
411e1bfb
AM
8349 off = (bfd_vma) -1;
8350 for (ent = stub_entry->h->elf.plt.plist; ent != NULL; ent = ent->next)
8351 if (ent->addend == stub_entry->addend)
8352 {
8353 off = ent->plt.offset;
8354 break;
8355 }
721956f4
AM
8356 if (off >= (bfd_vma) -2)
8357 abort ();
8358
8359 off &= ~ (bfd_vma) 1;
4ce794b7
AM
8360 off += (htab->plt->output_offset
8361 + htab->plt->output_section->vma
8362 - elf_gp (htab->plt->output_section->owner)
ad8e1ba5 8363 - htab->stub_group[stub_entry->id_sec->id].toc_off);
721956f4 8364
ad8e1ba5 8365 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
721956f4
AM
8366 {
8367 (*_bfd_error_handler)
8368 (_("linkage table error against `%s'"),
8369 stub_entry->h->elf.root.root.string);
8370 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
8371 htab->stub_error = TRUE;
8372 return FALSE;
721956f4
AM
8373 }
8374
97b639ba 8375 p = build_plt_stub (htab->stub_bfd, loc, off);
721956f4
AM
8376 size = p - loc;
8377 break;
8378
8379 default:
8380 BFD_FAIL ();
b34976b6 8381 return FALSE;
721956f4
AM
8382 }
8383
eea6121a 8384 stub_entry->stub_sec->size += size;
97b639ba 8385
ee75fd95 8386 if (htab->emit_stub_syms)
97b639ba
AM
8387 {
8388 struct elf_link_hash_entry *h;
ee75fd95
AM
8389 size_t len1, len2;
8390 char *name;
8391 const char *const stub_str[] = { "long_branch",
8392 "long_branch_r2off",
8393 "plt_branch",
8394 "plt_branch_r2off",
8395 "plt_call" };
8396
8397 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
8398 len2 = strlen (stub_entry->root.string);
8399 name = bfd_malloc (len1 + len2 + 2);
8400 if (name == NULL)
8401 return FALSE;
8402 memcpy (name, stub_entry->root.string, 9);
8403 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
8404 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
8405 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
97b639ba
AM
8406 if (h == NULL)
8407 return FALSE;
8408 if (h->root.type == bfd_link_hash_new)
8409 {
8410 h->root.type = bfd_link_hash_defined;
8411 h->root.u.def.section = stub_entry->stub_sec;
8412 h->root.u.def.value = stub_entry->stub_offset;
f5385ebf
AM
8413 h->ref_regular = 1;
8414 h->def_regular = 1;
8415 h->ref_regular_nonweak = 1;
8416 h->forced_local = 1;
8417 h->non_elf = 0;
97b639ba
AM
8418 }
8419 }
8420
b34976b6 8421 return TRUE;
721956f4
AM
8422}
8423
8424/* As above, but don't actually build the stub. Just bump offset so
8425 we know stub section sizes, and select plt_branch stubs where
8426 long_branch stubs won't do. */
8427
b34976b6 8428static bfd_boolean
4ce794b7 8429ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
721956f4
AM
8430{
8431 struct ppc_stub_hash_entry *stub_entry;
63bc6f6c 8432 struct bfd_link_info *info;
721956f4
AM
8433 struct ppc_link_hash_table *htab;
8434 bfd_vma off;
8435 int size;
8436
8437 /* Massage our args to the form they really have. */
8438 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
63bc6f6c
AM
8439 info = in_arg;
8440
8441 htab = ppc_hash_table (info);
721956f4
AM
8442
8443 if (stub_entry->stub_type == ppc_stub_plt_call)
8444 {
411e1bfb 8445 struct plt_entry *ent;
58ac9f71 8446 off = (bfd_vma) -1;
411e1bfb
AM
8447 for (ent = stub_entry->h->elf.plt.plist; ent != NULL; ent = ent->next)
8448 if (ent->addend == stub_entry->addend)
8449 {
8450 off = ent->plt.offset & ~(bfd_vma) 1;
8451 break;
8452 }
58ac9f71 8453 if (off >= (bfd_vma) -2)
411e1bfb 8454 abort ();
4ce794b7
AM
8455 off += (htab->plt->output_offset
8456 + htab->plt->output_section->vma
8457 - elf_gp (htab->plt->output_section->owner)
ad8e1ba5 8458 - htab->stub_group[stub_entry->id_sec->id].toc_off);
721956f4 8459
ad8e1ba5 8460 size = PLT_CALL_STUB_SIZE;
4ce794b7 8461 if (PPC_HA (off + 16) != PPC_HA (off))
721956f4
AM
8462 size += 4;
8463 }
8464 else
8465 {
ad8e1ba5
AM
8466 /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
8467 variants. */
721956f4
AM
8468 off = (stub_entry->target_value
8469 + stub_entry->target_section->output_offset
8470 + stub_entry->target_section->output_section->vma);
eea6121a 8471 off -= (stub_entry->stub_sec->size
721956f4
AM
8472 + stub_entry->stub_sec->output_offset
8473 + stub_entry->stub_sec->output_section->vma);
8474
ad8e1ba5
AM
8475 /* Reset the stub type from the plt variant in case we now
8476 can reach with a shorter stub. */
8477 if (stub_entry->stub_type >= ppc_stub_plt_branch)
8478 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
8479
8480 size = 4;
8481 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
8482 {
8483 off -= 12;
8484 size = 16;
8485 }
8486
8487 /* If the branch offset if too big, use a ppc_stub_plt_branch. */
721956f4
AM
8488 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
8489 {
8490 struct ppc_branch_hash_entry *br_entry;
8491
8492 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
8493 stub_entry->root.string + 9,
b34976b6 8494 TRUE, FALSE);
721956f4
AM
8495 if (br_entry == NULL)
8496 {
8497 (*_bfd_error_handler) (_("can't build branch stub `%s'"),
5c3dead3 8498 stub_entry->root.string);
b34976b6
AM
8499 htab->stub_error = TRUE;
8500 return FALSE;
721956f4
AM
8501 }
8502
8503 if (br_entry->iter != htab->stub_iteration)
8504 {
8505 br_entry->iter = htab->stub_iteration;
eea6121a
AM
8506 br_entry->offset = htab->brlt->size;
8507 htab->brlt->size += 8;
63bc6f6c 8508
ee75fd95 8509 if (htab->relbrlt != NULL)
eea6121a 8510 htab->relbrlt->size += sizeof (Elf64_External_Rela);
721956f4 8511 }
ad8e1ba5
AM
8512
8513 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
721956f4 8514 size = 16;
ad8e1ba5
AM
8515 if (stub_entry->stub_type != ppc_stub_plt_branch)
8516 size = 28;
721956f4 8517 }
ee75fd95
AM
8518
8519 if (info->emitrelocations
8520 && (stub_entry->stub_type == ppc_stub_long_branch
8521 || stub_entry->stub_type == ppc_stub_long_branch_r2off))
8522 stub_entry->stub_sec->reloc_count += 1;
721956f4
AM
8523 }
8524
eea6121a 8525 stub_entry->stub_sec->size += size;
b34976b6 8526 return TRUE;
721956f4
AM
8527}
8528
8529/* Set up various things so that we can make a list of input sections
8530 for each output section included in the link. Returns -1 on error,
cedb70c5 8531 0 when no stubs will be needed, and 1 on success. */
721956f4
AM
8532
8533int
4c52953f
AM
8534ppc64_elf_setup_section_lists (bfd *output_bfd,
8535 struct bfd_link_info *info,
8536 int no_multi_toc)
721956f4
AM
8537{
8538 bfd *input_bfd;
734b6cf9 8539 int top_id, top_index, id;
721956f4 8540 asection *section;
734b6cf9 8541 asection **input_list;
721956f4
AM
8542 bfd_size_type amt;
8543 struct ppc_link_hash_table *htab = ppc_hash_table (info);
8544
4c52953f
AM
8545 htab->no_multi_toc = no_multi_toc;
8546
4ce794b7 8547 if (htab->brlt == NULL)
721956f4
AM
8548 return 0;
8549
1e2f5b6e 8550 /* Find the top input section id. */
3d6f9012 8551 for (input_bfd = info->input_bfds, top_id = 3;
721956f4
AM
8552 input_bfd != NULL;
8553 input_bfd = input_bfd->link_next)
8554 {
721956f4
AM
8555 for (section = input_bfd->sections;
8556 section != NULL;
8557 section = section->next)
8558 {
8559 if (top_id < section->id)
8560 top_id = section->id;
8561 }
8562 }
721956f4 8563
8f3bab57 8564 htab->top_id = top_id;
721956f4 8565 amt = sizeof (struct map_stub) * (top_id + 1);
4ce794b7 8566 htab->stub_group = bfd_zmalloc (amt);
721956f4
AM
8567 if (htab->stub_group == NULL)
8568 return -1;
8569
3d6f9012
AM
8570 /* Set toc_off for com, und, abs and ind sections. */
8571 for (id = 0; id < 3; id++)
8572 htab->stub_group[id].toc_off = TOC_BASE_OFF;
721956f4 8573
3d6f9012 8574 elf_gp (output_bfd) = htab->toc_curr = ppc64_elf_toc (output_bfd);
734b6cf9
AM
8575
8576 /* We can't use output_bfd->section_count here to find the top output
8577 section index as some sections may have been removed, and
8423293d 8578 strip_excluded_output_sections doesn't renumber the indices. */
734b6cf9
AM
8579 for (section = output_bfd->sections, top_index = 0;
8580 section != NULL;
8581 section = section->next)
8582 {
8583 if (top_index < section->index)
8584 top_index = section->index;
8585 }
8586
8587 htab->top_index = top_index;
8588 amt = sizeof (asection *) * (top_index + 1);
4ce794b7 8589 input_list = bfd_zmalloc (amt);
734b6cf9
AM
8590 htab->input_list = input_list;
8591 if (input_list == NULL)
8592 return -1;
8593
721956f4
AM
8594 return 1;
8595}
8596
e717da7e
AM
8597/* The linker repeatedly calls this function for each TOC input section
8598 and linker generated GOT section. Group input bfds such that the toc
8599 within a group is less than 64k in size. Will break with cute linker
8600 scripts that play games with dot in the output toc section. */
ad8e1ba5
AM
8601
8602void
4ce794b7 8603ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
ad8e1ba5
AM
8604{
8605 struct ppc_link_hash_table *htab = ppc_hash_table (info);
8606
4c52953f
AM
8607 if (!htab->no_multi_toc)
8608 {
8609 bfd_vma addr = isec->output_offset + isec->output_section->vma;
8610 bfd_vma off = addr - htab->toc_curr;
8611
8612 if (off + isec->size > 0x10000)
8613 htab->toc_curr = addr;
99877b66 8614
4c52953f
AM
8615 elf_gp (isec->owner) = (htab->toc_curr
8616 - elf_gp (isec->output_section->owner)
8617 + TOC_BASE_OFF);
8618 }
ad8e1ba5
AM
8619}
8620
8621/* Called after the last call to the above function. */
8622
8623void
4c52953f 8624ppc64_elf_reinit_toc (bfd *output_bfd, struct bfd_link_info *info)
ad8e1ba5
AM
8625{
8626 struct ppc_link_hash_table *htab = ppc_hash_table (info);
ad8e1ba5 8627
4c52953f
AM
8628 htab->multi_toc_needed = htab->toc_curr != elf_gp (output_bfd);
8629
ad8e1ba5
AM
8630 /* toc_curr tracks the TOC offset used for code sections below in
8631 ppc64_elf_next_input_section. Start off at 0x8000. */
3d6f9012 8632 htab->toc_curr = TOC_BASE_OFF;
ad8e1ba5
AM
8633}
8634
9b5ecbd0
AM
8635/* No toc references were found in ISEC. If the code in ISEC makes no
8636 calls, then there's no need to use toc adjusting stubs when branching
8637 into ISEC. Actually, indirect calls from ISEC are OK as they will
4c52953f
AM
8638 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
8639 needed, and 2 if a cyclical call-graph was found but no other reason
8640 for a stub was detected. If called from the top level, a return of
8641 2 means the same as a return of 0. */
9b5ecbd0
AM
8642
8643static int
4ce794b7 8644toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
9b5ecbd0 8645{
4c52953f
AM
8646 Elf_Internal_Rela *relstart, *rel;
8647 Elf_Internal_Sym *local_syms;
9b5ecbd0 8648 int ret;
4c52953f 8649 struct ppc_link_hash_table *htab;
9b5ecbd0 8650
772119ce
AM
8651 /* We know none of our code bearing sections will need toc stubs. */
8652 if ((isec->flags & SEC_LINKER_CREATED) != 0)
8653 return 0;
8654
eea6121a 8655 if (isec->size == 0)
082c50f8
AM
8656 return 0;
8657
4c52953f
AM
8658 if (isec->output_section == NULL)
8659 return 0;
8660
9b5ecbd0
AM
8661 /* Hack for linux kernel. .fixup contains branches, but only back to
8662 the function that hit an exception. */
4c52953f
AM
8663 if (strcmp (isec->name, ".fixup") == 0)
8664 return 0;
9b5ecbd0 8665
4c52953f
AM
8666 if (isec->reloc_count == 0)
8667 return 0;
8668
8669 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
8670 info->keep_memory);
8671 if (relstart == NULL)
8672 return -1;
8673
8674 /* Look for branches to outside of this section. */
8675 local_syms = NULL;
8676 ret = 0;
8677 htab = ppc_hash_table (info);
8678 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
9b5ecbd0 8679 {
4c52953f
AM
8680 enum elf_ppc64_reloc_type r_type;
8681 unsigned long r_symndx;
8682 struct elf_link_hash_entry *h;
8683 Elf_Internal_Sym *sym;
8684 asection *sym_sec;
8685 long *opd_adjust;
8686 bfd_vma sym_value;
8687 bfd_vma dest;
8688
8689 r_type = ELF64_R_TYPE (rel->r_info);
8690 if (r_type != R_PPC64_REL24
8691 && r_type != R_PPC64_REL14
8692 && r_type != R_PPC64_REL14_BRTAKEN
8693 && r_type != R_PPC64_REL14_BRNTAKEN)
8694 continue;
8695
8696 r_symndx = ELF64_R_SYM (rel->r_info);
8697 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
8698 isec->owner))
9b5ecbd0 8699 {
4c52953f
AM
8700 ret = -1;
8701 break;
9b5ecbd0 8702 }
9b5ecbd0 8703
2917689a
AM
8704 /* Calls to dynamic lib functions go through a plt call stub
8705 that uses r2. Branches to undefined symbols might be a call
8706 using old-style dot symbols that can be satisfied by a plt
8707 call into a new-style dynamic library. */
4c52953f 8708 if (sym_sec == NULL)
2917689a
AM
8709 {
8710 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
8711 if (eh != NULL
8712 && eh->oh != NULL
8713 && eh->oh->elf.plt.plist != NULL)
8714 {
8715 ret = 1;
8716 break;
8717 }
4c52953f 8718
2917689a
AM
8719 /* Ignore other undefined symbols. */
8720 continue;
8721 }
8722
8723 /* Assume branches to other sections not included in the link need
8724 stubs too, to cover -R and absolute syms. */
4c52953f
AM
8725 if (sym_sec->output_section == NULL)
8726 {
8727 ret = 1;
8728 break;
8729 }
8730
8731 if (h == NULL)
8732 sym_value = sym->st_value;
8733 else
8734 {
8735 if (h->root.type != bfd_link_hash_defined
8736 && h->root.type != bfd_link_hash_defweak)
8737 abort ();
8738 sym_value = h->root.u.def.value;
8739 }
8740 sym_value += rel->r_addend;
8741
8742 /* If this branch reloc uses an opd sym, find the code section. */
8743 opd_adjust = get_opd_info (sym_sec);
8744 if (opd_adjust != NULL)
8745 {
4c52953f
AM
8746 if (h == NULL)
8747 {
8748 long adjust;
8749
8750 adjust = opd_adjust[sym->st_value / 8];
8751 if (adjust == -1)
8752 /* Assume deleted functions won't ever be called. */
8753 continue;
8754 sym_value += adjust;
8755 }
8756
8757 dest = opd_entry_value (sym_sec, sym_value, &sym_sec, NULL);
8758 if (dest == (bfd_vma) -1)
8759 continue;
8760 }
8761 else
8762 dest = (sym_value
8763 + sym_sec->output_offset
8764 + sym_sec->output_section->vma);
8765
8766 /* Ignore branch to self. */
8767 if (sym_sec == isec)
8768 continue;
8769
8770 /* If the called function uses the toc, we need a stub. */
8771 if (sym_sec->has_toc_reloc
8772 || sym_sec->makes_toc_func_call)
8773 {
8774 ret = 1;
8775 break;
8776 }
8777
8778 /* Assume any branch that needs a long branch stub might in fact
8779 need a plt_branch stub. A plt_branch stub uses r2. */
8780 else if (dest - (isec->output_offset
8781 + isec->output_section->vma
8782 + rel->r_offset) + (1 << 25) >= (2 << 25))
9b5ecbd0
AM
8783 {
8784 ret = 1;
8785 break;
8786 }
4c52953f
AM
8787
8788 /* If calling back to a section in the process of being tested, we
8789 can't say for sure that no toc adjusting stubs are needed, so
8790 don't return zero. */
8791 else if (sym_sec->call_check_in_progress)
8792 ret = 2;
8793
8794 /* Branches to another section that itself doesn't have any TOC
8795 references are OK. Recursively call ourselves to check. */
8796 else if (sym_sec->id <= htab->top_id
8797 && htab->stub_group[sym_sec->id].toc_off == 0)
8798 {
8799 int recur;
8800
8801 /* Mark current section as indeterminate, so that other
8802 sections that call back to current won't be marked as
8803 known. */
8804 isec->call_check_in_progress = 1;
8805 recur = toc_adjusting_stub_needed (info, sym_sec);
8806 isec->call_check_in_progress = 0;
8807
8808 if (recur < 0)
8809 {
8810 /* An error. Exit. */
8811 ret = -1;
8812 break;
8813 }
8814 else if (recur <= 1)
8815 {
8816 /* Known result. Mark as checked and set section flag. */
8817 htab->stub_group[sym_sec->id].toc_off = 1;
8818 if (recur != 0)
8819 {
8820 sym_sec->makes_toc_func_call = 1;
8821 ret = 1;
8822 break;
8823 }
8824 }
8825 else
8826 {
8827 /* Unknown result. Continue checking. */
8828 ret = 2;
8829 }
8830 }
9b5ecbd0
AM
8831 }
8832
4c52953f
AM
8833 if (local_syms != NULL
8834 && (elf_tdata (isec->owner)->symtab_hdr.contents
8835 != (unsigned char *) local_syms))
8836 free (local_syms);
8837 if (elf_section_data (isec)->relocs != relstart)
8838 free (relstart);
8839
9b5ecbd0
AM
8840 return ret;
8841}
8842
721956f4
AM
8843/* The linker repeatedly calls this function for each input section,
8844 in the order that input sections are linked into output sections.
8845 Build lists of input sections to determine groupings between which
8846 we may insert linker stubs. */
8847
9b5ecbd0 8848bfd_boolean
4ce794b7 8849ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
721956f4
AM
8850{
8851 struct ppc_link_hash_table *htab = ppc_hash_table (info);
8852
734b6cf9
AM
8853 if ((isec->output_section->flags & SEC_CODE) != 0
8854 && isec->output_section->index <= htab->top_index)
721956f4 8855 {
734b6cf9 8856 asection **list = htab->input_list + isec->output_section->index;
3d6f9012 8857 /* Steal the link_sec pointer for our list. */
721956f4 8858#define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
3d6f9012
AM
8859 /* This happens to make the list in reverse order,
8860 which is what we want. */
734b6cf9
AM
8861 PREV_SEC (isec) = *list;
8862 *list = isec;
721956f4 8863 }
ad8e1ba5 8864
4c52953f 8865 if (htab->multi_toc_needed)
9b5ecbd0 8866 {
4c52953f
AM
8867 /* If a code section has a function that uses the TOC then we need
8868 to use the right TOC (obviously). Also, make sure that .opd gets
8869 the correct TOC value for R_PPC64_TOC relocs that don't have or
8870 can't find their function symbol (shouldn't ever happen now). */
8871 if (isec->has_toc_reloc || (isec->flags & SEC_CODE) == 0)
8872 {
8873 if (elf_gp (isec->owner) != 0)
8874 htab->toc_curr = elf_gp (isec->owner);
8875 }
8876 else if (htab->stub_group[isec->id].toc_off == 0)
8877 {
8878 int ret = toc_adjusting_stub_needed (info, isec);
8879 if (ret < 0)
8880 return FALSE;
8881 else
8882 isec->makes_toc_func_call = ret & 1;
8883 }
9b5ecbd0 8884 }
ad8e1ba5
AM
8885
8886 /* Functions that don't use the TOC can belong in any TOC group.
8887 Use the last TOC base. This happens to make _init and _fini
8888 pasting work. */
8889 htab->stub_group[isec->id].toc_off = htab->toc_curr;
9b5ecbd0 8890 return TRUE;
721956f4
AM
8891}
8892
8893/* See whether we can group stub sections together. Grouping stub
8894 sections may result in fewer stubs. More importantly, we need to
8895 put all .init* and .fini* stubs at the beginning of the .init or
8896 .fini output sections respectively, because glibc splits the
8897 _init and _fini functions into multiple parts. Putting a stub in
8898 the middle of a function is not a good idea. */
8899
8900static void
4ce794b7
AM
8901group_sections (struct ppc_link_hash_table *htab,
8902 bfd_size_type stub_group_size,
8903 bfd_boolean stubs_always_before_branch)
721956f4 8904{
734b6cf9
AM
8905 asection **list = htab->input_list + htab->top_index;
8906 do
721956f4 8907 {
734b6cf9
AM
8908 asection *tail = *list;
8909 while (tail != NULL)
721956f4 8910 {
734b6cf9
AM
8911 asection *curr;
8912 asection *prev;
8913 bfd_size_type total;
8914 bfd_boolean big_sec;
8915 bfd_vma curr_toc;
8916
8917 curr = tail;
eea6121a 8918 total = tail->size;
5c3dead3
AM
8919 big_sec = total > stub_group_size;
8920 if (big_sec)
8921 (*_bfd_error_handler) (_("%B section %A exceeds stub group size"),
8922 tail->owner, tail);
734b6cf9
AM
8923 curr_toc = htab->stub_group[tail->id].toc_off;
8924
8925 while ((prev = PREV_SEC (curr)) != NULL
8926 && ((total += curr->output_offset - prev->output_offset)
ad8e1ba5
AM
8927 < stub_group_size)
8928 && htab->stub_group[prev->id].toc_off == curr_toc)
734b6cf9
AM
8929 curr = prev;
8930
8931 /* OK, the size from the start of CURR to the end is less
8932 than stub_group_size and thus can be handled by one stub
8933 section. (or the tail section is itself larger than
8934 stub_group_size, in which case we may be toast.) We
8935 should really be keeping track of the total size of stubs
8936 added here, as stubs contribute to the final output
8937 section size. That's a little tricky, and this way will
8938 only break if stubs added make the total size more than
8939 2^25, ie. for the default stub_group_size, if stubs total
8940 more than 2097152 bytes, or nearly 75000 plt call stubs. */
8941 do
721956f4
AM
8942 {
8943 prev = PREV_SEC (tail);
734b6cf9 8944 /* Set up this stub group. */
721956f4
AM
8945 htab->stub_group[tail->id].link_sec = curr;
8946 }
734b6cf9
AM
8947 while (tail != curr && (tail = prev) != NULL);
8948
8949 /* But wait, there's more! Input sections up to stub_group_size
8950 bytes before the stub section can be handled by it too.
8951 Don't do this if we have a really large section after the
8952 stubs, as adding more stubs increases the chance that
8953 branches may not reach into the stub section. */
8954 if (!stubs_always_before_branch && !big_sec)
8955 {
8956 total = 0;
8957 while (prev != NULL
8958 && ((total += tail->output_offset - prev->output_offset)
8959 < stub_group_size)
8960 && htab->stub_group[prev->id].toc_off == curr_toc)
8961 {
8962 tail = prev;
8963 prev = PREV_SEC (tail);
8964 htab->stub_group[tail->id].link_sec = curr;
8965 }
8966 }
8967 tail = prev;
721956f4
AM
8968 }
8969 }
734b6cf9
AM
8970 while (list-- != htab->input_list);
8971 free (htab->input_list);
721956f4
AM
8972#undef PREV_SEC
8973}
8974
721956f4
AM
8975/* Determine and set the size of the stub section for a final link.
8976
8977 The basic idea here is to examine all the relocations looking for
8978 PC-relative calls to a target that is unreachable with a "bl"
8979 instruction. */
8980
b34976b6 8981bfd_boolean
4ce794b7 8982ppc64_elf_size_stubs (bfd *output_bfd,
4ce794b7
AM
8983 struct bfd_link_info *info,
8984 bfd_signed_vma group_size,
8985 asection *(*add_stub_section) (const char *, asection *),
8986 void (*layout_sections_again) (void))
721956f4
AM
8987{
8988 bfd_size_type stub_group_size;
b34976b6 8989 bfd_boolean stubs_always_before_branch;
721956f4
AM
8990 struct ppc_link_hash_table *htab = ppc_hash_table (info);
8991
8992 /* Stash our params away. */
721956f4
AM
8993 htab->add_stub_section = add_stub_section;
8994 htab->layout_sections_again = layout_sections_again;
8995 stubs_always_before_branch = group_size < 0;
8996 if (group_size < 0)
8997 stub_group_size = -group_size;
8998 else
8999 stub_group_size = group_size;
9000 if (stub_group_size == 1)
9001 {
9002 /* Default values. */
58ac9f71
AM
9003 if (stubs_always_before_branch)
9004 {
9005 stub_group_size = 0x1e00000;
9006 if (htab->has_14bit_branch)
9007 stub_group_size = 0x7800;
9008 }
9009 else
9010 {
9011 stub_group_size = 0x1c00000;
9012 if (htab->has_14bit_branch)
9013 stub_group_size = 0x7000;
9014 }
721956f4
AM
9015 }
9016
9017 group_sections (htab, stub_group_size, stubs_always_before_branch);
9018
721956f4
AM
9019 while (1)
9020 {
9021 bfd *input_bfd;
9022 unsigned int bfd_indx;
9023 asection *stub_sec;
721956f4
AM
9024
9025 htab->stub_iteration += 1;
721956f4
AM
9026
9027 for (input_bfd = info->input_bfds, bfd_indx = 0;
9028 input_bfd != NULL;
9029 input_bfd = input_bfd->link_next, bfd_indx++)
9030 {
9031 Elf_Internal_Shdr *symtab_hdr;
9032 asection *section;
6cdc0ccc 9033 Elf_Internal_Sym *local_syms = NULL;
721956f4 9034
67f93c31
AM
9035 if (!is_ppc64_elf_target (input_bfd->xvec))
9036 continue;
9037
721956f4
AM
9038 /* We'll need the symbol table in a second. */
9039 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9040 if (symtab_hdr->sh_info == 0)
9041 continue;
9042
721956f4
AM
9043 /* Walk over each section attached to the input bfd. */
9044 for (section = input_bfd->sections;
9045 section != NULL;
9046 section = section->next)
9047 {
721956f4 9048 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
721956f4
AM
9049
9050 /* If there aren't any relocs, then there's nothing more
9051 to do. */
9052 if ((section->flags & SEC_RELOC) == 0
9053 || section->reloc_count == 0)
9054 continue;
9055
9056 /* If this section is a link-once section that will be
9057 discarded, then don't create any stubs. */
9058 if (section->output_section == NULL
9059 || section->output_section->owner != output_bfd)
9060 continue;
9061
1e2f5b6e
AM
9062 /* Get the relocs. */
9063 internal_relocs
4ce794b7 9064 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
45d6a902 9065 info->keep_memory);
721956f4 9066 if (internal_relocs == NULL)
1e2f5b6e 9067 goto error_ret_free_local;
721956f4
AM
9068
9069 /* Now examine each relocation. */
9070 irela = internal_relocs;
9071 irelaend = irela + section->reloc_count;
9072 for (; irela < irelaend; irela++)
9073 {
4ce794b7
AM
9074 enum elf_ppc64_reloc_type r_type;
9075 unsigned int r_indx;
721956f4
AM
9076 enum ppc_stub_type stub_type;
9077 struct ppc_stub_hash_entry *stub_entry;
8387904d 9078 asection *sym_sec, *code_sec;
721956f4
AM
9079 bfd_vma sym_value;
9080 bfd_vma destination;
8843416a 9081 bfd_boolean ok_dest;
721956f4 9082 struct ppc_link_hash_entry *hash;
8387904d 9083 struct ppc_link_hash_entry *fdh;
411e1bfb
AM
9084 struct elf_link_hash_entry *h;
9085 Elf_Internal_Sym *sym;
721956f4
AM
9086 char *stub_name;
9087 const asection *id_sec;
8387904d 9088 long *opd_adjust;
721956f4
AM
9089
9090 r_type = ELF64_R_TYPE (irela->r_info);
9091 r_indx = ELF64_R_SYM (irela->r_info);
9092
4ce794b7 9093 if (r_type >= R_PPC64_max)
721956f4
AM
9094 {
9095 bfd_set_error (bfd_error_bad_value);
6cdc0ccc 9096 goto error_ret_free_internal;
721956f4
AM
9097 }
9098
9099 /* Only look for stubs on branch instructions. */
4ce794b7
AM
9100 if (r_type != R_PPC64_REL24
9101 && r_type != R_PPC64_REL14
9102 && r_type != R_PPC64_REL14_BRTAKEN
9103 && r_type != R_PPC64_REL14_BRNTAKEN)
721956f4
AM
9104 continue;
9105
9106 /* Now determine the call target, its name, value,
9107 section. */
411e1bfb
AM
9108 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9109 r_indx, input_bfd))
9110 goto error_ret_free_internal;
9111 hash = (struct ppc_link_hash_entry *) h;
9112
8843416a 9113 ok_dest = FALSE;
8387904d 9114 fdh = NULL;
7fe2b9a6 9115 sym_value = 0;
411e1bfb 9116 if (hash == NULL)
721956f4 9117 {
411e1bfb 9118 sym_value = sym->st_value;
8843416a 9119 ok_dest = TRUE;
721956f4 9120 }
7fe2b9a6
AM
9121 else if (hash->elf.root.type == bfd_link_hash_defined
9122 || hash->elf.root.type == bfd_link_hash_defweak)
9123 {
9124 sym_value = hash->elf.root.u.def.value;
9125 if (sym_sec->output_section != NULL)
9126 ok_dest = TRUE;
9127 }
9128 else if (hash->elf.root.type == bfd_link_hash_undefweak
9129 || hash->elf.root.type == bfd_link_hash_undefined)
721956f4 9130 {
99877b66 9131 /* Recognise an old ABI func code entry sym, and
7fe2b9a6
AM
9132 use the func descriptor sym instead if it is
9133 defined. */
ceb1f1ef 9134 if (hash->elf.root.root.string[0] == '.'
8387904d
AM
9135 && (fdh = get_fdh (hash, htab)) != NULL)
9136 {
8387904d
AM
9137 if (fdh->elf.root.type == bfd_link_hash_defined
9138 || fdh->elf.root.type == bfd_link_hash_defweak)
9139 {
9140 sym_sec = fdh->elf.root.u.def.section;
9141 sym_value = fdh->elf.root.u.def.value;
9142 if (sym_sec->output_section != NULL)
9143 ok_dest = TRUE;
9144 }
99877b66
AM
9145 else
9146 fdh = NULL;
8387904d 9147 }
7fe2b9a6
AM
9148 }
9149 else
9150 {
9151 bfd_set_error (bfd_error_bad_value);
9152 goto error_ret_free_internal;
721956f4
AM
9153 }
9154
8843416a
AM
9155 destination = 0;
9156 if (ok_dest)
9157 {
9158 sym_value += irela->r_addend;
9159 destination = (sym_value
9160 + sym_sec->output_offset
9161 + sym_sec->output_section->vma);
9162 }
9163
8387904d
AM
9164 code_sec = sym_sec;
9165 opd_adjust = get_opd_info (sym_sec);
9166 if (opd_adjust != NULL)
9167 {
9168 bfd_vma dest;
9169
9170 if (hash == NULL)
9171 {
3f764659 9172 long adjust = opd_adjust[sym_value / 8];
8387904d
AM
9173 if (adjust == -1)
9174 continue;
9175 sym_value += adjust;
9176 }
9177 dest = opd_entry_value (sym_sec, sym_value,
9178 &code_sec, &sym_value);
9179 if (dest != (bfd_vma) -1)
9180 {
9181 destination = dest;
9182 if (fdh != NULL)
9183 {
9184 /* Fixup old ABI sym to point at code
9185 entry. */
99877b66 9186 hash->elf.root.type = bfd_link_hash_defweak;
8387904d
AM
9187 hash->elf.root.u.def.section = code_sec;
9188 hash->elf.root.u.def.value = sym_value;
9189 }
9190 }
9191 }
9192
721956f4
AM
9193 /* Determine what (if any) linker stub is needed. */
9194 stub_type = ppc_type_of_stub (section, irela, &hash,
9195 destination);
ad8e1ba5
AM
9196
9197 if (stub_type != ppc_stub_plt_call)
9198 {
9199 /* Check whether we need a TOC adjusting stub.
9200 Since the linker pastes together pieces from
9201 different object files when creating the
9202 _init and _fini functions, it may be that a
9203 call to what looks like a local sym is in
9204 fact a call needing a TOC adjustment. */
8387904d
AM
9205 if (code_sec != NULL
9206 && code_sec->output_section != NULL
9207 && (htab->stub_group[code_sec->id].toc_off
9b5ecbd0 9208 != htab->stub_group[section->id].toc_off)
4c52953f
AM
9209 && (code_sec->has_toc_reloc
9210 || code_sec->makes_toc_func_call))
ad8e1ba5
AM
9211 stub_type = ppc_stub_long_branch_r2off;
9212 }
9213
721956f4
AM
9214 if (stub_type == ppc_stub_none)
9215 continue;
9216
411e1bfb
AM
9217 /* __tls_get_addr calls might be eliminated. */
9218 if (stub_type != ppc_stub_plt_call
9219 && hash != NULL
8387904d
AM
9220 && (hash == htab->tls_get_addr
9221 || hash == htab->tls_get_addr_fd)
411e1bfb
AM
9222 && section->has_tls_reloc
9223 && irela != internal_relocs)
9224 {
9225 /* Get tls info. */
e7b938ca 9226 char *tls_mask;
411e1bfb 9227
0d4792f7 9228 if (!get_tls_mask (&tls_mask, NULL, &local_syms,
411e1bfb
AM
9229 irela - 1, input_bfd))
9230 goto error_ret_free_internal;
e7b938ca 9231 if (*tls_mask != 0)
411e1bfb
AM
9232 continue;
9233 }
9234
721956f4
AM
9235 /* Support for grouping stub sections. */
9236 id_sec = htab->stub_group[section->id].link_sec;
9237
9238 /* Get the name of this stub. */
9239 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
9240 if (!stub_name)
9241 goto error_ret_free_internal;
9242
9243 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 9244 stub_name, FALSE, FALSE);
721956f4
AM
9245 if (stub_entry != NULL)
9246 {
9247 /* The proper stub has already been created. */
9248 free (stub_name);
9249 continue;
9250 }
9251
9252 stub_entry = ppc_add_stub (stub_name, section, htab);
9253 if (stub_entry == NULL)
9254 {
9255 free (stub_name);
6cdc0ccc
AM
9256 error_ret_free_internal:
9257 if (elf_section_data (section)->relocs == NULL)
9258 free (internal_relocs);
9259 error_ret_free_local:
9260 if (local_syms != NULL
9261 && (symtab_hdr->contents
9262 != (unsigned char *) local_syms))
9263 free (local_syms);
b34976b6 9264 return FALSE;
721956f4
AM
9265 }
9266
ad8e1ba5 9267 stub_entry->stub_type = stub_type;
721956f4 9268 stub_entry->target_value = sym_value;
8387904d 9269 stub_entry->target_section = code_sec;
721956f4 9270 stub_entry->h = hash;
411e1bfb 9271 stub_entry->addend = irela->r_addend;
ee75fd95
AM
9272
9273 if (stub_entry->h != NULL)
9274 htab->stub_globals += 1;
721956f4
AM
9275 }
9276
9277 /* We're done with the internal relocs, free them. */
6cdc0ccc 9278 if (elf_section_data (section)->relocs != internal_relocs)
1e2f5b6e 9279 free (internal_relocs);
721956f4 9280 }
6cdc0ccc
AM
9281
9282 if (local_syms != NULL
9283 && symtab_hdr->contents != (unsigned char *) local_syms)
9284 {
9285 if (!info->keep_memory)
9286 free (local_syms);
9287 else
9288 symtab_hdr->contents = (unsigned char *) local_syms;
9289 }
721956f4
AM
9290 }
9291
5c3dead3 9292 /* We may have added some stubs. Find out the new size of the
721956f4
AM
9293 stub sections. */
9294 for (stub_sec = htab->stub_bfd->sections;
9295 stub_sec != NULL;
9296 stub_sec = stub_sec->next)
e717da7e 9297 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
ee75fd95 9298 {
5c3dead3 9299 stub_sec->rawsize = stub_sec->size;
ee75fd95
AM
9300 stub_sec->size = 0;
9301 stub_sec->reloc_count = 0;
9302 }
eea6121a
AM
9303
9304 htab->brlt->size = 0;
ee75fd95 9305 if (htab->relbrlt != NULL)
eea6121a 9306 htab->relbrlt->size = 0;
721956f4 9307
63bc6f6c 9308 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
721956f4 9309
5c3dead3
AM
9310 for (stub_sec = htab->stub_bfd->sections;
9311 stub_sec != NULL;
9312 stub_sec = stub_sec->next)
9313 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
9314 && stub_sec->rawsize != stub_sec->size)
9315 break;
9316
9317 /* Exit from this loop when no stubs have been added, and no stubs
9318 have changed size. */
9319 if (stub_sec == NULL)
9320 break;
9321
721956f4
AM
9322 /* Ask the linker to do its stuff. */
9323 (*htab->layout_sections_again) ();
9324 }
9325
c456f082 9326 /* It would be nice to strip htab->brlt from the output if the
afbe61cf
AM
9327 section is empty, but it's too late. If we strip sections here,
9328 the dynamic symbol table is corrupted since the section symbol
9329 for the stripped section isn't written. */
721956f4 9330
b34976b6 9331 return TRUE;
721956f4
AM
9332}
9333
9334/* Called after we have determined section placement. If sections
805fc799 9335 move, we'll be called again. Provide a value for TOCstart. */
721956f4 9336
805fc799 9337bfd_vma
4ce794b7 9338ppc64_elf_toc (bfd *obfd)
721956f4 9339{
805fc799
AM
9340 asection *s;
9341 bfd_vma TOCstart;
721956f4 9342
805fc799
AM
9343 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
9344 order. The TOC starts where the first of these sections starts. */
9345 s = bfd_get_section_by_name (obfd, ".got");
9346 if (s == NULL)
9347 s = bfd_get_section_by_name (obfd, ".toc");
9348 if (s == NULL)
9349 s = bfd_get_section_by_name (obfd, ".tocbss");
9350 if (s == NULL)
9351 s = bfd_get_section_by_name (obfd, ".plt");
9352 if (s == NULL)
9353 {
9354 /* This may happen for
9355 o references to TOC base (SYM@toc / TOC[tc0]) without a
9356 .toc directive
9357 o bad linker script
9358 o --gc-sections and empty TOC sections
9359
9360 FIXME: Warn user? */
9361
9362 /* Look for a likely section. We probably won't even be
9363 using TOCstart. */
9364 for (s = obfd->sections; s != NULL; s = s->next)
9365 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY))
9366 == (SEC_ALLOC | SEC_SMALL_DATA))
9367 break;
721956f4 9368 if (s == NULL)
805fc799
AM
9369 for (s = obfd->sections; s != NULL; s = s->next)
9370 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA))
9371 == (SEC_ALLOC | SEC_SMALL_DATA))
9372 break;
721956f4 9373 if (s == NULL)
805fc799
AM
9374 for (s = obfd->sections; s != NULL; s = s->next)
9375 if ((s->flags & (SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
9376 break;
721956f4 9377 if (s == NULL)
805fc799
AM
9378 for (s = obfd->sections; s != NULL; s = s->next)
9379 if ((s->flags & SEC_ALLOC) == SEC_ALLOC)
9380 break;
9381 }
721956f4 9382
805fc799
AM
9383 TOCstart = 0;
9384 if (s != NULL)
9385 TOCstart = s->output_section->vma + s->output_offset;
721956f4 9386
805fc799 9387 return TOCstart;
721956f4
AM
9388}
9389
9390/* Build all the stubs associated with the current output file.
9391 The stubs are kept in a hash table attached to the main linker
9392 hash table. This function is called via gldelf64ppc_finish. */
9393
b34976b6 9394bfd_boolean
4ce794b7
AM
9395ppc64_elf_build_stubs (bfd_boolean emit_stub_syms,
9396 struct bfd_link_info *info,
9397 char **stats)
5d1634d7
AM
9398{
9399 struct ppc_link_hash_table *htab = ppc_hash_table (info);
721956f4 9400 asection *stub_sec;
5d1634d7 9401 bfd_byte *p;
e717da7e 9402 int stub_sec_count = 0;
5d1634d7 9403
ad8e1ba5 9404 htab->emit_stub_syms = emit_stub_syms;
eea6121a
AM
9405
9406 /* Allocate memory to hold the linker stubs. */
721956f4
AM
9407 for (stub_sec = htab->stub_bfd->sections;
9408 stub_sec != NULL;
9409 stub_sec = stub_sec->next)
eea6121a
AM
9410 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
9411 && stub_sec->size != 0)
e717da7e 9412 {
eea6121a
AM
9413 stub_sec->contents = bfd_zalloc (htab->stub_bfd, stub_sec->size);
9414 if (stub_sec->contents == NULL)
9415 return FALSE;
9416 /* We want to check that built size is the same as calculated
9417 size. rawsize is a convenient location to use. */
9418 stub_sec->rawsize = stub_sec->size;
9419 stub_sec->size = 0;
e717da7e 9420 }
5d1634d7 9421
4ce794b7 9422 if (htab->plt != NULL)
5d1634d7 9423 {
9f951329 9424 unsigned int indx;
ad8e1ba5 9425 bfd_vma plt0;
9f951329 9426
721956f4 9427 /* Build the .glink plt call stub. */
4ce794b7
AM
9428 plt0 = (htab->plt->output_section->vma
9429 + htab->plt->output_offset
9430 - (htab->glink->output_section->vma
9431 + htab->glink->output_offset
ad8e1ba5
AM
9432 + GLINK_CALL_STUB_SIZE));
9433 if (plt0 + 0x80008000 > 0xffffffff)
721956f4 9434 {
ad8e1ba5
AM
9435 (*_bfd_error_handler) (_(".glink and .plt too far apart"));
9436 bfd_set_error (bfd_error_bad_value);
9437 return FALSE;
721956f4 9438 }
721956f4 9439
97b639ba
AM
9440 if (htab->emit_stub_syms)
9441 {
9442 struct elf_link_hash_entry *h;
9443 h = elf_link_hash_lookup (&htab->elf, "__glink", TRUE, FALSE, FALSE);
9444 if (h == NULL)
9445 return FALSE;
9446 if (h->root.type == bfd_link_hash_new)
9447 {
9448 h->root.type = bfd_link_hash_defined;
9449 h->root.u.def.section = htab->glink;
9450 h->root.u.def.value = 0;
f5385ebf
AM
9451 h->ref_regular = 1;
9452 h->def_regular = 1;
9453 h->ref_regular_nonweak = 1;
9454 h->forced_local = 1;
9455 h->non_elf = 0;
97b639ba
AM
9456 }
9457 }
4ce794b7
AM
9458 p = htab->glink->contents;
9459 bfd_put_32 (htab->glink->owner, MFCTR_R12, p);
ad8e1ba5 9460 p += 4;
4ce794b7 9461 bfd_put_32 (htab->glink->owner, SLDI_R11_R0_3, p);
ad8e1ba5 9462 p += 4;
4ce794b7 9463 bfd_put_32 (htab->glink->owner, ADDIC_R2_R0_32K, p);
ad8e1ba5 9464 p += 4;
4ce794b7 9465 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
ad8e1ba5 9466 p += 4;
4ce794b7 9467 bfd_put_32 (htab->glink->owner, SRADI_R2_R2_63, p);
ad8e1ba5 9468 p += 4;
4ce794b7 9469 bfd_put_32 (htab->glink->owner, SLDI_R11_R0_2, p);
ad8e1ba5 9470 p += 4;
4ce794b7 9471 bfd_put_32 (htab->glink->owner, AND_R2_R2_R11, p);
ad8e1ba5 9472 p += 4;
4ce794b7 9473 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
ad8e1ba5 9474 p += 4;
4ce794b7 9475 bfd_put_32 (htab->glink->owner, ADD_R12_R12_R2, p);
ad8e1ba5 9476 p += 4;
4ce794b7 9477 bfd_put_32 (htab->glink->owner, ADDIS_R12_R12 | PPC_HA (plt0), p);
ad8e1ba5 9478 p += 4;
4ce794b7 9479 bfd_put_32 (htab->glink->owner, LD_R11_0R12 | PPC_LO (plt0), p);
ad8e1ba5 9480 p += 4;
4ce794b7 9481 bfd_put_32 (htab->glink->owner, ADDI_R12_R12 | PPC_LO (plt0), p);
ad8e1ba5 9482 p += 4;
4ce794b7 9483 bfd_put_32 (htab->glink->owner, LD_R2_0R12 | 8, p);
ad8e1ba5 9484 p += 4;
4ce794b7 9485 bfd_put_32 (htab->glink->owner, MTCTR_R11, p);
ad8e1ba5 9486 p += 4;
4ce794b7 9487 bfd_put_32 (htab->glink->owner, LD_R11_0R12 | 16, p);
ad8e1ba5 9488 p += 4;
4ce794b7 9489 bfd_put_32 (htab->glink->owner, BCTR, p);
ad8e1ba5
AM
9490 p += 4;
9491
9f951329
AM
9492 /* Build the .glink lazy link call stubs. */
9493 indx = 0;
eea6121a 9494 while (p < htab->glink->contents + htab->glink->size)
9f951329
AM
9495 {
9496 if (indx < 0x8000)
9497 {
4ce794b7 9498 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
9f951329
AM
9499 p += 4;
9500 }
9501 else
9502 {
4ce794b7 9503 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
9f951329 9504 p += 4;
4ce794b7 9505 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx), p);
9f951329
AM
9506 p += 4;
9507 }
4ce794b7
AM
9508 bfd_put_32 (htab->glink->owner,
9509 B_DOT | ((htab->glink->contents - p) & 0x3fffffc), p);
a16d5acb 9510 indx++;
9f951329
AM
9511 p += 4;
9512 }
eea6121a 9513 htab->glink->rawsize = p - htab->glink->contents;
5d1634d7 9514 }
5d1634d7 9515
eea6121a 9516 if (htab->brlt->size != 0)
721956f4 9517 {
4ce794b7 9518 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
eea6121a 9519 htab->brlt->size);
4ce794b7 9520 if (htab->brlt->contents == NULL)
b34976b6 9521 return FALSE;
721956f4 9522 }
ee75fd95 9523 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
63bc6f6c
AM
9524 {
9525 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
eea6121a 9526 htab->relbrlt->size);
63bc6f6c
AM
9527 if (htab->relbrlt->contents == NULL)
9528 return FALSE;
9529 }
5d1634d7 9530
721956f4
AM
9531 /* Build the stubs as directed by the stub hash table. */
9532 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
5d1634d7 9533
721956f4
AM
9534 for (stub_sec = htab->stub_bfd->sections;
9535 stub_sec != NULL;
9536 stub_sec = stub_sec->next)
e717da7e
AM
9537 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
9538 {
9539 stub_sec_count += 1;
eea6121a 9540 if (stub_sec->rawsize != stub_sec->size)
e717da7e
AM
9541 break;
9542 }
5d1634d7 9543
721956f4 9544 if (stub_sec != NULL
eea6121a 9545 || htab->glink->rawsize != htab->glink->size)
5d1634d7 9546 {
b34976b6 9547 htab->stub_error = TRUE;
721956f4 9548 (*_bfd_error_handler) (_("stubs don't match calculated size"));
5d1634d7 9549 }
721956f4 9550
d2a300cf
AM
9551 if (htab->stub_error)
9552 return FALSE;
9553
9554 if (stats != NULL)
9555 {
9556 *stats = bfd_malloc (500);
9557 if (*stats == NULL)
9558 return FALSE;
9559
ee75fd95 9560 sprintf (*stats, _("linker stubs in %u group%s\n"
d2a300cf
AM
9561 " branch %lu\n"
9562 " toc adjust %lu\n"
9563 " long branch %lu\n"
9564 " long toc adj %lu\n"
9565 " plt call %lu"),
e717da7e 9566 stub_sec_count,
ee75fd95 9567 stub_sec_count == 1 ? "" : "s",
4ce794b7
AM
9568 htab->stub_count[ppc_stub_long_branch - 1],
9569 htab->stub_count[ppc_stub_long_branch_r2off - 1],
9570 htab->stub_count[ppc_stub_plt_branch - 1],
9571 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
9572 htab->stub_count[ppc_stub_plt_call - 1]);
d2a300cf
AM
9573 }
9574 return TRUE;
5bd4f169
AM
9575}
9576
99877b66
AM
9577/* This function undoes the changes made by add_symbol_adjust. */
9578
9579static bfd_boolean
9580undo_symbol_twiddle (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
9581{
9582 struct ppc_link_hash_entry *eh;
9583
9584 if (h->root.type == bfd_link_hash_indirect)
9585 return TRUE;
9586
9587 if (h->root.type == bfd_link_hash_warning)
9588 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9589
9590 eh = (struct ppc_link_hash_entry *) h;
9591 if (eh->elf.root.type != bfd_link_hash_undefweak || !eh->was_undefined)
9592 return TRUE;
9593
9594 eh->elf.root.type = bfd_link_hash_undefined;
9595 return TRUE;
9596}
9597
9598void
9599ppc64_elf_restore_symbols (struct bfd_link_info *info)
9600{
9601 struct ppc_link_hash_table *htab = ppc_hash_table (info);
9602 elf_link_hash_traverse (&htab->elf, undo_symbol_twiddle, info);
9603}
9604
60124e18
AM
9605/* What to do when ld finds relocations against symbols defined in
9606 discarded sections. */
9607
9608static unsigned int
9609ppc64_elf_action_discarded (asection *sec)
9610{
9611 if (strcmp (".opd", sec->name) == 0)
9612 return 0;
9613
9614 if (strcmp (".toc", sec->name) == 0)
9615 return 0;
9616
bce50a28
JJ
9617 if (strcmp (".toc1", sec->name) == 0)
9618 return 0;
9619
60124e18
AM
9620 return _bfd_elf_default_action_discarded (sec);
9621}
9622
5bd4f169
AM
9623/* The RELOCATE_SECTION function is called by the ELF backend linker
9624 to handle the relocations for a section.
9625
9626 The relocs are always passed as Rela structures; if the section
9627 actually uses Rel structures, the r_addend field will always be
9628 zero.
9629
9630 This function is responsible for adjust the section contents as
9631 necessary, and (if using Rela relocs and generating a
1049f94e 9632 relocatable output file) adjusting the reloc addend as
5bd4f169
AM
9633 necessary.
9634
9635 This function does not have to worry about setting the reloc
9636 address or the reloc symbol index.
9637
9638 LOCAL_SYMS is a pointer to the swapped in local symbols.
9639
9640 LOCAL_SECTIONS is an array giving the section in the input file
9641 corresponding to the st_shndx field of each local symbol.
9642
9643 The global hash table entry for the global symbols can be found
9644 via elf_sym_hashes (input_bfd).
9645
1049f94e 9646 When generating relocatable output, this function must handle
5bd4f169
AM
9647 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
9648 going to be the section symbol corresponding to the output
9649 section, which means that the addend must be adjusted
9650 accordingly. */
9651
b34976b6 9652static bfd_boolean
4ce794b7
AM
9653ppc64_elf_relocate_section (bfd *output_bfd,
9654 struct bfd_link_info *info,
9655 bfd *input_bfd,
9656 asection *input_section,
9657 bfd_byte *contents,
9658 Elf_Internal_Rela *relocs,
9659 Elf_Internal_Sym *local_syms,
9660 asection **local_sections)
5bd4f169 9661{
65f38f15 9662 struct ppc_link_hash_table *htab;
5bd4f169
AM
9663 Elf_Internal_Shdr *symtab_hdr;
9664 struct elf_link_hash_entry **sym_hashes;
5bd4f169
AM
9665 Elf_Internal_Rela *rel;
9666 Elf_Internal_Rela *relend;
411e1bfb
AM
9667 Elf_Internal_Rela outrel;
9668 bfd_byte *loc;
411e1bfb 9669 struct got_entry **local_got_ents;
5bd4f169 9670 bfd_vma TOCstart;
b34976b6
AM
9671 bfd_boolean ret = TRUE;
9672 bfd_boolean is_opd;
96e0dda4 9673 /* Disabled until we sort out how ld should choose 'y' vs 'at'. */
b34976b6 9674 bfd_boolean is_power4 = FALSE;
5bd4f169 9675
65f38f15 9676 /* Initialize howto table if needed. */
5bd4f169 9677 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
9678 ppc_howto_init ();
9679
65f38f15 9680 htab = ppc_hash_table (info);
ee75fd95
AM
9681
9682 /* Don't relocate stub sections. */
9683 if (input_section->owner == htab->stub_bfd)
9684 return TRUE;
9685
411e1bfb 9686 local_got_ents = elf_local_got_ents (input_bfd);
5bd4f169 9687 TOCstart = elf_gp (output_bfd);
5bd4f169
AM
9688 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9689 sym_hashes = elf_sym_hashes (input_bfd);
f0abc2a1 9690 is_opd = ppc64_elf_section_data (input_section)->opd.adjust != NULL;
65f38f15 9691
5bd4f169
AM
9692 rel = relocs;
9693 relend = relocs + input_section->reloc_count;
9694 for (; rel < relend; rel++)
9695 {
04c9666a 9696 enum elf_ppc64_reloc_type r_type;
4cc603a5 9697 bfd_vma addend, orig_addend;
5bd4f169
AM
9698 bfd_reloc_status_type r;
9699 Elf_Internal_Sym *sym;
9700 asection *sec;
039b3fef
AM
9701 struct elf_link_hash_entry *h_elf;
9702 struct ppc_link_hash_entry *h;
9703 struct ppc_link_hash_entry *fdh;
5bd4f169 9704 const char *sym_name;
0d4792f7 9705 unsigned long r_symndx, toc_symndx;
951fd09b 9706 char tls_mask, tls_gd, tls_type;
0d4792f7 9707 char sym_type;
5bd4f169 9708 bfd_vma relocation;
b34976b6
AM
9709 bfd_boolean unresolved_reloc;
9710 bfd_boolean warned;
50bc7936 9711 unsigned long insn, mask;
721956f4
AM
9712 struct ppc_stub_hash_entry *stub_entry;
9713 bfd_vma max_br_offset;
9714 bfd_vma from;
5bd4f169 9715
4ce794b7 9716 r_type = ELF64_R_TYPE (rel->r_info);
5bd4f169 9717 r_symndx = ELF64_R_SYM (rel->r_info);
ee87f2da
AM
9718
9719 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
9720 symbol of the previous ADDR64 reloc. The symbol gives us the
9721 proper TOC base to use. */
9722 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
9723 && rel != relocs
9724 && ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_ADDR64
9725 && is_opd)
9726 r_symndx = ELF64_R_SYM (rel[-1].r_info);
9727
4ce794b7
AM
9728 sym = NULL;
9729 sec = NULL;
039b3fef 9730 h_elf = NULL;
4ce794b7 9731 sym_name = NULL;
b34976b6
AM
9732 unresolved_reloc = FALSE;
9733 warned = FALSE;
4cc603a5 9734 orig_addend = rel->r_addend;
65f38f15 9735
0b13192e 9736 if (r_symndx < symtab_hdr->sh_info)
5bd4f169
AM
9737 {
9738 /* It's a local symbol. */
4025353c
AM
9739 long *opd_adjust;
9740
5bd4f169
AM
9741 sym = local_syms + r_symndx;
9742 sec = local_sections[r_symndx];
26c61ae5 9743 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
0d4792f7 9744 sym_type = ELF64_ST_TYPE (sym->st_info);
8517fae7 9745 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
4025353c
AM
9746 opd_adjust = get_opd_info (sec);
9747 if (opd_adjust != NULL)
1e2f5b6e 9748 {
3f764659 9749 long adjust = opd_adjust[(sym->st_value + rel->r_addend) / 8];
4025353c
AM
9750 if (adjust == -1)
9751 relocation = 0;
9752 else
4cc603a5
AM
9753 {
9754 /* If this is a relocation against the opd section sym
9755 and we have edited .opd, adjust the reloc addend so
9756 that ld -r and ld --emit-relocs output is correct.
9757 If it is a reloc against some other .opd symbol,
9758 then the symbol value will be adjusted later. */
9759 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
9760 rel->r_addend += adjust;
9761 else
9762 relocation += adjust;
9763 }
1e2f5b6e 9764 }
4cc603a5
AM
9765 if (info->relocatable)
9766 continue;
5bd4f169
AM
9767 }
9768 else
9769 {
4cc603a5
AM
9770 if (info->relocatable)
9771 continue;
b2a8e766
AM
9772 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
9773 r_symndx, symtab_hdr, sym_hashes,
039b3fef 9774 h_elf, sec, relocation,
b2a8e766 9775 unresolved_reloc, warned);
039b3fef
AM
9776 sym_name = h_elf->root.root.string;
9777 sym_type = h_elf->type;
5bd4f169 9778 }
039b3fef 9779 h = (struct ppc_link_hash_entry *) h_elf;
5bd4f169 9780
951fd09b
AM
9781 /* TLS optimizations. Replace instruction sequences and relocs
9782 based on information we collected in tls_optimize. We edit
9783 RELOCS so that --emit-relocs will output something sensible
9784 for the final instruction stream. */
9785 tls_mask = 0;
9786 tls_gd = 0;
0d4792f7 9787 toc_symndx = 0;
d881513a 9788 if (IS_PPC64_TLS_RELOC (r_type))
411e1bfb
AM
9789 {
9790 if (h != NULL)
039b3fef 9791 tls_mask = h->tls_mask;
411e1bfb
AM
9792 else if (local_got_ents != NULL)
9793 {
e7b938ca
AM
9794 char *lgot_masks;
9795 lgot_masks = (char *) (local_got_ents + symtab_hdr->sh_info);
9796 tls_mask = lgot_masks[r_symndx];
411e1bfb 9797 }
0d4792f7
AM
9798 if (tls_mask == 0 && r_type == R_PPC64_TLS)
9799 {
9800 /* Check for toc tls entries. */
9801 char *toc_tls;
9802
9803 if (!get_tls_mask (&toc_tls, &toc_symndx, &local_syms,
9804 rel, input_bfd))
9805 return FALSE;
9806
9807 if (toc_tls)
9808 tls_mask = *toc_tls;
9809 }
9810 }
9811
9812 /* Check that tls relocs are used with tls syms, and non-tls
9813 relocs are used with non-tls syms. */
9814 if (r_symndx != 0
9815 && r_type != R_PPC64_NONE
9816 && (h == NULL
039b3fef
AM
9817 || h->elf.root.type == bfd_link_hash_defined
9818 || h->elf.root.type == bfd_link_hash_defweak)
0d4792f7
AM
9819 && IS_PPC64_TLS_RELOC (r_type) != (sym_type == STT_TLS))
9820 {
9821 if (r_type == R_PPC64_TLS && tls_mask != 0)
9822 /* R_PPC64_TLS is OK against a symbol in the TOC. */
9823 ;
9824 else
9825 (*_bfd_error_handler)
9826 (sym_type == STT_TLS
d003868e
AM
9827 ? _("%B(%A+0x%lx): %s used with TLS symbol %s")
9828 : _("%B(%A+0x%lx): %s used with non-TLS symbol %s"),
9829 input_bfd,
9830 input_section,
0d4792f7
AM
9831 (long) rel->r_offset,
9832 ppc64_elf_howto_table[r_type]->name,
9833 sym_name);
411e1bfb
AM
9834 }
9835
9836 /* Ensure reloc mapping code below stays sane. */
9837 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
9838 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
9839 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
9840 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
9841 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
9842 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
9843 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
9844 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
9845 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
9846 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
9847 abort ();
0d4792f7 9848
411e1bfb
AM
9849 switch (r_type)
9850 {
9851 default:
411e1bfb
AM
9852 break;
9853
9854 case R_PPC64_TOC16:
9855 case R_PPC64_TOC16_LO:
9856 case R_PPC64_TOC16_DS:
9857 case R_PPC64_TOC16_LO_DS:
411e1bfb
AM
9858 {
9859 /* Check for toc tls entries. */
9860 char *toc_tls;
951fd09b 9861 int retval;
411e1bfb 9862
0d4792f7
AM
9863 retval = get_tls_mask (&toc_tls, &toc_symndx, &local_syms,
9864 rel, input_bfd);
951fd09b 9865 if (retval == 0)
411e1bfb
AM
9866 return FALSE;
9867
9868 if (toc_tls)
9869 {
951fd09b 9870 tls_mask = *toc_tls;
411e1bfb
AM
9871 if (r_type == R_PPC64_TOC16_DS
9872 || r_type == R_PPC64_TOC16_LO_DS)
81407a69
AM
9873 {
9874 if (tls_mask != 0
9875 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
9876 goto toctprel;
9877 }
411e1bfb 9878 else
951fd09b
AM
9879 {
9880 /* If we found a GD reloc pair, then we might be
9881 doing a GD->IE transition. */
9882 if (retval == 2)
9883 {
9884 tls_gd = TLS_TPRELGD;
9885 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
9886 goto tls_get_addr_check;
9887 }
9888 else if (retval == 3)
9889 {
9890 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
9891 goto tls_get_addr_check;
9892 }
9893 }
411e1bfb
AM
9894 }
9895 }
9896 break;
9897
9898 case R_PPC64_GOT_TPREL16_DS:
9899 case R_PPC64_GOT_TPREL16_LO_DS:
951fd09b
AM
9900 if (tls_mask != 0
9901 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 9902 {
81407a69 9903 toctprel:
411e1bfb
AM
9904 insn = bfd_get_32 (output_bfd, contents + rel->r_offset - 2);
9905 insn &= 31 << 21;
9906 insn |= 0x3c0d0000; /* addis 0,13,0 */
9907 bfd_put_32 (output_bfd, insn, contents + rel->r_offset - 2);
9908 r_type = R_PPC64_TPREL16_HA;
0d4792f7
AM
9909 if (toc_symndx != 0)
9910 {
9911 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
9912 /* We changed the symbol. Start over in order to
9913 get h, sym, sec etc. right. */
9914 rel--;
9915 continue;
9916 }
9917 else
9918 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
9919 }
9920 break;
9921
9922 case R_PPC64_TLS:
951fd09b
AM
9923 if (tls_mask != 0
9924 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 9925 {
50bc7936 9926 bfd_vma rtra;
411e1bfb 9927 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
772119ce 9928 if ((insn & ((0x3f << 26) | (31 << 11)))
411e1bfb
AM
9929 == ((31 << 26) | (13 << 11)))
9930 rtra = insn & ((1 << 26) - (1 << 16));
772119ce 9931 else if ((insn & ((0x3f << 26) | (31 << 16)))
411e1bfb
AM
9932 == ((31 << 26) | (13 << 16)))
9933 rtra = (insn & (31 << 21)) | ((insn & (31 << 11)) << 5);
9934 else
9935 abort ();
9936 if ((insn & ((1 << 11) - (1 << 1))) == 266 << 1)
9937 /* add -> addi. */
9938 insn = 14 << 26;
9939 else if ((insn & (31 << 1)) == 23 << 1
9940 && ((insn & (31 << 6)) < 14 << 6
9941 || ((insn & (31 << 6)) >= 16 << 6
9942 && (insn & (31 << 6)) < 24 << 6)))
9943 /* load and store indexed -> dform. */
9944 insn = (32 | ((insn >> 6) & 31)) << 26;
9945 else if ((insn & (31 << 1)) == 21 << 1
9946 && (insn & (0x1a << 6)) == 0)
9947 /* ldx, ldux, stdx, stdux -> ld, ldu, std, stdu. */
9948 insn = (((58 | ((insn >> 6) & 4)) << 26)
9949 | ((insn >> 6) & 1));
9950 else if ((insn & (31 << 1)) == 21 << 1
9951 && (insn & ((1 << 11) - (1 << 1))) == 341 << 1)
9952 /* lwax -> lwa. */
9953 insn = (58 << 26) | 2;
9954 else
9955 abort ();
9956 insn |= rtra;
9957 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
411e1bfb
AM
9958 /* Was PPC64_TLS which sits on insn boundary, now
9959 PPC64_TPREL16_LO which is at insn+2. */
9960 rel->r_offset += 2;
0d4792f7
AM
9961 r_type = R_PPC64_TPREL16_LO;
9962 if (toc_symndx != 0)
9963 {
9964 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
9965 /* We changed the symbol. Start over in order to
9966 get h, sym, sec etc. right. */
9967 rel--;
9968 continue;
9969 }
9970 else
9971 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
9972 }
9973 break;
9974
411e1bfb
AM
9975 case R_PPC64_GOT_TLSGD16_HI:
9976 case R_PPC64_GOT_TLSGD16_HA:
951fd09b
AM
9977 tls_gd = TLS_TPRELGD;
9978 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
9979 goto tls_gdld_hi;
9980 break;
9981
411e1bfb
AM
9982 case R_PPC64_GOT_TLSLD16_HI:
9983 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 9984 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
411e1bfb 9985 {
951fd09b
AM
9986 tls_gdld_hi:
9987 if ((tls_mask & tls_gd) != 0)
9988 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
9989 + R_PPC64_GOT_TPREL16_DS);
9990 else
411e1bfb 9991 {
951fd09b
AM
9992 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
9993 rel->r_offset -= 2;
9994 r_type = R_PPC64_NONE;
411e1bfb 9995 }
951fd09b 9996 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
9997 }
9998 break;
9999
951fd09b
AM
10000 case R_PPC64_GOT_TLSGD16:
10001 case R_PPC64_GOT_TLSGD16_LO:
10002 tls_gd = TLS_TPRELGD;
10003 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
10004 goto tls_get_addr_check;
10005 break;
411e1bfb 10006
951fd09b
AM
10007 case R_PPC64_GOT_TLSLD16:
10008 case R_PPC64_GOT_TLSLD16_LO:
10009 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
10010 {
10011 tls_get_addr_check:
10012 if (rel + 1 < relend)
411e1bfb 10013 {
951fd09b
AM
10014 enum elf_ppc64_reloc_type r_type2;
10015 unsigned long r_symndx2;
10016 struct elf_link_hash_entry *h2;
10017 bfd_vma insn1, insn2, insn3;
10018 bfd_vma offset;
10019
10020 /* The next instruction should be a call to
10021 __tls_get_addr. Peek at the reloc to be sure. */
4ce794b7 10022 r_type2 = ELF64_R_TYPE (rel[1].r_info);
951fd09b
AM
10023 r_symndx2 = ELF64_R_SYM (rel[1].r_info);
10024 if (r_symndx2 < symtab_hdr->sh_info
10025 || (r_type2 != R_PPC64_REL14
10026 && r_type2 != R_PPC64_REL14_BRTAKEN
10027 && r_type2 != R_PPC64_REL14_BRNTAKEN
10028 && r_type2 != R_PPC64_REL24))
10029 break;
10030
10031 h2 = sym_hashes[r_symndx2 - symtab_hdr->sh_info];
10032 while (h2->root.type == bfd_link_hash_indirect
10033 || h2->root.type == bfd_link_hash_warning)
10034 h2 = (struct elf_link_hash_entry *) h2->root.u.i.link;
8387904d
AM
10035 if (h2 == NULL || (h2 != &htab->tls_get_addr->elf
10036 && h2 != &htab->tls_get_addr_fd->elf))
951fd09b
AM
10037 break;
10038
10039 /* OK, it checks out. Replace the call. */
10040 offset = rel[1].r_offset;
10041 insn1 = bfd_get_32 (output_bfd,
10042 contents + rel->r_offset - 2);
10043 insn3 = bfd_get_32 (output_bfd,
10044 contents + offset + 4);
10045 if ((tls_mask & tls_gd) != 0)
411e1bfb 10046 {
951fd09b
AM
10047 /* IE */
10048 insn1 &= (1 << 26) - (1 << 2);
10049 insn1 |= 58 << 26; /* ld */
10050 insn2 = 0x7c636a14; /* add 3,3,13 */
10051 rel[1].r_info = ELF64_R_INFO (r_symndx2, R_PPC64_NONE);
10052 if ((tls_mask & TLS_EXPLICIT) == 0)
10053 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
10054 + R_PPC64_GOT_TPREL16_DS);
10055 else
10056 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
10057 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
10058 }
10059 else
951fd09b
AM
10060 {
10061 /* LE */
10062 insn1 = 0x3c6d0000; /* addis 3,13,0 */
10063 insn2 = 0x38630000; /* addi 3,3,0 */
10064 if (tls_gd == 0)
10065 {
10066 /* Was an LD reloc. */
10067 r_symndx = 0;
e1918d23
AM
10068 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
10069 rel[1].r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
951fd09b 10070 }
0d4792f7
AM
10071 else if (toc_symndx != 0)
10072 r_symndx = toc_symndx;
951fd09b
AM
10073 r_type = R_PPC64_TPREL16_HA;
10074 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
10075 rel[1].r_info = ELF64_R_INFO (r_symndx,
10076 R_PPC64_TPREL16_LO);
10077 rel[1].r_offset += 2;
10078 }
10079 if (insn3 == NOP
10080 || insn3 == CROR_151515 || insn3 == CROR_313131)
10081 {
10082 insn3 = insn2;
10083 insn2 = NOP;
10084 rel[1].r_offset += 4;
10085 }
10086 bfd_put_32 (output_bfd, insn1, contents + rel->r_offset - 2);
10087 bfd_put_32 (output_bfd, insn2, contents + offset);
10088 bfd_put_32 (output_bfd, insn3, contents + offset + 4);
0d4792f7 10089 if (tls_gd == 0 || toc_symndx != 0)
951fd09b 10090 {
0d4792f7
AM
10091 /* We changed the symbol. Start over in order
10092 to get h, sym, sec etc. right. */
951fd09b
AM
10093 rel--;
10094 continue;
10095 }
411e1bfb 10096 }
411e1bfb 10097 }
411e1bfb
AM
10098 break;
10099
10100 case R_PPC64_DTPMOD64:
951fd09b
AM
10101 if (rel + 1 < relend
10102 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
10103 && rel[1].r_offset == rel->r_offset + 8)
411e1bfb 10104 {
951fd09b
AM
10105 if ((tls_mask & TLS_GD) == 0)
10106 {
10107 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
10108 if ((tls_mask & TLS_TPRELGD) != 0)
10109 r_type = R_PPC64_TPREL64;
10110 else
10111 {
4ce794b7 10112 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
951fd09b
AM
10113 r_type = R_PPC64_NONE;
10114 }
10115 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
10116 }
10117 }
10118 else
10119 {
10120 if ((tls_mask & TLS_LD) == 0)
411e1bfb 10121 {
4ce794b7 10122 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
411e1bfb 10123 r_type = R_PPC64_NONE;
951fd09b 10124 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb 10125 }
411e1bfb
AM
10126 }
10127 break;
10128
10129 case R_PPC64_TPREL64:
951fd09b 10130 if ((tls_mask & TLS_TPREL) == 0)
411e1bfb
AM
10131 {
10132 r_type = R_PPC64_NONE;
10133 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
10134 }
10135 break;
10136 }
10137
10138 /* Handle other relocations that tweak non-addend part of insn. */
86c76c7b 10139 insn = 0;
b25116a9
AM
10140 max_br_offset = 1 << 25;
10141 addend = rel->r_addend;
65f38f15 10142 switch (r_type)
5bd4f169
AM
10143 {
10144 default:
65f38f15 10145 break;
5bd4f169 10146
65f38f15
AM
10147 /* Branch taken prediction relocations. */
10148 case R_PPC64_ADDR14_BRTAKEN:
10149 case R_PPC64_REL14_BRTAKEN:
cedb70c5
KH
10150 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
10151 /* Fall thru. */
65f38f15 10152
86c76c7b 10153 /* Branch not taken prediction relocations. */
65f38f15
AM
10154 case R_PPC64_ADDR14_BRNTAKEN:
10155 case R_PPC64_REL14_BRNTAKEN:
411e1bfb
AM
10156 insn |= bfd_get_32 (output_bfd,
10157 contents + rel->r_offset) & ~(0x01 << 21);
b25116a9 10158 /* Fall thru. */
86c76c7b 10159
b25116a9
AM
10160 case R_PPC64_REL14:
10161 max_br_offset = 1 << 15;
10162 /* Fall thru. */
5bd4f169 10163
65f38f15 10164 case R_PPC64_REL24:
ad8e1ba5
AM
10165 /* Calls to functions with a different TOC, such as calls to
10166 shared objects, need to alter the TOC pointer. This is
10167 done using a linkage stub. A REL24 branching to these
10168 linkage stubs needs to be followed by a nop, as the nop
10169 will be replaced with an instruction to restore the TOC
10170 base pointer. */
b25116a9 10171 stub_entry = NULL;
8387904d 10172 fdh = h;
ad8e1ba5 10173 if (((h != NULL
039b3fef
AM
10174 && (((fdh = h->oh) != NULL
10175 && fdh->elf.plt.plist != NULL)
10176 || (fdh = h)->elf.plt.plist != NULL))
8387904d 10177 || (sec != NULL
ad8e1ba5 10178 && sec->output_section != NULL
b25116a9 10179 && sec->id <= htab->top_id
ad8e1ba5
AM
10180 && (htab->stub_group[sec->id].toc_off
10181 != htab->stub_group[input_section->id].toc_off)))
721956f4 10182 && (stub_entry = ppc_get_stub_entry (input_section, sec, fdh,
ad8e1ba5
AM
10183 rel, htab)) != NULL
10184 && (stub_entry->stub_type == ppc_stub_plt_call
10185 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
10186 || stub_entry->stub_type == ppc_stub_long_branch_r2off))
41bd81ab 10187 {
b25116a9 10188 bfd_boolean can_plt_call = FALSE;
721956f4 10189
eea6121a 10190 if (rel->r_offset + 8 <= input_section->size)
41bd81ab 10191 {
b25116a9
AM
10192 unsigned long nop;
10193 nop = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
10194 if (nop == NOP
10195 || nop == CROR_151515 || nop == CROR_313131)
41bd81ab 10196 {
4ce794b7 10197 bfd_put_32 (input_bfd, LD_R2_40R1,
411e1bfb 10198 contents + rel->r_offset + 4);
b25116a9 10199 can_plt_call = TRUE;
41bd81ab 10200 }
5bd4f169 10201 }
721956f4
AM
10202
10203 if (!can_plt_call)
10204 {
ad8e1ba5
AM
10205 if (stub_entry->stub_type == ppc_stub_plt_call)
10206 {
10207 /* If this is a plain branch rather than a branch
4fa1c237
AM
10208 and link, don't require a nop. However, don't
10209 allow tail calls in a shared library as they
10210 will result in r2 being corrupted. */
b25116a9
AM
10211 unsigned long br;
10212 br = bfd_get_32 (input_bfd, contents + rel->r_offset);
4fa1c237 10213 if (info->executable && (br & 1) == 0)
b25116a9 10214 can_plt_call = TRUE;
15b552e9
AM
10215 else
10216 stub_entry = NULL;
ad8e1ba5 10217 }
6ab189d5 10218 else if (h != NULL
039b3fef 10219 && strcmp (h->elf.root.root.string,
6ab189d5
AM
10220 ".__libc_start_main") == 0)
10221 {
10222 /* Allow crt1 branch to go via a toc adjusting stub. */
b25116a9 10223 can_plt_call = TRUE;
6ab189d5 10224 }
ad8e1ba5
AM
10225 else
10226 {
10227 if (strcmp (input_section->output_section->name,
10228 ".init") == 0
10229 || strcmp (input_section->output_section->name,
10230 ".fini") == 0)
10231 (*_bfd_error_handler)
d003868e 10232 (_("%B(%A+0x%lx): automatic multiple TOCs "
ad8e1ba5
AM
10233 "not supported using your crt files; "
10234 "recompile with -mminimal-toc or upgrade gcc"),
d003868e
AM
10235 input_bfd,
10236 input_section,
ad8e1ba5
AM
10237 (long) rel->r_offset);
10238 else
10239 (*_bfd_error_handler)
d003868e 10240 (_("%B(%A+0x%lx): sibling call optimization to `%s' "
ad8e1ba5
AM
10241 "does not allow automatic multiple TOCs; "
10242 "recompile with -mminimal-toc or "
10243 "-fno-optimize-sibling-calls, "
10244 "or make `%s' extern"),
d003868e
AM
10245 input_bfd,
10246 input_section,
ad8e1ba5
AM
10247 (long) rel->r_offset,
10248 sym_name,
10249 sym_name);
10250 bfd_set_error (bfd_error_bad_value);
10251 ret = FALSE;
10252 }
721956f4
AM
10253 }
10254
b25116a9
AM
10255 if (can_plt_call
10256 && stub_entry->stub_type == ppc_stub_plt_call)
10257 unresolved_reloc = FALSE;
10258 }
10259
8387904d
AM
10260 if (stub_entry == NULL
10261 && get_opd_info (sec) != NULL)
10262 {
10263 /* The branch destination is the value of the opd entry. */
4cc603a5
AM
10264 bfd_vma off = (relocation + addend
10265 - sec->output_section->vma
10266 - sec->output_offset);
8387904d
AM
10267 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL);
10268 if (dest != (bfd_vma) -1)
10269 {
10270 relocation = dest;
10271 addend = 0;
10272 }
10273 }
10274
b25116a9
AM
10275 /* If the branch is out of reach we ought to have a long
10276 branch stub. */
10277 from = (rel->r_offset
10278 + input_section->output_offset
10279 + input_section->output_section->vma);
10280
10281 if (stub_entry == NULL
4cc603a5 10282 && (relocation + addend - from + max_br_offset
b25116a9
AM
10283 >= 2 * max_br_offset)
10284 && r_type != R_PPC64_ADDR14_BRTAKEN
10285 && r_type != R_PPC64_ADDR14_BRNTAKEN)
039b3fef
AM
10286 stub_entry = ppc_get_stub_entry (input_section, sec, h, rel,
10287 htab);
b25116a9
AM
10288
10289 if (stub_entry != NULL)
10290 {
10291 /* Munge up the value and addend so that we call the stub
10292 rather than the procedure directly. */
10293 relocation = (stub_entry->stub_offset
10294 + stub_entry->stub_sec->output_offset
10295 + stub_entry->stub_sec->output_section->vma);
10296 addend = 0;
10297 }
10298
10299 if (insn != 0)
10300 {
10301 if (is_power4)
721956f4 10302 {
b25116a9
AM
10303 /* Set 'a' bit. This is 0b00010 in BO field for branch
10304 on CR(BI) insns (BO == 001at or 011at), and 0b01000
10305 for branch on CTR insns (BO == 1a00t or 1a01t). */
10306 if ((insn & (0x14 << 21)) == (0x04 << 21))
10307 insn |= 0x02 << 21;
10308 else if ((insn & (0x14 << 21)) == (0x10 << 21))
10309 insn |= 0x08 << 21;
10310 else
10311 break;
10312 }
10313 else
10314 {
10315 /* Invert 'y' bit if not the default. */
4cc603a5 10316 if ((bfd_signed_vma) (relocation + addend - from) < 0)
b25116a9 10317 insn ^= 0x01 << 21;
721956f4 10318 }
b25116a9
AM
10319
10320 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
5bd4f169 10321 }
e86ce104 10322
06da1e8e
AM
10323 /* NOP out calls to undefined weak functions.
10324 We can thus call a weak function without first
10325 checking whether the function is defined. */
b25116a9 10326 else if (h != NULL
039b3fef 10327 && h->elf.root.type == bfd_link_hash_undefweak
b25116a9
AM
10328 && r_type == R_PPC64_REL24
10329 && relocation == 0
4cc603a5 10330 && addend == 0)
e86ce104 10331 {
06da1e8e
AM
10332 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
10333 continue;
e86ce104 10334 }
65f38f15
AM
10335 break;
10336 }
5bd4f169 10337
65f38f15 10338 /* Set `addend'. */
411e1bfb 10339 tls_type = 0;
65f38f15
AM
10340 switch (r_type)
10341 {
10342 default:
10343 (*_bfd_error_handler)
d003868e
AM
10344 (_("%B: unknown relocation type %d for symbol %s"),
10345 input_bfd, (int) r_type, sym_name);
5bd4f169 10346
65f38f15 10347 bfd_set_error (bfd_error_bad_value);
b34976b6 10348 ret = FALSE;
65f38f15 10349 continue;
5bd4f169 10350
65f38f15 10351 case R_PPC64_NONE:
411e1bfb 10352 case R_PPC64_TLS:
04c9666a
AM
10353 case R_PPC64_GNU_VTINHERIT:
10354 case R_PPC64_GNU_VTENTRY:
65f38f15 10355 continue;
5bd4f169
AM
10356
10357 /* GOT16 relocations. Like an ADDR16 using the symbol's
10358 address in the GOT as relocation value instead of the
411e1bfb 10359 symbol's value itself. Also, create a GOT entry for the
5bd4f169 10360 symbol and put the symbol value there. */
411e1bfb
AM
10361 case R_PPC64_GOT_TLSGD16:
10362 case R_PPC64_GOT_TLSGD16_LO:
10363 case R_PPC64_GOT_TLSGD16_HI:
10364 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 10365 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
10366 goto dogot;
10367
10368 case R_PPC64_GOT_TLSLD16:
10369 case R_PPC64_GOT_TLSLD16_LO:
10370 case R_PPC64_GOT_TLSLD16_HI:
10371 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 10372 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
10373 goto dogot;
10374
10375 case R_PPC64_GOT_TPREL16_DS:
10376 case R_PPC64_GOT_TPREL16_LO_DS:
10377 case R_PPC64_GOT_TPREL16_HI:
10378 case R_PPC64_GOT_TPREL16_HA:
10379 tls_type = TLS_TLS | TLS_TPREL;
10380 goto dogot;
10381
10382 case R_PPC64_GOT_DTPREL16_DS:
10383 case R_PPC64_GOT_DTPREL16_LO_DS:
10384 case R_PPC64_GOT_DTPREL16_HI:
10385 case R_PPC64_GOT_DTPREL16_HA:
10386 tls_type = TLS_TLS | TLS_DTPREL;
10387 goto dogot;
10388
65f38f15
AM
10389 case R_PPC64_GOT16:
10390 case R_PPC64_GOT16_LO:
10391 case R_PPC64_GOT16_HI:
10392 case R_PPC64_GOT16_HA:
10393 case R_PPC64_GOT16_DS:
10394 case R_PPC64_GOT16_LO_DS:
411e1bfb 10395 dogot:
5bd4f169
AM
10396 {
10397 /* Relocation is to the entry for this symbol in the global
10398 offset table. */
e717da7e 10399 asection *got;
d881513a 10400 bfd_vma *offp;
5bd4f169 10401 bfd_vma off;
d881513a 10402 unsigned long indx = 0;
65f38f15 10403
d881513a
AM
10404 if (tls_type == (TLS_TLS | TLS_LD)
10405 && (h == NULL
f5385ebf 10406 || !h->elf.def_dynamic))
e717da7e 10407 offp = &ppc64_tlsld_got (input_bfd)->offset;
411e1bfb 10408 else
5bd4f169 10409 {
d881513a 10410 struct got_entry *ent;
5bd4f169 10411
d881513a
AM
10412 if (h != NULL)
10413 {
10414 bfd_boolean dyn = htab->elf.dynamic_sections_created;
039b3fef
AM
10415 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared,
10416 &h->elf)
d881513a 10417 || (info->shared
039b3fef 10418 && SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
d881513a
AM
10419 /* This is actually a static link, or it is a
10420 -Bsymbolic link and the symbol is defined
10421 locally, or the symbol was forced to be local
10422 because of a version file. */
10423 ;
10424 else
10425 {
039b3fef 10426 indx = h->elf.dynindx;
d881513a
AM
10427 unresolved_reloc = FALSE;
10428 }
039b3fef 10429 ent = h->elf.got.glist;
d881513a 10430 }
411e1bfb 10431 else
5bd4f169 10432 {
d881513a
AM
10433 if (local_got_ents == NULL)
10434 abort ();
10435 ent = local_got_ents[r_symndx];
5bd4f169 10436 }
d881513a
AM
10437
10438 for (; ent != NULL; ent = ent->next)
4cc603a5 10439 if (ent->addend == orig_addend
e717da7e 10440 && ent->owner == input_bfd
d881513a
AM
10441 && ent->tls_type == tls_type)
10442 break;
10443 if (ent == NULL)
10444 abort ();
10445 offp = &ent->got.offset;
5bd4f169 10446 }
411e1bfb 10447
e717da7e
AM
10448 got = ppc64_elf_tdata (input_bfd)->got;
10449 if (got == NULL)
10450 abort ();
10451
411e1bfb
AM
10452 /* The offset must always be a multiple of 8. We use the
10453 least significant bit to record whether we have already
10454 processed this entry. */
d881513a 10455 off = *offp;
411e1bfb
AM
10456 if ((off & 1) != 0)
10457 off &= ~1;
5bd4f169
AM
10458 else
10459 {
411e1bfb
AM
10460 /* Generate relocs for the dynamic linker, except in
10461 the case of TLSLD where we'll use one entry per
10462 module. */
e717da7e
AM
10463 asection *relgot = ppc64_elf_tdata (input_bfd)->relgot;
10464
d881513a 10465 *offp = off | 1;
4e795f50
AM
10466 if ((info->shared || indx != 0)
10467 && (h == NULL
039b3fef
AM
10468 || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
10469 || h->elf.root.type != bfd_link_hash_undefweak))
5bd4f169 10470 {
e717da7e
AM
10471 outrel.r_offset = (got->output_section->vma
10472 + got->output_offset
411e1bfb 10473 + off);
4cc603a5 10474 outrel.r_addend = addend;
d881513a 10475 if (tls_type & (TLS_LD | TLS_GD))
5bd4f169 10476 {
411e1bfb 10477 outrel.r_addend = 0;
e515b051 10478 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
d881513a
AM
10479 if (tls_type == (TLS_TLS | TLS_GD))
10480 {
e717da7e
AM
10481 loc = relgot->contents;
10482 loc += (relgot->reloc_count++
d881513a
AM
10483 * sizeof (Elf64_External_Rela));
10484 bfd_elf64_swap_reloca_out (output_bfd,
10485 &outrel, loc);
e515b051 10486 outrel.r_offset += 8;
4cc603a5 10487 outrel.r_addend = addend;
d881513a
AM
10488 outrel.r_info
10489 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
d881513a 10490 }
411e1bfb 10491 }
951fd09b 10492 else if (tls_type == (TLS_TLS | TLS_DTPREL))
411e1bfb 10493 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
951fd09b 10494 else if (tls_type == (TLS_TLS | TLS_TPREL))
411e1bfb
AM
10495 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
10496 else if (indx == 0)
81407a69
AM
10497 {
10498 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_RELATIVE);
10499
10500 /* Write the .got section contents for the sake
10501 of prelink. */
e717da7e 10502 loc = got->contents + off;
23fbd6fa
JJ
10503 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
10504 loc);
81407a69 10505 }
411e1bfb
AM
10506 else
10507 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
81407a69
AM
10508
10509 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
e515b051
AM
10510 {
10511 outrel.r_addend += relocation;
10512 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
e1918d23 10513 outrel.r_addend -= htab->elf.tls_sec->vma;
e515b051 10514 }
e717da7e
AM
10515 loc = relgot->contents;
10516 loc += (relgot->reloc_count++
411e1bfb
AM
10517 * sizeof (Elf64_External_Rela));
10518 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
10519 }
10520
ad8e1ba5 10521 /* Init the .got section contents here if we're not
81407a69 10522 emitting a reloc. */
d881513a 10523 else
411e1bfb 10524 {
4cc603a5 10525 relocation += addend;
7b609f53
AM
10526 if (tls_type == (TLS_TLS | TLS_LD))
10527 relocation = 1;
10528 else if (tls_type != 0)
411e1bfb 10529 {
e1918d23 10530 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
7b609f53 10531 if (tls_type == (TLS_TLS | TLS_TPREL))
411e1bfb 10532 relocation += DTP_OFFSET - TP_OFFSET;
5bd4f169 10533
7b609f53
AM
10534 if (tls_type == (TLS_TLS | TLS_GD))
10535 {
10536 bfd_put_64 (output_bfd, relocation,
e717da7e 10537 got->contents + off + 8);
7b609f53
AM
10538 relocation = 1;
10539 }
411e1bfb 10540 }
7b609f53 10541
411e1bfb 10542 bfd_put_64 (output_bfd, relocation,
e717da7e 10543 got->contents + off);
5bd4f169
AM
10544 }
10545 }
10546
65f38f15
AM
10547 if (off >= (bfd_vma) -2)
10548 abort ();
10549
e717da7e 10550 relocation = got->output_offset + off;
65f38f15 10551
5bd4f169 10552 /* TOC base (r2) is TOC start plus 0x8000. */
e717da7e 10553 addend = -TOC_BASE_OFF;
5bd4f169 10554 }
65f38f15
AM
10555 break;
10556
10557 case R_PPC64_PLT16_HA:
10558 case R_PPC64_PLT16_HI:
10559 case R_PPC64_PLT16_LO:
10560 case R_PPC64_PLT32:
10561 case R_PPC64_PLT64:
10562 /* Relocation is to the entry for this symbol in the
10563 procedure linkage table. */
10564
10565 /* Resolve a PLT reloc against a local symbol directly,
10566 without using the procedure linkage table. */
10567 if (h == NULL)
10568 break;
10569
411e1bfb
AM
10570 /* It's possible that we didn't make a PLT entry for this
10571 symbol. This happens when statically linking PIC code,
10572 or when using -Bsymbolic. Go find a match if there is a
10573 PLT entry. */
4ce794b7 10574 if (htab->plt != NULL)
65f38f15 10575 {
411e1bfb 10576 struct plt_entry *ent;
039b3fef 10577 for (ent = h->elf.plt.plist; ent != NULL; ent = ent->next)
4cc603a5 10578 if (ent->addend == orig_addend
411e1bfb
AM
10579 && ent->plt.offset != (bfd_vma) -1)
10580 {
4ce794b7
AM
10581 relocation = (htab->plt->output_section->vma
10582 + htab->plt->output_offset
411e1bfb
AM
10583 + ent->plt.offset);
10584 unresolved_reloc = FALSE;
10585 }
65f38f15 10586 }
65f38f15 10587 break;
5bd4f169 10588
0b13192e
AM
10589 case R_PPC64_TOC:
10590 /* Relocation value is TOC base. */
10591 relocation = TOCstart;
10592 if (r_symndx == 0)
10593 relocation += htab->stub_group[input_section->id].toc_off;
8517fae7
AM
10594 else if (unresolved_reloc)
10595 ;
10596 else if (sec != NULL && sec->id <= htab->top_id)
0b13192e
AM
10597 relocation += htab->stub_group[sec->id].toc_off;
10598 else
10599 unresolved_reloc = TRUE;
10600 goto dodyn2;
10601
5bd4f169
AM
10602 /* TOC16 relocs. We want the offset relative to the TOC base,
10603 which is the address of the start of the TOC plus 0x8000.
10604 The TOC consists of sections .got, .toc, .tocbss, and .plt,
10605 in this order. */
65f38f15
AM
10606 case R_PPC64_TOC16:
10607 case R_PPC64_TOC16_LO:
10608 case R_PPC64_TOC16_HI:
10609 case R_PPC64_TOC16_DS:
10610 case R_PPC64_TOC16_LO_DS:
10611 case R_PPC64_TOC16_HA:
ad8e1ba5 10612 addend -= TOCstart + htab->stub_group[input_section->id].toc_off;
5bd4f169
AM
10613 break;
10614
10615 /* Relocate against the beginning of the section. */
65f38f15
AM
10616 case R_PPC64_SECTOFF:
10617 case R_PPC64_SECTOFF_LO:
10618 case R_PPC64_SECTOFF_HI:
10619 case R_PPC64_SECTOFF_DS:
10620 case R_PPC64_SECTOFF_LO_DS:
10621 case R_PPC64_SECTOFF_HA:
4ce794b7 10622 if (sec != NULL)
65f38f15 10623 addend -= sec->output_section->vma;
5bd4f169
AM
10624 break;
10625
721956f4
AM
10626 case R_PPC64_REL14:
10627 case R_PPC64_REL14_BRNTAKEN:
10628 case R_PPC64_REL14_BRTAKEN:
5d1634d7
AM
10629 case R_PPC64_REL24:
10630 break;
10631
411e1bfb
AM
10632 case R_PPC64_TPREL16:
10633 case R_PPC64_TPREL16_LO:
10634 case R_PPC64_TPREL16_HI:
10635 case R_PPC64_TPREL16_HA:
10636 case R_PPC64_TPREL16_DS:
10637 case R_PPC64_TPREL16_LO_DS:
10638 case R_PPC64_TPREL16_HIGHER:
10639 case R_PPC64_TPREL16_HIGHERA:
10640 case R_PPC64_TPREL16_HIGHEST:
10641 case R_PPC64_TPREL16_HIGHESTA:
e1918d23 10642 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
411e1bfb
AM
10643 if (info->shared)
10644 /* The TPREL16 relocs shouldn't really be used in shared
10645 libs as they will result in DT_TEXTREL being set, but
10646 support them anyway. */
10647 goto dodyn;
10648 break;
10649
10650 case R_PPC64_DTPREL16:
10651 case R_PPC64_DTPREL16_LO:
10652 case R_PPC64_DTPREL16_HI:
10653 case R_PPC64_DTPREL16_HA:
10654 case R_PPC64_DTPREL16_DS:
10655 case R_PPC64_DTPREL16_LO_DS:
10656 case R_PPC64_DTPREL16_HIGHER:
10657 case R_PPC64_DTPREL16_HIGHERA:
10658 case R_PPC64_DTPREL16_HIGHEST:
10659 case R_PPC64_DTPREL16_HIGHESTA:
e1918d23 10660 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
411e1bfb
AM
10661 break;
10662
e515b051
AM
10663 case R_PPC64_DTPMOD64:
10664 relocation = 1;
10665 addend = 0;
10666 goto dodyn;
10667
411e1bfb 10668 case R_PPC64_TPREL64:
e1918d23 10669 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
411e1bfb
AM
10670 goto dodyn;
10671
10672 case R_PPC64_DTPREL64:
e1918d23 10673 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
411e1bfb
AM
10674 /* Fall thru */
10675
65f38f15
AM
10676 /* Relocations that may need to be propagated if this is a
10677 dynamic object. */
04c9666a 10678 case R_PPC64_REL30:
65f38f15
AM
10679 case R_PPC64_REL32:
10680 case R_PPC64_REL64:
10681 case R_PPC64_ADDR14:
10682 case R_PPC64_ADDR14_BRNTAKEN:
10683 case R_PPC64_ADDR14_BRTAKEN:
10684 case R_PPC64_ADDR16:
10685 case R_PPC64_ADDR16_DS:
10686 case R_PPC64_ADDR16_HA:
10687 case R_PPC64_ADDR16_HI:
10688 case R_PPC64_ADDR16_HIGHER:
10689 case R_PPC64_ADDR16_HIGHERA:
10690 case R_PPC64_ADDR16_HIGHEST:
10691 case R_PPC64_ADDR16_HIGHESTA:
10692 case R_PPC64_ADDR16_LO:
10693 case R_PPC64_ADDR16_LO_DS:
10694 case R_PPC64_ADDR24:
65f38f15
AM
10695 case R_PPC64_ADDR32:
10696 case R_PPC64_ADDR64:
10697 case R_PPC64_UADDR16:
10698 case R_PPC64_UADDR32:
10699 case R_PPC64_UADDR64:
ec338859
AM
10700 /* r_symndx will be zero only for relocs against symbols
10701 from removed linkonce sections, or sections discarded by
10702 a linker script. */
411e1bfb 10703 dodyn:
5d1634d7
AM
10704 if (r_symndx == 0)
10705 break;
10706 /* Fall thru. */
10707
0b13192e 10708 dodyn2:
5d1634d7 10709 if ((input_section->flags & SEC_ALLOC) == 0)
ec338859
AM
10710 break;
10711
41bd81ab
AM
10712 if (NO_OPD_RELOCS && is_opd)
10713 break;
10714
65f38f15 10715 if ((info->shared
4e795f50 10716 && (h == NULL
039b3fef
AM
10717 || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
10718 || h->elf.root.type != bfd_link_hash_undefweak)
411e1bfb 10719 && (MUST_BE_DYN_RELOC (r_type)
039b3fef 10720 || !SYMBOL_CALLS_LOCAL (info, &h->elf)))
f4656909
AM
10721 || (ELIMINATE_COPY_RELOCS
10722 && !info->shared
65f38f15 10723 && h != NULL
039b3fef 10724 && h->elf.dynindx != -1
f5385ebf
AM
10725 && !h->elf.non_got_ref
10726 && h->elf.def_dynamic
10727 && !h->elf.def_regular))
65f38f15
AM
10728 {
10729 Elf_Internal_Rela outrel;
b34976b6 10730 bfd_boolean skip, relocate;
65f38f15 10731 asection *sreloc;
947216bf 10732 bfd_byte *loc;
1cf1f670 10733 bfd_vma out_off;
65f38f15
AM
10734
10735 /* When generating a dynamic object, these relocations
10736 are copied into the output file to be resolved at run
10737 time. */
10738
b34976b6
AM
10739 skip = FALSE;
10740 relocate = FALSE;
65f38f15 10741
1cf1f670
AM
10742 out_off = _bfd_elf_section_offset (output_bfd, info,
10743 input_section, rel->r_offset);
10744 if (out_off == (bfd_vma) -1)
b34976b6 10745 skip = TRUE;
1cf1f670 10746 else if (out_off == (bfd_vma) -2)
b34976b6 10747 skip = TRUE, relocate = TRUE;
1cf1f670
AM
10748 out_off += (input_section->output_section->vma
10749 + input_section->output_offset);
10750 outrel.r_offset = out_off;
411e1bfb 10751 outrel.r_addend = rel->r_addend;
65f38f15 10752
1cf1f670
AM
10753 /* Optimize unaligned reloc use. */
10754 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
10755 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
10756 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
10757 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
10758 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
10759 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
10760 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
10761 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
10762 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
10763
65f38f15 10764 if (skip)
0bb2d96a 10765 memset (&outrel, 0, sizeof outrel);
039b3fef 10766 else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
0b13192e
AM
10767 && !is_opd
10768 && r_type != R_PPC64_TOC)
039b3fef 10769 outrel.r_info = ELF64_R_INFO (h->elf.dynindx, r_type);
65f38f15
AM
10770 else
10771 {
41bd81ab
AM
10772 /* This symbol is local, or marked to become local,
10773 or this is an opd section reloc which must point
10774 at a local function. */
65f38f15 10775 outrel.r_addend += relocation;
e86ce104 10776 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
65f38f15 10777 {
3fad3c7c 10778 if (is_opd && h != NULL)
afbe61cf
AM
10779 {
10780 /* Lie about opd entries. This case occurs
10781 when building shared libraries and we
10782 reference a function in another shared
3fad3c7c
AM
10783 lib. The same thing happens for a weak
10784 definition in an application that's
10785 overridden by a strong definition in a
10786 shared lib. (I believe this is a generic
10787 bug in binutils handling of weak syms.)
10788 In these cases we won't use the opd
1e2f5b6e 10789 entry in this lib. */
b34976b6 10790 unresolved_reloc = FALSE;
afbe61cf 10791 }
65f38f15 10792 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69
AM
10793
10794 /* We need to relocate .opd contents for ld.so.
10795 Prelink also wants simple and consistent rules
10796 for relocs. This make all RELATIVE relocs have
10797 *r_offset equal to r_addend. */
10798 relocate = TRUE;
65f38f15
AM
10799 }
10800 else
10801 {
10802 long indx = 0;
10803
41bd81ab 10804 if (bfd_is_abs_section (sec))
65f38f15
AM
10805 ;
10806 else if (sec == NULL || sec->owner == NULL)
10807 {
10808 bfd_set_error (bfd_error_bad_value);
b34976b6 10809 return FALSE;
65f38f15
AM
10810 }
10811 else
10812 {
10813 asection *osec;
10814
10815 osec = sec->output_section;
10816 indx = elf_section_data (osec)->dynindx;
10817
10818 /* We are turning this relocation into one
10819 against a section symbol, so subtract out
10820 the output section's address but not the
10821 offset of the input section in the output
10822 section. */
10823 outrel.r_addend -= osec->vma;
10824 }
10825
10826 outrel.r_info = ELF64_R_INFO (indx, r_type);
10827 }
10828 }
10829
10830 sreloc = elf_section_data (input_section)->sreloc;
10831 if (sreloc == NULL)
10832 abort ();
10833
dfbb6ac9
AM
10834 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
10835 >= sreloc->size)
10836 abort ();
947216bf
AM
10837 loc = sreloc->contents;
10838 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15
AM
10839 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
10840
10841 /* If this reloc is against an external symbol, it will
10842 be computed at runtime, so there's no need to do
81407a69
AM
10843 anything now. However, for the sake of prelink ensure
10844 that the section contents are a known value. */
65f38f15 10845 if (! relocate)
81407a69
AM
10846 {
10847 unresolved_reloc = FALSE;
10848 /* The value chosen here is quite arbitrary as ld.so
10849 ignores section contents except for the special
10850 case of .opd where the contents might be accessed
10851 before relocation. Choose zero, as that won't
10852 cause reloc overflow. */
10853 relocation = 0;
10854 addend = 0;
10855 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
10856 to improve backward compatibility with older
10857 versions of ld. */
10858 if (r_type == R_PPC64_ADDR64)
10859 addend = outrel.r_addend;
10860 /* Adjust pc_relative relocs to have zero in *r_offset. */
4ce794b7 10861 else if (ppc64_elf_howto_table[r_type]->pc_relative)
000732f7
AM
10862 addend = (input_section->output_section->vma
10863 + input_section->output_offset
10864 + rel->r_offset);
81407a69 10865 }
65f38f15 10866 }
5bd4f169
AM
10867 break;
10868
65f38f15
AM
10869 case R_PPC64_COPY:
10870 case R_PPC64_GLOB_DAT:
10871 case R_PPC64_JMP_SLOT:
10872 case R_PPC64_RELATIVE:
10873 /* We shouldn't ever see these dynamic relocs in relocatable
10874 files. */
ae9a127f 10875 /* Fall through. */
65f38f15
AM
10876
10877 case R_PPC64_PLTGOT16:
10878 case R_PPC64_PLTGOT16_DS:
10879 case R_PPC64_PLTGOT16_HA:
10880 case R_PPC64_PLTGOT16_HI:
10881 case R_PPC64_PLTGOT16_LO:
10882 case R_PPC64_PLTGOT16_LO_DS:
10883 case R_PPC64_PLTREL32:
10884 case R_PPC64_PLTREL64:
10885 /* These ones haven't been implemented yet. */
10886
10887 (*_bfd_error_handler)
d003868e
AM
10888 (_("%B: relocation %s is not supported for symbol %s."),
10889 input_bfd,
4ce794b7 10890 ppc64_elf_howto_table[r_type]->name, sym_name);
5bd4f169
AM
10891
10892 bfd_set_error (bfd_error_invalid_operation);
b34976b6 10893 ret = FALSE;
5bd4f169 10894 continue;
65f38f15 10895 }
5bd4f169 10896
65f38f15
AM
10897 /* Do any further special processing. */
10898 switch (r_type)
10899 {
10900 default:
10901 break;
10902
10903 case R_PPC64_ADDR16_HA:
10904 case R_PPC64_ADDR16_HIGHERA:
10905 case R_PPC64_ADDR16_HIGHESTA:
86bbe32f
AM
10906 case R_PPC64_GOT16_HA:
10907 case R_PPC64_PLTGOT16_HA:
65f38f15
AM
10908 case R_PPC64_PLT16_HA:
10909 case R_PPC64_TOC16_HA:
10910 case R_PPC64_SECTOFF_HA:
411e1bfb
AM
10911 case R_PPC64_TPREL16_HA:
10912 case R_PPC64_DTPREL16_HA:
10913 case R_PPC64_GOT_TLSGD16_HA:
10914 case R_PPC64_GOT_TLSLD16_HA:
10915 case R_PPC64_GOT_TPREL16_HA:
10916 case R_PPC64_GOT_DTPREL16_HA:
10917 case R_PPC64_TPREL16_HIGHER:
10918 case R_PPC64_TPREL16_HIGHERA:
10919 case R_PPC64_TPREL16_HIGHEST:
10920 case R_PPC64_TPREL16_HIGHESTA:
10921 case R_PPC64_DTPREL16_HIGHER:
10922 case R_PPC64_DTPREL16_HIGHERA:
10923 case R_PPC64_DTPREL16_HIGHEST:
10924 case R_PPC64_DTPREL16_HIGHESTA:
65f38f15
AM
10925 /* It's just possible that this symbol is a weak symbol
10926 that's not actually defined anywhere. In that case,
10927 'sec' would be NULL, and we should leave the symbol
10928 alone (it will be set to zero elsewhere in the link). */
10929 if (sec != NULL)
e515b051
AM
10930 /* Add 0x10000 if sign bit in 0:15 is set.
10931 Bits 0:15 are not used. */
10932 addend += 0x8000;
65f38f15
AM
10933 break;
10934
10935 case R_PPC64_ADDR16_DS:
10936 case R_PPC64_ADDR16_LO_DS:
10937 case R_PPC64_GOT16_DS:
10938 case R_PPC64_GOT16_LO_DS:
10939 case R_PPC64_PLT16_LO_DS:
10940 case R_PPC64_SECTOFF_DS:
10941 case R_PPC64_SECTOFF_LO_DS:
10942 case R_PPC64_TOC16_DS:
10943 case R_PPC64_TOC16_LO_DS:
10944 case R_PPC64_PLTGOT16_DS:
10945 case R_PPC64_PLTGOT16_LO_DS:
411e1bfb
AM
10946 case R_PPC64_GOT_TPREL16_DS:
10947 case R_PPC64_GOT_TPREL16_LO_DS:
10948 case R_PPC64_GOT_DTPREL16_DS:
10949 case R_PPC64_GOT_DTPREL16_LO_DS:
10950 case R_PPC64_TPREL16_DS:
10951 case R_PPC64_TPREL16_LO_DS:
10952 case R_PPC64_DTPREL16_DS:
10953 case R_PPC64_DTPREL16_LO_DS:
adadcc0c
AM
10954 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
10955 mask = 3;
10956 /* If this reloc is against an lq insn, then the value must be
10957 a multiple of 16. This is somewhat of a hack, but the
10958 "correct" way to do this by defining _DQ forms of all the
10959 _DS relocs bloats all reloc switches in this file. It
10960 doesn't seem to make much sense to use any of these relocs
10961 in data, so testing the insn should be safe. */
494dac0c 10962 if ((insn & (0x3f << 26)) == (56u << 26))
adadcc0c
AM
10963 mask = 15;
10964 if (((relocation + addend) & mask) != 0)
65f38f15
AM
10965 {
10966 (*_bfd_error_handler)
d003868e
AM
10967 (_("%B: error: relocation %s not a multiple of %d"),
10968 input_bfd,
4ce794b7 10969 ppc64_elf_howto_table[r_type]->name,
adadcc0c 10970 mask + 1);
65f38f15 10971 bfd_set_error (bfd_error_bad_value);
b34976b6 10972 ret = FALSE;
65f38f15
AM
10973 continue;
10974 }
10975 break;
5bd4f169
AM
10976 }
10977
239e1f3a
AM
10978 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
10979 because such sections are not SEC_ALLOC and thus ld.so will
10980 not process them. */
65f38f15 10981 if (unresolved_reloc
239e1f3a 10982 && !((input_section->flags & SEC_DEBUGGING) != 0
f5385ebf 10983 && h->elf.def_dynamic))
9c07fe7c
AM
10984 {
10985 (*_bfd_error_handler)
d003868e
AM
10986 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
10987 input_bfd,
10988 input_section,
9c07fe7c 10989 (long) rel->r_offset,
7b609f53 10990 ppc64_elf_howto_table[(int) r_type]->name,
039b3fef 10991 h->elf.root.root.string);
b34976b6 10992 ret = FALSE;
9c07fe7c 10993 }
5bd4f169 10994
65f38f15 10995 r = _bfd_final_link_relocate (ppc64_elf_howto_table[(int) r_type],
5bd4f169
AM
10996 input_bfd,
10997 input_section,
10998 contents,
411e1bfb 10999 rel->r_offset,
5bd4f169
AM
11000 relocation,
11001 addend);
11002
ef60b7ff 11003 if (r != bfd_reloc_ok)
5bd4f169 11004 {
cd27b276
AM
11005 if (sym_name == NULL)
11006 sym_name = "(null)";
11007 if (r == bfd_reloc_overflow)
5bd4f169 11008 {
cd27b276
AM
11009 if (warned)
11010 continue;
11011 if (h != NULL
039b3fef 11012 && h->elf.root.type == bfd_link_hash_undefweak
4ce794b7 11013 && ppc64_elf_howto_table[r_type]->pc_relative)
5bd4f169
AM
11014 {
11015 /* Assume this is a call protected by other code that
11016 detects the symbol is undefined. If this is the case,
11017 we can safely ignore the overflow. If not, the
11018 program is hosed anyway, and a little warning isn't
11019 going to help. */
11020
11021 continue;
11022 }
11023
ef60b7ff 11024 if (!((*info->callbacks->reloc_overflow)
dfeffb9f
L
11025 (info, (h ? &h->elf.root : NULL), sym_name,
11026 ppc64_elf_howto_table[r_type]->name,
4cc603a5 11027 orig_addend, input_bfd, input_section, rel->r_offset)))
b34976b6 11028 return FALSE;
ef60b7ff
AM
11029 }
11030 else
11031 {
11032 (*_bfd_error_handler)
d003868e
AM
11033 (_("%B(%A+0x%lx): %s reloc against `%s': error %d"),
11034 input_bfd,
11035 input_section,
7b609f53 11036 (long) rel->r_offset,
4ce794b7 11037 ppc64_elf_howto_table[r_type]->name,
7b609f53
AM
11038 sym_name,
11039 (int) r);
b34976b6 11040 ret = FALSE;
ef60b7ff 11041 }
5bd4f169 11042 }
5bd4f169
AM
11043 }
11044
645ea6a9
AM
11045 /* If we're emitting relocations, then shortly after this function
11046 returns, reloc offsets and addends for this section will be
11047 adjusted. Worse, reloc symbol indices will be for the output
8860955f
AM
11048 file rather than the input. Save a copy of the relocs for
11049 opd_entry_value. */
4cc603a5 11050 if (is_opd && (info->emitrelocations || info->relocatable))
8860955f
AM
11051 {
11052 bfd_size_type amt;
11053 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
11054 rel = bfd_alloc (input_bfd, amt);
11055 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd_relocs == NULL);
11056 ppc64_elf_tdata (input_bfd)->opd_relocs = rel;
11057 if (rel == NULL)
11058 return FALSE;
11059 memcpy (rel, relocs, amt);
11060 }
5bd4f169
AM
11061 return ret;
11062}
11063
754021d0
AM
11064/* Adjust the value of any local symbols in opd sections. */
11065
11066static bfd_boolean
11067ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
11068 const char *name ATTRIBUTE_UNUSED,
11069 Elf_Internal_Sym *elfsym,
11070 asection *input_sec,
11071 struct elf_link_hash_entry *h)
11072{
4025353c 11073 long *opd_adjust, adjust;
754021d0
AM
11074 bfd_vma value;
11075
4025353c
AM
11076 if (h != NULL)
11077 return TRUE;
11078
11079 opd_adjust = get_opd_info (input_sec);
11080 if (opd_adjust == NULL)
754021d0
AM
11081 return TRUE;
11082
11083 value = elfsym->st_value - input_sec->output_offset;
11084 if (!info->relocatable)
11085 value -= input_sec->output_section->vma;
11086
3f764659 11087 adjust = opd_adjust[value / 8];
4025353c
AM
11088 if (adjust == -1)
11089 elfsym->st_value = 0;
11090 else
11091 elfsym->st_value += adjust;
754021d0
AM
11092 return TRUE;
11093}
11094
5bd4f169
AM
11095/* Finish up dynamic symbol handling. We set the contents of various
11096 dynamic sections here. */
11097
b34976b6 11098static bfd_boolean
4ce794b7
AM
11099ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
11100 struct bfd_link_info *info,
11101 struct elf_link_hash_entry *h,
11102 Elf_Internal_Sym *sym)
5bd4f169 11103{
65f38f15 11104 struct ppc_link_hash_table *htab;
8387904d
AM
11105 struct plt_entry *ent;
11106 Elf_Internal_Rela rela;
11107 bfd_byte *loc;
5bd4f169 11108
65f38f15 11109 htab = ppc_hash_table (info);
5bd4f169 11110
8387904d
AM
11111 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
11112 if (ent->plt.offset != (bfd_vma) -1)
11113 {
11114 /* This symbol has an entry in the procedure linkage
11115 table. Set it up. */
11116
11117 if (htab->plt == NULL
11118 || htab->relplt == NULL
11119 || htab->glink == NULL)
11120 abort ();
11121
11122 /* Create a JMP_SLOT reloc to inform the dynamic linker to
11123 fill in the PLT entry. */
11124 rela.r_offset = (htab->plt->output_section->vma
11125 + htab->plt->output_offset
11126 + ent->plt.offset);
11127 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
11128 rela.r_addend = ent->addend;
11129
11130 loc = htab->relplt->contents;
11131 loc += ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE) / PLT_ENTRY_SIZE
11132 * sizeof (Elf64_External_Rela));
11133 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
11134 }
5bd4f169 11135
f5385ebf 11136 if (h->needs_copy)
5bd4f169 11137 {
5bd4f169 11138 Elf_Internal_Rela rela;
947216bf 11139 bfd_byte *loc;
5bd4f169 11140
65f38f15 11141 /* This symbol needs a copy reloc. Set it up. */
5bd4f169 11142
65f38f15
AM
11143 if (h->dynindx == -1
11144 || (h->root.type != bfd_link_hash_defined
11145 && h->root.type != bfd_link_hash_defweak)
4ce794b7 11146 || htab->relbss == NULL)
65f38f15 11147 abort ();
5bd4f169
AM
11148
11149 rela.r_offset = (h->root.u.def.value
11150 + h->root.u.def.section->output_section->vma
11151 + h->root.u.def.section->output_offset);
11152 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
11153 rela.r_addend = 0;
4ce794b7
AM
11154 loc = htab->relbss->contents;
11155 loc += htab->relbss->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15 11156 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
5bd4f169
AM
11157 }
11158
5bd4f169
AM
11159 /* Mark some specially defined symbols as absolute. */
11160 if (strcmp (h->root.root.string, "_DYNAMIC") == 0)
11161 sym->st_shndx = SHN_ABS;
11162
b34976b6 11163 return TRUE;
5bd4f169
AM
11164}
11165
65f38f15
AM
11166/* Used to decide how to sort relocs in an optimal manner for the
11167 dynamic linker, before writing them out. */
11168
11169static enum elf_reloc_type_class
4ce794b7 11170ppc64_elf_reloc_type_class (const Elf_Internal_Rela *rela)
65f38f15 11171{
04c9666a 11172 enum elf_ppc64_reloc_type r_type;
a33d1f77 11173
4ce794b7 11174 r_type = ELF64_R_TYPE (rela->r_info);
a33d1f77 11175 switch (r_type)
65f38f15
AM
11176 {
11177 case R_PPC64_RELATIVE:
11178 return reloc_class_relative;
11179 case R_PPC64_JMP_SLOT:
11180 return reloc_class_plt;
11181 case R_PPC64_COPY:
11182 return reloc_class_copy;
11183 default:
11184 return reloc_class_normal;
11185 }
11186}
11187
5bd4f169
AM
11188/* Finish up the dynamic sections. */
11189
b34976b6 11190static bfd_boolean
4ce794b7
AM
11191ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
11192 struct bfd_link_info *info)
5bd4f169 11193{
65f38f15
AM
11194 struct ppc_link_hash_table *htab;
11195 bfd *dynobj;
5bd4f169 11196 asection *sdyn;
5bd4f169 11197
65f38f15
AM
11198 htab = ppc_hash_table (info);
11199 dynobj = htab->elf.dynobj;
5bd4f169
AM
11200 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
11201
65f38f15 11202 if (htab->elf.dynamic_sections_created)
5bd4f169 11203 {
5bd4f169
AM
11204 Elf64_External_Dyn *dyncon, *dynconend;
11205
4ce794b7 11206 if (sdyn == NULL || htab->got == NULL)
65f38f15 11207 abort ();
5bd4f169
AM
11208
11209 dyncon = (Elf64_External_Dyn *) sdyn->contents;
eea6121a 11210 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
5bd4f169
AM
11211 for (; dyncon < dynconend; dyncon++)
11212 {
11213 Elf_Internal_Dyn dyn;
19397422 11214 asection *s;
5bd4f169
AM
11215
11216 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
11217
11218 switch (dyn.d_tag)
11219 {
65f38f15
AM
11220 default:
11221 continue;
5bd4f169 11222
5d1634d7 11223 case DT_PPC64_GLINK:
4ce794b7 11224 s = htab->glink;
6348e046 11225 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
ad8e1ba5
AM
11226 /* We stupidly defined DT_PPC64_GLINK to be the start
11227 of glink rather than the first entry point, which is
11228 what ld.so needs, and now have a bigger stub to
11229 support automatic multiple TOCs. */
11230 dyn.d_un.d_ptr += GLINK_CALL_STUB_SIZE - 32;
5d1634d7
AM
11231 break;
11232
19397422
AM
11233 case DT_PPC64_OPD:
11234 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
11235 if (s == NULL)
11236 continue;
11237 dyn.d_un.d_ptr = s->vma;
19397422
AM
11238 break;
11239
11240 case DT_PPC64_OPDSZ:
11241 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
11242 if (s == NULL)
11243 continue;
eea6121a 11244 dyn.d_un.d_val = s->size;
19397422
AM
11245 break;
11246
65f38f15 11247 case DT_PLTGOT:
4ce794b7 11248 s = htab->plt;
6348e046 11249 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15
AM
11250 break;
11251
11252 case DT_JMPREL:
4ce794b7 11253 s = htab->relplt;
6348e046 11254 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15 11255 break;
5bd4f169 11256
65f38f15 11257 case DT_PLTRELSZ:
eea6121a 11258 dyn.d_un.d_val = htab->relplt->size;
5d1634d7
AM
11259 break;
11260
11261 case DT_RELASZ:
11262 /* Don't count procedure linkage table relocs in the
11263 overall reloc count. */
4ce794b7 11264 s = htab->relplt;
6348e046
AM
11265 if (s == NULL)
11266 continue;
eea6121a 11267 dyn.d_un.d_val -= s->size;
6348e046
AM
11268 break;
11269
11270 case DT_RELA:
11271 /* We may not be using the standard ELF linker script.
11272 If .rela.plt is the first .rela section, we adjust
11273 DT_RELA to not include it. */
4ce794b7 11274 s = htab->relplt;
6348e046
AM
11275 if (s == NULL)
11276 continue;
11277 if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
11278 continue;
eea6121a 11279 dyn.d_un.d_ptr += s->size;
65f38f15 11280 break;
5bd4f169 11281 }
5bd4f169 11282
65f38f15 11283 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
5bd4f169 11284 }
5bd4f169
AM
11285 }
11286
eea6121a 11287 if (htab->got != NULL && htab->got->size != 0)
5d1634d7
AM
11288 {
11289 /* Fill in the first entry in the global offset table.
11290 We use it to hold the link-time TOCbase. */
11291 bfd_put_64 (output_bfd,
60ee0d4a 11292 elf_gp (output_bfd) + TOC_BASE_OFF,
4ce794b7 11293 htab->got->contents);
5d1634d7
AM
11294
11295 /* Set .got entry size. */
4ce794b7 11296 elf_section_data (htab->got->output_section)->this_hdr.sh_entsize = 8;
5d1634d7
AM
11297 }
11298
eea6121a 11299 if (htab->plt != NULL && htab->plt->size != 0)
5d1634d7
AM
11300 {
11301 /* Set .plt entry size. */
4ce794b7 11302 elf_section_data (htab->plt->output_section)->this_hdr.sh_entsize
5d1634d7
AM
11303 = PLT_ENTRY_SIZE;
11304 }
11305
e717da7e 11306 /* We need to handle writing out multiple GOT sections ourselves,
7b53ace3
AM
11307 since we didn't add them to DYNOBJ. We know dynobj is the first
11308 bfd. */
e717da7e
AM
11309 while ((dynobj = dynobj->link_next) != NULL)
11310 {
11311 asection *s;
7b53ace3 11312
ee75fd95 11313 if (!is_ppc64_elf_target (dynobj->xvec))
7b53ace3
AM
11314 continue;
11315
e717da7e
AM
11316 s = ppc64_elf_tdata (dynobj)->got;
11317 if (s != NULL
eea6121a 11318 && s->size != 0
e717da7e
AM
11319 && s->output_section != bfd_abs_section_ptr
11320 && !bfd_set_section_contents (output_bfd, s->output_section,
11321 s->contents, s->output_offset,
eea6121a 11322 s->size))
e717da7e
AM
11323 return FALSE;
11324 s = ppc64_elf_tdata (dynobj)->relgot;
11325 if (s != NULL
eea6121a 11326 && s->size != 0
e717da7e
AM
11327 && s->output_section != bfd_abs_section_ptr
11328 && !bfd_set_section_contents (output_bfd, s->output_section,
11329 s->contents, s->output_offset,
eea6121a 11330 s->size))
e717da7e
AM
11331 return FALSE;
11332 }
f6c52c13 11333
b34976b6 11334 return TRUE;
5bd4f169
AM
11335}
11336
5bd4f169 11337#include "elf64-target.h"
This page took 1.726572 seconds and 4 git commands to generate.