Automatic date update in version.in
[binutils-gdb.git] / bfd / elf32-nios2.c
blob5456c07d2923eb1de5d8e28cbcd6f209c8cc619d
1 /* 32-bit ELF support for Nios II.
2 Copyright (C) 2012-2017 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"
35 /* Use RELA relocations. */
36 #ifndef USE_RELA
37 #define USE_RELA
38 #endif
40 #ifdef USE_REL
41 #undef USE_REL
42 #endif
44 /* Forward declarations. */
45 static bfd_reloc_status_type nios2_elf32_ignore_reloc
46 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
47 static bfd_reloc_status_type nios2_elf32_hi16_relocate
48 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
49 static bfd_reloc_status_type nios2_elf32_lo16_relocate
50 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
51 static bfd_reloc_status_type nios2_elf32_hiadj16_relocate
52 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
53 static bfd_reloc_status_type nios2_elf32_pcrel_lo16_relocate
54 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
55 static bfd_reloc_status_type nios2_elf32_pcrel_hiadj16_relocate
56 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
57 static bfd_reloc_status_type nios2_elf32_pcrel16_relocate
58 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
59 static bfd_reloc_status_type nios2_elf32_call26_relocate
60 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
61 static bfd_reloc_status_type nios2_elf32_gprel_relocate
62 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
63 static bfd_reloc_status_type nios2_elf32_ujmp_relocate
64 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
65 static bfd_reloc_status_type nios2_elf32_cjmp_relocate
66 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
67 static bfd_reloc_status_type nios2_elf32_callr_relocate
68 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
70 /* Target vector. */
71 extern const bfd_target nios2_elf32_le_vec;
72 extern const bfd_target nios2_elf32_be_vec;
74 /* Offset of tp and dtp pointers from start of TLS block. */
75 #define TP_OFFSET 0x7000
76 #define DTP_OFFSET 0x8000
78 /* The relocation tables used for SHT_REL sections. There are separate
79 tables for R1 and R2 encodings. */
80 static reloc_howto_type elf_nios2_r1_howto_table_rel[] = {
81 /* No relocation. */
82 HOWTO (R_NIOS2_NONE, /* type */
83 0, /* rightshift */
84 3, /* size (0 = byte, 1 = short, 2 = long) */
85 0, /* bitsize */
86 FALSE, /* pc_relative */
87 0, /* bitpos */
88 complain_overflow_dont, /* complain_on_overflow */
89 bfd_elf_generic_reloc, /* special_function */
90 "R_NIOS2_NONE", /* name */
91 FALSE, /* partial_inplace */
92 0, /* src_mask */
93 0, /* dst_mask */
94 FALSE), /* pcrel_offset */
96 /* 16-bit signed immediate relocation. */
97 HOWTO (R_NIOS2_S16, /* type */
98 0, /* rightshift */
99 2, /* size (0 = byte, 1 = short, 2 = long) */
100 16, /* bitsize */
101 FALSE, /* pc_relative */
102 6, /* bitpos */
103 complain_overflow_signed, /* complain on overflow */
104 bfd_elf_generic_reloc, /* special function */
105 "R_NIOS2_S16", /* name */
106 FALSE, /* partial_inplace */
107 0x003fffc0, /* src_mask */
108 0x003fffc0, /* dest_mask */
109 FALSE), /* pcrel_offset */
111 /* 16-bit unsigned immediate relocation. */
112 HOWTO (R_NIOS2_U16, /* type */
113 0, /* rightshift */
114 2, /* size (0 = byte, 1 = short, 2 = long) */
115 16, /* bitsize */
116 FALSE, /* pc_relative */
117 6, /* bitpos */
118 complain_overflow_unsigned, /* complain on overflow */
119 bfd_elf_generic_reloc, /* special function */
120 "R_NIOS2_U16", /* name */
121 FALSE, /* partial_inplace */
122 0x003fffc0, /* src_mask */
123 0x003fffc0, /* dest_mask */
124 FALSE), /* pcrel_offset */
126 HOWTO (R_NIOS2_PCREL16, /* type */
127 0, /* rightshift */
128 2, /* size (0 = byte, 1 = short, 2 = long) */
129 16, /* bitsize */
130 TRUE, /* pc_relative */
131 6, /* bitpos */
132 complain_overflow_signed, /* complain on overflow */
133 nios2_elf32_pcrel16_relocate, /* special function */
134 "R_NIOS2_PCREL16", /* name */
135 FALSE, /* partial_inplace */
136 0x003fffc0, /* src_mask */
137 0x003fffc0, /* dest_mask */
138 TRUE), /* pcrel_offset */
140 HOWTO (R_NIOS2_CALL26, /* type */
141 2, /* rightshift */
142 2, /* size (0 = byte, 1 = short, 2 = long) */
143 26, /* bitsize */
144 FALSE, /* pc_relative */
145 6, /* bitpos */
146 complain_overflow_dont, /* complain on overflow */
147 nios2_elf32_call26_relocate, /* special function */
148 "R_NIOS2_CALL26", /* name */
149 FALSE, /* partial_inplace */
150 0xffffffc0, /* src_mask */
151 0xffffffc0, /* dst_mask */
152 FALSE), /* pcrel_offset */
154 HOWTO (R_NIOS2_IMM5,
158 FALSE,
160 complain_overflow_bitfield,
161 bfd_elf_generic_reloc,
162 "R_NIOS2_IMM5",
163 FALSE,
164 0x000007c0,
165 0x000007c0,
166 FALSE),
168 HOWTO (R_NIOS2_CACHE_OPX,
172 FALSE,
174 complain_overflow_bitfield,
175 bfd_elf_generic_reloc,
176 "R_NIOS2_CACHE_OPX",
177 FALSE,
178 0x07c00000,
179 0x07c00000,
180 FALSE),
182 HOWTO (R_NIOS2_IMM6,
186 FALSE,
188 complain_overflow_bitfield,
189 bfd_elf_generic_reloc,
190 "R_NIOS2_IMM6",
191 FALSE,
192 0x00000fc0,
193 0x00000fc0,
194 FALSE),
196 HOWTO (R_NIOS2_IMM8,
200 FALSE,
202 complain_overflow_bitfield,
203 bfd_elf_generic_reloc,
204 "R_NIOS2_IMM8",
205 FALSE,
206 0x00003fc0,
207 0x00003fc0,
208 FALSE),
210 HOWTO (R_NIOS2_HI16,
214 FALSE,
216 complain_overflow_dont,
217 nios2_elf32_hi16_relocate,
218 "R_NIOS2_HI16",
219 FALSE,
220 0x003fffc0,
221 0x003fffc0,
222 FALSE),
224 HOWTO (R_NIOS2_LO16,
228 FALSE,
230 complain_overflow_dont,
231 nios2_elf32_lo16_relocate,
232 "R_NIOS2_LO16",
233 FALSE,
234 0x003fffc0,
235 0x003fffc0,
236 FALSE),
238 HOWTO (R_NIOS2_HIADJ16,
242 FALSE,
244 complain_overflow_dont,
245 nios2_elf32_hiadj16_relocate,
246 "R_NIOS2_HIADJ16",
247 FALSE,
248 0x003fffc0,
249 0x003fffc0,
250 FALSE),
252 HOWTO (R_NIOS2_BFD_RELOC_32,
254 2, /* long */
256 FALSE,
258 complain_overflow_dont,
259 bfd_elf_generic_reloc,
260 "R_NIOS2_BFD_RELOC32",
261 FALSE,
262 0xffffffff,
263 0xffffffff,
264 FALSE),
266 HOWTO (R_NIOS2_BFD_RELOC_16,
268 1, /* short */
270 FALSE,
272 complain_overflow_bitfield,
273 bfd_elf_generic_reloc,
274 "R_NIOS2_BFD_RELOC16",
275 FALSE,
276 0x0000ffff,
277 0x0000ffff,
278 FALSE),
280 HOWTO (R_NIOS2_BFD_RELOC_8,
282 0, /* byte */
284 FALSE,
286 complain_overflow_bitfield,
287 bfd_elf_generic_reloc,
288 "R_NIOS2_BFD_RELOC8",
289 FALSE,
290 0x000000ff,
291 0x000000ff,
292 FALSE),
294 HOWTO (R_NIOS2_GPREL,
298 FALSE,
300 complain_overflow_dont,
301 nios2_elf32_gprel_relocate,
302 "R_NIOS2_GPREL",
303 FALSE,
304 0x003fffc0,
305 0x003fffc0,
306 FALSE),
308 HOWTO (R_NIOS2_GNU_VTINHERIT,
310 2, /* short */
312 FALSE,
314 complain_overflow_dont,
315 NULL,
316 "R_NIOS2_GNU_VTINHERIT",
317 FALSE,
320 FALSE),
322 HOWTO (R_NIOS2_GNU_VTENTRY,
324 2, /* byte */
326 FALSE,
328 complain_overflow_dont,
329 _bfd_elf_rel_vtable_reloc_fn,
330 "R_NIOS2_GNU_VTENTRY",
331 FALSE,
334 FALSE),
336 HOWTO (R_NIOS2_UJMP,
340 FALSE,
342 complain_overflow_dont,
343 nios2_elf32_ujmp_relocate,
344 "R_NIOS2_UJMP",
345 FALSE,
346 0x003fffc0,
347 0x003fffc0,
348 FALSE),
350 HOWTO (R_NIOS2_CJMP,
354 FALSE,
356 complain_overflow_dont,
357 nios2_elf32_cjmp_relocate,
358 "R_NIOS2_CJMP",
359 FALSE,
360 0x003fffc0,
361 0x003fffc0,
362 FALSE),
364 HOWTO (R_NIOS2_CALLR,
368 FALSE,
370 complain_overflow_dont,
371 nios2_elf32_callr_relocate,
372 "R_NIOS2_CALLR",
373 FALSE,
374 0x003fffc0,
375 0x003fffc0,
376 FALSE),
378 HOWTO (R_NIOS2_ALIGN,
382 FALSE,
384 complain_overflow_dont,
385 nios2_elf32_ignore_reloc,
386 "R_NIOS2_ALIGN",
387 FALSE,
390 TRUE),
393 HOWTO (R_NIOS2_GOT16,
397 FALSE,
399 complain_overflow_bitfield,
400 bfd_elf_generic_reloc,
401 "R_NIOS2_GOT16",
402 FALSE,
403 0x003fffc0,
404 0x003fffc0,
405 FALSE),
407 HOWTO (R_NIOS2_CALL16,
411 FALSE,
413 complain_overflow_bitfield,
414 bfd_elf_generic_reloc,
415 "R_NIOS2_CALL16",
416 FALSE,
417 0x003fffc0,
418 0x003fffc0,
419 FALSE),
421 HOWTO (R_NIOS2_GOTOFF_LO,
425 FALSE,
427 complain_overflow_dont,
428 bfd_elf_generic_reloc,
429 "R_NIOS2_GOTOFF_LO",
430 FALSE,
431 0x003fffc0,
432 0x003fffc0,
433 FALSE),
435 HOWTO (R_NIOS2_GOTOFF_HA,
439 FALSE,
441 complain_overflow_dont,
442 bfd_elf_generic_reloc,
443 "R_NIOS2_GOTOFF_HA",
444 FALSE,
445 0x003fffc0,
446 0x003fffc0,
447 FALSE),
449 HOWTO (R_NIOS2_PCREL_LO,
453 TRUE,
455 complain_overflow_dont,
456 nios2_elf32_pcrel_lo16_relocate,
457 "R_NIOS2_PCREL_LO",
458 FALSE,
459 0x003fffc0,
460 0x003fffc0,
461 TRUE),
463 HOWTO (R_NIOS2_PCREL_HA,
467 FALSE, /* This is a PC-relative relocation, but we need to subtract
468 PC ourselves before the HIADJ. */
470 complain_overflow_dont,
471 nios2_elf32_pcrel_hiadj16_relocate,
472 "R_NIOS2_PCREL_HA",
473 FALSE,
474 0x003fffc0,
475 0x003fffc0,
476 TRUE),
478 HOWTO (R_NIOS2_TLS_GD16,
482 FALSE,
484 complain_overflow_bitfield,
485 bfd_elf_generic_reloc,
486 "R_NIOS2_TLS_GD16",
487 FALSE,
488 0x003fffc0,
489 0x003fffc0,
490 FALSE),
492 HOWTO (R_NIOS2_TLS_LDM16,
496 FALSE,
498 complain_overflow_bitfield,
499 bfd_elf_generic_reloc,
500 "R_NIOS2_TLS_LDM16",
501 FALSE,
502 0x003fffc0,
503 0x003fffc0,
504 FALSE),
506 HOWTO (R_NIOS2_TLS_LDO16,
510 FALSE,
512 complain_overflow_bitfield,
513 bfd_elf_generic_reloc,
514 "R_NIOS2_TLS_LDO16",
515 FALSE,
516 0x003fffc0,
517 0x003fffc0,
518 FALSE),
520 HOWTO (R_NIOS2_TLS_IE16,
524 FALSE,
526 complain_overflow_bitfield,
527 bfd_elf_generic_reloc,
528 "R_NIOS2_TLS_IE16",
529 FALSE,
530 0x003fffc0,
531 0x003fffc0,
532 FALSE),
534 HOWTO (R_NIOS2_TLS_LE16,
538 FALSE,
540 complain_overflow_bitfield,
541 bfd_elf_generic_reloc,
542 "R_NIOS2_TLS_LE16",
543 FALSE,
544 0x003fffc0,
545 0x003fffc0,
546 FALSE),
548 HOWTO (R_NIOS2_TLS_DTPMOD,
552 FALSE,
554 complain_overflow_dont,
555 bfd_elf_generic_reloc,
556 "R_NIOS2_TLS_DTPMOD",
557 FALSE,
558 0xffffffff,
559 0xffffffff,
560 FALSE),
562 HOWTO (R_NIOS2_TLS_DTPREL,
566 FALSE,
568 complain_overflow_dont,
569 bfd_elf_generic_reloc,
570 "R_NIOS2_TLS_DTPREL",
571 FALSE,
572 0xffffffff,
573 0xffffffff,
574 FALSE),
576 HOWTO (R_NIOS2_TLS_TPREL,
580 FALSE,
582 complain_overflow_dont,
583 bfd_elf_generic_reloc,
584 "R_NIOS2_TLS_TPREL",
585 FALSE,
586 0xffffffff,
587 0xffffffff,
588 FALSE),
590 HOWTO (R_NIOS2_COPY,
594 FALSE,
596 complain_overflow_dont,
597 bfd_elf_generic_reloc,
598 "R_NIOS2_COPY",
599 FALSE,
602 FALSE),
604 HOWTO (R_NIOS2_GLOB_DAT,
608 FALSE,
610 complain_overflow_dont,
611 bfd_elf_generic_reloc,
612 "R_NIOS2_GLOB_DAT",
613 FALSE,
614 0xffffffff,
615 0xffffffff,
616 FALSE),
618 HOWTO (R_NIOS2_JUMP_SLOT,
622 FALSE,
624 complain_overflow_dont,
625 bfd_elf_generic_reloc,
626 "R_NIOS2_JUMP_SLOT",
627 FALSE,
628 0xffffffff,
629 0xffffffff,
630 FALSE),
632 HOWTO (R_NIOS2_RELATIVE,
636 FALSE,
638 complain_overflow_dont,
639 bfd_elf_generic_reloc,
640 "R_NIOS2_RELATIVE",
641 FALSE,
642 0xffffffff,
643 0xffffffff,
644 FALSE),
646 HOWTO (R_NIOS2_GOTOFF,
650 FALSE,
652 complain_overflow_dont,
653 bfd_elf_generic_reloc,
654 "R_NIOS2_GOTOFF",
655 FALSE,
656 0xffffffff,
657 0xffffffff,
658 FALSE),
660 HOWTO (R_NIOS2_CALL26_NOAT, /* type */
661 2, /* rightshift */
662 2, /* size (0 = byte, 1 = short, 2 = long) */
663 26, /* bitsize */
664 FALSE, /* pc_relative */
665 6, /* bitpos */
666 complain_overflow_dont, /* complain on overflow */
667 nios2_elf32_call26_relocate, /* special function */
668 "R_NIOS2_CALL26_NOAT", /* name */
669 FALSE, /* partial_inplace */
670 0xffffffc0, /* src_mask */
671 0xffffffc0, /* dst_mask */
672 FALSE), /* pcrel_offset */
674 HOWTO (R_NIOS2_GOT_LO,
678 FALSE,
680 complain_overflow_dont,
681 bfd_elf_generic_reloc,
682 "R_NIOS2_GOT_LO",
683 FALSE,
684 0x003fffc0,
685 0x003fffc0,
686 FALSE),
688 HOWTO (R_NIOS2_GOT_HA,
692 FALSE,
694 complain_overflow_dont,
695 bfd_elf_generic_reloc,
696 "R_NIOS2_GOT_HA",
697 FALSE,
698 0x003fffc0,
699 0x003fffc0,
700 FALSE),
702 HOWTO (R_NIOS2_CALL_LO,
706 FALSE,
708 complain_overflow_dont,
709 bfd_elf_generic_reloc,
710 "R_NIOS2_CALL_LO",
711 FALSE,
712 0x003fffc0,
713 0x003fffc0,
714 FALSE),
716 HOWTO (R_NIOS2_CALL_HA,
720 FALSE,
722 complain_overflow_dont,
723 bfd_elf_generic_reloc,
724 "R_NIOS2_CALL_HA",
725 FALSE,
726 0x003fffc0,
727 0x003fffc0,
728 FALSE),
730 /* Add other relocations here. */
733 static reloc_howto_type elf_nios2_r2_howto_table_rel[] = {
734 /* No relocation. */
735 HOWTO (R_NIOS2_NONE, /* type */
736 0, /* rightshift */
737 0, /* size (0 = byte, 1 = short, 2 = long) */
738 0, /* bitsize */
739 FALSE, /* pc_relative */
740 0, /* bitpos */
741 complain_overflow_dont, /* complain_on_overflow */
742 bfd_elf_generic_reloc, /* special_function */
743 "R_NIOS2_NONE", /* name */
744 FALSE, /* partial_inplace */
745 0, /* src_mask */
746 0, /* dst_mask */
747 FALSE), /* pcrel_offset */
749 /* 16-bit signed immediate relocation. */
750 HOWTO (R_NIOS2_S16, /* type */
751 0, /* rightshift */
752 2, /* size (0 = byte, 1 = short, 2 = long) */
753 16, /* bitsize */
754 FALSE, /* pc_relative */
755 16, /* bitpos */
756 complain_overflow_signed, /* complain on overflow */
757 bfd_elf_generic_reloc, /* special function */
758 "R_NIOS2_S16", /* name */
759 FALSE, /* partial_inplace */
760 0xffff0000, /* src_mask */
761 0xffff0000, /* dest_mask */
762 FALSE), /* pcrel_offset */
764 /* 16-bit unsigned immediate relocation. */
765 HOWTO (R_NIOS2_U16, /* type */
766 0, /* rightshift */
767 2, /* size (0 = byte, 1 = short, 2 = long) */
768 16, /* bitsize */
769 FALSE, /* pc_relative */
770 16, /* bitpos */
771 complain_overflow_unsigned, /* complain on overflow */
772 bfd_elf_generic_reloc, /* special function */
773 "R_NIOS2_U16", /* name */
774 FALSE, /* partial_inplace */
775 0xffff0000, /* src_mask */
776 0xffff0000, /* dest_mask */
777 FALSE), /* pcrel_offset */
779 HOWTO (R_NIOS2_PCREL16, /* type */
780 0, /* rightshift */
781 2, /* size (0 = byte, 1 = short, 2 = long) */
782 16, /* bitsize */
783 TRUE, /* pc_relative */
784 16, /* bitpos */
785 complain_overflow_signed, /* complain on overflow */
786 nios2_elf32_pcrel16_relocate, /* special function */
787 "R_NIOS2_PCREL16", /* name */
788 FALSE, /* partial_inplace */
789 0xffff0000, /* src_mask */
790 0xffff0000, /* dest_mask */
791 TRUE), /* pcrel_offset */
793 HOWTO (R_NIOS2_CALL26, /* type */
794 2, /* rightshift */
795 2, /* size (0 = byte, 1 = short, 2 = long) */
796 26, /* bitsize */
797 FALSE, /* pc_relative */
798 6, /* bitpos */
799 complain_overflow_dont, /* complain on overflow */
800 nios2_elf32_call26_relocate, /* special function */
801 "R_NIOS2_CALL26", /* name */
802 FALSE, /* partial_inplace */
803 0xffffffc0, /* src_mask */
804 0xffffffc0, /* dst_mask */
805 FALSE), /* pcrel_offset */
807 HOWTO (R_NIOS2_IMM5,
811 FALSE,
813 complain_overflow_bitfield,
814 bfd_elf_generic_reloc,
815 "R_NIOS2_IMM5",
816 FALSE,
817 0x03e00000,
818 0x03e00000,
819 FALSE),
821 HOWTO (R_NIOS2_CACHE_OPX,
825 FALSE,
827 complain_overflow_bitfield,
828 bfd_elf_generic_reloc,
829 "R_NIOS2_CACHE_OPX",
830 FALSE,
831 0x0000f800,
832 0x0000f800,
833 FALSE),
835 HOWTO (R_NIOS2_IMM6,
839 FALSE,
841 complain_overflow_bitfield,
842 bfd_elf_generic_reloc,
843 "R_NIOS2_IMM6",
844 FALSE,
845 0xfc000000,
846 0xfc000000,
847 FALSE),
849 HOWTO (R_NIOS2_IMM8,
853 FALSE,
855 complain_overflow_bitfield,
856 bfd_elf_generic_reloc,
857 "R_NIOS2_IMM8",
858 FALSE,
859 0xff000000,
860 0xff000000,
861 FALSE),
863 HOWTO (R_NIOS2_HI16,
867 FALSE,
869 complain_overflow_dont,
870 nios2_elf32_hi16_relocate,
871 "R_NIOS2_HI16",
872 FALSE,
873 0xffff0000,
874 0xffff0000,
875 FALSE),
877 HOWTO (R_NIOS2_LO16,
881 FALSE,
883 complain_overflow_dont,
884 nios2_elf32_lo16_relocate,
885 "R_NIOS2_LO16",
886 FALSE,
887 0xffff0000,
888 0xffff0000,
889 FALSE),
891 HOWTO (R_NIOS2_HIADJ16,
895 FALSE,
897 complain_overflow_dont,
898 nios2_elf32_hiadj16_relocate,
899 "R_NIOS2_HIADJ16",
900 FALSE,
901 0xffff0000,
902 0xffff0000,
903 FALSE),
905 HOWTO (R_NIOS2_BFD_RELOC_32,
907 2, /* long */
909 FALSE,
911 complain_overflow_dont,
912 bfd_elf_generic_reloc,
913 "R_NIOS2_BFD_RELOC32",
914 FALSE,
915 0xffffffff,
916 0xffffffff,
917 FALSE),
919 HOWTO (R_NIOS2_BFD_RELOC_16,
921 1, /* short */
923 FALSE,
925 complain_overflow_bitfield,
926 bfd_elf_generic_reloc,
927 "R_NIOS2_BFD_RELOC16",
928 FALSE,
929 0x0000ffff,
930 0x0000ffff,
931 FALSE),
933 HOWTO (R_NIOS2_BFD_RELOC_8,
935 0, /* byte */
937 FALSE,
939 complain_overflow_bitfield,
940 bfd_elf_generic_reloc,
941 "R_NIOS2_BFD_RELOC8",
942 FALSE,
943 0x000000ff,
944 0x000000ff,
945 FALSE),
947 HOWTO (R_NIOS2_GPREL,
951 FALSE,
953 complain_overflow_dont,
954 nios2_elf32_gprel_relocate,
955 "R_NIOS2_GPREL",
956 FALSE,
957 0xffff0000,
958 0xffff0000,
959 FALSE),
961 HOWTO (R_NIOS2_GNU_VTINHERIT,
963 2, /* short */
965 FALSE,
967 complain_overflow_dont,
968 NULL,
969 "R_NIOS2_GNU_VTINHERIT",
970 FALSE,
973 FALSE),
975 HOWTO (R_NIOS2_GNU_VTENTRY,
977 2, /* byte */
979 FALSE,
981 complain_overflow_dont,
982 _bfd_elf_rel_vtable_reloc_fn,
983 "R_NIOS2_GNU_VTENTRY",
984 FALSE,
987 FALSE),
989 HOWTO (R_NIOS2_UJMP,
993 FALSE,
995 complain_overflow_dont,
996 nios2_elf32_ujmp_relocate,
997 "R_NIOS2_UJMP",
998 FALSE,
999 0xffff0000,
1000 0xffff0000,
1001 FALSE),
1003 HOWTO (R_NIOS2_CJMP,
1007 FALSE,
1009 complain_overflow_dont,
1010 nios2_elf32_cjmp_relocate,
1011 "R_NIOS2_CJMP",
1012 FALSE,
1013 0xffff0000,
1014 0xffff0000,
1015 FALSE),
1017 HOWTO (R_NIOS2_CALLR,
1021 FALSE,
1023 complain_overflow_dont,
1024 nios2_elf32_callr_relocate,
1025 "R_NIOS2_CALLR",
1026 FALSE,
1027 0xffff0000,
1028 0xffff0000,
1029 FALSE),
1031 HOWTO (R_NIOS2_ALIGN,
1035 FALSE,
1037 complain_overflow_dont,
1038 nios2_elf32_ignore_reloc,
1039 "R_NIOS2_ALIGN",
1040 FALSE,
1043 TRUE),
1045 HOWTO (R_NIOS2_GOT16,
1049 FALSE,
1051 complain_overflow_bitfield,
1052 bfd_elf_generic_reloc,
1053 "R_NIOS2_GOT16",
1054 FALSE,
1055 0xffff0000,
1056 0xffff0000,
1057 FALSE),
1059 HOWTO (R_NIOS2_CALL16,
1063 FALSE,
1065 complain_overflow_bitfield,
1066 bfd_elf_generic_reloc,
1067 "R_NIOS2_CALL16",
1068 FALSE,
1069 0xffff0000,
1070 0xffff0000,
1071 FALSE),
1073 HOWTO (R_NIOS2_GOTOFF_LO,
1077 FALSE,
1079 complain_overflow_dont,
1080 bfd_elf_generic_reloc,
1081 "R_NIOS2_GOTOFF_LO",
1082 FALSE,
1083 0xffff0000,
1084 0xffff0000,
1085 FALSE),
1087 HOWTO (R_NIOS2_GOTOFF_HA,
1091 FALSE,
1093 complain_overflow_dont,
1094 bfd_elf_generic_reloc,
1095 "R_NIOS2_GOTOFF_HA",
1096 FALSE,
1097 0xffff0000,
1098 0xffff0000,
1099 FALSE),
1101 HOWTO (R_NIOS2_PCREL_LO,
1105 TRUE,
1107 complain_overflow_dont,
1108 nios2_elf32_pcrel_lo16_relocate,
1109 "R_NIOS2_PCREL_LO",
1110 FALSE,
1111 0xffff0000,
1112 0xffff0000,
1113 TRUE),
1115 HOWTO (R_NIOS2_PCREL_HA,
1119 FALSE, /* This is a PC-relative relocation, but we need to subtract
1120 PC ourselves before the HIADJ. */
1122 complain_overflow_dont,
1123 nios2_elf32_pcrel_hiadj16_relocate,
1124 "R_NIOS2_PCREL_HA",
1125 FALSE,
1126 0xffff0000,
1127 0xffff0000,
1128 TRUE),
1130 HOWTO (R_NIOS2_TLS_GD16,
1134 FALSE,
1136 complain_overflow_bitfield,
1137 bfd_elf_generic_reloc,
1138 "R_NIOS2_TLS_GD16",
1139 FALSE,
1140 0xffff0000,
1141 0xffff0000,
1142 FALSE),
1144 HOWTO (R_NIOS2_TLS_LDM16,
1148 FALSE,
1150 complain_overflow_bitfield,
1151 bfd_elf_generic_reloc,
1152 "R_NIOS2_TLS_LDM16",
1153 FALSE,
1154 0xffff0000,
1155 0xffff0000,
1156 FALSE),
1158 HOWTO (R_NIOS2_TLS_LDO16,
1162 FALSE,
1164 complain_overflow_bitfield,
1165 bfd_elf_generic_reloc,
1166 "R_NIOS2_TLS_LDO16",
1167 FALSE,
1168 0xffff0000,
1169 0xffff0000,
1170 FALSE),
1172 HOWTO (R_NIOS2_TLS_IE16,
1176 FALSE,
1178 complain_overflow_bitfield,
1179 bfd_elf_generic_reloc,
1180 "R_NIOS2_TLS_IE16",
1181 FALSE,
1182 0xffff0000,
1183 0xffff0000,
1184 FALSE),
1186 HOWTO (R_NIOS2_TLS_LE16,
1190 FALSE,
1192 complain_overflow_bitfield,
1193 bfd_elf_generic_reloc,
1194 "R_NIOS2_TLS_LE16",
1195 FALSE,
1196 0xffff0000,
1197 0xffff0000,
1198 FALSE),
1200 HOWTO (R_NIOS2_TLS_DTPMOD,
1204 FALSE,
1206 complain_overflow_dont,
1207 bfd_elf_generic_reloc,
1208 "R_NIOS2_TLS_DTPMOD",
1209 FALSE,
1210 0xffffffff,
1211 0xffffffff,
1212 FALSE),
1214 HOWTO (R_NIOS2_TLS_DTPREL,
1218 FALSE,
1220 complain_overflow_dont,
1221 bfd_elf_generic_reloc,
1222 "R_NIOS2_TLS_DTPREL",
1223 FALSE,
1224 0xffffffff,
1225 0xffffffff,
1226 FALSE),
1228 HOWTO (R_NIOS2_TLS_TPREL,
1232 FALSE,
1234 complain_overflow_dont,
1235 bfd_elf_generic_reloc,
1236 "R_NIOS2_TLS_TPREL",
1237 FALSE,
1238 0xffffffff,
1239 0xffffffff,
1240 FALSE),
1242 HOWTO (R_NIOS2_COPY,
1246 FALSE,
1248 complain_overflow_dont,
1249 bfd_elf_generic_reloc,
1250 "R_NIOS2_COPY",
1251 FALSE,
1254 FALSE),
1256 HOWTO (R_NIOS2_GLOB_DAT,
1260 FALSE,
1262 complain_overflow_dont,
1263 bfd_elf_generic_reloc,
1264 "R_NIOS2_GLOB_DAT",
1265 FALSE,
1266 0xffffffff,
1267 0xffffffff,
1268 FALSE),
1270 HOWTO (R_NIOS2_JUMP_SLOT,
1274 FALSE,
1276 complain_overflow_dont,
1277 bfd_elf_generic_reloc,
1278 "R_NIOS2_JUMP_SLOT",
1279 FALSE,
1280 0xffffffff,
1281 0xffffffff,
1282 FALSE),
1284 HOWTO (R_NIOS2_RELATIVE,
1288 FALSE,
1290 complain_overflow_dont,
1291 bfd_elf_generic_reloc,
1292 "R_NIOS2_RELATIVE",
1293 FALSE,
1294 0xffffffff,
1295 0xffffffff,
1296 FALSE),
1298 HOWTO (R_NIOS2_GOTOFF,
1302 FALSE,
1304 complain_overflow_dont,
1305 bfd_elf_generic_reloc,
1306 "R_NIOS2_GOTOFF",
1307 FALSE,
1308 0xffffffff,
1309 0xffffffff,
1310 FALSE),
1312 HOWTO (R_NIOS2_CALL26_NOAT, /* type */
1313 2, /* rightshift */
1314 2, /* size (0 = byte, 1 = short, 2 = long) */
1315 26, /* bitsize */
1316 FALSE, /* pc_relative */
1317 6, /* bitpos */
1318 complain_overflow_dont, /* complain on overflow */
1319 nios2_elf32_call26_relocate, /* special function */
1320 "R_NIOS2_CALL26_NOAT", /* name */
1321 FALSE, /* partial_inplace */
1322 0xffffffc0, /* src_mask */
1323 0xffffffc0, /* dst_mask */
1324 FALSE), /* pcrel_offset */
1326 HOWTO (R_NIOS2_GOT_LO,
1330 FALSE,
1332 complain_overflow_dont,
1333 bfd_elf_generic_reloc,
1334 "R_NIOS2_GOT_LO",
1335 FALSE,
1336 0xffff0000,
1337 0xffff0000,
1338 FALSE),
1340 HOWTO (R_NIOS2_GOT_HA,
1344 FALSE,
1346 complain_overflow_dont,
1347 bfd_elf_generic_reloc,
1348 "R_NIOS2_GOT_HA",
1349 FALSE,
1350 0xffff0000,
1351 0xffff0000,
1352 FALSE),
1354 HOWTO (R_NIOS2_CALL_LO,
1358 FALSE,
1360 complain_overflow_dont,
1361 bfd_elf_generic_reloc,
1362 "R_NIOS2_CALL_LO",
1363 FALSE,
1364 0xffff0000,
1365 0xffff0000,
1366 FALSE),
1368 HOWTO (R_NIOS2_CALL_HA,
1372 FALSE,
1374 complain_overflow_dont,
1375 bfd_elf_generic_reloc,
1376 "R_NIOS2_CALL_HA",
1377 FALSE,
1378 0xffff0000,
1379 0xffff0000,
1380 FALSE),
1382 HOWTO (R_NIOS2_R2_S12,
1386 FALSE,
1388 complain_overflow_signed,
1389 bfd_elf_generic_reloc,
1390 "R_NIOS2_R2_S12",
1391 FALSE,
1392 0x0fff0000,
1393 0x0fff0000,
1394 FALSE),
1396 HOWTO (R_NIOS2_R2_I10_1_PCREL,
1400 TRUE,
1402 complain_overflow_signed,
1403 bfd_elf_generic_reloc, /* FIXME? */
1404 "R_NIOS2_R2_I10_1_PCREL",
1405 FALSE,
1406 0xffc0,
1407 0xffc0,
1408 TRUE),
1410 HOWTO (R_NIOS2_R2_T1I7_1_PCREL,
1414 TRUE,
1416 complain_overflow_signed,
1417 bfd_elf_generic_reloc, /* FIXME? */
1418 "R_NIOS2_R2_T1I7_1_PCREL",
1419 FALSE,
1420 0xfe00,
1421 0xfe00,
1422 TRUE),
1424 HOWTO (R_NIOS2_R2_T1I7_2,
1428 FALSE,
1430 complain_overflow_unsigned,
1431 bfd_elf_generic_reloc,
1432 "R_NIOS2_R2_T1I7_2",
1433 FALSE,
1434 0xfe00,
1435 0xfe00,
1436 FALSE),
1438 HOWTO (R_NIOS2_R2_T2I4,
1442 FALSE,
1444 complain_overflow_unsigned,
1445 bfd_elf_generic_reloc,
1446 "R_NIOS2_R2_T2I4",
1447 FALSE,
1448 0xf000,
1449 0xf000,
1450 FALSE),
1452 HOWTO (R_NIOS2_R2_T2I4_1,
1456 FALSE,
1458 complain_overflow_unsigned,
1459 bfd_elf_generic_reloc,
1460 "R_NIOS2_R2_T2I4_1",
1461 FALSE,
1462 0xf000,
1463 0xf000,
1464 FALSE),
1466 HOWTO (R_NIOS2_R2_T2I4_2,
1470 FALSE,
1472 complain_overflow_unsigned,
1473 bfd_elf_generic_reloc,
1474 "R_NIOS2_R2_T2I4_2",
1475 FALSE,
1476 0xf000,
1477 0xf000,
1478 FALSE),
1480 HOWTO (R_NIOS2_R2_X1I7_2,
1484 FALSE,
1486 complain_overflow_unsigned,
1487 bfd_elf_generic_reloc,
1488 "R_NIOS2_R2_X1I7_2",
1489 FALSE,
1490 0x1fc0,
1491 0x1fc0,
1492 FALSE),
1494 HOWTO (R_NIOS2_R2_X2L5,
1498 FALSE,
1500 complain_overflow_unsigned,
1501 bfd_elf_generic_reloc,
1502 "R_NIOS2_R2_X2L5",
1503 FALSE,
1504 0x07c0,
1505 0x07c0,
1506 FALSE),
1508 HOWTO (R_NIOS2_R2_F1I5_2,
1512 FALSE,
1514 complain_overflow_unsigned,
1515 bfd_elf_generic_reloc,
1516 "R_NIOS2_R2_F1L5_2",
1517 FALSE,
1518 0x07c0,
1519 0x07c0,
1520 FALSE),
1522 HOWTO (R_NIOS2_R2_L5I4X1,
1526 FALSE,
1528 complain_overflow_unsigned,
1529 bfd_elf_generic_reloc,
1530 "R_NIOS2_R2_L5I4X1",
1531 FALSE,
1532 0x03c0,
1533 0x03c0,
1534 FALSE),
1536 HOWTO (R_NIOS2_R2_T1X1I6,
1540 FALSE,
1542 complain_overflow_unsigned,
1543 bfd_elf_generic_reloc,
1544 "R_NIOS2_R2_T1X1I6",
1545 FALSE,
1546 0x7e00,
1547 0x7e00,
1548 FALSE),
1550 HOWTO (R_NIOS2_R2_T1X1I6_2,
1554 FALSE,
1556 complain_overflow_unsigned,
1557 bfd_elf_generic_reloc,
1558 "R_NIOS2_R2_T1I1X6_2",
1559 FALSE,
1560 0x7e00,
1561 0x7e00,
1562 FALSE),
1564 /* Add other relocations here. */
1567 static unsigned char elf_code_to_howto_index[R_NIOS2_ILLEGAL + 1];
1570 /* Return true if producing output for a R2 BFD. */
1571 #define BFD_IS_R2(abfd) (bfd_get_mach (abfd) == bfd_mach_nios2r2)
1573 /* Return the howto for relocation RTYPE. */
1574 static reloc_howto_type *
1575 lookup_howto (unsigned int rtype, bfd *abfd)
1577 static int initialized = 0;
1578 int i;
1579 /* R2 relocations are a superset of R1, so use that for the lookup
1580 table. */
1581 int r1_howto_tbl_size = (int) (sizeof (elf_nios2_r1_howto_table_rel)
1582 / sizeof (elf_nios2_r1_howto_table_rel[0]));
1583 int r2_howto_tbl_size = (int) (sizeof (elf_nios2_r2_howto_table_rel)
1584 / sizeof (elf_nios2_r2_howto_table_rel[0]));
1586 if (!initialized)
1588 initialized = 1;
1589 memset (elf_code_to_howto_index, 0xff,
1590 sizeof (elf_code_to_howto_index));
1591 for (i = 0; i < r2_howto_tbl_size; i++)
1593 elf_code_to_howto_index[elf_nios2_r2_howto_table_rel[i].type] = i;
1594 if (i < r1_howto_tbl_size)
1595 BFD_ASSERT (elf_nios2_r2_howto_table_rel[i].type
1596 == elf_nios2_r1_howto_table_rel[i].type);
1600 BFD_ASSERT (rtype <= R_NIOS2_ILLEGAL);
1601 i = elf_code_to_howto_index[rtype];
1602 if (BFD_IS_R2 (abfd))
1604 if (i >= r2_howto_tbl_size)
1605 return 0;
1606 return elf_nios2_r2_howto_table_rel + i;
1608 else
1610 if (i >= r1_howto_tbl_size)
1611 return 0;
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[] = {
1624 {BFD_RELOC_NONE, R_NIOS2_NONE},
1625 {BFD_RELOC_NIOS2_S16, R_NIOS2_S16},
1626 {BFD_RELOC_NIOS2_U16, R_NIOS2_U16},
1627 {BFD_RELOC_16_PCREL, R_NIOS2_PCREL16},
1628 {BFD_RELOC_NIOS2_CALL26, R_NIOS2_CALL26},
1629 {BFD_RELOC_NIOS2_IMM5, R_NIOS2_IMM5},
1630 {BFD_RELOC_NIOS2_CACHE_OPX, R_NIOS2_CACHE_OPX},
1631 {BFD_RELOC_NIOS2_IMM6, R_NIOS2_IMM6},
1632 {BFD_RELOC_NIOS2_IMM8, R_NIOS2_IMM8},
1633 {BFD_RELOC_NIOS2_HI16, R_NIOS2_HI16},
1634 {BFD_RELOC_NIOS2_LO16, R_NIOS2_LO16},
1635 {BFD_RELOC_NIOS2_HIADJ16, R_NIOS2_HIADJ16},
1636 {BFD_RELOC_32, R_NIOS2_BFD_RELOC_32},
1637 {BFD_RELOC_16, R_NIOS2_BFD_RELOC_16},
1638 {BFD_RELOC_8, R_NIOS2_BFD_RELOC_8},
1639 {BFD_RELOC_NIOS2_GPREL, R_NIOS2_GPREL},
1640 {BFD_RELOC_VTABLE_INHERIT, R_NIOS2_GNU_VTINHERIT},
1641 {BFD_RELOC_VTABLE_ENTRY, R_NIOS2_GNU_VTENTRY},
1642 {BFD_RELOC_NIOS2_UJMP, R_NIOS2_UJMP},
1643 {BFD_RELOC_NIOS2_CJMP, R_NIOS2_CJMP},
1644 {BFD_RELOC_NIOS2_CALLR, R_NIOS2_CALLR},
1645 {BFD_RELOC_NIOS2_ALIGN, R_NIOS2_ALIGN},
1646 {BFD_RELOC_NIOS2_GOT16, R_NIOS2_GOT16},
1647 {BFD_RELOC_NIOS2_CALL16, R_NIOS2_CALL16},
1648 {BFD_RELOC_NIOS2_GOTOFF_LO, R_NIOS2_GOTOFF_LO},
1649 {BFD_RELOC_NIOS2_GOTOFF_HA, R_NIOS2_GOTOFF_HA},
1650 {BFD_RELOC_NIOS2_PCREL_LO, R_NIOS2_PCREL_LO},
1651 {BFD_RELOC_NIOS2_PCREL_HA, R_NIOS2_PCREL_HA},
1652 {BFD_RELOC_NIOS2_TLS_GD16, R_NIOS2_TLS_GD16},
1653 {BFD_RELOC_NIOS2_TLS_LDM16, R_NIOS2_TLS_LDM16},
1654 {BFD_RELOC_NIOS2_TLS_LDO16, R_NIOS2_TLS_LDO16},
1655 {BFD_RELOC_NIOS2_TLS_IE16, R_NIOS2_TLS_IE16},
1656 {BFD_RELOC_NIOS2_TLS_LE16, R_NIOS2_TLS_LE16},
1657 {BFD_RELOC_NIOS2_TLS_DTPMOD, R_NIOS2_TLS_DTPMOD},
1658 {BFD_RELOC_NIOS2_TLS_DTPREL, R_NIOS2_TLS_DTPREL},
1659 {BFD_RELOC_NIOS2_TLS_TPREL, R_NIOS2_TLS_TPREL},
1660 {BFD_RELOC_NIOS2_COPY, R_NIOS2_COPY},
1661 {BFD_RELOC_NIOS2_GLOB_DAT, R_NIOS2_GLOB_DAT},
1662 {BFD_RELOC_NIOS2_JUMP_SLOT, R_NIOS2_JUMP_SLOT},
1663 {BFD_RELOC_NIOS2_RELATIVE, R_NIOS2_RELATIVE},
1664 {BFD_RELOC_NIOS2_GOTOFF, R_NIOS2_GOTOFF},
1665 {BFD_RELOC_NIOS2_CALL26_NOAT, R_NIOS2_CALL26_NOAT},
1666 {BFD_RELOC_NIOS2_GOT_LO, R_NIOS2_GOT_LO},
1667 {BFD_RELOC_NIOS2_GOT_HA, R_NIOS2_GOT_HA},
1668 {BFD_RELOC_NIOS2_CALL_LO, R_NIOS2_CALL_LO},
1669 {BFD_RELOC_NIOS2_CALL_HA, R_NIOS2_CALL_HA},
1670 {BFD_RELOC_NIOS2_R2_S12, R_NIOS2_R2_S12},
1671 {BFD_RELOC_NIOS2_R2_I10_1_PCREL, R_NIOS2_R2_I10_1_PCREL},
1672 {BFD_RELOC_NIOS2_R2_T1I7_1_PCREL, R_NIOS2_R2_T1I7_1_PCREL},
1673 {BFD_RELOC_NIOS2_R2_T1I7_2, R_NIOS2_R2_T1I7_2},
1674 {BFD_RELOC_NIOS2_R2_T2I4, R_NIOS2_R2_T2I4},
1675 {BFD_RELOC_NIOS2_R2_T2I4_1, R_NIOS2_R2_T2I4_1},
1676 {BFD_RELOC_NIOS2_R2_T2I4_2, R_NIOS2_R2_T2I4_2},
1677 {BFD_RELOC_NIOS2_R2_X1I7_2, R_NIOS2_R2_X1I7_2},
1678 {BFD_RELOC_NIOS2_R2_X2L5, R_NIOS2_R2_X2L5},
1679 {BFD_RELOC_NIOS2_R2_F1I5_2, R_NIOS2_R2_F1I5_2},
1680 {BFD_RELOC_NIOS2_R2_L5I4X1, R_NIOS2_R2_L5I4X1},
1681 {BFD_RELOC_NIOS2_R2_T1X1I6, R_NIOS2_R2_T1X1I6},
1682 {BFD_RELOC_NIOS2_R2_T1X1I6_2, R_NIOS2_R2_T1X1I6_2},
1685 enum elf32_nios2_stub_type
1687 nios2_stub_call26_before,
1688 nios2_stub_call26_after,
1689 nios2_stub_none
1692 struct elf32_nios2_stub_hash_entry
1694 /* Base hash table entry structure. */
1695 struct bfd_hash_entry bh_root;
1697 /* The stub section. */
1698 asection *stub_sec;
1700 /* Offset within stub_sec of the beginning of this stub. */
1701 bfd_vma stub_offset;
1703 /* Given the symbol's value and its section we can determine its final
1704 value when building the stubs (so the stub knows where to jump. */
1705 bfd_vma target_value;
1706 asection *target_section;
1708 enum elf32_nios2_stub_type stub_type;
1710 /* The symbol table entry, if any, that this was derived from. */
1711 struct elf32_nios2_link_hash_entry *hh;
1713 /* And the reloc addend that this was derived from. */
1714 bfd_vma addend;
1716 /* Where this stub is being called from, or, in the case of combined
1717 stub sections, the first input section in the group. */
1718 asection *id_sec;
1721 #define nios2_stub_hash_entry(ent) \
1722 ((struct elf32_nios2_stub_hash_entry *)(ent))
1724 #define nios2_stub_hash_lookup(table, string, create, copy) \
1725 ((struct elf32_nios2_stub_hash_entry *) \
1726 bfd_hash_lookup ((table), (string), (create), (copy)))
1729 /* The Nios II linker needs to keep track of the number of relocs that it
1730 decides to copy as dynamic relocs in check_relocs for each symbol.
1731 This is so that it can later discard them if they are found to be
1732 unnecessary. We store the information in a field extending the
1733 regular ELF linker hash table. */
1735 struct elf32_nios2_dyn_relocs
1737 struct elf32_nios2_dyn_relocs *next;
1739 /* The input section of the reloc. */
1740 asection *sec;
1742 /* Total number of relocs copied for the input section. */
1743 bfd_size_type count;
1745 /* Number of pc-relative relocs copied for the input section. */
1746 bfd_size_type pc_count;
1749 /* Nios II ELF linker hash entry. */
1751 struct elf32_nios2_link_hash_entry
1753 struct elf_link_hash_entry root;
1755 /* A pointer to the most recently used stub hash entry against this
1756 symbol. */
1757 struct elf32_nios2_stub_hash_entry *hsh_cache;
1759 /* Track dynamic relocs copied for this symbol. */
1760 struct elf32_nios2_dyn_relocs *dyn_relocs;
1762 #define GOT_UNKNOWN 0
1763 #define GOT_NORMAL 1
1764 #define GOT_TLS_GD 2
1765 #define GOT_TLS_IE 4
1766 unsigned char tls_type;
1768 /* We need to detect and take special action for symbols which are only
1769 referenced with %call() and not with %got(). Such symbols do not need
1770 a dynamic GOT reloc in shared objects, only a dynamic PLT reloc. Lazy
1771 linking will not work if the dynamic GOT reloc exists.
1772 To check for this condition efficiently, we compare got_types_used against
1773 CALL_USED, meaning
1774 (got_types_used & (GOT_USED | CALL_USED)) == CALL_USED.
1776 #define GOT_USED 1
1777 #define CALL_USED 2
1778 unsigned char got_types_used;
1781 #define elf32_nios2_hash_entry(ent) \
1782 ((struct elf32_nios2_link_hash_entry *) (ent))
1784 /* Get the Nios II elf linker hash table from a link_info structure. */
1785 #define elf32_nios2_hash_table(info) \
1786 ((struct elf32_nios2_link_hash_table *) ((info)->hash))
1788 /* Nios II ELF linker hash table. */
1789 struct elf32_nios2_link_hash_table
1791 /* The main hash table. */
1792 struct elf_link_hash_table root;
1794 /* The stub hash table. */
1795 struct bfd_hash_table bstab;
1797 /* Linker stub bfd. */
1798 bfd *stub_bfd;
1800 /* Linker call-backs. */
1801 asection * (*add_stub_section) (const char *, asection *, bfd_boolean);
1802 void (*layout_sections_again) (void);
1804 /* Array to keep track of which stub sections have been created, and
1805 information on stub grouping. */
1806 struct map_stub
1808 /* These are the section to which stubs in the group will be
1809 attached. */
1810 asection *first_sec, *last_sec;
1811 /* The stub sections. There might be stubs inserted either before
1812 or after the real section.*/
1813 asection *first_stub_sec, *last_stub_sec;
1814 } *stub_group;
1816 /* Assorted information used by nios2_elf32_size_stubs. */
1817 unsigned int bfd_count;
1818 unsigned int top_index;
1819 asection **input_list;
1820 Elf_Internal_Sym **all_local_syms;
1822 /* Short-cuts to get to dynamic linker sections. */
1823 asection *sbss;
1825 /* GOT pointer symbol _gp_got. */
1826 struct elf_link_hash_entry *h_gp_got;
1828 union {
1829 bfd_signed_vma refcount;
1830 bfd_vma offset;
1831 } tls_ldm_got;
1833 /* Small local sym cache. */
1834 struct sym_cache sym_cache;
1836 bfd_vma res_n_size;
1839 struct nios2_elf32_obj_tdata
1841 struct elf_obj_tdata root;
1843 /* tls_type for each local got entry. */
1844 char *local_got_tls_type;
1846 /* TRUE if TLS GD relocs have been seen for this object. */
1847 bfd_boolean has_tlsgd;
1850 #define elf32_nios2_tdata(abfd) \
1851 ((struct nios2_elf32_obj_tdata *) (abfd)->tdata.any)
1853 #define elf32_nios2_local_got_tls_type(abfd) \
1854 (elf32_nios2_tdata (abfd)->local_got_tls_type)
1856 /* The name of the dynamic interpreter. This is put in the .interp
1857 section. */
1858 #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
1860 /* PLT implementation for position-dependent code. */
1861 static const bfd_vma nios2_plt_entry[] = { /* .PLTn: */
1862 0x03c00034, /* movhi r15, %hiadj(plt_got_slot_address) */
1863 0x7bc00017, /* ldw r15, %lo(plt_got_slot_address)(r15) */
1864 0x7800683a /* jmp r15 */
1867 static const bfd_vma nios2_plt0_entry[] = { /* .PLTresolve */
1868 0x03800034, /* movhi r14, %hiadj(res_0) */
1869 0x73800004, /* addi r14, r14, %lo(res_0) */
1870 0x7b9fc83a, /* sub r15, r15, r14 */
1871 0x03400034, /* movhi r13, %hiadj(_GLOBAL_OFFSET_TABLE_) */
1872 0x6b800017, /* ldw r14, %lo(_GLOBAL_OFFSET_TABLE_+4)(r13) */
1873 0x6b400017, /* ldw r13, %lo(_GLOBAL_OFFSET_TABLE_+8)(r13) */
1874 0x6800683a /* jmp r13 */
1877 /* PLT implementation for position-independent code. */
1878 static const bfd_vma nios2_so_plt_entry[] = { /* .PLTn */
1879 0x03c00034, /* movhi r15, %hiadj(index * 4) */
1880 0x7bc00004, /* addi r15, r15, %lo(index * 4) */
1881 0x00000006 /* br .PLTresolve */
1884 static const bfd_vma nios2_so_plt0_entry[] = { /* .PLTresolve */
1885 0x001ce03a, /* nextpc r14 */
1886 0x03400034, /* movhi r13, %hiadj(_GLOBAL_OFFSET_TABLE_) */
1887 0x6b9b883a, /* add r13, r13, r14 */
1888 0x6b800017, /* ldw r14, %lo(_GLOBAL_OFFSET_TABLE_+4)(r13) */
1889 0x6b400017, /* ldw r13, %lo(_GLOBAL_OFFSET_TABLE_+8)(r13) */
1890 0x6800683a /* jmp r13 */
1893 /* CALL26 stub. */
1894 static const bfd_vma nios2_call26_stub_entry[] = {
1895 0x00400034, /* orhi at, r0, %hiadj(dest) */
1896 0x08400004, /* addi at, at, %lo(dest) */
1897 0x0800683a /* jmp at */
1900 /* Install 16-bit immediate value VALUE at offset OFFSET into section SEC. */
1901 static void
1902 nios2_elf32_install_imm16 (asection *sec, bfd_vma offset, bfd_vma value)
1904 bfd_vma word = bfd_get_32 (sec->owner, sec->contents + offset);
1906 BFD_ASSERT (value <= 0xffff || ((bfd_signed_vma) value) >= -0xffff);
1908 bfd_put_32 (sec->owner, word | ((value & 0xffff) << 6),
1909 sec->contents + offset);
1912 /* Install COUNT 32-bit values DATA starting at offset OFFSET into
1913 section SEC. */
1914 static void
1915 nios2_elf32_install_data (asection *sec, const bfd_vma *data, bfd_vma offset,
1916 int count)
1918 while (count--)
1920 bfd_put_32 (sec->owner, *data, sec->contents + offset);
1921 offset += 4;
1922 ++data;
1926 /* The usual way of loading a 32-bit constant into a Nios II register is to
1927 load the high 16 bits in one instruction and then add the low 16 bits with
1928 a signed add. This means that the high halfword needs to be adjusted to
1929 compensate for the sign bit of the low halfword. This function returns the
1930 adjusted high halfword for a given 32-bit constant. */
1931 static
1932 bfd_vma hiadj (bfd_vma symbol_value)
1934 return ((symbol_value + 0x8000) >> 16) & 0xffff;
1937 /* Implement elf_backend_grok_prstatus:
1938 Support for core dump NOTE sections. */
1939 static bfd_boolean
1940 nios2_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1942 int offset;
1943 size_t size;
1945 switch (note->descsz)
1947 default:
1948 return FALSE;
1950 case 212: /* Linux/Nios II */
1951 /* pr_cursig */
1952 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
1954 /* pr_pid */
1955 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 24);
1957 /* pr_reg */
1958 offset = 72;
1959 size = 136;
1961 break;
1964 /* Make a ".reg/999" section. */
1965 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
1966 size, note->descpos + offset);
1969 /* Implement elf_backend_grok_psinfo. */
1970 static bfd_boolean
1971 nios2_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1973 switch (note->descsz)
1975 default:
1976 return FALSE;
1978 case 124: /* Linux/Nios II elf_prpsinfo */
1979 elf_tdata (abfd)->core->program
1980 = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
1981 elf_tdata (abfd)->core->command
1982 = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
1985 /* Note that for some reason, a spurious space is tacked
1986 onto the end of the args in some (at least one anyway)
1987 implementations, so strip it off if it exists. */
1990 char *command = elf_tdata (abfd)->core->command;
1991 int n = strlen (command);
1993 if (0 < n && command[n - 1] == ' ')
1994 command[n - 1] = '\0';
1997 return TRUE;
2000 /* Assorted hash table functions. */
2002 /* Initialize an entry in the stub hash table. */
2003 static struct bfd_hash_entry *
2004 stub_hash_newfunc (struct bfd_hash_entry *entry,
2005 struct bfd_hash_table *table,
2006 const char *string)
2008 /* Allocate the structure if it has not already been allocated by a
2009 subclass. */
2010 if (entry == NULL)
2012 entry = bfd_hash_allocate (table,
2013 sizeof (struct elf32_nios2_stub_hash_entry));
2014 if (entry == NULL)
2015 return entry;
2018 /* Call the allocation method of the superclass. */
2019 entry = bfd_hash_newfunc (entry, table, string);
2020 if (entry != NULL)
2022 struct elf32_nios2_stub_hash_entry *hsh;
2024 /* Initialize the local fields. */
2025 hsh = (struct elf32_nios2_stub_hash_entry *) entry;
2026 hsh->stub_sec = NULL;
2027 hsh->stub_offset = 0;
2028 hsh->target_value = 0;
2029 hsh->target_section = NULL;
2030 hsh->stub_type = nios2_stub_none;
2031 hsh->hh = NULL;
2032 hsh->id_sec = NULL;
2035 return entry;
2038 /* Create an entry in a Nios II ELF linker hash table. */
2039 static struct bfd_hash_entry *
2040 link_hash_newfunc (struct bfd_hash_entry *entry,
2041 struct bfd_hash_table *table, const char *string)
2043 /* Allocate the structure if it has not already been allocated by a
2044 subclass. */
2045 if (entry == NULL)
2047 entry = bfd_hash_allocate (table,
2048 sizeof (struct elf32_nios2_link_hash_entry));
2049 if (entry == NULL)
2050 return entry;
2053 /* Call the allocation method of the superclass. */
2054 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
2055 if (entry)
2057 struct elf32_nios2_link_hash_entry *eh;
2059 eh = (struct elf32_nios2_link_hash_entry *) entry;
2060 eh->hsh_cache = NULL;
2061 eh->dyn_relocs = NULL;
2062 eh->tls_type = GOT_UNKNOWN;
2063 eh->got_types_used = 0;
2066 return entry;
2069 /* Section name for stubs is the associated section name plus this
2070 string. */
2071 #define STUB_SUFFIX ".stub"
2073 /* Build a name for an entry in the stub hash table. */
2074 static char *
2075 nios2_stub_name (const asection *input_section,
2076 const asection *sym_sec,
2077 const struct elf32_nios2_link_hash_entry *hh,
2078 const Elf_Internal_Rela *rel,
2079 enum elf32_nios2_stub_type stub_type)
2081 char *stub_name;
2082 bfd_size_type len;
2083 char stubpos = (stub_type == nios2_stub_call26_before) ? 'b' : 'a';
2085 if (hh)
2087 len = 8 + 1 + 1 + 1+ strlen (hh->root.root.root.string) + 1 + 8 + 1;
2088 stub_name = bfd_malloc (len);
2089 if (stub_name != NULL)
2091 sprintf (stub_name, "%08x_%c_%s+%x",
2092 input_section->id & 0xffffffff,
2093 stubpos,
2094 hh->root.root.root.string,
2095 (int) rel->r_addend & 0xffffffff);
2098 else
2100 len = 8 + 1 + 1 + 1+ 8 + 1 + 8 + 1 + 8 + 1;
2101 stub_name = bfd_malloc (len);
2102 if (stub_name != NULL)
2104 sprintf (stub_name, "%08x_%c_%x:%x+%x",
2105 input_section->id & 0xffffffff,
2106 stubpos,
2107 sym_sec->id & 0xffffffff,
2108 (int) ELF32_R_SYM (rel->r_info) & 0xffffffff,
2109 (int) rel->r_addend & 0xffffffff);
2112 return stub_name;
2115 /* Look up an entry in the stub hash. Stub entries are cached because
2116 creating the stub name takes a bit of time. */
2117 static struct elf32_nios2_stub_hash_entry *
2118 nios2_get_stub_entry (const asection *input_section,
2119 const asection *sym_sec,
2120 struct elf32_nios2_link_hash_entry *hh,
2121 const Elf_Internal_Rela *rel,
2122 struct elf32_nios2_link_hash_table *htab,
2123 enum elf32_nios2_stub_type stub_type)
2125 struct elf32_nios2_stub_hash_entry *hsh;
2126 const asection *id_sec;
2128 /* If this input section is part of a group of sections sharing one
2129 stub section, then use the id of the first/last section in the group,
2130 depending on the stub section placement relative to the group.
2131 Stub names need to include a section id, as there may well be
2132 more than one stub used to reach say, printf, and we need to
2133 distinguish between them. */
2134 if (stub_type == nios2_stub_call26_before)
2135 id_sec = htab->stub_group[input_section->id].first_sec;
2136 else
2137 id_sec = htab->stub_group[input_section->id].last_sec;
2139 if (hh != NULL && hh->hsh_cache != NULL
2140 && hh->hsh_cache->hh == hh
2141 && hh->hsh_cache->id_sec == id_sec
2142 && hh->hsh_cache->stub_type == stub_type)
2144 hsh = hh->hsh_cache;
2146 else
2148 char *stub_name;
2150 stub_name = nios2_stub_name (id_sec, sym_sec, hh, rel, stub_type);
2151 if (stub_name == NULL)
2152 return NULL;
2154 hsh = nios2_stub_hash_lookup (&htab->bstab,
2155 stub_name, FALSE, FALSE);
2157 if (hh != NULL)
2158 hh->hsh_cache = hsh;
2160 free (stub_name);
2163 return hsh;
2166 /* Add a new stub entry to the stub hash. Not all fields of the new
2167 stub entry are initialised. */
2168 static struct elf32_nios2_stub_hash_entry *
2169 nios2_add_stub (const char *stub_name,
2170 asection *section,
2171 struct elf32_nios2_link_hash_table *htab,
2172 enum elf32_nios2_stub_type stub_type)
2174 asection *link_sec;
2175 asection *stub_sec;
2176 asection **secptr, **linkptr;
2177 struct elf32_nios2_stub_hash_entry *hsh;
2178 bfd_boolean afterp;
2180 if (stub_type == nios2_stub_call26_before)
2182 link_sec = htab->stub_group[section->id].first_sec;
2183 secptr = &(htab->stub_group[section->id].first_stub_sec);
2184 linkptr = &(htab->stub_group[link_sec->id].first_stub_sec);
2185 afterp = FALSE;
2187 else
2189 link_sec = htab->stub_group[section->id].last_sec;
2190 secptr = &(htab->stub_group[section->id].last_stub_sec);
2191 linkptr = &(htab->stub_group[link_sec->id].last_stub_sec);
2192 afterp = TRUE;
2194 stub_sec = *secptr;
2195 if (stub_sec == NULL)
2197 stub_sec = *linkptr;
2198 if (stub_sec == NULL)
2200 size_t namelen;
2201 bfd_size_type len;
2202 char *s_name;
2204 namelen = strlen (link_sec->name);
2205 len = namelen + sizeof (STUB_SUFFIX);
2206 s_name = bfd_alloc (htab->stub_bfd, len);
2207 if (s_name == NULL)
2208 return NULL;
2210 memcpy (s_name, link_sec->name, namelen);
2211 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
2213 stub_sec = (*htab->add_stub_section) (s_name, link_sec, afterp);
2214 if (stub_sec == NULL)
2215 return NULL;
2216 *linkptr = stub_sec;
2218 *secptr = stub_sec;
2221 /* Enter this entry into the linker stub hash table. */
2222 hsh = nios2_stub_hash_lookup (&htab->bstab, stub_name,
2223 TRUE, FALSE);
2224 if (hsh == NULL)
2226 /* xgettext:c-format */
2227 _bfd_error_handler (_("%B: cannot create stub entry %s"),
2228 section->owner,
2229 stub_name);
2230 return NULL;
2233 hsh->stub_sec = stub_sec;
2234 hsh->stub_offset = 0;
2235 hsh->id_sec = link_sec;
2236 return hsh;
2239 /* Set up various things so that we can make a list of input sections
2240 for each output section included in the link. Returns -1 on error,
2241 0 when no stubs will be needed, and 1 on success. */
2243 nios2_elf32_setup_section_lists (bfd *output_bfd, struct bfd_link_info *info)
2245 bfd *input_bfd;
2246 unsigned int bfd_count;
2247 unsigned int top_id, top_index;
2248 asection *section;
2249 asection **input_list, **list;
2250 bfd_size_type amt;
2251 struct elf32_nios2_link_hash_table *htab = elf32_nios2_hash_table (info);
2253 /* Count the number of input BFDs and find the top input section id. */
2254 for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
2255 input_bfd != NULL;
2256 input_bfd = input_bfd->link.next)
2258 bfd_count += 1;
2259 for (section = input_bfd->sections;
2260 section != NULL;
2261 section = section->next)
2263 if (top_id < section->id)
2264 top_id = section->id;
2268 htab->bfd_count = bfd_count;
2270 amt = sizeof (struct map_stub) * (top_id + 1);
2271 htab->stub_group = bfd_zmalloc (amt);
2272 if (htab->stub_group == NULL)
2273 return -1;
2275 /* We can't use output_bfd->section_count here to find the top output
2276 section index as some sections may have been removed, and
2277 strip_excluded_output_sections doesn't renumber the indices. */
2278 for (section = output_bfd->sections, top_index = 0;
2279 section != NULL;
2280 section = section->next)
2282 if (top_index < section->index)
2283 top_index = section->index;
2286 htab->top_index = top_index;
2287 amt = sizeof (asection *) * (top_index + 1);
2288 input_list = bfd_malloc (amt);
2289 htab->input_list = input_list;
2290 if (input_list == NULL)
2291 return -1;
2293 /* For sections we aren't interested in, mark their entries with a
2294 value we can check later. */
2295 list = input_list + top_index;
2297 *list = bfd_abs_section_ptr;
2298 while (list-- != input_list);
2300 for (section = output_bfd->sections;
2301 section != NULL;
2302 section = section->next)
2304 /* FIXME: This is a bit of hack. Currently our .ctors and .dtors
2305 * have PC relative relocs in them but no code flag set. */
2306 if (((section->flags & SEC_CODE) != 0) ||
2307 strcmp(".ctors", section->name) ||
2308 strcmp(".dtors", section->name))
2309 input_list[section->index] = NULL;
2312 return 1;
2315 /* The linker repeatedly calls this function for each input section,
2316 in the order that input sections are linked into output sections.
2317 Build lists of input sections to determine groupings between which
2318 we may insert linker stubs. */
2319 void
2320 nios2_elf32_next_input_section (struct bfd_link_info *info, asection *isec)
2322 struct elf32_nios2_link_hash_table *htab = elf32_nios2_hash_table (info);
2324 if (isec->output_section->index <= htab->top_index)
2326 asection **list = htab->input_list + isec->output_section->index;
2327 if (*list != bfd_abs_section_ptr)
2329 /* Steal the last_sec pointer for our list.
2330 This happens to make the list in reverse order,
2331 which is what we want. */
2332 htab->stub_group[isec->id].last_sec = *list;
2333 *list = isec;
2338 /* Segment mask for CALL26 relocation relaxation. */
2339 #define CALL26_SEGMENT(x) ((x) & 0xf0000000)
2341 /* Fudge factor for approximate maximum size of all stubs that might
2342 be inserted by the linker. This does not actually limit the number
2343 of stubs that might be inserted, and only affects strategy for grouping
2344 and placement of stubs. Perhaps this should be computed based on number
2345 of relocations seen, or be specifiable on the command line. */
2346 #define MAX_STUB_SECTION_SIZE 0xffff
2348 /* See whether we can group stub sections together. Grouping stub
2349 sections may result in fewer stubs. More importantly, we need to
2350 put all .init* and .fini* stubs at the end of the .init or
2351 .fini output sections respectively, because glibc splits the
2352 _init and _fini functions into multiple parts. Putting a stub in
2353 the middle of a function is not a good idea.
2354 Rather than computing groups of a maximum fixed size, for Nios II
2355 CALL26 relaxation it makes more sense to compute the groups based on
2356 sections that fit within a 256MB address segment. Also do not allow
2357 a group to span more than one output section, since different output
2358 sections might correspond to different memory banks on a bare-metal
2359 target, etc. */
2360 static void
2361 group_sections (struct elf32_nios2_link_hash_table *htab)
2363 asection **list = htab->input_list + htab->top_index;
2366 /* The list is in reverse order so we'll search backwards looking
2367 for the first section that begins in the same memory segment,
2368 marking sections along the way to point at the tail for this
2369 group. */
2370 asection *tail = *list;
2371 if (tail == bfd_abs_section_ptr)
2372 continue;
2373 while (tail != NULL)
2375 bfd_vma start = tail->output_section->vma + tail->output_offset;
2376 bfd_vma end = start + tail->size;
2377 bfd_vma segment = CALL26_SEGMENT (end);
2378 asection *prev;
2380 if (segment != CALL26_SEGMENT (start)
2381 || segment != CALL26_SEGMENT (end + MAX_STUB_SECTION_SIZE))
2382 /* This section spans more than one memory segment, or is
2383 close enough to the end of the segment that adding stub
2384 sections before it might cause it to move so that it
2385 spans memory segments, or that stubs added at the end of
2386 this group might overflow into the next memory segment.
2387 Put it in a group by itself to localize the effects. */
2389 prev = htab->stub_group[tail->id].last_sec;
2390 htab->stub_group[tail->id].last_sec = tail;
2391 htab->stub_group[tail->id].first_sec = tail;
2393 else
2394 /* Collect more sections for this group. */
2396 asection *curr, *first;
2397 for (curr = tail; ; curr = prev)
2399 prev = htab->stub_group[curr->id].last_sec;
2400 if (!prev
2401 || tail->output_section != prev->output_section
2402 || (CALL26_SEGMENT (prev->output_section->vma
2403 + prev->output_offset)
2404 != segment))
2405 break;
2407 first = curr;
2408 for (curr = tail; ; curr = prev)
2410 prev = htab->stub_group[curr->id].last_sec;
2411 htab->stub_group[curr->id].last_sec = tail;
2412 htab->stub_group[curr->id].first_sec = first;
2413 if (curr == first)
2414 break;
2418 /* Reset tail for the next group. */
2419 tail = prev;
2422 while (list-- != htab->input_list);
2423 free (htab->input_list);
2426 /* Determine the type of stub needed, if any, for a call. */
2427 static enum elf32_nios2_stub_type
2428 nios2_type_of_stub (asection *input_sec,
2429 const Elf_Internal_Rela *rel,
2430 struct elf32_nios2_link_hash_entry *hh,
2431 struct elf32_nios2_link_hash_table *htab,
2432 bfd_vma destination,
2433 struct bfd_link_info *info ATTRIBUTE_UNUSED)
2435 bfd_vma location, segment, start, end;
2436 asection *s0, *s1, *s;
2438 if (hh != NULL &&
2439 !(hh->root.root.type == bfd_link_hash_defined
2440 || hh->root.root.type == bfd_link_hash_defweak))
2441 return nios2_stub_none;
2443 /* Determine where the call point is. */
2444 location = (input_sec->output_section->vma
2445 + input_sec->output_offset + rel->r_offset);
2446 segment = CALL26_SEGMENT (location);
2448 /* Nios II CALL and JMPI instructions can transfer control to addresses
2449 within the same 256MB segment as the PC. */
2450 if (segment == CALL26_SEGMENT (destination))
2451 return nios2_stub_none;
2453 /* Find the start and end addresses of the stub group. Also account for
2454 any already-created stub sections for this group. Note that for stubs
2455 in the end section, only the first instruction of the last stub
2456 (12 bytes long) needs to be within range. */
2457 s0 = htab->stub_group[input_sec->id].first_sec;
2458 s = htab->stub_group[s0->id].first_stub_sec;
2459 if (s != NULL && s->size > 0)
2460 start = s->output_section->vma + s->output_offset;
2461 else
2462 start = s0->output_section->vma + s0->output_offset;
2464 s1 = htab->stub_group[input_sec->id].last_sec;
2465 s = htab->stub_group[s1->id].last_stub_sec;
2466 if (s != NULL && s->size > 0)
2467 end = s->output_section->vma + s->output_offset + s->size - 8;
2468 else
2469 end = s1->output_section->vma + s1->output_offset + s1->size;
2471 BFD_ASSERT (start < end);
2472 BFD_ASSERT (start <= location);
2473 BFD_ASSERT (location < end);
2475 /* Put stubs at the end of the group unless that is not a valid
2476 location and the beginning of the group is. It might be that
2477 neither the beginning nor end works if we have an input section
2478 so large that it spans multiple segment boundaries. In that
2479 case, punt; the end result will be a relocation overflow error no
2480 matter what we do here.
2482 Note that adding stubs pushes up the addresses of all subsequent
2483 sections, so that stubs allocated on one pass through the
2484 relaxation loop may not be valid on the next pass. (E.g., we may
2485 allocate a stub at the beginning of the section on one pass and
2486 find that the call site has been bumped into the next memory
2487 segment on the next pass.) The important thing to note is that
2488 we never try to reclaim the space allocated to such unused stubs,
2489 so code size and section addresses can only increase with each
2490 iteration. Accounting for the start and end addresses of the
2491 already-created stub sections ensures that when the algorithm
2492 converges, it converges accurately, with the entire appropriate
2493 stub section accessible from the call site and not just the
2494 address at the start or end of the stub group proper. */
2496 if (segment == CALL26_SEGMENT (end))
2497 return nios2_stub_call26_after;
2498 else if (segment == CALL26_SEGMENT (start))
2499 return nios2_stub_call26_before;
2500 else
2501 /* Perhaps this should be a dedicated error code. */
2502 return nios2_stub_none;
2505 static bfd_boolean
2506 nios2_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg ATTRIBUTE_UNUSED)
2508 struct elf32_nios2_stub_hash_entry *hsh
2509 = (struct elf32_nios2_stub_hash_entry *) gen_entry;
2510 asection *stub_sec = hsh->stub_sec;
2511 bfd_vma sym_value;
2513 /* Make a note of the offset within the stubs for this entry. */
2514 hsh->stub_offset = stub_sec->size;
2516 switch (hsh->stub_type)
2518 case nios2_stub_call26_before:
2519 case nios2_stub_call26_after:
2520 /* A call26 stub looks like:
2521 orhi at, %hiadj(dest)
2522 addi at, at, %lo(dest)
2523 jmp at
2524 Note that call/jmpi instructions can't be used in PIC code
2525 so there is no reason for the stub to be PIC, either. */
2526 sym_value = (hsh->target_value
2527 + hsh->target_section->output_offset
2528 + hsh->target_section->output_section->vma
2529 + hsh->addend);
2531 nios2_elf32_install_data (stub_sec, nios2_call26_stub_entry,
2532 hsh->stub_offset, 3);
2533 nios2_elf32_install_imm16 (stub_sec, hsh->stub_offset,
2534 hiadj (sym_value));
2535 nios2_elf32_install_imm16 (stub_sec, hsh->stub_offset + 4,
2536 (sym_value & 0xffff));
2537 stub_sec->size += 12;
2538 break;
2539 default:
2540 BFD_FAIL ();
2541 return FALSE;
2544 return TRUE;
2547 /* As above, but don't actually build the stub. Just bump offset so
2548 we know stub section sizes. */
2549 static bfd_boolean
2550 nios2_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg ATTRIBUTE_UNUSED)
2552 struct elf32_nios2_stub_hash_entry *hsh
2553 = (struct elf32_nios2_stub_hash_entry *) gen_entry;
2555 switch (hsh->stub_type)
2557 case nios2_stub_call26_before:
2558 case nios2_stub_call26_after:
2559 hsh->stub_sec->size += 12;
2560 break;
2561 default:
2562 BFD_FAIL ();
2563 return FALSE;
2565 return TRUE;
2568 /* Read in all local syms for all input bfds.
2569 Returns -1 on error, 0 otherwise. */
2571 static int
2572 get_local_syms (bfd *output_bfd ATTRIBUTE_UNUSED, bfd *input_bfd,
2573 struct bfd_link_info *info)
2575 unsigned int bfd_indx;
2576 Elf_Internal_Sym *local_syms, **all_local_syms;
2577 struct elf32_nios2_link_hash_table *htab = elf32_nios2_hash_table (info);
2579 /* We want to read in symbol extension records only once. To do this
2580 we need to read in the local symbols in parallel and save them for
2581 later use; so hold pointers to the local symbols in an array. */
2582 bfd_size_type amt = sizeof (Elf_Internal_Sym *) * htab->bfd_count;
2583 all_local_syms = bfd_zmalloc (amt);
2584 htab->all_local_syms = all_local_syms;
2585 if (all_local_syms == NULL)
2586 return -1;
2588 /* Walk over all the input BFDs, swapping in local symbols. */
2589 for (bfd_indx = 0;
2590 input_bfd != NULL;
2591 input_bfd = input_bfd->link.next, bfd_indx++)
2593 Elf_Internal_Shdr *symtab_hdr;
2595 /* We'll need the symbol table in a second. */
2596 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2597 if (symtab_hdr->sh_info == 0)
2598 continue;
2600 /* We need an array of the local symbols attached to the input bfd. */
2601 local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
2602 if (local_syms == NULL)
2604 local_syms = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
2605 symtab_hdr->sh_info, 0,
2606 NULL, NULL, NULL);
2607 /* Cache them for elf_link_input_bfd. */
2608 symtab_hdr->contents = (unsigned char *) local_syms;
2610 if (local_syms == NULL)
2611 return -1;
2613 all_local_syms[bfd_indx] = local_syms;
2616 return 0;
2619 /* Determine and set the size of the stub section for a final link. */
2620 bfd_boolean
2621 nios2_elf32_size_stubs (bfd *output_bfd, bfd *stub_bfd,
2622 struct bfd_link_info *info,
2623 asection *(*add_stub_section) (const char *,
2624 asection *, bfd_boolean),
2625 void (*layout_sections_again) (void))
2627 bfd_boolean stub_changed = FALSE;
2628 struct elf32_nios2_link_hash_table *htab = elf32_nios2_hash_table (info);
2630 /* Stash our params away. */
2631 htab->stub_bfd = stub_bfd;
2632 htab->add_stub_section = add_stub_section;
2633 htab->layout_sections_again = layout_sections_again;
2635 /* FIXME: We only compute the section groups once. This could cause
2636 problems if adding a large stub section causes following sections,
2637 or parts of them, to move into another segment. However, this seems
2638 to be consistent with the way other back ends handle this.... */
2639 group_sections (htab);
2641 if (get_local_syms (output_bfd, info->input_bfds, info))
2643 if (htab->all_local_syms)
2644 goto error_ret_free_local;
2645 return FALSE;
2648 while (1)
2650 bfd *input_bfd;
2651 unsigned int bfd_indx;
2652 asection *stub_sec;
2654 for (input_bfd = info->input_bfds, bfd_indx = 0;
2655 input_bfd != NULL;
2656 input_bfd = input_bfd->link.next, bfd_indx++)
2658 Elf_Internal_Shdr *symtab_hdr;
2659 asection *section;
2660 Elf_Internal_Sym *local_syms;
2662 /* We'll need the symbol table in a second. */
2663 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2664 if (symtab_hdr->sh_info == 0)
2665 continue;
2667 local_syms = htab->all_local_syms[bfd_indx];
2669 /* Walk over each section attached to the input bfd. */
2670 for (section = input_bfd->sections;
2671 section != NULL;
2672 section = section->next)
2674 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
2676 /* If there aren't any relocs, then there's nothing more
2677 to do. */
2678 if ((section->flags & SEC_RELOC) == 0
2679 || section->reloc_count == 0)
2680 continue;
2682 /* If this section is a link-once section that will be
2683 discarded, then don't create any stubs. */
2684 if (section->output_section == NULL
2685 || section->output_section->owner != output_bfd)
2686 continue;
2688 /* Get the relocs. */
2689 internal_relocs
2690 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
2691 info->keep_memory);
2692 if (internal_relocs == NULL)
2693 goto error_ret_free_local;
2695 /* Now examine each relocation. */
2696 irela = internal_relocs;
2697 irelaend = irela + section->reloc_count;
2698 for (; irela < irelaend; irela++)
2700 unsigned int r_type, r_indx;
2701 enum elf32_nios2_stub_type stub_type;
2702 struct elf32_nios2_stub_hash_entry *hsh;
2703 asection *sym_sec;
2704 bfd_vma sym_value;
2705 bfd_vma destination;
2706 struct elf32_nios2_link_hash_entry *hh;
2707 char *stub_name;
2708 const asection *id_sec;
2710 r_type = ELF32_R_TYPE (irela->r_info);
2711 r_indx = ELF32_R_SYM (irela->r_info);
2713 if (r_type >= (unsigned int) R_NIOS2_ILLEGAL)
2715 bfd_set_error (bfd_error_bad_value);
2716 error_ret_free_internal:
2717 if (elf_section_data (section)->relocs == NULL)
2718 free (internal_relocs);
2719 goto error_ret_free_local;
2722 /* Only look for stubs on CALL and JMPI instructions. */
2723 if (r_type != (unsigned int) R_NIOS2_CALL26)
2724 continue;
2726 /* Now determine the call target, its name, value,
2727 section. */
2728 sym_sec = NULL;
2729 sym_value = 0;
2730 destination = 0;
2731 hh = NULL;
2732 if (r_indx < symtab_hdr->sh_info)
2734 /* It's a local symbol. */
2735 Elf_Internal_Sym *sym;
2736 Elf_Internal_Shdr *hdr;
2737 unsigned int shndx;
2739 sym = local_syms + r_indx;
2740 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
2741 sym_value = sym->st_value;
2742 shndx = sym->st_shndx;
2743 if (shndx < elf_numsections (input_bfd))
2745 hdr = elf_elfsections (input_bfd)[shndx];
2746 sym_sec = hdr->bfd_section;
2747 destination = (sym_value + irela->r_addend
2748 + sym_sec->output_offset
2749 + sym_sec->output_section->vma);
2752 else
2754 /* It's an external symbol. */
2755 int e_indx;
2757 e_indx = r_indx - symtab_hdr->sh_info;
2758 hh = ((struct elf32_nios2_link_hash_entry *)
2759 elf_sym_hashes (input_bfd)[e_indx]);
2761 while (hh->root.root.type == bfd_link_hash_indirect
2762 || hh->root.root.type == bfd_link_hash_warning)
2763 hh = ((struct elf32_nios2_link_hash_entry *)
2764 hh->root.root.u.i.link);
2766 if (hh->root.root.type == bfd_link_hash_defined
2767 || hh->root.root.type == bfd_link_hash_defweak)
2769 sym_sec = hh->root.root.u.def.section;
2770 sym_value = hh->root.root.u.def.value;
2772 if (sym_sec->output_section != NULL)
2773 destination = (sym_value + irela->r_addend
2774 + sym_sec->output_offset
2775 + sym_sec->output_section->vma);
2776 else
2777 continue;
2779 else if (hh->root.root.type == bfd_link_hash_undefweak)
2781 if (! bfd_link_pic (info))
2782 continue;
2784 else if (hh->root.root.type == bfd_link_hash_undefined)
2786 if (! (info->unresolved_syms_in_objects == RM_IGNORE
2787 && (ELF_ST_VISIBILITY (hh->root.other)
2788 == STV_DEFAULT)))
2789 continue;
2791 else
2793 bfd_set_error (bfd_error_bad_value);
2794 goto error_ret_free_internal;
2798 /* Determine what (if any) linker stub is needed. */
2799 stub_type = nios2_type_of_stub (section, irela, hh, htab,
2800 destination, info);
2801 if (stub_type == nios2_stub_none)
2802 continue;
2804 /* Support for grouping stub sections. */
2805 if (stub_type == nios2_stub_call26_before)
2806 id_sec = htab->stub_group[section->id].first_sec;
2807 else
2808 id_sec = htab->stub_group[section->id].last_sec;
2810 /* Get the name of this stub. */
2811 stub_name = nios2_stub_name (id_sec, sym_sec, hh, irela,
2812 stub_type);
2813 if (!stub_name)
2814 goto error_ret_free_internal;
2816 hsh = nios2_stub_hash_lookup (&htab->bstab,
2817 stub_name,
2818 FALSE, FALSE);
2819 if (hsh != NULL)
2821 /* The proper stub has already been created. */
2822 free (stub_name);
2823 continue;
2826 hsh = nios2_add_stub (stub_name, section, htab, stub_type);
2827 if (hsh == NULL)
2829 free (stub_name);
2830 goto error_ret_free_internal;
2832 hsh->target_value = sym_value;
2833 hsh->target_section = sym_sec;
2834 hsh->stub_type = stub_type;
2835 hsh->hh = hh;
2836 hsh->addend = irela->r_addend;
2837 stub_changed = TRUE;
2840 /* We're done with the internal relocs, free them. */
2841 if (elf_section_data (section)->relocs == NULL)
2842 free (internal_relocs);
2846 if (!stub_changed)
2847 break;
2849 /* OK, we've added some stubs. Find out the new size of the
2850 stub sections. */
2851 for (stub_sec = htab->stub_bfd->sections;
2852 stub_sec != NULL;
2853 stub_sec = stub_sec->next)
2854 stub_sec->size = 0;
2856 bfd_hash_traverse (&htab->bstab, nios2_size_one_stub, htab);
2858 /* Ask the linker to do its stuff. */
2859 (*htab->layout_sections_again) ();
2860 stub_changed = FALSE;
2863 free (htab->all_local_syms);
2864 return TRUE;
2866 error_ret_free_local:
2867 free (htab->all_local_syms);
2868 return FALSE;
2871 /* Build all the stubs associated with the current output file. The
2872 stubs are kept in a hash table attached to the main linker hash
2873 table. This function is called via nios2elf_finish in the linker. */
2874 bfd_boolean
2875 nios2_elf32_build_stubs (struct bfd_link_info *info)
2877 asection *stub_sec;
2878 struct bfd_hash_table *table;
2879 struct elf32_nios2_link_hash_table *htab;
2881 htab = elf32_nios2_hash_table (info);
2883 for (stub_sec = htab->stub_bfd->sections;
2884 stub_sec != NULL;
2885 stub_sec = stub_sec->next)
2886 /* The stub_bfd may contain non-stub sections if it is also the
2887 dynobj. Any such non-stub sections are created with the
2888 SEC_LINKER_CREATED flag set, while stub sections do not
2889 have that flag. Ignore any non-stub sections here. */
2890 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
2892 bfd_size_type size;
2894 /* Allocate memory to hold the linker stubs. */
2895 size = stub_sec->size;
2896 stub_sec->contents = bfd_zalloc (htab->stub_bfd, size);
2897 if (stub_sec->contents == NULL && size != 0)
2898 return FALSE;
2899 stub_sec->size = 0;
2902 /* Build the stubs as directed by the stub hash table. */
2903 table = &htab->bstab;
2904 bfd_hash_traverse (table, nios2_build_one_stub, info);
2906 return TRUE;
2910 #define is_nios2_elf(bfd) \
2911 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
2912 && elf_object_id (bfd) == NIOS2_ELF_DATA)
2914 /* Merge backend specific data from an object file to the output
2915 object file when linking. */
2917 static bfd_boolean
2918 nios2_elf32_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
2920 bfd *obfd = info->output_bfd;
2921 flagword old_flags;
2922 flagword new_flags;
2924 if (!is_nios2_elf (ibfd) || !is_nios2_elf (obfd))
2925 return TRUE;
2927 /* Check if we have the same endianness. */
2928 if (! _bfd_generic_verify_endian_match (ibfd, info))
2929 return FALSE;
2931 new_flags = elf_elfheader (ibfd)->e_flags;
2932 old_flags = elf_elfheader (obfd)->e_flags;
2933 if (!elf_flags_init (obfd))
2935 /* First call, no flags set. */
2936 elf_flags_init (obfd) = TRUE;
2937 elf_elfheader (obfd)->e_flags = new_flags;
2939 switch (new_flags)
2941 default:
2942 case EF_NIOS2_ARCH_R1:
2943 bfd_default_set_arch_mach (obfd, bfd_arch_nios2, bfd_mach_nios2r1);
2944 break;
2945 case EF_NIOS2_ARCH_R2:
2946 if (bfd_big_endian (ibfd))
2948 _bfd_error_handler
2949 (_("error: %B: Big-endian R2 is not supported."), ibfd);
2950 bfd_set_error (bfd_error_bad_value);
2951 return FALSE;
2953 bfd_default_set_arch_mach (obfd, bfd_arch_nios2, bfd_mach_nios2r2);
2954 break;
2958 /* Incompatible flags. */
2959 else if (new_flags != old_flags)
2961 /* So far, the only incompatible flags denote incompatible
2962 architectures. */
2963 _bfd_error_handler
2964 /* xgettext:c-format */
2965 (_("error: %B: Conflicting CPU architectures %d/%d"),
2966 ibfd, new_flags, old_flags);
2967 bfd_set_error (bfd_error_bad_value);
2968 return FALSE;
2971 /* Merge Tag_compatibility attributes and any common GNU ones. */
2972 _bfd_elf_merge_object_attributes (ibfd, info);
2974 return TRUE;
2978 /* Implement bfd_elf32_bfd_reloc_type_lookup:
2979 Given a BFD reloc type, return a howto structure. */
2980 static reloc_howto_type *
2981 nios2_elf32_bfd_reloc_type_lookup (bfd *abfd,
2982 bfd_reloc_code_real_type code)
2984 int i;
2986 for (i = 0;
2987 i < (int) (sizeof (nios2_reloc_map) / sizeof (struct elf_reloc_map));
2988 ++i)
2989 if (nios2_reloc_map[i].bfd_val == code)
2990 return lookup_howto (nios2_reloc_map[i].elf_val, abfd);
2991 return NULL;
2994 /* Implement bfd_elf32_bfd_reloc_name_lookup:
2995 Given a reloc name, return a howto structure. */
2996 static reloc_howto_type *
2997 nios2_elf32_bfd_reloc_name_lookup (bfd *abfd,
2998 const char *r_name)
3000 int i;
3001 reloc_howto_type *howto_tbl;
3002 int howto_tbl_size;
3004 if (BFD_IS_R2 (abfd))
3006 howto_tbl = elf_nios2_r2_howto_table_rel;
3007 howto_tbl_size = (int) (sizeof (elf_nios2_r2_howto_table_rel)
3008 / sizeof (elf_nios2_r2_howto_table_rel[0]));
3010 else
3012 howto_tbl = elf_nios2_r1_howto_table_rel;
3013 howto_tbl_size = (int) (sizeof (elf_nios2_r1_howto_table_rel)
3014 / sizeof (elf_nios2_r1_howto_table_rel[0]));
3017 for (i = 0; i < howto_tbl_size; i++)
3018 if (howto_tbl[i].name && strcasecmp (howto_tbl[i].name, r_name) == 0)
3019 return howto_tbl + i;
3020 return NULL;
3023 /* Implement elf_info_to_howto:
3024 Given a ELF32 relocation, fill in a arelent structure. */
3025 static void
3026 nios2_elf32_info_to_howto (bfd *abfd, arelent *cache_ptr,
3027 Elf_Internal_Rela *dst)
3029 unsigned int r_type;
3031 r_type = ELF32_R_TYPE (dst->r_info);
3032 cache_ptr->howto = lookup_howto (r_type, abfd);
3035 /* Return the base VMA address which should be subtracted from real addresses
3036 when resolving @dtpoff relocation.
3037 This is PT_TLS segment p_vaddr. */
3038 static bfd_vma
3039 dtpoff_base (struct bfd_link_info *info)
3041 /* If tls_sec is NULL, we should have signalled an error already. */
3042 if (elf_hash_table (info)->tls_sec == NULL)
3043 return 0;
3044 return elf_hash_table (info)->tls_sec->vma;
3047 /* Return the relocation value for @tpoff relocation
3048 if STT_TLS virtual address is ADDRESS. */
3049 static bfd_vma
3050 tpoff (struct bfd_link_info *info, bfd_vma address)
3052 struct elf_link_hash_table *htab = elf_hash_table (info);
3054 /* If tls_sec is NULL, we should have signalled an error already. */
3055 if (htab->tls_sec == NULL)
3056 return 0;
3057 return address - htab->tls_sec->vma;
3060 /* Set the GP value for OUTPUT_BFD. Returns FALSE if this is a
3061 dangerous relocation. */
3062 static bfd_boolean
3063 nios2_elf_assign_gp (bfd *output_bfd, bfd_vma *pgp, struct bfd_link_info *info)
3066 bfd_boolean gp_found;
3067 struct bfd_hash_entry *h;
3068 struct bfd_link_hash_entry *lh;
3070 /* If we've already figured out what GP will be, just return it. */
3071 *pgp = _bfd_get_gp_value (output_bfd);
3072 if (*pgp)
3073 return TRUE;
3075 h = bfd_hash_lookup (&info->hash->table, "_gp", FALSE, FALSE);
3076 lh = (struct bfd_link_hash_entry *) h;
3077 lookup:
3078 if (lh)
3080 switch (lh->type)
3082 case bfd_link_hash_undefined:
3083 case bfd_link_hash_undefweak:
3084 case bfd_link_hash_common:
3085 gp_found = FALSE;
3086 break;
3087 case bfd_link_hash_defined:
3088 case bfd_link_hash_defweak:
3089 gp_found = TRUE;
3091 asection *sym_sec = lh->u.def.section;
3092 bfd_vma sym_value = lh->u.def.value;
3094 if (sym_sec->output_section)
3095 sym_value = (sym_value + sym_sec->output_offset
3096 + sym_sec->output_section->vma);
3097 *pgp = sym_value;
3099 break;
3100 case bfd_link_hash_indirect:
3101 case bfd_link_hash_warning:
3102 lh = lh->u.i.link;
3103 /* @@FIXME ignoring warning for now */
3104 goto lookup;
3105 case bfd_link_hash_new:
3106 default:
3107 abort ();
3110 else
3111 gp_found = FALSE;
3113 if (!gp_found)
3115 /* Only get the error once. */
3116 *pgp = 4;
3117 _bfd_set_gp_value (output_bfd, *pgp);
3118 return FALSE;
3121 _bfd_set_gp_value (output_bfd, *pgp);
3123 return TRUE;
3126 /* Retrieve the previously cached _gp pointer, returning bfd_reloc_dangerous
3127 if it's not available as we don't have a link_info pointer available here
3128 to look it up in the output symbol table. We don't need to adjust the
3129 symbol value for an external symbol if we are producing relocatable
3130 output. */
3131 static bfd_reloc_status_type
3132 nios2_elf_final_gp (bfd *output_bfd, asymbol *symbol, bfd_boolean relocatable,
3133 char **error_message, bfd_vma *pgp)
3135 if (bfd_is_und_section (symbol->section) && !relocatable)
3137 *pgp = 0;
3138 return bfd_reloc_undefined;
3141 *pgp = _bfd_get_gp_value (output_bfd);
3142 if (*pgp == 0 && (!relocatable || (symbol->flags & BSF_SECTION_SYM) != 0))
3144 if (relocatable)
3146 /* Make up a value. */
3147 *pgp = symbol->section->output_section->vma + 0x4000;
3148 _bfd_set_gp_value (output_bfd, *pgp);
3150 else
3152 *error_message
3153 = (char *) _("global pointer relative relocation when _gp not defined");
3154 return bfd_reloc_dangerous;
3158 return bfd_reloc_ok;
3161 /* Do the relocations that require special handling. */
3162 static bfd_reloc_status_type
3163 nios2_elf32_do_hi16_relocate (bfd *abfd, reloc_howto_type *howto,
3164 asection *input_section,
3165 bfd_byte *data, bfd_vma offset,
3166 bfd_vma symbol_value, bfd_vma addend)
3168 symbol_value = symbol_value + addend;
3169 addend = 0;
3170 symbol_value = (symbol_value >> 16) & 0xffff;
3171 return _bfd_final_link_relocate (howto, abfd, input_section,
3172 data, offset, symbol_value, addend);
3175 static bfd_reloc_status_type
3176 nios2_elf32_do_lo16_relocate (bfd *abfd, reloc_howto_type *howto,
3177 asection *input_section,
3178 bfd_byte *data, bfd_vma offset,
3179 bfd_vma symbol_value, bfd_vma addend)
3181 symbol_value = symbol_value + addend;
3182 addend = 0;
3183 symbol_value = symbol_value & 0xffff;
3184 return _bfd_final_link_relocate (howto, abfd, input_section,
3185 data, offset, symbol_value, addend);
3188 static bfd_reloc_status_type
3189 nios2_elf32_do_hiadj16_relocate (bfd *abfd, reloc_howto_type *howto,
3190 asection *input_section,
3191 bfd_byte *data, bfd_vma offset,
3192 bfd_vma symbol_value, bfd_vma addend)
3194 symbol_value = symbol_value + addend;
3195 addend = 0;
3196 symbol_value = hiadj(symbol_value);
3197 return _bfd_final_link_relocate (howto, abfd, input_section, data, offset,
3198 symbol_value, addend);
3201 static bfd_reloc_status_type
3202 nios2_elf32_do_pcrel_lo16_relocate (bfd *abfd, reloc_howto_type *howto,
3203 asection *input_section,
3204 bfd_byte *data, bfd_vma offset,
3205 bfd_vma symbol_value, bfd_vma addend)
3207 symbol_value = symbol_value + addend;
3208 addend = 0;
3209 symbol_value = symbol_value & 0xffff;
3210 return _bfd_final_link_relocate (howto, abfd, input_section,
3211 data, offset, symbol_value, addend);
3214 static bfd_reloc_status_type
3215 nios2_elf32_do_pcrel_hiadj16_relocate (bfd *abfd, reloc_howto_type *howto,
3216 asection *input_section,
3217 bfd_byte *data, bfd_vma offset,
3218 bfd_vma symbol_value, bfd_vma addend)
3220 symbol_value = symbol_value + addend;
3221 symbol_value -= (input_section->output_section->vma
3222 + input_section->output_offset);
3223 symbol_value -= offset;
3224 addend = 0;
3225 symbol_value = hiadj(symbol_value);
3226 return _bfd_final_link_relocate (howto, abfd, input_section, data, offset,
3227 symbol_value, addend);
3230 static bfd_reloc_status_type
3231 nios2_elf32_do_pcrel16_relocate (bfd *abfd, reloc_howto_type *howto,
3232 asection *input_section,
3233 bfd_byte *data, bfd_vma offset,
3234 bfd_vma symbol_value, bfd_vma addend)
3236 /* NIOS2 pc relative relocations are relative to the next 32-bit instruction
3237 so we need to subtract 4 before doing a final_link_relocate. */
3238 symbol_value = symbol_value + addend - 4;
3239 addend = 0;
3240 return _bfd_final_link_relocate (howto, abfd, input_section,
3241 data, offset, symbol_value, addend);
3244 static bfd_reloc_status_type
3245 nios2_elf32_do_call26_relocate (bfd *abfd, reloc_howto_type *howto,
3246 asection *input_section,
3247 bfd_byte *data, bfd_vma offset,
3248 bfd_vma symbol_value, bfd_vma addend)
3250 /* Check that the relocation is in the same page as the current address. */
3251 if (CALL26_SEGMENT (symbol_value + addend)
3252 != CALL26_SEGMENT (input_section->output_section->vma
3253 + input_section->output_offset
3254 + offset))
3255 return bfd_reloc_overflow;
3257 /* Check that the target address is correctly aligned on a 4-byte
3258 boundary. */
3259 if ((symbol_value + addend) & 0x3)
3260 return bfd_reloc_overflow;
3262 return _bfd_final_link_relocate (howto, abfd, input_section,
3263 data, offset, symbol_value, addend);
3266 static bfd_reloc_status_type
3267 nios2_elf32_do_gprel_relocate (bfd *abfd, reloc_howto_type *howto,
3268 asection *input_section,
3269 bfd_byte *data, bfd_vma offset,
3270 bfd_vma symbol_value, bfd_vma addend)
3272 /* Because we need the output_bfd, the special handling is done
3273 in nios2_elf32_relocate_section or in nios2_elf32_gprel_relocate. */
3274 return _bfd_final_link_relocate (howto, abfd, input_section,
3275 data, offset, symbol_value, addend);
3278 static bfd_reloc_status_type
3279 nios2_elf32_do_ujmp_relocate (bfd *abfd, reloc_howto_type *howto,
3280 asection *input_section,
3281 bfd_byte *data, bfd_vma offset,
3282 bfd_vma symbol_value, bfd_vma addend)
3284 bfd_vma symbol_lo16, symbol_hi16;
3285 bfd_reloc_status_type r;
3286 symbol_value = symbol_value + addend;
3287 addend = 0;
3288 symbol_hi16 = (symbol_value >> 16) & 0xffff;
3289 symbol_lo16 = symbol_value & 0xffff;
3291 r = _bfd_final_link_relocate (howto, abfd, input_section,
3292 data, offset, symbol_hi16, addend);
3294 if (r == bfd_reloc_ok)
3295 return _bfd_final_link_relocate (howto, abfd, input_section,
3296 data, offset + 4, symbol_lo16, addend);
3298 return r;
3301 static bfd_reloc_status_type
3302 nios2_elf32_do_cjmp_relocate (bfd *abfd, reloc_howto_type *howto,
3303 asection *input_section,
3304 bfd_byte *data, bfd_vma offset,
3305 bfd_vma symbol_value, bfd_vma addend)
3307 bfd_vma symbol_lo16, symbol_hi16;
3308 bfd_reloc_status_type r;
3309 symbol_value = symbol_value + addend;
3310 addend = 0;
3311 symbol_hi16 = (symbol_value >> 16) & 0xffff;
3312 symbol_lo16 = symbol_value & 0xffff;
3314 r = _bfd_final_link_relocate (howto, abfd, input_section,
3315 data, offset, symbol_hi16, addend);
3317 if (r == bfd_reloc_ok)
3318 return _bfd_final_link_relocate (howto, abfd, input_section,
3319 data, offset + 4, symbol_lo16, addend);
3321 return r;
3324 static bfd_reloc_status_type
3325 nios2_elf32_do_callr_relocate (bfd *abfd, reloc_howto_type *howto,
3326 asection *input_section,
3327 bfd_byte *data, bfd_vma offset,
3328 bfd_vma symbol_value, bfd_vma addend)
3330 bfd_vma symbol_lo16, symbol_hi16;
3331 bfd_reloc_status_type r;
3332 symbol_value = symbol_value + addend;
3333 addend = 0;
3334 symbol_hi16 = (symbol_value >> 16) & 0xffff;
3335 symbol_lo16 = symbol_value & 0xffff;
3337 r = _bfd_final_link_relocate (howto, abfd, input_section,
3338 data, offset, symbol_hi16, addend);
3340 if (r == bfd_reloc_ok)
3341 return _bfd_final_link_relocate (howto, abfd, input_section,
3342 data, offset + 4, symbol_lo16, addend);
3344 return r;
3347 /* HOWTO handlers for relocations that require special handling. */
3349 /* This is for relocations used only when relaxing to ensure
3350 changes in size of section don't screw up .align. */
3351 static bfd_reloc_status_type
3352 nios2_elf32_ignore_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
3353 asymbol *symbol ATTRIBUTE_UNUSED,
3354 void *data ATTRIBUTE_UNUSED, asection *input_section,
3355 bfd *output_bfd,
3356 char **error_message ATTRIBUTE_UNUSED)
3358 if (output_bfd != NULL)
3359 reloc_entry->address += input_section->output_offset;
3360 return bfd_reloc_ok;
3363 static bfd_reloc_status_type
3364 nios2_elf32_hi16_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
3365 void *data, asection *input_section,
3366 bfd *output_bfd,
3367 char **error_message ATTRIBUTE_UNUSED)
3369 /* This part is from bfd_elf_generic_reloc. */
3370 if (output_bfd != NULL
3371 && (symbol->flags & BSF_SECTION_SYM) == 0
3372 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
3374 reloc_entry->address += input_section->output_offset;
3375 return bfd_reloc_ok;
3378 if (output_bfd != NULL)
3379 /* FIXME: See bfd_perform_relocation. Is this right? */
3380 return bfd_reloc_continue;
3382 return nios2_elf32_do_hi16_relocate (abfd, reloc_entry->howto,
3383 input_section,
3384 data, reloc_entry->address,
3385 (symbol->value
3386 + symbol->section->output_section->vma
3387 + symbol->section->output_offset),
3388 reloc_entry->addend);
3391 static bfd_reloc_status_type
3392 nios2_elf32_lo16_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
3393 void *data, asection *input_section,
3394 bfd *output_bfd,
3395 char **error_message ATTRIBUTE_UNUSED)
3397 /* This part is from bfd_elf_generic_reloc. */
3398 if (output_bfd != NULL
3399 && (symbol->flags & BSF_SECTION_SYM) == 0
3400 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
3402 reloc_entry->address += input_section->output_offset;
3403 return bfd_reloc_ok;
3406 if (output_bfd != NULL)
3407 /* FIXME: See bfd_perform_relocation. Is this right? */
3408 return bfd_reloc_continue;
3410 return nios2_elf32_do_lo16_relocate (abfd, reloc_entry->howto,
3411 input_section,
3412 data, reloc_entry->address,
3413 (symbol->value
3414 + symbol->section->output_section->vma
3415 + symbol->section->output_offset),
3416 reloc_entry->addend);
3419 static bfd_reloc_status_type
3420 nios2_elf32_hiadj16_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
3421 void *data, asection *input_section,
3422 bfd *output_bfd,
3423 char **error_message ATTRIBUTE_UNUSED)
3425 /* This part is from bfd_elf_generic_reloc. */
3426 if (output_bfd != NULL
3427 && (symbol->flags & BSF_SECTION_SYM) == 0
3428 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
3430 reloc_entry->address += input_section->output_offset;
3431 return bfd_reloc_ok;
3434 if (output_bfd != NULL)
3435 /* FIXME: See bfd_perform_relocation. Is this right? */
3436 return bfd_reloc_continue;
3438 return nios2_elf32_do_hiadj16_relocate (abfd, reloc_entry->howto,
3439 input_section,
3440 data, reloc_entry->address,
3441 (symbol->value
3442 + symbol->section->output_section->vma
3443 + symbol->section->output_offset),
3444 reloc_entry->addend);
3447 static bfd_reloc_status_type
3448 nios2_elf32_pcrel_lo16_relocate (bfd *abfd, arelent *reloc_entry,
3449 asymbol *symbol, void *data,
3450 asection *input_section, bfd *output_bfd,
3451 char **error_message ATTRIBUTE_UNUSED)
3453 /* This part is from bfd_elf_generic_reloc. */
3454 if (output_bfd != NULL
3455 && (symbol->flags & BSF_SECTION_SYM) == 0
3456 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
3458 reloc_entry->address += input_section->output_offset;
3459 return bfd_reloc_ok;
3462 if (output_bfd != NULL)
3463 /* FIXME: See bfd_perform_relocation. Is this right? */
3464 return bfd_reloc_continue;
3466 return nios2_elf32_do_pcrel_lo16_relocate (
3467 abfd, reloc_entry->howto, input_section, data, reloc_entry->address,
3468 (symbol->value + symbol->section->output_section->vma
3469 + symbol->section->output_offset),
3470 reloc_entry->addend);
3473 static bfd_reloc_status_type
3474 nios2_elf32_pcrel_hiadj16_relocate (bfd *abfd, arelent *reloc_entry,
3475 asymbol *symbol, void *data,
3476 asection *input_section, bfd *output_bfd,
3477 char **error_message ATTRIBUTE_UNUSED)
3479 /* This part is from bfd_elf_generic_reloc. */
3480 if (output_bfd != NULL
3481 && (symbol->flags & BSF_SECTION_SYM) == 0
3482 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
3484 reloc_entry->address += input_section->output_offset;
3485 return bfd_reloc_ok;
3488 if (output_bfd != NULL)
3489 /* FIXME: See bfd_perform_relocation. Is this right? */
3490 return bfd_reloc_continue;
3492 return nios2_elf32_do_pcrel_hiadj16_relocate (
3493 abfd, reloc_entry->howto, input_section, data, reloc_entry->address,
3494 (symbol->value + symbol->section->output_section->vma
3495 + symbol->section->output_offset),
3496 reloc_entry->addend);
3499 static bfd_reloc_status_type
3500 nios2_elf32_pcrel16_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
3501 void *data, asection *input_section,
3502 bfd *output_bfd,
3503 char **error_message ATTRIBUTE_UNUSED)
3505 /* This part is from bfd_elf_generic_reloc. */
3506 if (output_bfd != NULL
3507 && (symbol->flags & BSF_SECTION_SYM) == 0
3508 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
3510 reloc_entry->address += input_section->output_offset;
3511 return bfd_reloc_ok;
3514 if (output_bfd != NULL)
3515 /* FIXME: See bfd_perform_relocation. Is this right? */
3516 return bfd_reloc_continue;
3518 return nios2_elf32_do_pcrel16_relocate (abfd, reloc_entry->howto,
3519 input_section,
3520 data, reloc_entry->address,
3521 (symbol->value
3522 + symbol->section->output_section->vma
3523 + symbol->section->output_offset),
3524 reloc_entry->addend);
3527 static bfd_reloc_status_type
3528 nios2_elf32_call26_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
3529 void *data, asection *input_section,
3530 bfd *output_bfd,
3531 char **error_message ATTRIBUTE_UNUSED)
3533 /* This part is from bfd_elf_generic_reloc. */
3534 if (output_bfd != NULL
3535 && (symbol->flags & BSF_SECTION_SYM) == 0
3536 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
3538 reloc_entry->address += input_section->output_offset;
3539 return bfd_reloc_ok;
3542 if (output_bfd != NULL)
3543 /* FIXME: See bfd_perform_relocation. Is this right? */
3544 return bfd_reloc_continue;
3546 return nios2_elf32_do_call26_relocate (abfd, reloc_entry->howto,
3547 input_section,
3548 data, reloc_entry->address,
3549 (symbol->value
3550 + symbol->section->output_section->vma
3551 + symbol->section->output_offset),
3552 reloc_entry->addend);
3555 static bfd_reloc_status_type
3556 nios2_elf32_gprel_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
3557 void *data, asection *input_section,
3558 bfd *output_bfd, char **msg)
3560 bfd_vma relocation;
3561 bfd_vma gp;
3562 bfd_reloc_status_type r;
3565 /* This part is from bfd_elf_generic_reloc. */
3566 if (output_bfd != NULL
3567 && (symbol->flags & BSF_SECTION_SYM) == 0
3568 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
3570 reloc_entry->address += input_section->output_offset;
3571 return bfd_reloc_ok;
3574 if (output_bfd != NULL)
3575 /* FIXME: See bfd_perform_relocation. Is this right? */
3576 return bfd_reloc_continue;
3578 relocation = (symbol->value
3579 + symbol->section->output_section->vma
3580 + symbol->section->output_offset);
3582 /* This assumes we've already cached the _gp symbol. */
3583 r = nios2_elf_final_gp (abfd, symbol, FALSE, msg, &gp);
3584 if (r == bfd_reloc_ok)
3586 relocation = relocation + reloc_entry->addend - gp;
3587 reloc_entry->addend = 0;
3588 if ((signed) relocation < -32768 || (signed) relocation > 32767)
3590 *msg = _("global pointer relative address out of range");
3591 r = bfd_reloc_outofrange;
3593 else
3594 r = nios2_elf32_do_gprel_relocate (abfd, reloc_entry->howto,
3595 input_section,
3596 data, reloc_entry->address,
3597 relocation, reloc_entry->addend);
3600 return r;
3603 static bfd_reloc_status_type
3604 nios2_elf32_ujmp_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
3605 void *data, asection *input_section,
3606 bfd *output_bfd, char **msg ATTRIBUTE_UNUSED)
3608 /* This part is from bfd_elf_generic_reloc. */
3609 if (output_bfd != NULL
3610 && (symbol->flags & BSF_SECTION_SYM) == 0
3611 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
3613 reloc_entry->address += input_section->output_offset;
3614 return bfd_reloc_ok;
3617 if (output_bfd != NULL)
3618 /* FIXME: See bfd_perform_relocation. Is this right? */
3619 return bfd_reloc_continue;
3621 return nios2_elf32_do_ujmp_relocate (abfd, reloc_entry->howto,
3622 input_section,
3623 data, reloc_entry->address,
3624 (symbol->value
3625 + symbol->section->output_section->vma
3626 + symbol->section->output_offset),
3627 reloc_entry->addend);
3630 static bfd_reloc_status_type
3631 nios2_elf32_cjmp_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
3632 void *data, asection *input_section,
3633 bfd *output_bfd, char **msg ATTRIBUTE_UNUSED)
3635 /* This part is from bfd_elf_generic_reloc. */
3636 if (output_bfd != NULL
3637 && (symbol->flags & BSF_SECTION_SYM) == 0
3638 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
3640 reloc_entry->address += input_section->output_offset;
3641 return bfd_reloc_ok;
3644 if (output_bfd != NULL)
3645 /* FIXME: See bfd_perform_relocation. Is this right? */
3646 return bfd_reloc_continue;
3648 return nios2_elf32_do_cjmp_relocate (abfd, reloc_entry->howto,
3649 input_section,
3650 data, reloc_entry->address,
3651 (symbol->value
3652 + symbol->section->output_section->vma
3653 + symbol->section->output_offset),
3654 reloc_entry->addend);
3657 static bfd_reloc_status_type
3658 nios2_elf32_callr_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
3659 void *data, asection *input_section,
3660 bfd *output_bfd, char **msg ATTRIBUTE_UNUSED)
3662 /* This part is from bfd_elf_generic_reloc. */
3663 if (output_bfd != NULL
3664 && (symbol->flags & BSF_SECTION_SYM) == 0
3665 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
3667 reloc_entry->address += input_section->output_offset;
3668 return bfd_reloc_ok;
3671 if (output_bfd != NULL)
3672 /* FIXME: See bfd_perform_relocation. Is this right? */
3673 return bfd_reloc_continue;
3675 return nios2_elf32_do_callr_relocate (abfd, reloc_entry->howto,
3676 input_section,
3677 data, reloc_entry->address,
3678 (symbol->value
3679 + symbol->section->output_section->vma
3680 + symbol->section->output_offset),
3681 reloc_entry->addend);
3685 /* Implement elf_backend_relocate_section. */
3686 static bfd_boolean
3687 nios2_elf32_relocate_section (bfd *output_bfd,
3688 struct bfd_link_info *info,
3689 bfd *input_bfd,
3690 asection *input_section,
3691 bfd_byte *contents,
3692 Elf_Internal_Rela *relocs,
3693 Elf_Internal_Sym *local_syms,
3694 asection **local_sections)
3696 Elf_Internal_Shdr *symtab_hdr;
3697 struct elf_link_hash_entry **sym_hashes;
3698 Elf_Internal_Rela *rel;
3699 Elf_Internal_Rela *relend;
3700 struct elf32_nios2_link_hash_table *htab;
3701 asection *sgot;
3702 asection *splt;
3703 asection *sreloc = NULL;
3704 bfd_vma *local_got_offsets;
3705 bfd_vma got_base;
3707 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3708 sym_hashes = elf_sym_hashes (input_bfd);
3709 relend = relocs + input_section->reloc_count;
3711 htab = elf32_nios2_hash_table (info);
3712 sgot = htab->root.sgot;
3713 splt = htab->root.splt;
3714 local_got_offsets = elf_local_got_offsets (input_bfd);
3716 if (htab->h_gp_got == NULL)
3717 got_base = 0;
3718 else
3719 got_base = htab->h_gp_got->root.u.def.value;
3721 for (rel = relocs; rel < relend; rel++)
3723 reloc_howto_type *howto;
3724 unsigned long r_symndx;
3725 Elf_Internal_Sym *sym;
3726 asection *sec;
3727 struct elf_link_hash_entry *h;
3728 struct elf32_nios2_link_hash_entry *eh;
3729 bfd_vma relocation;
3730 bfd_vma gp;
3731 bfd_reloc_status_type r = bfd_reloc_ok;
3732 const char *name = NULL;
3733 int r_type;
3734 const char *format;
3735 char msgbuf[256];
3736 const char* msg = (const char*) NULL;
3737 bfd_boolean unresolved_reloc;
3738 bfd_vma off;
3739 int use_plt;
3741 r_type = ELF32_R_TYPE (rel->r_info);
3742 r_symndx = ELF32_R_SYM (rel->r_info);
3744 howto = lookup_howto ((unsigned) ELF32_R_TYPE (rel->r_info), output_bfd);
3745 h = NULL;
3746 sym = NULL;
3747 sec = NULL;
3749 if (r_symndx < symtab_hdr->sh_info)
3751 sym = local_syms + r_symndx;
3752 sec = local_sections[r_symndx];
3753 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
3755 else
3757 bfd_boolean warned, ignored;
3759 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
3760 r_symndx, symtab_hdr, sym_hashes,
3761 h, sec, relocation,
3762 unresolved_reloc, warned, ignored);
3765 if (sec && discarded_section (sec))
3766 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
3767 rel, 1, relend, howto, 0, contents);
3769 /* Nothing more to do unless this is a final link. */
3770 if (bfd_link_relocatable (info))
3771 continue;
3773 if (howto)
3775 bfd_boolean resolved_to_zero;
3777 resolved_to_zero = (h != NULL
3778 && UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
3779 switch (howto->type)
3781 case R_NIOS2_HI16:
3782 r = nios2_elf32_do_hi16_relocate (input_bfd, howto,
3783 input_section,
3784 contents, rel->r_offset,
3785 relocation, rel->r_addend);
3786 break;
3787 case R_NIOS2_LO16:
3788 r = nios2_elf32_do_lo16_relocate (input_bfd, howto,
3789 input_section,
3790 contents, rel->r_offset,
3791 relocation, rel->r_addend);
3792 break;
3793 case R_NIOS2_PCREL_LO:
3794 r = nios2_elf32_do_pcrel_lo16_relocate (input_bfd, howto,
3795 input_section,
3796 contents,
3797 rel->r_offset,
3798 relocation,
3799 rel->r_addend);
3800 break;
3801 case R_NIOS2_HIADJ16:
3802 r = nios2_elf32_do_hiadj16_relocate (input_bfd, howto,
3803 input_section, contents,
3804 rel->r_offset, relocation,
3805 rel->r_addend);
3806 break;
3807 case R_NIOS2_PCREL_HA:
3808 r = nios2_elf32_do_pcrel_hiadj16_relocate (input_bfd, howto,
3809 input_section,
3810 contents,
3811 rel->r_offset,
3812 relocation,
3813 rel->r_addend);
3814 break;
3815 case R_NIOS2_PCREL16:
3816 r = nios2_elf32_do_pcrel16_relocate (input_bfd, howto,
3817 input_section, contents,
3818 rel->r_offset, relocation,
3819 rel->r_addend);
3820 break;
3821 case R_NIOS2_GPREL:
3822 /* Turns an absolute address into a gp-relative address. */
3823 if (!nios2_elf_assign_gp (output_bfd, &gp, info))
3825 bfd_vma reloc_address;
3827 if (sec && sec->output_section)
3828 reloc_address = (sec->output_section->vma
3829 + sec->output_offset
3830 + rel->r_offset);
3831 else
3832 reloc_address = 0;
3834 format = _("global pointer relative relocation at address "
3835 "0x%08x when _gp not defined\n");
3836 sprintf (msgbuf, format, reloc_address);
3837 msg = msgbuf;
3838 r = bfd_reloc_dangerous;
3840 else
3842 bfd_vma symbol_address = rel->r_addend + relocation;
3843 relocation = symbol_address - gp;
3844 rel->r_addend = 0;
3845 if (((signed) relocation < -32768
3846 || (signed) relocation > 32767)
3847 && (!h
3848 || h->root.type == bfd_link_hash_defined
3849 || h->root.type == bfd_link_hash_defweak))
3851 if (h)
3852 name = h->root.root.string;
3853 /* xgettext:c-format */
3854 format = _("Unable to reach %s (at 0x%08x) from the "
3855 "global pointer (at 0x%08x) because the "
3856 "offset (%d) is out of the allowed range, "
3857 "-32678 to 32767.\n" );
3858 sprintf (msgbuf, format, name, symbol_address, gp,
3859 (signed)relocation);
3860 msg = msgbuf;
3861 r = bfd_reloc_outofrange;
3863 else
3864 r = _bfd_final_link_relocate (howto, input_bfd,
3865 input_section, contents,
3866 rel->r_offset, relocation,
3867 rel->r_addend);
3869 break;
3870 case R_NIOS2_UJMP:
3871 r = nios2_elf32_do_ujmp_relocate (input_bfd, howto,
3872 input_section,
3873 contents, rel->r_offset,
3874 relocation, rel->r_addend);
3875 break;
3876 case R_NIOS2_CJMP:
3877 r = nios2_elf32_do_cjmp_relocate (input_bfd, howto,
3878 input_section,
3879 contents, rel->r_offset,
3880 relocation, rel->r_addend);
3881 break;
3882 case R_NIOS2_CALLR:
3883 r = nios2_elf32_do_callr_relocate (input_bfd, howto,
3884 input_section, contents,
3885 rel->r_offset, relocation,
3886 rel->r_addend);
3887 break;
3888 case R_NIOS2_CALL26:
3889 case R_NIOS2_CALL26_NOAT:
3890 /* If we have a call to an undefined weak symbol, we just want
3891 to stuff a zero in the bits of the call instruction and
3892 bypass the normal call26 relocation handling, because it'll
3893 diagnose an overflow error if address 0 isn't in the same
3894 256MB segment as the call site. Presumably the call
3895 should be guarded by a null check anyway. */
3896 if (h != NULL && h->root.type == bfd_link_hash_undefweak)
3898 BFD_ASSERT (relocation == 0 && rel->r_addend == 0);
3899 r = _bfd_final_link_relocate (howto, input_bfd,
3900 input_section, contents,
3901 rel->r_offset, relocation,
3902 rel->r_addend);
3903 break;
3905 /* Handle relocations which should use the PLT entry.
3906 NIOS2_BFD_RELOC_32 relocations will use the symbol's value,
3907 which may point to a PLT entry, but we don't need to handle
3908 that here. If we created a PLT entry, all branches in this
3909 object should go to it. */
3910 if (h != NULL && splt != NULL && h->plt.offset != (bfd_vma) -1)
3912 /* If we've created a .plt section, and assigned a PLT entry
3913 to this function, it should not be known to bind locally.
3914 If it were, we would have cleared the PLT entry. */
3915 BFD_ASSERT (!SYMBOL_CALLS_LOCAL (info, h));
3917 relocation = (splt->output_section->vma
3918 + splt->output_offset
3919 + h->plt.offset);
3921 unresolved_reloc = FALSE;
3923 /* Detect R_NIOS2_CALL26 relocations that would overflow the
3924 256MB segment. Replace the target with a reference to a
3925 trampoline instead.
3926 Note that htab->stub_group is null if relaxation has been
3927 disabled by the --no-relax linker command-line option, so
3928 we can use that to skip this processing entirely. */
3929 if (howto->type == R_NIOS2_CALL26 && htab->stub_group)
3931 bfd_vma dest = relocation + rel->r_addend;
3932 enum elf32_nios2_stub_type stub_type;
3934 eh = (struct elf32_nios2_link_hash_entry *)h;
3935 stub_type = nios2_type_of_stub (input_section, rel, eh,
3936 htab, dest, NULL);
3938 if (stub_type != nios2_stub_none)
3940 struct elf32_nios2_stub_hash_entry *hsh;
3942 hsh = nios2_get_stub_entry (input_section, sec,
3943 eh, rel, htab, stub_type);
3944 if (hsh == NULL)
3946 r = bfd_reloc_undefined;
3947 break;
3950 dest = (hsh->stub_offset
3951 + hsh->stub_sec->output_offset
3952 + hsh->stub_sec->output_section->vma);
3953 r = nios2_elf32_do_call26_relocate (input_bfd, howto,
3954 input_section,
3955 contents,
3956 rel->r_offset,
3957 dest, 0);
3958 break;
3962 /* Normal case. */
3963 r = nios2_elf32_do_call26_relocate (input_bfd, howto,
3964 input_section, contents,
3965 rel->r_offset, relocation,
3966 rel->r_addend);
3967 break;
3968 case R_NIOS2_ALIGN:
3969 r = bfd_reloc_ok;
3970 /* For symmetry this would be
3971 r = nios2_elf32_do_ignore_reloc (input_bfd, howto,
3972 input_section, contents,
3973 rel->r_offset, relocation,
3974 rel->r_addend);
3975 but do_ignore_reloc would do no more than return
3976 bfd_reloc_ok. */
3977 break;
3979 case R_NIOS2_GOT16:
3980 case R_NIOS2_CALL16:
3981 case R_NIOS2_GOT_LO:
3982 case R_NIOS2_GOT_HA:
3983 case R_NIOS2_CALL_LO:
3984 case R_NIOS2_CALL_HA:
3985 /* Relocation is to the entry for this symbol in the
3986 global offset table. */
3987 if (sgot == NULL)
3989 r = bfd_reloc_notsupported;
3990 break;
3993 use_plt = 0;
3995 if (h != NULL)
3997 bfd_boolean dyn;
3999 eh = (struct elf32_nios2_link_hash_entry *)h;
4000 use_plt = (eh->got_types_used == CALL_USED
4001 && h->plt.offset != (bfd_vma) -1);
4003 off = h->got.offset;
4004 BFD_ASSERT (off != (bfd_vma) -1);
4005 dyn = htab->root.dynamic_sections_created;
4006 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
4007 bfd_link_pic (info),
4009 || (bfd_link_pic (info)
4010 && SYMBOL_REFERENCES_LOCAL (info, h))
4011 || ((ELF_ST_VISIBILITY (h->other)
4012 || resolved_to_zero)
4013 && h->root.type == bfd_link_hash_undefweak))
4015 /* This is actually a static link, or it is a -Bsymbolic
4016 link and the symbol is defined locally. We must
4017 initialize this entry in the global offset table.
4018 Since the offset must always be a multiple of 4, we
4019 use the least significant bit to record whether we
4020 have initialized it already.
4022 When doing a dynamic link, we create a .rela.got
4023 relocation entry to initialize the value. This is
4024 done in the finish_dynamic_symbol routine. */
4025 if ((off & 1) != 0)
4026 off &= ~1;
4027 else
4029 bfd_put_32 (output_bfd, relocation,
4030 sgot->contents + off);
4031 h->got.offset |= 1;
4034 else
4035 unresolved_reloc = FALSE;
4037 else
4039 BFD_ASSERT (local_got_offsets != NULL
4040 && local_got_offsets[r_symndx] != (bfd_vma) -1);
4042 off = local_got_offsets[r_symndx];
4044 /* The offset must always be a multiple of 4. We use the
4045 least significant bit to record whether we have already
4046 generated the necessary reloc. */
4047 if ((off & 1) != 0)
4048 off &= ~1;
4049 else
4051 bfd_put_32 (output_bfd, relocation,
4052 sgot->contents + off);
4054 if (bfd_link_pic (info))
4056 asection *srelgot;
4057 Elf_Internal_Rela outrel;
4058 bfd_byte *loc;
4060 srelgot = htab->root.srelgot;
4061 BFD_ASSERT (srelgot != NULL);
4063 outrel.r_addend = relocation;
4064 outrel.r_offset = (sgot->output_section->vma
4065 + sgot->output_offset
4066 + off);
4067 outrel.r_info = ELF32_R_INFO (0, R_NIOS2_RELATIVE);
4068 loc = srelgot->contents;
4069 loc += (srelgot->reloc_count++ *
4070 sizeof (Elf32_External_Rela));
4071 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4074 local_got_offsets[r_symndx] |= 1;
4078 if (use_plt && bfd_link_pic (info))
4080 off = ((h->plt.offset - 24) / 12 + 3) * 4;
4081 relocation = (htab->root.sgotplt->output_offset + off
4082 - got_base);
4084 else
4085 relocation = sgot->output_offset + off - got_base;
4087 /* This relocation does not use the addend. */
4088 rel->r_addend = 0;
4090 switch (howto->type)
4092 case R_NIOS2_GOT_LO:
4093 case R_NIOS2_CALL_LO:
4094 r = nios2_elf32_do_lo16_relocate (input_bfd, howto,
4095 input_section, contents,
4096 rel->r_offset, relocation,
4097 rel->r_addend);
4098 break;
4099 case R_NIOS2_GOT_HA:
4100 case R_NIOS2_CALL_HA:
4101 r = nios2_elf32_do_hiadj16_relocate (input_bfd, howto,
4102 input_section, contents,
4103 rel->r_offset,
4104 relocation,
4105 rel->r_addend);
4106 break;
4107 default:
4108 r = _bfd_final_link_relocate (howto, input_bfd,
4109 input_section, contents,
4110 rel->r_offset, relocation,
4111 rel->r_addend);
4112 break;
4114 break;
4116 case R_NIOS2_GOTOFF_LO:
4117 case R_NIOS2_GOTOFF_HA:
4118 case R_NIOS2_GOTOFF:
4119 /* Relocation is relative to the global offset table pointer. */
4121 BFD_ASSERT (sgot != NULL);
4122 if (sgot == NULL)
4124 r = bfd_reloc_notsupported;
4125 break;
4128 /* Note that sgot->output_offset is not involved in this
4129 calculation. We always want the start of .got. */
4130 relocation -= sgot->output_section->vma;
4132 /* Now we adjust the relocation to be relative to the GOT pointer
4133 (the _gp_got symbol), which possibly contains the 0x8000 bias. */
4134 relocation -= got_base;
4136 switch (howto->type)
4138 case R_NIOS2_GOTOFF_LO:
4139 r = nios2_elf32_do_lo16_relocate (input_bfd, howto,
4140 input_section, contents,
4141 rel->r_offset, relocation,
4142 rel->r_addend);
4143 break;
4144 case R_NIOS2_GOTOFF_HA:
4145 r = nios2_elf32_do_hiadj16_relocate (input_bfd, howto,
4146 input_section, contents,
4147 rel->r_offset,
4148 relocation,
4149 rel->r_addend);
4150 break;
4151 default:
4152 r = _bfd_final_link_relocate (howto, input_bfd,
4153 input_section, contents,
4154 rel->r_offset, relocation,
4155 rel->r_addend);
4156 break;
4158 break;
4160 case R_NIOS2_TLS_LDO16:
4161 relocation -= dtpoff_base (info) + DTP_OFFSET;
4163 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
4164 contents, rel->r_offset,
4165 relocation, rel->r_addend);
4166 break;
4167 case R_NIOS2_TLS_LDM16:
4168 if (htab->root.sgot == NULL)
4169 abort ();
4171 off = htab->tls_ldm_got.offset;
4173 if ((off & 1) != 0)
4174 off &= ~1;
4175 else
4177 /* If we don't know the module number, create a relocation
4178 for it. */
4179 if (bfd_link_pic (info))
4181 Elf_Internal_Rela outrel;
4182 bfd_byte *loc;
4184 if (htab->root.srelgot == NULL)
4185 abort ();
4187 outrel.r_addend = 0;
4188 outrel.r_offset = (htab->root.sgot->output_section->vma
4189 + htab->root.sgot->output_offset
4190 + off);
4191 outrel.r_info = ELF32_R_INFO (0, R_NIOS2_TLS_DTPMOD);
4193 loc = htab->root.srelgot->contents;
4194 loc += (htab->root.srelgot->reloc_count++
4195 * sizeof (Elf32_External_Rela));
4196 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4198 else
4199 bfd_put_32 (output_bfd, 1,
4200 htab->root.sgot->contents + off);
4202 htab->tls_ldm_got.offset |= 1;
4205 relocation = htab->root.sgot->output_offset + off - got_base;
4207 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
4208 contents, rel->r_offset,
4209 relocation, rel->r_addend);
4211 break;
4212 case R_NIOS2_TLS_GD16:
4213 case R_NIOS2_TLS_IE16:
4215 int indx;
4216 char tls_type;
4218 if (htab->root.sgot == NULL)
4219 abort ();
4221 indx = 0;
4222 if (h != NULL)
4224 bfd_boolean dyn;
4225 dyn = htab->root.dynamic_sections_created;
4226 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
4227 bfd_link_pic (info),
4229 && (!bfd_link_pic (info)
4230 || !SYMBOL_REFERENCES_LOCAL (info, h)))
4232 unresolved_reloc = FALSE;
4233 indx = h->dynindx;
4235 off = h->got.offset;
4236 tls_type = (((struct elf32_nios2_link_hash_entry *) h)
4237 ->tls_type);
4239 else
4241 if (local_got_offsets == NULL)
4242 abort ();
4243 off = local_got_offsets[r_symndx];
4244 tls_type = (elf32_nios2_local_got_tls_type (input_bfd)
4245 [r_symndx]);
4248 if (tls_type == GOT_UNKNOWN)
4249 abort ();
4251 if ((off & 1) != 0)
4252 off &= ~1;
4253 else
4255 bfd_boolean need_relocs = FALSE;
4256 Elf_Internal_Rela outrel;
4257 bfd_byte *loc = NULL;
4258 int cur_off = off;
4260 /* The GOT entries have not been initialized yet. Do it
4261 now, and emit any relocations. If both an IE GOT and a
4262 GD GOT are necessary, we emit the GD first. */
4264 if ((bfd_link_pic (info) || indx != 0)
4265 && (h == NULL
4266 || (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
4267 && !resolved_to_zero)
4268 || h->root.type != bfd_link_hash_undefweak))
4270 need_relocs = TRUE;
4271 if (htab->root.srelgot == NULL)
4272 abort ();
4273 loc = htab->root.srelgot->contents;
4274 loc += (htab->root.srelgot->reloc_count *
4275 sizeof (Elf32_External_Rela));
4278 if (tls_type & GOT_TLS_GD)
4280 if (need_relocs)
4282 outrel.r_addend = 0;
4283 outrel.r_offset = (htab->root.sgot->output_section->vma
4284 + htab->root.sgot->output_offset
4285 + cur_off);
4286 outrel.r_info = ELF32_R_INFO (indx,
4287 R_NIOS2_TLS_DTPMOD);
4289 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
4290 loc);
4291 htab->root.srelgot->reloc_count++;
4292 loc += sizeof (Elf32_External_Rela);
4294 if (indx == 0)
4295 bfd_put_32 (output_bfd,
4296 (relocation - dtpoff_base (info) -
4297 DTP_OFFSET),
4298 htab->root.sgot->contents + cur_off + 4);
4299 else
4301 outrel.r_addend = 0;
4302 outrel.r_info = ELF32_R_INFO (indx,
4303 R_NIOS2_TLS_DTPREL);
4304 outrel.r_offset += 4;
4306 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
4307 loc);
4308 htab->root.srelgot->reloc_count++;
4309 loc += sizeof (Elf32_External_Rela);
4312 else
4314 /* If we are not emitting relocations for a
4315 general dynamic reference, then we must be in a
4316 static link or an executable link with the
4317 symbol binding locally. Mark it as belonging
4318 to module 1, the executable. */
4319 bfd_put_32 (output_bfd, 1,
4320 htab->root.sgot->contents + cur_off);
4321 bfd_put_32 (output_bfd, (relocation -
4322 dtpoff_base (info) -
4323 DTP_OFFSET),
4324 htab->root.sgot->contents + cur_off + 4);
4327 cur_off += 8;
4330 if (tls_type & GOT_TLS_IE)
4332 if (need_relocs)
4334 if (indx == 0)
4335 outrel.r_addend = (relocation -
4336 dtpoff_base (info));
4337 else
4338 outrel.r_addend = 0;
4339 outrel.r_offset = (htab->root.sgot->output_section->vma
4340 + htab->root.sgot->output_offset
4341 + cur_off);
4342 outrel.r_info = ELF32_R_INFO (indx,
4343 R_NIOS2_TLS_TPREL);
4345 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
4346 loc);
4347 htab->root.srelgot->reloc_count++;
4348 loc += sizeof (Elf32_External_Rela);
4350 else
4351 bfd_put_32 (output_bfd, (tpoff (info, relocation)
4352 - TP_OFFSET),
4353 htab->root.sgot->contents + cur_off);
4354 cur_off += 4;
4357 if (h != NULL)
4358 h->got.offset |= 1;
4359 else
4360 local_got_offsets[r_symndx] |= 1;
4363 if ((tls_type & GOT_TLS_GD) && r_type != R_NIOS2_TLS_GD16)
4364 off += 8;
4365 relocation = htab->root.sgot->output_offset + off - got_base;
4367 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
4368 contents, rel->r_offset,
4369 relocation, rel->r_addend);
4372 break;
4373 case R_NIOS2_TLS_LE16:
4374 if (bfd_link_dll (info))
4376 _bfd_error_handler
4377 /* xgettext:c-format */
4378 (_("%B(%A+%#Lx): %s relocation not "
4379 "permitted in shared object"),
4380 input_bfd, input_section,
4381 rel->r_offset, howto->name);
4382 return FALSE;
4384 else
4385 relocation = tpoff (info, relocation) - TP_OFFSET;
4387 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
4388 contents, rel->r_offset,
4389 relocation, rel->r_addend);
4390 break;
4392 case R_NIOS2_BFD_RELOC_32:
4393 if (bfd_link_pic (info)
4394 && (input_section->flags & SEC_ALLOC) != 0
4395 && (h == NULL
4396 || (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
4397 && !resolved_to_zero)
4398 || h->root.type != bfd_link_hash_undefweak))
4400 Elf_Internal_Rela outrel;
4401 bfd_byte *loc;
4402 bfd_boolean skip, relocate;
4404 /* When generating a shared object, these relocations
4405 are copied into the output file to be resolved at run
4406 time. */
4408 skip = FALSE;
4409 relocate = FALSE;
4411 outrel.r_offset
4412 = _bfd_elf_section_offset (output_bfd, info,
4413 input_section, rel->r_offset);
4414 if (outrel.r_offset == (bfd_vma) -1)
4415 skip = TRUE;
4416 else if (outrel.r_offset == (bfd_vma) -2)
4417 skip = TRUE, relocate = TRUE;
4418 outrel.r_offset += (input_section->output_section->vma
4419 + input_section->output_offset);
4421 if (skip)
4422 memset (&outrel, 0, sizeof outrel);
4423 else if (h != NULL
4424 && h->dynindx != -1
4425 && (!bfd_link_pic (info)
4426 || !SYMBOLIC_BIND (info, h)
4427 || !h->def_regular))
4429 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
4430 outrel.r_addend = rel->r_addend;
4432 else
4434 /* This symbol is local, or marked to become local. */
4435 outrel.r_addend = relocation + rel->r_addend;
4436 relocate = TRUE;
4437 outrel.r_info = ELF32_R_INFO (0, R_NIOS2_RELATIVE);
4440 sreloc = elf_section_data (input_section)->sreloc;
4441 if (sreloc == NULL)
4442 abort ();
4444 loc = sreloc->contents;
4445 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
4446 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4448 /* This reloc will be computed at runtime, so there's no
4449 need to do anything now, except for R_NIOS2_BFD_RELOC_32
4450 relocations that have been turned into
4451 R_NIOS2_RELATIVE. */
4452 if (!relocate)
4453 break;
4456 r = _bfd_final_link_relocate (howto, input_bfd,
4457 input_section, contents,
4458 rel->r_offset, relocation,
4459 rel->r_addend);
4460 break;
4462 case R_NIOS2_TLS_DTPREL:
4463 relocation -= dtpoff_base (info);
4464 /* Fall through. */
4466 default:
4467 r = _bfd_final_link_relocate (howto, input_bfd,
4468 input_section, contents,
4469 rel->r_offset, relocation,
4470 rel->r_addend);
4471 break;
4474 else
4475 r = bfd_reloc_notsupported;
4477 if (r != bfd_reloc_ok)
4479 if (h != NULL)
4480 name = h->root.root.string;
4481 else
4483 name = bfd_elf_string_from_elf_section (input_bfd,
4484 symtab_hdr->sh_link,
4485 sym->st_name);
4486 if (name == NULL || *name == '\0')
4487 name = bfd_section_name (input_bfd, sec);
4490 switch (r)
4492 case bfd_reloc_overflow:
4493 (*info->callbacks->reloc_overflow) (info, NULL, name,
4494 howto->name, (bfd_vma) 0,
4495 input_bfd, input_section,
4496 rel->r_offset);
4497 break;
4499 case bfd_reloc_undefined:
4500 (*info->callbacks->undefined_symbol) (info, name, input_bfd,
4501 input_section,
4502 rel->r_offset, TRUE);
4503 break;
4505 case bfd_reloc_outofrange:
4506 if (msg == NULL)
4507 msg = _("relocation out of range");
4508 break;
4510 case bfd_reloc_notsupported:
4511 if (msg == NULL)
4512 msg = _("unsupported relocation");
4513 break;
4515 case bfd_reloc_dangerous:
4516 if (msg == NULL)
4517 msg = _("dangerous relocation");
4518 break;
4520 default:
4521 if (msg == NULL)
4522 msg = _("unknown error");
4523 break;
4526 if (msg)
4528 (*info->callbacks->warning) (info, msg, name, input_bfd,
4529 input_section, rel->r_offset);
4530 return FALSE;
4534 return TRUE;
4537 /* Implement elf-backend_section_flags:
4538 Convert NIOS2 specific section flags to bfd internal section flags. */
4539 static bfd_boolean
4540 nios2_elf32_section_flags (flagword *flags, const Elf_Internal_Shdr *hdr)
4542 if (hdr->sh_flags & SHF_NIOS2_GPREL)
4543 *flags |= SEC_SMALL_DATA;
4545 return TRUE;
4548 /* Implement elf_backend_fake_sections:
4549 Set the correct type for an NIOS2 ELF section. We do this by the
4550 section name, which is a hack, but ought to work. */
4551 static bfd_boolean
4552 nios2_elf32_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
4553 Elf_Internal_Shdr *hdr, asection *sec)
4555 register const char *name = bfd_get_section_name (abfd, sec);
4557 if ((sec->flags & SEC_SMALL_DATA)
4558 || strcmp (name, ".sdata") == 0
4559 || strcmp (name, ".sbss") == 0
4560 || strcmp (name, ".lit4") == 0 || strcmp (name, ".lit8") == 0)
4561 hdr->sh_flags |= SHF_NIOS2_GPREL;
4563 return TRUE;
4566 /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
4567 shortcuts to them in our hash table. */
4568 static bfd_boolean
4569 create_got_section (bfd *dynobj, struct bfd_link_info *info)
4571 struct elf32_nios2_link_hash_table *htab;
4572 struct elf_link_hash_entry *h;
4574 htab = elf32_nios2_hash_table (info);
4576 if (! _bfd_elf_create_got_section (dynobj, info))
4577 return FALSE;
4579 /* In order for the two loads in .PLTresolve to share the same %hiadj,
4580 _GLOBAL_OFFSET_TABLE_ must be aligned to a 16-byte boundary. */
4581 if (!bfd_set_section_alignment (dynobj, htab->root.sgotplt, 4))
4582 return FALSE;
4584 /* The Nios II ABI specifies that GOT-relative relocations are relative
4585 to the linker-created symbol _gp_got, rather than using
4586 _GLOBAL_OFFSET_TABLE_ directly. In particular, the latter always
4587 points to the base of the GOT while _gp_got may include a bias. */
4588 h = _bfd_elf_define_linkage_sym (dynobj, info, htab->root.sgotplt,
4589 "_gp_got");
4590 htab->h_gp_got = h;
4591 if (h == NULL)
4592 return FALSE;
4594 return TRUE;
4597 /* Implement elf_backend_create_dynamic_sections:
4598 Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
4599 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
4600 hash table. */
4601 static bfd_boolean
4602 nios2_elf32_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
4604 struct elf32_nios2_link_hash_table *htab;
4606 htab = elf32_nios2_hash_table (info);
4607 if (!htab->root.sgot && !create_got_section (dynobj, info))
4608 return FALSE;
4610 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
4611 return FALSE;
4613 /* In order for the two loads in a shared object .PLTresolve to share the
4614 same %hiadj, the start of the PLT (as well as the GOT) must be aligned
4615 to a 16-byte boundary. This is because the addresses for these loads
4616 include the -(.plt+4) PIC correction. */
4617 return bfd_set_section_alignment (dynobj, htab->root.splt, 4);
4620 /* Implement elf_backend_copy_indirect_symbol:
4621 Copy the extra info we tack onto an elf_link_hash_entry. */
4622 static void
4623 nios2_elf32_copy_indirect_symbol (struct bfd_link_info *info,
4624 struct elf_link_hash_entry *dir,
4625 struct elf_link_hash_entry *ind)
4627 struct elf32_nios2_link_hash_entry *edir, *eind;
4629 edir = (struct elf32_nios2_link_hash_entry *) dir;
4630 eind = (struct elf32_nios2_link_hash_entry *) ind;
4632 if (eind->dyn_relocs != NULL)
4634 if (edir->dyn_relocs != NULL)
4636 struct elf32_nios2_dyn_relocs **pp;
4637 struct elf32_nios2_dyn_relocs *p;
4639 /* Add reloc counts against the indirect sym to the direct sym
4640 list. Merge any entries against the same section. */
4641 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
4643 struct elf32_nios2_dyn_relocs *q;
4645 for (q = edir->dyn_relocs; q != NULL; q = q->next)
4646 if (q->sec == p->sec)
4648 q->pc_count += p->pc_count;
4649 q->count += p->count;
4650 *pp = p->next;
4651 break;
4653 if (q == NULL)
4654 pp = &p->next;
4656 *pp = edir->dyn_relocs;
4659 edir->dyn_relocs = eind->dyn_relocs;
4660 eind->dyn_relocs = NULL;
4663 if (ind->root.type == bfd_link_hash_indirect
4664 && dir->got.refcount <= 0)
4666 edir->tls_type = eind->tls_type;
4667 eind->tls_type = GOT_UNKNOWN;
4670 edir->got_types_used |= eind->got_types_used;
4672 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
4675 /* Set the right machine number for a NIOS2 ELF file. */
4677 static bfd_boolean
4678 nios2_elf32_object_p (bfd *abfd)
4680 unsigned long mach;
4682 mach = elf_elfheader (abfd)->e_flags;
4684 switch (mach)
4686 default:
4687 case EF_NIOS2_ARCH_R1:
4688 bfd_default_set_arch_mach (abfd, bfd_arch_nios2, bfd_mach_nios2r1);
4689 break;
4690 case EF_NIOS2_ARCH_R2:
4691 bfd_default_set_arch_mach (abfd, bfd_arch_nios2, bfd_mach_nios2r2);
4692 break;
4695 return TRUE;
4698 /* Implement elf_backend_check_relocs:
4699 Look through the relocs for a section during the first phase. */
4700 static bfd_boolean
4701 nios2_elf32_check_relocs (bfd *abfd, struct bfd_link_info *info,
4702 asection *sec, const Elf_Internal_Rela *relocs)
4704 Elf_Internal_Shdr *symtab_hdr;
4705 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
4706 const Elf_Internal_Rela *rel;
4707 const Elf_Internal_Rela *rel_end;
4708 struct elf32_nios2_link_hash_table *htab;
4709 asection *sreloc = NULL;
4710 bfd_signed_vma *local_got_refcounts;
4712 if (bfd_link_relocatable (info))
4713 return TRUE;
4715 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4716 sym_hashes = elf_sym_hashes (abfd);
4717 sym_hashes_end = (sym_hashes
4718 + symtab_hdr->sh_size / sizeof (Elf32_External_Sym));
4719 if (!elf_bad_symtab (abfd))
4720 sym_hashes_end -= symtab_hdr->sh_info;
4721 local_got_refcounts = elf_local_got_refcounts (abfd);
4723 htab = elf32_nios2_hash_table (info);
4725 rel_end = relocs + sec->reloc_count;
4726 for (rel = relocs; rel < rel_end; rel++)
4728 unsigned int r_type;
4729 struct elf_link_hash_entry *h;
4730 unsigned long r_symndx;
4732 r_symndx = ELF32_R_SYM (rel->r_info);
4733 if (r_symndx < symtab_hdr->sh_info)
4734 h = NULL;
4735 else
4737 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4738 while (h->root.type == bfd_link_hash_indirect
4739 || h->root.type == bfd_link_hash_warning)
4740 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4742 /* PR15323, ref flags aren't set for references in the same
4743 object. */
4744 h->root.non_ir_ref_regular = 1;
4747 r_type = ELF32_R_TYPE (rel->r_info);
4749 switch (r_type)
4751 case R_NIOS2_GOT16:
4752 case R_NIOS2_GOT_LO:
4753 case R_NIOS2_GOT_HA:
4754 case R_NIOS2_CALL16:
4755 case R_NIOS2_CALL_LO:
4756 case R_NIOS2_CALL_HA:
4757 case R_NIOS2_TLS_GD16:
4758 case R_NIOS2_TLS_IE16:
4759 /* This symbol requires a global offset table entry. */
4761 int tls_type, old_tls_type;
4763 switch (r_type)
4765 default:
4766 case R_NIOS2_GOT16:
4767 case R_NIOS2_GOT_LO:
4768 case R_NIOS2_GOT_HA:
4769 case R_NIOS2_CALL16:
4770 case R_NIOS2_CALL_LO:
4771 case R_NIOS2_CALL_HA:
4772 tls_type = GOT_NORMAL;
4773 break;
4774 case R_NIOS2_TLS_GD16:
4775 tls_type = GOT_TLS_GD;
4776 break;
4777 case R_NIOS2_TLS_IE16:
4778 tls_type = GOT_TLS_IE;
4779 break;
4782 if (h != NULL)
4784 struct elf32_nios2_link_hash_entry *eh
4785 = (struct elf32_nios2_link_hash_entry *)h;
4786 h->got.refcount++;
4787 old_tls_type = elf32_nios2_hash_entry(h)->tls_type;
4788 if (r_type == R_NIOS2_CALL16
4789 || r_type == R_NIOS2_CALL_LO
4790 || r_type == R_NIOS2_CALL_HA)
4792 /* Make sure a plt entry is created for this symbol if
4793 it turns out to be a function defined by a dynamic
4794 object. */
4795 h->plt.refcount++;
4796 h->needs_plt = 1;
4797 h->type = STT_FUNC;
4798 eh->got_types_used |= CALL_USED;
4800 else
4801 eh->got_types_used |= GOT_USED;
4803 else
4805 /* This is a global offset table entry for a local symbol. */
4806 if (local_got_refcounts == NULL)
4808 bfd_size_type size;
4810 size = symtab_hdr->sh_info;
4811 size *= (sizeof (bfd_signed_vma) + sizeof (char));
4812 local_got_refcounts
4813 = ((bfd_signed_vma *) bfd_zalloc (abfd, size));
4814 if (local_got_refcounts == NULL)
4815 return FALSE;
4816 elf_local_got_refcounts (abfd) = local_got_refcounts;
4817 elf32_nios2_local_got_tls_type (abfd)
4818 = (char *) (local_got_refcounts + symtab_hdr->sh_info);
4820 local_got_refcounts[r_symndx]++;
4821 old_tls_type = elf32_nios2_local_got_tls_type (abfd) [r_symndx];
4824 /* We will already have issued an error message if there is a
4825 TLS / non-TLS mismatch, based on the symbol type. We don't
4826 support any linker relaxations. So just combine any TLS
4827 types needed. */
4828 if (old_tls_type != GOT_UNKNOWN && old_tls_type != GOT_NORMAL
4829 && tls_type != GOT_NORMAL)
4830 tls_type |= old_tls_type;
4832 if (old_tls_type != tls_type)
4834 if (h != NULL)
4835 elf32_nios2_hash_entry (h)->tls_type = tls_type;
4836 else
4837 elf32_nios2_local_got_tls_type (abfd) [r_symndx] = tls_type;
4840 make_got:
4841 if (htab->root.sgot == NULL)
4843 if (htab->root.dynobj == NULL)
4844 htab->root.dynobj = abfd;
4845 if (!create_got_section (htab->root.dynobj, info))
4846 return FALSE;
4848 break;
4850 case R_NIOS2_TLS_LDM16:
4851 htab->tls_ldm_got.refcount++;
4852 goto make_got;
4854 /* This relocation describes the C++ object vtable hierarchy.
4855 Reconstruct it for later use during GC. */
4856 case R_NIOS2_GNU_VTINHERIT:
4857 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
4858 return FALSE;
4859 break;
4861 /* This relocation describes which C++ vtable entries are actually
4862 used. Record for later use during GC. */
4863 case R_NIOS2_GNU_VTENTRY:
4864 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
4865 return FALSE;
4866 break;
4868 case R_NIOS2_BFD_RELOC_32:
4869 case R_NIOS2_CALL26:
4870 case R_NIOS2_CALL26_NOAT:
4871 case R_NIOS2_HIADJ16:
4872 case R_NIOS2_LO16:
4874 if (h != NULL)
4876 /* If this reloc is in a read-only section, we might
4877 need a copy reloc. We can't check reliably at this
4878 stage whether the section is read-only, as input
4879 sections have not yet been mapped to output sections.
4880 Tentatively set the flag for now, and correct in
4881 adjust_dynamic_symbol. */
4882 if (!bfd_link_pic (info))
4883 h->non_got_ref = 1;
4885 /* Make sure a plt entry is created for this symbol if it
4886 turns out to be a function defined by a dynamic object. */
4887 h->plt.refcount++;
4889 if (r_type == R_NIOS2_CALL26 || r_type == R_NIOS2_CALL26_NOAT)
4890 h->needs_plt = 1;
4893 /* If we are creating a shared library, we need to copy the
4894 reloc into the shared library. */
4895 if (bfd_link_pic (info)
4896 && (sec->flags & SEC_ALLOC) != 0
4897 && (r_type == R_NIOS2_BFD_RELOC_32
4898 || (h != NULL && ! h->needs_plt
4899 && (! SYMBOLIC_BIND (info, h) || ! h->def_regular))))
4901 struct elf32_nios2_dyn_relocs *p;
4902 struct elf32_nios2_dyn_relocs **head;
4904 /* When creating a shared object, we must copy these
4905 reloc types into the output file. We create a reloc
4906 section in dynobj and make room for this reloc. */
4907 if (sreloc == NULL)
4909 if (htab->root.dynobj == NULL)
4910 htab->root.dynobj = abfd;
4912 sreloc = _bfd_elf_make_dynamic_reloc_section
4913 (sec, htab->root.dynobj, 2, abfd, TRUE);
4914 if (sreloc == NULL)
4915 return FALSE;
4918 /* If this is a global symbol, we count the number of
4919 relocations we need for this symbol. */
4920 if (h != NULL)
4921 head = &((struct elf32_nios2_link_hash_entry *) h)->dyn_relocs;
4922 else
4924 /* Track dynamic relocs needed for local syms too.
4925 We really need local syms available to do this
4926 easily. Oh well. */
4928 asection *s;
4929 void *vpp;
4930 Elf_Internal_Sym *isym;
4932 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4933 abfd, r_symndx);
4934 if (isym == NULL)
4935 return FALSE;
4937 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
4938 if (s == NULL)
4939 s = sec;
4941 vpp = &elf_section_data (s)->local_dynrel;
4942 head = (struct elf32_nios2_dyn_relocs **) vpp;
4945 p = *head;
4946 if (p == NULL || p->sec != sec)
4948 bfd_size_type amt = sizeof *p;
4949 p = ((struct elf32_nios2_dyn_relocs *)
4950 bfd_alloc (htab->root.dynobj, amt));
4951 if (p == NULL)
4952 return FALSE;
4953 p->next = *head;
4954 *head = p;
4955 p->sec = sec;
4956 p->count = 0;
4957 p->pc_count = 0;
4960 p->count += 1;
4963 break;
4967 return TRUE;
4971 /* Implement elf_backend_gc_mark_hook:
4972 Return the section that should be marked against GC for a given
4973 relocation. */
4974 static asection *
4975 nios2_elf32_gc_mark_hook (asection *sec,
4976 struct bfd_link_info *info,
4977 Elf_Internal_Rela *rel,
4978 struct elf_link_hash_entry *h,
4979 Elf_Internal_Sym *sym)
4981 if (h != NULL)
4982 switch (ELF32_R_TYPE (rel->r_info))
4984 case R_NIOS2_GNU_VTINHERIT:
4985 case R_NIOS2_GNU_VTENTRY:
4986 return NULL;
4988 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
4991 /* Implement elf_backend_finish_dynamic_symbols:
4992 Finish up dynamic symbol handling. We set the contents of various
4993 dynamic sections here. */
4994 static bfd_boolean
4995 nios2_elf32_finish_dynamic_symbol (bfd *output_bfd,
4996 struct bfd_link_info *info,
4997 struct elf_link_hash_entry *h,
4998 Elf_Internal_Sym *sym)
5000 struct elf32_nios2_link_hash_table *htab;
5001 struct elf32_nios2_link_hash_entry *eh
5002 = (struct elf32_nios2_link_hash_entry *)h;
5003 int use_plt;
5005 htab = elf32_nios2_hash_table (info);
5007 if (h->plt.offset != (bfd_vma) -1)
5009 asection *splt;
5010 asection *sgotplt;
5011 asection *srela;
5012 bfd_vma plt_index;
5013 bfd_vma got_offset;
5014 Elf_Internal_Rela rela;
5015 bfd_byte *loc;
5016 bfd_vma got_address;
5018 /* This symbol has an entry in the procedure linkage table. Set
5019 it up. */
5020 BFD_ASSERT (h->dynindx != -1);
5021 splt = htab->root.splt;
5022 sgotplt = htab->root.sgotplt;
5023 srela = htab->root.srelplt;
5024 BFD_ASSERT (splt != NULL && sgotplt != NULL && srela != NULL);
5026 /* Emit the PLT entry. */
5027 if (bfd_link_pic (info))
5029 nios2_elf32_install_data (splt, nios2_so_plt_entry, h->plt.offset,
5031 plt_index = (h->plt.offset - 24) / 12;
5032 got_offset = (plt_index + 3) * 4;
5033 nios2_elf32_install_imm16 (splt, h->plt.offset,
5034 hiadj(plt_index * 4));
5035 nios2_elf32_install_imm16 (splt, h->plt.offset + 4,
5036 (plt_index * 4) & 0xffff);
5037 nios2_elf32_install_imm16 (splt, h->plt.offset + 8,
5038 0xfff4 - h->plt.offset);
5039 got_address = (sgotplt->output_section->vma + sgotplt->output_offset
5040 + got_offset);
5042 /* Fill in the entry in the global offset table. There are no
5043 res_n slots for a shared object PLT, instead the .got.plt entries
5044 point to the PLT entries. */
5045 bfd_put_32 (output_bfd,
5046 splt->output_section->vma + splt->output_offset
5047 + h->plt.offset, sgotplt->contents + got_offset);
5049 else
5051 plt_index = (h->plt.offset - 28 - htab->res_n_size) / 12;
5052 got_offset = (plt_index + 3) * 4;
5054 nios2_elf32_install_data (splt, nios2_plt_entry, h->plt.offset, 3);
5055 got_address = (sgotplt->output_section->vma + sgotplt->output_offset
5056 + got_offset);
5057 nios2_elf32_install_imm16 (splt, h->plt.offset, hiadj(got_address));
5058 nios2_elf32_install_imm16 (splt, h->plt.offset + 4,
5059 got_address & 0xffff);
5061 /* Fill in the entry in the global offset table. */
5062 bfd_put_32 (output_bfd,
5063 splt->output_section->vma + splt->output_offset
5064 + plt_index * 4, sgotplt->contents + got_offset);
5067 /* Fill in the entry in the .rela.plt section. */
5068 rela.r_offset = got_address;
5069 rela.r_info = ELF32_R_INFO (h->dynindx, R_NIOS2_JUMP_SLOT);
5070 rela.r_addend = 0;
5071 loc = srela->contents + plt_index * sizeof (Elf32_External_Rela);
5072 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
5074 if (!h->def_regular)
5076 /* Mark the symbol as undefined, rather than as defined in
5077 the .plt section. Leave the value alone. */
5078 sym->st_shndx = SHN_UNDEF;
5079 /* If the symbol is weak, we do need to clear the value.
5080 Otherwise, the PLT entry would provide a definition for
5081 the symbol even if the symbol wasn't defined anywhere,
5082 and so the symbol would never be NULL. */
5083 if (!h->ref_regular_nonweak)
5084 sym->st_value = 0;
5088 use_plt = (eh->got_types_used == CALL_USED
5089 && h->plt.offset != (bfd_vma) -1);
5091 if (!use_plt && h->got.offset != (bfd_vma) -1
5092 && (elf32_nios2_hash_entry (h)->tls_type & GOT_TLS_GD) == 0
5093 && (elf32_nios2_hash_entry (h)->tls_type & GOT_TLS_IE) == 0)
5095 asection *sgot;
5096 asection *srela;
5097 Elf_Internal_Rela rela;
5098 bfd_byte *loc;
5099 bfd_vma offset;
5101 /* This symbol has an entry in the global offset table. Set it
5102 up. */
5103 sgot = htab->root.sgot;
5104 srela = htab->root.srelgot;
5105 BFD_ASSERT (sgot != NULL && srela != NULL);
5107 offset = (h->got.offset & ~(bfd_vma) 1);
5108 rela.r_offset = (sgot->output_section->vma
5109 + sgot->output_offset + offset);
5111 /* If this is a -Bsymbolic link, and the symbol is defined
5112 locally, we just want to emit a RELATIVE reloc. Likewise if
5113 the symbol was forced to be local because of a version file.
5114 The entry in the global offset table will already have been
5115 initialized in the relocate_section function. */
5117 if (bfd_link_pic (info) && SYMBOL_REFERENCES_LOCAL (info, h))
5119 rela.r_info = ELF32_R_INFO (0, R_NIOS2_RELATIVE);
5120 rela.r_addend = bfd_get_signed_32 (output_bfd,
5121 (sgot->contents + offset));
5122 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + offset);
5124 else
5126 bfd_put_32 (output_bfd, (bfd_vma) 0,
5127 sgot->contents + offset);
5128 rela.r_info = ELF32_R_INFO (h->dynindx, R_NIOS2_GLOB_DAT);
5129 rela.r_addend = 0;
5132 loc = srela->contents;
5133 loc += srela->reloc_count++ * sizeof (Elf32_External_Rela);
5134 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
5137 if (use_plt && h->got.offset != (bfd_vma) -1)
5139 bfd_vma offset = (h->got.offset & ~(bfd_vma) 1);
5140 asection *sgot = htab->root.sgot;
5141 asection *splt = htab->root.splt;
5142 bfd_put_32 (output_bfd, (splt->output_section->vma + splt->output_offset
5143 + h->plt.offset),
5144 sgot->contents + offset);
5147 if (h->needs_copy)
5149 asection *s;
5150 Elf_Internal_Rela rela;
5151 bfd_byte *loc;
5153 /* This symbol needs a copy reloc. Set it up. */
5154 BFD_ASSERT (h->dynindx != -1
5155 && (h->root.type == bfd_link_hash_defined
5156 || h->root.type == bfd_link_hash_defweak));
5158 rela.r_offset = (h->root.u.def.value
5159 + h->root.u.def.section->output_section->vma
5160 + h->root.u.def.section->output_offset);
5161 rela.r_info = ELF32_R_INFO (h->dynindx, R_NIOS2_COPY);
5162 rela.r_addend = 0;
5163 if (h->root.u.def.section == htab->root.sdynrelro)
5164 s = htab->root.sreldynrelro;
5165 else
5166 s = htab->root.srelbss;
5167 BFD_ASSERT (s != NULL);
5168 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
5169 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
5172 /* Mark _DYNAMIC, _GLOBAL_OFFSET_TABLE_, and _gp_got as absolute. */
5173 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
5174 || h == htab->root.hgot
5175 || h == htab->h_gp_got)
5176 sym->st_shndx = SHN_ABS;
5178 return TRUE;
5181 /* Implement elf_backend_finish_dynamic_sections. */
5182 static bfd_boolean
5183 nios2_elf32_finish_dynamic_sections (bfd *output_bfd,
5184 struct bfd_link_info *info)
5186 asection *sgotplt;
5187 asection *sdyn;
5188 struct elf32_nios2_link_hash_table *htab;
5190 htab = elf32_nios2_hash_table (info);
5191 sgotplt = htab->root.sgotplt;
5192 sdyn = NULL;
5194 if (htab->root.dynamic_sections_created)
5196 asection *splt;
5197 Elf32_External_Dyn *dyncon, *dynconend;
5199 splt = htab->root.splt;
5200 sdyn = bfd_get_linker_section (htab->root.dynobj, ".dynamic");
5201 BFD_ASSERT (splt != NULL && sdyn != NULL && sgotplt != NULL);
5203 dyncon = (Elf32_External_Dyn *) sdyn->contents;
5204 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
5205 for (; dyncon < dynconend; dyncon++)
5207 Elf_Internal_Dyn dyn;
5208 asection *s;
5210 bfd_elf32_swap_dyn_in (htab->root.dynobj, dyncon, &dyn);
5212 switch (dyn.d_tag)
5214 default:
5215 break;
5217 case DT_PLTGOT:
5218 s = htab->root.sgotplt;
5219 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
5220 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5221 break;
5223 case DT_JMPREL:
5224 s = htab->root.srelplt;
5225 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
5226 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5227 break;
5229 case DT_PLTRELSZ:
5230 s = htab->root.srelplt;
5231 dyn.d_un.d_val = s->size;
5232 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5233 break;
5235 case DT_NIOS2_GP:
5236 s = htab->root.sgotplt;
5237 dyn.d_un.d_ptr
5238 = s->output_section->vma + s->output_offset + 0x7ff0;
5239 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5240 break;
5244 /* Fill in the first entry in the procedure linkage table. */
5245 if (splt->size > 0)
5247 bfd_vma got_address = (sgotplt->output_section->vma
5248 + sgotplt->output_offset);
5249 if (bfd_link_pic (info))
5251 bfd_vma got_pcrel = got_address - (splt->output_section->vma
5252 + splt->output_offset);
5253 /* Both GOT and PLT must be aligned to a 16-byte boundary
5254 for the two loads to share the %hiadj part. The 4-byte
5255 offset for nextpc is accounted for in the %lo offsets
5256 on the loads. */
5257 BFD_ASSERT ((got_pcrel & 0xf) == 0);
5258 nios2_elf32_install_data (splt, nios2_so_plt0_entry, 0, 6);
5259 nios2_elf32_install_imm16 (splt, 4, hiadj (got_pcrel));
5260 nios2_elf32_install_imm16 (splt, 12, got_pcrel & 0xffff);
5261 nios2_elf32_install_imm16 (splt, 16, (got_pcrel + 4) & 0xffff);
5263 else
5265 /* Divide by 4 here, not 3 because we already corrected for the
5266 res_N branches. */
5267 bfd_vma res_size = (splt->size - 28) / 4;
5268 bfd_vma res_start = (splt->output_section->vma
5269 + splt->output_offset);
5270 bfd_vma res_offset;
5272 for (res_offset = 0; res_offset < res_size; res_offset += 4)
5273 bfd_put_32 (output_bfd,
5274 6 | ((res_size - (res_offset + 4)) << 6),
5275 splt->contents + res_offset);
5277 /* The GOT must be aligned to a 16-byte boundary for the
5278 two loads to share the same %hiadj part. */
5279 BFD_ASSERT ((got_address & 0xf) == 0);
5281 nios2_elf32_install_data (splt, nios2_plt0_entry, res_size, 7);
5282 nios2_elf32_install_imm16 (splt, res_size, hiadj (res_start));
5283 nios2_elf32_install_imm16 (splt, res_size + 4,
5284 res_start & 0xffff);
5285 nios2_elf32_install_imm16 (splt, res_size + 12,
5286 hiadj (got_address));
5287 nios2_elf32_install_imm16 (splt, res_size + 16,
5288 (got_address + 4) & 0xffff);
5289 nios2_elf32_install_imm16 (splt, res_size + 20,
5290 (got_address + 8) & 0xffff);
5295 /* Fill in the first three entries in the global offset table. */
5296 if (sgotplt != NULL && sgotplt->size > 0)
5298 if (sdyn == NULL)
5299 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents);
5300 else
5301 bfd_put_32 (output_bfd,
5302 sdyn->output_section->vma + sdyn->output_offset,
5303 sgotplt->contents);
5304 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 4);
5305 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 8);
5307 if (sgotplt->output_section != bfd_abs_section_ptr)
5308 elf_section_data (sgotplt->output_section)->this_hdr.sh_entsize = 4;
5311 return TRUE;
5314 /* Implement elf_backend_adjust_dynamic_symbol:
5315 Adjust a symbol defined by a dynamic object and referenced by a
5316 regular object. The current definition is in some section of the
5317 dynamic object, but we're not including those sections. We have to
5318 change the definition to something the rest of the link can
5319 understand. */
5320 static bfd_boolean
5321 nios2_elf32_adjust_dynamic_symbol (struct bfd_link_info *info,
5322 struct elf_link_hash_entry *h)
5324 struct elf32_nios2_link_hash_table *htab;
5325 bfd *dynobj;
5326 asection *s, *srel;
5327 unsigned align2;
5329 htab = elf32_nios2_hash_table (info);
5330 dynobj = htab->root.dynobj;
5332 /* Make sure we know what is going on here. */
5333 BFD_ASSERT (dynobj != NULL
5334 && (h->needs_plt
5335 || h->u.weakdef != NULL
5336 || (h->def_dynamic
5337 && h->ref_regular
5338 && !h->def_regular)));
5340 /* If this is a function, put it in the procedure linkage table. We
5341 will fill in the contents of the procedure linkage table later,
5342 when we know the address of the .got section. */
5343 if (h->type == STT_FUNC || h->needs_plt)
5345 if (h->plt.refcount <= 0
5346 || SYMBOL_CALLS_LOCAL (info, h)
5347 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
5348 && h->root.type == bfd_link_hash_undefweak))
5350 /* This case can occur if we saw a PLT reloc in an input
5351 file, but the symbol was never referred to by a dynamic
5352 object, or if all references were garbage collected. In
5353 such a case, we don't actually need to build a procedure
5354 linkage table, and we can just do a PCREL reloc instead. */
5355 h->plt.offset = (bfd_vma) -1;
5356 h->needs_plt = 0;
5359 return TRUE;
5362 /* Reinitialize the plt offset now that it is not used as a reference
5363 count any more. */
5364 h->plt.offset = (bfd_vma) -1;
5366 /* If this is a weak symbol, and there is a real definition, the
5367 processor independent code will have arranged for us to see the
5368 real definition first, and we can just use the same value. */
5369 if (h->u.weakdef != NULL)
5371 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
5372 || h->u.weakdef->root.type == bfd_link_hash_defweak);
5373 h->root.u.def.section = h->u.weakdef->root.u.def.section;
5374 h->root.u.def.value = h->u.weakdef->root.u.def.value;
5375 return TRUE;
5378 /* If there are no non-GOT references, we do not need a copy
5379 relocation. */
5380 if (!h->non_got_ref)
5381 return TRUE;
5383 /* This is a reference to a symbol defined by a dynamic object which
5384 is not a function.
5385 If we are creating a shared library, we must presume that the
5386 only references to the symbol are via the global offset table.
5387 For such cases we need not do anything here; the relocations will
5388 be handled correctly by relocate_section. */
5389 if (bfd_link_pic (info))
5390 return TRUE;
5392 if (h->size == 0)
5394 _bfd_error_handler (_("dynamic variable `%s' is zero size"),
5395 h->root.root.string);
5396 return TRUE;
5399 /* We must allocate the symbol in our .dynbss section, which will
5400 become part of the .bss section of the executable. There will be
5401 an entry for this symbol in the .dynsym section. The dynamic
5402 object will contain position independent code, so all references
5403 from the dynamic object to this symbol will go through the global
5404 offset table. The dynamic linker will use the .dynsym entry to
5405 determine the address it must put in the global offset table, so
5406 both the dynamic object and the regular object will refer to the
5407 same memory location for the variable. */
5408 /* We must generate a R_NIOS2_COPY reloc to tell the dynamic linker to
5409 copy the initial value out of the dynamic object and into the
5410 runtime process image. We need to remember the offset into the
5411 .rela.bss section we are going to use. */
5412 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
5414 s = htab->root.sdynrelro;
5415 srel = htab->root.sreldynrelro;
5417 else
5419 s = htab->root.sdynbss;
5420 srel = htab->root.srelbss;
5422 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
5424 srel->size += sizeof (Elf32_External_Rela);
5425 h->needs_copy = 1;
5428 align2 = bfd_log2 (h->size);
5429 if (align2 > h->root.u.def.section->alignment_power)
5430 align2 = h->root.u.def.section->alignment_power;
5432 /* Align dynbss. */
5433 s->size = BFD_ALIGN (s->size, (bfd_size_type)1 << align2);
5434 if (align2 > bfd_get_section_alignment (dynobj, s)
5435 && !bfd_set_section_alignment (dynobj, s, align2))
5436 return FALSE;
5438 /* Define the symbol as being at this point in the section. */
5439 h->root.u.def.section = s;
5440 h->root.u.def.value = s->size;
5442 /* Increment the section size to make room for the symbol. */
5443 s->size += h->size;
5445 return TRUE;
5448 /* Worker function for nios2_elf32_size_dynamic_sections. */
5449 static bfd_boolean
5450 adjust_dynrelocs (struct elf_link_hash_entry *h, PTR inf)
5452 struct bfd_link_info *info;
5453 struct elf32_nios2_link_hash_table *htab;
5455 if (h->root.type == bfd_link_hash_indirect)
5456 return TRUE;
5458 if (h->root.type == bfd_link_hash_warning)
5459 /* When warning symbols are created, they **replace** the "real"
5460 entry in the hash table, thus we never get to see the real
5461 symbol in a hash traversal. So look at it now. */
5462 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5464 info = (struct bfd_link_info *) inf;
5465 htab = elf32_nios2_hash_table (info);
5467 if (h->plt.offset != (bfd_vma)-1)
5468 h->plt.offset += htab->res_n_size;
5469 if (htab->root.splt == h->root.u.def.section)
5470 h->root.u.def.value += htab->res_n_size;
5472 return TRUE;
5475 /* Another worker function for nios2_elf32_size_dynamic_sections.
5476 Allocate space in .plt, .got and associated reloc sections for
5477 dynamic relocs. */
5478 static bfd_boolean
5479 allocate_dynrelocs (struct elf_link_hash_entry *h, PTR inf)
5481 struct bfd_link_info *info;
5482 struct elf32_nios2_link_hash_table *htab;
5483 struct elf32_nios2_link_hash_entry *eh;
5484 struct elf32_nios2_dyn_relocs *p;
5485 int use_plt;
5487 if (h->root.type == bfd_link_hash_indirect)
5488 return TRUE;
5490 if (h->root.type == bfd_link_hash_warning)
5491 /* When warning symbols are created, they **replace** the "real"
5492 entry in the hash table, thus we never get to see the real
5493 symbol in a hash traversal. So look at it now. */
5494 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5496 info = (struct bfd_link_info *) inf;
5497 htab = elf32_nios2_hash_table (info);
5499 if (htab->root.dynamic_sections_created
5500 && h->plt.refcount > 0)
5502 /* Make sure this symbol is output as a dynamic symbol.
5503 Undefined weak syms won't yet be marked as dynamic. */
5504 if (h->dynindx == -1
5505 && !h->forced_local
5506 && !bfd_elf_link_record_dynamic_symbol (info, h))
5507 return FALSE;
5509 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info), h))
5511 asection *s = htab->root.splt;
5513 /* Allocate room for the header. */
5514 if (s->size == 0)
5516 if (bfd_link_pic (info))
5517 s->size = 24;
5518 else
5519 s->size = 28;
5522 h->plt.offset = s->size;
5524 /* If this symbol is not defined in a regular file, and we are
5525 not generating a shared library, then set the symbol to this
5526 location in the .plt. This is required to make function
5527 pointers compare as equal between the normal executable and
5528 the shared library. */
5529 if (! bfd_link_pic (info)
5530 && !h->def_regular)
5532 h->root.u.def.section = s;
5533 h->root.u.def.value = h->plt.offset;
5536 /* Make room for this entry. */
5537 s->size += 12;
5539 /* We also need to make an entry in the .rela.plt section. */
5540 htab->root.srelplt->size += sizeof (Elf32_External_Rela);
5542 /* And the .got.plt section. */
5543 htab->root.sgotplt->size += 4;
5545 else
5547 h->plt.offset = (bfd_vma) -1;
5548 h->needs_plt = 0;
5551 else
5553 h->plt.offset = (bfd_vma) -1;
5554 h->needs_plt = 0;
5557 eh = (struct elf32_nios2_link_hash_entry *) h;
5558 use_plt = (eh->got_types_used == CALL_USED
5559 && h->plt.offset != (bfd_vma) -1);
5561 if (h->got.refcount > 0)
5563 asection *s;
5564 bfd_boolean dyn;
5565 int tls_type = eh->tls_type;
5566 int indx;
5568 /* Make sure this symbol is output as a dynamic symbol.
5569 Undefined weak syms won't yet be marked as dynamic. */
5570 if (h->dynindx == -1
5571 && !h->forced_local
5572 && !bfd_elf_link_record_dynamic_symbol (info, h))
5573 return FALSE;
5575 s = htab->root.sgot;
5576 h->got.offset = s->size;
5578 if (tls_type == GOT_UNKNOWN)
5579 abort ();
5581 if (tls_type == GOT_NORMAL)
5582 /* Non-TLS symbols need one GOT slot. */
5583 s->size += 4;
5584 else
5586 if (tls_type & GOT_TLS_GD)
5587 /* R_NIOS2_TLS_GD16 needs 2 consecutive GOT slots. */
5588 s->size += 8;
5589 if (tls_type & GOT_TLS_IE)
5590 /* R_NIOS2_TLS_IE16 needs one GOT slot. */
5591 s->size += 4;
5594 dyn = htab->root.dynamic_sections_created;
5596 indx = 0;
5597 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
5598 && (!bfd_link_pic (info)
5599 || !SYMBOL_REFERENCES_LOCAL (info, h)))
5600 indx = h->dynindx;
5602 if (tls_type != GOT_NORMAL
5603 && (bfd_link_pic (info) || indx != 0)
5604 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
5605 || h->root.type != bfd_link_hash_undefweak))
5607 if (tls_type & GOT_TLS_IE)
5608 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
5610 if (tls_type & GOT_TLS_GD)
5611 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
5613 if ((tls_type & GOT_TLS_GD) && indx != 0)
5614 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
5616 else if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
5617 || h->root.type != bfd_link_hash_undefweak)
5618 && !use_plt
5619 && (bfd_link_pic (info)
5620 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
5621 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
5623 else
5624 h->got.offset = (bfd_vma) -1;
5626 if (eh->dyn_relocs == NULL)
5627 return TRUE;
5629 /* In the shared -Bsymbolic case, discard space allocated for
5630 dynamic pc-relative relocs against symbols which turn out to be
5631 defined in regular objects. For the normal shared case, discard
5632 space for pc-relative relocs that have become local due to symbol
5633 visibility changes. */
5635 if (bfd_link_pic (info))
5637 if (h->def_regular
5638 && (h->forced_local || SYMBOLIC_BIND (info, h)))
5640 struct elf32_nios2_dyn_relocs **pp;
5642 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
5644 p->count -= p->pc_count;
5645 p->pc_count = 0;
5646 if (p->count == 0)
5647 *pp = p->next;
5648 else
5649 pp = &p->next;
5653 /* Also discard relocs on undefined weak syms with non-default
5654 visibility. */
5655 if (eh->dyn_relocs != NULL
5656 && h->root.type == bfd_link_hash_undefweak)
5658 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
5659 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
5660 eh->dyn_relocs = NULL;
5662 /* Make sure undefined weak symbols are output as a dynamic
5663 symbol in PIEs. */
5664 else if (h->dynindx == -1
5665 && !h->forced_local
5666 && !bfd_elf_link_record_dynamic_symbol (info, h))
5667 return FALSE;
5670 else
5672 /* For the non-shared case, discard space for relocs against
5673 symbols which turn out to need copy relocs or are not
5674 dynamic. */
5676 if (!h->non_got_ref
5677 && ((h->def_dynamic && !h->def_regular)
5678 || (htab->root.dynamic_sections_created
5679 && (h->root.type == bfd_link_hash_undefweak
5680 || h->root.type == bfd_link_hash_undefined))))
5682 /* Make sure this symbol is output as a dynamic symbol.
5683 Undefined weak syms won't yet be marked as dynamic. */
5684 if (h->dynindx == -1
5685 && !h->forced_local
5686 && !bfd_elf_link_record_dynamic_symbol (info, h))
5687 return FALSE;
5689 /* If that succeeded, we know we'll be keeping all the
5690 relocs. */
5691 if (h->dynindx != -1)
5692 goto keep;
5695 eh->dyn_relocs = NULL;
5697 keep: ;
5700 /* Finally, allocate space. */
5701 for (p = eh->dyn_relocs; p != NULL; p = p->next)
5703 asection *sreloc = elf_section_data (p->sec)->sreloc;
5704 sreloc->size += p->count * sizeof (Elf32_External_Rela);
5707 return TRUE;
5710 /* Implement elf_backend_size_dynamic_sections:
5711 Set the sizes of the dynamic sections. */
5712 static bfd_boolean
5713 nios2_elf32_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
5714 struct bfd_link_info *info)
5716 bfd *dynobj;
5717 asection *s;
5718 bfd_boolean relocs;
5719 bfd *ibfd;
5720 struct elf32_nios2_link_hash_table *htab;
5722 htab = elf32_nios2_hash_table (info);
5723 dynobj = htab->root.dynobj;
5724 BFD_ASSERT (dynobj != NULL);
5726 htab->res_n_size = 0;
5727 if (htab->root.dynamic_sections_created)
5729 /* Set the contents of the .interp section to the interpreter. */
5730 if (bfd_link_executable (info) && !info->nointerp)
5732 s = bfd_get_linker_section (dynobj, ".interp");
5733 BFD_ASSERT (s != NULL);
5734 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
5735 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
5738 else
5740 /* We may have created entries in the .rela.got section.
5741 However, if we are not creating the dynamic sections, we will
5742 not actually use these entries. Reset the size of .rela.got,
5743 which will cause it to get stripped from the output file
5744 below. */
5745 s = htab->root.srelgot;
5746 if (s != NULL)
5747 s->size = 0;
5750 /* Set up .got offsets for local syms, and space for local dynamic
5751 relocs. */
5752 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
5754 bfd_signed_vma *local_got;
5755 bfd_signed_vma *end_local_got;
5756 char *local_tls_type;
5757 bfd_size_type locsymcount;
5758 Elf_Internal_Shdr *symtab_hdr;
5759 asection *srel;
5761 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
5762 continue;
5764 for (s = ibfd->sections; s != NULL; s = s->next)
5766 struct elf32_nios2_dyn_relocs *p;
5768 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
5770 if (!bfd_is_abs_section (p->sec)
5771 && bfd_is_abs_section (p->sec->output_section))
5773 /* Input section has been discarded, either because
5774 it is a copy of a linkonce section or due to
5775 linker script /DISCARD/, so we'll be discarding
5776 the relocs too. */
5778 else if (p->count != 0)
5780 srel = elf_section_data (p->sec)->sreloc;
5781 srel->size += p->count * sizeof (Elf32_External_Rela);
5782 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
5783 info->flags |= DF_TEXTREL;
5788 local_got = elf_local_got_refcounts (ibfd);
5789 if (!local_got)
5790 continue;
5792 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
5793 locsymcount = symtab_hdr->sh_info;
5794 end_local_got = local_got + locsymcount;
5795 local_tls_type = elf32_nios2_local_got_tls_type (ibfd);
5796 s = htab->root.sgot;
5797 srel = htab->root.srelgot;
5798 for (; local_got < end_local_got; ++local_got, ++local_tls_type)
5800 if (*local_got > 0)
5802 *local_got = s->size;
5803 if (*local_tls_type & GOT_TLS_GD)
5804 /* TLS_GD relocs need an 8-byte structure in the GOT. */
5805 s->size += 8;
5806 if (*local_tls_type & GOT_TLS_IE)
5807 s->size += 4;
5808 if (*local_tls_type == GOT_NORMAL)
5809 s->size += 4;
5811 if (bfd_link_pic (info) || *local_tls_type == GOT_TLS_GD)
5812 srel->size += sizeof (Elf32_External_Rela);
5814 else
5815 *local_got = (bfd_vma) -1;
5819 if (htab->tls_ldm_got.refcount > 0)
5821 /* Allocate two GOT entries and one dynamic relocation (if necessary)
5822 for R_NIOS2_TLS_LDM16 relocations. */
5823 htab->tls_ldm_got.offset = htab->root.sgot->size;
5824 htab->root.sgot->size += 8;
5825 if (bfd_link_pic (info))
5826 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
5828 else
5829 htab->tls_ldm_got.offset = -1;
5831 /* Allocate global sym .plt and .got entries, and space for global
5832 sym dynamic relocs. */
5833 elf_link_hash_traverse (& htab->root, allocate_dynrelocs, info);
5835 if (htab->root.dynamic_sections_created)
5837 /* If the .got section is more than 0x8000 bytes, we add
5838 0x8000 to the value of _gp_got, so that 16-bit relocations
5839 have a greater chance of working. */
5840 if (htab->root.sgot->size >= 0x8000
5841 && htab->h_gp_got->root.u.def.value == 0)
5842 htab->h_gp_got->root.u.def.value = 0x8000;
5845 /* The check_relocs and adjust_dynamic_symbol entry points have
5846 determined the sizes of the various dynamic sections. Allocate
5847 memory for them. */
5848 relocs = FALSE;
5849 for (s = dynobj->sections; s != NULL; s = s->next)
5851 const char *name;
5853 if ((s->flags & SEC_LINKER_CREATED) == 0)
5854 continue;
5856 /* It's OK to base decisions on the section name, because none
5857 of the dynobj section names depend upon the input files. */
5858 name = bfd_get_section_name (dynobj, s);
5860 if (CONST_STRNEQ (name, ".rela"))
5862 if (s->size != 0)
5864 if (s != htab->root.srelplt)
5865 relocs = TRUE;
5867 /* We use the reloc_count field as a counter if we need
5868 to copy relocs into the output file. */
5869 s->reloc_count = 0;
5872 else if (s == htab->root.splt)
5874 /* Correct for the number of res_N branches. */
5875 if (s->size != 0 && !bfd_link_pic (info))
5877 htab->res_n_size = (s->size - 28) / 3;
5878 s->size += htab->res_n_size;
5881 else if (s != htab->sbss
5882 && s != htab->root.sgot
5883 && s != htab->root.sgotplt
5884 && s != htab->root.sdynbss
5885 && s != htab->root.sdynrelro)
5886 /* It's not one of our sections, so don't allocate space. */
5887 continue;
5889 if (s->size == 0)
5891 s->flags |= SEC_EXCLUDE;
5892 continue;
5895 if ((s->flags & SEC_HAS_CONTENTS) == 0)
5896 continue;
5898 /* Allocate memory for the section contents. */
5899 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
5900 if (s->contents == NULL)
5901 return FALSE;
5904 /* Adjust dynamic symbols that point to the plt to account for the
5905 now-known number of resN slots. */
5906 if (htab->res_n_size)
5907 elf_link_hash_traverse (& htab->root, adjust_dynrelocs, info);
5909 if (htab->root.dynamic_sections_created)
5911 /* Add some entries to the .dynamic section. We fill in the
5912 values later, in elf_nios2_finish_dynamic_sections, but we
5913 must add the entries now so that we get the correct size for
5914 the .dynamic section. The DT_DEBUG entry is filled in by the
5915 dynamic linker and used by the debugger. */
5916 #define add_dynamic_entry(TAG, VAL) \
5917 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
5919 if (!bfd_link_pic (info) && !add_dynamic_entry (DT_DEBUG, 0))
5920 return FALSE;
5922 if (htab->root.sgotplt->size != 0
5923 && !add_dynamic_entry (DT_PLTGOT, 0))
5924 return FALSE;
5926 if (htab->root.splt->size != 0
5927 && (!add_dynamic_entry (DT_PLTRELSZ, 0)
5928 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
5929 || !add_dynamic_entry (DT_JMPREL, 0)))
5930 return FALSE;
5932 if (relocs
5933 && (!add_dynamic_entry (DT_RELA, 0)
5934 || !add_dynamic_entry (DT_RELASZ, 0)
5935 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela))))
5936 return FALSE;
5938 if (!bfd_link_pic (info) && !add_dynamic_entry (DT_NIOS2_GP, 0))
5939 return FALSE;
5941 if ((info->flags & DF_TEXTREL) != 0
5942 && !add_dynamic_entry (DT_TEXTREL, 0))
5943 return FALSE;
5945 #undef add_dynamic_entry
5947 return TRUE;
5950 /* Free the derived linker hash table. */
5951 static void
5952 nios2_elf32_link_hash_table_free (bfd *obfd)
5954 struct elf32_nios2_link_hash_table *htab
5955 = (struct elf32_nios2_link_hash_table *) obfd->link.hash;
5957 bfd_hash_table_free (&htab->bstab);
5958 _bfd_elf_link_hash_table_free (obfd);
5961 /* Implement bfd_elf32_bfd_link_hash_table_create. */
5962 static struct bfd_link_hash_table *
5963 nios2_elf32_link_hash_table_create (bfd *abfd)
5965 struct elf32_nios2_link_hash_table *ret;
5966 bfd_size_type amt = sizeof (struct elf32_nios2_link_hash_table);
5968 ret = bfd_zmalloc (amt);
5969 if (ret == NULL)
5970 return NULL;
5972 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
5973 link_hash_newfunc,
5974 sizeof (struct
5975 elf32_nios2_link_hash_entry),
5976 NIOS2_ELF_DATA))
5978 free (ret);
5979 return NULL;
5982 /* Init the stub hash table too. */
5983 if (!bfd_hash_table_init (&ret->bstab, stub_hash_newfunc,
5984 sizeof (struct elf32_nios2_stub_hash_entry)))
5986 _bfd_elf_link_hash_table_free (abfd);
5987 return NULL;
5989 ret->root.root.hash_table_free = nios2_elf32_link_hash_table_free;
5991 return &ret->root.root;
5994 /* Implement elf_backend_reloc_type_class. */
5995 static enum elf_reloc_type_class
5996 nios2_elf32_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
5997 const asection *rel_sec ATTRIBUTE_UNUSED,
5998 const Elf_Internal_Rela *rela)
6000 switch ((int) ELF32_R_TYPE (rela->r_info))
6002 case R_NIOS2_RELATIVE:
6003 return reloc_class_relative;
6004 case R_NIOS2_JUMP_SLOT:
6005 return reloc_class_plt;
6006 case R_NIOS2_COPY:
6007 return reloc_class_copy;
6008 default:
6009 return reloc_class_normal;
6013 /* Return 1 if target is one of ours. */
6014 static bfd_boolean
6015 is_nios2_elf_target (const struct bfd_target *targ)
6017 return (targ == &nios2_elf32_le_vec
6018 || targ == &nios2_elf32_be_vec);
6021 /* Implement elf_backend_add_symbol_hook.
6022 This hook is called by the linker when adding symbols from an object
6023 file. We use it to put .comm items in .sbss, and not .bss. */
6024 static bfd_boolean
6025 nios2_elf_add_symbol_hook (bfd *abfd,
6026 struct bfd_link_info *info,
6027 Elf_Internal_Sym *sym,
6028 const char **namep ATTRIBUTE_UNUSED,
6029 flagword *flagsp ATTRIBUTE_UNUSED,
6030 asection **secp,
6031 bfd_vma *valp)
6033 if (sym->st_shndx == SHN_COMMON
6034 && !bfd_link_relocatable (info)
6035 && sym->st_size <= elf_gp_size (abfd)
6036 && is_nios2_elf_target (info->output_bfd->xvec))
6038 /* Common symbols less than or equal to -G nn bytes are automatically
6039 put into .sbss. */
6040 struct elf32_nios2_link_hash_table *htab;
6042 htab = elf32_nios2_hash_table (info);
6043 if (htab->sbss == NULL)
6045 flagword flags = SEC_IS_COMMON | SEC_LINKER_CREATED;
6047 if (htab->root.dynobj == NULL)
6048 htab->root.dynobj = abfd;
6050 htab->sbss = bfd_make_section_anyway_with_flags (htab->root.dynobj,
6051 ".sbss", flags);
6052 if (htab->sbss == NULL)
6053 return FALSE;
6056 *secp = htab->sbss;
6057 *valp = sym->st_size;
6060 return TRUE;
6063 /* Implement elf_backend_can_make_relative_eh_frame:
6064 Decide whether to attempt to turn absptr or lsda encodings in
6065 shared libraries into pcrel within the given input section. */
6066 static bfd_boolean
6067 nios2_elf32_can_make_relative_eh_frame (bfd *input_bfd ATTRIBUTE_UNUSED,
6068 struct bfd_link_info *info
6069 ATTRIBUTE_UNUSED,
6070 asection *eh_frame_section
6071 ATTRIBUTE_UNUSED)
6073 /* We can't use PC-relative encodings in the .eh_frame section. */
6074 return FALSE;
6077 /* Implement elf_backend_special_sections. */
6078 const struct bfd_elf_special_section elf32_nios2_special_sections[] =
6080 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS,
6081 SHF_ALLOC + SHF_WRITE + SHF_NIOS2_GPREL },
6082 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS,
6083 SHF_ALLOC + SHF_WRITE + SHF_NIOS2_GPREL },
6084 { NULL, 0, 0, 0, 0 }
6087 #define ELF_ARCH bfd_arch_nios2
6088 #define ELF_TARGET_ID NIOS2_ELF_DATA
6089 #define ELF_MACHINE_CODE EM_ALTERA_NIOS2
6091 /* The Nios II MMU uses a 4K page size. */
6093 #define ELF_MAXPAGESIZE 0x1000
6095 #define bfd_elf32_bfd_link_hash_table_create \
6096 nios2_elf32_link_hash_table_create
6098 #define bfd_elf32_bfd_merge_private_bfd_data \
6099 nios2_elf32_merge_private_bfd_data
6101 /* Relocation table lookup macros. */
6103 #define bfd_elf32_bfd_reloc_type_lookup nios2_elf32_bfd_reloc_type_lookup
6104 #define bfd_elf32_bfd_reloc_name_lookup nios2_elf32_bfd_reloc_name_lookup
6106 /* JUMP_TABLE_LINK macros. */
6108 /* elf_info_to_howto (using RELA relocations). */
6110 #define elf_info_to_howto nios2_elf32_info_to_howto
6112 /* elf backend functions. */
6114 #define elf_backend_can_gc_sections 1
6115 #define elf_backend_can_refcount 1
6116 #define elf_backend_plt_readonly 1
6117 #define elf_backend_want_got_plt 1
6118 #define elf_backend_want_dynrelro 1
6119 #define elf_backend_rela_normal 1
6120 #define elf_backend_dtrel_excludes_plt 1
6122 #define elf_backend_relocate_section nios2_elf32_relocate_section
6123 #define elf_backend_section_flags nios2_elf32_section_flags
6124 #define elf_backend_fake_sections nios2_elf32_fake_sections
6125 #define elf_backend_check_relocs nios2_elf32_check_relocs
6127 #define elf_backend_gc_mark_hook nios2_elf32_gc_mark_hook
6128 #define elf_backend_create_dynamic_sections \
6129 nios2_elf32_create_dynamic_sections
6130 #define elf_backend_finish_dynamic_symbol nios2_elf32_finish_dynamic_symbol
6131 #define elf_backend_finish_dynamic_sections \
6132 nios2_elf32_finish_dynamic_sections
6133 #define elf_backend_adjust_dynamic_symbol nios2_elf32_adjust_dynamic_symbol
6134 #define elf_backend_reloc_type_class nios2_elf32_reloc_type_class
6135 #define elf_backend_size_dynamic_sections nios2_elf32_size_dynamic_sections
6136 #define elf_backend_add_symbol_hook nios2_elf_add_symbol_hook
6137 #define elf_backend_copy_indirect_symbol nios2_elf32_copy_indirect_symbol
6138 #define elf_backend_object_p nios2_elf32_object_p
6140 #define elf_backend_grok_prstatus nios2_grok_prstatus
6141 #define elf_backend_grok_psinfo nios2_grok_psinfo
6143 #undef elf_backend_can_make_relative_eh_frame
6144 #define elf_backend_can_make_relative_eh_frame \
6145 nios2_elf32_can_make_relative_eh_frame
6147 #define elf_backend_special_sections elf32_nios2_special_sections
6149 #define TARGET_LITTLE_SYM nios2_elf32_le_vec
6150 #define TARGET_LITTLE_NAME "elf32-littlenios2"
6151 #define TARGET_BIG_SYM nios2_elf32_be_vec
6152 #define TARGET_BIG_NAME "elf32-bignios2"
6154 #define elf_backend_got_header_size 12
6155 #define elf_backend_default_execstack 0
6157 #include "elf32-target.h"