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