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