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