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