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