1 /* 32-bit ELF support for Nios II.
2 Copyright (C) 2012-2020 Free Software Foundation, Inc.
3 Contributed by Nigel Gray (ngray@altera.com).
4 Contributed by Mentor Graphics, Inc.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
23 /* This file handles Altera Nios II ELF targets. */
31 #include "elf/nios2.h"
32 #include "opcode/nios2.h"
33 #include "elf32-nios2.h"
34 #include "libiberty.h"
36 /* Use RELA relocations. */
45 /* Forward declarations. */
46 static bfd_reloc_status_type nios2_elf32_ignore_reloc
47 (bfd
*, arelent
*, asymbol
*, void *, asection
*, bfd
*, char **);
48 static bfd_reloc_status_type nios2_elf32_hi16_relocate
49 (bfd
*, arelent
*, asymbol
*, void *, asection
*, bfd
*, char **);
50 static bfd_reloc_status_type nios2_elf32_lo16_relocate
51 (bfd
*, arelent
*, asymbol
*, void *, asection
*, bfd
*, char **);
52 static bfd_reloc_status_type nios2_elf32_hiadj16_relocate
53 (bfd
*, arelent
*, asymbol
*, void *, asection
*, bfd
*, char **);
54 static bfd_reloc_status_type nios2_elf32_pcrel_lo16_relocate
55 (bfd
*, arelent
*, asymbol
*, void *, asection
*, bfd
*, char **);
56 static bfd_reloc_status_type nios2_elf32_pcrel_hiadj16_relocate
57 (bfd
*, arelent
*, asymbol
*, void *, asection
*, bfd
*, char **);
58 static bfd_reloc_status_type nios2_elf32_pcrel16_relocate
59 (bfd
*, arelent
*, asymbol
*, void *, asection
*, bfd
*, char **);
60 static bfd_reloc_status_type nios2_elf32_call26_relocate
61 (bfd
*, arelent
*, asymbol
*, void *, asection
*, bfd
*, char **);
62 static bfd_reloc_status_type nios2_elf32_gprel_relocate
63 (bfd
*, arelent
*, asymbol
*, void *, asection
*, bfd
*, char **);
64 static bfd_reloc_status_type nios2_elf32_ujmp_relocate
65 (bfd
*, arelent
*, asymbol
*, void *, asection
*, bfd
*, char **);
66 static bfd_reloc_status_type nios2_elf32_cjmp_relocate
67 (bfd
*, arelent
*, asymbol
*, void *, asection
*, bfd
*, char **);
68 static bfd_reloc_status_type nios2_elf32_callr_relocate
69 (bfd
*, arelent
*, asymbol
*, void *, asection
*, bfd
*, char **);
72 extern const bfd_target nios2_elf32_le_vec
;
73 extern const bfd_target nios2_elf32_be_vec
;
75 /* Offset of tp and dtp pointers from start of TLS block. */
76 #define TP_OFFSET 0x7000
77 #define DTP_OFFSET 0x8000
79 /* The relocation tables used for SHT_REL sections. There are separate
80 tables for R1 and R2 encodings. */
81 static reloc_howto_type elf_nios2_r1_howto_table_rel
[] = {
83 HOWTO (R_NIOS2_NONE
, /* type */
85 3, /* size (0 = byte, 1 = short, 2 = long) */
87 FALSE
, /* pc_relative */
89 complain_overflow_dont
, /* complain_on_overflow */
90 bfd_elf_generic_reloc
, /* special_function */
91 "R_NIOS2_NONE", /* name */
92 FALSE
, /* partial_inplace */
95 FALSE
), /* pcrel_offset */
97 /* 16-bit signed immediate relocation. */
98 HOWTO (R_NIOS2_S16
, /* type */
100 2, /* size (0 = byte, 1 = short, 2 = long) */
102 FALSE
, /* pc_relative */
104 complain_overflow_signed
, /* complain on overflow */
105 bfd_elf_generic_reloc
, /* special function */
106 "R_NIOS2_S16", /* name */
107 FALSE
, /* partial_inplace */
108 0x003fffc0, /* src_mask */
109 0x003fffc0, /* dest_mask */
110 FALSE
), /* pcrel_offset */
112 /* 16-bit unsigned immediate relocation. */
113 HOWTO (R_NIOS2_U16
, /* type */
115 2, /* size (0 = byte, 1 = short, 2 = long) */
117 FALSE
, /* pc_relative */
119 complain_overflow_unsigned
, /* complain on overflow */
120 bfd_elf_generic_reloc
, /* special function */
121 "R_NIOS2_U16", /* name */
122 FALSE
, /* partial_inplace */
123 0x003fffc0, /* src_mask */
124 0x003fffc0, /* dest_mask */
125 FALSE
), /* pcrel_offset */
127 HOWTO (R_NIOS2_PCREL16
, /* type */
129 2, /* size (0 = byte, 1 = short, 2 = long) */
131 TRUE
, /* pc_relative */
133 complain_overflow_signed
, /* complain on overflow */
134 nios2_elf32_pcrel16_relocate
, /* special function */
135 "R_NIOS2_PCREL16", /* name */
136 FALSE
, /* partial_inplace */
137 0x003fffc0, /* src_mask */
138 0x003fffc0, /* dest_mask */
139 TRUE
), /* pcrel_offset */
141 HOWTO (R_NIOS2_CALL26
, /* type */
143 2, /* size (0 = byte, 1 = short, 2 = long) */
145 FALSE
, /* pc_relative */
147 complain_overflow_dont
, /* complain on overflow */
148 nios2_elf32_call26_relocate
, /* special function */
149 "R_NIOS2_CALL26", /* name */
150 FALSE
, /* partial_inplace */
151 0xffffffc0, /* src_mask */
152 0xffffffc0, /* dst_mask */
153 FALSE
), /* pcrel_offset */
161 complain_overflow_bitfield
,
162 bfd_elf_generic_reloc
,
169 HOWTO (R_NIOS2_CACHE_OPX
,
175 complain_overflow_bitfield
,
176 bfd_elf_generic_reloc
,
189 complain_overflow_bitfield
,
190 bfd_elf_generic_reloc
,
203 complain_overflow_bitfield
,
204 bfd_elf_generic_reloc
,
217 complain_overflow_dont
,
218 nios2_elf32_hi16_relocate
,
231 complain_overflow_dont
,
232 nios2_elf32_lo16_relocate
,
239 HOWTO (R_NIOS2_HIADJ16
,
245 complain_overflow_dont
,
246 nios2_elf32_hiadj16_relocate
,
253 HOWTO (R_NIOS2_BFD_RELOC_32
,
259 complain_overflow_dont
,
260 bfd_elf_generic_reloc
,
261 "R_NIOS2_BFD_RELOC32",
267 HOWTO (R_NIOS2_BFD_RELOC_16
,
273 complain_overflow_bitfield
,
274 bfd_elf_generic_reloc
,
275 "R_NIOS2_BFD_RELOC16",
281 HOWTO (R_NIOS2_BFD_RELOC_8
,
287 complain_overflow_bitfield
,
288 bfd_elf_generic_reloc
,
289 "R_NIOS2_BFD_RELOC8",
295 HOWTO (R_NIOS2_GPREL
,
301 complain_overflow_dont
,
302 nios2_elf32_gprel_relocate
,
309 HOWTO (R_NIOS2_GNU_VTINHERIT
,
315 complain_overflow_dont
,
317 "R_NIOS2_GNU_VTINHERIT",
323 HOWTO (R_NIOS2_GNU_VTENTRY
,
329 complain_overflow_dont
,
330 _bfd_elf_rel_vtable_reloc_fn
,
331 "R_NIOS2_GNU_VTENTRY",
343 complain_overflow_dont
,
344 nios2_elf32_ujmp_relocate
,
357 complain_overflow_dont
,
358 nios2_elf32_cjmp_relocate
,
365 HOWTO (R_NIOS2_CALLR
,
371 complain_overflow_dont
,
372 nios2_elf32_callr_relocate
,
379 HOWTO (R_NIOS2_ALIGN
,
385 complain_overflow_dont
,
386 nios2_elf32_ignore_reloc
,
394 HOWTO (R_NIOS2_GOT16
,
400 complain_overflow_bitfield
,
401 bfd_elf_generic_reloc
,
408 HOWTO (R_NIOS2_CALL16
,
414 complain_overflow_bitfield
,
415 bfd_elf_generic_reloc
,
422 HOWTO (R_NIOS2_GOTOFF_LO
,
428 complain_overflow_dont
,
429 bfd_elf_generic_reloc
,
436 HOWTO (R_NIOS2_GOTOFF_HA
,
442 complain_overflow_dont
,
443 bfd_elf_generic_reloc
,
450 HOWTO (R_NIOS2_PCREL_LO
,
456 complain_overflow_dont
,
457 nios2_elf32_pcrel_lo16_relocate
,
464 HOWTO (R_NIOS2_PCREL_HA
,
468 FALSE
, /* This is a PC-relative relocation, but we need to subtract
469 PC ourselves before the HIADJ. */
471 complain_overflow_dont
,
472 nios2_elf32_pcrel_hiadj16_relocate
,
479 HOWTO (R_NIOS2_TLS_GD16
,
485 complain_overflow_bitfield
,
486 bfd_elf_generic_reloc
,
493 HOWTO (R_NIOS2_TLS_LDM16
,
499 complain_overflow_bitfield
,
500 bfd_elf_generic_reloc
,
507 HOWTO (R_NIOS2_TLS_LDO16
,
513 complain_overflow_bitfield
,
514 bfd_elf_generic_reloc
,
521 HOWTO (R_NIOS2_TLS_IE16
,
527 complain_overflow_bitfield
,
528 bfd_elf_generic_reloc
,
535 HOWTO (R_NIOS2_TLS_LE16
,
541 complain_overflow_bitfield
,
542 bfd_elf_generic_reloc
,
549 HOWTO (R_NIOS2_TLS_DTPMOD
,
555 complain_overflow_dont
,
556 bfd_elf_generic_reloc
,
557 "R_NIOS2_TLS_DTPMOD",
563 HOWTO (R_NIOS2_TLS_DTPREL
,
569 complain_overflow_dont
,
570 bfd_elf_generic_reloc
,
571 "R_NIOS2_TLS_DTPREL",
577 HOWTO (R_NIOS2_TLS_TPREL
,
583 complain_overflow_dont
,
584 bfd_elf_generic_reloc
,
597 complain_overflow_dont
,
598 bfd_elf_generic_reloc
,
605 HOWTO (R_NIOS2_GLOB_DAT
,
611 complain_overflow_dont
,
612 bfd_elf_generic_reloc
,
619 HOWTO (R_NIOS2_JUMP_SLOT
,
625 complain_overflow_dont
,
626 bfd_elf_generic_reloc
,
633 HOWTO (R_NIOS2_RELATIVE
,
639 complain_overflow_dont
,
640 bfd_elf_generic_reloc
,
647 HOWTO (R_NIOS2_GOTOFF
,
653 complain_overflow_dont
,
654 bfd_elf_generic_reloc
,
661 HOWTO (R_NIOS2_CALL26_NOAT
, /* type */
663 2, /* size (0 = byte, 1 = short, 2 = long) */
665 FALSE
, /* pc_relative */
667 complain_overflow_dont
, /* complain on overflow */
668 nios2_elf32_call26_relocate
, /* special function */
669 "R_NIOS2_CALL26_NOAT", /* name */
670 FALSE
, /* partial_inplace */
671 0xffffffc0, /* src_mask */
672 0xffffffc0, /* dst_mask */
673 FALSE
), /* pcrel_offset */
675 HOWTO (R_NIOS2_GOT_LO
,
681 complain_overflow_dont
,
682 bfd_elf_generic_reloc
,
689 HOWTO (R_NIOS2_GOT_HA
,
695 complain_overflow_dont
,
696 bfd_elf_generic_reloc
,
703 HOWTO (R_NIOS2_CALL_LO
,
709 complain_overflow_dont
,
710 bfd_elf_generic_reloc
,
717 HOWTO (R_NIOS2_CALL_HA
,
723 complain_overflow_dont
,
724 bfd_elf_generic_reloc
,
731 /* Add other relocations here. */
734 static reloc_howto_type elf_nios2_r2_howto_table_rel
[] = {
736 HOWTO (R_NIOS2_NONE
, /* type */
738 0, /* size (0 = byte, 1 = short, 2 = long) */
740 FALSE
, /* pc_relative */
742 complain_overflow_dont
, /* complain_on_overflow */
743 bfd_elf_generic_reloc
, /* special_function */
744 "R_NIOS2_NONE", /* name */
745 FALSE
, /* partial_inplace */
748 FALSE
), /* pcrel_offset */
750 /* 16-bit signed immediate relocation. */
751 HOWTO (R_NIOS2_S16
, /* type */
753 2, /* size (0 = byte, 1 = short, 2 = long) */
755 FALSE
, /* pc_relative */
757 complain_overflow_signed
, /* complain on overflow */
758 bfd_elf_generic_reloc
, /* special function */
759 "R_NIOS2_S16", /* name */
760 FALSE
, /* partial_inplace */
761 0xffff0000, /* src_mask */
762 0xffff0000, /* dest_mask */
763 FALSE
), /* pcrel_offset */
765 /* 16-bit unsigned immediate relocation. */
766 HOWTO (R_NIOS2_U16
, /* type */
768 2, /* size (0 = byte, 1 = short, 2 = long) */
770 FALSE
, /* pc_relative */
772 complain_overflow_unsigned
, /* complain on overflow */
773 bfd_elf_generic_reloc
, /* special function */
774 "R_NIOS2_U16", /* name */
775 FALSE
, /* partial_inplace */
776 0xffff0000, /* src_mask */
777 0xffff0000, /* dest_mask */
778 FALSE
), /* pcrel_offset */
780 HOWTO (R_NIOS2_PCREL16
, /* type */
782 2, /* size (0 = byte, 1 = short, 2 = long) */
784 TRUE
, /* pc_relative */
786 complain_overflow_signed
, /* complain on overflow */
787 nios2_elf32_pcrel16_relocate
, /* special function */
788 "R_NIOS2_PCREL16", /* name */
789 FALSE
, /* partial_inplace */
790 0xffff0000, /* src_mask */
791 0xffff0000, /* dest_mask */
792 TRUE
), /* pcrel_offset */
794 HOWTO (R_NIOS2_CALL26
, /* type */
796 2, /* size (0 = byte, 1 = short, 2 = long) */
798 FALSE
, /* pc_relative */
800 complain_overflow_dont
, /* complain on overflow */
801 nios2_elf32_call26_relocate
, /* special function */
802 "R_NIOS2_CALL26", /* name */
803 FALSE
, /* partial_inplace */
804 0xffffffc0, /* src_mask */
805 0xffffffc0, /* dst_mask */
806 FALSE
), /* pcrel_offset */
814 complain_overflow_bitfield
,
815 bfd_elf_generic_reloc
,
822 HOWTO (R_NIOS2_CACHE_OPX
,
828 complain_overflow_bitfield
,
829 bfd_elf_generic_reloc
,
842 complain_overflow_bitfield
,
843 bfd_elf_generic_reloc
,
856 complain_overflow_bitfield
,
857 bfd_elf_generic_reloc
,
870 complain_overflow_dont
,
871 nios2_elf32_hi16_relocate
,
884 complain_overflow_dont
,
885 nios2_elf32_lo16_relocate
,
892 HOWTO (R_NIOS2_HIADJ16
,
898 complain_overflow_dont
,
899 nios2_elf32_hiadj16_relocate
,
906 HOWTO (R_NIOS2_BFD_RELOC_32
,
912 complain_overflow_dont
,
913 bfd_elf_generic_reloc
,
914 "R_NIOS2_BFD_RELOC32",
920 HOWTO (R_NIOS2_BFD_RELOC_16
,
926 complain_overflow_bitfield
,
927 bfd_elf_generic_reloc
,
928 "R_NIOS2_BFD_RELOC16",
934 HOWTO (R_NIOS2_BFD_RELOC_8
,
940 complain_overflow_bitfield
,
941 bfd_elf_generic_reloc
,
942 "R_NIOS2_BFD_RELOC8",
948 HOWTO (R_NIOS2_GPREL
,
954 complain_overflow_dont
,
955 nios2_elf32_gprel_relocate
,
962 HOWTO (R_NIOS2_GNU_VTINHERIT
,
968 complain_overflow_dont
,
970 "R_NIOS2_GNU_VTINHERIT",
976 HOWTO (R_NIOS2_GNU_VTENTRY
,
982 complain_overflow_dont
,
983 _bfd_elf_rel_vtable_reloc_fn
,
984 "R_NIOS2_GNU_VTENTRY",
996 complain_overflow_dont
,
997 nios2_elf32_ujmp_relocate
,
1004 HOWTO (R_NIOS2_CJMP
,
1010 complain_overflow_dont
,
1011 nios2_elf32_cjmp_relocate
,
1018 HOWTO (R_NIOS2_CALLR
,
1024 complain_overflow_dont
,
1025 nios2_elf32_callr_relocate
,
1032 HOWTO (R_NIOS2_ALIGN
,
1038 complain_overflow_dont
,
1039 nios2_elf32_ignore_reloc
,
1046 HOWTO (R_NIOS2_GOT16
,
1052 complain_overflow_bitfield
,
1053 bfd_elf_generic_reloc
,
1060 HOWTO (R_NIOS2_CALL16
,
1066 complain_overflow_bitfield
,
1067 bfd_elf_generic_reloc
,
1074 HOWTO (R_NIOS2_GOTOFF_LO
,
1080 complain_overflow_dont
,
1081 bfd_elf_generic_reloc
,
1082 "R_NIOS2_GOTOFF_LO",
1088 HOWTO (R_NIOS2_GOTOFF_HA
,
1094 complain_overflow_dont
,
1095 bfd_elf_generic_reloc
,
1096 "R_NIOS2_GOTOFF_HA",
1102 HOWTO (R_NIOS2_PCREL_LO
,
1108 complain_overflow_dont
,
1109 nios2_elf32_pcrel_lo16_relocate
,
1116 HOWTO (R_NIOS2_PCREL_HA
,
1120 FALSE
, /* This is a PC-relative relocation, but we need to subtract
1121 PC ourselves before the HIADJ. */
1123 complain_overflow_dont
,
1124 nios2_elf32_pcrel_hiadj16_relocate
,
1131 HOWTO (R_NIOS2_TLS_GD16
,
1137 complain_overflow_bitfield
,
1138 bfd_elf_generic_reloc
,
1145 HOWTO (R_NIOS2_TLS_LDM16
,
1151 complain_overflow_bitfield
,
1152 bfd_elf_generic_reloc
,
1153 "R_NIOS2_TLS_LDM16",
1159 HOWTO (R_NIOS2_TLS_LDO16
,
1165 complain_overflow_bitfield
,
1166 bfd_elf_generic_reloc
,
1167 "R_NIOS2_TLS_LDO16",
1173 HOWTO (R_NIOS2_TLS_IE16
,
1179 complain_overflow_bitfield
,
1180 bfd_elf_generic_reloc
,
1187 HOWTO (R_NIOS2_TLS_LE16
,
1193 complain_overflow_bitfield
,
1194 bfd_elf_generic_reloc
,
1201 HOWTO (R_NIOS2_TLS_DTPMOD
,
1207 complain_overflow_dont
,
1208 bfd_elf_generic_reloc
,
1209 "R_NIOS2_TLS_DTPMOD",
1215 HOWTO (R_NIOS2_TLS_DTPREL
,
1221 complain_overflow_dont
,
1222 bfd_elf_generic_reloc
,
1223 "R_NIOS2_TLS_DTPREL",
1229 HOWTO (R_NIOS2_TLS_TPREL
,
1235 complain_overflow_dont
,
1236 bfd_elf_generic_reloc
,
1237 "R_NIOS2_TLS_TPREL",
1243 HOWTO (R_NIOS2_COPY
,
1249 complain_overflow_dont
,
1250 bfd_elf_generic_reloc
,
1257 HOWTO (R_NIOS2_GLOB_DAT
,
1263 complain_overflow_dont
,
1264 bfd_elf_generic_reloc
,
1271 HOWTO (R_NIOS2_JUMP_SLOT
,
1277 complain_overflow_dont
,
1278 bfd_elf_generic_reloc
,
1279 "R_NIOS2_JUMP_SLOT",
1285 HOWTO (R_NIOS2_RELATIVE
,
1291 complain_overflow_dont
,
1292 bfd_elf_generic_reloc
,
1299 HOWTO (R_NIOS2_GOTOFF
,
1305 complain_overflow_dont
,
1306 bfd_elf_generic_reloc
,
1313 HOWTO (R_NIOS2_CALL26_NOAT
, /* type */
1315 2, /* size (0 = byte, 1 = short, 2 = long) */
1317 FALSE
, /* pc_relative */
1319 complain_overflow_dont
, /* complain on overflow */
1320 nios2_elf32_call26_relocate
, /* special function */
1321 "R_NIOS2_CALL26_NOAT", /* name */
1322 FALSE
, /* partial_inplace */
1323 0xffffffc0, /* src_mask */
1324 0xffffffc0, /* dst_mask */
1325 FALSE
), /* pcrel_offset */
1327 HOWTO (R_NIOS2_GOT_LO
,
1333 complain_overflow_dont
,
1334 bfd_elf_generic_reloc
,
1341 HOWTO (R_NIOS2_GOT_HA
,
1347 complain_overflow_dont
,
1348 bfd_elf_generic_reloc
,
1355 HOWTO (R_NIOS2_CALL_LO
,
1361 complain_overflow_dont
,
1362 bfd_elf_generic_reloc
,
1369 HOWTO (R_NIOS2_CALL_HA
,
1375 complain_overflow_dont
,
1376 bfd_elf_generic_reloc
,
1383 HOWTO (R_NIOS2_R2_S12
,
1389 complain_overflow_signed
,
1390 bfd_elf_generic_reloc
,
1397 HOWTO (R_NIOS2_R2_I10_1_PCREL
,
1403 complain_overflow_signed
,
1404 bfd_elf_generic_reloc
, /* FIXME? */
1405 "R_NIOS2_R2_I10_1_PCREL",
1411 HOWTO (R_NIOS2_R2_T1I7_1_PCREL
,
1417 complain_overflow_signed
,
1418 bfd_elf_generic_reloc
, /* FIXME? */
1419 "R_NIOS2_R2_T1I7_1_PCREL",
1425 HOWTO (R_NIOS2_R2_T1I7_2
,
1431 complain_overflow_unsigned
,
1432 bfd_elf_generic_reloc
,
1433 "R_NIOS2_R2_T1I7_2",
1439 HOWTO (R_NIOS2_R2_T2I4
,
1445 complain_overflow_unsigned
,
1446 bfd_elf_generic_reloc
,
1453 HOWTO (R_NIOS2_R2_T2I4_1
,
1459 complain_overflow_unsigned
,
1460 bfd_elf_generic_reloc
,
1461 "R_NIOS2_R2_T2I4_1",
1467 HOWTO (R_NIOS2_R2_T2I4_2
,
1473 complain_overflow_unsigned
,
1474 bfd_elf_generic_reloc
,
1475 "R_NIOS2_R2_T2I4_2",
1481 HOWTO (R_NIOS2_R2_X1I7_2
,
1487 complain_overflow_unsigned
,
1488 bfd_elf_generic_reloc
,
1489 "R_NIOS2_R2_X1I7_2",
1495 HOWTO (R_NIOS2_R2_X2L5
,
1501 complain_overflow_unsigned
,
1502 bfd_elf_generic_reloc
,
1509 HOWTO (R_NIOS2_R2_F1I5_2
,
1515 complain_overflow_unsigned
,
1516 bfd_elf_generic_reloc
,
1517 "R_NIOS2_R2_F1L5_2",
1523 HOWTO (R_NIOS2_R2_L5I4X1
,
1529 complain_overflow_unsigned
,
1530 bfd_elf_generic_reloc
,
1531 "R_NIOS2_R2_L5I4X1",
1537 HOWTO (R_NIOS2_R2_T1X1I6
,
1543 complain_overflow_unsigned
,
1544 bfd_elf_generic_reloc
,
1545 "R_NIOS2_R2_T1X1I6",
1551 HOWTO (R_NIOS2_R2_T1X1I6_2
,
1557 complain_overflow_unsigned
,
1558 bfd_elf_generic_reloc
,
1559 "R_NIOS2_R2_T1I1X6_2",
1565 /* Add other relocations here. */
1568 static unsigned char elf_code_to_howto_index
[R_NIOS2_ILLEGAL
+ 1];
1571 /* Return true if producing output for a R2 BFD. */
1572 #define BFD_IS_R2(abfd) (bfd_get_mach (abfd) == bfd_mach_nios2r2)
1574 /* Return the howto for relocation RTYPE. */
1575 static reloc_howto_type
*
1576 lookup_howto (unsigned int rtype
, bfd
*abfd
)
1578 static int initialized
= 0;
1580 /* R2 relocations are a superset of R1, so use that for the lookup
1582 int r1_howto_tbl_size
= (int) ARRAY_SIZE (elf_nios2_r1_howto_table_rel
);
1583 int r2_howto_tbl_size
= (int) ARRAY_SIZE (elf_nios2_r2_howto_table_rel
);
1588 memset (elf_code_to_howto_index
, 0xff,
1589 sizeof (elf_code_to_howto_index
));
1590 for (i
= 0; i
< r2_howto_tbl_size
; i
++)
1592 elf_code_to_howto_index
[elf_nios2_r2_howto_table_rel
[i
].type
] = i
;
1593 if (i
< r1_howto_tbl_size
)
1594 BFD_ASSERT (elf_nios2_r2_howto_table_rel
[i
].type
1595 == elf_nios2_r1_howto_table_rel
[i
].type
);
1599 if (rtype
> R_NIOS2_ILLEGAL
)
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
[] =
1625 {BFD_RELOC_NONE
, R_NIOS2_NONE
},
1626 {BFD_RELOC_NIOS2_S16
, R_NIOS2_S16
},
1627 {BFD_RELOC_NIOS2_U16
, R_NIOS2_U16
},
1628 {BFD_RELOC_16_PCREL
, R_NIOS2_PCREL16
},
1629 {BFD_RELOC_NIOS2_CALL26
, R_NIOS2_CALL26
},
1630 {BFD_RELOC_NIOS2_IMM5
, R_NIOS2_IMM5
},
1631 {BFD_RELOC_NIOS2_CACHE_OPX
, R_NIOS2_CACHE_OPX
},
1632 {BFD_RELOC_NIOS2_IMM6
, R_NIOS2_IMM6
},
1633 {BFD_RELOC_NIOS2_IMM8
, R_NIOS2_IMM8
},
1634 {BFD_RELOC_NIOS2_HI16
, R_NIOS2_HI16
},
1635 {BFD_RELOC_NIOS2_LO16
, R_NIOS2_LO16
},
1636 {BFD_RELOC_NIOS2_HIADJ16
, R_NIOS2_HIADJ16
},
1637 {BFD_RELOC_32
, R_NIOS2_BFD_RELOC_32
},
1638 {BFD_RELOC_16
, R_NIOS2_BFD_RELOC_16
},
1639 {BFD_RELOC_8
, R_NIOS2_BFD_RELOC_8
},
1640 {BFD_RELOC_NIOS2_GPREL
, R_NIOS2_GPREL
},
1641 {BFD_RELOC_VTABLE_INHERIT
, R_NIOS2_GNU_VTINHERIT
},
1642 {BFD_RELOC_VTABLE_ENTRY
, R_NIOS2_GNU_VTENTRY
},
1643 {BFD_RELOC_NIOS2_UJMP
, R_NIOS2_UJMP
},
1644 {BFD_RELOC_NIOS2_CJMP
, R_NIOS2_CJMP
},
1645 {BFD_RELOC_NIOS2_CALLR
, R_NIOS2_CALLR
},
1646 {BFD_RELOC_NIOS2_ALIGN
, R_NIOS2_ALIGN
},
1647 {BFD_RELOC_NIOS2_GOT16
, R_NIOS2_GOT16
},
1648 {BFD_RELOC_NIOS2_CALL16
, R_NIOS2_CALL16
},
1649 {BFD_RELOC_NIOS2_GOTOFF_LO
, R_NIOS2_GOTOFF_LO
},
1650 {BFD_RELOC_NIOS2_GOTOFF_HA
, R_NIOS2_GOTOFF_HA
},
1651 {BFD_RELOC_NIOS2_PCREL_LO
, R_NIOS2_PCREL_LO
},
1652 {BFD_RELOC_NIOS2_PCREL_HA
, R_NIOS2_PCREL_HA
},
1653 {BFD_RELOC_NIOS2_TLS_GD16
, R_NIOS2_TLS_GD16
},
1654 {BFD_RELOC_NIOS2_TLS_LDM16
, R_NIOS2_TLS_LDM16
},
1655 {BFD_RELOC_NIOS2_TLS_LDO16
, R_NIOS2_TLS_LDO16
},
1656 {BFD_RELOC_NIOS2_TLS_IE16
, R_NIOS2_TLS_IE16
},
1657 {BFD_RELOC_NIOS2_TLS_LE16
, R_NIOS2_TLS_LE16
},
1658 {BFD_RELOC_NIOS2_TLS_DTPMOD
, R_NIOS2_TLS_DTPMOD
},
1659 {BFD_RELOC_NIOS2_TLS_DTPREL
, R_NIOS2_TLS_DTPREL
},
1660 {BFD_RELOC_NIOS2_TLS_TPREL
, R_NIOS2_TLS_TPREL
},
1661 {BFD_RELOC_NIOS2_COPY
, R_NIOS2_COPY
},
1662 {BFD_RELOC_NIOS2_GLOB_DAT
, R_NIOS2_GLOB_DAT
},
1663 {BFD_RELOC_NIOS2_JUMP_SLOT
, R_NIOS2_JUMP_SLOT
},
1664 {BFD_RELOC_NIOS2_RELATIVE
, R_NIOS2_RELATIVE
},
1665 {BFD_RELOC_NIOS2_GOTOFF
, R_NIOS2_GOTOFF
},
1666 {BFD_RELOC_NIOS2_CALL26_NOAT
, R_NIOS2_CALL26_NOAT
},
1667 {BFD_RELOC_NIOS2_GOT_LO
, R_NIOS2_GOT_LO
},
1668 {BFD_RELOC_NIOS2_GOT_HA
, R_NIOS2_GOT_HA
},
1669 {BFD_RELOC_NIOS2_CALL_LO
, R_NIOS2_CALL_LO
},
1670 {BFD_RELOC_NIOS2_CALL_HA
, R_NIOS2_CALL_HA
},
1671 {BFD_RELOC_NIOS2_R2_S12
, R_NIOS2_R2_S12
},
1672 {BFD_RELOC_NIOS2_R2_I10_1_PCREL
, R_NIOS2_R2_I10_1_PCREL
},
1673 {BFD_RELOC_NIOS2_R2_T1I7_1_PCREL
, R_NIOS2_R2_T1I7_1_PCREL
},
1674 {BFD_RELOC_NIOS2_R2_T1I7_2
, R_NIOS2_R2_T1I7_2
},
1675 {BFD_RELOC_NIOS2_R2_T2I4
, R_NIOS2_R2_T2I4
},
1676 {BFD_RELOC_NIOS2_R2_T2I4_1
, R_NIOS2_R2_T2I4_1
},
1677 {BFD_RELOC_NIOS2_R2_T2I4_2
, R_NIOS2_R2_T2I4_2
},
1678 {BFD_RELOC_NIOS2_R2_X1I7_2
, R_NIOS2_R2_X1I7_2
},
1679 {BFD_RELOC_NIOS2_R2_X2L5
, R_NIOS2_R2_X2L5
},
1680 {BFD_RELOC_NIOS2_R2_F1I5_2
, R_NIOS2_R2_F1I5_2
},
1681 {BFD_RELOC_NIOS2_R2_L5I4X1
, R_NIOS2_R2_L5I4X1
},
1682 {BFD_RELOC_NIOS2_R2_T1X1I6
, R_NIOS2_R2_T1X1I6
},
1683 {BFD_RELOC_NIOS2_R2_T1X1I6_2
, R_NIOS2_R2_T1X1I6_2
},
1686 enum elf32_nios2_stub_type
1688 nios2_stub_call26_before
,
1689 nios2_stub_call26_after
,
1693 struct elf32_nios2_stub_hash_entry
1695 /* Base hash table entry structure. */
1696 struct bfd_hash_entry bh_root
;
1698 /* The stub section. */
1701 /* Offset within stub_sec of the beginning of this stub. */
1702 bfd_vma stub_offset
;
1704 /* Given the symbol's value and its section we can determine its final
1705 value when building the stubs (so the stub knows where to jump. */
1706 bfd_vma target_value
;
1707 asection
*target_section
;
1709 enum elf32_nios2_stub_type stub_type
;
1711 /* The symbol table entry, if any, that this was derived from. */
1712 struct elf32_nios2_link_hash_entry
*hh
;
1714 /* And the reloc addend that this was derived from. */
1717 /* Where this stub is being called from, or, in the case of combined
1718 stub sections, the first input section in the group. */
1722 #define nios2_stub_hash_entry(ent) \
1723 ((struct elf32_nios2_stub_hash_entry *)(ent))
1725 #define nios2_stub_hash_lookup(table, string, create, copy) \
1726 ((struct elf32_nios2_stub_hash_entry *) \
1727 bfd_hash_lookup ((table), (string), (create), (copy)))
1730 /* Nios II ELF linker hash entry. */
1732 struct elf32_nios2_link_hash_entry
1734 struct elf_link_hash_entry root
;
1736 /* A pointer to the most recently used stub hash entry against this
1738 struct elf32_nios2_stub_hash_entry
*hsh_cache
;
1740 /* Track dynamic relocs copied for this symbol. */
1741 struct elf_dyn_relocs
*dyn_relocs
;
1743 #define GOT_UNKNOWN 0
1744 #define GOT_NORMAL 1
1745 #define GOT_TLS_GD 2
1746 #define GOT_TLS_IE 4
1747 unsigned char tls_type
;
1749 /* We need to detect and take special action for symbols which are only
1750 referenced with %call() and not with %got(). Such symbols do not need
1751 a dynamic GOT reloc in shared objects, only a dynamic PLT reloc. Lazy
1752 linking will not work if the dynamic GOT reloc exists.
1753 To check for this condition efficiently, we compare got_types_used against
1755 (got_types_used & (GOT_USED | CALL_USED)) == CALL_USED.
1759 unsigned char got_types_used
;
1762 #define elf32_nios2_hash_entry(ent) \
1763 ((struct elf32_nios2_link_hash_entry *) (ent))
1765 /* Get the Nios II elf linker hash table from a link_info structure. */
1766 #define elf32_nios2_hash_table(info) \
1767 ((struct elf32_nios2_link_hash_table *) ((info)->hash))
1769 /* Nios II ELF linker hash table. */
1770 struct elf32_nios2_link_hash_table
1772 /* The main hash table. */
1773 struct elf_link_hash_table root
;
1775 /* The stub hash table. */
1776 struct bfd_hash_table bstab
;
1778 /* Linker stub bfd. */
1781 /* Linker call-backs. */
1782 asection
* (*add_stub_section
) (const char *, asection
*, bfd_boolean
);
1783 void (*layout_sections_again
) (void);
1785 /* Array to keep track of which stub sections have been created, and
1786 information on stub grouping. */
1789 /* These are the section to which stubs in the group will be
1791 asection
*first_sec
, *last_sec
;
1792 /* The stub sections. There might be stubs inserted either before
1793 or after the real section.*/
1794 asection
*first_stub_sec
, *last_stub_sec
;
1797 /* Assorted information used by nios2_elf32_size_stubs. */
1798 unsigned int bfd_count
;
1799 unsigned int top_index
;
1800 asection
**input_list
;
1801 Elf_Internal_Sym
**all_local_syms
;
1803 /* Short-cuts to get to dynamic linker sections. */
1806 /* GOT pointer symbol _gp_got. */
1807 struct elf_link_hash_entry
*h_gp_got
;
1810 bfd_signed_vma refcount
;
1814 /* Small local sym cache. */
1815 struct sym_cache sym_cache
;
1820 struct nios2_elf32_obj_tdata
1822 struct elf_obj_tdata root
;
1824 /* tls_type for each local got entry. */
1825 char *local_got_tls_type
;
1827 /* TRUE if TLS GD relocs have been seen for this object. */
1828 bfd_boolean has_tlsgd
;
1831 #define elf32_nios2_tdata(abfd) \
1832 ((struct nios2_elf32_obj_tdata *) (abfd)->tdata.any)
1834 #define elf32_nios2_local_got_tls_type(abfd) \
1835 (elf32_nios2_tdata (abfd)->local_got_tls_type)
1837 /* The name of the dynamic interpreter. This is put in the .interp
1839 #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
1841 /* PLT implementation for position-dependent code. */
1842 static const bfd_vma nios2_plt_entry
[] = { /* .PLTn: */
1843 0x03c00034, /* movhi r15, %hiadj(plt_got_slot_address) */
1844 0x7bc00017, /* ldw r15, %lo(plt_got_slot_address)(r15) */
1845 0x7800683a /* jmp r15 */
1848 static const bfd_vma nios2_plt0_entry
[] = { /* .PLTresolve */
1849 0x03800034, /* movhi r14, %hiadj(res_0) */
1850 0x73800004, /* addi r14, r14, %lo(res_0) */
1851 0x7b9fc83a, /* sub r15, r15, r14 */
1852 0x03400034, /* movhi r13, %hiadj(_GLOBAL_OFFSET_TABLE_) */
1853 0x6b800017, /* ldw r14, %lo(_GLOBAL_OFFSET_TABLE_+4)(r13) */
1854 0x6b400017, /* ldw r13, %lo(_GLOBAL_OFFSET_TABLE_+8)(r13) */
1855 0x6800683a /* jmp r13 */
1858 /* PLT implementation for position-independent code. */
1859 static const bfd_vma nios2_so_plt_entry
[] = { /* .PLTn */
1860 0x03c00034, /* movhi r15, %hiadj(index * 4) */
1861 0x7bc00004, /* addi r15, r15, %lo(index * 4) */
1862 0x00000006 /* br .PLTresolve */
1865 static const bfd_vma nios2_so_plt0_entry
[] = { /* .PLTresolve */
1866 0x001ce03a, /* nextpc r14 */
1867 0x03400034, /* movhi r13, %hiadj(_GLOBAL_OFFSET_TABLE_) */
1868 0x6b9b883a, /* add r13, r13, r14 */
1869 0x6b800017, /* ldw r14, %lo(_GLOBAL_OFFSET_TABLE_+4)(r13) */
1870 0x6b400017, /* ldw r13, %lo(_GLOBAL_OFFSET_TABLE_+8)(r13) */
1871 0x6800683a /* jmp r13 */
1875 static const bfd_vma nios2_call26_stub_entry
[] = {
1876 0x00400034, /* orhi at, r0, %hiadj(dest) */
1877 0x08400004, /* addi at, at, %lo(dest) */
1878 0x0800683a /* jmp at */
1881 /* Install 16-bit immediate value VALUE at offset OFFSET into section SEC. */
1883 nios2_elf32_install_imm16 (asection
*sec
, bfd_vma offset
, bfd_vma value
)
1885 bfd_vma word
= bfd_get_32 (sec
->owner
, sec
->contents
+ offset
);
1887 BFD_ASSERT (value
<= 0xffff || ((bfd_signed_vma
) value
) >= -0xffff);
1889 bfd_put_32 (sec
->owner
, word
| ((value
& 0xffff) << 6),
1890 sec
->contents
+ offset
);
1893 /* Install COUNT 32-bit values DATA starting at offset OFFSET into
1896 nios2_elf32_install_data (asection
*sec
, const bfd_vma
*data
, bfd_vma offset
,
1901 bfd_put_32 (sec
->owner
, *data
, sec
->contents
+ offset
);
1907 /* The usual way of loading a 32-bit constant into a Nios II register is to
1908 load the high 16 bits in one instruction and then add the low 16 bits with
1909 a signed add. This means that the high halfword needs to be adjusted to
1910 compensate for the sign bit of the low halfword. This function returns the
1911 adjusted high halfword for a given 32-bit constant. */
1913 bfd_vma
hiadj (bfd_vma symbol_value
)
1915 return ((symbol_value
+ 0x8000) >> 16) & 0xffff;
1918 /* Implement elf_backend_grok_prstatus:
1919 Support for core dump NOTE sections. */
1921 nios2_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
1926 switch (note
->descsz
)
1931 case 212: /* Linux/Nios II */
1933 elf_tdata (abfd
)->core
->signal
= bfd_get_16 (abfd
, note
->descdata
+ 12);
1936 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, note
->descdata
+ 24);
1945 /* Make a ".reg/999" section. */
1946 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
1947 size
, note
->descpos
+ offset
);
1950 /* Implement elf_backend_grok_psinfo. */
1952 nios2_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
1954 switch (note
->descsz
)
1959 case 124: /* Linux/Nios II elf_prpsinfo */
1960 elf_tdata (abfd
)->core
->program
1961 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 28, 16);
1962 elf_tdata (abfd
)->core
->command
1963 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 44, 80);
1966 /* Note that for some reason, a spurious space is tacked
1967 onto the end of the args in some (at least one anyway)
1968 implementations, so strip it off if it exists. */
1971 char *command
= elf_tdata (abfd
)->core
->command
;
1972 int n
= strlen (command
);
1974 if (0 < n
&& command
[n
- 1] == ' ')
1975 command
[n
- 1] = '\0';
1981 /* Assorted hash table functions. */
1983 /* Initialize an entry in the stub hash table. */
1984 static struct bfd_hash_entry
*
1985 stub_hash_newfunc (struct bfd_hash_entry
*entry
,
1986 struct bfd_hash_table
*table
,
1989 /* Allocate the structure if it has not already been allocated by a
1993 entry
= bfd_hash_allocate (table
,
1994 sizeof (struct elf32_nios2_stub_hash_entry
));
1999 /* Call the allocation method of the superclass. */
2000 entry
= bfd_hash_newfunc (entry
, table
, string
);
2003 struct elf32_nios2_stub_hash_entry
*hsh
;
2005 /* Initialize the local fields. */
2006 hsh
= (struct elf32_nios2_stub_hash_entry
*) entry
;
2007 hsh
->stub_sec
= NULL
;
2008 hsh
->stub_offset
= 0;
2009 hsh
->target_value
= 0;
2010 hsh
->target_section
= NULL
;
2011 hsh
->stub_type
= nios2_stub_none
;
2019 /* Create an entry in a Nios II ELF linker hash table. */
2020 static struct bfd_hash_entry
*
2021 link_hash_newfunc (struct bfd_hash_entry
*entry
,
2022 struct bfd_hash_table
*table
, const char *string
)
2024 /* Allocate the structure if it has not already been allocated by a
2028 entry
= bfd_hash_allocate (table
,
2029 sizeof (struct elf32_nios2_link_hash_entry
));
2034 /* Call the allocation method of the superclass. */
2035 entry
= _bfd_elf_link_hash_newfunc (entry
, table
, string
);
2038 struct elf32_nios2_link_hash_entry
*eh
;
2040 eh
= (struct elf32_nios2_link_hash_entry
*) entry
;
2041 eh
->hsh_cache
= NULL
;
2042 eh
->dyn_relocs
= NULL
;
2043 eh
->tls_type
= GOT_UNKNOWN
;
2044 eh
->got_types_used
= 0;
2050 /* Section name for stubs is the associated section name plus this
2052 #define STUB_SUFFIX ".stub"
2054 /* Build a name for an entry in the stub hash table. */
2056 nios2_stub_name (const asection
*input_section
,
2057 const asection
*sym_sec
,
2058 const struct elf32_nios2_link_hash_entry
*hh
,
2059 const Elf_Internal_Rela
*rel
,
2060 enum elf32_nios2_stub_type stub_type
)
2064 char stubpos
= (stub_type
== nios2_stub_call26_before
) ? 'b' : 'a';
2068 len
= 8 + 1 + 1 + 1+ strlen (hh
->root
.root
.root
.string
) + 1 + 8 + 1;
2069 stub_name
= bfd_malloc (len
);
2070 if (stub_name
!= NULL
)
2072 sprintf (stub_name
, "%08x_%c_%s+%x",
2073 input_section
->id
& 0xffffffff,
2075 hh
->root
.root
.root
.string
,
2076 (int) rel
->r_addend
& 0xffffffff);
2081 len
= 8 + 1 + 1 + 1+ 8 + 1 + 8 + 1 + 8 + 1;
2082 stub_name
= bfd_malloc (len
);
2083 if (stub_name
!= NULL
)
2085 sprintf (stub_name
, "%08x_%c_%x:%x+%x",
2086 input_section
->id
& 0xffffffff,
2088 sym_sec
->id
& 0xffffffff,
2089 (int) ELF32_R_SYM (rel
->r_info
) & 0xffffffff,
2090 (int) rel
->r_addend
& 0xffffffff);
2096 /* Look up an entry in the stub hash. Stub entries are cached because
2097 creating the stub name takes a bit of time. */
2098 static struct elf32_nios2_stub_hash_entry
*
2099 nios2_get_stub_entry (const asection
*input_section
,
2100 const asection
*sym_sec
,
2101 struct elf32_nios2_link_hash_entry
*hh
,
2102 const Elf_Internal_Rela
*rel
,
2103 struct elf32_nios2_link_hash_table
*htab
,
2104 enum elf32_nios2_stub_type stub_type
)
2106 struct elf32_nios2_stub_hash_entry
*hsh
;
2107 const asection
*id_sec
;
2109 /* If this input section is part of a group of sections sharing one
2110 stub section, then use the id of the first/last section in the group,
2111 depending on the stub section placement relative to the group.
2112 Stub names need to include a section id, as there may well be
2113 more than one stub used to reach say, printf, and we need to
2114 distinguish between them. */
2115 if (stub_type
== nios2_stub_call26_before
)
2116 id_sec
= htab
->stub_group
[input_section
->id
].first_sec
;
2118 id_sec
= htab
->stub_group
[input_section
->id
].last_sec
;
2120 if (hh
!= NULL
&& hh
->hsh_cache
!= NULL
2121 && hh
->hsh_cache
->hh
== hh
2122 && hh
->hsh_cache
->id_sec
== id_sec
2123 && hh
->hsh_cache
->stub_type
== stub_type
)
2125 hsh
= hh
->hsh_cache
;
2131 stub_name
= nios2_stub_name (id_sec
, sym_sec
, hh
, rel
, stub_type
);
2132 if (stub_name
== NULL
)
2135 hsh
= nios2_stub_hash_lookup (&htab
->bstab
,
2136 stub_name
, FALSE
, FALSE
);
2139 hh
->hsh_cache
= hsh
;
2147 /* Add a new stub entry to the stub hash. Not all fields of the new
2148 stub entry are initialised. */
2149 static struct elf32_nios2_stub_hash_entry
*
2150 nios2_add_stub (const char *stub_name
,
2152 struct elf32_nios2_link_hash_table
*htab
,
2153 enum elf32_nios2_stub_type stub_type
)
2157 asection
**secptr
, **linkptr
;
2158 struct elf32_nios2_stub_hash_entry
*hsh
;
2161 if (stub_type
== nios2_stub_call26_before
)
2163 link_sec
= htab
->stub_group
[section
->id
].first_sec
;
2164 secptr
= &(htab
->stub_group
[section
->id
].first_stub_sec
);
2165 linkptr
= &(htab
->stub_group
[link_sec
->id
].first_stub_sec
);
2170 link_sec
= htab
->stub_group
[section
->id
].last_sec
;
2171 secptr
= &(htab
->stub_group
[section
->id
].last_stub_sec
);
2172 linkptr
= &(htab
->stub_group
[link_sec
->id
].last_stub_sec
);
2176 if (stub_sec
== NULL
)
2178 stub_sec
= *linkptr
;
2179 if (stub_sec
== NULL
)
2185 namelen
= strlen (link_sec
->name
);
2186 len
= namelen
+ sizeof (STUB_SUFFIX
);
2187 s_name
= bfd_alloc (htab
->stub_bfd
, len
);
2191 memcpy (s_name
, link_sec
->name
, namelen
);
2192 memcpy (s_name
+ namelen
, STUB_SUFFIX
, sizeof (STUB_SUFFIX
));
2194 stub_sec
= (*htab
->add_stub_section
) (s_name
, link_sec
, afterp
);
2195 if (stub_sec
== NULL
)
2197 *linkptr
= stub_sec
;
2202 /* Enter this entry into the linker stub hash table. */
2203 hsh
= nios2_stub_hash_lookup (&htab
->bstab
, stub_name
,
2207 /* xgettext:c-format */
2208 _bfd_error_handler (_("%pB: cannot create stub entry %s"),
2214 hsh
->stub_sec
= stub_sec
;
2215 hsh
->stub_offset
= 0;
2216 hsh
->id_sec
= link_sec
;
2220 /* Set up various things so that we can make a list of input sections
2221 for each output section included in the link. Returns -1 on error,
2222 0 when no stubs will be needed, and 1 on success. */
2224 nios2_elf32_setup_section_lists (bfd
*output_bfd
, struct bfd_link_info
*info
)
2227 unsigned int bfd_count
;
2228 unsigned int top_id
, top_index
;
2230 asection
**input_list
, **list
;
2232 struct elf32_nios2_link_hash_table
*htab
= elf32_nios2_hash_table (info
);
2234 /* Count the number of input BFDs and find the top input section id. */
2235 for (input_bfd
= info
->input_bfds
, bfd_count
= 0, top_id
= 0;
2237 input_bfd
= input_bfd
->link
.next
)
2240 for (section
= input_bfd
->sections
;
2242 section
= section
->next
)
2244 if (top_id
< section
->id
)
2245 top_id
= section
->id
;
2249 htab
->bfd_count
= bfd_count
;
2251 amt
= sizeof (struct map_stub
) * (top_id
+ 1);
2252 htab
->stub_group
= bfd_zmalloc (amt
);
2253 if (htab
->stub_group
== NULL
)
2256 /* We can't use output_bfd->section_count here to find the top output
2257 section index as some sections may have been removed, and
2258 strip_excluded_output_sections doesn't renumber the indices. */
2259 for (section
= output_bfd
->sections
, top_index
= 0;
2261 section
= section
->next
)
2263 if (top_index
< section
->index
)
2264 top_index
= section
->index
;
2267 htab
->top_index
= top_index
;
2268 amt
= sizeof (asection
*) * (top_index
+ 1);
2269 input_list
= bfd_malloc (amt
);
2270 htab
->input_list
= input_list
;
2271 if (input_list
== NULL
)
2274 /* For sections we aren't interested in, mark their entries with a
2275 value we can check later. */
2276 list
= input_list
+ top_index
;
2278 *list
= bfd_abs_section_ptr
;
2279 while (list
-- != input_list
);
2281 for (section
= output_bfd
->sections
;
2283 section
= section
->next
)
2285 /* FIXME: This is a bit of hack. Currently our .ctors and .dtors
2286 * have PC relative relocs in them but no code flag set. */
2287 if (((section
->flags
& SEC_CODE
) != 0) ||
2288 strcmp(".ctors", section
->name
) ||
2289 strcmp(".dtors", section
->name
))
2290 input_list
[section
->index
] = NULL
;
2296 /* The linker repeatedly calls this function for each input section,
2297 in the order that input sections are linked into output sections.
2298 Build lists of input sections to determine groupings between which
2299 we may insert linker stubs. */
2301 nios2_elf32_next_input_section (struct bfd_link_info
*info
, asection
*isec
)
2303 struct elf32_nios2_link_hash_table
*htab
= elf32_nios2_hash_table (info
);
2305 if (isec
->output_section
->index
<= htab
->top_index
)
2307 asection
**list
= htab
->input_list
+ isec
->output_section
->index
;
2308 if (*list
!= bfd_abs_section_ptr
)
2310 /* Steal the last_sec pointer for our list.
2311 This happens to make the list in reverse order,
2312 which is what we want. */
2313 htab
->stub_group
[isec
->id
].last_sec
= *list
;
2319 /* Segment mask for CALL26 relocation relaxation. */
2320 #define CALL26_SEGMENT(x) ((x) & 0xf0000000)
2322 /* Fudge factor for approximate maximum size of all stubs that might
2323 be inserted by the linker. This does not actually limit the number
2324 of stubs that might be inserted, and only affects strategy for grouping
2325 and placement of stubs. Perhaps this should be computed based on number
2326 of relocations seen, or be specifiable on the command line. */
2327 #define MAX_STUB_SECTION_SIZE 0xffff
2329 /* See whether we can group stub sections together. Grouping stub
2330 sections may result in fewer stubs. More importantly, we need to
2331 put all .init* and .fini* stubs at the end of the .init or
2332 .fini output sections respectively, because glibc splits the
2333 _init and _fini functions into multiple parts. Putting a stub in
2334 the middle of a function is not a good idea.
2335 Rather than computing groups of a maximum fixed size, for Nios II
2336 CALL26 relaxation it makes more sense to compute the groups based on
2337 sections that fit within a 256MB address segment. Also do not allow
2338 a group to span more than one output section, since different output
2339 sections might correspond to different memory banks on a bare-metal
2342 group_sections (struct elf32_nios2_link_hash_table
*htab
)
2344 asection
**list
= htab
->input_list
+ htab
->top_index
;
2347 /* The list is in reverse order so we'll search backwards looking
2348 for the first section that begins in the same memory segment,
2349 marking sections along the way to point at the tail for this
2351 asection
*tail
= *list
;
2352 if (tail
== bfd_abs_section_ptr
)
2354 while (tail
!= NULL
)
2356 bfd_vma start
= tail
->output_section
->vma
+ tail
->output_offset
;
2357 bfd_vma end
= start
+ tail
->size
;
2358 bfd_vma segment
= CALL26_SEGMENT (end
);
2361 if (segment
!= CALL26_SEGMENT (start
)
2362 || segment
!= CALL26_SEGMENT (end
+ MAX_STUB_SECTION_SIZE
))
2363 /* This section spans more than one memory segment, or is
2364 close enough to the end of the segment that adding stub
2365 sections before it might cause it to move so that it
2366 spans memory segments, or that stubs added at the end of
2367 this group might overflow into the next memory segment.
2368 Put it in a group by itself to localize the effects. */
2370 prev
= htab
->stub_group
[tail
->id
].last_sec
;
2371 htab
->stub_group
[tail
->id
].last_sec
= tail
;
2372 htab
->stub_group
[tail
->id
].first_sec
= tail
;
2375 /* Collect more sections for this group. */
2377 asection
*curr
, *first
;
2378 for (curr
= tail
; ; curr
= prev
)
2380 prev
= htab
->stub_group
[curr
->id
].last_sec
;
2382 || tail
->output_section
!= prev
->output_section
2383 || (CALL26_SEGMENT (prev
->output_section
->vma
2384 + prev
->output_offset
)
2389 for (curr
= tail
; ; curr
= prev
)
2391 prev
= htab
->stub_group
[curr
->id
].last_sec
;
2392 htab
->stub_group
[curr
->id
].last_sec
= tail
;
2393 htab
->stub_group
[curr
->id
].first_sec
= first
;
2399 /* Reset tail for the next group. */
2403 while (list
-- != htab
->input_list
);
2404 free (htab
->input_list
);
2407 /* Determine the type of stub needed, if any, for a call. */
2408 static enum elf32_nios2_stub_type
2409 nios2_type_of_stub (asection
*input_sec
,
2410 const Elf_Internal_Rela
*rel
,
2411 struct elf32_nios2_link_hash_entry
*hh
,
2412 struct elf32_nios2_link_hash_table
*htab
,
2413 bfd_vma destination
,
2414 struct bfd_link_info
*info ATTRIBUTE_UNUSED
)
2416 bfd_vma location
, segment
, start
, end
;
2417 asection
*s0
, *s1
, *s
;
2420 !(hh
->root
.root
.type
== bfd_link_hash_defined
2421 || hh
->root
.root
.type
== bfd_link_hash_defweak
))
2422 return nios2_stub_none
;
2424 /* Determine where the call point is. */
2425 location
= (input_sec
->output_section
->vma
2426 + input_sec
->output_offset
+ rel
->r_offset
);
2427 segment
= CALL26_SEGMENT (location
);
2429 /* Nios II CALL and JMPI instructions can transfer control to addresses
2430 within the same 256MB segment as the PC. */
2431 if (segment
== CALL26_SEGMENT (destination
))
2432 return nios2_stub_none
;
2434 /* Find the start and end addresses of the stub group. Also account for
2435 any already-created stub sections for this group. Note that for stubs
2436 in the end section, only the first instruction of the last stub
2437 (12 bytes long) needs to be within range. */
2438 s0
= htab
->stub_group
[input_sec
->id
].first_sec
;
2439 s
= htab
->stub_group
[s0
->id
].first_stub_sec
;
2440 if (s
!= NULL
&& s
->size
> 0)
2441 start
= s
->output_section
->vma
+ s
->output_offset
;
2443 start
= s0
->output_section
->vma
+ s0
->output_offset
;
2445 s1
= htab
->stub_group
[input_sec
->id
].last_sec
;
2446 s
= htab
->stub_group
[s1
->id
].last_stub_sec
;
2447 if (s
!= NULL
&& s
->size
> 0)
2448 end
= s
->output_section
->vma
+ s
->output_offset
+ s
->size
- 8;
2450 end
= s1
->output_section
->vma
+ s1
->output_offset
+ s1
->size
;
2452 BFD_ASSERT (start
< end
);
2453 BFD_ASSERT (start
<= location
);
2454 BFD_ASSERT (location
< end
);
2456 /* Put stubs at the end of the group unless that is not a valid
2457 location and the beginning of the group is. It might be that
2458 neither the beginning nor end works if we have an input section
2459 so large that it spans multiple segment boundaries. In that
2460 case, punt; the end result will be a relocation overflow error no
2461 matter what we do here.
2463 Note that adding stubs pushes up the addresses of all subsequent
2464 sections, so that stubs allocated on one pass through the
2465 relaxation loop may not be valid on the next pass. (E.g., we may
2466 allocate a stub at the beginning of the section on one pass and
2467 find that the call site has been bumped into the next memory
2468 segment on the next pass.) The important thing to note is that
2469 we never try to reclaim the space allocated to such unused stubs,
2470 so code size and section addresses can only increase with each
2471 iteration. Accounting for the start and end addresses of the
2472 already-created stub sections ensures that when the algorithm
2473 converges, it converges accurately, with the entire appropriate
2474 stub section accessible from the call site and not just the
2475 address at the start or end of the stub group proper. */
2477 if (segment
== CALL26_SEGMENT (end
))
2478 return nios2_stub_call26_after
;
2479 else if (segment
== CALL26_SEGMENT (start
))
2480 return nios2_stub_call26_before
;
2482 /* Perhaps this should be a dedicated error code. */
2483 return nios2_stub_none
;
2487 nios2_build_one_stub (struct bfd_hash_entry
*gen_entry
, void *in_arg ATTRIBUTE_UNUSED
)
2489 struct elf32_nios2_stub_hash_entry
*hsh
2490 = (struct elf32_nios2_stub_hash_entry
*) gen_entry
;
2491 asection
*stub_sec
= hsh
->stub_sec
;
2493 struct bfd_link_info
*info
;
2495 info
= (struct bfd_link_info
*) in_arg
;
2497 /* Fail if the target section could not be assigned to an output
2498 section. The user should fix his linker script. */
2499 if (hsh
->target_section
->output_section
== NULL
2500 && info
->non_contiguous_regions
)
2502 _bfd_error_handler (_("Could not assign '%pA' to an output section. "
2503 "Retry without --enable-non-contiguous-regions.\n"),
2504 hsh
->target_section
);
2507 /* Make a note of the offset within the stubs for this entry. */
2508 hsh
->stub_offset
= stub_sec
->size
;
2510 switch (hsh
->stub_type
)
2512 case nios2_stub_call26_before
:
2513 case nios2_stub_call26_after
:
2514 /* A call26 stub looks like:
2515 orhi at, %hiadj(dest)
2516 addi at, at, %lo(dest)
2518 Note that call/jmpi instructions can't be used in PIC code
2519 so there is no reason for the stub to be PIC, either. */
2520 sym_value
= (hsh
->target_value
2521 + hsh
->target_section
->output_offset
2522 + hsh
->target_section
->output_section
->vma
2525 nios2_elf32_install_data (stub_sec
, nios2_call26_stub_entry
,
2526 hsh
->stub_offset
, 3);
2527 nios2_elf32_install_imm16 (stub_sec
, hsh
->stub_offset
,
2529 nios2_elf32_install_imm16 (stub_sec
, hsh
->stub_offset
+ 4,
2530 (sym_value
& 0xffff));
2531 stub_sec
->size
+= 12;
2541 /* As above, but don't actually build the stub. Just bump offset so
2542 we know stub section sizes. */
2544 nios2_size_one_stub (struct bfd_hash_entry
*gen_entry
, void *in_arg ATTRIBUTE_UNUSED
)
2546 struct elf32_nios2_stub_hash_entry
*hsh
2547 = (struct elf32_nios2_stub_hash_entry
*) gen_entry
;
2549 switch (hsh
->stub_type
)
2551 case nios2_stub_call26_before
:
2552 case nios2_stub_call26_after
:
2553 hsh
->stub_sec
->size
+= 12;
2562 /* Read in all local syms for all input bfds.
2563 Returns -1 on error, 0 otherwise. */
2566 get_local_syms (bfd
*output_bfd ATTRIBUTE_UNUSED
, bfd
*input_bfd
,
2567 struct bfd_link_info
*info
)
2569 unsigned int bfd_indx
;
2570 Elf_Internal_Sym
*local_syms
, **all_local_syms
;
2571 struct elf32_nios2_link_hash_table
*htab
= elf32_nios2_hash_table (info
);
2573 /* We want to read in symbol extension records only once. To do this
2574 we need to read in the local symbols in parallel and save them for
2575 later use; so hold pointers to the local symbols in an array. */
2576 size_t amt
= sizeof (Elf_Internal_Sym
*) * htab
->bfd_count
;
2577 all_local_syms
= bfd_zmalloc (amt
);
2578 htab
->all_local_syms
= all_local_syms
;
2579 if (all_local_syms
== NULL
)
2582 /* Walk over all the input BFDs, swapping in local symbols. */
2585 input_bfd
= input_bfd
->link
.next
, bfd_indx
++)
2587 Elf_Internal_Shdr
*symtab_hdr
;
2589 /* We'll need the symbol table in a second. */
2590 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
2591 if (symtab_hdr
->sh_info
== 0)
2594 /* We need an array of the local symbols attached to the input bfd. */
2595 local_syms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
2596 if (local_syms
== NULL
)
2598 local_syms
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
,
2599 symtab_hdr
->sh_info
, 0,
2601 /* Cache them for elf_link_input_bfd. */
2602 symtab_hdr
->contents
= (unsigned char *) local_syms
;
2604 if (local_syms
== NULL
)
2607 all_local_syms
[bfd_indx
] = local_syms
;
2613 /* Determine and set the size of the stub section for a final link. */
2615 nios2_elf32_size_stubs (bfd
*output_bfd
, bfd
*stub_bfd
,
2616 struct bfd_link_info
*info
,
2617 asection
*(*add_stub_section
) (const char *,
2618 asection
*, bfd_boolean
),
2619 void (*layout_sections_again
) (void))
2621 bfd_boolean stub_changed
= FALSE
;
2622 struct elf32_nios2_link_hash_table
*htab
= elf32_nios2_hash_table (info
);
2624 /* Stash our params away. */
2625 htab
->stub_bfd
= stub_bfd
;
2626 htab
->add_stub_section
= add_stub_section
;
2627 htab
->layout_sections_again
= layout_sections_again
;
2629 /* FIXME: We only compute the section groups once. This could cause
2630 problems if adding a large stub section causes following sections,
2631 or parts of them, to move into another segment. However, this seems
2632 to be consistent with the way other back ends handle this.... */
2633 group_sections (htab
);
2635 if (get_local_syms (output_bfd
, info
->input_bfds
, info
))
2637 if (htab
->all_local_syms
)
2638 goto error_ret_free_local
;
2645 unsigned int bfd_indx
;
2648 for (input_bfd
= info
->input_bfds
, bfd_indx
= 0;
2650 input_bfd
= input_bfd
->link
.next
, bfd_indx
++)
2652 Elf_Internal_Shdr
*symtab_hdr
;
2654 Elf_Internal_Sym
*local_syms
;
2656 /* We'll need the symbol table in a second. */
2657 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
2658 if (symtab_hdr
->sh_info
== 0)
2661 local_syms
= htab
->all_local_syms
[bfd_indx
];
2663 /* Walk over each section attached to the input bfd. */
2664 for (section
= input_bfd
->sections
;
2666 section
= section
->next
)
2668 Elf_Internal_Rela
*internal_relocs
, *irelaend
, *irela
;
2670 /* If there aren't any relocs, then there's nothing more
2672 if ((section
->flags
& SEC_RELOC
) == 0
2673 || section
->reloc_count
== 0)
2676 /* If this section is a link-once section that will be
2677 discarded, then don't create any stubs. */
2678 if (section
->output_section
== NULL
2679 || section
->output_section
->owner
!= output_bfd
)
2682 /* Get the relocs. */
2684 = _bfd_elf_link_read_relocs (input_bfd
, section
, NULL
, NULL
,
2686 if (internal_relocs
== NULL
)
2687 goto error_ret_free_local
;
2689 /* Now examine each relocation. */
2690 irela
= internal_relocs
;
2691 irelaend
= irela
+ section
->reloc_count
;
2692 for (; irela
< irelaend
; irela
++)
2694 unsigned int r_type
, r_indx
;
2695 enum elf32_nios2_stub_type stub_type
;
2696 struct elf32_nios2_stub_hash_entry
*hsh
;
2699 bfd_vma destination
;
2700 struct elf32_nios2_link_hash_entry
*hh
;
2702 const asection
*id_sec
;
2704 r_type
= ELF32_R_TYPE (irela
->r_info
);
2705 r_indx
= ELF32_R_SYM (irela
->r_info
);
2707 if (r_type
>= (unsigned int) R_NIOS2_ILLEGAL
)
2709 bfd_set_error (bfd_error_bad_value
);
2710 error_ret_free_internal
:
2711 if (elf_section_data (section
)->relocs
== NULL
)
2712 free (internal_relocs
);
2713 goto error_ret_free_local
;
2716 /* Only look for stubs on CALL and JMPI instructions. */
2717 if (r_type
!= (unsigned int) R_NIOS2_CALL26
)
2720 /* Now determine the call target, its name, value,
2726 if (r_indx
< symtab_hdr
->sh_info
)
2728 /* It's a local symbol. */
2729 Elf_Internal_Sym
*sym
;
2730 Elf_Internal_Shdr
*hdr
;
2733 sym
= local_syms
+ r_indx
;
2734 if (ELF_ST_TYPE (sym
->st_info
) != STT_SECTION
)
2735 sym_value
= sym
->st_value
;
2736 shndx
= sym
->st_shndx
;
2737 if (shndx
< elf_numsections (input_bfd
))
2739 hdr
= elf_elfsections (input_bfd
)[shndx
];
2740 sym_sec
= hdr
->bfd_section
;
2741 destination
= (sym_value
+ irela
->r_addend
2742 + sym_sec
->output_offset
2743 + sym_sec
->output_section
->vma
);
2748 /* It's an external symbol. */
2751 e_indx
= r_indx
- symtab_hdr
->sh_info
;
2752 hh
= ((struct elf32_nios2_link_hash_entry
*)
2753 elf_sym_hashes (input_bfd
)[e_indx
]);
2755 while (hh
->root
.root
.type
== bfd_link_hash_indirect
2756 || hh
->root
.root
.type
== bfd_link_hash_warning
)
2757 hh
= ((struct elf32_nios2_link_hash_entry
*)
2758 hh
->root
.root
.u
.i
.link
);
2760 if (hh
->root
.root
.type
== bfd_link_hash_defined
2761 || hh
->root
.root
.type
== bfd_link_hash_defweak
)
2763 sym_sec
= hh
->root
.root
.u
.def
.section
;
2764 sym_value
= hh
->root
.root
.u
.def
.value
;
2766 if (sym_sec
->output_section
!= NULL
)
2767 destination
= (sym_value
+ irela
->r_addend
2768 + sym_sec
->output_offset
2769 + sym_sec
->output_section
->vma
);
2773 else if (hh
->root
.root
.type
== bfd_link_hash_undefweak
)
2775 if (! bfd_link_pic (info
))
2778 else if (hh
->root
.root
.type
== bfd_link_hash_undefined
)
2780 if (! (info
->unresolved_syms_in_objects
== RM_IGNORE
2781 && (ELF_ST_VISIBILITY (hh
->root
.other
)
2787 bfd_set_error (bfd_error_bad_value
);
2788 goto error_ret_free_internal
;
2792 /* Determine what (if any) linker stub is needed. */
2793 stub_type
= nios2_type_of_stub (section
, irela
, hh
, htab
,
2795 if (stub_type
== nios2_stub_none
)
2798 /* Support for grouping stub sections. */
2799 if (stub_type
== nios2_stub_call26_before
)
2800 id_sec
= htab
->stub_group
[section
->id
].first_sec
;
2802 id_sec
= htab
->stub_group
[section
->id
].last_sec
;
2804 /* Get the name of this stub. */
2805 stub_name
= nios2_stub_name (id_sec
, sym_sec
, hh
, irela
,
2808 goto error_ret_free_internal
;
2810 hsh
= nios2_stub_hash_lookup (&htab
->bstab
,
2815 /* The proper stub has already been created. */
2820 hsh
= nios2_add_stub (stub_name
, section
, htab
, stub_type
);
2824 goto error_ret_free_internal
;
2826 hsh
->target_value
= sym_value
;
2827 hsh
->target_section
= sym_sec
;
2828 hsh
->stub_type
= stub_type
;
2830 hsh
->addend
= irela
->r_addend
;
2831 stub_changed
= TRUE
;
2834 /* We're done with the internal relocs, free them. */
2835 if (elf_section_data (section
)->relocs
== NULL
)
2836 free (internal_relocs
);
2843 /* OK, we've added some stubs. Find out the new size of the
2845 for (stub_sec
= htab
->stub_bfd
->sections
;
2847 stub_sec
= stub_sec
->next
)
2850 bfd_hash_traverse (&htab
->bstab
, nios2_size_one_stub
, htab
);
2852 /* Ask the linker to do its stuff. */
2853 (*htab
->layout_sections_again
) ();
2854 stub_changed
= FALSE
;
2857 free (htab
->all_local_syms
);
2860 error_ret_free_local
:
2861 free (htab
->all_local_syms
);
2865 /* Build all the stubs associated with the current output file. The
2866 stubs are kept in a hash table attached to the main linker hash
2867 table. This function is called via nios2elf_finish in the linker. */
2869 nios2_elf32_build_stubs (struct bfd_link_info
*info
)
2872 struct bfd_hash_table
*table
;
2873 struct elf32_nios2_link_hash_table
*htab
;
2875 htab
= elf32_nios2_hash_table (info
);
2877 for (stub_sec
= htab
->stub_bfd
->sections
;
2879 stub_sec
= stub_sec
->next
)
2880 /* The stub_bfd may contain non-stub sections if it is also the
2881 dynobj. Any such non-stub sections are created with the
2882 SEC_LINKER_CREATED flag set, while stub sections do not
2883 have that flag. Ignore any non-stub sections here. */
2884 if ((stub_sec
->flags
& SEC_LINKER_CREATED
) == 0)
2888 /* Allocate memory to hold the linker stubs. */
2889 size
= stub_sec
->size
;
2890 stub_sec
->contents
= bfd_zalloc (htab
->stub_bfd
, size
);
2891 if (stub_sec
->contents
== NULL
&& size
!= 0)
2896 /* Build the stubs as directed by the stub hash table. */
2897 table
= &htab
->bstab
;
2898 bfd_hash_traverse (table
, nios2_build_one_stub
, info
);
2904 #define is_nios2_elf(bfd) \
2905 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
2906 && elf_object_id (bfd) == NIOS2_ELF_DATA)
2908 /* Merge backend specific data from an object file to the output
2909 object file when linking. */
2912 nios2_elf32_merge_private_bfd_data (bfd
*ibfd
, struct bfd_link_info
*info
)
2914 bfd
*obfd
= info
->output_bfd
;
2918 if (!is_nios2_elf (ibfd
) || !is_nios2_elf (obfd
))
2921 /* Check if we have the same endianness. */
2922 if (! _bfd_generic_verify_endian_match (ibfd
, info
))
2925 new_flags
= elf_elfheader (ibfd
)->e_flags
;
2926 old_flags
= elf_elfheader (obfd
)->e_flags
;
2927 if (!elf_flags_init (obfd
))
2929 /* First call, no flags set. */
2930 elf_flags_init (obfd
) = TRUE
;
2931 elf_elfheader (obfd
)->e_flags
= new_flags
;
2936 case EF_NIOS2_ARCH_R1
:
2937 bfd_default_set_arch_mach (obfd
, bfd_arch_nios2
, bfd_mach_nios2r1
);
2939 case EF_NIOS2_ARCH_R2
:
2940 if (bfd_big_endian (ibfd
))
2943 (_("error: %pB: big-endian R2 is not supported"), ibfd
);
2944 bfd_set_error (bfd_error_bad_value
);
2947 bfd_default_set_arch_mach (obfd
, bfd_arch_nios2
, bfd_mach_nios2r2
);
2952 /* Incompatible flags. */
2953 else if (new_flags
!= old_flags
)
2955 /* So far, the only incompatible flags denote incompatible
2958 /* xgettext:c-format */
2959 (_("error: %pB: conflicting CPU architectures %d/%d"),
2960 ibfd
, new_flags
, old_flags
);
2961 bfd_set_error (bfd_error_bad_value
);
2965 /* Merge Tag_compatibility attributes and any common GNU ones. */
2966 _bfd_elf_merge_object_attributes (ibfd
, info
);
2971 /* Implement bfd_elf32_bfd_reloc_type_lookup:
2972 Given a BFD reloc type, return a howto structure. */
2974 static reloc_howto_type
*
2975 nios2_elf32_bfd_reloc_type_lookup (bfd
*abfd
,
2976 bfd_reloc_code_real_type code
)
2980 for (i
= 0; i
< (int) ARRAY_SIZE (nios2_reloc_map
); ++i
)
2981 if (nios2_reloc_map
[i
].bfd_val
== code
)
2982 return lookup_howto (nios2_reloc_map
[i
].elf_val
, abfd
);
2986 /* Implement bfd_elf32_bfd_reloc_name_lookup:
2987 Given a reloc name, return a howto structure. */
2989 static reloc_howto_type
*
2990 nios2_elf32_bfd_reloc_name_lookup (bfd
*abfd
,
2994 reloc_howto_type
*howto_tbl
;
2997 if (BFD_IS_R2 (abfd
))
2999 howto_tbl
= elf_nios2_r2_howto_table_rel
;
3000 howto_tbl_size
= (int) ARRAY_SIZE (elf_nios2_r2_howto_table_rel
);
3004 howto_tbl
= elf_nios2_r1_howto_table_rel
;
3005 howto_tbl_size
= (int) ARRAY_SIZE (elf_nios2_r1_howto_table_rel
);
3008 for (i
= 0; i
< howto_tbl_size
; i
++)
3009 if (howto_tbl
[i
].name
&& strcasecmp (howto_tbl
[i
].name
, r_name
) == 0)
3010 return howto_tbl
+ i
;
3015 /* Implement elf_info_to_howto:
3016 Given a ELF32 relocation, fill in a arelent structure. */
3019 nios2_elf32_info_to_howto (bfd
*abfd
, arelent
*cache_ptr
,
3020 Elf_Internal_Rela
*dst
)
3022 unsigned int r_type
;
3024 r_type
= ELF32_R_TYPE (dst
->r_info
);
3025 if ((cache_ptr
->howto
= lookup_howto (r_type
, abfd
)) == NULL
)
3027 /* xgettext:c-format */
3028 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
3030 bfd_set_error (bfd_error_bad_value
);
3036 /* Return the base VMA address which should be subtracted from real addresses
3037 when resolving @dtpoff relocation.
3038 This is PT_TLS segment p_vaddr. */
3040 dtpoff_base (struct bfd_link_info
*info
)
3042 /* If tls_sec is NULL, we should have signalled an error already. */
3043 if (elf_hash_table (info
)->tls_sec
== NULL
)
3045 return elf_hash_table (info
)->tls_sec
->vma
;
3048 /* Return the relocation value for @tpoff relocation
3049 if STT_TLS virtual address is ADDRESS. */
3051 tpoff (struct bfd_link_info
*info
, bfd_vma address
)
3053 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
3055 /* If tls_sec is NULL, we should have signalled an error already. */
3056 if (htab
->tls_sec
== NULL
)
3058 return address
- htab
->tls_sec
->vma
;
3061 /* Set the GP value for OUTPUT_BFD. Returns FALSE if this is a
3062 dangerous relocation. */
3064 nios2_elf_assign_gp (bfd
*output_bfd
, bfd_vma
*pgp
, struct bfd_link_info
*info
)
3067 bfd_boolean gp_found
;
3068 struct bfd_hash_entry
*h
;
3069 struct bfd_link_hash_entry
*lh
;
3071 /* If we've already figured out what GP will be, just return it. */
3072 *pgp
= _bfd_get_gp_value (output_bfd
);
3076 h
= bfd_hash_lookup (&info
->hash
->table
, "_gp", FALSE
, FALSE
);
3077 lh
= (struct bfd_link_hash_entry
*) h
;
3083 case bfd_link_hash_undefined
:
3084 case bfd_link_hash_undefweak
:
3085 case bfd_link_hash_common
:
3088 case bfd_link_hash_defined
:
3089 case bfd_link_hash_defweak
:
3092 asection
*sym_sec
= lh
->u
.def
.section
;
3093 bfd_vma sym_value
= lh
->u
.def
.value
;
3095 if (sym_sec
->output_section
)
3096 sym_value
= (sym_value
+ sym_sec
->output_offset
3097 + sym_sec
->output_section
->vma
);
3101 case bfd_link_hash_indirect
:
3102 case bfd_link_hash_warning
:
3104 /* @@FIXME ignoring warning for now */
3106 case bfd_link_hash_new
:
3116 /* Only get the error once. */
3118 _bfd_set_gp_value (output_bfd
, *pgp
);
3122 _bfd_set_gp_value (output_bfd
, *pgp
);
3127 /* Retrieve the previously cached _gp pointer, returning bfd_reloc_dangerous
3128 if it's not available as we don't have a link_info pointer available here
3129 to look it up in the output symbol table. We don't need to adjust the
3130 symbol value for an external symbol if we are producing relocatable
3132 static bfd_reloc_status_type
3133 nios2_elf_final_gp (bfd
*output_bfd
, asymbol
*symbol
, bfd_boolean relocatable
,
3134 char **error_message
, bfd_vma
*pgp
)
3136 if (bfd_is_und_section (symbol
->section
) && !relocatable
)
3139 return bfd_reloc_undefined
;
3142 *pgp
= _bfd_get_gp_value (output_bfd
);
3143 if (*pgp
== 0 && (!relocatable
|| (symbol
->flags
& BSF_SECTION_SYM
) != 0))
3147 /* Make up a value. */
3148 *pgp
= symbol
->section
->output_section
->vma
+ 0x4000;
3149 _bfd_set_gp_value (output_bfd
, *pgp
);
3154 = (char *) _("global pointer relative relocation when _gp not defined");
3155 return bfd_reloc_dangerous
;
3159 return bfd_reloc_ok
;
3162 /* Do the relocations that require special handling. */
3163 static bfd_reloc_status_type
3164 nios2_elf32_do_hi16_relocate (bfd
*abfd
, reloc_howto_type
*howto
,
3165 asection
*input_section
,
3166 bfd_byte
*data
, bfd_vma offset
,
3167 bfd_vma symbol_value
, bfd_vma addend
)
3169 symbol_value
= symbol_value
+ addend
;
3171 symbol_value
= (symbol_value
>> 16) & 0xffff;
3172 return _bfd_final_link_relocate (howto
, abfd
, input_section
,
3173 data
, offset
, symbol_value
, addend
);
3176 static bfd_reloc_status_type
3177 nios2_elf32_do_lo16_relocate (bfd
*abfd
, reloc_howto_type
*howto
,
3178 asection
*input_section
,
3179 bfd_byte
*data
, bfd_vma offset
,
3180 bfd_vma symbol_value
, bfd_vma addend
)
3182 symbol_value
= symbol_value
+ addend
;
3184 symbol_value
= symbol_value
& 0xffff;
3185 return _bfd_final_link_relocate (howto
, abfd
, input_section
,
3186 data
, offset
, symbol_value
, addend
);
3189 static bfd_reloc_status_type
3190 nios2_elf32_do_hiadj16_relocate (bfd
*abfd
, reloc_howto_type
*howto
,
3191 asection
*input_section
,
3192 bfd_byte
*data
, bfd_vma offset
,
3193 bfd_vma symbol_value
, bfd_vma addend
)
3195 symbol_value
= symbol_value
+ addend
;
3197 symbol_value
= hiadj(symbol_value
);
3198 return _bfd_final_link_relocate (howto
, abfd
, input_section
, data
, offset
,
3199 symbol_value
, addend
);
3202 static bfd_reloc_status_type
3203 nios2_elf32_do_pcrel_lo16_relocate (bfd
*abfd
, reloc_howto_type
*howto
,
3204 asection
*input_section
,
3205 bfd_byte
*data
, bfd_vma offset
,
3206 bfd_vma symbol_value
, bfd_vma addend
)
3208 symbol_value
= symbol_value
+ addend
;
3210 symbol_value
= symbol_value
& 0xffff;
3211 return _bfd_final_link_relocate (howto
, abfd
, input_section
,
3212 data
, offset
, symbol_value
, addend
);
3215 static bfd_reloc_status_type
3216 nios2_elf32_do_pcrel_hiadj16_relocate (bfd
*abfd
, reloc_howto_type
*howto
,
3217 asection
*input_section
,
3218 bfd_byte
*data
, bfd_vma offset
,
3219 bfd_vma symbol_value
, bfd_vma addend
)
3221 symbol_value
= symbol_value
+ addend
;
3222 symbol_value
-= (input_section
->output_section
->vma
3223 + input_section
->output_offset
);
3224 symbol_value
-= offset
;
3226 symbol_value
= hiadj(symbol_value
);
3227 return _bfd_final_link_relocate (howto
, abfd
, input_section
, data
, offset
,
3228 symbol_value
, addend
);
3231 static bfd_reloc_status_type
3232 nios2_elf32_do_pcrel16_relocate (bfd
*abfd
, reloc_howto_type
*howto
,
3233 asection
*input_section
,
3234 bfd_byte
*data
, bfd_vma offset
,
3235 bfd_vma symbol_value
, bfd_vma addend
)
3237 /* NIOS2 pc relative relocations are relative to the next 32-bit instruction
3238 so we need to subtract 4 before doing a final_link_relocate. */
3239 symbol_value
= symbol_value
+ addend
- 4;
3241 return _bfd_final_link_relocate (howto
, abfd
, input_section
,
3242 data
, offset
, symbol_value
, addend
);
3245 static bfd_reloc_status_type
3246 nios2_elf32_do_call26_relocate (bfd
*abfd
, reloc_howto_type
*howto
,
3247 asection
*input_section
,
3248 bfd_byte
*data
, bfd_vma offset
,
3249 bfd_vma symbol_value
, bfd_vma addend
)
3251 /* Check that the relocation is in the same page as the current address. */
3252 if (CALL26_SEGMENT (symbol_value
+ addend
)
3253 != CALL26_SEGMENT (input_section
->output_section
->vma
3254 + input_section
->output_offset
3256 return bfd_reloc_overflow
;
3258 /* Check that the target address is correctly aligned on a 4-byte
3260 if ((symbol_value
+ addend
) & 0x3)
3261 return bfd_reloc_overflow
;
3263 return _bfd_final_link_relocate (howto
, abfd
, input_section
,
3264 data
, offset
, symbol_value
, addend
);
3267 static bfd_reloc_status_type
3268 nios2_elf32_do_gprel_relocate (bfd
*abfd
, reloc_howto_type
*howto
,
3269 asection
*input_section
,
3270 bfd_byte
*data
, bfd_vma offset
,
3271 bfd_vma symbol_value
, bfd_vma addend
)
3273 /* Because we need the output_bfd, the special handling is done
3274 in nios2_elf32_relocate_section or in nios2_elf32_gprel_relocate. */
3275 return _bfd_final_link_relocate (howto
, abfd
, input_section
,
3276 data
, offset
, symbol_value
, addend
);
3279 static bfd_reloc_status_type
3280 nios2_elf32_do_ujmp_relocate (bfd
*abfd
, reloc_howto_type
*howto
,
3281 asection
*input_section
,
3282 bfd_byte
*data
, bfd_vma offset
,
3283 bfd_vma symbol_value
, bfd_vma addend
)
3285 bfd_vma symbol_lo16
, symbol_hi16
;
3286 bfd_reloc_status_type r
;
3287 symbol_value
= symbol_value
+ addend
;
3289 symbol_hi16
= (symbol_value
>> 16) & 0xffff;
3290 symbol_lo16
= symbol_value
& 0xffff;
3292 r
= _bfd_final_link_relocate (howto
, abfd
, input_section
,
3293 data
, offset
, symbol_hi16
, addend
);
3295 if (r
== bfd_reloc_ok
)
3296 return _bfd_final_link_relocate (howto
, abfd
, input_section
,
3297 data
, offset
+ 4, symbol_lo16
, addend
);
3302 static bfd_reloc_status_type
3303 nios2_elf32_do_cjmp_relocate (bfd
*abfd
, reloc_howto_type
*howto
,
3304 asection
*input_section
,
3305 bfd_byte
*data
, bfd_vma offset
,
3306 bfd_vma symbol_value
, bfd_vma addend
)
3308 bfd_vma symbol_lo16
, symbol_hi16
;
3309 bfd_reloc_status_type r
;
3310 symbol_value
= symbol_value
+ addend
;
3312 symbol_hi16
= (symbol_value
>> 16) & 0xffff;
3313 symbol_lo16
= symbol_value
& 0xffff;
3315 r
= _bfd_final_link_relocate (howto
, abfd
, input_section
,
3316 data
, offset
, symbol_hi16
, addend
);
3318 if (r
== bfd_reloc_ok
)
3319 return _bfd_final_link_relocate (howto
, abfd
, input_section
,
3320 data
, offset
+ 4, symbol_lo16
, addend
);
3325 static bfd_reloc_status_type
3326 nios2_elf32_do_callr_relocate (bfd
*abfd
, reloc_howto_type
*howto
,
3327 asection
*input_section
,
3328 bfd_byte
*data
, bfd_vma offset
,
3329 bfd_vma symbol_value
, bfd_vma addend
)
3331 bfd_vma symbol_lo16
, symbol_hi16
;
3332 bfd_reloc_status_type r
;
3333 symbol_value
= symbol_value
+ addend
;
3335 symbol_hi16
= (symbol_value
>> 16) & 0xffff;
3336 symbol_lo16
= symbol_value
& 0xffff;
3338 r
= _bfd_final_link_relocate (howto
, abfd
, input_section
,
3339 data
, offset
, symbol_hi16
, addend
);
3341 if (r
== bfd_reloc_ok
)
3342 return _bfd_final_link_relocate (howto
, abfd
, input_section
,
3343 data
, offset
+ 4, symbol_lo16
, addend
);
3348 /* HOWTO handlers for relocations that require special handling. */
3350 /* This is for relocations used only when relaxing to ensure
3351 changes in size of section don't screw up .align. */
3352 static bfd_reloc_status_type
3353 nios2_elf32_ignore_reloc (bfd
*abfd ATTRIBUTE_UNUSED
, arelent
*reloc_entry
,
3354 asymbol
*symbol ATTRIBUTE_UNUSED
,
3355 void *data ATTRIBUTE_UNUSED
, asection
*input_section
,
3357 char **error_message ATTRIBUTE_UNUSED
)
3359 if (output_bfd
!= NULL
)
3360 reloc_entry
->address
+= input_section
->output_offset
;
3361 return bfd_reloc_ok
;
3364 static bfd_reloc_status_type
3365 nios2_elf32_hi16_relocate (bfd
*abfd
, arelent
*reloc_entry
, asymbol
*symbol
,
3366 void *data
, asection
*input_section
,
3368 char **error_message ATTRIBUTE_UNUSED
)
3370 /* This part is from bfd_elf_generic_reloc. */
3371 if (output_bfd
!= NULL
3372 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
3373 && (!reloc_entry
->howto
->partial_inplace
|| reloc_entry
->addend
== 0))
3375 reloc_entry
->address
+= input_section
->output_offset
;
3376 return bfd_reloc_ok
;
3379 if (output_bfd
!= NULL
)
3380 /* FIXME: See bfd_perform_relocation. Is this right? */
3381 return bfd_reloc_continue
;
3383 return nios2_elf32_do_hi16_relocate (abfd
, reloc_entry
->howto
,
3385 data
, reloc_entry
->address
,
3387 + symbol
->section
->output_section
->vma
3388 + symbol
->section
->output_offset
),
3389 reloc_entry
->addend
);
3392 static bfd_reloc_status_type
3393 nios2_elf32_lo16_relocate (bfd
*abfd
, arelent
*reloc_entry
, asymbol
*symbol
,
3394 void *data
, asection
*input_section
,
3396 char **error_message ATTRIBUTE_UNUSED
)
3398 /* This part is from bfd_elf_generic_reloc. */
3399 if (output_bfd
!= NULL
3400 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
3401 && (!reloc_entry
->howto
->partial_inplace
|| reloc_entry
->addend
== 0))
3403 reloc_entry
->address
+= input_section
->output_offset
;
3404 return bfd_reloc_ok
;
3407 if (output_bfd
!= NULL
)
3408 /* FIXME: See bfd_perform_relocation. Is this right? */
3409 return bfd_reloc_continue
;
3411 return nios2_elf32_do_lo16_relocate (abfd
, reloc_entry
->howto
,
3413 data
, reloc_entry
->address
,
3415 + symbol
->section
->output_section
->vma
3416 + symbol
->section
->output_offset
),
3417 reloc_entry
->addend
);
3420 static bfd_reloc_status_type
3421 nios2_elf32_hiadj16_relocate (bfd
*abfd
, arelent
*reloc_entry
, asymbol
*symbol
,
3422 void *data
, asection
*input_section
,
3424 char **error_message ATTRIBUTE_UNUSED
)
3426 /* This part is from bfd_elf_generic_reloc. */
3427 if (output_bfd
!= NULL
3428 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
3429 && (!reloc_entry
->howto
->partial_inplace
|| reloc_entry
->addend
== 0))
3431 reloc_entry
->address
+= input_section
->output_offset
;
3432 return bfd_reloc_ok
;
3435 if (output_bfd
!= NULL
)
3436 /* FIXME: See bfd_perform_relocation. Is this right? */
3437 return bfd_reloc_continue
;
3439 return nios2_elf32_do_hiadj16_relocate (abfd
, reloc_entry
->howto
,
3441 data
, reloc_entry
->address
,
3443 + symbol
->section
->output_section
->vma
3444 + symbol
->section
->output_offset
),
3445 reloc_entry
->addend
);
3448 static bfd_reloc_status_type
3449 nios2_elf32_pcrel_lo16_relocate (bfd
*abfd
, arelent
*reloc_entry
,
3450 asymbol
*symbol
, void *data
,
3451 asection
*input_section
, bfd
*output_bfd
,
3452 char **error_message ATTRIBUTE_UNUSED
)
3454 /* This part is from bfd_elf_generic_reloc. */
3455 if (output_bfd
!= NULL
3456 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
3457 && (!reloc_entry
->howto
->partial_inplace
|| reloc_entry
->addend
== 0))
3459 reloc_entry
->address
+= input_section
->output_offset
;
3460 return bfd_reloc_ok
;
3463 if (output_bfd
!= NULL
)
3464 /* FIXME: See bfd_perform_relocation. Is this right? */
3465 return bfd_reloc_continue
;
3467 return nios2_elf32_do_pcrel_lo16_relocate (
3468 abfd
, reloc_entry
->howto
, input_section
, data
, reloc_entry
->address
,
3469 (symbol
->value
+ symbol
->section
->output_section
->vma
3470 + symbol
->section
->output_offset
),
3471 reloc_entry
->addend
);
3474 static bfd_reloc_status_type
3475 nios2_elf32_pcrel_hiadj16_relocate (bfd
*abfd
, arelent
*reloc_entry
,
3476 asymbol
*symbol
, void *data
,
3477 asection
*input_section
, bfd
*output_bfd
,
3478 char **error_message ATTRIBUTE_UNUSED
)
3480 /* This part is from bfd_elf_generic_reloc. */
3481 if (output_bfd
!= NULL
3482 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
3483 && (!reloc_entry
->howto
->partial_inplace
|| reloc_entry
->addend
== 0))
3485 reloc_entry
->address
+= input_section
->output_offset
;
3486 return bfd_reloc_ok
;
3489 if (output_bfd
!= NULL
)
3490 /* FIXME: See bfd_perform_relocation. Is this right? */
3491 return bfd_reloc_continue
;
3493 return nios2_elf32_do_pcrel_hiadj16_relocate (
3494 abfd
, reloc_entry
->howto
, input_section
, data
, reloc_entry
->address
,
3495 (symbol
->value
+ symbol
->section
->output_section
->vma
3496 + symbol
->section
->output_offset
),
3497 reloc_entry
->addend
);
3500 static bfd_reloc_status_type
3501 nios2_elf32_pcrel16_relocate (bfd
*abfd
, arelent
*reloc_entry
, asymbol
*symbol
,
3502 void *data
, asection
*input_section
,
3504 char **error_message ATTRIBUTE_UNUSED
)
3506 /* This part is from bfd_elf_generic_reloc. */
3507 if (output_bfd
!= NULL
3508 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
3509 && (!reloc_entry
->howto
->partial_inplace
|| reloc_entry
->addend
== 0))
3511 reloc_entry
->address
+= input_section
->output_offset
;
3512 return bfd_reloc_ok
;
3515 if (output_bfd
!= NULL
)
3516 /* FIXME: See bfd_perform_relocation. Is this right? */
3517 return bfd_reloc_continue
;
3519 return nios2_elf32_do_pcrel16_relocate (abfd
, reloc_entry
->howto
,
3521 data
, reloc_entry
->address
,
3523 + symbol
->section
->output_section
->vma
3524 + symbol
->section
->output_offset
),
3525 reloc_entry
->addend
);
3528 static bfd_reloc_status_type
3529 nios2_elf32_call26_relocate (bfd
*abfd
, arelent
*reloc_entry
, asymbol
*symbol
,
3530 void *data
, asection
*input_section
,
3532 char **error_message ATTRIBUTE_UNUSED
)
3534 /* This part is from bfd_elf_generic_reloc. */
3535 if (output_bfd
!= NULL
3536 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
3537 && (!reloc_entry
->howto
->partial_inplace
|| reloc_entry
->addend
== 0))
3539 reloc_entry
->address
+= input_section
->output_offset
;
3540 return bfd_reloc_ok
;
3543 if (output_bfd
!= NULL
)
3544 /* FIXME: See bfd_perform_relocation. Is this right? */
3545 return bfd_reloc_continue
;
3547 return nios2_elf32_do_call26_relocate (abfd
, reloc_entry
->howto
,
3549 data
, reloc_entry
->address
,
3551 + symbol
->section
->output_section
->vma
3552 + symbol
->section
->output_offset
),
3553 reloc_entry
->addend
);
3556 static bfd_reloc_status_type
3557 nios2_elf32_gprel_relocate (bfd
*abfd
, arelent
*reloc_entry
, asymbol
*symbol
,
3558 void *data
, asection
*input_section
,
3559 bfd
*output_bfd
, char **msg
)
3563 bfd_reloc_status_type r
;
3566 /* This part is from bfd_elf_generic_reloc. */
3567 if (output_bfd
!= NULL
3568 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
3569 && (!reloc_entry
->howto
->partial_inplace
|| reloc_entry
->addend
== 0))
3571 reloc_entry
->address
+= input_section
->output_offset
;
3572 return bfd_reloc_ok
;
3575 if (output_bfd
!= NULL
)
3576 /* FIXME: See bfd_perform_relocation. Is this right? */
3577 return bfd_reloc_continue
;
3579 relocation
= (symbol
->value
3580 + symbol
->section
->output_section
->vma
3581 + symbol
->section
->output_offset
);
3583 /* This assumes we've already cached the _gp symbol. */
3584 r
= nios2_elf_final_gp (abfd
, symbol
, FALSE
, msg
, &gp
);
3585 if (r
== bfd_reloc_ok
)
3587 relocation
= relocation
+ reloc_entry
->addend
- gp
;
3588 reloc_entry
->addend
= 0;
3589 if ((signed) relocation
< -32768 || (signed) relocation
> 32767)
3591 *msg
= _("global pointer relative address out of range");
3592 r
= bfd_reloc_outofrange
;
3595 r
= nios2_elf32_do_gprel_relocate (abfd
, reloc_entry
->howto
,
3597 data
, reloc_entry
->address
,
3598 relocation
, reloc_entry
->addend
);
3604 static bfd_reloc_status_type
3605 nios2_elf32_ujmp_relocate (bfd
*abfd
, arelent
*reloc_entry
, asymbol
*symbol
,
3606 void *data
, asection
*input_section
,
3607 bfd
*output_bfd
, char **msg ATTRIBUTE_UNUSED
)
3609 /* This part is from bfd_elf_generic_reloc. */
3610 if (output_bfd
!= NULL
3611 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
3612 && (!reloc_entry
->howto
->partial_inplace
|| reloc_entry
->addend
== 0))
3614 reloc_entry
->address
+= input_section
->output_offset
;
3615 return bfd_reloc_ok
;
3618 if (output_bfd
!= NULL
)
3619 /* FIXME: See bfd_perform_relocation. Is this right? */
3620 return bfd_reloc_continue
;
3622 return nios2_elf32_do_ujmp_relocate (abfd
, reloc_entry
->howto
,
3624 data
, reloc_entry
->address
,
3626 + symbol
->section
->output_section
->vma
3627 + symbol
->section
->output_offset
),
3628 reloc_entry
->addend
);
3631 static bfd_reloc_status_type
3632 nios2_elf32_cjmp_relocate (bfd
*abfd
, arelent
*reloc_entry
, asymbol
*symbol
,
3633 void *data
, asection
*input_section
,
3634 bfd
*output_bfd
, char **msg ATTRIBUTE_UNUSED
)
3636 /* This part is from bfd_elf_generic_reloc. */
3637 if (output_bfd
!= NULL
3638 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
3639 && (!reloc_entry
->howto
->partial_inplace
|| reloc_entry
->addend
== 0))
3641 reloc_entry
->address
+= input_section
->output_offset
;
3642 return bfd_reloc_ok
;
3645 if (output_bfd
!= NULL
)
3646 /* FIXME: See bfd_perform_relocation. Is this right? */
3647 return bfd_reloc_continue
;
3649 return nios2_elf32_do_cjmp_relocate (abfd
, reloc_entry
->howto
,
3651 data
, reloc_entry
->address
,
3653 + symbol
->section
->output_section
->vma
3654 + symbol
->section
->output_offset
),
3655 reloc_entry
->addend
);
3658 static bfd_reloc_status_type
3659 nios2_elf32_callr_relocate (bfd
*abfd
, arelent
*reloc_entry
, asymbol
*symbol
,
3660 void *data
, asection
*input_section
,
3661 bfd
*output_bfd
, char **msg ATTRIBUTE_UNUSED
)
3663 /* This part is from bfd_elf_generic_reloc. */
3664 if (output_bfd
!= NULL
3665 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
3666 && (!reloc_entry
->howto
->partial_inplace
|| reloc_entry
->addend
== 0))
3668 reloc_entry
->address
+= input_section
->output_offset
;
3669 return bfd_reloc_ok
;
3672 if (output_bfd
!= NULL
)
3673 /* FIXME: See bfd_perform_relocation. Is this right? */
3674 return bfd_reloc_continue
;
3676 return nios2_elf32_do_callr_relocate (abfd
, reloc_entry
->howto
,
3678 data
, reloc_entry
->address
,
3680 + symbol
->section
->output_section
->vma
3681 + symbol
->section
->output_offset
),
3682 reloc_entry
->addend
);
3686 /* Implement elf_backend_relocate_section. */
3688 nios2_elf32_relocate_section (bfd
*output_bfd
,
3689 struct bfd_link_info
*info
,
3691 asection
*input_section
,
3693 Elf_Internal_Rela
*relocs
,
3694 Elf_Internal_Sym
*local_syms
,
3695 asection
**local_sections
)
3697 Elf_Internal_Shdr
*symtab_hdr
;
3698 struct elf_link_hash_entry
**sym_hashes
;
3699 Elf_Internal_Rela
*rel
;
3700 Elf_Internal_Rela
*relend
;
3701 struct elf32_nios2_link_hash_table
*htab
;
3704 asection
*sreloc
= NULL
;
3705 bfd_vma
*local_got_offsets
;
3708 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
3709 sym_hashes
= elf_sym_hashes (input_bfd
);
3710 relend
= relocs
+ input_section
->reloc_count
;
3712 htab
= elf32_nios2_hash_table (info
);
3713 sgot
= htab
->root
.sgot
;
3714 splt
= htab
->root
.splt
;
3715 local_got_offsets
= elf_local_got_offsets (input_bfd
);
3717 if (htab
->h_gp_got
== NULL
)
3720 got_base
= htab
->h_gp_got
->root
.u
.def
.value
;
3722 for (rel
= relocs
; rel
< relend
; rel
++)
3724 reloc_howto_type
*howto
;
3725 unsigned long r_symndx
;
3726 Elf_Internal_Sym
*sym
;
3728 struct elf_link_hash_entry
*h
;
3729 struct elf32_nios2_link_hash_entry
*eh
;
3732 bfd_reloc_status_type r
= bfd_reloc_ok
;
3733 const char *name
= NULL
;
3736 char *msgbuf
= NULL
;
3738 bfd_boolean unresolved_reloc
;
3742 r_type
= ELF32_R_TYPE (rel
->r_info
);
3743 r_symndx
= ELF32_R_SYM (rel
->r_info
);
3745 howto
= lookup_howto ((unsigned) ELF32_R_TYPE (rel
->r_info
), output_bfd
);
3750 if (r_symndx
< symtab_hdr
->sh_info
)
3752 sym
= local_syms
+ r_symndx
;
3753 sec
= local_sections
[r_symndx
];
3754 relocation
= _bfd_elf_rela_local_sym (output_bfd
, sym
, &sec
, rel
);
3758 bfd_boolean warned
, ignored
;
3760 RELOC_FOR_GLOBAL_SYMBOL (info
, input_bfd
, input_section
, rel
,
3761 r_symndx
, symtab_hdr
, sym_hashes
,
3763 unresolved_reloc
, warned
, ignored
);
3766 if (sec
&& discarded_section (sec
))
3767 RELOC_AGAINST_DISCARDED_SECTION (info
, input_bfd
, input_section
,
3768 rel
, 1, relend
, howto
, 0, contents
);
3770 /* Nothing more to do unless this is a final link. */
3771 if (bfd_link_relocatable (info
))
3776 bfd_boolean resolved_to_zero
;
3778 resolved_to_zero
= (h
!= NULL
3779 && UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
));
3780 switch (howto
->type
)
3783 r
= nios2_elf32_do_hi16_relocate (input_bfd
, howto
,
3785 contents
, rel
->r_offset
,
3786 relocation
, rel
->r_addend
);
3789 r
= nios2_elf32_do_lo16_relocate (input_bfd
, howto
,
3791 contents
, rel
->r_offset
,
3792 relocation
, rel
->r_addend
);
3794 case R_NIOS2_PCREL_LO
:
3795 r
= nios2_elf32_do_pcrel_lo16_relocate (input_bfd
, howto
,
3802 case R_NIOS2_HIADJ16
:
3803 r
= nios2_elf32_do_hiadj16_relocate (input_bfd
, howto
,
3804 input_section
, contents
,
3805 rel
->r_offset
, relocation
,
3808 case R_NIOS2_PCREL_HA
:
3809 r
= nios2_elf32_do_pcrel_hiadj16_relocate (input_bfd
, howto
,
3816 case R_NIOS2_PCREL16
:
3817 r
= nios2_elf32_do_pcrel16_relocate (input_bfd
, howto
,
3818 input_section
, contents
,
3819 rel
->r_offset
, relocation
,
3823 /* Turns an absolute address into a gp-relative address. */
3824 if (!nios2_elf_assign_gp (output_bfd
, &gp
, info
))
3826 bfd_vma reloc_address
;
3828 if (sec
&& sec
->output_section
)
3829 reloc_address
= (sec
->output_section
->vma
3830 + sec
->output_offset
3835 format
= _("global pointer relative relocation at address "
3836 "%#" PRIx64
" when _gp not defined\n");
3837 if (asprintf (&msgbuf
, format
,
3838 (uint64_t) reloc_address
) == -1)
3841 r
= bfd_reloc_dangerous
;
3845 bfd_vma symbol_address
= rel
->r_addend
+ relocation
;
3846 relocation
= symbol_address
- gp
;
3848 if (((signed) relocation
< -32768
3849 || (signed) relocation
> 32767)
3851 || h
->root
.type
== bfd_link_hash_defined
3852 || h
->root
.type
== bfd_link_hash_defweak
))
3855 name
= h
->root
.root
.string
;
3858 name
= (bfd_elf_string_from_elf_section
3859 (input_bfd
, symtab_hdr
->sh_link
,
3861 if (name
== NULL
|| *name
== '\0')
3862 name
= bfd_section_name (sec
);
3864 /* xgettext:c-format */
3865 format
= _("unable to reach %s (at %#" PRIx64
") from "
3866 "the global pointer (at %#" PRIx64
") "
3867 "because the offset (%" PRId64
") is out of "
3868 "the allowed range, -32678 to 32767\n" );
3869 if (asprintf (&msgbuf
, format
, name
,
3870 (uint64_t) symbol_address
, (uint64_t) gp
,
3871 (int64_t) relocation
) == -1)
3874 r
= bfd_reloc_outofrange
;
3877 r
= _bfd_final_link_relocate (howto
, input_bfd
,
3878 input_section
, contents
,
3879 rel
->r_offset
, relocation
,
3884 r
= nios2_elf32_do_ujmp_relocate (input_bfd
, howto
,
3886 contents
, rel
->r_offset
,
3887 relocation
, rel
->r_addend
);
3890 r
= nios2_elf32_do_cjmp_relocate (input_bfd
, howto
,
3892 contents
, rel
->r_offset
,
3893 relocation
, rel
->r_addend
);
3896 r
= nios2_elf32_do_callr_relocate (input_bfd
, howto
,
3897 input_section
, contents
,
3898 rel
->r_offset
, relocation
,
3901 case R_NIOS2_CALL26
:
3902 case R_NIOS2_CALL26_NOAT
:
3903 /* If we have a call to an undefined weak symbol, we just want
3904 to stuff a zero in the bits of the call instruction and
3905 bypass the normal call26 relocation handling, because it'll
3906 diagnose an overflow error if address 0 isn't in the same
3907 256MB segment as the call site. Presumably the call
3908 should be guarded by a null check anyway. */
3909 if (h
!= NULL
&& h
->root
.type
== bfd_link_hash_undefweak
)
3911 BFD_ASSERT (relocation
== 0 && rel
->r_addend
== 0);
3912 r
= _bfd_final_link_relocate (howto
, input_bfd
,
3913 input_section
, contents
,
3914 rel
->r_offset
, relocation
,
3918 /* Handle relocations which should use the PLT entry.
3919 NIOS2_BFD_RELOC_32 relocations will use the symbol's value,
3920 which may point to a PLT entry, but we don't need to handle
3921 that here. If we created a PLT entry, all branches in this
3922 object should go to it. */
3923 if (h
!= NULL
&& splt
!= NULL
&& h
->plt
.offset
!= (bfd_vma
) -1)
3925 /* If we've created a .plt section, and assigned a PLT entry
3926 to this function, it should not be known to bind locally.
3927 If it were, we would have cleared the PLT entry. */
3928 BFD_ASSERT (!SYMBOL_CALLS_LOCAL (info
, h
));
3930 relocation
= (splt
->output_section
->vma
3931 + splt
->output_offset
3934 unresolved_reloc
= FALSE
;
3936 /* Detect R_NIOS2_CALL26 relocations that would overflow the
3937 256MB segment. Replace the target with a reference to a
3939 Note that htab->stub_group is null if relaxation has been
3940 disabled by the --no-relax linker command-line option, so
3941 we can use that to skip this processing entirely. */
3942 if (howto
->type
== R_NIOS2_CALL26
&& htab
->stub_group
)
3944 bfd_vma dest
= relocation
+ rel
->r_addend
;
3945 enum elf32_nios2_stub_type stub_type
;
3947 eh
= (struct elf32_nios2_link_hash_entry
*)h
;
3948 stub_type
= nios2_type_of_stub (input_section
, rel
, eh
,
3951 if (stub_type
!= nios2_stub_none
)
3953 struct elf32_nios2_stub_hash_entry
*hsh
;
3955 hsh
= nios2_get_stub_entry (input_section
, sec
,
3956 eh
, rel
, htab
, stub_type
);
3959 r
= bfd_reloc_undefined
;
3963 dest
= (hsh
->stub_offset
3964 + hsh
->stub_sec
->output_offset
3965 + hsh
->stub_sec
->output_section
->vma
);
3966 r
= nios2_elf32_do_call26_relocate (input_bfd
, howto
,
3976 r
= nios2_elf32_do_call26_relocate (input_bfd
, howto
,
3977 input_section
, contents
,
3978 rel
->r_offset
, relocation
,
3983 /* For symmetry this would be
3984 r = nios2_elf32_do_ignore_reloc (input_bfd, howto,
3985 input_section, contents,
3986 rel->r_offset, relocation,
3988 but do_ignore_reloc would do no more than return
3993 case R_NIOS2_CALL16
:
3994 case R_NIOS2_GOT_LO
:
3995 case R_NIOS2_GOT_HA
:
3996 case R_NIOS2_CALL_LO
:
3997 case R_NIOS2_CALL_HA
:
3998 /* Relocation is to the entry for this symbol in the
3999 global offset table. */
4002 r
= bfd_reloc_notsupported
;
4012 eh
= (struct elf32_nios2_link_hash_entry
*)h
;
4013 use_plt
= (eh
->got_types_used
== CALL_USED
4014 && h
->plt
.offset
!= (bfd_vma
) -1);
4016 off
= h
->got
.offset
;
4017 BFD_ASSERT (off
!= (bfd_vma
) -1);
4018 dyn
= htab
->root
.dynamic_sections_created
;
4019 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
,
4020 bfd_link_pic (info
),
4022 || (bfd_link_pic (info
)
4023 && SYMBOL_REFERENCES_LOCAL (info
, h
))
4024 || ((ELF_ST_VISIBILITY (h
->other
)
4025 || resolved_to_zero
)
4026 && h
->root
.type
== bfd_link_hash_undefweak
))
4028 /* This is actually a static link, or it is a -Bsymbolic
4029 link and the symbol is defined locally. We must
4030 initialize this entry in the global offset table.
4031 Since the offset must always be a multiple of 4, we
4032 use the least significant bit to record whether we
4033 have initialized it already.
4035 When doing a dynamic link, we create a .rela.got
4036 relocation entry to initialize the value. This is
4037 done in the finish_dynamic_symbol routine. */
4042 bfd_put_32 (output_bfd
, relocation
,
4043 sgot
->contents
+ off
);
4048 unresolved_reloc
= FALSE
;
4052 BFD_ASSERT (local_got_offsets
!= NULL
4053 && local_got_offsets
[r_symndx
] != (bfd_vma
) -1);
4055 off
= local_got_offsets
[r_symndx
];
4057 /* The offset must always be a multiple of 4. We use the
4058 least significant bit to record whether we have already
4059 generated the necessary reloc. */
4064 bfd_put_32 (output_bfd
, relocation
,
4065 sgot
->contents
+ off
);
4067 if (bfd_link_pic (info
))
4070 Elf_Internal_Rela outrel
;
4073 srelgot
= htab
->root
.srelgot
;
4074 BFD_ASSERT (srelgot
!= NULL
);
4076 outrel
.r_addend
= relocation
;
4077 outrel
.r_offset
= (sgot
->output_section
->vma
4078 + sgot
->output_offset
4080 outrel
.r_info
= ELF32_R_INFO (0, R_NIOS2_RELATIVE
);
4081 loc
= srelgot
->contents
;
4082 loc
+= (srelgot
->reloc_count
++ *
4083 sizeof (Elf32_External_Rela
));
4084 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
, loc
);
4087 local_got_offsets
[r_symndx
] |= 1;
4091 if (use_plt
&& bfd_link_pic (info
))
4093 off
= ((h
->plt
.offset
- 24) / 12 + 3) * 4;
4094 relocation
= (htab
->root
.sgotplt
->output_offset
+ off
4098 relocation
= sgot
->output_offset
+ off
- got_base
;
4100 /* This relocation does not use the addend. */
4103 switch (howto
->type
)
4105 case R_NIOS2_GOT_LO
:
4106 case R_NIOS2_CALL_LO
:
4107 r
= nios2_elf32_do_lo16_relocate (input_bfd
, howto
,
4108 input_section
, contents
,
4109 rel
->r_offset
, relocation
,
4112 case R_NIOS2_GOT_HA
:
4113 case R_NIOS2_CALL_HA
:
4114 r
= nios2_elf32_do_hiadj16_relocate (input_bfd
, howto
,
4115 input_section
, contents
,
4121 r
= _bfd_final_link_relocate (howto
, input_bfd
,
4122 input_section
, contents
,
4123 rel
->r_offset
, relocation
,
4129 case R_NIOS2_GOTOFF_LO
:
4130 case R_NIOS2_GOTOFF_HA
:
4131 case R_NIOS2_GOTOFF
:
4132 /* Relocation is relative to the global offset table pointer. */
4134 BFD_ASSERT (sgot
!= NULL
);
4137 r
= bfd_reloc_notsupported
;
4141 /* Note that sgot->output_offset is not involved in this
4142 calculation. We always want the start of .got. */
4143 relocation
-= sgot
->output_section
->vma
;
4145 /* Now we adjust the relocation to be relative to the GOT pointer
4146 (the _gp_got symbol), which possibly contains the 0x8000 bias. */
4147 relocation
-= got_base
;
4149 switch (howto
->type
)
4151 case R_NIOS2_GOTOFF_LO
:
4152 r
= nios2_elf32_do_lo16_relocate (input_bfd
, howto
,
4153 input_section
, contents
,
4154 rel
->r_offset
, relocation
,
4157 case R_NIOS2_GOTOFF_HA
:
4158 r
= nios2_elf32_do_hiadj16_relocate (input_bfd
, howto
,
4159 input_section
, contents
,
4165 r
= _bfd_final_link_relocate (howto
, input_bfd
,
4166 input_section
, contents
,
4167 rel
->r_offset
, relocation
,
4173 case R_NIOS2_TLS_LDO16
:
4174 relocation
-= dtpoff_base (info
) + DTP_OFFSET
;
4176 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
4177 contents
, rel
->r_offset
,
4178 relocation
, rel
->r_addend
);
4180 case R_NIOS2_TLS_LDM16
:
4181 if (htab
->root
.sgot
== NULL
)
4184 off
= htab
->tls_ldm_got
.offset
;
4190 /* If we don't know the module number, create a relocation
4192 if (bfd_link_pic (info
))
4194 Elf_Internal_Rela outrel
;
4197 if (htab
->root
.srelgot
== NULL
)
4200 outrel
.r_addend
= 0;
4201 outrel
.r_offset
= (htab
->root
.sgot
->output_section
->vma
4202 + htab
->root
.sgot
->output_offset
4204 outrel
.r_info
= ELF32_R_INFO (0, R_NIOS2_TLS_DTPMOD
);
4206 loc
= htab
->root
.srelgot
->contents
;
4207 loc
+= (htab
->root
.srelgot
->reloc_count
++
4208 * sizeof (Elf32_External_Rela
));
4209 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
, loc
);
4212 bfd_put_32 (output_bfd
, 1,
4213 htab
->root
.sgot
->contents
+ off
);
4215 htab
->tls_ldm_got
.offset
|= 1;
4218 relocation
= htab
->root
.sgot
->output_offset
+ off
- got_base
;
4220 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
4221 contents
, rel
->r_offset
,
4222 relocation
, rel
->r_addend
);
4225 case R_NIOS2_TLS_GD16
:
4226 case R_NIOS2_TLS_IE16
:
4231 if (htab
->root
.sgot
== NULL
)
4238 dyn
= htab
->root
.dynamic_sections_created
;
4239 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
,
4240 bfd_link_pic (info
),
4242 && (!bfd_link_pic (info
)
4243 || !SYMBOL_REFERENCES_LOCAL (info
, h
)))
4245 unresolved_reloc
= FALSE
;
4248 off
= h
->got
.offset
;
4249 tls_type
= (((struct elf32_nios2_link_hash_entry
*) h
)
4254 if (local_got_offsets
== NULL
)
4256 off
= local_got_offsets
[r_symndx
];
4257 tls_type
= (elf32_nios2_local_got_tls_type (input_bfd
)
4261 if (tls_type
== GOT_UNKNOWN
)
4268 bfd_boolean need_relocs
= FALSE
;
4269 Elf_Internal_Rela outrel
;
4270 bfd_byte
*loc
= NULL
;
4273 /* The GOT entries have not been initialized yet. Do it
4274 now, and emit any relocations. If both an IE GOT and a
4275 GD GOT are necessary, we emit the GD first. */
4277 if ((bfd_link_pic (info
) || indx
!= 0)
4279 || (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
4280 && !resolved_to_zero
)
4281 || h
->root
.type
!= bfd_link_hash_undefweak
))
4284 if (htab
->root
.srelgot
== NULL
)
4286 loc
= htab
->root
.srelgot
->contents
;
4287 loc
+= (htab
->root
.srelgot
->reloc_count
*
4288 sizeof (Elf32_External_Rela
));
4291 if (tls_type
& GOT_TLS_GD
)
4295 outrel
.r_addend
= 0;
4296 outrel
.r_offset
= (htab
->root
.sgot
->output_section
->vma
4297 + htab
->root
.sgot
->output_offset
4299 outrel
.r_info
= ELF32_R_INFO (indx
,
4300 R_NIOS2_TLS_DTPMOD
);
4302 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
,
4304 htab
->root
.srelgot
->reloc_count
++;
4305 loc
+= sizeof (Elf32_External_Rela
);
4308 bfd_put_32 (output_bfd
,
4309 (relocation
- dtpoff_base (info
) -
4311 htab
->root
.sgot
->contents
+ cur_off
+ 4);
4314 outrel
.r_addend
= 0;
4315 outrel
.r_info
= ELF32_R_INFO (indx
,
4316 R_NIOS2_TLS_DTPREL
);
4317 outrel
.r_offset
+= 4;
4319 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
,
4321 htab
->root
.srelgot
->reloc_count
++;
4322 loc
+= sizeof (Elf32_External_Rela
);
4327 /* If we are not emitting relocations for a
4328 general dynamic reference, then we must be in a
4329 static link or an executable link with the
4330 symbol binding locally. Mark it as belonging
4331 to module 1, the executable. */
4332 bfd_put_32 (output_bfd
, 1,
4333 htab
->root
.sgot
->contents
+ cur_off
);
4334 bfd_put_32 (output_bfd
, (relocation
-
4335 dtpoff_base (info
) -
4337 htab
->root
.sgot
->contents
+ cur_off
+ 4);
4343 if (tls_type
& GOT_TLS_IE
)
4348 outrel
.r_addend
= (relocation
-
4349 dtpoff_base (info
));
4351 outrel
.r_addend
= 0;
4352 outrel
.r_offset
= (htab
->root
.sgot
->output_section
->vma
4353 + htab
->root
.sgot
->output_offset
4355 outrel
.r_info
= ELF32_R_INFO (indx
,
4358 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
,
4360 htab
->root
.srelgot
->reloc_count
++;
4361 loc
+= sizeof (Elf32_External_Rela
);
4364 bfd_put_32 (output_bfd
, (tpoff (info
, relocation
)
4366 htab
->root
.sgot
->contents
+ cur_off
);
4373 local_got_offsets
[r_symndx
] |= 1;
4376 if ((tls_type
& GOT_TLS_GD
) && r_type
!= R_NIOS2_TLS_GD16
)
4378 relocation
= htab
->root
.sgot
->output_offset
+ off
- got_base
;
4380 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
4381 contents
, rel
->r_offset
,
4382 relocation
, rel
->r_addend
);
4386 case R_NIOS2_TLS_LE16
:
4387 if (bfd_link_dll (info
))
4390 /* xgettext:c-format */
4391 (_("%pB(%pA+%#" PRIx64
"): %s relocation not "
4392 "permitted in shared object"),
4393 input_bfd
, input_section
,
4394 (uint64_t) rel
->r_offset
, howto
->name
);
4398 relocation
= tpoff (info
, relocation
) - TP_OFFSET
;
4400 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
4401 contents
, rel
->r_offset
,
4402 relocation
, rel
->r_addend
);
4405 case R_NIOS2_BFD_RELOC_32
:
4406 if (bfd_link_pic (info
)
4407 && (input_section
->flags
& SEC_ALLOC
) != 0
4409 || (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
4410 && !resolved_to_zero
)
4411 || h
->root
.type
!= bfd_link_hash_undefweak
))
4413 Elf_Internal_Rela outrel
;
4415 bfd_boolean skip
, relocate
;
4417 /* When generating a shared object, these relocations
4418 are copied into the output file to be resolved at run
4425 = _bfd_elf_section_offset (output_bfd
, info
,
4426 input_section
, rel
->r_offset
);
4427 if (outrel
.r_offset
== (bfd_vma
) -1)
4429 else if (outrel
.r_offset
== (bfd_vma
) -2)
4430 skip
= TRUE
, relocate
= TRUE
;
4431 outrel
.r_offset
+= (input_section
->output_section
->vma
4432 + input_section
->output_offset
);
4435 memset (&outrel
, 0, sizeof outrel
);
4438 && (!bfd_link_pic (info
)
4439 || !SYMBOLIC_BIND (info
, h
)
4440 || !h
->def_regular
))
4442 outrel
.r_info
= ELF32_R_INFO (h
->dynindx
, r_type
);
4443 outrel
.r_addend
= rel
->r_addend
;
4447 /* This symbol is local, or marked to become local. */
4448 outrel
.r_addend
= relocation
+ rel
->r_addend
;
4450 outrel
.r_info
= ELF32_R_INFO (0, R_NIOS2_RELATIVE
);
4453 sreloc
= elf_section_data (input_section
)->sreloc
;
4457 loc
= sreloc
->contents
;
4458 loc
+= sreloc
->reloc_count
++ * sizeof (Elf32_External_Rela
);
4459 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
, loc
);
4461 /* This reloc will be computed at runtime, so there's no
4462 need to do anything now, except for R_NIOS2_BFD_RELOC_32
4463 relocations that have been turned into
4464 R_NIOS2_RELATIVE. */
4469 r
= _bfd_final_link_relocate (howto
, input_bfd
,
4470 input_section
, contents
,
4471 rel
->r_offset
, relocation
,
4475 case R_NIOS2_TLS_DTPREL
:
4476 relocation
-= dtpoff_base (info
);
4480 r
= _bfd_final_link_relocate (howto
, input_bfd
,
4481 input_section
, contents
,
4482 rel
->r_offset
, relocation
,
4488 r
= bfd_reloc_notsupported
;
4490 if (r
!= bfd_reloc_ok
)
4493 name
= h
->root
.root
.string
;
4496 name
= bfd_elf_string_from_elf_section (input_bfd
,
4497 symtab_hdr
->sh_link
,
4499 if (name
== NULL
|| *name
== '\0')
4500 name
= bfd_section_name (sec
);
4505 case bfd_reloc_overflow
:
4506 (*info
->callbacks
->reloc_overflow
) (info
, NULL
, name
,
4507 howto
->name
, (bfd_vma
) 0,
4508 input_bfd
, input_section
,
4512 case bfd_reloc_undefined
:
4513 (*info
->callbacks
->undefined_symbol
) (info
, name
, input_bfd
,
4515 rel
->r_offset
, TRUE
);
4518 case bfd_reloc_outofrange
:
4520 msg
= _("relocation out of range");
4523 case bfd_reloc_notsupported
:
4525 msg
= _("unsupported relocation");
4528 case bfd_reloc_dangerous
:
4530 msg
= _("dangerous relocation");
4535 msg
= _("unknown error");
4541 (*info
->callbacks
->warning
) (info
, msg
, name
, input_bfd
,
4542 input_section
, rel
->r_offset
);
4552 /* Implement elf-backend_section_flags:
4553 Convert NIOS2 specific section flags to bfd internal section flags. */
4555 nios2_elf32_section_flags (const Elf_Internal_Shdr
*hdr
)
4557 if (hdr
->sh_flags
& SHF_NIOS2_GPREL
)
4558 hdr
->bfd_section
->flags
|= SEC_SMALL_DATA
;
4563 /* Implement elf_backend_fake_sections:
4564 Set the correct type for an NIOS2 ELF section. We do this by the
4565 section name, which is a hack, but ought to work. */
4567 nios2_elf32_fake_sections (bfd
*abfd ATTRIBUTE_UNUSED
,
4568 Elf_Internal_Shdr
*hdr
, asection
*sec
)
4570 const char *name
= bfd_section_name (sec
);
4572 if ((sec
->flags
& SEC_SMALL_DATA
)
4573 || strcmp (name
, ".sdata") == 0
4574 || strcmp (name
, ".sbss") == 0
4575 || strcmp (name
, ".lit4") == 0 || strcmp (name
, ".lit8") == 0)
4576 hdr
->sh_flags
|= SHF_NIOS2_GPREL
;
4581 /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
4582 shortcuts to them in our hash table. */
4584 create_got_section (bfd
*dynobj
, struct bfd_link_info
*info
)
4586 struct elf32_nios2_link_hash_table
*htab
;
4587 struct elf_link_hash_entry
*h
;
4589 htab
= elf32_nios2_hash_table (info
);
4591 if (! _bfd_elf_create_got_section (dynobj
, info
))
4594 /* In order for the two loads in .PLTresolve to share the same %hiadj,
4595 _GLOBAL_OFFSET_TABLE_ must be aligned to a 16-byte boundary. */
4596 if (!bfd_set_section_alignment (htab
->root
.sgotplt
, 4))
4599 /* The Nios II ABI specifies that GOT-relative relocations are relative
4600 to the linker-created symbol _gp_got, rather than using
4601 _GLOBAL_OFFSET_TABLE_ directly. In particular, the latter always
4602 points to the base of the GOT while _gp_got may include a bias. */
4603 h
= _bfd_elf_define_linkage_sym (dynobj
, info
, htab
->root
.sgotplt
,
4612 /* Implement elf_backend_create_dynamic_sections:
4613 Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
4614 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
4617 nios2_elf32_create_dynamic_sections (bfd
*dynobj
, struct bfd_link_info
*info
)
4619 struct elf32_nios2_link_hash_table
*htab
;
4621 htab
= elf32_nios2_hash_table (info
);
4622 if (!htab
->root
.sgot
&& !create_got_section (dynobj
, info
))
4625 if (!_bfd_elf_create_dynamic_sections (dynobj
, info
))
4628 /* In order for the two loads in a shared object .PLTresolve to share the
4629 same %hiadj, the start of the PLT (as well as the GOT) must be aligned
4630 to a 16-byte boundary. This is because the addresses for these loads
4631 include the -(.plt+4) PIC correction. */
4632 return bfd_set_section_alignment (htab
->root
.splt
, 4);
4635 /* Implement elf_backend_copy_indirect_symbol:
4636 Copy the extra info we tack onto an elf_link_hash_entry. */
4638 nios2_elf32_copy_indirect_symbol (struct bfd_link_info
*info
,
4639 struct elf_link_hash_entry
*dir
,
4640 struct elf_link_hash_entry
*ind
)
4642 struct elf32_nios2_link_hash_entry
*edir
, *eind
;
4644 edir
= (struct elf32_nios2_link_hash_entry
*) dir
;
4645 eind
= (struct elf32_nios2_link_hash_entry
*) ind
;
4647 if (eind
->dyn_relocs
!= NULL
)
4649 if (edir
->dyn_relocs
!= NULL
)
4651 struct elf_dyn_relocs
**pp
;
4652 struct elf_dyn_relocs
*p
;
4654 /* Add reloc counts against the indirect sym to the direct sym
4655 list. Merge any entries against the same section. */
4656 for (pp
= &eind
->dyn_relocs
; (p
= *pp
) != NULL
; )
4658 struct elf_dyn_relocs
*q
;
4660 for (q
= edir
->dyn_relocs
; q
!= NULL
; q
= q
->next
)
4661 if (q
->sec
== p
->sec
)
4663 q
->pc_count
+= p
->pc_count
;
4664 q
->count
+= p
->count
;
4671 *pp
= edir
->dyn_relocs
;
4674 edir
->dyn_relocs
= eind
->dyn_relocs
;
4675 eind
->dyn_relocs
= NULL
;
4678 if (ind
->root
.type
== bfd_link_hash_indirect
4679 && dir
->got
.refcount
<= 0)
4681 edir
->tls_type
= eind
->tls_type
;
4682 eind
->tls_type
= GOT_UNKNOWN
;
4685 edir
->got_types_used
|= eind
->got_types_used
;
4687 _bfd_elf_link_hash_copy_indirect (info
, dir
, ind
);
4690 /* Set the right machine number for a NIOS2 ELF file. */
4693 nios2_elf32_object_p (bfd
*abfd
)
4697 mach
= elf_elfheader (abfd
)->e_flags
;
4702 case EF_NIOS2_ARCH_R1
:
4703 bfd_default_set_arch_mach (abfd
, bfd_arch_nios2
, bfd_mach_nios2r1
);
4705 case EF_NIOS2_ARCH_R2
:
4706 bfd_default_set_arch_mach (abfd
, bfd_arch_nios2
, bfd_mach_nios2r2
);
4713 /* Implement elf_backend_check_relocs:
4714 Look through the relocs for a section during the first phase. */
4716 nios2_elf32_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
,
4717 asection
*sec
, const Elf_Internal_Rela
*relocs
)
4719 Elf_Internal_Shdr
*symtab_hdr
;
4720 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
4721 const Elf_Internal_Rela
*rel
;
4722 const Elf_Internal_Rela
*rel_end
;
4723 struct elf32_nios2_link_hash_table
*htab
;
4724 asection
*sreloc
= NULL
;
4725 bfd_signed_vma
*local_got_refcounts
;
4727 if (bfd_link_relocatable (info
))
4730 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4731 sym_hashes
= elf_sym_hashes (abfd
);
4732 sym_hashes_end
= (sym_hashes
4733 + symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
));
4734 if (!elf_bad_symtab (abfd
))
4735 sym_hashes_end
-= symtab_hdr
->sh_info
;
4736 local_got_refcounts
= elf_local_got_refcounts (abfd
);
4738 htab
= elf32_nios2_hash_table (info
);
4740 rel_end
= relocs
+ sec
->reloc_count
;
4741 for (rel
= relocs
; rel
< rel_end
; rel
++)
4743 unsigned int r_type
;
4744 struct elf_link_hash_entry
*h
;
4745 unsigned long r_symndx
;
4747 r_symndx
= ELF32_R_SYM (rel
->r_info
);
4748 if (r_symndx
< symtab_hdr
->sh_info
)
4752 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
4753 while (h
->root
.type
== bfd_link_hash_indirect
4754 || h
->root
.type
== bfd_link_hash_warning
)
4755 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4758 r_type
= ELF32_R_TYPE (rel
->r_info
);
4763 case R_NIOS2_GOT_LO
:
4764 case R_NIOS2_GOT_HA
:
4765 case R_NIOS2_CALL16
:
4766 case R_NIOS2_CALL_LO
:
4767 case R_NIOS2_CALL_HA
:
4768 case R_NIOS2_TLS_GD16
:
4769 case R_NIOS2_TLS_IE16
:
4770 /* This symbol requires a global offset table entry. */
4772 int tls_type
, old_tls_type
;
4778 case R_NIOS2_GOT_LO
:
4779 case R_NIOS2_GOT_HA
:
4780 case R_NIOS2_CALL16
:
4781 case R_NIOS2_CALL_LO
:
4782 case R_NIOS2_CALL_HA
:
4783 tls_type
= GOT_NORMAL
;
4785 case R_NIOS2_TLS_GD16
:
4786 tls_type
= GOT_TLS_GD
;
4788 case R_NIOS2_TLS_IE16
:
4789 tls_type
= GOT_TLS_IE
;
4795 struct elf32_nios2_link_hash_entry
*eh
4796 = (struct elf32_nios2_link_hash_entry
*)h
;
4798 old_tls_type
= elf32_nios2_hash_entry(h
)->tls_type
;
4799 if (r_type
== R_NIOS2_CALL16
4800 || r_type
== R_NIOS2_CALL_LO
4801 || r_type
== R_NIOS2_CALL_HA
)
4803 /* Make sure a plt entry is created for this symbol if
4804 it turns out to be a function defined by a dynamic
4809 eh
->got_types_used
|= CALL_USED
;
4812 eh
->got_types_used
|= GOT_USED
;
4816 /* This is a global offset table entry for a local symbol. */
4817 if (local_got_refcounts
== NULL
)
4821 size
= symtab_hdr
->sh_info
;
4822 size
*= (sizeof (bfd_signed_vma
) + sizeof (char));
4824 = ((bfd_signed_vma
*) bfd_zalloc (abfd
, size
));
4825 if (local_got_refcounts
== NULL
)
4827 elf_local_got_refcounts (abfd
) = local_got_refcounts
;
4828 elf32_nios2_local_got_tls_type (abfd
)
4829 = (char *) (local_got_refcounts
+ symtab_hdr
->sh_info
);
4831 local_got_refcounts
[r_symndx
]++;
4832 old_tls_type
= elf32_nios2_local_got_tls_type (abfd
) [r_symndx
];
4835 /* We will already have issued an error message if there is a
4836 TLS / non-TLS mismatch, based on the symbol type. We don't
4837 support any linker relaxations. So just combine any TLS
4839 if (old_tls_type
!= GOT_UNKNOWN
&& old_tls_type
!= GOT_NORMAL
4840 && tls_type
!= GOT_NORMAL
)
4841 tls_type
|= old_tls_type
;
4843 if (old_tls_type
!= tls_type
)
4846 elf32_nios2_hash_entry (h
)->tls_type
= tls_type
;
4848 elf32_nios2_local_got_tls_type (abfd
) [r_symndx
] = tls_type
;
4852 if (htab
->root
.sgot
== NULL
)
4854 if (htab
->root
.dynobj
== NULL
)
4855 htab
->root
.dynobj
= abfd
;
4856 if (!create_got_section (htab
->root
.dynobj
, info
))
4861 case R_NIOS2_TLS_LDM16
:
4862 htab
->tls_ldm_got
.refcount
++;
4865 /* This relocation describes the C++ object vtable hierarchy.
4866 Reconstruct it for later use during GC. */
4867 case R_NIOS2_GNU_VTINHERIT
:
4868 if (!bfd_elf_gc_record_vtinherit (abfd
, sec
, h
, rel
->r_offset
))
4872 /* This relocation describes which C++ vtable entries are actually
4873 used. Record for later use during GC. */
4874 case R_NIOS2_GNU_VTENTRY
:
4875 if (!bfd_elf_gc_record_vtentry (abfd
, sec
, h
, rel
->r_addend
))
4879 case R_NIOS2_BFD_RELOC_32
:
4880 case R_NIOS2_CALL26
:
4881 case R_NIOS2_CALL26_NOAT
:
4882 case R_NIOS2_HIADJ16
:
4887 /* If this reloc is in a read-only section, we might
4888 need a copy reloc. We can't check reliably at this
4889 stage whether the section is read-only, as input
4890 sections have not yet been mapped to output sections.
4891 Tentatively set the flag for now, and correct in
4892 adjust_dynamic_symbol. */
4893 if (!bfd_link_pic (info
))
4896 /* Make sure a plt entry is created for this symbol if it
4897 turns out to be a function defined by a dynamic object. */
4900 if (r_type
== R_NIOS2_CALL26
|| r_type
== R_NIOS2_CALL26_NOAT
)
4904 /* If we are creating a shared library, we need to copy the
4905 reloc into the shared library. */
4906 if (bfd_link_pic (info
)
4907 && (sec
->flags
& SEC_ALLOC
) != 0
4908 && (r_type
== R_NIOS2_BFD_RELOC_32
4909 || (h
!= NULL
&& ! h
->needs_plt
4910 && (! SYMBOLIC_BIND (info
, h
) || ! h
->def_regular
))))
4912 struct elf_dyn_relocs
*p
;
4913 struct elf_dyn_relocs
**head
;
4915 /* When creating a shared object, we must copy these
4916 reloc types into the output file. We create a reloc
4917 section in dynobj and make room for this reloc. */
4920 if (htab
->root
.dynobj
== NULL
)
4921 htab
->root
.dynobj
= abfd
;
4923 sreloc
= _bfd_elf_make_dynamic_reloc_section
4924 (sec
, htab
->root
.dynobj
, 2, abfd
, TRUE
);
4929 /* If this is a global symbol, we count the number of
4930 relocations we need for this symbol. */
4932 head
= &((struct elf32_nios2_link_hash_entry
*) h
)->dyn_relocs
;
4935 /* Track dynamic relocs needed for local syms too.
4936 We really need local syms available to do this
4941 Elf_Internal_Sym
*isym
;
4943 isym
= bfd_sym_from_r_symndx (&htab
->sym_cache
,
4948 s
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
4952 vpp
= &elf_section_data (s
)->local_dynrel
;
4953 head
= (struct elf_dyn_relocs
**) vpp
;
4957 if (p
== NULL
|| p
->sec
!= sec
)
4959 size_t amt
= sizeof *p
;
4960 p
= ((struct elf_dyn_relocs
*)
4961 bfd_alloc (htab
->root
.dynobj
, amt
));
4982 /* Implement elf_backend_gc_mark_hook:
4983 Return the section that should be marked against GC for a given
4986 nios2_elf32_gc_mark_hook (asection
*sec
,
4987 struct bfd_link_info
*info
,
4988 Elf_Internal_Rela
*rel
,
4989 struct elf_link_hash_entry
*h
,
4990 Elf_Internal_Sym
*sym
)
4993 switch (ELF32_R_TYPE (rel
->r_info
))
4995 case R_NIOS2_GNU_VTINHERIT
:
4996 case R_NIOS2_GNU_VTENTRY
:
4999 return _bfd_elf_gc_mark_hook (sec
, info
, rel
, h
, sym
);
5002 /* Implement elf_backend_finish_dynamic_symbols:
5003 Finish up dynamic symbol handling. We set the contents of various
5004 dynamic sections here. */
5006 nios2_elf32_finish_dynamic_symbol (bfd
*output_bfd
,
5007 struct bfd_link_info
*info
,
5008 struct elf_link_hash_entry
*h
,
5009 Elf_Internal_Sym
*sym
)
5011 struct elf32_nios2_link_hash_table
*htab
;
5012 struct elf32_nios2_link_hash_entry
*eh
5013 = (struct elf32_nios2_link_hash_entry
*)h
;
5016 htab
= elf32_nios2_hash_table (info
);
5018 if (h
->plt
.offset
!= (bfd_vma
) -1)
5025 Elf_Internal_Rela rela
;
5027 bfd_vma got_address
;
5029 /* This symbol has an entry in the procedure linkage table. Set
5031 BFD_ASSERT (h
->dynindx
!= -1);
5032 splt
= htab
->root
.splt
;
5033 sgotplt
= htab
->root
.sgotplt
;
5034 srela
= htab
->root
.srelplt
;
5035 BFD_ASSERT (splt
!= NULL
&& sgotplt
!= NULL
&& srela
!= NULL
);
5037 /* Emit the PLT entry. */
5038 if (bfd_link_pic (info
))
5040 nios2_elf32_install_data (splt
, nios2_so_plt_entry
, h
->plt
.offset
,
5042 plt_index
= (h
->plt
.offset
- 24) / 12;
5043 got_offset
= (plt_index
+ 3) * 4;
5044 nios2_elf32_install_imm16 (splt
, h
->plt
.offset
,
5045 hiadj(plt_index
* 4));
5046 nios2_elf32_install_imm16 (splt
, h
->plt
.offset
+ 4,
5047 (plt_index
* 4) & 0xffff);
5048 nios2_elf32_install_imm16 (splt
, h
->plt
.offset
+ 8,
5049 0xfff4 - h
->plt
.offset
);
5050 got_address
= (sgotplt
->output_section
->vma
+ sgotplt
->output_offset
5053 /* Fill in the entry in the global offset table. There are no
5054 res_n slots for a shared object PLT, instead the .got.plt entries
5055 point to the PLT entries. */
5056 bfd_put_32 (output_bfd
,
5057 splt
->output_section
->vma
+ splt
->output_offset
5058 + h
->plt
.offset
, sgotplt
->contents
+ got_offset
);
5062 plt_index
= (h
->plt
.offset
- 28 - htab
->res_n_size
) / 12;
5063 got_offset
= (plt_index
+ 3) * 4;
5065 nios2_elf32_install_data (splt
, nios2_plt_entry
, h
->plt
.offset
, 3);
5066 got_address
= (sgotplt
->output_section
->vma
+ sgotplt
->output_offset
5068 nios2_elf32_install_imm16 (splt
, h
->plt
.offset
, hiadj(got_address
));
5069 nios2_elf32_install_imm16 (splt
, h
->plt
.offset
+ 4,
5070 got_address
& 0xffff);
5072 /* Fill in the entry in the global offset table. */
5073 bfd_put_32 (output_bfd
,
5074 splt
->output_section
->vma
+ splt
->output_offset
5075 + plt_index
* 4, sgotplt
->contents
+ got_offset
);
5078 /* Fill in the entry in the .rela.plt section. */
5079 rela
.r_offset
= got_address
;
5080 rela
.r_info
= ELF32_R_INFO (h
->dynindx
, R_NIOS2_JUMP_SLOT
);
5082 loc
= srela
->contents
+ plt_index
* sizeof (Elf32_External_Rela
);
5083 bfd_elf32_swap_reloca_out (output_bfd
, &rela
, loc
);
5085 if (!h
->def_regular
)
5087 /* Mark the symbol as undefined, rather than as defined in
5088 the .plt section. Leave the value alone. */
5089 sym
->st_shndx
= SHN_UNDEF
;
5090 /* If the symbol is weak, we do need to clear the value.
5091 Otherwise, the PLT entry would provide a definition for
5092 the symbol even if the symbol wasn't defined anywhere,
5093 and so the symbol would never be NULL. */
5094 if (!h
->ref_regular_nonweak
)
5099 use_plt
= (eh
->got_types_used
== CALL_USED
5100 && h
->plt
.offset
!= (bfd_vma
) -1);
5102 if (!use_plt
&& h
->got
.offset
!= (bfd_vma
) -1
5103 && (elf32_nios2_hash_entry (h
)->tls_type
& GOT_TLS_GD
) == 0
5104 && (elf32_nios2_hash_entry (h
)->tls_type
& GOT_TLS_IE
) == 0)
5108 Elf_Internal_Rela rela
;
5112 /* This symbol has an entry in the global offset table. Set it
5114 sgot
= htab
->root
.sgot
;
5115 srela
= htab
->root
.srelgot
;
5116 BFD_ASSERT (sgot
!= NULL
&& srela
!= NULL
);
5118 offset
= (h
->got
.offset
& ~(bfd_vma
) 1);
5119 rela
.r_offset
= (sgot
->output_section
->vma
5120 + sgot
->output_offset
+ offset
);
5122 /* If this is a -Bsymbolic link, and the symbol is defined
5123 locally, we just want to emit a RELATIVE reloc. Likewise if
5124 the symbol was forced to be local because of a version file.
5125 The entry in the global offset table will already have been
5126 initialized in the relocate_section function. */
5128 if (bfd_link_pic (info
) && SYMBOL_REFERENCES_LOCAL (info
, h
))
5130 rela
.r_info
= ELF32_R_INFO (0, R_NIOS2_RELATIVE
);
5131 rela
.r_addend
= bfd_get_signed_32 (output_bfd
,
5132 (sgot
->contents
+ offset
));
5133 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
+ offset
);
5137 bfd_put_32 (output_bfd
, (bfd_vma
) 0,
5138 sgot
->contents
+ offset
);
5139 rela
.r_info
= ELF32_R_INFO (h
->dynindx
, R_NIOS2_GLOB_DAT
);
5143 loc
= srela
->contents
;
5144 loc
+= srela
->reloc_count
++ * sizeof (Elf32_External_Rela
);
5145 bfd_elf32_swap_reloca_out (output_bfd
, &rela
, loc
);
5148 if (use_plt
&& h
->got
.offset
!= (bfd_vma
) -1)
5150 bfd_vma offset
= (h
->got
.offset
& ~(bfd_vma
) 1);
5151 asection
*sgot
= htab
->root
.sgot
;
5152 asection
*splt
= htab
->root
.splt
;
5153 bfd_put_32 (output_bfd
, (splt
->output_section
->vma
+ splt
->output_offset
5155 sgot
->contents
+ offset
);
5161 Elf_Internal_Rela rela
;
5164 /* This symbol needs a copy reloc. Set it up. */
5165 BFD_ASSERT (h
->dynindx
!= -1
5166 && (h
->root
.type
== bfd_link_hash_defined
5167 || h
->root
.type
== bfd_link_hash_defweak
));
5169 rela
.r_offset
= (h
->root
.u
.def
.value
5170 + h
->root
.u
.def
.section
->output_section
->vma
5171 + h
->root
.u
.def
.section
->output_offset
);
5172 rela
.r_info
= ELF32_R_INFO (h
->dynindx
, R_NIOS2_COPY
);
5174 if (h
->root
.u
.def
.section
== htab
->root
.sdynrelro
)
5175 s
= htab
->root
.sreldynrelro
;
5177 s
= htab
->root
.srelbss
;
5178 BFD_ASSERT (s
!= NULL
);
5179 loc
= s
->contents
+ s
->reloc_count
++ * sizeof (Elf32_External_Rela
);
5180 bfd_elf32_swap_reloca_out (output_bfd
, &rela
, loc
);
5183 /* Mark _DYNAMIC, _GLOBAL_OFFSET_TABLE_, and _gp_got as absolute. */
5184 if (strcmp (h
->root
.root
.string
, "_DYNAMIC") == 0
5185 || h
== htab
->root
.hgot
5186 || h
== htab
->h_gp_got
)
5187 sym
->st_shndx
= SHN_ABS
;
5192 /* Implement elf_backend_finish_dynamic_sections. */
5194 nios2_elf32_finish_dynamic_sections (bfd
*output_bfd
,
5195 struct bfd_link_info
*info
)
5199 struct elf32_nios2_link_hash_table
*htab
;
5201 htab
= elf32_nios2_hash_table (info
);
5202 sgotplt
= htab
->root
.sgotplt
;
5205 if (htab
->root
.dynamic_sections_created
)
5208 Elf32_External_Dyn
*dyncon
, *dynconend
;
5210 splt
= htab
->root
.splt
;
5211 sdyn
= bfd_get_linker_section (htab
->root
.dynobj
, ".dynamic");
5212 BFD_ASSERT (splt
!= NULL
&& sdyn
!= NULL
&& sgotplt
!= NULL
);
5214 dyncon
= (Elf32_External_Dyn
*) sdyn
->contents
;
5215 dynconend
= (Elf32_External_Dyn
*) (sdyn
->contents
+ sdyn
->size
);
5216 for (; dyncon
< dynconend
; dyncon
++)
5218 Elf_Internal_Dyn dyn
;
5221 bfd_elf32_swap_dyn_in (htab
->root
.dynobj
, dyncon
, &dyn
);
5229 s
= htab
->root
.sgotplt
;
5230 dyn
.d_un
.d_ptr
= s
->output_section
->vma
+ s
->output_offset
;
5231 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
5235 s
= htab
->root
.srelplt
;
5236 dyn
.d_un
.d_ptr
= s
->output_section
->vma
+ s
->output_offset
;
5237 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
5241 s
= htab
->root
.srelplt
;
5242 dyn
.d_un
.d_val
= s
->size
;
5243 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
5247 s
= htab
->root
.sgotplt
;
5249 = s
->output_section
->vma
+ s
->output_offset
+ 0x7ff0;
5250 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
5255 /* Fill in the first entry in the procedure linkage table. */
5258 bfd_vma got_address
= (sgotplt
->output_section
->vma
5259 + sgotplt
->output_offset
);
5260 if (bfd_link_pic (info
))
5262 bfd_vma got_pcrel
= got_address
- (splt
->output_section
->vma
5263 + splt
->output_offset
);
5264 /* Both GOT and PLT must be aligned to a 16-byte boundary
5265 for the two loads to share the %hiadj part. The 4-byte
5266 offset for nextpc is accounted for in the %lo offsets
5268 BFD_ASSERT ((got_pcrel
& 0xf) == 0);
5269 nios2_elf32_install_data (splt
, nios2_so_plt0_entry
, 0, 6);
5270 nios2_elf32_install_imm16 (splt
, 4, hiadj (got_pcrel
));
5271 nios2_elf32_install_imm16 (splt
, 12, got_pcrel
& 0xffff);
5272 nios2_elf32_install_imm16 (splt
, 16, (got_pcrel
+ 4) & 0xffff);
5276 /* Divide by 4 here, not 3 because we already corrected for the
5278 bfd_vma res_size
= (splt
->size
- 28) / 4;
5279 bfd_vma res_start
= (splt
->output_section
->vma
5280 + splt
->output_offset
);
5283 for (res_offset
= 0; res_offset
< res_size
; res_offset
+= 4)
5284 bfd_put_32 (output_bfd
,
5285 6 | ((res_size
- (res_offset
+ 4)) << 6),
5286 splt
->contents
+ res_offset
);
5288 /* The GOT must be aligned to a 16-byte boundary for the
5289 two loads to share the same %hiadj part. */
5290 BFD_ASSERT ((got_address
& 0xf) == 0);
5292 nios2_elf32_install_data (splt
, nios2_plt0_entry
, res_size
, 7);
5293 nios2_elf32_install_imm16 (splt
, res_size
, hiadj (res_start
));
5294 nios2_elf32_install_imm16 (splt
, res_size
+ 4,
5295 res_start
& 0xffff);
5296 nios2_elf32_install_imm16 (splt
, res_size
+ 12,
5297 hiadj (got_address
));
5298 nios2_elf32_install_imm16 (splt
, res_size
+ 16,
5299 (got_address
+ 4) & 0xffff);
5300 nios2_elf32_install_imm16 (splt
, res_size
+ 20,
5301 (got_address
+ 8) & 0xffff);
5306 /* Fill in the first three entries in the global offset table. */
5307 if (sgotplt
!= NULL
&& sgotplt
->size
> 0)
5310 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgotplt
->contents
);
5312 bfd_put_32 (output_bfd
,
5313 sdyn
->output_section
->vma
+ sdyn
->output_offset
,
5315 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgotplt
->contents
+ 4);
5316 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgotplt
->contents
+ 8);
5318 if (sgotplt
->output_section
!= bfd_abs_section_ptr
)
5319 elf_section_data (sgotplt
->output_section
)->this_hdr
.sh_entsize
= 4;
5325 /* Implement elf_backend_adjust_dynamic_symbol:
5326 Adjust a symbol defined by a dynamic object and referenced by a
5327 regular object. The current definition is in some section of the
5328 dynamic object, but we're not including those sections. We have to
5329 change the definition to something the rest of the link can
5332 nios2_elf32_adjust_dynamic_symbol (struct bfd_link_info
*info
,
5333 struct elf_link_hash_entry
*h
)
5335 struct elf32_nios2_link_hash_table
*htab
;
5340 htab
= elf32_nios2_hash_table (info
);
5341 dynobj
= htab
->root
.dynobj
;
5343 /* Make sure we know what is going on here. */
5344 BFD_ASSERT (dynobj
!= NULL
5349 && !h
->def_regular
)));
5351 /* If this is a function, put it in the procedure linkage table. We
5352 will fill in the contents of the procedure linkage table later,
5353 when we know the address of the .got section. */
5354 if (h
->type
== STT_FUNC
|| h
->needs_plt
)
5356 if (h
->plt
.refcount
<= 0
5357 || SYMBOL_CALLS_LOCAL (info
, h
)
5358 || (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
5359 && h
->root
.type
== bfd_link_hash_undefweak
))
5361 /* This case can occur if we saw a PLT reloc in an input
5362 file, but the symbol was never referred to by a dynamic
5363 object, or if all references were garbage collected. In
5364 such a case, we don't actually need to build a procedure
5365 linkage table, and we can just do a PCREL reloc instead. */
5366 h
->plt
.offset
= (bfd_vma
) -1;
5373 /* Reinitialize the plt offset now that it is not used as a reference
5375 h
->plt
.offset
= (bfd_vma
) -1;
5377 /* If this is a weak symbol, and there is a real definition, the
5378 processor independent code will have arranged for us to see the
5379 real definition first, and we can just use the same value. */
5380 if (h
->is_weakalias
)
5382 struct elf_link_hash_entry
*def
= weakdef (h
);
5383 BFD_ASSERT (def
->root
.type
== bfd_link_hash_defined
);
5384 h
->root
.u
.def
.section
= def
->root
.u
.def
.section
;
5385 h
->root
.u
.def
.value
= def
->root
.u
.def
.value
;
5389 /* If there are no non-GOT references, we do not need a copy
5391 if (!h
->non_got_ref
)
5394 /* This is a reference to a symbol defined by a dynamic object which
5396 If we are creating a shared library, we must presume that the
5397 only references to the symbol are via the global offset table.
5398 For such cases we need not do anything here; the relocations will
5399 be handled correctly by relocate_section. */
5400 if (bfd_link_pic (info
))
5405 _bfd_error_handler (_("dynamic variable `%s' is zero size"),
5406 h
->root
.root
.string
);
5410 /* We must allocate the symbol in our .dynbss section, which will
5411 become part of the .bss section of the executable. There will be
5412 an entry for this symbol in the .dynsym section. The dynamic
5413 object will contain position independent code, so all references
5414 from the dynamic object to this symbol will go through the global
5415 offset table. The dynamic linker will use the .dynsym entry to
5416 determine the address it must put in the global offset table, so
5417 both the dynamic object and the regular object will refer to the
5418 same memory location for the variable. */
5419 /* We must generate a R_NIOS2_COPY reloc to tell the dynamic linker to
5420 copy the initial value out of the dynamic object and into the
5421 runtime process image. We need to remember the offset into the
5422 .rela.bss section we are going to use. */
5423 if ((h
->root
.u
.def
.section
->flags
& SEC_READONLY
) != 0)
5425 s
= htab
->root
.sdynrelro
;
5426 srel
= htab
->root
.sreldynrelro
;
5430 s
= htab
->root
.sdynbss
;
5431 srel
= htab
->root
.srelbss
;
5433 if ((h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0)
5435 srel
->size
+= sizeof (Elf32_External_Rela
);
5439 align2
= bfd_log2 (h
->size
);
5440 if (align2
> h
->root
.u
.def
.section
->alignment_power
)
5441 align2
= h
->root
.u
.def
.section
->alignment_power
;
5444 s
->size
= BFD_ALIGN (s
->size
, (bfd_size_type
)1 << align2
);
5445 if (align2
> bfd_section_alignment (s
)
5446 && !bfd_set_section_alignment (s
, align2
))
5449 /* Define the symbol as being at this point in the section. */
5450 h
->root
.u
.def
.section
= s
;
5451 h
->root
.u
.def
.value
= s
->size
;
5453 /* Increment the section size to make room for the symbol. */
5459 /* Worker function for nios2_elf32_size_dynamic_sections. */
5461 adjust_dynrelocs (struct elf_link_hash_entry
*h
, PTR inf
)
5463 struct bfd_link_info
*info
;
5464 struct elf32_nios2_link_hash_table
*htab
;
5466 if (h
->root
.type
== bfd_link_hash_indirect
)
5469 if (h
->root
.type
== bfd_link_hash_warning
)
5470 /* When warning symbols are created, they **replace** the "real"
5471 entry in the hash table, thus we never get to see the real
5472 symbol in a hash traversal. So look at it now. */
5473 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5475 info
= (struct bfd_link_info
*) inf
;
5476 htab
= elf32_nios2_hash_table (info
);
5478 if (h
->plt
.offset
!= (bfd_vma
)-1)
5479 h
->plt
.offset
+= htab
->res_n_size
;
5480 if (htab
->root
.splt
== h
->root
.u
.def
.section
)
5481 h
->root
.u
.def
.value
+= htab
->res_n_size
;
5486 /* Another worker function for nios2_elf32_size_dynamic_sections.
5487 Allocate space in .plt, .got and associated reloc sections for
5490 allocate_dynrelocs (struct elf_link_hash_entry
*h
, PTR inf
)
5492 struct bfd_link_info
*info
;
5493 struct elf32_nios2_link_hash_table
*htab
;
5494 struct elf32_nios2_link_hash_entry
*eh
;
5495 struct elf_dyn_relocs
*p
;
5498 if (h
->root
.type
== bfd_link_hash_indirect
)
5501 if (h
->root
.type
== bfd_link_hash_warning
)
5502 /* When warning symbols are created, they **replace** the "real"
5503 entry in the hash table, thus we never get to see the real
5504 symbol in a hash traversal. So look at it now. */
5505 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5507 info
= (struct bfd_link_info
*) inf
;
5508 htab
= elf32_nios2_hash_table (info
);
5510 if (htab
->root
.dynamic_sections_created
5511 && h
->plt
.refcount
> 0)
5513 /* Make sure this symbol is output as a dynamic symbol.
5514 Undefined weak syms won't yet be marked as dynamic. */
5515 if (h
->dynindx
== -1
5517 && !bfd_elf_link_record_dynamic_symbol (info
, h
))
5520 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info
), h
))
5522 asection
*s
= htab
->root
.splt
;
5524 /* Allocate room for the header. */
5527 if (bfd_link_pic (info
))
5533 h
->plt
.offset
= s
->size
;
5535 /* If this symbol is not defined in a regular file, and we are
5536 not generating a shared library, then set the symbol to this
5537 location in the .plt. This is required to make function
5538 pointers compare as equal between the normal executable and
5539 the shared library. */
5540 if (! bfd_link_pic (info
)
5543 h
->root
.u
.def
.section
= s
;
5544 h
->root
.u
.def
.value
= h
->plt
.offset
;
5547 /* Make room for this entry. */
5550 /* We also need to make an entry in the .rela.plt section. */
5551 htab
->root
.srelplt
->size
+= sizeof (Elf32_External_Rela
);
5553 /* And the .got.plt section. */
5554 htab
->root
.sgotplt
->size
+= 4;
5558 h
->plt
.offset
= (bfd_vma
) -1;
5564 h
->plt
.offset
= (bfd_vma
) -1;
5568 eh
= (struct elf32_nios2_link_hash_entry
*) h
;
5569 use_plt
= (eh
->got_types_used
== CALL_USED
5570 && h
->plt
.offset
!= (bfd_vma
) -1);
5572 if (h
->got
.refcount
> 0)
5576 int tls_type
= eh
->tls_type
;
5579 /* Make sure this symbol is output as a dynamic symbol.
5580 Undefined weak syms won't yet be marked as dynamic. */
5581 if (h
->dynindx
== -1
5583 && !bfd_elf_link_record_dynamic_symbol (info
, h
))
5586 s
= htab
->root
.sgot
;
5587 h
->got
.offset
= s
->size
;
5589 if (tls_type
== GOT_UNKNOWN
)
5592 if (tls_type
== GOT_NORMAL
)
5593 /* Non-TLS symbols need one GOT slot. */
5597 if (tls_type
& GOT_TLS_GD
)
5598 /* R_NIOS2_TLS_GD16 needs 2 consecutive GOT slots. */
5600 if (tls_type
& GOT_TLS_IE
)
5601 /* R_NIOS2_TLS_IE16 needs one GOT slot. */
5605 dyn
= htab
->root
.dynamic_sections_created
;
5608 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, bfd_link_pic (info
), h
)
5609 && (!bfd_link_pic (info
)
5610 || !SYMBOL_REFERENCES_LOCAL (info
, h
)))
5613 if (tls_type
!= GOT_NORMAL
5614 && (bfd_link_pic (info
) || indx
!= 0)
5615 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
5616 || h
->root
.type
!= bfd_link_hash_undefweak
))
5618 if (tls_type
& GOT_TLS_IE
)
5619 htab
->root
.srelgot
->size
+= sizeof (Elf32_External_Rela
);
5621 if (tls_type
& GOT_TLS_GD
)
5622 htab
->root
.srelgot
->size
+= sizeof (Elf32_External_Rela
);
5624 if ((tls_type
& GOT_TLS_GD
) && indx
!= 0)
5625 htab
->root
.srelgot
->size
+= sizeof (Elf32_External_Rela
);
5627 else if ((ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
5628 || h
->root
.type
!= bfd_link_hash_undefweak
)
5630 && (bfd_link_pic (info
)
5631 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, 0, h
)))
5632 htab
->root
.srelgot
->size
+= sizeof (Elf32_External_Rela
);
5635 h
->got
.offset
= (bfd_vma
) -1;
5637 if (eh
->dyn_relocs
== NULL
)
5640 /* In the shared -Bsymbolic case, discard space allocated for
5641 dynamic pc-relative relocs against symbols which turn out to be
5642 defined in regular objects. For the normal shared case, discard
5643 space for pc-relative relocs that have become local due to symbol
5644 visibility changes. */
5646 if (bfd_link_pic (info
))
5649 && (h
->forced_local
|| SYMBOLIC_BIND (info
, h
)))
5651 struct elf_dyn_relocs
**pp
;
5653 for (pp
= &eh
->dyn_relocs
; (p
= *pp
) != NULL
; )
5655 p
->count
-= p
->pc_count
;
5664 /* Also discard relocs on undefined weak syms with non-default
5666 if (eh
->dyn_relocs
!= NULL
5667 && h
->root
.type
== bfd_link_hash_undefweak
)
5669 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
5670 || UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
))
5671 eh
->dyn_relocs
= NULL
;
5673 /* Make sure undefined weak symbols are output as a dynamic
5675 else if (h
->dynindx
== -1
5677 && !bfd_elf_link_record_dynamic_symbol (info
, h
))
5683 /* For the non-shared case, discard space for relocs against
5684 symbols which turn out to need copy relocs or are not
5688 && ((h
->def_dynamic
&& !h
->def_regular
)
5689 || (htab
->root
.dynamic_sections_created
5690 && (h
->root
.type
== bfd_link_hash_undefweak
5691 || h
->root
.type
== bfd_link_hash_undefined
))))
5693 /* Make sure this symbol is output as a dynamic symbol.
5694 Undefined weak syms won't yet be marked as dynamic. */
5695 if (h
->dynindx
== -1
5697 && !bfd_elf_link_record_dynamic_symbol (info
, h
))
5700 /* If that succeeded, we know we'll be keeping all the
5702 if (h
->dynindx
!= -1)
5706 eh
->dyn_relocs
= NULL
;
5711 /* Finally, allocate space. */
5712 for (p
= eh
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
5714 asection
*sreloc
= elf_section_data (p
->sec
)->sreloc
;
5715 sreloc
->size
+= p
->count
* sizeof (Elf32_External_Rela
);
5721 /* Implement elf_backend_size_dynamic_sections:
5722 Set the sizes of the dynamic sections. */
5724 nios2_elf32_size_dynamic_sections (bfd
*output_bfd ATTRIBUTE_UNUSED
,
5725 struct bfd_link_info
*info
)
5731 struct elf32_nios2_link_hash_table
*htab
;
5733 htab
= elf32_nios2_hash_table (info
);
5734 dynobj
= htab
->root
.dynobj
;
5735 BFD_ASSERT (dynobj
!= NULL
);
5737 htab
->res_n_size
= 0;
5738 if (htab
->root
.dynamic_sections_created
)
5740 /* Set the contents of the .interp section to the interpreter. */
5741 if (bfd_link_executable (info
) && !info
->nointerp
)
5743 s
= bfd_get_linker_section (dynobj
, ".interp");
5744 BFD_ASSERT (s
!= NULL
);
5745 s
->size
= sizeof ELF_DYNAMIC_INTERPRETER
;
5746 s
->contents
= (unsigned char *) ELF_DYNAMIC_INTERPRETER
;
5751 /* We may have created entries in the .rela.got section.
5752 However, if we are not creating the dynamic sections, we will
5753 not actually use these entries. Reset the size of .rela.got,
5754 which will cause it to get stripped from the output file
5756 s
= htab
->root
.srelgot
;
5761 /* Set up .got offsets for local syms, and space for local dynamic
5763 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
5765 bfd_signed_vma
*local_got
;
5766 bfd_signed_vma
*end_local_got
;
5767 char *local_tls_type
;
5768 bfd_size_type locsymcount
;
5769 Elf_Internal_Shdr
*symtab_hdr
;
5772 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
5775 for (s
= ibfd
->sections
; s
!= NULL
; s
= s
->next
)
5777 struct elf_dyn_relocs
*p
;
5779 for (p
= elf_section_data (s
)->local_dynrel
; p
!= NULL
; p
= p
->next
)
5781 if (!bfd_is_abs_section (p
->sec
)
5782 && bfd_is_abs_section (p
->sec
->output_section
))
5784 /* Input section has been discarded, either because
5785 it is a copy of a linkonce section or due to
5786 linker script /DISCARD/, so we'll be discarding
5789 else if (p
->count
!= 0)
5791 srel
= elf_section_data (p
->sec
)->sreloc
;
5792 srel
->size
+= p
->count
* sizeof (Elf32_External_Rela
);
5793 if ((p
->sec
->output_section
->flags
& SEC_READONLY
) != 0)
5794 info
->flags
|= DF_TEXTREL
;
5799 local_got
= elf_local_got_refcounts (ibfd
);
5803 symtab_hdr
= &elf_tdata (ibfd
)->symtab_hdr
;
5804 locsymcount
= symtab_hdr
->sh_info
;
5805 end_local_got
= local_got
+ locsymcount
;
5806 local_tls_type
= elf32_nios2_local_got_tls_type (ibfd
);
5807 s
= htab
->root
.sgot
;
5808 srel
= htab
->root
.srelgot
;
5809 for (; local_got
< end_local_got
; ++local_got
, ++local_tls_type
)
5813 *local_got
= s
->size
;
5814 if (*local_tls_type
& GOT_TLS_GD
)
5815 /* TLS_GD relocs need an 8-byte structure in the GOT. */
5817 if (*local_tls_type
& GOT_TLS_IE
)
5819 if (*local_tls_type
== GOT_NORMAL
)
5822 if (bfd_link_pic (info
) || *local_tls_type
== GOT_TLS_GD
)
5823 srel
->size
+= sizeof (Elf32_External_Rela
);
5826 *local_got
= (bfd_vma
) -1;
5830 if (htab
->tls_ldm_got
.refcount
> 0)
5832 /* Allocate two GOT entries and one dynamic relocation (if necessary)
5833 for R_NIOS2_TLS_LDM16 relocations. */
5834 htab
->tls_ldm_got
.offset
= htab
->root
.sgot
->size
;
5835 htab
->root
.sgot
->size
+= 8;
5836 if (bfd_link_pic (info
))
5837 htab
->root
.srelgot
->size
+= sizeof (Elf32_External_Rela
);
5840 htab
->tls_ldm_got
.offset
= -1;
5842 /* Allocate global sym .plt and .got entries, and space for global
5843 sym dynamic relocs. */
5844 elf_link_hash_traverse (& htab
->root
, allocate_dynrelocs
, info
);
5846 if (htab
->root
.dynamic_sections_created
)
5848 /* If the .got section is more than 0x8000 bytes, we add
5849 0x8000 to the value of _gp_got, so that 16-bit relocations
5850 have a greater chance of working. */
5851 if (htab
->root
.sgot
->size
>= 0x8000
5852 && htab
->h_gp_got
->root
.u
.def
.value
== 0)
5853 htab
->h_gp_got
->root
.u
.def
.value
= 0x8000;
5856 /* The check_relocs and adjust_dynamic_symbol entry points have
5857 determined the sizes of the various dynamic sections. Allocate
5860 for (s
= dynobj
->sections
; s
!= NULL
; s
= s
->next
)
5864 if ((s
->flags
& SEC_LINKER_CREATED
) == 0)
5867 /* It's OK to base decisions on the section name, because none
5868 of the dynobj section names depend upon the input files. */
5869 name
= bfd_section_name (s
);
5871 if (CONST_STRNEQ (name
, ".rela"))
5875 if (s
!= htab
->root
.srelplt
)
5878 /* We use the reloc_count field as a counter if we need
5879 to copy relocs into the output file. */
5883 else if (s
== htab
->root
.splt
)
5885 /* Correct for the number of res_N branches. */
5886 if (s
->size
!= 0 && !bfd_link_pic (info
))
5888 htab
->res_n_size
= (s
->size
- 28) / 3;
5889 s
->size
+= htab
->res_n_size
;
5892 else if (s
!= htab
->sbss
5893 && s
!= htab
->root
.sgot
5894 && s
!= htab
->root
.sgotplt
5895 && s
!= htab
->root
.sdynbss
5896 && s
!= htab
->root
.sdynrelro
)
5897 /* It's not one of our sections, so don't allocate space. */
5902 s
->flags
|= SEC_EXCLUDE
;
5906 if ((s
->flags
& SEC_HAS_CONTENTS
) == 0)
5909 /* Allocate memory for the section contents. */
5910 s
->contents
= (bfd_byte
*) bfd_zalloc (dynobj
, s
->size
);
5911 if (s
->contents
== NULL
)
5915 /* Adjust dynamic symbols that point to the plt to account for the
5916 now-known number of resN slots. */
5917 if (htab
->res_n_size
)
5918 elf_link_hash_traverse (& htab
->root
, adjust_dynrelocs
, info
);
5920 if (htab
->root
.dynamic_sections_created
)
5922 /* Add some entries to the .dynamic section. We fill in the
5923 values later, in elf_nios2_finish_dynamic_sections, but we
5924 must add the entries now so that we get the correct size for
5925 the .dynamic section. The DT_DEBUG entry is filled in by the
5926 dynamic linker and used by the debugger. */
5927 #define add_dynamic_entry(TAG, VAL) \
5928 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
5930 if (!bfd_link_pic (info
) && !add_dynamic_entry (DT_DEBUG
, 0))
5933 if (htab
->root
.sgotplt
->size
!= 0
5934 && !add_dynamic_entry (DT_PLTGOT
, 0))
5937 if (htab
->root
.splt
->size
!= 0
5938 && (!add_dynamic_entry (DT_PLTRELSZ
, 0)
5939 || !add_dynamic_entry (DT_PLTREL
, DT_RELA
)
5940 || !add_dynamic_entry (DT_JMPREL
, 0)))
5944 && (!add_dynamic_entry (DT_RELA
, 0)
5945 || !add_dynamic_entry (DT_RELASZ
, 0)
5946 || !add_dynamic_entry (DT_RELAENT
, sizeof (Elf32_External_Rela
))))
5949 if (!bfd_link_pic (info
) && !add_dynamic_entry (DT_NIOS2_GP
, 0))
5952 if ((info
->flags
& DF_TEXTREL
) != 0
5953 && !add_dynamic_entry (DT_TEXTREL
, 0))
5956 #undef add_dynamic_entry
5961 /* Free the derived linker hash table. */
5963 nios2_elf32_link_hash_table_free (bfd
*obfd
)
5965 struct elf32_nios2_link_hash_table
*htab
5966 = (struct elf32_nios2_link_hash_table
*) obfd
->link
.hash
;
5968 bfd_hash_table_free (&htab
->bstab
);
5969 _bfd_elf_link_hash_table_free (obfd
);
5972 /* Implement bfd_elf32_bfd_link_hash_table_create. */
5973 static struct bfd_link_hash_table
*
5974 nios2_elf32_link_hash_table_create (bfd
*abfd
)
5976 struct elf32_nios2_link_hash_table
*ret
;
5977 size_t amt
= sizeof (struct elf32_nios2_link_hash_table
);
5979 ret
= bfd_zmalloc (amt
);
5983 if (!_bfd_elf_link_hash_table_init (&ret
->root
, abfd
,
5986 elf32_nios2_link_hash_entry
),
5993 /* Init the stub hash table too. */
5994 if (!bfd_hash_table_init (&ret
->bstab
, stub_hash_newfunc
,
5995 sizeof (struct elf32_nios2_stub_hash_entry
)))
5997 _bfd_elf_link_hash_table_free (abfd
);
6000 ret
->root
.root
.hash_table_free
= nios2_elf32_link_hash_table_free
;
6002 return &ret
->root
.root
;
6005 /* Implement elf_backend_reloc_type_class. */
6006 static enum elf_reloc_type_class
6007 nios2_elf32_reloc_type_class (const struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
6008 const asection
*rel_sec ATTRIBUTE_UNUSED
,
6009 const Elf_Internal_Rela
*rela
)
6011 switch ((int) ELF32_R_TYPE (rela
->r_info
))
6013 case R_NIOS2_RELATIVE
:
6014 return reloc_class_relative
;
6015 case R_NIOS2_JUMP_SLOT
:
6016 return reloc_class_plt
;
6018 return reloc_class_copy
;
6020 return reloc_class_normal
;
6024 /* Return 1 if target is one of ours. */
6026 is_nios2_elf_target (const struct bfd_target
*targ
)
6028 return (targ
== &nios2_elf32_le_vec
6029 || targ
== &nios2_elf32_be_vec
);
6032 /* Implement elf_backend_add_symbol_hook.
6033 This hook is called by the linker when adding symbols from an object
6034 file. We use it to put .comm items in .sbss, and not .bss. */
6036 nios2_elf_add_symbol_hook (bfd
*abfd
,
6037 struct bfd_link_info
*info
,
6038 Elf_Internal_Sym
*sym
,
6039 const char **namep ATTRIBUTE_UNUSED
,
6040 flagword
*flagsp ATTRIBUTE_UNUSED
,
6044 if (sym
->st_shndx
== SHN_COMMON
6045 && !bfd_link_relocatable (info
)
6046 && sym
->st_size
<= elf_gp_size (abfd
)
6047 && is_nios2_elf_target (info
->output_bfd
->xvec
))
6049 /* Common symbols less than or equal to -G nn bytes are automatically
6051 struct elf32_nios2_link_hash_table
*htab
;
6053 htab
= elf32_nios2_hash_table (info
);
6054 if (htab
->sbss
== NULL
)
6056 flagword flags
= SEC_IS_COMMON
| SEC_LINKER_CREATED
;
6058 if (htab
->root
.dynobj
== NULL
)
6059 htab
->root
.dynobj
= abfd
;
6061 htab
->sbss
= bfd_make_section_anyway_with_flags (htab
->root
.dynobj
,
6063 if (htab
->sbss
== NULL
)
6068 *valp
= sym
->st_size
;
6074 /* Implement elf_backend_can_make_relative_eh_frame:
6075 Decide whether to attempt to turn absptr or lsda encodings in
6076 shared libraries into pcrel within the given input section. */
6078 nios2_elf32_can_make_relative_eh_frame (bfd
*input_bfd ATTRIBUTE_UNUSED
,
6079 struct bfd_link_info
*info
6081 asection
*eh_frame_section
6084 /* We can't use PC-relative encodings in the .eh_frame section. */
6088 /* Implement elf_backend_special_sections. */
6089 const struct bfd_elf_special_section elf32_nios2_special_sections
[] =
6091 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS
,
6092 SHF_ALLOC
+ SHF_WRITE
+ SHF_NIOS2_GPREL
},
6093 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS
,
6094 SHF_ALLOC
+ SHF_WRITE
+ SHF_NIOS2_GPREL
},
6095 { NULL
, 0, 0, 0, 0 }
6098 #define ELF_ARCH bfd_arch_nios2
6099 #define ELF_TARGET_ID NIOS2_ELF_DATA
6100 #define ELF_MACHINE_CODE EM_ALTERA_NIOS2
6102 /* The Nios II MMU uses a 4K page size. */
6104 #define ELF_MAXPAGESIZE 0x1000
6106 #define bfd_elf32_bfd_link_hash_table_create \
6107 nios2_elf32_link_hash_table_create
6109 #define bfd_elf32_bfd_merge_private_bfd_data \
6110 nios2_elf32_merge_private_bfd_data
6112 /* Relocation table lookup macros. */
6114 #define bfd_elf32_bfd_reloc_type_lookup nios2_elf32_bfd_reloc_type_lookup
6115 #define bfd_elf32_bfd_reloc_name_lookup nios2_elf32_bfd_reloc_name_lookup
6117 /* JUMP_TABLE_LINK macros. */
6119 /* elf_info_to_howto (using RELA relocations). */
6121 #define elf_info_to_howto nios2_elf32_info_to_howto
6123 /* elf backend functions. */
6125 #define elf_backend_can_gc_sections 1
6126 #define elf_backend_can_refcount 1
6127 #define elf_backend_plt_readonly 1
6128 #define elf_backend_want_got_plt 1
6129 #define elf_backend_want_dynrelro 1
6130 #define elf_backend_rela_normal 1
6131 #define elf_backend_dtrel_excludes_plt 1
6133 #define elf_backend_relocate_section nios2_elf32_relocate_section
6134 #define elf_backend_section_flags nios2_elf32_section_flags
6135 #define elf_backend_fake_sections nios2_elf32_fake_sections
6136 #define elf_backend_check_relocs nios2_elf32_check_relocs
6138 #define elf_backend_gc_mark_hook nios2_elf32_gc_mark_hook
6139 #define elf_backend_create_dynamic_sections \
6140 nios2_elf32_create_dynamic_sections
6141 #define elf_backend_finish_dynamic_symbol nios2_elf32_finish_dynamic_symbol
6142 #define elf_backend_finish_dynamic_sections \
6143 nios2_elf32_finish_dynamic_sections
6144 #define elf_backend_adjust_dynamic_symbol nios2_elf32_adjust_dynamic_symbol
6145 #define elf_backend_reloc_type_class nios2_elf32_reloc_type_class
6146 #define elf_backend_size_dynamic_sections nios2_elf32_size_dynamic_sections
6147 #define elf_backend_add_symbol_hook nios2_elf_add_symbol_hook
6148 #define elf_backend_copy_indirect_symbol nios2_elf32_copy_indirect_symbol
6149 #define elf_backend_object_p nios2_elf32_object_p
6151 #define elf_backend_grok_prstatus nios2_grok_prstatus
6152 #define elf_backend_grok_psinfo nios2_grok_psinfo
6154 #undef elf_backend_can_make_relative_eh_frame
6155 #define elf_backend_can_make_relative_eh_frame \
6156 nios2_elf32_can_make_relative_eh_frame
6158 #define elf_backend_special_sections elf32_nios2_special_sections
6160 #define TARGET_LITTLE_SYM nios2_elf32_le_vec
6161 #define TARGET_LITTLE_NAME "elf32-littlenios2"
6162 #define TARGET_BIG_SYM nios2_elf32_be_vec
6163 #define TARGET_BIG_NAME "elf32-bignios2"
6165 #define elf_backend_got_header_size 12
6166 #define elf_backend_default_execstack 0
6168 #include "elf32-target.h"