1 /* 32-bit ELF support for Nios II.
2 Copyright (C) 2012-2015 Free Software Foundation, Inc.
3 Contributed by Nigel Gray (ngray@altera.com).
4 Contributed by Mentor Graphics, Inc.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
23 /* This file handles Altera Nios II ELF targets. */
31 #include "elf/nios2.h"
32 #include "opcode/nios2.h"
33 #include "elf32-nios2.h"
35 /* Use RELA relocations. */
44 /* Forward declarations. */
45 static bfd_reloc_status_type nios2_elf32_ignore_reloc
46 (bfd
*, arelent
*, asymbol
*, void *, asection
*, bfd
*, char **);
47 static bfd_reloc_status_type nios2_elf32_hi16_relocate
48 (bfd
*, arelent
*, asymbol
*, void *, asection
*, bfd
*, char **);
49 static bfd_reloc_status_type nios2_elf32_lo16_relocate
50 (bfd
*, arelent
*, asymbol
*, void *, asection
*, bfd
*, char **);
51 static bfd_reloc_status_type nios2_elf32_hiadj16_relocate
52 (bfd
*, arelent
*, asymbol
*, void *, asection
*, bfd
*, char **);
53 static bfd_reloc_status_type nios2_elf32_pcrel_lo16_relocate
54 (bfd
*, arelent
*, asymbol
*, void *, asection
*, bfd
*, char **);
55 static bfd_reloc_status_type nios2_elf32_pcrel_hiadj16_relocate
56 (bfd
*, arelent
*, asymbol
*, void *, asection
*, bfd
*, char **);
57 static bfd_reloc_status_type nios2_elf32_pcrel16_relocate
58 (bfd
*, arelent
*, asymbol
*, void *, asection
*, bfd
*, char **);
59 static bfd_reloc_status_type nios2_elf32_call26_relocate
60 (bfd
*, arelent
*, asymbol
*, void *, asection
*, bfd
*, char **);
61 static bfd_reloc_status_type nios2_elf32_gprel_relocate
62 (bfd
*, arelent
*, asymbol
*, void *, asection
*, bfd
*, char **);
63 static bfd_reloc_status_type nios2_elf32_ujmp_relocate
64 (bfd
*, arelent
*, asymbol
*, void *, asection
*, bfd
*, char **);
65 static bfd_reloc_status_type nios2_elf32_cjmp_relocate
66 (bfd
*, arelent
*, asymbol
*, void *, asection
*, bfd
*, char **);
67 static bfd_reloc_status_type nios2_elf32_callr_relocate
68 (bfd
*, arelent
*, asymbol
*, void *, asection
*, bfd
*, char **);
71 extern const bfd_target nios2_elf32_le_vec
;
72 extern const bfd_target nios2_elf32_be_vec
;
74 /* Offset of tp and dtp pointers from start of TLS block. */
75 #define TP_OFFSET 0x7000
76 #define DTP_OFFSET 0x8000
78 /* The relocation table used for SHT_REL sections. */
79 static reloc_howto_type elf_nios2_howto_table_rel
[] = {
81 HOWTO (R_NIOS2_NONE
, /* type */
83 3, /* size (0 = byte, 1 = short, 2 = long) */
85 FALSE
, /* pc_relative */
87 complain_overflow_dont
, /* complain_on_overflow */
88 bfd_elf_generic_reloc
, /* special_function */
89 "R_NIOS2_NONE", /* name */
90 FALSE
, /* partial_inplace */
93 FALSE
), /* pcrel_offset */
95 /* 16-bit signed immediate relocation. */
96 HOWTO (R_NIOS2_S16
, /* type */
98 2, /* size (0 = byte, 1 = short, 2 = long) */
100 FALSE
, /* pc_relative */
102 complain_overflow_signed
, /* complain on overflow */
103 bfd_elf_generic_reloc
, /* special function */
104 "R_NIOS2_S16", /* name */
105 FALSE
, /* partial_inplace */
106 0x003fffc0, /* src_mask */
107 0x003fffc0, /* dest_mask */
108 FALSE
), /* pcrel_offset */
110 /* 16-bit unsigned immediate relocation. */
111 HOWTO (R_NIOS2_U16
, /* type */
113 2, /* size (0 = byte, 1 = short, 2 = long) */
115 FALSE
, /* pc_relative */
117 complain_overflow_unsigned
, /* complain on overflow */
118 bfd_elf_generic_reloc
, /* special function */
119 "R_NIOS2_U16", /* name */
120 FALSE
, /* partial_inplace */
121 0x003fffc0, /* src_mask */
122 0x003fffc0, /* dest_mask */
123 FALSE
), /* pcrel_offset */
125 HOWTO (R_NIOS2_PCREL16
, /* type */
127 2, /* size (0 = byte, 1 = short, 2 = long) */
129 TRUE
, /* pc_relative */
131 complain_overflow_signed
, /* complain on overflow */
132 nios2_elf32_pcrel16_relocate
, /* special function */
133 "R_NIOS2_PCREL16", /* name */
134 FALSE
, /* partial_inplace */
135 0x003fffc0, /* src_mask */
136 0x003fffc0, /* dest_mask */
137 TRUE
), /* pcrel_offset */
139 HOWTO (R_NIOS2_CALL26
, /* type */
141 2, /* size (0 = byte, 1 = short, 2 = long) */
143 FALSE
, /* pc_relative */
145 complain_overflow_dont
, /* complain on overflow */
146 nios2_elf32_call26_relocate
, /* special function */
147 "R_NIOS2_CALL26", /* name */
148 FALSE
, /* partial_inplace */
149 0xffffffc0, /* src_mask */
150 0xffffffc0, /* dst_mask */
151 FALSE
), /* pcrel_offset */
159 complain_overflow_bitfield
,
160 bfd_elf_generic_reloc
,
167 HOWTO (R_NIOS2_CACHE_OPX
,
173 complain_overflow_bitfield
,
174 bfd_elf_generic_reloc
,
187 complain_overflow_bitfield
,
188 bfd_elf_generic_reloc
,
201 complain_overflow_bitfield
,
202 bfd_elf_generic_reloc
,
215 complain_overflow_dont
,
216 nios2_elf32_hi16_relocate
,
229 complain_overflow_dont
,
230 nios2_elf32_lo16_relocate
,
237 HOWTO (R_NIOS2_HIADJ16
,
243 complain_overflow_dont
,
244 nios2_elf32_hiadj16_relocate
,
251 HOWTO (R_NIOS2_BFD_RELOC_32
,
257 complain_overflow_dont
,
258 bfd_elf_generic_reloc
,
259 "R_NIOS2_BFD_RELOC32",
265 HOWTO (R_NIOS2_BFD_RELOC_16
,
271 complain_overflow_bitfield
,
272 bfd_elf_generic_reloc
,
273 "R_NIOS2_BFD_RELOC16",
279 HOWTO (R_NIOS2_BFD_RELOC_8
,
285 complain_overflow_bitfield
,
286 bfd_elf_generic_reloc
,
287 "R_NIOS2_BFD_RELOC8",
293 HOWTO (R_NIOS2_GPREL
,
299 complain_overflow_dont
,
300 nios2_elf32_gprel_relocate
,
307 HOWTO (R_NIOS2_GNU_VTINHERIT
,
313 complain_overflow_dont
,
315 "R_NIOS2_GNU_VTINHERIT",
321 HOWTO (R_NIOS2_GNU_VTENTRY
,
327 complain_overflow_dont
,
328 _bfd_elf_rel_vtable_reloc_fn
,
329 "R_NIOS2_GNU_VTENTRY",
341 complain_overflow_dont
,
342 nios2_elf32_ujmp_relocate
,
355 complain_overflow_dont
,
356 nios2_elf32_cjmp_relocate
,
363 HOWTO (R_NIOS2_CALLR
,
369 complain_overflow_dont
,
370 nios2_elf32_callr_relocate
,
377 HOWTO (R_NIOS2_ALIGN
,
383 complain_overflow_dont
,
384 nios2_elf32_ignore_reloc
,
392 HOWTO (R_NIOS2_GOT16
,
398 complain_overflow_bitfield
,
399 bfd_elf_generic_reloc
,
406 HOWTO (R_NIOS2_CALL16
,
412 complain_overflow_bitfield
,
413 bfd_elf_generic_reloc
,
420 HOWTO (R_NIOS2_GOTOFF_LO
,
426 complain_overflow_dont
,
427 bfd_elf_generic_reloc
,
434 HOWTO (R_NIOS2_GOTOFF_HA
,
440 complain_overflow_dont
,
441 bfd_elf_generic_reloc
,
448 HOWTO (R_NIOS2_PCREL_LO
,
454 complain_overflow_dont
,
455 nios2_elf32_pcrel_lo16_relocate
,
462 HOWTO (R_NIOS2_PCREL_HA
,
466 FALSE
, /* This is a PC-relative relocation, but we need to subtract
467 PC ourselves before the HIADJ. */
469 complain_overflow_dont
,
470 nios2_elf32_pcrel_hiadj16_relocate
,
477 HOWTO (R_NIOS2_TLS_GD16
,
483 complain_overflow_bitfield
,
484 bfd_elf_generic_reloc
,
491 HOWTO (R_NIOS2_TLS_LDM16
,
497 complain_overflow_bitfield
,
498 bfd_elf_generic_reloc
,
505 HOWTO (R_NIOS2_TLS_LDO16
,
511 complain_overflow_bitfield
,
512 bfd_elf_generic_reloc
,
519 HOWTO (R_NIOS2_TLS_IE16
,
525 complain_overflow_bitfield
,
526 bfd_elf_generic_reloc
,
533 HOWTO (R_NIOS2_TLS_LE16
,
539 complain_overflow_bitfield
,
540 bfd_elf_generic_reloc
,
547 HOWTO (R_NIOS2_TLS_DTPMOD
,
553 complain_overflow_dont
,
554 bfd_elf_generic_reloc
,
555 "R_NIOS2_TLS_DTPMOD",
561 HOWTO (R_NIOS2_TLS_DTPREL
,
567 complain_overflow_dont
,
568 bfd_elf_generic_reloc
,
569 "R_NIOS2_TLS_DTPREL",
575 HOWTO (R_NIOS2_TLS_TPREL
,
581 complain_overflow_dont
,
582 bfd_elf_generic_reloc
,
595 complain_overflow_dont
,
596 bfd_elf_generic_reloc
,
603 HOWTO (R_NIOS2_GLOB_DAT
,
609 complain_overflow_dont
,
610 bfd_elf_generic_reloc
,
617 HOWTO (R_NIOS2_JUMP_SLOT
,
623 complain_overflow_dont
,
624 bfd_elf_generic_reloc
,
631 HOWTO (R_NIOS2_RELATIVE
,
637 complain_overflow_dont
,
638 bfd_elf_generic_reloc
,
645 HOWTO (R_NIOS2_GOTOFF
,
651 complain_overflow_dont
,
652 bfd_elf_generic_reloc
,
659 HOWTO (R_NIOS2_CALL26_NOAT
, /* type */
661 2, /* size (0 = byte, 1 = short, 2 = long) */
663 FALSE
, /* pc_relative */
665 complain_overflow_dont
, /* complain on overflow */
666 nios2_elf32_call26_relocate
, /* special function */
667 "R_NIOS2_CALL26_NOAT", /* name */
668 FALSE
, /* partial_inplace */
669 0xffffffc0, /* src_mask */
670 0xffffffc0, /* dst_mask */
671 FALSE
), /* pcrel_offset */
673 HOWTO (R_NIOS2_GOT_LO
,
679 complain_overflow_dont
,
680 bfd_elf_generic_reloc
,
687 HOWTO (R_NIOS2_GOT_HA
,
693 complain_overflow_dont
,
694 bfd_elf_generic_reloc
,
701 HOWTO (R_NIOS2_CALL_LO
,
707 complain_overflow_dont
,
708 bfd_elf_generic_reloc
,
715 HOWTO (R_NIOS2_CALL_HA
,
721 complain_overflow_dont
,
722 bfd_elf_generic_reloc
,
729 /* Add other relocations here. */
732 static unsigned char elf_code_to_howto_index
[R_NIOS2_ILLEGAL
+ 1];
734 /* Return the howto for relocation RTYPE. */
735 static reloc_howto_type
*
736 lookup_howto (unsigned int rtype
)
738 static int initialized
= 0;
740 int howto_tbl_size
= (int) (sizeof (elf_nios2_howto_table_rel
)
741 / sizeof (elf_nios2_howto_table_rel
[0]));
746 memset (elf_code_to_howto_index
, 0xff,
747 sizeof (elf_code_to_howto_index
));
748 for (i
= 0; i
< howto_tbl_size
; i
++)
749 elf_code_to_howto_index
[elf_nios2_howto_table_rel
[i
].type
] = i
;
752 BFD_ASSERT (rtype
<= R_NIOS2_ILLEGAL
);
753 i
= elf_code_to_howto_index
[rtype
];
754 if (i
>= howto_tbl_size
)
756 return elf_nios2_howto_table_rel
+ i
;
759 /* Map for converting BFD reloc types to Nios II reloc types. */
762 bfd_reloc_code_real_type bfd_val
;
763 enum elf_nios2_reloc_type elf_val
;
766 static const struct elf_reloc_map nios2_reloc_map
[] = {
767 {BFD_RELOC_NONE
, R_NIOS2_NONE
},
768 {BFD_RELOC_NIOS2_S16
, R_NIOS2_S16
},
769 {BFD_RELOC_NIOS2_U16
, R_NIOS2_U16
},
770 {BFD_RELOC_16_PCREL
, R_NIOS2_PCREL16
},
771 {BFD_RELOC_NIOS2_CALL26
, R_NIOS2_CALL26
},
772 {BFD_RELOC_NIOS2_IMM5
, R_NIOS2_IMM5
},
773 {BFD_RELOC_NIOS2_CACHE_OPX
, R_NIOS2_CACHE_OPX
},
774 {BFD_RELOC_NIOS2_IMM6
, R_NIOS2_IMM6
},
775 {BFD_RELOC_NIOS2_IMM8
, R_NIOS2_IMM8
},
776 {BFD_RELOC_NIOS2_HI16
, R_NIOS2_HI16
},
777 {BFD_RELOC_NIOS2_LO16
, R_NIOS2_LO16
},
778 {BFD_RELOC_NIOS2_HIADJ16
, R_NIOS2_HIADJ16
},
779 {BFD_RELOC_32
, R_NIOS2_BFD_RELOC_32
},
780 {BFD_RELOC_16
, R_NIOS2_BFD_RELOC_16
},
781 {BFD_RELOC_8
, R_NIOS2_BFD_RELOC_8
},
782 {BFD_RELOC_NIOS2_GPREL
, R_NIOS2_GPREL
},
783 {BFD_RELOC_VTABLE_INHERIT
, R_NIOS2_GNU_VTINHERIT
},
784 {BFD_RELOC_VTABLE_ENTRY
, R_NIOS2_GNU_VTENTRY
},
785 {BFD_RELOC_NIOS2_UJMP
, R_NIOS2_UJMP
},
786 {BFD_RELOC_NIOS2_CJMP
, R_NIOS2_CJMP
},
787 {BFD_RELOC_NIOS2_CALLR
, R_NIOS2_CALLR
},
788 {BFD_RELOC_NIOS2_ALIGN
, R_NIOS2_ALIGN
},
789 {BFD_RELOC_NIOS2_GOT16
, R_NIOS2_GOT16
},
790 {BFD_RELOC_NIOS2_CALL16
, R_NIOS2_CALL16
},
791 {BFD_RELOC_NIOS2_GOTOFF_LO
, R_NIOS2_GOTOFF_LO
},
792 {BFD_RELOC_NIOS2_GOTOFF_HA
, R_NIOS2_GOTOFF_HA
},
793 {BFD_RELOC_NIOS2_PCREL_LO
, R_NIOS2_PCREL_LO
},
794 {BFD_RELOC_NIOS2_PCREL_HA
, R_NIOS2_PCREL_HA
},
795 {BFD_RELOC_NIOS2_TLS_GD16
, R_NIOS2_TLS_GD16
},
796 {BFD_RELOC_NIOS2_TLS_LDM16
, R_NIOS2_TLS_LDM16
},
797 {BFD_RELOC_NIOS2_TLS_LDO16
, R_NIOS2_TLS_LDO16
},
798 {BFD_RELOC_NIOS2_TLS_IE16
, R_NIOS2_TLS_IE16
},
799 {BFD_RELOC_NIOS2_TLS_LE16
, R_NIOS2_TLS_LE16
},
800 {BFD_RELOC_NIOS2_TLS_DTPMOD
, R_NIOS2_TLS_DTPMOD
},
801 {BFD_RELOC_NIOS2_TLS_DTPREL
, R_NIOS2_TLS_DTPREL
},
802 {BFD_RELOC_NIOS2_TLS_TPREL
, R_NIOS2_TLS_TPREL
},
803 {BFD_RELOC_NIOS2_COPY
, R_NIOS2_COPY
},
804 {BFD_RELOC_NIOS2_GLOB_DAT
, R_NIOS2_GLOB_DAT
},
805 {BFD_RELOC_NIOS2_JUMP_SLOT
, R_NIOS2_JUMP_SLOT
},
806 {BFD_RELOC_NIOS2_RELATIVE
, R_NIOS2_RELATIVE
},
807 {BFD_RELOC_NIOS2_GOTOFF
, R_NIOS2_GOTOFF
},
808 {BFD_RELOC_NIOS2_CALL26_NOAT
, R_NIOS2_CALL26_NOAT
},
809 {BFD_RELOC_NIOS2_GOT_LO
, R_NIOS2_GOT_LO
},
810 {BFD_RELOC_NIOS2_GOT_HA
, R_NIOS2_GOT_HA
},
811 {BFD_RELOC_NIOS2_CALL_LO
, R_NIOS2_CALL_LO
},
812 {BFD_RELOC_NIOS2_CALL_HA
, R_NIOS2_CALL_HA
},
815 enum elf32_nios2_stub_type
817 nios2_stub_call26_before
,
818 nios2_stub_call26_after
,
822 struct elf32_nios2_stub_hash_entry
824 /* Base hash table entry structure. */
825 struct bfd_hash_entry bh_root
;
827 /* The stub section. */
830 /* Offset within stub_sec of the beginning of this stub. */
833 /* Given the symbol's value and its section we can determine its final
834 value when building the stubs (so the stub knows where to jump. */
835 bfd_vma target_value
;
836 asection
*target_section
;
838 enum elf32_nios2_stub_type stub_type
;
840 /* The symbol table entry, if any, that this was derived from. */
841 struct elf32_nios2_link_hash_entry
*hh
;
843 /* And the reloc addend that this was derived from. */
846 /* Where this stub is being called from, or, in the case of combined
847 stub sections, the first input section in the group. */
851 #define nios2_stub_hash_entry(ent) \
852 ((struct elf32_nios2_stub_hash_entry *)(ent))
854 #define nios2_stub_hash_lookup(table, string, create, copy) \
855 ((struct elf32_nios2_stub_hash_entry *) \
856 bfd_hash_lookup ((table), (string), (create), (copy)))
859 /* The Nios II linker needs to keep track of the number of relocs that it
860 decides to copy as dynamic relocs in check_relocs for each symbol.
861 This is so that it can later discard them if they are found to be
862 unnecessary. We store the information in a field extending the
863 regular ELF linker hash table. */
865 struct elf32_nios2_dyn_relocs
867 struct elf32_nios2_dyn_relocs
*next
;
869 /* The input section of the reloc. */
872 /* Total number of relocs copied for the input section. */
875 /* Number of pc-relative relocs copied for the input section. */
876 bfd_size_type pc_count
;
879 /* Nios II ELF linker hash entry. */
881 struct elf32_nios2_link_hash_entry
883 struct elf_link_hash_entry root
;
885 /* A pointer to the most recently used stub hash entry against this
887 struct elf32_nios2_stub_hash_entry
*hsh_cache
;
889 /* Track dynamic relocs copied for this symbol. */
890 struct elf32_nios2_dyn_relocs
*dyn_relocs
;
892 #define GOT_UNKNOWN 0
896 unsigned char tls_type
;
898 /* We need to detect and take special action for symbols which are only
899 referenced with %call() and not with %got(). Such symbols do not need
900 a dynamic GOT reloc in shared objects, only a dynamic PLT reloc. Lazy
901 linking will not work if the dynamic GOT reloc exists.
902 To check for this condition efficiently, we compare got_types_used against
904 (got_types_used & (GOT_USED | CALL_USED)) == CALL_USED.
908 unsigned char got_types_used
;
911 #define elf32_nios2_hash_entry(ent) \
912 ((struct elf32_nios2_link_hash_entry *) (ent))
914 /* Get the Nios II elf linker hash table from a link_info structure. */
915 #define elf32_nios2_hash_table(info) \
916 ((struct elf32_nios2_link_hash_table *) ((info)->hash))
918 /* Nios II ELF linker hash table. */
919 struct elf32_nios2_link_hash_table
921 /* The main hash table. */
922 struct elf_link_hash_table root
;
924 /* The stub hash table. */
925 struct bfd_hash_table bstab
;
927 /* Linker stub bfd. */
930 /* Linker call-backs. */
931 asection
* (*add_stub_section
) (const char *, asection
*, bfd_boolean
);
932 void (*layout_sections_again
) (void);
934 /* Array to keep track of which stub sections have been created, and
935 information on stub grouping. */
938 /* These are the section to which stubs in the group will be
940 asection
*first_sec
, *last_sec
;
941 /* The stub sections. There might be stubs inserted either before
942 or after the real section.*/
943 asection
*first_stub_sec
, *last_stub_sec
;
946 /* Assorted information used by nios2_elf32_size_stubs. */
947 unsigned int bfd_count
;
949 asection
**input_list
;
950 Elf_Internal_Sym
**all_local_syms
;
952 /* Short-cuts to get to dynamic linker sections. */
957 /* GOT pointer symbol _gp_got. */
958 struct elf_link_hash_entry
*h_gp_got
;
961 bfd_signed_vma refcount
;
965 /* Small local sym cache. */
966 struct sym_cache sym_cache
;
971 struct nios2_elf32_obj_tdata
973 struct elf_obj_tdata root
;
975 /* tls_type for each local got entry. */
976 char *local_got_tls_type
;
978 /* TRUE if TLS GD relocs have been seen for this object. */
979 bfd_boolean has_tlsgd
;
982 #define elf32_nios2_tdata(abfd) \
983 ((struct nios2_elf32_obj_tdata *) (abfd)->tdata.any)
985 #define elf32_nios2_local_got_tls_type(abfd) \
986 (elf32_nios2_tdata (abfd)->local_got_tls_type)
988 /* The name of the dynamic interpreter. This is put in the .interp
990 #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
992 /* PLT implementation for position-dependent code. */
993 static const bfd_vma nios2_plt_entry
[] = { /* .PLTn: */
994 0x03c00034, /* movhi r15, %hiadj(plt_got_slot_address) */
995 0x7bc00017, /* ldw r15, %lo(plt_got_slot_address)(r15) */
996 0x7800683a /* jmp r15 */
999 static const bfd_vma nios2_plt0_entry
[] = { /* .PLTresolve */
1000 0x03800034, /* movhi r14, %hiadj(res_0) */
1001 0x73800004, /* addi r14, r14, %lo(res_0) */
1002 0x7b9fc83a, /* sub r15, r15, r14 */
1003 0x03400034, /* movhi r13, %hiadj(_GLOBAL_OFFSET_TABLE_) */
1004 0x6b800017, /* ldw r14, %lo(_GLOBAL_OFFSET_TABLE_+4)(r13) */
1005 0x6b400017, /* ldw r13, %lo(_GLOBAL_OFFSET_TABLE_+8)(r13) */
1006 0x6800683a /* jmp r13 */
1009 /* PLT implementation for position-independent code. */
1010 static const bfd_vma nios2_so_plt_entry
[] = { /* .PLTn */
1011 0x03c00034, /* movhi r15, %hiadj(index * 4) */
1012 0x7bc00004, /* addi r15, r15, %lo(index * 4) */
1013 0x00000006 /* br .PLTresolve */
1016 static const bfd_vma nios2_so_plt0_entry
[] = { /* .PLTresolve */
1017 0x001ce03a, /* nextpc r14 */
1018 0x03400034, /* movhi r13, %hiadj(_GLOBAL_OFFSET_TABLE_) */
1019 0x6b9b883a, /* add r13, r13, r14 */
1020 0x6b800017, /* ldw r14, %lo(_GLOBAL_OFFSET_TABLE_+4)(r13) */
1021 0x6b400017, /* ldw r13, %lo(_GLOBAL_OFFSET_TABLE_+8)(r13) */
1022 0x6800683a /* jmp r13 */
1026 static const bfd_vma nios2_call26_stub_entry
[] = {
1027 0x00400034, /* orhi at, r0, %hiadj(dest) */
1028 0x08400004, /* addi at, at, %lo(dest) */
1029 0x0800683a /* jmp at */
1032 /* Install 16-bit immediate value VALUE at offset OFFSET into section SEC. */
1034 nios2_elf32_install_imm16 (asection
*sec
, bfd_vma offset
, bfd_vma value
)
1036 bfd_vma word
= bfd_get_32 (sec
->owner
, sec
->contents
+ offset
);
1038 BFD_ASSERT(value
<= 0xffff);
1040 bfd_put_32 (sec
->owner
, word
| ((value
& 0xffff) << 6),
1041 sec
->contents
+ offset
);
1044 /* Install COUNT 32-bit values DATA starting at offset OFFSET into
1047 nios2_elf32_install_data (asection
*sec
, const bfd_vma
*data
, bfd_vma offset
,
1052 bfd_put_32 (sec
->owner
, *data
, sec
->contents
+ offset
);
1058 /* The usual way of loading a 32-bit constant into a Nios II register is to
1059 load the high 16 bits in one instruction and then add the low 16 bits with
1060 a signed add. This means that the high halfword needs to be adjusted to
1061 compensate for the sign bit of the low halfword. This function returns the
1062 adjusted high halfword for a given 32-bit constant. */
1064 bfd_vma
hiadj (bfd_vma symbol_value
)
1066 return ((symbol_value
+ 0x8000) >> 16) & 0xffff;
1069 /* Implement elf_backend_grok_prstatus:
1070 Support for core dump NOTE sections. */
1072 nios2_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
1077 switch (note
->descsz
)
1082 case 212: /* Linux/Nios II */
1084 elf_tdata (abfd
)->core
->signal
= bfd_get_16 (abfd
, note
->descdata
+ 12);
1087 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, note
->descdata
+ 24);
1096 /* Make a ".reg/999" section. */
1097 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
1098 size
, note
->descpos
+ offset
);
1101 /* Implement elf_backend_grok_psinfo. */
1103 nios2_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
1105 switch (note
->descsz
)
1110 case 124: /* Linux/Nios II elf_prpsinfo */
1111 elf_tdata (abfd
)->core
->program
1112 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 28, 16);
1113 elf_tdata (abfd
)->core
->command
1114 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 44, 80);
1117 /* Note that for some reason, a spurious space is tacked
1118 onto the end of the args in some (at least one anyway)
1119 implementations, so strip it off if it exists. */
1122 char *command
= elf_tdata (abfd
)->core
->command
;
1123 int n
= strlen (command
);
1125 if (0 < n
&& command
[n
- 1] == ' ')
1126 command
[n
- 1] = '\0';
1132 /* Assorted hash table functions. */
1134 /* Initialize an entry in the stub hash table. */
1135 static struct bfd_hash_entry
*
1136 stub_hash_newfunc (struct bfd_hash_entry
*entry
,
1137 struct bfd_hash_table
*table
,
1140 /* Allocate the structure if it has not already been allocated by a
1144 entry
= bfd_hash_allocate (table
,
1145 sizeof (struct elf32_nios2_stub_hash_entry
));
1150 /* Call the allocation method of the superclass. */
1151 entry
= bfd_hash_newfunc (entry
, table
, string
);
1154 struct elf32_nios2_stub_hash_entry
*hsh
;
1156 /* Initialize the local fields. */
1157 hsh
= (struct elf32_nios2_stub_hash_entry
*) entry
;
1158 hsh
->stub_sec
= NULL
;
1159 hsh
->stub_offset
= 0;
1160 hsh
->target_value
= 0;
1161 hsh
->target_section
= NULL
;
1162 hsh
->stub_type
= nios2_stub_none
;
1170 /* Create an entry in a Nios II ELF linker hash table. */
1171 static struct bfd_hash_entry
*
1172 link_hash_newfunc (struct bfd_hash_entry
*entry
,
1173 struct bfd_hash_table
*table
, const char *string
)
1175 /* Allocate the structure if it has not already been allocated by a
1179 entry
= bfd_hash_allocate (table
,
1180 sizeof (struct elf32_nios2_link_hash_entry
));
1185 /* Call the allocation method of the superclass. */
1186 entry
= _bfd_elf_link_hash_newfunc (entry
, table
, string
);
1189 struct elf32_nios2_link_hash_entry
*eh
;
1191 eh
= (struct elf32_nios2_link_hash_entry
*) entry
;
1192 eh
->hsh_cache
= NULL
;
1193 eh
->dyn_relocs
= NULL
;
1194 eh
->tls_type
= GOT_UNKNOWN
;
1195 eh
->got_types_used
= 0;
1201 /* Section name for stubs is the associated section name plus this
1203 #define STUB_SUFFIX ".stub"
1205 /* Build a name for an entry in the stub hash table. */
1207 nios2_stub_name (const asection
*input_section
,
1208 const asection
*sym_sec
,
1209 const struct elf32_nios2_link_hash_entry
*hh
,
1210 const Elf_Internal_Rela
*rel
,
1211 enum elf32_nios2_stub_type stub_type
)
1215 char stubpos
= (stub_type
== nios2_stub_call26_before
) ? 'b' : 'a';
1219 len
= 8 + 1 + 1 + 1+ strlen (hh
->root
.root
.root
.string
) + 1 + 8 + 1;
1220 stub_name
= bfd_malloc (len
);
1221 if (stub_name
!= NULL
)
1223 sprintf (stub_name
, "%08x_%c_%s+%x",
1224 input_section
->id
& 0xffffffff,
1226 hh
->root
.root
.root
.string
,
1227 (int) rel
->r_addend
& 0xffffffff);
1232 len
= 8 + 1 + 1 + 1+ 8 + 1 + 8 + 1 + 8 + 1;
1233 stub_name
= bfd_malloc (len
);
1234 if (stub_name
!= NULL
)
1236 sprintf (stub_name
, "%08x_%c_%x:%x+%x",
1237 input_section
->id
& 0xffffffff,
1239 sym_sec
->id
& 0xffffffff,
1240 (int) ELF32_R_SYM (rel
->r_info
) & 0xffffffff,
1241 (int) rel
->r_addend
& 0xffffffff);
1247 /* Look up an entry in the stub hash. Stub entries are cached because
1248 creating the stub name takes a bit of time. */
1249 static struct elf32_nios2_stub_hash_entry
*
1250 nios2_get_stub_entry (const asection
*input_section
,
1251 const asection
*sym_sec
,
1252 struct elf32_nios2_link_hash_entry
*hh
,
1253 const Elf_Internal_Rela
*rel
,
1254 struct elf32_nios2_link_hash_table
*htab
,
1255 enum elf32_nios2_stub_type stub_type
)
1257 struct elf32_nios2_stub_hash_entry
*hsh
;
1258 const asection
*id_sec
;
1260 /* If this input section is part of a group of sections sharing one
1261 stub section, then use the id of the first/last section in the group,
1262 depending on the stub section placement relative to the group.
1263 Stub names need to include a section id, as there may well be
1264 more than one stub used to reach say, printf, and we need to
1265 distinguish between them. */
1266 if (stub_type
== nios2_stub_call26_before
)
1267 id_sec
= htab
->stub_group
[input_section
->id
].first_sec
;
1269 id_sec
= htab
->stub_group
[input_section
->id
].last_sec
;
1271 if (hh
!= NULL
&& hh
->hsh_cache
!= NULL
1272 && hh
->hsh_cache
->hh
== hh
1273 && hh
->hsh_cache
->id_sec
== id_sec
1274 && hh
->hsh_cache
->stub_type
== stub_type
)
1276 hsh
= hh
->hsh_cache
;
1282 stub_name
= nios2_stub_name (id_sec
, sym_sec
, hh
, rel
, stub_type
);
1283 if (stub_name
== NULL
)
1286 hsh
= nios2_stub_hash_lookup (&htab
->bstab
,
1287 stub_name
, FALSE
, FALSE
);
1290 hh
->hsh_cache
= hsh
;
1298 /* Add a new stub entry to the stub hash. Not all fields of the new
1299 stub entry are initialised. */
1300 static struct elf32_nios2_stub_hash_entry
*
1301 nios2_add_stub (const char *stub_name
,
1303 struct elf32_nios2_link_hash_table
*htab
,
1304 enum elf32_nios2_stub_type stub_type
)
1308 asection
**secptr
, **linkptr
;
1309 struct elf32_nios2_stub_hash_entry
*hsh
;
1312 if (stub_type
== nios2_stub_call26_before
)
1314 link_sec
= htab
->stub_group
[section
->id
].first_sec
;
1315 secptr
= &(htab
->stub_group
[section
->id
].first_stub_sec
);
1316 linkptr
= &(htab
->stub_group
[link_sec
->id
].first_stub_sec
);
1321 link_sec
= htab
->stub_group
[section
->id
].last_sec
;
1322 secptr
= &(htab
->stub_group
[section
->id
].last_stub_sec
);
1323 linkptr
= &(htab
->stub_group
[link_sec
->id
].last_stub_sec
);
1327 if (stub_sec
== NULL
)
1329 stub_sec
= *linkptr
;
1330 if (stub_sec
== NULL
)
1336 namelen
= strlen (link_sec
->name
);
1337 len
= namelen
+ sizeof (STUB_SUFFIX
);
1338 s_name
= bfd_alloc (htab
->stub_bfd
, len
);
1342 memcpy (s_name
, link_sec
->name
, namelen
);
1343 memcpy (s_name
+ namelen
, STUB_SUFFIX
, sizeof (STUB_SUFFIX
));
1345 stub_sec
= (*htab
->add_stub_section
) (s_name
, link_sec
, afterp
);
1346 if (stub_sec
== NULL
)
1348 *linkptr
= stub_sec
;
1353 /* Enter this entry into the linker stub hash table. */
1354 hsh
= nios2_stub_hash_lookup (&htab
->bstab
, stub_name
,
1358 (*_bfd_error_handler
) (_("%B: cannot create stub entry %s"),
1364 hsh
->stub_sec
= stub_sec
;
1365 hsh
->stub_offset
= 0;
1366 hsh
->id_sec
= link_sec
;
1370 /* Set up various things so that we can make a list of input sections
1371 for each output section included in the link. Returns -1 on error,
1372 0 when no stubs will be needed, and 1 on success. */
1374 nios2_elf32_setup_section_lists (bfd
*output_bfd
, struct bfd_link_info
*info
)
1377 unsigned int bfd_count
;
1378 int top_id
, top_index
;
1380 asection
**input_list
, **list
;
1382 struct elf32_nios2_link_hash_table
*htab
= elf32_nios2_hash_table (info
);
1384 /* Count the number of input BFDs and find the top input section id. */
1385 for (input_bfd
= info
->input_bfds
, bfd_count
= 0, top_id
= 0;
1387 input_bfd
= input_bfd
->link
.next
)
1390 for (section
= input_bfd
->sections
;
1392 section
= section
->next
)
1394 if (top_id
< section
->id
)
1395 top_id
= section
->id
;
1399 htab
->bfd_count
= bfd_count
;
1401 amt
= sizeof (struct map_stub
) * (top_id
+ 1);
1402 htab
->stub_group
= bfd_zmalloc (amt
);
1403 if (htab
->stub_group
== NULL
)
1406 /* We can't use output_bfd->section_count here to find the top output
1407 section index as some sections may have been removed, and
1408 strip_excluded_output_sections doesn't renumber the indices. */
1409 for (section
= output_bfd
->sections
, top_index
= 0;
1411 section
= section
->next
)
1413 if (top_index
< section
->index
)
1414 top_index
= section
->index
;
1417 htab
->top_index
= top_index
;
1418 amt
= sizeof (asection
*) * (top_index
+ 1);
1419 input_list
= bfd_malloc (amt
);
1420 htab
->input_list
= input_list
;
1421 if (input_list
== NULL
)
1424 /* For sections we aren't interested in, mark their entries with a
1425 value we can check later. */
1426 list
= input_list
+ top_index
;
1428 *list
= bfd_abs_section_ptr
;
1429 while (list
-- != input_list
);
1431 for (section
= output_bfd
->sections
;
1433 section
= section
->next
)
1435 /* FIXME: This is a bit of hack. Currently our .ctors and .dtors
1436 * have PC relative relocs in them but no code flag set. */
1437 if (((section
->flags
& SEC_CODE
) != 0) ||
1438 strcmp(".ctors", section
->name
) ||
1439 strcmp(".dtors", section
->name
))
1440 input_list
[section
->index
] = NULL
;
1446 /* The linker repeatedly calls this function for each input section,
1447 in the order that input sections are linked into output sections.
1448 Build lists of input sections to determine groupings between which
1449 we may insert linker stubs. */
1451 nios2_elf32_next_input_section (struct bfd_link_info
*info
, asection
*isec
)
1453 struct elf32_nios2_link_hash_table
*htab
= elf32_nios2_hash_table (info
);
1455 if (isec
->output_section
->index
<= htab
->top_index
)
1457 asection
**list
= htab
->input_list
+ isec
->output_section
->index
;
1458 if (*list
!= bfd_abs_section_ptr
)
1460 /* Steal the last_sec pointer for our list.
1461 This happens to make the list in reverse order,
1462 which is what we want. */
1463 htab
->stub_group
[isec
->id
].last_sec
= *list
;
1469 /* Segment mask for CALL26 relocation relaxation. */
1470 #define CALL26_SEGMENT(x) ((x) & 0xf0000000)
1472 /* Fudge factor for approximate maximum size of all stubs that might
1473 be inserted by the linker. This does not actually limit the number
1474 of stubs that might be inserted, and only affects strategy for grouping
1475 and placement of stubs. Perhaps this should be computed based on number
1476 of relocations seen, or be specifiable on the command line. */
1477 #define MAX_STUB_SECTION_SIZE 0xffff
1479 /* See whether we can group stub sections together. Grouping stub
1480 sections may result in fewer stubs. More importantly, we need to
1481 put all .init* and .fini* stubs at the end of the .init or
1482 .fini output sections respectively, because glibc splits the
1483 _init and _fini functions into multiple parts. Putting a stub in
1484 the middle of a function is not a good idea.
1485 Rather than computing groups of a maximum fixed size, for Nios II
1486 CALL26 relaxation it makes more sense to compute the groups based on
1487 sections that fit within a 256MB address segment. Also do not allow
1488 a group to span more than one output section, since different output
1489 sections might correspond to different memory banks on a bare-metal
1492 group_sections (struct elf32_nios2_link_hash_table
*htab
)
1494 asection
**list
= htab
->input_list
+ htab
->top_index
;
1497 /* The list is in reverse order so we'll search backwards looking
1498 for the first section that begins in the same memory segment,
1499 marking sections along the way to point at the tail for this
1501 asection
*tail
= *list
;
1502 if (tail
== bfd_abs_section_ptr
)
1504 while (tail
!= NULL
)
1506 bfd_vma start
= tail
->output_section
->vma
+ tail
->output_offset
;
1507 bfd_vma end
= start
+ tail
->size
;
1508 bfd_vma segment
= CALL26_SEGMENT (end
);
1511 if (segment
!= CALL26_SEGMENT (start
)
1512 || segment
!= CALL26_SEGMENT (end
+ MAX_STUB_SECTION_SIZE
))
1513 /* This section spans more than one memory segment, or is
1514 close enough to the end of the segment that adding stub
1515 sections before it might cause it to move so that it
1516 spans memory segments, or that stubs added at the end of
1517 this group might overflow into the next memory segment.
1518 Put it in a group by itself to localize the effects. */
1520 prev
= htab
->stub_group
[tail
->id
].last_sec
;
1521 htab
->stub_group
[tail
->id
].last_sec
= tail
;
1522 htab
->stub_group
[tail
->id
].first_sec
= tail
;
1525 /* Collect more sections for this group. */
1527 asection
*curr
, *first
;
1528 for (curr
= tail
; ; curr
= prev
)
1530 prev
= htab
->stub_group
[curr
->id
].last_sec
;
1532 || tail
->output_section
!= prev
->output_section
1533 || (CALL26_SEGMENT (prev
->output_section
->vma
1534 + prev
->output_offset
)
1539 for (curr
= tail
; ; curr
= prev
)
1541 prev
= htab
->stub_group
[curr
->id
].last_sec
;
1542 htab
->stub_group
[curr
->id
].last_sec
= tail
;
1543 htab
->stub_group
[curr
->id
].first_sec
= first
;
1549 /* Reset tail for the next group. */
1553 while (list
-- != htab
->input_list
);
1554 free (htab
->input_list
);
1557 /* Determine the type of stub needed, if any, for a call. */
1558 static enum elf32_nios2_stub_type
1559 nios2_type_of_stub (asection
*input_sec
,
1560 const Elf_Internal_Rela
*rel
,
1561 struct elf32_nios2_link_hash_entry
*hh
,
1562 struct elf32_nios2_link_hash_table
*htab
,
1563 bfd_vma destination
,
1564 struct bfd_link_info
*info ATTRIBUTE_UNUSED
)
1566 bfd_vma location
, segment
, start
, end
;
1567 asection
*s0
, *s1
, *s
;
1570 !(hh
->root
.root
.type
== bfd_link_hash_defined
1571 || hh
->root
.root
.type
== bfd_link_hash_defweak
))
1572 return nios2_stub_none
;
1574 /* Determine where the call point is. */
1575 location
= (input_sec
->output_section
->vma
1576 + input_sec
->output_offset
+ rel
->r_offset
);
1577 segment
= CALL26_SEGMENT (location
);
1579 /* Nios II CALL and JMPI instructions can transfer control to addresses
1580 within the same 256MB segment as the PC. */
1581 if (segment
== CALL26_SEGMENT (destination
))
1582 return nios2_stub_none
;
1584 /* Find the start and end addresses of the stub group. Also account for
1585 any already-created stub sections for this group. Note that for stubs
1586 in the end section, only the first instruction of the last stub
1587 (12 bytes long) needs to be within range. */
1588 s0
= htab
->stub_group
[input_sec
->id
].first_sec
;
1589 s
= htab
->stub_group
[s0
->id
].first_stub_sec
;
1590 if (s
!= NULL
&& s
->size
> 0)
1591 start
= s
->output_section
->vma
+ s
->output_offset
;
1593 start
= s0
->output_section
->vma
+ s0
->output_offset
;
1595 s1
= htab
->stub_group
[input_sec
->id
].last_sec
;
1596 s
= htab
->stub_group
[s1
->id
].last_stub_sec
;
1597 if (s
!= NULL
&& s
->size
> 0)
1598 end
= s
->output_section
->vma
+ s
->output_offset
+ s
->size
- 8;
1600 end
= s1
->output_section
->vma
+ s1
->output_offset
+ s1
->size
;
1602 BFD_ASSERT (start
< end
);
1603 BFD_ASSERT (start
<= location
);
1604 BFD_ASSERT (location
< end
);
1606 /* Put stubs at the end of the group unless that is not a valid
1607 location and the beginning of the group is. It might be that
1608 neither the beginning nor end works if we have an input section
1609 so large that it spans multiple segment boundaries. In that
1610 case, punt; the end result will be a relocation overflow error no
1611 matter what we do here.
1613 Note that adding stubs pushes up the addresses of all subsequent
1614 sections, so that stubs allocated on one pass through the
1615 relaxation loop may not be valid on the next pass. (E.g., we may
1616 allocate a stub at the beginning of the section on one pass and
1617 find that the call site has been bumped into the next memory
1618 segment on the next pass.) The important thing to note is that
1619 we never try to reclaim the space allocated to such unused stubs,
1620 so code size and section addresses can only increase with each
1621 iteration. Accounting for the start and end addresses of the
1622 already-created stub sections ensures that when the algorithm
1623 converges, it converges accurately, with the entire appropriate
1624 stub section accessible from the call site and not just the
1625 address at the start or end of the stub group proper. */
1627 if (segment
== CALL26_SEGMENT (end
))
1628 return nios2_stub_call26_after
;
1629 else if (segment
== CALL26_SEGMENT (start
))
1630 return nios2_stub_call26_before
;
1632 /* Perhaps this should be a dedicated error code. */
1633 return nios2_stub_none
;
1637 nios2_build_one_stub (struct bfd_hash_entry
*gen_entry
, void *in_arg ATTRIBUTE_UNUSED
)
1639 struct elf32_nios2_stub_hash_entry
*hsh
1640 = (struct elf32_nios2_stub_hash_entry
*) gen_entry
;
1641 asection
*stub_sec
= hsh
->stub_sec
;
1644 /* Make a note of the offset within the stubs for this entry. */
1645 hsh
->stub_offset
= stub_sec
->size
;
1647 switch (hsh
->stub_type
)
1649 case nios2_stub_call26_before
:
1650 case nios2_stub_call26_after
:
1651 /* A call26 stub looks like:
1652 orhi at, %hiadj(dest)
1653 addi at, at, %lo(dest)
1655 Note that call/jmpi instructions can't be used in PIC code
1656 so there is no reason for the stub to be PIC, either. */
1657 sym_value
= (hsh
->target_value
1658 + hsh
->target_section
->output_offset
1659 + hsh
->target_section
->output_section
->vma
1662 nios2_elf32_install_data (stub_sec
, nios2_call26_stub_entry
,
1663 hsh
->stub_offset
, 3);
1664 nios2_elf32_install_imm16 (stub_sec
, hsh
->stub_offset
,
1666 nios2_elf32_install_imm16 (stub_sec
, hsh
->stub_offset
+ 4,
1667 (sym_value
& 0xffff));
1668 stub_sec
->size
+= 12;
1678 /* As above, but don't actually build the stub. Just bump offset so
1679 we know stub section sizes. */
1681 nios2_size_one_stub (struct bfd_hash_entry
*gen_entry
, void *in_arg ATTRIBUTE_UNUSED
)
1683 struct elf32_nios2_stub_hash_entry
*hsh
1684 = (struct elf32_nios2_stub_hash_entry
*) gen_entry
;
1686 switch (hsh
->stub_type
)
1688 case nios2_stub_call26_before
:
1689 case nios2_stub_call26_after
:
1690 hsh
->stub_sec
->size
+= 12;
1699 /* Read in all local syms for all input bfds.
1700 Returns -1 on error, 0 otherwise. */
1703 get_local_syms (bfd
*output_bfd ATTRIBUTE_UNUSED
, bfd
*input_bfd
,
1704 struct bfd_link_info
*info
)
1706 unsigned int bfd_indx
;
1707 Elf_Internal_Sym
*local_syms
, **all_local_syms
;
1708 struct elf32_nios2_link_hash_table
*htab
= elf32_nios2_hash_table (info
);
1710 /* We want to read in symbol extension records only once. To do this
1711 we need to read in the local symbols in parallel and save them for
1712 later use; so hold pointers to the local symbols in an array. */
1713 bfd_size_type amt
= sizeof (Elf_Internal_Sym
*) * htab
->bfd_count
;
1714 all_local_syms
= bfd_zmalloc (amt
);
1715 htab
->all_local_syms
= all_local_syms
;
1716 if (all_local_syms
== NULL
)
1719 /* Walk over all the input BFDs, swapping in local symbols. */
1722 input_bfd
= input_bfd
->link
.next
, bfd_indx
++)
1724 Elf_Internal_Shdr
*symtab_hdr
;
1726 /* We'll need the symbol table in a second. */
1727 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
1728 if (symtab_hdr
->sh_info
== 0)
1731 /* We need an array of the local symbols attached to the input bfd. */
1732 local_syms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
1733 if (local_syms
== NULL
)
1735 local_syms
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
,
1736 symtab_hdr
->sh_info
, 0,
1738 /* Cache them for elf_link_input_bfd. */
1739 symtab_hdr
->contents
= (unsigned char *) local_syms
;
1741 if (local_syms
== NULL
)
1744 all_local_syms
[bfd_indx
] = local_syms
;
1750 /* Determine and set the size of the stub section for a final link. */
1752 nios2_elf32_size_stubs (bfd
*output_bfd
, bfd
*stub_bfd
,
1753 struct bfd_link_info
*info
,
1754 asection
*(*add_stub_section
) (const char *,
1755 asection
*, bfd_boolean
),
1756 void (*layout_sections_again
) (void))
1758 bfd_boolean stub_changed
= FALSE
;
1759 struct elf32_nios2_link_hash_table
*htab
= elf32_nios2_hash_table (info
);
1761 /* Stash our params away. */
1762 htab
->stub_bfd
= stub_bfd
;
1763 htab
->add_stub_section
= add_stub_section
;
1764 htab
->layout_sections_again
= layout_sections_again
;
1766 /* FIXME: We only compute the section groups once. This could cause
1767 problems if adding a large stub section causes following sections,
1768 or parts of them, to move into another segment. However, this seems
1769 to be consistent with the way other back ends handle this.... */
1770 group_sections (htab
);
1772 if (get_local_syms (output_bfd
, info
->input_bfds
, info
))
1774 if (htab
->all_local_syms
)
1775 goto error_ret_free_local
;
1782 unsigned int bfd_indx
;
1785 for (input_bfd
= info
->input_bfds
, bfd_indx
= 0;
1787 input_bfd
= input_bfd
->link
.next
, bfd_indx
++)
1789 Elf_Internal_Shdr
*symtab_hdr
;
1791 Elf_Internal_Sym
*local_syms
;
1793 /* We'll need the symbol table in a second. */
1794 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
1795 if (symtab_hdr
->sh_info
== 0)
1798 local_syms
= htab
->all_local_syms
[bfd_indx
];
1800 /* Walk over each section attached to the input bfd. */
1801 for (section
= input_bfd
->sections
;
1803 section
= section
->next
)
1805 Elf_Internal_Rela
*internal_relocs
, *irelaend
, *irela
;
1807 /* If there aren't any relocs, then there's nothing more
1809 if ((section
->flags
& SEC_RELOC
) == 0
1810 || section
->reloc_count
== 0)
1813 /* If this section is a link-once section that will be
1814 discarded, then don't create any stubs. */
1815 if (section
->output_section
== NULL
1816 || section
->output_section
->owner
!= output_bfd
)
1819 /* Get the relocs. */
1821 = _bfd_elf_link_read_relocs (input_bfd
, section
, NULL
, NULL
,
1823 if (internal_relocs
== NULL
)
1824 goto error_ret_free_local
;
1826 /* Now examine each relocation. */
1827 irela
= internal_relocs
;
1828 irelaend
= irela
+ section
->reloc_count
;
1829 for (; irela
< irelaend
; irela
++)
1831 unsigned int r_type
, r_indx
;
1832 enum elf32_nios2_stub_type stub_type
;
1833 struct elf32_nios2_stub_hash_entry
*hsh
;
1836 bfd_vma destination
;
1837 struct elf32_nios2_link_hash_entry
*hh
;
1839 const asection
*id_sec
;
1841 r_type
= ELF32_R_TYPE (irela
->r_info
);
1842 r_indx
= ELF32_R_SYM (irela
->r_info
);
1844 if (r_type
>= (unsigned int) R_NIOS2_ILLEGAL
)
1846 bfd_set_error (bfd_error_bad_value
);
1847 error_ret_free_internal
:
1848 if (elf_section_data (section
)->relocs
== NULL
)
1849 free (internal_relocs
);
1850 goto error_ret_free_local
;
1853 /* Only look for stubs on CALL and JMPI instructions. */
1854 if (r_type
!= (unsigned int) R_NIOS2_CALL26
)
1857 /* Now determine the call target, its name, value,
1863 if (r_indx
< symtab_hdr
->sh_info
)
1865 /* It's a local symbol. */
1866 Elf_Internal_Sym
*sym
;
1867 Elf_Internal_Shdr
*hdr
;
1870 sym
= local_syms
+ r_indx
;
1871 if (ELF_ST_TYPE (sym
->st_info
) != STT_SECTION
)
1872 sym_value
= sym
->st_value
;
1873 shndx
= sym
->st_shndx
;
1874 if (shndx
< elf_numsections (input_bfd
))
1876 hdr
= elf_elfsections (input_bfd
)[shndx
];
1877 sym_sec
= hdr
->bfd_section
;
1878 destination
= (sym_value
+ irela
->r_addend
1879 + sym_sec
->output_offset
1880 + sym_sec
->output_section
->vma
);
1885 /* It's an external symbol. */
1888 e_indx
= r_indx
- symtab_hdr
->sh_info
;
1889 hh
= ((struct elf32_nios2_link_hash_entry
*)
1890 elf_sym_hashes (input_bfd
)[e_indx
]);
1892 while (hh
->root
.root
.type
== bfd_link_hash_indirect
1893 || hh
->root
.root
.type
== bfd_link_hash_warning
)
1894 hh
= ((struct elf32_nios2_link_hash_entry
*)
1895 hh
->root
.root
.u
.i
.link
);
1897 if (hh
->root
.root
.type
== bfd_link_hash_defined
1898 || hh
->root
.root
.type
== bfd_link_hash_defweak
)
1900 sym_sec
= hh
->root
.root
.u
.def
.section
;
1901 sym_value
= hh
->root
.root
.u
.def
.value
;
1903 if (sym_sec
->output_section
!= NULL
)
1904 destination
= (sym_value
+ irela
->r_addend
1905 + sym_sec
->output_offset
1906 + sym_sec
->output_section
->vma
);
1910 else if (hh
->root
.root
.type
== bfd_link_hash_undefweak
)
1915 else if (hh
->root
.root
.type
== bfd_link_hash_undefined
)
1917 if (! (info
->unresolved_syms_in_objects
== RM_IGNORE
1918 && (ELF_ST_VISIBILITY (hh
->root
.other
)
1924 bfd_set_error (bfd_error_bad_value
);
1925 goto error_ret_free_internal
;
1929 /* Determine what (if any) linker stub is needed. */
1930 stub_type
= nios2_type_of_stub (section
, irela
, hh
, htab
,
1932 if (stub_type
== nios2_stub_none
)
1935 /* Support for grouping stub sections. */
1936 if (stub_type
== nios2_stub_call26_before
)
1937 id_sec
= htab
->stub_group
[section
->id
].first_sec
;
1939 id_sec
= htab
->stub_group
[section
->id
].last_sec
;
1941 /* Get the name of this stub. */
1942 stub_name
= nios2_stub_name (id_sec
, sym_sec
, hh
, irela
,
1945 goto error_ret_free_internal
;
1947 hsh
= nios2_stub_hash_lookup (&htab
->bstab
,
1952 /* The proper stub has already been created. */
1957 hsh
= nios2_add_stub (stub_name
, section
, htab
, stub_type
);
1961 goto error_ret_free_internal
;
1963 hsh
->target_value
= sym_value
;
1964 hsh
->target_section
= sym_sec
;
1965 hsh
->stub_type
= stub_type
;
1967 hsh
->addend
= irela
->r_addend
;
1968 stub_changed
= TRUE
;
1971 /* We're done with the internal relocs, free them. */
1972 if (elf_section_data (section
)->relocs
== NULL
)
1973 free (internal_relocs
);
1980 /* OK, we've added some stubs. Find out the new size of the
1982 for (stub_sec
= htab
->stub_bfd
->sections
;
1984 stub_sec
= stub_sec
->next
)
1987 bfd_hash_traverse (&htab
->bstab
, nios2_size_one_stub
, htab
);
1989 /* Ask the linker to do its stuff. */
1990 (*htab
->layout_sections_again
) ();
1991 stub_changed
= FALSE
;
1994 free (htab
->all_local_syms
);
1997 error_ret_free_local
:
1998 free (htab
->all_local_syms
);
2002 /* Build all the stubs associated with the current output file. The
2003 stubs are kept in a hash table attached to the main linker hash
2004 table. This function is called via nios2elf_finish in the linker. */
2006 nios2_elf32_build_stubs (struct bfd_link_info
*info
)
2009 struct bfd_hash_table
*table
;
2010 struct elf32_nios2_link_hash_table
*htab
;
2012 htab
= elf32_nios2_hash_table (info
);
2014 for (stub_sec
= htab
->stub_bfd
->sections
;
2016 stub_sec
= stub_sec
->next
)
2017 /* The stub_bfd may contain non-stub sections if it is also the
2018 dynobj. Any such non-stub sections are created with the
2019 SEC_LINKER_CREATED flag set, while stub sections do not
2020 have that flag. Ignore any non-stub sections here. */
2021 if ((stub_sec
->flags
& SEC_LINKER_CREATED
) == 0)
2025 /* Allocate memory to hold the linker stubs. */
2026 size
= stub_sec
->size
;
2027 stub_sec
->contents
= bfd_zalloc (htab
->stub_bfd
, size
);
2028 if (stub_sec
->contents
== NULL
&& size
!= 0)
2033 /* Build the stubs as directed by the stub hash table. */
2034 table
= &htab
->bstab
;
2035 bfd_hash_traverse (table
, nios2_build_one_stub
, info
);
2041 /* Implement bfd_elf32_bfd_reloc_type_lookup:
2042 Given a BFD reloc type, return a howto structure. */
2043 static reloc_howto_type
*
2044 nios2_elf32_bfd_reloc_type_lookup (bfd
*abfd ATTRIBUTE_UNUSED
,
2045 bfd_reloc_code_real_type code
)
2049 i
< (int) (sizeof (nios2_reloc_map
) / sizeof (struct elf_reloc_map
));
2051 if (nios2_reloc_map
[i
].bfd_val
== code
)
2052 return &elf_nios2_howto_table_rel
[(int) nios2_reloc_map
[i
].elf_val
];
2056 /* Implement bfd_elf32_bfd_reloc_name_lookup:
2057 Given a reloc name, return a howto structure. */
2058 static reloc_howto_type
*
2059 nios2_elf32_bfd_reloc_name_lookup (bfd
*abfd ATTRIBUTE_UNUSED
,
2064 i
< (sizeof (elf_nios2_howto_table_rel
)
2065 / sizeof (elf_nios2_howto_table_rel
[0]));
2067 if (elf_nios2_howto_table_rel
[i
].name
2068 && strcasecmp (elf_nios2_howto_table_rel
[i
].name
, r_name
) == 0)
2069 return &elf_nios2_howto_table_rel
[i
];
2074 /* Implement elf_info_to_howto:
2075 Given a ELF32 relocation, fill in a arelent structure. */
2077 nios2_elf32_info_to_howto (bfd
*abfd ATTRIBUTE_UNUSED
, arelent
*cache_ptr
,
2078 Elf_Internal_Rela
*dst
)
2080 unsigned int r_type
;
2082 r_type
= ELF32_R_TYPE (dst
->r_info
);
2083 BFD_ASSERT (r_type
< R_NIOS2_ILLEGAL
);
2084 cache_ptr
->howto
= &elf_nios2_howto_table_rel
[r_type
];
2087 /* Return the base VMA address which should be subtracted from real addresses
2088 when resolving @dtpoff relocation.
2089 This is PT_TLS segment p_vaddr. */
2091 dtpoff_base (struct bfd_link_info
*info
)
2093 /* If tls_sec is NULL, we should have signalled an error already. */
2094 if (elf_hash_table (info
)->tls_sec
== NULL
)
2096 return elf_hash_table (info
)->tls_sec
->vma
;
2099 /* Return the relocation value for @tpoff relocation
2100 if STT_TLS virtual address is ADDRESS. */
2102 tpoff (struct bfd_link_info
*info
, bfd_vma address
)
2104 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
2106 /* If tls_sec is NULL, we should have signalled an error already. */
2107 if (htab
->tls_sec
== NULL
)
2109 return address
- htab
->tls_sec
->vma
;
2112 /* Set the GP value for OUTPUT_BFD. Returns FALSE if this is a
2113 dangerous relocation. */
2115 nios2_elf_assign_gp (bfd
*output_bfd
, bfd_vma
*pgp
, struct bfd_link_info
*info
)
2118 bfd_boolean gp_found
;
2119 struct bfd_hash_entry
*h
;
2120 struct bfd_link_hash_entry
*lh
;
2122 /* If we've already figured out what GP will be, just return it. */
2123 *pgp
= _bfd_get_gp_value (output_bfd
);
2127 h
= bfd_hash_lookup (&info
->hash
->table
, "_gp", FALSE
, FALSE
);
2128 lh
= (struct bfd_link_hash_entry
*) h
;
2134 case bfd_link_hash_undefined
:
2135 case bfd_link_hash_undefweak
:
2136 case bfd_link_hash_common
:
2139 case bfd_link_hash_defined
:
2140 case bfd_link_hash_defweak
:
2142 *pgp
= lh
->u
.def
.value
;
2144 case bfd_link_hash_indirect
:
2145 case bfd_link_hash_warning
:
2147 /* @@FIXME ignoring warning for now */
2149 case bfd_link_hash_new
:
2159 /* Only get the error once. */
2161 _bfd_set_gp_value (output_bfd
, *pgp
);
2165 _bfd_set_gp_value (output_bfd
, *pgp
);
2170 /* Retrieve the previously cached _gp pointer, returning bfd_reloc_dangerous
2171 if it's not available as we don't have a link_info pointer available here
2172 to look it up in the output symbol table. We don't need to adjust the
2173 symbol value for an external symbol if we are producing relocatable
2175 static bfd_reloc_status_type
2176 nios2_elf_final_gp (bfd
*output_bfd
, asymbol
*symbol
, bfd_boolean relocatable
,
2177 char **error_message
, bfd_vma
*pgp
)
2179 if (bfd_is_und_section (symbol
->section
) && !relocatable
)
2182 return bfd_reloc_undefined
;
2185 *pgp
= _bfd_get_gp_value (output_bfd
);
2186 if (*pgp
== 0 && (!relocatable
|| (symbol
->flags
& BSF_SECTION_SYM
) != 0))
2190 /* Make up a value. */
2191 *pgp
= symbol
->section
->output_section
->vma
+ 0x4000;
2192 _bfd_set_gp_value (output_bfd
, *pgp
);
2197 = (char *) _("global pointer relative relocation when _gp not defined");
2198 return bfd_reloc_dangerous
;
2202 return bfd_reloc_ok
;
2205 /* Do the relocations that require special handling. */
2206 static bfd_reloc_status_type
2207 nios2_elf32_do_hi16_relocate (bfd
*abfd
, reloc_howto_type
*howto
,
2208 asection
*input_section
,
2209 bfd_byte
*data
, bfd_vma offset
,
2210 bfd_vma symbol_value
, bfd_vma addend
)
2212 symbol_value
= symbol_value
+ addend
;
2214 symbol_value
= (symbol_value
>> 16) & 0xffff;
2215 return _bfd_final_link_relocate (howto
, abfd
, input_section
,
2216 data
, offset
, symbol_value
, addend
);
2219 static bfd_reloc_status_type
2220 nios2_elf32_do_lo16_relocate (bfd
*abfd
, reloc_howto_type
*howto
,
2221 asection
*input_section
,
2222 bfd_byte
*data
, bfd_vma offset
,
2223 bfd_vma symbol_value
, bfd_vma addend
)
2225 symbol_value
= symbol_value
+ addend
;
2227 symbol_value
= symbol_value
& 0xffff;
2228 return _bfd_final_link_relocate (howto
, abfd
, input_section
,
2229 data
, offset
, symbol_value
, addend
);
2232 static bfd_reloc_status_type
2233 nios2_elf32_do_hiadj16_relocate (bfd
*abfd
, reloc_howto_type
*howto
,
2234 asection
*input_section
,
2235 bfd_byte
*data
, bfd_vma offset
,
2236 bfd_vma symbol_value
, bfd_vma addend
)
2238 symbol_value
= symbol_value
+ addend
;
2240 symbol_value
= hiadj(symbol_value
);
2241 return _bfd_final_link_relocate (howto
, abfd
, input_section
, data
, offset
,
2242 symbol_value
, addend
);
2245 static bfd_reloc_status_type
2246 nios2_elf32_do_pcrel_lo16_relocate (bfd
*abfd
, reloc_howto_type
*howto
,
2247 asection
*input_section
,
2248 bfd_byte
*data
, bfd_vma offset
,
2249 bfd_vma symbol_value
, bfd_vma addend
)
2251 symbol_value
= symbol_value
+ addend
;
2253 symbol_value
= symbol_value
& 0xffff;
2254 return _bfd_final_link_relocate (howto
, abfd
, input_section
,
2255 data
, offset
, symbol_value
, addend
);
2258 static bfd_reloc_status_type
2259 nios2_elf32_do_pcrel_hiadj16_relocate (bfd
*abfd
, reloc_howto_type
*howto
,
2260 asection
*input_section
,
2261 bfd_byte
*data
, bfd_vma offset
,
2262 bfd_vma symbol_value
, bfd_vma addend
)
2264 symbol_value
= symbol_value
+ addend
;
2265 symbol_value
-= (input_section
->output_section
->vma
2266 + input_section
->output_offset
);
2267 symbol_value
-= offset
;
2269 symbol_value
= hiadj(symbol_value
);
2270 return _bfd_final_link_relocate (howto
, abfd
, input_section
, data
, offset
,
2271 symbol_value
, addend
);
2274 static bfd_reloc_status_type
2275 nios2_elf32_do_pcrel16_relocate (bfd
*abfd
, reloc_howto_type
*howto
,
2276 asection
*input_section
,
2277 bfd_byte
*data
, bfd_vma offset
,
2278 bfd_vma symbol_value
, bfd_vma addend
)
2280 /* NIOS2 pc relative relocations are relative to the next 32-bit instruction
2281 so we need to subtract 4 before doing a final_link_relocate. */
2282 symbol_value
= symbol_value
+ addend
- 4;
2284 return _bfd_final_link_relocate (howto
, abfd
, input_section
,
2285 data
, offset
, symbol_value
, addend
);
2288 static bfd_reloc_status_type
2289 nios2_elf32_do_call26_relocate (bfd
*abfd
, reloc_howto_type
*howto
,
2290 asection
*input_section
,
2291 bfd_byte
*data
, bfd_vma offset
,
2292 bfd_vma symbol_value
, bfd_vma addend
)
2294 /* Check that the relocation is in the same page as the current address. */
2295 if (CALL26_SEGMENT (symbol_value
+ addend
)
2296 != CALL26_SEGMENT (input_section
->output_section
->vma
2297 + input_section
->output_offset
2299 return bfd_reloc_overflow
;
2301 return _bfd_final_link_relocate (howto
, abfd
, input_section
,
2302 data
, offset
, symbol_value
, addend
);
2305 static bfd_reloc_status_type
2306 nios2_elf32_do_gprel_relocate (bfd
*abfd
, reloc_howto_type
*howto
,
2307 asection
*input_section
,
2308 bfd_byte
*data
, bfd_vma offset
,
2309 bfd_vma symbol_value
, bfd_vma addend
)
2311 /* Because we need the output_bfd, the special handling is done
2312 in nios2_elf32_relocate_section or in nios2_elf32_gprel_relocate. */
2313 return _bfd_final_link_relocate (howto
, abfd
, input_section
,
2314 data
, offset
, symbol_value
, addend
);
2317 static bfd_reloc_status_type
2318 nios2_elf32_do_ujmp_relocate (bfd
*abfd
, reloc_howto_type
*howto
,
2319 asection
*input_section
,
2320 bfd_byte
*data
, bfd_vma offset
,
2321 bfd_vma symbol_value
, bfd_vma addend
)
2323 bfd_vma symbol_lo16
, symbol_hi16
;
2324 bfd_reloc_status_type r
;
2325 symbol_value
= symbol_value
+ addend
;
2327 symbol_hi16
= (symbol_value
>> 16) & 0xffff;
2328 symbol_lo16
= symbol_value
& 0xffff;
2330 r
= _bfd_final_link_relocate (howto
, abfd
, input_section
,
2331 data
, offset
, symbol_hi16
, addend
);
2333 if (r
== bfd_reloc_ok
)
2334 return _bfd_final_link_relocate (howto
, abfd
, input_section
,
2335 data
, offset
+ 4, symbol_lo16
, addend
);
2340 static bfd_reloc_status_type
2341 nios2_elf32_do_cjmp_relocate (bfd
*abfd
, reloc_howto_type
*howto
,
2342 asection
*input_section
,
2343 bfd_byte
*data
, bfd_vma offset
,
2344 bfd_vma symbol_value
, bfd_vma addend
)
2346 bfd_vma symbol_lo16
, symbol_hi16
;
2347 bfd_reloc_status_type r
;
2348 symbol_value
= symbol_value
+ addend
;
2350 symbol_hi16
= (symbol_value
>> 16) & 0xffff;
2351 symbol_lo16
= symbol_value
& 0xffff;
2353 r
= _bfd_final_link_relocate (howto
, abfd
, input_section
,
2354 data
, offset
, symbol_hi16
, addend
);
2356 if (r
== bfd_reloc_ok
)
2357 return _bfd_final_link_relocate (howto
, abfd
, input_section
,
2358 data
, offset
+ 4, symbol_lo16
, addend
);
2363 static bfd_reloc_status_type
2364 nios2_elf32_do_callr_relocate (bfd
*abfd
, reloc_howto_type
*howto
,
2365 asection
*input_section
,
2366 bfd_byte
*data
, bfd_vma offset
,
2367 bfd_vma symbol_value
, bfd_vma addend
)
2369 bfd_vma symbol_lo16
, symbol_hi16
;
2370 bfd_reloc_status_type r
;
2371 symbol_value
= symbol_value
+ addend
;
2373 symbol_hi16
= (symbol_value
>> 16) & 0xffff;
2374 symbol_lo16
= symbol_value
& 0xffff;
2376 r
= _bfd_final_link_relocate (howto
, abfd
, input_section
,
2377 data
, offset
, symbol_hi16
, addend
);
2379 if (r
== bfd_reloc_ok
)
2380 return _bfd_final_link_relocate (howto
, abfd
, input_section
,
2381 data
, offset
+ 4, symbol_lo16
, addend
);
2386 /* HOWTO handlers for relocations that require special handling. */
2388 /* This is for relocations used only when relaxing to ensure
2389 changes in size of section don't screw up .align. */
2390 static bfd_reloc_status_type
2391 nios2_elf32_ignore_reloc (bfd
*abfd ATTRIBUTE_UNUSED
, arelent
*reloc_entry
,
2392 asymbol
*symbol ATTRIBUTE_UNUSED
,
2393 void *data ATTRIBUTE_UNUSED
, asection
*input_section
,
2395 char **error_message ATTRIBUTE_UNUSED
)
2397 if (output_bfd
!= NULL
)
2398 reloc_entry
->address
+= input_section
->output_offset
;
2399 return bfd_reloc_ok
;
2402 static bfd_reloc_status_type
2403 nios2_elf32_hi16_relocate (bfd
*abfd
, arelent
*reloc_entry
, asymbol
*symbol
,
2404 void *data
, asection
*input_section
,
2406 char **error_message ATTRIBUTE_UNUSED
)
2408 /* This part is from bfd_elf_generic_reloc. */
2409 if (output_bfd
!= NULL
2410 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
2411 && (!reloc_entry
->howto
->partial_inplace
|| reloc_entry
->addend
== 0))
2413 reloc_entry
->address
+= input_section
->output_offset
;
2414 return bfd_reloc_ok
;
2417 if (output_bfd
!= NULL
)
2418 /* FIXME: See bfd_perform_relocation. Is this right? */
2419 return bfd_reloc_continue
;
2421 return nios2_elf32_do_hi16_relocate (abfd
, reloc_entry
->howto
,
2423 data
, reloc_entry
->address
,
2425 + symbol
->section
->output_section
->vma
2426 + symbol
->section
->output_offset
),
2427 reloc_entry
->addend
);
2430 static bfd_reloc_status_type
2431 nios2_elf32_lo16_relocate (bfd
*abfd
, arelent
*reloc_entry
, asymbol
*symbol
,
2432 void *data
, asection
*input_section
,
2434 char **error_message ATTRIBUTE_UNUSED
)
2436 /* This part is from bfd_elf_generic_reloc. */
2437 if (output_bfd
!= NULL
2438 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
2439 && (!reloc_entry
->howto
->partial_inplace
|| reloc_entry
->addend
== 0))
2441 reloc_entry
->address
+= input_section
->output_offset
;
2442 return bfd_reloc_ok
;
2445 if (output_bfd
!= NULL
)
2446 /* FIXME: See bfd_perform_relocation. Is this right? */
2447 return bfd_reloc_continue
;
2449 return nios2_elf32_do_lo16_relocate (abfd
, reloc_entry
->howto
,
2451 data
, reloc_entry
->address
,
2453 + symbol
->section
->output_section
->vma
2454 + symbol
->section
->output_offset
),
2455 reloc_entry
->addend
);
2458 static bfd_reloc_status_type
2459 nios2_elf32_hiadj16_relocate (bfd
*abfd
, arelent
*reloc_entry
, asymbol
*symbol
,
2460 void *data
, asection
*input_section
,
2462 char **error_message ATTRIBUTE_UNUSED
)
2464 /* This part is from bfd_elf_generic_reloc. */
2465 if (output_bfd
!= NULL
2466 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
2467 && (!reloc_entry
->howto
->partial_inplace
|| reloc_entry
->addend
== 0))
2469 reloc_entry
->address
+= input_section
->output_offset
;
2470 return bfd_reloc_ok
;
2473 if (output_bfd
!= NULL
)
2474 /* FIXME: See bfd_perform_relocation. Is this right? */
2475 return bfd_reloc_continue
;
2477 return nios2_elf32_do_hiadj16_relocate (abfd
, reloc_entry
->howto
,
2479 data
, reloc_entry
->address
,
2481 + symbol
->section
->output_section
->vma
2482 + symbol
->section
->output_offset
),
2483 reloc_entry
->addend
);
2486 static bfd_reloc_status_type
2487 nios2_elf32_pcrel_lo16_relocate (bfd
*abfd
, arelent
*reloc_entry
,
2488 asymbol
*symbol
, void *data
,
2489 asection
*input_section
, bfd
*output_bfd
,
2490 char **error_message ATTRIBUTE_UNUSED
)
2492 /* This part is from bfd_elf_generic_reloc. */
2493 if (output_bfd
!= NULL
2494 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
2495 && (!reloc_entry
->howto
->partial_inplace
|| reloc_entry
->addend
== 0))
2497 reloc_entry
->address
+= input_section
->output_offset
;
2498 return bfd_reloc_ok
;
2501 if (output_bfd
!= NULL
)
2502 /* FIXME: See bfd_perform_relocation. Is this right? */
2503 return bfd_reloc_continue
;
2505 return nios2_elf32_do_pcrel_lo16_relocate (
2506 abfd
, reloc_entry
->howto
, input_section
, data
, reloc_entry
->address
,
2507 (symbol
->value
+ symbol
->section
->output_section
->vma
2508 + symbol
->section
->output_offset
),
2509 reloc_entry
->addend
);
2512 static bfd_reloc_status_type
2513 nios2_elf32_pcrel_hiadj16_relocate (bfd
*abfd
, arelent
*reloc_entry
,
2514 asymbol
*symbol
, void *data
,
2515 asection
*input_section
, bfd
*output_bfd
,
2516 char **error_message ATTRIBUTE_UNUSED
)
2518 /* This part is from bfd_elf_generic_reloc. */
2519 if (output_bfd
!= NULL
2520 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
2521 && (!reloc_entry
->howto
->partial_inplace
|| reloc_entry
->addend
== 0))
2523 reloc_entry
->address
+= input_section
->output_offset
;
2524 return bfd_reloc_ok
;
2527 if (output_bfd
!= NULL
)
2528 /* FIXME: See bfd_perform_relocation. Is this right? */
2529 return bfd_reloc_continue
;
2531 return nios2_elf32_do_pcrel_hiadj16_relocate (
2532 abfd
, reloc_entry
->howto
, input_section
, data
, reloc_entry
->address
,
2533 (symbol
->value
+ symbol
->section
->output_section
->vma
2534 + symbol
->section
->output_offset
),
2535 reloc_entry
->addend
);
2538 static bfd_reloc_status_type
2539 nios2_elf32_pcrel16_relocate (bfd
*abfd
, arelent
*reloc_entry
, asymbol
*symbol
,
2540 void *data
, asection
*input_section
,
2542 char **error_message ATTRIBUTE_UNUSED
)
2544 /* This part is from bfd_elf_generic_reloc. */
2545 if (output_bfd
!= NULL
2546 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
2547 && (!reloc_entry
->howto
->partial_inplace
|| reloc_entry
->addend
== 0))
2549 reloc_entry
->address
+= input_section
->output_offset
;
2550 return bfd_reloc_ok
;
2553 if (output_bfd
!= NULL
)
2554 /* FIXME: See bfd_perform_relocation. Is this right? */
2555 return bfd_reloc_continue
;
2557 return nios2_elf32_do_pcrel16_relocate (abfd
, reloc_entry
->howto
,
2559 data
, reloc_entry
->address
,
2561 + symbol
->section
->output_section
->vma
2562 + symbol
->section
->output_offset
),
2563 reloc_entry
->addend
);
2566 static bfd_reloc_status_type
2567 nios2_elf32_call26_relocate (bfd
*abfd
, arelent
*reloc_entry
, asymbol
*symbol
,
2568 void *data
, asection
*input_section
,
2570 char **error_message ATTRIBUTE_UNUSED
)
2572 /* This part is from bfd_elf_generic_reloc. */
2573 if (output_bfd
!= NULL
2574 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
2575 && (!reloc_entry
->howto
->partial_inplace
|| reloc_entry
->addend
== 0))
2577 reloc_entry
->address
+= input_section
->output_offset
;
2578 return bfd_reloc_ok
;
2581 if (output_bfd
!= NULL
)
2582 /* FIXME: See bfd_perform_relocation. Is this right? */
2583 return bfd_reloc_continue
;
2585 return nios2_elf32_do_call26_relocate (abfd
, reloc_entry
->howto
,
2587 data
, reloc_entry
->address
,
2589 + symbol
->section
->output_section
->vma
2590 + symbol
->section
->output_offset
),
2591 reloc_entry
->addend
);
2594 static bfd_reloc_status_type
2595 nios2_elf32_gprel_relocate (bfd
*abfd
, arelent
*reloc_entry
, asymbol
*symbol
,
2596 void *data
, asection
*input_section
,
2597 bfd
*output_bfd
, char **msg
)
2601 bfd_reloc_status_type r
;
2604 /* This part is from bfd_elf_generic_reloc. */
2605 if (output_bfd
!= NULL
2606 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
2607 && (!reloc_entry
->howto
->partial_inplace
|| reloc_entry
->addend
== 0))
2609 reloc_entry
->address
+= input_section
->output_offset
;
2610 return bfd_reloc_ok
;
2613 if (output_bfd
!= NULL
)
2614 /* FIXME: See bfd_perform_relocation. Is this right? */
2615 return bfd_reloc_continue
;
2617 relocation
= (symbol
->value
2618 + symbol
->section
->output_section
->vma
2619 + symbol
->section
->output_offset
);
2621 /* This assumes we've already cached the _gp symbol. */
2622 r
= nios2_elf_final_gp (abfd
, symbol
, FALSE
, msg
, &gp
);
2623 if (r
== bfd_reloc_ok
)
2625 relocation
= relocation
+ reloc_entry
->addend
- gp
;
2626 reloc_entry
->addend
= 0;
2627 if ((signed) relocation
< -32768 || (signed) relocation
> 32767)
2629 *msg
= _("global pointer relative address out of range");
2630 r
= bfd_reloc_outofrange
;
2633 r
= nios2_elf32_do_gprel_relocate (abfd
, reloc_entry
->howto
,
2635 data
, reloc_entry
->address
,
2636 relocation
, reloc_entry
->addend
);
2642 static bfd_reloc_status_type
2643 nios2_elf32_ujmp_relocate (bfd
*abfd
, arelent
*reloc_entry
, asymbol
*symbol
,
2644 void *data
, asection
*input_section
,
2645 bfd
*output_bfd
, char **msg ATTRIBUTE_UNUSED
)
2647 /* This part is from bfd_elf_generic_reloc. */
2648 if (output_bfd
!= NULL
2649 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
2650 && (!reloc_entry
->howto
->partial_inplace
|| reloc_entry
->addend
== 0))
2652 reloc_entry
->address
+= input_section
->output_offset
;
2653 return bfd_reloc_ok
;
2656 if (output_bfd
!= NULL
)
2657 /* FIXME: See bfd_perform_relocation. Is this right? */
2658 return bfd_reloc_continue
;
2660 return nios2_elf32_do_ujmp_relocate (abfd
, reloc_entry
->howto
,
2662 data
, reloc_entry
->address
,
2664 + symbol
->section
->output_section
->vma
2665 + symbol
->section
->output_offset
),
2666 reloc_entry
->addend
);
2669 static bfd_reloc_status_type
2670 nios2_elf32_cjmp_relocate (bfd
*abfd
, arelent
*reloc_entry
, asymbol
*symbol
,
2671 void *data
, asection
*input_section
,
2672 bfd
*output_bfd
, char **msg ATTRIBUTE_UNUSED
)
2674 /* This part is from bfd_elf_generic_reloc. */
2675 if (output_bfd
!= NULL
2676 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
2677 && (!reloc_entry
->howto
->partial_inplace
|| reloc_entry
->addend
== 0))
2679 reloc_entry
->address
+= input_section
->output_offset
;
2680 return bfd_reloc_ok
;
2683 if (output_bfd
!= NULL
)
2684 /* FIXME: See bfd_perform_relocation. Is this right? */
2685 return bfd_reloc_continue
;
2687 return nios2_elf32_do_cjmp_relocate (abfd
, reloc_entry
->howto
,
2689 data
, reloc_entry
->address
,
2691 + symbol
->section
->output_section
->vma
2692 + symbol
->section
->output_offset
),
2693 reloc_entry
->addend
);
2696 static bfd_reloc_status_type
2697 nios2_elf32_callr_relocate (bfd
*abfd
, arelent
*reloc_entry
, asymbol
*symbol
,
2698 void *data
, asection
*input_section
,
2699 bfd
*output_bfd
, char **msg ATTRIBUTE_UNUSED
)
2701 /* This part is from bfd_elf_generic_reloc. */
2702 if (output_bfd
!= NULL
2703 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
2704 && (!reloc_entry
->howto
->partial_inplace
|| reloc_entry
->addend
== 0))
2706 reloc_entry
->address
+= input_section
->output_offset
;
2707 return bfd_reloc_ok
;
2710 if (output_bfd
!= NULL
)
2711 /* FIXME: See bfd_perform_relocation. Is this right? */
2712 return bfd_reloc_continue
;
2714 return nios2_elf32_do_callr_relocate (abfd
, reloc_entry
->howto
,
2716 data
, reloc_entry
->address
,
2718 + symbol
->section
->output_section
->vma
2719 + symbol
->section
->output_offset
),
2720 reloc_entry
->addend
);
2724 /* Implement elf_backend_relocate_section. */
2726 nios2_elf32_relocate_section (bfd
*output_bfd
,
2727 struct bfd_link_info
*info
,
2729 asection
*input_section
,
2731 Elf_Internal_Rela
*relocs
,
2732 Elf_Internal_Sym
*local_syms
,
2733 asection
**local_sections
)
2735 Elf_Internal_Shdr
*symtab_hdr
;
2736 struct elf_link_hash_entry
**sym_hashes
;
2737 Elf_Internal_Rela
*rel
;
2738 Elf_Internal_Rela
*relend
;
2739 struct elf32_nios2_link_hash_table
*htab
;
2742 asection
*sreloc
= NULL
;
2743 bfd_vma
*local_got_offsets
;
2746 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
2747 sym_hashes
= elf_sym_hashes (input_bfd
);
2748 relend
= relocs
+ input_section
->reloc_count
;
2750 htab
= elf32_nios2_hash_table (info
);
2751 sgot
= htab
->root
.sgot
;
2752 splt
= htab
->root
.splt
;
2753 local_got_offsets
= elf_local_got_offsets (input_bfd
);
2755 if (elf32_nios2_hash_table (info
)->h_gp_got
== NULL
)
2758 got_base
= elf32_nios2_hash_table (info
)->h_gp_got
->root
.u
.def
.value
;
2760 for (rel
= relocs
; rel
< relend
; rel
++)
2762 reloc_howto_type
*howto
;
2763 unsigned long r_symndx
;
2764 Elf_Internal_Sym
*sym
;
2766 struct elf_link_hash_entry
*h
;
2767 struct elf32_nios2_link_hash_entry
*eh
;
2770 bfd_vma reloc_address
;
2771 bfd_reloc_status_type r
= bfd_reloc_ok
;
2772 const char *name
= NULL
;
2776 const char* msg
= (const char*) NULL
;
2777 bfd_boolean unresolved_reloc
;
2781 r_type
= ELF32_R_TYPE (rel
->r_info
);
2782 r_symndx
= ELF32_R_SYM (rel
->r_info
);
2784 howto
= lookup_howto ((unsigned) ELF32_R_TYPE (rel
->r_info
));
2789 if (r_symndx
< symtab_hdr
->sh_info
)
2791 sym
= local_syms
+ r_symndx
;
2792 sec
= local_sections
[r_symndx
];
2793 relocation
= _bfd_elf_rela_local_sym (output_bfd
, sym
, &sec
, rel
);
2797 bfd_boolean warned
, ignored
;
2799 RELOC_FOR_GLOBAL_SYMBOL (info
, input_bfd
, input_section
, rel
,
2800 r_symndx
, symtab_hdr
, sym_hashes
,
2802 unresolved_reloc
, warned
, ignored
);
2805 if (sec
&& discarded_section (sec
))
2806 RELOC_AGAINST_DISCARDED_SECTION (info
, input_bfd
, input_section
,
2807 rel
, 1, relend
, howto
, 0, contents
);
2809 /* Nothing more to do unless this is a final link. */
2810 if (info
->relocatable
)
2813 if (sec
&& sec
->output_section
)
2814 reloc_address
= (sec
->output_section
->vma
+ sec
->output_offset
2821 switch (howto
->type
)
2824 r
= nios2_elf32_do_hi16_relocate (input_bfd
, howto
,
2826 contents
, rel
->r_offset
,
2827 relocation
, rel
->r_addend
);
2830 r
= nios2_elf32_do_lo16_relocate (input_bfd
, howto
,
2832 contents
, rel
->r_offset
,
2833 relocation
, rel
->r_addend
);
2835 case R_NIOS2_PCREL_LO
:
2836 r
= nios2_elf32_do_pcrel_lo16_relocate (input_bfd
, howto
,
2843 case R_NIOS2_HIADJ16
:
2844 r
= nios2_elf32_do_hiadj16_relocate (input_bfd
, howto
,
2845 input_section
, contents
,
2846 rel
->r_offset
, relocation
,
2849 case R_NIOS2_PCREL_HA
:
2850 r
= nios2_elf32_do_pcrel_hiadj16_relocate (input_bfd
, howto
,
2857 case R_NIOS2_PCREL16
:
2858 r
= nios2_elf32_do_pcrel16_relocate (input_bfd
, howto
,
2859 input_section
, contents
,
2860 rel
->r_offset
, relocation
,
2864 /* Turns an absolute address into a gp-relative address. */
2865 if (!nios2_elf_assign_gp (output_bfd
, &gp
, info
))
2867 format
= _("global pointer relative relocation at address "
2868 "0x%08x when _gp not defined\n");
2869 sprintf (msgbuf
, format
, reloc_address
);
2871 r
= bfd_reloc_dangerous
;
2875 bfd_vma symbol_address
= rel
->r_addend
+ relocation
;
2876 relocation
= relocation
+ rel
->r_addend
- gp
;
2878 if (((signed) relocation
< -32768
2879 || (signed) relocation
> 32767)
2881 || h
->root
.type
== bfd_link_hash_defined
2882 || h
->root
.type
== bfd_link_hash_defweak
))
2884 format
= _("Unable to reach %s (at 0x%08x) from the "
2885 "global pointer (at 0x%08x) because the "
2886 "offset (%d) is out of the allowed range, "
2887 "-32678 to 32767.\n" );
2888 sprintf (msgbuf
, format
, name
, symbol_address
, gp
,
2889 (signed)relocation
);
2891 r
= bfd_reloc_outofrange
;
2894 r
= _bfd_final_link_relocate (howto
, input_bfd
,
2895 input_section
, contents
,
2896 rel
->r_offset
, relocation
,
2902 r
= nios2_elf32_do_ujmp_relocate (input_bfd
, howto
,
2904 contents
, rel
->r_offset
,
2905 relocation
, rel
->r_addend
);
2908 r
= nios2_elf32_do_cjmp_relocate (input_bfd
, howto
,
2910 contents
, rel
->r_offset
,
2911 relocation
, rel
->r_addend
);
2914 r
= nios2_elf32_do_callr_relocate (input_bfd
, howto
,
2915 input_section
, contents
,
2916 rel
->r_offset
, relocation
,
2919 case R_NIOS2_CALL26
:
2920 case R_NIOS2_CALL26_NOAT
:
2921 /* If we have a call to an undefined weak symbol, we just want
2922 to stuff a zero in the bits of the call instruction and
2923 bypass the normal call26 relocation handling, because it'll
2924 diagnose an overflow error if address 0 isn't in the same
2925 256MB segment as the call site. Presumably the call
2926 should be guarded by a null check anyway. */
2927 if (h
!= NULL
&& h
->root
.type
== bfd_link_hash_undefweak
)
2929 BFD_ASSERT (relocation
== 0 && rel
->r_addend
== 0);
2930 r
= _bfd_final_link_relocate (howto
, input_bfd
,
2931 input_section
, contents
,
2932 rel
->r_offset
, relocation
,
2936 /* Handle relocations which should use the PLT entry.
2937 NIOS2_BFD_RELOC_32 relocations will use the symbol's value,
2938 which may point to a PLT entry, but we don't need to handle
2939 that here. If we created a PLT entry, all branches in this
2940 object should go to it. */
2941 if (h
!= NULL
&& splt
!= NULL
&& h
->plt
.offset
!= (bfd_vma
) -1)
2943 /* If we've created a .plt section, and assigned a PLT entry
2944 to this function, it should not be known to bind locally.
2945 If it were, we would have cleared the PLT entry. */
2946 BFD_ASSERT (!SYMBOL_CALLS_LOCAL (info
, h
));
2948 relocation
= (splt
->output_section
->vma
2949 + splt
->output_offset
2952 unresolved_reloc
= FALSE
;
2954 /* Detect R_NIOS2_CALL26 relocations that would overflow the
2955 256MB segment. Replace the target with a reference to a
2957 Note that htab->stub_group is null if relaxation has been
2958 disabled by the --no-relax linker command-line option, so
2959 we can use that to skip this processing entirely. */
2960 if (howto
->type
== R_NIOS2_CALL26
&& htab
->stub_group
)
2962 bfd_vma dest
= relocation
+ rel
->r_addend
;
2963 enum elf32_nios2_stub_type stub_type
;
2965 eh
= (struct elf32_nios2_link_hash_entry
*)h
;
2966 stub_type
= nios2_type_of_stub (input_section
, rel
, eh
,
2969 if (stub_type
!= nios2_stub_none
)
2971 struct elf32_nios2_stub_hash_entry
*hsh
;
2973 hsh
= nios2_get_stub_entry (input_section
, sec
,
2974 eh
, rel
, htab
, stub_type
);
2977 r
= bfd_reloc_undefined
;
2981 dest
= (hsh
->stub_offset
2982 + hsh
->stub_sec
->output_offset
2983 + hsh
->stub_sec
->output_section
->vma
);
2984 r
= nios2_elf32_do_call26_relocate (input_bfd
, howto
,
2994 r
= nios2_elf32_do_call26_relocate (input_bfd
, howto
,
2995 input_section
, contents
,
2996 rel
->r_offset
, relocation
,
3001 /* For symmetry this would be
3002 r = nios2_elf32_do_ignore_reloc (input_bfd, howto,
3003 input_section, contents,
3004 rel->r_offset, relocation,
3006 but do_ignore_reloc would do no more than return
3011 case R_NIOS2_CALL16
:
3012 case R_NIOS2_GOT_LO
:
3013 case R_NIOS2_GOT_HA
:
3014 case R_NIOS2_CALL_LO
:
3015 case R_NIOS2_CALL_HA
:
3016 /* Relocation is to the entry for this symbol in the
3017 global offset table. */
3020 r
= bfd_reloc_notsupported
;
3030 eh
= (struct elf32_nios2_link_hash_entry
*)h
;
3031 use_plt
= (eh
->got_types_used
== CALL_USED
3032 && h
->plt
.offset
!= (bfd_vma
) -1);
3034 off
= h
->got
.offset
;
3035 BFD_ASSERT (off
!= (bfd_vma
) -1);
3036 dyn
= elf_hash_table (info
)->dynamic_sections_created
;
3037 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, info
->shared
, h
)
3039 && SYMBOL_REFERENCES_LOCAL (info
, h
))
3040 || (ELF_ST_VISIBILITY (h
->other
)
3041 && h
->root
.type
== bfd_link_hash_undefweak
))
3043 /* This is actually a static link, or it is a -Bsymbolic
3044 link and the symbol is defined locally. We must
3045 initialize this entry in the global offset table.
3046 Since the offset must always be a multiple of 4, we
3047 use the least significant bit to record whether we
3048 have initialized it already.
3050 When doing a dynamic link, we create a .rela.got
3051 relocation entry to initialize the value. This is
3052 done in the finish_dynamic_symbol routine. */
3057 bfd_put_32 (output_bfd
, relocation
,
3058 sgot
->contents
+ off
);
3063 unresolved_reloc
= FALSE
;
3067 BFD_ASSERT (local_got_offsets
!= NULL
3068 && local_got_offsets
[r_symndx
] != (bfd_vma
) -1);
3070 off
= local_got_offsets
[r_symndx
];
3072 /* The offset must always be a multiple of 4. We use the
3073 least significant bit to record whether we have already
3074 generated the necessary reloc. */
3079 bfd_put_32 (output_bfd
, relocation
,
3080 sgot
->contents
+ off
);
3085 Elf_Internal_Rela outrel
;
3088 srelgot
= htab
->root
.srelgot
;
3089 BFD_ASSERT (srelgot
!= NULL
);
3091 outrel
.r_addend
= relocation
;
3092 outrel
.r_offset
= (sgot
->output_section
->vma
3093 + sgot
->output_offset
3095 outrel
.r_info
= ELF32_R_INFO (0, R_NIOS2_RELATIVE
);
3096 loc
= srelgot
->contents
;
3097 loc
+= (srelgot
->reloc_count
++ *
3098 sizeof (Elf32_External_Rela
));
3099 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
, loc
);
3102 local_got_offsets
[r_symndx
] |= 1;
3106 if (use_plt
&& info
->shared
)
3108 off
= ((h
->plt
.offset
- 24) / 12 + 3) * 4;
3109 relocation
= (htab
->root
.sgotplt
->output_offset
+ off
3113 relocation
= sgot
->output_offset
+ off
- got_base
;
3115 /* This relocation does not use the addend. */
3118 switch (howto
->type
)
3120 case R_NIOS2_GOT_LO
:
3121 case R_NIOS2_CALL_LO
:
3122 r
= nios2_elf32_do_lo16_relocate (input_bfd
, howto
,
3123 input_section
, contents
,
3124 rel
->r_offset
, relocation
,
3127 case R_NIOS2_GOT_HA
:
3128 case R_NIOS2_CALL_HA
:
3129 r
= nios2_elf32_do_hiadj16_relocate (input_bfd
, howto
,
3130 input_section
, contents
,
3136 r
= _bfd_final_link_relocate (howto
, input_bfd
,
3137 input_section
, contents
,
3138 rel
->r_offset
, relocation
,
3144 case R_NIOS2_GOTOFF_LO
:
3145 case R_NIOS2_GOTOFF_HA
:
3146 case R_NIOS2_GOTOFF
:
3147 /* Relocation is relative to the global offset table pointer. */
3149 BFD_ASSERT (sgot
!= NULL
);
3152 r
= bfd_reloc_notsupported
;
3156 /* Note that sgot->output_offset is not involved in this
3157 calculation. We always want the start of .got. */
3158 relocation
-= sgot
->output_section
->vma
;
3160 /* Now we adjust the relocation to be relative to the GOT pointer
3161 (the _gp_got symbol), which possibly contains the 0x8000 bias. */
3162 relocation
-= got_base
;
3164 switch (howto
->type
)
3166 case R_NIOS2_GOTOFF_LO
:
3167 r
= nios2_elf32_do_lo16_relocate (input_bfd
, howto
,
3168 input_section
, contents
,
3169 rel
->r_offset
, relocation
,
3172 case R_NIOS2_GOTOFF_HA
:
3173 r
= nios2_elf32_do_hiadj16_relocate (input_bfd
, howto
,
3174 input_section
, contents
,
3180 r
= _bfd_final_link_relocate (howto
, input_bfd
,
3181 input_section
, contents
,
3182 rel
->r_offset
, relocation
,
3188 case R_NIOS2_TLS_LDO16
:
3189 relocation
-= dtpoff_base (info
) + DTP_OFFSET
;
3191 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
3192 contents
, rel
->r_offset
,
3193 relocation
, rel
->r_addend
);
3195 case R_NIOS2_TLS_LDM16
:
3196 if (htab
->root
.sgot
== NULL
)
3199 off
= htab
->tls_ldm_got
.offset
;
3205 /* If we don't know the module number, create a relocation
3209 Elf_Internal_Rela outrel
;
3212 if (htab
->root
.srelgot
== NULL
)
3215 outrel
.r_addend
= 0;
3216 outrel
.r_offset
= (htab
->root
.sgot
->output_section
->vma
3217 + htab
->root
.sgot
->output_offset
3219 outrel
.r_info
= ELF32_R_INFO (0, R_NIOS2_TLS_DTPMOD
);
3221 loc
= htab
->root
.srelgot
->contents
;
3222 loc
+= (htab
->root
.srelgot
->reloc_count
++
3223 * sizeof (Elf32_External_Rela
));
3224 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
, loc
);
3227 bfd_put_32 (output_bfd
, 1,
3228 htab
->root
.sgot
->contents
+ off
);
3230 htab
->tls_ldm_got
.offset
|= 1;
3233 relocation
= htab
->root
.sgot
->output_offset
+ off
- got_base
;
3235 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
3236 contents
, rel
->r_offset
,
3237 relocation
, rel
->r_addend
);
3240 case R_NIOS2_TLS_GD16
:
3241 case R_NIOS2_TLS_IE16
:
3246 if (htab
->root
.sgot
== NULL
)
3253 dyn
= htab
->root
.dynamic_sections_created
;
3254 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, info
->shared
, h
)
3256 || !SYMBOL_REFERENCES_LOCAL (info
, h
)))
3258 unresolved_reloc
= FALSE
;
3261 off
= h
->got
.offset
;
3262 tls_type
= (((struct elf32_nios2_link_hash_entry
*) h
)
3267 if (local_got_offsets
== NULL
)
3269 off
= local_got_offsets
[r_symndx
];
3270 tls_type
= (elf32_nios2_local_got_tls_type (input_bfd
)
3274 if (tls_type
== GOT_UNKNOWN
)
3281 bfd_boolean need_relocs
= FALSE
;
3282 Elf_Internal_Rela outrel
;
3283 bfd_byte
*loc
= NULL
;
3286 /* The GOT entries have not been initialized yet. Do it
3287 now, and emit any relocations. If both an IE GOT and a
3288 GD GOT are necessary, we emit the GD first. */
3290 if ((info
->shared
|| indx
!= 0)
3292 || ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
3293 || h
->root
.type
!= bfd_link_hash_undefweak
))
3296 if (htab
->root
.srelgot
== NULL
)
3298 loc
= htab
->root
.srelgot
->contents
;
3299 loc
+= (htab
->root
.srelgot
->reloc_count
*
3300 sizeof (Elf32_External_Rela
));
3303 if (tls_type
& GOT_TLS_GD
)
3307 outrel
.r_addend
= 0;
3308 outrel
.r_offset
= (htab
->root
.sgot
->output_section
->vma
3309 + htab
->root
.sgot
->output_offset
3311 outrel
.r_info
= ELF32_R_INFO (indx
,
3312 R_NIOS2_TLS_DTPMOD
);
3314 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
,
3316 htab
->root
.srelgot
->reloc_count
++;
3317 loc
+= sizeof (Elf32_External_Rela
);
3320 bfd_put_32 (output_bfd
,
3321 (relocation
- dtpoff_base (info
) -
3323 htab
->root
.sgot
->contents
+ cur_off
+ 4);
3326 outrel
.r_addend
= 0;
3327 outrel
.r_info
= ELF32_R_INFO (indx
,
3328 R_NIOS2_TLS_DTPREL
);
3329 outrel
.r_offset
+= 4;
3331 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
,
3333 htab
->root
.srelgot
->reloc_count
++;
3334 loc
+= sizeof (Elf32_External_Rela
);
3339 /* If we are not emitting relocations for a
3340 general dynamic reference, then we must be in a
3341 static link or an executable link with the
3342 symbol binding locally. Mark it as belonging
3343 to module 1, the executable. */
3344 bfd_put_32 (output_bfd
, 1,
3345 htab
->root
.sgot
->contents
+ cur_off
);
3346 bfd_put_32 (output_bfd
, (relocation
-
3347 dtpoff_base (info
) -
3349 htab
->root
.sgot
->contents
+ cur_off
+ 4);
3355 if (tls_type
& GOT_TLS_IE
)
3360 outrel
.r_addend
= (relocation
-
3361 dtpoff_base (info
));
3363 outrel
.r_addend
= 0;
3364 outrel
.r_offset
= (htab
->root
.sgot
->output_section
->vma
3365 + htab
->root
.sgot
->output_offset
3367 outrel
.r_info
= ELF32_R_INFO (indx
,
3370 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
,
3372 htab
->root
.srelgot
->reloc_count
++;
3373 loc
+= sizeof (Elf32_External_Rela
);
3376 bfd_put_32 (output_bfd
, (tpoff (info
, relocation
)
3378 htab
->root
.sgot
->contents
+ cur_off
);
3385 local_got_offsets
[r_symndx
] |= 1;
3388 if ((tls_type
& GOT_TLS_GD
) && r_type
!= R_NIOS2_TLS_GD16
)
3390 relocation
= htab
->root
.sgot
->output_offset
+ off
- got_base
;
3392 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
3393 contents
, rel
->r_offset
,
3394 relocation
, rel
->r_addend
);
3398 case R_NIOS2_TLS_LE16
:
3399 if (info
->shared
&& !info
->pie
)
3401 (*_bfd_error_handler
)
3402 (_("%B(%A+0x%lx): R_NIOS2_TLS_LE16 relocation not "
3403 "permitted in shared object"),
3404 input_bfd
, input_section
,
3405 (long) rel
->r_offset
, howto
->name
);
3409 relocation
= tpoff (info
, relocation
) - TP_OFFSET
;
3411 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
3412 contents
, rel
->r_offset
,
3413 relocation
, rel
->r_addend
);
3416 case R_NIOS2_BFD_RELOC_32
:
3418 && (input_section
->flags
& SEC_ALLOC
) != 0
3420 || ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
3421 || h
->root
.type
!= bfd_link_hash_undefweak
))
3423 Elf_Internal_Rela outrel
;
3425 bfd_boolean skip
, relocate
;
3427 /* When generating a shared object, these relocations
3428 are copied into the output file to be resolved at run
3435 = _bfd_elf_section_offset (output_bfd
, info
,
3436 input_section
, rel
->r_offset
);
3437 if (outrel
.r_offset
== (bfd_vma
) -1)
3439 else if (outrel
.r_offset
== (bfd_vma
) -2)
3440 skip
= TRUE
, relocate
= TRUE
;
3441 outrel
.r_offset
+= (input_section
->output_section
->vma
3442 + input_section
->output_offset
);
3445 memset (&outrel
, 0, sizeof outrel
);
3450 || !h
->def_regular
))
3452 outrel
.r_info
= ELF32_R_INFO (h
->dynindx
, r_type
);
3453 outrel
.r_addend
= rel
->r_addend
;
3457 /* This symbol is local, or marked to become local. */
3458 outrel
.r_addend
= relocation
+ rel
->r_addend
;
3460 outrel
.r_info
= ELF32_R_INFO (0, R_NIOS2_RELATIVE
);
3463 sreloc
= elf_section_data (input_section
)->sreloc
;
3467 loc
= sreloc
->contents
;
3468 loc
+= sreloc
->reloc_count
++ * sizeof (Elf32_External_Rela
);
3469 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
, loc
);
3471 /* This reloc will be computed at runtime, so there's no
3472 need to do anything now, except for R_NIOS2_BFD_RELOC_32
3473 relocations that have been turned into
3474 R_NIOS2_RELATIVE. */
3479 r
= _bfd_final_link_relocate (howto
, input_bfd
,
3480 input_section
, contents
,
3481 rel
->r_offset
, relocation
,
3485 case R_NIOS2_TLS_DTPREL
:
3486 relocation
-= dtpoff_base (info
);
3490 r
= _bfd_final_link_relocate (howto
, input_bfd
,
3491 input_section
, contents
,
3492 rel
->r_offset
, relocation
,
3498 r
= bfd_reloc_notsupported
;
3500 if (r
!= bfd_reloc_ok
)
3503 name
= h
->root
.root
.string
;
3506 name
= bfd_elf_string_from_elf_section (input_bfd
,
3507 symtab_hdr
->sh_link
,
3509 if (name
== NULL
|| *name
== '\0')
3510 name
= bfd_section_name (input_bfd
, sec
);
3515 case bfd_reloc_overflow
:
3516 r
= info
->callbacks
->reloc_overflow (info
, NULL
, name
,
3517 howto
->name
, (bfd_vma
) 0,
3518 input_bfd
, input_section
,
3522 case bfd_reloc_undefined
:
3523 r
= info
->callbacks
->undefined_symbol (info
, name
, input_bfd
,
3525 rel
->r_offset
, TRUE
);
3528 case bfd_reloc_outofrange
:
3530 msg
= _("relocation out of range");
3533 case bfd_reloc_notsupported
:
3535 msg
= _("unsupported relocation");
3538 case bfd_reloc_dangerous
:
3540 msg
= _("dangerous relocation");
3545 msg
= _("unknown error");
3551 r
= info
->callbacks
->warning
3552 (info
, msg
, name
, input_bfd
, input_section
, rel
->r_offset
);
3560 /* Implement elf-backend_section_flags:
3561 Convert NIOS2 specific section flags to bfd internal section flags. */
3563 nios2_elf32_section_flags (flagword
*flags
, const Elf_Internal_Shdr
*hdr
)
3565 if (hdr
->sh_flags
& SHF_NIOS2_GPREL
)
3566 *flags
|= SEC_SMALL_DATA
;
3571 /* Implement elf_backend_fake_sections:
3572 Set the correct type for an NIOS2 ELF section. We do this by the
3573 section name, which is a hack, but ought to work. */
3575 nios2_elf32_fake_sections (bfd
*abfd ATTRIBUTE_UNUSED
,
3576 Elf_Internal_Shdr
*hdr
, asection
*sec
)
3578 register const char *name
= bfd_get_section_name (abfd
, sec
);
3580 if ((sec
->flags
& SEC_SMALL_DATA
)
3581 || strcmp (name
, ".sdata") == 0
3582 || strcmp (name
, ".sbss") == 0
3583 || strcmp (name
, ".lit4") == 0 || strcmp (name
, ".lit8") == 0)
3584 hdr
->sh_flags
|= SHF_NIOS2_GPREL
;
3589 /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
3590 shortcuts to them in our hash table. */
3592 create_got_section (bfd
*dynobj
, struct bfd_link_info
*info
)
3594 struct elf32_nios2_link_hash_table
*htab
;
3595 struct elf_link_hash_entry
*h
;
3597 htab
= elf32_nios2_hash_table (info
);
3599 if (! _bfd_elf_create_got_section (dynobj
, info
))
3602 /* In order for the two loads in .PLTresolve to share the same %hiadj,
3603 _GLOBAL_OFFSET_TABLE_ must be aligned to a 16-byte boundary. */
3604 if (!bfd_set_section_alignment (dynobj
, htab
->root
.sgotplt
, 4))
3607 /* The Nios II ABI specifies that GOT-relative relocations are relative
3608 to the linker-created symbol _gp_got, rather than using
3609 _GLOBAL_OFFSET_TABLE_ directly. In particular, the latter always
3610 points to the base of the GOT while _gp_got may include a bias. */
3611 h
= _bfd_elf_define_linkage_sym (dynobj
, info
, htab
->root
.sgotplt
,
3613 elf32_nios2_hash_table (info
)->h_gp_got
= h
;
3620 /* Implement elf_backend_create_dynamic_sections:
3621 Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
3622 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
3625 nios2_elf32_create_dynamic_sections (bfd
*dynobj
, struct bfd_link_info
*info
)
3627 struct elf32_nios2_link_hash_table
*htab
;
3629 htab
= elf32_nios2_hash_table (info
);
3630 if (!htab
->root
.sgot
&& !create_got_section (dynobj
, info
))
3633 _bfd_elf_create_dynamic_sections (dynobj
, info
);
3635 /* In order for the two loads in a shared object .PLTresolve to share the
3636 same %hiadj, the start of the PLT (as well as the GOT) must be aligned
3637 to a 16-byte boundary. This is because the addresses for these loads
3638 include the -(.plt+4) PIC correction. */
3639 if (!bfd_set_section_alignment (dynobj
, htab
->root
.splt
, 4))
3642 htab
->sdynbss
= bfd_get_linker_section (dynobj
, ".dynbss");
3647 htab
->srelbss
= bfd_get_linker_section (dynobj
, ".rela.bss");
3655 /* Implement elf_backend_copy_indirect_symbol:
3656 Copy the extra info we tack onto an elf_link_hash_entry. */
3658 nios2_elf32_copy_indirect_symbol (struct bfd_link_info
*info
,
3659 struct elf_link_hash_entry
*dir
,
3660 struct elf_link_hash_entry
*ind
)
3662 struct elf32_nios2_link_hash_entry
*edir
, *eind
;
3664 edir
= (struct elf32_nios2_link_hash_entry
*) dir
;
3665 eind
= (struct elf32_nios2_link_hash_entry
*) ind
;
3667 if (eind
->dyn_relocs
!= NULL
)
3669 if (edir
->dyn_relocs
!= NULL
)
3671 struct elf32_nios2_dyn_relocs
**pp
;
3672 struct elf32_nios2_dyn_relocs
*p
;
3674 /* Add reloc counts against the indirect sym to the direct sym
3675 list. Merge any entries against the same section. */
3676 for (pp
= &eind
->dyn_relocs
; (p
= *pp
) != NULL
; )
3678 struct elf32_nios2_dyn_relocs
*q
;
3680 for (q
= edir
->dyn_relocs
; q
!= NULL
; q
= q
->next
)
3681 if (q
->sec
== p
->sec
)
3683 q
->pc_count
+= p
->pc_count
;
3684 q
->count
+= p
->count
;
3691 *pp
= edir
->dyn_relocs
;
3694 edir
->dyn_relocs
= eind
->dyn_relocs
;
3695 eind
->dyn_relocs
= NULL
;
3698 if (ind
->root
.type
== bfd_link_hash_indirect
3699 && dir
->got
.refcount
<= 0)
3701 edir
->tls_type
= eind
->tls_type
;
3702 eind
->tls_type
= GOT_UNKNOWN
;
3705 edir
->got_types_used
|= eind
->got_types_used
;
3707 _bfd_elf_link_hash_copy_indirect (info
, dir
, ind
);
3710 /* Implement elf_backend_check_relocs:
3711 Look through the relocs for a section during the first phase. */
3713 nios2_elf32_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
,
3714 asection
*sec
, const Elf_Internal_Rela
*relocs
)
3717 Elf_Internal_Shdr
*symtab_hdr
;
3718 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
3719 const Elf_Internal_Rela
*rel
;
3720 const Elf_Internal_Rela
*rel_end
;
3721 struct elf32_nios2_link_hash_table
*htab
;
3724 asection
*sreloc
= NULL
;
3725 bfd_signed_vma
*local_got_refcounts
;
3727 if (info
->relocatable
)
3730 dynobj
= elf_hash_table (info
)->dynobj
;
3731 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3732 sym_hashes
= elf_sym_hashes (abfd
);
3733 sym_hashes_end
= (sym_hashes
3734 + symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
));
3735 if (!elf_bad_symtab (abfd
))
3736 sym_hashes_end
-= symtab_hdr
->sh_info
;
3737 local_got_refcounts
= elf_local_got_refcounts (abfd
);
3739 htab
= elf32_nios2_hash_table (info
);
3740 sgot
= htab
->root
.sgot
;
3741 srelgot
= htab
->root
.srelgot
;
3743 rel_end
= relocs
+ sec
->reloc_count
;
3744 for (rel
= relocs
; rel
< rel_end
; rel
++)
3746 unsigned int r_type
;
3747 struct elf_link_hash_entry
*h
;
3748 unsigned long r_symndx
;
3750 r_symndx
= ELF32_R_SYM (rel
->r_info
);
3751 if (r_symndx
< symtab_hdr
->sh_info
)
3755 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
3756 while (h
->root
.type
== bfd_link_hash_indirect
3757 || h
->root
.type
== bfd_link_hash_warning
)
3758 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3760 /* PR15323, ref flags aren't set for references in the same
3762 h
->root
.non_ir_ref
= 1;
3765 r_type
= ELF32_R_TYPE (rel
->r_info
);
3770 case R_NIOS2_GOT_LO
:
3771 case R_NIOS2_GOT_HA
:
3772 case R_NIOS2_CALL16
:
3773 case R_NIOS2_CALL_LO
:
3774 case R_NIOS2_CALL_HA
:
3775 case R_NIOS2_TLS_GD16
:
3776 case R_NIOS2_TLS_IE16
:
3777 /* This symbol requires a global offset table entry. */
3779 int tls_type
, old_tls_type
;
3785 case R_NIOS2_GOT_LO
:
3786 case R_NIOS2_GOT_HA
:
3787 case R_NIOS2_CALL16
:
3788 case R_NIOS2_CALL_LO
:
3789 case R_NIOS2_CALL_HA
:
3790 tls_type
= GOT_NORMAL
;
3792 case R_NIOS2_TLS_GD16
:
3793 tls_type
= GOT_TLS_GD
;
3795 case R_NIOS2_TLS_IE16
:
3796 tls_type
= GOT_TLS_IE
;
3802 /* Create the .got section. */
3803 elf_hash_table (info
)->dynobj
= dynobj
= abfd
;
3804 nios2_elf32_create_dynamic_sections (dynobj
, info
);
3809 sgot
= htab
->root
.sgot
;
3810 BFD_ASSERT (sgot
!= NULL
);
3814 && (h
!= NULL
|| info
->shared
))
3816 srelgot
= htab
->root
.srelgot
;
3817 BFD_ASSERT (srelgot
!= NULL
);
3822 struct elf32_nios2_link_hash_entry
*eh
3823 = (struct elf32_nios2_link_hash_entry
*)h
;
3825 old_tls_type
= elf32_nios2_hash_entry(h
)->tls_type
;
3826 if (r_type
== R_NIOS2_CALL16
3827 || r_type
== R_NIOS2_CALL_LO
3828 || r_type
== R_NIOS2_CALL_HA
)
3830 /* Make sure a plt entry is created for this symbol if
3831 it turns out to be a function defined by a dynamic
3836 eh
->got_types_used
|= CALL_USED
;
3839 eh
->got_types_used
|= GOT_USED
;
3843 /* This is a global offset table entry for a local symbol. */
3844 if (local_got_refcounts
== NULL
)
3848 size
= symtab_hdr
->sh_info
;
3849 size
*= (sizeof (bfd_signed_vma
) + sizeof (char));
3851 = ((bfd_signed_vma
*) bfd_zalloc (abfd
, size
));
3852 if (local_got_refcounts
== NULL
)
3854 elf_local_got_refcounts (abfd
) = local_got_refcounts
;
3855 elf32_nios2_local_got_tls_type (abfd
)
3856 = (char *) (local_got_refcounts
+ symtab_hdr
->sh_info
);
3858 local_got_refcounts
[r_symndx
]++;
3859 old_tls_type
= elf32_nios2_local_got_tls_type (abfd
) [r_symndx
];
3862 /* We will already have issued an error message if there is a
3863 TLS / non-TLS mismatch, based on the symbol type. We don't
3864 support any linker relaxations. So just combine any TLS
3866 if (old_tls_type
!= GOT_UNKNOWN
&& old_tls_type
!= GOT_NORMAL
3867 && tls_type
!= GOT_NORMAL
)
3868 tls_type
|= old_tls_type
;
3870 if (old_tls_type
!= tls_type
)
3873 elf32_nios2_hash_entry (h
)->tls_type
= tls_type
;
3875 elf32_nios2_local_got_tls_type (abfd
) [r_symndx
] = tls_type
;
3879 case R_NIOS2_TLS_LDM16
:
3880 if (r_type
== R_NIOS2_TLS_LDM16
)
3881 htab
->tls_ldm_got
.refcount
++;
3883 if (htab
->root
.sgot
== NULL
)
3885 if (htab
->root
.dynobj
== NULL
)
3886 htab
->root
.dynobj
= abfd
;
3887 if (!create_got_section (htab
->root
.dynobj
, info
))
3892 /* This relocation describes the C++ object vtable hierarchy.
3893 Reconstruct it for later use during GC. */
3894 case R_NIOS2_GNU_VTINHERIT
:
3895 if (!bfd_elf_gc_record_vtinherit (abfd
, sec
, h
, rel
->r_offset
))
3899 /* This relocation describes which C++ vtable entries are actually
3900 used. Record for later use during GC. */
3901 case R_NIOS2_GNU_VTENTRY
:
3902 if (!bfd_elf_gc_record_vtentry (abfd
, sec
, h
, rel
->r_addend
))
3906 case R_NIOS2_BFD_RELOC_32
:
3907 case R_NIOS2_CALL26
:
3908 case R_NIOS2_CALL26_NOAT
:
3909 case R_NIOS2_HIADJ16
:
3914 /* If this reloc is in a read-only section, we might
3915 need a copy reloc. We can't check reliably at this
3916 stage whether the section is read-only, as input
3917 sections have not yet been mapped to output sections.
3918 Tentatively set the flag for now, and correct in
3919 adjust_dynamic_symbol. */
3923 /* Make sure a plt entry is created for this symbol if it
3924 turns out to be a function defined by a dynamic object. */
3927 if (r_type
== R_NIOS2_CALL26
|| r_type
== R_NIOS2_CALL26_NOAT
)
3931 /* If we are creating a shared library, we need to copy the
3932 reloc into the shared library. */
3934 && (sec
->flags
& SEC_ALLOC
) != 0
3935 && (r_type
== R_NIOS2_BFD_RELOC_32
3936 || (h
!= NULL
&& ! h
->needs_plt
3937 && (! info
->symbolic
|| ! h
->def_regular
))))
3939 struct elf32_nios2_dyn_relocs
*p
;
3940 struct elf32_nios2_dyn_relocs
**head
;
3942 /* When creating a shared object, we must copy these
3943 reloc types into the output file. We create a reloc
3944 section in dynobj and make room for this reloc. */
3947 sreloc
= _bfd_elf_make_dynamic_reloc_section
3948 (sec
, dynobj
, 2, abfd
, TRUE
);
3953 /* If this is a global symbol, we count the number of
3954 relocations we need for this symbol. */
3956 head
= &((struct elf32_nios2_link_hash_entry
*) h
)->dyn_relocs
;
3959 /* Track dynamic relocs needed for local syms too.
3960 We really need local syms available to do this
3965 Elf_Internal_Sym
*isym
;
3967 isym
= bfd_sym_from_r_symndx (&htab
->sym_cache
,
3972 s
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
3976 vpp
= &elf_section_data (s
)->local_dynrel
;
3977 head
= (struct elf32_nios2_dyn_relocs
**) vpp
;
3981 if (p
== NULL
|| p
->sec
!= sec
)
3983 bfd_size_type amt
= sizeof *p
;
3984 p
= ((struct elf32_nios2_dyn_relocs
*)
3985 bfd_alloc (htab
->root
.dynobj
, amt
));
4006 /* Implement elf_backend_gc_mark_hook:
4007 Return the section that should be marked against GC for a given
4010 nios2_elf32_gc_mark_hook (asection
*sec
,
4011 struct bfd_link_info
*info
,
4012 Elf_Internal_Rela
*rel
,
4013 struct elf_link_hash_entry
*h
,
4014 Elf_Internal_Sym
*sym
)
4017 switch (ELF32_R_TYPE (rel
->r_info
))
4019 case R_NIOS2_GNU_VTINHERIT
:
4020 case R_NIOS2_GNU_VTENTRY
:
4023 return _bfd_elf_gc_mark_hook (sec
, info
, rel
, h
, sym
);
4026 /* Implement elf_backend_gc_sweep_hook:
4027 Update the got entry reference counts for the section being removed. */
4029 nios2_elf32_gc_sweep_hook (bfd
*abfd
,
4030 struct bfd_link_info
*info
,
4032 const Elf_Internal_Rela
*relocs
)
4034 Elf_Internal_Shdr
*symtab_hdr
;
4035 struct elf_link_hash_entry
**sym_hashes
;
4036 bfd_signed_vma
*local_got_refcounts
;
4037 const Elf_Internal_Rela
*rel
, *relend
;
4040 if (info
->relocatable
)
4043 elf_section_data (sec
)->local_dynrel
= NULL
;
4045 dynobj
= elf_hash_table (info
)->dynobj
;
4049 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4050 sym_hashes
= elf_sym_hashes (abfd
);
4051 local_got_refcounts
= elf_local_got_refcounts (abfd
);
4053 relend
= relocs
+ sec
->reloc_count
;
4054 for (rel
= relocs
; rel
< relend
; rel
++)
4056 unsigned long r_symndx
;
4057 struct elf_link_hash_entry
*h
= NULL
;
4060 r_symndx
= ELF32_R_SYM (rel
->r_info
);
4061 if (r_symndx
>= symtab_hdr
->sh_info
)
4063 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
4064 while (h
->root
.type
== bfd_link_hash_indirect
4065 || h
->root
.type
== bfd_link_hash_warning
)
4066 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4069 r_type
= ELF32_R_TYPE (rel
->r_info
);
4073 case R_NIOS2_GOT_LO
:
4074 case R_NIOS2_GOT_HA
:
4075 case R_NIOS2_CALL16
:
4076 case R_NIOS2_CALL_LO
:
4077 case R_NIOS2_CALL_HA
:
4080 if (h
->got
.refcount
> 0)
4083 else if (local_got_refcounts
!= NULL
)
4085 if (local_got_refcounts
[r_symndx
] > 0)
4086 --local_got_refcounts
[r_symndx
];
4090 case R_NIOS2_PCREL_LO
:
4091 case R_NIOS2_PCREL_HA
:
4092 case R_NIOS2_BFD_RELOC_32
:
4093 case R_NIOS2_CALL26
:
4094 case R_NIOS2_CALL26_NOAT
:
4097 struct elf32_nios2_link_hash_entry
*eh
;
4098 struct elf32_nios2_dyn_relocs
**pp
;
4099 struct elf32_nios2_dyn_relocs
*p
;
4101 eh
= (struct elf32_nios2_link_hash_entry
*) h
;
4103 if (h
->plt
.refcount
> 0)
4106 if (r_type
== R_NIOS2_PCREL_LO
|| r_type
== R_NIOS2_PCREL_HA
4107 || r_type
== R_NIOS2_BFD_RELOC_32
)
4109 for (pp
= &eh
->dyn_relocs
; (p
= *pp
) != NULL
;
4130 /* Implement elf_backend_finish_dynamic_symbols:
4131 Finish up dynamic symbol handling. We set the contents of various
4132 dynamic sections here. */
4134 nios2_elf32_finish_dynamic_symbol (bfd
*output_bfd
,
4135 struct bfd_link_info
*info
,
4136 struct elf_link_hash_entry
*h
,
4137 Elf_Internal_Sym
*sym
)
4139 struct elf32_nios2_link_hash_table
*htab
;
4140 struct elf32_nios2_link_hash_entry
*eh
4141 = (struct elf32_nios2_link_hash_entry
*)h
;
4144 htab
= elf32_nios2_hash_table (info
);
4146 if (h
->plt
.offset
!= (bfd_vma
) -1)
4153 Elf_Internal_Rela rela
;
4155 bfd_vma got_address
;
4157 /* This symbol has an entry in the procedure linkage table. Set
4159 BFD_ASSERT (h
->dynindx
!= -1);
4160 splt
= htab
->root
.splt
;
4161 sgotplt
= htab
->root
.sgotplt
;
4162 srela
= htab
->root
.srelplt
;
4163 BFD_ASSERT (splt
!= NULL
&& sgotplt
!= NULL
&& srela
!= NULL
);
4165 /* Emit the PLT entry. */
4168 nios2_elf32_install_data (splt
, nios2_so_plt_entry
, h
->plt
.offset
,
4170 plt_index
= (h
->plt
.offset
- 24) / 12;
4171 got_offset
= (plt_index
+ 3) * 4;
4172 nios2_elf32_install_imm16 (splt
, h
->plt
.offset
,
4173 hiadj(plt_index
* 4));
4174 nios2_elf32_install_imm16 (splt
, h
->plt
.offset
+ 4,
4175 (plt_index
* 4) & 0xffff);
4176 nios2_elf32_install_imm16 (splt
, h
->plt
.offset
+ 8,
4177 0xfff4 - h
->plt
.offset
);
4178 got_address
= (sgotplt
->output_section
->vma
+ sgotplt
->output_offset
4181 /* Fill in the entry in the global offset table. There are no
4182 res_n slots for a shared object PLT, instead the .got.plt entries
4183 point to the PLT entries. */
4184 bfd_put_32 (output_bfd
,
4185 splt
->output_section
->vma
+ splt
->output_offset
4186 + h
->plt
.offset
, sgotplt
->contents
+ got_offset
);
4190 plt_index
= (h
->plt
.offset
- 28 - htab
->res_n_size
) / 12;
4191 got_offset
= (plt_index
+ 3) * 4;
4193 nios2_elf32_install_data (splt
, nios2_plt_entry
, h
->plt
.offset
, 3);
4194 got_address
= (sgotplt
->output_section
->vma
+ sgotplt
->output_offset
4196 nios2_elf32_install_imm16 (splt
, h
->plt
.offset
, hiadj(got_address
));
4197 nios2_elf32_install_imm16 (splt
, h
->plt
.offset
+ 4,
4198 got_address
& 0xffff);
4200 /* Fill in the entry in the global offset table. */
4201 bfd_put_32 (output_bfd
,
4202 splt
->output_section
->vma
+ splt
->output_offset
4203 + plt_index
* 4, sgotplt
->contents
+ got_offset
);
4206 /* Fill in the entry in the .rela.plt section. */
4207 rela
.r_offset
= got_address
;
4208 rela
.r_info
= ELF32_R_INFO (h
->dynindx
, R_NIOS2_JUMP_SLOT
);
4210 loc
= srela
->contents
+ plt_index
* sizeof (Elf32_External_Rela
);
4211 bfd_elf32_swap_reloca_out (output_bfd
, &rela
, loc
);
4213 if (!h
->def_regular
)
4215 /* Mark the symbol as undefined, rather than as defined in
4216 the .plt section. Leave the value alone. */
4217 sym
->st_shndx
= SHN_UNDEF
;
4218 /* If the symbol is weak, we do need to clear the value.
4219 Otherwise, the PLT entry would provide a definition for
4220 the symbol even if the symbol wasn't defined anywhere,
4221 and so the symbol would never be NULL. */
4222 if (!h
->ref_regular_nonweak
)
4227 use_plt
= (eh
->got_types_used
== CALL_USED
4228 && h
->plt
.offset
!= (bfd_vma
) -1);
4230 if (!use_plt
&& h
->got
.offset
!= (bfd_vma
) -1
4231 && (elf32_nios2_hash_entry (h
)->tls_type
& GOT_TLS_GD
) == 0
4232 && (elf32_nios2_hash_entry (h
)->tls_type
& GOT_TLS_IE
) == 0)
4236 Elf_Internal_Rela rela
;
4240 /* This symbol has an entry in the global offset table. Set it
4242 sgot
= htab
->root
.sgot
;
4243 srela
= htab
->root
.srelgot
;
4244 BFD_ASSERT (sgot
!= NULL
&& srela
!= NULL
);
4246 offset
= (h
->got
.offset
& ~(bfd_vma
) 1);
4247 rela
.r_offset
= (sgot
->output_section
->vma
4248 + sgot
->output_offset
+ offset
);
4250 /* If this is a -Bsymbolic link, and the symbol is defined
4251 locally, we just want to emit a RELATIVE reloc. Likewise if
4252 the symbol was forced to be local because of a version file.
4253 The entry in the global offset table will already have been
4254 initialized in the relocate_section function. */
4256 if (info
->shared
&& SYMBOL_REFERENCES_LOCAL (info
, h
))
4258 rela
.r_info
= ELF32_R_INFO (0, R_NIOS2_RELATIVE
);
4259 rela
.r_addend
= bfd_get_signed_32 (output_bfd
,
4260 (sgot
->contents
+ offset
));
4261 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
+ offset
);
4265 bfd_put_32 (output_bfd
, (bfd_vma
) 0,
4266 sgot
->contents
+ offset
);
4267 rela
.r_info
= ELF32_R_INFO (h
->dynindx
, R_NIOS2_GLOB_DAT
);
4271 loc
= srela
->contents
;
4272 loc
+= srela
->reloc_count
++ * sizeof (Elf32_External_Rela
);
4273 bfd_elf32_swap_reloca_out (output_bfd
, &rela
, loc
);
4276 if (use_plt
&& h
->got
.offset
!= (bfd_vma
) -1)
4278 bfd_vma offset
= (h
->got
.offset
& ~(bfd_vma
) 1);
4279 asection
*sgot
= htab
->root
.sgot
;
4280 asection
*splt
= htab
->root
.splt
;
4281 bfd_put_32 (output_bfd
, (splt
->output_section
->vma
+ splt
->output_offset
4283 sgot
->contents
+ offset
);
4289 Elf_Internal_Rela rela
;
4292 /* This symbol needs a copy reloc. Set it up. */
4293 BFD_ASSERT (h
->dynindx
!= -1
4294 && (h
->root
.type
== bfd_link_hash_defined
4295 || h
->root
.type
== bfd_link_hash_defweak
));
4298 BFD_ASSERT (s
!= NULL
);
4300 rela
.r_offset
= (h
->root
.u
.def
.value
4301 + h
->root
.u
.def
.section
->output_section
->vma
4302 + h
->root
.u
.def
.section
->output_offset
);
4303 rela
.r_info
= ELF32_R_INFO (h
->dynindx
, R_NIOS2_COPY
);
4305 loc
= s
->contents
+ s
->reloc_count
++ * sizeof (Elf32_External_Rela
);
4306 bfd_elf32_swap_reloca_out (output_bfd
, &rela
, loc
);
4309 /* Mark _DYNAMIC, _GLOBAL_OFFSET_TABLE_, and _gp_got as absolute. */
4310 if (strcmp (h
->root
.root
.string
, "_DYNAMIC") == 0
4311 || h
== elf_hash_table (info
)->hgot
4312 || h
== elf32_nios2_hash_table (info
)->h_gp_got
)
4313 sym
->st_shndx
= SHN_ABS
;
4318 /* Implement elf_backend_finish_dynamic_sections. */
4320 nios2_elf32_finish_dynamic_sections (bfd
*output_bfd
,
4321 struct bfd_link_info
*info
)
4326 struct elf32_nios2_link_hash_table
*htab
;
4328 htab
= elf32_nios2_hash_table (info
);
4329 dynobj
= elf_hash_table (info
)->dynobj
;
4330 sgotplt
= htab
->root
.sgotplt
;
4331 BFD_ASSERT (sgotplt
!= NULL
);
4332 sdyn
= bfd_get_linker_section (dynobj
, ".dynamic");
4334 if (elf_hash_table (info
)->dynamic_sections_created
)
4337 Elf32_External_Dyn
*dyncon
, *dynconend
;
4339 splt
= htab
->root
.splt
;
4340 BFD_ASSERT (splt
!= NULL
&& sdyn
!= NULL
);
4342 dyncon
= (Elf32_External_Dyn
*) sdyn
->contents
;
4343 dynconend
= (Elf32_External_Dyn
*) (sdyn
->contents
+ sdyn
->size
);
4344 for (; dyncon
< dynconend
; dyncon
++)
4346 Elf_Internal_Dyn dyn
;
4349 bfd_elf32_swap_dyn_in (dynobj
, dyncon
, &dyn
);
4357 s
= htab
->root
.sgot
;
4358 BFD_ASSERT (s
!= NULL
);
4359 dyn
.d_un
.d_ptr
= s
->output_section
->vma
;
4360 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
4364 s
= htab
->root
.srelplt
;
4365 BFD_ASSERT (s
!= NULL
);
4366 dyn
.d_un
.d_ptr
= s
->output_section
->vma
;
4367 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
4371 s
= htab
->root
.srelplt
;
4372 BFD_ASSERT (s
!= NULL
);
4373 dyn
.d_un
.d_val
= s
->size
;
4374 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
4378 /* The procedure linkage table relocs (DT_JMPREL) should
4379 not be included in the overall relocs (DT_RELA).
4380 Therefore, we override the DT_RELASZ entry here to
4381 make it not include the JMPREL relocs. Since the
4382 linker script arranges for .rela.plt to follow all
4383 other relocation sections, we don't have to worry
4384 about changing the DT_RELA entry. */
4385 s
= htab
->root
.srelplt
;
4387 dyn
.d_un
.d_val
-= s
->size
;
4388 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
4392 s
= htab
->root
.sgot
;
4393 BFD_ASSERT (s
!= NULL
);
4394 dyn
.d_un
.d_ptr
= s
->output_section
->vma
+ 0x7ff0;
4395 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
4400 /* Fill in the first entry in the procedure linkage table. */
4403 bfd_vma got_address
= (sgotplt
->output_section
->vma
4404 + sgotplt
->output_offset
);
4407 bfd_vma corrected
= got_address
- (splt
->output_section
->vma
4408 + splt
->output_offset
+ 4);
4409 nios2_elf32_install_data (splt
, nios2_so_plt0_entry
, 0, 6);
4410 nios2_elf32_install_imm16 (splt
, 4, hiadj (corrected
));
4411 nios2_elf32_install_imm16 (splt
, 12, (corrected
& 0xffff) + 4);
4412 nios2_elf32_install_imm16 (splt
, 16, (corrected
& 0xffff) + 8);
4416 /* Divide by 4 here, not 3 because we already corrected for the
4418 bfd_vma res_size
= (splt
->size
- 28) / 4;
4419 bfd_vma res_start
= (splt
->output_section
->vma
4420 + splt
->output_offset
);
4423 for (res_offset
= 0; res_offset
< res_size
; res_offset
+= 4)
4424 bfd_put_32 (output_bfd
,
4425 6 | ((res_size
- (res_offset
+ 4)) << 6),
4426 splt
->contents
+ res_offset
);
4428 nios2_elf32_install_data (splt
, nios2_plt0_entry
, res_size
, 7);
4429 nios2_elf32_install_imm16 (splt
, res_size
, hiadj (res_start
));
4430 nios2_elf32_install_imm16 (splt
, res_size
+ 4,
4431 res_start
& 0xffff);
4432 nios2_elf32_install_imm16 (splt
, res_size
+ 12,
4433 hiadj (got_address
));
4434 nios2_elf32_install_imm16 (splt
, res_size
+ 16,
4435 (got_address
& 0xffff) + 4);
4436 nios2_elf32_install_imm16 (splt
, res_size
+ 20,
4437 (got_address
& 0xffff) + 8);
4441 /* Fill in the first three entries in the global offset table. */
4442 if (sgotplt
->size
> 0)
4445 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgotplt
->contents
);
4447 bfd_put_32 (output_bfd
,
4448 sdyn
->output_section
->vma
+ sdyn
->output_offset
,
4450 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgotplt
->contents
+ 4);
4451 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgotplt
->contents
+ 8);
4454 elf_section_data (sgotplt
->output_section
)->this_hdr
.sh_entsize
= 4;
4459 /* Implement elf_backend_adjust_dynamic_symbol:
4460 Adjust a symbol defined by a dynamic object and referenced by a
4461 regular object. The current definition is in some section of the
4462 dynamic object, but we're not including those sections. We have to
4463 change the definition to something the rest of the link can
4466 nios2_elf32_adjust_dynamic_symbol (struct bfd_link_info
*info
,
4467 struct elf_link_hash_entry
*h
)
4469 struct elf32_nios2_link_hash_table
*htab
;
4474 htab
= elf32_nios2_hash_table (info
);
4475 dynobj
= elf_hash_table (info
)->dynobj
;
4477 /* Make sure we know what is going on here. */
4478 BFD_ASSERT (dynobj
!= NULL
4480 || h
->u
.weakdef
!= NULL
4483 && !h
->def_regular
)));
4485 /* If this is a function, put it in the procedure linkage table. We
4486 will fill in the contents of the procedure linkage table later,
4487 when we know the address of the .got section. */
4488 if (h
->type
== STT_FUNC
|| h
->needs_plt
)
4490 if (h
->plt
.refcount
<= 0
4491 || SYMBOL_CALLS_LOCAL (info
, h
)
4492 || (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
4493 && h
->root
.type
== bfd_link_hash_undefweak
))
4495 /* This case can occur if we saw a PLT reloc in an input
4496 file, but the symbol was never referred to by a dynamic
4497 object, or if all references were garbage collected. In
4498 such a case, we don't actually need to build a procedure
4499 linkage table, and we can just do a PCREL reloc instead. */
4500 h
->plt
.offset
= (bfd_vma
) -1;
4507 /* Reinitialize the plt offset now that it is not used as a reference
4509 h
->plt
.offset
= (bfd_vma
) -1;
4511 /* If this is a weak symbol, and there is a real definition, the
4512 processor independent code will have arranged for us to see the
4513 real definition first, and we can just use the same value. */
4514 if (h
->u
.weakdef
!= NULL
)
4516 BFD_ASSERT (h
->u
.weakdef
->root
.type
== bfd_link_hash_defined
4517 || h
->u
.weakdef
->root
.type
== bfd_link_hash_defweak
);
4518 h
->root
.u
.def
.section
= h
->u
.weakdef
->root
.u
.def
.section
;
4519 h
->root
.u
.def
.value
= h
->u
.weakdef
->root
.u
.def
.value
;
4523 /* If there are no non-GOT references, we do not need a copy
4525 if (!h
->non_got_ref
)
4528 /* This is a reference to a symbol defined by a dynamic object which
4530 If we are creating a shared library, we must presume that the
4531 only references to the symbol are via the global offset table.
4532 For such cases we need not do anything here; the relocations will
4533 be handled correctly by relocate_section. */
4539 (*_bfd_error_handler
) (_("dynamic variable `%s' is zero size"),
4540 h
->root
.root
.string
);
4544 /* We must allocate the symbol in our .dynbss section, which will
4545 become part of the .bss section of the executable. There will be
4546 an entry for this symbol in the .dynsym section. The dynamic
4547 object will contain position independent code, so all references
4548 from the dynamic object to this symbol will go through the global
4549 offset table. The dynamic linker will use the .dynsym entry to
4550 determine the address it must put in the global offset table, so
4551 both the dynamic object and the regular object will refer to the
4552 same memory location for the variable. */
4554 BFD_ASSERT (s
!= NULL
);
4556 /* We must generate a R_NIOS2_COPY reloc to tell the dynamic linker to
4557 copy the initial value out of the dynamic object and into the
4558 runtime process image. We need to remember the offset into the
4559 .rela.bss section we are going to use. */
4560 if ((h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0)
4564 srel
= htab
->srelbss
;
4565 BFD_ASSERT (srel
!= NULL
);
4566 srel
->size
+= sizeof (Elf32_External_Rela
);
4570 align2
= bfd_log2 (h
->size
);
4571 if (align2
> h
->root
.u
.def
.section
->alignment_power
)
4572 align2
= h
->root
.u
.def
.section
->alignment_power
;
4575 s
->size
= BFD_ALIGN (s
->size
, (bfd_size_type
)1 << align2
);
4576 if (align2
> bfd_get_section_alignment (dynobj
, s
)
4577 && !bfd_set_section_alignment (dynobj
, s
, align2
))
4580 /* Define the symbol as being at this point in the section. */
4581 h
->root
.u
.def
.section
= s
;
4582 h
->root
.u
.def
.value
= s
->size
;
4584 /* Increment the section size to make room for the symbol. */
4590 /* Worker function for nios2_elf32_size_dynamic_sections. */
4592 adjust_dynrelocs (struct elf_link_hash_entry
*h
, PTR inf
)
4594 struct bfd_link_info
*info
;
4595 struct elf32_nios2_link_hash_table
*htab
;
4597 if (h
->root
.type
== bfd_link_hash_indirect
)
4600 if (h
->root
.type
== bfd_link_hash_warning
)
4601 /* When warning symbols are created, they **replace** the "real"
4602 entry in the hash table, thus we never get to see the real
4603 symbol in a hash traversal. So look at it now. */
4604 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4606 info
= (struct bfd_link_info
*) inf
;
4607 htab
= elf32_nios2_hash_table (info
);
4609 if (h
->plt
.offset
!= (bfd_vma
)-1)
4610 h
->plt
.offset
+= htab
->res_n_size
;
4611 if (htab
->root
.splt
== h
->root
.u
.def
.section
)
4612 h
->root
.u
.def
.value
+= htab
->res_n_size
;
4617 /* Another worker function for nios2_elf32_size_dynamic_sections.
4618 Allocate space in .plt, .got and associated reloc sections for
4621 allocate_dynrelocs (struct elf_link_hash_entry
*h
, PTR inf
)
4623 struct bfd_link_info
*info
;
4624 struct elf32_nios2_link_hash_table
*htab
;
4625 struct elf32_nios2_link_hash_entry
*eh
;
4626 struct elf32_nios2_dyn_relocs
*p
;
4629 if (h
->root
.type
== bfd_link_hash_indirect
)
4632 if (h
->root
.type
== bfd_link_hash_warning
)
4633 /* When warning symbols are created, they **replace** the "real"
4634 entry in the hash table, thus we never get to see the real
4635 symbol in a hash traversal. So look at it now. */
4636 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4638 info
= (struct bfd_link_info
*) inf
;
4639 htab
= elf32_nios2_hash_table (info
);
4641 if (htab
->root
.dynamic_sections_created
4642 && h
->plt
.refcount
> 0)
4644 /* Make sure this symbol is output as a dynamic symbol.
4645 Undefined weak syms won't yet be marked as dynamic. */
4646 if (h
->dynindx
== -1
4648 && !bfd_elf_link_record_dynamic_symbol (info
, h
))
4651 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info
->shared
, h
))
4653 asection
*s
= htab
->root
.splt
;
4655 /* Allocate room for the header. */
4664 h
->plt
.offset
= s
->size
;
4666 /* If this symbol is not defined in a regular file, and we are
4667 not generating a shared library, then set the symbol to this
4668 location in the .plt. This is required to make function
4669 pointers compare as equal between the normal executable and
4670 the shared library. */
4674 h
->root
.u
.def
.section
= s
;
4675 h
->root
.u
.def
.value
= h
->plt
.offset
;
4678 /* Make room for this entry. */
4681 /* We also need to make an entry in the .rela.plt section. */
4682 htab
->root
.srelplt
->size
+= sizeof (Elf32_External_Rela
);
4684 /* And the .got.plt section. */
4685 htab
->root
.sgotplt
->size
+= 4;
4689 h
->plt
.offset
= (bfd_vma
) -1;
4695 h
->plt
.offset
= (bfd_vma
) -1;
4699 eh
= (struct elf32_nios2_link_hash_entry
*) h
;
4700 use_plt
= (eh
->got_types_used
== CALL_USED
4701 && h
->plt
.offset
!= (bfd_vma
) -1);
4703 if (h
->got
.refcount
> 0)
4707 int tls_type
= eh
->tls_type
;
4710 /* Make sure this symbol is output as a dynamic symbol.
4711 Undefined weak syms won't yet be marked as dynamic. */
4712 if (h
->dynindx
== -1
4714 && !bfd_elf_link_record_dynamic_symbol (info
, h
))
4717 s
= htab
->root
.sgot
;
4718 h
->got
.offset
= s
->size
;
4720 if (tls_type
== GOT_UNKNOWN
)
4723 if (tls_type
== GOT_NORMAL
)
4724 /* Non-TLS symbols need one GOT slot. */
4728 if (tls_type
& GOT_TLS_GD
)
4729 /* R_NIOS2_TLS_GD16 needs 2 consecutive GOT slots. */
4731 if (tls_type
& GOT_TLS_IE
)
4732 /* R_NIOS2_TLS_IE16 needs one GOT slot. */
4736 dyn
= htab
->root
.dynamic_sections_created
;
4739 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, info
->shared
, h
)
4741 || !SYMBOL_REFERENCES_LOCAL (info
, h
)))
4744 if (tls_type
!= GOT_NORMAL
4745 && (info
->shared
|| indx
!= 0)
4746 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
4747 || h
->root
.type
!= bfd_link_hash_undefweak
))
4749 if (tls_type
& GOT_TLS_IE
)
4750 htab
->root
.srelgot
->size
+= sizeof (Elf32_External_Rela
);
4752 if (tls_type
& GOT_TLS_GD
)
4753 htab
->root
.srelgot
->size
+= sizeof (Elf32_External_Rela
);
4755 if ((tls_type
& GOT_TLS_GD
) && indx
!= 0)
4756 htab
->root
.srelgot
->size
+= sizeof (Elf32_External_Rela
);
4758 else if ((ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
4759 || h
->root
.type
!= bfd_link_hash_undefweak
)
4762 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, 0, h
)))
4763 htab
->root
.srelgot
->size
+= sizeof (Elf32_External_Rela
);
4766 h
->got
.offset
= (bfd_vma
) -1;
4768 if (eh
->dyn_relocs
== NULL
)
4771 /* In the shared -Bsymbolic case, discard space allocated for
4772 dynamic pc-relative relocs against symbols which turn out to be
4773 defined in regular objects. For the normal shared case, discard
4774 space for pc-relative relocs that have become local due to symbol
4775 visibility changes. */
4780 && (h
->forced_local
|| info
->symbolic
))
4782 struct elf32_nios2_dyn_relocs
**pp
;
4784 for (pp
= &eh
->dyn_relocs
; (p
= *pp
) != NULL
; )
4786 p
->count
-= p
->pc_count
;
4795 /* Also discard relocs on undefined weak syms with non-default
4797 if (eh
->dyn_relocs
!= NULL
4798 && h
->root
.type
== bfd_link_hash_undefweak
)
4800 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
4801 eh
->dyn_relocs
= NULL
;
4803 /* Make sure undefined weak symbols are output as a dynamic
4805 else if (h
->dynindx
== -1
4807 && !bfd_elf_link_record_dynamic_symbol (info
, h
))
4813 /* For the non-shared case, discard space for relocs against
4814 symbols which turn out to need copy relocs or are not
4818 && ((h
->def_dynamic
&& !h
->def_regular
)
4819 || (htab
->root
.dynamic_sections_created
4820 && (h
->root
.type
== bfd_link_hash_undefweak
4821 || h
->root
.type
== bfd_link_hash_undefined
))))
4823 /* Make sure this symbol is output as a dynamic symbol.
4824 Undefined weak syms won't yet be marked as dynamic. */
4825 if (h
->dynindx
== -1
4827 && !bfd_elf_link_record_dynamic_symbol (info
, h
))
4830 /* If that succeeded, we know we'll be keeping all the
4832 if (h
->dynindx
!= -1)
4836 eh
->dyn_relocs
= NULL
;
4841 /* Finally, allocate space. */
4842 for (p
= eh
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
4844 asection
*sreloc
= elf_section_data (p
->sec
)->sreloc
;
4845 sreloc
->size
+= p
->count
* sizeof (Elf32_External_Rela
);
4851 /* Implement elf_backend_size_dynamic_sections:
4852 Set the sizes of the dynamic sections. */
4854 nios2_elf32_size_dynamic_sections (bfd
*output_bfd ATTRIBUTE_UNUSED
,
4855 struct bfd_link_info
*info
)
4863 struct elf32_nios2_link_hash_table
*htab
;
4865 htab
= elf32_nios2_hash_table (info
);
4866 dynobj
= elf_hash_table (info
)->dynobj
;
4867 BFD_ASSERT (dynobj
!= NULL
);
4869 htab
->res_n_size
= 0;
4870 if (elf_hash_table (info
)->dynamic_sections_created
)
4872 /* Set the contents of the .interp section to the interpreter. */
4873 if (info
->executable
)
4875 s
= bfd_get_linker_section (dynobj
, ".interp");
4876 BFD_ASSERT (s
!= NULL
);
4877 s
->size
= sizeof ELF_DYNAMIC_INTERPRETER
;
4878 s
->contents
= (unsigned char *) ELF_DYNAMIC_INTERPRETER
;
4883 /* We may have created entries in the .rela.got section.
4884 However, if we are not creating the dynamic sections, we will
4885 not actually use these entries. Reset the size of .rela.got,
4886 which will cause it to get stripped from the output file
4888 s
= htab
->root
.srelgot
;
4893 /* Set up .got offsets for local syms, and space for local dynamic
4895 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
4897 bfd_signed_vma
*local_got
;
4898 bfd_signed_vma
*end_local_got
;
4899 char *local_tls_type
;
4900 bfd_size_type locsymcount
;
4901 Elf_Internal_Shdr
*symtab_hdr
;
4904 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
4907 for (s
= ibfd
->sections
; s
!= NULL
; s
= s
->next
)
4909 struct elf32_nios2_dyn_relocs
*p
;
4911 for (p
= elf_section_data (s
)->local_dynrel
; p
!= NULL
; p
= p
->next
)
4913 if (!bfd_is_abs_section (p
->sec
)
4914 && bfd_is_abs_section (p
->sec
->output_section
))
4916 /* Input section has been discarded, either because
4917 it is a copy of a linkonce section or due to
4918 linker script /DISCARD/, so we'll be discarding
4921 else if (p
->count
!= 0)
4923 srel
= elf_section_data (p
->sec
)->sreloc
;
4924 srel
->size
+= p
->count
* sizeof (Elf32_External_Rela
);
4925 if ((p
->sec
->output_section
->flags
& SEC_READONLY
) != 0)
4926 info
->flags
|= DF_TEXTREL
;
4931 local_got
= elf_local_got_refcounts (ibfd
);
4935 symtab_hdr
= &elf_tdata (ibfd
)->symtab_hdr
;
4936 locsymcount
= symtab_hdr
->sh_info
;
4937 end_local_got
= local_got
+ locsymcount
;
4938 local_tls_type
= elf32_nios2_local_got_tls_type (ibfd
);
4939 s
= htab
->root
.sgot
;
4940 srel
= htab
->root
.srelgot
;
4941 for (; local_got
< end_local_got
; ++local_got
, ++local_tls_type
)
4945 *local_got
= s
->size
;
4946 if (*local_tls_type
& GOT_TLS_GD
)
4947 /* TLS_GD relocs need an 8-byte structure in the GOT. */
4949 if (*local_tls_type
& GOT_TLS_IE
)
4951 if (*local_tls_type
== GOT_NORMAL
)
4954 if (info
->shared
|| *local_tls_type
== GOT_TLS_GD
)
4955 srel
->size
+= sizeof (Elf32_External_Rela
);
4958 *local_got
= (bfd_vma
) -1;
4962 if (htab
->tls_ldm_got
.refcount
> 0)
4964 /* Allocate two GOT entries and one dynamic relocation (if necessary)
4965 for R_NIOS2_TLS_LDM16 relocations. */
4966 htab
->tls_ldm_got
.offset
= htab
->root
.sgot
->size
;
4967 htab
->root
.sgot
->size
+= 8;
4969 htab
->root
.srelgot
->size
+= sizeof (Elf32_External_Rela
);
4972 htab
->tls_ldm_got
.offset
= -1;
4974 /* Allocate global sym .plt and .got entries, and space for global
4975 sym dynamic relocs. */
4976 elf_link_hash_traverse (& htab
->root
, allocate_dynrelocs
, info
);
4978 if (elf_hash_table (info
)->dynamic_sections_created
)
4980 /* If the .got section is more than 0x8000 bytes, we add
4981 0x8000 to the value of _gp_got, so that 16-bit relocations
4982 have a greater chance of working. */
4983 if (htab
->root
.sgot
->size
>= 0x8000
4984 && elf32_nios2_hash_table (info
)->h_gp_got
->root
.u
.def
.value
== 0)
4985 elf32_nios2_hash_table (info
)->h_gp_got
->root
.u
.def
.value
= 0x8000;
4988 /* The check_relocs and adjust_dynamic_symbol entry points have
4989 determined the sizes of the various dynamic sections. Allocate
4994 for (s
= dynobj
->sections
; s
!= NULL
; s
= s
->next
)
4998 if ((s
->flags
& SEC_LINKER_CREATED
) == 0)
5001 /* It's OK to base decisions on the section name, because none
5002 of the dynobj section names depend upon the input files. */
5003 name
= bfd_get_section_name (dynobj
, s
);
5005 if (strcmp (name
, ".plt") == 0)
5007 /* Remember whether there is a PLT. */
5010 /* Correct for the number of res_N branches. */
5011 if (plt
&& !info
->shared
)
5013 htab
->res_n_size
= (s
->size
-28) / 3;
5014 s
->size
+= htab
->res_n_size
;
5017 else if (CONST_STRNEQ (name
, ".rela"))
5023 /* We use the reloc_count field as a counter if we need
5024 to copy relocs into the output file. */
5028 else if (CONST_STRNEQ (name
, ".got"))
5030 else if (strcmp (name
, ".dynbss") != 0)
5031 /* It's not one of our sections, so don't allocate space. */
5036 /* If we don't need this section, strip it from the
5037 output file. This is mostly to handle .rela.bss and
5038 .rela.plt. We must create both sections in
5039 create_dynamic_sections, because they must be created
5040 before the linker maps input sections to output
5041 sections. The linker does that before
5042 adjust_dynamic_symbol is called, and it is that
5043 function which decides whether anything needs to go
5044 into these sections. */
5045 s
->flags
|= SEC_EXCLUDE
;
5049 if ((s
->flags
& SEC_HAS_CONTENTS
) == 0)
5052 /* Allocate memory for the section contents. */
5053 /* FIXME: This should be a call to bfd_alloc not bfd_zalloc.
5054 Unused entries should be reclaimed before the section's contents
5055 are written out, but at the moment this does not happen. Thus in
5056 order to prevent writing out garbage, we initialize the section's
5057 contents to zero. */
5058 s
->contents
= (bfd_byte
*) bfd_zalloc (dynobj
, s
->size
);
5059 if (s
->contents
== NULL
)
5063 /* Adjust dynamic symbols that point to the plt to account for the
5064 now-known number of resN slots. */
5065 if (htab
->res_n_size
)
5066 elf_link_hash_traverse (& htab
->root
, adjust_dynrelocs
, info
);
5068 if (elf_hash_table (info
)->dynamic_sections_created
)
5070 /* Add some entries to the .dynamic section. We fill in the
5071 values later, in elf_nios2_finish_dynamic_sections, but we
5072 must add the entries now so that we get the correct size for
5073 the .dynamic section. The DT_DEBUG entry is filled in by the
5074 dynamic linker and used by the debugger. */
5075 #define add_dynamic_entry(TAG, VAL) \
5076 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
5078 if (!info
->shared
&& !add_dynamic_entry (DT_DEBUG
, 0))
5081 if (got
&& !add_dynamic_entry (DT_PLTGOT
, 0))
5085 && (!add_dynamic_entry (DT_PLTRELSZ
, 0)
5086 || !add_dynamic_entry (DT_PLTREL
, DT_RELA
)
5087 || !add_dynamic_entry (DT_JMPREL
, 0)))
5091 && (!add_dynamic_entry (DT_RELA
, 0)
5092 || !add_dynamic_entry (DT_RELASZ
, 0)
5093 || !add_dynamic_entry (DT_RELAENT
, sizeof (Elf32_External_Rela
))))
5096 if (!info
->shared
&& !add_dynamic_entry (DT_NIOS2_GP
, 0))
5099 if ((info
->flags
& DF_TEXTREL
) != 0
5100 && !add_dynamic_entry (DT_TEXTREL
, 0))
5103 #undef add_dynamic_entry
5108 /* Free the derived linker hash table. */
5110 nios2_elf32_link_hash_table_free (bfd
*obfd
)
5112 struct elf32_nios2_link_hash_table
*htab
5113 = (struct elf32_nios2_link_hash_table
*) obfd
->link
.hash
;
5115 bfd_hash_table_free (&htab
->bstab
);
5116 _bfd_elf_link_hash_table_free (obfd
);
5119 /* Implement bfd_elf32_bfd_link_hash_table_create. */
5120 static struct bfd_link_hash_table
*
5121 nios2_elf32_link_hash_table_create (bfd
*abfd
)
5123 struct elf32_nios2_link_hash_table
*ret
;
5124 bfd_size_type amt
= sizeof (struct elf32_nios2_link_hash_table
);
5126 ret
= bfd_zmalloc (amt
);
5130 if (!_bfd_elf_link_hash_table_init (&ret
->root
, abfd
,
5133 elf32_nios2_link_hash_entry
),
5140 /* Init the stub hash table too. */
5141 if (!bfd_hash_table_init (&ret
->bstab
, stub_hash_newfunc
,
5142 sizeof (struct elf32_nios2_stub_hash_entry
)))
5144 _bfd_elf_link_hash_table_free (abfd
);
5147 ret
->root
.root
.hash_table_free
= nios2_elf32_link_hash_table_free
;
5149 return &ret
->root
.root
;
5152 /* Implement elf_backend_reloc_type_class. */
5153 static enum elf_reloc_type_class
5154 nios2_elf32_reloc_type_class (const struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
5155 const asection
*rel_sec ATTRIBUTE_UNUSED
,
5156 const Elf_Internal_Rela
*rela
)
5158 switch ((int) ELF32_R_TYPE (rela
->r_info
))
5160 case R_NIOS2_RELATIVE
:
5161 return reloc_class_relative
;
5162 case R_NIOS2_JUMP_SLOT
:
5163 return reloc_class_plt
;
5165 return reloc_class_copy
;
5167 return reloc_class_normal
;
5171 /* Return 1 if target is one of ours. */
5173 is_nios2_elf_target (const struct bfd_target
*targ
)
5175 return (targ
== &nios2_elf32_le_vec
5176 || targ
== &nios2_elf32_be_vec
);
5179 /* Implement elf_backend_add_symbol_hook.
5180 This hook is called by the linker when adding symbols from an object
5181 file. We use it to put .comm items in .sbss, and not .bss. */
5183 nios2_elf_add_symbol_hook (bfd
*abfd
,
5184 struct bfd_link_info
*info
,
5185 Elf_Internal_Sym
*sym
,
5186 const char **namep ATTRIBUTE_UNUSED
,
5187 flagword
*flagsp ATTRIBUTE_UNUSED
,
5193 if (sym
->st_shndx
== SHN_COMMON
5194 && !info
->relocatable
5195 && sym
->st_size
<= elf_gp_size (abfd
)
5196 && is_nios2_elf_target (info
->output_bfd
->xvec
))
5198 /* Common symbols less than or equal to -G nn bytes are automatically
5200 struct elf32_nios2_link_hash_table
*htab
;
5202 htab
= elf32_nios2_hash_table (info
);
5203 if (htab
->sbss
== NULL
)
5205 flagword flags
= SEC_IS_COMMON
| SEC_LINKER_CREATED
;
5207 dynobj
= elf_hash_table (info
)->dynobj
;
5211 htab
->sbss
= bfd_make_section_anyway_with_flags (dynobj
, ".sbss",
5213 if (htab
->sbss
== NULL
)
5218 *valp
= sym
->st_size
;
5224 /* Implement elf_backend_can_make_relative_eh_frame:
5225 Decide whether to attempt to turn absptr or lsda encodings in
5226 shared libraries into pcrel within the given input section. */
5228 nios2_elf32_can_make_relative_eh_frame (bfd
*input_bfd ATTRIBUTE_UNUSED
,
5229 struct bfd_link_info
*info
5231 asection
*eh_frame_section
5234 /* We can't use PC-relative encodings in the .eh_frame section. */
5238 /* Implement elf_backend_special_sections. */
5239 const struct bfd_elf_special_section elf32_nios2_special_sections
[] =
5241 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS
,
5242 SHF_ALLOC
+ SHF_WRITE
+ SHF_NIOS2_GPREL
},
5243 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS
,
5244 SHF_ALLOC
+ SHF_WRITE
+ SHF_NIOS2_GPREL
},
5245 { NULL
, 0, 0, 0, 0 }
5248 #define ELF_ARCH bfd_arch_nios2
5249 #define ELF_TARGET_ID NIOS2_ELF_DATA
5250 #define ELF_MACHINE_CODE EM_ALTERA_NIOS2
5252 /* The Nios II MMU uses a 4K page size. */
5254 #define ELF_MAXPAGESIZE 0x1000
5256 #define bfd_elf32_bfd_link_hash_table_create \
5257 nios2_elf32_link_hash_table_create
5259 /* Relocation table lookup macros. */
5261 #define bfd_elf32_bfd_reloc_type_lookup nios2_elf32_bfd_reloc_type_lookup
5262 #define bfd_elf32_bfd_reloc_name_lookup nios2_elf32_bfd_reloc_name_lookup
5264 /* JUMP_TABLE_LINK macros. */
5266 /* elf_info_to_howto (using RELA relocations). */
5268 #define elf_info_to_howto nios2_elf32_info_to_howto
5270 /* elf backend functions. */
5272 #define elf_backend_can_gc_sections 1
5273 #define elf_backend_can_refcount 1
5274 #define elf_backend_plt_readonly 1
5275 #define elf_backend_want_got_plt 1
5276 #define elf_backend_rela_normal 1
5278 #define elf_backend_relocate_section nios2_elf32_relocate_section
5279 #define elf_backend_section_flags nios2_elf32_section_flags
5280 #define elf_backend_fake_sections nios2_elf32_fake_sections
5281 #define elf_backend_check_relocs nios2_elf32_check_relocs
5283 #define elf_backend_gc_mark_hook nios2_elf32_gc_mark_hook
5284 #define elf_backend_gc_sweep_hook nios2_elf32_gc_sweep_hook
5285 #define elf_backend_create_dynamic_sections \
5286 nios2_elf32_create_dynamic_sections
5287 #define elf_backend_finish_dynamic_symbol nios2_elf32_finish_dynamic_symbol
5288 #define elf_backend_finish_dynamic_sections \
5289 nios2_elf32_finish_dynamic_sections
5290 #define elf_backend_adjust_dynamic_symbol nios2_elf32_adjust_dynamic_symbol
5291 #define elf_backend_reloc_type_class nios2_elf32_reloc_type_class
5292 #define elf_backend_size_dynamic_sections nios2_elf32_size_dynamic_sections
5293 #define elf_backend_add_symbol_hook nios2_elf_add_symbol_hook
5294 #define elf_backend_copy_indirect_symbol nios2_elf32_copy_indirect_symbol
5296 #define elf_backend_grok_prstatus nios2_grok_prstatus
5297 #define elf_backend_grok_psinfo nios2_grok_psinfo
5299 #undef elf_backend_can_make_relative_eh_frame
5300 #define elf_backend_can_make_relative_eh_frame \
5301 nios2_elf32_can_make_relative_eh_frame
5303 #define elf_backend_special_sections elf32_nios2_special_sections
5305 #define TARGET_LITTLE_SYM nios2_elf32_le_vec
5306 #define TARGET_LITTLE_NAME "elf32-littlenios2"
5307 #define TARGET_BIG_SYM nios2_elf32_be_vec
5308 #define TARGET_BIG_NAME "elf32-bignios2"
5310 #define elf_backend_got_header_size 12
5311 #define elf_backend_default_execstack 0
5313 #include "elf32-target.h"