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