Fix TARGET_CHAR_BIT/HOST_CHAR_BIT confusion in gmp-utils.c
[binutils-gdb.git] / bfd / elf32-nios2.c
blob153bfadc619ac14ce95d5d24d98034ce30ac321a
1 /* 32-bit ELF support for Nios II.
2 Copyright (C) 2012-2020 Free Software Foundation, Inc.
3 Contributed by Nigel Gray (ngray@altera.com).
4 Contributed by Mentor Graphics, Inc.
6 This file is part of BFD, the Binary File Descriptor library.
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 3 of the License, or
11 (at your option) any later version.
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.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
23 /* This file handles Altera Nios II ELF targets. */
25 #include "sysdep.h"
26 #include "bfd.h"
27 #include "libbfd.h"
28 #include "bfdlink.h"
29 #include "genlink.h"
30 #include "elf-bfd.h"
31 #include "elf/nios2.h"
32 #include "opcode/nios2.h"
33 #include "elf32-nios2.h"
34 #include "libiberty.h"
36 /* Use RELA relocations. */
37 #ifndef USE_RELA
38 #define USE_RELA
39 #endif
41 #ifdef USE_REL
42 #undef USE_REL
43 #endif
45 /* Forward declarations. */
46 static bfd_reloc_status_type nios2_elf32_ignore_reloc
47 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
48 static bfd_reloc_status_type nios2_elf32_hi16_relocate
49 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
50 static bfd_reloc_status_type nios2_elf32_lo16_relocate
51 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
52 static bfd_reloc_status_type nios2_elf32_hiadj16_relocate
53 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
54 static bfd_reloc_status_type nios2_elf32_pcrel_lo16_relocate
55 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
56 static bfd_reloc_status_type nios2_elf32_pcrel_hiadj16_relocate
57 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
58 static bfd_reloc_status_type nios2_elf32_pcrel16_relocate
59 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
60 static bfd_reloc_status_type nios2_elf32_call26_relocate
61 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
62 static bfd_reloc_status_type nios2_elf32_gprel_relocate
63 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
64 static bfd_reloc_status_type nios2_elf32_ujmp_relocate
65 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
66 static bfd_reloc_status_type nios2_elf32_cjmp_relocate
67 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
68 static bfd_reloc_status_type nios2_elf32_callr_relocate
69 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
71 /* Target vector. */
72 extern const bfd_target nios2_elf32_le_vec;
73 extern const bfd_target nios2_elf32_be_vec;
75 /* Offset of tp and dtp pointers from start of TLS block. */
76 #define TP_OFFSET 0x7000
77 #define DTP_OFFSET 0x8000
79 /* The relocation tables used for SHT_REL sections. There are separate
80 tables for R1 and R2 encodings. */
81 static reloc_howto_type elf_nios2_r1_howto_table_rel[] = {
82 /* No relocation. */
83 HOWTO (R_NIOS2_NONE, /* type */
84 0, /* rightshift */
85 3, /* size (0 = byte, 1 = short, 2 = long) */
86 0, /* bitsize */
87 FALSE, /* pc_relative */
88 0, /* bitpos */
89 complain_overflow_dont, /* complain_on_overflow */
90 bfd_elf_generic_reloc, /* special_function */
91 "R_NIOS2_NONE", /* name */
92 FALSE, /* partial_inplace */
93 0, /* src_mask */
94 0, /* dst_mask */
95 FALSE), /* pcrel_offset */
97 /* 16-bit signed immediate relocation. */
98 HOWTO (R_NIOS2_S16, /* type */
99 0, /* rightshift */
100 2, /* size (0 = byte, 1 = short, 2 = long) */
101 16, /* bitsize */
102 FALSE, /* pc_relative */
103 6, /* bitpos */
104 complain_overflow_signed, /* complain on overflow */
105 bfd_elf_generic_reloc, /* special function */
106 "R_NIOS2_S16", /* name */
107 FALSE, /* partial_inplace */
108 0x003fffc0, /* src_mask */
109 0x003fffc0, /* dest_mask */
110 FALSE), /* pcrel_offset */
112 /* 16-bit unsigned immediate relocation. */
113 HOWTO (R_NIOS2_U16, /* type */
114 0, /* rightshift */
115 2, /* size (0 = byte, 1 = short, 2 = long) */
116 16, /* bitsize */
117 FALSE, /* pc_relative */
118 6, /* bitpos */
119 complain_overflow_unsigned, /* complain on overflow */
120 bfd_elf_generic_reloc, /* special function */
121 "R_NIOS2_U16", /* name */
122 FALSE, /* partial_inplace */
123 0x003fffc0, /* src_mask */
124 0x003fffc0, /* dest_mask */
125 FALSE), /* pcrel_offset */
127 HOWTO (R_NIOS2_PCREL16, /* type */
128 0, /* rightshift */
129 2, /* size (0 = byte, 1 = short, 2 = long) */
130 16, /* bitsize */
131 TRUE, /* pc_relative */
132 6, /* bitpos */
133 complain_overflow_signed, /* complain on overflow */
134 nios2_elf32_pcrel16_relocate, /* special function */
135 "R_NIOS2_PCREL16", /* name */
136 FALSE, /* partial_inplace */
137 0x003fffc0, /* src_mask */
138 0x003fffc0, /* dest_mask */
139 TRUE), /* pcrel_offset */
141 HOWTO (R_NIOS2_CALL26, /* type */
142 2, /* rightshift */
143 2, /* size (0 = byte, 1 = short, 2 = long) */
144 26, /* bitsize */
145 FALSE, /* pc_relative */
146 6, /* bitpos */
147 complain_overflow_dont, /* complain on overflow */
148 nios2_elf32_call26_relocate, /* special function */
149 "R_NIOS2_CALL26", /* name */
150 FALSE, /* partial_inplace */
151 0xffffffc0, /* src_mask */
152 0xffffffc0, /* dst_mask */
153 FALSE), /* pcrel_offset */
155 HOWTO (R_NIOS2_IMM5,
159 FALSE,
161 complain_overflow_bitfield,
162 bfd_elf_generic_reloc,
163 "R_NIOS2_IMM5",
164 FALSE,
165 0x000007c0,
166 0x000007c0,
167 FALSE),
169 HOWTO (R_NIOS2_CACHE_OPX,
173 FALSE,
175 complain_overflow_bitfield,
176 bfd_elf_generic_reloc,
177 "R_NIOS2_CACHE_OPX",
178 FALSE,
179 0x07c00000,
180 0x07c00000,
181 FALSE),
183 HOWTO (R_NIOS2_IMM6,
187 FALSE,
189 complain_overflow_bitfield,
190 bfd_elf_generic_reloc,
191 "R_NIOS2_IMM6",
192 FALSE,
193 0x00000fc0,
194 0x00000fc0,
195 FALSE),
197 HOWTO (R_NIOS2_IMM8,
201 FALSE,
203 complain_overflow_bitfield,
204 bfd_elf_generic_reloc,
205 "R_NIOS2_IMM8",
206 FALSE,
207 0x00003fc0,
208 0x00003fc0,
209 FALSE),
211 HOWTO (R_NIOS2_HI16,
215 FALSE,
217 complain_overflow_dont,
218 nios2_elf32_hi16_relocate,
219 "R_NIOS2_HI16",
220 FALSE,
221 0x003fffc0,
222 0x003fffc0,
223 FALSE),
225 HOWTO (R_NIOS2_LO16,
229 FALSE,
231 complain_overflow_dont,
232 nios2_elf32_lo16_relocate,
233 "R_NIOS2_LO16",
234 FALSE,
235 0x003fffc0,
236 0x003fffc0,
237 FALSE),
239 HOWTO (R_NIOS2_HIADJ16,
243 FALSE,
245 complain_overflow_dont,
246 nios2_elf32_hiadj16_relocate,
247 "R_NIOS2_HIADJ16",
248 FALSE,
249 0x003fffc0,
250 0x003fffc0,
251 FALSE),
253 HOWTO (R_NIOS2_BFD_RELOC_32,
255 2, /* long */
257 FALSE,
259 complain_overflow_dont,
260 bfd_elf_generic_reloc,
261 "R_NIOS2_BFD_RELOC32",
262 FALSE,
263 0xffffffff,
264 0xffffffff,
265 FALSE),
267 HOWTO (R_NIOS2_BFD_RELOC_16,
269 1, /* short */
271 FALSE,
273 complain_overflow_bitfield,
274 bfd_elf_generic_reloc,
275 "R_NIOS2_BFD_RELOC16",
276 FALSE,
277 0x0000ffff,
278 0x0000ffff,
279 FALSE),
281 HOWTO (R_NIOS2_BFD_RELOC_8,
283 0, /* byte */
285 FALSE,
287 complain_overflow_bitfield,
288 bfd_elf_generic_reloc,
289 "R_NIOS2_BFD_RELOC8",
290 FALSE,
291 0x000000ff,
292 0x000000ff,
293 FALSE),
295 HOWTO (R_NIOS2_GPREL,
299 FALSE,
301 complain_overflow_dont,
302 nios2_elf32_gprel_relocate,
303 "R_NIOS2_GPREL",
304 FALSE,
305 0x003fffc0,
306 0x003fffc0,
307 FALSE),
309 HOWTO (R_NIOS2_GNU_VTINHERIT,
311 2, /* short */
313 FALSE,
315 complain_overflow_dont,
316 NULL,
317 "R_NIOS2_GNU_VTINHERIT",
318 FALSE,
321 FALSE),
323 HOWTO (R_NIOS2_GNU_VTENTRY,
325 2, /* byte */
327 FALSE,
329 complain_overflow_dont,
330 _bfd_elf_rel_vtable_reloc_fn,
331 "R_NIOS2_GNU_VTENTRY",
332 FALSE,
335 FALSE),
337 HOWTO (R_NIOS2_UJMP,
341 FALSE,
343 complain_overflow_dont,
344 nios2_elf32_ujmp_relocate,
345 "R_NIOS2_UJMP",
346 FALSE,
347 0x003fffc0,
348 0x003fffc0,
349 FALSE),
351 HOWTO (R_NIOS2_CJMP,
355 FALSE,
357 complain_overflow_dont,
358 nios2_elf32_cjmp_relocate,
359 "R_NIOS2_CJMP",
360 FALSE,
361 0x003fffc0,
362 0x003fffc0,
363 FALSE),
365 HOWTO (R_NIOS2_CALLR,
369 FALSE,
371 complain_overflow_dont,
372 nios2_elf32_callr_relocate,
373 "R_NIOS2_CALLR",
374 FALSE,
375 0x003fffc0,
376 0x003fffc0,
377 FALSE),
379 HOWTO (R_NIOS2_ALIGN,
383 FALSE,
385 complain_overflow_dont,
386 nios2_elf32_ignore_reloc,
387 "R_NIOS2_ALIGN",
388 FALSE,
391 TRUE),
394 HOWTO (R_NIOS2_GOT16,
398 FALSE,
400 complain_overflow_bitfield,
401 bfd_elf_generic_reloc,
402 "R_NIOS2_GOT16",
403 FALSE,
404 0x003fffc0,
405 0x003fffc0,
406 FALSE),
408 HOWTO (R_NIOS2_CALL16,
412 FALSE,
414 complain_overflow_bitfield,
415 bfd_elf_generic_reloc,
416 "R_NIOS2_CALL16",
417 FALSE,
418 0x003fffc0,
419 0x003fffc0,
420 FALSE),
422 HOWTO (R_NIOS2_GOTOFF_LO,
426 FALSE,
428 complain_overflow_dont,
429 bfd_elf_generic_reloc,
430 "R_NIOS2_GOTOFF_LO",
431 FALSE,
432 0x003fffc0,
433 0x003fffc0,
434 FALSE),
436 HOWTO (R_NIOS2_GOTOFF_HA,
440 FALSE,
442 complain_overflow_dont,
443 bfd_elf_generic_reloc,
444 "R_NIOS2_GOTOFF_HA",
445 FALSE,
446 0x003fffc0,
447 0x003fffc0,
448 FALSE),
450 HOWTO (R_NIOS2_PCREL_LO,
454 TRUE,
456 complain_overflow_dont,
457 nios2_elf32_pcrel_lo16_relocate,
458 "R_NIOS2_PCREL_LO",
459 FALSE,
460 0x003fffc0,
461 0x003fffc0,
462 TRUE),
464 HOWTO (R_NIOS2_PCREL_HA,
468 FALSE, /* This is a PC-relative relocation, but we need to subtract
469 PC ourselves before the HIADJ. */
471 complain_overflow_dont,
472 nios2_elf32_pcrel_hiadj16_relocate,
473 "R_NIOS2_PCREL_HA",
474 FALSE,
475 0x003fffc0,
476 0x003fffc0,
477 TRUE),
479 HOWTO (R_NIOS2_TLS_GD16,
483 FALSE,
485 complain_overflow_bitfield,
486 bfd_elf_generic_reloc,
487 "R_NIOS2_TLS_GD16",
488 FALSE,
489 0x003fffc0,
490 0x003fffc0,
491 FALSE),
493 HOWTO (R_NIOS2_TLS_LDM16,
497 FALSE,
499 complain_overflow_bitfield,
500 bfd_elf_generic_reloc,
501 "R_NIOS2_TLS_LDM16",
502 FALSE,
503 0x003fffc0,
504 0x003fffc0,
505 FALSE),
507 HOWTO (R_NIOS2_TLS_LDO16,
511 FALSE,
513 complain_overflow_bitfield,
514 bfd_elf_generic_reloc,
515 "R_NIOS2_TLS_LDO16",
516 FALSE,
517 0x003fffc0,
518 0x003fffc0,
519 FALSE),
521 HOWTO (R_NIOS2_TLS_IE16,
525 FALSE,
527 complain_overflow_bitfield,
528 bfd_elf_generic_reloc,
529 "R_NIOS2_TLS_IE16",
530 FALSE,
531 0x003fffc0,
532 0x003fffc0,
533 FALSE),
535 HOWTO (R_NIOS2_TLS_LE16,
539 FALSE,
541 complain_overflow_bitfield,
542 bfd_elf_generic_reloc,
543 "R_NIOS2_TLS_LE16",
544 FALSE,
545 0x003fffc0,
546 0x003fffc0,
547 FALSE),
549 HOWTO (R_NIOS2_TLS_DTPMOD,
553 FALSE,
555 complain_overflow_dont,
556 bfd_elf_generic_reloc,
557 "R_NIOS2_TLS_DTPMOD",
558 FALSE,
559 0xffffffff,
560 0xffffffff,
561 FALSE),
563 HOWTO (R_NIOS2_TLS_DTPREL,
567 FALSE,
569 complain_overflow_dont,
570 bfd_elf_generic_reloc,
571 "R_NIOS2_TLS_DTPREL",
572 FALSE,
573 0xffffffff,
574 0xffffffff,
575 FALSE),
577 HOWTO (R_NIOS2_TLS_TPREL,
581 FALSE,
583 complain_overflow_dont,
584 bfd_elf_generic_reloc,
585 "R_NIOS2_TLS_TPREL",
586 FALSE,
587 0xffffffff,
588 0xffffffff,
589 FALSE),
591 HOWTO (R_NIOS2_COPY,
595 FALSE,
597 complain_overflow_dont,
598 bfd_elf_generic_reloc,
599 "R_NIOS2_COPY",
600 FALSE,
603 FALSE),
605 HOWTO (R_NIOS2_GLOB_DAT,
609 FALSE,
611 complain_overflow_dont,
612 bfd_elf_generic_reloc,
613 "R_NIOS2_GLOB_DAT",
614 FALSE,
615 0xffffffff,
616 0xffffffff,
617 FALSE),
619 HOWTO (R_NIOS2_JUMP_SLOT,
623 FALSE,
625 complain_overflow_dont,
626 bfd_elf_generic_reloc,
627 "R_NIOS2_JUMP_SLOT",
628 FALSE,
629 0xffffffff,
630 0xffffffff,
631 FALSE),
633 HOWTO (R_NIOS2_RELATIVE,
637 FALSE,
639 complain_overflow_dont,
640 bfd_elf_generic_reloc,
641 "R_NIOS2_RELATIVE",
642 FALSE,
643 0xffffffff,
644 0xffffffff,
645 FALSE),
647 HOWTO (R_NIOS2_GOTOFF,
651 FALSE,
653 complain_overflow_dont,
654 bfd_elf_generic_reloc,
655 "R_NIOS2_GOTOFF",
656 FALSE,
657 0xffffffff,
658 0xffffffff,
659 FALSE),
661 HOWTO (R_NIOS2_CALL26_NOAT, /* type */
662 2, /* rightshift */
663 2, /* size (0 = byte, 1 = short, 2 = long) */
664 26, /* bitsize */
665 FALSE, /* pc_relative */
666 6, /* bitpos */
667 complain_overflow_dont, /* complain on overflow */
668 nios2_elf32_call26_relocate, /* special function */
669 "R_NIOS2_CALL26_NOAT", /* name */
670 FALSE, /* partial_inplace */
671 0xffffffc0, /* src_mask */
672 0xffffffc0, /* dst_mask */
673 FALSE), /* pcrel_offset */
675 HOWTO (R_NIOS2_GOT_LO,
679 FALSE,
681 complain_overflow_dont,
682 bfd_elf_generic_reloc,
683 "R_NIOS2_GOT_LO",
684 FALSE,
685 0x003fffc0,
686 0x003fffc0,
687 FALSE),
689 HOWTO (R_NIOS2_GOT_HA,
693 FALSE,
695 complain_overflow_dont,
696 bfd_elf_generic_reloc,
697 "R_NIOS2_GOT_HA",
698 FALSE,
699 0x003fffc0,
700 0x003fffc0,
701 FALSE),
703 HOWTO (R_NIOS2_CALL_LO,
707 FALSE,
709 complain_overflow_dont,
710 bfd_elf_generic_reloc,
711 "R_NIOS2_CALL_LO",
712 FALSE,
713 0x003fffc0,
714 0x003fffc0,
715 FALSE),
717 HOWTO (R_NIOS2_CALL_HA,
721 FALSE,
723 complain_overflow_dont,
724 bfd_elf_generic_reloc,
725 "R_NIOS2_CALL_HA",
726 FALSE,
727 0x003fffc0,
728 0x003fffc0,
729 FALSE),
731 /* Add other relocations here. */
734 static reloc_howto_type elf_nios2_r2_howto_table_rel[] = {
735 /* No relocation. */
736 HOWTO (R_NIOS2_NONE, /* type */
737 0, /* rightshift */
738 0, /* size (0 = byte, 1 = short, 2 = long) */
739 0, /* bitsize */
740 FALSE, /* pc_relative */
741 0, /* bitpos */
742 complain_overflow_dont, /* complain_on_overflow */
743 bfd_elf_generic_reloc, /* special_function */
744 "R_NIOS2_NONE", /* name */
745 FALSE, /* partial_inplace */
746 0, /* src_mask */
747 0, /* dst_mask */
748 FALSE), /* pcrel_offset */
750 /* 16-bit signed immediate relocation. */
751 HOWTO (R_NIOS2_S16, /* type */
752 0, /* rightshift */
753 2, /* size (0 = byte, 1 = short, 2 = long) */
754 16, /* bitsize */
755 FALSE, /* pc_relative */
756 16, /* bitpos */
757 complain_overflow_signed, /* complain on overflow */
758 bfd_elf_generic_reloc, /* special function */
759 "R_NIOS2_S16", /* name */
760 FALSE, /* partial_inplace */
761 0xffff0000, /* src_mask */
762 0xffff0000, /* dest_mask */
763 FALSE), /* pcrel_offset */
765 /* 16-bit unsigned immediate relocation. */
766 HOWTO (R_NIOS2_U16, /* type */
767 0, /* rightshift */
768 2, /* size (0 = byte, 1 = short, 2 = long) */
769 16, /* bitsize */
770 FALSE, /* pc_relative */
771 16, /* bitpos */
772 complain_overflow_unsigned, /* complain on overflow */
773 bfd_elf_generic_reloc, /* special function */
774 "R_NIOS2_U16", /* name */
775 FALSE, /* partial_inplace */
776 0xffff0000, /* src_mask */
777 0xffff0000, /* dest_mask */
778 FALSE), /* pcrel_offset */
780 HOWTO (R_NIOS2_PCREL16, /* type */
781 0, /* rightshift */
782 2, /* size (0 = byte, 1 = short, 2 = long) */
783 16, /* bitsize */
784 TRUE, /* pc_relative */
785 16, /* bitpos */
786 complain_overflow_signed, /* complain on overflow */
787 nios2_elf32_pcrel16_relocate, /* special function */
788 "R_NIOS2_PCREL16", /* name */
789 FALSE, /* partial_inplace */
790 0xffff0000, /* src_mask */
791 0xffff0000, /* dest_mask */
792 TRUE), /* pcrel_offset */
794 HOWTO (R_NIOS2_CALL26, /* type */
795 2, /* rightshift */
796 2, /* size (0 = byte, 1 = short, 2 = long) */
797 26, /* bitsize */
798 FALSE, /* pc_relative */
799 6, /* bitpos */
800 complain_overflow_dont, /* complain on overflow */
801 nios2_elf32_call26_relocate, /* special function */
802 "R_NIOS2_CALL26", /* name */
803 FALSE, /* partial_inplace */
804 0xffffffc0, /* src_mask */
805 0xffffffc0, /* dst_mask */
806 FALSE), /* pcrel_offset */
808 HOWTO (R_NIOS2_IMM5,
812 FALSE,
814 complain_overflow_bitfield,
815 bfd_elf_generic_reloc,
816 "R_NIOS2_IMM5",
817 FALSE,
818 0x03e00000,
819 0x03e00000,
820 FALSE),
822 HOWTO (R_NIOS2_CACHE_OPX,
826 FALSE,
828 complain_overflow_bitfield,
829 bfd_elf_generic_reloc,
830 "R_NIOS2_CACHE_OPX",
831 FALSE,
832 0x0000f800,
833 0x0000f800,
834 FALSE),
836 HOWTO (R_NIOS2_IMM6,
840 FALSE,
842 complain_overflow_bitfield,
843 bfd_elf_generic_reloc,
844 "R_NIOS2_IMM6",
845 FALSE,
846 0xfc000000,
847 0xfc000000,
848 FALSE),
850 HOWTO (R_NIOS2_IMM8,
854 FALSE,
856 complain_overflow_bitfield,
857 bfd_elf_generic_reloc,
858 "R_NIOS2_IMM8",
859 FALSE,
860 0xff000000,
861 0xff000000,
862 FALSE),
864 HOWTO (R_NIOS2_HI16,
868 FALSE,
870 complain_overflow_dont,
871 nios2_elf32_hi16_relocate,
872 "R_NIOS2_HI16",
873 FALSE,
874 0xffff0000,
875 0xffff0000,
876 FALSE),
878 HOWTO (R_NIOS2_LO16,
882 FALSE,
884 complain_overflow_dont,
885 nios2_elf32_lo16_relocate,
886 "R_NIOS2_LO16",
887 FALSE,
888 0xffff0000,
889 0xffff0000,
890 FALSE),
892 HOWTO (R_NIOS2_HIADJ16,
896 FALSE,
898 complain_overflow_dont,
899 nios2_elf32_hiadj16_relocate,
900 "R_NIOS2_HIADJ16",
901 FALSE,
902 0xffff0000,
903 0xffff0000,
904 FALSE),
906 HOWTO (R_NIOS2_BFD_RELOC_32,
908 2, /* long */
910 FALSE,
912 complain_overflow_dont,
913 bfd_elf_generic_reloc,
914 "R_NIOS2_BFD_RELOC32",
915 FALSE,
916 0xffffffff,
917 0xffffffff,
918 FALSE),
920 HOWTO (R_NIOS2_BFD_RELOC_16,
922 1, /* short */
924 FALSE,
926 complain_overflow_bitfield,
927 bfd_elf_generic_reloc,
928 "R_NIOS2_BFD_RELOC16",
929 FALSE,
930 0x0000ffff,
931 0x0000ffff,
932 FALSE),
934 HOWTO (R_NIOS2_BFD_RELOC_8,
936 0, /* byte */
938 FALSE,
940 complain_overflow_bitfield,
941 bfd_elf_generic_reloc,
942 "R_NIOS2_BFD_RELOC8",
943 FALSE,
944 0x000000ff,
945 0x000000ff,
946 FALSE),
948 HOWTO (R_NIOS2_GPREL,
952 FALSE,
954 complain_overflow_dont,
955 nios2_elf32_gprel_relocate,
956 "R_NIOS2_GPREL",
957 FALSE,
958 0xffff0000,
959 0xffff0000,
960 FALSE),
962 HOWTO (R_NIOS2_GNU_VTINHERIT,
964 2, /* short */
966 FALSE,
968 complain_overflow_dont,
969 NULL,
970 "R_NIOS2_GNU_VTINHERIT",
971 FALSE,
974 FALSE),
976 HOWTO (R_NIOS2_GNU_VTENTRY,
978 2, /* byte */
980 FALSE,
982 complain_overflow_dont,
983 _bfd_elf_rel_vtable_reloc_fn,
984 "R_NIOS2_GNU_VTENTRY",
985 FALSE,
988 FALSE),
990 HOWTO (R_NIOS2_UJMP,
994 FALSE,
996 complain_overflow_dont,
997 nios2_elf32_ujmp_relocate,
998 "R_NIOS2_UJMP",
999 FALSE,
1000 0xffff0000,
1001 0xffff0000,
1002 FALSE),
1004 HOWTO (R_NIOS2_CJMP,
1008 FALSE,
1010 complain_overflow_dont,
1011 nios2_elf32_cjmp_relocate,
1012 "R_NIOS2_CJMP",
1013 FALSE,
1014 0xffff0000,
1015 0xffff0000,
1016 FALSE),
1018 HOWTO (R_NIOS2_CALLR,
1022 FALSE,
1024 complain_overflow_dont,
1025 nios2_elf32_callr_relocate,
1026 "R_NIOS2_CALLR",
1027 FALSE,
1028 0xffff0000,
1029 0xffff0000,
1030 FALSE),
1032 HOWTO (R_NIOS2_ALIGN,
1036 FALSE,
1038 complain_overflow_dont,
1039 nios2_elf32_ignore_reloc,
1040 "R_NIOS2_ALIGN",
1041 FALSE,
1044 TRUE),
1046 HOWTO (R_NIOS2_GOT16,
1050 FALSE,
1052 complain_overflow_bitfield,
1053 bfd_elf_generic_reloc,
1054 "R_NIOS2_GOT16",
1055 FALSE,
1056 0xffff0000,
1057 0xffff0000,
1058 FALSE),
1060 HOWTO (R_NIOS2_CALL16,
1064 FALSE,
1066 complain_overflow_bitfield,
1067 bfd_elf_generic_reloc,
1068 "R_NIOS2_CALL16",
1069 FALSE,
1070 0xffff0000,
1071 0xffff0000,
1072 FALSE),
1074 HOWTO (R_NIOS2_GOTOFF_LO,
1078 FALSE,
1080 complain_overflow_dont,
1081 bfd_elf_generic_reloc,
1082 "R_NIOS2_GOTOFF_LO",
1083 FALSE,
1084 0xffff0000,
1085 0xffff0000,
1086 FALSE),
1088 HOWTO (R_NIOS2_GOTOFF_HA,
1092 FALSE,
1094 complain_overflow_dont,
1095 bfd_elf_generic_reloc,
1096 "R_NIOS2_GOTOFF_HA",
1097 FALSE,
1098 0xffff0000,
1099 0xffff0000,
1100 FALSE),
1102 HOWTO (R_NIOS2_PCREL_LO,
1106 TRUE,
1108 complain_overflow_dont,
1109 nios2_elf32_pcrel_lo16_relocate,
1110 "R_NIOS2_PCREL_LO",
1111 FALSE,
1112 0xffff0000,
1113 0xffff0000,
1114 TRUE),
1116 HOWTO (R_NIOS2_PCREL_HA,
1120 FALSE, /* This is a PC-relative relocation, but we need to subtract
1121 PC ourselves before the HIADJ. */
1123 complain_overflow_dont,
1124 nios2_elf32_pcrel_hiadj16_relocate,
1125 "R_NIOS2_PCREL_HA",
1126 FALSE,
1127 0xffff0000,
1128 0xffff0000,
1129 TRUE),
1131 HOWTO (R_NIOS2_TLS_GD16,
1135 FALSE,
1137 complain_overflow_bitfield,
1138 bfd_elf_generic_reloc,
1139 "R_NIOS2_TLS_GD16",
1140 FALSE,
1141 0xffff0000,
1142 0xffff0000,
1143 FALSE),
1145 HOWTO (R_NIOS2_TLS_LDM16,
1149 FALSE,
1151 complain_overflow_bitfield,
1152 bfd_elf_generic_reloc,
1153 "R_NIOS2_TLS_LDM16",
1154 FALSE,
1155 0xffff0000,
1156 0xffff0000,
1157 FALSE),
1159 HOWTO (R_NIOS2_TLS_LDO16,
1163 FALSE,
1165 complain_overflow_bitfield,
1166 bfd_elf_generic_reloc,
1167 "R_NIOS2_TLS_LDO16",
1168 FALSE,
1169 0xffff0000,
1170 0xffff0000,
1171 FALSE),
1173 HOWTO (R_NIOS2_TLS_IE16,
1177 FALSE,
1179 complain_overflow_bitfield,
1180 bfd_elf_generic_reloc,
1181 "R_NIOS2_TLS_IE16",
1182 FALSE,
1183 0xffff0000,
1184 0xffff0000,
1185 FALSE),
1187 HOWTO (R_NIOS2_TLS_LE16,
1191 FALSE,
1193 complain_overflow_bitfield,
1194 bfd_elf_generic_reloc,
1195 "R_NIOS2_TLS_LE16",
1196 FALSE,
1197 0xffff0000,
1198 0xffff0000,
1199 FALSE),
1201 HOWTO (R_NIOS2_TLS_DTPMOD,
1205 FALSE,
1207 complain_overflow_dont,
1208 bfd_elf_generic_reloc,
1209 "R_NIOS2_TLS_DTPMOD",
1210 FALSE,
1211 0xffffffff,
1212 0xffffffff,
1213 FALSE),
1215 HOWTO (R_NIOS2_TLS_DTPREL,
1219 FALSE,
1221 complain_overflow_dont,
1222 bfd_elf_generic_reloc,
1223 "R_NIOS2_TLS_DTPREL",
1224 FALSE,
1225 0xffffffff,
1226 0xffffffff,
1227 FALSE),
1229 HOWTO (R_NIOS2_TLS_TPREL,
1233 FALSE,
1235 complain_overflow_dont,
1236 bfd_elf_generic_reloc,
1237 "R_NIOS2_TLS_TPREL",
1238 FALSE,
1239 0xffffffff,
1240 0xffffffff,
1241 FALSE),
1243 HOWTO (R_NIOS2_COPY,
1247 FALSE,
1249 complain_overflow_dont,
1250 bfd_elf_generic_reloc,
1251 "R_NIOS2_COPY",
1252 FALSE,
1255 FALSE),
1257 HOWTO (R_NIOS2_GLOB_DAT,
1261 FALSE,
1263 complain_overflow_dont,
1264 bfd_elf_generic_reloc,
1265 "R_NIOS2_GLOB_DAT",
1266 FALSE,
1267 0xffffffff,
1268 0xffffffff,
1269 FALSE),
1271 HOWTO (R_NIOS2_JUMP_SLOT,
1275 FALSE,
1277 complain_overflow_dont,
1278 bfd_elf_generic_reloc,
1279 "R_NIOS2_JUMP_SLOT",
1280 FALSE,
1281 0xffffffff,
1282 0xffffffff,
1283 FALSE),
1285 HOWTO (R_NIOS2_RELATIVE,
1289 FALSE,
1291 complain_overflow_dont,
1292 bfd_elf_generic_reloc,
1293 "R_NIOS2_RELATIVE",
1294 FALSE,
1295 0xffffffff,
1296 0xffffffff,
1297 FALSE),
1299 HOWTO (R_NIOS2_GOTOFF,
1303 FALSE,
1305 complain_overflow_dont,
1306 bfd_elf_generic_reloc,
1307 "R_NIOS2_GOTOFF",
1308 FALSE,
1309 0xffffffff,
1310 0xffffffff,
1311 FALSE),
1313 HOWTO (R_NIOS2_CALL26_NOAT, /* type */
1314 2, /* rightshift */
1315 2, /* size (0 = byte, 1 = short, 2 = long) */
1316 26, /* bitsize */
1317 FALSE, /* pc_relative */
1318 6, /* bitpos */
1319 complain_overflow_dont, /* complain on overflow */
1320 nios2_elf32_call26_relocate, /* special function */
1321 "R_NIOS2_CALL26_NOAT", /* name */
1322 FALSE, /* partial_inplace */
1323 0xffffffc0, /* src_mask */
1324 0xffffffc0, /* dst_mask */
1325 FALSE), /* pcrel_offset */
1327 HOWTO (R_NIOS2_GOT_LO,
1331 FALSE,
1333 complain_overflow_dont,
1334 bfd_elf_generic_reloc,
1335 "R_NIOS2_GOT_LO",
1336 FALSE,
1337 0xffff0000,
1338 0xffff0000,
1339 FALSE),
1341 HOWTO (R_NIOS2_GOT_HA,
1345 FALSE,
1347 complain_overflow_dont,
1348 bfd_elf_generic_reloc,
1349 "R_NIOS2_GOT_HA",
1350 FALSE,
1351 0xffff0000,
1352 0xffff0000,
1353 FALSE),
1355 HOWTO (R_NIOS2_CALL_LO,
1359 FALSE,
1361 complain_overflow_dont,
1362 bfd_elf_generic_reloc,
1363 "R_NIOS2_CALL_LO",
1364 FALSE,
1365 0xffff0000,
1366 0xffff0000,
1367 FALSE),
1369 HOWTO (R_NIOS2_CALL_HA,
1373 FALSE,
1375 complain_overflow_dont,
1376 bfd_elf_generic_reloc,
1377 "R_NIOS2_CALL_HA",
1378 FALSE,
1379 0xffff0000,
1380 0xffff0000,
1381 FALSE),
1383 HOWTO (R_NIOS2_R2_S12,
1387 FALSE,
1389 complain_overflow_signed,
1390 bfd_elf_generic_reloc,
1391 "R_NIOS2_R2_S12",
1392 FALSE,
1393 0x0fff0000,
1394 0x0fff0000,
1395 FALSE),
1397 HOWTO (R_NIOS2_R2_I10_1_PCREL,
1401 TRUE,
1403 complain_overflow_signed,
1404 bfd_elf_generic_reloc, /* FIXME? */
1405 "R_NIOS2_R2_I10_1_PCREL",
1406 FALSE,
1407 0xffc0,
1408 0xffc0,
1409 TRUE),
1411 HOWTO (R_NIOS2_R2_T1I7_1_PCREL,
1415 TRUE,
1417 complain_overflow_signed,
1418 bfd_elf_generic_reloc, /* FIXME? */
1419 "R_NIOS2_R2_T1I7_1_PCREL",
1420 FALSE,
1421 0xfe00,
1422 0xfe00,
1423 TRUE),
1425 HOWTO (R_NIOS2_R2_T1I7_2,
1429 FALSE,
1431 complain_overflow_unsigned,
1432 bfd_elf_generic_reloc,
1433 "R_NIOS2_R2_T1I7_2",
1434 FALSE,
1435 0xfe00,
1436 0xfe00,
1437 FALSE),
1439 HOWTO (R_NIOS2_R2_T2I4,
1443 FALSE,
1445 complain_overflow_unsigned,
1446 bfd_elf_generic_reloc,
1447 "R_NIOS2_R2_T2I4",
1448 FALSE,
1449 0xf000,
1450 0xf000,
1451 FALSE),
1453 HOWTO (R_NIOS2_R2_T2I4_1,
1457 FALSE,
1459 complain_overflow_unsigned,
1460 bfd_elf_generic_reloc,
1461 "R_NIOS2_R2_T2I4_1",
1462 FALSE,
1463 0xf000,
1464 0xf000,
1465 FALSE),
1467 HOWTO (R_NIOS2_R2_T2I4_2,
1471 FALSE,
1473 complain_overflow_unsigned,
1474 bfd_elf_generic_reloc,
1475 "R_NIOS2_R2_T2I4_2",
1476 FALSE,
1477 0xf000,
1478 0xf000,
1479 FALSE),
1481 HOWTO (R_NIOS2_R2_X1I7_2,
1485 FALSE,
1487 complain_overflow_unsigned,
1488 bfd_elf_generic_reloc,
1489 "R_NIOS2_R2_X1I7_2",
1490 FALSE,
1491 0x1fc0,
1492 0x1fc0,
1493 FALSE),
1495 HOWTO (R_NIOS2_R2_X2L5,
1499 FALSE,
1501 complain_overflow_unsigned,
1502 bfd_elf_generic_reloc,
1503 "R_NIOS2_R2_X2L5",
1504 FALSE,
1505 0x07c0,
1506 0x07c0,
1507 FALSE),
1509 HOWTO (R_NIOS2_R2_F1I5_2,
1513 FALSE,
1515 complain_overflow_unsigned,
1516 bfd_elf_generic_reloc,
1517 "R_NIOS2_R2_F1L5_2",
1518 FALSE,
1519 0x07c0,
1520 0x07c0,
1521 FALSE),
1523 HOWTO (R_NIOS2_R2_L5I4X1,
1527 FALSE,
1529 complain_overflow_unsigned,
1530 bfd_elf_generic_reloc,
1531 "R_NIOS2_R2_L5I4X1",
1532 FALSE,
1533 0x03c0,
1534 0x03c0,
1535 FALSE),
1537 HOWTO (R_NIOS2_R2_T1X1I6,
1541 FALSE,
1543 complain_overflow_unsigned,
1544 bfd_elf_generic_reloc,
1545 "R_NIOS2_R2_T1X1I6",
1546 FALSE,
1547 0x7e00,
1548 0x7e00,
1549 FALSE),
1551 HOWTO (R_NIOS2_R2_T1X1I6_2,
1555 FALSE,
1557 complain_overflow_unsigned,
1558 bfd_elf_generic_reloc,
1559 "R_NIOS2_R2_T1I1X6_2",
1560 FALSE,
1561 0x7e00,
1562 0x7e00,
1563 FALSE),
1565 /* Add other relocations here. */
1568 static unsigned char elf_code_to_howto_index[R_NIOS2_ILLEGAL + 1];
1571 /* Return true if producing output for a R2 BFD. */
1572 #define BFD_IS_R2(abfd) (bfd_get_mach (abfd) == bfd_mach_nios2r2)
1574 /* Return the howto for relocation RTYPE. */
1575 static reloc_howto_type *
1576 lookup_howto (unsigned int rtype, bfd *abfd)
1578 static int initialized = 0;
1579 int i;
1580 /* R2 relocations are a superset of R1, so use that for the lookup
1581 table. */
1582 int r1_howto_tbl_size = (int) ARRAY_SIZE (elf_nios2_r1_howto_table_rel);
1583 int r2_howto_tbl_size = (int) ARRAY_SIZE (elf_nios2_r2_howto_table_rel);
1585 if (!initialized)
1587 initialized = 1;
1588 memset (elf_code_to_howto_index, 0xff,
1589 sizeof (elf_code_to_howto_index));
1590 for (i = 0; i < r2_howto_tbl_size; i++)
1592 elf_code_to_howto_index[elf_nios2_r2_howto_table_rel[i].type] = i;
1593 if (i < r1_howto_tbl_size)
1594 BFD_ASSERT (elf_nios2_r2_howto_table_rel[i].type
1595 == elf_nios2_r1_howto_table_rel[i].type);
1599 if (rtype > R_NIOS2_ILLEGAL)
1600 return NULL;
1601 i = elf_code_to_howto_index[rtype];
1602 if (BFD_IS_R2 (abfd))
1604 if (i >= r2_howto_tbl_size)
1605 return NULL;
1606 return elf_nios2_r2_howto_table_rel + i;
1608 else
1610 if (i >= r1_howto_tbl_size)
1611 return NULL;
1612 return elf_nios2_r1_howto_table_rel + i;
1616 /* Map for converting BFD reloc types to Nios II reloc types. */
1617 struct elf_reloc_map
1619 bfd_reloc_code_real_type bfd_val;
1620 enum elf_nios2_reloc_type elf_val;
1623 static const struct elf_reloc_map nios2_reloc_map[] =
1625 {BFD_RELOC_NONE, R_NIOS2_NONE},
1626 {BFD_RELOC_NIOS2_S16, R_NIOS2_S16},
1627 {BFD_RELOC_NIOS2_U16, R_NIOS2_U16},
1628 {BFD_RELOC_16_PCREL, R_NIOS2_PCREL16},
1629 {BFD_RELOC_NIOS2_CALL26, R_NIOS2_CALL26},
1630 {BFD_RELOC_NIOS2_IMM5, R_NIOS2_IMM5},
1631 {BFD_RELOC_NIOS2_CACHE_OPX, R_NIOS2_CACHE_OPX},
1632 {BFD_RELOC_NIOS2_IMM6, R_NIOS2_IMM6},
1633 {BFD_RELOC_NIOS2_IMM8, R_NIOS2_IMM8},
1634 {BFD_RELOC_NIOS2_HI16, R_NIOS2_HI16},
1635 {BFD_RELOC_NIOS2_LO16, R_NIOS2_LO16},
1636 {BFD_RELOC_NIOS2_HIADJ16, R_NIOS2_HIADJ16},
1637 {BFD_RELOC_32, R_NIOS2_BFD_RELOC_32},
1638 {BFD_RELOC_16, R_NIOS2_BFD_RELOC_16},
1639 {BFD_RELOC_8, R_NIOS2_BFD_RELOC_8},
1640 {BFD_RELOC_NIOS2_GPREL, R_NIOS2_GPREL},
1641 {BFD_RELOC_VTABLE_INHERIT, R_NIOS2_GNU_VTINHERIT},
1642 {BFD_RELOC_VTABLE_ENTRY, R_NIOS2_GNU_VTENTRY},
1643 {BFD_RELOC_NIOS2_UJMP, R_NIOS2_UJMP},
1644 {BFD_RELOC_NIOS2_CJMP, R_NIOS2_CJMP},
1645 {BFD_RELOC_NIOS2_CALLR, R_NIOS2_CALLR},
1646 {BFD_RELOC_NIOS2_ALIGN, R_NIOS2_ALIGN},
1647 {BFD_RELOC_NIOS2_GOT16, R_NIOS2_GOT16},
1648 {BFD_RELOC_NIOS2_CALL16, R_NIOS2_CALL16},
1649 {BFD_RELOC_NIOS2_GOTOFF_LO, R_NIOS2_GOTOFF_LO},
1650 {BFD_RELOC_NIOS2_GOTOFF_HA, R_NIOS2_GOTOFF_HA},
1651 {BFD_RELOC_NIOS2_PCREL_LO, R_NIOS2_PCREL_LO},
1652 {BFD_RELOC_NIOS2_PCREL_HA, R_NIOS2_PCREL_HA},
1653 {BFD_RELOC_NIOS2_TLS_GD16, R_NIOS2_TLS_GD16},
1654 {BFD_RELOC_NIOS2_TLS_LDM16, R_NIOS2_TLS_LDM16},
1655 {BFD_RELOC_NIOS2_TLS_LDO16, R_NIOS2_TLS_LDO16},
1656 {BFD_RELOC_NIOS2_TLS_IE16, R_NIOS2_TLS_IE16},
1657 {BFD_RELOC_NIOS2_TLS_LE16, R_NIOS2_TLS_LE16},
1658 {BFD_RELOC_NIOS2_TLS_DTPMOD, R_NIOS2_TLS_DTPMOD},
1659 {BFD_RELOC_NIOS2_TLS_DTPREL, R_NIOS2_TLS_DTPREL},
1660 {BFD_RELOC_NIOS2_TLS_TPREL, R_NIOS2_TLS_TPREL},
1661 {BFD_RELOC_NIOS2_COPY, R_NIOS2_COPY},
1662 {BFD_RELOC_NIOS2_GLOB_DAT, R_NIOS2_GLOB_DAT},
1663 {BFD_RELOC_NIOS2_JUMP_SLOT, R_NIOS2_JUMP_SLOT},
1664 {BFD_RELOC_NIOS2_RELATIVE, R_NIOS2_RELATIVE},
1665 {BFD_RELOC_NIOS2_GOTOFF, R_NIOS2_GOTOFF},
1666 {BFD_RELOC_NIOS2_CALL26_NOAT, R_NIOS2_CALL26_NOAT},
1667 {BFD_RELOC_NIOS2_GOT_LO, R_NIOS2_GOT_LO},
1668 {BFD_RELOC_NIOS2_GOT_HA, R_NIOS2_GOT_HA},
1669 {BFD_RELOC_NIOS2_CALL_LO, R_NIOS2_CALL_LO},
1670 {BFD_RELOC_NIOS2_CALL_HA, R_NIOS2_CALL_HA},
1671 {BFD_RELOC_NIOS2_R2_S12, R_NIOS2_R2_S12},
1672 {BFD_RELOC_NIOS2_R2_I10_1_PCREL, R_NIOS2_R2_I10_1_PCREL},
1673 {BFD_RELOC_NIOS2_R2_T1I7_1_PCREL, R_NIOS2_R2_T1I7_1_PCREL},
1674 {BFD_RELOC_NIOS2_R2_T1I7_2, R_NIOS2_R2_T1I7_2},
1675 {BFD_RELOC_NIOS2_R2_T2I4, R_NIOS2_R2_T2I4},
1676 {BFD_RELOC_NIOS2_R2_T2I4_1, R_NIOS2_R2_T2I4_1},
1677 {BFD_RELOC_NIOS2_R2_T2I4_2, R_NIOS2_R2_T2I4_2},
1678 {BFD_RELOC_NIOS2_R2_X1I7_2, R_NIOS2_R2_X1I7_2},
1679 {BFD_RELOC_NIOS2_R2_X2L5, R_NIOS2_R2_X2L5},
1680 {BFD_RELOC_NIOS2_R2_F1I5_2, R_NIOS2_R2_F1I5_2},
1681 {BFD_RELOC_NIOS2_R2_L5I4X1, R_NIOS2_R2_L5I4X1},
1682 {BFD_RELOC_NIOS2_R2_T1X1I6, R_NIOS2_R2_T1X1I6},
1683 {BFD_RELOC_NIOS2_R2_T1X1I6_2, R_NIOS2_R2_T1X1I6_2},
1686 enum elf32_nios2_stub_type
1688 nios2_stub_call26_before,
1689 nios2_stub_call26_after,
1690 nios2_stub_none
1693 struct elf32_nios2_stub_hash_entry
1695 /* Base hash table entry structure. */
1696 struct bfd_hash_entry bh_root;
1698 /* The stub section. */
1699 asection *stub_sec;
1701 /* Offset within stub_sec of the beginning of this stub. */
1702 bfd_vma stub_offset;
1704 /* Given the symbol's value and its section we can determine its final
1705 value when building the stubs (so the stub knows where to jump. */
1706 bfd_vma target_value;
1707 asection *target_section;
1709 enum elf32_nios2_stub_type stub_type;
1711 /* The symbol table entry, if any, that this was derived from. */
1712 struct elf32_nios2_link_hash_entry *hh;
1714 /* And the reloc addend that this was derived from. */
1715 bfd_vma addend;
1717 /* Where this stub is being called from, or, in the case of combined
1718 stub sections, the first input section in the group. */
1719 asection *id_sec;
1722 #define nios2_stub_hash_entry(ent) \
1723 ((struct elf32_nios2_stub_hash_entry *)(ent))
1725 #define nios2_stub_hash_lookup(table, string, create, copy) \
1726 ((struct elf32_nios2_stub_hash_entry *) \
1727 bfd_hash_lookup ((table), (string), (create), (copy)))
1730 /* Nios II ELF linker hash entry. */
1732 struct elf32_nios2_link_hash_entry
1734 struct elf_link_hash_entry root;
1736 /* A pointer to the most recently used stub hash entry against this
1737 symbol. */
1738 struct elf32_nios2_stub_hash_entry *hsh_cache;
1740 #define GOT_UNKNOWN 0
1741 #define GOT_NORMAL 1
1742 #define GOT_TLS_GD 2
1743 #define GOT_TLS_IE 4
1744 unsigned char tls_type;
1746 /* We need to detect and take special action for symbols which are only
1747 referenced with %call() and not with %got(). Such symbols do not need
1748 a dynamic GOT reloc in shared objects, only a dynamic PLT reloc. Lazy
1749 linking will not work if the dynamic GOT reloc exists.
1750 To check for this condition efficiently, we compare got_types_used against
1751 CALL_USED, meaning
1752 (got_types_used & (GOT_USED | CALL_USED)) == CALL_USED.
1754 #define GOT_USED 1
1755 #define CALL_USED 2
1756 unsigned char got_types_used;
1759 #define elf32_nios2_hash_entry(ent) \
1760 ((struct elf32_nios2_link_hash_entry *) (ent))
1762 /* Get the Nios II elf linker hash table from a link_info structure. */
1763 #define elf32_nios2_hash_table(info) \
1764 ((struct elf32_nios2_link_hash_table *) ((info)->hash))
1766 /* Nios II ELF linker hash table. */
1767 struct elf32_nios2_link_hash_table
1769 /* The main hash table. */
1770 struct elf_link_hash_table root;
1772 /* The stub hash table. */
1773 struct bfd_hash_table bstab;
1775 /* Linker stub bfd. */
1776 bfd *stub_bfd;
1778 /* Linker call-backs. */
1779 asection * (*add_stub_section) (const char *, asection *, bfd_boolean);
1780 void (*layout_sections_again) (void);
1782 /* Array to keep track of which stub sections have been created, and
1783 information on stub grouping. */
1784 struct map_stub
1786 /* These are the section to which stubs in the group will be
1787 attached. */
1788 asection *first_sec, *last_sec;
1789 /* The stub sections. There might be stubs inserted either before
1790 or after the real section.*/
1791 asection *first_stub_sec, *last_stub_sec;
1792 } *stub_group;
1794 /* Assorted information used by nios2_elf32_size_stubs. */
1795 unsigned int bfd_count;
1796 unsigned int top_index;
1797 asection **input_list;
1798 Elf_Internal_Sym **all_local_syms;
1800 /* Short-cuts to get to dynamic linker sections. */
1801 asection *sbss;
1803 /* GOT pointer symbol _gp_got. */
1804 struct elf_link_hash_entry *h_gp_got;
1806 union {
1807 bfd_signed_vma refcount;
1808 bfd_vma offset;
1809 } tls_ldm_got;
1811 bfd_vma res_n_size;
1814 struct nios2_elf32_obj_tdata
1816 struct elf_obj_tdata root;
1818 /* tls_type for each local got entry. */
1819 char *local_got_tls_type;
1821 /* TRUE if TLS GD relocs have been seen for this object. */
1822 bfd_boolean has_tlsgd;
1825 #define elf32_nios2_tdata(abfd) \
1826 ((struct nios2_elf32_obj_tdata *) (abfd)->tdata.any)
1828 #define elf32_nios2_local_got_tls_type(abfd) \
1829 (elf32_nios2_tdata (abfd)->local_got_tls_type)
1831 /* The name of the dynamic interpreter. This is put in the .interp
1832 section. */
1833 #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
1835 /* PLT implementation for position-dependent code. */
1836 static const bfd_vma nios2_plt_entry[] = { /* .PLTn: */
1837 0x03c00034, /* movhi r15, %hiadj(plt_got_slot_address) */
1838 0x7bc00017, /* ldw r15, %lo(plt_got_slot_address)(r15) */
1839 0x7800683a /* jmp r15 */
1842 static const bfd_vma nios2_plt0_entry[] = { /* .PLTresolve */
1843 0x03800034, /* movhi r14, %hiadj(res_0) */
1844 0x73800004, /* addi r14, r14, %lo(res_0) */
1845 0x7b9fc83a, /* sub r15, r15, r14 */
1846 0x03400034, /* movhi r13, %hiadj(_GLOBAL_OFFSET_TABLE_) */
1847 0x6b800017, /* ldw r14, %lo(_GLOBAL_OFFSET_TABLE_+4)(r13) */
1848 0x6b400017, /* ldw r13, %lo(_GLOBAL_OFFSET_TABLE_+8)(r13) */
1849 0x6800683a /* jmp r13 */
1852 /* PLT implementation for position-independent code. */
1853 static const bfd_vma nios2_so_plt_entry[] = { /* .PLTn */
1854 0x03c00034, /* movhi r15, %hiadj(index * 4) */
1855 0x7bc00004, /* addi r15, r15, %lo(index * 4) */
1856 0x00000006 /* br .PLTresolve */
1859 static const bfd_vma nios2_so_plt0_entry[] = { /* .PLTresolve */
1860 0x001ce03a, /* nextpc r14 */
1861 0x03400034, /* movhi r13, %hiadj(_GLOBAL_OFFSET_TABLE_) */
1862 0x6b9b883a, /* add r13, r13, r14 */
1863 0x6b800017, /* ldw r14, %lo(_GLOBAL_OFFSET_TABLE_+4)(r13) */
1864 0x6b400017, /* ldw r13, %lo(_GLOBAL_OFFSET_TABLE_+8)(r13) */
1865 0x6800683a /* jmp r13 */
1868 /* CALL26 stub. */
1869 static const bfd_vma nios2_call26_stub_entry[] = {
1870 0x00400034, /* orhi at, r0, %hiadj(dest) */
1871 0x08400004, /* addi at, at, %lo(dest) */
1872 0x0800683a /* jmp at */
1875 /* Install 16-bit immediate value VALUE at offset OFFSET into section SEC. */
1876 static void
1877 nios2_elf32_install_imm16 (asection *sec, bfd_vma offset, bfd_vma value)
1879 bfd_vma word = bfd_get_32 (sec->owner, sec->contents + offset);
1881 BFD_ASSERT (value <= 0xffff || ((bfd_signed_vma) value) >= -0xffff);
1883 bfd_put_32 (sec->owner, word | ((value & 0xffff) << 6),
1884 sec->contents + offset);
1887 /* Install COUNT 32-bit values DATA starting at offset OFFSET into
1888 section SEC. */
1889 static void
1890 nios2_elf32_install_data (asection *sec, const bfd_vma *data, bfd_vma offset,
1891 int count)
1893 while (count--)
1895 bfd_put_32 (sec->owner, *data, sec->contents + offset);
1896 offset += 4;
1897 ++data;
1901 /* The usual way of loading a 32-bit constant into a Nios II register is to
1902 load the high 16 bits in one instruction and then add the low 16 bits with
1903 a signed add. This means that the high halfword needs to be adjusted to
1904 compensate for the sign bit of the low halfword. This function returns the
1905 adjusted high halfword for a given 32-bit constant. */
1906 static
1907 bfd_vma hiadj (bfd_vma symbol_value)
1909 return ((symbol_value + 0x8000) >> 16) & 0xffff;
1912 /* Implement elf_backend_grok_prstatus:
1913 Support for core dump NOTE sections. */
1914 static bfd_boolean
1915 nios2_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1917 int offset;
1918 size_t size;
1920 switch (note->descsz)
1922 default:
1923 return FALSE;
1925 case 212: /* Linux/Nios II */
1926 /* pr_cursig */
1927 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
1929 /* pr_pid */
1930 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 24);
1932 /* pr_reg */
1933 offset = 72;
1934 size = 136;
1936 break;
1939 /* Make a ".reg/999" section. */
1940 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
1941 size, note->descpos + offset);
1944 /* Implement elf_backend_grok_psinfo. */
1945 static bfd_boolean
1946 nios2_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1948 switch (note->descsz)
1950 default:
1951 return FALSE;
1953 case 124: /* Linux/Nios II elf_prpsinfo */
1954 elf_tdata (abfd)->core->program
1955 = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
1956 elf_tdata (abfd)->core->command
1957 = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
1960 /* Note that for some reason, a spurious space is tacked
1961 onto the end of the args in some (at least one anyway)
1962 implementations, so strip it off if it exists. */
1965 char *command = elf_tdata (abfd)->core->command;
1966 int n = strlen (command);
1968 if (0 < n && command[n - 1] == ' ')
1969 command[n - 1] = '\0';
1972 return TRUE;
1975 /* Assorted hash table functions. */
1977 /* Initialize an entry in the stub hash table. */
1978 static struct bfd_hash_entry *
1979 stub_hash_newfunc (struct bfd_hash_entry *entry,
1980 struct bfd_hash_table *table,
1981 const char *string)
1983 /* Allocate the structure if it has not already been allocated by a
1984 subclass. */
1985 if (entry == NULL)
1987 entry = bfd_hash_allocate (table,
1988 sizeof (struct elf32_nios2_stub_hash_entry));
1989 if (entry == NULL)
1990 return entry;
1993 /* Call the allocation method of the superclass. */
1994 entry = bfd_hash_newfunc (entry, table, string);
1995 if (entry != NULL)
1997 struct elf32_nios2_stub_hash_entry *hsh;
1999 /* Initialize the local fields. */
2000 hsh = (struct elf32_nios2_stub_hash_entry *) entry;
2001 hsh->stub_sec = NULL;
2002 hsh->stub_offset = 0;
2003 hsh->target_value = 0;
2004 hsh->target_section = NULL;
2005 hsh->stub_type = nios2_stub_none;
2006 hsh->hh = NULL;
2007 hsh->id_sec = NULL;
2010 return entry;
2013 /* Create an entry in a Nios II ELF linker hash table. */
2014 static struct bfd_hash_entry *
2015 link_hash_newfunc (struct bfd_hash_entry *entry,
2016 struct bfd_hash_table *table, const char *string)
2018 /* Allocate the structure if it has not already been allocated by a
2019 subclass. */
2020 if (entry == NULL)
2022 entry = bfd_hash_allocate (table,
2023 sizeof (struct elf32_nios2_link_hash_entry));
2024 if (entry == NULL)
2025 return entry;
2028 /* Call the allocation method of the superclass. */
2029 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
2030 if (entry)
2032 struct elf32_nios2_link_hash_entry *eh;
2034 eh = (struct elf32_nios2_link_hash_entry *) entry;
2035 eh->hsh_cache = NULL;
2036 eh->tls_type = GOT_UNKNOWN;
2037 eh->got_types_used = 0;
2040 return entry;
2043 /* Section name for stubs is the associated section name plus this
2044 string. */
2045 #define STUB_SUFFIX ".stub"
2047 /* Build a name for an entry in the stub hash table. */
2048 static char *
2049 nios2_stub_name (const asection *input_section,
2050 const asection *sym_sec,
2051 const struct elf32_nios2_link_hash_entry *hh,
2052 const Elf_Internal_Rela *rel,
2053 enum elf32_nios2_stub_type stub_type)
2055 char *stub_name;
2056 bfd_size_type len;
2057 char stubpos = (stub_type == nios2_stub_call26_before) ? 'b' : 'a';
2059 if (hh)
2061 len = 8 + 1 + 1 + 1+ strlen (hh->root.root.root.string) + 1 + 8 + 1;
2062 stub_name = bfd_malloc (len);
2063 if (stub_name != NULL)
2065 sprintf (stub_name, "%08x_%c_%s+%x",
2066 input_section->id & 0xffffffff,
2067 stubpos,
2068 hh->root.root.root.string,
2069 (int) rel->r_addend & 0xffffffff);
2072 else
2074 len = 8 + 1 + 1 + 1+ 8 + 1 + 8 + 1 + 8 + 1;
2075 stub_name = bfd_malloc (len);
2076 if (stub_name != NULL)
2078 sprintf (stub_name, "%08x_%c_%x:%x+%x",
2079 input_section->id & 0xffffffff,
2080 stubpos,
2081 sym_sec->id & 0xffffffff,
2082 (int) ELF32_R_SYM (rel->r_info) & 0xffffffff,
2083 (int) rel->r_addend & 0xffffffff);
2086 return stub_name;
2089 /* Look up an entry in the stub hash. Stub entries are cached because
2090 creating the stub name takes a bit of time. */
2091 static struct elf32_nios2_stub_hash_entry *
2092 nios2_get_stub_entry (const asection *input_section,
2093 const asection *sym_sec,
2094 struct elf32_nios2_link_hash_entry *hh,
2095 const Elf_Internal_Rela *rel,
2096 struct elf32_nios2_link_hash_table *htab,
2097 enum elf32_nios2_stub_type stub_type)
2099 struct elf32_nios2_stub_hash_entry *hsh;
2100 const asection *id_sec;
2102 /* If this input section is part of a group of sections sharing one
2103 stub section, then use the id of the first/last section in the group,
2104 depending on the stub section placement relative to the group.
2105 Stub names need to include a section id, as there may well be
2106 more than one stub used to reach say, printf, and we need to
2107 distinguish between them. */
2108 if (stub_type == nios2_stub_call26_before)
2109 id_sec = htab->stub_group[input_section->id].first_sec;
2110 else
2111 id_sec = htab->stub_group[input_section->id].last_sec;
2113 if (hh != NULL && hh->hsh_cache != NULL
2114 && hh->hsh_cache->hh == hh
2115 && hh->hsh_cache->id_sec == id_sec
2116 && hh->hsh_cache->stub_type == stub_type)
2118 hsh = hh->hsh_cache;
2120 else
2122 char *stub_name;
2124 stub_name = nios2_stub_name (id_sec, sym_sec, hh, rel, stub_type);
2125 if (stub_name == NULL)
2126 return NULL;
2128 hsh = nios2_stub_hash_lookup (&htab->bstab,
2129 stub_name, FALSE, FALSE);
2131 if (hh != NULL)
2132 hh->hsh_cache = hsh;
2134 free (stub_name);
2137 return hsh;
2140 /* Add a new stub entry to the stub hash. Not all fields of the new
2141 stub entry are initialised. */
2142 static struct elf32_nios2_stub_hash_entry *
2143 nios2_add_stub (const char *stub_name,
2144 asection *section,
2145 struct elf32_nios2_link_hash_table *htab,
2146 enum elf32_nios2_stub_type stub_type)
2148 asection *link_sec;
2149 asection *stub_sec;
2150 asection **secptr, **linkptr;
2151 struct elf32_nios2_stub_hash_entry *hsh;
2152 bfd_boolean afterp;
2154 if (stub_type == nios2_stub_call26_before)
2156 link_sec = htab->stub_group[section->id].first_sec;
2157 secptr = &(htab->stub_group[section->id].first_stub_sec);
2158 linkptr = &(htab->stub_group[link_sec->id].first_stub_sec);
2159 afterp = FALSE;
2161 else
2163 link_sec = htab->stub_group[section->id].last_sec;
2164 secptr = &(htab->stub_group[section->id].last_stub_sec);
2165 linkptr = &(htab->stub_group[link_sec->id].last_stub_sec);
2166 afterp = TRUE;
2168 stub_sec = *secptr;
2169 if (stub_sec == NULL)
2171 stub_sec = *linkptr;
2172 if (stub_sec == NULL)
2174 size_t namelen;
2175 bfd_size_type len;
2176 char *s_name;
2178 namelen = strlen (link_sec->name);
2179 len = namelen + sizeof (STUB_SUFFIX);
2180 s_name = bfd_alloc (htab->stub_bfd, len);
2181 if (s_name == NULL)
2182 return NULL;
2184 memcpy (s_name, link_sec->name, namelen);
2185 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
2187 stub_sec = (*htab->add_stub_section) (s_name, link_sec, afterp);
2188 if (stub_sec == NULL)
2189 return NULL;
2190 *linkptr = stub_sec;
2192 *secptr = stub_sec;
2195 /* Enter this entry into the linker stub hash table. */
2196 hsh = nios2_stub_hash_lookup (&htab->bstab, stub_name,
2197 TRUE, FALSE);
2198 if (hsh == NULL)
2200 /* xgettext:c-format */
2201 _bfd_error_handler (_("%pB: cannot create stub entry %s"),
2202 section->owner,
2203 stub_name);
2204 return NULL;
2207 hsh->stub_sec = stub_sec;
2208 hsh->stub_offset = 0;
2209 hsh->id_sec = link_sec;
2210 return hsh;
2213 /* Set up various things so that we can make a list of input sections
2214 for each output section included in the link. Returns -1 on error,
2215 0 when no stubs will be needed, and 1 on success. */
2217 nios2_elf32_setup_section_lists (bfd *output_bfd, struct bfd_link_info *info)
2219 bfd *input_bfd;
2220 unsigned int bfd_count;
2221 unsigned int top_id, top_index;
2222 asection *section;
2223 asection **input_list, **list;
2224 size_t amt;
2225 struct elf32_nios2_link_hash_table *htab = elf32_nios2_hash_table (info);
2227 /* Count the number of input BFDs and find the top input section id. */
2228 for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
2229 input_bfd != NULL;
2230 input_bfd = input_bfd->link.next)
2232 bfd_count += 1;
2233 for (section = input_bfd->sections;
2234 section != NULL;
2235 section = section->next)
2237 if (top_id < section->id)
2238 top_id = section->id;
2242 htab->bfd_count = bfd_count;
2244 amt = sizeof (struct map_stub) * (top_id + 1);
2245 htab->stub_group = bfd_zmalloc (amt);
2246 if (htab->stub_group == NULL)
2247 return -1;
2249 /* We can't use output_bfd->section_count here to find the top output
2250 section index as some sections may have been removed, and
2251 strip_excluded_output_sections doesn't renumber the indices. */
2252 for (section = output_bfd->sections, top_index = 0;
2253 section != NULL;
2254 section = section->next)
2256 if (top_index < section->index)
2257 top_index = section->index;
2260 htab->top_index = top_index;
2261 amt = sizeof (asection *) * (top_index + 1);
2262 input_list = bfd_malloc (amt);
2263 htab->input_list = input_list;
2264 if (input_list == NULL)
2265 return -1;
2267 /* For sections we aren't interested in, mark their entries with a
2268 value we can check later. */
2269 list = input_list + top_index;
2271 *list = bfd_abs_section_ptr;
2272 while (list-- != input_list);
2274 for (section = output_bfd->sections;
2275 section != NULL;
2276 section = section->next)
2278 /* FIXME: This is a bit of hack. Currently our .ctors and .dtors
2279 * have PC relative relocs in them but no code flag set. */
2280 if (((section->flags & SEC_CODE) != 0) ||
2281 strcmp(".ctors", section->name) ||
2282 strcmp(".dtors", section->name))
2283 input_list[section->index] = NULL;
2286 return 1;
2289 /* The linker repeatedly calls this function for each input section,
2290 in the order that input sections are linked into output sections.
2291 Build lists of input sections to determine groupings between which
2292 we may insert linker stubs. */
2293 void
2294 nios2_elf32_next_input_section (struct bfd_link_info *info, asection *isec)
2296 struct elf32_nios2_link_hash_table *htab = elf32_nios2_hash_table (info);
2298 if (isec->output_section->index <= htab->top_index)
2300 asection **list = htab->input_list + isec->output_section->index;
2301 if (*list != bfd_abs_section_ptr)
2303 /* Steal the last_sec pointer for our list.
2304 This happens to make the list in reverse order,
2305 which is what we want. */
2306 htab->stub_group[isec->id].last_sec = *list;
2307 *list = isec;
2312 /* Segment mask for CALL26 relocation relaxation. */
2313 #define CALL26_SEGMENT(x) ((x) & 0xf0000000)
2315 /* Fudge factor for approximate maximum size of all stubs that might
2316 be inserted by the linker. This does not actually limit the number
2317 of stubs that might be inserted, and only affects strategy for grouping
2318 and placement of stubs. Perhaps this should be computed based on number
2319 of relocations seen, or be specifiable on the command line. */
2320 #define MAX_STUB_SECTION_SIZE 0xffff
2322 /* See whether we can group stub sections together. Grouping stub
2323 sections may result in fewer stubs. More importantly, we need to
2324 put all .init* and .fini* stubs at the end of the .init or
2325 .fini output sections respectively, because glibc splits the
2326 _init and _fini functions into multiple parts. Putting a stub in
2327 the middle of a function is not a good idea.
2328 Rather than computing groups of a maximum fixed size, for Nios II
2329 CALL26 relaxation it makes more sense to compute the groups based on
2330 sections that fit within a 256MB address segment. Also do not allow
2331 a group to span more than one output section, since different output
2332 sections might correspond to different memory banks on a bare-metal
2333 target, etc. */
2334 static void
2335 group_sections (struct elf32_nios2_link_hash_table *htab)
2337 asection **list = htab->input_list + htab->top_index;
2340 /* The list is in reverse order so we'll search backwards looking
2341 for the first section that begins in the same memory segment,
2342 marking sections along the way to point at the tail for this
2343 group. */
2344 asection *tail = *list;
2345 if (tail == bfd_abs_section_ptr)
2346 continue;
2347 while (tail != NULL)
2349 bfd_vma start = tail->output_section->vma + tail->output_offset;
2350 bfd_vma end = start + tail->size;
2351 bfd_vma segment = CALL26_SEGMENT (end);
2352 asection *prev;
2354 if (segment != CALL26_SEGMENT (start)
2355 || segment != CALL26_SEGMENT (end + MAX_STUB_SECTION_SIZE))
2356 /* This section spans more than one memory segment, or is
2357 close enough to the end of the segment that adding stub
2358 sections before it might cause it to move so that it
2359 spans memory segments, or that stubs added at the end of
2360 this group might overflow into the next memory segment.
2361 Put it in a group by itself to localize the effects. */
2363 prev = htab->stub_group[tail->id].last_sec;
2364 htab->stub_group[tail->id].last_sec = tail;
2365 htab->stub_group[tail->id].first_sec = tail;
2367 else
2368 /* Collect more sections for this group. */
2370 asection *curr, *first;
2371 for (curr = tail; ; curr = prev)
2373 prev = htab->stub_group[curr->id].last_sec;
2374 if (!prev
2375 || tail->output_section != prev->output_section
2376 || (CALL26_SEGMENT (prev->output_section->vma
2377 + prev->output_offset)
2378 != segment))
2379 break;
2381 first = curr;
2382 for (curr = tail; ; curr = prev)
2384 prev = htab->stub_group[curr->id].last_sec;
2385 htab->stub_group[curr->id].last_sec = tail;
2386 htab->stub_group[curr->id].first_sec = first;
2387 if (curr == first)
2388 break;
2392 /* Reset tail for the next group. */
2393 tail = prev;
2396 while (list-- != htab->input_list);
2397 free (htab->input_list);
2400 /* Determine the type of stub needed, if any, for a call. */
2401 static enum elf32_nios2_stub_type
2402 nios2_type_of_stub (asection *input_sec,
2403 const Elf_Internal_Rela *rel,
2404 struct elf32_nios2_link_hash_entry *hh,
2405 struct elf32_nios2_link_hash_table *htab,
2406 bfd_vma destination,
2407 struct bfd_link_info *info ATTRIBUTE_UNUSED)
2409 bfd_vma location, segment, start, end;
2410 asection *s0, *s1, *s;
2412 if (hh != NULL &&
2413 !(hh->root.root.type == bfd_link_hash_defined
2414 || hh->root.root.type == bfd_link_hash_defweak))
2415 return nios2_stub_none;
2417 /* Determine where the call point is. */
2418 location = (input_sec->output_section->vma
2419 + input_sec->output_offset + rel->r_offset);
2420 segment = CALL26_SEGMENT (location);
2422 /* Nios II CALL and JMPI instructions can transfer control to addresses
2423 within the same 256MB segment as the PC. */
2424 if (segment == CALL26_SEGMENT (destination))
2425 return nios2_stub_none;
2427 /* Find the start and end addresses of the stub group. Also account for
2428 any already-created stub sections for this group. Note that for stubs
2429 in the end section, only the first instruction of the last stub
2430 (12 bytes long) needs to be within range. */
2431 s0 = htab->stub_group[input_sec->id].first_sec;
2432 s = htab->stub_group[s0->id].first_stub_sec;
2433 if (s != NULL && s->size > 0)
2434 start = s->output_section->vma + s->output_offset;
2435 else
2436 start = s0->output_section->vma + s0->output_offset;
2438 s1 = htab->stub_group[input_sec->id].last_sec;
2439 s = htab->stub_group[s1->id].last_stub_sec;
2440 if (s != NULL && s->size > 0)
2441 end = s->output_section->vma + s->output_offset + s->size - 8;
2442 else
2443 end = s1->output_section->vma + s1->output_offset + s1->size;
2445 BFD_ASSERT (start < end);
2446 BFD_ASSERT (start <= location);
2447 BFD_ASSERT (location < end);
2449 /* Put stubs at the end of the group unless that is not a valid
2450 location and the beginning of the group is. It might be that
2451 neither the beginning nor end works if we have an input section
2452 so large that it spans multiple segment boundaries. In that
2453 case, punt; the end result will be a relocation overflow error no
2454 matter what we do here.
2456 Note that adding stubs pushes up the addresses of all subsequent
2457 sections, so that stubs allocated on one pass through the
2458 relaxation loop may not be valid on the next pass. (E.g., we may
2459 allocate a stub at the beginning of the section on one pass and
2460 find that the call site has been bumped into the next memory
2461 segment on the next pass.) The important thing to note is that
2462 we never try to reclaim the space allocated to such unused stubs,
2463 so code size and section addresses can only increase with each
2464 iteration. Accounting for the start and end addresses of the
2465 already-created stub sections ensures that when the algorithm
2466 converges, it converges accurately, with the entire appropriate
2467 stub section accessible from the call site and not just the
2468 address at the start or end of the stub group proper. */
2470 if (segment == CALL26_SEGMENT (end))
2471 return nios2_stub_call26_after;
2472 else if (segment == CALL26_SEGMENT (start))
2473 return nios2_stub_call26_before;
2474 else
2475 /* Perhaps this should be a dedicated error code. */
2476 return nios2_stub_none;
2479 static bfd_boolean
2480 nios2_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg ATTRIBUTE_UNUSED)
2482 struct elf32_nios2_stub_hash_entry *hsh
2483 = (struct elf32_nios2_stub_hash_entry *) gen_entry;
2484 asection *stub_sec = hsh->stub_sec;
2485 bfd_vma sym_value;
2486 struct bfd_link_info *info;
2488 info = (struct bfd_link_info *) in_arg;
2490 /* Fail if the target section could not be assigned to an output
2491 section. The user should fix his linker script. */
2492 if (hsh->target_section->output_section == NULL
2493 && info->non_contiguous_regions)
2494 info->callbacks->einfo (_("%F%P: Could not assign '%pA' to an output section. "
2495 "Retry without --enable-non-contiguous-regions.\n"),
2496 hsh->target_section);
2498 /* Make a note of the offset within the stubs for this entry. */
2499 hsh->stub_offset = stub_sec->size;
2501 switch (hsh->stub_type)
2503 case nios2_stub_call26_before:
2504 case nios2_stub_call26_after:
2505 /* A call26 stub looks like:
2506 orhi at, %hiadj(dest)
2507 addi at, at, %lo(dest)
2508 jmp at
2509 Note that call/jmpi instructions can't be used in PIC code
2510 so there is no reason for the stub to be PIC, either. */
2511 sym_value = (hsh->target_value
2512 + hsh->target_section->output_offset
2513 + hsh->target_section->output_section->vma
2514 + hsh->addend);
2516 nios2_elf32_install_data (stub_sec, nios2_call26_stub_entry,
2517 hsh->stub_offset, 3);
2518 nios2_elf32_install_imm16 (stub_sec, hsh->stub_offset,
2519 hiadj (sym_value));
2520 nios2_elf32_install_imm16 (stub_sec, hsh->stub_offset + 4,
2521 (sym_value & 0xffff));
2522 stub_sec->size += 12;
2523 break;
2524 default:
2525 BFD_FAIL ();
2526 return FALSE;
2529 return TRUE;
2532 /* As above, but don't actually build the stub. Just bump offset so
2533 we know stub section sizes. */
2534 static bfd_boolean
2535 nios2_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg ATTRIBUTE_UNUSED)
2537 struct elf32_nios2_stub_hash_entry *hsh
2538 = (struct elf32_nios2_stub_hash_entry *) gen_entry;
2540 switch (hsh->stub_type)
2542 case nios2_stub_call26_before:
2543 case nios2_stub_call26_after:
2544 hsh->stub_sec->size += 12;
2545 break;
2546 default:
2547 BFD_FAIL ();
2548 return FALSE;
2550 return TRUE;
2553 /* Read in all local syms for all input bfds.
2554 Returns -1 on error, 0 otherwise. */
2556 static int
2557 get_local_syms (bfd *output_bfd ATTRIBUTE_UNUSED, bfd *input_bfd,
2558 struct bfd_link_info *info)
2560 unsigned int bfd_indx;
2561 Elf_Internal_Sym *local_syms, **all_local_syms;
2562 struct elf32_nios2_link_hash_table *htab = elf32_nios2_hash_table (info);
2564 /* We want to read in symbol extension records only once. To do this
2565 we need to read in the local symbols in parallel and save them for
2566 later use; so hold pointers to the local symbols in an array. */
2567 size_t amt = sizeof (Elf_Internal_Sym *) * htab->bfd_count;
2568 all_local_syms = bfd_zmalloc (amt);
2569 htab->all_local_syms = all_local_syms;
2570 if (all_local_syms == NULL)
2571 return -1;
2573 /* Walk over all the input BFDs, swapping in local symbols. */
2574 for (bfd_indx = 0;
2575 input_bfd != NULL;
2576 input_bfd = input_bfd->link.next, bfd_indx++)
2578 Elf_Internal_Shdr *symtab_hdr;
2580 /* We'll need the symbol table in a second. */
2581 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2582 if (symtab_hdr->sh_info == 0)
2583 continue;
2585 /* We need an array of the local symbols attached to the input bfd. */
2586 local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
2587 if (local_syms == NULL)
2589 local_syms = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
2590 symtab_hdr->sh_info, 0,
2591 NULL, NULL, NULL);
2592 /* Cache them for elf_link_input_bfd. */
2593 symtab_hdr->contents = (unsigned char *) local_syms;
2595 if (local_syms == NULL)
2596 return -1;
2598 all_local_syms[bfd_indx] = local_syms;
2601 return 0;
2604 /* Determine and set the size of the stub section for a final link. */
2605 bfd_boolean
2606 nios2_elf32_size_stubs (bfd *output_bfd, bfd *stub_bfd,
2607 struct bfd_link_info *info,
2608 asection *(*add_stub_section) (const char *,
2609 asection *, bfd_boolean),
2610 void (*layout_sections_again) (void))
2612 bfd_boolean stub_changed = FALSE;
2613 struct elf32_nios2_link_hash_table *htab = elf32_nios2_hash_table (info);
2615 /* Stash our params away. */
2616 htab->stub_bfd = stub_bfd;
2617 htab->add_stub_section = add_stub_section;
2618 htab->layout_sections_again = layout_sections_again;
2620 /* FIXME: We only compute the section groups once. This could cause
2621 problems if adding a large stub section causes following sections,
2622 or parts of them, to move into another segment. However, this seems
2623 to be consistent with the way other back ends handle this.... */
2624 group_sections (htab);
2626 if (get_local_syms (output_bfd, info->input_bfds, info))
2628 if (htab->all_local_syms)
2629 goto error_ret_free_local;
2630 return FALSE;
2633 while (1)
2635 bfd *input_bfd;
2636 unsigned int bfd_indx;
2637 asection *stub_sec;
2639 for (input_bfd = info->input_bfds, bfd_indx = 0;
2640 input_bfd != NULL;
2641 input_bfd = input_bfd->link.next, bfd_indx++)
2643 Elf_Internal_Shdr *symtab_hdr;
2644 asection *section;
2645 Elf_Internal_Sym *local_syms;
2647 /* We'll need the symbol table in a second. */
2648 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2649 if (symtab_hdr->sh_info == 0)
2650 continue;
2652 local_syms = htab->all_local_syms[bfd_indx];
2654 /* Walk over each section attached to the input bfd. */
2655 for (section = input_bfd->sections;
2656 section != NULL;
2657 section = section->next)
2659 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
2661 /* If there aren't any relocs, then there's nothing more
2662 to do. */
2663 if ((section->flags & SEC_RELOC) == 0
2664 || section->reloc_count == 0)
2665 continue;
2667 /* If this section is a link-once section that will be
2668 discarded, then don't create any stubs. */
2669 if (section->output_section == NULL
2670 || section->output_section->owner != output_bfd)
2671 continue;
2673 /* Get the relocs. */
2674 internal_relocs
2675 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
2676 info->keep_memory);
2677 if (internal_relocs == NULL)
2678 goto error_ret_free_local;
2680 /* Now examine each relocation. */
2681 irela = internal_relocs;
2682 irelaend = irela + section->reloc_count;
2683 for (; irela < irelaend; irela++)
2685 unsigned int r_type, r_indx;
2686 enum elf32_nios2_stub_type stub_type;
2687 struct elf32_nios2_stub_hash_entry *hsh;
2688 asection *sym_sec;
2689 bfd_vma sym_value;
2690 bfd_vma destination;
2691 struct elf32_nios2_link_hash_entry *hh;
2692 char *stub_name;
2693 const asection *id_sec;
2695 r_type = ELF32_R_TYPE (irela->r_info);
2696 r_indx = ELF32_R_SYM (irela->r_info);
2698 if (r_type >= (unsigned int) R_NIOS2_ILLEGAL)
2700 bfd_set_error (bfd_error_bad_value);
2701 error_ret_free_internal:
2702 if (elf_section_data (section)->relocs == NULL)
2703 free (internal_relocs);
2704 goto error_ret_free_local;
2707 /* Only look for stubs on CALL and JMPI instructions. */
2708 if (r_type != (unsigned int) R_NIOS2_CALL26)
2709 continue;
2711 /* Now determine the call target, its name, value,
2712 section. */
2713 sym_sec = NULL;
2714 sym_value = 0;
2715 destination = 0;
2716 hh = NULL;
2717 if (r_indx < symtab_hdr->sh_info)
2719 /* It's a local symbol. */
2720 Elf_Internal_Sym *sym;
2721 Elf_Internal_Shdr *hdr;
2722 unsigned int shndx;
2724 sym = local_syms + r_indx;
2725 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
2726 sym_value = sym->st_value;
2727 shndx = sym->st_shndx;
2728 if (shndx < elf_numsections (input_bfd))
2730 hdr = elf_elfsections (input_bfd)[shndx];
2731 sym_sec = hdr->bfd_section;
2732 destination = (sym_value + irela->r_addend
2733 + sym_sec->output_offset
2734 + sym_sec->output_section->vma);
2737 else
2739 /* It's an external symbol. */
2740 int e_indx;
2742 e_indx = r_indx - symtab_hdr->sh_info;
2743 hh = ((struct elf32_nios2_link_hash_entry *)
2744 elf_sym_hashes (input_bfd)[e_indx]);
2746 while (hh->root.root.type == bfd_link_hash_indirect
2747 || hh->root.root.type == bfd_link_hash_warning)
2748 hh = ((struct elf32_nios2_link_hash_entry *)
2749 hh->root.root.u.i.link);
2751 if (hh->root.root.type == bfd_link_hash_defined
2752 || hh->root.root.type == bfd_link_hash_defweak)
2754 sym_sec = hh->root.root.u.def.section;
2755 sym_value = hh->root.root.u.def.value;
2757 if (sym_sec->output_section != NULL)
2758 destination = (sym_value + irela->r_addend
2759 + sym_sec->output_offset
2760 + sym_sec->output_section->vma);
2761 else
2762 continue;
2764 else if (hh->root.root.type == bfd_link_hash_undefweak)
2766 if (! bfd_link_pic (info))
2767 continue;
2769 else if (hh->root.root.type == bfd_link_hash_undefined)
2771 if (! (info->unresolved_syms_in_objects == RM_IGNORE
2772 && (ELF_ST_VISIBILITY (hh->root.other)
2773 == STV_DEFAULT)))
2774 continue;
2776 else
2778 bfd_set_error (bfd_error_bad_value);
2779 goto error_ret_free_internal;
2783 /* Determine what (if any) linker stub is needed. */
2784 stub_type = nios2_type_of_stub (section, irela, hh, htab,
2785 destination, info);
2786 if (stub_type == nios2_stub_none)
2787 continue;
2789 /* Support for grouping stub sections. */
2790 if (stub_type == nios2_stub_call26_before)
2791 id_sec = htab->stub_group[section->id].first_sec;
2792 else
2793 id_sec = htab->stub_group[section->id].last_sec;
2795 /* Get the name of this stub. */
2796 stub_name = nios2_stub_name (id_sec, sym_sec, hh, irela,
2797 stub_type);
2798 if (!stub_name)
2799 goto error_ret_free_internal;
2801 hsh = nios2_stub_hash_lookup (&htab->bstab,
2802 stub_name,
2803 FALSE, FALSE);
2804 if (hsh != NULL)
2806 /* The proper stub has already been created. */
2807 free (stub_name);
2808 continue;
2811 hsh = nios2_add_stub (stub_name, section, htab, stub_type);
2812 if (hsh == NULL)
2814 free (stub_name);
2815 goto error_ret_free_internal;
2817 hsh->target_value = sym_value;
2818 hsh->target_section = sym_sec;
2819 hsh->stub_type = stub_type;
2820 hsh->hh = hh;
2821 hsh->addend = irela->r_addend;
2822 stub_changed = TRUE;
2825 /* We're done with the internal relocs, free them. */
2826 if (elf_section_data (section)->relocs == NULL)
2827 free (internal_relocs);
2831 if (!stub_changed)
2832 break;
2834 /* OK, we've added some stubs. Find out the new size of the
2835 stub sections. */
2836 for (stub_sec = htab->stub_bfd->sections;
2837 stub_sec != NULL;
2838 stub_sec = stub_sec->next)
2839 stub_sec->size = 0;
2841 bfd_hash_traverse (&htab->bstab, nios2_size_one_stub, htab);
2843 /* Ask the linker to do its stuff. */
2844 (*htab->layout_sections_again) ();
2845 stub_changed = FALSE;
2848 free (htab->all_local_syms);
2849 return TRUE;
2851 error_ret_free_local:
2852 free (htab->all_local_syms);
2853 return FALSE;
2856 /* Build all the stubs associated with the current output file. The
2857 stubs are kept in a hash table attached to the main linker hash
2858 table. This function is called via nios2elf_finish in the linker. */
2859 bfd_boolean
2860 nios2_elf32_build_stubs (struct bfd_link_info *info)
2862 asection *stub_sec;
2863 struct bfd_hash_table *table;
2864 struct elf32_nios2_link_hash_table *htab;
2866 htab = elf32_nios2_hash_table (info);
2868 for (stub_sec = htab->stub_bfd->sections;
2869 stub_sec != NULL;
2870 stub_sec = stub_sec->next)
2871 /* The stub_bfd may contain non-stub sections if it is also the
2872 dynobj. Any such non-stub sections are created with the
2873 SEC_LINKER_CREATED flag set, while stub sections do not
2874 have that flag. Ignore any non-stub sections here. */
2875 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
2877 bfd_size_type size;
2879 /* Allocate memory to hold the linker stubs. */
2880 size = stub_sec->size;
2881 stub_sec->contents = bfd_zalloc (htab->stub_bfd, size);
2882 if (stub_sec->contents == NULL && size != 0)
2883 return FALSE;
2884 stub_sec->size = 0;
2887 /* Build the stubs as directed by the stub hash table. */
2888 table = &htab->bstab;
2889 bfd_hash_traverse (table, nios2_build_one_stub, info);
2891 return TRUE;
2895 #define is_nios2_elf(bfd) \
2896 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
2897 && elf_object_id (bfd) == NIOS2_ELF_DATA)
2899 /* Merge backend specific data from an object file to the output
2900 object file when linking. */
2902 static bfd_boolean
2903 nios2_elf32_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
2905 bfd *obfd = info->output_bfd;
2906 flagword old_flags;
2907 flagword new_flags;
2909 if (!is_nios2_elf (ibfd) || !is_nios2_elf (obfd))
2910 return TRUE;
2912 /* Check if we have the same endianness. */
2913 if (! _bfd_generic_verify_endian_match (ibfd, info))
2914 return FALSE;
2916 new_flags = elf_elfheader (ibfd)->e_flags;
2917 old_flags = elf_elfheader (obfd)->e_flags;
2918 if (!elf_flags_init (obfd))
2920 /* First call, no flags set. */
2921 elf_flags_init (obfd) = TRUE;
2922 elf_elfheader (obfd)->e_flags = new_flags;
2924 switch (new_flags)
2926 default:
2927 case EF_NIOS2_ARCH_R1:
2928 bfd_default_set_arch_mach (obfd, bfd_arch_nios2, bfd_mach_nios2r1);
2929 break;
2930 case EF_NIOS2_ARCH_R2:
2931 if (bfd_big_endian (ibfd))
2933 _bfd_error_handler
2934 (_("error: %pB: big-endian R2 is not supported"), ibfd);
2935 bfd_set_error (bfd_error_bad_value);
2936 return FALSE;
2938 bfd_default_set_arch_mach (obfd, bfd_arch_nios2, bfd_mach_nios2r2);
2939 break;
2943 /* Incompatible flags. */
2944 else if (new_flags != old_flags)
2946 /* So far, the only incompatible flags denote incompatible
2947 architectures. */
2948 _bfd_error_handler
2949 /* xgettext:c-format */
2950 (_("error: %pB: conflicting CPU architectures %d/%d"),
2951 ibfd, new_flags, old_flags);
2952 bfd_set_error (bfd_error_bad_value);
2953 return FALSE;
2956 /* Merge Tag_compatibility attributes and any common GNU ones. */
2957 _bfd_elf_merge_object_attributes (ibfd, info);
2959 return TRUE;
2962 /* Implement bfd_elf32_bfd_reloc_type_lookup:
2963 Given a BFD reloc type, return a howto structure. */
2965 static reloc_howto_type *
2966 nios2_elf32_bfd_reloc_type_lookup (bfd *abfd,
2967 bfd_reloc_code_real_type code)
2969 int i;
2971 for (i = 0; i < (int) ARRAY_SIZE (nios2_reloc_map); ++i)
2972 if (nios2_reloc_map[i].bfd_val == code)
2973 return lookup_howto (nios2_reloc_map[i].elf_val, abfd);
2974 return NULL;
2977 /* Implement bfd_elf32_bfd_reloc_name_lookup:
2978 Given a reloc name, return a howto structure. */
2980 static reloc_howto_type *
2981 nios2_elf32_bfd_reloc_name_lookup (bfd *abfd,
2982 const char *r_name)
2984 int i;
2985 reloc_howto_type *howto_tbl;
2986 int howto_tbl_size;
2988 if (BFD_IS_R2 (abfd))
2990 howto_tbl = elf_nios2_r2_howto_table_rel;
2991 howto_tbl_size = (int) ARRAY_SIZE (elf_nios2_r2_howto_table_rel);
2993 else
2995 howto_tbl = elf_nios2_r1_howto_table_rel;
2996 howto_tbl_size = (int) ARRAY_SIZE (elf_nios2_r1_howto_table_rel);
2999 for (i = 0; i < howto_tbl_size; i++)
3000 if (howto_tbl[i].name && strcasecmp (howto_tbl[i].name, r_name) == 0)
3001 return howto_tbl + i;
3003 return NULL;
3006 /* Implement elf_info_to_howto:
3007 Given a ELF32 relocation, fill in a arelent structure. */
3009 static bfd_boolean
3010 nios2_elf32_info_to_howto (bfd *abfd, arelent *cache_ptr,
3011 Elf_Internal_Rela *dst)
3013 unsigned int r_type;
3015 r_type = ELF32_R_TYPE (dst->r_info);
3016 if ((cache_ptr->howto = lookup_howto (r_type, abfd)) == NULL)
3018 /* xgettext:c-format */
3019 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
3020 abfd, r_type);
3021 bfd_set_error (bfd_error_bad_value);
3022 return FALSE;
3024 return TRUE;
3027 /* Return the base VMA address which should be subtracted from real addresses
3028 when resolving @dtpoff relocation.
3029 This is PT_TLS segment p_vaddr. */
3030 static bfd_vma
3031 dtpoff_base (struct bfd_link_info *info)
3033 /* If tls_sec is NULL, we should have signalled an error already. */
3034 if (elf_hash_table (info)->tls_sec == NULL)
3035 return 0;
3036 return elf_hash_table (info)->tls_sec->vma;
3039 /* Return the relocation value for @tpoff relocation
3040 if STT_TLS virtual address is ADDRESS. */
3041 static bfd_vma
3042 tpoff (struct bfd_link_info *info, bfd_vma address)
3044 struct elf_link_hash_table *htab = elf_hash_table (info);
3046 /* If tls_sec is NULL, we should have signalled an error already. */
3047 if (htab->tls_sec == NULL)
3048 return 0;
3049 return address - htab->tls_sec->vma;
3052 /* Set the GP value for OUTPUT_BFD. Returns FALSE if this is a
3053 dangerous relocation. */
3054 static bfd_boolean
3055 nios2_elf_assign_gp (bfd *output_bfd, bfd_vma *pgp, struct bfd_link_info *info)
3058 bfd_boolean gp_found;
3059 struct bfd_hash_entry *h;
3060 struct bfd_link_hash_entry *lh;
3062 /* If we've already figured out what GP will be, just return it. */
3063 *pgp = _bfd_get_gp_value (output_bfd);
3064 if (*pgp)
3065 return TRUE;
3067 h = bfd_hash_lookup (&info->hash->table, "_gp", FALSE, FALSE);
3068 lh = (struct bfd_link_hash_entry *) h;
3069 lookup:
3070 if (lh)
3072 switch (lh->type)
3074 case bfd_link_hash_undefined:
3075 case bfd_link_hash_undefweak:
3076 case bfd_link_hash_common:
3077 gp_found = FALSE;
3078 break;
3079 case bfd_link_hash_defined:
3080 case bfd_link_hash_defweak:
3081 gp_found = TRUE;
3083 asection *sym_sec = lh->u.def.section;
3084 bfd_vma sym_value = lh->u.def.value;
3086 if (sym_sec->output_section)
3087 sym_value = (sym_value + sym_sec->output_offset
3088 + sym_sec->output_section->vma);
3089 *pgp = sym_value;
3091 break;
3092 case bfd_link_hash_indirect:
3093 case bfd_link_hash_warning:
3094 lh = lh->u.i.link;
3095 /* @@FIXME ignoring warning for now */
3096 goto lookup;
3097 case bfd_link_hash_new:
3098 default:
3099 abort ();
3102 else
3103 gp_found = FALSE;
3105 if (!gp_found)
3107 /* Only get the error once. */
3108 *pgp = 4;
3109 _bfd_set_gp_value (output_bfd, *pgp);
3110 return FALSE;
3113 _bfd_set_gp_value (output_bfd, *pgp);
3115 return TRUE;
3118 /* Retrieve the previously cached _gp pointer, returning bfd_reloc_dangerous
3119 if it's not available as we don't have a link_info pointer available here
3120 to look it up in the output symbol table. We don't need to adjust the
3121 symbol value for an external symbol if we are producing relocatable
3122 output. */
3123 static bfd_reloc_status_type
3124 nios2_elf_final_gp (bfd *output_bfd, asymbol *symbol, bfd_boolean relocatable,
3125 char **error_message, bfd_vma *pgp)
3127 if (bfd_is_und_section (symbol->section) && !relocatable)
3129 *pgp = 0;
3130 return bfd_reloc_undefined;
3133 *pgp = _bfd_get_gp_value (output_bfd);
3134 if (*pgp == 0 && (!relocatable || (symbol->flags & BSF_SECTION_SYM) != 0))
3136 if (relocatable)
3138 /* Make up a value. */
3139 *pgp = symbol->section->output_section->vma + 0x4000;
3140 _bfd_set_gp_value (output_bfd, *pgp);
3142 else
3144 *error_message
3145 = (char *) _("global pointer relative relocation when _gp not defined");
3146 return bfd_reloc_dangerous;
3150 return bfd_reloc_ok;
3153 /* Do the relocations that require special handling. */
3154 static bfd_reloc_status_type
3155 nios2_elf32_do_hi16_relocate (bfd *abfd, reloc_howto_type *howto,
3156 asection *input_section,
3157 bfd_byte *data, bfd_vma offset,
3158 bfd_vma symbol_value, bfd_vma addend)
3160 symbol_value = symbol_value + addend;
3161 addend = 0;
3162 symbol_value = (symbol_value >> 16) & 0xffff;
3163 return _bfd_final_link_relocate (howto, abfd, input_section,
3164 data, offset, symbol_value, addend);
3167 static bfd_reloc_status_type
3168 nios2_elf32_do_lo16_relocate (bfd *abfd, reloc_howto_type *howto,
3169 asection *input_section,
3170 bfd_byte *data, bfd_vma offset,
3171 bfd_vma symbol_value, bfd_vma addend)
3173 symbol_value = symbol_value + addend;
3174 addend = 0;
3175 symbol_value = symbol_value & 0xffff;
3176 return _bfd_final_link_relocate (howto, abfd, input_section,
3177 data, offset, symbol_value, addend);
3180 static bfd_reloc_status_type
3181 nios2_elf32_do_hiadj16_relocate (bfd *abfd, reloc_howto_type *howto,
3182 asection *input_section,
3183 bfd_byte *data, bfd_vma offset,
3184 bfd_vma symbol_value, bfd_vma addend)
3186 symbol_value = symbol_value + addend;
3187 addend = 0;
3188 symbol_value = hiadj(symbol_value);
3189 return _bfd_final_link_relocate (howto, abfd, input_section, data, offset,
3190 symbol_value, addend);
3193 static bfd_reloc_status_type
3194 nios2_elf32_do_pcrel_lo16_relocate (bfd *abfd, reloc_howto_type *howto,
3195 asection *input_section,
3196 bfd_byte *data, bfd_vma offset,
3197 bfd_vma symbol_value, bfd_vma addend)
3199 symbol_value = symbol_value + addend;
3200 addend = 0;
3201 symbol_value = symbol_value & 0xffff;
3202 return _bfd_final_link_relocate (howto, abfd, input_section,
3203 data, offset, symbol_value, addend);
3206 static bfd_reloc_status_type
3207 nios2_elf32_do_pcrel_hiadj16_relocate (bfd *abfd, reloc_howto_type *howto,
3208 asection *input_section,
3209 bfd_byte *data, bfd_vma offset,
3210 bfd_vma symbol_value, bfd_vma addend)
3212 symbol_value = symbol_value + addend;
3213 symbol_value -= (input_section->output_section->vma
3214 + input_section->output_offset);
3215 symbol_value -= offset;
3216 addend = 0;
3217 symbol_value = hiadj(symbol_value);
3218 return _bfd_final_link_relocate (howto, abfd, input_section, data, offset,
3219 symbol_value, addend);
3222 static bfd_reloc_status_type
3223 nios2_elf32_do_pcrel16_relocate (bfd *abfd, reloc_howto_type *howto,
3224 asection *input_section,
3225 bfd_byte *data, bfd_vma offset,
3226 bfd_vma symbol_value, bfd_vma addend)
3228 /* NIOS2 pc relative relocations are relative to the next 32-bit instruction
3229 so we need to subtract 4 before doing a final_link_relocate. */
3230 symbol_value = symbol_value + addend - 4;
3231 addend = 0;
3232 return _bfd_final_link_relocate (howto, abfd, input_section,
3233 data, offset, symbol_value, addend);
3236 static bfd_reloc_status_type
3237 nios2_elf32_do_call26_relocate (bfd *abfd, reloc_howto_type *howto,
3238 asection *input_section,
3239 bfd_byte *data, bfd_vma offset,
3240 bfd_vma symbol_value, bfd_vma addend)
3242 /* Check that the relocation is in the same page as the current address. */
3243 if (CALL26_SEGMENT (symbol_value + addend)
3244 != CALL26_SEGMENT (input_section->output_section->vma
3245 + input_section->output_offset
3246 + offset))
3247 return bfd_reloc_overflow;
3249 /* Check that the target address is correctly aligned on a 4-byte
3250 boundary. */
3251 if ((symbol_value + addend) & 0x3)
3252 return bfd_reloc_overflow;
3254 return _bfd_final_link_relocate (howto, abfd, input_section,
3255 data, offset, symbol_value, addend);
3258 static bfd_reloc_status_type
3259 nios2_elf32_do_gprel_relocate (bfd *abfd, reloc_howto_type *howto,
3260 asection *input_section,
3261 bfd_byte *data, bfd_vma offset,
3262 bfd_vma symbol_value, bfd_vma addend)
3264 /* Because we need the output_bfd, the special handling is done
3265 in nios2_elf32_relocate_section or in nios2_elf32_gprel_relocate. */
3266 return _bfd_final_link_relocate (howto, abfd, input_section,
3267 data, offset, symbol_value, addend);
3270 static bfd_reloc_status_type
3271 nios2_elf32_do_ujmp_relocate (bfd *abfd, reloc_howto_type *howto,
3272 asection *input_section,
3273 bfd_byte *data, bfd_vma offset,
3274 bfd_vma symbol_value, bfd_vma addend)
3276 bfd_vma symbol_lo16, symbol_hi16;
3277 bfd_reloc_status_type r;
3278 symbol_value = symbol_value + addend;
3279 addend = 0;
3280 symbol_hi16 = (symbol_value >> 16) & 0xffff;
3281 symbol_lo16 = symbol_value & 0xffff;
3283 r = _bfd_final_link_relocate (howto, abfd, input_section,
3284 data, offset, symbol_hi16, addend);
3286 if (r == bfd_reloc_ok)
3287 return _bfd_final_link_relocate (howto, abfd, input_section,
3288 data, offset + 4, symbol_lo16, addend);
3290 return r;
3293 static bfd_reloc_status_type
3294 nios2_elf32_do_cjmp_relocate (bfd *abfd, reloc_howto_type *howto,
3295 asection *input_section,
3296 bfd_byte *data, bfd_vma offset,
3297 bfd_vma symbol_value, bfd_vma addend)
3299 bfd_vma symbol_lo16, symbol_hi16;
3300 bfd_reloc_status_type r;
3301 symbol_value = symbol_value + addend;
3302 addend = 0;
3303 symbol_hi16 = (symbol_value >> 16) & 0xffff;
3304 symbol_lo16 = symbol_value & 0xffff;
3306 r = _bfd_final_link_relocate (howto, abfd, input_section,
3307 data, offset, symbol_hi16, addend);
3309 if (r == bfd_reloc_ok)
3310 return _bfd_final_link_relocate (howto, abfd, input_section,
3311 data, offset + 4, symbol_lo16, addend);
3313 return r;
3316 static bfd_reloc_status_type
3317 nios2_elf32_do_callr_relocate (bfd *abfd, reloc_howto_type *howto,
3318 asection *input_section,
3319 bfd_byte *data, bfd_vma offset,
3320 bfd_vma symbol_value, bfd_vma addend)
3322 bfd_vma symbol_lo16, symbol_hi16;
3323 bfd_reloc_status_type r;
3324 symbol_value = symbol_value + addend;
3325 addend = 0;
3326 symbol_hi16 = (symbol_value >> 16) & 0xffff;
3327 symbol_lo16 = symbol_value & 0xffff;
3329 r = _bfd_final_link_relocate (howto, abfd, input_section,
3330 data, offset, symbol_hi16, addend);
3332 if (r == bfd_reloc_ok)
3333 return _bfd_final_link_relocate (howto, abfd, input_section,
3334 data, offset + 4, symbol_lo16, addend);
3336 return r;
3339 /* HOWTO handlers for relocations that require special handling. */
3341 /* This is for relocations used only when relaxing to ensure
3342 changes in size of section don't screw up .align. */
3343 static bfd_reloc_status_type
3344 nios2_elf32_ignore_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
3345 asymbol *symbol ATTRIBUTE_UNUSED,
3346 void *data ATTRIBUTE_UNUSED, asection *input_section,
3347 bfd *output_bfd,
3348 char **error_message ATTRIBUTE_UNUSED)
3350 if (output_bfd != NULL)
3351 reloc_entry->address += input_section->output_offset;
3352 return bfd_reloc_ok;
3355 static bfd_reloc_status_type
3356 nios2_elf32_hi16_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
3357 void *data, asection *input_section,
3358 bfd *output_bfd,
3359 char **error_message ATTRIBUTE_UNUSED)
3361 /* This part is from bfd_elf_generic_reloc. */
3362 if (output_bfd != NULL
3363 && (symbol->flags & BSF_SECTION_SYM) == 0
3364 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
3366 reloc_entry->address += input_section->output_offset;
3367 return bfd_reloc_ok;
3370 if (output_bfd != NULL)
3371 /* FIXME: See bfd_perform_relocation. Is this right? */
3372 return bfd_reloc_continue;
3374 return nios2_elf32_do_hi16_relocate (abfd, reloc_entry->howto,
3375 input_section,
3376 data, reloc_entry->address,
3377 (symbol->value
3378 + symbol->section->output_section->vma
3379 + symbol->section->output_offset),
3380 reloc_entry->addend);
3383 static bfd_reloc_status_type
3384 nios2_elf32_lo16_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
3385 void *data, asection *input_section,
3386 bfd *output_bfd,
3387 char **error_message ATTRIBUTE_UNUSED)
3389 /* This part is from bfd_elf_generic_reloc. */
3390 if (output_bfd != NULL
3391 && (symbol->flags & BSF_SECTION_SYM) == 0
3392 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
3394 reloc_entry->address += input_section->output_offset;
3395 return bfd_reloc_ok;
3398 if (output_bfd != NULL)
3399 /* FIXME: See bfd_perform_relocation. Is this right? */
3400 return bfd_reloc_continue;
3402 return nios2_elf32_do_lo16_relocate (abfd, reloc_entry->howto,
3403 input_section,
3404 data, reloc_entry->address,
3405 (symbol->value
3406 + symbol->section->output_section->vma
3407 + symbol->section->output_offset),
3408 reloc_entry->addend);
3411 static bfd_reloc_status_type
3412 nios2_elf32_hiadj16_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
3413 void *data, asection *input_section,
3414 bfd *output_bfd,
3415 char **error_message ATTRIBUTE_UNUSED)
3417 /* This part is from bfd_elf_generic_reloc. */
3418 if (output_bfd != NULL
3419 && (symbol->flags & BSF_SECTION_SYM) == 0
3420 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
3422 reloc_entry->address += input_section->output_offset;
3423 return bfd_reloc_ok;
3426 if (output_bfd != NULL)
3427 /* FIXME: See bfd_perform_relocation. Is this right? */
3428 return bfd_reloc_continue;
3430 return nios2_elf32_do_hiadj16_relocate (abfd, reloc_entry->howto,
3431 input_section,
3432 data, reloc_entry->address,
3433 (symbol->value
3434 + symbol->section->output_section->vma
3435 + symbol->section->output_offset),
3436 reloc_entry->addend);
3439 static bfd_reloc_status_type
3440 nios2_elf32_pcrel_lo16_relocate (bfd *abfd, arelent *reloc_entry,
3441 asymbol *symbol, void *data,
3442 asection *input_section, bfd *output_bfd,
3443 char **error_message ATTRIBUTE_UNUSED)
3445 /* This part is from bfd_elf_generic_reloc. */
3446 if (output_bfd != NULL
3447 && (symbol->flags & BSF_SECTION_SYM) == 0
3448 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
3450 reloc_entry->address += input_section->output_offset;
3451 return bfd_reloc_ok;
3454 if (output_bfd != NULL)
3455 /* FIXME: See bfd_perform_relocation. Is this right? */
3456 return bfd_reloc_continue;
3458 return nios2_elf32_do_pcrel_lo16_relocate (
3459 abfd, reloc_entry->howto, input_section, data, reloc_entry->address,
3460 (symbol->value + symbol->section->output_section->vma
3461 + symbol->section->output_offset),
3462 reloc_entry->addend);
3465 static bfd_reloc_status_type
3466 nios2_elf32_pcrel_hiadj16_relocate (bfd *abfd, arelent *reloc_entry,
3467 asymbol *symbol, void *data,
3468 asection *input_section, bfd *output_bfd,
3469 char **error_message ATTRIBUTE_UNUSED)
3471 /* This part is from bfd_elf_generic_reloc. */
3472 if (output_bfd != NULL
3473 && (symbol->flags & BSF_SECTION_SYM) == 0
3474 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
3476 reloc_entry->address += input_section->output_offset;
3477 return bfd_reloc_ok;
3480 if (output_bfd != NULL)
3481 /* FIXME: See bfd_perform_relocation. Is this right? */
3482 return bfd_reloc_continue;
3484 return nios2_elf32_do_pcrel_hiadj16_relocate (
3485 abfd, reloc_entry->howto, input_section, data, reloc_entry->address,
3486 (symbol->value + symbol->section->output_section->vma
3487 + symbol->section->output_offset),
3488 reloc_entry->addend);
3491 static bfd_reloc_status_type
3492 nios2_elf32_pcrel16_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
3493 void *data, asection *input_section,
3494 bfd *output_bfd,
3495 char **error_message ATTRIBUTE_UNUSED)
3497 /* This part is from bfd_elf_generic_reloc. */
3498 if (output_bfd != NULL
3499 && (symbol->flags & BSF_SECTION_SYM) == 0
3500 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
3502 reloc_entry->address += input_section->output_offset;
3503 return bfd_reloc_ok;
3506 if (output_bfd != NULL)
3507 /* FIXME: See bfd_perform_relocation. Is this right? */
3508 return bfd_reloc_continue;
3510 return nios2_elf32_do_pcrel16_relocate (abfd, reloc_entry->howto,
3511 input_section,
3512 data, reloc_entry->address,
3513 (symbol->value
3514 + symbol->section->output_section->vma
3515 + symbol->section->output_offset),
3516 reloc_entry->addend);
3519 static bfd_reloc_status_type
3520 nios2_elf32_call26_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
3521 void *data, asection *input_section,
3522 bfd *output_bfd,
3523 char **error_message ATTRIBUTE_UNUSED)
3525 /* This part is from bfd_elf_generic_reloc. */
3526 if (output_bfd != NULL
3527 && (symbol->flags & BSF_SECTION_SYM) == 0
3528 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
3530 reloc_entry->address += input_section->output_offset;
3531 return bfd_reloc_ok;
3534 if (output_bfd != NULL)
3535 /* FIXME: See bfd_perform_relocation. Is this right? */
3536 return bfd_reloc_continue;
3538 return nios2_elf32_do_call26_relocate (abfd, reloc_entry->howto,
3539 input_section,
3540 data, reloc_entry->address,
3541 (symbol->value
3542 + symbol->section->output_section->vma
3543 + symbol->section->output_offset),
3544 reloc_entry->addend);
3547 static bfd_reloc_status_type
3548 nios2_elf32_gprel_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
3549 void *data, asection *input_section,
3550 bfd *output_bfd, char **msg)
3552 bfd_vma relocation;
3553 bfd_vma gp;
3554 bfd_reloc_status_type r;
3557 /* This part is from bfd_elf_generic_reloc. */
3558 if (output_bfd != NULL
3559 && (symbol->flags & BSF_SECTION_SYM) == 0
3560 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
3562 reloc_entry->address += input_section->output_offset;
3563 return bfd_reloc_ok;
3566 if (output_bfd != NULL)
3567 /* FIXME: See bfd_perform_relocation. Is this right? */
3568 return bfd_reloc_continue;
3570 relocation = (symbol->value
3571 + symbol->section->output_section->vma
3572 + symbol->section->output_offset);
3574 /* This assumes we've already cached the _gp symbol. */
3575 r = nios2_elf_final_gp (abfd, symbol, FALSE, msg, &gp);
3576 if (r == bfd_reloc_ok)
3578 relocation = relocation + reloc_entry->addend - gp;
3579 reloc_entry->addend = 0;
3580 if ((signed) relocation < -32768 || (signed) relocation > 32767)
3582 *msg = _("global pointer relative address out of range");
3583 r = bfd_reloc_outofrange;
3585 else
3586 r = nios2_elf32_do_gprel_relocate (abfd, reloc_entry->howto,
3587 input_section,
3588 data, reloc_entry->address,
3589 relocation, reloc_entry->addend);
3592 return r;
3595 static bfd_reloc_status_type
3596 nios2_elf32_ujmp_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
3597 void *data, asection *input_section,
3598 bfd *output_bfd, char **msg ATTRIBUTE_UNUSED)
3600 /* This part is from bfd_elf_generic_reloc. */
3601 if (output_bfd != NULL
3602 && (symbol->flags & BSF_SECTION_SYM) == 0
3603 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
3605 reloc_entry->address += input_section->output_offset;
3606 return bfd_reloc_ok;
3609 if (output_bfd != NULL)
3610 /* FIXME: See bfd_perform_relocation. Is this right? */
3611 return bfd_reloc_continue;
3613 return nios2_elf32_do_ujmp_relocate (abfd, reloc_entry->howto,
3614 input_section,
3615 data, reloc_entry->address,
3616 (symbol->value
3617 + symbol->section->output_section->vma
3618 + symbol->section->output_offset),
3619 reloc_entry->addend);
3622 static bfd_reloc_status_type
3623 nios2_elf32_cjmp_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
3624 void *data, asection *input_section,
3625 bfd *output_bfd, char **msg ATTRIBUTE_UNUSED)
3627 /* This part is from bfd_elf_generic_reloc. */
3628 if (output_bfd != NULL
3629 && (symbol->flags & BSF_SECTION_SYM) == 0
3630 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
3632 reloc_entry->address += input_section->output_offset;
3633 return bfd_reloc_ok;
3636 if (output_bfd != NULL)
3637 /* FIXME: See bfd_perform_relocation. Is this right? */
3638 return bfd_reloc_continue;
3640 return nios2_elf32_do_cjmp_relocate (abfd, reloc_entry->howto,
3641 input_section,
3642 data, reloc_entry->address,
3643 (symbol->value
3644 + symbol->section->output_section->vma
3645 + symbol->section->output_offset),
3646 reloc_entry->addend);
3649 static bfd_reloc_status_type
3650 nios2_elf32_callr_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
3651 void *data, asection *input_section,
3652 bfd *output_bfd, char **msg ATTRIBUTE_UNUSED)
3654 /* This part is from bfd_elf_generic_reloc. */
3655 if (output_bfd != NULL
3656 && (symbol->flags & BSF_SECTION_SYM) == 0
3657 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
3659 reloc_entry->address += input_section->output_offset;
3660 return bfd_reloc_ok;
3663 if (output_bfd != NULL)
3664 /* FIXME: See bfd_perform_relocation. Is this right? */
3665 return bfd_reloc_continue;
3667 return nios2_elf32_do_callr_relocate (abfd, reloc_entry->howto,
3668 input_section,
3669 data, reloc_entry->address,
3670 (symbol->value
3671 + symbol->section->output_section->vma
3672 + symbol->section->output_offset),
3673 reloc_entry->addend);
3677 /* Implement elf_backend_relocate_section. */
3678 static bfd_boolean
3679 nios2_elf32_relocate_section (bfd *output_bfd,
3680 struct bfd_link_info *info,
3681 bfd *input_bfd,
3682 asection *input_section,
3683 bfd_byte *contents,
3684 Elf_Internal_Rela *relocs,
3685 Elf_Internal_Sym *local_syms,
3686 asection **local_sections)
3688 Elf_Internal_Shdr *symtab_hdr;
3689 struct elf_link_hash_entry **sym_hashes;
3690 Elf_Internal_Rela *rel;
3691 Elf_Internal_Rela *relend;
3692 struct elf32_nios2_link_hash_table *htab;
3693 asection *sgot;
3694 asection *splt;
3695 asection *sreloc = NULL;
3696 bfd_vma *local_got_offsets;
3697 bfd_vma got_base;
3699 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3700 sym_hashes = elf_sym_hashes (input_bfd);
3701 relend = relocs + input_section->reloc_count;
3703 htab = elf32_nios2_hash_table (info);
3704 sgot = htab->root.sgot;
3705 splt = htab->root.splt;
3706 local_got_offsets = elf_local_got_offsets (input_bfd);
3708 if (htab->h_gp_got == NULL)
3709 got_base = 0;
3710 else
3711 got_base = htab->h_gp_got->root.u.def.value;
3713 for (rel = relocs; rel < relend; rel++)
3715 reloc_howto_type *howto;
3716 unsigned long r_symndx;
3717 Elf_Internal_Sym *sym;
3718 asection *sec;
3719 struct elf_link_hash_entry *h;
3720 struct elf32_nios2_link_hash_entry *eh;
3721 bfd_vma relocation;
3722 bfd_vma gp;
3723 bfd_reloc_status_type r = bfd_reloc_ok;
3724 const char *name = NULL;
3725 int r_type;
3726 const char *format;
3727 char *msgbuf = NULL;
3728 char *msg = NULL;
3729 bfd_boolean unresolved_reloc;
3730 bfd_vma off;
3731 int use_plt;
3733 r_type = ELF32_R_TYPE (rel->r_info);
3734 r_symndx = ELF32_R_SYM (rel->r_info);
3736 howto = lookup_howto ((unsigned) ELF32_R_TYPE (rel->r_info), output_bfd);
3737 h = NULL;
3738 sym = NULL;
3739 sec = NULL;
3741 if (r_symndx < symtab_hdr->sh_info)
3743 sym = local_syms + r_symndx;
3744 sec = local_sections[r_symndx];
3745 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
3747 else
3749 bfd_boolean warned, ignored;
3751 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
3752 r_symndx, symtab_hdr, sym_hashes,
3753 h, sec, relocation,
3754 unresolved_reloc, warned, ignored);
3757 if (sec && discarded_section (sec))
3758 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
3759 rel, 1, relend, howto, 0, contents);
3761 /* Nothing more to do unless this is a final link. */
3762 if (bfd_link_relocatable (info))
3763 continue;
3765 if (howto)
3767 bfd_boolean resolved_to_zero;
3769 resolved_to_zero = (h != NULL
3770 && UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
3771 switch (howto->type)
3773 case R_NIOS2_HI16:
3774 r = nios2_elf32_do_hi16_relocate (input_bfd, howto,
3775 input_section,
3776 contents, rel->r_offset,
3777 relocation, rel->r_addend);
3778 break;
3779 case R_NIOS2_LO16:
3780 r = nios2_elf32_do_lo16_relocate (input_bfd, howto,
3781 input_section,
3782 contents, rel->r_offset,
3783 relocation, rel->r_addend);
3784 break;
3785 case R_NIOS2_PCREL_LO:
3786 r = nios2_elf32_do_pcrel_lo16_relocate (input_bfd, howto,
3787 input_section,
3788 contents,
3789 rel->r_offset,
3790 relocation,
3791 rel->r_addend);
3792 break;
3793 case R_NIOS2_HIADJ16:
3794 r = nios2_elf32_do_hiadj16_relocate (input_bfd, howto,
3795 input_section, contents,
3796 rel->r_offset, relocation,
3797 rel->r_addend);
3798 break;
3799 case R_NIOS2_PCREL_HA:
3800 r = nios2_elf32_do_pcrel_hiadj16_relocate (input_bfd, howto,
3801 input_section,
3802 contents,
3803 rel->r_offset,
3804 relocation,
3805 rel->r_addend);
3806 break;
3807 case R_NIOS2_PCREL16:
3808 r = nios2_elf32_do_pcrel16_relocate (input_bfd, howto,
3809 input_section, contents,
3810 rel->r_offset, relocation,
3811 rel->r_addend);
3812 break;
3813 case R_NIOS2_GPREL:
3814 /* Turns an absolute address into a gp-relative address. */
3815 if (!nios2_elf_assign_gp (output_bfd, &gp, info))
3817 bfd_vma reloc_address;
3819 if (sec && sec->output_section)
3820 reloc_address = (sec->output_section->vma
3821 + sec->output_offset
3822 + rel->r_offset);
3823 else
3824 reloc_address = 0;
3826 format = _("global pointer relative relocation at address "
3827 "%#" PRIx64 " when _gp not defined\n");
3828 if (asprintf (&msgbuf, format,
3829 (uint64_t) reloc_address) == -1)
3830 msgbuf = NULL;
3831 msg = msgbuf;
3832 r = bfd_reloc_dangerous;
3834 else
3836 bfd_vma symbol_address = rel->r_addend + relocation;
3837 relocation = symbol_address - gp;
3838 rel->r_addend = 0;
3839 if (((signed) relocation < -32768
3840 || (signed) relocation > 32767)
3841 && (!h
3842 || h->root.type == bfd_link_hash_defined
3843 || h->root.type == bfd_link_hash_defweak))
3845 if (h)
3846 name = h->root.root.string;
3847 else
3849 name = (bfd_elf_string_from_elf_section
3850 (input_bfd, symtab_hdr->sh_link,
3851 sym->st_name));
3852 if (name == NULL || *name == '\0')
3853 name = bfd_section_name (sec);
3855 /* xgettext:c-format */
3856 format = _("unable to reach %s (at %#" PRIx64 ") from "
3857 "the global pointer (at %#" PRIx64 ") "
3858 "because the offset (%" PRId64 ") is out of "
3859 "the allowed range, -32678 to 32767\n" );
3860 if (asprintf (&msgbuf, format, name,
3861 (uint64_t) symbol_address, (uint64_t) gp,
3862 (int64_t) relocation) == -1)
3863 msgbuf = NULL;
3864 msg = msgbuf;
3865 r = bfd_reloc_outofrange;
3867 else
3868 r = _bfd_final_link_relocate (howto, input_bfd,
3869 input_section, contents,
3870 rel->r_offset, relocation,
3871 rel->r_addend);
3873 break;
3874 case R_NIOS2_UJMP:
3875 r = nios2_elf32_do_ujmp_relocate (input_bfd, howto,
3876 input_section,
3877 contents, rel->r_offset,
3878 relocation, rel->r_addend);
3879 break;
3880 case R_NIOS2_CJMP:
3881 r = nios2_elf32_do_cjmp_relocate (input_bfd, howto,
3882 input_section,
3883 contents, rel->r_offset,
3884 relocation, rel->r_addend);
3885 break;
3886 case R_NIOS2_CALLR:
3887 r = nios2_elf32_do_callr_relocate (input_bfd, howto,
3888 input_section, contents,
3889 rel->r_offset, relocation,
3890 rel->r_addend);
3891 break;
3892 case R_NIOS2_CALL26:
3893 case R_NIOS2_CALL26_NOAT:
3894 /* If we have a call to an undefined weak symbol, we just want
3895 to stuff a zero in the bits of the call instruction and
3896 bypass the normal call26 relocation handling, because it'll
3897 diagnose an overflow error if address 0 isn't in the same
3898 256MB segment as the call site. Presumably the call
3899 should be guarded by a null check anyway. */
3900 if (h != NULL && h->root.type == bfd_link_hash_undefweak)
3902 BFD_ASSERT (relocation == 0 && rel->r_addend == 0);
3903 r = _bfd_final_link_relocate (howto, input_bfd,
3904 input_section, contents,
3905 rel->r_offset, relocation,
3906 rel->r_addend);
3907 break;
3909 /* Handle relocations which should use the PLT entry.
3910 NIOS2_BFD_RELOC_32 relocations will use the symbol's value,
3911 which may point to a PLT entry, but we don't need to handle
3912 that here. If we created a PLT entry, all branches in this
3913 object should go to it. */
3914 if (h != NULL && splt != NULL && h->plt.offset != (bfd_vma) -1)
3916 /* If we've created a .plt section, and assigned a PLT entry
3917 to this function, it should not be known to bind locally.
3918 If it were, we would have cleared the PLT entry. */
3919 BFD_ASSERT (!SYMBOL_CALLS_LOCAL (info, h));
3921 relocation = (splt->output_section->vma
3922 + splt->output_offset
3923 + h->plt.offset);
3925 unresolved_reloc = FALSE;
3927 /* Detect R_NIOS2_CALL26 relocations that would overflow the
3928 256MB segment. Replace the target with a reference to a
3929 trampoline instead.
3930 Note that htab->stub_group is null if relaxation has been
3931 disabled by the --no-relax linker command-line option, so
3932 we can use that to skip this processing entirely. */
3933 if (howto->type == R_NIOS2_CALL26 && htab->stub_group)
3935 bfd_vma dest = relocation + rel->r_addend;
3936 enum elf32_nios2_stub_type stub_type;
3938 eh = (struct elf32_nios2_link_hash_entry *)h;
3939 stub_type = nios2_type_of_stub (input_section, rel, eh,
3940 htab, dest, NULL);
3942 if (stub_type != nios2_stub_none)
3944 struct elf32_nios2_stub_hash_entry *hsh;
3946 hsh = nios2_get_stub_entry (input_section, sec,
3947 eh, rel, htab, stub_type);
3948 if (hsh == NULL)
3950 r = bfd_reloc_undefined;
3951 break;
3954 dest = (hsh->stub_offset
3955 + hsh->stub_sec->output_offset
3956 + hsh->stub_sec->output_section->vma);
3957 r = nios2_elf32_do_call26_relocate (input_bfd, howto,
3958 input_section,
3959 contents,
3960 rel->r_offset,
3961 dest, 0);
3962 break;
3966 /* Normal case. */
3967 r = nios2_elf32_do_call26_relocate (input_bfd, howto,
3968 input_section, contents,
3969 rel->r_offset, relocation,
3970 rel->r_addend);
3971 break;
3972 case R_NIOS2_ALIGN:
3973 r = bfd_reloc_ok;
3974 /* For symmetry this would be
3975 r = nios2_elf32_do_ignore_reloc (input_bfd, howto,
3976 input_section, contents,
3977 rel->r_offset, relocation,
3978 rel->r_addend);
3979 but do_ignore_reloc would do no more than return
3980 bfd_reloc_ok. */
3981 break;
3983 case R_NIOS2_GOT16:
3984 case R_NIOS2_CALL16:
3985 case R_NIOS2_GOT_LO:
3986 case R_NIOS2_GOT_HA:
3987 case R_NIOS2_CALL_LO:
3988 case R_NIOS2_CALL_HA:
3989 /* Relocation is to the entry for this symbol in the
3990 global offset table. */
3991 if (sgot == NULL)
3993 r = bfd_reloc_notsupported;
3994 break;
3997 use_plt = 0;
3999 if (h != NULL)
4001 bfd_boolean dyn;
4003 eh = (struct elf32_nios2_link_hash_entry *)h;
4004 use_plt = (eh->got_types_used == CALL_USED
4005 && h->plt.offset != (bfd_vma) -1);
4007 off = h->got.offset;
4008 BFD_ASSERT (off != (bfd_vma) -1);
4009 dyn = htab->root.dynamic_sections_created;
4010 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
4011 bfd_link_pic (info),
4013 || (bfd_link_pic (info)
4014 && SYMBOL_REFERENCES_LOCAL (info, h))
4015 || ((ELF_ST_VISIBILITY (h->other)
4016 || resolved_to_zero)
4017 && h->root.type == bfd_link_hash_undefweak))
4019 /* This is actually a static link, or it is a -Bsymbolic
4020 link and the symbol is defined locally. We must
4021 initialize this entry in the global offset table.
4022 Since the offset must always be a multiple of 4, we
4023 use the least significant bit to record whether we
4024 have initialized it already.
4026 When doing a dynamic link, we create a .rela.got
4027 relocation entry to initialize the value. This is
4028 done in the finish_dynamic_symbol routine. */
4029 if ((off & 1) != 0)
4030 off &= ~1;
4031 else
4033 bfd_put_32 (output_bfd, relocation,
4034 sgot->contents + off);
4035 h->got.offset |= 1;
4038 else
4039 unresolved_reloc = FALSE;
4041 else
4043 BFD_ASSERT (local_got_offsets != NULL
4044 && local_got_offsets[r_symndx] != (bfd_vma) -1);
4046 off = local_got_offsets[r_symndx];
4048 /* The offset must always be a multiple of 4. We use the
4049 least significant bit to record whether we have already
4050 generated the necessary reloc. */
4051 if ((off & 1) != 0)
4052 off &= ~1;
4053 else
4055 bfd_put_32 (output_bfd, relocation,
4056 sgot->contents + off);
4058 if (bfd_link_pic (info))
4060 asection *srelgot;
4061 Elf_Internal_Rela outrel;
4062 bfd_byte *loc;
4064 srelgot = htab->root.srelgot;
4065 BFD_ASSERT (srelgot != NULL);
4067 outrel.r_addend = relocation;
4068 outrel.r_offset = (sgot->output_section->vma
4069 + sgot->output_offset
4070 + off);
4071 outrel.r_info = ELF32_R_INFO (0, R_NIOS2_RELATIVE);
4072 loc = srelgot->contents;
4073 loc += (srelgot->reloc_count++ *
4074 sizeof (Elf32_External_Rela));
4075 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4078 local_got_offsets[r_symndx] |= 1;
4082 if (use_plt && bfd_link_pic (info))
4084 off = ((h->plt.offset - 24) / 12 + 3) * 4;
4085 relocation = (htab->root.sgotplt->output_offset + off
4086 - got_base);
4088 else
4089 relocation = sgot->output_offset + off - got_base;
4091 /* This relocation does not use the addend. */
4092 rel->r_addend = 0;
4094 switch (howto->type)
4096 case R_NIOS2_GOT_LO:
4097 case R_NIOS2_CALL_LO:
4098 r = nios2_elf32_do_lo16_relocate (input_bfd, howto,
4099 input_section, contents,
4100 rel->r_offset, relocation,
4101 rel->r_addend);
4102 break;
4103 case R_NIOS2_GOT_HA:
4104 case R_NIOS2_CALL_HA:
4105 r = nios2_elf32_do_hiadj16_relocate (input_bfd, howto,
4106 input_section, contents,
4107 rel->r_offset,
4108 relocation,
4109 rel->r_addend);
4110 break;
4111 default:
4112 r = _bfd_final_link_relocate (howto, input_bfd,
4113 input_section, contents,
4114 rel->r_offset, relocation,
4115 rel->r_addend);
4116 break;
4118 break;
4120 case R_NIOS2_GOTOFF_LO:
4121 case R_NIOS2_GOTOFF_HA:
4122 case R_NIOS2_GOTOFF:
4123 /* Relocation is relative to the global offset table pointer. */
4125 BFD_ASSERT (sgot != NULL);
4126 if (sgot == NULL)
4128 r = bfd_reloc_notsupported;
4129 break;
4132 /* Note that sgot->output_offset is not involved in this
4133 calculation. We always want the start of .got. */
4134 relocation -= sgot->output_section->vma;
4136 /* Now we adjust the relocation to be relative to the GOT pointer
4137 (the _gp_got symbol), which possibly contains the 0x8000 bias. */
4138 relocation -= got_base;
4140 switch (howto->type)
4142 case R_NIOS2_GOTOFF_LO:
4143 r = nios2_elf32_do_lo16_relocate (input_bfd, howto,
4144 input_section, contents,
4145 rel->r_offset, relocation,
4146 rel->r_addend);
4147 break;
4148 case R_NIOS2_GOTOFF_HA:
4149 r = nios2_elf32_do_hiadj16_relocate (input_bfd, howto,
4150 input_section, contents,
4151 rel->r_offset,
4152 relocation,
4153 rel->r_addend);
4154 break;
4155 default:
4156 r = _bfd_final_link_relocate (howto, input_bfd,
4157 input_section, contents,
4158 rel->r_offset, relocation,
4159 rel->r_addend);
4160 break;
4162 break;
4164 case R_NIOS2_TLS_LDO16:
4165 relocation -= dtpoff_base (info) + DTP_OFFSET;
4167 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
4168 contents, rel->r_offset,
4169 relocation, rel->r_addend);
4170 break;
4171 case R_NIOS2_TLS_LDM16:
4172 if (htab->root.sgot == NULL)
4173 abort ();
4175 off = htab->tls_ldm_got.offset;
4177 if ((off & 1) != 0)
4178 off &= ~1;
4179 else
4181 /* If we don't know the module number, create a relocation
4182 for it. */
4183 if (bfd_link_pic (info))
4185 Elf_Internal_Rela outrel;
4186 bfd_byte *loc;
4188 if (htab->root.srelgot == NULL)
4189 abort ();
4191 outrel.r_addend = 0;
4192 outrel.r_offset = (htab->root.sgot->output_section->vma
4193 + htab->root.sgot->output_offset
4194 + off);
4195 outrel.r_info = ELF32_R_INFO (0, R_NIOS2_TLS_DTPMOD);
4197 loc = htab->root.srelgot->contents;
4198 loc += (htab->root.srelgot->reloc_count++
4199 * sizeof (Elf32_External_Rela));
4200 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4202 else
4203 bfd_put_32 (output_bfd, 1,
4204 htab->root.sgot->contents + off);
4206 htab->tls_ldm_got.offset |= 1;
4209 relocation = htab->root.sgot->output_offset + off - got_base;
4211 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
4212 contents, rel->r_offset,
4213 relocation, rel->r_addend);
4215 break;
4216 case R_NIOS2_TLS_GD16:
4217 case R_NIOS2_TLS_IE16:
4219 int indx;
4220 char tls_type;
4222 if (htab->root.sgot == NULL)
4223 abort ();
4225 indx = 0;
4226 if (h != NULL)
4228 bfd_boolean dyn;
4229 dyn = htab->root.dynamic_sections_created;
4230 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
4231 bfd_link_pic (info),
4233 && (!bfd_link_pic (info)
4234 || !SYMBOL_REFERENCES_LOCAL (info, h)))
4236 unresolved_reloc = FALSE;
4237 indx = h->dynindx;
4239 off = h->got.offset;
4240 tls_type = (((struct elf32_nios2_link_hash_entry *) h)
4241 ->tls_type);
4243 else
4245 if (local_got_offsets == NULL)
4246 abort ();
4247 off = local_got_offsets[r_symndx];
4248 tls_type = (elf32_nios2_local_got_tls_type (input_bfd)
4249 [r_symndx]);
4252 if (tls_type == GOT_UNKNOWN)
4253 abort ();
4255 if ((off & 1) != 0)
4256 off &= ~1;
4257 else
4259 bfd_boolean need_relocs = FALSE;
4260 Elf_Internal_Rela outrel;
4261 bfd_byte *loc = NULL;
4262 int cur_off = off;
4264 /* The GOT entries have not been initialized yet. Do it
4265 now, and emit any relocations. If both an IE GOT and a
4266 GD GOT are necessary, we emit the GD first. */
4268 if ((bfd_link_pic (info) || indx != 0)
4269 && (h == NULL
4270 || (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
4271 && !resolved_to_zero)
4272 || h->root.type != bfd_link_hash_undefweak))
4274 need_relocs = TRUE;
4275 if (htab->root.srelgot == NULL)
4276 abort ();
4277 loc = htab->root.srelgot->contents;
4278 loc += (htab->root.srelgot->reloc_count *
4279 sizeof (Elf32_External_Rela));
4282 if (tls_type & GOT_TLS_GD)
4284 if (need_relocs)
4286 outrel.r_addend = 0;
4287 outrel.r_offset = (htab->root.sgot->output_section->vma
4288 + htab->root.sgot->output_offset
4289 + cur_off);
4290 outrel.r_info = ELF32_R_INFO (indx,
4291 R_NIOS2_TLS_DTPMOD);
4293 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
4294 loc);
4295 htab->root.srelgot->reloc_count++;
4296 loc += sizeof (Elf32_External_Rela);
4298 if (indx == 0)
4299 bfd_put_32 (output_bfd,
4300 (relocation - dtpoff_base (info) -
4301 DTP_OFFSET),
4302 htab->root.sgot->contents + cur_off + 4);
4303 else
4305 outrel.r_addend = 0;
4306 outrel.r_info = ELF32_R_INFO (indx,
4307 R_NIOS2_TLS_DTPREL);
4308 outrel.r_offset += 4;
4310 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
4311 loc);
4312 htab->root.srelgot->reloc_count++;
4313 loc += sizeof (Elf32_External_Rela);
4316 else
4318 /* If we are not emitting relocations for a
4319 general dynamic reference, then we must be in a
4320 static link or an executable link with the
4321 symbol binding locally. Mark it as belonging
4322 to module 1, the executable. */
4323 bfd_put_32 (output_bfd, 1,
4324 htab->root.sgot->contents + cur_off);
4325 bfd_put_32 (output_bfd, (relocation -
4326 dtpoff_base (info) -
4327 DTP_OFFSET),
4328 htab->root.sgot->contents + cur_off + 4);
4331 cur_off += 8;
4334 if (tls_type & GOT_TLS_IE)
4336 if (need_relocs)
4338 if (indx == 0)
4339 outrel.r_addend = (relocation -
4340 dtpoff_base (info));
4341 else
4342 outrel.r_addend = 0;
4343 outrel.r_offset = (htab->root.sgot->output_section->vma
4344 + htab->root.sgot->output_offset
4345 + cur_off);
4346 outrel.r_info = ELF32_R_INFO (indx,
4347 R_NIOS2_TLS_TPREL);
4349 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
4350 loc);
4351 htab->root.srelgot->reloc_count++;
4352 loc += sizeof (Elf32_External_Rela);
4354 else
4355 bfd_put_32 (output_bfd, (tpoff (info, relocation)
4356 - TP_OFFSET),
4357 htab->root.sgot->contents + cur_off);
4358 cur_off += 4;
4361 if (h != NULL)
4362 h->got.offset |= 1;
4363 else
4364 local_got_offsets[r_symndx] |= 1;
4367 if ((tls_type & GOT_TLS_GD) && r_type != R_NIOS2_TLS_GD16)
4368 off += 8;
4369 relocation = htab->root.sgot->output_offset + off - got_base;
4371 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
4372 contents, rel->r_offset,
4373 relocation, rel->r_addend);
4376 break;
4377 case R_NIOS2_TLS_LE16:
4378 if (bfd_link_dll (info))
4380 _bfd_error_handler
4381 /* xgettext:c-format */
4382 (_("%pB(%pA+%#" PRIx64 "): %s relocation not "
4383 "permitted in shared object"),
4384 input_bfd, input_section,
4385 (uint64_t) rel->r_offset, howto->name);
4386 return FALSE;
4388 else
4389 relocation = tpoff (info, relocation) - TP_OFFSET;
4391 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
4392 contents, rel->r_offset,
4393 relocation, rel->r_addend);
4394 break;
4396 case R_NIOS2_BFD_RELOC_32:
4397 if (bfd_link_pic (info)
4398 && (input_section->flags & SEC_ALLOC) != 0
4399 && (h == NULL
4400 || (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
4401 && !resolved_to_zero)
4402 || h->root.type != bfd_link_hash_undefweak))
4404 Elf_Internal_Rela outrel;
4405 bfd_byte *loc;
4406 bfd_boolean skip, relocate;
4408 /* When generating a shared object, these relocations
4409 are copied into the output file to be resolved at run
4410 time. */
4412 skip = FALSE;
4413 relocate = FALSE;
4415 outrel.r_offset
4416 = _bfd_elf_section_offset (output_bfd, info,
4417 input_section, rel->r_offset);
4418 if (outrel.r_offset == (bfd_vma) -1)
4419 skip = TRUE;
4420 else if (outrel.r_offset == (bfd_vma) -2)
4421 skip = TRUE, relocate = TRUE;
4422 outrel.r_offset += (input_section->output_section->vma
4423 + input_section->output_offset);
4425 if (skip)
4426 memset (&outrel, 0, sizeof outrel);
4427 else if (h != NULL
4428 && h->dynindx != -1
4429 && (!bfd_link_pic (info)
4430 || !SYMBOLIC_BIND (info, h)
4431 || !h->def_regular))
4433 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
4434 outrel.r_addend = rel->r_addend;
4436 else
4438 /* This symbol is local, or marked to become local. */
4439 outrel.r_addend = relocation + rel->r_addend;
4440 relocate = TRUE;
4441 outrel.r_info = ELF32_R_INFO (0, R_NIOS2_RELATIVE);
4444 sreloc = elf_section_data (input_section)->sreloc;
4445 if (sreloc == NULL)
4446 abort ();
4448 loc = sreloc->contents;
4449 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
4450 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4452 /* This reloc will be computed at runtime, so there's no
4453 need to do anything now, except for R_NIOS2_BFD_RELOC_32
4454 relocations that have been turned into
4455 R_NIOS2_RELATIVE. */
4456 if (!relocate)
4457 break;
4460 r = _bfd_final_link_relocate (howto, input_bfd,
4461 input_section, contents,
4462 rel->r_offset, relocation,
4463 rel->r_addend);
4464 break;
4466 case R_NIOS2_TLS_DTPREL:
4467 relocation -= dtpoff_base (info);
4468 /* Fall through. */
4470 default:
4471 r = _bfd_final_link_relocate (howto, input_bfd,
4472 input_section, contents,
4473 rel->r_offset, relocation,
4474 rel->r_addend);
4475 break;
4478 else
4479 r = bfd_reloc_notsupported;
4481 if (r != bfd_reloc_ok)
4483 if (h != NULL)
4484 name = h->root.root.string;
4485 else
4487 name = bfd_elf_string_from_elf_section (input_bfd,
4488 symtab_hdr->sh_link,
4489 sym->st_name);
4490 if (name == NULL || *name == '\0')
4491 name = bfd_section_name (sec);
4494 switch (r)
4496 case bfd_reloc_overflow:
4497 (*info->callbacks->reloc_overflow) (info, NULL, name,
4498 howto->name, (bfd_vma) 0,
4499 input_bfd, input_section,
4500 rel->r_offset);
4501 break;
4503 case bfd_reloc_undefined:
4504 (*info->callbacks->undefined_symbol) (info, name, input_bfd,
4505 input_section,
4506 rel->r_offset, TRUE);
4507 break;
4509 case bfd_reloc_outofrange:
4510 if (msg == NULL)
4511 msg = _("relocation out of range");
4512 break;
4514 case bfd_reloc_notsupported:
4515 if (msg == NULL)
4516 msg = _("unsupported relocation");
4517 break;
4519 case bfd_reloc_dangerous:
4520 if (msg == NULL)
4521 msg = _("dangerous relocation");
4522 break;
4524 default:
4525 if (msg == NULL)
4526 msg = _("unknown error");
4527 break;
4530 if (msg)
4532 (*info->callbacks->warning) (info, msg, name, input_bfd,
4533 input_section, rel->r_offset);
4534 free (msgbuf);
4535 return FALSE;
4539 return TRUE;
4542 /* Implement elf-backend_section_flags:
4543 Convert NIOS2 specific section flags to bfd internal section flags. */
4544 static bfd_boolean
4545 nios2_elf32_section_flags (const Elf_Internal_Shdr *hdr)
4547 if (hdr->sh_flags & SHF_NIOS2_GPREL)
4548 hdr->bfd_section->flags |= SEC_SMALL_DATA;
4550 return TRUE;
4553 /* Implement elf_backend_fake_sections:
4554 Set the correct type for an NIOS2 ELF section. We do this by the
4555 section name, which is a hack, but ought to work. */
4556 static bfd_boolean
4557 nios2_elf32_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
4558 Elf_Internal_Shdr *hdr, asection *sec)
4560 const char *name = bfd_section_name (sec);
4562 if ((sec->flags & SEC_SMALL_DATA)
4563 || strcmp (name, ".sdata") == 0
4564 || strcmp (name, ".sbss") == 0
4565 || strcmp (name, ".lit4") == 0 || strcmp (name, ".lit8") == 0)
4566 hdr->sh_flags |= SHF_NIOS2_GPREL;
4568 return TRUE;
4571 /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
4572 shortcuts to them in our hash table. */
4573 static bfd_boolean
4574 create_got_section (bfd *dynobj, struct bfd_link_info *info)
4576 struct elf32_nios2_link_hash_table *htab;
4577 struct elf_link_hash_entry *h;
4579 htab = elf32_nios2_hash_table (info);
4581 if (! _bfd_elf_create_got_section (dynobj, info))
4582 return FALSE;
4584 /* In order for the two loads in .PLTresolve to share the same %hiadj,
4585 _GLOBAL_OFFSET_TABLE_ must be aligned to a 16-byte boundary. */
4586 if (!bfd_set_section_alignment (htab->root.sgotplt, 4))
4587 return FALSE;
4589 /* The Nios II ABI specifies that GOT-relative relocations are relative
4590 to the linker-created symbol _gp_got, rather than using
4591 _GLOBAL_OFFSET_TABLE_ directly. In particular, the latter always
4592 points to the base of the GOT while _gp_got may include a bias. */
4593 h = _bfd_elf_define_linkage_sym (dynobj, info, htab->root.sgotplt,
4594 "_gp_got");
4595 htab->h_gp_got = h;
4596 if (h == NULL)
4597 return FALSE;
4599 return TRUE;
4602 /* Implement elf_backend_create_dynamic_sections:
4603 Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
4604 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
4605 hash table. */
4606 static bfd_boolean
4607 nios2_elf32_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
4609 struct elf32_nios2_link_hash_table *htab;
4611 htab = elf32_nios2_hash_table (info);
4612 if (!htab->root.sgot && !create_got_section (dynobj, info))
4613 return FALSE;
4615 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
4616 return FALSE;
4618 /* In order for the two loads in a shared object .PLTresolve to share the
4619 same %hiadj, the start of the PLT (as well as the GOT) must be aligned
4620 to a 16-byte boundary. This is because the addresses for these loads
4621 include the -(.plt+4) PIC correction. */
4622 return bfd_set_section_alignment (htab->root.splt, 4);
4625 /* Implement elf_backend_copy_indirect_symbol:
4626 Copy the extra info we tack onto an elf_link_hash_entry. */
4627 static void
4628 nios2_elf32_copy_indirect_symbol (struct bfd_link_info *info,
4629 struct elf_link_hash_entry *dir,
4630 struct elf_link_hash_entry *ind)
4632 struct elf32_nios2_link_hash_entry *edir, *eind;
4634 edir = (struct elf32_nios2_link_hash_entry *) dir;
4635 eind = (struct elf32_nios2_link_hash_entry *) ind;
4637 if (ind->root.type == bfd_link_hash_indirect
4638 && dir->got.refcount <= 0)
4640 edir->tls_type = eind->tls_type;
4641 eind->tls_type = GOT_UNKNOWN;
4644 edir->got_types_used |= eind->got_types_used;
4646 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
4649 /* Set the right machine number for a NIOS2 ELF file. */
4651 static bfd_boolean
4652 nios2_elf32_object_p (bfd *abfd)
4654 unsigned long mach;
4656 mach = elf_elfheader (abfd)->e_flags;
4658 switch (mach)
4660 default:
4661 case EF_NIOS2_ARCH_R1:
4662 bfd_default_set_arch_mach (abfd, bfd_arch_nios2, bfd_mach_nios2r1);
4663 break;
4664 case EF_NIOS2_ARCH_R2:
4665 bfd_default_set_arch_mach (abfd, bfd_arch_nios2, bfd_mach_nios2r2);
4666 break;
4669 return TRUE;
4672 /* Implement elf_backend_check_relocs:
4673 Look through the relocs for a section during the first phase. */
4674 static bfd_boolean
4675 nios2_elf32_check_relocs (bfd *abfd, struct bfd_link_info *info,
4676 asection *sec, const Elf_Internal_Rela *relocs)
4678 Elf_Internal_Shdr *symtab_hdr;
4679 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
4680 const Elf_Internal_Rela *rel;
4681 const Elf_Internal_Rela *rel_end;
4682 struct elf32_nios2_link_hash_table *htab;
4683 asection *sreloc = NULL;
4684 bfd_signed_vma *local_got_refcounts;
4686 if (bfd_link_relocatable (info))
4687 return TRUE;
4689 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4690 sym_hashes = elf_sym_hashes (abfd);
4691 sym_hashes_end = (sym_hashes
4692 + symtab_hdr->sh_size / sizeof (Elf32_External_Sym));
4693 if (!elf_bad_symtab (abfd))
4694 sym_hashes_end -= symtab_hdr->sh_info;
4695 local_got_refcounts = elf_local_got_refcounts (abfd);
4697 htab = elf32_nios2_hash_table (info);
4699 rel_end = relocs + sec->reloc_count;
4700 for (rel = relocs; rel < rel_end; rel++)
4702 unsigned int r_type;
4703 struct elf_link_hash_entry *h;
4704 unsigned long r_symndx;
4706 r_symndx = ELF32_R_SYM (rel->r_info);
4707 if (r_symndx < symtab_hdr->sh_info)
4708 h = NULL;
4709 else
4711 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4712 while (h->root.type == bfd_link_hash_indirect
4713 || h->root.type == bfd_link_hash_warning)
4714 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4717 r_type = ELF32_R_TYPE (rel->r_info);
4719 switch (r_type)
4721 case R_NIOS2_GOT16:
4722 case R_NIOS2_GOT_LO:
4723 case R_NIOS2_GOT_HA:
4724 case R_NIOS2_CALL16:
4725 case R_NIOS2_CALL_LO:
4726 case R_NIOS2_CALL_HA:
4727 case R_NIOS2_TLS_GD16:
4728 case R_NIOS2_TLS_IE16:
4729 /* This symbol requires a global offset table entry. */
4731 int tls_type, old_tls_type;
4733 switch (r_type)
4735 default:
4736 case R_NIOS2_GOT16:
4737 case R_NIOS2_GOT_LO:
4738 case R_NIOS2_GOT_HA:
4739 case R_NIOS2_CALL16:
4740 case R_NIOS2_CALL_LO:
4741 case R_NIOS2_CALL_HA:
4742 tls_type = GOT_NORMAL;
4743 break;
4744 case R_NIOS2_TLS_GD16:
4745 tls_type = GOT_TLS_GD;
4746 break;
4747 case R_NIOS2_TLS_IE16:
4748 tls_type = GOT_TLS_IE;
4749 break;
4752 if (h != NULL)
4754 struct elf32_nios2_link_hash_entry *eh
4755 = (struct elf32_nios2_link_hash_entry *)h;
4756 h->got.refcount++;
4757 old_tls_type = elf32_nios2_hash_entry(h)->tls_type;
4758 if (r_type == R_NIOS2_CALL16
4759 || r_type == R_NIOS2_CALL_LO
4760 || r_type == R_NIOS2_CALL_HA)
4762 /* Make sure a plt entry is created for this symbol if
4763 it turns out to be a function defined by a dynamic
4764 object. */
4765 h->plt.refcount++;
4766 h->needs_plt = 1;
4767 h->type = STT_FUNC;
4768 eh->got_types_used |= CALL_USED;
4770 else
4771 eh->got_types_used |= GOT_USED;
4773 else
4775 /* This is a global offset table entry for a local symbol. */
4776 if (local_got_refcounts == NULL)
4778 bfd_size_type size;
4780 size = symtab_hdr->sh_info;
4781 size *= (sizeof (bfd_signed_vma) + sizeof (char));
4782 local_got_refcounts
4783 = ((bfd_signed_vma *) bfd_zalloc (abfd, size));
4784 if (local_got_refcounts == NULL)
4785 return FALSE;
4786 elf_local_got_refcounts (abfd) = local_got_refcounts;
4787 elf32_nios2_local_got_tls_type (abfd)
4788 = (char *) (local_got_refcounts + symtab_hdr->sh_info);
4790 local_got_refcounts[r_symndx]++;
4791 old_tls_type = elf32_nios2_local_got_tls_type (abfd) [r_symndx];
4794 /* We will already have issued an error message if there is a
4795 TLS / non-TLS mismatch, based on the symbol type. We don't
4796 support any linker relaxations. So just combine any TLS
4797 types needed. */
4798 if (old_tls_type != GOT_UNKNOWN && old_tls_type != GOT_NORMAL
4799 && tls_type != GOT_NORMAL)
4800 tls_type |= old_tls_type;
4802 if (old_tls_type != tls_type)
4804 if (h != NULL)
4805 elf32_nios2_hash_entry (h)->tls_type = tls_type;
4806 else
4807 elf32_nios2_local_got_tls_type (abfd) [r_symndx] = tls_type;
4810 make_got:
4811 if (htab->root.sgot == NULL)
4813 if (htab->root.dynobj == NULL)
4814 htab->root.dynobj = abfd;
4815 if (!create_got_section (htab->root.dynobj, info))
4816 return FALSE;
4818 break;
4820 case R_NIOS2_TLS_LDM16:
4821 htab->tls_ldm_got.refcount++;
4822 goto make_got;
4824 /* This relocation describes the C++ object vtable hierarchy.
4825 Reconstruct it for later use during GC. */
4826 case R_NIOS2_GNU_VTINHERIT:
4827 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
4828 return FALSE;
4829 break;
4831 /* This relocation describes which C++ vtable entries are actually
4832 used. Record for later use during GC. */
4833 case R_NIOS2_GNU_VTENTRY:
4834 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
4835 return FALSE;
4836 break;
4838 case R_NIOS2_BFD_RELOC_32:
4839 case R_NIOS2_CALL26:
4840 case R_NIOS2_CALL26_NOAT:
4841 case R_NIOS2_HIADJ16:
4842 case R_NIOS2_LO16:
4844 if (h != NULL)
4846 /* If this reloc is in a read-only section, we might
4847 need a copy reloc. We can't check reliably at this
4848 stage whether the section is read-only, as input
4849 sections have not yet been mapped to output sections.
4850 Tentatively set the flag for now, and correct in
4851 adjust_dynamic_symbol. */
4852 if (!bfd_link_pic (info))
4853 h->non_got_ref = 1;
4855 /* Make sure a plt entry is created for this symbol if it
4856 turns out to be a function defined by a dynamic object. */
4857 h->plt.refcount++;
4859 if (r_type == R_NIOS2_CALL26 || r_type == R_NIOS2_CALL26_NOAT)
4860 h->needs_plt = 1;
4863 /* If we are creating a shared library, we need to copy the
4864 reloc into the shared library. */
4865 if (bfd_link_pic (info)
4866 && (sec->flags & SEC_ALLOC) != 0
4867 && (r_type == R_NIOS2_BFD_RELOC_32
4868 || (h != NULL && ! h->needs_plt
4869 && (! SYMBOLIC_BIND (info, h) || ! h->def_regular))))
4871 struct elf_dyn_relocs *p;
4872 struct elf_dyn_relocs **head;
4874 /* When creating a shared object, we must copy these
4875 reloc types into the output file. We create a reloc
4876 section in dynobj and make room for this reloc. */
4877 if (sreloc == NULL)
4879 if (htab->root.dynobj == NULL)
4880 htab->root.dynobj = abfd;
4882 sreloc = _bfd_elf_make_dynamic_reloc_section
4883 (sec, htab->root.dynobj, 2, abfd, TRUE);
4884 if (sreloc == NULL)
4885 return FALSE;
4888 /* If this is a global symbol, we count the number of
4889 relocations we need for this symbol. */
4890 if (h != NULL)
4891 head = &h->dyn_relocs;
4892 else
4894 /* Track dynamic relocs needed for local syms too.
4895 We really need local syms available to do this
4896 easily. Oh well. */
4898 asection *s;
4899 void *vpp;
4900 Elf_Internal_Sym *isym;
4902 isym = bfd_sym_from_r_symndx (&htab->root.sym_cache,
4903 abfd, r_symndx);
4904 if (isym == NULL)
4905 return FALSE;
4907 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
4908 if (s == NULL)
4909 s = sec;
4911 vpp = &elf_section_data (s)->local_dynrel;
4912 head = (struct elf_dyn_relocs **) vpp;
4915 p = *head;
4916 if (p == NULL || p->sec != sec)
4918 size_t amt = sizeof *p;
4919 p = ((struct elf_dyn_relocs *)
4920 bfd_alloc (htab->root.dynobj, amt));
4921 if (p == NULL)
4922 return FALSE;
4923 p->next = *head;
4924 *head = p;
4925 p->sec = sec;
4926 p->count = 0;
4927 p->pc_count = 0;
4930 p->count += 1;
4933 break;
4937 return TRUE;
4941 /* Implement elf_backend_gc_mark_hook:
4942 Return the section that should be marked against GC for a given
4943 relocation. */
4944 static asection *
4945 nios2_elf32_gc_mark_hook (asection *sec,
4946 struct bfd_link_info *info,
4947 Elf_Internal_Rela *rel,
4948 struct elf_link_hash_entry *h,
4949 Elf_Internal_Sym *sym)
4951 if (h != NULL)
4952 switch (ELF32_R_TYPE (rel->r_info))
4954 case R_NIOS2_GNU_VTINHERIT:
4955 case R_NIOS2_GNU_VTENTRY:
4956 return NULL;
4958 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
4961 /* Implement elf_backend_finish_dynamic_symbols:
4962 Finish up dynamic symbol handling. We set the contents of various
4963 dynamic sections here. */
4964 static bfd_boolean
4965 nios2_elf32_finish_dynamic_symbol (bfd *output_bfd,
4966 struct bfd_link_info *info,
4967 struct elf_link_hash_entry *h,
4968 Elf_Internal_Sym *sym)
4970 struct elf32_nios2_link_hash_table *htab;
4971 struct elf32_nios2_link_hash_entry *eh
4972 = (struct elf32_nios2_link_hash_entry *)h;
4973 int use_plt;
4975 htab = elf32_nios2_hash_table (info);
4977 if (h->plt.offset != (bfd_vma) -1)
4979 asection *splt;
4980 asection *sgotplt;
4981 asection *srela;
4982 bfd_vma plt_index;
4983 bfd_vma got_offset;
4984 Elf_Internal_Rela rela;
4985 bfd_byte *loc;
4986 bfd_vma got_address;
4988 /* This symbol has an entry in the procedure linkage table. Set
4989 it up. */
4990 BFD_ASSERT (h->dynindx != -1);
4991 splt = htab->root.splt;
4992 sgotplt = htab->root.sgotplt;
4993 srela = htab->root.srelplt;
4994 BFD_ASSERT (splt != NULL && sgotplt != NULL && srela != NULL);
4996 /* Emit the PLT entry. */
4997 if (bfd_link_pic (info))
4999 nios2_elf32_install_data (splt, nios2_so_plt_entry, h->plt.offset,
5001 plt_index = (h->plt.offset - 24) / 12;
5002 got_offset = (plt_index + 3) * 4;
5003 nios2_elf32_install_imm16 (splt, h->plt.offset,
5004 hiadj(plt_index * 4));
5005 nios2_elf32_install_imm16 (splt, h->plt.offset + 4,
5006 (plt_index * 4) & 0xffff);
5007 nios2_elf32_install_imm16 (splt, h->plt.offset + 8,
5008 0xfff4 - h->plt.offset);
5009 got_address = (sgotplt->output_section->vma + sgotplt->output_offset
5010 + got_offset);
5012 /* Fill in the entry in the global offset table. There are no
5013 res_n slots for a shared object PLT, instead the .got.plt entries
5014 point to the PLT entries. */
5015 bfd_put_32 (output_bfd,
5016 splt->output_section->vma + splt->output_offset
5017 + h->plt.offset, sgotplt->contents + got_offset);
5019 else
5021 plt_index = (h->plt.offset - 28 - htab->res_n_size) / 12;
5022 got_offset = (plt_index + 3) * 4;
5024 nios2_elf32_install_data (splt, nios2_plt_entry, h->plt.offset, 3);
5025 got_address = (sgotplt->output_section->vma + sgotplt->output_offset
5026 + got_offset);
5027 nios2_elf32_install_imm16 (splt, h->plt.offset, hiadj(got_address));
5028 nios2_elf32_install_imm16 (splt, h->plt.offset + 4,
5029 got_address & 0xffff);
5031 /* Fill in the entry in the global offset table. */
5032 bfd_put_32 (output_bfd,
5033 splt->output_section->vma + splt->output_offset
5034 + plt_index * 4, sgotplt->contents + got_offset);
5037 /* Fill in the entry in the .rela.plt section. */
5038 rela.r_offset = got_address;
5039 rela.r_info = ELF32_R_INFO (h->dynindx, R_NIOS2_JUMP_SLOT);
5040 rela.r_addend = 0;
5041 loc = srela->contents + plt_index * sizeof (Elf32_External_Rela);
5042 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
5044 if (!h->def_regular)
5046 /* Mark the symbol as undefined, rather than as defined in
5047 the .plt section. Leave the value alone. */
5048 sym->st_shndx = SHN_UNDEF;
5049 /* If the symbol is weak, we do need to clear the value.
5050 Otherwise, the PLT entry would provide a definition for
5051 the symbol even if the symbol wasn't defined anywhere,
5052 and so the symbol would never be NULL. */
5053 if (!h->ref_regular_nonweak)
5054 sym->st_value = 0;
5058 use_plt = (eh->got_types_used == CALL_USED
5059 && h->plt.offset != (bfd_vma) -1);
5061 if (!use_plt && h->got.offset != (bfd_vma) -1
5062 && (elf32_nios2_hash_entry (h)->tls_type & GOT_TLS_GD) == 0
5063 && (elf32_nios2_hash_entry (h)->tls_type & GOT_TLS_IE) == 0)
5065 asection *sgot;
5066 asection *srela;
5067 Elf_Internal_Rela rela;
5068 bfd_byte *loc;
5069 bfd_vma offset;
5071 /* This symbol has an entry in the global offset table. Set it
5072 up. */
5073 sgot = htab->root.sgot;
5074 srela = htab->root.srelgot;
5075 BFD_ASSERT (sgot != NULL && srela != NULL);
5077 offset = (h->got.offset & ~(bfd_vma) 1);
5078 rela.r_offset = (sgot->output_section->vma
5079 + sgot->output_offset + offset);
5081 /* If this is a -Bsymbolic link, and the symbol is defined
5082 locally, we just want to emit a RELATIVE reloc. Likewise if
5083 the symbol was forced to be local because of a version file.
5084 The entry in the global offset table will already have been
5085 initialized in the relocate_section function. */
5087 if (bfd_link_pic (info) && SYMBOL_REFERENCES_LOCAL (info, h))
5089 rela.r_info = ELF32_R_INFO (0, R_NIOS2_RELATIVE);
5090 rela.r_addend = bfd_get_signed_32 (output_bfd,
5091 (sgot->contents + offset));
5092 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + offset);
5094 else
5096 bfd_put_32 (output_bfd, (bfd_vma) 0,
5097 sgot->contents + offset);
5098 rela.r_info = ELF32_R_INFO (h->dynindx, R_NIOS2_GLOB_DAT);
5099 rela.r_addend = 0;
5102 loc = srela->contents;
5103 loc += srela->reloc_count++ * sizeof (Elf32_External_Rela);
5104 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
5107 if (use_plt && h->got.offset != (bfd_vma) -1)
5109 bfd_vma offset = (h->got.offset & ~(bfd_vma) 1);
5110 asection *sgot = htab->root.sgot;
5111 asection *splt = htab->root.splt;
5112 bfd_put_32 (output_bfd, (splt->output_section->vma + splt->output_offset
5113 + h->plt.offset),
5114 sgot->contents + offset);
5117 if (h->needs_copy)
5119 asection *s;
5120 Elf_Internal_Rela rela;
5121 bfd_byte *loc;
5123 /* This symbol needs a copy reloc. Set it up. */
5124 BFD_ASSERT (h->dynindx != -1
5125 && (h->root.type == bfd_link_hash_defined
5126 || h->root.type == bfd_link_hash_defweak));
5128 rela.r_offset = (h->root.u.def.value
5129 + h->root.u.def.section->output_section->vma
5130 + h->root.u.def.section->output_offset);
5131 rela.r_info = ELF32_R_INFO (h->dynindx, R_NIOS2_COPY);
5132 rela.r_addend = 0;
5133 if (h->root.u.def.section == htab->root.sdynrelro)
5134 s = htab->root.sreldynrelro;
5135 else
5136 s = htab->root.srelbss;
5137 BFD_ASSERT (s != NULL);
5138 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
5139 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
5142 /* Mark _DYNAMIC, _GLOBAL_OFFSET_TABLE_, and _gp_got as absolute. */
5143 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
5144 || h == htab->root.hgot
5145 || h == htab->h_gp_got)
5146 sym->st_shndx = SHN_ABS;
5148 return TRUE;
5151 /* Implement elf_backend_finish_dynamic_sections. */
5152 static bfd_boolean
5153 nios2_elf32_finish_dynamic_sections (bfd *output_bfd,
5154 struct bfd_link_info *info)
5156 asection *sgotplt;
5157 asection *sdyn;
5158 struct elf32_nios2_link_hash_table *htab;
5160 htab = elf32_nios2_hash_table (info);
5161 sgotplt = htab->root.sgotplt;
5162 sdyn = NULL;
5164 if (htab->root.dynamic_sections_created)
5166 asection *splt;
5167 Elf32_External_Dyn *dyncon, *dynconend;
5169 splt = htab->root.splt;
5170 sdyn = bfd_get_linker_section (htab->root.dynobj, ".dynamic");
5171 BFD_ASSERT (splt != NULL && sdyn != NULL && sgotplt != NULL);
5173 dyncon = (Elf32_External_Dyn *) sdyn->contents;
5174 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
5175 for (; dyncon < dynconend; dyncon++)
5177 Elf_Internal_Dyn dyn;
5178 asection *s;
5180 bfd_elf32_swap_dyn_in (htab->root.dynobj, dyncon, &dyn);
5182 switch (dyn.d_tag)
5184 default:
5185 break;
5187 case DT_PLTGOT:
5188 s = htab->root.sgotplt;
5189 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
5190 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5191 break;
5193 case DT_JMPREL:
5194 s = htab->root.srelplt;
5195 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
5196 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5197 break;
5199 case DT_PLTRELSZ:
5200 s = htab->root.srelplt;
5201 dyn.d_un.d_val = s->size;
5202 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5203 break;
5205 case DT_NIOS2_GP:
5206 s = htab->root.sgotplt;
5207 dyn.d_un.d_ptr
5208 = s->output_section->vma + s->output_offset + 0x7ff0;
5209 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5210 break;
5214 /* Fill in the first entry in the procedure linkage table. */
5215 if (splt->size > 0)
5217 bfd_vma got_address = (sgotplt->output_section->vma
5218 + sgotplt->output_offset);
5219 if (bfd_link_pic (info))
5221 bfd_vma got_pcrel = got_address - (splt->output_section->vma
5222 + splt->output_offset);
5223 /* Both GOT and PLT must be aligned to a 16-byte boundary
5224 for the two loads to share the %hiadj part. The 4-byte
5225 offset for nextpc is accounted for in the %lo offsets
5226 on the loads. */
5227 BFD_ASSERT ((got_pcrel & 0xf) == 0);
5228 nios2_elf32_install_data (splt, nios2_so_plt0_entry, 0, 6);
5229 nios2_elf32_install_imm16 (splt, 4, hiadj (got_pcrel));
5230 nios2_elf32_install_imm16 (splt, 12, got_pcrel & 0xffff);
5231 nios2_elf32_install_imm16 (splt, 16, (got_pcrel + 4) & 0xffff);
5233 else
5235 /* Divide by 4 here, not 3 because we already corrected for the
5236 res_N branches. */
5237 bfd_vma res_size = (splt->size - 28) / 4;
5238 bfd_vma res_start = (splt->output_section->vma
5239 + splt->output_offset);
5240 bfd_vma res_offset;
5242 for (res_offset = 0; res_offset < res_size; res_offset += 4)
5243 bfd_put_32 (output_bfd,
5244 6 | ((res_size - (res_offset + 4)) << 6),
5245 splt->contents + res_offset);
5247 /* The GOT must be aligned to a 16-byte boundary for the
5248 two loads to share the same %hiadj part. */
5249 BFD_ASSERT ((got_address & 0xf) == 0);
5251 nios2_elf32_install_data (splt, nios2_plt0_entry, res_size, 7);
5252 nios2_elf32_install_imm16 (splt, res_size, hiadj (res_start));
5253 nios2_elf32_install_imm16 (splt, res_size + 4,
5254 res_start & 0xffff);
5255 nios2_elf32_install_imm16 (splt, res_size + 12,
5256 hiadj (got_address));
5257 nios2_elf32_install_imm16 (splt, res_size + 16,
5258 (got_address + 4) & 0xffff);
5259 nios2_elf32_install_imm16 (splt, res_size + 20,
5260 (got_address + 8) & 0xffff);
5265 /* Fill in the first three entries in the global offset table. */
5266 if (sgotplt != NULL && sgotplt->size > 0)
5268 if (sdyn == NULL)
5269 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents);
5270 else
5271 bfd_put_32 (output_bfd,
5272 sdyn->output_section->vma + sdyn->output_offset,
5273 sgotplt->contents);
5274 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 4);
5275 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 8);
5277 if (sgotplt->output_section != bfd_abs_section_ptr)
5278 elf_section_data (sgotplt->output_section)->this_hdr.sh_entsize = 4;
5281 return TRUE;
5284 /* Implement elf_backend_adjust_dynamic_symbol:
5285 Adjust a symbol defined by a dynamic object and referenced by a
5286 regular object. The current definition is in some section of the
5287 dynamic object, but we're not including those sections. We have to
5288 change the definition to something the rest of the link can
5289 understand. */
5290 static bfd_boolean
5291 nios2_elf32_adjust_dynamic_symbol (struct bfd_link_info *info,
5292 struct elf_link_hash_entry *h)
5294 struct elf32_nios2_link_hash_table *htab;
5295 bfd *dynobj;
5296 asection *s, *srel;
5297 unsigned align2;
5299 htab = elf32_nios2_hash_table (info);
5300 dynobj = htab->root.dynobj;
5302 /* Make sure we know what is going on here. */
5303 BFD_ASSERT (dynobj != NULL
5304 && (h->needs_plt
5305 || h->is_weakalias
5306 || (h->def_dynamic
5307 && h->ref_regular
5308 && !h->def_regular)));
5310 /* If this is a function, put it in the procedure linkage table. We
5311 will fill in the contents of the procedure linkage table later,
5312 when we know the address of the .got section. */
5313 if (h->type == STT_FUNC || h->needs_plt)
5315 if (h->plt.refcount <= 0
5316 || SYMBOL_CALLS_LOCAL (info, h)
5317 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
5318 && h->root.type == bfd_link_hash_undefweak))
5320 /* This case can occur if we saw a PLT reloc in an input
5321 file, but the symbol was never referred to by a dynamic
5322 object, or if all references were garbage collected. In
5323 such a case, we don't actually need to build a procedure
5324 linkage table, and we can just do a PCREL reloc instead. */
5325 h->plt.offset = (bfd_vma) -1;
5326 h->needs_plt = 0;
5329 return TRUE;
5332 /* Reinitialize the plt offset now that it is not used as a reference
5333 count any more. */
5334 h->plt.offset = (bfd_vma) -1;
5336 /* If this is a weak symbol, and there is a real definition, the
5337 processor independent code will have arranged for us to see the
5338 real definition first, and we can just use the same value. */
5339 if (h->is_weakalias)
5341 struct elf_link_hash_entry *def = weakdef (h);
5342 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
5343 h->root.u.def.section = def->root.u.def.section;
5344 h->root.u.def.value = def->root.u.def.value;
5345 return TRUE;
5348 /* If there are no non-GOT references, we do not need a copy
5349 relocation. */
5350 if (!h->non_got_ref)
5351 return TRUE;
5353 /* This is a reference to a symbol defined by a dynamic object which
5354 is not a function.
5355 If we are creating a shared library, we must presume that the
5356 only references to the symbol are via the global offset table.
5357 For such cases we need not do anything here; the relocations will
5358 be handled correctly by relocate_section. */
5359 if (bfd_link_pic (info))
5360 return TRUE;
5362 if (h->size == 0)
5364 _bfd_error_handler (_("dynamic variable `%s' is zero size"),
5365 h->root.root.string);
5366 return TRUE;
5369 /* We must allocate the symbol in our .dynbss section, which will
5370 become part of the .bss section of the executable. There will be
5371 an entry for this symbol in the .dynsym section. The dynamic
5372 object will contain position independent code, so all references
5373 from the dynamic object to this symbol will go through the global
5374 offset table. The dynamic linker will use the .dynsym entry to
5375 determine the address it must put in the global offset table, so
5376 both the dynamic object and the regular object will refer to the
5377 same memory location for the variable. */
5378 /* We must generate a R_NIOS2_COPY reloc to tell the dynamic linker to
5379 copy the initial value out of the dynamic object and into the
5380 runtime process image. We need to remember the offset into the
5381 .rela.bss section we are going to use. */
5382 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
5384 s = htab->root.sdynrelro;
5385 srel = htab->root.sreldynrelro;
5387 else
5389 s = htab->root.sdynbss;
5390 srel = htab->root.srelbss;
5392 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
5394 srel->size += sizeof (Elf32_External_Rela);
5395 h->needs_copy = 1;
5398 align2 = bfd_log2 (h->size);
5399 if (align2 > h->root.u.def.section->alignment_power)
5400 align2 = h->root.u.def.section->alignment_power;
5402 /* Align dynbss. */
5403 s->size = BFD_ALIGN (s->size, (bfd_size_type)1 << align2);
5404 if (align2 > bfd_section_alignment (s)
5405 && !bfd_set_section_alignment (s, align2))
5406 return FALSE;
5408 /* Define the symbol as being at this point in the section. */
5409 h->root.u.def.section = s;
5410 h->root.u.def.value = s->size;
5412 /* Increment the section size to make room for the symbol. */
5413 s->size += h->size;
5415 return TRUE;
5418 /* Worker function for nios2_elf32_size_dynamic_sections. */
5419 static bfd_boolean
5420 adjust_dynrelocs (struct elf_link_hash_entry *h, PTR inf)
5422 struct bfd_link_info *info;
5423 struct elf32_nios2_link_hash_table *htab;
5425 if (h->root.type == bfd_link_hash_indirect)
5426 return TRUE;
5428 if (h->root.type == bfd_link_hash_warning)
5429 /* When warning symbols are created, they **replace** the "real"
5430 entry in the hash table, thus we never get to see the real
5431 symbol in a hash traversal. So look at it now. */
5432 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5434 info = (struct bfd_link_info *) inf;
5435 htab = elf32_nios2_hash_table (info);
5437 if (h->plt.offset != (bfd_vma)-1)
5438 h->plt.offset += htab->res_n_size;
5439 if (htab->root.splt == h->root.u.def.section)
5440 h->root.u.def.value += htab->res_n_size;
5442 return TRUE;
5445 /* Another worker function for nios2_elf32_size_dynamic_sections.
5446 Allocate space in .plt, .got and associated reloc sections for
5447 dynamic relocs. */
5448 static bfd_boolean
5449 allocate_dynrelocs (struct elf_link_hash_entry *h, PTR inf)
5451 struct bfd_link_info *info;
5452 struct elf32_nios2_link_hash_table *htab;
5453 struct elf32_nios2_link_hash_entry *eh;
5454 struct elf_dyn_relocs *p;
5455 int use_plt;
5457 if (h->root.type == bfd_link_hash_indirect)
5458 return TRUE;
5460 if (h->root.type == bfd_link_hash_warning)
5461 /* When warning symbols are created, they **replace** the "real"
5462 entry in the hash table, thus we never get to see the real
5463 symbol in a hash traversal. So look at it now. */
5464 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5466 info = (struct bfd_link_info *) inf;
5467 htab = elf32_nios2_hash_table (info);
5469 if (htab->root.dynamic_sections_created
5470 && h->plt.refcount > 0)
5472 /* Make sure this symbol is output as a dynamic symbol.
5473 Undefined weak syms won't yet be marked as dynamic. */
5474 if (h->dynindx == -1
5475 && !h->forced_local
5476 && !bfd_elf_link_record_dynamic_symbol (info, h))
5477 return FALSE;
5479 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info), h))
5481 asection *s = htab->root.splt;
5483 /* Allocate room for the header. */
5484 if (s->size == 0)
5486 if (bfd_link_pic (info))
5487 s->size = 24;
5488 else
5489 s->size = 28;
5492 h->plt.offset = s->size;
5494 /* If this symbol is not defined in a regular file, and we are
5495 not generating a shared library, then set the symbol to this
5496 location in the .plt. This is required to make function
5497 pointers compare as equal between the normal executable and
5498 the shared library. */
5499 if (! bfd_link_pic (info)
5500 && !h->def_regular)
5502 h->root.u.def.section = s;
5503 h->root.u.def.value = h->plt.offset;
5506 /* Make room for this entry. */
5507 s->size += 12;
5509 /* We also need to make an entry in the .rela.plt section. */
5510 htab->root.srelplt->size += sizeof (Elf32_External_Rela);
5512 /* And the .got.plt section. */
5513 htab->root.sgotplt->size += 4;
5515 else
5517 h->plt.offset = (bfd_vma) -1;
5518 h->needs_plt = 0;
5521 else
5523 h->plt.offset = (bfd_vma) -1;
5524 h->needs_plt = 0;
5527 eh = (struct elf32_nios2_link_hash_entry *) h;
5528 use_plt = (eh->got_types_used == CALL_USED
5529 && h->plt.offset != (bfd_vma) -1);
5531 if (h->got.refcount > 0)
5533 asection *s;
5534 bfd_boolean dyn;
5535 int tls_type = eh->tls_type;
5536 int indx;
5538 /* Make sure this symbol is output as a dynamic symbol.
5539 Undefined weak syms won't yet be marked as dynamic. */
5540 if (h->dynindx == -1
5541 && !h->forced_local
5542 && !bfd_elf_link_record_dynamic_symbol (info, h))
5543 return FALSE;
5545 s = htab->root.sgot;
5546 h->got.offset = s->size;
5548 if (tls_type == GOT_UNKNOWN)
5549 abort ();
5551 if (tls_type == GOT_NORMAL)
5552 /* Non-TLS symbols need one GOT slot. */
5553 s->size += 4;
5554 else
5556 if (tls_type & GOT_TLS_GD)
5557 /* R_NIOS2_TLS_GD16 needs 2 consecutive GOT slots. */
5558 s->size += 8;
5559 if (tls_type & GOT_TLS_IE)
5560 /* R_NIOS2_TLS_IE16 needs one GOT slot. */
5561 s->size += 4;
5564 dyn = htab->root.dynamic_sections_created;
5566 indx = 0;
5567 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
5568 && (!bfd_link_pic (info)
5569 || !SYMBOL_REFERENCES_LOCAL (info, h)))
5570 indx = h->dynindx;
5572 if (tls_type != GOT_NORMAL
5573 && (bfd_link_pic (info) || indx != 0)
5574 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
5575 || h->root.type != bfd_link_hash_undefweak))
5577 if (tls_type & GOT_TLS_IE)
5578 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
5580 if (tls_type & GOT_TLS_GD)
5581 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
5583 if ((tls_type & GOT_TLS_GD) && indx != 0)
5584 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
5586 else if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
5587 || h->root.type != bfd_link_hash_undefweak)
5588 && !use_plt
5589 && (bfd_link_pic (info)
5590 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
5591 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
5593 else
5594 h->got.offset = (bfd_vma) -1;
5596 if (h->dyn_relocs == NULL)
5597 return TRUE;
5599 /* In the shared -Bsymbolic case, discard space allocated for
5600 dynamic pc-relative relocs against symbols which turn out to be
5601 defined in regular objects. For the normal shared case, discard
5602 space for pc-relative relocs that have become local due to symbol
5603 visibility changes. */
5605 if (bfd_link_pic (info))
5607 if (h->def_regular
5608 && (h->forced_local || SYMBOLIC_BIND (info, h)))
5610 struct elf_dyn_relocs **pp;
5612 for (pp = &h->dyn_relocs; (p = *pp) != NULL; )
5614 p->count -= p->pc_count;
5615 p->pc_count = 0;
5616 if (p->count == 0)
5617 *pp = p->next;
5618 else
5619 pp = &p->next;
5623 /* Also discard relocs on undefined weak syms with non-default
5624 visibility. */
5625 if (h->dyn_relocs != NULL
5626 && h->root.type == bfd_link_hash_undefweak)
5628 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
5629 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
5630 h->dyn_relocs = NULL;
5632 /* Make sure undefined weak symbols are output as a dynamic
5633 symbol in PIEs. */
5634 else if (h->dynindx == -1
5635 && !h->forced_local
5636 && !bfd_elf_link_record_dynamic_symbol (info, h))
5637 return FALSE;
5640 else
5642 /* For the non-shared case, discard space for relocs against
5643 symbols which turn out to need copy relocs or are not
5644 dynamic. */
5646 if (!h->non_got_ref
5647 && ((h->def_dynamic && !h->def_regular)
5648 || (htab->root.dynamic_sections_created
5649 && (h->root.type == bfd_link_hash_undefweak
5650 || h->root.type == bfd_link_hash_undefined))))
5652 /* Make sure this symbol is output as a dynamic symbol.
5653 Undefined weak syms won't yet be marked as dynamic. */
5654 if (h->dynindx == -1
5655 && !h->forced_local
5656 && !bfd_elf_link_record_dynamic_symbol (info, h))
5657 return FALSE;
5659 /* If that succeeded, we know we'll be keeping all the
5660 relocs. */
5661 if (h->dynindx != -1)
5662 goto keep;
5665 h->dyn_relocs = NULL;
5667 keep: ;
5670 /* Finally, allocate space. */
5671 for (p = h->dyn_relocs; p != NULL; p = p->next)
5673 asection *sreloc = elf_section_data (p->sec)->sreloc;
5674 sreloc->size += p->count * sizeof (Elf32_External_Rela);
5677 return TRUE;
5680 /* Implement elf_backend_size_dynamic_sections:
5681 Set the sizes of the dynamic sections. */
5682 static bfd_boolean
5683 nios2_elf32_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
5684 struct bfd_link_info *info)
5686 bfd *dynobj;
5687 asection *s;
5688 bfd_boolean relocs;
5689 bfd *ibfd;
5690 struct elf32_nios2_link_hash_table *htab;
5692 htab = elf32_nios2_hash_table (info);
5693 dynobj = htab->root.dynobj;
5694 BFD_ASSERT (dynobj != NULL);
5696 htab->res_n_size = 0;
5697 if (htab->root.dynamic_sections_created)
5699 /* Set the contents of the .interp section to the interpreter. */
5700 if (bfd_link_executable (info) && !info->nointerp)
5702 s = bfd_get_linker_section (dynobj, ".interp");
5703 BFD_ASSERT (s != NULL);
5704 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
5705 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
5708 else
5710 /* We may have created entries in the .rela.got section.
5711 However, if we are not creating the dynamic sections, we will
5712 not actually use these entries. Reset the size of .rela.got,
5713 which will cause it to get stripped from the output file
5714 below. */
5715 s = htab->root.srelgot;
5716 if (s != NULL)
5717 s->size = 0;
5720 /* Set up .got offsets for local syms, and space for local dynamic
5721 relocs. */
5722 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
5724 bfd_signed_vma *local_got;
5725 bfd_signed_vma *end_local_got;
5726 char *local_tls_type;
5727 bfd_size_type locsymcount;
5728 Elf_Internal_Shdr *symtab_hdr;
5729 asection *srel;
5731 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
5732 continue;
5734 for (s = ibfd->sections; s != NULL; s = s->next)
5736 struct elf_dyn_relocs *p;
5738 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
5740 if (!bfd_is_abs_section (p->sec)
5741 && bfd_is_abs_section (p->sec->output_section))
5743 /* Input section has been discarded, either because
5744 it is a copy of a linkonce section or due to
5745 linker script /DISCARD/, so we'll be discarding
5746 the relocs too. */
5748 else if (p->count != 0)
5750 srel = elf_section_data (p->sec)->sreloc;
5751 srel->size += p->count * sizeof (Elf32_External_Rela);
5756 local_got = elf_local_got_refcounts (ibfd);
5757 if (!local_got)
5758 continue;
5760 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
5761 locsymcount = symtab_hdr->sh_info;
5762 end_local_got = local_got + locsymcount;
5763 local_tls_type = elf32_nios2_local_got_tls_type (ibfd);
5764 s = htab->root.sgot;
5765 srel = htab->root.srelgot;
5766 for (; local_got < end_local_got; ++local_got, ++local_tls_type)
5768 if (*local_got > 0)
5770 *local_got = s->size;
5771 if (*local_tls_type & GOT_TLS_GD)
5772 /* TLS_GD relocs need an 8-byte structure in the GOT. */
5773 s->size += 8;
5774 if (*local_tls_type & GOT_TLS_IE)
5775 s->size += 4;
5776 if (*local_tls_type == GOT_NORMAL)
5777 s->size += 4;
5779 if (bfd_link_pic (info) || *local_tls_type == GOT_TLS_GD)
5780 srel->size += sizeof (Elf32_External_Rela);
5782 else
5783 *local_got = (bfd_vma) -1;
5787 if (htab->tls_ldm_got.refcount > 0)
5789 /* Allocate two GOT entries and one dynamic relocation (if necessary)
5790 for R_NIOS2_TLS_LDM16 relocations. */
5791 htab->tls_ldm_got.offset = htab->root.sgot->size;
5792 htab->root.sgot->size += 8;
5793 if (bfd_link_pic (info))
5794 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
5796 else
5797 htab->tls_ldm_got.offset = -1;
5799 /* Allocate global sym .plt and .got entries, and space for global
5800 sym dynamic relocs. */
5801 elf_link_hash_traverse (& htab->root, allocate_dynrelocs, info);
5803 if (htab->root.dynamic_sections_created)
5805 /* If the .got section is more than 0x8000 bytes, we add
5806 0x8000 to the value of _gp_got, so that 16-bit relocations
5807 have a greater chance of working. */
5808 if (htab->root.sgot->size >= 0x8000
5809 && htab->h_gp_got->root.u.def.value == 0)
5810 htab->h_gp_got->root.u.def.value = 0x8000;
5813 /* The check_relocs and adjust_dynamic_symbol entry points have
5814 determined the sizes of the various dynamic sections. Allocate
5815 memory for them. */
5816 relocs = FALSE;
5817 for (s = dynobj->sections; s != NULL; s = s->next)
5819 const char *name;
5821 if ((s->flags & SEC_LINKER_CREATED) == 0)
5822 continue;
5824 /* It's OK to base decisions on the section name, because none
5825 of the dynobj section names depend upon the input files. */
5826 name = bfd_section_name (s);
5828 if (CONST_STRNEQ (name, ".rela"))
5830 if (s->size != 0)
5832 if (s != htab->root.srelplt)
5833 relocs = TRUE;
5835 /* We use the reloc_count field as a counter if we need
5836 to copy relocs into the output file. */
5837 s->reloc_count = 0;
5840 else if (s == htab->root.splt)
5842 /* Correct for the number of res_N branches. */
5843 if (s->size != 0 && !bfd_link_pic (info))
5845 htab->res_n_size = (s->size - 28) / 3;
5846 s->size += htab->res_n_size;
5849 else if (s != htab->sbss
5850 && s != htab->root.sgot
5851 && s != htab->root.sgotplt
5852 && s != htab->root.sdynbss
5853 && s != htab->root.sdynrelro)
5854 /* It's not one of our sections, so don't allocate space. */
5855 continue;
5857 if (s->size == 0)
5859 s->flags |= SEC_EXCLUDE;
5860 continue;
5863 if ((s->flags & SEC_HAS_CONTENTS) == 0)
5864 continue;
5866 /* Allocate memory for the section contents. */
5867 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
5868 if (s->contents == NULL)
5869 return FALSE;
5872 /* Adjust dynamic symbols that point to the plt to account for the
5873 now-known number of resN slots. */
5874 if (htab->res_n_size)
5875 elf_link_hash_traverse (& htab->root, adjust_dynrelocs, info);
5877 return _bfd_elf_add_dynamic_tags (output_bfd, info, relocs);
5880 /* Free the derived linker hash table. */
5881 static void
5882 nios2_elf32_link_hash_table_free (bfd *obfd)
5884 struct elf32_nios2_link_hash_table *htab
5885 = (struct elf32_nios2_link_hash_table *) obfd->link.hash;
5887 bfd_hash_table_free (&htab->bstab);
5888 _bfd_elf_link_hash_table_free (obfd);
5891 /* Implement bfd_elf32_bfd_link_hash_table_create. */
5892 static struct bfd_link_hash_table *
5893 nios2_elf32_link_hash_table_create (bfd *abfd)
5895 struct elf32_nios2_link_hash_table *ret;
5896 size_t amt = sizeof (struct elf32_nios2_link_hash_table);
5898 ret = bfd_zmalloc (amt);
5899 if (ret == NULL)
5900 return NULL;
5902 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
5903 link_hash_newfunc,
5904 sizeof (struct
5905 elf32_nios2_link_hash_entry),
5906 NIOS2_ELF_DATA))
5908 free (ret);
5909 return NULL;
5912 /* Init the stub hash table too. */
5913 if (!bfd_hash_table_init (&ret->bstab, stub_hash_newfunc,
5914 sizeof (struct elf32_nios2_stub_hash_entry)))
5916 _bfd_elf_link_hash_table_free (abfd);
5917 return NULL;
5919 ret->root.root.hash_table_free = nios2_elf32_link_hash_table_free;
5921 return &ret->root.root;
5924 /* Implement elf_backend_reloc_type_class. */
5925 static enum elf_reloc_type_class
5926 nios2_elf32_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
5927 const asection *rel_sec ATTRIBUTE_UNUSED,
5928 const Elf_Internal_Rela *rela)
5930 switch ((int) ELF32_R_TYPE (rela->r_info))
5932 case R_NIOS2_RELATIVE:
5933 return reloc_class_relative;
5934 case R_NIOS2_JUMP_SLOT:
5935 return reloc_class_plt;
5936 case R_NIOS2_COPY:
5937 return reloc_class_copy;
5938 default:
5939 return reloc_class_normal;
5943 /* Return 1 if target is one of ours. */
5944 static bfd_boolean
5945 is_nios2_elf_target (const struct bfd_target *targ)
5947 return (targ == &nios2_elf32_le_vec
5948 || targ == &nios2_elf32_be_vec);
5951 /* Implement elf_backend_add_symbol_hook.
5952 This hook is called by the linker when adding symbols from an object
5953 file. We use it to put .comm items in .sbss, and not .bss. */
5954 static bfd_boolean
5955 nios2_elf_add_symbol_hook (bfd *abfd,
5956 struct bfd_link_info *info,
5957 Elf_Internal_Sym *sym,
5958 const char **namep ATTRIBUTE_UNUSED,
5959 flagword *flagsp ATTRIBUTE_UNUSED,
5960 asection **secp,
5961 bfd_vma *valp)
5963 if (sym->st_shndx == SHN_COMMON
5964 && !bfd_link_relocatable (info)
5965 && sym->st_size <= elf_gp_size (abfd)
5966 && is_nios2_elf_target (info->output_bfd->xvec))
5968 /* Common symbols less than or equal to -G nn bytes are automatically
5969 put into .sbss. */
5970 struct elf32_nios2_link_hash_table *htab;
5972 htab = elf32_nios2_hash_table (info);
5973 if (htab->sbss == NULL)
5975 flagword flags = SEC_IS_COMMON | SEC_SMALL_DATA | SEC_LINKER_CREATED;
5977 if (htab->root.dynobj == NULL)
5978 htab->root.dynobj = abfd;
5980 htab->sbss = bfd_make_section_anyway_with_flags (htab->root.dynobj,
5981 ".sbss", flags);
5982 if (htab->sbss == NULL)
5983 return FALSE;
5986 *secp = htab->sbss;
5987 *valp = sym->st_size;
5990 return TRUE;
5993 /* Implement elf_backend_can_make_relative_eh_frame:
5994 Decide whether to attempt to turn absptr or lsda encodings in
5995 shared libraries into pcrel within the given input section. */
5996 static bfd_boolean
5997 nios2_elf32_can_make_relative_eh_frame (bfd *input_bfd ATTRIBUTE_UNUSED,
5998 struct bfd_link_info *info
5999 ATTRIBUTE_UNUSED,
6000 asection *eh_frame_section
6001 ATTRIBUTE_UNUSED)
6003 /* We can't use PC-relative encodings in the .eh_frame section. */
6004 return FALSE;
6007 /* Implement elf_backend_special_sections. */
6008 const struct bfd_elf_special_section elf32_nios2_special_sections[] =
6010 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS,
6011 SHF_ALLOC + SHF_WRITE + SHF_NIOS2_GPREL },
6012 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS,
6013 SHF_ALLOC + SHF_WRITE + SHF_NIOS2_GPREL },
6014 { NULL, 0, 0, 0, 0 }
6017 #define ELF_ARCH bfd_arch_nios2
6018 #define ELF_TARGET_ID NIOS2_ELF_DATA
6019 #define ELF_MACHINE_CODE EM_ALTERA_NIOS2
6021 /* The Nios II MMU uses a 4K page size. */
6023 #define ELF_MAXPAGESIZE 0x1000
6025 #define bfd_elf32_bfd_link_hash_table_create \
6026 nios2_elf32_link_hash_table_create
6028 #define bfd_elf32_bfd_merge_private_bfd_data \
6029 nios2_elf32_merge_private_bfd_data
6031 /* Relocation table lookup macros. */
6033 #define bfd_elf32_bfd_reloc_type_lookup nios2_elf32_bfd_reloc_type_lookup
6034 #define bfd_elf32_bfd_reloc_name_lookup nios2_elf32_bfd_reloc_name_lookup
6036 /* JUMP_TABLE_LINK macros. */
6038 /* elf_info_to_howto (using RELA relocations). */
6040 #define elf_info_to_howto nios2_elf32_info_to_howto
6042 /* elf backend functions. */
6044 #define elf_backend_can_gc_sections 1
6045 #define elf_backend_can_refcount 1
6046 #define elf_backend_plt_readonly 1
6047 #define elf_backend_want_got_plt 1
6048 #define elf_backend_want_dynrelro 1
6049 #define elf_backend_rela_normal 1
6050 #define elf_backend_dtrel_excludes_plt 1
6052 #define elf_backend_relocate_section nios2_elf32_relocate_section
6053 #define elf_backend_section_flags nios2_elf32_section_flags
6054 #define elf_backend_fake_sections nios2_elf32_fake_sections
6055 #define elf_backend_check_relocs nios2_elf32_check_relocs
6057 #define elf_backend_gc_mark_hook nios2_elf32_gc_mark_hook
6058 #define elf_backend_create_dynamic_sections \
6059 nios2_elf32_create_dynamic_sections
6060 #define elf_backend_finish_dynamic_symbol nios2_elf32_finish_dynamic_symbol
6061 #define elf_backend_finish_dynamic_sections \
6062 nios2_elf32_finish_dynamic_sections
6063 #define elf_backend_adjust_dynamic_symbol nios2_elf32_adjust_dynamic_symbol
6064 #define elf_backend_reloc_type_class nios2_elf32_reloc_type_class
6065 #define elf_backend_size_dynamic_sections nios2_elf32_size_dynamic_sections
6066 #define elf_backend_add_symbol_hook nios2_elf_add_symbol_hook
6067 #define elf_backend_copy_indirect_symbol nios2_elf32_copy_indirect_symbol
6068 #define elf_backend_object_p nios2_elf32_object_p
6070 #define elf_backend_grok_prstatus nios2_grok_prstatus
6071 #define elf_backend_grok_psinfo nios2_grok_psinfo
6073 #undef elf_backend_can_make_relative_eh_frame
6074 #define elf_backend_can_make_relative_eh_frame \
6075 nios2_elf32_can_make_relative_eh_frame
6077 #define elf_backend_special_sections elf32_nios2_special_sections
6079 #define TARGET_LITTLE_SYM nios2_elf32_le_vec
6080 #define TARGET_LITTLE_NAME "elf32-littlenios2"
6081 #define TARGET_BIG_SYM nios2_elf32_be_vec
6082 #define TARGET_BIG_NAME "elf32-bignios2"
6084 #define elf_backend_got_header_size 12
6085 #define elf_backend_default_execstack 0
6087 #include "elf32-target.h"