1 /* 32-bit ELF support for Nios II.
2 Copyright (C) 2012-2015 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. */
31 #include "elf/nios2.h"
32 #include "opcode/nios2.h"
33 #include "elf32-nios2.h"
35 /* Use RELA relocations. */
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 **);
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
[] = {
82 HOWTO (R_NIOS2_NONE
, /* type */
84 3, /* size (0 = byte, 1 = short, 2 = long) */
86 FALSE
, /* pc_relative */
88 complain_overflow_dont
, /* complain_on_overflow */
89 bfd_elf_generic_reloc
, /* special_function */
90 "R_NIOS2_NONE", /* name */
91 FALSE
, /* partial_inplace */
94 FALSE
), /* pcrel_offset */
96 /* 16-bit signed immediate relocation. */
97 HOWTO (R_NIOS2_S16
, /* type */
99 2, /* size (0 = byte, 1 = short, 2 = long) */
101 FALSE
, /* pc_relative */
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 */
114 2, /* size (0 = byte, 1 = short, 2 = long) */
116 FALSE
, /* pc_relative */
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 */
128 2, /* size (0 = byte, 1 = short, 2 = long) */
130 TRUE
, /* pc_relative */
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 */
142 2, /* size (0 = byte, 1 = short, 2 = long) */
144 FALSE
, /* pc_relative */
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 */
160 complain_overflow_bitfield
,
161 bfd_elf_generic_reloc
,
168 HOWTO (R_NIOS2_CACHE_OPX
,
174 complain_overflow_bitfield
,
175 bfd_elf_generic_reloc
,
188 complain_overflow_bitfield
,
189 bfd_elf_generic_reloc
,
202 complain_overflow_bitfield
,
203 bfd_elf_generic_reloc
,
216 complain_overflow_dont
,
217 nios2_elf32_hi16_relocate
,
230 complain_overflow_dont
,
231 nios2_elf32_lo16_relocate
,
238 HOWTO (R_NIOS2_HIADJ16
,
244 complain_overflow_dont
,
245 nios2_elf32_hiadj16_relocate
,
252 HOWTO (R_NIOS2_BFD_RELOC_32
,
258 complain_overflow_dont
,
259 bfd_elf_generic_reloc
,
260 "R_NIOS2_BFD_RELOC32",
266 HOWTO (R_NIOS2_BFD_RELOC_16
,
272 complain_overflow_bitfield
,
273 bfd_elf_generic_reloc
,
274 "R_NIOS2_BFD_RELOC16",
280 HOWTO (R_NIOS2_BFD_RELOC_8
,
286 complain_overflow_bitfield
,
287 bfd_elf_generic_reloc
,
288 "R_NIOS2_BFD_RELOC8",
294 HOWTO (R_NIOS2_GPREL
,
300 complain_overflow_dont
,
301 nios2_elf32_gprel_relocate
,
308 HOWTO (R_NIOS2_GNU_VTINHERIT
,
314 complain_overflow_dont
,
316 "R_NIOS2_GNU_VTINHERIT",
322 HOWTO (R_NIOS2_GNU_VTENTRY
,
328 complain_overflow_dont
,
329 _bfd_elf_rel_vtable_reloc_fn
,
330 "R_NIOS2_GNU_VTENTRY",
342 complain_overflow_dont
,
343 nios2_elf32_ujmp_relocate
,
356 complain_overflow_dont
,
357 nios2_elf32_cjmp_relocate
,
364 HOWTO (R_NIOS2_CALLR
,
370 complain_overflow_dont
,
371 nios2_elf32_callr_relocate
,
378 HOWTO (R_NIOS2_ALIGN
,
384 complain_overflow_dont
,
385 nios2_elf32_ignore_reloc
,
393 HOWTO (R_NIOS2_GOT16
,
399 complain_overflow_bitfield
,
400 bfd_elf_generic_reloc
,
407 HOWTO (R_NIOS2_CALL16
,
413 complain_overflow_bitfield
,
414 bfd_elf_generic_reloc
,
421 HOWTO (R_NIOS2_GOTOFF_LO
,
427 complain_overflow_dont
,
428 bfd_elf_generic_reloc
,
435 HOWTO (R_NIOS2_GOTOFF_HA
,
441 complain_overflow_dont
,
442 bfd_elf_generic_reloc
,
449 HOWTO (R_NIOS2_PCREL_LO
,
455 complain_overflow_dont
,
456 nios2_elf32_pcrel_lo16_relocate
,
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
,
478 HOWTO (R_NIOS2_TLS_GD16
,
484 complain_overflow_bitfield
,
485 bfd_elf_generic_reloc
,
492 HOWTO (R_NIOS2_TLS_LDM16
,
498 complain_overflow_bitfield
,
499 bfd_elf_generic_reloc
,
506 HOWTO (R_NIOS2_TLS_LDO16
,
512 complain_overflow_bitfield
,
513 bfd_elf_generic_reloc
,
520 HOWTO (R_NIOS2_TLS_IE16
,
526 complain_overflow_bitfield
,
527 bfd_elf_generic_reloc
,
534 HOWTO (R_NIOS2_TLS_LE16
,
540 complain_overflow_bitfield
,
541 bfd_elf_generic_reloc
,
548 HOWTO (R_NIOS2_TLS_DTPMOD
,
554 complain_overflow_dont
,
555 bfd_elf_generic_reloc
,
556 "R_NIOS2_TLS_DTPMOD",
562 HOWTO (R_NIOS2_TLS_DTPREL
,
568 complain_overflow_dont
,
569 bfd_elf_generic_reloc
,
570 "R_NIOS2_TLS_DTPREL",
576 HOWTO (R_NIOS2_TLS_TPREL
,
582 complain_overflow_dont
,
583 bfd_elf_generic_reloc
,
596 complain_overflow_dont
,
597 bfd_elf_generic_reloc
,
604 HOWTO (R_NIOS2_GLOB_DAT
,
610 complain_overflow_dont
,
611 bfd_elf_generic_reloc
,
618 HOWTO (R_NIOS2_JUMP_SLOT
,
624 complain_overflow_dont
,
625 bfd_elf_generic_reloc
,
632 HOWTO (R_NIOS2_RELATIVE
,
638 complain_overflow_dont
,
639 bfd_elf_generic_reloc
,
646 HOWTO (R_NIOS2_GOTOFF
,
652 complain_overflow_dont
,
653 bfd_elf_generic_reloc
,
660 HOWTO (R_NIOS2_CALL26_NOAT
, /* type */
662 2, /* size (0 = byte, 1 = short, 2 = long) */
664 FALSE
, /* pc_relative */
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
,
680 complain_overflow_dont
,
681 bfd_elf_generic_reloc
,
688 HOWTO (R_NIOS2_GOT_HA
,
694 complain_overflow_dont
,
695 bfd_elf_generic_reloc
,
702 HOWTO (R_NIOS2_CALL_LO
,
708 complain_overflow_dont
,
709 bfd_elf_generic_reloc
,
716 HOWTO (R_NIOS2_CALL_HA
,
722 complain_overflow_dont
,
723 bfd_elf_generic_reloc
,
730 /* Add other relocations here. */
733 static reloc_howto_type elf_nios2_r2_howto_table_rel
[] = {
735 HOWTO (R_NIOS2_NONE
, /* type */
737 0, /* size (0 = byte, 1 = short, 2 = long) */
739 FALSE
, /* pc_relative */
741 complain_overflow_dont
, /* complain_on_overflow */
742 bfd_elf_generic_reloc
, /* special_function */
743 "R_NIOS2_NONE", /* name */
744 FALSE
, /* partial_inplace */
747 FALSE
), /* pcrel_offset */
749 /* 16-bit signed immediate relocation. */
750 HOWTO (R_NIOS2_S16
, /* type */
752 2, /* size (0 = byte, 1 = short, 2 = long) */
754 FALSE
, /* pc_relative */
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 */
767 2, /* size (0 = byte, 1 = short, 2 = long) */
769 FALSE
, /* pc_relative */
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 */
781 2, /* size (0 = byte, 1 = short, 2 = long) */
783 TRUE
, /* pc_relative */
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 */
795 2, /* size (0 = byte, 1 = short, 2 = long) */
797 FALSE
, /* pc_relative */
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 */
813 complain_overflow_bitfield
,
814 bfd_elf_generic_reloc
,
821 HOWTO (R_NIOS2_CACHE_OPX
,
827 complain_overflow_bitfield
,
828 bfd_elf_generic_reloc
,
841 complain_overflow_bitfield
,
842 bfd_elf_generic_reloc
,
855 complain_overflow_bitfield
,
856 bfd_elf_generic_reloc
,
869 complain_overflow_dont
,
870 nios2_elf32_hi16_relocate
,
883 complain_overflow_dont
,
884 nios2_elf32_lo16_relocate
,
891 HOWTO (R_NIOS2_HIADJ16
,
897 complain_overflow_dont
,
898 nios2_elf32_hiadj16_relocate
,
905 HOWTO (R_NIOS2_BFD_RELOC_32
,
911 complain_overflow_dont
,
912 bfd_elf_generic_reloc
,
913 "R_NIOS2_BFD_RELOC32",
919 HOWTO (R_NIOS2_BFD_RELOC_16
,
925 complain_overflow_bitfield
,
926 bfd_elf_generic_reloc
,
927 "R_NIOS2_BFD_RELOC16",
933 HOWTO (R_NIOS2_BFD_RELOC_8
,
939 complain_overflow_bitfield
,
940 bfd_elf_generic_reloc
,
941 "R_NIOS2_BFD_RELOC8",
947 HOWTO (R_NIOS2_GPREL
,
953 complain_overflow_dont
,
954 nios2_elf32_gprel_relocate
,
961 HOWTO (R_NIOS2_GNU_VTINHERIT
,
967 complain_overflow_dont
,
969 "R_NIOS2_GNU_VTINHERIT",
975 HOWTO (R_NIOS2_GNU_VTENTRY
,
981 complain_overflow_dont
,
982 _bfd_elf_rel_vtable_reloc_fn
,
983 "R_NIOS2_GNU_VTENTRY",
995 complain_overflow_dont
,
996 nios2_elf32_ujmp_relocate
,
1003 HOWTO (R_NIOS2_CJMP
,
1009 complain_overflow_dont
,
1010 nios2_elf32_cjmp_relocate
,
1017 HOWTO (R_NIOS2_CALLR
,
1023 complain_overflow_dont
,
1024 nios2_elf32_callr_relocate
,
1031 HOWTO (R_NIOS2_ALIGN
,
1037 complain_overflow_dont
,
1038 nios2_elf32_ignore_reloc
,
1045 HOWTO (R_NIOS2_GOT16
,
1051 complain_overflow_bitfield
,
1052 bfd_elf_generic_reloc
,
1059 HOWTO (R_NIOS2_CALL16
,
1065 complain_overflow_bitfield
,
1066 bfd_elf_generic_reloc
,
1073 HOWTO (R_NIOS2_GOTOFF_LO
,
1079 complain_overflow_dont
,
1080 bfd_elf_generic_reloc
,
1081 "R_NIOS2_GOTOFF_LO",
1087 HOWTO (R_NIOS2_GOTOFF_HA
,
1093 complain_overflow_dont
,
1094 bfd_elf_generic_reloc
,
1095 "R_NIOS2_GOTOFF_HA",
1101 HOWTO (R_NIOS2_PCREL_LO
,
1107 complain_overflow_dont
,
1108 nios2_elf32_pcrel_lo16_relocate
,
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
,
1130 HOWTO (R_NIOS2_TLS_GD16
,
1136 complain_overflow_bitfield
,
1137 bfd_elf_generic_reloc
,
1144 HOWTO (R_NIOS2_TLS_LDM16
,
1150 complain_overflow_bitfield
,
1151 bfd_elf_generic_reloc
,
1152 "R_NIOS2_TLS_LDM16",
1158 HOWTO (R_NIOS2_TLS_LDO16
,
1164 complain_overflow_bitfield
,
1165 bfd_elf_generic_reloc
,
1166 "R_NIOS2_TLS_LDO16",
1172 HOWTO (R_NIOS2_TLS_IE16
,
1178 complain_overflow_bitfield
,
1179 bfd_elf_generic_reloc
,
1186 HOWTO (R_NIOS2_TLS_LE16
,
1192 complain_overflow_bitfield
,
1193 bfd_elf_generic_reloc
,
1200 HOWTO (R_NIOS2_TLS_DTPMOD
,
1206 complain_overflow_dont
,
1207 bfd_elf_generic_reloc
,
1208 "R_NIOS2_TLS_DTPMOD",
1214 HOWTO (R_NIOS2_TLS_DTPREL
,
1220 complain_overflow_dont
,
1221 bfd_elf_generic_reloc
,
1222 "R_NIOS2_TLS_DTPREL",
1228 HOWTO (R_NIOS2_TLS_TPREL
,
1234 complain_overflow_dont
,
1235 bfd_elf_generic_reloc
,
1236 "R_NIOS2_TLS_TPREL",
1242 HOWTO (R_NIOS2_COPY
,
1248 complain_overflow_dont
,
1249 bfd_elf_generic_reloc
,
1256 HOWTO (R_NIOS2_GLOB_DAT
,
1262 complain_overflow_dont
,
1263 bfd_elf_generic_reloc
,
1270 HOWTO (R_NIOS2_JUMP_SLOT
,
1276 complain_overflow_dont
,
1277 bfd_elf_generic_reloc
,
1278 "R_NIOS2_JUMP_SLOT",
1284 HOWTO (R_NIOS2_RELATIVE
,
1290 complain_overflow_dont
,
1291 bfd_elf_generic_reloc
,
1298 HOWTO (R_NIOS2_GOTOFF
,
1304 complain_overflow_dont
,
1305 bfd_elf_generic_reloc
,
1312 HOWTO (R_NIOS2_CALL26_NOAT
, /* type */
1314 2, /* size (0 = byte, 1 = short, 2 = long) */
1316 FALSE
, /* pc_relative */
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
,
1332 complain_overflow_dont
,
1333 bfd_elf_generic_reloc
,
1340 HOWTO (R_NIOS2_GOT_HA
,
1346 complain_overflow_dont
,
1347 bfd_elf_generic_reloc
,
1354 HOWTO (R_NIOS2_CALL_LO
,
1360 complain_overflow_dont
,
1361 bfd_elf_generic_reloc
,
1368 HOWTO (R_NIOS2_CALL_HA
,
1374 complain_overflow_dont
,
1375 bfd_elf_generic_reloc
,
1382 HOWTO (R_NIOS2_R2_S12
,
1388 complain_overflow_signed
,
1389 bfd_elf_generic_reloc
,
1396 HOWTO (R_NIOS2_R2_I10_1_PCREL
,
1402 complain_overflow_signed
,
1403 bfd_elf_generic_reloc
, /* FIXME? */
1404 "R_NIOS2_R2_I10_1_PCREL",
1410 HOWTO (R_NIOS2_R2_T1I7_1_PCREL
,
1416 complain_overflow_signed
,
1417 bfd_elf_generic_reloc
, /* FIXME? */
1418 "R_NIOS2_R2_T1I7_1_PCREL",
1424 HOWTO (R_NIOS2_R2_T1I7_2
,
1430 complain_overflow_unsigned
,
1431 bfd_elf_generic_reloc
,
1432 "R_NIOS2_R2_T1I7_2",
1438 HOWTO (R_NIOS2_R2_T2I4
,
1444 complain_overflow_unsigned
,
1445 bfd_elf_generic_reloc
,
1452 HOWTO (R_NIOS2_R2_T2I4_1
,
1458 complain_overflow_unsigned
,
1459 bfd_elf_generic_reloc
,
1460 "R_NIOS2_R2_T2I4_1",
1466 HOWTO (R_NIOS2_R2_T2I4_2
,
1472 complain_overflow_unsigned
,
1473 bfd_elf_generic_reloc
,
1474 "R_NIOS2_R2_T2I4_2",
1480 HOWTO (R_NIOS2_R2_X1I7_2
,
1486 complain_overflow_unsigned
,
1487 bfd_elf_generic_reloc
,
1488 "R_NIOS2_R2_X1I7_2",
1494 HOWTO (R_NIOS2_R2_X2L5
,
1500 complain_overflow_unsigned
,
1501 bfd_elf_generic_reloc
,
1508 HOWTO (R_NIOS2_R2_F1I5_2
,
1514 complain_overflow_unsigned
,
1515 bfd_elf_generic_reloc
,
1516 "R_NIOS2_R2_F1L5_2",
1522 HOWTO (R_NIOS2_R2_L5I4X1
,
1528 complain_overflow_unsigned
,
1529 bfd_elf_generic_reloc
,
1530 "R_NIOS2_R2_L5I4X1",
1536 HOWTO (R_NIOS2_R2_T1X1I6
,
1542 complain_overflow_unsigned
,
1543 bfd_elf_generic_reloc
,
1544 "R_NIOS2_R2_T1X1I6",
1550 HOWTO (R_NIOS2_R2_T1X1I6_2
,
1556 complain_overflow_unsigned
,
1557 bfd_elf_generic_reloc
,
1558 "R_NIOS2_R2_T1I1X6_2",
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;
1579 /* R2 relocations are a superset of R1, so use that for the lookup
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]));
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
)
1606 return elf_nios2_r2_howto_table_rel
+ i
;
1610 if (i
>= r1_howto_tbl_size
)
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
,
1692 struct elf32_nios2_stub_hash_entry
1694 /* Base hash table entry structure. */
1695 struct bfd_hash_entry bh_root
;
1697 /* The stub section. */
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. */
1716 /* Where this stub is being called from, or, in the case of combined
1717 stub sections, the first input section in the group. */
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. */
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
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
1774 (got_types_used & (GOT_USED | CALL_USED)) == CALL_USED.
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. */
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. */
1808 /* These are the section to which stubs in the group will be
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
;
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. */
1827 /* GOT pointer symbol _gp_got. */
1828 struct elf_link_hash_entry
*h_gp_got
;
1831 bfd_signed_vma refcount
;
1835 /* Small local sym cache. */
1836 struct sym_cache sym_cache
;
1841 struct nios2_elf32_obj_tdata
1843 struct elf_obj_tdata root
;
1845 /* tls_type for each local got entry. */
1846 char *local_got_tls_type
;
1848 /* TRUE if TLS GD relocs have been seen for this object. */
1849 bfd_boolean has_tlsgd
;
1852 #define elf32_nios2_tdata(abfd) \
1853 ((struct nios2_elf32_obj_tdata *) (abfd)->tdata.any)
1855 #define elf32_nios2_local_got_tls_type(abfd) \
1856 (elf32_nios2_tdata (abfd)->local_got_tls_type)
1858 /* The name of the dynamic interpreter. This is put in the .interp
1860 #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
1862 /* PLT implementation for position-dependent code. */
1863 static const bfd_vma nios2_plt_entry
[] = { /* .PLTn: */
1864 0x03c00034, /* movhi r15, %hiadj(plt_got_slot_address) */
1865 0x7bc00017, /* ldw r15, %lo(plt_got_slot_address)(r15) */
1866 0x7800683a /* jmp r15 */
1869 static const bfd_vma nios2_plt0_entry
[] = { /* .PLTresolve */
1870 0x03800034, /* movhi r14, %hiadj(res_0) */
1871 0x73800004, /* addi r14, r14, %lo(res_0) */
1872 0x7b9fc83a, /* sub r15, r15, r14 */
1873 0x03400034, /* movhi r13, %hiadj(_GLOBAL_OFFSET_TABLE_) */
1874 0x6b800017, /* ldw r14, %lo(_GLOBAL_OFFSET_TABLE_+4)(r13) */
1875 0x6b400017, /* ldw r13, %lo(_GLOBAL_OFFSET_TABLE_+8)(r13) */
1876 0x6800683a /* jmp r13 */
1879 /* PLT implementation for position-independent code. */
1880 static const bfd_vma nios2_so_plt_entry
[] = { /* .PLTn */
1881 0x03c00034, /* movhi r15, %hiadj(index * 4) */
1882 0x7bc00004, /* addi r15, r15, %lo(index * 4) */
1883 0x00000006 /* br .PLTresolve */
1886 static const bfd_vma nios2_so_plt0_entry
[] = { /* .PLTresolve */
1887 0x001ce03a, /* nextpc r14 */
1888 0x03400034, /* movhi r13, %hiadj(_GLOBAL_OFFSET_TABLE_) */
1889 0x6b9b883a, /* add r13, r13, r14 */
1890 0x6b800017, /* ldw r14, %lo(_GLOBAL_OFFSET_TABLE_+4)(r13) */
1891 0x6b400017, /* ldw r13, %lo(_GLOBAL_OFFSET_TABLE_+8)(r13) */
1892 0x6800683a /* jmp r13 */
1896 static const bfd_vma nios2_call26_stub_entry
[] = {
1897 0x00400034, /* orhi at, r0, %hiadj(dest) */
1898 0x08400004, /* addi at, at, %lo(dest) */
1899 0x0800683a /* jmp at */
1902 /* Install 16-bit immediate value VALUE at offset OFFSET into section SEC. */
1904 nios2_elf32_install_imm16 (asection
*sec
, bfd_vma offset
, bfd_vma value
)
1906 bfd_vma word
= bfd_get_32 (sec
->owner
, sec
->contents
+ offset
);
1908 BFD_ASSERT(value
<= 0xffff);
1910 bfd_put_32 (sec
->owner
, word
| ((value
& 0xffff) << 6),
1911 sec
->contents
+ offset
);
1914 /* Install COUNT 32-bit values DATA starting at offset OFFSET into
1917 nios2_elf32_install_data (asection
*sec
, const bfd_vma
*data
, bfd_vma offset
,
1922 bfd_put_32 (sec
->owner
, *data
, sec
->contents
+ offset
);
1928 /* The usual way of loading a 32-bit constant into a Nios II register is to
1929 load the high 16 bits in one instruction and then add the low 16 bits with
1930 a signed add. This means that the high halfword needs to be adjusted to
1931 compensate for the sign bit of the low halfword. This function returns the
1932 adjusted high halfword for a given 32-bit constant. */
1934 bfd_vma
hiadj (bfd_vma symbol_value
)
1936 return ((symbol_value
+ 0x8000) >> 16) & 0xffff;
1939 /* Implement elf_backend_grok_prstatus:
1940 Support for core dump NOTE sections. */
1942 nios2_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
1947 switch (note
->descsz
)
1952 case 212: /* Linux/Nios II */
1954 elf_tdata (abfd
)->core
->signal
= bfd_get_16 (abfd
, note
->descdata
+ 12);
1957 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, note
->descdata
+ 24);
1966 /* Make a ".reg/999" section. */
1967 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
1968 size
, note
->descpos
+ offset
);
1971 /* Implement elf_backend_grok_psinfo. */
1973 nios2_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
1975 switch (note
->descsz
)
1980 case 124: /* Linux/Nios II elf_prpsinfo */
1981 elf_tdata (abfd
)->core
->program
1982 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 28, 16);
1983 elf_tdata (abfd
)->core
->command
1984 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 44, 80);
1987 /* Note that for some reason, a spurious space is tacked
1988 onto the end of the args in some (at least one anyway)
1989 implementations, so strip it off if it exists. */
1992 char *command
= elf_tdata (abfd
)->core
->command
;
1993 int n
= strlen (command
);
1995 if (0 < n
&& command
[n
- 1] == ' ')
1996 command
[n
- 1] = '\0';
2002 /* Assorted hash table functions. */
2004 /* Initialize an entry in the stub hash table. */
2005 static struct bfd_hash_entry
*
2006 stub_hash_newfunc (struct bfd_hash_entry
*entry
,
2007 struct bfd_hash_table
*table
,
2010 /* Allocate the structure if it has not already been allocated by a
2014 entry
= bfd_hash_allocate (table
,
2015 sizeof (struct elf32_nios2_stub_hash_entry
));
2020 /* Call the allocation method of the superclass. */
2021 entry
= bfd_hash_newfunc (entry
, table
, string
);
2024 struct elf32_nios2_stub_hash_entry
*hsh
;
2026 /* Initialize the local fields. */
2027 hsh
= (struct elf32_nios2_stub_hash_entry
*) entry
;
2028 hsh
->stub_sec
= NULL
;
2029 hsh
->stub_offset
= 0;
2030 hsh
->target_value
= 0;
2031 hsh
->target_section
= NULL
;
2032 hsh
->stub_type
= nios2_stub_none
;
2040 /* Create an entry in a Nios II ELF linker hash table. */
2041 static struct bfd_hash_entry
*
2042 link_hash_newfunc (struct bfd_hash_entry
*entry
,
2043 struct bfd_hash_table
*table
, const char *string
)
2045 /* Allocate the structure if it has not already been allocated by a
2049 entry
= bfd_hash_allocate (table
,
2050 sizeof (struct elf32_nios2_link_hash_entry
));
2055 /* Call the allocation method of the superclass. */
2056 entry
= _bfd_elf_link_hash_newfunc (entry
, table
, string
);
2059 struct elf32_nios2_link_hash_entry
*eh
;
2061 eh
= (struct elf32_nios2_link_hash_entry
*) entry
;
2062 eh
->hsh_cache
= NULL
;
2063 eh
->dyn_relocs
= NULL
;
2064 eh
->tls_type
= GOT_UNKNOWN
;
2065 eh
->got_types_used
= 0;
2071 /* Section name for stubs is the associated section name plus this
2073 #define STUB_SUFFIX ".stub"
2075 /* Build a name for an entry in the stub hash table. */
2077 nios2_stub_name (const asection
*input_section
,
2078 const asection
*sym_sec
,
2079 const struct elf32_nios2_link_hash_entry
*hh
,
2080 const Elf_Internal_Rela
*rel
,
2081 enum elf32_nios2_stub_type stub_type
)
2085 char stubpos
= (stub_type
== nios2_stub_call26_before
) ? 'b' : 'a';
2089 len
= 8 + 1 + 1 + 1+ strlen (hh
->root
.root
.root
.string
) + 1 + 8 + 1;
2090 stub_name
= bfd_malloc (len
);
2091 if (stub_name
!= NULL
)
2093 sprintf (stub_name
, "%08x_%c_%s+%x",
2094 input_section
->id
& 0xffffffff,
2096 hh
->root
.root
.root
.string
,
2097 (int) rel
->r_addend
& 0xffffffff);
2102 len
= 8 + 1 + 1 + 1+ 8 + 1 + 8 + 1 + 8 + 1;
2103 stub_name
= bfd_malloc (len
);
2104 if (stub_name
!= NULL
)
2106 sprintf (stub_name
, "%08x_%c_%x:%x+%x",
2107 input_section
->id
& 0xffffffff,
2109 sym_sec
->id
& 0xffffffff,
2110 (int) ELF32_R_SYM (rel
->r_info
) & 0xffffffff,
2111 (int) rel
->r_addend
& 0xffffffff);
2117 /* Look up an entry in the stub hash. Stub entries are cached because
2118 creating the stub name takes a bit of time. */
2119 static struct elf32_nios2_stub_hash_entry
*
2120 nios2_get_stub_entry (const asection
*input_section
,
2121 const asection
*sym_sec
,
2122 struct elf32_nios2_link_hash_entry
*hh
,
2123 const Elf_Internal_Rela
*rel
,
2124 struct elf32_nios2_link_hash_table
*htab
,
2125 enum elf32_nios2_stub_type stub_type
)
2127 struct elf32_nios2_stub_hash_entry
*hsh
;
2128 const asection
*id_sec
;
2130 /* If this input section is part of a group of sections sharing one
2131 stub section, then use the id of the first/last section in the group,
2132 depending on the stub section placement relative to the group.
2133 Stub names need to include a section id, as there may well be
2134 more than one stub used to reach say, printf, and we need to
2135 distinguish between them. */
2136 if (stub_type
== nios2_stub_call26_before
)
2137 id_sec
= htab
->stub_group
[input_section
->id
].first_sec
;
2139 id_sec
= htab
->stub_group
[input_section
->id
].last_sec
;
2141 if (hh
!= NULL
&& hh
->hsh_cache
!= NULL
2142 && hh
->hsh_cache
->hh
== hh
2143 && hh
->hsh_cache
->id_sec
== id_sec
2144 && hh
->hsh_cache
->stub_type
== stub_type
)
2146 hsh
= hh
->hsh_cache
;
2152 stub_name
= nios2_stub_name (id_sec
, sym_sec
, hh
, rel
, stub_type
);
2153 if (stub_name
== NULL
)
2156 hsh
= nios2_stub_hash_lookup (&htab
->bstab
,
2157 stub_name
, FALSE
, FALSE
);
2160 hh
->hsh_cache
= hsh
;
2168 /* Add a new stub entry to the stub hash. Not all fields of the new
2169 stub entry are initialised. */
2170 static struct elf32_nios2_stub_hash_entry
*
2171 nios2_add_stub (const char *stub_name
,
2173 struct elf32_nios2_link_hash_table
*htab
,
2174 enum elf32_nios2_stub_type stub_type
)
2178 asection
**secptr
, **linkptr
;
2179 struct elf32_nios2_stub_hash_entry
*hsh
;
2182 if (stub_type
== nios2_stub_call26_before
)
2184 link_sec
= htab
->stub_group
[section
->id
].first_sec
;
2185 secptr
= &(htab
->stub_group
[section
->id
].first_stub_sec
);
2186 linkptr
= &(htab
->stub_group
[link_sec
->id
].first_stub_sec
);
2191 link_sec
= htab
->stub_group
[section
->id
].last_sec
;
2192 secptr
= &(htab
->stub_group
[section
->id
].last_stub_sec
);
2193 linkptr
= &(htab
->stub_group
[link_sec
->id
].last_stub_sec
);
2197 if (stub_sec
== NULL
)
2199 stub_sec
= *linkptr
;
2200 if (stub_sec
== NULL
)
2206 namelen
= strlen (link_sec
->name
);
2207 len
= namelen
+ sizeof (STUB_SUFFIX
);
2208 s_name
= bfd_alloc (htab
->stub_bfd
, len
);
2212 memcpy (s_name
, link_sec
->name
, namelen
);
2213 memcpy (s_name
+ namelen
, STUB_SUFFIX
, sizeof (STUB_SUFFIX
));
2215 stub_sec
= (*htab
->add_stub_section
) (s_name
, link_sec
, afterp
);
2216 if (stub_sec
== NULL
)
2218 *linkptr
= stub_sec
;
2223 /* Enter this entry into the linker stub hash table. */
2224 hsh
= nios2_stub_hash_lookup (&htab
->bstab
, stub_name
,
2228 (*_bfd_error_handler
) (_("%B: cannot create stub entry %s"),
2234 hsh
->stub_sec
= stub_sec
;
2235 hsh
->stub_offset
= 0;
2236 hsh
->id_sec
= link_sec
;
2240 /* Set up various things so that we can make a list of input sections
2241 for each output section included in the link. Returns -1 on error,
2242 0 when no stubs will be needed, and 1 on success. */
2244 nios2_elf32_setup_section_lists (bfd
*output_bfd
, struct bfd_link_info
*info
)
2247 unsigned int bfd_count
;
2248 unsigned int top_id
, top_index
;
2250 asection
**input_list
, **list
;
2252 struct elf32_nios2_link_hash_table
*htab
= elf32_nios2_hash_table (info
);
2254 /* Count the number of input BFDs and find the top input section id. */
2255 for (input_bfd
= info
->input_bfds
, bfd_count
= 0, top_id
= 0;
2257 input_bfd
= input_bfd
->link
.next
)
2260 for (section
= input_bfd
->sections
;
2262 section
= section
->next
)
2264 if (top_id
< section
->id
)
2265 top_id
= section
->id
;
2269 htab
->bfd_count
= bfd_count
;
2271 amt
= sizeof (struct map_stub
) * (top_id
+ 1);
2272 htab
->stub_group
= bfd_zmalloc (amt
);
2273 if (htab
->stub_group
== NULL
)
2276 /* We can't use output_bfd->section_count here to find the top output
2277 section index as some sections may have been removed, and
2278 strip_excluded_output_sections doesn't renumber the indices. */
2279 for (section
= output_bfd
->sections
, top_index
= 0;
2281 section
= section
->next
)
2283 if (top_index
< section
->index
)
2284 top_index
= section
->index
;
2287 htab
->top_index
= top_index
;
2288 amt
= sizeof (asection
*) * (top_index
+ 1);
2289 input_list
= bfd_malloc (amt
);
2290 htab
->input_list
= input_list
;
2291 if (input_list
== NULL
)
2294 /* For sections we aren't interested in, mark their entries with a
2295 value we can check later. */
2296 list
= input_list
+ top_index
;
2298 *list
= bfd_abs_section_ptr
;
2299 while (list
-- != input_list
);
2301 for (section
= output_bfd
->sections
;
2303 section
= section
->next
)
2305 /* FIXME: This is a bit of hack. Currently our .ctors and .dtors
2306 * have PC relative relocs in them but no code flag set. */
2307 if (((section
->flags
& SEC_CODE
) != 0) ||
2308 strcmp(".ctors", section
->name
) ||
2309 strcmp(".dtors", section
->name
))
2310 input_list
[section
->index
] = NULL
;
2316 /* The linker repeatedly calls this function for each input section,
2317 in the order that input sections are linked into output sections.
2318 Build lists of input sections to determine groupings between which
2319 we may insert linker stubs. */
2321 nios2_elf32_next_input_section (struct bfd_link_info
*info
, asection
*isec
)
2323 struct elf32_nios2_link_hash_table
*htab
= elf32_nios2_hash_table (info
);
2325 if (isec
->output_section
->index
<= htab
->top_index
)
2327 asection
**list
= htab
->input_list
+ isec
->output_section
->index
;
2328 if (*list
!= bfd_abs_section_ptr
)
2330 /* Steal the last_sec pointer for our list.
2331 This happens to make the list in reverse order,
2332 which is what we want. */
2333 htab
->stub_group
[isec
->id
].last_sec
= *list
;
2339 /* Segment mask for CALL26 relocation relaxation. */
2340 #define CALL26_SEGMENT(x) ((x) & 0xf0000000)
2342 /* Fudge factor for approximate maximum size of all stubs that might
2343 be inserted by the linker. This does not actually limit the number
2344 of stubs that might be inserted, and only affects strategy for grouping
2345 and placement of stubs. Perhaps this should be computed based on number
2346 of relocations seen, or be specifiable on the command line. */
2347 #define MAX_STUB_SECTION_SIZE 0xffff
2349 /* See whether we can group stub sections together. Grouping stub
2350 sections may result in fewer stubs. More importantly, we need to
2351 put all .init* and .fini* stubs at the end of the .init or
2352 .fini output sections respectively, because glibc splits the
2353 _init and _fini functions into multiple parts. Putting a stub in
2354 the middle of a function is not a good idea.
2355 Rather than computing groups of a maximum fixed size, for Nios II
2356 CALL26 relaxation it makes more sense to compute the groups based on
2357 sections that fit within a 256MB address segment. Also do not allow
2358 a group to span more than one output section, since different output
2359 sections might correspond to different memory banks on a bare-metal
2362 group_sections (struct elf32_nios2_link_hash_table
*htab
)
2364 asection
**list
= htab
->input_list
+ htab
->top_index
;
2367 /* The list is in reverse order so we'll search backwards looking
2368 for the first section that begins in the same memory segment,
2369 marking sections along the way to point at the tail for this
2371 asection
*tail
= *list
;
2372 if (tail
== bfd_abs_section_ptr
)
2374 while (tail
!= NULL
)
2376 bfd_vma start
= tail
->output_section
->vma
+ tail
->output_offset
;
2377 bfd_vma end
= start
+ tail
->size
;
2378 bfd_vma segment
= CALL26_SEGMENT (end
);
2381 if (segment
!= CALL26_SEGMENT (start
)
2382 || segment
!= CALL26_SEGMENT (end
+ MAX_STUB_SECTION_SIZE
))
2383 /* This section spans more than one memory segment, or is
2384 close enough to the end of the segment that adding stub
2385 sections before it might cause it to move so that it
2386 spans memory segments, or that stubs added at the end of
2387 this group might overflow into the next memory segment.
2388 Put it in a group by itself to localize the effects. */
2390 prev
= htab
->stub_group
[tail
->id
].last_sec
;
2391 htab
->stub_group
[tail
->id
].last_sec
= tail
;
2392 htab
->stub_group
[tail
->id
].first_sec
= tail
;
2395 /* Collect more sections for this group. */
2397 asection
*curr
, *first
;
2398 for (curr
= tail
; ; curr
= prev
)
2400 prev
= htab
->stub_group
[curr
->id
].last_sec
;
2402 || tail
->output_section
!= prev
->output_section
2403 || (CALL26_SEGMENT (prev
->output_section
->vma
2404 + prev
->output_offset
)
2409 for (curr
= tail
; ; curr
= prev
)
2411 prev
= htab
->stub_group
[curr
->id
].last_sec
;
2412 htab
->stub_group
[curr
->id
].last_sec
= tail
;
2413 htab
->stub_group
[curr
->id
].first_sec
= first
;
2419 /* Reset tail for the next group. */
2423 while (list
-- != htab
->input_list
);
2424 free (htab
->input_list
);
2427 /* Determine the type of stub needed, if any, for a call. */
2428 static enum elf32_nios2_stub_type
2429 nios2_type_of_stub (asection
*input_sec
,
2430 const Elf_Internal_Rela
*rel
,
2431 struct elf32_nios2_link_hash_entry
*hh
,
2432 struct elf32_nios2_link_hash_table
*htab
,
2433 bfd_vma destination
,
2434 struct bfd_link_info
*info ATTRIBUTE_UNUSED
)
2436 bfd_vma location
, segment
, start
, end
;
2437 asection
*s0
, *s1
, *s
;
2440 !(hh
->root
.root
.type
== bfd_link_hash_defined
2441 || hh
->root
.root
.type
== bfd_link_hash_defweak
))
2442 return nios2_stub_none
;
2444 /* Determine where the call point is. */
2445 location
= (input_sec
->output_section
->vma
2446 + input_sec
->output_offset
+ rel
->r_offset
);
2447 segment
= CALL26_SEGMENT (location
);
2449 /* Nios II CALL and JMPI instructions can transfer control to addresses
2450 within the same 256MB segment as the PC. */
2451 if (segment
== CALL26_SEGMENT (destination
))
2452 return nios2_stub_none
;
2454 /* Find the start and end addresses of the stub group. Also account for
2455 any already-created stub sections for this group. Note that for stubs
2456 in the end section, only the first instruction of the last stub
2457 (12 bytes long) needs to be within range. */
2458 s0
= htab
->stub_group
[input_sec
->id
].first_sec
;
2459 s
= htab
->stub_group
[s0
->id
].first_stub_sec
;
2460 if (s
!= NULL
&& s
->size
> 0)
2461 start
= s
->output_section
->vma
+ s
->output_offset
;
2463 start
= s0
->output_section
->vma
+ s0
->output_offset
;
2465 s1
= htab
->stub_group
[input_sec
->id
].last_sec
;
2466 s
= htab
->stub_group
[s1
->id
].last_stub_sec
;
2467 if (s
!= NULL
&& s
->size
> 0)
2468 end
= s
->output_section
->vma
+ s
->output_offset
+ s
->size
- 8;
2470 end
= s1
->output_section
->vma
+ s1
->output_offset
+ s1
->size
;
2472 BFD_ASSERT (start
< end
);
2473 BFD_ASSERT (start
<= location
);
2474 BFD_ASSERT (location
< end
);
2476 /* Put stubs at the end of the group unless that is not a valid
2477 location and the beginning of the group is. It might be that
2478 neither the beginning nor end works if we have an input section
2479 so large that it spans multiple segment boundaries. In that
2480 case, punt; the end result will be a relocation overflow error no
2481 matter what we do here.
2483 Note that adding stubs pushes up the addresses of all subsequent
2484 sections, so that stubs allocated on one pass through the
2485 relaxation loop may not be valid on the next pass. (E.g., we may
2486 allocate a stub at the beginning of the section on one pass and
2487 find that the call site has been bumped into the next memory
2488 segment on the next pass.) The important thing to note is that
2489 we never try to reclaim the space allocated to such unused stubs,
2490 so code size and section addresses can only increase with each
2491 iteration. Accounting for the start and end addresses of the
2492 already-created stub sections ensures that when the algorithm
2493 converges, it converges accurately, with the entire appropriate
2494 stub section accessible from the call site and not just the
2495 address at the start or end of the stub group proper. */
2497 if (segment
== CALL26_SEGMENT (end
))
2498 return nios2_stub_call26_after
;
2499 else if (segment
== CALL26_SEGMENT (start
))
2500 return nios2_stub_call26_before
;
2502 /* Perhaps this should be a dedicated error code. */
2503 return nios2_stub_none
;
2507 nios2_build_one_stub (struct bfd_hash_entry
*gen_entry
, void *in_arg ATTRIBUTE_UNUSED
)
2509 struct elf32_nios2_stub_hash_entry
*hsh
2510 = (struct elf32_nios2_stub_hash_entry
*) gen_entry
;
2511 asection
*stub_sec
= hsh
->stub_sec
;
2514 /* Make a note of the offset within the stubs for this entry. */
2515 hsh
->stub_offset
= stub_sec
->size
;
2517 switch (hsh
->stub_type
)
2519 case nios2_stub_call26_before
:
2520 case nios2_stub_call26_after
:
2521 /* A call26 stub looks like:
2522 orhi at, %hiadj(dest)
2523 addi at, at, %lo(dest)
2525 Note that call/jmpi instructions can't be used in PIC code
2526 so there is no reason for the stub to be PIC, either. */
2527 sym_value
= (hsh
->target_value
2528 + hsh
->target_section
->output_offset
2529 + hsh
->target_section
->output_section
->vma
2532 nios2_elf32_install_data (stub_sec
, nios2_call26_stub_entry
,
2533 hsh
->stub_offset
, 3);
2534 nios2_elf32_install_imm16 (stub_sec
, hsh
->stub_offset
,
2536 nios2_elf32_install_imm16 (stub_sec
, hsh
->stub_offset
+ 4,
2537 (sym_value
& 0xffff));
2538 stub_sec
->size
+= 12;
2548 /* As above, but don't actually build the stub. Just bump offset so
2549 we know stub section sizes. */
2551 nios2_size_one_stub (struct bfd_hash_entry
*gen_entry
, void *in_arg ATTRIBUTE_UNUSED
)
2553 struct elf32_nios2_stub_hash_entry
*hsh
2554 = (struct elf32_nios2_stub_hash_entry
*) gen_entry
;
2556 switch (hsh
->stub_type
)
2558 case nios2_stub_call26_before
:
2559 case nios2_stub_call26_after
:
2560 hsh
->stub_sec
->size
+= 12;
2569 /* Read in all local syms for all input bfds.
2570 Returns -1 on error, 0 otherwise. */
2573 get_local_syms (bfd
*output_bfd ATTRIBUTE_UNUSED
, bfd
*input_bfd
,
2574 struct bfd_link_info
*info
)
2576 unsigned int bfd_indx
;
2577 Elf_Internal_Sym
*local_syms
, **all_local_syms
;
2578 struct elf32_nios2_link_hash_table
*htab
= elf32_nios2_hash_table (info
);
2580 /* We want to read in symbol extension records only once. To do this
2581 we need to read in the local symbols in parallel and save them for
2582 later use; so hold pointers to the local symbols in an array. */
2583 bfd_size_type amt
= sizeof (Elf_Internal_Sym
*) * htab
->bfd_count
;
2584 all_local_syms
= bfd_zmalloc (amt
);
2585 htab
->all_local_syms
= all_local_syms
;
2586 if (all_local_syms
== NULL
)
2589 /* Walk over all the input BFDs, swapping in local symbols. */
2592 input_bfd
= input_bfd
->link
.next
, bfd_indx
++)
2594 Elf_Internal_Shdr
*symtab_hdr
;
2596 /* We'll need the symbol table in a second. */
2597 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
2598 if (symtab_hdr
->sh_info
== 0)
2601 /* We need an array of the local symbols attached to the input bfd. */
2602 local_syms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
2603 if (local_syms
== NULL
)
2605 local_syms
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
,
2606 symtab_hdr
->sh_info
, 0,
2608 /* Cache them for elf_link_input_bfd. */
2609 symtab_hdr
->contents
= (unsigned char *) local_syms
;
2611 if (local_syms
== NULL
)
2614 all_local_syms
[bfd_indx
] = local_syms
;
2620 /* Determine and set the size of the stub section for a final link. */
2622 nios2_elf32_size_stubs (bfd
*output_bfd
, bfd
*stub_bfd
,
2623 struct bfd_link_info
*info
,
2624 asection
*(*add_stub_section
) (const char *,
2625 asection
*, bfd_boolean
),
2626 void (*layout_sections_again
) (void))
2628 bfd_boolean stub_changed
= FALSE
;
2629 struct elf32_nios2_link_hash_table
*htab
= elf32_nios2_hash_table (info
);
2631 /* Stash our params away. */
2632 htab
->stub_bfd
= stub_bfd
;
2633 htab
->add_stub_section
= add_stub_section
;
2634 htab
->layout_sections_again
= layout_sections_again
;
2636 /* FIXME: We only compute the section groups once. This could cause
2637 problems if adding a large stub section causes following sections,
2638 or parts of them, to move into another segment. However, this seems
2639 to be consistent with the way other back ends handle this.... */
2640 group_sections (htab
);
2642 if (get_local_syms (output_bfd
, info
->input_bfds
, info
))
2644 if (htab
->all_local_syms
)
2645 goto error_ret_free_local
;
2652 unsigned int bfd_indx
;
2655 for (input_bfd
= info
->input_bfds
, bfd_indx
= 0;
2657 input_bfd
= input_bfd
->link
.next
, bfd_indx
++)
2659 Elf_Internal_Shdr
*symtab_hdr
;
2661 Elf_Internal_Sym
*local_syms
;
2663 /* We'll need the symbol table in a second. */
2664 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
2665 if (symtab_hdr
->sh_info
== 0)
2668 local_syms
= htab
->all_local_syms
[bfd_indx
];
2670 /* Walk over each section attached to the input bfd. */
2671 for (section
= input_bfd
->sections
;
2673 section
= section
->next
)
2675 Elf_Internal_Rela
*internal_relocs
, *irelaend
, *irela
;
2677 /* If there aren't any relocs, then there's nothing more
2679 if ((section
->flags
& SEC_RELOC
) == 0
2680 || section
->reloc_count
== 0)
2683 /* If this section is a link-once section that will be
2684 discarded, then don't create any stubs. */
2685 if (section
->output_section
== NULL
2686 || section
->output_section
->owner
!= output_bfd
)
2689 /* Get the relocs. */
2691 = _bfd_elf_link_read_relocs (input_bfd
, section
, NULL
, NULL
,
2693 if (internal_relocs
== NULL
)
2694 goto error_ret_free_local
;
2696 /* Now examine each relocation. */
2697 irela
= internal_relocs
;
2698 irelaend
= irela
+ section
->reloc_count
;
2699 for (; irela
< irelaend
; irela
++)
2701 unsigned int r_type
, r_indx
;
2702 enum elf32_nios2_stub_type stub_type
;
2703 struct elf32_nios2_stub_hash_entry
*hsh
;
2706 bfd_vma destination
;
2707 struct elf32_nios2_link_hash_entry
*hh
;
2709 const asection
*id_sec
;
2711 r_type
= ELF32_R_TYPE (irela
->r_info
);
2712 r_indx
= ELF32_R_SYM (irela
->r_info
);
2714 if (r_type
>= (unsigned int) R_NIOS2_ILLEGAL
)
2716 bfd_set_error (bfd_error_bad_value
);
2717 error_ret_free_internal
:
2718 if (elf_section_data (section
)->relocs
== NULL
)
2719 free (internal_relocs
);
2720 goto error_ret_free_local
;
2723 /* Only look for stubs on CALL and JMPI instructions. */
2724 if (r_type
!= (unsigned int) R_NIOS2_CALL26
)
2727 /* Now determine the call target, its name, value,
2733 if (r_indx
< symtab_hdr
->sh_info
)
2735 /* It's a local symbol. */
2736 Elf_Internal_Sym
*sym
;
2737 Elf_Internal_Shdr
*hdr
;
2740 sym
= local_syms
+ r_indx
;
2741 if (ELF_ST_TYPE (sym
->st_info
) != STT_SECTION
)
2742 sym_value
= sym
->st_value
;
2743 shndx
= sym
->st_shndx
;
2744 if (shndx
< elf_numsections (input_bfd
))
2746 hdr
= elf_elfsections (input_bfd
)[shndx
];
2747 sym_sec
= hdr
->bfd_section
;
2748 destination
= (sym_value
+ irela
->r_addend
2749 + sym_sec
->output_offset
2750 + sym_sec
->output_section
->vma
);
2755 /* It's an external symbol. */
2758 e_indx
= r_indx
- symtab_hdr
->sh_info
;
2759 hh
= ((struct elf32_nios2_link_hash_entry
*)
2760 elf_sym_hashes (input_bfd
)[e_indx
]);
2762 while (hh
->root
.root
.type
== bfd_link_hash_indirect
2763 || hh
->root
.root
.type
== bfd_link_hash_warning
)
2764 hh
= ((struct elf32_nios2_link_hash_entry
*)
2765 hh
->root
.root
.u
.i
.link
);
2767 if (hh
->root
.root
.type
== bfd_link_hash_defined
2768 || hh
->root
.root
.type
== bfd_link_hash_defweak
)
2770 sym_sec
= hh
->root
.root
.u
.def
.section
;
2771 sym_value
= hh
->root
.root
.u
.def
.value
;
2773 if (sym_sec
->output_section
!= NULL
)
2774 destination
= (sym_value
+ irela
->r_addend
2775 + sym_sec
->output_offset
2776 + sym_sec
->output_section
->vma
);
2780 else if (hh
->root
.root
.type
== bfd_link_hash_undefweak
)
2782 if (! bfd_link_pic (info
))
2785 else if (hh
->root
.root
.type
== bfd_link_hash_undefined
)
2787 if (! (info
->unresolved_syms_in_objects
== RM_IGNORE
2788 && (ELF_ST_VISIBILITY (hh
->root
.other
)
2794 bfd_set_error (bfd_error_bad_value
);
2795 goto error_ret_free_internal
;
2799 /* Determine what (if any) linker stub is needed. */
2800 stub_type
= nios2_type_of_stub (section
, irela
, hh
, htab
,
2802 if (stub_type
== nios2_stub_none
)
2805 /* Support for grouping stub sections. */
2806 if (stub_type
== nios2_stub_call26_before
)
2807 id_sec
= htab
->stub_group
[section
->id
].first_sec
;
2809 id_sec
= htab
->stub_group
[section
->id
].last_sec
;
2811 /* Get the name of this stub. */
2812 stub_name
= nios2_stub_name (id_sec
, sym_sec
, hh
, irela
,
2815 goto error_ret_free_internal
;
2817 hsh
= nios2_stub_hash_lookup (&htab
->bstab
,
2822 /* The proper stub has already been created. */
2827 hsh
= nios2_add_stub (stub_name
, section
, htab
, stub_type
);
2831 goto error_ret_free_internal
;
2833 hsh
->target_value
= sym_value
;
2834 hsh
->target_section
= sym_sec
;
2835 hsh
->stub_type
= stub_type
;
2837 hsh
->addend
= irela
->r_addend
;
2838 stub_changed
= TRUE
;
2841 /* We're done with the internal relocs, free them. */
2842 if (elf_section_data (section
)->relocs
== NULL
)
2843 free (internal_relocs
);
2850 /* OK, we've added some stubs. Find out the new size of the
2852 for (stub_sec
= htab
->stub_bfd
->sections
;
2854 stub_sec
= stub_sec
->next
)
2857 bfd_hash_traverse (&htab
->bstab
, nios2_size_one_stub
, htab
);
2859 /* Ask the linker to do its stuff. */
2860 (*htab
->layout_sections_again
) ();
2861 stub_changed
= FALSE
;
2864 free (htab
->all_local_syms
);
2867 error_ret_free_local
:
2868 free (htab
->all_local_syms
);
2872 /* Build all the stubs associated with the current output file. The
2873 stubs are kept in a hash table attached to the main linker hash
2874 table. This function is called via nios2elf_finish in the linker. */
2876 nios2_elf32_build_stubs (struct bfd_link_info
*info
)
2879 struct bfd_hash_table
*table
;
2880 struct elf32_nios2_link_hash_table
*htab
;
2882 htab
= elf32_nios2_hash_table (info
);
2884 for (stub_sec
= htab
->stub_bfd
->sections
;
2886 stub_sec
= stub_sec
->next
)
2887 /* The stub_bfd may contain non-stub sections if it is also the
2888 dynobj. Any such non-stub sections are created with the
2889 SEC_LINKER_CREATED flag set, while stub sections do not
2890 have that flag. Ignore any non-stub sections here. */
2891 if ((stub_sec
->flags
& SEC_LINKER_CREATED
) == 0)
2895 /* Allocate memory to hold the linker stubs. */
2896 size
= stub_sec
->size
;
2897 stub_sec
->contents
= bfd_zalloc (htab
->stub_bfd
, size
);
2898 if (stub_sec
->contents
== NULL
&& size
!= 0)
2903 /* Build the stubs as directed by the stub hash table. */
2904 table
= &htab
->bstab
;
2905 bfd_hash_traverse (table
, nios2_build_one_stub
, info
);
2911 #define is_nios2_elf(bfd) \
2912 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
2913 && elf_object_id (bfd) == NIOS2_ELF_DATA)
2915 /* Merge backend specific data from an object file to the output
2916 object file when linking. */
2919 nios2_elf32_merge_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
2924 if (!is_nios2_elf (ibfd
) || !is_nios2_elf (obfd
))
2927 /* Check if we have the same endianness. */
2928 if (! _bfd_generic_verify_endian_match (ibfd
, obfd
))
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
;
2942 case EF_NIOS2_ARCH_R1
:
2943 bfd_default_set_arch_mach (obfd
, bfd_arch_nios2
, bfd_mach_nios2r1
);
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
);
2953 bfd_default_set_arch_mach (obfd
, bfd_arch_nios2
, bfd_mach_nios2r2
);
2958 /* Incompatible flags. */
2959 else if (new_flags
!= old_flags
)
2961 /* So far, the only incompatible flags denote incompatible
2963 (*_bfd_error_handler
)
2964 (_("error: %B: Conflicting CPU architectures %d/%d"),
2965 ibfd
, new_flags
, old_flags
);
2966 bfd_set_error (bfd_error_bad_value
);
2970 /* Merge Tag_compatibility attributes and any common GNU ones. */
2971 _bfd_elf_merge_object_attributes (ibfd
, obfd
);
2977 /* Implement bfd_elf32_bfd_reloc_type_lookup:
2978 Given a BFD reloc type, return a howto structure. */
2979 static reloc_howto_type
*
2980 nios2_elf32_bfd_reloc_type_lookup (bfd
*abfd
,
2981 bfd_reloc_code_real_type code
)
2986 i
< (int) (sizeof (nios2_reloc_map
) / sizeof (struct elf_reloc_map
));
2988 if (nios2_reloc_map
[i
].bfd_val
== code
)
2989 return lookup_howto (nios2_reloc_map
[i
].elf_val
, abfd
);
2993 /* Implement bfd_elf32_bfd_reloc_name_lookup:
2994 Given a reloc name, return a howto structure. */
2995 static reloc_howto_type
*
2996 nios2_elf32_bfd_reloc_name_lookup (bfd
*abfd
,
3000 reloc_howto_type
*howto_tbl
;
3003 if (BFD_IS_R2 (abfd
))
3005 howto_tbl
= elf_nios2_r2_howto_table_rel
;
3006 howto_tbl_size
= (int) (sizeof (elf_nios2_r2_howto_table_rel
)
3007 / sizeof (elf_nios2_r2_howto_table_rel
[0]));
3011 howto_tbl
= elf_nios2_r1_howto_table_rel
;
3012 howto_tbl_size
= (int) (sizeof (elf_nios2_r1_howto_table_rel
)
3013 / sizeof (elf_nios2_r1_howto_table_rel
[0]));
3016 for (i
= 0; i
< howto_tbl_size
; i
++)
3017 if (howto_tbl
[i
].name
&& strcasecmp (howto_tbl
[i
].name
, r_name
) == 0)
3018 return howto_tbl
+ i
;
3022 /* Implement elf_info_to_howto:
3023 Given a ELF32 relocation, fill in a arelent structure. */
3025 nios2_elf32_info_to_howto (bfd
*abfd
, arelent
*cache_ptr
,
3026 Elf_Internal_Rela
*dst
)
3028 unsigned int r_type
;
3030 r_type
= ELF32_R_TYPE (dst
->r_info
);
3031 cache_ptr
->howto
= lookup_howto (r_type
, abfd
);
3034 /* Return the base VMA address which should be subtracted from real addresses
3035 when resolving @dtpoff relocation.
3036 This is PT_TLS segment p_vaddr. */
3038 dtpoff_base (struct bfd_link_info
*info
)
3040 /* If tls_sec is NULL, we should have signalled an error already. */
3041 if (elf_hash_table (info
)->tls_sec
== NULL
)
3043 return elf_hash_table (info
)->tls_sec
->vma
;
3046 /* Return the relocation value for @tpoff relocation
3047 if STT_TLS virtual address is ADDRESS. */
3049 tpoff (struct bfd_link_info
*info
, bfd_vma address
)
3051 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
3053 /* If tls_sec is NULL, we should have signalled an error already. */
3054 if (htab
->tls_sec
== NULL
)
3056 return address
- htab
->tls_sec
->vma
;
3059 /* Set the GP value for OUTPUT_BFD. Returns FALSE if this is a
3060 dangerous relocation. */
3062 nios2_elf_assign_gp (bfd
*output_bfd
, bfd_vma
*pgp
, struct bfd_link_info
*info
)
3065 bfd_boolean gp_found
;
3066 struct bfd_hash_entry
*h
;
3067 struct bfd_link_hash_entry
*lh
;
3069 /* If we've already figured out what GP will be, just return it. */
3070 *pgp
= _bfd_get_gp_value (output_bfd
);
3074 h
= bfd_hash_lookup (&info
->hash
->table
, "_gp", FALSE
, FALSE
);
3075 lh
= (struct bfd_link_hash_entry
*) h
;
3081 case bfd_link_hash_undefined
:
3082 case bfd_link_hash_undefweak
:
3083 case bfd_link_hash_common
:
3086 case bfd_link_hash_defined
:
3087 case bfd_link_hash_defweak
:
3089 *pgp
= lh
->u
.def
.value
;
3091 case bfd_link_hash_indirect
:
3092 case bfd_link_hash_warning
:
3094 /* @@FIXME ignoring warning for now */
3096 case bfd_link_hash_new
:
3106 /* Only get the error once. */
3108 _bfd_set_gp_value (output_bfd
, *pgp
);
3112 _bfd_set_gp_value (output_bfd
, *pgp
);
3117 /* Retrieve the previously cached _gp pointer, returning bfd_reloc_dangerous
3118 if it's not available as we don't have a link_info pointer available here
3119 to look it up in the output symbol table. We don't need to adjust the
3120 symbol value for an external symbol if we are producing relocatable
3122 static bfd_reloc_status_type
3123 nios2_elf_final_gp (bfd
*output_bfd
, asymbol
*symbol
, bfd_boolean relocatable
,
3124 char **error_message
, bfd_vma
*pgp
)
3126 if (bfd_is_und_section (symbol
->section
) && !relocatable
)
3129 return bfd_reloc_undefined
;
3132 *pgp
= _bfd_get_gp_value (output_bfd
);
3133 if (*pgp
== 0 && (!relocatable
|| (symbol
->flags
& BSF_SECTION_SYM
) != 0))
3137 /* Make up a value. */
3138 *pgp
= symbol
->section
->output_section
->vma
+ 0x4000;
3139 _bfd_set_gp_value (output_bfd
, *pgp
);
3144 = (char *) _("global pointer relative relocation when _gp not defined");
3145 return bfd_reloc_dangerous
;
3149 return bfd_reloc_ok
;
3152 /* Do the relocations that require special handling. */
3153 static bfd_reloc_status_type
3154 nios2_elf32_do_hi16_relocate (bfd
*abfd
, reloc_howto_type
*howto
,
3155 asection
*input_section
,
3156 bfd_byte
*data
, bfd_vma offset
,
3157 bfd_vma symbol_value
, bfd_vma addend
)
3159 symbol_value
= symbol_value
+ addend
;
3161 symbol_value
= (symbol_value
>> 16) & 0xffff;
3162 return _bfd_final_link_relocate (howto
, abfd
, input_section
,
3163 data
, offset
, symbol_value
, addend
);
3166 static bfd_reloc_status_type
3167 nios2_elf32_do_lo16_relocate (bfd
*abfd
, reloc_howto_type
*howto
,
3168 asection
*input_section
,
3169 bfd_byte
*data
, bfd_vma offset
,
3170 bfd_vma symbol_value
, bfd_vma addend
)
3172 symbol_value
= symbol_value
+ addend
;
3174 symbol_value
= symbol_value
& 0xffff;
3175 return _bfd_final_link_relocate (howto
, abfd
, input_section
,
3176 data
, offset
, symbol_value
, addend
);
3179 static bfd_reloc_status_type
3180 nios2_elf32_do_hiadj16_relocate (bfd
*abfd
, reloc_howto_type
*howto
,
3181 asection
*input_section
,
3182 bfd_byte
*data
, bfd_vma offset
,
3183 bfd_vma symbol_value
, bfd_vma addend
)
3185 symbol_value
= symbol_value
+ addend
;
3187 symbol_value
= hiadj(symbol_value
);
3188 return _bfd_final_link_relocate (howto
, abfd
, input_section
, data
, offset
,
3189 symbol_value
, addend
);
3192 static bfd_reloc_status_type
3193 nios2_elf32_do_pcrel_lo16_relocate (bfd
*abfd
, reloc_howto_type
*howto
,
3194 asection
*input_section
,
3195 bfd_byte
*data
, bfd_vma offset
,
3196 bfd_vma symbol_value
, bfd_vma addend
)
3198 symbol_value
= symbol_value
+ addend
;
3200 symbol_value
= symbol_value
& 0xffff;
3201 return _bfd_final_link_relocate (howto
, abfd
, input_section
,
3202 data
, offset
, symbol_value
, addend
);
3205 static bfd_reloc_status_type
3206 nios2_elf32_do_pcrel_hiadj16_relocate (bfd
*abfd
, reloc_howto_type
*howto
,
3207 asection
*input_section
,
3208 bfd_byte
*data
, bfd_vma offset
,
3209 bfd_vma symbol_value
, bfd_vma addend
)
3211 symbol_value
= symbol_value
+ addend
;
3212 symbol_value
-= (input_section
->output_section
->vma
3213 + input_section
->output_offset
);
3214 symbol_value
-= offset
;
3216 symbol_value
= hiadj(symbol_value
);
3217 return _bfd_final_link_relocate (howto
, abfd
, input_section
, data
, offset
,
3218 symbol_value
, addend
);
3221 static bfd_reloc_status_type
3222 nios2_elf32_do_pcrel16_relocate (bfd
*abfd
, reloc_howto_type
*howto
,
3223 asection
*input_section
,
3224 bfd_byte
*data
, bfd_vma offset
,
3225 bfd_vma symbol_value
, bfd_vma addend
)
3227 /* NIOS2 pc relative relocations are relative to the next 32-bit instruction
3228 so we need to subtract 4 before doing a final_link_relocate. */
3229 symbol_value
= symbol_value
+ addend
- 4;
3231 return _bfd_final_link_relocate (howto
, abfd
, input_section
,
3232 data
, offset
, symbol_value
, addend
);
3235 static bfd_reloc_status_type
3236 nios2_elf32_do_call26_relocate (bfd
*abfd
, reloc_howto_type
*howto
,
3237 asection
*input_section
,
3238 bfd_byte
*data
, bfd_vma offset
,
3239 bfd_vma symbol_value
, bfd_vma addend
)
3241 /* Check that the relocation is in the same page as the current address. */
3242 if (CALL26_SEGMENT (symbol_value
+ addend
)
3243 != CALL26_SEGMENT (input_section
->output_section
->vma
3244 + input_section
->output_offset
3246 return bfd_reloc_overflow
;
3248 /* Check that the target address is correctly aligned on a 4-byte
3250 if ((symbol_value
+ addend
) & 0x3)
3251 return bfd_reloc_overflow
;
3253 return _bfd_final_link_relocate (howto
, abfd
, input_section
,
3254 data
, offset
, symbol_value
, addend
);
3257 static bfd_reloc_status_type
3258 nios2_elf32_do_gprel_relocate (bfd
*abfd
, reloc_howto_type
*howto
,
3259 asection
*input_section
,
3260 bfd_byte
*data
, bfd_vma offset
,
3261 bfd_vma symbol_value
, bfd_vma addend
)
3263 /* Because we need the output_bfd, the special handling is done
3264 in nios2_elf32_relocate_section or in nios2_elf32_gprel_relocate. */
3265 return _bfd_final_link_relocate (howto
, abfd
, input_section
,
3266 data
, offset
, symbol_value
, addend
);
3269 static bfd_reloc_status_type
3270 nios2_elf32_do_ujmp_relocate (bfd
*abfd
, reloc_howto_type
*howto
,
3271 asection
*input_section
,
3272 bfd_byte
*data
, bfd_vma offset
,
3273 bfd_vma symbol_value
, bfd_vma addend
)
3275 bfd_vma symbol_lo16
, symbol_hi16
;
3276 bfd_reloc_status_type r
;
3277 symbol_value
= symbol_value
+ addend
;
3279 symbol_hi16
= (symbol_value
>> 16) & 0xffff;
3280 symbol_lo16
= symbol_value
& 0xffff;
3282 r
= _bfd_final_link_relocate (howto
, abfd
, input_section
,
3283 data
, offset
, symbol_hi16
, addend
);
3285 if (r
== bfd_reloc_ok
)
3286 return _bfd_final_link_relocate (howto
, abfd
, input_section
,
3287 data
, offset
+ 4, symbol_lo16
, addend
);
3292 static bfd_reloc_status_type
3293 nios2_elf32_do_cjmp_relocate (bfd
*abfd
, reloc_howto_type
*howto
,
3294 asection
*input_section
,
3295 bfd_byte
*data
, bfd_vma offset
,
3296 bfd_vma symbol_value
, bfd_vma addend
)
3298 bfd_vma symbol_lo16
, symbol_hi16
;
3299 bfd_reloc_status_type r
;
3300 symbol_value
= symbol_value
+ addend
;
3302 symbol_hi16
= (symbol_value
>> 16) & 0xffff;
3303 symbol_lo16
= symbol_value
& 0xffff;
3305 r
= _bfd_final_link_relocate (howto
, abfd
, input_section
,
3306 data
, offset
, symbol_hi16
, addend
);
3308 if (r
== bfd_reloc_ok
)
3309 return _bfd_final_link_relocate (howto
, abfd
, input_section
,
3310 data
, offset
+ 4, symbol_lo16
, addend
);
3315 static bfd_reloc_status_type
3316 nios2_elf32_do_callr_relocate (bfd
*abfd
, reloc_howto_type
*howto
,
3317 asection
*input_section
,
3318 bfd_byte
*data
, bfd_vma offset
,
3319 bfd_vma symbol_value
, bfd_vma addend
)
3321 bfd_vma symbol_lo16
, symbol_hi16
;
3322 bfd_reloc_status_type r
;
3323 symbol_value
= symbol_value
+ addend
;
3325 symbol_hi16
= (symbol_value
>> 16) & 0xffff;
3326 symbol_lo16
= symbol_value
& 0xffff;
3328 r
= _bfd_final_link_relocate (howto
, abfd
, input_section
,
3329 data
, offset
, symbol_hi16
, addend
);
3331 if (r
== bfd_reloc_ok
)
3332 return _bfd_final_link_relocate (howto
, abfd
, input_section
,
3333 data
, offset
+ 4, symbol_lo16
, addend
);
3338 /* HOWTO handlers for relocations that require special handling. */
3340 /* This is for relocations used only when relaxing to ensure
3341 changes in size of section don't screw up .align. */
3342 static bfd_reloc_status_type
3343 nios2_elf32_ignore_reloc (bfd
*abfd ATTRIBUTE_UNUSED
, arelent
*reloc_entry
,
3344 asymbol
*symbol ATTRIBUTE_UNUSED
,
3345 void *data ATTRIBUTE_UNUSED
, asection
*input_section
,
3347 char **error_message ATTRIBUTE_UNUSED
)
3349 if (output_bfd
!= NULL
)
3350 reloc_entry
->address
+= input_section
->output_offset
;
3351 return bfd_reloc_ok
;
3354 static bfd_reloc_status_type
3355 nios2_elf32_hi16_relocate (bfd
*abfd
, arelent
*reloc_entry
, asymbol
*symbol
,
3356 void *data
, asection
*input_section
,
3358 char **error_message ATTRIBUTE_UNUSED
)
3360 /* This part is from bfd_elf_generic_reloc. */
3361 if (output_bfd
!= NULL
3362 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
3363 && (!reloc_entry
->howto
->partial_inplace
|| reloc_entry
->addend
== 0))
3365 reloc_entry
->address
+= input_section
->output_offset
;
3366 return bfd_reloc_ok
;
3369 if (output_bfd
!= NULL
)
3370 /* FIXME: See bfd_perform_relocation. Is this right? */
3371 return bfd_reloc_continue
;
3373 return nios2_elf32_do_hi16_relocate (abfd
, reloc_entry
->howto
,
3375 data
, reloc_entry
->address
,
3377 + symbol
->section
->output_section
->vma
3378 + symbol
->section
->output_offset
),
3379 reloc_entry
->addend
);
3382 static bfd_reloc_status_type
3383 nios2_elf32_lo16_relocate (bfd
*abfd
, arelent
*reloc_entry
, asymbol
*symbol
,
3384 void *data
, asection
*input_section
,
3386 char **error_message ATTRIBUTE_UNUSED
)
3388 /* This part is from bfd_elf_generic_reloc. */
3389 if (output_bfd
!= NULL
3390 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
3391 && (!reloc_entry
->howto
->partial_inplace
|| reloc_entry
->addend
== 0))
3393 reloc_entry
->address
+= input_section
->output_offset
;
3394 return bfd_reloc_ok
;
3397 if (output_bfd
!= NULL
)
3398 /* FIXME: See bfd_perform_relocation. Is this right? */
3399 return bfd_reloc_continue
;
3401 return nios2_elf32_do_lo16_relocate (abfd
, reloc_entry
->howto
,
3403 data
, reloc_entry
->address
,
3405 + symbol
->section
->output_section
->vma
3406 + symbol
->section
->output_offset
),
3407 reloc_entry
->addend
);
3410 static bfd_reloc_status_type
3411 nios2_elf32_hiadj16_relocate (bfd
*abfd
, arelent
*reloc_entry
, asymbol
*symbol
,
3412 void *data
, asection
*input_section
,
3414 char **error_message ATTRIBUTE_UNUSED
)
3416 /* This part is from bfd_elf_generic_reloc. */
3417 if (output_bfd
!= NULL
3418 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
3419 && (!reloc_entry
->howto
->partial_inplace
|| reloc_entry
->addend
== 0))
3421 reloc_entry
->address
+= input_section
->output_offset
;
3422 return bfd_reloc_ok
;
3425 if (output_bfd
!= NULL
)
3426 /* FIXME: See bfd_perform_relocation. Is this right? */
3427 return bfd_reloc_continue
;
3429 return nios2_elf32_do_hiadj16_relocate (abfd
, reloc_entry
->howto
,
3431 data
, reloc_entry
->address
,
3433 + symbol
->section
->output_section
->vma
3434 + symbol
->section
->output_offset
),
3435 reloc_entry
->addend
);
3438 static bfd_reloc_status_type
3439 nios2_elf32_pcrel_lo16_relocate (bfd
*abfd
, arelent
*reloc_entry
,
3440 asymbol
*symbol
, void *data
,
3441 asection
*input_section
, bfd
*output_bfd
,
3442 char **error_message ATTRIBUTE_UNUSED
)
3444 /* This part is from bfd_elf_generic_reloc. */
3445 if (output_bfd
!= NULL
3446 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
3447 && (!reloc_entry
->howto
->partial_inplace
|| reloc_entry
->addend
== 0))
3449 reloc_entry
->address
+= input_section
->output_offset
;
3450 return bfd_reloc_ok
;
3453 if (output_bfd
!= NULL
)
3454 /* FIXME: See bfd_perform_relocation. Is this right? */
3455 return bfd_reloc_continue
;
3457 return nios2_elf32_do_pcrel_lo16_relocate (
3458 abfd
, reloc_entry
->howto
, input_section
, data
, reloc_entry
->address
,
3459 (symbol
->value
+ symbol
->section
->output_section
->vma
3460 + symbol
->section
->output_offset
),
3461 reloc_entry
->addend
);
3464 static bfd_reloc_status_type
3465 nios2_elf32_pcrel_hiadj16_relocate (bfd
*abfd
, arelent
*reloc_entry
,
3466 asymbol
*symbol
, void *data
,
3467 asection
*input_section
, bfd
*output_bfd
,
3468 char **error_message ATTRIBUTE_UNUSED
)
3470 /* This part is from bfd_elf_generic_reloc. */
3471 if (output_bfd
!= NULL
3472 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
3473 && (!reloc_entry
->howto
->partial_inplace
|| reloc_entry
->addend
== 0))
3475 reloc_entry
->address
+= input_section
->output_offset
;
3476 return bfd_reloc_ok
;
3479 if (output_bfd
!= NULL
)
3480 /* FIXME: See bfd_perform_relocation. Is this right? */
3481 return bfd_reloc_continue
;
3483 return nios2_elf32_do_pcrel_hiadj16_relocate (
3484 abfd
, reloc_entry
->howto
, input_section
, data
, reloc_entry
->address
,
3485 (symbol
->value
+ symbol
->section
->output_section
->vma
3486 + symbol
->section
->output_offset
),
3487 reloc_entry
->addend
);
3490 static bfd_reloc_status_type
3491 nios2_elf32_pcrel16_relocate (bfd
*abfd
, arelent
*reloc_entry
, asymbol
*symbol
,
3492 void *data
, asection
*input_section
,
3494 char **error_message ATTRIBUTE_UNUSED
)
3496 /* This part is from bfd_elf_generic_reloc. */
3497 if (output_bfd
!= NULL
3498 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
3499 && (!reloc_entry
->howto
->partial_inplace
|| reloc_entry
->addend
== 0))
3501 reloc_entry
->address
+= input_section
->output_offset
;
3502 return bfd_reloc_ok
;
3505 if (output_bfd
!= NULL
)
3506 /* FIXME: See bfd_perform_relocation. Is this right? */
3507 return bfd_reloc_continue
;
3509 return nios2_elf32_do_pcrel16_relocate (abfd
, reloc_entry
->howto
,
3511 data
, reloc_entry
->address
,
3513 + symbol
->section
->output_section
->vma
3514 + symbol
->section
->output_offset
),
3515 reloc_entry
->addend
);
3518 static bfd_reloc_status_type
3519 nios2_elf32_call26_relocate (bfd
*abfd
, arelent
*reloc_entry
, asymbol
*symbol
,
3520 void *data
, asection
*input_section
,
3522 char **error_message ATTRIBUTE_UNUSED
)
3524 /* This part is from bfd_elf_generic_reloc. */
3525 if (output_bfd
!= NULL
3526 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
3527 && (!reloc_entry
->howto
->partial_inplace
|| reloc_entry
->addend
== 0))
3529 reloc_entry
->address
+= input_section
->output_offset
;
3530 return bfd_reloc_ok
;
3533 if (output_bfd
!= NULL
)
3534 /* FIXME: See bfd_perform_relocation. Is this right? */
3535 return bfd_reloc_continue
;
3537 return nios2_elf32_do_call26_relocate (abfd
, reloc_entry
->howto
,
3539 data
, reloc_entry
->address
,
3541 + symbol
->section
->output_section
->vma
3542 + symbol
->section
->output_offset
),
3543 reloc_entry
->addend
);
3546 static bfd_reloc_status_type
3547 nios2_elf32_gprel_relocate (bfd
*abfd
, arelent
*reloc_entry
, asymbol
*symbol
,
3548 void *data
, asection
*input_section
,
3549 bfd
*output_bfd
, char **msg
)
3553 bfd_reloc_status_type r
;
3556 /* This part is from bfd_elf_generic_reloc. */
3557 if (output_bfd
!= NULL
3558 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
3559 && (!reloc_entry
->howto
->partial_inplace
|| reloc_entry
->addend
== 0))
3561 reloc_entry
->address
+= input_section
->output_offset
;
3562 return bfd_reloc_ok
;
3565 if (output_bfd
!= NULL
)
3566 /* FIXME: See bfd_perform_relocation. Is this right? */
3567 return bfd_reloc_continue
;
3569 relocation
= (symbol
->value
3570 + symbol
->section
->output_section
->vma
3571 + symbol
->section
->output_offset
);
3573 /* This assumes we've already cached the _gp symbol. */
3574 r
= nios2_elf_final_gp (abfd
, symbol
, FALSE
, msg
, &gp
);
3575 if (r
== bfd_reloc_ok
)
3577 relocation
= relocation
+ reloc_entry
->addend
- gp
;
3578 reloc_entry
->addend
= 0;
3579 if ((signed) relocation
< -32768 || (signed) relocation
> 32767)
3581 *msg
= _("global pointer relative address out of range");
3582 r
= bfd_reloc_outofrange
;
3585 r
= nios2_elf32_do_gprel_relocate (abfd
, reloc_entry
->howto
,
3587 data
, reloc_entry
->address
,
3588 relocation
, reloc_entry
->addend
);
3594 static bfd_reloc_status_type
3595 nios2_elf32_ujmp_relocate (bfd
*abfd
, arelent
*reloc_entry
, asymbol
*symbol
,
3596 void *data
, asection
*input_section
,
3597 bfd
*output_bfd
, char **msg ATTRIBUTE_UNUSED
)
3599 /* This part is from bfd_elf_generic_reloc. */
3600 if (output_bfd
!= NULL
3601 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
3602 && (!reloc_entry
->howto
->partial_inplace
|| reloc_entry
->addend
== 0))
3604 reloc_entry
->address
+= input_section
->output_offset
;
3605 return bfd_reloc_ok
;
3608 if (output_bfd
!= NULL
)
3609 /* FIXME: See bfd_perform_relocation. Is this right? */
3610 return bfd_reloc_continue
;
3612 return nios2_elf32_do_ujmp_relocate (abfd
, reloc_entry
->howto
,
3614 data
, reloc_entry
->address
,
3616 + symbol
->section
->output_section
->vma
3617 + symbol
->section
->output_offset
),
3618 reloc_entry
->addend
);
3621 static bfd_reloc_status_type
3622 nios2_elf32_cjmp_relocate (bfd
*abfd
, arelent
*reloc_entry
, asymbol
*symbol
,
3623 void *data
, asection
*input_section
,
3624 bfd
*output_bfd
, char **msg ATTRIBUTE_UNUSED
)
3626 /* This part is from bfd_elf_generic_reloc. */
3627 if (output_bfd
!= NULL
3628 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
3629 && (!reloc_entry
->howto
->partial_inplace
|| reloc_entry
->addend
== 0))
3631 reloc_entry
->address
+= input_section
->output_offset
;
3632 return bfd_reloc_ok
;
3635 if (output_bfd
!= NULL
)
3636 /* FIXME: See bfd_perform_relocation. Is this right? */
3637 return bfd_reloc_continue
;
3639 return nios2_elf32_do_cjmp_relocate (abfd
, reloc_entry
->howto
,
3641 data
, reloc_entry
->address
,
3643 + symbol
->section
->output_section
->vma
3644 + symbol
->section
->output_offset
),
3645 reloc_entry
->addend
);
3648 static bfd_reloc_status_type
3649 nios2_elf32_callr_relocate (bfd
*abfd
, arelent
*reloc_entry
, asymbol
*symbol
,
3650 void *data
, asection
*input_section
,
3651 bfd
*output_bfd
, char **msg ATTRIBUTE_UNUSED
)
3653 /* This part is from bfd_elf_generic_reloc. */
3654 if (output_bfd
!= NULL
3655 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
3656 && (!reloc_entry
->howto
->partial_inplace
|| reloc_entry
->addend
== 0))
3658 reloc_entry
->address
+= input_section
->output_offset
;
3659 return bfd_reloc_ok
;
3662 if (output_bfd
!= NULL
)
3663 /* FIXME: See bfd_perform_relocation. Is this right? */
3664 return bfd_reloc_continue
;
3666 return nios2_elf32_do_callr_relocate (abfd
, reloc_entry
->howto
,
3668 data
, reloc_entry
->address
,
3670 + symbol
->section
->output_section
->vma
3671 + symbol
->section
->output_offset
),
3672 reloc_entry
->addend
);
3676 /* Implement elf_backend_relocate_section. */
3678 nios2_elf32_relocate_section (bfd
*output_bfd
,
3679 struct bfd_link_info
*info
,
3681 asection
*input_section
,
3683 Elf_Internal_Rela
*relocs
,
3684 Elf_Internal_Sym
*local_syms
,
3685 asection
**local_sections
)
3687 Elf_Internal_Shdr
*symtab_hdr
;
3688 struct elf_link_hash_entry
**sym_hashes
;
3689 Elf_Internal_Rela
*rel
;
3690 Elf_Internal_Rela
*relend
;
3691 struct elf32_nios2_link_hash_table
*htab
;
3694 asection
*sreloc
= NULL
;
3695 bfd_vma
*local_got_offsets
;
3698 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
3699 sym_hashes
= elf_sym_hashes (input_bfd
);
3700 relend
= relocs
+ input_section
->reloc_count
;
3702 htab
= elf32_nios2_hash_table (info
);
3703 sgot
= htab
->root
.sgot
;
3704 splt
= htab
->root
.splt
;
3705 local_got_offsets
= elf_local_got_offsets (input_bfd
);
3707 if (elf32_nios2_hash_table (info
)->h_gp_got
== NULL
)
3710 got_base
= elf32_nios2_hash_table (info
)->h_gp_got
->root
.u
.def
.value
;
3712 for (rel
= relocs
; rel
< relend
; rel
++)
3714 reloc_howto_type
*howto
;
3715 unsigned long r_symndx
;
3716 Elf_Internal_Sym
*sym
;
3718 struct elf_link_hash_entry
*h
;
3719 struct elf32_nios2_link_hash_entry
*eh
;
3722 bfd_vma reloc_address
;
3723 bfd_reloc_status_type r
= bfd_reloc_ok
;
3724 const char *name
= NULL
;
3728 const char* msg
= (const char*) NULL
;
3729 bfd_boolean unresolved_reloc
;
3733 r_type
= ELF32_R_TYPE (rel
->r_info
);
3734 r_symndx
= ELF32_R_SYM (rel
->r_info
);
3736 howto
= lookup_howto ((unsigned) ELF32_R_TYPE (rel
->r_info
), output_bfd
);
3741 if (r_symndx
< symtab_hdr
->sh_info
)
3743 sym
= local_syms
+ r_symndx
;
3744 sec
= local_sections
[r_symndx
];
3745 relocation
= _bfd_elf_rela_local_sym (output_bfd
, sym
, &sec
, rel
);
3749 bfd_boolean warned
, ignored
;
3751 RELOC_FOR_GLOBAL_SYMBOL (info
, input_bfd
, input_section
, rel
,
3752 r_symndx
, symtab_hdr
, sym_hashes
,
3754 unresolved_reloc
, warned
, ignored
);
3757 if (sec
&& discarded_section (sec
))
3758 RELOC_AGAINST_DISCARDED_SECTION (info
, input_bfd
, input_section
,
3759 rel
, 1, relend
, howto
, 0, contents
);
3761 /* Nothing more to do unless this is a final link. */
3762 if (bfd_link_relocatable (info
))
3765 if (sec
&& sec
->output_section
)
3766 reloc_address
= (sec
->output_section
->vma
+ sec
->output_offset
3773 switch (howto
->type
)
3776 r
= nios2_elf32_do_hi16_relocate (input_bfd
, howto
,
3778 contents
, rel
->r_offset
,
3779 relocation
, rel
->r_addend
);
3782 r
= nios2_elf32_do_lo16_relocate (input_bfd
, howto
,
3784 contents
, rel
->r_offset
,
3785 relocation
, rel
->r_addend
);
3787 case R_NIOS2_PCREL_LO
:
3788 r
= nios2_elf32_do_pcrel_lo16_relocate (input_bfd
, howto
,
3795 case R_NIOS2_HIADJ16
:
3796 r
= nios2_elf32_do_hiadj16_relocate (input_bfd
, howto
,
3797 input_section
, contents
,
3798 rel
->r_offset
, relocation
,
3801 case R_NIOS2_PCREL_HA
:
3802 r
= nios2_elf32_do_pcrel_hiadj16_relocate (input_bfd
, howto
,
3809 case R_NIOS2_PCREL16
:
3810 r
= nios2_elf32_do_pcrel16_relocate (input_bfd
, howto
,
3811 input_section
, contents
,
3812 rel
->r_offset
, relocation
,
3816 /* Turns an absolute address into a gp-relative address. */
3817 if (!nios2_elf_assign_gp (output_bfd
, &gp
, info
))
3819 format
= _("global pointer relative relocation at address "
3820 "0x%08x when _gp not defined\n");
3821 sprintf (msgbuf
, format
, reloc_address
);
3823 r
= bfd_reloc_dangerous
;
3827 bfd_vma symbol_address
= rel
->r_addend
+ relocation
;
3828 relocation
= relocation
+ rel
->r_addend
- gp
;
3830 if (((signed) relocation
< -32768
3831 || (signed) relocation
> 32767)
3833 || h
->root
.type
== bfd_link_hash_defined
3834 || h
->root
.type
== bfd_link_hash_defweak
))
3836 format
= _("Unable to reach %s (at 0x%08x) from the "
3837 "global pointer (at 0x%08x) because the "
3838 "offset (%d) is out of the allowed range, "
3839 "-32678 to 32767.\n" );
3840 sprintf (msgbuf
, format
, name
, symbol_address
, gp
,
3841 (signed)relocation
);
3843 r
= bfd_reloc_outofrange
;
3846 r
= _bfd_final_link_relocate (howto
, input_bfd
,
3847 input_section
, contents
,
3848 rel
->r_offset
, relocation
,
3854 r
= nios2_elf32_do_ujmp_relocate (input_bfd
, howto
,
3856 contents
, rel
->r_offset
,
3857 relocation
, rel
->r_addend
);
3860 r
= nios2_elf32_do_cjmp_relocate (input_bfd
, howto
,
3862 contents
, rel
->r_offset
,
3863 relocation
, rel
->r_addend
);
3866 r
= nios2_elf32_do_callr_relocate (input_bfd
, howto
,
3867 input_section
, contents
,
3868 rel
->r_offset
, relocation
,
3871 case R_NIOS2_CALL26
:
3872 case R_NIOS2_CALL26_NOAT
:
3873 /* If we have a call to an undefined weak symbol, we just want
3874 to stuff a zero in the bits of the call instruction and
3875 bypass the normal call26 relocation handling, because it'll
3876 diagnose an overflow error if address 0 isn't in the same
3877 256MB segment as the call site. Presumably the call
3878 should be guarded by a null check anyway. */
3879 if (h
!= NULL
&& h
->root
.type
== bfd_link_hash_undefweak
)
3881 BFD_ASSERT (relocation
== 0 && rel
->r_addend
== 0);
3882 r
= _bfd_final_link_relocate (howto
, input_bfd
,
3883 input_section
, contents
,
3884 rel
->r_offset
, relocation
,
3888 /* Handle relocations which should use the PLT entry.
3889 NIOS2_BFD_RELOC_32 relocations will use the symbol's value,
3890 which may point to a PLT entry, but we don't need to handle
3891 that here. If we created a PLT entry, all branches in this
3892 object should go to it. */
3893 if (h
!= NULL
&& splt
!= NULL
&& h
->plt
.offset
!= (bfd_vma
) -1)
3895 /* If we've created a .plt section, and assigned a PLT entry
3896 to this function, it should not be known to bind locally.
3897 If it were, we would have cleared the PLT entry. */
3898 BFD_ASSERT (!SYMBOL_CALLS_LOCAL (info
, h
));
3900 relocation
= (splt
->output_section
->vma
3901 + splt
->output_offset
3904 unresolved_reloc
= FALSE
;
3906 /* Detect R_NIOS2_CALL26 relocations that would overflow the
3907 256MB segment. Replace the target with a reference to a
3909 Note that htab->stub_group is null if relaxation has been
3910 disabled by the --no-relax linker command-line option, so
3911 we can use that to skip this processing entirely. */
3912 if (howto
->type
== R_NIOS2_CALL26
&& htab
->stub_group
)
3914 bfd_vma dest
= relocation
+ rel
->r_addend
;
3915 enum elf32_nios2_stub_type stub_type
;
3917 eh
= (struct elf32_nios2_link_hash_entry
*)h
;
3918 stub_type
= nios2_type_of_stub (input_section
, rel
, eh
,
3921 if (stub_type
!= nios2_stub_none
)
3923 struct elf32_nios2_stub_hash_entry
*hsh
;
3925 hsh
= nios2_get_stub_entry (input_section
, sec
,
3926 eh
, rel
, htab
, stub_type
);
3929 r
= bfd_reloc_undefined
;
3933 dest
= (hsh
->stub_offset
3934 + hsh
->stub_sec
->output_offset
3935 + hsh
->stub_sec
->output_section
->vma
);
3936 r
= nios2_elf32_do_call26_relocate (input_bfd
, howto
,
3946 r
= nios2_elf32_do_call26_relocate (input_bfd
, howto
,
3947 input_section
, contents
,
3948 rel
->r_offset
, relocation
,
3953 /* For symmetry this would be
3954 r = nios2_elf32_do_ignore_reloc (input_bfd, howto,
3955 input_section, contents,
3956 rel->r_offset, relocation,
3958 but do_ignore_reloc would do no more than return
3963 case R_NIOS2_CALL16
:
3964 case R_NIOS2_GOT_LO
:
3965 case R_NIOS2_GOT_HA
:
3966 case R_NIOS2_CALL_LO
:
3967 case R_NIOS2_CALL_HA
:
3968 /* Relocation is to the entry for this symbol in the
3969 global offset table. */
3972 r
= bfd_reloc_notsupported
;
3982 eh
= (struct elf32_nios2_link_hash_entry
*)h
;
3983 use_plt
= (eh
->got_types_used
== CALL_USED
3984 && h
->plt
.offset
!= (bfd_vma
) -1);
3986 off
= h
->got
.offset
;
3987 BFD_ASSERT (off
!= (bfd_vma
) -1);
3988 dyn
= elf_hash_table (info
)->dynamic_sections_created
;
3989 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
,
3990 bfd_link_pic (info
),
3992 || (bfd_link_pic (info
)
3993 && SYMBOL_REFERENCES_LOCAL (info
, h
))
3994 || (ELF_ST_VISIBILITY (h
->other
)
3995 && h
->root
.type
== bfd_link_hash_undefweak
))
3997 /* This is actually a static link, or it is a -Bsymbolic
3998 link and the symbol is defined locally. We must
3999 initialize this entry in the global offset table.
4000 Since the offset must always be a multiple of 4, we
4001 use the least significant bit to record whether we
4002 have initialized it already.
4004 When doing a dynamic link, we create a .rela.got
4005 relocation entry to initialize the value. This is
4006 done in the finish_dynamic_symbol routine. */
4011 bfd_put_32 (output_bfd
, relocation
,
4012 sgot
->contents
+ off
);
4017 unresolved_reloc
= FALSE
;
4021 BFD_ASSERT (local_got_offsets
!= NULL
4022 && local_got_offsets
[r_symndx
] != (bfd_vma
) -1);
4024 off
= local_got_offsets
[r_symndx
];
4026 /* The offset must always be a multiple of 4. We use the
4027 least significant bit to record whether we have already
4028 generated the necessary reloc. */
4033 bfd_put_32 (output_bfd
, relocation
,
4034 sgot
->contents
+ off
);
4036 if (bfd_link_pic (info
))
4039 Elf_Internal_Rela outrel
;
4042 srelgot
= htab
->root
.srelgot
;
4043 BFD_ASSERT (srelgot
!= NULL
);
4045 outrel
.r_addend
= relocation
;
4046 outrel
.r_offset
= (sgot
->output_section
->vma
4047 + sgot
->output_offset
4049 outrel
.r_info
= ELF32_R_INFO (0, R_NIOS2_RELATIVE
);
4050 loc
= srelgot
->contents
;
4051 loc
+= (srelgot
->reloc_count
++ *
4052 sizeof (Elf32_External_Rela
));
4053 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
, loc
);
4056 local_got_offsets
[r_symndx
] |= 1;
4060 if (use_plt
&& bfd_link_pic (info
))
4062 off
= ((h
->plt
.offset
- 24) / 12 + 3) * 4;
4063 relocation
= (htab
->root
.sgotplt
->output_offset
+ off
4067 relocation
= sgot
->output_offset
+ off
- got_base
;
4069 /* This relocation does not use the addend. */
4072 switch (howto
->type
)
4074 case R_NIOS2_GOT_LO
:
4075 case R_NIOS2_CALL_LO
:
4076 r
= nios2_elf32_do_lo16_relocate (input_bfd
, howto
,
4077 input_section
, contents
,
4078 rel
->r_offset
, relocation
,
4081 case R_NIOS2_GOT_HA
:
4082 case R_NIOS2_CALL_HA
:
4083 r
= nios2_elf32_do_hiadj16_relocate (input_bfd
, howto
,
4084 input_section
, contents
,
4090 r
= _bfd_final_link_relocate (howto
, input_bfd
,
4091 input_section
, contents
,
4092 rel
->r_offset
, relocation
,
4098 case R_NIOS2_GOTOFF_LO
:
4099 case R_NIOS2_GOTOFF_HA
:
4100 case R_NIOS2_GOTOFF
:
4101 /* Relocation is relative to the global offset table pointer. */
4103 BFD_ASSERT (sgot
!= NULL
);
4106 r
= bfd_reloc_notsupported
;
4110 /* Note that sgot->output_offset is not involved in this
4111 calculation. We always want the start of .got. */
4112 relocation
-= sgot
->output_section
->vma
;
4114 /* Now we adjust the relocation to be relative to the GOT pointer
4115 (the _gp_got symbol), which possibly contains the 0x8000 bias. */
4116 relocation
-= got_base
;
4118 switch (howto
->type
)
4120 case R_NIOS2_GOTOFF_LO
:
4121 r
= nios2_elf32_do_lo16_relocate (input_bfd
, howto
,
4122 input_section
, contents
,
4123 rel
->r_offset
, relocation
,
4126 case R_NIOS2_GOTOFF_HA
:
4127 r
= nios2_elf32_do_hiadj16_relocate (input_bfd
, howto
,
4128 input_section
, contents
,
4134 r
= _bfd_final_link_relocate (howto
, input_bfd
,
4135 input_section
, contents
,
4136 rel
->r_offset
, relocation
,
4142 case R_NIOS2_TLS_LDO16
:
4143 relocation
-= dtpoff_base (info
) + DTP_OFFSET
;
4145 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
4146 contents
, rel
->r_offset
,
4147 relocation
, rel
->r_addend
);
4149 case R_NIOS2_TLS_LDM16
:
4150 if (htab
->root
.sgot
== NULL
)
4153 off
= htab
->tls_ldm_got
.offset
;
4159 /* If we don't know the module number, create a relocation
4161 if (bfd_link_pic (info
))
4163 Elf_Internal_Rela outrel
;
4166 if (htab
->root
.srelgot
== NULL
)
4169 outrel
.r_addend
= 0;
4170 outrel
.r_offset
= (htab
->root
.sgot
->output_section
->vma
4171 + htab
->root
.sgot
->output_offset
4173 outrel
.r_info
= ELF32_R_INFO (0, R_NIOS2_TLS_DTPMOD
);
4175 loc
= htab
->root
.srelgot
->contents
;
4176 loc
+= (htab
->root
.srelgot
->reloc_count
++
4177 * sizeof (Elf32_External_Rela
));
4178 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
, loc
);
4181 bfd_put_32 (output_bfd
, 1,
4182 htab
->root
.sgot
->contents
+ off
);
4184 htab
->tls_ldm_got
.offset
|= 1;
4187 relocation
= htab
->root
.sgot
->output_offset
+ off
- got_base
;
4189 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
4190 contents
, rel
->r_offset
,
4191 relocation
, rel
->r_addend
);
4194 case R_NIOS2_TLS_GD16
:
4195 case R_NIOS2_TLS_IE16
:
4200 if (htab
->root
.sgot
== NULL
)
4207 dyn
= htab
->root
.dynamic_sections_created
;
4208 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
,
4209 bfd_link_pic (info
),
4211 && (!bfd_link_pic (info
)
4212 || !SYMBOL_REFERENCES_LOCAL (info
, h
)))
4214 unresolved_reloc
= FALSE
;
4217 off
= h
->got
.offset
;
4218 tls_type
= (((struct elf32_nios2_link_hash_entry
*) h
)
4223 if (local_got_offsets
== NULL
)
4225 off
= local_got_offsets
[r_symndx
];
4226 tls_type
= (elf32_nios2_local_got_tls_type (input_bfd
)
4230 if (tls_type
== GOT_UNKNOWN
)
4237 bfd_boolean need_relocs
= FALSE
;
4238 Elf_Internal_Rela outrel
;
4239 bfd_byte
*loc
= NULL
;
4242 /* The GOT entries have not been initialized yet. Do it
4243 now, and emit any relocations. If both an IE GOT and a
4244 GD GOT are necessary, we emit the GD first. */
4246 if ((bfd_link_pic (info
) || indx
!= 0)
4248 || ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
4249 || h
->root
.type
!= bfd_link_hash_undefweak
))
4252 if (htab
->root
.srelgot
== NULL
)
4254 loc
= htab
->root
.srelgot
->contents
;
4255 loc
+= (htab
->root
.srelgot
->reloc_count
*
4256 sizeof (Elf32_External_Rela
));
4259 if (tls_type
& GOT_TLS_GD
)
4263 outrel
.r_addend
= 0;
4264 outrel
.r_offset
= (htab
->root
.sgot
->output_section
->vma
4265 + htab
->root
.sgot
->output_offset
4267 outrel
.r_info
= ELF32_R_INFO (indx
,
4268 R_NIOS2_TLS_DTPMOD
);
4270 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
,
4272 htab
->root
.srelgot
->reloc_count
++;
4273 loc
+= sizeof (Elf32_External_Rela
);
4276 bfd_put_32 (output_bfd
,
4277 (relocation
- dtpoff_base (info
) -
4279 htab
->root
.sgot
->contents
+ cur_off
+ 4);
4282 outrel
.r_addend
= 0;
4283 outrel
.r_info
= ELF32_R_INFO (indx
,
4284 R_NIOS2_TLS_DTPREL
);
4285 outrel
.r_offset
+= 4;
4287 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
,
4289 htab
->root
.srelgot
->reloc_count
++;
4290 loc
+= sizeof (Elf32_External_Rela
);
4295 /* If we are not emitting relocations for a
4296 general dynamic reference, then we must be in a
4297 static link or an executable link with the
4298 symbol binding locally. Mark it as belonging
4299 to module 1, the executable. */
4300 bfd_put_32 (output_bfd
, 1,
4301 htab
->root
.sgot
->contents
+ cur_off
);
4302 bfd_put_32 (output_bfd
, (relocation
-
4303 dtpoff_base (info
) -
4305 htab
->root
.sgot
->contents
+ cur_off
+ 4);
4311 if (tls_type
& GOT_TLS_IE
)
4316 outrel
.r_addend
= (relocation
-
4317 dtpoff_base (info
));
4319 outrel
.r_addend
= 0;
4320 outrel
.r_offset
= (htab
->root
.sgot
->output_section
->vma
4321 + htab
->root
.sgot
->output_offset
4323 outrel
.r_info
= ELF32_R_INFO (indx
,
4326 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
,
4328 htab
->root
.srelgot
->reloc_count
++;
4329 loc
+= sizeof (Elf32_External_Rela
);
4332 bfd_put_32 (output_bfd
, (tpoff (info
, relocation
)
4334 htab
->root
.sgot
->contents
+ cur_off
);
4341 local_got_offsets
[r_symndx
] |= 1;
4344 if ((tls_type
& GOT_TLS_GD
) && r_type
!= R_NIOS2_TLS_GD16
)
4346 relocation
= htab
->root
.sgot
->output_offset
+ off
- got_base
;
4348 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
4349 contents
, rel
->r_offset
,
4350 relocation
, rel
->r_addend
);
4354 case R_NIOS2_TLS_LE16
:
4355 if (bfd_link_dll (info
))
4357 (*_bfd_error_handler
)
4358 (_("%B(%A+0x%lx): R_NIOS2_TLS_LE16 relocation not "
4359 "permitted in shared object"),
4360 input_bfd
, input_section
,
4361 (long) rel
->r_offset
, howto
->name
);
4365 relocation
= tpoff (info
, relocation
) - TP_OFFSET
;
4367 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
4368 contents
, rel
->r_offset
,
4369 relocation
, rel
->r_addend
);
4372 case R_NIOS2_BFD_RELOC_32
:
4373 if (bfd_link_pic (info
)
4374 && (input_section
->flags
& SEC_ALLOC
) != 0
4376 || ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
4377 || h
->root
.type
!= bfd_link_hash_undefweak
))
4379 Elf_Internal_Rela outrel
;
4381 bfd_boolean skip
, relocate
;
4383 /* When generating a shared object, these relocations
4384 are copied into the output file to be resolved at run
4391 = _bfd_elf_section_offset (output_bfd
, info
,
4392 input_section
, rel
->r_offset
);
4393 if (outrel
.r_offset
== (bfd_vma
) -1)
4395 else if (outrel
.r_offset
== (bfd_vma
) -2)
4396 skip
= TRUE
, relocate
= TRUE
;
4397 outrel
.r_offset
+= (input_section
->output_section
->vma
4398 + input_section
->output_offset
);
4401 memset (&outrel
, 0, sizeof outrel
);
4404 && (!bfd_link_pic (info
)
4405 || !SYMBOLIC_BIND (info
, h
)
4406 || !h
->def_regular
))
4408 outrel
.r_info
= ELF32_R_INFO (h
->dynindx
, r_type
);
4409 outrel
.r_addend
= rel
->r_addend
;
4413 /* This symbol is local, or marked to become local. */
4414 outrel
.r_addend
= relocation
+ rel
->r_addend
;
4416 outrel
.r_info
= ELF32_R_INFO (0, R_NIOS2_RELATIVE
);
4419 sreloc
= elf_section_data (input_section
)->sreloc
;
4423 loc
= sreloc
->contents
;
4424 loc
+= sreloc
->reloc_count
++ * sizeof (Elf32_External_Rela
);
4425 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
, loc
);
4427 /* This reloc will be computed at runtime, so there's no
4428 need to do anything now, except for R_NIOS2_BFD_RELOC_32
4429 relocations that have been turned into
4430 R_NIOS2_RELATIVE. */
4435 r
= _bfd_final_link_relocate (howto
, input_bfd
,
4436 input_section
, contents
,
4437 rel
->r_offset
, relocation
,
4441 case R_NIOS2_TLS_DTPREL
:
4442 relocation
-= dtpoff_base (info
);
4446 r
= _bfd_final_link_relocate (howto
, input_bfd
,
4447 input_section
, contents
,
4448 rel
->r_offset
, relocation
,
4454 r
= bfd_reloc_notsupported
;
4456 if (r
!= bfd_reloc_ok
)
4459 name
= h
->root
.root
.string
;
4462 name
= bfd_elf_string_from_elf_section (input_bfd
,
4463 symtab_hdr
->sh_link
,
4465 if (name
== NULL
|| *name
== '\0')
4466 name
= bfd_section_name (input_bfd
, sec
);
4471 case bfd_reloc_overflow
:
4472 r
= info
->callbacks
->reloc_overflow (info
, NULL
, name
,
4473 howto
->name
, (bfd_vma
) 0,
4474 input_bfd
, input_section
,
4478 case bfd_reloc_undefined
:
4479 r
= info
->callbacks
->undefined_symbol (info
, name
, input_bfd
,
4481 rel
->r_offset
, TRUE
);
4484 case bfd_reloc_outofrange
:
4486 msg
= _("relocation out of range");
4489 case bfd_reloc_notsupported
:
4491 msg
= _("unsupported relocation");
4494 case bfd_reloc_dangerous
:
4496 msg
= _("dangerous relocation");
4501 msg
= _("unknown error");
4507 r
= info
->callbacks
->warning
4508 (info
, msg
, name
, input_bfd
, input_section
, rel
->r_offset
);
4516 /* Implement elf-backend_section_flags:
4517 Convert NIOS2 specific section flags to bfd internal section flags. */
4519 nios2_elf32_section_flags (flagword
*flags
, const Elf_Internal_Shdr
*hdr
)
4521 if (hdr
->sh_flags
& SHF_NIOS2_GPREL
)
4522 *flags
|= SEC_SMALL_DATA
;
4527 /* Implement elf_backend_fake_sections:
4528 Set the correct type for an NIOS2 ELF section. We do this by the
4529 section name, which is a hack, but ought to work. */
4531 nios2_elf32_fake_sections (bfd
*abfd ATTRIBUTE_UNUSED
,
4532 Elf_Internal_Shdr
*hdr
, asection
*sec
)
4534 register const char *name
= bfd_get_section_name (abfd
, sec
);
4536 if ((sec
->flags
& SEC_SMALL_DATA
)
4537 || strcmp (name
, ".sdata") == 0
4538 || strcmp (name
, ".sbss") == 0
4539 || strcmp (name
, ".lit4") == 0 || strcmp (name
, ".lit8") == 0)
4540 hdr
->sh_flags
|= SHF_NIOS2_GPREL
;
4545 /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
4546 shortcuts to them in our hash table. */
4548 create_got_section (bfd
*dynobj
, struct bfd_link_info
*info
)
4550 struct elf32_nios2_link_hash_table
*htab
;
4551 struct elf_link_hash_entry
*h
;
4553 htab
= elf32_nios2_hash_table (info
);
4555 if (! _bfd_elf_create_got_section (dynobj
, info
))
4558 /* In order for the two loads in .PLTresolve to share the same %hiadj,
4559 _GLOBAL_OFFSET_TABLE_ must be aligned to a 16-byte boundary. */
4560 if (!bfd_set_section_alignment (dynobj
, htab
->root
.sgotplt
, 4))
4563 /* The Nios II ABI specifies that GOT-relative relocations are relative
4564 to the linker-created symbol _gp_got, rather than using
4565 _GLOBAL_OFFSET_TABLE_ directly. In particular, the latter always
4566 points to the base of the GOT while _gp_got may include a bias. */
4567 h
= _bfd_elf_define_linkage_sym (dynobj
, info
, htab
->root
.sgotplt
,
4569 elf32_nios2_hash_table (info
)->h_gp_got
= h
;
4576 /* Implement elf_backend_create_dynamic_sections:
4577 Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
4578 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
4581 nios2_elf32_create_dynamic_sections (bfd
*dynobj
, struct bfd_link_info
*info
)
4583 struct elf32_nios2_link_hash_table
*htab
;
4585 htab
= elf32_nios2_hash_table (info
);
4586 if (!htab
->root
.sgot
&& !create_got_section (dynobj
, info
))
4589 _bfd_elf_create_dynamic_sections (dynobj
, info
);
4591 /* In order for the two loads in a shared object .PLTresolve to share the
4592 same %hiadj, the start of the PLT (as well as the GOT) must be aligned
4593 to a 16-byte boundary. This is because the addresses for these loads
4594 include the -(.plt+4) PIC correction. */
4595 if (!bfd_set_section_alignment (dynobj
, htab
->root
.splt
, 4))
4598 htab
->sdynbss
= bfd_get_linker_section (dynobj
, ".dynbss");
4601 if (!bfd_link_pic (info
))
4603 htab
->srelbss
= bfd_get_linker_section (dynobj
, ".rela.bss");
4611 /* Implement elf_backend_copy_indirect_symbol:
4612 Copy the extra info we tack onto an elf_link_hash_entry. */
4614 nios2_elf32_copy_indirect_symbol (struct bfd_link_info
*info
,
4615 struct elf_link_hash_entry
*dir
,
4616 struct elf_link_hash_entry
*ind
)
4618 struct elf32_nios2_link_hash_entry
*edir
, *eind
;
4620 edir
= (struct elf32_nios2_link_hash_entry
*) dir
;
4621 eind
= (struct elf32_nios2_link_hash_entry
*) ind
;
4623 if (eind
->dyn_relocs
!= NULL
)
4625 if (edir
->dyn_relocs
!= NULL
)
4627 struct elf32_nios2_dyn_relocs
**pp
;
4628 struct elf32_nios2_dyn_relocs
*p
;
4630 /* Add reloc counts against the indirect sym to the direct sym
4631 list. Merge any entries against the same section. */
4632 for (pp
= &eind
->dyn_relocs
; (p
= *pp
) != NULL
; )
4634 struct elf32_nios2_dyn_relocs
*q
;
4636 for (q
= edir
->dyn_relocs
; q
!= NULL
; q
= q
->next
)
4637 if (q
->sec
== p
->sec
)
4639 q
->pc_count
+= p
->pc_count
;
4640 q
->count
+= p
->count
;
4647 *pp
= edir
->dyn_relocs
;
4650 edir
->dyn_relocs
= eind
->dyn_relocs
;
4651 eind
->dyn_relocs
= NULL
;
4654 if (ind
->root
.type
== bfd_link_hash_indirect
4655 && dir
->got
.refcount
<= 0)
4657 edir
->tls_type
= eind
->tls_type
;
4658 eind
->tls_type
= GOT_UNKNOWN
;
4661 edir
->got_types_used
|= eind
->got_types_used
;
4663 _bfd_elf_link_hash_copy_indirect (info
, dir
, ind
);
4666 /* Set the right machine number for a NIOS2 ELF file. */
4669 nios2_elf32_object_p (bfd
*abfd
)
4673 mach
= elf_elfheader (abfd
)->e_flags
;
4678 case EF_NIOS2_ARCH_R1
:
4679 bfd_default_set_arch_mach (abfd
, bfd_arch_nios2
, bfd_mach_nios2r1
);
4681 case EF_NIOS2_ARCH_R2
:
4682 bfd_default_set_arch_mach (abfd
, bfd_arch_nios2
, bfd_mach_nios2r2
);
4689 /* Implement elf_backend_check_relocs:
4690 Look through the relocs for a section during the first phase. */
4692 nios2_elf32_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
,
4693 asection
*sec
, const Elf_Internal_Rela
*relocs
)
4696 Elf_Internal_Shdr
*symtab_hdr
;
4697 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
4698 const Elf_Internal_Rela
*rel
;
4699 const Elf_Internal_Rela
*rel_end
;
4700 struct elf32_nios2_link_hash_table
*htab
;
4703 asection
*sreloc
= NULL
;
4704 bfd_signed_vma
*local_got_refcounts
;
4706 if (bfd_link_relocatable (info
))
4709 dynobj
= elf_hash_table (info
)->dynobj
;
4710 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4711 sym_hashes
= elf_sym_hashes (abfd
);
4712 sym_hashes_end
= (sym_hashes
4713 + symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
));
4714 if (!elf_bad_symtab (abfd
))
4715 sym_hashes_end
-= symtab_hdr
->sh_info
;
4716 local_got_refcounts
= elf_local_got_refcounts (abfd
);
4718 htab
= elf32_nios2_hash_table (info
);
4719 sgot
= htab
->root
.sgot
;
4720 srelgot
= htab
->root
.srelgot
;
4722 rel_end
= relocs
+ sec
->reloc_count
;
4723 for (rel
= relocs
; rel
< rel_end
; rel
++)
4725 unsigned int r_type
;
4726 struct elf_link_hash_entry
*h
;
4727 unsigned long r_symndx
;
4729 r_symndx
= ELF32_R_SYM (rel
->r_info
);
4730 if (r_symndx
< symtab_hdr
->sh_info
)
4734 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
4735 while (h
->root
.type
== bfd_link_hash_indirect
4736 || h
->root
.type
== bfd_link_hash_warning
)
4737 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4739 /* PR15323, ref flags aren't set for references in the same
4741 h
->root
.non_ir_ref
= 1;
4744 r_type
= ELF32_R_TYPE (rel
->r_info
);
4749 case R_NIOS2_GOT_LO
:
4750 case R_NIOS2_GOT_HA
:
4751 case R_NIOS2_CALL16
:
4752 case R_NIOS2_CALL_LO
:
4753 case R_NIOS2_CALL_HA
:
4754 case R_NIOS2_TLS_GD16
:
4755 case R_NIOS2_TLS_IE16
:
4756 /* This symbol requires a global offset table entry. */
4758 int tls_type
, old_tls_type
;
4764 case R_NIOS2_GOT_LO
:
4765 case R_NIOS2_GOT_HA
:
4766 case R_NIOS2_CALL16
:
4767 case R_NIOS2_CALL_LO
:
4768 case R_NIOS2_CALL_HA
:
4769 tls_type
= GOT_NORMAL
;
4771 case R_NIOS2_TLS_GD16
:
4772 tls_type
= GOT_TLS_GD
;
4774 case R_NIOS2_TLS_IE16
:
4775 tls_type
= GOT_TLS_IE
;
4781 /* Create the .got section. */
4782 elf_hash_table (info
)->dynobj
= dynobj
= abfd
;
4783 nios2_elf32_create_dynamic_sections (dynobj
, info
);
4788 sgot
= htab
->root
.sgot
;
4789 BFD_ASSERT (sgot
!= NULL
);
4793 && (h
!= NULL
|| bfd_link_pic (info
)))
4795 srelgot
= htab
->root
.srelgot
;
4796 BFD_ASSERT (srelgot
!= NULL
);
4801 struct elf32_nios2_link_hash_entry
*eh
4802 = (struct elf32_nios2_link_hash_entry
*)h
;
4804 old_tls_type
= elf32_nios2_hash_entry(h
)->tls_type
;
4805 if (r_type
== R_NIOS2_CALL16
4806 || r_type
== R_NIOS2_CALL_LO
4807 || r_type
== R_NIOS2_CALL_HA
)
4809 /* Make sure a plt entry is created for this symbol if
4810 it turns out to be a function defined by a dynamic
4815 eh
->got_types_used
|= CALL_USED
;
4818 eh
->got_types_used
|= GOT_USED
;
4822 /* This is a global offset table entry for a local symbol. */
4823 if (local_got_refcounts
== NULL
)
4827 size
= symtab_hdr
->sh_info
;
4828 size
*= (sizeof (bfd_signed_vma
) + sizeof (char));
4830 = ((bfd_signed_vma
*) bfd_zalloc (abfd
, size
));
4831 if (local_got_refcounts
== NULL
)
4833 elf_local_got_refcounts (abfd
) = local_got_refcounts
;
4834 elf32_nios2_local_got_tls_type (abfd
)
4835 = (char *) (local_got_refcounts
+ symtab_hdr
->sh_info
);
4837 local_got_refcounts
[r_symndx
]++;
4838 old_tls_type
= elf32_nios2_local_got_tls_type (abfd
) [r_symndx
];
4841 /* We will already have issued an error message if there is a
4842 TLS / non-TLS mismatch, based on the symbol type. We don't
4843 support any linker relaxations. So just combine any TLS
4845 if (old_tls_type
!= GOT_UNKNOWN
&& old_tls_type
!= GOT_NORMAL
4846 && tls_type
!= GOT_NORMAL
)
4847 tls_type
|= old_tls_type
;
4849 if (old_tls_type
!= tls_type
)
4852 elf32_nios2_hash_entry (h
)->tls_type
= tls_type
;
4854 elf32_nios2_local_got_tls_type (abfd
) [r_symndx
] = tls_type
;
4858 case R_NIOS2_TLS_LDM16
:
4859 if (r_type
== R_NIOS2_TLS_LDM16
)
4860 htab
->tls_ldm_got
.refcount
++;
4862 if (htab
->root
.sgot
== NULL
)
4864 if (htab
->root
.dynobj
== NULL
)
4865 htab
->root
.dynobj
= abfd
;
4866 if (!create_got_section (htab
->root
.dynobj
, info
))
4871 /* This relocation describes the C++ object vtable hierarchy.
4872 Reconstruct it for later use during GC. */
4873 case R_NIOS2_GNU_VTINHERIT
:
4874 if (!bfd_elf_gc_record_vtinherit (abfd
, sec
, h
, rel
->r_offset
))
4878 /* This relocation describes which C++ vtable entries are actually
4879 used. Record for later use during GC. */
4880 case R_NIOS2_GNU_VTENTRY
:
4881 if (!bfd_elf_gc_record_vtentry (abfd
, sec
, h
, rel
->r_addend
))
4885 case R_NIOS2_BFD_RELOC_32
:
4886 case R_NIOS2_CALL26
:
4887 case R_NIOS2_CALL26_NOAT
:
4888 case R_NIOS2_HIADJ16
:
4893 /* If this reloc is in a read-only section, we might
4894 need a copy reloc. We can't check reliably at this
4895 stage whether the section is read-only, as input
4896 sections have not yet been mapped to output sections.
4897 Tentatively set the flag for now, and correct in
4898 adjust_dynamic_symbol. */
4899 if (!bfd_link_pic (info
))
4902 /* Make sure a plt entry is created for this symbol if it
4903 turns out to be a function defined by a dynamic object. */
4906 if (r_type
== R_NIOS2_CALL26
|| r_type
== R_NIOS2_CALL26_NOAT
)
4910 /* If we are creating a shared library, we need to copy the
4911 reloc into the shared library. */
4912 if (bfd_link_pic (info
)
4913 && (sec
->flags
& SEC_ALLOC
) != 0
4914 && (r_type
== R_NIOS2_BFD_RELOC_32
4915 || (h
!= NULL
&& ! h
->needs_plt
4916 && (! SYMBOLIC_BIND (info
, h
) || ! h
->def_regular
))))
4918 struct elf32_nios2_dyn_relocs
*p
;
4919 struct elf32_nios2_dyn_relocs
**head
;
4921 /* When creating a shared object, we must copy these
4922 reloc types into the output file. We create a reloc
4923 section in dynobj and make room for this reloc. */
4926 sreloc
= _bfd_elf_make_dynamic_reloc_section
4927 (sec
, dynobj
, 2, abfd
, TRUE
);
4932 /* If this is a global symbol, we count the number of
4933 relocations we need for this symbol. */
4935 head
= &((struct elf32_nios2_link_hash_entry
*) h
)->dyn_relocs
;
4938 /* Track dynamic relocs needed for local syms too.
4939 We really need local syms available to do this
4944 Elf_Internal_Sym
*isym
;
4946 isym
= bfd_sym_from_r_symndx (&htab
->sym_cache
,
4951 s
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
4955 vpp
= &elf_section_data (s
)->local_dynrel
;
4956 head
= (struct elf32_nios2_dyn_relocs
**) vpp
;
4960 if (p
== NULL
|| p
->sec
!= sec
)
4962 bfd_size_type amt
= sizeof *p
;
4963 p
= ((struct elf32_nios2_dyn_relocs
*)
4964 bfd_alloc (htab
->root
.dynobj
, amt
));
4985 /* Implement elf_backend_gc_mark_hook:
4986 Return the section that should be marked against GC for a given
4989 nios2_elf32_gc_mark_hook (asection
*sec
,
4990 struct bfd_link_info
*info
,
4991 Elf_Internal_Rela
*rel
,
4992 struct elf_link_hash_entry
*h
,
4993 Elf_Internal_Sym
*sym
)
4996 switch (ELF32_R_TYPE (rel
->r_info
))
4998 case R_NIOS2_GNU_VTINHERIT
:
4999 case R_NIOS2_GNU_VTENTRY
:
5002 return _bfd_elf_gc_mark_hook (sec
, info
, rel
, h
, sym
);
5005 /* Implement elf_backend_gc_sweep_hook:
5006 Update the got entry reference counts for the section being removed. */
5008 nios2_elf32_gc_sweep_hook (bfd
*abfd
,
5009 struct bfd_link_info
*info
,
5011 const Elf_Internal_Rela
*relocs
)
5013 Elf_Internal_Shdr
*symtab_hdr
;
5014 struct elf_link_hash_entry
**sym_hashes
;
5015 bfd_signed_vma
*local_got_refcounts
;
5016 const Elf_Internal_Rela
*rel
, *relend
;
5019 if (bfd_link_relocatable (info
))
5022 elf_section_data (sec
)->local_dynrel
= NULL
;
5024 dynobj
= elf_hash_table (info
)->dynobj
;
5028 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
5029 sym_hashes
= elf_sym_hashes (abfd
);
5030 local_got_refcounts
= elf_local_got_refcounts (abfd
);
5032 relend
= relocs
+ sec
->reloc_count
;
5033 for (rel
= relocs
; rel
< relend
; rel
++)
5035 unsigned long r_symndx
;
5036 struct elf_link_hash_entry
*h
= NULL
;
5039 r_symndx
= ELF32_R_SYM (rel
->r_info
);
5040 if (r_symndx
>= symtab_hdr
->sh_info
)
5042 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
5043 while (h
->root
.type
== bfd_link_hash_indirect
5044 || h
->root
.type
== bfd_link_hash_warning
)
5045 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5048 r_type
= ELF32_R_TYPE (rel
->r_info
);
5052 case R_NIOS2_GOT_LO
:
5053 case R_NIOS2_GOT_HA
:
5054 case R_NIOS2_CALL16
:
5055 case R_NIOS2_CALL_LO
:
5056 case R_NIOS2_CALL_HA
:
5059 if (h
->got
.refcount
> 0)
5062 else if (local_got_refcounts
!= NULL
)
5064 if (local_got_refcounts
[r_symndx
] > 0)
5065 --local_got_refcounts
[r_symndx
];
5069 case R_NIOS2_PCREL_LO
:
5070 case R_NIOS2_PCREL_HA
:
5071 case R_NIOS2_BFD_RELOC_32
:
5072 case R_NIOS2_CALL26
:
5073 case R_NIOS2_CALL26_NOAT
:
5076 struct elf32_nios2_link_hash_entry
*eh
;
5077 struct elf32_nios2_dyn_relocs
**pp
;
5078 struct elf32_nios2_dyn_relocs
*p
;
5080 eh
= (struct elf32_nios2_link_hash_entry
*) h
;
5082 if (h
->plt
.refcount
> 0)
5085 if (r_type
== R_NIOS2_PCREL_LO
|| r_type
== R_NIOS2_PCREL_HA
5086 || r_type
== R_NIOS2_BFD_RELOC_32
)
5088 for (pp
= &eh
->dyn_relocs
; (p
= *pp
) != NULL
;
5109 /* Implement elf_backend_finish_dynamic_symbols:
5110 Finish up dynamic symbol handling. We set the contents of various
5111 dynamic sections here. */
5113 nios2_elf32_finish_dynamic_symbol (bfd
*output_bfd
,
5114 struct bfd_link_info
*info
,
5115 struct elf_link_hash_entry
*h
,
5116 Elf_Internal_Sym
*sym
)
5118 struct elf32_nios2_link_hash_table
*htab
;
5119 struct elf32_nios2_link_hash_entry
*eh
5120 = (struct elf32_nios2_link_hash_entry
*)h
;
5123 htab
= elf32_nios2_hash_table (info
);
5125 if (h
->plt
.offset
!= (bfd_vma
) -1)
5132 Elf_Internal_Rela rela
;
5134 bfd_vma got_address
;
5136 /* This symbol has an entry in the procedure linkage table. Set
5138 BFD_ASSERT (h
->dynindx
!= -1);
5139 splt
= htab
->root
.splt
;
5140 sgotplt
= htab
->root
.sgotplt
;
5141 srela
= htab
->root
.srelplt
;
5142 BFD_ASSERT (splt
!= NULL
&& sgotplt
!= NULL
&& srela
!= NULL
);
5144 /* Emit the PLT entry. */
5145 if (bfd_link_pic (info
))
5147 nios2_elf32_install_data (splt
, nios2_so_plt_entry
, h
->plt
.offset
,
5149 plt_index
= (h
->plt
.offset
- 24) / 12;
5150 got_offset
= (plt_index
+ 3) * 4;
5151 nios2_elf32_install_imm16 (splt
, h
->plt
.offset
,
5152 hiadj(plt_index
* 4));
5153 nios2_elf32_install_imm16 (splt
, h
->plt
.offset
+ 4,
5154 (plt_index
* 4) & 0xffff);
5155 nios2_elf32_install_imm16 (splt
, h
->plt
.offset
+ 8,
5156 0xfff4 - h
->plt
.offset
);
5157 got_address
= (sgotplt
->output_section
->vma
+ sgotplt
->output_offset
5160 /* Fill in the entry in the global offset table. There are no
5161 res_n slots for a shared object PLT, instead the .got.plt entries
5162 point to the PLT entries. */
5163 bfd_put_32 (output_bfd
,
5164 splt
->output_section
->vma
+ splt
->output_offset
5165 + h
->plt
.offset
, sgotplt
->contents
+ got_offset
);
5169 plt_index
= (h
->plt
.offset
- 28 - htab
->res_n_size
) / 12;
5170 got_offset
= (plt_index
+ 3) * 4;
5172 nios2_elf32_install_data (splt
, nios2_plt_entry
, h
->plt
.offset
, 3);
5173 got_address
= (sgotplt
->output_section
->vma
+ sgotplt
->output_offset
5175 nios2_elf32_install_imm16 (splt
, h
->plt
.offset
, hiadj(got_address
));
5176 nios2_elf32_install_imm16 (splt
, h
->plt
.offset
+ 4,
5177 got_address
& 0xffff);
5179 /* Fill in the entry in the global offset table. */
5180 bfd_put_32 (output_bfd
,
5181 splt
->output_section
->vma
+ splt
->output_offset
5182 + plt_index
* 4, sgotplt
->contents
+ got_offset
);
5185 /* Fill in the entry in the .rela.plt section. */
5186 rela
.r_offset
= got_address
;
5187 rela
.r_info
= ELF32_R_INFO (h
->dynindx
, R_NIOS2_JUMP_SLOT
);
5189 loc
= srela
->contents
+ plt_index
* sizeof (Elf32_External_Rela
);
5190 bfd_elf32_swap_reloca_out (output_bfd
, &rela
, loc
);
5192 if (!h
->def_regular
)
5194 /* Mark the symbol as undefined, rather than as defined in
5195 the .plt section. Leave the value alone. */
5196 sym
->st_shndx
= SHN_UNDEF
;
5197 /* If the symbol is weak, we do need to clear the value.
5198 Otherwise, the PLT entry would provide a definition for
5199 the symbol even if the symbol wasn't defined anywhere,
5200 and so the symbol would never be NULL. */
5201 if (!h
->ref_regular_nonweak
)
5206 use_plt
= (eh
->got_types_used
== CALL_USED
5207 && h
->plt
.offset
!= (bfd_vma
) -1);
5209 if (!use_plt
&& h
->got
.offset
!= (bfd_vma
) -1
5210 && (elf32_nios2_hash_entry (h
)->tls_type
& GOT_TLS_GD
) == 0
5211 && (elf32_nios2_hash_entry (h
)->tls_type
& GOT_TLS_IE
) == 0)
5215 Elf_Internal_Rela rela
;
5219 /* This symbol has an entry in the global offset table. Set it
5221 sgot
= htab
->root
.sgot
;
5222 srela
= htab
->root
.srelgot
;
5223 BFD_ASSERT (sgot
!= NULL
&& srela
!= NULL
);
5225 offset
= (h
->got
.offset
& ~(bfd_vma
) 1);
5226 rela
.r_offset
= (sgot
->output_section
->vma
5227 + sgot
->output_offset
+ offset
);
5229 /* If this is a -Bsymbolic link, and the symbol is defined
5230 locally, we just want to emit a RELATIVE reloc. Likewise if
5231 the symbol was forced to be local because of a version file.
5232 The entry in the global offset table will already have been
5233 initialized in the relocate_section function. */
5235 if (bfd_link_pic (info
) && SYMBOL_REFERENCES_LOCAL (info
, h
))
5237 rela
.r_info
= ELF32_R_INFO (0, R_NIOS2_RELATIVE
);
5238 rela
.r_addend
= bfd_get_signed_32 (output_bfd
,
5239 (sgot
->contents
+ offset
));
5240 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
+ offset
);
5244 bfd_put_32 (output_bfd
, (bfd_vma
) 0,
5245 sgot
->contents
+ offset
);
5246 rela
.r_info
= ELF32_R_INFO (h
->dynindx
, R_NIOS2_GLOB_DAT
);
5250 loc
= srela
->contents
;
5251 loc
+= srela
->reloc_count
++ * sizeof (Elf32_External_Rela
);
5252 bfd_elf32_swap_reloca_out (output_bfd
, &rela
, loc
);
5255 if (use_plt
&& h
->got
.offset
!= (bfd_vma
) -1)
5257 bfd_vma offset
= (h
->got
.offset
& ~(bfd_vma
) 1);
5258 asection
*sgot
= htab
->root
.sgot
;
5259 asection
*splt
= htab
->root
.splt
;
5260 bfd_put_32 (output_bfd
, (splt
->output_section
->vma
+ splt
->output_offset
5262 sgot
->contents
+ offset
);
5268 Elf_Internal_Rela rela
;
5271 /* This symbol needs a copy reloc. Set it up. */
5272 BFD_ASSERT (h
->dynindx
!= -1
5273 && (h
->root
.type
== bfd_link_hash_defined
5274 || h
->root
.type
== bfd_link_hash_defweak
));
5277 BFD_ASSERT (s
!= NULL
);
5279 rela
.r_offset
= (h
->root
.u
.def
.value
5280 + h
->root
.u
.def
.section
->output_section
->vma
5281 + h
->root
.u
.def
.section
->output_offset
);
5282 rela
.r_info
= ELF32_R_INFO (h
->dynindx
, R_NIOS2_COPY
);
5284 loc
= s
->contents
+ s
->reloc_count
++ * sizeof (Elf32_External_Rela
);
5285 bfd_elf32_swap_reloca_out (output_bfd
, &rela
, loc
);
5288 /* Mark _DYNAMIC, _GLOBAL_OFFSET_TABLE_, and _gp_got as absolute. */
5289 if (strcmp (h
->root
.root
.string
, "_DYNAMIC") == 0
5290 || h
== elf_hash_table (info
)->hgot
5291 || h
== elf32_nios2_hash_table (info
)->h_gp_got
)
5292 sym
->st_shndx
= SHN_ABS
;
5297 /* Implement elf_backend_finish_dynamic_sections. */
5299 nios2_elf32_finish_dynamic_sections (bfd
*output_bfd
,
5300 struct bfd_link_info
*info
)
5305 struct elf32_nios2_link_hash_table
*htab
;
5307 htab
= elf32_nios2_hash_table (info
);
5308 dynobj
= elf_hash_table (info
)->dynobj
;
5309 sgotplt
= htab
->root
.sgotplt
;
5310 BFD_ASSERT (sgotplt
!= NULL
);
5311 sdyn
= bfd_get_linker_section (dynobj
, ".dynamic");
5313 if (elf_hash_table (info
)->dynamic_sections_created
)
5316 Elf32_External_Dyn
*dyncon
, *dynconend
;
5318 splt
= htab
->root
.splt
;
5319 BFD_ASSERT (splt
!= NULL
&& sdyn
!= NULL
);
5321 dyncon
= (Elf32_External_Dyn
*) sdyn
->contents
;
5322 dynconend
= (Elf32_External_Dyn
*) (sdyn
->contents
+ sdyn
->size
);
5323 for (; dyncon
< dynconend
; dyncon
++)
5325 Elf_Internal_Dyn dyn
;
5328 bfd_elf32_swap_dyn_in (dynobj
, dyncon
, &dyn
);
5336 s
= htab
->root
.sgot
;
5337 BFD_ASSERT (s
!= NULL
);
5338 dyn
.d_un
.d_ptr
= s
->output_section
->vma
;
5339 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
5343 s
= htab
->root
.srelplt
;
5344 BFD_ASSERT (s
!= NULL
);
5345 dyn
.d_un
.d_ptr
= s
->output_section
->vma
;
5346 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
5350 s
= htab
->root
.srelplt
;
5351 BFD_ASSERT (s
!= NULL
);
5352 dyn
.d_un
.d_val
= s
->size
;
5353 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
5357 /* The procedure linkage table relocs (DT_JMPREL) should
5358 not be included in the overall relocs (DT_RELA).
5359 Therefore, we override the DT_RELASZ entry here to
5360 make it not include the JMPREL relocs. Since the
5361 linker script arranges for .rela.plt to follow all
5362 other relocation sections, we don't have to worry
5363 about changing the DT_RELA entry. */
5364 s
= htab
->root
.srelplt
;
5366 dyn
.d_un
.d_val
-= s
->size
;
5367 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
5371 s
= htab
->root
.sgot
;
5372 BFD_ASSERT (s
!= NULL
);
5373 dyn
.d_un
.d_ptr
= s
->output_section
->vma
+ 0x7ff0;
5374 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
5379 /* Fill in the first entry in the procedure linkage table. */
5382 bfd_vma got_address
= (sgotplt
->output_section
->vma
5383 + sgotplt
->output_offset
);
5384 if (bfd_link_pic (info
))
5386 bfd_vma corrected
= got_address
- (splt
->output_section
->vma
5387 + splt
->output_offset
+ 4);
5388 nios2_elf32_install_data (splt
, nios2_so_plt0_entry
, 0, 6);
5389 nios2_elf32_install_imm16 (splt
, 4, hiadj (corrected
));
5390 nios2_elf32_install_imm16 (splt
, 12, (corrected
& 0xffff) + 4);
5391 nios2_elf32_install_imm16 (splt
, 16, (corrected
& 0xffff) + 8);
5395 /* Divide by 4 here, not 3 because we already corrected for the
5397 bfd_vma res_size
= (splt
->size
- 28) / 4;
5398 bfd_vma res_start
= (splt
->output_section
->vma
5399 + splt
->output_offset
);
5402 for (res_offset
= 0; res_offset
< res_size
; res_offset
+= 4)
5403 bfd_put_32 (output_bfd
,
5404 6 | ((res_size
- (res_offset
+ 4)) << 6),
5405 splt
->contents
+ res_offset
);
5407 nios2_elf32_install_data (splt
, nios2_plt0_entry
, res_size
, 7);
5408 nios2_elf32_install_imm16 (splt
, res_size
, hiadj (res_start
));
5409 nios2_elf32_install_imm16 (splt
, res_size
+ 4,
5410 res_start
& 0xffff);
5411 nios2_elf32_install_imm16 (splt
, res_size
+ 12,
5412 hiadj (got_address
));
5413 nios2_elf32_install_imm16 (splt
, res_size
+ 16,
5414 (got_address
& 0xffff) + 4);
5415 nios2_elf32_install_imm16 (splt
, res_size
+ 20,
5416 (got_address
& 0xffff) + 8);
5420 /* Fill in the first three entries in the global offset table. */
5421 if (sgotplt
->size
> 0)
5424 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgotplt
->contents
);
5426 bfd_put_32 (output_bfd
,
5427 sdyn
->output_section
->vma
+ sdyn
->output_offset
,
5429 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgotplt
->contents
+ 4);
5430 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgotplt
->contents
+ 8);
5433 elf_section_data (sgotplt
->output_section
)->this_hdr
.sh_entsize
= 4;
5438 /* Implement elf_backend_adjust_dynamic_symbol:
5439 Adjust a symbol defined by a dynamic object and referenced by a
5440 regular object. The current definition is in some section of the
5441 dynamic object, but we're not including those sections. We have to
5442 change the definition to something the rest of the link can
5445 nios2_elf32_adjust_dynamic_symbol (struct bfd_link_info
*info
,
5446 struct elf_link_hash_entry
*h
)
5448 struct elf32_nios2_link_hash_table
*htab
;
5453 htab
= elf32_nios2_hash_table (info
);
5454 dynobj
= elf_hash_table (info
)->dynobj
;
5456 /* Make sure we know what is going on here. */
5457 BFD_ASSERT (dynobj
!= NULL
5459 || h
->u
.weakdef
!= NULL
5462 && !h
->def_regular
)));
5464 /* If this is a function, put it in the procedure linkage table. We
5465 will fill in the contents of the procedure linkage table later,
5466 when we know the address of the .got section. */
5467 if (h
->type
== STT_FUNC
|| h
->needs_plt
)
5469 if (h
->plt
.refcount
<= 0
5470 || SYMBOL_CALLS_LOCAL (info
, h
)
5471 || (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
5472 && h
->root
.type
== bfd_link_hash_undefweak
))
5474 /* This case can occur if we saw a PLT reloc in an input
5475 file, but the symbol was never referred to by a dynamic
5476 object, or if all references were garbage collected. In
5477 such a case, we don't actually need to build a procedure
5478 linkage table, and we can just do a PCREL reloc instead. */
5479 h
->plt
.offset
= (bfd_vma
) -1;
5486 /* Reinitialize the plt offset now that it is not used as a reference
5488 h
->plt
.offset
= (bfd_vma
) -1;
5490 /* If this is a weak symbol, and there is a real definition, the
5491 processor independent code will have arranged for us to see the
5492 real definition first, and we can just use the same value. */
5493 if (h
->u
.weakdef
!= NULL
)
5495 BFD_ASSERT (h
->u
.weakdef
->root
.type
== bfd_link_hash_defined
5496 || h
->u
.weakdef
->root
.type
== bfd_link_hash_defweak
);
5497 h
->root
.u
.def
.section
= h
->u
.weakdef
->root
.u
.def
.section
;
5498 h
->root
.u
.def
.value
= h
->u
.weakdef
->root
.u
.def
.value
;
5502 /* If there are no non-GOT references, we do not need a copy
5504 if (!h
->non_got_ref
)
5507 /* This is a reference to a symbol defined by a dynamic object which
5509 If we are creating a shared library, we must presume that the
5510 only references to the symbol are via the global offset table.
5511 For such cases we need not do anything here; the relocations will
5512 be handled correctly by relocate_section. */
5513 if (bfd_link_pic (info
))
5518 (*_bfd_error_handler
) (_("dynamic variable `%s' is zero size"),
5519 h
->root
.root
.string
);
5523 /* We must allocate the symbol in our .dynbss section, which will
5524 become part of the .bss section of the executable. There will be
5525 an entry for this symbol in the .dynsym section. The dynamic
5526 object will contain position independent code, so all references
5527 from the dynamic object to this symbol will go through the global
5528 offset table. The dynamic linker will use the .dynsym entry to
5529 determine the address it must put in the global offset table, so
5530 both the dynamic object and the regular object will refer to the
5531 same memory location for the variable. */
5533 BFD_ASSERT (s
!= NULL
);
5535 /* We must generate a R_NIOS2_COPY reloc to tell the dynamic linker to
5536 copy the initial value out of the dynamic object and into the
5537 runtime process image. We need to remember the offset into the
5538 .rela.bss section we are going to use. */
5539 if ((h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0)
5543 srel
= htab
->srelbss
;
5544 BFD_ASSERT (srel
!= NULL
);
5545 srel
->size
+= sizeof (Elf32_External_Rela
);
5549 align2
= bfd_log2 (h
->size
);
5550 if (align2
> h
->root
.u
.def
.section
->alignment_power
)
5551 align2
= h
->root
.u
.def
.section
->alignment_power
;
5554 s
->size
= BFD_ALIGN (s
->size
, (bfd_size_type
)1 << align2
);
5555 if (align2
> bfd_get_section_alignment (dynobj
, s
)
5556 && !bfd_set_section_alignment (dynobj
, s
, align2
))
5559 /* Define the symbol as being at this point in the section. */
5560 h
->root
.u
.def
.section
= s
;
5561 h
->root
.u
.def
.value
= s
->size
;
5563 /* Increment the section size to make room for the symbol. */
5569 /* Worker function for nios2_elf32_size_dynamic_sections. */
5571 adjust_dynrelocs (struct elf_link_hash_entry
*h
, PTR inf
)
5573 struct bfd_link_info
*info
;
5574 struct elf32_nios2_link_hash_table
*htab
;
5576 if (h
->root
.type
== bfd_link_hash_indirect
)
5579 if (h
->root
.type
== bfd_link_hash_warning
)
5580 /* When warning symbols are created, they **replace** the "real"
5581 entry in the hash table, thus we never get to see the real
5582 symbol in a hash traversal. So look at it now. */
5583 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5585 info
= (struct bfd_link_info
*) inf
;
5586 htab
= elf32_nios2_hash_table (info
);
5588 if (h
->plt
.offset
!= (bfd_vma
)-1)
5589 h
->plt
.offset
+= htab
->res_n_size
;
5590 if (htab
->root
.splt
== h
->root
.u
.def
.section
)
5591 h
->root
.u
.def
.value
+= htab
->res_n_size
;
5596 /* Another worker function for nios2_elf32_size_dynamic_sections.
5597 Allocate space in .plt, .got and associated reloc sections for
5600 allocate_dynrelocs (struct elf_link_hash_entry
*h
, PTR inf
)
5602 struct bfd_link_info
*info
;
5603 struct elf32_nios2_link_hash_table
*htab
;
5604 struct elf32_nios2_link_hash_entry
*eh
;
5605 struct elf32_nios2_dyn_relocs
*p
;
5608 if (h
->root
.type
== bfd_link_hash_indirect
)
5611 if (h
->root
.type
== bfd_link_hash_warning
)
5612 /* When warning symbols are created, they **replace** the "real"
5613 entry in the hash table, thus we never get to see the real
5614 symbol in a hash traversal. So look at it now. */
5615 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5617 info
= (struct bfd_link_info
*) inf
;
5618 htab
= elf32_nios2_hash_table (info
);
5620 if (htab
->root
.dynamic_sections_created
5621 && h
->plt
.refcount
> 0)
5623 /* Make sure this symbol is output as a dynamic symbol.
5624 Undefined weak syms won't yet be marked as dynamic. */
5625 if (h
->dynindx
== -1
5627 && !bfd_elf_link_record_dynamic_symbol (info
, h
))
5630 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info
), h
))
5632 asection
*s
= htab
->root
.splt
;
5634 /* Allocate room for the header. */
5637 if (bfd_link_pic (info
))
5643 h
->plt
.offset
= s
->size
;
5645 /* If this symbol is not defined in a regular file, and we are
5646 not generating a shared library, then set the symbol to this
5647 location in the .plt. This is required to make function
5648 pointers compare as equal between the normal executable and
5649 the shared library. */
5650 if (! bfd_link_pic (info
)
5653 h
->root
.u
.def
.section
= s
;
5654 h
->root
.u
.def
.value
= h
->plt
.offset
;
5657 /* Make room for this entry. */
5660 /* We also need to make an entry in the .rela.plt section. */
5661 htab
->root
.srelplt
->size
+= sizeof (Elf32_External_Rela
);
5663 /* And the .got.plt section. */
5664 htab
->root
.sgotplt
->size
+= 4;
5668 h
->plt
.offset
= (bfd_vma
) -1;
5674 h
->plt
.offset
= (bfd_vma
) -1;
5678 eh
= (struct elf32_nios2_link_hash_entry
*) h
;
5679 use_plt
= (eh
->got_types_used
== CALL_USED
5680 && h
->plt
.offset
!= (bfd_vma
) -1);
5682 if (h
->got
.refcount
> 0)
5686 int tls_type
= eh
->tls_type
;
5689 /* Make sure this symbol is output as a dynamic symbol.
5690 Undefined weak syms won't yet be marked as dynamic. */
5691 if (h
->dynindx
== -1
5693 && !bfd_elf_link_record_dynamic_symbol (info
, h
))
5696 s
= htab
->root
.sgot
;
5697 h
->got
.offset
= s
->size
;
5699 if (tls_type
== GOT_UNKNOWN
)
5702 if (tls_type
== GOT_NORMAL
)
5703 /* Non-TLS symbols need one GOT slot. */
5707 if (tls_type
& GOT_TLS_GD
)
5708 /* R_NIOS2_TLS_GD16 needs 2 consecutive GOT slots. */
5710 if (tls_type
& GOT_TLS_IE
)
5711 /* R_NIOS2_TLS_IE16 needs one GOT slot. */
5715 dyn
= htab
->root
.dynamic_sections_created
;
5718 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, bfd_link_pic (info
), h
)
5719 && (!bfd_link_pic (info
)
5720 || !SYMBOL_REFERENCES_LOCAL (info
, h
)))
5723 if (tls_type
!= GOT_NORMAL
5724 && (bfd_link_pic (info
) || indx
!= 0)
5725 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
5726 || h
->root
.type
!= bfd_link_hash_undefweak
))
5728 if (tls_type
& GOT_TLS_IE
)
5729 htab
->root
.srelgot
->size
+= sizeof (Elf32_External_Rela
);
5731 if (tls_type
& GOT_TLS_GD
)
5732 htab
->root
.srelgot
->size
+= sizeof (Elf32_External_Rela
);
5734 if ((tls_type
& GOT_TLS_GD
) && indx
!= 0)
5735 htab
->root
.srelgot
->size
+= sizeof (Elf32_External_Rela
);
5737 else if ((ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
5738 || h
->root
.type
!= bfd_link_hash_undefweak
)
5740 && (bfd_link_pic (info
)
5741 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, 0, h
)))
5742 htab
->root
.srelgot
->size
+= sizeof (Elf32_External_Rela
);
5745 h
->got
.offset
= (bfd_vma
) -1;
5747 if (eh
->dyn_relocs
== NULL
)
5750 /* In the shared -Bsymbolic case, discard space allocated for
5751 dynamic pc-relative relocs against symbols which turn out to be
5752 defined in regular objects. For the normal shared case, discard
5753 space for pc-relative relocs that have become local due to symbol
5754 visibility changes. */
5756 if (bfd_link_pic (info
))
5759 && (h
->forced_local
|| SYMBOLIC_BIND (info
, h
)))
5761 struct elf32_nios2_dyn_relocs
**pp
;
5763 for (pp
= &eh
->dyn_relocs
; (p
= *pp
) != NULL
; )
5765 p
->count
-= p
->pc_count
;
5774 /* Also discard relocs on undefined weak syms with non-default
5776 if (eh
->dyn_relocs
!= NULL
5777 && h
->root
.type
== bfd_link_hash_undefweak
)
5779 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
5780 eh
->dyn_relocs
= NULL
;
5782 /* Make sure undefined weak symbols are output as a dynamic
5784 else if (h
->dynindx
== -1
5786 && !bfd_elf_link_record_dynamic_symbol (info
, h
))
5792 /* For the non-shared case, discard space for relocs against
5793 symbols which turn out to need copy relocs or are not
5797 && ((h
->def_dynamic
&& !h
->def_regular
)
5798 || (htab
->root
.dynamic_sections_created
5799 && (h
->root
.type
== bfd_link_hash_undefweak
5800 || h
->root
.type
== bfd_link_hash_undefined
))))
5802 /* Make sure this symbol is output as a dynamic symbol.
5803 Undefined weak syms won't yet be marked as dynamic. */
5804 if (h
->dynindx
== -1
5806 && !bfd_elf_link_record_dynamic_symbol (info
, h
))
5809 /* If that succeeded, we know we'll be keeping all the
5811 if (h
->dynindx
!= -1)
5815 eh
->dyn_relocs
= NULL
;
5820 /* Finally, allocate space. */
5821 for (p
= eh
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
5823 asection
*sreloc
= elf_section_data (p
->sec
)->sreloc
;
5824 sreloc
->size
+= p
->count
* sizeof (Elf32_External_Rela
);
5830 /* Implement elf_backend_size_dynamic_sections:
5831 Set the sizes of the dynamic sections. */
5833 nios2_elf32_size_dynamic_sections (bfd
*output_bfd ATTRIBUTE_UNUSED
,
5834 struct bfd_link_info
*info
)
5842 struct elf32_nios2_link_hash_table
*htab
;
5844 htab
= elf32_nios2_hash_table (info
);
5845 dynobj
= elf_hash_table (info
)->dynobj
;
5846 BFD_ASSERT (dynobj
!= NULL
);
5848 htab
->res_n_size
= 0;
5849 if (elf_hash_table (info
)->dynamic_sections_created
)
5851 /* Set the contents of the .interp section to the interpreter. */
5852 if (bfd_link_executable (info
) && !info
->nointerp
)
5854 s
= bfd_get_linker_section (dynobj
, ".interp");
5855 BFD_ASSERT (s
!= NULL
);
5856 s
->size
= sizeof ELF_DYNAMIC_INTERPRETER
;
5857 s
->contents
= (unsigned char *) ELF_DYNAMIC_INTERPRETER
;
5862 /* We may have created entries in the .rela.got section.
5863 However, if we are not creating the dynamic sections, we will
5864 not actually use these entries. Reset the size of .rela.got,
5865 which will cause it to get stripped from the output file
5867 s
= htab
->root
.srelgot
;
5872 /* Set up .got offsets for local syms, and space for local dynamic
5874 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
5876 bfd_signed_vma
*local_got
;
5877 bfd_signed_vma
*end_local_got
;
5878 char *local_tls_type
;
5879 bfd_size_type locsymcount
;
5880 Elf_Internal_Shdr
*symtab_hdr
;
5883 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
5886 for (s
= ibfd
->sections
; s
!= NULL
; s
= s
->next
)
5888 struct elf32_nios2_dyn_relocs
*p
;
5890 for (p
= elf_section_data (s
)->local_dynrel
; p
!= NULL
; p
= p
->next
)
5892 if (!bfd_is_abs_section (p
->sec
)
5893 && bfd_is_abs_section (p
->sec
->output_section
))
5895 /* Input section has been discarded, either because
5896 it is a copy of a linkonce section or due to
5897 linker script /DISCARD/, so we'll be discarding
5900 else if (p
->count
!= 0)
5902 srel
= elf_section_data (p
->sec
)->sreloc
;
5903 srel
->size
+= p
->count
* sizeof (Elf32_External_Rela
);
5904 if ((p
->sec
->output_section
->flags
& SEC_READONLY
) != 0)
5905 info
->flags
|= DF_TEXTREL
;
5910 local_got
= elf_local_got_refcounts (ibfd
);
5914 symtab_hdr
= &elf_tdata (ibfd
)->symtab_hdr
;
5915 locsymcount
= symtab_hdr
->sh_info
;
5916 end_local_got
= local_got
+ locsymcount
;
5917 local_tls_type
= elf32_nios2_local_got_tls_type (ibfd
);
5918 s
= htab
->root
.sgot
;
5919 srel
= htab
->root
.srelgot
;
5920 for (; local_got
< end_local_got
; ++local_got
, ++local_tls_type
)
5924 *local_got
= s
->size
;
5925 if (*local_tls_type
& GOT_TLS_GD
)
5926 /* TLS_GD relocs need an 8-byte structure in the GOT. */
5928 if (*local_tls_type
& GOT_TLS_IE
)
5930 if (*local_tls_type
== GOT_NORMAL
)
5933 if (bfd_link_pic (info
) || *local_tls_type
== GOT_TLS_GD
)
5934 srel
->size
+= sizeof (Elf32_External_Rela
);
5937 *local_got
= (bfd_vma
) -1;
5941 if (htab
->tls_ldm_got
.refcount
> 0)
5943 /* Allocate two GOT entries and one dynamic relocation (if necessary)
5944 for R_NIOS2_TLS_LDM16 relocations. */
5945 htab
->tls_ldm_got
.offset
= htab
->root
.sgot
->size
;
5946 htab
->root
.sgot
->size
+= 8;
5947 if (bfd_link_pic (info
))
5948 htab
->root
.srelgot
->size
+= sizeof (Elf32_External_Rela
);
5951 htab
->tls_ldm_got
.offset
= -1;
5953 /* Allocate global sym .plt and .got entries, and space for global
5954 sym dynamic relocs. */
5955 elf_link_hash_traverse (& htab
->root
, allocate_dynrelocs
, info
);
5957 if (elf_hash_table (info
)->dynamic_sections_created
)
5959 /* If the .got section is more than 0x8000 bytes, we add
5960 0x8000 to the value of _gp_got, so that 16-bit relocations
5961 have a greater chance of working. */
5962 if (htab
->root
.sgot
->size
>= 0x8000
5963 && elf32_nios2_hash_table (info
)->h_gp_got
->root
.u
.def
.value
== 0)
5964 elf32_nios2_hash_table (info
)->h_gp_got
->root
.u
.def
.value
= 0x8000;
5967 /* The check_relocs and adjust_dynamic_symbol entry points have
5968 determined the sizes of the various dynamic sections. Allocate
5973 for (s
= dynobj
->sections
; s
!= NULL
; s
= s
->next
)
5977 if ((s
->flags
& SEC_LINKER_CREATED
) == 0)
5980 /* It's OK to base decisions on the section name, because none
5981 of the dynobj section names depend upon the input files. */
5982 name
= bfd_get_section_name (dynobj
, s
);
5984 if (strcmp (name
, ".plt") == 0)
5986 /* Remember whether there is a PLT. */
5989 /* Correct for the number of res_N branches. */
5990 if (plt
&& !bfd_link_pic (info
))
5992 htab
->res_n_size
= (s
->size
-28) / 3;
5993 s
->size
+= htab
->res_n_size
;
5996 else if (CONST_STRNEQ (name
, ".rela"))
6002 /* We use the reloc_count field as a counter if we need
6003 to copy relocs into the output file. */
6007 else if (CONST_STRNEQ (name
, ".got"))
6009 else if (strcmp (name
, ".dynbss") != 0)
6010 /* It's not one of our sections, so don't allocate space. */
6015 /* If we don't need this section, strip it from the
6016 output file. This is mostly to handle .rela.bss and
6017 .rela.plt. We must create both sections in
6018 create_dynamic_sections, because they must be created
6019 before the linker maps input sections to output
6020 sections. The linker does that before
6021 adjust_dynamic_symbol is called, and it is that
6022 function which decides whether anything needs to go
6023 into these sections. */
6024 s
->flags
|= SEC_EXCLUDE
;
6028 if ((s
->flags
& SEC_HAS_CONTENTS
) == 0)
6031 /* Allocate memory for the section contents. */
6032 /* FIXME: This should be a call to bfd_alloc not bfd_zalloc.
6033 Unused entries should be reclaimed before the section's contents
6034 are written out, but at the moment this does not happen. Thus in
6035 order to prevent writing out garbage, we initialize the section's
6036 contents to zero. */
6037 s
->contents
= (bfd_byte
*) bfd_zalloc (dynobj
, s
->size
);
6038 if (s
->contents
== NULL
)
6042 /* Adjust dynamic symbols that point to the plt to account for the
6043 now-known number of resN slots. */
6044 if (htab
->res_n_size
)
6045 elf_link_hash_traverse (& htab
->root
, adjust_dynrelocs
, info
);
6047 if (elf_hash_table (info
)->dynamic_sections_created
)
6049 /* Add some entries to the .dynamic section. We fill in the
6050 values later, in elf_nios2_finish_dynamic_sections, but we
6051 must add the entries now so that we get the correct size for
6052 the .dynamic section. The DT_DEBUG entry is filled in by the
6053 dynamic linker and used by the debugger. */
6054 #define add_dynamic_entry(TAG, VAL) \
6055 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
6057 if (!bfd_link_pic (info
) && !add_dynamic_entry (DT_DEBUG
, 0))
6060 if (got
&& !add_dynamic_entry (DT_PLTGOT
, 0))
6064 && (!add_dynamic_entry (DT_PLTRELSZ
, 0)
6065 || !add_dynamic_entry (DT_PLTREL
, DT_RELA
)
6066 || !add_dynamic_entry (DT_JMPREL
, 0)))
6070 && (!add_dynamic_entry (DT_RELA
, 0)
6071 || !add_dynamic_entry (DT_RELASZ
, 0)
6072 || !add_dynamic_entry (DT_RELAENT
, sizeof (Elf32_External_Rela
))))
6075 if (!bfd_link_pic (info
) && !add_dynamic_entry (DT_NIOS2_GP
, 0))
6078 if ((info
->flags
& DF_TEXTREL
) != 0
6079 && !add_dynamic_entry (DT_TEXTREL
, 0))
6082 #undef add_dynamic_entry
6087 /* Free the derived linker hash table. */
6089 nios2_elf32_link_hash_table_free (bfd
*obfd
)
6091 struct elf32_nios2_link_hash_table
*htab
6092 = (struct elf32_nios2_link_hash_table
*) obfd
->link
.hash
;
6094 bfd_hash_table_free (&htab
->bstab
);
6095 _bfd_elf_link_hash_table_free (obfd
);
6098 /* Implement bfd_elf32_bfd_link_hash_table_create. */
6099 static struct bfd_link_hash_table
*
6100 nios2_elf32_link_hash_table_create (bfd
*abfd
)
6102 struct elf32_nios2_link_hash_table
*ret
;
6103 bfd_size_type amt
= sizeof (struct elf32_nios2_link_hash_table
);
6105 ret
= bfd_zmalloc (amt
);
6109 if (!_bfd_elf_link_hash_table_init (&ret
->root
, abfd
,
6112 elf32_nios2_link_hash_entry
),
6119 /* Init the stub hash table too. */
6120 if (!bfd_hash_table_init (&ret
->bstab
, stub_hash_newfunc
,
6121 sizeof (struct elf32_nios2_stub_hash_entry
)))
6123 _bfd_elf_link_hash_table_free (abfd
);
6126 ret
->root
.root
.hash_table_free
= nios2_elf32_link_hash_table_free
;
6128 return &ret
->root
.root
;
6131 /* Implement elf_backend_reloc_type_class. */
6132 static enum elf_reloc_type_class
6133 nios2_elf32_reloc_type_class (const struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
6134 const asection
*rel_sec ATTRIBUTE_UNUSED
,
6135 const Elf_Internal_Rela
*rela
)
6137 switch ((int) ELF32_R_TYPE (rela
->r_info
))
6139 case R_NIOS2_RELATIVE
:
6140 return reloc_class_relative
;
6141 case R_NIOS2_JUMP_SLOT
:
6142 return reloc_class_plt
;
6144 return reloc_class_copy
;
6146 return reloc_class_normal
;
6150 /* Return 1 if target is one of ours. */
6152 is_nios2_elf_target (const struct bfd_target
*targ
)
6154 return (targ
== &nios2_elf32_le_vec
6155 || targ
== &nios2_elf32_be_vec
);
6158 /* Implement elf_backend_add_symbol_hook.
6159 This hook is called by the linker when adding symbols from an object
6160 file. We use it to put .comm items in .sbss, and not .bss. */
6162 nios2_elf_add_symbol_hook (bfd
*abfd
,
6163 struct bfd_link_info
*info
,
6164 Elf_Internal_Sym
*sym
,
6165 const char **namep ATTRIBUTE_UNUSED
,
6166 flagword
*flagsp ATTRIBUTE_UNUSED
,
6172 if (sym
->st_shndx
== SHN_COMMON
6173 && !bfd_link_relocatable (info
)
6174 && sym
->st_size
<= elf_gp_size (abfd
)
6175 && is_nios2_elf_target (info
->output_bfd
->xvec
))
6177 /* Common symbols less than or equal to -G nn bytes are automatically
6179 struct elf32_nios2_link_hash_table
*htab
;
6181 htab
= elf32_nios2_hash_table (info
);
6182 if (htab
->sbss
== NULL
)
6184 flagword flags
= SEC_IS_COMMON
| SEC_LINKER_CREATED
;
6186 dynobj
= elf_hash_table (info
)->dynobj
;
6190 htab
->sbss
= bfd_make_section_anyway_with_flags (dynobj
, ".sbss",
6192 if (htab
->sbss
== NULL
)
6197 *valp
= sym
->st_size
;
6203 /* Implement elf_backend_can_make_relative_eh_frame:
6204 Decide whether to attempt to turn absptr or lsda encodings in
6205 shared libraries into pcrel within the given input section. */
6207 nios2_elf32_can_make_relative_eh_frame (bfd
*input_bfd ATTRIBUTE_UNUSED
,
6208 struct bfd_link_info
*info
6210 asection
*eh_frame_section
6213 /* We can't use PC-relative encodings in the .eh_frame section. */
6217 /* Implement elf_backend_special_sections. */
6218 const struct bfd_elf_special_section elf32_nios2_special_sections
[] =
6220 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS
,
6221 SHF_ALLOC
+ SHF_WRITE
+ SHF_NIOS2_GPREL
},
6222 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS
,
6223 SHF_ALLOC
+ SHF_WRITE
+ SHF_NIOS2_GPREL
},
6224 { NULL
, 0, 0, 0, 0 }
6227 #define ELF_ARCH bfd_arch_nios2
6228 #define ELF_TARGET_ID NIOS2_ELF_DATA
6229 #define ELF_MACHINE_CODE EM_ALTERA_NIOS2
6231 /* The Nios II MMU uses a 4K page size. */
6233 #define ELF_MAXPAGESIZE 0x1000
6235 #define bfd_elf32_bfd_link_hash_table_create \
6236 nios2_elf32_link_hash_table_create
6238 #define bfd_elf32_bfd_merge_private_bfd_data \
6239 nios2_elf32_merge_private_bfd_data
6241 /* Relocation table lookup macros. */
6243 #define bfd_elf32_bfd_reloc_type_lookup nios2_elf32_bfd_reloc_type_lookup
6244 #define bfd_elf32_bfd_reloc_name_lookup nios2_elf32_bfd_reloc_name_lookup
6246 /* JUMP_TABLE_LINK macros. */
6248 /* elf_info_to_howto (using RELA relocations). */
6250 #define elf_info_to_howto nios2_elf32_info_to_howto
6252 /* elf backend functions. */
6254 #define elf_backend_can_gc_sections 1
6255 #define elf_backend_can_refcount 1
6256 #define elf_backend_plt_readonly 1
6257 #define elf_backend_want_got_plt 1
6258 #define elf_backend_rela_normal 1
6260 #define elf_backend_relocate_section nios2_elf32_relocate_section
6261 #define elf_backend_section_flags nios2_elf32_section_flags
6262 #define elf_backend_fake_sections nios2_elf32_fake_sections
6263 #define elf_backend_check_relocs nios2_elf32_check_relocs
6265 #define elf_backend_gc_mark_hook nios2_elf32_gc_mark_hook
6266 #define elf_backend_gc_sweep_hook nios2_elf32_gc_sweep_hook
6267 #define elf_backend_create_dynamic_sections \
6268 nios2_elf32_create_dynamic_sections
6269 #define elf_backend_finish_dynamic_symbol nios2_elf32_finish_dynamic_symbol
6270 #define elf_backend_finish_dynamic_sections \
6271 nios2_elf32_finish_dynamic_sections
6272 #define elf_backend_adjust_dynamic_symbol nios2_elf32_adjust_dynamic_symbol
6273 #define elf_backend_reloc_type_class nios2_elf32_reloc_type_class
6274 #define elf_backend_size_dynamic_sections nios2_elf32_size_dynamic_sections
6275 #define elf_backend_add_symbol_hook nios2_elf_add_symbol_hook
6276 #define elf_backend_copy_indirect_symbol nios2_elf32_copy_indirect_symbol
6277 #define elf_backend_object_p nios2_elf32_object_p
6279 #define elf_backend_grok_prstatus nios2_grok_prstatus
6280 #define elf_backend_grok_psinfo nios2_grok_psinfo
6282 #undef elf_backend_can_make_relative_eh_frame
6283 #define elf_backend_can_make_relative_eh_frame \
6284 nios2_elf32_can_make_relative_eh_frame
6286 #define elf_backend_special_sections elf32_nios2_special_sections
6288 #define TARGET_LITTLE_SYM nios2_elf32_le_vec
6289 #define TARGET_LITTLE_NAME "elf32-littlenios2"
6290 #define TARGET_BIG_SYM nios2_elf32_be_vec
6291 #define TARGET_BIG_NAME "elf32-bignios2"
6293 #define elf_backend_got_header_size 12
6294 #define elf_backend_default_execstack 0
6296 #include "elf32-target.h"