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