dda658de08c79a0166d56427116845bfcbf0fb9c
[deliverable/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 /* Add extra PPC sections. */
2406
2407 static struct bfd_elf_special_section const ppc64_elf_special_sections[]=
2408 {
2409 { ".sdata", 0, NULL, 0,
2410 SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2411 { ".sbss", 0, NULL, 0,
2412 SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2413 { ".plt", 0, NULL, 0,
2414 SHT_NOBITS, 0 },
2415 { ".toc", 0, NULL, 0,
2416 SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2417 { ".toc1", 0, NULL, 0,
2418 SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2419 { ".tocbss", 0, NULL, 0,
2420 SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2421 { NULL, 0, NULL, 0,
2422 0, 0 }
2423 };
2424
2425 struct _ppc64_elf_section_data
2426 {
2427 struct bfd_elf_section_data elf;
2428
2429 /* An array with one entry for each opd function descriptor. */
2430 union
2431 {
2432 /* Points to the function code section for local opd entries. */
2433 asection **func_sec;
2434 /* After editing .opd, adjust references to opd local syms. */
2435 long *adjust;
2436 } opd;
2437
2438 /* An array for toc sections, indexed by offset/8.
2439 Specifies the relocation symbol index used at a given toc offset. */
2440 unsigned *t_symndx;
2441 };
2442
2443 #define ppc64_elf_section_data(sec) \
2444 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
2445
2446 static bfd_boolean
2447 ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
2448 {
2449 struct _ppc64_elf_section_data *sdata;
2450 bfd_size_type amt = sizeof (*sdata);
2451
2452 sdata = bfd_zalloc (abfd, amt);
2453 if (sdata == NULL)
2454 return FALSE;
2455 sec->used_by_bfd = sdata;
2456
2457 return _bfd_elf_new_section_hook (abfd, sec);
2458 }
2459 \f
2460 /* The following functions are specific to the ELF linker, while
2461 functions above are used generally. Those named ppc64_elf_* are
2462 called by the main ELF linker code. They appear in this file more
2463 or less in the order in which they are called. eg.
2464 ppc64_elf_check_relocs is called early in the link process,
2465 ppc64_elf_finish_dynamic_sections is one of the last functions
2466 called.
2467
2468 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
2469 functions have both a function code symbol and a function descriptor
2470 symbol. A call to foo in a relocatable object file looks like:
2471
2472 . .text
2473 . x:
2474 . bl .foo
2475 . nop
2476
2477 The function definition in another object file might be:
2478
2479 . .section .opd
2480 . foo: .quad .foo
2481 . .quad .TOC.@tocbase
2482 . .quad 0
2483 .
2484 . .text
2485 . .foo: blr
2486
2487 When the linker resolves the call during a static link, the branch
2488 unsurprisingly just goes to .foo and the .opd information is unused.
2489 If the function definition is in a shared library, things are a little
2490 different: The call goes via a plt call stub, the opd information gets
2491 copied to the plt, and the linker patches the nop.
2492
2493 . x:
2494 . bl .foo_stub
2495 . ld 2,40(1)
2496 .
2497 .
2498 . .foo_stub:
2499 . addis 12,2,Lfoo@toc@ha # in practice, the call stub
2500 . addi 12,12,Lfoo@toc@l # is slightly optimized, but
2501 . std 2,40(1) # this is the general idea
2502 . ld 11,0(12)
2503 . ld 2,8(12)
2504 . mtctr 11
2505 . ld 11,16(12)
2506 . bctr
2507 .
2508 . .section .plt
2509 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
2510
2511 The "reloc ()" notation is supposed to indicate that the linker emits
2512 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
2513 copying.
2514
2515 What are the difficulties here? Well, firstly, the relocations
2516 examined by the linker in check_relocs are against the function code
2517 sym .foo, while the dynamic relocation in the plt is emitted against
2518 the function descriptor symbol, foo. Somewhere along the line, we need
2519 to carefully copy dynamic link information from one symbol to the other.
2520 Secondly, the generic part of the elf linker will make .foo a dynamic
2521 symbol as is normal for most other backends. We need foo dynamic
2522 instead, at least for an application final link. However, when
2523 creating a shared library containing foo, we need to have both symbols
2524 dynamic so that references to .foo are satisfied during the early
2525 stages of linking. Otherwise the linker might decide to pull in a
2526 definition from some other object, eg. a static library. */
2527
2528 /* The linker needs to keep track of the number of relocs that it
2529 decides to copy as dynamic relocs in check_relocs for each symbol.
2530 This is so that it can later discard them if they are found to be
2531 unnecessary. We store the information in a field extending the
2532 regular ELF linker hash table. */
2533
2534 struct ppc_dyn_relocs
2535 {
2536 struct ppc_dyn_relocs *next;
2537
2538 /* The input section of the reloc. */
2539 asection *sec;
2540
2541 /* Total number of relocs copied for the input section. */
2542 bfd_size_type count;
2543
2544 /* Number of pc-relative relocs copied for the input section. */
2545 bfd_size_type pc_count;
2546 };
2547
2548 /* Track GOT entries needed for a given symbol. We might need more
2549 than one got entry per symbol. */
2550 struct got_entry
2551 {
2552 struct got_entry *next;
2553
2554 /* The symbol addend that we'll be placing in the GOT. */
2555 bfd_vma addend;
2556
2557 /* Unlike other ELF targets, we use separate GOT entries for the same
2558 symbol referenced from different input files. This is to support
2559 automatic multiple TOC/GOT sections, where the TOC base can vary
2560 from one input file to another.
2561
2562 Point to the BFD owning this GOT entry. */
2563 bfd *owner;
2564
2565 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
2566 TLS_TPREL or TLS_DTPREL for tls entries. */
2567 char tls_type;
2568
2569 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
2570 union
2571 {
2572 bfd_signed_vma refcount;
2573 bfd_vma offset;
2574 } got;
2575 };
2576
2577 /* The same for PLT. */
2578 struct plt_entry
2579 {
2580 struct plt_entry *next;
2581
2582 bfd_vma addend;
2583
2584 union
2585 {
2586 bfd_signed_vma refcount;
2587 bfd_vma offset;
2588 } plt;
2589 };
2590
2591 /* Of those relocs that might be copied as dynamic relocs, this macro
2592 selects those that must be copied when linking a shared library,
2593 even when the symbol is local. */
2594
2595 #define MUST_BE_DYN_RELOC(RTYPE) \
2596 ((RTYPE) != R_PPC64_REL32 \
2597 && (RTYPE) != R_PPC64_REL64 \
2598 && (RTYPE) != R_PPC64_REL30)
2599
2600 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
2601 copying dynamic variables from a shared lib into an app's dynbss
2602 section, and instead use a dynamic relocation to point into the
2603 shared lib. With code that gcc generates, it's vital that this be
2604 enabled; In the PowerPC64 ABI, the address of a function is actually
2605 the address of a function descriptor, which resides in the .opd
2606 section. gcc uses the descriptor directly rather than going via the
2607 GOT as some other ABI's do, which means that initialized function
2608 pointers must reference the descriptor. Thus, a function pointer
2609 initialized to the address of a function in a shared library will
2610 either require a copy reloc, or a dynamic reloc. Using a copy reloc
2611 redefines the function desctriptor symbol to point to the copy. This
2612 presents a problem as a plt entry for that function is also
2613 initialized from the function descriptor symbol and the copy reloc
2614 may not be initialized first. */
2615 #define ELIMINATE_COPY_RELOCS 1
2616
2617 /* Section name for stubs is the associated section name plus this
2618 string. */
2619 #define STUB_SUFFIX ".stub"
2620
2621 /* Linker stubs.
2622 ppc_stub_long_branch:
2623 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
2624 destination, but a 24 bit branch in a stub section will reach.
2625 . b dest
2626
2627 ppc_stub_plt_branch:
2628 Similar to the above, but a 24 bit branch in the stub section won't
2629 reach its destination.
2630 . addis %r12,%r2,xxx@toc@ha
2631 . ld %r11,xxx@toc@l(%r12)
2632 . mtctr %r11
2633 . bctr
2634
2635 ppc_stub_plt_call:
2636 Used to call a function in a shared library.
2637 . addis %r12,%r2,xxx@toc@ha
2638 . std %r2,40(%r1)
2639 . ld %r11,xxx+0@toc@l(%r12)
2640 . ld %r2,xxx+8@toc@l(%r12)
2641 . mtctr %r11
2642 . ld %r11,xxx+16@toc@l(%r12)
2643 . bctr
2644
2645 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
2646 code to adjust the value and save r2 to support multiple toc sections.
2647 A ppc_stub_long_branch with an r2 offset looks like:
2648 . std %r2,40(%r1)
2649 . addis %r2,%r2,off@ha
2650 . addi %r2,%r2,off@l
2651 . b dest
2652
2653 A ppc_stub_plt_branch with an r2 offset looks like:
2654 . std %r2,40(%r1)
2655 . addis %r12,%r2,xxx@toc@ha
2656 . ld %r11,xxx@toc@l(%r12)
2657 . addis %r2,%r2,off@ha
2658 . addi %r2,%r2,off@l
2659 . mtctr %r11
2660 . bctr
2661 */
2662
2663 enum ppc_stub_type {
2664 ppc_stub_none,
2665 ppc_stub_long_branch,
2666 ppc_stub_long_branch_r2off,
2667 ppc_stub_plt_branch,
2668 ppc_stub_plt_branch_r2off,
2669 ppc_stub_plt_call
2670 };
2671
2672 struct ppc_stub_hash_entry {
2673
2674 /* Base hash table entry structure. */
2675 struct bfd_hash_entry root;
2676
2677 enum ppc_stub_type stub_type;
2678
2679 /* The stub section. */
2680 asection *stub_sec;
2681
2682 /* Offset within stub_sec of the beginning of this stub. */
2683 bfd_vma stub_offset;
2684
2685 /* Given the symbol's value and its section we can determine its final
2686 value when building the stubs (so the stub knows where to jump. */
2687 bfd_vma target_value;
2688 asection *target_section;
2689
2690 /* The symbol table entry, if any, that this was derived from. */
2691 struct ppc_link_hash_entry *h;
2692
2693 /* And the reloc addend that this was derived from. */
2694 bfd_vma addend;
2695
2696 /* Where this stub is being called from, or, in the case of combined
2697 stub sections, the first input section in the group. */
2698 asection *id_sec;
2699 };
2700
2701 struct ppc_branch_hash_entry {
2702
2703 /* Base hash table entry structure. */
2704 struct bfd_hash_entry root;
2705
2706 /* Offset within .branch_lt. */
2707 unsigned int offset;
2708
2709 /* Generation marker. */
2710 unsigned int iter;
2711 };
2712
2713 struct ppc_link_hash_entry
2714 {
2715 struct elf_link_hash_entry elf;
2716
2717 /* A pointer to the most recently used stub hash entry against this
2718 symbol. */
2719 struct ppc_stub_hash_entry *stub_cache;
2720
2721 /* Track dynamic relocs copied for this symbol. */
2722 struct ppc_dyn_relocs *dyn_relocs;
2723
2724 /* Link between function code and descriptor symbols. */
2725 struct elf_link_hash_entry *oh;
2726
2727 /* Flag function code and descriptor symbols. */
2728 unsigned int is_func:1;
2729 unsigned int is_func_descriptor:1;
2730 unsigned int is_entry:1;
2731
2732 /* Contexts in which symbol is used in the GOT (or TOC).
2733 TLS_GD .. TLS_EXPLICIT bits are or'd into the mask as the
2734 corresponding relocs are encountered during check_relocs.
2735 tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
2736 indicate the corresponding GOT entry type is not needed.
2737 tls_optimize may also set TLS_TPRELGD when a GD reloc turns into
2738 a TPREL one. We use a separate flag rather than setting TPREL
2739 just for convenience in distinguishing the two cases. */
2740 #define TLS_GD 1 /* GD reloc. */
2741 #define TLS_LD 2 /* LD reloc. */
2742 #define TLS_TPREL 4 /* TPREL reloc, => IE. */
2743 #define TLS_DTPREL 8 /* DTPREL reloc, => LD. */
2744 #define TLS_TLS 16 /* Any TLS reloc. */
2745 #define TLS_EXPLICIT 32 /* Marks TOC section TLS relocs. */
2746 #define TLS_TPRELGD 64 /* TPREL reloc resulting from GD->IE. */
2747 char tls_mask;
2748 };
2749
2750 /* ppc64 ELF linker hash table. */
2751
2752 struct ppc_link_hash_table
2753 {
2754 struct elf_link_hash_table elf;
2755
2756 /* The stub hash table. */
2757 struct bfd_hash_table stub_hash_table;
2758
2759 /* Another hash table for plt_branch stubs. */
2760 struct bfd_hash_table branch_hash_table;
2761
2762 /* Linker stub bfd. */
2763 bfd *stub_bfd;
2764
2765 /* Linker call-backs. */
2766 asection * (*add_stub_section) (const char *, asection *);
2767 void (*layout_sections_again) (void);
2768
2769 /* Array to keep track of which stub sections have been created, and
2770 information on stub grouping. */
2771 struct map_stub {
2772 /* This is the section to which stubs in the group will be attached. */
2773 asection *link_sec;
2774 /* The stub section. */
2775 asection *stub_sec;
2776 /* Along with elf_gp, specifies the TOC pointer used in this group. */
2777 bfd_vma toc_off;
2778 } *stub_group;
2779
2780 /* Support for multiple toc sections. */
2781 unsigned int no_multi_toc;
2782 unsigned int multi_toc_needed;
2783
2784 /* Temp used when calculating TOC pointers. */
2785 bfd_vma toc_curr;
2786
2787 /* Highest input section id. */
2788 int top_id;
2789
2790 /* Highest output section index. */
2791 int top_index;
2792
2793 /* List of input sections for each output section. */
2794 asection **input_list;
2795
2796 /* Short-cuts to get to dynamic linker sections. */
2797 asection *got;
2798 asection *plt;
2799 asection *relplt;
2800 asection *dynbss;
2801 asection *relbss;
2802 asection *glink;
2803 asection *sfpr;
2804 asection *brlt;
2805 asection *relbrlt;
2806
2807 /* Short-cut to first output tls section. */
2808 asection *tls_sec;
2809
2810 /* Shortcut to .__tls_get_addr. */
2811 struct elf_link_hash_entry *tls_get_addr;
2812
2813 /* Statistics. */
2814 unsigned long stub_count[ppc_stub_plt_call];
2815
2816 /* Set if we should emit symbols for stubs. */
2817 unsigned int emit_stub_syms;
2818
2819 /* Set on error. */
2820 unsigned int stub_error;
2821
2822 /* Flag set when small branches are detected. Used to
2823 select suitable defaults for the stub group size. */
2824 unsigned int has_14bit_branch;
2825
2826 /* Set if we detect a reference undefined weak symbol. */
2827 unsigned int have_undefweak;
2828
2829 /* Incremented every time we size stubs. */
2830 unsigned int stub_iteration;
2831
2832 /* Small local sym to section mapping cache. */
2833 struct sym_sec_cache sym_sec;
2834 };
2835
2836 /* Get the ppc64 ELF linker hash table from a link_info structure. */
2837
2838 #define ppc_hash_table(p) \
2839 ((struct ppc_link_hash_table *) ((p)->hash))
2840
2841 #define ppc_stub_hash_lookup(table, string, create, copy) \
2842 ((struct ppc_stub_hash_entry *) \
2843 bfd_hash_lookup ((table), (string), (create), (copy)))
2844
2845 #define ppc_branch_hash_lookup(table, string, create, copy) \
2846 ((struct ppc_branch_hash_entry *) \
2847 bfd_hash_lookup ((table), (string), (create), (copy)))
2848
2849 /* Create an entry in the stub hash table. */
2850
2851 static struct bfd_hash_entry *
2852 stub_hash_newfunc (struct bfd_hash_entry *entry,
2853 struct bfd_hash_table *table,
2854 const char *string)
2855 {
2856 /* Allocate the structure if it has not already been allocated by a
2857 subclass. */
2858 if (entry == NULL)
2859 {
2860 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
2861 if (entry == NULL)
2862 return entry;
2863 }
2864
2865 /* Call the allocation method of the superclass. */
2866 entry = bfd_hash_newfunc (entry, table, string);
2867 if (entry != NULL)
2868 {
2869 struct ppc_stub_hash_entry *eh;
2870
2871 /* Initialize the local fields. */
2872 eh = (struct ppc_stub_hash_entry *) entry;
2873 eh->stub_type = ppc_stub_none;
2874 eh->stub_sec = NULL;
2875 eh->stub_offset = 0;
2876 eh->target_value = 0;
2877 eh->target_section = NULL;
2878 eh->h = NULL;
2879 eh->id_sec = NULL;
2880 }
2881
2882 return entry;
2883 }
2884
2885 /* Create an entry in the branch hash table. */
2886
2887 static struct bfd_hash_entry *
2888 branch_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_branch_hash_entry));
2897 if (entry == NULL)
2898 return entry;
2899 }
2900
2901 /* Call the allocation method of the superclass. */
2902 entry = bfd_hash_newfunc (entry, table, string);
2903 if (entry != NULL)
2904 {
2905 struct ppc_branch_hash_entry *eh;
2906
2907 /* Initialize the local fields. */
2908 eh = (struct ppc_branch_hash_entry *) entry;
2909 eh->offset = 0;
2910 eh->iter = 0;
2911 }
2912
2913 return entry;
2914 }
2915
2916 /* Create an entry in a ppc64 ELF linker hash table. */
2917
2918 static struct bfd_hash_entry *
2919 link_hash_newfunc (struct bfd_hash_entry *entry,
2920 struct bfd_hash_table *table,
2921 const char *string)
2922 {
2923 /* Allocate the structure if it has not already been allocated by a
2924 subclass. */
2925 if (entry == NULL)
2926 {
2927 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
2928 if (entry == NULL)
2929 return entry;
2930 }
2931
2932 /* Call the allocation method of the superclass. */
2933 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
2934 if (entry != NULL)
2935 {
2936 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
2937
2938 eh->stub_cache = NULL;
2939 eh->dyn_relocs = NULL;
2940 eh->oh = NULL;
2941 eh->is_func = 0;
2942 eh->is_func_descriptor = 0;
2943 eh->is_entry = 0;
2944 eh->tls_mask = 0;
2945 }
2946
2947 return entry;
2948 }
2949
2950 /* Create a ppc64 ELF linker hash table. */
2951
2952 static struct bfd_link_hash_table *
2953 ppc64_elf_link_hash_table_create (bfd *abfd)
2954 {
2955 struct ppc_link_hash_table *htab;
2956 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
2957
2958 htab = bfd_zmalloc (amt);
2959 if (htab == NULL)
2960 return NULL;
2961
2962 if (! _bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc))
2963 {
2964 free (htab);
2965 return NULL;
2966 }
2967
2968 /* Init the stub hash table too. */
2969 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc))
2970 return NULL;
2971
2972 /* And the branch hash table. */
2973 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc))
2974 return NULL;
2975
2976 /* Initializing two fields of the union is just cosmetic. We really
2977 only care about glist, but when compiled on a 32-bit host the
2978 bfd_vma fields are larger. Setting the bfd_vma to zero makes
2979 debugger inspection of these fields look nicer. */
2980 htab->elf.init_refcount.refcount = 0;
2981 htab->elf.init_refcount.glist = NULL;
2982 htab->elf.init_offset.offset = 0;
2983 htab->elf.init_offset.glist = NULL;
2984
2985 return &htab->elf.root;
2986 }
2987
2988 /* Free the derived linker hash table. */
2989
2990 static void
2991 ppc64_elf_link_hash_table_free (struct bfd_link_hash_table *hash)
2992 {
2993 struct ppc_link_hash_table *ret = (struct ppc_link_hash_table *) hash;
2994
2995 bfd_hash_table_free (&ret->stub_hash_table);
2996 bfd_hash_table_free (&ret->branch_hash_table);
2997 _bfd_generic_link_hash_table_free (hash);
2998 }
2999
3000 /* Satisfy the ELF linker by filling in some fields in our fake bfd. */
3001
3002 void
3003 ppc64_elf_init_stub_bfd (bfd *abfd, struct bfd_link_info *info)
3004 {
3005 struct ppc_link_hash_table *htab;
3006
3007 elf_elfheader (abfd)->e_ident[EI_CLASS] = ELFCLASS64;
3008
3009 /* Always hook our dynamic sections into the first bfd, which is the
3010 linker created stub bfd. This ensures that the GOT header is at
3011 the start of the output TOC section. */
3012 htab = ppc_hash_table (info);
3013 htab->stub_bfd = abfd;
3014 htab->elf.dynobj = abfd;
3015 }
3016
3017 /* Build a name for an entry in the stub hash table. */
3018
3019 static char *
3020 ppc_stub_name (const asection *input_section,
3021 const asection *sym_sec,
3022 const struct ppc_link_hash_entry *h,
3023 const Elf_Internal_Rela *rel)
3024 {
3025 char *stub_name;
3026 bfd_size_type len;
3027
3028 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
3029 offsets from a sym as a branch target? In fact, we could
3030 probably assume the addend is always zero. */
3031 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
3032
3033 if (h)
3034 {
3035 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
3036 stub_name = bfd_malloc (len);
3037 if (stub_name != NULL)
3038 {
3039 sprintf (stub_name, "%08x_%s+%x",
3040 input_section->id & 0xffffffff,
3041 h->elf.root.root.string,
3042 (int) rel->r_addend & 0xffffffff);
3043 }
3044 }
3045 else
3046 {
3047 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
3048 stub_name = bfd_malloc (len);
3049 if (stub_name != NULL)
3050 {
3051 sprintf (stub_name, "%08x_%x:%x+%x",
3052 input_section->id & 0xffffffff,
3053 sym_sec->id & 0xffffffff,
3054 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
3055 (int) rel->r_addend & 0xffffffff);
3056 }
3057 }
3058 return stub_name;
3059 }
3060
3061 /* Look up an entry in the stub hash. Stub entries are cached because
3062 creating the stub name takes a bit of time. */
3063
3064 static struct ppc_stub_hash_entry *
3065 ppc_get_stub_entry (const asection *input_section,
3066 const asection *sym_sec,
3067 struct elf_link_hash_entry *hash,
3068 const Elf_Internal_Rela *rel,
3069 struct ppc_link_hash_table *htab)
3070 {
3071 struct ppc_stub_hash_entry *stub_entry;
3072 struct ppc_link_hash_entry *h = (struct ppc_link_hash_entry *) hash;
3073 const asection *id_sec;
3074
3075 /* If this input section is part of a group of sections sharing one
3076 stub section, then use the id of the first section in the group.
3077 Stub names need to include a section id, as there may well be
3078 more than one stub used to reach say, printf, and we need to
3079 distinguish between them. */
3080 id_sec = htab->stub_group[input_section->id].link_sec;
3081
3082 if (h != NULL && h->stub_cache != NULL
3083 && h->stub_cache->h == h
3084 && h->stub_cache->id_sec == id_sec)
3085 {
3086 stub_entry = h->stub_cache;
3087 }
3088 else
3089 {
3090 char *stub_name;
3091
3092 stub_name = ppc_stub_name (id_sec, sym_sec, h, rel);
3093 if (stub_name == NULL)
3094 return NULL;
3095
3096 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
3097 stub_name, FALSE, FALSE);
3098 if (h != NULL)
3099 h->stub_cache = stub_entry;
3100
3101 free (stub_name);
3102 }
3103
3104 return stub_entry;
3105 }
3106
3107 /* Add a new stub entry to the stub hash. Not all fields of the new
3108 stub entry are initialised. */
3109
3110 static struct ppc_stub_hash_entry *
3111 ppc_add_stub (const char *stub_name,
3112 asection *section,
3113 struct ppc_link_hash_table *htab)
3114 {
3115 asection *link_sec;
3116 asection *stub_sec;
3117 struct ppc_stub_hash_entry *stub_entry;
3118
3119 link_sec = htab->stub_group[section->id].link_sec;
3120 stub_sec = htab->stub_group[section->id].stub_sec;
3121 if (stub_sec == NULL)
3122 {
3123 stub_sec = htab->stub_group[link_sec->id].stub_sec;
3124 if (stub_sec == NULL)
3125 {
3126 size_t namelen;
3127 bfd_size_type len;
3128 char *s_name;
3129
3130 namelen = strlen (link_sec->name);
3131 len = namelen + sizeof (STUB_SUFFIX);
3132 s_name = bfd_alloc (htab->stub_bfd, len);
3133 if (s_name == NULL)
3134 return NULL;
3135
3136 memcpy (s_name, link_sec->name, namelen);
3137 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
3138 stub_sec = (*htab->add_stub_section) (s_name, link_sec);
3139 if (stub_sec == NULL)
3140 return NULL;
3141 htab->stub_group[link_sec->id].stub_sec = stub_sec;
3142 }
3143 htab->stub_group[section->id].stub_sec = stub_sec;
3144 }
3145
3146 /* Enter this entry into the linker stub hash table. */
3147 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
3148 TRUE, FALSE);
3149 if (stub_entry == NULL)
3150 {
3151 (*_bfd_error_handler) (_("%s: cannot create stub entry %s"),
3152 bfd_archive_filename (section->owner),
3153 stub_name);
3154 return NULL;
3155 }
3156
3157 stub_entry->stub_sec = stub_sec;
3158 stub_entry->stub_offset = 0;
3159 stub_entry->id_sec = link_sec;
3160 return stub_entry;
3161 }
3162
3163 /* Create sections for linker generated code. */
3164
3165 static bfd_boolean
3166 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
3167 {
3168 struct ppc_link_hash_table *htab;
3169 flagword flags;
3170
3171 htab = ppc_hash_table (info);
3172
3173 /* Create .sfpr for code to save and restore fp regs. */
3174 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
3175 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3176 htab->sfpr = bfd_make_section_anyway (dynobj, ".sfpr");
3177 if (htab->sfpr == NULL
3178 || ! bfd_set_section_flags (dynobj, htab->sfpr, flags)
3179 || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
3180 return FALSE;
3181
3182 /* Create .glink for lazy dynamic linking support. */
3183 htab->glink = bfd_make_section_anyway (dynobj, ".glink");
3184 if (htab->glink == NULL
3185 || ! bfd_set_section_flags (dynobj, htab->glink, flags)
3186 || ! bfd_set_section_alignment (dynobj, htab->glink, 2))
3187 return FALSE;
3188
3189 /* Create .branch_lt for plt_branch stubs. */
3190 flags = (SEC_ALLOC | SEC_LOAD
3191 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3192 htab->brlt = bfd_make_section_anyway (dynobj, ".branch_lt");
3193 if (htab->brlt == NULL
3194 || ! bfd_set_section_flags (dynobj, htab->brlt, flags)
3195 || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
3196 return FALSE;
3197
3198 if (info->shared)
3199 {
3200 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3201 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3202 htab->relbrlt = bfd_make_section_anyway (dynobj, ".rela.branch_lt");
3203 if (!htab->relbrlt
3204 || ! bfd_set_section_flags (dynobj, htab->relbrlt, flags)
3205 || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
3206 return FALSE;
3207 }
3208 return TRUE;
3209 }
3210
3211 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
3212 not already done. */
3213
3214 static bfd_boolean
3215 create_got_section (bfd *abfd, struct bfd_link_info *info)
3216 {
3217 asection *got, *relgot;
3218 flagword flags;
3219 struct ppc_link_hash_table *htab = ppc_hash_table (info);
3220
3221 if (!htab->got)
3222 {
3223 if (! _bfd_elf_create_got_section (htab->elf.dynobj, info))
3224 return FALSE;
3225
3226 htab->got = bfd_get_section_by_name (htab->elf.dynobj, ".got");
3227 if (!htab->got)
3228 abort ();
3229 }
3230
3231 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3232 | SEC_LINKER_CREATED);
3233
3234 got = bfd_make_section (abfd, ".got");
3235 if (!got
3236 || !bfd_set_section_flags (abfd, got, flags)
3237 || !bfd_set_section_alignment (abfd, got, 3))
3238 return FALSE;
3239
3240 relgot = bfd_make_section (abfd, ".rela.got");
3241 if (!relgot
3242 || ! bfd_set_section_flags (abfd, relgot, flags | SEC_READONLY)
3243 || ! bfd_set_section_alignment (abfd, relgot, 3))
3244 return FALSE;
3245
3246 ppc64_elf_tdata (abfd)->got = got;
3247 ppc64_elf_tdata (abfd)->relgot = relgot;
3248 return TRUE;
3249 }
3250
3251 /* Create the dynamic sections, and set up shortcuts. */
3252
3253 static bfd_boolean
3254 ppc64_elf_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
3255 {
3256 struct ppc_link_hash_table *htab;
3257
3258 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
3259 return FALSE;
3260
3261 htab = ppc_hash_table (info);
3262 if (!htab->got)
3263 htab->got = bfd_get_section_by_name (dynobj, ".got");
3264 htab->plt = bfd_get_section_by_name (dynobj, ".plt");
3265 htab->relplt = bfd_get_section_by_name (dynobj, ".rela.plt");
3266 htab->dynbss = bfd_get_section_by_name (dynobj, ".dynbss");
3267 if (!info->shared)
3268 htab->relbss = bfd_get_section_by_name (dynobj, ".rela.bss");
3269
3270 if (!htab->got || !htab->plt || !htab->relplt || !htab->dynbss
3271 || (!info->shared && !htab->relbss))
3272 abort ();
3273
3274 return TRUE;
3275 }
3276
3277 /* Copy the extra info we tack onto an elf_link_hash_entry. */
3278
3279 static void
3280 ppc64_elf_copy_indirect_symbol (struct elf_backend_data *bed ATTRIBUTE_UNUSED,
3281 struct elf_link_hash_entry *dir,
3282 struct elf_link_hash_entry *ind)
3283 {
3284 struct ppc_link_hash_entry *edir, *eind;
3285 flagword mask;
3286
3287 edir = (struct ppc_link_hash_entry *) dir;
3288 eind = (struct ppc_link_hash_entry *) ind;
3289
3290 /* Copy over any dynamic relocs we may have on the indirect sym. */
3291 if (eind->dyn_relocs != NULL)
3292 {
3293 if (edir->dyn_relocs != NULL)
3294 {
3295 struct ppc_dyn_relocs **pp;
3296 struct ppc_dyn_relocs *p;
3297
3298 if (eind->elf.root.type == bfd_link_hash_indirect)
3299 abort ();
3300
3301 /* Add reloc counts against the weak sym to the strong sym
3302 list. Merge any entries against the same section. */
3303 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
3304 {
3305 struct ppc_dyn_relocs *q;
3306
3307 for (q = edir->dyn_relocs; q != NULL; q = q->next)
3308 if (q->sec == p->sec)
3309 {
3310 q->pc_count += p->pc_count;
3311 q->count += p->count;
3312 *pp = p->next;
3313 break;
3314 }
3315 if (q == NULL)
3316 pp = &p->next;
3317 }
3318 *pp = edir->dyn_relocs;
3319 }
3320
3321 edir->dyn_relocs = eind->dyn_relocs;
3322 eind->dyn_relocs = NULL;
3323 }
3324
3325 edir->is_func |= eind->is_func;
3326 edir->is_func_descriptor |= eind->is_func_descriptor;
3327 edir->is_entry |= eind->is_entry;
3328 edir->tls_mask |= eind->tls_mask;
3329
3330 mask = (ELF_LINK_HASH_REF_DYNAMIC | ELF_LINK_HASH_REF_REGULAR
3331 | ELF_LINK_HASH_REF_REGULAR_NONWEAK | ELF_LINK_NON_GOT_REF);
3332 /* If called to transfer flags for a weakdef during processing
3333 of elf_adjust_dynamic_symbol, don't copy ELF_LINK_NON_GOT_REF.
3334 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
3335 if (ELIMINATE_COPY_RELOCS
3336 && eind->elf.root.type != bfd_link_hash_indirect
3337 && (edir->elf.elf_link_hash_flags & ELF_LINK_HASH_DYNAMIC_ADJUSTED) != 0)
3338 mask &= ~ELF_LINK_NON_GOT_REF;
3339
3340 edir->elf.elf_link_hash_flags |= eind->elf.elf_link_hash_flags & mask;
3341
3342 /* If we were called to copy over info for a weak sym, that's all. */
3343 if (eind->elf.root.type != bfd_link_hash_indirect)
3344 return;
3345
3346 /* Copy over got entries that we may have already seen to the
3347 symbol which just became indirect. */
3348 if (eind->elf.got.glist != NULL)
3349 {
3350 if (edir->elf.got.glist != NULL)
3351 {
3352 struct got_entry **entp;
3353 struct got_entry *ent;
3354
3355 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
3356 {
3357 struct got_entry *dent;
3358
3359 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
3360 if (dent->addend == ent->addend
3361 && dent->owner == ent->owner
3362 && dent->tls_type == ent->tls_type)
3363 {
3364 dent->got.refcount += ent->got.refcount;
3365 *entp = ent->next;
3366 break;
3367 }
3368 if (dent == NULL)
3369 entp = &ent->next;
3370 }
3371 *entp = edir->elf.got.glist;
3372 }
3373
3374 edir->elf.got.glist = eind->elf.got.glist;
3375 eind->elf.got.glist = NULL;
3376 }
3377
3378 /* And plt entries. */
3379 if (eind->elf.plt.plist != NULL)
3380 {
3381 if (edir->elf.plt.plist != NULL)
3382 {
3383 struct plt_entry **entp;
3384 struct plt_entry *ent;
3385
3386 for (entp = &eind->elf.plt.plist; (ent = *entp) != NULL; )
3387 {
3388 struct plt_entry *dent;
3389
3390 for (dent = edir->elf.plt.plist; dent != NULL; dent = dent->next)
3391 if (dent->addend == ent->addend)
3392 {
3393 dent->plt.refcount += ent->plt.refcount;
3394 *entp = ent->next;
3395 break;
3396 }
3397 if (dent == NULL)
3398 entp = &ent->next;
3399 }
3400 *entp = edir->elf.plt.plist;
3401 }
3402
3403 edir->elf.plt.plist = eind->elf.plt.plist;
3404 eind->elf.plt.plist = NULL;
3405 }
3406
3407 if (edir->elf.dynindx == -1)
3408 {
3409 edir->elf.dynindx = eind->elf.dynindx;
3410 edir->elf.dynstr_index = eind->elf.dynstr_index;
3411 eind->elf.dynindx = -1;
3412 eind->elf.dynstr_index = 0;
3413 }
3414 else
3415 BFD_ASSERT (eind->elf.dynindx == -1);
3416 }
3417
3418 /* Set a flag, used by ppc64_elf_gc_mark_hook, on the entry symbol and
3419 symbols undefined on the command-line. */
3420
3421 bfd_boolean
3422 ppc64_elf_mark_entry_syms (struct bfd_link_info *info)
3423 {
3424 struct ppc_link_hash_table *htab;
3425 struct bfd_sym_chain *sym;
3426
3427 htab = ppc_hash_table (info);
3428 for (sym = info->gc_sym_list; sym; sym = sym->next)
3429 {
3430 struct elf_link_hash_entry *h;
3431
3432 h = elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, FALSE);
3433 if (h != NULL)
3434 ((struct ppc_link_hash_entry *) h)->is_entry = 1;
3435 }
3436 return TRUE;
3437 }
3438
3439 static bfd_boolean
3440 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
3441 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
3442 {
3443 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
3444 char *local_got_tls_masks;
3445
3446 if (local_got_ents == NULL)
3447 {
3448 bfd_size_type size = symtab_hdr->sh_info;
3449
3450 size *= sizeof (*local_got_ents) + sizeof (*local_got_tls_masks);
3451 local_got_ents = bfd_zalloc (abfd, size);
3452 if (local_got_ents == NULL)
3453 return FALSE;
3454 elf_local_got_ents (abfd) = local_got_ents;
3455 }
3456
3457 if ((tls_type & TLS_EXPLICIT) == 0)
3458 {
3459 struct got_entry *ent;
3460
3461 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
3462 if (ent->addend == r_addend
3463 && ent->owner == abfd
3464 && ent->tls_type == tls_type)
3465 break;
3466 if (ent == NULL)
3467 {
3468 bfd_size_type amt = sizeof (*ent);
3469 ent = bfd_alloc (abfd, amt);
3470 if (ent == NULL)
3471 return FALSE;
3472 ent->next = local_got_ents[r_symndx];
3473 ent->addend = r_addend;
3474 ent->owner = abfd;
3475 ent->tls_type = tls_type;
3476 ent->got.refcount = 0;
3477 local_got_ents[r_symndx] = ent;
3478 }
3479 ent->got.refcount += 1;
3480 }
3481
3482 local_got_tls_masks = (char *) (local_got_ents + symtab_hdr->sh_info);
3483 local_got_tls_masks[r_symndx] |= tls_type;
3484 return TRUE;
3485 }
3486
3487 static bfd_boolean
3488 update_plt_info (bfd *abfd, struct ppc_link_hash_entry *eh, bfd_vma addend)
3489 {
3490 struct plt_entry *ent;
3491
3492 for (ent = eh->elf.plt.plist; ent != NULL; ent = ent->next)
3493 if (ent->addend == addend)
3494 break;
3495 if (ent == NULL)
3496 {
3497 bfd_size_type amt = sizeof (*ent);
3498 ent = bfd_alloc (abfd, amt);
3499 if (ent == NULL)
3500 return FALSE;
3501 ent->next = eh->elf.plt.plist;
3502 ent->addend = addend;
3503 ent->plt.refcount = 0;
3504 eh->elf.plt.plist = ent;
3505 }
3506 ent->plt.refcount += 1;
3507 eh->elf.elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
3508 eh->is_func = 1;
3509 return TRUE;
3510 }
3511
3512 /* Look through the relocs for a section during the first phase, and
3513 calculate needed space in the global offset table, procedure
3514 linkage table, and dynamic reloc sections. */
3515
3516 static bfd_boolean
3517 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
3518 asection *sec, const Elf_Internal_Rela *relocs)
3519 {
3520 struct ppc_link_hash_table *htab;
3521 Elf_Internal_Shdr *symtab_hdr;
3522 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
3523 const Elf_Internal_Rela *rel;
3524 const Elf_Internal_Rela *rel_end;
3525 asection *sreloc;
3526 asection **opd_sym_map;
3527
3528 if (info->relocatable)
3529 return TRUE;
3530
3531 htab = ppc_hash_table (info);
3532 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3533
3534 sym_hashes = elf_sym_hashes (abfd);
3535 sym_hashes_end = (sym_hashes
3536 + symtab_hdr->sh_size / sizeof (Elf64_External_Sym)
3537 - symtab_hdr->sh_info);
3538
3539 sreloc = NULL;
3540 opd_sym_map = NULL;
3541 if (strcmp (bfd_get_section_name (abfd, sec), ".opd") == 0)
3542 {
3543 /* Garbage collection needs some extra help with .opd sections.
3544 We don't want to necessarily keep everything referenced by
3545 relocs in .opd, as that would keep all functions. Instead,
3546 if we reference an .opd symbol (a function descriptor), we
3547 want to keep the function code symbol's section. This is
3548 easy for global symbols, but for local syms we need to keep
3549 information about the associated function section. Later, if
3550 edit_opd deletes entries, we'll use this array to adjust
3551 local syms in .opd. */
3552 union opd_info {
3553 asection *func_section;
3554 long entry_adjust;
3555 };
3556 bfd_size_type amt;
3557
3558 amt = sec->_raw_size * sizeof (union opd_info) / 24;
3559 opd_sym_map = bfd_zalloc (abfd, amt);
3560 if (opd_sym_map == NULL)
3561 return FALSE;
3562 ppc64_elf_section_data (sec)->opd.func_sec = opd_sym_map;
3563 }
3564
3565 if (htab->sfpr == NULL
3566 && !create_linkage_sections (htab->elf.dynobj, info))
3567 return FALSE;
3568
3569 rel_end = relocs + sec->reloc_count;
3570 for (rel = relocs; rel < rel_end; rel++)
3571 {
3572 unsigned long r_symndx;
3573 struct elf_link_hash_entry *h;
3574 enum elf_ppc64_reloc_type r_type;
3575 int tls_type = 0;
3576
3577 r_symndx = ELF64_R_SYM (rel->r_info);
3578 if (r_symndx < symtab_hdr->sh_info)
3579 h = NULL;
3580 else
3581 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3582
3583 r_type = ELF64_R_TYPE (rel->r_info);
3584 switch (r_type)
3585 {
3586 case R_PPC64_GOT_TLSLD16:
3587 case R_PPC64_GOT_TLSLD16_LO:
3588 case R_PPC64_GOT_TLSLD16_HI:
3589 case R_PPC64_GOT_TLSLD16_HA:
3590 ppc64_tlsld_got (abfd)->refcount += 1;
3591 tls_type = TLS_TLS | TLS_LD;
3592 goto dogottls;
3593
3594 case R_PPC64_GOT_TLSGD16:
3595 case R_PPC64_GOT_TLSGD16_LO:
3596 case R_PPC64_GOT_TLSGD16_HI:
3597 case R_PPC64_GOT_TLSGD16_HA:
3598 tls_type = TLS_TLS | TLS_GD;
3599 goto dogottls;
3600
3601 case R_PPC64_GOT_TPREL16_DS:
3602 case R_PPC64_GOT_TPREL16_LO_DS:
3603 case R_PPC64_GOT_TPREL16_HI:
3604 case R_PPC64_GOT_TPREL16_HA:
3605 if (info->shared)
3606 info->flags |= DF_STATIC_TLS;
3607 tls_type = TLS_TLS | TLS_TPREL;
3608 goto dogottls;
3609
3610 case R_PPC64_GOT_DTPREL16_DS:
3611 case R_PPC64_GOT_DTPREL16_LO_DS:
3612 case R_PPC64_GOT_DTPREL16_HI:
3613 case R_PPC64_GOT_DTPREL16_HA:
3614 tls_type = TLS_TLS | TLS_DTPREL;
3615 dogottls:
3616 sec->has_tls_reloc = 1;
3617 /* Fall thru */
3618
3619 case R_PPC64_GOT16:
3620 case R_PPC64_GOT16_DS:
3621 case R_PPC64_GOT16_HA:
3622 case R_PPC64_GOT16_HI:
3623 case R_PPC64_GOT16_LO:
3624 case R_PPC64_GOT16_LO_DS:
3625 /* This symbol requires a global offset table entry. */
3626 sec->has_gp_reloc = 1;
3627 if (ppc64_elf_tdata (abfd)->got == NULL
3628 && !create_got_section (abfd, info))
3629 return FALSE;
3630
3631 if (h != NULL)
3632 {
3633 struct ppc_link_hash_entry *eh;
3634 struct got_entry *ent;
3635
3636 eh = (struct ppc_link_hash_entry *) h;
3637 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
3638 if (ent->addend == rel->r_addend
3639 && ent->owner == abfd
3640 && ent->tls_type == tls_type)
3641 break;
3642 if (ent == NULL)
3643 {
3644 bfd_size_type amt = sizeof (*ent);
3645 ent = bfd_alloc (abfd, amt);
3646 if (ent == NULL)
3647 return FALSE;
3648 ent->next = eh->elf.got.glist;
3649 ent->addend = rel->r_addend;
3650 ent->owner = abfd;
3651 ent->tls_type = tls_type;
3652 ent->got.refcount = 0;
3653 eh->elf.got.glist = ent;
3654 }
3655 ent->got.refcount += 1;
3656 eh->tls_mask |= tls_type;
3657 }
3658 else
3659 /* This is a global offset table entry for a local symbol. */
3660 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
3661 rel->r_addend, tls_type))
3662 return FALSE;
3663 break;
3664
3665 case R_PPC64_PLT16_HA:
3666 case R_PPC64_PLT16_HI:
3667 case R_PPC64_PLT16_LO:
3668 case R_PPC64_PLT32:
3669 case R_PPC64_PLT64:
3670 /* This symbol requires a procedure linkage table entry. We
3671 actually build the entry in adjust_dynamic_symbol,
3672 because this might be a case of linking PIC code without
3673 linking in any dynamic objects, in which case we don't
3674 need to generate a procedure linkage table after all. */
3675 if (h == NULL)
3676 {
3677 /* It does not make sense to have a procedure linkage
3678 table entry for a local symbol. */
3679 bfd_set_error (bfd_error_bad_value);
3680 return FALSE;
3681 }
3682 else
3683 if (!update_plt_info (abfd, (struct ppc_link_hash_entry *) h,
3684 rel->r_addend))
3685 return FALSE;
3686 break;
3687
3688 /* The following relocations don't need to propagate the
3689 relocation if linking a shared object since they are
3690 section relative. */
3691 case R_PPC64_SECTOFF:
3692 case R_PPC64_SECTOFF_LO:
3693 case R_PPC64_SECTOFF_HI:
3694 case R_PPC64_SECTOFF_HA:
3695 case R_PPC64_SECTOFF_DS:
3696 case R_PPC64_SECTOFF_LO_DS:
3697 case R_PPC64_DTPREL16:
3698 case R_PPC64_DTPREL16_LO:
3699 case R_PPC64_DTPREL16_HI:
3700 case R_PPC64_DTPREL16_HA:
3701 case R_PPC64_DTPREL16_DS:
3702 case R_PPC64_DTPREL16_LO_DS:
3703 case R_PPC64_DTPREL16_HIGHER:
3704 case R_PPC64_DTPREL16_HIGHERA:
3705 case R_PPC64_DTPREL16_HIGHEST:
3706 case R_PPC64_DTPREL16_HIGHESTA:
3707 break;
3708
3709 /* Nor do these. */
3710 case R_PPC64_TOC16:
3711 case R_PPC64_TOC16_LO:
3712 case R_PPC64_TOC16_HI:
3713 case R_PPC64_TOC16_HA:
3714 case R_PPC64_TOC16_DS:
3715 case R_PPC64_TOC16_LO_DS:
3716 sec->has_gp_reloc = 1;
3717 break;
3718
3719 /* This relocation describes the C++ object vtable hierarchy.
3720 Reconstruct it for later use during GC. */
3721 case R_PPC64_GNU_VTINHERIT:
3722 if (!_bfd_elf64_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
3723 return FALSE;
3724 break;
3725
3726 /* This relocation describes which C++ vtable entries are actually
3727 used. Record for later use during GC. */
3728 case R_PPC64_GNU_VTENTRY:
3729 if (!_bfd_elf64_gc_record_vtentry (abfd, sec, h, rel->r_addend))
3730 return FALSE;
3731 break;
3732
3733 case R_PPC64_REL14:
3734 case R_PPC64_REL14_BRTAKEN:
3735 case R_PPC64_REL14_BRNTAKEN:
3736 htab->has_14bit_branch = 1;
3737 /* Fall through. */
3738
3739 case R_PPC64_REL24:
3740 if (h != NULL
3741 && h->root.root.string[0] == '.'
3742 && h->root.root.string[1] != 0)
3743 {
3744 /* We may need a .plt entry if the function this reloc
3745 refers to is in a shared lib. */
3746 if (!update_plt_info (abfd, (struct ppc_link_hash_entry *) h,
3747 rel->r_addend))
3748 return FALSE;
3749 if (h == htab->tls_get_addr)
3750 sec->has_tls_reloc = 1;
3751 else if ((strncmp (h->root.root.string, ".__tls_get_addr", 15)
3752 == 0)
3753 && (h->root.root.string[15] == 0
3754 || h->root.root.string[15] == '@'))
3755 {
3756 htab->tls_get_addr = h;
3757 sec->has_tls_reloc = 1;
3758 }
3759 }
3760 break;
3761
3762 case R_PPC64_TPREL64:
3763 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
3764 if (info->shared)
3765 info->flags |= DF_STATIC_TLS;
3766 goto dotlstoc;
3767
3768 case R_PPC64_DTPMOD64:
3769 if (rel + 1 < rel_end
3770 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
3771 && rel[1].r_offset == rel->r_offset + 8)
3772 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
3773 else
3774 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
3775 goto dotlstoc;
3776
3777 case R_PPC64_DTPREL64:
3778 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
3779 if (rel != relocs
3780 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
3781 && rel[-1].r_offset == rel->r_offset - 8)
3782 /* This is the second reloc of a dtpmod, dtprel pair.
3783 Don't mark with TLS_DTPREL. */
3784 goto dodyn;
3785
3786 dotlstoc:
3787 sec->has_tls_reloc = 1;
3788 if (h != NULL)
3789 {
3790 struct ppc_link_hash_entry *eh;
3791 eh = (struct ppc_link_hash_entry *) h;
3792 eh->tls_mask |= tls_type;
3793 }
3794 else
3795 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
3796 rel->r_addend, tls_type))
3797 return FALSE;
3798
3799 if (ppc64_elf_section_data (sec)->t_symndx == NULL)
3800 {
3801 /* One extra to simplify get_tls_mask. */
3802 bfd_size_type amt = sec->_raw_size * sizeof (unsigned) / 8 + 1;
3803 ppc64_elf_section_data (sec)->t_symndx = bfd_zalloc (abfd, amt);
3804 if (ppc64_elf_section_data (sec)->t_symndx == NULL)
3805 return FALSE;
3806 }
3807 BFD_ASSERT (rel->r_offset % 8 == 0);
3808 ppc64_elf_section_data (sec)->t_symndx[rel->r_offset / 8] = r_symndx;
3809
3810 /* Mark the second slot of a GD or LD entry.
3811 -1 to indicate GD and -2 to indicate LD. */
3812 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
3813 ppc64_elf_section_data (sec)->t_symndx[rel->r_offset / 8 + 1] = -1;
3814 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
3815 ppc64_elf_section_data (sec)->t_symndx[rel->r_offset / 8 + 1] = -2;
3816 goto dodyn;
3817
3818 case R_PPC64_TPREL16:
3819 case R_PPC64_TPREL16_LO:
3820 case R_PPC64_TPREL16_HI:
3821 case R_PPC64_TPREL16_HA:
3822 case R_PPC64_TPREL16_DS:
3823 case R_PPC64_TPREL16_LO_DS:
3824 case R_PPC64_TPREL16_HIGHER:
3825 case R_PPC64_TPREL16_HIGHERA:
3826 case R_PPC64_TPREL16_HIGHEST:
3827 case R_PPC64_TPREL16_HIGHESTA:
3828 if (info->shared)
3829 {
3830 info->flags |= DF_STATIC_TLS;
3831 goto dodyn;
3832 }
3833 break;
3834
3835 case R_PPC64_ADDR64:
3836 if (opd_sym_map != NULL
3837 && h != NULL
3838 && h->root.root.string[0] == '.'
3839 && h->root.root.string[1] != 0)
3840 {
3841 struct elf_link_hash_entry *fdh;
3842
3843 fdh = elf_link_hash_lookup (&htab->elf, h->root.root.string + 1,
3844 FALSE, FALSE, FALSE);
3845 if (fdh != NULL)
3846 {
3847 ((struct ppc_link_hash_entry *) fdh)->is_func_descriptor = 1;
3848 ((struct ppc_link_hash_entry *) fdh)->oh = h;
3849 ((struct ppc_link_hash_entry *) h)->is_func = 1;
3850 ((struct ppc_link_hash_entry *) h)->oh = fdh;
3851 }
3852 }
3853 if (opd_sym_map != NULL
3854 && h == NULL
3855 && rel + 1 < rel_end
3856 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
3857 {
3858 asection *s;
3859
3860 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec, sec,
3861 r_symndx);
3862 if (s == NULL)
3863 return FALSE;
3864 else if (s != sec)
3865 opd_sym_map[rel->r_offset / 24] = s;
3866 }
3867 /* Fall through. */
3868
3869 case R_PPC64_REL30:
3870 case R_PPC64_REL32:
3871 case R_PPC64_REL64:
3872 case R_PPC64_ADDR14:
3873 case R_PPC64_ADDR14_BRNTAKEN:
3874 case R_PPC64_ADDR14_BRTAKEN:
3875 case R_PPC64_ADDR16:
3876 case R_PPC64_ADDR16_DS:
3877 case R_PPC64_ADDR16_HA:
3878 case R_PPC64_ADDR16_HI:
3879 case R_PPC64_ADDR16_HIGHER:
3880 case R_PPC64_ADDR16_HIGHERA:
3881 case R_PPC64_ADDR16_HIGHEST:
3882 case R_PPC64_ADDR16_HIGHESTA:
3883 case R_PPC64_ADDR16_LO:
3884 case R_PPC64_ADDR16_LO_DS:
3885 case R_PPC64_ADDR24:
3886 case R_PPC64_ADDR32:
3887 case R_PPC64_UADDR16:
3888 case R_PPC64_UADDR32:
3889 case R_PPC64_UADDR64:
3890 case R_PPC64_TOC:
3891 if (h != NULL && !info->shared)
3892 /* We may need a copy reloc. */
3893 h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
3894
3895 /* Don't propagate .opd relocs. */
3896 if (NO_OPD_RELOCS && opd_sym_map != NULL)
3897 break;
3898
3899 /* Don't propagate relocs that the dynamic linker won't relocate. */
3900 if ((sec->flags & SEC_ALLOC) == 0)
3901 break;
3902
3903 /* If we are creating a shared library, and this is a reloc
3904 against a global symbol, or a non PC relative reloc
3905 against a local symbol, then we need to copy the reloc
3906 into the shared library. However, if we are linking with
3907 -Bsymbolic, we do not need to copy a reloc against a
3908 global symbol which is defined in an object we are
3909 including in the link (i.e., DEF_REGULAR is set). At
3910 this point we have not seen all the input files, so it is
3911 possible that DEF_REGULAR is not set now but will be set
3912 later (it is never cleared). In case of a weak definition,
3913 DEF_REGULAR may be cleared later by a strong definition in
3914 a shared library. We account for that possibility below by
3915 storing information in the dyn_relocs field of the hash
3916 table entry. A similar situation occurs when creating
3917 shared libraries and symbol visibility changes render the
3918 symbol local.
3919
3920 If on the other hand, we are creating an executable, we
3921 may need to keep relocations for symbols satisfied by a
3922 dynamic library if we manage to avoid copy relocs for the
3923 symbol. */
3924 dodyn:
3925 if ((info->shared
3926 && (MUST_BE_DYN_RELOC (r_type)
3927 || (h != NULL
3928 && (! info->symbolic
3929 || h->root.type == bfd_link_hash_defweak
3930 || (h->elf_link_hash_flags
3931 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
3932 || (ELIMINATE_COPY_RELOCS
3933 && !info->shared
3934 && h != NULL
3935 && (h->root.type == bfd_link_hash_defweak
3936 || (h->elf_link_hash_flags
3937 & ELF_LINK_HASH_DEF_REGULAR) == 0)))
3938 {
3939 struct ppc_dyn_relocs *p;
3940 struct ppc_dyn_relocs **head;
3941
3942 /* We must copy these reloc types into the output file.
3943 Create a reloc section in dynobj and make room for
3944 this reloc. */
3945 if (sreloc == NULL)
3946 {
3947 const char *name;
3948 bfd *dynobj;
3949
3950 name = (bfd_elf_string_from_elf_section
3951 (abfd,
3952 elf_elfheader (abfd)->e_shstrndx,
3953 elf_section_data (sec)->rel_hdr.sh_name));
3954 if (name == NULL)
3955 return FALSE;
3956
3957 if (strncmp (name, ".rela", 5) != 0
3958 || strcmp (bfd_get_section_name (abfd, sec),
3959 name + 5) != 0)
3960 {
3961 (*_bfd_error_handler)
3962 (_("%s: bad relocation section name `%s\'"),
3963 bfd_archive_filename (abfd), name);
3964 bfd_set_error (bfd_error_bad_value);
3965 }
3966
3967 dynobj = htab->elf.dynobj;
3968 sreloc = bfd_get_section_by_name (dynobj, name);
3969 if (sreloc == NULL)
3970 {
3971 flagword flags;
3972
3973 sreloc = bfd_make_section (dynobj, name);
3974 flags = (SEC_HAS_CONTENTS | SEC_READONLY
3975 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3976 if ((sec->flags & SEC_ALLOC) != 0)
3977 flags |= SEC_ALLOC | SEC_LOAD;
3978 if (sreloc == NULL
3979 || ! bfd_set_section_flags (dynobj, sreloc, flags)
3980 || ! bfd_set_section_alignment (dynobj, sreloc, 3))
3981 return FALSE;
3982 }
3983 elf_section_data (sec)->sreloc = sreloc;
3984 }
3985
3986 /* If this is a global symbol, we count the number of
3987 relocations we need for this symbol. */
3988 if (h != NULL)
3989 {
3990 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
3991 }
3992 else
3993 {
3994 /* Track dynamic relocs needed for local syms too.
3995 We really need local syms available to do this
3996 easily. Oh well. */
3997
3998 asection *s;
3999 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
4000 sec, r_symndx);
4001 if (s == NULL)
4002 return FALSE;
4003
4004 head = ((struct ppc_dyn_relocs **)
4005 &elf_section_data (s)->local_dynrel);
4006 }
4007
4008 p = *head;
4009 if (p == NULL || p->sec != sec)
4010 {
4011 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
4012 if (p == NULL)
4013 return FALSE;
4014 p->next = *head;
4015 *head = p;
4016 p->sec = sec;
4017 p->count = 0;
4018 p->pc_count = 0;
4019 }
4020
4021 p->count += 1;
4022 if (!MUST_BE_DYN_RELOC (r_type))
4023 p->pc_count += 1;
4024 }
4025 break;
4026
4027 default:
4028 break;
4029 }
4030 }
4031
4032 return TRUE;
4033 }
4034
4035 /* Return the section that should be marked against GC for a given
4036 relocation. */
4037
4038 static asection *
4039 ppc64_elf_gc_mark_hook (asection *sec,
4040 struct bfd_link_info *info ATTRIBUTE_UNUSED,
4041 Elf_Internal_Rela *rel,
4042 struct elf_link_hash_entry *h,
4043 Elf_Internal_Sym *sym)
4044 {
4045 asection *rsec = NULL;
4046
4047 if (h != NULL)
4048 {
4049 enum elf_ppc64_reloc_type r_type;
4050 struct ppc_link_hash_entry *fdh;
4051
4052 r_type = ELF64_R_TYPE (rel->r_info);
4053 switch (r_type)
4054 {
4055 case R_PPC64_GNU_VTINHERIT:
4056 case R_PPC64_GNU_VTENTRY:
4057 break;
4058
4059 default:
4060 switch (h->root.type)
4061 {
4062 case bfd_link_hash_defined:
4063 case bfd_link_hash_defweak:
4064 fdh = (struct ppc_link_hash_entry *) h;
4065
4066 /* Function descriptor syms cause the associated
4067 function code sym section to be marked. */
4068 if (fdh->is_func_descriptor)
4069 rsec = fdh->oh->root.u.def.section;
4070
4071 /* Function entry syms return NULL if they are in .opd
4072 and are not ._start (or others undefined on the ld
4073 command line). Thus we avoid marking all function
4074 sections, as all functions are referenced in .opd. */
4075 else if ((fdh->oh != NULL
4076 && ((struct ppc_link_hash_entry *) fdh->oh)->is_entry)
4077 || ppc64_elf_section_data (sec)->opd.func_sec == NULL)
4078 rsec = h->root.u.def.section;
4079 break;
4080
4081 case bfd_link_hash_common:
4082 rsec = h->root.u.c.p->section;
4083 break;
4084
4085 default:
4086 break;
4087 }
4088 }
4089 }
4090 else
4091 {
4092 asection **opd_sym_section;
4093
4094 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
4095 opd_sym_section = ppc64_elf_section_data (rsec)->opd.func_sec;
4096 if (opd_sym_section != NULL)
4097 rsec = opd_sym_section[sym->st_value / 24];
4098 else if (ppc64_elf_section_data (sec)->opd.func_sec != NULL)
4099 rsec = NULL;
4100 }
4101
4102 return rsec;
4103 }
4104
4105 /* Update the .got, .plt. and dynamic reloc reference counts for the
4106 section being removed. */
4107
4108 static bfd_boolean
4109 ppc64_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
4110 asection *sec, const Elf_Internal_Rela *relocs)
4111 {
4112 struct ppc_link_hash_table *htab;
4113 Elf_Internal_Shdr *symtab_hdr;
4114 struct elf_link_hash_entry **sym_hashes;
4115 struct got_entry **local_got_ents;
4116 const Elf_Internal_Rela *rel, *relend;
4117
4118 elf_section_data (sec)->local_dynrel = NULL;
4119
4120 htab = ppc_hash_table (info);
4121 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4122 sym_hashes = elf_sym_hashes (abfd);
4123 local_got_ents = elf_local_got_ents (abfd);
4124
4125 relend = relocs + sec->reloc_count;
4126 for (rel = relocs; rel < relend; rel++)
4127 {
4128 unsigned long r_symndx;
4129 enum elf_ppc64_reloc_type r_type;
4130 struct elf_link_hash_entry *h = NULL;
4131 char tls_type = 0;
4132
4133 r_symndx = ELF64_R_SYM (rel->r_info);
4134 r_type = ELF64_R_TYPE (rel->r_info);
4135 if (r_symndx >= symtab_hdr->sh_info)
4136 {
4137 struct ppc_link_hash_entry *eh;
4138 struct ppc_dyn_relocs **pp;
4139 struct ppc_dyn_relocs *p;
4140
4141 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4142 eh = (struct ppc_link_hash_entry *) h;
4143
4144 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
4145 if (p->sec == sec)
4146 {
4147 /* Everything must go for SEC. */
4148 *pp = p->next;
4149 break;
4150 }
4151 }
4152
4153 switch (r_type)
4154 {
4155 case R_PPC64_GOT_TLSLD16:
4156 case R_PPC64_GOT_TLSLD16_LO:
4157 case R_PPC64_GOT_TLSLD16_HI:
4158 case R_PPC64_GOT_TLSLD16_HA:
4159 ppc64_tlsld_got (abfd)->refcount -= 1;
4160 tls_type = TLS_TLS | TLS_LD;
4161 goto dogot;
4162
4163 case R_PPC64_GOT_TLSGD16:
4164 case R_PPC64_GOT_TLSGD16_LO:
4165 case R_PPC64_GOT_TLSGD16_HI:
4166 case R_PPC64_GOT_TLSGD16_HA:
4167 tls_type = TLS_TLS | TLS_GD;
4168 goto dogot;
4169
4170 case R_PPC64_GOT_TPREL16_DS:
4171 case R_PPC64_GOT_TPREL16_LO_DS:
4172 case R_PPC64_GOT_TPREL16_HI:
4173 case R_PPC64_GOT_TPREL16_HA:
4174 tls_type = TLS_TLS | TLS_TPREL;
4175 goto dogot;
4176
4177 case R_PPC64_GOT_DTPREL16_DS:
4178 case R_PPC64_GOT_DTPREL16_LO_DS:
4179 case R_PPC64_GOT_DTPREL16_HI:
4180 case R_PPC64_GOT_DTPREL16_HA:
4181 tls_type = TLS_TLS | TLS_DTPREL;
4182 goto dogot;
4183
4184 case R_PPC64_GOT16:
4185 case R_PPC64_GOT16_DS:
4186 case R_PPC64_GOT16_HA:
4187 case R_PPC64_GOT16_HI:
4188 case R_PPC64_GOT16_LO:
4189 case R_PPC64_GOT16_LO_DS:
4190 dogot:
4191 {
4192 struct got_entry *ent;
4193
4194 if (h != NULL)
4195 ent = h->got.glist;
4196 else
4197 ent = local_got_ents[r_symndx];
4198
4199 for (; ent != NULL; ent = ent->next)
4200 if (ent->addend == rel->r_addend
4201 && ent->owner == abfd
4202 && ent->tls_type == tls_type)
4203 break;
4204 if (ent == NULL)
4205 abort ();
4206 if (ent->got.refcount > 0)
4207 ent->got.refcount -= 1;
4208 }
4209 break;
4210
4211 case R_PPC64_PLT16_HA:
4212 case R_PPC64_PLT16_HI:
4213 case R_PPC64_PLT16_LO:
4214 case R_PPC64_PLT32:
4215 case R_PPC64_PLT64:
4216 case R_PPC64_REL14:
4217 case R_PPC64_REL14_BRNTAKEN:
4218 case R_PPC64_REL14_BRTAKEN:
4219 case R_PPC64_REL24:
4220 if (h != NULL)
4221 {
4222 struct plt_entry *ent;
4223
4224 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
4225 if (ent->addend == rel->r_addend)
4226 break;
4227 if (ent == NULL)
4228 abort ();
4229 if (ent->plt.refcount > 0)
4230 ent->plt.refcount -= 1;
4231 }
4232 break;
4233
4234 default:
4235 break;
4236 }
4237 }
4238 return TRUE;
4239 }
4240
4241 /* Called via elf_link_hash_traverse to transfer dynamic linking
4242 information on function code symbol entries to their corresponding
4243 function descriptor symbol entries. */
4244 static bfd_boolean
4245 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
4246 {
4247 struct bfd_link_info *info;
4248 struct ppc_link_hash_table *htab;
4249 struct plt_entry *ent;
4250
4251 if (h->root.type == bfd_link_hash_indirect)
4252 return TRUE;
4253
4254 if (h->root.type == bfd_link_hash_warning)
4255 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4256
4257 info = inf;
4258 htab = ppc_hash_table (info);
4259
4260 /* If this is a function code symbol, transfer dynamic linking
4261 information to the function descriptor symbol. */
4262 if (!((struct ppc_link_hash_entry *) h)->is_func)
4263 return TRUE;
4264
4265 if (h->root.type == bfd_link_hash_undefweak
4266 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR))
4267 htab->have_undefweak = TRUE;
4268
4269 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
4270 if (ent->plt.refcount > 0)
4271 break;
4272 if (ent != NULL
4273 && h->root.root.string[0] == '.'
4274 && h->root.root.string[1] != '\0')
4275 {
4276 struct elf_link_hash_entry *fdh = ((struct ppc_link_hash_entry *) h)->oh;
4277 bfd_boolean force_local;
4278
4279 /* Find the corresponding function descriptor symbol. Create it
4280 as undefined if necessary. */
4281
4282 if (fdh == NULL)
4283 fdh = elf_link_hash_lookup (&htab->elf, h->root.root.string + 1,
4284 FALSE, FALSE, TRUE);
4285
4286 if (fdh == NULL
4287 && info->shared
4288 && (h->root.type == bfd_link_hash_undefined
4289 || h->root.type == bfd_link_hash_undefweak))
4290 {
4291 bfd *abfd;
4292 asymbol *newsym;
4293 struct bfd_link_hash_entry *bh;
4294
4295 abfd = h->root.u.undef.abfd;
4296 newsym = bfd_make_empty_symbol (abfd);
4297 newsym->name = h->root.root.string + 1;
4298 newsym->section = bfd_und_section_ptr;
4299 newsym->value = 0;
4300 newsym->flags = BSF_OBJECT;
4301 if (h->root.type == bfd_link_hash_undefweak)
4302 newsym->flags |= BSF_WEAK;
4303
4304 bh = &fdh->root;
4305 if ( !(_bfd_generic_link_add_one_symbol
4306 (info, abfd, newsym->name, newsym->flags,
4307 newsym->section, newsym->value, NULL, FALSE, FALSE, &bh)))
4308 {
4309 return FALSE;
4310 }
4311 fdh = (struct elf_link_hash_entry *) bh;
4312 fdh->elf_link_hash_flags &= ~ELF_LINK_NON_ELF;
4313 }
4314
4315 if (fdh != NULL
4316 && (fdh->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0
4317 && (info->shared
4318 || (fdh->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
4319 || (fdh->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0
4320 || (fdh->root.type == bfd_link_hash_undefweak
4321 && ELF_ST_VISIBILITY (fdh->other) == STV_DEFAULT)))
4322 {
4323 if (fdh->dynindx == -1)
4324 if (! bfd_elf64_link_record_dynamic_symbol (info, fdh))
4325 return FALSE;
4326 fdh->elf_link_hash_flags |= (h->elf_link_hash_flags
4327 & (ELF_LINK_HASH_REF_REGULAR
4328 | ELF_LINK_HASH_REF_DYNAMIC
4329 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
4330 | ELF_LINK_NON_GOT_REF));
4331 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
4332 {
4333 fdh->plt.plist = h->plt.plist;
4334 fdh->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
4335 }
4336 ((struct ppc_link_hash_entry *) fdh)->is_func_descriptor = 1;
4337 ((struct ppc_link_hash_entry *) fdh)->oh = h;
4338 ((struct ppc_link_hash_entry *) h)->oh = fdh;
4339 }
4340
4341 /* Now that the info is on the function descriptor, clear the
4342 function code sym info. Any function code syms for which we
4343 don't have a definition in a regular file, we force local.
4344 This prevents a shared library from exporting syms that have
4345 been imported from another library. Function code syms that
4346 are really in the library we must leave global to prevent the
4347 linker dragging in a definition from a static library. */
4348 force_local = (info->shared
4349 && ((h->elf_link_hash_flags
4350 & ELF_LINK_HASH_DEF_REGULAR) == 0
4351 || fdh == NULL
4352 || (fdh->elf_link_hash_flags
4353 & ELF_LINK_HASH_DEF_REGULAR) == 0
4354 || (fdh->elf_link_hash_flags
4355 & ELF_LINK_FORCED_LOCAL) != 0));
4356 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
4357 }
4358
4359 return TRUE;
4360 }
4361
4362 #define MIN_SAVE_FPR 14
4363 #define MAX_SAVE_FPR 31
4364
4365 /* Called near the start of bfd_elf_size_dynamic_sections. We use
4366 this hook to a) provide some gcc support functions, and b) transfer
4367 dynamic linking information gathered so far on function code symbol
4368 entries, to their corresponding function descriptor symbol entries. */
4369 static bfd_boolean
4370 ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
4371 struct bfd_link_info *info)
4372 {
4373 struct ppc_link_hash_table *htab;
4374 unsigned int lowest_savef = MAX_SAVE_FPR + 2;
4375 unsigned int lowest_restf = MAX_SAVE_FPR + 2;
4376 unsigned int i;
4377 struct elf_link_hash_entry *h;
4378 bfd_byte *p;
4379 char sym[10];
4380
4381 htab = ppc_hash_table (info);
4382
4383 if (htab->sfpr == NULL)
4384 /* We don't have any relocs. */
4385 return TRUE;
4386
4387 /* First provide any missing ._savef* and ._restf* functions. */
4388 memcpy (sym, "._savef14", 10);
4389 for (i = MIN_SAVE_FPR; i <= MAX_SAVE_FPR; i++)
4390 {
4391 sym[7] = i / 10 + '0';
4392 sym[8] = i % 10 + '0';
4393 h = elf_link_hash_lookup (&htab->elf, sym, FALSE, FALSE, TRUE);
4394 if (h != NULL
4395 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
4396 {
4397 if (lowest_savef > i)
4398 lowest_savef = i;
4399 h->root.type = bfd_link_hash_defined;
4400 h->root.u.def.section = htab->sfpr;
4401 h->root.u.def.value = (i - lowest_savef) * 4;
4402 h->type = STT_FUNC;
4403 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
4404 _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
4405 }
4406 }
4407
4408 memcpy (sym, "._restf14", 10);
4409 for (i = MIN_SAVE_FPR; i <= MAX_SAVE_FPR; i++)
4410 {
4411 sym[7] = i / 10 + '0';
4412 sym[8] = i % 10 + '0';
4413 h = elf_link_hash_lookup (&htab->elf, sym, FALSE, FALSE, TRUE);
4414 if (h != NULL
4415 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
4416 {
4417 if (lowest_restf > i)
4418 lowest_restf = i;
4419 h->root.type = bfd_link_hash_defined;
4420 h->root.u.def.section = htab->sfpr;
4421 h->root.u.def.value = ((MAX_SAVE_FPR + 2 - lowest_savef) * 4
4422 + (i - lowest_restf) * 4);
4423 h->type = STT_FUNC;
4424 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
4425 _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
4426 }
4427 }
4428
4429 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
4430
4431 htab->sfpr->_raw_size = ((MAX_SAVE_FPR + 2 - lowest_savef) * 4
4432 + (MAX_SAVE_FPR + 2 - lowest_restf) * 4);
4433
4434 if (htab->sfpr->_raw_size == 0)
4435 {
4436 if (!htab->have_undefweak)
4437 {
4438 _bfd_strip_section_from_output (info, htab->sfpr);
4439 return TRUE;
4440 }
4441
4442 htab->sfpr->_raw_size = 4;
4443 }
4444
4445 p = bfd_alloc (htab->elf.dynobj, htab->sfpr->_raw_size);
4446 if (p == NULL)
4447 return FALSE;
4448 htab->sfpr->contents = p;
4449
4450 for (i = lowest_savef; i <= MAX_SAVE_FPR; i++)
4451 {
4452 unsigned int fpr = i << 21;
4453 unsigned int stackoff = (1 << 16) - (MAX_SAVE_FPR + 1 - i) * 8;
4454 bfd_put_32 (htab->elf.dynobj, STFD_FR0_0R1 + fpr + stackoff, p);
4455 p += 4;
4456 }
4457 if (lowest_savef <= MAX_SAVE_FPR)
4458 {
4459 bfd_put_32 (htab->elf.dynobj, BLR, p);
4460 p += 4;
4461 }
4462
4463 for (i = lowest_restf; i <= MAX_SAVE_FPR; i++)
4464 {
4465 unsigned int fpr = i << 21;
4466 unsigned int stackoff = (1 << 16) - (MAX_SAVE_FPR + 1 - i) * 8;
4467 bfd_put_32 (htab->elf.dynobj, LFD_FR0_0R1 + fpr + stackoff, p);
4468 p += 4;
4469 }
4470 if (lowest_restf <= MAX_SAVE_FPR
4471 || htab->sfpr->_raw_size == 4)
4472 {
4473 bfd_put_32 (htab->elf.dynobj, BLR, p);
4474 }
4475
4476 return TRUE;
4477 }
4478
4479 /* Adjust a symbol defined by a dynamic object and referenced by a
4480 regular object. The current definition is in some section of the
4481 dynamic object, but we're not including those sections. We have to
4482 change the definition to something the rest of the link can
4483 understand. */
4484
4485 static bfd_boolean
4486 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
4487 struct elf_link_hash_entry *h)
4488 {
4489 struct ppc_link_hash_table *htab;
4490 asection *s;
4491 unsigned int power_of_two;
4492
4493 htab = ppc_hash_table (info);
4494
4495 /* Deal with function syms. */
4496 if (h->type == STT_FUNC
4497 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
4498 {
4499 /* Clear procedure linkage table information for any symbol that
4500 won't need a .plt entry. */
4501 struct plt_entry *ent;
4502 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
4503 if (ent->plt.refcount > 0)
4504 break;
4505 if (!((struct ppc_link_hash_entry *) h)->is_func_descriptor
4506 || ent == NULL
4507 || SYMBOL_CALLS_LOCAL (info, h)
4508 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
4509 && h->root.type == bfd_link_hash_undefweak))
4510 {
4511 h->plt.plist = NULL;
4512 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
4513 }
4514 }
4515 else
4516 h->plt.plist = NULL;
4517
4518 /* If this is a weak symbol, and there is a real definition, the
4519 processor independent code will have arranged for us to see the
4520 real definition first, and we can just use the same value. */
4521 if (h->weakdef != NULL)
4522 {
4523 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
4524 || h->weakdef->root.type == bfd_link_hash_defweak);
4525 h->root.u.def.section = h->weakdef->root.u.def.section;
4526 h->root.u.def.value = h->weakdef->root.u.def.value;
4527 if (ELIMINATE_COPY_RELOCS)
4528 h->elf_link_hash_flags
4529 = ((h->elf_link_hash_flags & ~ELF_LINK_NON_GOT_REF)
4530 | (h->weakdef->elf_link_hash_flags & ELF_LINK_NON_GOT_REF));
4531 return TRUE;
4532 }
4533
4534 /* If we are creating a shared library, we must presume that the
4535 only references to the symbol are via the global offset table.
4536 For such cases we need not do anything here; the relocations will
4537 be handled correctly by relocate_section. */
4538 if (info->shared)
4539 return TRUE;
4540
4541 /* If there are no references to this symbol that do not use the
4542 GOT, we don't need to generate a copy reloc. */
4543 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
4544 return TRUE;
4545
4546 if (ELIMINATE_COPY_RELOCS)
4547 {
4548 struct ppc_link_hash_entry * eh;
4549 struct ppc_dyn_relocs *p;
4550
4551 eh = (struct ppc_link_hash_entry *) h;
4552 for (p = eh->dyn_relocs; p != NULL; p = p->next)
4553 {
4554 s = p->sec->output_section;
4555 if (s != NULL && (s->flags & SEC_READONLY) != 0)
4556 break;
4557 }
4558
4559 /* If we didn't find any dynamic relocs in read-only sections, then
4560 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
4561 if (p == NULL)
4562 {
4563 h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
4564 return TRUE;
4565 }
4566 }
4567
4568 if (h->plt.plist != NULL)
4569 return TRUE;
4570
4571 /* This is a reference to a symbol defined by a dynamic object which
4572 is not a function. */
4573
4574 /* We must allocate the symbol in our .dynbss section, which will
4575 become part of the .bss section of the executable. There will be
4576 an entry for this symbol in the .dynsym section. The dynamic
4577 object will contain position independent code, so all references
4578 from the dynamic object to this symbol will go through the global
4579 offset table. The dynamic linker will use the .dynsym entry to
4580 determine the address it must put in the global offset table, so
4581 both the dynamic object and the regular object will refer to the
4582 same memory location for the variable. */
4583
4584 /* We must generate a R_PPC64_COPY reloc to tell the dynamic linker
4585 to copy the initial value out of the dynamic object and into the
4586 runtime process image. We need to remember the offset into the
4587 .rela.bss section we are going to use. */
4588 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
4589 {
4590 htab->relbss->_raw_size += sizeof (Elf64_External_Rela);
4591 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
4592 }
4593
4594 /* We need to figure out the alignment required for this symbol. I
4595 have no idea how ELF linkers handle this. */
4596 power_of_two = bfd_log2 (h->size);
4597 if (power_of_two > 4)
4598 power_of_two = 4;
4599
4600 /* Apply the required alignment. */
4601 s = htab->dynbss;
4602 s->_raw_size = BFD_ALIGN (s->_raw_size, (bfd_size_type) (1 << power_of_two));
4603 if (power_of_two > bfd_get_section_alignment (htab->elf.dynobj, s))
4604 {
4605 if (! bfd_set_section_alignment (htab->elf.dynobj, s, power_of_two))
4606 return FALSE;
4607 }
4608
4609 /* Define the symbol as being at this point in the section. */
4610 h->root.u.def.section = s;
4611 h->root.u.def.value = s->_raw_size;
4612
4613 /* Increment the section size to make room for the symbol. */
4614 s->_raw_size += h->size;
4615
4616 return TRUE;
4617 }
4618
4619 /* If given a function descriptor symbol, hide both the function code
4620 sym and the descriptor. */
4621 static void
4622 ppc64_elf_hide_symbol (struct bfd_link_info *info,
4623 struct elf_link_hash_entry *h,
4624 bfd_boolean force_local)
4625 {
4626 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
4627
4628 if (((struct ppc_link_hash_entry *) h)->is_func_descriptor)
4629 {
4630 struct elf_link_hash_entry *fh = ((struct ppc_link_hash_entry *) h)->oh;
4631
4632 if (fh == NULL)
4633 {
4634 const char *p, *q;
4635 struct ppc_link_hash_table *htab;
4636 char save;
4637
4638 /* We aren't supposed to use alloca in BFD because on
4639 systems which do not have alloca the version in libiberty
4640 calls xmalloc, which might cause the program to crash
4641 when it runs out of memory. This function doesn't have a
4642 return status, so there's no way to gracefully return an
4643 error. So cheat. We know that string[-1] can be safely
4644 dereferenced; It's either a string in an ELF string
4645 table, or allocated in an objalloc structure. */
4646
4647 p = h->root.root.string - 1;
4648 save = *p;
4649 *(char *) p = '.';
4650 htab = ppc_hash_table (info);
4651 fh = elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
4652 *(char *) p = save;
4653
4654 /* Unfortunately, if it so happens that the string we were
4655 looking for was allocated immediately before this string,
4656 then we overwrote the string terminator. That's the only
4657 reason the lookup should fail. */
4658 if (fh == NULL)
4659 {
4660 q = h->root.root.string + strlen (h->root.root.string);
4661 while (q >= h->root.root.string && *q == *p)
4662 --q, --p;
4663 if (q < h->root.root.string && *p == '.')
4664 fh = elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
4665 }
4666 if (fh != NULL)
4667 {
4668 ((struct ppc_link_hash_entry *) h)->oh = fh;
4669 ((struct ppc_link_hash_entry *) fh)->oh = h;
4670 }
4671 }
4672 if (fh != NULL)
4673 _bfd_elf_link_hash_hide_symbol (info, fh, force_local);
4674 }
4675 }
4676
4677 static bfd_boolean
4678 get_sym_h (struct elf_link_hash_entry **hp, Elf_Internal_Sym **symp,
4679 asection **symsecp, char **tls_maskp, Elf_Internal_Sym **locsymsp,
4680 unsigned long r_symndx, bfd *ibfd)
4681 {
4682 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
4683
4684 if (r_symndx >= symtab_hdr->sh_info)
4685 {
4686 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
4687 struct elf_link_hash_entry *h;
4688
4689 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4690 while (h->root.type == bfd_link_hash_indirect
4691 || h->root.type == bfd_link_hash_warning)
4692 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4693
4694 if (hp != NULL)
4695 *hp = h;
4696
4697 if (symp != NULL)
4698 *symp = NULL;
4699
4700 if (symsecp != NULL)
4701 {
4702 asection *symsec = NULL;
4703 if (h->root.type == bfd_link_hash_defined
4704 || h->root.type == bfd_link_hash_defweak)
4705 symsec = h->root.u.def.section;
4706 *symsecp = symsec;
4707 }
4708
4709 if (tls_maskp != NULL)
4710 {
4711 struct ppc_link_hash_entry *eh;
4712
4713 eh = (struct ppc_link_hash_entry *) h;
4714 *tls_maskp = &eh->tls_mask;
4715 }
4716 }
4717 else
4718 {
4719 Elf_Internal_Sym *sym;
4720 Elf_Internal_Sym *locsyms = *locsymsp;
4721
4722 if (locsyms == NULL)
4723 {
4724 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
4725 if (locsyms == NULL)
4726 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
4727 symtab_hdr->sh_info,
4728 0, NULL, NULL, NULL);
4729 if (locsyms == NULL)
4730 return FALSE;
4731 *locsymsp = locsyms;
4732 }
4733 sym = locsyms + r_symndx;
4734
4735 if (hp != NULL)
4736 *hp = NULL;
4737
4738 if (symp != NULL)
4739 *symp = sym;
4740
4741 if (symsecp != NULL)
4742 {
4743 asection *symsec = NULL;
4744 if ((sym->st_shndx != SHN_UNDEF
4745 && sym->st_shndx < SHN_LORESERVE)
4746 || sym->st_shndx > SHN_HIRESERVE)
4747 symsec = bfd_section_from_elf_index (ibfd, sym->st_shndx);
4748 *symsecp = symsec;
4749 }
4750
4751 if (tls_maskp != NULL)
4752 {
4753 struct got_entry **lgot_ents;
4754 char *tls_mask;
4755
4756 tls_mask = NULL;
4757 lgot_ents = elf_local_got_ents (ibfd);
4758 if (lgot_ents != NULL)
4759 {
4760 char *lgot_masks = (char *) (lgot_ents + symtab_hdr->sh_info);
4761 tls_mask = &lgot_masks[r_symndx];
4762 }
4763 *tls_maskp = tls_mask;
4764 }
4765 }
4766 return TRUE;
4767 }
4768
4769 /* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
4770 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
4771 type suitable for optimization, and 1 otherwise. */
4772
4773 static int
4774 get_tls_mask (char **tls_maskp, Elf_Internal_Sym **locsymsp,
4775 const Elf_Internal_Rela *rel, bfd *ibfd)
4776 {
4777 unsigned long r_symndx;
4778 unsigned int next_r;
4779 struct elf_link_hash_entry *h;
4780 Elf_Internal_Sym *sym;
4781 asection *sec;
4782 bfd_vma off;
4783
4784 r_symndx = ELF64_R_SYM (rel->r_info);
4785 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
4786 return 0;
4787
4788 if ((*tls_maskp != NULL && **tls_maskp != 0)
4789 || sec == NULL
4790 || ppc64_elf_section_data (sec)->t_symndx == NULL)
4791 return 1;
4792
4793 /* Look inside a TOC section too. */
4794 if (h != NULL)
4795 {
4796 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
4797 off = h->root.u.def.value;
4798 }
4799 else
4800 off = sym->st_value;
4801 off += rel->r_addend;
4802 BFD_ASSERT (off % 8 == 0);
4803 r_symndx = ppc64_elf_section_data (sec)->t_symndx[off / 8];
4804 next_r = ppc64_elf_section_data (sec)->t_symndx[off / 8 + 1];
4805 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
4806 return 0;
4807 if (h == NULL
4808 || h->root.type == bfd_link_hash_defined
4809 || h->root.type == bfd_link_hash_defweak)
4810 {
4811 if (next_r == (unsigned) -1)
4812 return 2;
4813 if (next_r == (unsigned) -2
4814 && (h == NULL
4815 || !(h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)))
4816 return 3;
4817 }
4818 return 1;
4819 }
4820
4821 bfd_boolean
4822 ppc64_elf_edit_opd (bfd *obfd, struct bfd_link_info *info)
4823 {
4824 bfd *ibfd;
4825
4826 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
4827 {
4828 asection *sec;
4829 Elf_Internal_Rela *relstart, *rel, *relend;
4830 Elf_Internal_Shdr *symtab_hdr;
4831 Elf_Internal_Sym *local_syms;
4832 struct elf_link_hash_entry **sym_hashes;
4833 bfd_vma offset;
4834 bfd_size_type amt;
4835 long *adjust;
4836 bfd_boolean need_edit;
4837
4838 sec = bfd_get_section_by_name (ibfd, ".opd");
4839 if (sec == NULL)
4840 continue;
4841
4842 amt = sec->_raw_size * sizeof (long) / 24;
4843 adjust = ppc64_elf_section_data (sec)->opd.adjust;
4844 if (adjust == NULL)
4845 {
4846 /* Must be a ld -r link. ie. check_relocs hasn't been
4847 called. */
4848 adjust = bfd_zalloc (obfd, amt);
4849 ppc64_elf_section_data (sec)->opd.adjust = adjust;
4850 }
4851 memset (adjust, 0, amt);
4852
4853 if (sec->output_section == bfd_abs_section_ptr)
4854 continue;
4855
4856 /* Look through the section relocs. */
4857 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
4858 continue;
4859
4860 local_syms = NULL;
4861 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
4862 sym_hashes = elf_sym_hashes (ibfd);
4863
4864 /* Read the relocations. */
4865 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
4866 info->keep_memory);
4867 if (relstart == NULL)
4868 return FALSE;
4869
4870 /* First run through the relocs to check they are sane, and to
4871 determine whether we need to edit this opd section. */
4872 need_edit = FALSE;
4873 offset = 0;
4874 relend = relstart + sec->reloc_count;
4875 for (rel = relstart; rel < relend; rel++)
4876 {
4877 enum elf_ppc64_reloc_type r_type;
4878 unsigned long r_symndx;
4879 asection *sym_sec;
4880 struct elf_link_hash_entry *h;
4881 Elf_Internal_Sym *sym;
4882
4883 /* .opd contains a regular array of 24 byte entries. We're
4884 only interested in the reloc pointing to a function entry
4885 point. */
4886 r_type = ELF64_R_TYPE (rel->r_info);
4887 if (r_type == R_PPC64_TOC)
4888 continue;
4889
4890 if (r_type != R_PPC64_ADDR64)
4891 {
4892 (*_bfd_error_handler)
4893 (_("%s: unexpected reloc type %u in .opd section"),
4894 bfd_archive_filename (ibfd), r_type);
4895 need_edit = FALSE;
4896 break;
4897 }
4898
4899 if (rel + 1 >= relend)
4900 continue;
4901 r_type = ELF64_R_TYPE ((rel + 1)->r_info);
4902 if (r_type != R_PPC64_TOC)
4903 continue;
4904
4905 if (rel->r_offset != offset)
4906 {
4907 /* If someone messes with .opd alignment then after a
4908 "ld -r" we might have padding in the middle of .opd.
4909 Also, there's nothing to prevent someone putting
4910 something silly in .opd with the assembler. No .opd
4911 optimization for them! */
4912 (*_bfd_error_handler)
4913 (_("%s: .opd is not a regular array of opd entries"),
4914 bfd_archive_filename (ibfd));
4915 need_edit = FALSE;
4916 break;
4917 }
4918
4919 r_symndx = ELF64_R_SYM (rel->r_info);
4920 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
4921 r_symndx, ibfd))
4922 goto error_free_rel;
4923
4924 if (sym_sec == NULL || sym_sec->owner == NULL)
4925 {
4926 const char *sym_name;
4927 if (h != NULL)
4928 sym_name = h->root.root.string;
4929 else
4930 sym_name = bfd_elf_local_sym_name (ibfd, sym);
4931
4932 (*_bfd_error_handler)
4933 (_("%s: undefined sym `%s' in .opd section"),
4934 bfd_archive_filename (ibfd),
4935 sym_name);
4936 need_edit = FALSE;
4937 break;
4938 }
4939
4940 /* opd entries are always for functions defined in the
4941 current input bfd. If the symbol isn't defined in the
4942 input bfd, then we won't be using the function in this
4943 bfd; It must be defined in a linkonce section in another
4944 bfd, or is weak. It's also possible that we are
4945 discarding the function due to a linker script /DISCARD/,
4946 which we test for via the output_section. */
4947 if (sym_sec->owner != ibfd
4948 || sym_sec->output_section == bfd_abs_section_ptr)
4949 need_edit = TRUE;
4950
4951 offset += 24;
4952 }
4953
4954 if (need_edit)
4955 {
4956 Elf_Internal_Rela *write_rel;
4957 bfd_byte *rptr, *wptr;
4958 bfd_boolean skip;
4959
4960 /* This seems a waste of time as input .opd sections are all
4961 zeros as generated by gcc, but I suppose there's no reason
4962 this will always be so. We might start putting something in
4963 the third word of .opd entries. */
4964 if ((sec->flags & SEC_IN_MEMORY) == 0)
4965 {
4966 bfd_byte *loc = bfd_alloc (ibfd, sec->_raw_size);
4967 if (loc == NULL
4968 || !bfd_get_section_contents (ibfd, sec, loc, 0,
4969 sec->_raw_size))
4970 {
4971 if (local_syms != NULL
4972 && symtab_hdr->contents != (unsigned char *) local_syms)
4973 free (local_syms);
4974 error_free_rel:
4975 if (elf_section_data (sec)->relocs != relstart)
4976 free (relstart);
4977 return FALSE;
4978 }
4979 sec->contents = loc;
4980 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
4981 }
4982
4983 elf_section_data (sec)->relocs = relstart;
4984
4985 wptr = sec->contents;
4986 rptr = sec->contents;
4987 write_rel = relstart;
4988 skip = FALSE;
4989 offset = 0;
4990 for (rel = relstart; rel < relend; rel++)
4991 {
4992 if (rel->r_offset == offset)
4993 {
4994 unsigned long r_symndx;
4995 asection *sym_sec;
4996 struct elf_link_hash_entry *h;
4997 Elf_Internal_Sym *sym;
4998
4999 r_symndx = ELF64_R_SYM (rel->r_info);
5000 get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
5001 r_symndx, ibfd);
5002
5003 skip = (sym_sec->owner != ibfd
5004 || sym_sec->output_section == bfd_abs_section_ptr);
5005 if (skip)
5006 {
5007 if (h != NULL && sym_sec->owner == ibfd)
5008 {
5009 /* Arrange for the function descriptor sym
5010 to be dropped. */
5011 struct ppc_link_hash_entry *fdh;
5012 struct ppc_link_hash_entry *fh;
5013
5014 fh = (struct ppc_link_hash_entry *) h;
5015 fdh = (struct ppc_link_hash_entry *) fh->oh;
5016 if (fdh == NULL)
5017 {
5018 const char *fd_name;
5019 struct ppc_link_hash_table *htab;
5020
5021 fd_name = h->root.root.string + 1;
5022 htab = ppc_hash_table (info);
5023 fdh = (struct ppc_link_hash_entry *)
5024 elf_link_hash_lookup (&htab->elf, fd_name,
5025 FALSE, FALSE, FALSE);
5026 fdh->is_func_descriptor = 1;
5027 fdh->oh = &fh->elf;
5028 fh->is_func = 1;
5029 fh->oh = &fdh->elf;
5030 }
5031
5032 fdh->elf.root.u.def.value = 0;
5033 fdh->elf.root.u.def.section = sym_sec;
5034 }
5035 }
5036 else
5037 {
5038 /* We'll be keeping this opd entry. */
5039
5040 if (h != NULL)
5041 {
5042 /* Redefine the function descriptor symbol
5043 to this location in the opd section.
5044 We've checked above that opd relocs are
5045 ordered. */
5046 struct ppc_link_hash_entry *fdh;
5047 struct ppc_link_hash_entry *fh;
5048
5049 fh = (struct ppc_link_hash_entry *) h;
5050 fdh = (struct ppc_link_hash_entry *) fh->oh;
5051 if (fdh == NULL)
5052 {
5053 const char *fd_name;
5054 struct ppc_link_hash_table *htab;
5055
5056 fd_name = h->root.root.string + 1;
5057 htab = ppc_hash_table (info);
5058 fdh = (struct ppc_link_hash_entry *)
5059 elf_link_hash_lookup (&htab->elf, fd_name,
5060 FALSE, FALSE, FALSE);
5061 fdh->is_func_descriptor = 1;
5062 fdh->oh = &fh->elf;
5063 fh->is_func = 1;
5064 fh->oh = &fdh->elf;
5065 }
5066
5067 fdh->elf.root.u.def.value = wptr - sec->contents;
5068 }
5069 else
5070 {
5071 /* Local syms are a bit tricky. We could
5072 tweak them as they can be cached, but
5073 we'd need to look through the local syms
5074 for the function descriptor sym which we
5075 don't have at the moment. So keep an
5076 array of adjustments. */
5077 adjust[rel->r_offset / 24] = wptr - rptr;
5078 }
5079
5080 if (wptr != rptr)
5081 memcpy (wptr, rptr, 24);
5082 wptr += 24;
5083 }
5084 rptr += 24;
5085 offset += 24;
5086 }
5087
5088 /* We need to adjust any reloc offsets to point to the
5089 new opd entries. While we're at it, we may as well
5090 remove redundant relocs. */
5091 if (!skip)
5092 {
5093 rel->r_offset += wptr - rptr;
5094 if (write_rel != rel)
5095 memcpy (write_rel, rel, sizeof (*rel));
5096 ++write_rel;
5097 }
5098 }
5099
5100 sec->_cooked_size = wptr - sec->contents;
5101 sec->reloc_count = write_rel - relstart;
5102 /* Fudge the size too, as this is used later in
5103 elf_bfd_final_link if we are emitting relocs. */
5104 elf_section_data (sec)->rel_hdr.sh_size
5105 = sec->reloc_count * elf_section_data (sec)->rel_hdr.sh_entsize;
5106 BFD_ASSERT (elf_section_data (sec)->rel_hdr2 == NULL);
5107 }
5108 else if (elf_section_data (sec)->relocs != relstart)
5109 free (relstart);
5110
5111 if (local_syms != NULL
5112 && symtab_hdr->contents != (unsigned char *) local_syms)
5113 {
5114 if (!info->keep_memory)
5115 free (local_syms);
5116 else
5117 symtab_hdr->contents = (unsigned char *) local_syms;
5118 }
5119 }
5120
5121 return TRUE;
5122 }
5123
5124 /* Set htab->tls_sec. */
5125
5126 bfd_boolean
5127 ppc64_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
5128 {
5129 asection *tls;
5130 struct ppc_link_hash_table *htab;
5131
5132 for (tls = obfd->sections; tls != NULL; tls = tls->next)
5133 if ((tls->flags & (SEC_THREAD_LOCAL | SEC_LOAD))
5134 == (SEC_THREAD_LOCAL | SEC_LOAD))
5135 break;
5136
5137 htab = ppc_hash_table (info);
5138 htab->tls_sec = tls;
5139
5140 if (htab->tls_get_addr != NULL)
5141 {
5142 struct elf_link_hash_entry *h = htab->tls_get_addr;
5143
5144 while (h->root.type == bfd_link_hash_indirect
5145 || h->root.type == bfd_link_hash_warning)
5146 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5147
5148 htab->tls_get_addr = h;
5149 }
5150
5151 return tls != NULL;
5152 }
5153
5154 /* Run through all the TLS relocs looking for optimization
5155 opportunities. The linker has been hacked (see ppc64elf.em) to do
5156 a preliminary section layout so that we know the TLS segment
5157 offsets. We can't optimize earlier because some optimizations need
5158 to know the tp offset, and we need to optimize before allocating
5159 dynamic relocations. */
5160
5161 bfd_boolean
5162 ppc64_elf_tls_optimize (bfd *obfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
5163 {
5164 bfd *ibfd;
5165 asection *sec;
5166 struct ppc_link_hash_table *htab;
5167
5168 if (info->relocatable || info->shared)
5169 return TRUE;
5170
5171 htab = ppc_hash_table (info);
5172 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
5173 {
5174 Elf_Internal_Sym *locsyms = NULL;
5175
5176 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
5177 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
5178 {
5179 Elf_Internal_Rela *relstart, *rel, *relend;
5180 int expecting_tls_get_addr;
5181
5182 /* Read the relocations. */
5183 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
5184 info->keep_memory);
5185 if (relstart == NULL)
5186 return FALSE;
5187
5188 expecting_tls_get_addr = 0;
5189 relend = relstart + sec->reloc_count;
5190 for (rel = relstart; rel < relend; rel++)
5191 {
5192 enum elf_ppc64_reloc_type r_type;
5193 unsigned long r_symndx;
5194 struct elf_link_hash_entry *h;
5195 Elf_Internal_Sym *sym;
5196 asection *sym_sec;
5197 char *tls_mask;
5198 char tls_set, tls_clear, tls_type = 0;
5199 bfd_vma value;
5200 bfd_boolean ok_tprel, is_local;
5201
5202 r_symndx = ELF64_R_SYM (rel->r_info);
5203 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
5204 r_symndx, ibfd))
5205 {
5206 err_free_rel:
5207 if (elf_section_data (sec)->relocs != relstart)
5208 free (relstart);
5209 if (locsyms != NULL
5210 && (elf_tdata (ibfd)->symtab_hdr.contents
5211 != (unsigned char *) locsyms))
5212 free (locsyms);
5213 return FALSE;
5214 }
5215
5216 if (h != NULL)
5217 {
5218 if (h->root.type != bfd_link_hash_defined
5219 && h->root.type != bfd_link_hash_defweak)
5220 continue;
5221 value = h->root.u.def.value;
5222 }
5223 else
5224 value = sym->st_value;
5225
5226 ok_tprel = FALSE;
5227 is_local = FALSE;
5228 if (h == NULL
5229 || !(h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC))
5230 {
5231 is_local = TRUE;
5232 value += sym_sec->output_offset;
5233 value += sym_sec->output_section->vma;
5234 value -= htab->tls_sec->vma;
5235 ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
5236 < (bfd_vma) 1 << 32);
5237 }
5238
5239 r_type = ELF64_R_TYPE (rel->r_info);
5240 switch (r_type)
5241 {
5242 case R_PPC64_GOT_TLSLD16:
5243 case R_PPC64_GOT_TLSLD16_LO:
5244 case R_PPC64_GOT_TLSLD16_HI:
5245 case R_PPC64_GOT_TLSLD16_HA:
5246 /* These relocs should never be against a symbol
5247 defined in a shared lib. Leave them alone if
5248 that turns out to be the case. */
5249 ppc64_tlsld_got (ibfd)->refcount -= 1;
5250 if (!is_local)
5251 continue;
5252
5253 /* LD -> LE */
5254 tls_set = 0;
5255 tls_clear = TLS_LD;
5256 tls_type = TLS_TLS | TLS_LD;
5257 expecting_tls_get_addr = 1;
5258 break;
5259
5260 case R_PPC64_GOT_TLSGD16:
5261 case R_PPC64_GOT_TLSGD16_LO:
5262 case R_PPC64_GOT_TLSGD16_HI:
5263 case R_PPC64_GOT_TLSGD16_HA:
5264 if (ok_tprel)
5265 /* GD -> LE */
5266 tls_set = 0;
5267 else
5268 /* GD -> IE */
5269 tls_set = TLS_TLS | TLS_TPRELGD;
5270 tls_clear = TLS_GD;
5271 tls_type = TLS_TLS | TLS_GD;
5272 expecting_tls_get_addr = 1;
5273 break;
5274
5275 case R_PPC64_GOT_TPREL16_DS:
5276 case R_PPC64_GOT_TPREL16_LO_DS:
5277 case R_PPC64_GOT_TPREL16_HI:
5278 case R_PPC64_GOT_TPREL16_HA:
5279 expecting_tls_get_addr = 0;
5280 if (ok_tprel)
5281 {
5282 /* IE -> LE */
5283 tls_set = 0;
5284 tls_clear = TLS_TPREL;
5285 tls_type = TLS_TLS | TLS_TPREL;
5286 break;
5287 }
5288 else
5289 continue;
5290
5291 case R_PPC64_REL14:
5292 case R_PPC64_REL14_BRTAKEN:
5293 case R_PPC64_REL14_BRNTAKEN:
5294 case R_PPC64_REL24:
5295 if (h != NULL
5296 && h == htab->tls_get_addr)
5297 {
5298 if (!expecting_tls_get_addr
5299 && rel != relstart
5300 && ((ELF64_R_TYPE (rel[-1].r_info)
5301 == R_PPC64_TOC16)
5302 || (ELF64_R_TYPE (rel[-1].r_info)
5303 == R_PPC64_TOC16_LO)))
5304 {
5305 /* Check for toc tls entries. */
5306 char *toc_tls;
5307 int retval;
5308
5309 retval = get_tls_mask (&toc_tls, &locsyms,
5310 rel - 1, ibfd);
5311 if (retval == 0)
5312 goto err_free_rel;
5313 if (toc_tls != NULL)
5314 expecting_tls_get_addr = retval > 1;
5315 }
5316
5317 if (expecting_tls_get_addr)
5318 {
5319 struct plt_entry *ent;
5320 for (ent = h->plt.plist; ent; ent = ent->next)
5321 if (ent->addend == 0)
5322 {
5323 if (ent->plt.refcount > 0)
5324 ent->plt.refcount -= 1;
5325 break;
5326 }
5327 }
5328 }
5329 expecting_tls_get_addr = 0;
5330 continue;
5331
5332 case R_PPC64_TPREL64:
5333 expecting_tls_get_addr = 0;
5334 if (ok_tprel)
5335 {
5336 /* IE -> LE */
5337 tls_set = TLS_EXPLICIT;
5338 tls_clear = TLS_TPREL;
5339 break;
5340 }
5341 else
5342 continue;
5343
5344 case R_PPC64_DTPMOD64:
5345 expecting_tls_get_addr = 0;
5346 if (rel + 1 < relend
5347 && (rel[1].r_info
5348 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
5349 && rel[1].r_offset == rel->r_offset + 8)
5350 {
5351 if (ok_tprel)
5352 /* GD -> LE */
5353 tls_set = TLS_EXPLICIT | TLS_GD;
5354 else
5355 /* GD -> IE */
5356 tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
5357 tls_clear = TLS_GD;
5358 }
5359 else
5360 {
5361 if (!is_local)
5362 continue;
5363
5364 /* LD -> LE */
5365 tls_set = TLS_EXPLICIT;
5366 tls_clear = TLS_LD;
5367 }
5368 break;
5369
5370 default:
5371 expecting_tls_get_addr = 0;
5372 continue;
5373 }
5374
5375 if ((tls_set & TLS_EXPLICIT) == 0)
5376 {
5377 struct got_entry *ent;
5378
5379 /* Adjust got entry for this reloc. */
5380 if (h != NULL)
5381 ent = h->got.glist;
5382 else
5383 ent = elf_local_got_ents (ibfd)[r_symndx];
5384
5385 for (; ent != NULL; ent = ent->next)
5386 if (ent->addend == rel->r_addend
5387 && ent->owner == ibfd
5388 && ent->tls_type == tls_type)
5389 break;
5390 if (ent == NULL)
5391 abort ();
5392
5393 if (tls_set == 0)
5394 {
5395 /* We managed to get rid of a got entry. */
5396 if (ent->got.refcount > 0)
5397 ent->got.refcount -= 1;
5398 }
5399 }
5400 else if (h != NULL)
5401 {
5402 struct ppc_link_hash_entry * eh;
5403 struct ppc_dyn_relocs **pp;
5404 struct ppc_dyn_relocs *p;
5405
5406 /* Adjust dynamic relocs. */
5407 eh = (struct ppc_link_hash_entry *) h;
5408 for (pp = &eh->dyn_relocs;
5409 (p = *pp) != NULL;
5410 pp = &p->next)
5411 if (p->sec == sec)
5412 {
5413 /* If we got rid of a DTPMOD/DTPREL reloc
5414 pair then we'll lose one or two dyn
5415 relocs. */
5416 if (tls_set == (TLS_EXPLICIT | TLS_GD))
5417 p->count -= 1;
5418 p->count -= 1;
5419 if (p->count == 0)
5420 *pp = p->next;
5421 break;
5422 }
5423 }
5424
5425 *tls_mask |= tls_set;
5426 *tls_mask &= ~tls_clear;
5427 }
5428
5429 if (elf_section_data (sec)->relocs != relstart)
5430 free (relstart);
5431 }
5432
5433 if (locsyms != NULL
5434 && (elf_tdata (ibfd)->symtab_hdr.contents
5435 != (unsigned char *) locsyms))
5436 {
5437 if (!info->keep_memory)
5438 free (locsyms);
5439 else
5440 elf_tdata (ibfd)->symtab_hdr.contents = (unsigned char *) locsyms;
5441 }
5442 }
5443 return TRUE;
5444 }
5445
5446 /* This is the condition under which ppc64_elf_finish_dynamic_symbol
5447 will be called from elflink.h. If elflink.h doesn't call our
5448 finish_dynamic_symbol routine, we'll need to do something about
5449 initializing any .plt and .got entries in ppc64_elf_relocate_section. */
5450 #define WILL_CALL_FINISH_DYNAMIC_SYMBOL(DYN, SHARED, H) \
5451 ((DYN) \
5452 && ((SHARED) \
5453 || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0) \
5454 && ((H)->dynindx != -1 \
5455 || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0))
5456
5457 /* Allocate space in .plt, .got and associated reloc sections for
5458 dynamic relocs. */
5459
5460 static bfd_boolean
5461 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
5462 {
5463 struct bfd_link_info *info;
5464 struct ppc_link_hash_table *htab;
5465 asection *s;
5466 struct ppc_link_hash_entry *eh;
5467 struct ppc_dyn_relocs *p;
5468 struct got_entry *gent;
5469
5470 if (h->root.type == bfd_link_hash_indirect)
5471 return TRUE;
5472
5473 if (h->root.type == bfd_link_hash_warning)
5474 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5475
5476 info = (struct bfd_link_info *) inf;
5477 htab = ppc_hash_table (info);
5478
5479 if (htab->elf.dynamic_sections_created
5480 && h->dynindx != -1
5481 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
5482 {
5483 struct plt_entry *pent;
5484 bfd_boolean doneone = FALSE;
5485 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
5486 if (pent->plt.refcount > 0)
5487 {
5488 BFD_ASSERT (((struct ppc_link_hash_entry *) h)->is_func_descriptor);
5489
5490 /* If this is the first .plt entry, make room for the special
5491 first entry. */
5492 s = htab->plt;
5493 if (s->_raw_size == 0)
5494 s->_raw_size += PLT_INITIAL_ENTRY_SIZE;
5495
5496 pent->plt.offset = s->_raw_size;
5497
5498 /* Make room for this entry. */
5499 s->_raw_size += PLT_ENTRY_SIZE;
5500
5501 /* Make room for the .glink code. */
5502 s = htab->glink;
5503 if (s->_raw_size == 0)
5504 s->_raw_size += GLINK_CALL_STUB_SIZE;
5505 /* We need bigger stubs past index 32767. */
5506 if (s->_raw_size >= GLINK_CALL_STUB_SIZE + 32768*2*4)
5507 s->_raw_size += 4;
5508 s->_raw_size += 2*4;
5509
5510 /* We also need to make an entry in the .rela.plt section. */
5511 s = htab->relplt;
5512 s->_raw_size += sizeof (Elf64_External_Rela);
5513 doneone = TRUE;
5514 }
5515 else
5516 pent->plt.offset = (bfd_vma) -1;
5517 if (!doneone)
5518 {
5519 h->plt.plist = NULL;
5520 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
5521 }
5522 }
5523 else
5524 {
5525 h->plt.plist = NULL;
5526 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
5527 }
5528
5529 eh = (struct ppc_link_hash_entry *) h;
5530 /* Run through the TLS GD got entries first if we're changing them
5531 to TPREL. */
5532 if ((eh->tls_mask & TLS_TPRELGD) != 0)
5533 for (gent = h->got.glist; gent != NULL; gent = gent->next)
5534 if (gent->got.refcount > 0
5535 && (gent->tls_type & TLS_GD) != 0)
5536 {
5537 /* This was a GD entry that has been converted to TPREL. If
5538 there happens to be a TPREL entry we can use that one. */
5539 struct got_entry *ent;
5540 for (ent = h->got.glist; ent != NULL; ent = ent->next)
5541 if (ent->got.refcount > 0
5542 && (ent->tls_type & TLS_TPREL) != 0
5543 && ent->addend == gent->addend
5544 && ent->owner == gent->owner)
5545 {
5546 gent->got.refcount = 0;
5547 break;
5548 }
5549
5550 /* If not, then we'll be using our own TPREL entry. */
5551 if (gent->got.refcount != 0)
5552 gent->tls_type = TLS_TLS | TLS_TPREL;
5553 }
5554
5555 for (gent = h->got.glist; gent != NULL; gent = gent->next)
5556 if (gent->got.refcount > 0)
5557 {
5558 bfd_boolean dyn;
5559
5560 /* Make sure this symbol is output as a dynamic symbol.
5561 Undefined weak syms won't yet be marked as dynamic,
5562 nor will all TLS symbols. */
5563 if (h->dynindx == -1
5564 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
5565 {
5566 if (! bfd_elf64_link_record_dynamic_symbol (info, h))
5567 return FALSE;
5568 }
5569
5570 if ((gent->tls_type & TLS_LD) != 0
5571 && !(h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC))
5572 {
5573 gent->got.offset = ppc64_tlsld_got (gent->owner)->offset;
5574 continue;
5575 }
5576
5577 s = ppc64_elf_tdata (gent->owner)->got;
5578 gent->got.offset = s->_raw_size;
5579 s->_raw_size
5580 += (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)) ? 16 : 8;
5581 dyn = htab->elf.dynamic_sections_created;
5582 if ((info->shared
5583 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))
5584 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
5585 || h->root.type != bfd_link_hash_undefweak))
5586 ppc64_elf_tdata (gent->owner)->relgot->_raw_size
5587 += (gent->tls_type & eh->tls_mask & TLS_GD
5588 ? 2 * sizeof (Elf64_External_Rela)
5589 : sizeof (Elf64_External_Rela));
5590 }
5591 else
5592 gent->got.offset = (bfd_vma) -1;
5593
5594 if (eh->dyn_relocs == NULL)
5595 return TRUE;
5596
5597 /* In the shared -Bsymbolic case, discard space allocated for
5598 dynamic pc-relative relocs against symbols which turn out to be
5599 defined in regular objects. For the normal shared case, discard
5600 space for relocs that have become local due to symbol visibility
5601 changes. */
5602
5603 if (info->shared)
5604 {
5605 /* Relocs that use pc_count are those that appear on a call insn,
5606 or certain REL relocs (see MUST_BE_DYN_RELOC) that can be
5607 generated via assembly. We want calls to protected symbols to
5608 resolve directly to the function rather than going via the plt.
5609 If people want function pointer comparisons to work as expected
5610 then they should avoid writing weird assembly. */
5611 if (SYMBOL_CALLS_LOCAL (info, h))
5612 {
5613 struct ppc_dyn_relocs **pp;
5614
5615 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
5616 {
5617 p->count -= p->pc_count;
5618 p->pc_count = 0;
5619 if (p->count == 0)
5620 *pp = p->next;
5621 else
5622 pp = &p->next;
5623 }
5624 }
5625
5626 /* Also discard relocs on undefined weak syms with non-default
5627 visibility. */
5628 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
5629 && h->root.type == bfd_link_hash_undefweak)
5630 eh->dyn_relocs = NULL;
5631 }
5632 else if (ELIMINATE_COPY_RELOCS)
5633 {
5634 /* For the non-shared case, discard space for relocs against
5635 symbols which turn out to need copy relocs or are not
5636 dynamic. */
5637
5638 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
5639 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
5640 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
5641 {
5642 /* Make sure this symbol is output as a dynamic symbol.
5643 Undefined weak syms won't yet be marked as dynamic. */
5644 if (h->dynindx == -1
5645 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
5646 {
5647 if (! bfd_elf64_link_record_dynamic_symbol (info, h))
5648 return FALSE;
5649 }
5650
5651 /* If that succeeded, we know we'll be keeping all the
5652 relocs. */
5653 if (h->dynindx != -1)
5654 goto keep;
5655 }
5656
5657 eh->dyn_relocs = NULL;
5658
5659 keep: ;
5660 }
5661
5662 /* Finally, allocate space. */
5663 for (p = eh->dyn_relocs; p != NULL; p = p->next)
5664 {
5665 asection *sreloc = elf_section_data (p->sec)->sreloc;
5666 sreloc->_raw_size += p->count * sizeof (Elf64_External_Rela);
5667 }
5668
5669 return TRUE;
5670 }
5671
5672 /* Find any dynamic relocs that apply to read-only sections. */
5673
5674 static bfd_boolean
5675 readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
5676 {
5677 struct ppc_link_hash_entry *eh;
5678 struct ppc_dyn_relocs *p;
5679
5680 if (h->root.type == bfd_link_hash_warning)
5681 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5682
5683 eh = (struct ppc_link_hash_entry *) h;
5684 for (p = eh->dyn_relocs; p != NULL; p = p->next)
5685 {
5686 asection *s = p->sec->output_section;
5687
5688 if (s != NULL && (s->flags & SEC_READONLY) != 0)
5689 {
5690 struct bfd_link_info *info = inf;
5691
5692 info->flags |= DF_TEXTREL;
5693
5694 /* Not an error, just cut short the traversal. */
5695 return FALSE;
5696 }
5697 }
5698 return TRUE;
5699 }
5700
5701 /* Set the sizes of the dynamic sections. */
5702
5703 static bfd_boolean
5704 ppc64_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
5705 struct bfd_link_info *info)
5706 {
5707 struct ppc_link_hash_table *htab;
5708 bfd *dynobj;
5709 asection *s;
5710 bfd_boolean relocs;
5711 bfd *ibfd;
5712
5713 htab = ppc_hash_table (info);
5714 dynobj = htab->elf.dynobj;
5715 if (dynobj == NULL)
5716 abort ();
5717
5718 if (htab->elf.dynamic_sections_created)
5719 {
5720 /* Set the contents of the .interp section to the interpreter. */
5721 if (info->executable)
5722 {
5723 s = bfd_get_section_by_name (dynobj, ".interp");
5724 if (s == NULL)
5725 abort ();
5726 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
5727 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
5728 }
5729 }
5730
5731 /* Set up .got offsets for local syms, and space for local dynamic
5732 relocs. */
5733 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
5734 {
5735 struct got_entry **lgot_ents;
5736 struct got_entry **end_lgot_ents;
5737 char *lgot_masks;
5738 bfd_size_type locsymcount;
5739 Elf_Internal_Shdr *symtab_hdr;
5740 asection *srel;
5741
5742 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
5743 continue;
5744
5745 if (ppc64_tlsld_got (ibfd)->refcount > 0)
5746 {
5747 s = ppc64_elf_tdata (ibfd)->got;
5748 ppc64_tlsld_got (ibfd)->offset = s->_raw_size;
5749 s->_raw_size += 16;
5750 if (info->shared)
5751 {
5752 srel = ppc64_elf_tdata (ibfd)->relgot;
5753 srel->_raw_size += sizeof (Elf64_External_Rela);
5754 }
5755 }
5756 else
5757 ppc64_tlsld_got (ibfd)->offset = (bfd_vma) -1;
5758
5759 for (s = ibfd->sections; s != NULL; s = s->next)
5760 {
5761 struct ppc_dyn_relocs *p;
5762
5763 for (p = *((struct ppc_dyn_relocs **)
5764 &elf_section_data (s)->local_dynrel);
5765 p != NULL;
5766 p = p->next)
5767 {
5768 if (!bfd_is_abs_section (p->sec)
5769 && bfd_is_abs_section (p->sec->output_section))
5770 {
5771 /* Input section has been discarded, either because
5772 it is a copy of a linkonce section or due to
5773 linker script /DISCARD/, so we'll be discarding
5774 the relocs too. */
5775 }
5776 else if (p->count != 0)
5777 {
5778 srel = elf_section_data (p->sec)->sreloc;
5779 srel->_raw_size += p->count * sizeof (Elf64_External_Rela);
5780 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
5781 info->flags |= DF_TEXTREL;
5782 }
5783 }
5784 }
5785
5786 lgot_ents = elf_local_got_ents (ibfd);
5787 if (!lgot_ents)
5788 continue;
5789
5790 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
5791 locsymcount = symtab_hdr->sh_info;
5792 end_lgot_ents = lgot_ents + locsymcount;
5793 lgot_masks = (char *) end_lgot_ents;
5794 s = ppc64_elf_tdata (ibfd)->got;
5795 srel = ppc64_elf_tdata (ibfd)->relgot;
5796 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
5797 {
5798 struct got_entry *ent;
5799
5800 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
5801 if (ent->got.refcount > 0)
5802 {
5803 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
5804 {
5805 if (ppc64_tlsld_got (ibfd)->offset == (bfd_vma) -1)
5806 {
5807 ppc64_tlsld_got (ibfd)->offset = s->_raw_size;
5808 s->_raw_size += 16;
5809 if (info->shared)
5810 srel->_raw_size += sizeof (Elf64_External_Rela);
5811 }
5812 ent->got.offset = ppc64_tlsld_got (ibfd)->offset;
5813 }
5814 else
5815 {
5816 ent->got.offset = s->_raw_size;
5817 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
5818 {
5819 s->_raw_size += 16;
5820 if (info->shared)
5821 srel->_raw_size += 2 * sizeof (Elf64_External_Rela);
5822 }
5823 else
5824 {
5825 s->_raw_size += 8;
5826 if (info->shared)
5827 srel->_raw_size += sizeof (Elf64_External_Rela);
5828 }
5829 }
5830 }
5831 else
5832 ent->got.offset = (bfd_vma) -1;
5833 }
5834 }
5835
5836 /* Allocate global sym .plt and .got entries, and space for global
5837 sym dynamic relocs. */
5838 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
5839
5840 /* We now have determined the sizes of the various dynamic sections.
5841 Allocate memory for them. */
5842 relocs = FALSE;
5843 for (s = dynobj->sections; s != NULL; s = s->next)
5844 {
5845 if ((s->flags & SEC_LINKER_CREATED) == 0)
5846 continue;
5847
5848 /* Reset _cooked_size since prelim layout will set it wrongly,
5849 and a non-zero _cooked_size sticks. */
5850 s->_cooked_size = 0;
5851
5852 if (s == htab->brlt || s == htab->relbrlt)
5853 /* These haven't been allocated yet; don't strip. */
5854 continue;
5855 else if (s == htab->got
5856 || s == htab->plt
5857 || s == htab->glink)
5858 {
5859 /* Strip this section if we don't need it; see the
5860 comment below. */
5861 }
5862 else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
5863 {
5864 if (s->_raw_size == 0)
5865 {
5866 /* If we don't need this section, strip it from the
5867 output file. This is mostly to handle .rela.bss and
5868 .rela.plt. We must create both sections in
5869 create_dynamic_sections, because they must be created
5870 before the linker maps input sections to output
5871 sections. The linker does that before
5872 adjust_dynamic_symbol is called, and it is that
5873 function which decides whether anything needs to go
5874 into these sections. */
5875 }
5876 else
5877 {
5878 if (s != htab->relplt)
5879 relocs = TRUE;
5880
5881 /* We use the reloc_count field as a counter if we need
5882 to copy relocs into the output file. */
5883 s->reloc_count = 0;
5884 }
5885 }
5886 else
5887 {
5888 /* It's not one of our sections, so don't allocate space. */
5889 continue;
5890 }
5891
5892 if (s->_raw_size == 0)
5893 {
5894 _bfd_strip_section_from_output (info, s);
5895 continue;
5896 }
5897
5898 /* .plt is in the bss section. We don't initialise it. */
5899 if ((s->flags & SEC_LOAD) == 0)
5900 continue;
5901
5902 /* Allocate memory for the section contents. We use bfd_zalloc
5903 here in case unused entries are not reclaimed before the
5904 section's contents are written out. This should not happen,
5905 but this way if it does we get a R_PPC64_NONE reloc in .rela
5906 sections instead of garbage.
5907 We also rely on the section contents being zero when writing
5908 the GOT. */
5909 s->contents = bfd_zalloc (dynobj, s->_raw_size);
5910 if (s->contents == NULL)
5911 return FALSE;
5912 }
5913
5914 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
5915 {
5916 s = ppc64_elf_tdata (ibfd)->got;
5917 if (s != NULL && s != htab->got)
5918 {
5919 s->_cooked_size = 0;
5920 if (s->_raw_size == 0)
5921 _bfd_strip_section_from_output (info, s);
5922 else
5923 {
5924 s->contents = bfd_zalloc (ibfd, s->_raw_size);
5925 if (s->contents == NULL)
5926 return FALSE;
5927 }
5928 }
5929 s = ppc64_elf_tdata (ibfd)->relgot;
5930 if (s != NULL)
5931 {
5932 s->_cooked_size = 0;
5933 if (s->_raw_size == 0)
5934 _bfd_strip_section_from_output (info, s);
5935 else
5936 {
5937 s->contents = bfd_zalloc (ibfd, s->_raw_size);
5938 if (s->contents == NULL)
5939 return FALSE;
5940 relocs = TRUE;
5941 s->reloc_count = 0;
5942 }
5943 }
5944 }
5945
5946 if (htab->elf.dynamic_sections_created)
5947 {
5948 /* Add some entries to the .dynamic section. We fill in the
5949 values later, in ppc64_elf_finish_dynamic_sections, but we
5950 must add the entries now so that we get the correct size for
5951 the .dynamic section. The DT_DEBUG entry is filled in by the
5952 dynamic linker and used by the debugger. */
5953 #define add_dynamic_entry(TAG, VAL) \
5954 bfd_elf64_add_dynamic_entry (info, (TAG), (VAL))
5955
5956 if (info->executable)
5957 {
5958 if (!add_dynamic_entry (DT_DEBUG, 0))
5959 return FALSE;
5960 }
5961
5962 if (htab->plt != NULL && htab->plt->_raw_size != 0)
5963 {
5964 if (!add_dynamic_entry (DT_PLTGOT, 0)
5965 || !add_dynamic_entry (DT_PLTRELSZ, 0)
5966 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
5967 || !add_dynamic_entry (DT_JMPREL, 0)
5968 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
5969 return FALSE;
5970 }
5971
5972 if (NO_OPD_RELOCS)
5973 {
5974 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
5975 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
5976 return FALSE;
5977 }
5978
5979 if (relocs)
5980 {
5981 if (!add_dynamic_entry (DT_RELA, 0)
5982 || !add_dynamic_entry (DT_RELASZ, 0)
5983 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
5984 return FALSE;
5985
5986 /* If any dynamic relocs apply to a read-only section,
5987 then we need a DT_TEXTREL entry. */
5988 if ((info->flags & DF_TEXTREL) == 0)
5989 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs, info);
5990
5991 if ((info->flags & DF_TEXTREL) != 0)
5992 {
5993 if (!add_dynamic_entry (DT_TEXTREL, 0))
5994 return FALSE;
5995 }
5996 }
5997 }
5998 #undef add_dynamic_entry
5999
6000 return TRUE;
6001 }
6002
6003 /* Determine the type of stub needed, if any, for a call. */
6004
6005 static inline enum ppc_stub_type
6006 ppc_type_of_stub (asection *input_sec,
6007 const Elf_Internal_Rela *rel,
6008 struct ppc_link_hash_entry **hash,
6009 bfd_vma destination)
6010 {
6011 struct ppc_link_hash_entry *h = *hash;
6012 bfd_vma location;
6013 bfd_vma branch_offset;
6014 bfd_vma max_branch_offset;
6015 enum elf_ppc64_reloc_type r_type;
6016
6017 if (h != NULL)
6018 {
6019 if (h->oh != NULL
6020 && h->oh->dynindx != -1)
6021 {
6022 struct plt_entry *ent;
6023 for (ent = h->oh->plt.plist; ent != NULL; ent = ent->next)
6024 if (ent->addend == rel->r_addend
6025 && ent->plt.offset != (bfd_vma) -1)
6026 {
6027 *hash = (struct ppc_link_hash_entry *) h->oh;
6028 return ppc_stub_plt_call;
6029 }
6030 }
6031
6032 if (h->elf.root.type != bfd_link_hash_defined
6033 && h->elf.root.type != bfd_link_hash_defweak)
6034 return ppc_stub_none;
6035 }
6036
6037 /* Determine where the call point is. */
6038 location = (input_sec->output_offset
6039 + input_sec->output_section->vma
6040 + rel->r_offset);
6041
6042 branch_offset = destination - location;
6043 r_type = ELF64_R_TYPE (rel->r_info);
6044
6045 /* Determine if a long branch stub is needed. */
6046 max_branch_offset = 1 << 25;
6047 if (r_type != R_PPC64_REL24)
6048 max_branch_offset = 1 << 15;
6049
6050 if (branch_offset + max_branch_offset >= 2 * max_branch_offset)
6051 /* We need a stub. Figure out whether a long_branch or plt_branch
6052 is needed later. */
6053 return ppc_stub_long_branch;
6054
6055 return ppc_stub_none;
6056 }
6057
6058 /* Build a .plt call stub. */
6059
6060 static inline bfd_byte *
6061 build_plt_stub (bfd *obfd, bfd_byte *p, int offset)
6062 {
6063 #define PPC_LO(v) ((v) & 0xffff)
6064 #define PPC_HI(v) (((v) >> 16) & 0xffff)
6065 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
6066
6067 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
6068 bfd_put_32 (obfd, STD_R2_40R1, p), p += 4;
6069 bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset), p), p += 4;
6070 if (PPC_HA (offset + 8) != PPC_HA (offset))
6071 bfd_put_32 (obfd, ADDIS_R12_R12 | 1, p), p += 4;
6072 offset += 8;
6073 bfd_put_32 (obfd, LD_R2_0R12 | PPC_LO (offset), p), p += 4;
6074 if (PPC_HA (offset + 8) != PPC_HA (offset))
6075 bfd_put_32 (obfd, ADDIS_R12_R12 | 1, p), p += 4;
6076 offset += 8;
6077 bfd_put_32 (obfd, MTCTR_R11, p), p += 4;
6078 bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset), p), p += 4;
6079 bfd_put_32 (obfd, BCTR, p), p += 4;
6080 return p;
6081 }
6082
6083 static bfd_boolean
6084 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
6085 {
6086 struct ppc_stub_hash_entry *stub_entry;
6087 struct ppc_branch_hash_entry *br_entry;
6088 struct bfd_link_info *info;
6089 struct ppc_link_hash_table *htab;
6090 asection *stub_sec;
6091 bfd *stub_bfd;
6092 bfd_byte *loc;
6093 bfd_byte *p;
6094 unsigned int indx;
6095 struct plt_entry *ent;
6096 bfd_vma off;
6097 int size;
6098
6099 /* Massage our args to the form they really have. */
6100 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
6101 info = in_arg;
6102
6103 htab = ppc_hash_table (info);
6104 stub_sec = stub_entry->stub_sec;
6105
6106 /* Make a note of the offset within the stubs for this entry. */
6107 stub_entry->stub_offset = stub_sec->_cooked_size;
6108 loc = stub_sec->contents + stub_entry->stub_offset;
6109
6110 if (htab->emit_stub_syms)
6111 {
6112 struct elf_link_hash_entry *h;
6113 h = elf_link_hash_lookup (&htab->elf, stub_entry->root.string,
6114 TRUE, FALSE, FALSE);
6115 if (h == NULL)
6116 return FALSE;
6117 h->root.type = bfd_link_hash_defined;
6118 h->root.u.def.section = stub_entry->stub_sec;
6119 h->root.u.def.value = stub_entry->stub_offset;
6120 h->elf_link_hash_flags = (ELF_LINK_HASH_REF_REGULAR
6121 | ELF_LINK_HASH_DEF_REGULAR
6122 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
6123 | ELF_LINK_FORCED_LOCAL);
6124 }
6125
6126 stub_bfd = stub_sec->owner;
6127
6128 htab->stub_count[stub_entry->stub_type - 1] += 1;
6129 switch (stub_entry->stub_type)
6130 {
6131 case ppc_stub_long_branch:
6132 case ppc_stub_long_branch_r2off:
6133 /* Branches are relative. This is where we are going to. */
6134 off = (stub_entry->target_value
6135 + stub_entry->target_section->output_offset
6136 + stub_entry->target_section->output_section->vma);
6137
6138 /* And this is where we are coming from. */
6139 off -= (stub_entry->stub_offset
6140 + stub_sec->output_offset
6141 + stub_sec->output_section->vma);
6142
6143 if (stub_entry->stub_type != ppc_stub_long_branch_r2off)
6144 size = 4;
6145 else
6146 {
6147 bfd_vma r2off;
6148
6149 r2off = (htab->stub_group[stub_entry->target_section->id].toc_off
6150 - htab->stub_group[stub_entry->id_sec->id].toc_off);
6151 bfd_put_32 (stub_bfd, STD_R2_40R1, loc);
6152 loc += 4;
6153 bfd_put_32 (stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
6154 loc += 4;
6155 bfd_put_32 (stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
6156 loc += 4;
6157 off -= 12;
6158 size = 16;
6159 }
6160 bfd_put_32 (stub_bfd, B_DOT | (off & 0x3fffffc), loc);
6161
6162 BFD_ASSERT (off + (1 << 25) < (bfd_vma) (1 << 26));
6163 break;
6164
6165 case ppc_stub_plt_branch:
6166 case ppc_stub_plt_branch_r2off:
6167 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
6168 stub_entry->root.string + 9,
6169 FALSE, FALSE);
6170 if (br_entry == NULL)
6171 {
6172 (*_bfd_error_handler) (_("can't find branch stub `%s'"),
6173 stub_entry->root.string + 9);
6174 htab->stub_error = TRUE;
6175 return FALSE;
6176 }
6177
6178 off = (stub_entry->target_value
6179 + stub_entry->target_section->output_offset
6180 + stub_entry->target_section->output_section->vma);
6181
6182 bfd_put_64 (htab->brlt->owner, off,
6183 htab->brlt->contents + br_entry->offset);
6184
6185 if (info->shared)
6186 {
6187 /* Create a reloc for the branch lookup table entry. */
6188 Elf_Internal_Rela rela;
6189 bfd_byte *rl;
6190
6191 rela.r_offset = (br_entry->offset
6192 + htab->brlt->output_offset
6193 + htab->brlt->output_section->vma);
6194 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
6195 rela.r_addend = off;
6196
6197 rl = htab->relbrlt->contents;
6198 rl += htab->relbrlt->reloc_count++ * sizeof (Elf64_External_Rela);
6199 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
6200 }
6201
6202 off = (br_entry->offset
6203 + htab->brlt->output_offset
6204 + htab->brlt->output_section->vma
6205 - elf_gp (htab->brlt->output_section->owner)
6206 - htab->stub_group[stub_entry->id_sec->id].toc_off);
6207
6208 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
6209 {
6210 (*_bfd_error_handler)
6211 (_("linkage table error against `%s'"),
6212 stub_entry->root.string);
6213 bfd_set_error (bfd_error_bad_value);
6214 htab->stub_error = TRUE;
6215 return FALSE;
6216 }
6217
6218 indx = off;
6219 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
6220 {
6221 bfd_put_32 (stub_bfd, ADDIS_R12_R2 | PPC_HA (indx), loc);
6222 loc += 4;
6223 bfd_put_32 (stub_bfd, LD_R11_0R12 | PPC_LO (indx), loc);
6224 size = 16;
6225 }
6226 else
6227 {
6228 bfd_vma r2off;
6229
6230 r2off = (htab->stub_group[stub_entry->target_section->id].toc_off
6231 - htab->stub_group[stub_entry->id_sec->id].toc_off);
6232 bfd_put_32 (stub_bfd, STD_R2_40R1, loc);
6233 loc += 4;
6234 bfd_put_32 (stub_bfd, ADDIS_R12_R2 | PPC_HA (indx), loc);
6235 loc += 4;
6236 bfd_put_32 (stub_bfd, LD_R11_0R12 | PPC_LO (indx), loc);
6237 loc += 4;
6238 bfd_put_32 (stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
6239 loc += 4;
6240 bfd_put_32 (stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
6241 size = 28;
6242 }
6243 loc += 4;
6244 bfd_put_32 (stub_bfd, MTCTR_R11, loc);
6245 loc += 4;
6246 bfd_put_32 (stub_bfd, BCTR, loc);
6247 break;
6248
6249 case ppc_stub_plt_call:
6250 /* Do the best we can for shared libraries built without
6251 exporting ".foo" for each "foo". This can happen when symbol
6252 versioning scripts strip all bar a subset of symbols. */
6253 if (stub_entry->h->oh->root.type != bfd_link_hash_defined
6254 && stub_entry->h->oh->root.type != bfd_link_hash_defweak)
6255 {
6256 /* Point the symbol at the stub. There may be multiple stubs,
6257 we don't really care; The main thing is to make this sym
6258 defined somewhere. Maybe defining the symbol in the stub
6259 section is a silly idea. If we didn't do this, htab->top_id
6260 could disappear. */
6261 stub_entry->h->oh->root.type = bfd_link_hash_defined;
6262 stub_entry->h->oh->root.u.def.section = stub_entry->stub_sec;
6263 stub_entry->h->oh->root.u.def.value = stub_entry->stub_offset;
6264 }
6265
6266 /* Now build the stub. */
6267 off = (bfd_vma) -1;
6268 for (ent = stub_entry->h->elf.plt.plist; ent != NULL; ent = ent->next)
6269 if (ent->addend == stub_entry->addend)
6270 {
6271 off = ent->plt.offset;
6272 break;
6273 }
6274 if (off >= (bfd_vma) -2)
6275 abort ();
6276
6277 off &= ~ (bfd_vma) 1;
6278 off += (htab->plt->output_offset
6279 + htab->plt->output_section->vma
6280 - elf_gp (htab->plt->output_section->owner)
6281 - htab->stub_group[stub_entry->id_sec->id].toc_off);
6282
6283 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
6284 {
6285 (*_bfd_error_handler)
6286 (_("linkage table error against `%s'"),
6287 stub_entry->h->elf.root.root.string);
6288 bfd_set_error (bfd_error_bad_value);
6289 htab->stub_error = TRUE;
6290 return FALSE;
6291 }
6292
6293 p = build_plt_stub (stub_bfd, loc, off);
6294 size = p - loc;
6295 break;
6296
6297 default:
6298 BFD_FAIL ();
6299 return FALSE;
6300 }
6301
6302 stub_sec->_cooked_size += size;
6303 return TRUE;
6304 }
6305
6306 /* As above, but don't actually build the stub. Just bump offset so
6307 we know stub section sizes, and select plt_branch stubs where
6308 long_branch stubs won't do. */
6309
6310 static bfd_boolean
6311 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
6312 {
6313 struct ppc_stub_hash_entry *stub_entry;
6314 struct ppc_link_hash_table *htab;
6315 bfd_vma off;
6316 int size;
6317
6318 /* Massage our args to the form they really have. */
6319 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
6320 htab = in_arg;
6321
6322 if (stub_entry->stub_type == ppc_stub_plt_call)
6323 {
6324 struct plt_entry *ent;
6325 off = (bfd_vma) -1;
6326 for (ent = stub_entry->h->elf.plt.plist; ent != NULL; ent = ent->next)
6327 if (ent->addend == stub_entry->addend)
6328 {
6329 off = ent->plt.offset & ~(bfd_vma) 1;
6330 break;
6331 }
6332 if (off >= (bfd_vma) -2)
6333 abort ();
6334 off += (htab->plt->output_offset
6335 + htab->plt->output_section->vma
6336 - elf_gp (htab->plt->output_section->owner)
6337 - htab->stub_group[stub_entry->id_sec->id].toc_off);
6338
6339 size = PLT_CALL_STUB_SIZE;
6340 if (PPC_HA (off + 16) != PPC_HA (off))
6341 size += 4;
6342 }
6343 else
6344 {
6345 /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
6346 variants. */
6347 off = (stub_entry->target_value
6348 + stub_entry->target_section->output_offset
6349 + stub_entry->target_section->output_section->vma);
6350 off -= (stub_entry->stub_sec->_raw_size
6351 + stub_entry->stub_sec->output_offset
6352 + stub_entry->stub_sec->output_section->vma);
6353
6354 /* Reset the stub type from the plt variant in case we now
6355 can reach with a shorter stub. */
6356 if (stub_entry->stub_type >= ppc_stub_plt_branch)
6357 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
6358
6359 size = 4;
6360 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
6361 {
6362 off -= 12;
6363 size = 16;
6364 }
6365
6366 /* If the branch offset if too big, use a ppc_stub_plt_branch. */
6367 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
6368 {
6369 struct ppc_branch_hash_entry *br_entry;
6370
6371 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
6372 stub_entry->root.string + 9,
6373 TRUE, FALSE);
6374 if (br_entry == NULL)
6375 {
6376 (*_bfd_error_handler) (_("can't build branch stub `%s'"),
6377 stub_entry->root.string + 9);
6378 htab->stub_error = TRUE;
6379 return FALSE;
6380 }
6381
6382 if (br_entry->iter != htab->stub_iteration)
6383 {
6384 br_entry->iter = htab->stub_iteration;
6385 br_entry->offset = htab->brlt->_raw_size;
6386 htab->brlt->_raw_size += 8;
6387 }
6388
6389 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
6390 size = 16;
6391 if (stub_entry->stub_type != ppc_stub_plt_branch)
6392 size = 28;
6393 }
6394 }
6395
6396 stub_entry->stub_sec->_raw_size += size;
6397 return TRUE;
6398 }
6399
6400 /* Set up various things so that we can make a list of input sections
6401 for each output section included in the link. Returns -1 on error,
6402 0 when no stubs will be needed, and 1 on success. */
6403
6404 int
6405 ppc64_elf_setup_section_lists (bfd *output_bfd, struct bfd_link_info *info)
6406 {
6407 bfd *input_bfd;
6408 int top_id, top_index, id;
6409 asection *section;
6410 asection **input_list;
6411 bfd_size_type amt;
6412 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6413
6414 if (htab->brlt == NULL)
6415 return 0;
6416
6417 /* Find the top input section id. */
6418 for (input_bfd = info->input_bfds, top_id = 3;
6419 input_bfd != NULL;
6420 input_bfd = input_bfd->link_next)
6421 {
6422 for (section = input_bfd->sections;
6423 section != NULL;
6424 section = section->next)
6425 {
6426 if (top_id < section->id)
6427 top_id = section->id;
6428 }
6429 }
6430
6431 htab->top_id = top_id;
6432 amt = sizeof (struct map_stub) * (top_id + 1);
6433 htab->stub_group = bfd_zmalloc (amt);
6434 if (htab->stub_group == NULL)
6435 return -1;
6436
6437 /* Set toc_off for com, und, abs and ind sections. */
6438 for (id = 0; id < 3; id++)
6439 htab->stub_group[id].toc_off = TOC_BASE_OFF;
6440
6441 elf_gp (output_bfd) = htab->toc_curr = ppc64_elf_toc (output_bfd);
6442
6443 /* We can't use output_bfd->section_count here to find the top output
6444 section index as some sections may have been removed, and
6445 _bfd_strip_section_from_output doesn't renumber the indices. */
6446 for (section = output_bfd->sections, top_index = 0;
6447 section != NULL;
6448 section = section->next)
6449 {
6450 if (top_index < section->index)
6451 top_index = section->index;
6452 }
6453
6454 htab->top_index = top_index;
6455 amt = sizeof (asection *) * (top_index + 1);
6456 input_list = bfd_zmalloc (amt);
6457 htab->input_list = input_list;
6458 if (input_list == NULL)
6459 return -1;
6460
6461 return 1;
6462 }
6463
6464 /* The linker repeatedly calls this function for each TOC input section
6465 and linker generated GOT section. Group input bfds such that the toc
6466 within a group is less than 64k in size. Will break with cute linker
6467 scripts that play games with dot in the output toc section. */
6468
6469 void
6470 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
6471 {
6472 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6473
6474 if (!htab->no_multi_toc)
6475 {
6476 bfd_vma addr = isec->output_offset + isec->output_section->vma;
6477 bfd_vma off = addr - htab->toc_curr;
6478 if (off + isec->_raw_size > 0x10000)
6479 {
6480 htab->toc_curr = addr;
6481 htab->multi_toc_needed = 1;
6482 }
6483 elf_gp (isec->owner) = (htab->toc_curr
6484 - elf_gp (isec->output_section->owner)
6485 + TOC_BASE_OFF);
6486 }
6487 }
6488
6489 /* Called after the last call to the above function. */
6490
6491 void
6492 ppc64_elf_reinit_toc (bfd *output_bfd ATTRIBUTE_UNUSED,
6493 struct bfd_link_info *info)
6494 {
6495 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6496
6497 /* toc_curr tracks the TOC offset used for code sections below in
6498 ppc64_elf_next_input_section. Start off at 0x8000. */
6499 htab->toc_curr = TOC_BASE_OFF;
6500 }
6501
6502 /* No toc references were found in ISEC. If the code in ISEC makes no
6503 calls, then there's no need to use toc adjusting stubs when branching
6504 into ISEC. Actually, indirect calls from ISEC are OK as they will
6505 load r2. */
6506
6507 static int
6508 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
6509 {
6510 bfd_byte *contents;
6511 bfd_size_type i;
6512 int ret;
6513 int branch_ok;
6514
6515 /* We know none of our code bearing sections will need toc stubs. */
6516 if ((isec->flags & SEC_LINKER_CREATED) != 0)
6517 return 0;
6518
6519 /* Hack for linux kernel. .fixup contains branches, but only back to
6520 the function that hit an exception. */
6521 branch_ok = strcmp (isec->name, ".fixup") == 0;
6522
6523 contents = elf_section_data (isec)->this_hdr.contents;
6524 if (contents == NULL)
6525 {
6526 contents = bfd_malloc (isec->_raw_size);
6527 if (contents == NULL)
6528 return -1;
6529 if (! bfd_get_section_contents (isec->owner, isec, contents,
6530 0, isec->_raw_size))
6531 {
6532 free (contents);
6533 return -1;
6534 }
6535 if (info->keep_memory)
6536 elf_section_data (isec)->this_hdr.contents = contents;
6537 }
6538
6539 /* Code scan, because we don't necessarily have relocs on calls to
6540 static functions. */
6541 ret = 0;
6542 for (i = 0; i < isec->_raw_size; i += 4)
6543 {
6544 unsigned long insn = bfd_get_32 (isec->owner, contents + i);
6545 /* Is this a branch? */
6546 if ((insn & (0x3f << 26)) == (18 << 26)
6547 /* If branch and link, it's a function call. */
6548 && ((insn & 1) != 0
6549 /* Sibling calls use a plain branch. I don't know a way
6550 of deciding whether a branch is really a sibling call. */
6551 || !branch_ok))
6552 {
6553 ret = 1;
6554 break;
6555 }
6556 }
6557
6558 if (elf_section_data (isec)->this_hdr.contents != contents)
6559 free (contents);
6560 return ret;
6561 }
6562
6563 /* The linker repeatedly calls this function for each input section,
6564 in the order that input sections are linked into output sections.
6565 Build lists of input sections to determine groupings between which
6566 we may insert linker stubs. */
6567
6568 bfd_boolean
6569 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
6570 {
6571 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6572 int ret;
6573
6574 if ((isec->output_section->flags & SEC_CODE) != 0
6575 && isec->output_section->index <= htab->top_index)
6576 {
6577 asection **list = htab->input_list + isec->output_section->index;
6578 /* Steal the link_sec pointer for our list. */
6579 #define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
6580 /* This happens to make the list in reverse order,
6581 which is what we want. */
6582 PREV_SEC (isec) = *list;
6583 *list = isec;
6584 }
6585
6586 /* If a code section has a function that uses the TOC then we need
6587 to use the right TOC (obviously). Also, make sure that .opd gets
6588 the correct TOC value. */
6589 if (isec->has_gp_reloc || (isec->flags & SEC_CODE) == 0)
6590 {
6591 if (elf_gp (isec->owner) != 0)
6592 htab->toc_curr = elf_gp (isec->owner);
6593 }
6594 else if ((ret = toc_adjusting_stub_needed (info, isec)) < 0)
6595 return FALSE;
6596 else
6597 isec->has_gp_reloc = ret;
6598
6599 /* Functions that don't use the TOC can belong in any TOC group.
6600 Use the last TOC base. This happens to make _init and _fini
6601 pasting work. */
6602 htab->stub_group[isec->id].toc_off = htab->toc_curr;
6603 return TRUE;
6604 }
6605
6606 /* See whether we can group stub sections together. Grouping stub
6607 sections may result in fewer stubs. More importantly, we need to
6608 put all .init* and .fini* stubs at the beginning of the .init or
6609 .fini output sections respectively, because glibc splits the
6610 _init and _fini functions into multiple parts. Putting a stub in
6611 the middle of a function is not a good idea. */
6612
6613 static void
6614 group_sections (struct ppc_link_hash_table *htab,
6615 bfd_size_type stub_group_size,
6616 bfd_boolean stubs_always_before_branch)
6617 {
6618 asection **list = htab->input_list + htab->top_index;
6619 do
6620 {
6621 asection *tail = *list;
6622 while (tail != NULL)
6623 {
6624 asection *curr;
6625 asection *prev;
6626 bfd_size_type total;
6627 bfd_boolean big_sec;
6628 bfd_vma curr_toc;
6629
6630 curr = tail;
6631 if (tail->_cooked_size)
6632 total = tail->_cooked_size;
6633 else
6634 total = tail->_raw_size;
6635 big_sec = total >= stub_group_size;
6636 curr_toc = htab->stub_group[tail->id].toc_off;
6637
6638 while ((prev = PREV_SEC (curr)) != NULL
6639 && ((total += curr->output_offset - prev->output_offset)
6640 < stub_group_size)
6641 && htab->stub_group[prev->id].toc_off == curr_toc)
6642 curr = prev;
6643
6644 /* OK, the size from the start of CURR to the end is less
6645 than stub_group_size and thus can be handled by one stub
6646 section. (or the tail section is itself larger than
6647 stub_group_size, in which case we may be toast.) We
6648 should really be keeping track of the total size of stubs
6649 added here, as stubs contribute to the final output
6650 section size. That's a little tricky, and this way will
6651 only break if stubs added make the total size more than
6652 2^25, ie. for the default stub_group_size, if stubs total
6653 more than 2097152 bytes, or nearly 75000 plt call stubs. */
6654 do
6655 {
6656 prev = PREV_SEC (tail);
6657 /* Set up this stub group. */
6658 htab->stub_group[tail->id].link_sec = curr;
6659 }
6660 while (tail != curr && (tail = prev) != NULL);
6661
6662 /* But wait, there's more! Input sections up to stub_group_size
6663 bytes before the stub section can be handled by it too.
6664 Don't do this if we have a really large section after the
6665 stubs, as adding more stubs increases the chance that
6666 branches may not reach into the stub section. */
6667 if (!stubs_always_before_branch && !big_sec)
6668 {
6669 total = 0;
6670 while (prev != NULL
6671 && ((total += tail->output_offset - prev->output_offset)
6672 < stub_group_size)
6673 && htab->stub_group[prev->id].toc_off == curr_toc)
6674 {
6675 tail = prev;
6676 prev = PREV_SEC (tail);
6677 htab->stub_group[tail->id].link_sec = curr;
6678 }
6679 }
6680 tail = prev;
6681 }
6682 }
6683 while (list-- != htab->input_list);
6684 free (htab->input_list);
6685 #undef PREV_SEC
6686 }
6687
6688 /* Determine and set the size of the stub section for a final link.
6689
6690 The basic idea here is to examine all the relocations looking for
6691 PC-relative calls to a target that is unreachable with a "bl"
6692 instruction. */
6693
6694 bfd_boolean
6695 ppc64_elf_size_stubs (bfd *output_bfd,
6696 struct bfd_link_info *info,
6697 bfd_signed_vma group_size,
6698 asection *(*add_stub_section) (const char *, asection *),
6699 void (*layout_sections_again) (void))
6700 {
6701 bfd_size_type stub_group_size;
6702 bfd_boolean stubs_always_before_branch;
6703 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6704
6705 /* Stash our params away. */
6706 htab->add_stub_section = add_stub_section;
6707 htab->layout_sections_again = layout_sections_again;
6708 stubs_always_before_branch = group_size < 0;
6709 if (group_size < 0)
6710 stub_group_size = -group_size;
6711 else
6712 stub_group_size = group_size;
6713 if (stub_group_size == 1)
6714 {
6715 /* Default values. */
6716 if (stubs_always_before_branch)
6717 {
6718 stub_group_size = 0x1e00000;
6719 if (htab->has_14bit_branch)
6720 stub_group_size = 0x7800;
6721 }
6722 else
6723 {
6724 stub_group_size = 0x1c00000;
6725 if (htab->has_14bit_branch)
6726 stub_group_size = 0x7000;
6727 }
6728 }
6729
6730 group_sections (htab, stub_group_size, stubs_always_before_branch);
6731
6732 while (1)
6733 {
6734 bfd *input_bfd;
6735 unsigned int bfd_indx;
6736 asection *stub_sec;
6737 bfd_boolean stub_changed;
6738
6739 htab->stub_iteration += 1;
6740 stub_changed = FALSE;
6741
6742 for (input_bfd = info->input_bfds, bfd_indx = 0;
6743 input_bfd != NULL;
6744 input_bfd = input_bfd->link_next, bfd_indx++)
6745 {
6746 Elf_Internal_Shdr *symtab_hdr;
6747 asection *section;
6748 Elf_Internal_Sym *local_syms = NULL;
6749
6750 /* We'll need the symbol table in a second. */
6751 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
6752 if (symtab_hdr->sh_info == 0)
6753 continue;
6754
6755 /* Walk over each section attached to the input bfd. */
6756 for (section = input_bfd->sections;
6757 section != NULL;
6758 section = section->next)
6759 {
6760 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
6761
6762 /* If there aren't any relocs, then there's nothing more
6763 to do. */
6764 if ((section->flags & SEC_RELOC) == 0
6765 || section->reloc_count == 0)
6766 continue;
6767
6768 /* If this section is a link-once section that will be
6769 discarded, then don't create any stubs. */
6770 if (section->output_section == NULL
6771 || section->output_section->owner != output_bfd)
6772 continue;
6773
6774 /* Get the relocs. */
6775 internal_relocs
6776 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
6777 info->keep_memory);
6778 if (internal_relocs == NULL)
6779 goto error_ret_free_local;
6780
6781 /* Now examine each relocation. */
6782 irela = internal_relocs;
6783 irelaend = irela + section->reloc_count;
6784 for (; irela < irelaend; irela++)
6785 {
6786 enum elf_ppc64_reloc_type r_type;
6787 unsigned int r_indx;
6788 enum ppc_stub_type stub_type;
6789 struct ppc_stub_hash_entry *stub_entry;
6790 asection *sym_sec;
6791 bfd_vma sym_value;
6792 bfd_vma destination;
6793 struct ppc_link_hash_entry *hash;
6794 struct elf_link_hash_entry *h;
6795 Elf_Internal_Sym *sym;
6796 char *stub_name;
6797 const asection *id_sec;
6798
6799 r_type = ELF64_R_TYPE (irela->r_info);
6800 r_indx = ELF64_R_SYM (irela->r_info);
6801
6802 if (r_type >= R_PPC64_max)
6803 {
6804 bfd_set_error (bfd_error_bad_value);
6805 goto error_ret_free_internal;
6806 }
6807
6808 /* Only look for stubs on branch instructions. */
6809 if (r_type != R_PPC64_REL24
6810 && r_type != R_PPC64_REL14
6811 && r_type != R_PPC64_REL14_BRTAKEN
6812 && r_type != R_PPC64_REL14_BRNTAKEN)
6813 continue;
6814
6815 /* Now determine the call target, its name, value,
6816 section. */
6817 destination = 0;
6818 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
6819 r_indx, input_bfd))
6820 goto error_ret_free_internal;
6821 hash = (struct ppc_link_hash_entry *) h;
6822
6823 if (hash == NULL)
6824 {
6825 /* It's a local symbol. */
6826 sym_value = sym->st_value;
6827 destination = (sym_value + irela->r_addend
6828 + sym_sec->output_offset
6829 + sym_sec->output_section->vma);
6830 }
6831 else
6832 {
6833 /* It's an external symbol. */
6834 sym_value = 0;
6835 if (hash->elf.root.type == bfd_link_hash_defined
6836 || hash->elf.root.type == bfd_link_hash_defweak)
6837 {
6838 sym_value = hash->elf.root.u.def.value;
6839 if (sym_sec->output_section != NULL)
6840 destination = (sym_value + irela->r_addend
6841 + sym_sec->output_offset
6842 + sym_sec->output_section->vma);
6843 }
6844 else if (hash->elf.root.type == bfd_link_hash_undefweak)
6845 ;
6846 else if (hash->elf.root.type == bfd_link_hash_undefined)
6847 ;
6848 else
6849 {
6850 bfd_set_error (bfd_error_bad_value);
6851 goto error_ret_free_internal;
6852 }
6853 }
6854
6855 /* Determine what (if any) linker stub is needed. */
6856 stub_type = ppc_type_of_stub (section, irela, &hash,
6857 destination);
6858
6859 if (stub_type != ppc_stub_plt_call)
6860 {
6861 /* Check whether we need a TOC adjusting stub.
6862 Since the linker pastes together pieces from
6863 different object files when creating the
6864 _init and _fini functions, it may be that a
6865 call to what looks like a local sym is in
6866 fact a call needing a TOC adjustment. */
6867 if (sym_sec != NULL
6868 && sym_sec->output_section != NULL
6869 && (htab->stub_group[sym_sec->id].toc_off
6870 != htab->stub_group[section->id].toc_off)
6871 && sym_sec->has_gp_reloc
6872 && section->has_gp_reloc)
6873 stub_type = ppc_stub_long_branch_r2off;
6874 }
6875
6876 if (stub_type == ppc_stub_none)
6877 continue;
6878
6879 /* __tls_get_addr calls might be eliminated. */
6880 if (stub_type != ppc_stub_plt_call
6881 && hash != NULL
6882 && &hash->elf == htab->tls_get_addr
6883 && section->has_tls_reloc
6884 && irela != internal_relocs)
6885 {
6886 /* Get tls info. */
6887 char *tls_mask;
6888
6889 if (!get_tls_mask (&tls_mask, &local_syms,
6890 irela - 1, input_bfd))
6891 goto error_ret_free_internal;
6892 if (*tls_mask != 0)
6893 continue;
6894 }
6895
6896 /* Support for grouping stub sections. */
6897 id_sec = htab->stub_group[section->id].link_sec;
6898
6899 /* Get the name of this stub. */
6900 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
6901 if (!stub_name)
6902 goto error_ret_free_internal;
6903
6904 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
6905 stub_name, FALSE, FALSE);
6906 if (stub_entry != NULL)
6907 {
6908 /* The proper stub has already been created. */
6909 free (stub_name);
6910 continue;
6911 }
6912
6913 stub_entry = ppc_add_stub (stub_name, section, htab);
6914 if (stub_entry == NULL)
6915 {
6916 free (stub_name);
6917 error_ret_free_internal:
6918 if (elf_section_data (section)->relocs == NULL)
6919 free (internal_relocs);
6920 error_ret_free_local:
6921 if (local_syms != NULL
6922 && (symtab_hdr->contents
6923 != (unsigned char *) local_syms))
6924 free (local_syms);
6925 return FALSE;
6926 }
6927
6928 stub_entry->stub_type = stub_type;
6929 stub_entry->target_value = sym_value;
6930 stub_entry->target_section = sym_sec;
6931 stub_entry->h = hash;
6932 stub_entry->addend = irela->r_addend;
6933 stub_changed = TRUE;
6934 }
6935
6936 /* We're done with the internal relocs, free them. */
6937 if (elf_section_data (section)->relocs != internal_relocs)
6938 free (internal_relocs);
6939 }
6940
6941 if (local_syms != NULL
6942 && symtab_hdr->contents != (unsigned char *) local_syms)
6943 {
6944 if (!info->keep_memory)
6945 free (local_syms);
6946 else
6947 symtab_hdr->contents = (unsigned char *) local_syms;
6948 }
6949 }
6950
6951 if (!stub_changed)
6952 break;
6953
6954 /* OK, we've added some stubs. Find out the new size of the
6955 stub sections. */
6956 for (stub_sec = htab->stub_bfd->sections;
6957 stub_sec != NULL;
6958 stub_sec = stub_sec->next)
6959 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
6960 {
6961 stub_sec->_raw_size = 0;
6962 stub_sec->_cooked_size = 0;
6963 }
6964 htab->brlt->_raw_size = 0;
6965 htab->brlt->_cooked_size = 0;
6966
6967 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, htab);
6968
6969 /* Ask the linker to do its stuff. */
6970 (*htab->layout_sections_again) ();
6971 }
6972
6973 /* It would be nice to strip .branch_lt from the output if the
6974 section is empty, but it's too late. If we strip sections here,
6975 the dynamic symbol table is corrupted since the section symbol
6976 for the stripped section isn't written. */
6977
6978 return TRUE;
6979 }
6980
6981 /* Called after we have determined section placement. If sections
6982 move, we'll be called again. Provide a value for TOCstart. */
6983
6984 bfd_vma
6985 ppc64_elf_toc (bfd *obfd)
6986 {
6987 asection *s;
6988 bfd_vma TOCstart;
6989
6990 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
6991 order. The TOC starts where the first of these sections starts. */
6992 s = bfd_get_section_by_name (obfd, ".got");
6993 if (s == NULL)
6994 s = bfd_get_section_by_name (obfd, ".toc");
6995 if (s == NULL)
6996 s = bfd_get_section_by_name (obfd, ".tocbss");
6997 if (s == NULL)
6998 s = bfd_get_section_by_name (obfd, ".plt");
6999 if (s == NULL)
7000 {
7001 /* This may happen for
7002 o references to TOC base (SYM@toc / TOC[tc0]) without a
7003 .toc directive
7004 o bad linker script
7005 o --gc-sections and empty TOC sections
7006
7007 FIXME: Warn user? */
7008
7009 /* Look for a likely section. We probably won't even be
7010 using TOCstart. */
7011 for (s = obfd->sections; s != NULL; s = s->next)
7012 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY))
7013 == (SEC_ALLOC | SEC_SMALL_DATA))
7014 break;
7015 if (s == NULL)
7016 for (s = obfd->sections; s != NULL; s = s->next)
7017 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA))
7018 == (SEC_ALLOC | SEC_SMALL_DATA))
7019 break;
7020 if (s == NULL)
7021 for (s = obfd->sections; s != NULL; s = s->next)
7022 if ((s->flags & (SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
7023 break;
7024 if (s == NULL)
7025 for (s = obfd->sections; s != NULL; s = s->next)
7026 if ((s->flags & SEC_ALLOC) == SEC_ALLOC)
7027 break;
7028 }
7029
7030 TOCstart = 0;
7031 if (s != NULL)
7032 TOCstart = s->output_section->vma + s->output_offset;
7033
7034 return TOCstart;
7035 }
7036
7037 /* Build all the stubs associated with the current output file.
7038 The stubs are kept in a hash table attached to the main linker
7039 hash table. This function is called via gldelf64ppc_finish. */
7040
7041 bfd_boolean
7042 ppc64_elf_build_stubs (bfd_boolean emit_stub_syms,
7043 struct bfd_link_info *info,
7044 char **stats)
7045 {
7046 struct ppc_link_hash_table *htab = ppc_hash_table (info);
7047 asection *stub_sec;
7048 bfd_byte *p;
7049 int stub_sec_count = 0;
7050
7051 htab->emit_stub_syms = emit_stub_syms;
7052 for (stub_sec = htab->stub_bfd->sections;
7053 stub_sec != NULL;
7054 stub_sec = stub_sec->next)
7055 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
7056 {
7057 bfd_size_type size;
7058
7059 /* Allocate memory to hold the linker stubs. */
7060 size = stub_sec->_raw_size;
7061 if (size != 0)
7062 {
7063 stub_sec->contents = bfd_zalloc (htab->stub_bfd, size);
7064 if (stub_sec->contents == NULL)
7065 return FALSE;
7066 }
7067 stub_sec->_cooked_size = 0;
7068 }
7069
7070 if (htab->plt != NULL)
7071 {
7072 unsigned int indx;
7073 bfd_vma plt0;
7074
7075 /* Build the .glink plt call stub. */
7076 plt0 = (htab->plt->output_section->vma
7077 + htab->plt->output_offset
7078 - (htab->glink->output_section->vma
7079 + htab->glink->output_offset
7080 + GLINK_CALL_STUB_SIZE));
7081 if (plt0 + 0x80008000 > 0xffffffff)
7082 {
7083 (*_bfd_error_handler) (_(".glink and .plt too far apart"));
7084 bfd_set_error (bfd_error_bad_value);
7085 return FALSE;
7086 }
7087
7088 p = htab->glink->contents;
7089 bfd_put_32 (htab->glink->owner, MFCTR_R12, p);
7090 p += 4;
7091 bfd_put_32 (htab->glink->owner, SLDI_R11_R0_3, p);
7092 p += 4;
7093 bfd_put_32 (htab->glink->owner, ADDIC_R2_R0_32K, p);
7094 p += 4;
7095 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
7096 p += 4;
7097 bfd_put_32 (htab->glink->owner, SRADI_R2_R2_63, p);
7098 p += 4;
7099 bfd_put_32 (htab->glink->owner, SLDI_R11_R0_2, p);
7100 p += 4;
7101 bfd_put_32 (htab->glink->owner, AND_R2_R2_R11, p);
7102 p += 4;
7103 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
7104 p += 4;
7105 bfd_put_32 (htab->glink->owner, ADD_R12_R12_R2, p);
7106 p += 4;
7107 bfd_put_32 (htab->glink->owner, ADDIS_R12_R12 | PPC_HA (plt0), p);
7108 p += 4;
7109 bfd_put_32 (htab->glink->owner, LD_R11_0R12 | PPC_LO (plt0), p);
7110 p += 4;
7111 bfd_put_32 (htab->glink->owner, ADDI_R12_R12 | PPC_LO (plt0), p);
7112 p += 4;
7113 bfd_put_32 (htab->glink->owner, LD_R2_0R12 | 8, p);
7114 p += 4;
7115 bfd_put_32 (htab->glink->owner, MTCTR_R11, p);
7116 p += 4;
7117 bfd_put_32 (htab->glink->owner, LD_R11_0R12 | 16, p);
7118 p += 4;
7119 bfd_put_32 (htab->glink->owner, BCTR, p);
7120 p += 4;
7121
7122 /* Build the .glink lazy link call stubs. */
7123 indx = 0;
7124 while (p < htab->glink->contents + htab->glink->_raw_size)
7125 {
7126 if (indx < 0x8000)
7127 {
7128 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
7129 p += 4;
7130 }
7131 else
7132 {
7133 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
7134 p += 4;
7135 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx), p);
7136 p += 4;
7137 }
7138 bfd_put_32 (htab->glink->owner,
7139 B_DOT | ((htab->glink->contents - p) & 0x3fffffc), p);
7140 indx++;
7141 p += 4;
7142 }
7143 htab->glink->_cooked_size = p - htab->glink->contents;
7144 }
7145
7146 if (htab->brlt->_raw_size != 0)
7147 {
7148 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
7149 htab->brlt->_raw_size);
7150 if (htab->brlt->contents == NULL)
7151 return FALSE;
7152 }
7153
7154 /* Build the stubs as directed by the stub hash table. */
7155 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
7156
7157 for (stub_sec = htab->stub_bfd->sections;
7158 stub_sec != NULL;
7159 stub_sec = stub_sec->next)
7160 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
7161 {
7162 stub_sec_count += 1;
7163 if (stub_sec->_raw_size != stub_sec->_cooked_size)
7164 break;
7165 }
7166
7167 if (stub_sec != NULL
7168 || htab->glink->_raw_size != htab->glink->_cooked_size)
7169 {
7170 htab->stub_error = TRUE;
7171 (*_bfd_error_handler) (_("stubs don't match calculated size"));
7172 }
7173
7174 if (htab->stub_error)
7175 return FALSE;
7176
7177 if (stats != NULL)
7178 {
7179 *stats = bfd_malloc (500);
7180 if (*stats == NULL)
7181 return FALSE;
7182
7183 sprintf (*stats, _("linker stubs in %u groups\n"
7184 " branch %lu\n"
7185 " toc adjust %lu\n"
7186 " long branch %lu\n"
7187 " long toc adj %lu\n"
7188 " plt call %lu"),
7189 stub_sec_count,
7190 htab->stub_count[ppc_stub_long_branch - 1],
7191 htab->stub_count[ppc_stub_long_branch_r2off - 1],
7192 htab->stub_count[ppc_stub_plt_branch - 1],
7193 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
7194 htab->stub_count[ppc_stub_plt_call - 1]);
7195 }
7196 return TRUE;
7197 }
7198
7199 /* The RELOCATE_SECTION function is called by the ELF backend linker
7200 to handle the relocations for a section.
7201
7202 The relocs are always passed as Rela structures; if the section
7203 actually uses Rel structures, the r_addend field will always be
7204 zero.
7205
7206 This function is responsible for adjust the section contents as
7207 necessary, and (if using Rela relocs and generating a
7208 relocatable output file) adjusting the reloc addend as
7209 necessary.
7210
7211 This function does not have to worry about setting the reloc
7212 address or the reloc symbol index.
7213
7214 LOCAL_SYMS is a pointer to the swapped in local symbols.
7215
7216 LOCAL_SECTIONS is an array giving the section in the input file
7217 corresponding to the st_shndx field of each local symbol.
7218
7219 The global hash table entry for the global symbols can be found
7220 via elf_sym_hashes (input_bfd).
7221
7222 When generating relocatable output, this function must handle
7223 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
7224 going to be the section symbol corresponding to the output
7225 section, which means that the addend must be adjusted
7226 accordingly. */
7227
7228 static bfd_boolean
7229 ppc64_elf_relocate_section (bfd *output_bfd,
7230 struct bfd_link_info *info,
7231 bfd *input_bfd,
7232 asection *input_section,
7233 bfd_byte *contents,
7234 Elf_Internal_Rela *relocs,
7235 Elf_Internal_Sym *local_syms,
7236 asection **local_sections)
7237 {
7238 struct ppc_link_hash_table *htab;
7239 Elf_Internal_Shdr *symtab_hdr;
7240 struct elf_link_hash_entry **sym_hashes;
7241 Elf_Internal_Rela *rel;
7242 Elf_Internal_Rela *relend;
7243 Elf_Internal_Rela outrel;
7244 bfd_byte *loc;
7245 struct got_entry **local_got_ents;
7246 bfd_vma TOCstart;
7247 bfd_boolean ret = TRUE;
7248 bfd_boolean is_opd;
7249 /* Disabled until we sort out how ld should choose 'y' vs 'at'. */
7250 bfd_boolean is_power4 = FALSE;
7251
7252 if (info->relocatable)
7253 return TRUE;
7254
7255 /* Initialize howto table if needed. */
7256 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
7257 ppc_howto_init ();
7258
7259 htab = ppc_hash_table (info);
7260 local_got_ents = elf_local_got_ents (input_bfd);
7261 TOCstart = elf_gp (output_bfd);
7262 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
7263 sym_hashes = elf_sym_hashes (input_bfd);
7264 is_opd = ppc64_elf_section_data (input_section)->opd.adjust != NULL;
7265
7266 rel = relocs;
7267 relend = relocs + input_section->reloc_count;
7268 for (; rel < relend; rel++)
7269 {
7270 enum elf_ppc64_reloc_type r_type;
7271 bfd_vma addend;
7272 bfd_reloc_status_type r;
7273 Elf_Internal_Sym *sym;
7274 asection *sec;
7275 struct elf_link_hash_entry *h;
7276 struct elf_link_hash_entry *fdh;
7277 const char *sym_name;
7278 unsigned long r_symndx;
7279 char tls_mask, tls_gd, tls_type;
7280 bfd_vma relocation;
7281 bfd_boolean unresolved_reloc;
7282 bfd_boolean warned;
7283 long insn, mask;
7284 struct ppc_stub_hash_entry *stub_entry;
7285 bfd_vma max_br_offset;
7286 bfd_vma from;
7287
7288 r_type = ELF64_R_TYPE (rel->r_info);
7289 r_symndx = ELF64_R_SYM (rel->r_info);
7290 sym = NULL;
7291 sec = NULL;
7292 h = NULL;
7293 sym_name = NULL;
7294 unresolved_reloc = FALSE;
7295 warned = FALSE;
7296
7297 if (r_symndx < symtab_hdr->sh_info)
7298 {
7299 /* It's a local symbol. */
7300 sym = local_syms + r_symndx;
7301 sec = local_sections[r_symndx];
7302 sym_name = bfd_elf_local_sym_name (input_bfd, sym);
7303 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, sec, rel);
7304 if (elf_section_data (sec) != NULL)
7305 {
7306 long *opd_sym_adjust;
7307
7308 opd_sym_adjust = ppc64_elf_section_data (sec)->opd.adjust;
7309 if (opd_sym_adjust != NULL && sym->st_value % 24 == 0)
7310 relocation += opd_sym_adjust[sym->st_value / 24];
7311 }
7312 }
7313 else
7314 {
7315 /* It's a global symbol. */
7316 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7317 while (h->root.type == bfd_link_hash_indirect
7318 || h->root.type == bfd_link_hash_warning)
7319 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7320 sym_name = h->root.root.string;
7321 relocation = 0;
7322 if (h->root.type == bfd_link_hash_defined
7323 || h->root.type == bfd_link_hash_defweak)
7324 {
7325 sec = h->root.u.def.section;
7326 if (sec->output_section == NULL)
7327 /* Set a flag that will be cleared later if we find a
7328 relocation value for this symbol. output_section
7329 is typically NULL for symbols satisfied by a shared
7330 library. */
7331 unresolved_reloc = TRUE;
7332 else
7333 relocation = (h->root.u.def.value
7334 + sec->output_section->vma
7335 + sec->output_offset);
7336 }
7337 else if (h->root.type == bfd_link_hash_undefweak)
7338 ;
7339 else if (!info->executable
7340 && !info->no_undefined
7341 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
7342 ;
7343 else
7344 {
7345 if (! ((*info->callbacks->undefined_symbol)
7346 (info, h->root.root.string, input_bfd, input_section,
7347 rel->r_offset, (!info->shared
7348 || info->no_undefined
7349 || ELF_ST_VISIBILITY (h->other)))))
7350 return FALSE;
7351 warned = TRUE;
7352 }
7353 }
7354
7355 /* TLS optimizations. Replace instruction sequences and relocs
7356 based on information we collected in tls_optimize. We edit
7357 RELOCS so that --emit-relocs will output something sensible
7358 for the final instruction stream. */
7359 tls_mask = 0;
7360 tls_gd = 0;
7361 if (IS_PPC64_TLS_RELOC (r_type))
7362 {
7363 if (h != NULL)
7364 tls_mask = ((struct ppc_link_hash_entry *) h)->tls_mask;
7365 else if (local_got_ents != NULL)
7366 {
7367 char *lgot_masks;
7368 lgot_masks = (char *) (local_got_ents + symtab_hdr->sh_info);
7369 tls_mask = lgot_masks[r_symndx];
7370 }
7371 }
7372
7373 /* Ensure reloc mapping code below stays sane. */
7374 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
7375 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
7376 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
7377 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
7378 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
7379 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
7380 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
7381 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
7382 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
7383 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
7384 abort ();
7385 switch (r_type)
7386 {
7387 default:
7388 break;
7389
7390 case R_PPC64_TOC16:
7391 case R_PPC64_TOC16_LO:
7392 case R_PPC64_TOC16_DS:
7393 case R_PPC64_TOC16_LO_DS:
7394 {
7395 /* Check for toc tls entries. */
7396 char *toc_tls;
7397 int retval;
7398
7399 retval = get_tls_mask (&toc_tls, &local_syms, rel, input_bfd);
7400 if (retval == 0)
7401 return FALSE;
7402
7403 if (toc_tls)
7404 {
7405 tls_mask = *toc_tls;
7406 if (r_type == R_PPC64_TOC16_DS
7407 || r_type == R_PPC64_TOC16_LO_DS)
7408 {
7409 if (tls_mask != 0
7410 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
7411 goto toctprel;
7412 }
7413 else
7414 {
7415 /* If we found a GD reloc pair, then we might be
7416 doing a GD->IE transition. */
7417 if (retval == 2)
7418 {
7419 tls_gd = TLS_TPRELGD;
7420 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
7421 goto tls_get_addr_check;
7422 }
7423 else if (retval == 3)
7424 {
7425 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
7426 goto tls_get_addr_check;
7427 }
7428 }
7429 }
7430 }
7431 break;
7432
7433 case R_PPC64_GOT_TPREL16_DS:
7434 case R_PPC64_GOT_TPREL16_LO_DS:
7435 if (tls_mask != 0
7436 && (tls_mask & TLS_TPREL) == 0)
7437 {
7438 bfd_vma insn;
7439 toctprel:
7440 insn = bfd_get_32 (output_bfd, contents + rel->r_offset - 2);
7441 insn &= 31 << 21;
7442 insn |= 0x3c0d0000; /* addis 0,13,0 */
7443 bfd_put_32 (output_bfd, insn, contents + rel->r_offset - 2);
7444 r_type = R_PPC64_TPREL16_HA;
7445 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
7446 }
7447 break;
7448
7449 case R_PPC64_TLS:
7450 if (tls_mask == 0)
7451 {
7452 /* Check for toc tls entries. */
7453 char *toc_tls;
7454
7455 if (!get_tls_mask (&toc_tls, &local_syms, rel, input_bfd))
7456 return FALSE;
7457
7458 if (toc_tls)
7459 tls_mask = *toc_tls;
7460 }
7461 if (tls_mask != 0
7462 && (tls_mask & TLS_TPREL) == 0)
7463 {
7464 bfd_vma insn, rtra;
7465 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
7466 if ((insn & ((0x3f << 26) | (31 << 11)))
7467 == ((31 << 26) | (13 << 11)))
7468 rtra = insn & ((1 << 26) - (1 << 16));
7469 else if ((insn & ((0x3f << 26) | (31 << 16)))
7470 == ((31 << 26) | (13 << 16)))
7471 rtra = (insn & (31 << 21)) | ((insn & (31 << 11)) << 5);
7472 else
7473 abort ();
7474 if ((insn & ((1 << 11) - (1 << 1))) == 266 << 1)
7475 /* add -> addi. */
7476 insn = 14 << 26;
7477 else if ((insn & (31 << 1)) == 23 << 1
7478 && ((insn & (31 << 6)) < 14 << 6
7479 || ((insn & (31 << 6)) >= 16 << 6
7480 && (insn & (31 << 6)) < 24 << 6)))
7481 /* load and store indexed -> dform. */
7482 insn = (32 | ((insn >> 6) & 31)) << 26;
7483 else if ((insn & (31 << 1)) == 21 << 1
7484 && (insn & (0x1a << 6)) == 0)
7485 /* ldx, ldux, stdx, stdux -> ld, ldu, std, stdu. */
7486 insn = (((58 | ((insn >> 6) & 4)) << 26)
7487 | ((insn >> 6) & 1));
7488 else if ((insn & (31 << 1)) == 21 << 1
7489 && (insn & ((1 << 11) - (1 << 1))) == 341 << 1)
7490 /* lwax -> lwa. */
7491 insn = (58 << 26) | 2;
7492 else
7493 abort ();
7494 insn |= rtra;
7495 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
7496 r_type = R_PPC64_TPREL16_LO;
7497 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
7498 /* Was PPC64_TLS which sits on insn boundary, now
7499 PPC64_TPREL16_LO which is at insn+2. */
7500 rel->r_offset += 2;
7501 }
7502 break;
7503
7504 case R_PPC64_GOT_TLSGD16_HI:
7505 case R_PPC64_GOT_TLSGD16_HA:
7506 tls_gd = TLS_TPRELGD;
7507 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
7508 goto tls_gdld_hi;
7509 break;
7510
7511 case R_PPC64_GOT_TLSLD16_HI:
7512 case R_PPC64_GOT_TLSLD16_HA:
7513 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
7514 {
7515 tls_gdld_hi:
7516 if ((tls_mask & tls_gd) != 0)
7517 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
7518 + R_PPC64_GOT_TPREL16_DS);
7519 else
7520 {
7521 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
7522 rel->r_offset -= 2;
7523 r_type = R_PPC64_NONE;
7524 }
7525 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
7526 }
7527 break;
7528
7529 case R_PPC64_GOT_TLSGD16:
7530 case R_PPC64_GOT_TLSGD16_LO:
7531 tls_gd = TLS_TPRELGD;
7532 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
7533 goto tls_get_addr_check;
7534 break;
7535
7536 case R_PPC64_GOT_TLSLD16:
7537 case R_PPC64_GOT_TLSLD16_LO:
7538 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
7539 {
7540 tls_get_addr_check:
7541 if (rel + 1 < relend)
7542 {
7543 enum elf_ppc64_reloc_type r_type2;
7544 unsigned long r_symndx2;
7545 struct elf_link_hash_entry *h2;
7546 bfd_vma insn1, insn2, insn3;
7547 bfd_vma offset;
7548
7549 /* The next instruction should be a call to
7550 __tls_get_addr. Peek at the reloc to be sure. */
7551 r_type2 = ELF64_R_TYPE (rel[1].r_info);
7552 r_symndx2 = ELF64_R_SYM (rel[1].r_info);
7553 if (r_symndx2 < symtab_hdr->sh_info
7554 || (r_type2 != R_PPC64_REL14
7555 && r_type2 != R_PPC64_REL14_BRTAKEN
7556 && r_type2 != R_PPC64_REL14_BRNTAKEN
7557 && r_type2 != R_PPC64_REL24))
7558 break;
7559
7560 h2 = sym_hashes[r_symndx2 - symtab_hdr->sh_info];
7561 while (h2->root.type == bfd_link_hash_indirect
7562 || h2->root.type == bfd_link_hash_warning)
7563 h2 = (struct elf_link_hash_entry *) h2->root.u.i.link;
7564 if (h2 == NULL || h2 != htab->tls_get_addr)
7565 break;
7566
7567 /* OK, it checks out. Replace the call. */
7568 offset = rel[1].r_offset;
7569 insn1 = bfd_get_32 (output_bfd,
7570 contents + rel->r_offset - 2);
7571 insn3 = bfd_get_32 (output_bfd,
7572 contents + offset + 4);
7573 if ((tls_mask & tls_gd) != 0)
7574 {
7575 /* IE */
7576 insn1 &= (1 << 26) - (1 << 2);
7577 insn1 |= 58 << 26; /* ld */
7578 insn2 = 0x7c636a14; /* add 3,3,13 */
7579 rel[1].r_info = ELF64_R_INFO (r_symndx2, R_PPC64_NONE);
7580 if ((tls_mask & TLS_EXPLICIT) == 0)
7581 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
7582 + R_PPC64_GOT_TPREL16_DS);
7583 else
7584 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
7585 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
7586 }
7587 else
7588 {
7589 /* LE */
7590 insn1 = 0x3c6d0000; /* addis 3,13,0 */
7591 insn2 = 0x38630000; /* addi 3,3,0 */
7592 if (tls_gd == 0)
7593 {
7594 /* Was an LD reloc. */
7595 r_symndx = 0;
7596 rel->r_addend = htab->tls_sec->vma + DTP_OFFSET;
7597 rel[1].r_addend = htab->tls_sec->vma + DTP_OFFSET;
7598 }
7599 r_type = R_PPC64_TPREL16_HA;
7600 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
7601 rel[1].r_info = ELF64_R_INFO (r_symndx,
7602 R_PPC64_TPREL16_LO);
7603 rel[1].r_offset += 2;
7604 }
7605 if (insn3 == NOP
7606 || insn3 == CROR_151515 || insn3 == CROR_313131)
7607 {
7608 insn3 = insn2;
7609 insn2 = NOP;
7610 rel[1].r_offset += 4;
7611 }
7612 bfd_put_32 (output_bfd, insn1, contents + rel->r_offset - 2);
7613 bfd_put_32 (output_bfd, insn2, contents + offset);
7614 bfd_put_32 (output_bfd, insn3, contents + offset + 4);
7615 if (tls_gd == 0)
7616 {
7617 /* We changed the symbol on an LD reloc. Start over
7618 in order to get h, sym, sec etc. right. */
7619 rel--;
7620 continue;
7621 }
7622 }
7623 }
7624 break;
7625
7626 case R_PPC64_DTPMOD64:
7627 if (rel + 1 < relend
7628 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
7629 && rel[1].r_offset == rel->r_offset + 8)
7630 {
7631 if ((tls_mask & TLS_GD) == 0)
7632 {
7633 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
7634 if ((tls_mask & TLS_TPRELGD) != 0)
7635 r_type = R_PPC64_TPREL64;
7636 else
7637 {
7638 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
7639 r_type = R_PPC64_NONE;
7640 }
7641 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
7642 }
7643 }
7644 else
7645 {
7646 if ((tls_mask & TLS_LD) == 0)
7647 {
7648 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
7649 r_type = R_PPC64_NONE;
7650 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
7651 }
7652 }
7653 break;
7654
7655 case R_PPC64_TPREL64:
7656 if ((tls_mask & TLS_TPREL) == 0)
7657 {
7658 r_type = R_PPC64_NONE;
7659 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
7660 }
7661 break;
7662 }
7663
7664 /* Handle other relocations that tweak non-addend part of insn. */
7665 insn = 0;
7666 switch (r_type)
7667 {
7668 default:
7669 break;
7670
7671 /* Branch taken prediction relocations. */
7672 case R_PPC64_ADDR14_BRTAKEN:
7673 case R_PPC64_REL14_BRTAKEN:
7674 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
7675 /* Fall thru. */
7676
7677 /* Branch not taken prediction relocations. */
7678 case R_PPC64_ADDR14_BRNTAKEN:
7679 case R_PPC64_REL14_BRNTAKEN:
7680 insn |= bfd_get_32 (output_bfd,
7681 contents + rel->r_offset) & ~(0x01 << 21);
7682 if (is_power4)
7683 {
7684 /* Set 'a' bit. This is 0b00010 in BO field for branch
7685 on CR(BI) insns (BO == 001at or 011at), and 0b01000
7686 for branch on CTR insns (BO == 1a00t or 1a01t). */
7687 if ((insn & (0x14 << 21)) == (0x04 << 21))
7688 insn |= 0x02 << 21;
7689 else if ((insn & (0x14 << 21)) == (0x10 << 21))
7690 insn |= 0x08 << 21;
7691 else
7692 break;
7693 }
7694 else
7695 {
7696 from = (rel->r_offset
7697 + input_section->output_offset
7698 + input_section->output_section->vma);
7699
7700 /* Invert 'y' bit if not the default. */
7701 if ((bfd_signed_vma) (relocation + rel->r_addend - from) < 0)
7702 insn ^= 0x01 << 21;
7703 }
7704
7705 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
7706 break;
7707
7708 case R_PPC64_REL24:
7709 /* Calls to functions with a different TOC, such as calls to
7710 shared objects, need to alter the TOC pointer. This is
7711 done using a linkage stub. A REL24 branching to these
7712 linkage stubs needs to be followed by a nop, as the nop
7713 will be replaced with an instruction to restore the TOC
7714 base pointer. */
7715 if (((h != NULL
7716 && (fdh = ((struct ppc_link_hash_entry *) h)->oh) != NULL
7717 && fdh->plt.plist != NULL)
7718 || ((fdh = h, sec) != NULL
7719 && sec->output_section != NULL
7720 && (htab->stub_group[sec->id].toc_off
7721 != htab->stub_group[input_section->id].toc_off)))
7722 && (stub_entry = ppc_get_stub_entry (input_section, sec, fdh,
7723 rel, htab)) != NULL
7724 && (stub_entry->stub_type == ppc_stub_plt_call
7725 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
7726 || stub_entry->stub_type == ppc_stub_long_branch_r2off))
7727 {
7728 bfd_boolean can_plt_call = 0;
7729
7730 if (rel->r_offset + 8 <= input_section->_cooked_size)
7731 {
7732 insn = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
7733 if (insn == NOP
7734 || insn == CROR_151515 || insn == CROR_313131)
7735 {
7736 bfd_put_32 (input_bfd, LD_R2_40R1,
7737 contents + rel->r_offset + 4);
7738 can_plt_call = 1;
7739 }
7740 }
7741
7742 if (!can_plt_call)
7743 {
7744 if (stub_entry->stub_type == ppc_stub_plt_call)
7745 {
7746 /* If this is a plain branch rather than a branch
7747 and link, don't require a nop. */
7748 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
7749 if ((insn & 1) == 0)
7750 can_plt_call = 1;
7751 }
7752 else
7753 {
7754 if (strcmp (input_section->output_section->name,
7755 ".init") == 0
7756 || strcmp (input_section->output_section->name,
7757 ".fini") == 0)
7758 (*_bfd_error_handler)
7759 (_("%s(%s+0x%lx): automatic multiple TOCs "
7760 "not supported using your crt files; "
7761 "recompile with -mminimal-toc or upgrade gcc"),
7762 bfd_archive_filename (input_bfd),
7763 input_section->name,
7764 (long) rel->r_offset);
7765 else
7766 (*_bfd_error_handler)
7767 (_("%s(%s+0x%lx): sibling call optimization to `%s' "
7768 "does not allow automatic multiple TOCs; "
7769 "recompile with -mminimal-toc or "
7770 "-fno-optimize-sibling-calls, "
7771 "or make `%s' extern"),
7772 bfd_archive_filename (input_bfd),
7773 input_section->name,
7774 (long) rel->r_offset,
7775 sym_name,
7776 sym_name);
7777 bfd_set_error (bfd_error_bad_value);
7778 ret = FALSE;
7779 }
7780 }
7781
7782 if (can_plt_call)
7783 {
7784 relocation = (stub_entry->stub_offset
7785 + stub_entry->stub_sec->output_offset
7786 + stub_entry->stub_sec->output_section->vma);
7787 if (stub_entry->stub_type == ppc_stub_plt_call)
7788 unresolved_reloc = FALSE;
7789 }
7790 }
7791
7792 if (h != NULL
7793 && h->root.type == bfd_link_hash_undefweak
7794 && relocation == 0
7795 && rel->r_addend == 0)
7796 {
7797 /* Tweak calls to undefined weak functions to point at a
7798 blr. We can thus call a weak function without first
7799 checking whether the function is defined. We have a
7800 blr at the end of .sfpr. */
7801 BFD_ASSERT (htab->sfpr->_raw_size != 0);
7802 relocation = (htab->sfpr->_raw_size - 4
7803 + htab->sfpr->output_offset
7804 + htab->sfpr->output_section->vma);
7805 from = (rel->r_offset
7806 + input_section->output_offset
7807 + input_section->output_section->vma);
7808
7809 /* But let's not be silly about it. If the blr isn't in
7810 reach, just go to the next instruction. */
7811 if (relocation - from + (1 << 25) >= (1 << 26)
7812 || htab->sfpr->_raw_size == 0)
7813 relocation = from + 4;
7814 }
7815 break;
7816 }
7817
7818 /* Set `addend'. */
7819 tls_type = 0;
7820 addend = rel->r_addend;
7821 switch (r_type)
7822 {
7823 default:
7824 (*_bfd_error_handler)
7825 (_("%s: unknown relocation type %d for symbol %s"),
7826 bfd_archive_filename (input_bfd), (int) r_type, sym_name);
7827
7828 bfd_set_error (bfd_error_bad_value);
7829 ret = FALSE;
7830 continue;
7831
7832 case R_PPC64_NONE:
7833 case R_PPC64_TLS:
7834 case R_PPC64_GNU_VTINHERIT:
7835 case R_PPC64_GNU_VTENTRY:
7836 continue;
7837
7838 /* GOT16 relocations. Like an ADDR16 using the symbol's
7839 address in the GOT as relocation value instead of the
7840 symbol's value itself. Also, create a GOT entry for the
7841 symbol and put the symbol value there. */
7842 case R_PPC64_GOT_TLSGD16:
7843 case R_PPC64_GOT_TLSGD16_LO:
7844 case R_PPC64_GOT_TLSGD16_HI:
7845 case R_PPC64_GOT_TLSGD16_HA:
7846 tls_type = TLS_TLS | TLS_GD;
7847 goto dogot;
7848
7849 case R_PPC64_GOT_TLSLD16:
7850 case R_PPC64_GOT_TLSLD16_LO:
7851 case R_PPC64_GOT_TLSLD16_HI:
7852 case R_PPC64_GOT_TLSLD16_HA:
7853 tls_type = TLS_TLS | TLS_LD;
7854 goto dogot;
7855
7856 case R_PPC64_GOT_TPREL16_DS:
7857 case R_PPC64_GOT_TPREL16_LO_DS:
7858 case R_PPC64_GOT_TPREL16_HI:
7859 case R_PPC64_GOT_TPREL16_HA:
7860 tls_type = TLS_TLS | TLS_TPREL;
7861 goto dogot;
7862
7863 case R_PPC64_GOT_DTPREL16_DS:
7864 case R_PPC64_GOT_DTPREL16_LO_DS:
7865 case R_PPC64_GOT_DTPREL16_HI:
7866 case R_PPC64_GOT_DTPREL16_HA:
7867 tls_type = TLS_TLS | TLS_DTPREL;
7868 goto dogot;
7869
7870 case R_PPC64_GOT16:
7871 case R_PPC64_GOT16_LO:
7872 case R_PPC64_GOT16_HI:
7873 case R_PPC64_GOT16_HA:
7874 case R_PPC64_GOT16_DS:
7875 case R_PPC64_GOT16_LO_DS:
7876 dogot:
7877 {
7878 /* Relocation is to the entry for this symbol in the global
7879 offset table. */
7880 asection *got;
7881 bfd_vma *offp;
7882 bfd_vma off;
7883 unsigned long indx = 0;
7884
7885 if (tls_type == (TLS_TLS | TLS_LD)
7886 && (h == NULL
7887 || !(h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)))
7888 offp = &ppc64_tlsld_got (input_bfd)->offset;
7889 else
7890 {
7891 struct got_entry *ent;
7892
7893 if (h != NULL)
7894 {
7895 bfd_boolean dyn = htab->elf.dynamic_sections_created;
7896 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
7897 || (info->shared
7898 && SYMBOL_REFERENCES_LOCAL (info, h)))
7899 /* This is actually a static link, or it is a
7900 -Bsymbolic link and the symbol is defined
7901 locally, or the symbol was forced to be local
7902 because of a version file. */
7903 ;
7904 else
7905 {
7906 indx = h->dynindx;
7907 unresolved_reloc = FALSE;
7908 }
7909 ent = h->got.glist;
7910 }
7911 else
7912 {
7913 if (local_got_ents == NULL)
7914 abort ();
7915 ent = local_got_ents[r_symndx];
7916 }
7917
7918 for (; ent != NULL; ent = ent->next)
7919 if (ent->addend == rel->r_addend
7920 && ent->owner == input_bfd
7921 && ent->tls_type == tls_type)
7922 break;
7923 if (ent == NULL)
7924 abort ();
7925 offp = &ent->got.offset;
7926 }
7927
7928 got = ppc64_elf_tdata (input_bfd)->got;
7929 if (got == NULL)
7930 abort ();
7931
7932 /* The offset must always be a multiple of 8. We use the
7933 least significant bit to record whether we have already
7934 processed this entry. */
7935 off = *offp;
7936 if ((off & 1) != 0)
7937 off &= ~1;
7938 else
7939 {
7940 /* Generate relocs for the dynamic linker, except in
7941 the case of TLSLD where we'll use one entry per
7942 module. */
7943 asection *relgot = ppc64_elf_tdata (input_bfd)->relgot;
7944
7945 *offp = off | 1;
7946 if ((info->shared || indx != 0)
7947 && (h == NULL
7948 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
7949 || h->root.type != bfd_link_hash_undefweak))
7950 {
7951 outrel.r_offset = (got->output_section->vma
7952 + got->output_offset
7953 + off);
7954 outrel.r_addend = rel->r_addend;
7955 if (tls_type & (TLS_LD | TLS_GD))
7956 {
7957 outrel.r_addend = 0;
7958 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
7959 if (tls_type == (TLS_TLS | TLS_GD))
7960 {
7961 loc = relgot->contents;
7962 loc += (relgot->reloc_count++
7963 * sizeof (Elf64_External_Rela));
7964 bfd_elf64_swap_reloca_out (output_bfd,
7965 &outrel, loc);
7966 outrel.r_offset += 8;
7967 outrel.r_addend = rel->r_addend;
7968 outrel.r_info
7969 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
7970 }
7971 }
7972 else if (tls_type == (TLS_TLS | TLS_DTPREL))
7973 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
7974 else if (tls_type == (TLS_TLS | TLS_TPREL))
7975 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
7976 else if (indx == 0)
7977 {
7978 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_RELATIVE);
7979
7980 /* Write the .got section contents for the sake
7981 of prelink. */
7982 loc = got->contents + off;
7983 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
7984 loc);
7985 }
7986 else
7987 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
7988
7989 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
7990 {
7991 outrel.r_addend += relocation;
7992 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
7993 outrel.r_addend -= htab->tls_sec->vma;
7994 }
7995 loc = relgot->contents;
7996 loc += (relgot->reloc_count++
7997 * sizeof (Elf64_External_Rela));
7998 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
7999 }
8000
8001 /* Init the .got section contents here if we're not
8002 emitting a reloc. */
8003 else
8004 {
8005 relocation += rel->r_addend;
8006 if (tls_type == (TLS_TLS | TLS_LD))
8007 relocation = 1;
8008 else if (tls_type != 0)
8009 {
8010 relocation -= htab->tls_sec->vma + DTP_OFFSET;
8011 if (tls_type == (TLS_TLS | TLS_TPREL))
8012 relocation += DTP_OFFSET - TP_OFFSET;
8013
8014 if (tls_type == (TLS_TLS | TLS_GD))
8015 {
8016 bfd_put_64 (output_bfd, relocation,
8017 got->contents + off + 8);
8018 relocation = 1;
8019 }
8020 }
8021
8022 bfd_put_64 (output_bfd, relocation,
8023 got->contents + off);
8024 }
8025 }
8026
8027 if (off >= (bfd_vma) -2)
8028 abort ();
8029
8030 relocation = got->output_offset + off;
8031
8032 /* TOC base (r2) is TOC start plus 0x8000. */
8033 addend = -TOC_BASE_OFF;
8034 }
8035 break;
8036
8037 case R_PPC64_PLT16_HA:
8038 case R_PPC64_PLT16_HI:
8039 case R_PPC64_PLT16_LO:
8040 case R_PPC64_PLT32:
8041 case R_PPC64_PLT64:
8042 /* Relocation is to the entry for this symbol in the
8043 procedure linkage table. */
8044
8045 /* Resolve a PLT reloc against a local symbol directly,
8046 without using the procedure linkage table. */
8047 if (h == NULL)
8048 break;
8049
8050 /* It's possible that we didn't make a PLT entry for this
8051 symbol. This happens when statically linking PIC code,
8052 or when using -Bsymbolic. Go find a match if there is a
8053 PLT entry. */
8054 if (htab->plt != NULL)
8055 {
8056 struct plt_entry *ent;
8057 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
8058 if (ent->addend == rel->r_addend
8059 && ent->plt.offset != (bfd_vma) -1)
8060 {
8061 relocation = (htab->plt->output_section->vma
8062 + htab->plt->output_offset
8063 + ent->plt.offset);
8064 unresolved_reloc = FALSE;
8065 }
8066 }
8067 break;
8068
8069 case R_PPC64_TOC:
8070 /* Relocation value is TOC base. */
8071 relocation = TOCstart;
8072 if (r_symndx == 0)
8073 relocation += htab->stub_group[input_section->id].toc_off;
8074 else if (sec != NULL && !unresolved_reloc)
8075 relocation += htab->stub_group[sec->id].toc_off;
8076 else
8077 unresolved_reloc = TRUE;
8078 goto dodyn2;
8079
8080 /* TOC16 relocs. We want the offset relative to the TOC base,
8081 which is the address of the start of the TOC plus 0x8000.
8082 The TOC consists of sections .got, .toc, .tocbss, and .plt,
8083 in this order. */
8084 case R_PPC64_TOC16:
8085 case R_PPC64_TOC16_LO:
8086 case R_PPC64_TOC16_HI:
8087 case R_PPC64_TOC16_DS:
8088 case R_PPC64_TOC16_LO_DS:
8089 case R_PPC64_TOC16_HA:
8090 addend -= TOCstart + htab->stub_group[input_section->id].toc_off;
8091 break;
8092
8093 /* Relocate against the beginning of the section. */
8094 case R_PPC64_SECTOFF:
8095 case R_PPC64_SECTOFF_LO:
8096 case R_PPC64_SECTOFF_HI:
8097 case R_PPC64_SECTOFF_DS:
8098 case R_PPC64_SECTOFF_LO_DS:
8099 case R_PPC64_SECTOFF_HA:
8100 if (sec != NULL)
8101 addend -= sec->output_section->vma;
8102 break;
8103
8104 case R_PPC64_REL14:
8105 case R_PPC64_REL14_BRNTAKEN:
8106 case R_PPC64_REL14_BRTAKEN:
8107 case R_PPC64_REL24:
8108 break;
8109
8110 case R_PPC64_TPREL16:
8111 case R_PPC64_TPREL16_LO:
8112 case R_PPC64_TPREL16_HI:
8113 case R_PPC64_TPREL16_HA:
8114 case R_PPC64_TPREL16_DS:
8115 case R_PPC64_TPREL16_LO_DS:
8116 case R_PPC64_TPREL16_HIGHER:
8117 case R_PPC64_TPREL16_HIGHERA:
8118 case R_PPC64_TPREL16_HIGHEST:
8119 case R_PPC64_TPREL16_HIGHESTA:
8120 addend -= htab->tls_sec->vma + TP_OFFSET;
8121 if (info->shared)
8122 /* The TPREL16 relocs shouldn't really be used in shared
8123 libs as they will result in DT_TEXTREL being set, but
8124 support them anyway. */
8125 goto dodyn;
8126 break;
8127
8128 case R_PPC64_DTPREL16:
8129 case R_PPC64_DTPREL16_LO:
8130 case R_PPC64_DTPREL16_HI:
8131 case R_PPC64_DTPREL16_HA:
8132 case R_PPC64_DTPREL16_DS:
8133 case R_PPC64_DTPREL16_LO_DS:
8134 case R_PPC64_DTPREL16_HIGHER:
8135 case R_PPC64_DTPREL16_HIGHERA:
8136 case R_PPC64_DTPREL16_HIGHEST:
8137 case R_PPC64_DTPREL16_HIGHESTA:
8138 addend -= htab->tls_sec->vma + DTP_OFFSET;
8139 break;
8140
8141 case R_PPC64_DTPMOD64:
8142 relocation = 1;
8143 addend = 0;
8144 goto dodyn;
8145
8146 case R_PPC64_TPREL64:
8147 addend -= htab->tls_sec->vma + TP_OFFSET;
8148 goto dodyn;
8149
8150 case R_PPC64_DTPREL64:
8151 addend -= htab->tls_sec->vma + DTP_OFFSET;
8152 /* Fall thru */
8153
8154 /* Relocations that may need to be propagated if this is a
8155 dynamic object. */
8156 case R_PPC64_REL30:
8157 case R_PPC64_REL32:
8158 case R_PPC64_REL64:
8159 case R_PPC64_ADDR14:
8160 case R_PPC64_ADDR14_BRNTAKEN:
8161 case R_PPC64_ADDR14_BRTAKEN:
8162 case R_PPC64_ADDR16:
8163 case R_PPC64_ADDR16_DS:
8164 case R_PPC64_ADDR16_HA:
8165 case R_PPC64_ADDR16_HI:
8166 case R_PPC64_ADDR16_HIGHER:
8167 case R_PPC64_ADDR16_HIGHERA:
8168 case R_PPC64_ADDR16_HIGHEST:
8169 case R_PPC64_ADDR16_HIGHESTA:
8170 case R_PPC64_ADDR16_LO:
8171 case R_PPC64_ADDR16_LO_DS:
8172 case R_PPC64_ADDR24:
8173 case R_PPC64_ADDR32:
8174 case R_PPC64_ADDR64:
8175 case R_PPC64_UADDR16:
8176 case R_PPC64_UADDR32:
8177 case R_PPC64_UADDR64:
8178 /* r_symndx will be zero only for relocs against symbols
8179 from removed linkonce sections, or sections discarded by
8180 a linker script. */
8181 dodyn:
8182 if (r_symndx == 0)
8183 break;
8184 /* Fall thru. */
8185
8186 dodyn2:
8187 if ((input_section->flags & SEC_ALLOC) == 0)
8188 break;
8189
8190 if (NO_OPD_RELOCS && is_opd)
8191 break;
8192
8193 if ((info->shared
8194 && (h == NULL
8195 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
8196 || h->root.type != bfd_link_hash_undefweak)
8197 && (MUST_BE_DYN_RELOC (r_type)
8198 || !SYMBOL_CALLS_LOCAL (info, h)))
8199 || (ELIMINATE_COPY_RELOCS
8200 && !info->shared
8201 && h != NULL
8202 && h->dynindx != -1
8203 && (h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
8204 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
8205 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0))
8206 {
8207 Elf_Internal_Rela outrel;
8208 bfd_boolean skip, relocate;
8209 asection *sreloc;
8210 bfd_byte *loc;
8211 bfd_vma out_off;
8212
8213 /* When generating a dynamic object, these relocations
8214 are copied into the output file to be resolved at run
8215 time. */
8216
8217 skip = FALSE;
8218 relocate = FALSE;
8219
8220 out_off = _bfd_elf_section_offset (output_bfd, info,
8221 input_section, rel->r_offset);
8222 if (out_off == (bfd_vma) -1)
8223 skip = TRUE;
8224 else if (out_off == (bfd_vma) -2)
8225 skip = TRUE, relocate = TRUE;
8226 out_off += (input_section->output_section->vma
8227 + input_section->output_offset);
8228 outrel.r_offset = out_off;
8229 outrel.r_addend = rel->r_addend;
8230
8231 /* Optimize unaligned reloc use. */
8232 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
8233 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
8234 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
8235 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
8236 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
8237 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
8238 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
8239 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
8240 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
8241
8242 if (skip)
8243 memset (&outrel, 0, sizeof outrel);
8244 else if (!SYMBOL_REFERENCES_LOCAL (info, h)
8245 && !is_opd
8246 && r_type != R_PPC64_TOC)
8247 outrel.r_info = ELF64_R_INFO (h->dynindx, r_type);
8248 else
8249 {
8250 /* This symbol is local, or marked to become local,
8251 or this is an opd section reloc which must point
8252 at a local function. */
8253 outrel.r_addend += relocation;
8254 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
8255 {
8256 if (is_opd && h != NULL)
8257 {
8258 /* Lie about opd entries. This case occurs
8259 when building shared libraries and we
8260 reference a function in another shared
8261 lib. The same thing happens for a weak
8262 definition in an application that's
8263 overridden by a strong definition in a
8264 shared lib. (I believe this is a generic
8265 bug in binutils handling of weak syms.)
8266 In these cases we won't use the opd
8267 entry in this lib. */
8268 unresolved_reloc = FALSE;
8269 }
8270 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
8271
8272 /* We need to relocate .opd contents for ld.so.
8273 Prelink also wants simple and consistent rules
8274 for relocs. This make all RELATIVE relocs have
8275 *r_offset equal to r_addend. */
8276 relocate = TRUE;
8277 }
8278 else
8279 {
8280 long indx = 0;
8281
8282 if (bfd_is_abs_section (sec))
8283 ;
8284 else if (sec == NULL || sec->owner == NULL)
8285 {
8286 bfd_set_error (bfd_error_bad_value);
8287 return FALSE;
8288 }
8289 else
8290 {
8291 asection *osec;
8292
8293 osec = sec->output_section;
8294 indx = elf_section_data (osec)->dynindx;
8295
8296 /* We are turning this relocation into one
8297 against a section symbol, so subtract out
8298 the output section's address but not the
8299 offset of the input section in the output
8300 section. */
8301 outrel.r_addend -= osec->vma;
8302 }
8303
8304 outrel.r_info = ELF64_R_INFO (indx, r_type);
8305 }
8306 }
8307
8308 sreloc = elf_section_data (input_section)->sreloc;
8309 if (sreloc == NULL)
8310 abort ();
8311
8312 loc = sreloc->contents;
8313 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
8314 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
8315
8316 /* If this reloc is against an external symbol, it will
8317 be computed at runtime, so there's no need to do
8318 anything now. However, for the sake of prelink ensure
8319 that the section contents are a known value. */
8320 if (! relocate)
8321 {
8322 unresolved_reloc = FALSE;
8323 /* The value chosen here is quite arbitrary as ld.so
8324 ignores section contents except for the special
8325 case of .opd where the contents might be accessed
8326 before relocation. Choose zero, as that won't
8327 cause reloc overflow. */
8328 relocation = 0;
8329 addend = 0;
8330 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
8331 to improve backward compatibility with older
8332 versions of ld. */
8333 if (r_type == R_PPC64_ADDR64)
8334 addend = outrel.r_addend;
8335 /* Adjust pc_relative relocs to have zero in *r_offset. */
8336 else if (ppc64_elf_howto_table[r_type]->pc_relative)
8337 addend = (input_section->output_section->vma
8338 + input_section->output_offset
8339 + rel->r_offset);
8340 }
8341 }
8342 break;
8343
8344 case R_PPC64_COPY:
8345 case R_PPC64_GLOB_DAT:
8346 case R_PPC64_JMP_SLOT:
8347 case R_PPC64_RELATIVE:
8348 /* We shouldn't ever see these dynamic relocs in relocatable
8349 files. */
8350 /* Fall through. */
8351
8352 case R_PPC64_PLTGOT16:
8353 case R_PPC64_PLTGOT16_DS:
8354 case R_PPC64_PLTGOT16_HA:
8355 case R_PPC64_PLTGOT16_HI:
8356 case R_PPC64_PLTGOT16_LO:
8357 case R_PPC64_PLTGOT16_LO_DS:
8358 case R_PPC64_PLTREL32:
8359 case R_PPC64_PLTREL64:
8360 /* These ones haven't been implemented yet. */
8361
8362 (*_bfd_error_handler)
8363 (_("%s: relocation %s is not supported for symbol %s."),
8364 bfd_archive_filename (input_bfd),
8365 ppc64_elf_howto_table[r_type]->name, sym_name);
8366
8367 bfd_set_error (bfd_error_invalid_operation);
8368 ret = FALSE;
8369 continue;
8370 }
8371
8372 /* Do any further special processing. */
8373 switch (r_type)
8374 {
8375 default:
8376 break;
8377
8378 case R_PPC64_ADDR16_HA:
8379 case R_PPC64_ADDR16_HIGHERA:
8380 case R_PPC64_ADDR16_HIGHESTA:
8381 case R_PPC64_GOT16_HA:
8382 case R_PPC64_PLTGOT16_HA:
8383 case R_PPC64_PLT16_HA:
8384 case R_PPC64_TOC16_HA:
8385 case R_PPC64_SECTOFF_HA:
8386 case R_PPC64_TPREL16_HA:
8387 case R_PPC64_DTPREL16_HA:
8388 case R_PPC64_GOT_TLSGD16_HA:
8389 case R_PPC64_GOT_TLSLD16_HA:
8390 case R_PPC64_GOT_TPREL16_HA:
8391 case R_PPC64_GOT_DTPREL16_HA:
8392 case R_PPC64_TPREL16_HIGHER:
8393 case R_PPC64_TPREL16_HIGHERA:
8394 case R_PPC64_TPREL16_HIGHEST:
8395 case R_PPC64_TPREL16_HIGHESTA:
8396 case R_PPC64_DTPREL16_HIGHER:
8397 case R_PPC64_DTPREL16_HIGHERA:
8398 case R_PPC64_DTPREL16_HIGHEST:
8399 case R_PPC64_DTPREL16_HIGHESTA:
8400 /* It's just possible that this symbol is a weak symbol
8401 that's not actually defined anywhere. In that case,
8402 'sec' would be NULL, and we should leave the symbol
8403 alone (it will be set to zero elsewhere in the link). */
8404 if (sec != NULL)
8405 /* Add 0x10000 if sign bit in 0:15 is set.
8406 Bits 0:15 are not used. */
8407 addend += 0x8000;
8408 break;
8409
8410 case R_PPC64_ADDR16_DS:
8411 case R_PPC64_ADDR16_LO_DS:
8412 case R_PPC64_GOT16_DS:
8413 case R_PPC64_GOT16_LO_DS:
8414 case R_PPC64_PLT16_LO_DS:
8415 case R_PPC64_SECTOFF_DS:
8416 case R_PPC64_SECTOFF_LO_DS:
8417 case R_PPC64_TOC16_DS:
8418 case R_PPC64_TOC16_LO_DS:
8419 case R_PPC64_PLTGOT16_DS:
8420 case R_PPC64_PLTGOT16_LO_DS:
8421 case R_PPC64_GOT_TPREL16_DS:
8422 case R_PPC64_GOT_TPREL16_LO_DS:
8423 case R_PPC64_GOT_DTPREL16_DS:
8424 case R_PPC64_GOT_DTPREL16_LO_DS:
8425 case R_PPC64_TPREL16_DS:
8426 case R_PPC64_TPREL16_LO_DS:
8427 case R_PPC64_DTPREL16_DS:
8428 case R_PPC64_DTPREL16_LO_DS:
8429 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
8430 mask = 3;
8431 /* If this reloc is against an lq insn, then the value must be
8432 a multiple of 16. This is somewhat of a hack, but the
8433 "correct" way to do this by defining _DQ forms of all the
8434 _DS relocs bloats all reloc switches in this file. It
8435 doesn't seem to make much sense to use any of these relocs
8436 in data, so testing the insn should be safe. */
8437 if ((insn & (0x3f << 26)) == (56 << 26))
8438 mask = 15;
8439 if (((relocation + addend) & mask) != 0)
8440 {
8441 (*_bfd_error_handler)
8442 (_("%s: error: relocation %s not a multiple of %d"),
8443 bfd_archive_filename (input_bfd),
8444 ppc64_elf_howto_table[r_type]->name,
8445 mask + 1);
8446 bfd_set_error (bfd_error_bad_value);
8447 ret = FALSE;
8448 continue;
8449 }
8450 break;
8451
8452 case R_PPC64_REL14:
8453 case R_PPC64_REL14_BRNTAKEN:
8454 case R_PPC64_REL14_BRTAKEN:
8455 max_br_offset = 1 << 15;
8456 goto branch_check;
8457
8458 case R_PPC64_REL24:
8459 max_br_offset = 1 << 25;
8460
8461 branch_check:
8462 /* If the branch is out of reach or the TOC register needs
8463 adjusting, then redirect the call to the local stub for
8464 this function. */
8465 from = (rel->r_offset
8466 + input_section->output_offset
8467 + input_section->output_section->vma);
8468 if ((relocation + addend - from + max_br_offset >= 2 * max_br_offset
8469 || (sec != NULL
8470 && sec->output_section != NULL
8471 && sec->id <= htab->top_id
8472 && (htab->stub_group[sec->id].toc_off
8473 != htab->stub_group[input_section->id].toc_off)))
8474 && (stub_entry = ppc_get_stub_entry (input_section, sec, h,
8475 rel, htab)) != NULL)
8476 {
8477 /* Munge up the value and addend so that we call the stub
8478 rather than the procedure directly. */
8479 relocation = (stub_entry->stub_offset
8480 + stub_entry->stub_sec->output_offset
8481 + stub_entry->stub_sec->output_section->vma);
8482 addend = 0;
8483 }
8484 break;
8485 }
8486
8487 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
8488 because such sections are not SEC_ALLOC and thus ld.so will
8489 not process them. */
8490 if (unresolved_reloc
8491 && !((input_section->flags & SEC_DEBUGGING) != 0
8492 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0))
8493 {
8494 (*_bfd_error_handler)
8495 (_("%s(%s+0x%lx): unresolvable %s relocation against symbol `%s'"),
8496 bfd_archive_filename (input_bfd),
8497 bfd_get_section_name (input_bfd, input_section),
8498 (long) rel->r_offset,
8499 ppc64_elf_howto_table[(int) r_type]->name,
8500 h->root.root.string);
8501 ret = FALSE;
8502 }
8503
8504 r = _bfd_final_link_relocate (ppc64_elf_howto_table[(int) r_type],
8505 input_bfd,
8506 input_section,
8507 contents,
8508 rel->r_offset,
8509 relocation,
8510 addend);
8511
8512 if (r != bfd_reloc_ok)
8513 {
8514 if (sym_name == NULL)
8515 sym_name = "(null)";
8516 if (r == bfd_reloc_overflow)
8517 {
8518 if (warned)
8519 continue;
8520 if (h != NULL
8521 && h->root.type == bfd_link_hash_undefweak
8522 && ppc64_elf_howto_table[r_type]->pc_relative)
8523 {
8524 /* Assume this is a call protected by other code that
8525 detects the symbol is undefined. If this is the case,
8526 we can safely ignore the overflow. If not, the
8527 program is hosed anyway, and a little warning isn't
8528 going to help. */
8529
8530 continue;
8531 }
8532
8533 if (!((*info->callbacks->reloc_overflow)
8534 (info, sym_name, ppc64_elf_howto_table[r_type]->name,
8535 rel->r_addend, input_bfd, input_section, rel->r_offset)))
8536 return FALSE;
8537 }
8538 else
8539 {
8540 (*_bfd_error_handler)
8541 (_("%s(%s+0x%lx): %s reloc against `%s': error %d"),
8542 bfd_archive_filename (input_bfd),
8543 bfd_get_section_name (input_bfd, input_section),
8544 (long) rel->r_offset,
8545 ppc64_elf_howto_table[r_type]->name,
8546 sym_name,
8547 (int) r);
8548 ret = FALSE;
8549 }
8550 }
8551 }
8552
8553 return ret;
8554 }
8555
8556 /* Finish up dynamic symbol handling. We set the contents of various
8557 dynamic sections here. */
8558
8559 static bfd_boolean
8560 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
8561 struct bfd_link_info *info,
8562 struct elf_link_hash_entry *h,
8563 Elf_Internal_Sym *sym)
8564 {
8565 struct ppc_link_hash_table *htab;
8566 bfd *dynobj;
8567
8568 htab = ppc_hash_table (info);
8569 dynobj = htab->elf.dynobj;
8570
8571 if (((struct ppc_link_hash_entry *) h)->is_func_descriptor)
8572 {
8573 struct plt_entry *ent;
8574 Elf_Internal_Rela rela;
8575 bfd_byte *loc;
8576
8577 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
8578 if (ent->plt.offset != (bfd_vma) -1)
8579 {
8580 /* This symbol has an entry in the procedure linkage
8581 table. Set it up. */
8582
8583 if (htab->plt == NULL
8584 || htab->relplt == NULL
8585 || htab->glink == NULL)
8586 abort ();
8587
8588 /* Create a JMP_SLOT reloc to inform the dynamic linker to
8589 fill in the PLT entry. */
8590 rela.r_offset = (htab->plt->output_section->vma
8591 + htab->plt->output_offset
8592 + ent->plt.offset);
8593 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
8594 rela.r_addend = ent->addend;
8595
8596 loc = htab->relplt->contents;
8597 loc += ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE) / PLT_ENTRY_SIZE
8598 * sizeof (Elf64_External_Rela));
8599 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
8600 }
8601 }
8602
8603 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
8604 {
8605 Elf_Internal_Rela rela;
8606 bfd_byte *loc;
8607
8608 /* This symbol needs a copy reloc. Set it up. */
8609
8610 if (h->dynindx == -1
8611 || (h->root.type != bfd_link_hash_defined
8612 && h->root.type != bfd_link_hash_defweak)
8613 || htab->relbss == NULL)
8614 abort ();
8615
8616 rela.r_offset = (h->root.u.def.value
8617 + h->root.u.def.section->output_section->vma
8618 + h->root.u.def.section->output_offset);
8619 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
8620 rela.r_addend = 0;
8621 loc = htab->relbss->contents;
8622 loc += htab->relbss->reloc_count++ * sizeof (Elf64_External_Rela);
8623 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
8624 }
8625
8626 /* Mark some specially defined symbols as absolute. */
8627 if (strcmp (h->root.root.string, "_DYNAMIC") == 0)
8628 sym->st_shndx = SHN_ABS;
8629
8630 return TRUE;
8631 }
8632
8633 /* Used to decide how to sort relocs in an optimal manner for the
8634 dynamic linker, before writing them out. */
8635
8636 static enum elf_reloc_type_class
8637 ppc64_elf_reloc_type_class (const Elf_Internal_Rela *rela)
8638 {
8639 enum elf_ppc64_reloc_type r_type;
8640
8641 r_type = ELF64_R_TYPE (rela->r_info);
8642 switch (r_type)
8643 {
8644 case R_PPC64_RELATIVE:
8645 return reloc_class_relative;
8646 case R_PPC64_JMP_SLOT:
8647 return reloc_class_plt;
8648 case R_PPC64_COPY:
8649 return reloc_class_copy;
8650 default:
8651 return reloc_class_normal;
8652 }
8653 }
8654
8655 /* Finish up the dynamic sections. */
8656
8657 static bfd_boolean
8658 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
8659 struct bfd_link_info *info)
8660 {
8661 struct ppc_link_hash_table *htab;
8662 bfd *dynobj;
8663 asection *sdyn;
8664
8665 htab = ppc_hash_table (info);
8666 dynobj = htab->elf.dynobj;
8667 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
8668
8669 if (htab->elf.dynamic_sections_created)
8670 {
8671 Elf64_External_Dyn *dyncon, *dynconend;
8672
8673 if (sdyn == NULL || htab->got == NULL)
8674 abort ();
8675
8676 dyncon = (Elf64_External_Dyn *) sdyn->contents;
8677 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
8678 for (; dyncon < dynconend; dyncon++)
8679 {
8680 Elf_Internal_Dyn dyn;
8681 asection *s;
8682
8683 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
8684
8685 switch (dyn.d_tag)
8686 {
8687 default:
8688 continue;
8689
8690 case DT_PPC64_GLINK:
8691 s = htab->glink;
8692 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
8693 /* We stupidly defined DT_PPC64_GLINK to be the start
8694 of glink rather than the first entry point, which is
8695 what ld.so needs, and now have a bigger stub to
8696 support automatic multiple TOCs. */
8697 dyn.d_un.d_ptr += GLINK_CALL_STUB_SIZE - 32;
8698 break;
8699
8700 case DT_PPC64_OPD:
8701 s = bfd_get_section_by_name (output_bfd, ".opd");
8702 if (s == NULL)
8703 continue;
8704 dyn.d_un.d_ptr = s->vma;
8705 break;
8706
8707 case DT_PPC64_OPDSZ:
8708 s = bfd_get_section_by_name (output_bfd, ".opd");
8709 if (s == NULL)
8710 continue;
8711 dyn.d_un.d_val = s->_raw_size;
8712 break;
8713
8714 case DT_PLTGOT:
8715 s = htab->plt;
8716 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
8717 break;
8718
8719 case DT_JMPREL:
8720 s = htab->relplt;
8721 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
8722 break;
8723
8724 case DT_PLTRELSZ:
8725 dyn.d_un.d_val = htab->relplt->_raw_size;
8726 break;
8727
8728 case DT_RELASZ:
8729 /* Don't count procedure linkage table relocs in the
8730 overall reloc count. */
8731 s = htab->relplt;
8732 if (s == NULL)
8733 continue;
8734 dyn.d_un.d_val -= s->_raw_size;
8735 break;
8736
8737 case DT_RELA:
8738 /* We may not be using the standard ELF linker script.
8739 If .rela.plt is the first .rela section, we adjust
8740 DT_RELA to not include it. */
8741 s = htab->relplt;
8742 if (s == NULL)
8743 continue;
8744 if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
8745 continue;
8746 dyn.d_un.d_ptr += s->_raw_size;
8747 break;
8748 }
8749
8750 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
8751 }
8752 }
8753
8754 if (htab->got != NULL && htab->got->_raw_size != 0)
8755 {
8756 /* Fill in the first entry in the global offset table.
8757 We use it to hold the link-time TOCbase. */
8758 bfd_put_64 (output_bfd,
8759 elf_gp (output_bfd) + TOC_BASE_OFF,
8760 htab->got->contents);
8761
8762 /* Set .got entry size. */
8763 elf_section_data (htab->got->output_section)->this_hdr.sh_entsize = 8;
8764 }
8765
8766 if (htab->plt != NULL && htab->plt->_raw_size != 0)
8767 {
8768 /* Set .plt entry size. */
8769 elf_section_data (htab->plt->output_section)->this_hdr.sh_entsize
8770 = PLT_ENTRY_SIZE;
8771 }
8772
8773 /* We need to handle writing out multiple GOT sections ourselves,
8774 since we didn't add them to DYNOBJ. */
8775 while ((dynobj = dynobj->link_next) != NULL)
8776 {
8777 asection *s;
8778 s = ppc64_elf_tdata (dynobj)->got;
8779 if (s != NULL
8780 && s->_raw_size != 0
8781 && s->output_section != bfd_abs_section_ptr
8782 && !bfd_set_section_contents (output_bfd, s->output_section,
8783 s->contents, s->output_offset,
8784 s->_raw_size))
8785 return FALSE;
8786 s = ppc64_elf_tdata (dynobj)->relgot;
8787 if (s != NULL
8788 && s->_raw_size != 0
8789 && s->output_section != bfd_abs_section_ptr
8790 && !bfd_set_section_contents (output_bfd, s->output_section,
8791 s->contents, s->output_offset,
8792 s->_raw_size))
8793 return FALSE;
8794 }
8795
8796 return TRUE;
8797 }
8798
8799 #include "elf64-target.h"
This page took 0.279316 seconds and 4 git commands to generate.