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