1 /* Or1k-specific support for 32-bit ELF.
2 Copyright (C) 2001-2018 Free Software Foundation, Inc.
3 Contributed for OR32 by Johan Rydberg, jrydberg@opencores.org
5 PIC parts added by Stefan Kristiansson, stefan.kristiansson@saunalahti.fi,
6 largely based on elf32-m32r.c and elf32-microblaze.c.
8 This file is part of BFD, the Binary File Descriptor library.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, see <http://www.gnu.org/licenses/>. */
28 #include "libiberty.h"
30 #define N_ONES(X) (((bfd_vma)2 << (X)) - 1)
32 #define PLT_ENTRY_SIZE 20
34 #define PLT0_ENTRY_WORD0 0x19800000 /* l.movhi r12, 0 <- hi(.got+4) */
35 #define PLT0_ENTRY_WORD1 0xa98c0000 /* l.ori r12, r12, 0 <- lo(.got+4) */
36 #define PLT0_ENTRY_WORD2 0x85ec0004 /* l.lwz r15, 4(r12) <- *(.got+8)*/
37 #define PLT0_ENTRY_WORD3 0x44007800 /* l.jr r15 */
38 #define PLT0_ENTRY_WORD4 0x858c0000 /* l.lwz r12, 0(r12) */
40 #define PLT0_PIC_ENTRY_WORD0 0x85900004 /* l.lwz r12, 4(r16) */
41 #define PLT0_PIC_ENTRY_WORD1 0x85f00008 /* l.lwz r15, 8(r16) */
42 #define PLT0_PIC_ENTRY_WORD2 0x44007800 /* l.jr r15 */
43 #define PLT0_PIC_ENTRY_WORD3 0x15000000 /* l.nop */
44 #define PLT0_PIC_ENTRY_WORD4 0x15000000 /* l.nop */
46 #define PLT_ENTRY_WORD0 0x19800000 /* l.movhi r12, 0 <- hi(got idx addr) */
47 #define PLT_ENTRY_WORD1 0xa98c0000 /* l.ori r12, r12, 0 <- lo(got idx addr) */
48 #define PLT_ENTRY_WORD2 0x858c0000 /* l.lwz r12, 0(r12) */
49 #define PLT_ENTRY_WORD3 0x44006000 /* l.jr r12 */
50 #define PLT_ENTRY_WORD4 0xa9600000 /* l.ori r11, r0, 0 <- reloc offset */
52 #define PLT_PIC_ENTRY_WORD0 0x85900000 /* l.lwz r12, 0(r16) <- index in got */
53 #define PLT_PIC_ENTRY_WORD1 0xa9600000 /* l.ori r11, r0, 0 <- reloc offset */
54 #define PLT_PIC_ENTRY_WORD2 0x44006000 /* l.jr r12 */
55 #define PLT_PIC_ENTRY_WORD3 0x15000000 /* l.nop */
56 #define PLT_PIC_ENTRY_WORD4 0x15000000 /* l.nop */
58 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
60 static reloc_howto_type or1k_elf_howto_table[] =
62 /* This reloc does nothing. */
63 HOWTO (R_OR1K_NONE, /* type */
65 3, /* size (0 = byte, 1 = short, 2 = long) */
67 FALSE, /* pc_relative */
69 complain_overflow_dont, /* complain_on_overflow */
70 bfd_elf_generic_reloc, /* special_function */
71 "R_OR1K_NONE", /* name */
72 FALSE, /* partial_inplace */
75 FALSE), /* pcrel_offset */
79 2, /* size (0 = byte, 1 = short, 2 = long) */
81 FALSE, /* pc_relative */
83 complain_overflow_unsigned, /* complain_on_overflow */
84 bfd_elf_generic_reloc, /* special_function */
85 "R_OR1K_32", /* name */
86 FALSE, /* partial_inplace */
88 0xffffffff, /* dst_mask */
89 FALSE), /* pcrel_offset */
93 1, /* size (0 = byte, 1 = short, 2 = long) */
95 FALSE, /* pc_relative */
97 complain_overflow_unsigned, /* complain_on_overflow */
98 bfd_elf_generic_reloc, /* special_function */
99 "R_OR1K_16", /* name */
100 FALSE, /* partial_inplace */
102 0xffff, /* dst_mask */
103 FALSE), /* pcrel_offset */
107 0, /* size (0 = byte, 1 = short, 2 = long) */
109 FALSE, /* pc_relative */
111 complain_overflow_unsigned, /* complain_on_overflow */
112 bfd_elf_generic_reloc, /* special_function */
113 "R_OR1K_8", /* name */
114 FALSE, /* partial_inplace */
117 FALSE), /* pcrel_offset */
119 HOWTO (R_OR1K_LO_16_IN_INSN, /* type */
121 2, /* size (0 = byte, 1 = short, 2 = long) */
123 FALSE, /* pc_relative */
125 complain_overflow_dont, /* complain_on_overflow */
126 bfd_elf_generic_reloc, /* special_function */
127 "R_OR1K_LO_16_IN_INSN", /* name */
128 FALSE, /* partial_inplace */
130 0x0000ffff, /* dst_mask */
131 FALSE), /* pcrel_offset */
133 HOWTO (R_OR1K_HI_16_IN_INSN, /* type */
135 2, /* size (0 = byte, 1 = short, 2 = long) */
137 FALSE, /* pc_relative */
139 complain_overflow_dont, /* complain_on_overflow */
140 bfd_elf_generic_reloc, /* special_function */
141 "R_OR1K_HI_16_IN_INSN", /* name */
142 FALSE, /* partial_inplace */
144 0x0000ffff, /* dst_mask */
145 FALSE), /* pcrel_offset */
147 /* A PC relative 26 bit relocation, right shifted by 2. */
148 HOWTO (R_OR1K_INSN_REL_26, /* type */
150 2, /* size (0 = byte, 1 = short, 2 = long) */
152 TRUE, /* pc_relative */
154 complain_overflow_signed, /* complain_on_overflow */
155 bfd_elf_generic_reloc, /* special_function */
156 "R_OR1K_INSN_REL_26", /* name */
157 FALSE, /* partial_inplace */
159 0x03ffffff, /* dst_mask */
160 TRUE), /* pcrel_offset */
162 /* GNU extension to record C++ vtable hierarchy. */
163 HOWTO (R_OR1K_GNU_VTINHERIT, /* type */
165 2, /* size (0 = byte, 1 = short, 2 = long) */
167 FALSE, /* pc_relative */
169 complain_overflow_dont, /* complain_on_overflow */
170 NULL, /* special_function */
171 "R_OR1K_GNU_VTINHERIT", /* name */
172 FALSE, /* partial_inplace */
175 FALSE), /* pcrel_offset */
177 /* GNU extension to record C++ vtable member usage. */
178 HOWTO (R_OR1K_GNU_VTENTRY, /* type */
180 2, /* size (0 = byte, 1 = short, 2 = long) */
182 FALSE, /* pc_relative */
184 complain_overflow_dont, /* complain_on_overflow */
185 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
186 "R_OR1K_GNU_VTENTRY", /* name */
187 FALSE, /* partial_inplace */
190 FALSE), /* pcrel_offset */
192 HOWTO (R_OR1K_32_PCREL,
194 2, /* size (0 = byte, 1 = short, 2 = long) */
196 TRUE, /* pc_relative */
198 complain_overflow_signed, /* complain_on_overflow */
199 bfd_elf_generic_reloc, /* special_function */
200 "R_OR1K_32_PCREL", /* name */
201 FALSE, /* partial_inplace */
203 0xffffffff, /* dst_mask */
204 TRUE), /* pcrel_offset */
206 HOWTO (R_OR1K_16_PCREL,
208 1, /* size (0 = byte, 1 = short, 2 = long) */
210 TRUE, /* pc_relative */
212 complain_overflow_signed, /* complain_on_overflow */
213 bfd_elf_generic_reloc, /* special_function */
214 "R_OR1K_16_PCREL", /* name */
215 FALSE, /* partial_inplace */
217 0xffff, /* dst_mask */
218 TRUE), /* pcrel_offset */
220 HOWTO (R_OR1K_8_PCREL,
222 0, /* size (0 = byte, 1 = short, 2 = long) */
224 TRUE, /* pc_relative */
226 complain_overflow_signed, /* complain_on_overflow */
227 bfd_elf_generic_reloc, /* special_function */
228 "R_OR1K_8_PCREL", /* name */
229 FALSE, /* partial_inplace */
232 TRUE), /* pcrel_offset */
234 HOWTO (R_OR1K_GOTPC_HI16, /* Type. */
235 16, /* Rightshift. */
236 2, /* Size (0 = byte, 1 = short, 2 = long). */
238 TRUE, /* PC_relative. */
240 complain_overflow_dont, /* Complain on overflow. */
241 bfd_elf_generic_reloc, /* Special Function. */
242 "R_OR1K_GOTPC_HI16", /* Name. */
243 FALSE, /* Partial Inplace. */
244 0, /* Source Mask. */
245 0xffff, /* Dest Mask. */
246 TRUE), /* PC relative offset? */
248 HOWTO (R_OR1K_GOTPC_LO16, /* Type. */
250 2, /* Size (0 = byte, 1 = short, 2 = long). */
252 TRUE, /* PC_relative. */
254 complain_overflow_dont, /* Complain on overflow. */
255 bfd_elf_generic_reloc, /* Special Function. */
256 "R_OR1K_GOTPC_LO16", /* Name. */
257 FALSE, /* Partial Inplace. */
258 0, /* Source Mask. */
259 0xffff, /* Dest Mask. */
260 TRUE), /* PC relative offset? */
262 HOWTO (R_OR1K_GOT16, /* type */
264 2, /* size (0 = byte, 1 = short, 2 = long) */
266 FALSE, /* pc_relative */
268 complain_overflow_signed, /* complain_on_overflow */
269 bfd_elf_generic_reloc, /* special_function */
270 "R_OR1K_GOT16", /* name */
271 FALSE, /* partial_inplace */
273 0xffff, /* dst_mask */
274 FALSE), /* pcrel_offset */
276 /* A 26 bit PLT relocation. Shifted by 2. */
277 HOWTO (R_OR1K_PLT26, /* Type. */
279 2, /* Size (0 = byte, 1 = short, 2 = long). */
281 TRUE, /* PC_relative. */
283 complain_overflow_signed, /* Complain on overflow. */
284 bfd_elf_generic_reloc,/* Special Function. */
285 "R_OR1K_PLT26", /* Name. */
286 FALSE, /* Partial Inplace. */
287 0, /* Source Mask. */
288 0x03ffffff, /* Dest Mask. */
289 TRUE), /* PC relative offset? */
291 HOWTO (R_OR1K_GOTOFF_HI16, /* type */
293 2, /* size (0 = byte, 1 = short, 2 = long) */
295 FALSE, /* pc_relative */
297 complain_overflow_dont, /* complain_on_overflow */
298 bfd_elf_generic_reloc, /* special_function */
299 "R_OR1K_GOTOFF_HI16", /* name */
300 FALSE, /* partial_inplace */
302 0xffff, /* dst_mask */
303 FALSE), /* pcrel_offset */
305 HOWTO (R_OR1K_GOTOFF_LO16, /* type */
307 2, /* size (0 = byte, 1 = short, 2 = long) */
309 FALSE, /* pc_relative */
311 complain_overflow_dont, /* complain_on_overflow */
312 bfd_elf_generic_reloc, /* special_function */
313 "R_OR1K_GOTOFF_LO16", /* name */
314 FALSE, /* partial_inplace */
316 0xffff, /* dst_mask */
317 FALSE), /* pcrel_offset */
319 HOWTO (R_OR1K_COPY, /* type */
321 2, /* size (0 = byte, 1 = short, 2 = long) */
323 FALSE, /* pc_relative */
325 complain_overflow_bitfield, /* complain_on_overflow */
326 bfd_elf_generic_reloc, /* special_function */
327 "R_OR1K_COPY", /* name */
328 FALSE, /* partial_inplace */
329 0xffffffff, /* src_mask */
330 0xffffffff, /* dst_mask */
331 FALSE), /* pcrel_offset */
333 HOWTO (R_OR1K_GLOB_DAT, /* type */
335 2, /* size (0 = byte, 1 = short, 2 = long) */
337 FALSE, /* pc_relative */
339 complain_overflow_bitfield, /* complain_on_overflow */
340 bfd_elf_generic_reloc, /* special_function */
341 "R_OR1K_GLOB_DAT", /* name */
342 FALSE, /* partial_inplace */
343 0xffffffff, /* src_mask */
344 0xffffffff, /* dst_mask */
345 FALSE), /* pcrel_offset */
347 HOWTO (R_OR1K_JMP_SLOT, /* type */
349 2, /* size (0 = byte, 1 = short, 2 = long) */
351 FALSE, /* pc_relative */
353 complain_overflow_bitfield, /* complain_on_overflow */
354 bfd_elf_generic_reloc, /* special_function */
355 "R_OR1K_JMP_SLOT", /* name */
356 FALSE, /* partial_inplace */
357 0xffffffff, /* src_mask */
358 0xffffffff, /* dst_mask */
359 FALSE), /* pcrel_offset */
361 HOWTO (R_OR1K_RELATIVE, /* type */
363 2, /* size (0 = byte, 1 = short, 2 = long) */
365 FALSE, /* pc_relative */
367 complain_overflow_bitfield, /* complain_on_overflow */
368 bfd_elf_generic_reloc, /* special_function */
369 "R_OR1K_RELATIVE", /* name */
370 FALSE, /* partial_inplace */
371 0xffffffff, /* src_mask */
372 0xffffffff, /* dst_mask */
373 FALSE), /* pcrel_offset */
375 HOWTO (R_OR1K_TLS_GD_HI16, /* type */
377 2, /* size (0 = byte, 1 = short, 2 = long) */
379 FALSE, /* pc_relative */
381 complain_overflow_dont, /* complain_on_overflow */
382 bfd_elf_generic_reloc, /* special_function */
383 "R_OR1K_TLS_GD_HI16", /* name */
384 FALSE, /* partial_inplace */
386 0xffff, /* dst_mask */
387 FALSE), /* pcrel_offset */
389 HOWTO (R_OR1K_TLS_GD_LO16, /* type */
391 2, /* size (0 = byte, 1 = short, 2 = long) */
393 FALSE, /* pc_relative */
395 complain_overflow_dont, /* complain_on_overflow */
396 bfd_elf_generic_reloc, /* special_function */
397 "R_OR1K_TLS_GD_LO16", /* name */
398 FALSE, /* partial_inplace */
400 0xffff, /* dst_mask */
401 FALSE), /* pcrel_offset */
403 HOWTO (R_OR1K_TLS_LDM_HI16, /* type */
405 2, /* size (0 = byte, 1 = short, 2 = long) */
407 FALSE, /* pc_relative */
409 complain_overflow_dont, /* complain_on_overflow */
410 bfd_elf_generic_reloc, /* special_function */
411 "R_OR1K_TLS_LDM_HI16", /* name */
412 FALSE, /* partial_inplace */
414 0xffff, /* dst_mask */
415 FALSE), /* pcrel_offset */
417 HOWTO (R_OR1K_TLS_LDM_LO16, /* type */
419 2, /* size (0 = byte, 1 = short, 2 = long) */
421 FALSE, /* pc_relative */
423 complain_overflow_dont, /* complain_on_overflow */
424 bfd_elf_generic_reloc, /* special_function */
425 "R_OR1K_TLS_LDM_LO16", /* name */
426 FALSE, /* partial_inplace */
428 0xffff, /* dst_mask */
429 FALSE), /* pcrel_offset */
431 HOWTO (R_OR1K_TLS_LDO_HI16, /* type */
433 2, /* size (0 = byte, 1 = short, 2 = long) */
435 FALSE, /* pc_relative */
437 complain_overflow_dont, /* complain_on_overflow */
438 bfd_elf_generic_reloc, /* special_function */
439 "R_OR1K_TLS_LDO_HI16", /* name */
440 FALSE, /* partial_inplace */
442 0xffff, /* dst_mask */
443 FALSE), /* pcrel_offset */
445 HOWTO (R_OR1K_TLS_LDO_LO16, /* type */
447 2, /* size (0 = byte, 1 = short, 2 = long) */
449 FALSE, /* pc_relative */
451 complain_overflow_dont, /* complain_on_overflow */
452 bfd_elf_generic_reloc, /* special_function */
453 "R_OR1K_TLS_LDO_LO16", /* name */
454 FALSE, /* partial_inplace */
456 0xffff, /* dst_mask */
457 FALSE), /* pcrel_offset */
459 HOWTO (R_OR1K_TLS_IE_HI16, /* type */
461 2, /* size (0 = byte, 1 = short, 2 = long) */
463 FALSE, /* pc_relative */
465 complain_overflow_dont, /* complain_on_overflow */
466 bfd_elf_generic_reloc, /* special_function */
467 "R_OR1K_TLS_IE_HI16", /* name */
468 FALSE, /* partial_inplace */
470 0xffff, /* dst_mask */
471 FALSE), /* pcrel_offset */
473 HOWTO (R_OR1K_TLS_IE_LO16, /* type */
475 2, /* size (0 = byte, 1 = short, 2 = long) */
477 FALSE, /* pc_relative */
479 complain_overflow_dont, /* complain_on_overflow */
480 bfd_elf_generic_reloc, /* special_function */
481 "R_OR1K_TLS_IE_LO16", /* name */
482 FALSE, /* partial_inplace */
484 0xffff, /* dst_mask */
485 FALSE), /* pcrel_offset */
487 HOWTO (R_OR1K_TLS_LE_HI16, /* type */
489 2, /* size (0 = byte, 1 = short, 2 = long) */
491 FALSE, /* pc_relative */
493 complain_overflow_dont, /* complain_on_overflow */
494 bfd_elf_generic_reloc, /* special_function */
495 "R_OR1K_TLS_LE_HI16", /* name */
496 FALSE, /* partial_inplace */
498 0xffff, /* dst_mask */
499 FALSE), /* pcrel_offset */
501 HOWTO (R_OR1K_TLS_LE_LO16, /* type */
503 2, /* size (0 = byte, 1 = short, 2 = long) */
505 FALSE, /* pc_relative */
507 complain_overflow_dont, /* complain_on_overflow */
508 bfd_elf_generic_reloc, /* special_function */
509 "R_OR1K_TLS_LE_LO16", /* name */
510 FALSE, /* partial_inplace */
512 0xffff, /* dst_mask */
513 FALSE), /* pcrel_offset */
515 HOWTO (R_OR1K_TLS_TPOFF, /* type */
517 2, /* size (0 = byte, 1 = short, 2 = long) */
519 FALSE, /* pc_relative */
521 complain_overflow_bitfield, /* complain_on_overflow */
522 bfd_elf_generic_reloc, /* special_function */
523 "R_OR1K_TLS_TPOFF", /* name */
524 FALSE, /* partial_inplace */
525 0xffffffff, /* src_mask */
526 0xffffffff, /* dst_mask */
527 FALSE), /* pcrel_offset */
529 HOWTO (R_OR1K_TLS_DTPOFF, /* type */
531 2, /* size (0 = byte, 1 = short, 2 = long) */
533 FALSE, /* pc_relative */
535 complain_overflow_bitfield, /* complain_on_overflow */
536 bfd_elf_generic_reloc, /* special_function */
537 "R_OR1K_TLS_DTPOFF", /* name */
538 FALSE, /* partial_inplace */
539 0xffffffff, /* src_mask */
540 0xffffffff, /* dst_mask */
541 FALSE), /* pcrel_offset */
543 HOWTO (R_OR1K_TLS_DTPMOD, /* type */
545 2, /* size (0 = byte, 1 = short, 2 = long) */
547 FALSE, /* pc_relative */
549 complain_overflow_bitfield, /* complain_on_overflow */
550 bfd_elf_generic_reloc, /* special_function */
551 "R_OR1K_TLS_DTPMOD", /* name */
552 FALSE, /* partial_inplace */
553 0xffffffff, /* src_mask */
554 0xffffffff, /* dst_mask */
555 FALSE), /* pcrel_offset */
557 HOWTO (R_OR1K_AHI16, /* type */
559 2, /* size (0 = byte, 1 = short, 2 = long) */
561 FALSE, /* pc_relative */
563 complain_overflow_dont, /* complain_on_overflow */
564 bfd_elf_generic_reloc, /* special_function */
565 "R_OR1K_AHI16", /* name */
566 FALSE, /* partial_inplace */
568 0xffff, /* dst_mask */
569 FALSE), /* pcrel_offset */
571 HOWTO (R_OR1K_GOTOFF_AHI16, /* type */
573 2, /* size (0 = byte, 1 = short, 2 = long) */
575 FALSE, /* pc_relative */
577 complain_overflow_dont, /* complain_on_overflow */
578 bfd_elf_generic_reloc, /* special_function */
579 "R_OR1K_GOTOFF_AHI16", /* name */
580 FALSE, /* partial_inplace */
582 0xffff, /* dst_mask */
583 FALSE), /* pcrel_offset */
585 HOWTO (R_OR1K_TLS_IE_AHI16, /* type */
587 2, /* size (0 = byte, 1 = short, 2 = long) */
589 FALSE, /* pc_relative */
591 complain_overflow_dont, /* complain_on_overflow */
592 bfd_elf_generic_reloc, /* special_function */
593 "R_OR1K_TLS_IE_AHI16", /* name */
594 FALSE, /* partial_inplace */
596 0xffff, /* dst_mask */
597 FALSE), /* pcrel_offset */
599 HOWTO (R_OR1K_TLS_LE_AHI16, /* type */
601 2, /* size (0 = byte, 1 = short, 2 = long) */
603 FALSE, /* pc_relative */
605 complain_overflow_dont, /* complain_on_overflow */
606 bfd_elf_generic_reloc, /* special_function */
607 "R_OR1K_TLS_LE_AHI16", /* name */
608 FALSE, /* partial_inplace */
610 0xffff, /* dst_mask */
611 FALSE), /* pcrel_offset */
613 HOWTO (R_OR1K_SLO16, /* type */
615 2, /* size (0 = byte, 1 = short, 2 = long) */
617 FALSE, /* pc_relative */
619 complain_overflow_dont, /* complain_on_overflow */
620 bfd_elf_generic_reloc, /* special_function */
621 "R_OR1K_SLO16", /* name */
622 FALSE, /* partial_inplace */
624 0xffff, /* dst_mask */
625 FALSE), /* pcrel_offset */
627 HOWTO (R_OR1K_GOTOFF_SLO16, /* type */
629 2, /* size (0 = byte, 1 = short, 2 = long) */
631 FALSE, /* pc_relative */
633 complain_overflow_dont, /* complain_on_overflow */
634 bfd_elf_generic_reloc, /* special_function */
635 "R_OR1K_GOTOFF_SLO16", /* name */
636 FALSE, /* partial_inplace */
638 0xffff, /* dst_mask */
639 FALSE), /* pcrel_offset */
641 HOWTO (R_OR1K_TLS_LE_SLO16, /* type */
643 2, /* size (0 = byte, 1 = short, 2 = long) */
645 FALSE, /* pc_relative */
647 complain_overflow_dont, /* complain_on_overflow */
648 bfd_elf_generic_reloc, /* special_function */
649 "R_OR1K_TLS_LE_SLO16", /* name */
650 FALSE, /* partial_inplace */
652 0xffff, /* dst_mask */
653 FALSE), /* pcrel_offset */
656 /* Map BFD reloc types to Or1k ELF reloc types. */
658 struct or1k_reloc_map
660 bfd_reloc_code_real_type bfd_reloc_val;
661 unsigned int or1k_reloc_val;
664 static const struct or1k_reloc_map or1k_reloc_map[] =
666 { BFD_RELOC_NONE, R_OR1K_NONE },
667 { BFD_RELOC_32, R_OR1K_32 },
668 { BFD_RELOC_16, R_OR1K_16 },
669 { BFD_RELOC_8, R_OR1K_8 },
670 { BFD_RELOC_LO16, R_OR1K_LO_16_IN_INSN },
671 { BFD_RELOC_HI16, R_OR1K_HI_16_IN_INSN },
672 { BFD_RELOC_HI16_S, R_OR1K_AHI16 },
673 { BFD_RELOC_OR1K_REL_26, R_OR1K_INSN_REL_26 },
674 { BFD_RELOC_VTABLE_ENTRY, R_OR1K_GNU_VTENTRY },
675 { BFD_RELOC_VTABLE_INHERIT, R_OR1K_GNU_VTINHERIT },
676 { BFD_RELOC_32_PCREL, R_OR1K_32_PCREL },
677 { BFD_RELOC_16_PCREL, R_OR1K_16_PCREL },
678 { BFD_RELOC_8_PCREL, R_OR1K_8_PCREL },
679 { BFD_RELOC_LO16_GOTOFF, R_OR1K_GOTOFF_LO16 },
680 { BFD_RELOC_HI16_GOTOFF, R_OR1K_GOTOFF_HI16 },
681 { BFD_RELOC_HI16_S_GOTOFF, R_OR1K_GOTOFF_AHI16 },
682 { BFD_RELOC_OR1K_GOTPC_HI16, R_OR1K_GOTPC_HI16 },
683 { BFD_RELOC_OR1K_GOTPC_LO16, R_OR1K_GOTPC_LO16 },
684 { BFD_RELOC_OR1K_GOT16, R_OR1K_GOT16 },
685 { BFD_RELOC_OR1K_PLT26, R_OR1K_PLT26 },
686 { BFD_RELOC_OR1K_GLOB_DAT, R_OR1K_GLOB_DAT },
687 { BFD_RELOC_OR1K_COPY, R_OR1K_COPY },
688 { BFD_RELOC_OR1K_JMP_SLOT, R_OR1K_JMP_SLOT },
689 { BFD_RELOC_OR1K_RELATIVE, R_OR1K_RELATIVE },
690 { BFD_RELOC_OR1K_TLS_GD_HI16, R_OR1K_TLS_GD_HI16 },
691 { BFD_RELOC_OR1K_TLS_GD_LO16, R_OR1K_TLS_GD_LO16 },
692 { BFD_RELOC_OR1K_TLS_LDM_HI16, R_OR1K_TLS_LDM_HI16 },
693 { BFD_RELOC_OR1K_TLS_LDM_LO16, R_OR1K_TLS_LDM_LO16 },
694 { BFD_RELOC_OR1K_TLS_LDO_HI16, R_OR1K_TLS_LDO_HI16 },
695 { BFD_RELOC_OR1K_TLS_LDO_LO16, R_OR1K_TLS_LDO_LO16 },
696 { BFD_RELOC_OR1K_TLS_IE_HI16, R_OR1K_TLS_IE_HI16 },
697 { BFD_RELOC_OR1K_TLS_IE_LO16, R_OR1K_TLS_IE_LO16 },
698 { BFD_RELOC_OR1K_TLS_IE_AHI16, R_OR1K_TLS_IE_AHI16 },
699 { BFD_RELOC_OR1K_TLS_LE_HI16, R_OR1K_TLS_LE_HI16 },
700 { BFD_RELOC_OR1K_TLS_LE_LO16, R_OR1K_TLS_LE_LO16 },
701 { BFD_RELOC_OR1K_TLS_LE_AHI16, R_OR1K_TLS_LE_AHI16 },
702 { BFD_RELOC_OR1K_SLO16, R_OR1K_SLO16 },
703 { BFD_RELOC_OR1K_GOTOFF_SLO16, R_OR1K_GOTOFF_SLO16 },
704 { BFD_RELOC_OR1K_TLS_LE_SLO16, R_OR1K_TLS_LE_SLO16 },
707 #define TLS_UNKNOWN 0
714 /* ELF linker hash entry. */
715 struct elf_or1k_link_hash_entry
717 struct elf_link_hash_entry root;
719 /* Track dynamic relocs copied for this symbol. */
720 struct elf_dyn_relocs *dyn_relocs;
722 /* Track type of TLS access. */
723 unsigned char tls_type;
726 /* ELF object data. */
727 struct elf_or1k_obj_tdata
729 struct elf_obj_tdata root;
731 /* tls_type for each local got entry. */
732 unsigned char *local_tls_type;
735 #define elf_or1k_tdata(abfd) \
736 ((struct elf_or1k_obj_tdata *) (abfd)->tdata.any)
738 #define elf_or1k_local_tls_type(abfd) \
739 (elf_or1k_tdata (abfd)->local_tls_type)
741 /* ELF linker hash table. */
742 struct elf_or1k_link_hash_table
744 struct elf_link_hash_table root;
746 /* Small local sym to section mapping cache. */
747 struct sym_cache sym_sec;
750 /* Get the ELF linker hash table from a link_info structure. */
751 #define or1k_elf_hash_table(p) \
752 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
753 == OR1K_ELF_DATA ? ((struct elf_or1k_link_hash_table *) ((p)->hash)) : NULL)
756 elf_or1k_mkobject (bfd *abfd)
758 return bfd_elf_allocate_object (abfd, sizeof (struct elf_or1k_obj_tdata),
762 /* Create an entry in an or1k ELF linker hash table. */
764 static struct bfd_hash_entry *
765 or1k_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
766 struct bfd_hash_table *table,
769 struct elf_or1k_link_hash_entry *ret =
770 (struct elf_or1k_link_hash_entry *) entry;
772 /* Allocate the structure if it has not already been allocated by a
775 ret = bfd_hash_allocate (table,
776 sizeof (struct elf_or1k_link_hash_entry));
780 /* Call the allocation method of the superclass. */
781 ret = ((struct elf_or1k_link_hash_entry *)
782 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
786 struct elf_or1k_link_hash_entry *eh;
788 eh = (struct elf_or1k_link_hash_entry *) ret;
789 eh->dyn_relocs = NULL;
790 eh->tls_type = TLS_UNKNOWN;
793 return (struct bfd_hash_entry *) ret;
796 /* Create an or1k ELF linker hash table. */
798 static struct bfd_link_hash_table *
799 or1k_elf_link_hash_table_create (bfd *abfd)
801 struct elf_or1k_link_hash_table *ret;
802 bfd_size_type amt = sizeof (struct elf_or1k_link_hash_table);
804 ret = bfd_zmalloc (amt);
808 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
809 or1k_elf_link_hash_newfunc,
810 sizeof (struct elf_or1k_link_hash_entry),
817 return &ret->root.root;
820 static reloc_howto_type *
821 or1k_reloc_type_lookup (bfd * abfd ATTRIBUTE_UNUSED,
822 bfd_reloc_code_real_type bcode)
826 for (i = 0; i < ARRAY_SIZE (or1k_reloc_map); i++)
827 if (or1k_reloc_map[i].bfd_reloc_val == bcode)
829 unsigned int ocode = or1k_reloc_map[i].or1k_reloc_val;
830 if (ocode < (unsigned int) R_OR1K_max)
831 return &or1k_elf_howto_table[ocode];
839 static reloc_howto_type *
840 or1k_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
845 for (i = 0; i < R_OR1K_max; i++)
846 if (or1k_elf_howto_table[i].name != NULL
847 && strcasecmp (or1k_elf_howto_table[i].name, r_name) == 0)
848 return &or1k_elf_howto_table[i];
853 /* Set the howto pointer for an Or1k ELF reloc. */
856 or1k_info_to_howto_rela (bfd * abfd,
858 Elf_Internal_Rela * dst)
862 r_type = ELF32_R_TYPE (dst->r_info);
863 if (r_type >= (unsigned int) R_OR1K_max)
865 /* xgettext:c-format */
866 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
868 bfd_set_error (bfd_error_bad_value);
871 cache_ptr->howto = & or1k_elf_howto_table[r_type];
876 /* Return the relocation value for @tpoff relocations.. */
878 tpoff (struct bfd_link_info *info, bfd_vma address)
880 /* If tls_sec is NULL, we should have signalled an error already. */
881 if (elf_hash_table (info)->tls_sec == NULL)
884 /* The thread pointer on or1k stores the address after the TCB where
885 the data is, just compute the difference. No need to compensate
886 for the size of TCB. */
887 return (address - elf_hash_table (info)->tls_sec->vma);
890 /* Like _bfd_final_link_relocate, but handles non-contiguous fields. */
892 static bfd_reloc_status_type
893 or1k_final_link_relocate (reloc_howto_type *howto, bfd *input_bfd,
894 asection *input_section, bfd_byte *contents,
895 bfd_vma offset, bfd_vma value)
897 bfd_reloc_status_type status = bfd_reloc_ok;
898 int size = bfd_get_reloc_size (howto);
901 /* Sanity check the address. */
902 if (offset + size > bfd_get_section_limit_octets (input_bfd, input_section))
903 return bfd_reloc_outofrange;
905 if (howto->pc_relative)
907 value -= (input_section->output_section->vma
908 + input_section->output_offset);
909 if (howto->pcrel_offset)
916 case R_OR1K_GOTOFF_AHI16:
917 case R_OR1K_TLS_IE_AHI16:
918 case R_OR1K_TLS_LE_AHI16:
919 /* Adjust the operand to match with a signed LO16. */
923 case R_OR1K_INSN_REL_26:
924 /* Diagnose mis-aligned branch targets. */
926 status = bfd_reloc_dangerous;
930 status = bfd_check_overflow (howto->complain_on_overflow,
933 bfd_arch_bits_per_address (input_bfd),
935 value >>= howto->rightshift;
937 /* If we're overwriting the entire destination,
938 then no need to read the current contents. */
939 if (size == 0 || howto->dst_mask == N_ONES (size))
943 BFD_ASSERT (size == 4);
944 x = bfd_get_32 (input_bfd, contents + offset);
950 case R_OR1K_GOTOFF_SLO16:
951 case R_OR1K_TLS_LE_SLO16:
952 /* The split imm16 field used for stores. */
953 x = (x & ~0x3e007ff) | ((value & 0xf800) << 10) | (value & 0x7ff);
958 bfd_vma fieldmask = howto->dst_mask;
959 value <<= howto->bitpos;
960 x = (x & ~fieldmask) | (value & fieldmask);
965 /* Put the relocated value back in the object file. */
971 bfd_put_8 (input_bfd, x, contents + offset);
974 bfd_put_16 (input_bfd, x, contents + offset);
977 bfd_put_32 (input_bfd, x, contents + offset);
981 bfd_put_64 (input_bfd, x, contents + offset);
986 (_("%pB: Cannot handle relocation value size of %d"),
993 /* Relocate an Or1k ELF section.
995 The RELOCATE_SECTION function is called by the new ELF backend linker
996 to handle the relocations for a section.
998 The relocs are always passed as Rela structures; if the section
999 actually uses Rel structures, the r_addend field will always be
1002 This function is responsible for adjusting the section contents as
1003 necessary, and (if using Rela relocs and generating a relocatable
1004 output file) adjusting the reloc addend as necessary.
1006 This function does not have to worry about setting the reloc
1007 address or the reloc symbol index.
1009 LOCAL_SYMS is a pointer to the swapped in local symbols.
1011 LOCAL_SECTIONS is an array giving the section in the input file
1012 corresponding to the st_shndx field of each local symbol.
1014 The global hash table entry for the global symbols can be found
1015 via elf_sym_hashes (input_bfd).
1017 When generating relocatable output, this function must handle
1018 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
1019 going to be the section symbol corresponding to the output
1020 section, which means that the addend must be adjusted
1024 or1k_elf_relocate_section (bfd *output_bfd,
1025 struct bfd_link_info *info,
1027 asection *input_section,
1029 Elf_Internal_Rela *relocs,
1030 Elf_Internal_Sym *local_syms,
1031 asection **local_sections)
1033 Elf_Internal_Shdr *symtab_hdr;
1034 struct elf_link_hash_entry **sym_hashes;
1035 Elf_Internal_Rela *rel;
1036 Elf_Internal_Rela *relend;
1037 struct elf_or1k_link_hash_table *htab = or1k_elf_hash_table (info);
1040 bfd_vma *local_got_offsets;
1041 asection *sgot, *splt;
1042 bfd_vma plt_base, got_base;
1043 bfd_boolean ret_val = TRUE;
1048 dynobj = htab->root.dynobj;
1049 local_got_offsets = elf_local_got_offsets (input_bfd);
1051 sreloc = elf_section_data (input_section)->sreloc;
1053 splt = htab->root.splt;
1056 plt_base = splt->output_section->vma + splt->output_offset;
1058 sgot = htab->root.sgot;
1061 got_base = sgot->output_section->vma + sgot->output_offset;
1063 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1064 sym_hashes = elf_sym_hashes (input_bfd);
1065 relend = relocs + input_section->reloc_count;
1067 for (rel = relocs; rel < relend; rel++)
1069 reloc_howto_type *howto;
1070 unsigned long r_symndx;
1071 Elf_Internal_Sym *sym;
1073 struct elf_link_hash_entry *h;
1075 bfd_reloc_status_type r;
1076 const char *name = NULL;
1079 r_type = ELF32_R_TYPE (rel->r_info);
1080 r_symndx = ELF32_R_SYM (rel->r_info);
1082 if (r_type == R_OR1K_GNU_VTINHERIT
1083 || r_type == R_OR1K_GNU_VTENTRY)
1086 if (r_type < 0 || r_type >= (int) R_OR1K_max)
1089 (_("%pB: unknown relocation type %d"),
1090 input_bfd, (int) r_type);
1091 bfd_set_error (bfd_error_bad_value);
1096 howto = or1k_elf_howto_table + ELF32_R_TYPE (rel->r_info);
1101 if (r_symndx < symtab_hdr->sh_info)
1103 sym = local_syms + r_symndx;
1104 sec = local_sections[r_symndx];
1105 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
1107 name = bfd_elf_string_from_elf_section
1108 (input_bfd, symtab_hdr->sh_link, sym->st_name);
1109 name = (name == NULL) ? bfd_section_name (input_bfd, sec) : name;
1113 bfd_boolean unresolved_reloc, warned, ignored;
1115 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
1116 r_symndx, symtab_hdr, sym_hashes,
1118 unresolved_reloc, warned, ignored);
1119 name = h->root.root.string;
1122 if (sec != NULL && discarded_section (sec))
1123 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
1124 rel, 1, relend, howto, 0, contents);
1126 if (bfd_link_relocatable (info))
1129 switch (howto->type)
1132 /* If the call is not local, redirect the branch to the PLT.
1133 Otherwise do nothing to send the branch to the symbol direct. */
1134 if (!SYMBOL_CALLS_LOCAL (info, h))
1136 BFD_ASSERT (h->plt.offset != (bfd_vma) -1);
1137 relocation = plt_base + h->plt.offset;
1140 /* Addend should be zero. */
1141 if (rel->r_addend != 0)
1144 (_("%pB: addend should be zero for plt relocations"),
1146 bfd_set_error (bfd_error_bad_value);
1152 /* Relocation is to the entry for this symbol in the global
1154 BFD_ASSERT (sgot != NULL);
1160 off = h->got.offset;
1161 BFD_ASSERT (off != (bfd_vma) -1);
1163 dyn = htab->root.dynamic_sections_created;
1164 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
1165 bfd_link_pic (info),
1167 || (bfd_link_pic (info)
1168 && SYMBOL_REFERENCES_LOCAL (info, h)))
1170 /* This is actually a static link, or it is a
1171 -Bsymbolic link and the symbol is defined
1172 locally, or the symbol was forced to be local
1173 because of a version file. We must initialize
1174 this entry in the global offset table. Since the
1175 offset must always be a multiple of 4, we use the
1176 least significant bit to record whether we have
1177 initialized it already.
1179 When doing a dynamic link, we create a .rela.got
1180 relocation entry to initialize the value. This
1181 is done in the finish_dynamic_symbol routine. */
1186 /* Write entry in GOT. */
1187 bfd_put_32 (output_bfd, relocation,
1188 sgot->contents + off);
1189 /* Mark GOT entry as having been written. */
1194 relocation = sgot->output_offset + off;
1201 BFD_ASSERT (local_got_offsets != NULL
1202 && local_got_offsets[r_symndx] != (bfd_vma) -1);
1204 /* Get offset into GOT table. */
1205 off = local_got_offsets[r_symndx];
1207 /* The offset must always be a multiple of 4. We use
1208 the least significant bit to record whether we have
1209 already processed this entry. */
1214 /* Write entry in GOT. */
1215 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
1216 if (bfd_link_pic (info))
1219 Elf_Internal_Rela outrel;
1221 /* We need to generate a R_OR1K_RELATIVE reloc
1222 for the dynamic linker. */
1223 srelgot = htab->root.srelgot;
1224 BFD_ASSERT (srelgot != NULL);
1226 outrel.r_offset = got_base + off;
1227 outrel.r_info = ELF32_R_INFO (0, R_OR1K_RELATIVE);
1228 outrel.r_addend = relocation;
1229 loc = srelgot->contents;
1230 loc += srelgot->reloc_count * sizeof (Elf32_External_Rela);
1231 bfd_elf32_swap_reloca_out (output_bfd, &outrel,loc);
1232 ++srelgot->reloc_count;
1235 local_got_offsets[r_symndx] |= 1;
1237 relocation = sgot->output_offset + off;
1240 /* Addend should be zero. */
1241 if (rel->r_addend != 0)
1244 (_("%pB: addend should be zero for got relocations"),
1246 bfd_set_error (bfd_error_bad_value);
1251 case R_OR1K_GOTOFF_LO16:
1252 case R_OR1K_GOTOFF_HI16:
1253 case R_OR1K_GOTOFF_AHI16:
1254 case R_OR1K_GOTOFF_SLO16:
1255 /* Relocation is offset from GOT. */
1256 BFD_ASSERT (sgot != NULL);
1257 if (!SYMBOL_REFERENCES_LOCAL (info, h))
1260 (_("%pB: gotoff relocation against dynamic symbol %s"),
1261 input_bfd, h->root.root.string);
1263 bfd_set_error (bfd_error_bad_value);
1265 relocation -= got_base;
1268 case R_OR1K_INSN_REL_26:
1269 /* For a non-shared link, these will reference either the plt
1270 or a .dynbss copy of the symbol. */
1271 if (bfd_link_pic (info) && !SYMBOL_REFERENCES_LOCAL (info, h))
1274 (_("%pB: pc-relative relocation against dynamic symbol %s"),
1277 bfd_set_error (bfd_error_bad_value);
1281 case R_OR1K_HI_16_IN_INSN:
1282 case R_OR1K_LO_16_IN_INSN:
1285 if (bfd_link_pic (info))
1288 (_("%pB: non-pic relocation against symbol %s"),
1291 bfd_set_error (bfd_error_bad_value);
1298 /* r_symndx will be STN_UNDEF (zero) only for relocs against symbols
1299 from removed linkonce sections, or sections discarded by
1301 if (r_symndx == STN_UNDEF
1302 || (input_section->flags & SEC_ALLOC) == 0)
1305 /* Emit a direct relocation if the symbol is dynamic,
1306 or a RELATIVE reloc for shared objects. We can omit
1307 RELATIVE relocs to local undefweak symbols. */
1308 if (bfd_link_pic (info)
1310 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1311 || h->root.type != bfd_link_hash_undefweak)
1315 && ((h->def_dynamic && !h->def_regular)
1316 || h->root.type == bfd_link_hash_undefweak
1317 || h->root.type == bfd_link_hash_undefined)))
1319 Elf_Internal_Rela outrel;
1323 /* When generating a shared object, these relocations
1324 are copied into the output file to be resolved at run
1327 BFD_ASSERT (sreloc != NULL);
1332 _bfd_elf_section_offset (output_bfd, info, input_section,
1334 if (outrel.r_offset == (bfd_vma) -1)
1336 else if (outrel.r_offset == (bfd_vma) -2)
1338 outrel.r_offset += (input_section->output_section->vma
1339 + input_section->output_offset);
1342 memset (&outrel, 0, sizeof outrel);
1343 else if (SYMBOL_REFERENCES_LOCAL (info, h))
1345 outrel.r_info = ELF32_R_INFO (0, R_OR1K_RELATIVE);
1346 outrel.r_addend = relocation + rel->r_addend;
1350 BFD_ASSERT (h->dynindx != -1);
1351 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
1352 outrel.r_addend = rel->r_addend;
1355 loc = sreloc->contents;
1356 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
1357 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1363 case R_OR1K_TLS_LDM_HI16:
1364 case R_OR1K_TLS_LDM_LO16:
1365 case R_OR1K_TLS_LDO_HI16:
1366 case R_OR1K_TLS_LDO_LO16:
1367 /* TODO: implement support for local dynamic. */
1370 (_("%pB: support for local dynamic not implemented"),
1372 bfd_set_error (bfd_error_bad_value);
1375 case R_OR1K_TLS_GD_HI16:
1376 case R_OR1K_TLS_GD_LO16:
1377 case R_OR1K_TLS_IE_HI16:
1378 case R_OR1K_TLS_IE_LO16:
1379 case R_OR1K_TLS_IE_AHI16:
1382 Elf_Internal_Rela rela;
1386 sreloc = bfd_get_section_by_name (dynobj, ".rela.got");
1388 /* Mark as TLS related GOT entry by setting
1389 bit 2 as well as bit 1. */
1392 gotoff = h->got.offset;
1397 gotoff = local_got_offsets[r_symndx];
1398 local_got_offsets[r_symndx] |= 3;
1401 /* Only process the relocation once. */
1404 relocation = sgot->output_offset + (gotoff & ~3);
1408 BFD_ASSERT (elf_hash_table (info)->hgot == NULL
1409 || elf_hash_table (info)->hgot->root.u.def.value == 0);
1411 /* Dynamic entries will require relocations. if we do not need
1412 them we will just use the default R_OR1K_NONE and
1413 not set anything. */
1414 dynamic = bfd_link_pic (info)
1415 || (sec && (sec->flags & SEC_ALLOC) != 0
1417 && (h->root.type == bfd_link_hash_defweak || !h->def_regular));
1420 if (dynamic && (howto->type == R_OR1K_TLS_GD_HI16
1421 || howto->type == R_OR1K_TLS_GD_LO16))
1425 /* Add DTPMOD and DTPOFF GOT and rela entries. */
1426 for (i = 0; i < 2; ++i)
1428 rela.r_offset = got_base + gotoff + i*4;
1429 if (h != NULL && h->dynindx != -1)
1431 rela.r_info = ELF32_R_INFO (h->dynindx,
1432 (i == 0 ? R_OR1K_TLS_DTPMOD : R_OR1K_TLS_DTPOFF));
1437 rela.r_info = ELF32_R_INFO (0,
1438 (i == 0 ? R_OR1K_TLS_DTPMOD : R_OR1K_TLS_DTPOFF));
1439 rela.r_addend = tpoff (info, relocation);
1442 loc = sreloc->contents;
1443 loc += sreloc->reloc_count++ *
1444 sizeof (Elf32_External_Rela);
1446 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
1447 bfd_put_32 (output_bfd, 0, sgot->contents + gotoff + i*4);
1451 else if (howto->type == R_OR1K_TLS_GD_HI16
1452 || howto->type == R_OR1K_TLS_GD_LO16)
1454 bfd_put_32 (output_bfd, 1, sgot->contents + gotoff);
1455 bfd_put_32 (output_bfd, tpoff (info, relocation),
1456 sgot->contents + gotoff + 4);
1461 /* Add TPOFF GOT and rela entries. */
1462 rela.r_offset = got_base + gotoff;
1463 if (h != NULL && h->dynindx != -1)
1465 rela.r_info = ELF32_R_INFO (h->dynindx, R_OR1K_TLS_TPOFF);
1470 rela.r_info = ELF32_R_INFO (0, R_OR1K_TLS_TPOFF);
1471 rela.r_addend = tpoff (info, relocation);
1474 loc = sreloc->contents;
1475 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
1477 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
1478 bfd_put_32 (output_bfd, 0, sgot->contents + gotoff);
1483 bfd_put_32 (output_bfd, tpoff (info, relocation),
1484 sgot->contents + gotoff);
1486 relocation = sgot->output_offset + gotoff;
1490 case R_OR1K_TLS_LE_HI16:
1491 case R_OR1K_TLS_LE_LO16:
1492 case R_OR1K_TLS_LE_AHI16:
1493 case R_OR1K_TLS_LE_SLO16:
1494 /* Relocation is offset from TP. */
1495 relocation = tpoff (info, relocation);
1498 case R_OR1K_TLS_DTPMOD:
1499 case R_OR1K_TLS_DTPOFF:
1500 case R_OR1K_TLS_TPOFF:
1501 /* These are resolved dynamically on load and shouldn't
1502 be used as linker input. */
1505 (_("%pB: will not resolve runtime TLS relocation"),
1507 bfd_set_error (bfd_error_bad_value);
1514 r = or1k_final_link_relocate (howto, input_bfd, input_section, contents,
1515 rel->r_offset, relocation + rel->r_addend);
1517 if (r != bfd_reloc_ok)
1519 const char *msg = NULL;
1523 case bfd_reloc_overflow:
1524 (*info->callbacks->reloc_overflow)
1525 (info, (h ? &h->root : NULL), name, howto->name,
1526 (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
1529 case bfd_reloc_undefined:
1530 (*info->callbacks->undefined_symbol)
1531 (info, name, input_bfd, input_section, rel->r_offset, TRUE);
1534 case bfd_reloc_outofrange:
1535 msg = _("internal error: out of range error");
1538 case bfd_reloc_notsupported:
1539 msg = _("internal error: unsupported relocation error");
1542 case bfd_reloc_dangerous:
1543 msg = _("internal error: dangerous relocation");
1547 msg = _("internal error: unknown error");
1552 (*info->callbacks->warning) (info, msg, name, input_bfd,
1553 input_section, rel->r_offset);
1560 /* Return the section that should be marked against GC for a given
1564 or1k_elf_gc_mark_hook (asection *sec,
1565 struct bfd_link_info *info,
1566 Elf_Internal_Rela *rel,
1567 struct elf_link_hash_entry *h,
1568 Elf_Internal_Sym *sym)
1571 switch (ELF32_R_TYPE (rel->r_info))
1573 case R_OR1K_GNU_VTINHERIT:
1574 case R_OR1K_GNU_VTENTRY:
1578 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
1581 /* Look through the relocs for a section during the first phase. */
1584 or1k_elf_check_relocs (bfd *abfd,
1585 struct bfd_link_info *info,
1587 const Elf_Internal_Rela *relocs)
1589 Elf_Internal_Shdr *symtab_hdr;
1590 struct elf_link_hash_entry **sym_hashes;
1591 const Elf_Internal_Rela *rel;
1593 const Elf_Internal_Rela *rel_end;
1594 struct elf_or1k_link_hash_table *htab;
1596 asection *sreloc = NULL;
1598 if (bfd_link_relocatable (info))
1601 /* Don't do anything special with non-loaded, non-alloced sections.
1602 In particular, any relocs in such sections should not affect GOT
1603 and PLT reference counting (ie. we don't allow them to create GOT
1604 or PLT entries), there's no possibility or desire to optimize TLS
1605 relocs, and there's not much point in propagating relocs to shared
1606 libs that the dynamic linker won't relocate. */
1607 if ((sec->flags & SEC_ALLOC) == 0)
1610 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1611 sym_hashes = elf_sym_hashes (abfd);
1613 htab = or1k_elf_hash_table (info);
1617 dynobj = htab->root.dynobj;
1619 rel_end = relocs + sec->reloc_count;
1620 for (rel = relocs; rel < rel_end; rel++)
1622 struct elf_link_hash_entry *h;
1623 unsigned long r_symndx;
1624 unsigned char tls_type;
1627 r_symndx = ELF32_R_SYM (rel->r_info);
1628 if (r_symndx < symtab_hdr->sh_info)
1632 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1633 while (h->root.type == bfd_link_hash_indirect
1634 || h->root.type == bfd_link_hash_warning)
1635 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1638 r_type = ELF32_R_TYPE (rel->r_info);
1641 case R_OR1K_TLS_GD_HI16:
1642 case R_OR1K_TLS_GD_LO16:
1645 case R_OR1K_TLS_LDM_HI16:
1646 case R_OR1K_TLS_LDM_LO16:
1647 case R_OR1K_TLS_LDO_HI16:
1648 case R_OR1K_TLS_LDO_LO16:
1651 case R_OR1K_TLS_IE_HI16:
1652 case R_OR1K_TLS_IE_LO16:
1653 case R_OR1K_TLS_IE_AHI16:
1656 case R_OR1K_TLS_LE_HI16:
1657 case R_OR1K_TLS_LE_LO16:
1658 case R_OR1K_TLS_LE_AHI16:
1659 case R_OR1K_TLS_LE_SLO16:
1663 tls_type = TLS_NONE;
1666 /* Record TLS type. */
1668 ((struct elf_or1k_link_hash_entry *) h)->tls_type = tls_type;
1671 unsigned char *local_tls_type;
1673 /* This is a TLS type record for a local symbol. */
1674 local_tls_type = (unsigned char *) elf_or1k_local_tls_type (abfd);
1675 if (local_tls_type == NULL)
1679 size = symtab_hdr->sh_info;
1680 local_tls_type = bfd_zalloc (abfd, size);
1681 if (local_tls_type == NULL)
1683 elf_or1k_local_tls_type (abfd) = local_tls_type;
1685 local_tls_type[r_symndx] = tls_type;
1690 /* This relocation describes the C++ object vtable hierarchy.
1691 Reconstruct it for later use during GC. */
1692 case R_OR1K_GNU_VTINHERIT:
1693 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
1697 /* This relocation describes which C++ vtable entries are actually
1698 used. Record for later use during GC. */
1699 case R_OR1K_GNU_VTENTRY:
1700 BFD_ASSERT (h != NULL);
1702 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
1706 /* This relocation requires .plt entry. */
1711 h->plt.refcount += 1;
1716 case R_OR1K_TLS_GD_HI16:
1717 case R_OR1K_TLS_GD_LO16:
1718 case R_OR1K_TLS_IE_HI16:
1719 case R_OR1K_TLS_IE_LO16:
1720 case R_OR1K_TLS_IE_AHI16:
1722 h->got.refcount += 1;
1725 bfd_signed_vma *local_got_refcounts;
1727 /* This is a global offset table entry for a local symbol. */
1728 local_got_refcounts = elf_local_got_refcounts (abfd);
1729 if (local_got_refcounts == NULL)
1733 size = symtab_hdr->sh_info;
1734 size *= sizeof (bfd_signed_vma);
1735 local_got_refcounts = bfd_zalloc (abfd, size);
1736 if (local_got_refcounts == NULL)
1738 elf_local_got_refcounts (abfd) = local_got_refcounts;
1740 local_got_refcounts[r_symndx] += 1;
1744 case R_OR1K_GOTOFF_HI16:
1745 case R_OR1K_GOTOFF_LO16:
1746 case R_OR1K_GOTOFF_AHI16:
1747 case R_OR1K_GOTOFF_SLO16:
1748 if (htab->root.sgot == NULL)
1751 htab->root.dynobj = dynobj = abfd;
1752 if (!_bfd_elf_create_got_section (dynobj, info))
1757 case R_OR1K_INSN_REL_26:
1758 case R_OR1K_HI_16_IN_INSN:
1759 case R_OR1K_LO_16_IN_INSN:
1764 if (h != NULL && !bfd_link_pic (info))
1766 /* We may need a copy reloc. */
1769 /* We may also need a .plt entry. */
1770 h->plt.refcount += 1;
1771 if (r_type != R_OR1K_INSN_REL_26)
1772 h->pointer_equality_needed = 1;
1775 /* If we are creating a shared library, and this is a reloc
1776 against a global symbol, or a non PC relative reloc
1777 against a local symbol, then we need to copy the reloc
1778 into the shared library. However, if we are linking with
1779 -Bsymbolic, we do not need to copy a reloc against a
1780 global symbol which is defined in an object we are
1781 including in the link (i.e., DEF_REGULAR is set). At
1782 this point we have not seen all the input files, so it is
1783 possible that DEF_REGULAR is not set now but will be set
1784 later (it is never cleared). In case of a weak definition,
1785 DEF_REGULAR may be cleared later by a strong definition in
1786 a shared library. We account for that possibility below by
1787 storing information in the relocs_copied field of the hash
1788 table entry. A similar situation occurs when creating
1789 shared libraries and symbol visibility changes render the
1792 If on the other hand, we are creating an executable, we
1793 may need to keep relocations for symbols satisfied by a
1794 dynamic library if we manage to avoid copy relocs for the
1797 if ((bfd_link_pic (info)
1798 && (sec->flags & SEC_ALLOC) != 0
1799 && (r_type != R_OR1K_INSN_REL_26
1801 && (!SYMBOLIC_BIND (info, h)
1802 || h->root.type == bfd_link_hash_defweak
1803 || !h->def_regular))))
1804 || (!bfd_link_pic (info)
1805 && (sec->flags & SEC_ALLOC) != 0
1807 && (h->root.type == bfd_link_hash_defweak
1808 || !h->def_regular)))
1810 struct elf_dyn_relocs *p;
1811 struct elf_dyn_relocs **head;
1813 /* When creating a shared object, we must copy these
1814 relocs into the output file. We create a reloc
1815 section in dynobj and make room for the reloc. */
1819 unsigned int strndx = elf_elfheader (abfd)->e_shstrndx;
1820 unsigned int shnam = _bfd_elf_single_rel_hdr (sec)->sh_name;
1822 name = bfd_elf_string_from_elf_section (abfd, strndx, shnam);
1826 if (strncmp (name, ".rela", 5) != 0
1827 || strcmp (bfd_get_section_name (abfd, sec),
1831 /* xgettext:c-format */
1832 (_("%pB: bad relocation section name `%s\'"),
1836 if (htab->root.dynobj == NULL)
1837 htab->root.dynobj = abfd;
1838 dynobj = htab->root.dynobj;
1840 sreloc = bfd_get_section_by_name (dynobj, name);
1843 sreloc = _bfd_elf_make_dynamic_reloc_section
1844 (sec, dynobj, 2, abfd, /*rela?*/ TRUE);
1849 elf_section_data (sec)->sreloc = sreloc;
1852 /* If this is a global symbol, we count the number of
1853 relocations we need for this symbol. */
1855 head = &((struct elf_or1k_link_hash_entry *) h)->dyn_relocs;
1858 /* Track dynamic relocs needed for local syms too.
1859 We really need local syms available to do this
1863 Elf_Internal_Sym *isym;
1866 isym = bfd_sym_from_r_symndx (&htab->sym_sec,
1871 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
1875 vpp = &elf_section_data (s)->local_dynrel;
1876 head = (struct elf_dyn_relocs **) vpp;
1880 if (p == NULL || p->sec != sec)
1882 bfd_size_type amt = sizeof *p;
1883 p = ((struct elf_dyn_relocs *)
1884 bfd_alloc (htab->root.dynobj, amt));
1895 if (r_type == R_OR1K_INSN_REL_26)
1906 /* Finish up the dynamic sections. */
1909 or1k_elf_finish_dynamic_sections (bfd *output_bfd,
1910 struct bfd_link_info *info)
1913 asection *sdyn, *sgot;
1914 struct elf_or1k_link_hash_table *htab;
1916 htab = or1k_elf_hash_table (info);
1920 dynobj = htab->root.dynobj;
1922 sgot = htab->root.sgotplt;
1923 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
1925 if (htab->root.dynamic_sections_created)
1928 Elf32_External_Dyn *dyncon, *dynconend;
1930 BFD_ASSERT (sgot != NULL && sdyn != NULL);
1932 dyncon = (Elf32_External_Dyn *) sdyn->contents;
1933 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
1935 for (; dyncon < dynconend; dyncon++)
1937 Elf_Internal_Dyn dyn;
1940 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
1948 s = htab->root.sgotplt;
1949 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
1953 s = htab->root.srelplt;
1954 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
1958 s = htab->root.srelplt;
1959 dyn.d_un.d_val = s->size;
1962 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1966 /* Fill in the first entry in the procedure linkage table. */
1967 splt = htab->root.splt;
1968 if (splt && splt->size > 0)
1970 if (bfd_link_pic (info))
1972 bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD0,
1974 bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD1,
1975 splt->contents + 4);
1976 bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD2,
1977 splt->contents + 8);
1978 bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD3,
1979 splt->contents + 12);
1980 bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD4,
1981 splt->contents + 16);
1986 /* addr = .got + 4 */
1987 addr = sgot->output_section->vma + sgot->output_offset + 4;
1988 bfd_put_32 (output_bfd,
1989 PLT0_ENTRY_WORD0 | ((addr >> 16) & 0xffff),
1991 bfd_put_32 (output_bfd,
1992 PLT0_ENTRY_WORD1 | (addr & 0xffff),
1993 splt->contents + 4);
1994 bfd_put_32 (output_bfd, PLT0_ENTRY_WORD2, splt->contents + 8);
1995 bfd_put_32 (output_bfd, PLT0_ENTRY_WORD3, splt->contents + 12);
1996 bfd_put_32 (output_bfd, PLT0_ENTRY_WORD4, splt->contents + 16);
1999 elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4;
2003 /* Set the first entry in the global offset table to the address of
2004 the dynamic section. */
2005 if (sgot && sgot->size > 0)
2008 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
2010 bfd_put_32 (output_bfd,
2011 sdyn->output_section->vma + sdyn->output_offset,
2013 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
2016 if (htab->root.sgot && htab->root.sgot->size > 0)
2017 elf_section_data (htab->root.sgot->output_section)->this_hdr.sh_entsize = 4;
2022 /* Finish up dynamic symbol handling. We set the contents of various
2023 dynamic sections here. */
2026 or1k_elf_finish_dynamic_symbol (bfd *output_bfd,
2027 struct bfd_link_info *info,
2028 struct elf_link_hash_entry *h,
2029 Elf_Internal_Sym *sym)
2031 struct elf_or1k_link_hash_table *htab;
2034 htab = or1k_elf_hash_table (info);
2038 if (h->plt.offset != (bfd_vma) -1)
2047 Elf_Internal_Rela rela;
2049 /* This symbol has an entry in the procedure linkage table. Set
2051 BFD_ASSERT (h->dynindx != -1);
2053 splt = htab->root.splt;
2054 sgot = htab->root.sgotplt;
2055 srela = htab->root.srelplt;
2056 BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
2058 /* Get the index in the procedure linkage table which
2059 corresponds to this symbol. This is the index of this symbol
2060 in all the symbols for which we are making plt entries. The
2061 first entry in the procedure linkage table is reserved. */
2062 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
2064 /* Get the offset into the .got table of the entry that
2065 corresponds to this function. Each .got entry is 4 bytes.
2066 The first three are reserved. */
2067 got_offset = (plt_index + 3) * 4;
2068 got_addr = got_offset;
2070 /* Fill in the entry in the procedure linkage table. */
2071 if (! bfd_link_pic (info))
2073 got_addr += htab->root.sgotplt->output_section->vma
2074 + htab->root.sgotplt->output_offset;
2075 bfd_put_32 (output_bfd, PLT_ENTRY_WORD0 | ((got_addr >> 16) & 0xffff),
2076 splt->contents + h->plt.offset);
2077 bfd_put_32 (output_bfd, PLT_ENTRY_WORD1 | (got_addr & 0xffff),
2078 splt->contents + h->plt.offset + 4);
2079 bfd_put_32 (output_bfd, (bfd_vma) PLT_ENTRY_WORD2,
2080 splt->contents + h->plt.offset + 8);
2081 bfd_put_32 (output_bfd, (bfd_vma) PLT_ENTRY_WORD3,
2082 splt->contents + h->plt.offset + 12);
2083 bfd_put_32 (output_bfd, PLT_ENTRY_WORD4
2084 | plt_index * sizeof (Elf32_External_Rela),
2085 splt->contents + h->plt.offset + 16);
2089 bfd_put_32 (output_bfd, PLT_PIC_ENTRY_WORD0 | (got_addr & 0xffff),
2090 splt->contents + h->plt.offset);
2091 bfd_put_32 (output_bfd, PLT_PIC_ENTRY_WORD1
2092 | plt_index * sizeof (Elf32_External_Rela),
2093 splt->contents + h->plt.offset + 4);
2094 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC_ENTRY_WORD2,
2095 splt->contents + h->plt.offset + 8);
2096 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC_ENTRY_WORD3,
2097 splt->contents + h->plt.offset + 12);
2098 bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC_ENTRY_WORD4,
2099 splt->contents + h->plt.offset + 16);
2102 /* Fill in the entry in the global offset table. */
2103 bfd_put_32 (output_bfd,
2104 (splt->output_section->vma
2105 + splt->output_offset), /* Same offset. */
2106 sgot->contents + got_offset);
2108 /* Fill in the entry in the .rela.plt section. */
2109 rela.r_offset = (sgot->output_section->vma
2110 + sgot->output_offset
2112 rela.r_info = ELF32_R_INFO (h->dynindx, R_OR1K_JMP_SLOT);
2114 loc = srela->contents;
2115 loc += plt_index * sizeof (Elf32_External_Rela);
2116 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
2118 if (!h->def_regular)
2120 /* Mark the symbol as undefined, rather than as defined in
2121 the .plt section. Leave the value alone. */
2122 sym->st_shndx = SHN_UNDEF;
2127 if (h->got.offset != (bfd_vma) -1
2128 && (h->got.offset & 2) == 0) /* Homemade TLS check. */
2132 Elf_Internal_Rela rela;
2134 /* This symbol has an entry in the global offset table. Set it
2136 sgot = htab->root.sgot;
2137 srela = htab->root.srelgot;
2138 BFD_ASSERT (sgot != NULL && srela != NULL);
2140 rela.r_offset = (sgot->output_section->vma
2141 + sgot->output_offset
2142 + (h->got.offset &~ 1));
2144 /* If this is a -Bsymbolic link, and the symbol is defined
2145 locally, we just want to emit a RELATIVE reloc. Likewise if
2146 the symbol was forced to be local because of a version file.
2147 The entry in the global offset table will already have been
2148 initialized in the relocate_section function. */
2149 if (bfd_link_pic (info) && SYMBOL_REFERENCES_LOCAL (info, h))
2151 rela.r_info = ELF32_R_INFO (0, R_OR1K_RELATIVE);
2152 rela.r_addend = (h->root.u.def.value
2153 + h->root.u.def.section->output_section->vma
2154 + h->root.u.def.section->output_offset);
2158 BFD_ASSERT ((h->got.offset & 1) == 0);
2159 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
2160 rela.r_info = ELF32_R_INFO (h->dynindx, R_OR1K_GLOB_DAT);
2164 loc = srela->contents;
2165 loc += srela->reloc_count * sizeof (Elf32_External_Rela);
2166 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
2167 ++srela->reloc_count;
2173 Elf_Internal_Rela rela;
2175 /* This symbols needs a copy reloc. Set it up. */
2176 BFD_ASSERT (h->dynindx != -1
2177 && (h->root.type == bfd_link_hash_defined
2178 || h->root.type == bfd_link_hash_defweak));
2180 rela.r_offset = (h->root.u.def.value
2181 + h->root.u.def.section->output_section->vma
2182 + h->root.u.def.section->output_offset);
2183 rela.r_info = ELF32_R_INFO (h->dynindx, R_OR1K_COPY);
2185 if (h->root.u.def.section == htab->root.sdynrelro)
2186 s = htab->root.sreldynrelro;
2188 s = htab->root.srelbss;
2189 loc = s->contents + s->reloc_count * sizeof (Elf32_External_Rela);
2190 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
2194 /* Mark some specially defined symbols as absolute. */
2195 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
2196 || h == htab->root.hgot)
2197 sym->st_shndx = SHN_ABS;
2202 static enum elf_reloc_type_class
2203 or1k_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
2204 const asection *rel_sec ATTRIBUTE_UNUSED,
2205 const Elf_Internal_Rela *rela)
2207 switch ((int) ELF32_R_TYPE (rela->r_info))
2209 case R_OR1K_RELATIVE: return reloc_class_relative;
2210 case R_OR1K_JMP_SLOT: return reloc_class_plt;
2211 case R_OR1K_COPY: return reloc_class_copy;
2212 default: return reloc_class_normal;
2216 /* Find dynamic relocs for H that apply to read-only sections. */
2219 readonly_dynrelocs (struct elf_link_hash_entry *h)
2221 struct elf_dyn_relocs *p;
2222 struct elf_or1k_link_hash_entry *eh = (struct elf_or1k_link_hash_entry *) h;
2224 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2226 asection *s = p->sec->output_section;
2228 if (s != NULL && (s->flags & SEC_READONLY) != 0)
2234 /* Adjust a symbol defined by a dynamic object and referenced by a
2235 regular object. The current definition is in some section of the
2236 dynamic object, but we're not including those sections. We have to
2237 change the definition to something the rest of the link can
2241 or1k_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
2242 struct elf_link_hash_entry *h)
2244 struct elf_or1k_link_hash_table *htab;
2248 dynobj = elf_hash_table (info)->dynobj;
2250 /* Make sure we know what is going on here. */
2251 BFD_ASSERT (dynobj != NULL
2256 && !h->def_regular)));
2258 /* If this is a function, put it in the procedure linkage table. We
2259 will fill in the contents of the procedure linkage table later,
2260 when we know the address of the .got section. */
2261 if (h->type == STT_FUNC
2264 if (! bfd_link_pic (info)
2267 && h->root.type != bfd_link_hash_undefweak
2268 && h->root.type != bfd_link_hash_undefined)
2270 /* This case can occur if we saw a PLT reloc in an input
2271 file, but the symbol was never referred to by a dynamic
2272 object. In such a case, we don't actually need to build
2273 a procedure linkage table, and we can just do a PCREL
2275 h->plt.offset = (bfd_vma) -1;
2282 h->plt.offset = (bfd_vma) -1;
2284 /* If this is a weak symbol, and there is a real definition, the
2285 processor independent code will have arranged for us to see the
2286 real definition first, and we can just use the same value. */
2287 if (h->is_weakalias)
2289 struct elf_link_hash_entry *def = weakdef (h);
2290 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
2291 h->root.u.def.section = def->root.u.def.section;
2292 h->root.u.def.value = def->root.u.def.value;
2296 /* This is a reference to a symbol defined by a dynamic object which
2297 is not a function. */
2299 /* If we are creating a shared library, we must presume that the
2300 only references to the symbol are via the global offset table.
2301 For such cases we need not do anything here; the relocations will
2302 be handled correctly by relocate_section. */
2303 if (bfd_link_pic (info))
2306 /* If there are no references to this symbol that do not use the
2307 GOT, we don't need to generate a copy reloc. */
2308 if (!h->non_got_ref)
2311 /* If -z nocopyreloc was given, we won't generate them either. */
2312 if (info->nocopyreloc)
2318 /* If we don't find any dynamic relocs in read-only sections, then
2319 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
2320 if (!readonly_dynrelocs (h))
2326 /* We must allocate the symbol in our .dynbss section, which will
2327 become part of the .bss section of the executable. There will be
2328 an entry for this symbol in the .dynsym section. The dynamic
2329 object will contain position independent code, so all references
2330 from the dynamic object to this symbol will go through the global
2331 offset table. The dynamic linker will use the .dynsym entry to
2332 determine the address it must put in the global offset table, so
2333 both the dynamic object and the regular object will refer to the
2334 same memory location for the variable. */
2336 htab = or1k_elf_hash_table (info);
2340 /* We must generate a R_OR1K_COPY reloc to tell the dynamic linker
2341 to copy the initial value out of the dynamic object and into the
2342 runtime process image. We need to remember the offset into the
2343 .rela.bss section we are going to use. */
2344 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
2346 s = htab->root.sdynrelro;
2347 srel = htab->root.sreldynrelro;
2351 s = htab->root.sdynbss;
2352 srel = htab->root.srelbss;
2354 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
2356 srel->size += sizeof (Elf32_External_Rela);
2360 return _bfd_elf_adjust_dynamic_copy (info, h, s);
2363 /* Allocate space in .plt, .got and associated reloc sections for
2367 allocate_dynrelocs (struct elf_link_hash_entry *h, void * inf)
2369 struct bfd_link_info *info;
2370 struct elf_or1k_link_hash_table *htab;
2371 struct elf_or1k_link_hash_entry *eh;
2372 struct elf_dyn_relocs *p;
2374 if (h->root.type == bfd_link_hash_indirect)
2377 info = (struct bfd_link_info *) inf;
2378 htab = or1k_elf_hash_table (info);
2382 eh = (struct elf_or1k_link_hash_entry *) h;
2384 if (htab->root.dynamic_sections_created
2385 && h->plt.refcount > 0)
2387 /* Make sure this symbol is output as a dynamic symbol.
2388 Undefined weak syms won't yet be marked as dynamic. */
2389 if (h->dynindx == -1
2390 && !h->forced_local)
2392 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2396 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info), h))
2398 asection *s = htab->root.splt;
2400 /* If this is the first .plt entry, make room for the special
2403 s->size = PLT_ENTRY_SIZE;
2405 h->plt.offset = s->size;
2407 /* If this symbol is not defined in a regular file, and we are
2408 not generating a shared library, then set the symbol to this
2409 location in the .plt. This is required to make function
2410 pointers compare as equal between the normal executable and
2411 the shared library. */
2412 if (! bfd_link_pic (info)
2415 h->root.u.def.section = s;
2416 h->root.u.def.value = h->plt.offset;
2419 /* Make room for this entry. */
2420 s->size += PLT_ENTRY_SIZE;
2422 /* We also need to make an entry in the .got.plt section, which
2423 will be placed in the .got section by the linker script. */
2424 htab->root.sgotplt->size += 4;
2426 /* We also need to make an entry in the .rel.plt section. */
2427 htab->root.srelplt->size += sizeof (Elf32_External_Rela);
2431 h->plt.offset = (bfd_vma) -1;
2437 h->plt.offset = (bfd_vma) -1;
2441 if (h->got.refcount > 0)
2445 unsigned char tls_type;
2447 /* Make sure this symbol is output as a dynamic symbol.
2448 Undefined weak syms won't yet be marked as dynamic. */
2449 if (h->dynindx == -1
2450 && !h->forced_local)
2452 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2456 s = htab->root.sgot;
2458 h->got.offset = s->size;
2460 tls_type = ((struct elf_or1k_link_hash_entry *) h)->tls_type;
2462 /* TLS GD requires two GOT and two relocs. */
2463 if (tls_type == TLS_GD)
2467 dyn = htab->root.dynamic_sections_created;
2468 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h))
2470 if (tls_type == TLS_GD)
2471 htab->root.srelgot->size += 2 * sizeof (Elf32_External_Rela);
2473 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
2477 h->got.offset = (bfd_vma) -1;
2479 if (eh->dyn_relocs == NULL)
2482 /* In the shared -Bsymbolic case, discard space allocated for
2483 dynamic pc-relative relocs against symbols which turn out to be
2484 defined in regular objects. For the normal shared case, discard
2485 space for pc-relative relocs that have become local due to symbol
2486 visibility changes. */
2488 if (bfd_link_pic (info))
2490 if (SYMBOL_CALLS_LOCAL (info, h))
2492 struct elf_dyn_relocs **pp;
2494 for (pp = &eh->dyn_relocs; (p = *pp) != NULL;)
2496 p->count -= p->pc_count;
2505 /* Also discard relocs on undefined weak syms with non-default
2507 if (eh->dyn_relocs != NULL
2508 && h->root.type == bfd_link_hash_undefweak)
2510 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2511 eh->dyn_relocs = NULL;
2513 /* Make sure undefined weak symbols are output as a dynamic
2515 else if (h->dynindx == -1
2516 && !h->forced_local)
2518 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2525 /* For the non-shared case, discard space for relocs against
2526 symbols which turn out to need copy relocs or are not
2532 || (htab->root.dynamic_sections_created
2533 && (h->root.type == bfd_link_hash_undefweak
2534 || h->root.type == bfd_link_hash_undefined))))
2536 /* Make sure this symbol is output as a dynamic symbol.
2537 Undefined weak syms won't yet be marked as dynamic. */
2538 if (h->dynindx == -1
2539 && !h->forced_local)
2541 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2545 /* If that succeeded, we know we'll be keeping all the
2547 if (h->dynindx != -1)
2551 eh->dyn_relocs = NULL;
2556 /* Finally, allocate space. */
2557 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2559 asection *sreloc = elf_section_data (p->sec)->sreloc;
2560 sreloc->size += p->count * sizeof (Elf32_External_Rela);
2566 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
2567 read-only sections. */
2570 maybe_set_textrel (struct elf_link_hash_entry *h, void *info_p)
2574 if (h->root.type == bfd_link_hash_indirect)
2577 sec = readonly_dynrelocs (h);
2580 struct bfd_link_info *info = (struct bfd_link_info *) info_p;
2582 info->flags |= DF_TEXTREL;
2583 info->callbacks->minfo
2584 (_("%pB: dynamic relocation against `%pT' in read-only section `%pA'\n"),
2585 sec->owner, h->root.root.string, sec);
2587 /* Not an error, just cut short the traversal. */
2593 /* Set the sizes of the dynamic sections. */
2596 or1k_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
2597 struct bfd_link_info *info)
2599 struct elf_or1k_link_hash_table *htab;
2605 htab = or1k_elf_hash_table (info);
2609 dynobj = htab->root.dynobj;
2610 BFD_ASSERT (dynobj != NULL);
2612 if (htab->root.dynamic_sections_created)
2614 /* Set the contents of the .interp section to the interpreter. */
2615 if (bfd_link_executable (info) && !info->nointerp)
2617 s = bfd_get_section_by_name (dynobj, ".interp");
2618 BFD_ASSERT (s != NULL);
2619 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
2620 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2624 /* Set up .got offsets for local syms, and space for local dynamic
2626 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
2628 bfd_signed_vma *local_got;
2629 bfd_signed_vma *end_local_got;
2630 bfd_size_type locsymcount;
2631 Elf_Internal_Shdr *symtab_hdr;
2632 unsigned char *local_tls_type;
2635 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
2638 for (s = ibfd->sections; s != NULL; s = s->next)
2640 struct elf_dyn_relocs *p;
2642 for (p = ((struct elf_dyn_relocs *)
2643 elf_section_data (s)->local_dynrel);
2647 if (! bfd_is_abs_section (p->sec)
2648 && bfd_is_abs_section (p->sec->output_section))
2650 /* Input section has been discarded, either because
2651 it is a copy of a linkonce section or due to
2652 linker script /DISCARD/, so we'll be discarding
2655 else if (p->count != 0)
2657 srel = elf_section_data (p->sec)->sreloc;
2658 srel->size += p->count * sizeof (Elf32_External_Rela);
2659 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
2660 info->flags |= DF_TEXTREL;
2665 local_got = elf_local_got_refcounts (ibfd);
2669 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
2670 locsymcount = symtab_hdr->sh_info;
2671 end_local_got = local_got + locsymcount;
2672 s = htab->root.sgot;
2673 srel = htab->root.srelgot;
2674 local_tls_type = (unsigned char *) elf_or1k_local_tls_type (ibfd);
2675 for (; local_got < end_local_got; ++local_got)
2679 *local_got = s->size;
2681 /* TLS GD requires two GOT and two relocs. */
2682 if (local_tls_type != NULL && *local_tls_type == TLS_GD)
2686 if (bfd_link_pic (info))
2688 if (local_tls_type != NULL && *local_tls_type == TLS_GD)
2689 srel->size += 2 * sizeof (Elf32_External_Rela);
2691 srel->size += sizeof (Elf32_External_Rela);
2696 *local_got = (bfd_vma) -1;
2703 /* Allocate global sym .plt and .got entries, and space for global
2704 sym dynamic relocs. */
2705 elf_link_hash_traverse (&htab->root, allocate_dynrelocs, info);
2707 /* We now have determined the sizes of the various dynamic sections.
2708 Allocate memory for them. */
2710 for (s = dynobj->sections; s != NULL; s = s->next)
2712 if ((s->flags & SEC_LINKER_CREATED) == 0)
2715 if (s == htab->root.splt
2716 || s == htab->root.sgot
2717 || s == htab->root.sgotplt
2718 || s == htab->root.sdynbss
2719 || s == htab->root.sdynrelro)
2721 /* Strip this section if we don't need it; see the
2724 else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rela"))
2726 if (s->size != 0 && s != htab->root.srelplt)
2729 /* We use the reloc_count field as a counter if we need
2730 to copy relocs into the output file. */
2734 /* It's not one of our sections, so don't allocate space. */
2739 /* If we don't need this section, strip it from the
2740 output file. This is mostly to handle .rela.bss and
2741 .rela.plt. We must create both sections in
2742 create_dynamic_sections, because they must be created
2743 before the linker maps input sections to output
2744 sections. The linker does that before
2745 adjust_dynamic_symbol is called, and it is that
2746 function which decides whether anything needs to go
2747 into these sections. */
2748 s->flags |= SEC_EXCLUDE;
2752 if ((s->flags & SEC_HAS_CONTENTS) == 0)
2755 /* Allocate memory for the section contents. We use bfd_zalloc
2756 here in case unused entries are not reclaimed before the
2757 section's contents are written out. This should not happen,
2758 but this way if it does, we get a R_OR1K_NONE reloc instead
2760 s->contents = bfd_zalloc (dynobj, s->size);
2762 if (s->contents == NULL)
2766 if (htab->root.dynamic_sections_created)
2768 /* Add some entries to the .dynamic section. We fill in the
2769 values later, in or1k_elf_finish_dynamic_sections, but we
2770 must add the entries now so that we get the correct size for
2771 the .dynamic section. The DT_DEBUG entry is filled in by the
2772 dynamic linker and used by the debugger. */
2773 #define add_dynamic_entry(TAG, VAL) \
2774 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
2776 if (bfd_link_executable (info))
2778 if (! add_dynamic_entry (DT_DEBUG, 0))
2782 if (htab->root.splt->size != 0)
2784 if (! add_dynamic_entry (DT_PLTGOT, 0)
2785 || ! add_dynamic_entry (DT_PLTRELSZ, 0)
2786 || ! add_dynamic_entry (DT_PLTREL, DT_RELA)
2787 || ! add_dynamic_entry (DT_JMPREL, 0))
2793 if (! add_dynamic_entry (DT_RELA, 0)
2794 || ! add_dynamic_entry (DT_RELASZ, 0)
2795 || ! add_dynamic_entry (DT_RELAENT,
2796 sizeof (Elf32_External_Rela)))
2799 /* If any dynamic relocs apply to a read-only section,
2800 then we need a DT_TEXTREL entry. */
2801 if ((info->flags & DF_TEXTREL) == 0)
2802 elf_link_hash_traverse (&htab->root, maybe_set_textrel, info);
2804 if ((info->flags & DF_TEXTREL) != 0)
2806 if (! add_dynamic_entry (DT_TEXTREL, 0))
2812 #undef add_dynamic_entry
2816 /* Copy the extra info we tack onto an elf_link_hash_entry. */
2819 or1k_elf_copy_indirect_symbol (struct bfd_link_info *info,
2820 struct elf_link_hash_entry *dir,
2821 struct elf_link_hash_entry *ind)
2823 struct elf_or1k_link_hash_entry * edir;
2824 struct elf_or1k_link_hash_entry * eind;
2826 edir = (struct elf_or1k_link_hash_entry *) dir;
2827 eind = (struct elf_or1k_link_hash_entry *) ind;
2829 if (eind->dyn_relocs != NULL)
2831 if (edir->dyn_relocs != NULL)
2833 struct elf_dyn_relocs **pp;
2834 struct elf_dyn_relocs *p;
2836 /* Add reloc counts against the indirect sym to the direct sym
2837 list. Merge any entries against the same section. */
2838 for (pp = &eind->dyn_relocs; (p = *pp) != NULL;)
2840 struct elf_dyn_relocs *q;
2842 for (q = edir->dyn_relocs; q != NULL; q = q->next)
2843 if (q->sec == p->sec)
2845 q->pc_count += p->pc_count;
2846 q->count += p->count;
2853 *pp = edir->dyn_relocs;
2856 edir->dyn_relocs = eind->dyn_relocs;
2857 eind->dyn_relocs = NULL;
2860 if (ind->root.type == bfd_link_hash_indirect)
2862 if (dir->got.refcount <= 0)
2864 edir->tls_type = eind->tls_type;
2865 eind->tls_type = TLS_UNKNOWN;
2869 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
2872 /* Set the right machine number. */
2875 or1k_elf_object_p (bfd *abfd)
2877 unsigned long mach = bfd_mach_or1k;
2879 if (elf_elfheader (abfd)->e_flags & EF_OR1K_NODELAY)
2880 mach = bfd_mach_or1knd;
2882 return bfd_default_set_arch_mach (abfd, bfd_arch_or1k, mach);
2885 /* Store the machine number in the flags field. */
2888 or1k_elf_final_write_processing (bfd *abfd,
2889 bfd_boolean linker ATTRIBUTE_UNUSED)
2891 switch (bfd_get_mach (abfd))
2896 case bfd_mach_or1knd:
2897 elf_elfheader (abfd)->e_flags |= EF_OR1K_NODELAY;
2903 or1k_elf_set_private_flags (bfd *abfd, flagword flags)
2905 BFD_ASSERT (!elf_flags_init (abfd)
2906 || elf_elfheader (abfd)->e_flags == flags);
2908 elf_elfheader (abfd)->e_flags = flags;
2909 elf_flags_init (abfd) = TRUE;
2913 /* Make sure all input files are consistent with respect to
2914 EF_OR1K_NODELAY flag setting. */
2917 elf32_or1k_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
2919 bfd *obfd = info->output_bfd;
2923 in_flags = elf_elfheader (ibfd)->e_flags;
2924 out_flags = elf_elfheader (obfd)->e_flags;
2926 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2927 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2930 if (!elf_flags_init (obfd))
2932 elf_flags_init (obfd) = TRUE;
2933 elf_elfheader (obfd)->e_flags = in_flags;
2938 if (in_flags == out_flags)
2941 if ((in_flags & EF_OR1K_NODELAY) != (out_flags & EF_OR1K_NODELAY))
2944 (_("%pB: %s flag mismatch with previous modules"),
2945 ibfd, "EF_OR1K_NODELAY");
2947 bfd_set_error (bfd_error_bad_value);
2955 #define ELF_ARCH bfd_arch_or1k
2956 #define ELF_MACHINE_CODE EM_OR1K
2957 #define ELF_TARGET_ID OR1K_ELF_DATA
2958 #define ELF_MAXPAGESIZE 0x2000
2960 #define TARGET_BIG_SYM or1k_elf32_vec
2961 #define TARGET_BIG_NAME "elf32-or1k"
2963 #define elf_info_to_howto_rel NULL
2964 #define elf_info_to_howto or1k_info_to_howto_rela
2965 #define elf_backend_relocate_section or1k_elf_relocate_section
2966 #define elf_backend_gc_mark_hook or1k_elf_gc_mark_hook
2967 #define elf_backend_check_relocs or1k_elf_check_relocs
2968 #define elf_backend_reloc_type_class or1k_elf_reloc_type_class
2969 #define elf_backend_can_gc_sections 1
2970 #define elf_backend_rela_normal 1
2972 #define bfd_elf32_mkobject elf_or1k_mkobject
2974 #define bfd_elf32_bfd_merge_private_bfd_data elf32_or1k_merge_private_bfd_data
2975 #define bfd_elf32_bfd_set_private_flags or1k_elf_set_private_flags
2976 #define bfd_elf32_bfd_reloc_type_lookup or1k_reloc_type_lookup
2977 #define bfd_elf32_bfd_reloc_name_lookup or1k_reloc_name_lookup
2979 #define elf_backend_object_p or1k_elf_object_p
2980 #define elf_backend_final_write_processing or1k_elf_final_write_processing
2981 #define elf_backend_can_refcount 1
2983 #define elf_backend_plt_readonly 1
2984 #define elf_backend_want_got_plt 1
2985 #define elf_backend_want_plt_sym 0
2986 #define elf_backend_got_header_size 12
2987 #define elf_backend_dtrel_excludes_plt 1
2988 #define elf_backend_want_dynrelro 1
2990 #define bfd_elf32_bfd_link_hash_table_create or1k_elf_link_hash_table_create
2991 #define elf_backend_copy_indirect_symbol or1k_elf_copy_indirect_symbol
2992 #define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
2993 #define elf_backend_finish_dynamic_sections or1k_elf_finish_dynamic_sections
2994 #define elf_backend_size_dynamic_sections or1k_elf_size_dynamic_sections
2995 #define elf_backend_adjust_dynamic_symbol or1k_elf_adjust_dynamic_symbol
2996 #define elf_backend_finish_dynamic_symbol or1k_elf_finish_dynamic_symbol
2998 #include "elf32-target.h"