1 /* 32-bit ELF support for Nios II.
2 Copyright (C) 2012-2014 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 bfd_elf32_littlenios2_vec
;
72 extern const bfd_target bfd_elf32_bignios2_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 0, /* 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_NIOS2_S16
, R_NIOS2_S16
},
768 {BFD_RELOC_NIOS2_U16
, R_NIOS2_U16
},
769 {BFD_RELOC_16_PCREL
, R_NIOS2_PCREL16
},
770 {BFD_RELOC_NIOS2_CALL26
, R_NIOS2_CALL26
},
771 {BFD_RELOC_NIOS2_IMM5
, R_NIOS2_IMM5
},
772 {BFD_RELOC_NIOS2_CACHE_OPX
, R_NIOS2_CACHE_OPX
},
773 {BFD_RELOC_NIOS2_IMM6
, R_NIOS2_IMM6
},
774 {BFD_RELOC_NIOS2_IMM8
, R_NIOS2_IMM8
},
775 {BFD_RELOC_NIOS2_HI16
, R_NIOS2_HI16
},
776 {BFD_RELOC_NIOS2_LO16
, R_NIOS2_LO16
},
777 {BFD_RELOC_NIOS2_HIADJ16
, R_NIOS2_HIADJ16
},
778 {BFD_RELOC_32
, R_NIOS2_BFD_RELOC_32
},
779 {BFD_RELOC_16
, R_NIOS2_BFD_RELOC_16
},
780 {BFD_RELOC_8
, R_NIOS2_BFD_RELOC_8
},
781 {BFD_RELOC_NIOS2_GPREL
, R_NIOS2_GPREL
},
782 {BFD_RELOC_VTABLE_INHERIT
, R_NIOS2_GNU_VTINHERIT
},
783 {BFD_RELOC_VTABLE_ENTRY
, R_NIOS2_GNU_VTENTRY
},
784 {BFD_RELOC_NIOS2_UJMP
, R_NIOS2_UJMP
},
785 {BFD_RELOC_NIOS2_CJMP
, R_NIOS2_CJMP
},
786 {BFD_RELOC_NIOS2_CALLR
, R_NIOS2_CALLR
},
787 {BFD_RELOC_NIOS2_ALIGN
, R_NIOS2_ALIGN
},
788 {BFD_RELOC_NIOS2_GOT16
, R_NIOS2_GOT16
},
789 {BFD_RELOC_NIOS2_CALL16
, R_NIOS2_CALL16
},
790 {BFD_RELOC_NIOS2_GOTOFF_LO
, R_NIOS2_GOTOFF_LO
},
791 {BFD_RELOC_NIOS2_GOTOFF_HA
, R_NIOS2_GOTOFF_HA
},
792 {BFD_RELOC_NIOS2_PCREL_LO
, R_NIOS2_PCREL_LO
},
793 {BFD_RELOC_NIOS2_PCREL_HA
, R_NIOS2_PCREL_HA
},
794 {BFD_RELOC_NIOS2_TLS_GD16
, R_NIOS2_TLS_GD16
},
795 {BFD_RELOC_NIOS2_TLS_LDM16
, R_NIOS2_TLS_LDM16
},
796 {BFD_RELOC_NIOS2_TLS_LDO16
, R_NIOS2_TLS_LDO16
},
797 {BFD_RELOC_NIOS2_TLS_IE16
, R_NIOS2_TLS_IE16
},
798 {BFD_RELOC_NIOS2_TLS_LE16
, R_NIOS2_TLS_LE16
},
799 {BFD_RELOC_NIOS2_TLS_DTPMOD
, R_NIOS2_TLS_DTPMOD
},
800 {BFD_RELOC_NIOS2_TLS_DTPREL
, R_NIOS2_TLS_DTPREL
},
801 {BFD_RELOC_NIOS2_TLS_TPREL
, R_NIOS2_TLS_TPREL
},
802 {BFD_RELOC_NIOS2_COPY
, R_NIOS2_COPY
},
803 {BFD_RELOC_NIOS2_GLOB_DAT
, R_NIOS2_GLOB_DAT
},
804 {BFD_RELOC_NIOS2_JUMP_SLOT
, R_NIOS2_JUMP_SLOT
},
805 {BFD_RELOC_NIOS2_RELATIVE
, R_NIOS2_RELATIVE
},
806 {BFD_RELOC_NIOS2_GOTOFF
, R_NIOS2_GOTOFF
},
807 {BFD_RELOC_NIOS2_CALL26_NOAT
, R_NIOS2_CALL26_NOAT
},
808 {BFD_RELOC_NIOS2_GOT_LO
, R_NIOS2_GOT_LO
},
809 {BFD_RELOC_NIOS2_GOT_HA
, R_NIOS2_GOT_HA
},
810 {BFD_RELOC_NIOS2_CALL_LO
, R_NIOS2_CALL_LO
},
811 {BFD_RELOC_NIOS2_CALL_HA
, R_NIOS2_CALL_HA
},
814 enum elf32_nios2_stub_type
816 nios2_stub_call26_before
,
817 nios2_stub_call26_after
,
821 struct elf32_nios2_stub_hash_entry
823 /* Base hash table entry structure. */
824 struct bfd_hash_entry bh_root
;
826 /* The stub section. */
829 /* Offset within stub_sec of the beginning of this stub. */
832 /* Given the symbol's value and its section we can determine its final
833 value when building the stubs (so the stub knows where to jump. */
834 bfd_vma target_value
;
835 asection
*target_section
;
837 enum elf32_nios2_stub_type stub_type
;
839 /* The symbol table entry, if any, that this was derived from. */
840 struct elf32_nios2_link_hash_entry
*hh
;
842 /* And the reloc addend that this was derived from. */
845 /* Where this stub is being called from, or, in the case of combined
846 stub sections, the first input section in the group. */
850 #define nios2_stub_hash_entry(ent) \
851 ((struct elf32_nios2_stub_hash_entry *)(ent))
853 #define nios2_stub_hash_lookup(table, string, create, copy) \
854 ((struct elf32_nios2_stub_hash_entry *) \
855 bfd_hash_lookup ((table), (string), (create), (copy)))
858 /* The Nios II linker needs to keep track of the number of relocs that it
859 decides to copy as dynamic relocs in check_relocs for each symbol.
860 This is so that it can later discard them if they are found to be
861 unnecessary. We store the information in a field extending the
862 regular ELF linker hash table. */
864 struct elf32_nios2_dyn_relocs
866 struct elf32_nios2_dyn_relocs
*next
;
868 /* The input section of the reloc. */
871 /* Total number of relocs copied for the input section. */
874 /* Number of pc-relative relocs copied for the input section. */
875 bfd_size_type pc_count
;
878 /* Nios II ELF linker hash entry. */
880 struct elf32_nios2_link_hash_entry
882 struct elf_link_hash_entry root
;
884 /* A pointer to the most recently used stub hash entry against this
886 struct elf32_nios2_stub_hash_entry
*hsh_cache
;
888 /* Track dynamic relocs copied for this symbol. */
889 struct elf32_nios2_dyn_relocs
*dyn_relocs
;
891 #define GOT_UNKNOWN 0
895 unsigned char tls_type
;
897 /* We need to detect and take special action for symbols which are only
898 referenced with %call() and not with %got(). Such symbols do not need
899 a dynamic GOT reloc in shared objects, only a dynamic PLT reloc. Lazy
900 linking will not work if the dynamic GOT reloc exists.
901 To check for this condition efficiently, we compare got_types_used against
903 (got_types_used & (GOT_USED | CALL_USED)) == CALL_USED.
907 unsigned char got_types_used
;
910 #define elf32_nios2_hash_entry(ent) \
911 ((struct elf32_nios2_link_hash_entry *) (ent))
913 /* Get the Nios II elf linker hash table from a link_info structure. */
914 #define elf32_nios2_hash_table(info) \
915 ((struct elf32_nios2_link_hash_table *) ((info)->hash))
917 /* Nios II ELF linker hash table. */
918 struct elf32_nios2_link_hash_table
920 /* The main hash table. */
921 struct elf_link_hash_table root
;
923 /* The stub hash table. */
924 struct bfd_hash_table bstab
;
926 /* Linker stub bfd. */
929 /* Linker call-backs. */
930 asection
* (*add_stub_section
) (const char *, asection
*, bfd_boolean
);
931 void (*layout_sections_again
) (void);
933 /* Array to keep track of which stub sections have been created, and
934 information on stub grouping. */
937 /* These are the section to which stubs in the group will be
939 asection
*first_sec
, *last_sec
;
940 /* The stub sections. There might be stubs inserted either before
941 or after the real section.*/
942 asection
*first_stub_sec
, *last_stub_sec
;
945 /* Assorted information used by nios2_elf32_size_stubs. */
946 unsigned int bfd_count
;
948 asection
**input_list
;
949 Elf_Internal_Sym
**all_local_syms
;
951 /* Short-cuts to get to dynamic linker sections. */
956 /* GOT pointer symbol _gp_got. */
957 struct elf_link_hash_entry
*h_gp_got
;
960 bfd_signed_vma refcount
;
964 /* Small local sym cache. */
965 struct sym_cache sym_cache
;
970 struct nios2_elf32_obj_tdata
972 struct elf_obj_tdata root
;
974 /* tls_type for each local got entry. */
975 char *local_got_tls_type
;
977 /* TRUE if TLS GD relocs have been seen for this object. */
978 bfd_boolean has_tlsgd
;
981 #define elf32_nios2_tdata(abfd) \
982 ((struct nios2_elf32_obj_tdata *) (abfd)->tdata.any)
984 #define elf32_nios2_local_got_tls_type(abfd) \
985 (elf32_nios2_tdata (abfd)->local_got_tls_type)
987 /* The name of the dynamic interpreter. This is put in the .interp
989 #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
991 /* PLT implementation for position-dependent code. */
992 static const bfd_vma nios2_plt_entry
[] = { /* .PLTn: */
993 0x03c00034, /* movhi r15, %hiadj(plt_got_slot_address) */
994 0x7bc00017, /* ldw r15, %lo(plt_got_slot_address)(r15) */
995 0x7800683a /* jmp r15 */
998 static const bfd_vma nios2_plt0_entry
[] = { /* .PLTresolve */
999 0x03800034, /* movhi r14, %hiadj(res_0) */
1000 0x73800004, /* addi r14, r14, %lo(res_0) */
1001 0x7b9fc83a, /* sub r15, r15, r14 */
1002 0x03400034, /* movhi r13, %hiadj(_GLOBAL_OFFSET_TABLE_) */
1003 0x6b800017, /* ldw r14, %lo(_GLOBAL_OFFSET_TABLE_+4)(r13) */
1004 0x6b400017, /* ldw r13, %lo(_GLOBAL_OFFSET_TABLE_+8)(r13) */
1005 0x6800683a /* jmp r13 */
1008 /* PLT implementation for position-independent code. */
1009 static const bfd_vma nios2_so_plt_entry
[] = { /* .PLTn */
1010 0x03c00034, /* movhi r15, %hiadj(index * 4) */
1011 0x7bc00004, /* addi r15, r15, %lo(index * 4) */
1012 0x00000006 /* br .PLTresolve */
1015 static const bfd_vma nios2_so_plt0_entry
[] = { /* .PLTresolve */
1016 0x001ce03a, /* nextpc r14 */
1017 0x03400034, /* movhi r13, %hiadj(_GLOBAL_OFFSET_TABLE_) */
1018 0x6b9b883a, /* add r13, r13, r14 */
1019 0x6b800017, /* ldw r14, %lo(_GLOBAL_OFFSET_TABLE_+4)(r13) */
1020 0x6b400017, /* ldw r13, %lo(_GLOBAL_OFFSET_TABLE_+8)(r13) */
1021 0x6800683a /* jmp r13 */
1025 static const bfd_vma nios2_call26_stub_entry
[] = {
1026 0x00400034, /* orhi at, r0, %hiadj(dest) */
1027 0x08400004, /* addi at, at, %lo(dest) */
1028 0x0800683a /* jmp at */
1031 /* Install 16-bit immediate value VALUE at offset OFFSET into section SEC. */
1033 nios2_elf32_install_imm16 (asection
*sec
, bfd_vma offset
, bfd_vma value
)
1035 bfd_vma word
= bfd_get_32 (sec
->owner
, sec
->contents
+ offset
);
1037 BFD_ASSERT(value
<= 0xffff);
1039 bfd_put_32 (sec
->owner
, word
| ((value
& 0xffff) << 6),
1040 sec
->contents
+ offset
);
1043 /* Install COUNT 32-bit values DATA starting at offset OFFSET into
1046 nios2_elf32_install_data (asection
*sec
, const bfd_vma
*data
, bfd_vma offset
,
1051 bfd_put_32 (sec
->owner
, *data
, sec
->contents
+ offset
);
1057 /* The usual way of loading a 32-bit constant into a Nios II register is to
1058 load the high 16 bits in one instruction and then add the low 16 bits with
1059 a signed add. This means that the high halfword needs to be adjusted to
1060 compensate for the sign bit of the low halfword. This function returns the
1061 adjusted high halfword for a given 32-bit constant. */
1063 bfd_vma
hiadj (bfd_vma symbol_value
)
1065 return ((symbol_value
+ 0x8000) >> 16) & 0xffff;
1068 /* Implement elf_backend_grok_prstatus:
1069 Support for core dump NOTE sections. */
1071 nios2_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
1076 switch (note
->descsz
)
1081 case 212: /* Linux/Nios II */
1083 elf_tdata (abfd
)->core
->signal
= bfd_get_16 (abfd
, note
->descdata
+ 12);
1086 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, note
->descdata
+ 24);
1095 /* Make a ".reg/999" section. */
1096 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
1097 size
, note
->descpos
+ offset
);
1100 /* Implement elf_backend_grok_psinfo. */
1102 nios2_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
1104 switch (note
->descsz
)
1109 case 124: /* Linux/Nios II elf_prpsinfo */
1110 elf_tdata (abfd
)->core
->program
1111 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 28, 16);
1112 elf_tdata (abfd
)->core
->command
1113 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 44, 80);
1116 /* Note that for some reason, a spurious space is tacked
1117 onto the end of the args in some (at least one anyway)
1118 implementations, so strip it off if it exists. */
1121 char *command
= elf_tdata (abfd
)->core
->command
;
1122 int n
= strlen (command
);
1124 if (0 < n
&& command
[n
- 1] == ' ')
1125 command
[n
- 1] = '\0';
1131 /* Assorted hash table functions. */
1133 /* Initialize an entry in the stub hash table. */
1134 static struct bfd_hash_entry
*
1135 stub_hash_newfunc (struct bfd_hash_entry
*entry
,
1136 struct bfd_hash_table
*table
,
1139 /* Allocate the structure if it has not already been allocated by a
1143 entry
= bfd_hash_allocate (table
,
1144 sizeof (struct elf32_nios2_stub_hash_entry
));
1149 /* Call the allocation method of the superclass. */
1150 entry
= bfd_hash_newfunc (entry
, table
, string
);
1153 struct elf32_nios2_stub_hash_entry
*hsh
;
1155 /* Initialize the local fields. */
1156 hsh
= (struct elf32_nios2_stub_hash_entry
*) entry
;
1157 hsh
->stub_sec
= NULL
;
1158 hsh
->stub_offset
= 0;
1159 hsh
->target_value
= 0;
1160 hsh
->target_section
= NULL
;
1161 hsh
->stub_type
= nios2_stub_none
;
1169 /* Create an entry in a Nios II ELF linker hash table. */
1170 static struct bfd_hash_entry
*
1171 link_hash_newfunc (struct bfd_hash_entry
*entry
,
1172 struct bfd_hash_table
*table
, const char *string
)
1174 /* Allocate the structure if it has not already been allocated by a
1178 entry
= bfd_hash_allocate (table
,
1179 sizeof (struct elf32_nios2_link_hash_entry
));
1184 /* Call the allocation method of the superclass. */
1185 entry
= _bfd_elf_link_hash_newfunc (entry
, table
, string
);
1188 struct elf32_nios2_link_hash_entry
*eh
;
1190 eh
= (struct elf32_nios2_link_hash_entry
*) entry
;
1191 eh
->hsh_cache
= NULL
;
1192 eh
->dyn_relocs
= NULL
;
1193 eh
->tls_type
= GOT_UNKNOWN
;
1194 eh
->got_types_used
= 0;
1200 /* Section name for stubs is the associated section name plus this
1202 #define STUB_SUFFIX ".stub"
1204 /* Build a name for an entry in the stub hash table. */
1206 nios2_stub_name (const asection
*input_section
,
1207 const asection
*sym_sec
,
1208 const struct elf32_nios2_link_hash_entry
*hh
,
1209 const Elf_Internal_Rela
*rel
,
1210 enum elf32_nios2_stub_type stub_type
)
1214 char stubpos
= (stub_type
== nios2_stub_call26_before
) ? 'b' : 'a';
1218 len
= 8 + 1 + 1 + 1+ strlen (hh
->root
.root
.root
.string
) + 1 + 8 + 1;
1219 stub_name
= bfd_malloc (len
);
1220 if (stub_name
!= NULL
)
1222 sprintf (stub_name
, "%08x_%c_%s+%x",
1223 input_section
->id
& 0xffffffff,
1225 hh
->root
.root
.root
.string
,
1226 (int) rel
->r_addend
& 0xffffffff);
1231 len
= 8 + 1 + 1 + 1+ 8 + 1 + 8 + 1 + 8 + 1;
1232 stub_name
= bfd_malloc (len
);
1233 if (stub_name
!= NULL
)
1235 sprintf (stub_name
, "%08x_%c_%x:%x+%x",
1236 input_section
->id
& 0xffffffff,
1238 sym_sec
->id
& 0xffffffff,
1239 (int) ELF32_R_SYM (rel
->r_info
) & 0xffffffff,
1240 (int) rel
->r_addend
& 0xffffffff);
1246 /* Look up an entry in the stub hash. Stub entries are cached because
1247 creating the stub name takes a bit of time. */
1248 static struct elf32_nios2_stub_hash_entry
*
1249 nios2_get_stub_entry (const asection
*input_section
,
1250 const asection
*sym_sec
,
1251 struct elf32_nios2_link_hash_entry
*hh
,
1252 const Elf_Internal_Rela
*rel
,
1253 struct elf32_nios2_link_hash_table
*htab
,
1254 enum elf32_nios2_stub_type stub_type
)
1256 struct elf32_nios2_stub_hash_entry
*hsh
;
1257 const asection
*id_sec
;
1259 /* If this input section is part of a group of sections sharing one
1260 stub section, then use the id of the first/last section in the group,
1261 depending on the stub section placement relative to the group.
1262 Stub names need to include a section id, as there may well be
1263 more than one stub used to reach say, printf, and we need to
1264 distinguish between them. */
1265 if (stub_type
== nios2_stub_call26_before
)
1266 id_sec
= htab
->stub_group
[input_section
->id
].first_sec
;
1268 id_sec
= htab
->stub_group
[input_section
->id
].last_sec
;
1270 if (hh
!= NULL
&& hh
->hsh_cache
!= NULL
1271 && hh
->hsh_cache
->hh
== hh
1272 && hh
->hsh_cache
->id_sec
== id_sec
1273 && hh
->hsh_cache
->stub_type
== stub_type
)
1275 hsh
= hh
->hsh_cache
;
1281 stub_name
= nios2_stub_name (id_sec
, sym_sec
, hh
, rel
, stub_type
);
1282 if (stub_name
== NULL
)
1285 hsh
= nios2_stub_hash_lookup (&htab
->bstab
,
1286 stub_name
, FALSE
, FALSE
);
1289 hh
->hsh_cache
= hsh
;
1297 /* Add a new stub entry to the stub hash. Not all fields of the new
1298 stub entry are initialised. */
1299 static struct elf32_nios2_stub_hash_entry
*
1300 nios2_add_stub (const char *stub_name
,
1302 struct elf32_nios2_link_hash_table
*htab
,
1303 enum elf32_nios2_stub_type stub_type
)
1307 asection
**secptr
, **linkptr
;
1308 struct elf32_nios2_stub_hash_entry
*hsh
;
1311 if (stub_type
== nios2_stub_call26_before
)
1313 link_sec
= htab
->stub_group
[section
->id
].first_sec
;
1314 secptr
= &(htab
->stub_group
[section
->id
].first_stub_sec
);
1315 linkptr
= &(htab
->stub_group
[link_sec
->id
].first_stub_sec
);
1320 link_sec
= htab
->stub_group
[section
->id
].last_sec
;
1321 secptr
= &(htab
->stub_group
[section
->id
].last_stub_sec
);
1322 linkptr
= &(htab
->stub_group
[link_sec
->id
].last_stub_sec
);
1326 if (stub_sec
== NULL
)
1328 stub_sec
= *linkptr
;
1329 if (stub_sec
== NULL
)
1335 namelen
= strlen (link_sec
->name
);
1336 len
= namelen
+ sizeof (STUB_SUFFIX
);
1337 s_name
= bfd_alloc (htab
->stub_bfd
, len
);
1341 memcpy (s_name
, link_sec
->name
, namelen
);
1342 memcpy (s_name
+ namelen
, STUB_SUFFIX
, sizeof (STUB_SUFFIX
));
1344 stub_sec
= (*htab
->add_stub_section
) (s_name
, link_sec
, afterp
);
1345 if (stub_sec
== NULL
)
1347 *linkptr
= stub_sec
;
1352 /* Enter this entry into the linker stub hash table. */
1353 hsh
= nios2_stub_hash_lookup (&htab
->bstab
, stub_name
,
1357 (*_bfd_error_handler
) (_("%B: cannot create stub entry %s"),
1363 hsh
->stub_sec
= stub_sec
;
1364 hsh
->stub_offset
= 0;
1365 hsh
->id_sec
= link_sec
;
1369 /* Set up various things so that we can make a list of input sections
1370 for each output section included in the link. Returns -1 on error,
1371 0 when no stubs will be needed, and 1 on success. */
1373 nios2_elf32_setup_section_lists (bfd
*output_bfd
, struct bfd_link_info
*info
)
1376 unsigned int bfd_count
;
1377 int top_id
, top_index
;
1379 asection
**input_list
, **list
;
1381 struct elf32_nios2_link_hash_table
*htab
= elf32_nios2_hash_table (info
);
1383 /* Count the number of input BFDs and find the top input section id. */
1384 for (input_bfd
= info
->input_bfds
, bfd_count
= 0, top_id
= 0;
1386 input_bfd
= input_bfd
->link_next
)
1389 for (section
= input_bfd
->sections
;
1391 section
= section
->next
)
1393 if (top_id
< section
->id
)
1394 top_id
= section
->id
;
1398 htab
->bfd_count
= bfd_count
;
1400 amt
= sizeof (struct map_stub
) * (top_id
+ 1);
1401 htab
->stub_group
= bfd_zmalloc (amt
);
1402 if (htab
->stub_group
== NULL
)
1405 /* We can't use output_bfd->section_count here to find the top output
1406 section index as some sections may have been removed, and
1407 strip_excluded_output_sections doesn't renumber the indices. */
1408 for (section
= output_bfd
->sections
, top_index
= 0;
1410 section
= section
->next
)
1412 if (top_index
< section
->index
)
1413 top_index
= section
->index
;
1416 htab
->top_index
= top_index
;
1417 amt
= sizeof (asection
*) * (top_index
+ 1);
1418 input_list
= bfd_malloc (amt
);
1419 htab
->input_list
= input_list
;
1420 if (input_list
== NULL
)
1423 /* For sections we aren't interested in, mark their entries with a
1424 value we can check later. */
1425 list
= input_list
+ top_index
;
1427 *list
= bfd_abs_section_ptr
;
1428 while (list
-- != input_list
);
1430 for (section
= output_bfd
->sections
;
1432 section
= section
->next
)
1434 /* FIXME: This is a bit of hack. Currently our .ctors and .dtors
1435 * have PC relative relocs in them but no code flag set. */
1436 if (((section
->flags
& SEC_CODE
) != 0) ||
1437 strcmp(".ctors", section
->name
) ||
1438 strcmp(".dtors", section
->name
))
1439 input_list
[section
->index
] = NULL
;
1445 /* The linker repeatedly calls this function for each input section,
1446 in the order that input sections are linked into output sections.
1447 Build lists of input sections to determine groupings between which
1448 we may insert linker stubs. */
1450 nios2_elf32_next_input_section (struct bfd_link_info
*info
, asection
*isec
)
1452 struct elf32_nios2_link_hash_table
*htab
= elf32_nios2_hash_table (info
);
1454 if (isec
->output_section
->index
<= htab
->top_index
)
1456 asection
**list
= htab
->input_list
+ isec
->output_section
->index
;
1457 if (*list
!= bfd_abs_section_ptr
)
1459 /* Steal the last_sec pointer for our list.
1460 This happens to make the list in reverse order,
1461 which is what we want. */
1462 htab
->stub_group
[isec
->id
].last_sec
= *list
;
1468 /* Segment mask for CALL26 relocation relaxation. */
1469 #define CALL26_SEGMENT(x) ((x) & 0xf0000000)
1471 /* Fudge factor for approximate maximum size of all stubs that might
1472 be inserted by the linker. This does not actually limit the number
1473 of stubs that might be inserted, and only affects strategy for grouping
1474 and placement of stubs. Perhaps this should be computed based on number
1475 of relocations seen, or be specifiable on the command line. */
1476 #define MAX_STUB_SECTION_SIZE 0xffff
1478 /* See whether we can group stub sections together. Grouping stub
1479 sections may result in fewer stubs. More importantly, we need to
1480 put all .init* and .fini* stubs at the end of the .init or
1481 .fini output sections respectively, because glibc splits the
1482 _init and _fini functions into multiple parts. Putting a stub in
1483 the middle of a function is not a good idea.
1484 Rather than computing groups of a maximum fixed size, for Nios II
1485 CALL26 relaxation it makes more sense to compute the groups based on
1486 sections that fit within a 256MB address segment. Also do not allow
1487 a group to span more than one output section, since different output
1488 sections might correspond to different memory banks on a bare-metal
1491 group_sections (struct elf32_nios2_link_hash_table
*htab
)
1493 asection
**list
= htab
->input_list
+ htab
->top_index
;
1496 /* The list is in reverse order so we'll search backwards looking
1497 for the first section that begins in the same memory segment,
1498 marking sections along the way to point at the tail for this
1500 asection
*tail
= *list
;
1501 if (tail
== bfd_abs_section_ptr
)
1503 while (tail
!= NULL
)
1505 bfd_vma start
= tail
->output_section
->vma
+ tail
->output_offset
;
1506 bfd_vma end
= start
+ tail
->size
;
1507 bfd_vma segment
= CALL26_SEGMENT (end
);
1510 if (segment
!= CALL26_SEGMENT (start
)
1511 || segment
!= CALL26_SEGMENT (end
+ MAX_STUB_SECTION_SIZE
))
1512 /* This section spans more than one memory segment, or is
1513 close enough to the end of the segment that adding stub
1514 sections before it might cause it to move so that it
1515 spans memory segments, or that stubs added at the end of
1516 this group might overflow into the next memory segment.
1517 Put it in a group by itself to localize the effects. */
1519 prev
= htab
->stub_group
[tail
->id
].last_sec
;
1520 htab
->stub_group
[tail
->id
].last_sec
= tail
;
1521 htab
->stub_group
[tail
->id
].first_sec
= tail
;
1524 /* Collect more sections for this group. */
1526 asection
*curr
, *first
;
1527 for (curr
= tail
; ; curr
= prev
)
1529 prev
= htab
->stub_group
[curr
->id
].last_sec
;
1531 || tail
->output_section
!= prev
->output_section
1532 || (CALL26_SEGMENT (prev
->output_section
->vma
1533 + prev
->output_offset
)
1538 for (curr
= tail
; ; curr
= prev
)
1540 prev
= htab
->stub_group
[curr
->id
].last_sec
;
1541 htab
->stub_group
[curr
->id
].last_sec
= tail
;
1542 htab
->stub_group
[curr
->id
].first_sec
= first
;
1548 /* Reset tail for the next group. */
1552 while (list
-- != htab
->input_list
);
1553 free (htab
->input_list
);
1556 /* Determine the type of stub needed, if any, for a call. */
1557 static enum elf32_nios2_stub_type
1558 nios2_type_of_stub (asection
*input_sec
,
1559 const Elf_Internal_Rela
*rel
,
1560 struct elf32_nios2_link_hash_entry
*hh
,
1561 struct elf32_nios2_link_hash_table
*htab
,
1562 bfd_vma destination
,
1563 struct bfd_link_info
*info ATTRIBUTE_UNUSED
)
1565 bfd_vma location
, segment
, start
, end
;
1566 asection
*s0
, *s1
, *s
;
1569 !(hh
->root
.root
.type
== bfd_link_hash_defined
1570 || hh
->root
.root
.type
== bfd_link_hash_defweak
))
1571 return nios2_stub_none
;
1573 /* Determine where the call point is. */
1574 location
= (input_sec
->output_section
->vma
1575 + input_sec
->output_offset
+ rel
->r_offset
);
1576 segment
= CALL26_SEGMENT (location
);
1578 /* Nios II CALL and JMPI instructions can transfer control to addresses
1579 within the same 256MB segment as the PC. */
1580 if (segment
== CALL26_SEGMENT (destination
))
1581 return nios2_stub_none
;
1583 /* Find the start and end addresses of the stub group. Also account for
1584 any already-created stub sections for this group. Note that for stubs
1585 in the end section, only the first instruction of the last stub
1586 (12 bytes long) needs to be within range. */
1587 s0
= htab
->stub_group
[input_sec
->id
].first_sec
;
1588 s
= htab
->stub_group
[s0
->id
].first_stub_sec
;
1589 if (s
!= NULL
&& s
->size
> 0)
1590 start
= s
->output_section
->vma
+ s
->output_offset
;
1592 start
= s0
->output_section
->vma
+ s0
->output_offset
;
1594 s1
= htab
->stub_group
[input_sec
->id
].last_sec
;
1595 s
= htab
->stub_group
[s1
->id
].last_stub_sec
;
1596 if (s
!= NULL
&& s
->size
> 0)
1597 end
= s
->output_section
->vma
+ s
->output_offset
+ s
->size
- 8;
1599 end
= s1
->output_section
->vma
+ s1
->output_offset
+ s1
->size
;
1601 BFD_ASSERT (start
< end
);
1602 BFD_ASSERT (start
<= location
);
1603 BFD_ASSERT (location
< end
);
1605 /* Put stubs at the end of the group unless that is not a valid
1606 location and the beginning of the group is. It might be that
1607 neither the beginning nor end works if we have an input section
1608 so large that it spans multiple segment boundaries. In that
1609 case, punt; the end result will be a relocation overflow error no
1610 matter what we do here.
1612 Note that adding stubs pushes up the addresses of all subsequent
1613 sections, so that stubs allocated on one pass through the
1614 relaxation loop may not be valid on the next pass. (E.g., we may
1615 allocate a stub at the beginning of the section on one pass and
1616 find that the call site has been bumped into the next memory
1617 segment on the next pass.) The important thing to note is that
1618 we never try to reclaim the space allocated to such unused stubs,
1619 so code size and section addresses can only increase with each
1620 iteration. Accounting for the start and end addresses of the
1621 already-created stub sections ensures that when the algorithm
1622 converges, it converges accurately, with the entire appropriate
1623 stub section accessible from the call site and not just the
1624 address at the start or end of the stub group proper. */
1626 if (segment
== CALL26_SEGMENT (end
))
1627 return nios2_stub_call26_after
;
1628 else if (segment
== CALL26_SEGMENT (start
))
1629 return nios2_stub_call26_before
;
1631 /* Perhaps this should be a dedicated error code. */
1632 return nios2_stub_none
;
1636 nios2_build_one_stub (struct bfd_hash_entry
*gen_entry
, void *in_arg ATTRIBUTE_UNUSED
)
1638 struct elf32_nios2_stub_hash_entry
*hsh
1639 = (struct elf32_nios2_stub_hash_entry
*) gen_entry
;
1640 asection
*stub_sec
= hsh
->stub_sec
;
1643 /* Make a note of the offset within the stubs for this entry. */
1644 hsh
->stub_offset
= stub_sec
->size
;
1646 switch (hsh
->stub_type
)
1648 case nios2_stub_call26_before
:
1649 case nios2_stub_call26_after
:
1650 /* A call26 stub looks like:
1651 orhi at, %hiadj(dest)
1652 addi at, at, %lo(dest)
1654 Note that call/jmpi instructions can't be used in PIC code
1655 so there is no reason for the stub to be PIC, either. */
1656 sym_value
= (hsh
->target_value
1657 + hsh
->target_section
->output_offset
1658 + hsh
->target_section
->output_section
->vma
1661 nios2_elf32_install_data (stub_sec
, nios2_call26_stub_entry
,
1662 hsh
->stub_offset
, 3);
1663 nios2_elf32_install_imm16 (stub_sec
, hsh
->stub_offset
,
1665 nios2_elf32_install_imm16 (stub_sec
, hsh
->stub_offset
+ 4,
1666 (sym_value
& 0xffff));
1667 stub_sec
->size
+= 12;
1677 /* As above, but don't actually build the stub. Just bump offset so
1678 we know stub section sizes. */
1680 nios2_size_one_stub (struct bfd_hash_entry
*gen_entry
, void *in_arg ATTRIBUTE_UNUSED
)
1682 struct elf32_nios2_stub_hash_entry
*hsh
1683 = (struct elf32_nios2_stub_hash_entry
*) gen_entry
;
1685 switch (hsh
->stub_type
)
1687 case nios2_stub_call26_before
:
1688 case nios2_stub_call26_after
:
1689 hsh
->stub_sec
->size
+= 12;
1698 /* Read in all local syms for all input bfds.
1699 Returns -1 on error, 0 otherwise. */
1702 get_local_syms (bfd
*output_bfd ATTRIBUTE_UNUSED
, bfd
*input_bfd
,
1703 struct bfd_link_info
*info
)
1705 unsigned int bfd_indx
;
1706 Elf_Internal_Sym
*local_syms
, **all_local_syms
;
1707 struct elf32_nios2_link_hash_table
*htab
= elf32_nios2_hash_table (info
);
1709 /* We want to read in symbol extension records only once. To do this
1710 we need to read in the local symbols in parallel and save them for
1711 later use; so hold pointers to the local symbols in an array. */
1712 bfd_size_type amt
= sizeof (Elf_Internal_Sym
*) * htab
->bfd_count
;
1713 all_local_syms
= bfd_zmalloc (amt
);
1714 htab
->all_local_syms
= all_local_syms
;
1715 if (all_local_syms
== NULL
)
1718 /* Walk over all the input BFDs, swapping in local symbols. */
1721 input_bfd
= input_bfd
->link_next
, bfd_indx
++)
1723 Elf_Internal_Shdr
*symtab_hdr
;
1725 /* We'll need the symbol table in a second. */
1726 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
1727 if (symtab_hdr
->sh_info
== 0)
1730 /* We need an array of the local symbols attached to the input bfd. */
1731 local_syms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
1732 if (local_syms
== NULL
)
1734 local_syms
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
,
1735 symtab_hdr
->sh_info
, 0,
1737 /* Cache them for elf_link_input_bfd. */
1738 symtab_hdr
->contents
= (unsigned char *) local_syms
;
1740 if (local_syms
== NULL
)
1743 all_local_syms
[bfd_indx
] = local_syms
;
1749 /* Determine and set the size of the stub section for a final link. */
1751 nios2_elf32_size_stubs (bfd
*output_bfd
, bfd
*stub_bfd
,
1752 struct bfd_link_info
*info
,
1753 asection
*(*add_stub_section
) (const char *,
1754 asection
*, bfd_boolean
),
1755 void (*layout_sections_again
) (void))
1757 bfd_boolean stub_changed
= FALSE
;
1758 struct elf32_nios2_link_hash_table
*htab
= elf32_nios2_hash_table (info
);
1760 /* Stash our params away. */
1761 htab
->stub_bfd
= stub_bfd
;
1762 htab
->add_stub_section
= add_stub_section
;
1763 htab
->layout_sections_again
= layout_sections_again
;
1765 /* FIXME: We only compute the section groups once. This could cause
1766 problems if adding a large stub section causes following sections,
1767 or parts of them, to move into another segment. However, this seems
1768 to be consistent with the way other back ends handle this.... */
1769 group_sections (htab
);
1771 if (get_local_syms (output_bfd
, info
->input_bfds
, info
))
1773 if (htab
->all_local_syms
)
1774 goto error_ret_free_local
;
1781 unsigned int bfd_indx
;
1784 for (input_bfd
= info
->input_bfds
, bfd_indx
= 0;
1786 input_bfd
= input_bfd
->link_next
, bfd_indx
++)
1788 Elf_Internal_Shdr
*symtab_hdr
;
1790 Elf_Internal_Sym
*local_syms
;
1792 /* We'll need the symbol table in a second. */
1793 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
1794 if (symtab_hdr
->sh_info
== 0)
1797 local_syms
= htab
->all_local_syms
[bfd_indx
];
1799 /* Walk over each section attached to the input bfd. */
1800 for (section
= input_bfd
->sections
;
1802 section
= section
->next
)
1804 Elf_Internal_Rela
*internal_relocs
, *irelaend
, *irela
;
1806 /* If there aren't any relocs, then there's nothing more
1808 if ((section
->flags
& SEC_RELOC
) == 0
1809 || section
->reloc_count
== 0)
1812 /* If this section is a link-once section that will be
1813 discarded, then don't create any stubs. */
1814 if (section
->output_section
== NULL
1815 || section
->output_section
->owner
!= output_bfd
)
1818 /* Get the relocs. */
1820 = _bfd_elf_link_read_relocs (input_bfd
, section
, NULL
, NULL
,
1822 if (internal_relocs
== NULL
)
1823 goto error_ret_free_local
;
1825 /* Now examine each relocation. */
1826 irela
= internal_relocs
;
1827 irelaend
= irela
+ section
->reloc_count
;
1828 for (; irela
< irelaend
; irela
++)
1830 unsigned int r_type
, r_indx
;
1831 enum elf32_nios2_stub_type stub_type
;
1832 struct elf32_nios2_stub_hash_entry
*hsh
;
1835 bfd_vma destination
;
1836 struct elf32_nios2_link_hash_entry
*hh
;
1838 const asection
*id_sec
;
1840 r_type
= ELF32_R_TYPE (irela
->r_info
);
1841 r_indx
= ELF32_R_SYM (irela
->r_info
);
1843 if (r_type
>= (unsigned int) R_NIOS2_ILLEGAL
)
1845 bfd_set_error (bfd_error_bad_value
);
1846 error_ret_free_internal
:
1847 if (elf_section_data (section
)->relocs
== NULL
)
1848 free (internal_relocs
);
1849 goto error_ret_free_local
;
1852 /* Only look for stubs on CALL and JMPI instructions. */
1853 if (r_type
!= (unsigned int) R_NIOS2_CALL26
)
1856 /* Now determine the call target, its name, value,
1862 if (r_indx
< symtab_hdr
->sh_info
)
1864 /* It's a local symbol. */
1865 Elf_Internal_Sym
*sym
;
1866 Elf_Internal_Shdr
*hdr
;
1869 sym
= local_syms
+ r_indx
;
1870 if (ELF_ST_TYPE (sym
->st_info
) != STT_SECTION
)
1871 sym_value
= sym
->st_value
;
1872 shndx
= sym
->st_shndx
;
1873 if (shndx
< elf_numsections (input_bfd
))
1875 hdr
= elf_elfsections (input_bfd
)[shndx
];
1876 sym_sec
= hdr
->bfd_section
;
1877 destination
= (sym_value
+ irela
->r_addend
1878 + sym_sec
->output_offset
1879 + sym_sec
->output_section
->vma
);
1884 /* It's an external symbol. */
1887 e_indx
= r_indx
- symtab_hdr
->sh_info
;
1888 hh
= ((struct elf32_nios2_link_hash_entry
*)
1889 elf_sym_hashes (input_bfd
)[e_indx
]);
1891 while (hh
->root
.root
.type
== bfd_link_hash_indirect
1892 || hh
->root
.root
.type
== bfd_link_hash_warning
)
1893 hh
= ((struct elf32_nios2_link_hash_entry
*)
1894 hh
->root
.root
.u
.i
.link
);
1896 if (hh
->root
.root
.type
== bfd_link_hash_defined
1897 || hh
->root
.root
.type
== bfd_link_hash_defweak
)
1899 sym_sec
= hh
->root
.root
.u
.def
.section
;
1900 sym_value
= hh
->root
.root
.u
.def
.value
;
1902 if (sym_sec
->output_section
!= NULL
)
1903 destination
= (sym_value
+ irela
->r_addend
1904 + sym_sec
->output_offset
1905 + sym_sec
->output_section
->vma
);
1909 else if (hh
->root
.root
.type
== bfd_link_hash_undefweak
)
1914 else if (hh
->root
.root
.type
== bfd_link_hash_undefined
)
1916 if (! (info
->unresolved_syms_in_objects
== RM_IGNORE
1917 && (ELF_ST_VISIBILITY (hh
->root
.other
)
1923 bfd_set_error (bfd_error_bad_value
);
1924 goto error_ret_free_internal
;
1928 /* Determine what (if any) linker stub is needed. */
1929 stub_type
= nios2_type_of_stub (section
, irela
, hh
, htab
,
1931 if (stub_type
== nios2_stub_none
)
1934 /* Support for grouping stub sections. */
1935 if (stub_type
== nios2_stub_call26_before
)
1936 id_sec
= htab
->stub_group
[section
->id
].first_sec
;
1938 id_sec
= htab
->stub_group
[section
->id
].last_sec
;
1940 /* Get the name of this stub. */
1941 stub_name
= nios2_stub_name (id_sec
, sym_sec
, hh
, irela
,
1944 goto error_ret_free_internal
;
1946 hsh
= nios2_stub_hash_lookup (&htab
->bstab
,
1951 /* The proper stub has already been created. */
1956 hsh
= nios2_add_stub (stub_name
, section
, htab
, stub_type
);
1960 goto error_ret_free_internal
;
1962 hsh
->target_value
= sym_value
;
1963 hsh
->target_section
= sym_sec
;
1964 hsh
->stub_type
= stub_type
;
1966 hsh
->addend
= irela
->r_addend
;
1967 stub_changed
= TRUE
;
1970 /* We're done with the internal relocs, free them. */
1971 if (elf_section_data (section
)->relocs
== NULL
)
1972 free (internal_relocs
);
1979 /* OK, we've added some stubs. Find out the new size of the
1981 for (stub_sec
= htab
->stub_bfd
->sections
;
1983 stub_sec
= stub_sec
->next
)
1986 bfd_hash_traverse (&htab
->bstab
, nios2_size_one_stub
, htab
);
1988 /* Ask the linker to do its stuff. */
1989 (*htab
->layout_sections_again
) ();
1990 stub_changed
= FALSE
;
1993 free (htab
->all_local_syms
);
1996 error_ret_free_local
:
1997 free (htab
->all_local_syms
);
2001 /* Build all the stubs associated with the current output file. The
2002 stubs are kept in a hash table attached to the main linker hash
2003 table. This function is called via nios2elf_finish in the linker. */
2005 nios2_elf32_build_stubs (struct bfd_link_info
*info
)
2008 struct bfd_hash_table
*table
;
2009 struct elf32_nios2_link_hash_table
*htab
;
2011 htab
= elf32_nios2_hash_table (info
);
2013 for (stub_sec
= htab
->stub_bfd
->sections
;
2015 stub_sec
= stub_sec
->next
)
2019 /* Allocate memory to hold the linker stubs. */
2020 size
= stub_sec
->size
;
2021 stub_sec
->contents
= bfd_zalloc (htab
->stub_bfd
, size
);
2022 if (stub_sec
->contents
== NULL
&& size
!= 0)
2027 /* Build the stubs as directed by the stub hash table. */
2028 table
= &htab
->bstab
;
2029 bfd_hash_traverse (table
, nios2_build_one_stub
, info
);
2035 /* Implement bfd_elf32_bfd_reloc_type_lookup:
2036 Given a BFD reloc type, return a howto structure. */
2037 static reloc_howto_type
*
2038 nios2_elf32_bfd_reloc_type_lookup (bfd
*abfd ATTRIBUTE_UNUSED
,
2039 bfd_reloc_code_real_type code
)
2043 i
< (int) (sizeof (nios2_reloc_map
) / sizeof (struct elf_reloc_map
));
2045 if (nios2_reloc_map
[i
].bfd_val
== code
)
2046 return &elf_nios2_howto_table_rel
[(int) nios2_reloc_map
[i
].elf_val
];
2050 /* Implement bfd_elf32_bfd_reloc_name_lookup:
2051 Given a reloc name, return a howto structure. */
2052 static reloc_howto_type
*
2053 nios2_elf32_bfd_reloc_name_lookup (bfd
*abfd ATTRIBUTE_UNUSED
,
2058 i
< (sizeof (elf_nios2_howto_table_rel
)
2059 / sizeof (elf_nios2_howto_table_rel
[0]));
2061 if (elf_nios2_howto_table_rel
[i
].name
2062 && strcasecmp (elf_nios2_howto_table_rel
[i
].name
, r_name
) == 0)
2063 return &elf_nios2_howto_table_rel
[i
];
2068 /* Implement elf_info_to_howto:
2069 Given a ELF32 relocation, fill in a arelent structure. */
2071 nios2_elf32_info_to_howto (bfd
*abfd ATTRIBUTE_UNUSED
, arelent
*cache_ptr
,
2072 Elf_Internal_Rela
*dst
)
2074 unsigned int r_type
;
2076 r_type
= ELF32_R_TYPE (dst
->r_info
);
2077 BFD_ASSERT (r_type
< R_NIOS2_ILLEGAL
);
2078 cache_ptr
->howto
= &elf_nios2_howto_table_rel
[r_type
];
2081 /* Return the base VMA address which should be subtracted from real addresses
2082 when resolving @dtpoff relocation.
2083 This is PT_TLS segment p_vaddr. */
2085 dtpoff_base (struct bfd_link_info
*info
)
2087 /* If tls_sec is NULL, we should have signalled an error already. */
2088 if (elf_hash_table (info
)->tls_sec
== NULL
)
2090 return elf_hash_table (info
)->tls_sec
->vma
;
2093 /* Return the relocation value for @tpoff relocation
2094 if STT_TLS virtual address is ADDRESS. */
2096 tpoff (struct bfd_link_info
*info
, bfd_vma address
)
2098 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
2100 /* If tls_sec is NULL, we should have signalled an error already. */
2101 if (htab
->tls_sec
== NULL
)
2103 return address
- htab
->tls_sec
->vma
;
2106 /* Set the GP value for OUTPUT_BFD. Returns FALSE if this is a
2107 dangerous relocation. */
2109 nios2_elf_assign_gp (bfd
*output_bfd
, bfd_vma
*pgp
, struct bfd_link_info
*info
)
2112 bfd_boolean gp_found
;
2113 struct bfd_hash_entry
*h
;
2114 struct bfd_link_hash_entry
*lh
;
2116 /* If we've already figured out what GP will be, just return it. */
2117 *pgp
= _bfd_get_gp_value (output_bfd
);
2121 h
= bfd_hash_lookup (&info
->hash
->table
, "_gp", FALSE
, FALSE
);
2122 lh
= (struct bfd_link_hash_entry
*) h
;
2128 case bfd_link_hash_undefined
:
2129 case bfd_link_hash_undefweak
:
2130 case bfd_link_hash_common
:
2133 case bfd_link_hash_defined
:
2134 case bfd_link_hash_defweak
:
2136 *pgp
= lh
->u
.def
.value
;
2138 case bfd_link_hash_indirect
:
2139 case bfd_link_hash_warning
:
2141 /* @@FIXME ignoring warning for now */
2143 case bfd_link_hash_new
:
2153 /* Only get the error once. */
2155 _bfd_set_gp_value (output_bfd
, *pgp
);
2159 _bfd_set_gp_value (output_bfd
, *pgp
);
2164 /* Retrieve the previously cached _gp pointer, returning bfd_reloc_dangerous
2165 if it's not available as we don't have a link_info pointer available here
2166 to look it up in the output symbol table. We don't need to adjust the
2167 symbol value for an external symbol if we are producing relocatable
2169 static bfd_reloc_status_type
2170 nios2_elf_final_gp (bfd
*output_bfd
, asymbol
*symbol
, bfd_boolean relocatable
,
2171 char **error_message
, bfd_vma
*pgp
)
2173 if (bfd_is_und_section (symbol
->section
) && !relocatable
)
2176 return bfd_reloc_undefined
;
2179 *pgp
= _bfd_get_gp_value (output_bfd
);
2180 if (*pgp
== 0 && (!relocatable
|| (symbol
->flags
& BSF_SECTION_SYM
) != 0))
2184 /* Make up a value. */
2185 *pgp
= symbol
->section
->output_section
->vma
+ 0x4000;
2186 _bfd_set_gp_value (output_bfd
, *pgp
);
2191 = (char *) _("global pointer relative relocation when _gp not defined");
2192 return bfd_reloc_dangerous
;
2196 return bfd_reloc_ok
;
2199 /* Do the relocations that require special handling. */
2200 static bfd_reloc_status_type
2201 nios2_elf32_do_hi16_relocate (bfd
*abfd
, reloc_howto_type
*howto
,
2202 asection
*input_section
,
2203 bfd_byte
*data
, bfd_vma offset
,
2204 bfd_vma symbol_value
, bfd_vma addend
)
2206 symbol_value
= symbol_value
+ addend
;
2208 symbol_value
= (symbol_value
>> 16) & 0xffff;
2209 return _bfd_final_link_relocate (howto
, abfd
, input_section
,
2210 data
, offset
, symbol_value
, addend
);
2213 static bfd_reloc_status_type
2214 nios2_elf32_do_lo16_relocate (bfd
*abfd
, reloc_howto_type
*howto
,
2215 asection
*input_section
,
2216 bfd_byte
*data
, bfd_vma offset
,
2217 bfd_vma symbol_value
, bfd_vma addend
)
2219 symbol_value
= symbol_value
+ addend
;
2221 symbol_value
= symbol_value
& 0xffff;
2222 return _bfd_final_link_relocate (howto
, abfd
, input_section
,
2223 data
, offset
, symbol_value
, addend
);
2226 static bfd_reloc_status_type
2227 nios2_elf32_do_hiadj16_relocate (bfd
*abfd
, reloc_howto_type
*howto
,
2228 asection
*input_section
,
2229 bfd_byte
*data
, bfd_vma offset
,
2230 bfd_vma symbol_value
, bfd_vma addend
)
2232 symbol_value
= symbol_value
+ addend
;
2234 symbol_value
= hiadj(symbol_value
);
2235 return _bfd_final_link_relocate (howto
, abfd
, input_section
, data
, offset
,
2236 symbol_value
, addend
);
2239 static bfd_reloc_status_type
2240 nios2_elf32_do_pcrel_lo16_relocate (bfd
*abfd
, reloc_howto_type
*howto
,
2241 asection
*input_section
,
2242 bfd_byte
*data
, bfd_vma offset
,
2243 bfd_vma symbol_value
, bfd_vma addend
)
2245 symbol_value
= symbol_value
+ addend
;
2247 symbol_value
= symbol_value
& 0xffff;
2248 return _bfd_final_link_relocate (howto
, abfd
, input_section
,
2249 data
, offset
, symbol_value
, addend
);
2252 static bfd_reloc_status_type
2253 nios2_elf32_do_pcrel_hiadj16_relocate (bfd
*abfd
, reloc_howto_type
*howto
,
2254 asection
*input_section
,
2255 bfd_byte
*data
, bfd_vma offset
,
2256 bfd_vma symbol_value
, bfd_vma addend
)
2258 symbol_value
= symbol_value
+ addend
;
2259 symbol_value
-= (input_section
->output_section
->vma
2260 + input_section
->output_offset
);
2261 symbol_value
-= offset
;
2263 symbol_value
= hiadj(symbol_value
);
2264 return _bfd_final_link_relocate (howto
, abfd
, input_section
, data
, offset
,
2265 symbol_value
, addend
);
2268 static bfd_reloc_status_type
2269 nios2_elf32_do_pcrel16_relocate (bfd
*abfd
, reloc_howto_type
*howto
,
2270 asection
*input_section
,
2271 bfd_byte
*data
, bfd_vma offset
,
2272 bfd_vma symbol_value
, bfd_vma addend
)
2274 /* NIOS2 pc relative relocations are relative to the next 32-bit instruction
2275 so we need to subtract 4 before doing a final_link_relocate. */
2276 symbol_value
= symbol_value
+ addend
- 4;
2278 return _bfd_final_link_relocate (howto
, abfd
, input_section
,
2279 data
, offset
, symbol_value
, addend
);
2282 static bfd_reloc_status_type
2283 nios2_elf32_do_call26_relocate (bfd
*abfd
, reloc_howto_type
*howto
,
2284 asection
*input_section
,
2285 bfd_byte
*data
, bfd_vma offset
,
2286 bfd_vma symbol_value
, bfd_vma addend
)
2288 /* Check that the relocation is in the same page as the current address. */
2289 if (CALL26_SEGMENT (symbol_value
+ addend
)
2290 != CALL26_SEGMENT (input_section
->output_section
->vma
2291 + input_section
->output_offset
2293 return bfd_reloc_overflow
;
2295 return _bfd_final_link_relocate (howto
, abfd
, input_section
,
2296 data
, offset
, symbol_value
, addend
);
2299 static bfd_reloc_status_type
2300 nios2_elf32_do_gprel_relocate (bfd
*abfd
, reloc_howto_type
*howto
,
2301 asection
*input_section
,
2302 bfd_byte
*data
, bfd_vma offset
,
2303 bfd_vma symbol_value
, bfd_vma addend
)
2305 /* Because we need the output_bfd, the special handling is done
2306 in nios2_elf32_relocate_section or in nios2_elf32_gprel_relocate. */
2307 return _bfd_final_link_relocate (howto
, abfd
, input_section
,
2308 data
, offset
, symbol_value
, addend
);
2311 static bfd_reloc_status_type
2312 nios2_elf32_do_ujmp_relocate (bfd
*abfd
, reloc_howto_type
*howto
,
2313 asection
*input_section
,
2314 bfd_byte
*data
, bfd_vma offset
,
2315 bfd_vma symbol_value
, bfd_vma addend
)
2317 bfd_vma symbol_lo16
, symbol_hi16
;
2318 bfd_reloc_status_type r
;
2319 symbol_value
= symbol_value
+ addend
;
2321 symbol_hi16
= (symbol_value
>> 16) & 0xffff;
2322 symbol_lo16
= symbol_value
& 0xffff;
2324 r
= _bfd_final_link_relocate (howto
, abfd
, input_section
,
2325 data
, offset
, symbol_hi16
, addend
);
2327 if (r
== bfd_reloc_ok
)
2328 return _bfd_final_link_relocate (howto
, abfd
, input_section
,
2329 data
, offset
+ 4, symbol_lo16
, addend
);
2334 static bfd_reloc_status_type
2335 nios2_elf32_do_cjmp_relocate (bfd
*abfd
, reloc_howto_type
*howto
,
2336 asection
*input_section
,
2337 bfd_byte
*data
, bfd_vma offset
,
2338 bfd_vma symbol_value
, bfd_vma addend
)
2340 bfd_vma symbol_lo16
, symbol_hi16
;
2341 bfd_reloc_status_type r
;
2342 symbol_value
= symbol_value
+ addend
;
2344 symbol_hi16
= (symbol_value
>> 16) & 0xffff;
2345 symbol_lo16
= symbol_value
& 0xffff;
2347 r
= _bfd_final_link_relocate (howto
, abfd
, input_section
,
2348 data
, offset
, symbol_hi16
, addend
);
2350 if (r
== bfd_reloc_ok
)
2351 return _bfd_final_link_relocate (howto
, abfd
, input_section
,
2352 data
, offset
+ 4, symbol_lo16
, addend
);
2357 static bfd_reloc_status_type
2358 nios2_elf32_do_callr_relocate (bfd
*abfd
, reloc_howto_type
*howto
,
2359 asection
*input_section
,
2360 bfd_byte
*data
, bfd_vma offset
,
2361 bfd_vma symbol_value
, bfd_vma addend
)
2363 bfd_vma symbol_lo16
, symbol_hi16
;
2364 bfd_reloc_status_type r
;
2365 symbol_value
= symbol_value
+ addend
;
2367 symbol_hi16
= (symbol_value
>> 16) & 0xffff;
2368 symbol_lo16
= symbol_value
& 0xffff;
2370 r
= _bfd_final_link_relocate (howto
, abfd
, input_section
,
2371 data
, offset
, symbol_hi16
, addend
);
2373 if (r
== bfd_reloc_ok
)
2374 return _bfd_final_link_relocate (howto
, abfd
, input_section
,
2375 data
, offset
+ 4, symbol_lo16
, addend
);
2380 /* HOWTO handlers for relocations that require special handling. */
2382 /* This is for relocations used only when relaxing to ensure
2383 changes in size of section don't screw up .align. */
2384 static bfd_reloc_status_type
2385 nios2_elf32_ignore_reloc (bfd
*abfd ATTRIBUTE_UNUSED
, arelent
*reloc_entry
,
2386 asymbol
*symbol ATTRIBUTE_UNUSED
,
2387 void *data ATTRIBUTE_UNUSED
, asection
*input_section
,
2389 char **error_message ATTRIBUTE_UNUSED
)
2391 if (output_bfd
!= NULL
)
2392 reloc_entry
->address
+= input_section
->output_offset
;
2393 return bfd_reloc_ok
;
2396 static bfd_reloc_status_type
2397 nios2_elf32_hi16_relocate (bfd
*abfd
, arelent
*reloc_entry
, asymbol
*symbol
,
2398 void *data
, asection
*input_section
,
2400 char **error_message ATTRIBUTE_UNUSED
)
2402 /* This part is from bfd_elf_generic_reloc. */
2403 if (output_bfd
!= NULL
2404 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
2405 && (!reloc_entry
->howto
->partial_inplace
|| reloc_entry
->addend
== 0))
2407 reloc_entry
->address
+= input_section
->output_offset
;
2408 return bfd_reloc_ok
;
2411 if (output_bfd
!= NULL
)
2412 /* FIXME: See bfd_perform_relocation. Is this right? */
2413 return bfd_reloc_continue
;
2415 return nios2_elf32_do_hi16_relocate (abfd
, reloc_entry
->howto
,
2417 data
, reloc_entry
->address
,
2419 + symbol
->section
->output_section
->vma
2420 + symbol
->section
->output_offset
),
2421 reloc_entry
->addend
);
2424 static bfd_reloc_status_type
2425 nios2_elf32_lo16_relocate (bfd
*abfd
, arelent
*reloc_entry
, asymbol
*symbol
,
2426 void *data
, asection
*input_section
,
2428 char **error_message ATTRIBUTE_UNUSED
)
2430 /* This part is from bfd_elf_generic_reloc. */
2431 if (output_bfd
!= NULL
2432 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
2433 && (!reloc_entry
->howto
->partial_inplace
|| reloc_entry
->addend
== 0))
2435 reloc_entry
->address
+= input_section
->output_offset
;
2436 return bfd_reloc_ok
;
2439 if (output_bfd
!= NULL
)
2440 /* FIXME: See bfd_perform_relocation. Is this right? */
2441 return bfd_reloc_continue
;
2443 return nios2_elf32_do_lo16_relocate (abfd
, reloc_entry
->howto
,
2445 data
, reloc_entry
->address
,
2447 + symbol
->section
->output_section
->vma
2448 + symbol
->section
->output_offset
),
2449 reloc_entry
->addend
);
2452 static bfd_reloc_status_type
2453 nios2_elf32_hiadj16_relocate (bfd
*abfd
, arelent
*reloc_entry
, asymbol
*symbol
,
2454 void *data
, asection
*input_section
,
2456 char **error_message ATTRIBUTE_UNUSED
)
2458 /* This part is from bfd_elf_generic_reloc. */
2459 if (output_bfd
!= NULL
2460 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
2461 && (!reloc_entry
->howto
->partial_inplace
|| reloc_entry
->addend
== 0))
2463 reloc_entry
->address
+= input_section
->output_offset
;
2464 return bfd_reloc_ok
;
2467 if (output_bfd
!= NULL
)
2468 /* FIXME: See bfd_perform_relocation. Is this right? */
2469 return bfd_reloc_continue
;
2471 return nios2_elf32_do_hiadj16_relocate (abfd
, reloc_entry
->howto
,
2473 data
, reloc_entry
->address
,
2475 + symbol
->section
->output_section
->vma
2476 + symbol
->section
->output_offset
),
2477 reloc_entry
->addend
);
2480 static bfd_reloc_status_type
2481 nios2_elf32_pcrel_lo16_relocate (bfd
*abfd
, arelent
*reloc_entry
,
2482 asymbol
*symbol
, void *data
,
2483 asection
*input_section
, bfd
*output_bfd
,
2484 char **error_message ATTRIBUTE_UNUSED
)
2486 /* This part is from bfd_elf_generic_reloc. */
2487 if (output_bfd
!= NULL
2488 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
2489 && (!reloc_entry
->howto
->partial_inplace
|| reloc_entry
->addend
== 0))
2491 reloc_entry
->address
+= input_section
->output_offset
;
2492 return bfd_reloc_ok
;
2495 if (output_bfd
!= NULL
)
2496 /* FIXME: See bfd_perform_relocation. Is this right? */
2497 return bfd_reloc_continue
;
2499 return nios2_elf32_do_pcrel_lo16_relocate (
2500 abfd
, reloc_entry
->howto
, input_section
, data
, reloc_entry
->address
,
2501 (symbol
->value
+ symbol
->section
->output_section
->vma
2502 + symbol
->section
->output_offset
),
2503 reloc_entry
->addend
);
2506 static bfd_reloc_status_type
2507 nios2_elf32_pcrel_hiadj16_relocate (bfd
*abfd
, arelent
*reloc_entry
,
2508 asymbol
*symbol
, void *data
,
2509 asection
*input_section
, bfd
*output_bfd
,
2510 char **error_message ATTRIBUTE_UNUSED
)
2512 /* This part is from bfd_elf_generic_reloc. */
2513 if (output_bfd
!= NULL
2514 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
2515 && (!reloc_entry
->howto
->partial_inplace
|| reloc_entry
->addend
== 0))
2517 reloc_entry
->address
+= input_section
->output_offset
;
2518 return bfd_reloc_ok
;
2521 if (output_bfd
!= NULL
)
2522 /* FIXME: See bfd_perform_relocation. Is this right? */
2523 return bfd_reloc_continue
;
2525 return nios2_elf32_do_pcrel_hiadj16_relocate (
2526 abfd
, reloc_entry
->howto
, input_section
, data
, reloc_entry
->address
,
2527 (symbol
->value
+ symbol
->section
->output_section
->vma
2528 + symbol
->section
->output_offset
),
2529 reloc_entry
->addend
);
2532 static bfd_reloc_status_type
2533 nios2_elf32_pcrel16_relocate (bfd
*abfd
, arelent
*reloc_entry
, asymbol
*symbol
,
2534 void *data
, asection
*input_section
,
2536 char **error_message ATTRIBUTE_UNUSED
)
2538 /* This part is from bfd_elf_generic_reloc. */
2539 if (output_bfd
!= NULL
2540 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
2541 && (!reloc_entry
->howto
->partial_inplace
|| reloc_entry
->addend
== 0))
2543 reloc_entry
->address
+= input_section
->output_offset
;
2544 return bfd_reloc_ok
;
2547 if (output_bfd
!= NULL
)
2548 /* FIXME: See bfd_perform_relocation. Is this right? */
2549 return bfd_reloc_continue
;
2551 return nios2_elf32_do_pcrel16_relocate (abfd
, reloc_entry
->howto
,
2553 data
, reloc_entry
->address
,
2555 + symbol
->section
->output_section
->vma
2556 + symbol
->section
->output_offset
),
2557 reloc_entry
->addend
);
2560 static bfd_reloc_status_type
2561 nios2_elf32_call26_relocate (bfd
*abfd
, arelent
*reloc_entry
, asymbol
*symbol
,
2562 void *data
, asection
*input_section
,
2564 char **error_message ATTRIBUTE_UNUSED
)
2566 /* This part is from bfd_elf_generic_reloc. */
2567 if (output_bfd
!= NULL
2568 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
2569 && (!reloc_entry
->howto
->partial_inplace
|| reloc_entry
->addend
== 0))
2571 reloc_entry
->address
+= input_section
->output_offset
;
2572 return bfd_reloc_ok
;
2575 if (output_bfd
!= NULL
)
2576 /* FIXME: See bfd_perform_relocation. Is this right? */
2577 return bfd_reloc_continue
;
2579 return nios2_elf32_do_call26_relocate (abfd
, reloc_entry
->howto
,
2581 data
, reloc_entry
->address
,
2583 + symbol
->section
->output_section
->vma
2584 + symbol
->section
->output_offset
),
2585 reloc_entry
->addend
);
2588 static bfd_reloc_status_type
2589 nios2_elf32_gprel_relocate (bfd
*abfd
, arelent
*reloc_entry
, asymbol
*symbol
,
2590 void *data
, asection
*input_section
,
2591 bfd
*output_bfd
, char **msg
)
2595 bfd_reloc_status_type r
;
2598 /* This part is from bfd_elf_generic_reloc. */
2599 if (output_bfd
!= NULL
2600 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
2601 && (!reloc_entry
->howto
->partial_inplace
|| reloc_entry
->addend
== 0))
2603 reloc_entry
->address
+= input_section
->output_offset
;
2604 return bfd_reloc_ok
;
2607 if (output_bfd
!= NULL
)
2608 /* FIXME: See bfd_perform_relocation. Is this right? */
2609 return bfd_reloc_continue
;
2611 relocation
= (symbol
->value
2612 + symbol
->section
->output_section
->vma
2613 + symbol
->section
->output_offset
);
2615 /* This assumes we've already cached the _gp symbol. */
2616 r
= nios2_elf_final_gp (abfd
, symbol
, FALSE
, msg
, &gp
);
2617 if (r
== bfd_reloc_ok
)
2619 relocation
= relocation
+ reloc_entry
->addend
- gp
;
2620 reloc_entry
->addend
= 0;
2621 if ((signed) relocation
< -32768 || (signed) relocation
> 32767)
2623 *msg
= _("global pointer relative address out of range");
2624 r
= bfd_reloc_outofrange
;
2627 r
= nios2_elf32_do_gprel_relocate (abfd
, reloc_entry
->howto
,
2629 data
, reloc_entry
->address
,
2630 relocation
, reloc_entry
->addend
);
2636 static bfd_reloc_status_type
2637 nios2_elf32_ujmp_relocate (bfd
*abfd
, arelent
*reloc_entry
, asymbol
*symbol
,
2638 void *data
, asection
*input_section
,
2639 bfd
*output_bfd
, char **msg ATTRIBUTE_UNUSED
)
2641 /* This part is from bfd_elf_generic_reloc. */
2642 if (output_bfd
!= NULL
2643 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
2644 && (!reloc_entry
->howto
->partial_inplace
|| reloc_entry
->addend
== 0))
2646 reloc_entry
->address
+= input_section
->output_offset
;
2647 return bfd_reloc_ok
;
2650 if (output_bfd
!= NULL
)
2651 /* FIXME: See bfd_perform_relocation. Is this right? */
2652 return bfd_reloc_continue
;
2654 return nios2_elf32_do_ujmp_relocate (abfd
, reloc_entry
->howto
,
2656 data
, reloc_entry
->address
,
2658 + symbol
->section
->output_section
->vma
2659 + symbol
->section
->output_offset
),
2660 reloc_entry
->addend
);
2663 static bfd_reloc_status_type
2664 nios2_elf32_cjmp_relocate (bfd
*abfd
, arelent
*reloc_entry
, asymbol
*symbol
,
2665 void *data
, asection
*input_section
,
2666 bfd
*output_bfd
, char **msg ATTRIBUTE_UNUSED
)
2668 /* This part is from bfd_elf_generic_reloc. */
2669 if (output_bfd
!= NULL
2670 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
2671 && (!reloc_entry
->howto
->partial_inplace
|| reloc_entry
->addend
== 0))
2673 reloc_entry
->address
+= input_section
->output_offset
;
2674 return bfd_reloc_ok
;
2677 if (output_bfd
!= NULL
)
2678 /* FIXME: See bfd_perform_relocation. Is this right? */
2679 return bfd_reloc_continue
;
2681 return nios2_elf32_do_cjmp_relocate (abfd
, reloc_entry
->howto
,
2683 data
, reloc_entry
->address
,
2685 + symbol
->section
->output_section
->vma
2686 + symbol
->section
->output_offset
),
2687 reloc_entry
->addend
);
2690 static bfd_reloc_status_type
2691 nios2_elf32_callr_relocate (bfd
*abfd
, arelent
*reloc_entry
, asymbol
*symbol
,
2692 void *data
, asection
*input_section
,
2693 bfd
*output_bfd
, char **msg ATTRIBUTE_UNUSED
)
2695 /* This part is from bfd_elf_generic_reloc. */
2696 if (output_bfd
!= NULL
2697 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
2698 && (!reloc_entry
->howto
->partial_inplace
|| reloc_entry
->addend
== 0))
2700 reloc_entry
->address
+= input_section
->output_offset
;
2701 return bfd_reloc_ok
;
2704 if (output_bfd
!= NULL
)
2705 /* FIXME: See bfd_perform_relocation. Is this right? */
2706 return bfd_reloc_continue
;
2708 return nios2_elf32_do_callr_relocate (abfd
, reloc_entry
->howto
,
2710 data
, reloc_entry
->address
,
2712 + symbol
->section
->output_section
->vma
2713 + symbol
->section
->output_offset
),
2714 reloc_entry
->addend
);
2718 /* Implement elf_backend_relocate_section. */
2720 nios2_elf32_relocate_section (bfd
*output_bfd
,
2721 struct bfd_link_info
*info
,
2723 asection
*input_section
,
2725 Elf_Internal_Rela
*relocs
,
2726 Elf_Internal_Sym
*local_syms
,
2727 asection
**local_sections
)
2729 Elf_Internal_Shdr
*symtab_hdr
;
2730 struct elf_link_hash_entry
**sym_hashes
;
2731 Elf_Internal_Rela
*rel
;
2732 Elf_Internal_Rela
*relend
;
2733 struct elf32_nios2_link_hash_table
*htab
;
2736 asection
*sreloc
= NULL
;
2737 bfd_vma
*local_got_offsets
;
2740 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
2741 sym_hashes
= elf_sym_hashes (input_bfd
);
2742 relend
= relocs
+ input_section
->reloc_count
;
2744 htab
= elf32_nios2_hash_table (info
);
2745 sgot
= htab
->root
.sgot
;
2746 splt
= htab
->root
.splt
;
2747 local_got_offsets
= elf_local_got_offsets (input_bfd
);
2749 if (elf32_nios2_hash_table (info
)->h_gp_got
== NULL
)
2752 got_base
= elf32_nios2_hash_table (info
)->h_gp_got
->root
.u
.def
.value
;
2754 for (rel
= relocs
; rel
< relend
; rel
++)
2756 reloc_howto_type
*howto
;
2757 unsigned long r_symndx
;
2758 Elf_Internal_Sym
*sym
;
2760 struct elf_link_hash_entry
*h
;
2761 struct elf32_nios2_link_hash_entry
*eh
;
2764 bfd_vma reloc_address
;
2765 bfd_reloc_status_type r
= bfd_reloc_ok
;
2766 const char *name
= NULL
;
2770 const char* msg
= (const char*) NULL
;
2771 bfd_boolean unresolved_reloc
;
2775 r_type
= ELF32_R_TYPE (rel
->r_info
);
2776 r_symndx
= ELF32_R_SYM (rel
->r_info
);
2778 howto
= lookup_howto ((unsigned) ELF32_R_TYPE (rel
->r_info
));
2783 if (r_symndx
< symtab_hdr
->sh_info
)
2785 sym
= local_syms
+ r_symndx
;
2786 sec
= local_sections
[r_symndx
];
2787 relocation
= _bfd_elf_rela_local_sym (output_bfd
, sym
, &sec
, rel
);
2791 bfd_boolean warned
, ignored
;
2793 RELOC_FOR_GLOBAL_SYMBOL (info
, input_bfd
, input_section
, rel
,
2794 r_symndx
, symtab_hdr
, sym_hashes
,
2796 unresolved_reloc
, warned
, ignored
);
2799 if (sec
&& discarded_section (sec
))
2800 RELOC_AGAINST_DISCARDED_SECTION (info
, input_bfd
, input_section
,
2801 rel
, 1, relend
, howto
, 0, contents
);
2803 /* Nothing more to do unless this is a final link. */
2804 if (info
->relocatable
)
2807 if (sec
&& sec
->output_section
)
2808 reloc_address
= (sec
->output_section
->vma
+ sec
->output_offset
2815 switch (howto
->type
)
2818 r
= nios2_elf32_do_hi16_relocate (input_bfd
, howto
,
2820 contents
, rel
->r_offset
,
2821 relocation
, rel
->r_addend
);
2824 r
= nios2_elf32_do_lo16_relocate (input_bfd
, howto
,
2826 contents
, rel
->r_offset
,
2827 relocation
, rel
->r_addend
);
2829 case R_NIOS2_PCREL_LO
:
2830 r
= nios2_elf32_do_pcrel_lo16_relocate (input_bfd
, howto
,
2837 case R_NIOS2_HIADJ16
:
2838 r
= nios2_elf32_do_hiadj16_relocate (input_bfd
, howto
,
2839 input_section
, contents
,
2840 rel
->r_offset
, relocation
,
2843 case R_NIOS2_PCREL_HA
:
2844 r
= nios2_elf32_do_pcrel_hiadj16_relocate (input_bfd
, howto
,
2851 case R_NIOS2_PCREL16
:
2852 r
= nios2_elf32_do_pcrel16_relocate (input_bfd
, howto
,
2853 input_section
, contents
,
2854 rel
->r_offset
, relocation
,
2858 /* Turns an absolute address into a gp-relative address. */
2859 if (!nios2_elf_assign_gp (output_bfd
, &gp
, info
))
2861 format
= _("global pointer relative relocation at address "
2862 "0x%08x when _gp not defined\n");
2863 sprintf (msgbuf
, format
, reloc_address
);
2865 r
= bfd_reloc_dangerous
;
2869 bfd_vma symbol_address
= rel
->r_addend
+ relocation
;
2870 relocation
= relocation
+ rel
->r_addend
- gp
;
2872 if (((signed) relocation
< -32768
2873 || (signed) relocation
> 32767)
2875 || h
->root
.type
== bfd_link_hash_defined
2876 || h
->root
.type
== bfd_link_hash_defweak
))
2878 format
= _("Unable to reach %s (at 0x%08x) from the "
2879 "global pointer (at 0x%08x) because the "
2880 "offset (%d) is out of the allowed range, "
2881 "-32678 to 32767.\n" );
2882 sprintf (msgbuf
, format
, name
, symbol_address
, gp
,
2883 (signed)relocation
);
2885 r
= bfd_reloc_outofrange
;
2888 r
= _bfd_final_link_relocate (howto
, input_bfd
,
2889 input_section
, contents
,
2890 rel
->r_offset
, relocation
,
2896 r
= nios2_elf32_do_ujmp_relocate (input_bfd
, howto
,
2898 contents
, rel
->r_offset
,
2899 relocation
, rel
->r_addend
);
2902 r
= nios2_elf32_do_cjmp_relocate (input_bfd
, howto
,
2904 contents
, rel
->r_offset
,
2905 relocation
, rel
->r_addend
);
2908 r
= nios2_elf32_do_callr_relocate (input_bfd
, howto
,
2909 input_section
, contents
,
2910 rel
->r_offset
, relocation
,
2913 case R_NIOS2_CALL26
:
2914 case R_NIOS2_CALL26_NOAT
:
2915 /* If we have a call to an undefined weak symbol, we just want
2916 to stuff a zero in the bits of the call instruction and
2917 bypass the normal call26 relocation handling, because it'll
2918 diagnose an overflow error if address 0 isn't in the same
2919 256MB segment as the call site. Presumably the call
2920 should be guarded by a null check anyway. */
2921 if (h
!= NULL
&& h
->root
.type
== bfd_link_hash_undefweak
)
2923 BFD_ASSERT (relocation
== 0 && rel
->r_addend
== 0);
2924 r
= _bfd_final_link_relocate (howto
, input_bfd
,
2925 input_section
, contents
,
2926 rel
->r_offset
, relocation
,
2930 /* Handle relocations which should use the PLT entry.
2931 NIOS2_BFD_RELOC_32 relocations will use the symbol's value,
2932 which may point to a PLT entry, but we don't need to handle
2933 that here. If we created a PLT entry, all branches in this
2934 object should go to it. */
2935 if (h
!= NULL
&& splt
!= NULL
&& h
->plt
.offset
!= (bfd_vma
) -1)
2937 /* If we've created a .plt section, and assigned a PLT entry
2938 to this function, it should not be known to bind locally.
2939 If it were, we would have cleared the PLT entry. */
2940 BFD_ASSERT (!SYMBOL_CALLS_LOCAL (info
, h
));
2942 relocation
= (splt
->output_section
->vma
2943 + splt
->output_offset
2946 unresolved_reloc
= FALSE
;
2948 /* Detect R_NIOS2_CALL26 relocations that would overflow the
2949 256MB segment. Replace the target with a reference to a
2951 Note that htab->stub_group is null if relaxation has been
2952 disabled by the --no-relax linker command-line option, so
2953 we can use that to skip this processing entirely. */
2954 if (howto
->type
== R_NIOS2_CALL26
&& htab
->stub_group
)
2956 bfd_vma dest
= relocation
+ rel
->r_addend
;
2957 enum elf32_nios2_stub_type stub_type
;
2959 eh
= (struct elf32_nios2_link_hash_entry
*)h
;
2960 stub_type
= nios2_type_of_stub (input_section
, rel
, eh
,
2963 if (stub_type
!= nios2_stub_none
)
2965 struct elf32_nios2_stub_hash_entry
*hsh
;
2967 hsh
= nios2_get_stub_entry (input_section
, sec
,
2968 eh
, rel
, htab
, stub_type
);
2971 r
= bfd_reloc_undefined
;
2975 dest
= (hsh
->stub_offset
2976 + hsh
->stub_sec
->output_offset
2977 + hsh
->stub_sec
->output_section
->vma
);
2978 r
= nios2_elf32_do_call26_relocate (input_bfd
, howto
,
2988 r
= nios2_elf32_do_call26_relocate (input_bfd
, howto
,
2989 input_section
, contents
,
2990 rel
->r_offset
, relocation
,
2995 /* For symmetry this would be
2996 r = nios2_elf32_do_ignore_reloc (input_bfd, howto,
2997 input_section, contents,
2998 rel->r_offset, relocation,
3000 but do_ignore_reloc would do no more than return
3005 case R_NIOS2_CALL16
:
3006 case R_NIOS2_GOT_LO
:
3007 case R_NIOS2_GOT_HA
:
3008 case R_NIOS2_CALL_LO
:
3009 case R_NIOS2_CALL_HA
:
3010 /* Relocation is to the entry for this symbol in the
3011 global offset table. */
3014 r
= bfd_reloc_notsupported
;
3024 eh
= (struct elf32_nios2_link_hash_entry
*)h
;
3025 use_plt
= (eh
->got_types_used
== CALL_USED
3026 && h
->plt
.offset
!= (bfd_vma
) -1);
3028 off
= h
->got
.offset
;
3029 BFD_ASSERT (off
!= (bfd_vma
) -1);
3030 dyn
= elf_hash_table (info
)->dynamic_sections_created
;
3031 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, info
->shared
, h
)
3033 && SYMBOL_REFERENCES_LOCAL (info
, h
))
3034 || (ELF_ST_VISIBILITY (h
->other
)
3035 && h
->root
.type
== bfd_link_hash_undefweak
))
3037 /* This is actually a static link, or it is a -Bsymbolic
3038 link and the symbol is defined locally. We must
3039 initialize this entry in the global offset table.
3040 Since the offset must always be a multiple of 4, we
3041 use the least significant bit to record whether we
3042 have initialized it already.
3044 When doing a dynamic link, we create a .rela.got
3045 relocation entry to initialize the value. This is
3046 done in the finish_dynamic_symbol routine. */
3051 bfd_put_32 (output_bfd
, relocation
,
3052 sgot
->contents
+ off
);
3057 unresolved_reloc
= FALSE
;
3061 BFD_ASSERT (local_got_offsets
!= NULL
3062 && local_got_offsets
[r_symndx
] != (bfd_vma
) -1);
3064 off
= local_got_offsets
[r_symndx
];
3066 /* The offset must always be a multiple of 4. We use the
3067 least significant bit to record whether we have already
3068 generated the necessary reloc. */
3073 bfd_put_32 (output_bfd
, relocation
,
3074 sgot
->contents
+ off
);
3079 Elf_Internal_Rela outrel
;
3082 srelgot
= htab
->root
.srelgot
;
3083 BFD_ASSERT (srelgot
!= NULL
);
3085 outrel
.r_addend
= relocation
;
3086 outrel
.r_offset
= (sgot
->output_section
->vma
3087 + sgot
->output_offset
3089 outrel
.r_info
= ELF32_R_INFO (0, R_NIOS2_RELATIVE
);
3090 loc
= srelgot
->contents
;
3091 loc
+= (srelgot
->reloc_count
++ *
3092 sizeof (Elf32_External_Rela
));
3093 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
, loc
);
3096 local_got_offsets
[r_symndx
] |= 1;
3100 if (use_plt
&& info
->shared
)
3102 off
= ((h
->plt
.offset
- 24) / 12 + 3) * 4;
3103 relocation
= (htab
->root
.sgotplt
->output_offset
+ off
3107 relocation
= sgot
->output_offset
+ off
- got_base
;
3109 /* This relocation does not use the addend. */
3112 switch (howto
->type
)
3114 case R_NIOS2_GOT_LO
:
3115 case R_NIOS2_CALL_LO
:
3116 r
= nios2_elf32_do_lo16_relocate (input_bfd
, howto
,
3117 input_section
, contents
,
3118 rel
->r_offset
, relocation
,
3121 case R_NIOS2_GOT_HA
:
3122 case R_NIOS2_CALL_HA
:
3123 r
= nios2_elf32_do_hiadj16_relocate (input_bfd
, howto
,
3124 input_section
, contents
,
3130 r
= _bfd_final_link_relocate (howto
, input_bfd
,
3131 input_section
, contents
,
3132 rel
->r_offset
, relocation
,
3138 case R_NIOS2_GOTOFF_LO
:
3139 case R_NIOS2_GOTOFF_HA
:
3140 case R_NIOS2_GOTOFF
:
3141 /* Relocation is relative to the global offset table pointer. */
3143 BFD_ASSERT (sgot
!= NULL
);
3146 r
= bfd_reloc_notsupported
;
3150 /* Note that sgot->output_offset is not involved in this
3151 calculation. We always want the start of .got. */
3152 relocation
-= sgot
->output_section
->vma
;
3154 /* Now we adjust the relocation to be relative to the GOT pointer
3155 (the _gp_got symbol), which possibly contains the 0x8000 bias. */
3156 relocation
-= got_base
;
3158 switch (howto
->type
)
3160 case R_NIOS2_GOTOFF_LO
:
3161 r
= nios2_elf32_do_lo16_relocate (input_bfd
, howto
,
3162 input_section
, contents
,
3163 rel
->r_offset
, relocation
,
3166 case R_NIOS2_GOTOFF_HA
:
3167 r
= nios2_elf32_do_hiadj16_relocate (input_bfd
, howto
,
3168 input_section
, contents
,
3174 r
= _bfd_final_link_relocate (howto
, input_bfd
,
3175 input_section
, contents
,
3176 rel
->r_offset
, relocation
,
3182 case R_NIOS2_TLS_LDO16
:
3183 relocation
-= dtpoff_base (info
) + DTP_OFFSET
;
3185 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
3186 contents
, rel
->r_offset
,
3187 relocation
, rel
->r_addend
);
3189 case R_NIOS2_TLS_LDM16
:
3190 if (htab
->root
.sgot
== NULL
)
3193 off
= htab
->tls_ldm_got
.offset
;
3199 /* If we don't know the module number, create a relocation
3203 Elf_Internal_Rela outrel
;
3206 if (htab
->root
.srelgot
== NULL
)
3209 outrel
.r_addend
= 0;
3210 outrel
.r_offset
= (htab
->root
.sgot
->output_section
->vma
3211 + htab
->root
.sgot
->output_offset
3213 outrel
.r_info
= ELF32_R_INFO (0, R_NIOS2_TLS_DTPMOD
);
3215 loc
= htab
->root
.srelgot
->contents
;
3216 loc
+= (htab
->root
.srelgot
->reloc_count
++
3217 * sizeof (Elf32_External_Rela
));
3218 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
, loc
);
3221 bfd_put_32 (output_bfd
, 1,
3222 htab
->root
.sgot
->contents
+ off
);
3224 htab
->tls_ldm_got
.offset
|= 1;
3227 relocation
= htab
->root
.sgot
->output_offset
+ off
- got_base
;
3229 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
3230 contents
, rel
->r_offset
,
3231 relocation
, rel
->r_addend
);
3234 case R_NIOS2_TLS_GD16
:
3235 case R_NIOS2_TLS_IE16
:
3240 if (htab
->root
.sgot
== NULL
)
3247 dyn
= htab
->root
.dynamic_sections_created
;
3248 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, info
->shared
, h
)
3250 || !SYMBOL_REFERENCES_LOCAL (info
, h
)))
3252 unresolved_reloc
= FALSE
;
3255 off
= h
->got
.offset
;
3256 tls_type
= (((struct elf32_nios2_link_hash_entry
*) h
)
3261 if (local_got_offsets
== NULL
)
3263 off
= local_got_offsets
[r_symndx
];
3264 tls_type
= (elf32_nios2_local_got_tls_type (input_bfd
)
3268 if (tls_type
== GOT_UNKNOWN
)
3275 bfd_boolean need_relocs
= FALSE
;
3276 Elf_Internal_Rela outrel
;
3277 bfd_byte
*loc
= NULL
;
3280 /* The GOT entries have not been initialized yet. Do it
3281 now, and emit any relocations. If both an IE GOT and a
3282 GD GOT are necessary, we emit the GD first. */
3284 if ((info
->shared
|| indx
!= 0)
3286 || ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
3287 || h
->root
.type
!= bfd_link_hash_undefweak
))
3290 if (htab
->root
.srelgot
== NULL
)
3292 loc
= htab
->root
.srelgot
->contents
;
3293 loc
+= (htab
->root
.srelgot
->reloc_count
*
3294 sizeof (Elf32_External_Rela
));
3297 if (tls_type
& GOT_TLS_GD
)
3301 outrel
.r_addend
= 0;
3302 outrel
.r_offset
= (htab
->root
.sgot
->output_section
->vma
3303 + htab
->root
.sgot
->output_offset
3305 outrel
.r_info
= ELF32_R_INFO (indx
,
3306 R_NIOS2_TLS_DTPMOD
);
3308 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
,
3310 htab
->root
.srelgot
->reloc_count
++;
3311 loc
+= sizeof (Elf32_External_Rela
);
3314 bfd_put_32 (output_bfd
,
3315 (relocation
- dtpoff_base (info
) -
3317 htab
->root
.sgot
->contents
+ cur_off
+ 4);
3320 outrel
.r_addend
= 0;
3321 outrel
.r_info
= ELF32_R_INFO (indx
,
3322 R_NIOS2_TLS_DTPREL
);
3323 outrel
.r_offset
+= 4;
3325 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
,
3327 htab
->root
.srelgot
->reloc_count
++;
3328 loc
+= sizeof (Elf32_External_Rela
);
3333 /* If we are not emitting relocations for a
3334 general dynamic reference, then we must be in a
3335 static link or an executable link with the
3336 symbol binding locally. Mark it as belonging
3337 to module 1, the executable. */
3338 bfd_put_32 (output_bfd
, 1,
3339 htab
->root
.sgot
->contents
+ cur_off
);
3340 bfd_put_32 (output_bfd
, (relocation
-
3341 dtpoff_base (info
) -
3343 htab
->root
.sgot
->contents
+ cur_off
+ 4);
3349 if (tls_type
& GOT_TLS_IE
)
3354 outrel
.r_addend
= (relocation
-
3355 dtpoff_base (info
));
3357 outrel
.r_addend
= 0;
3358 outrel
.r_offset
= (htab
->root
.sgot
->output_section
->vma
3359 + htab
->root
.sgot
->output_offset
3361 outrel
.r_info
= ELF32_R_INFO (indx
,
3364 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
,
3366 htab
->root
.srelgot
->reloc_count
++;
3367 loc
+= sizeof (Elf32_External_Rela
);
3370 bfd_put_32 (output_bfd
, (tpoff (info
, relocation
)
3372 htab
->root
.sgot
->contents
+ cur_off
);
3379 local_got_offsets
[r_symndx
] |= 1;
3382 if ((tls_type
& GOT_TLS_GD
) && r_type
!= R_NIOS2_TLS_GD16
)
3384 relocation
= htab
->root
.sgot
->output_offset
+ off
- got_base
;
3386 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
3387 contents
, rel
->r_offset
,
3388 relocation
, rel
->r_addend
);
3392 case R_NIOS2_TLS_LE16
:
3393 if (info
->shared
&& !info
->pie
)
3395 (*_bfd_error_handler
)
3396 (_("%B(%A+0x%lx): R_NIOS2_TLS_LE16 relocation not "
3397 "permitted in shared object"),
3398 input_bfd
, input_section
,
3399 (long) rel
->r_offset
, howto
->name
);
3403 relocation
= tpoff (info
, relocation
) - TP_OFFSET
;
3405 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
3406 contents
, rel
->r_offset
,
3407 relocation
, rel
->r_addend
);
3410 case R_NIOS2_BFD_RELOC_32
:
3412 && (input_section
->flags
& SEC_ALLOC
) != 0
3414 || ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
3415 || h
->root
.type
!= bfd_link_hash_undefweak
))
3417 Elf_Internal_Rela outrel
;
3419 bfd_boolean skip
, relocate
;
3421 /* When generating a shared object, these relocations
3422 are copied into the output file to be resolved at run
3429 = _bfd_elf_section_offset (output_bfd
, info
,
3430 input_section
, rel
->r_offset
);
3431 if (outrel
.r_offset
== (bfd_vma
) -1)
3433 else if (outrel
.r_offset
== (bfd_vma
) -2)
3434 skip
= TRUE
, relocate
= TRUE
;
3435 outrel
.r_offset
+= (input_section
->output_section
->vma
3436 + input_section
->output_offset
);
3439 memset (&outrel
, 0, sizeof outrel
);
3444 || !h
->def_regular
))
3446 outrel
.r_info
= ELF32_R_INFO (h
->dynindx
, r_type
);
3447 outrel
.r_addend
= rel
->r_addend
;
3451 /* This symbol is local, or marked to become local. */
3452 outrel
.r_addend
= relocation
+ rel
->r_addend
;
3454 outrel
.r_info
= ELF32_R_INFO (0, R_NIOS2_RELATIVE
);
3457 sreloc
= elf_section_data (input_section
)->sreloc
;
3461 loc
= sreloc
->contents
;
3462 loc
+= sreloc
->reloc_count
++ * sizeof (Elf32_External_Rela
);
3463 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
, loc
);
3465 /* This reloc will be computed at runtime, so there's no
3466 need to do anything now, except for R_NIOS2_BFD_RELOC_32
3467 relocations that have been turned into
3468 R_NIOS2_RELATIVE. */
3473 r
= _bfd_final_link_relocate (howto
, input_bfd
,
3474 input_section
, contents
,
3475 rel
->r_offset
, relocation
,
3479 case R_NIOS2_TLS_DTPREL
:
3480 relocation
-= dtpoff_base (info
);
3484 r
= _bfd_final_link_relocate (howto
, input_bfd
,
3485 input_section
, contents
,
3486 rel
->r_offset
, relocation
,
3492 r
= bfd_reloc_notsupported
;
3494 if (r
!= bfd_reloc_ok
)
3497 name
= h
->root
.root
.string
;
3500 name
= bfd_elf_string_from_elf_section (input_bfd
,
3501 symtab_hdr
->sh_link
,
3503 if (name
== NULL
|| *name
== '\0')
3504 name
= bfd_section_name (input_bfd
, sec
);
3509 case bfd_reloc_overflow
:
3510 r
= info
->callbacks
->reloc_overflow (info
, NULL
, name
,
3511 howto
->name
, (bfd_vma
) 0,
3512 input_bfd
, input_section
,
3516 case bfd_reloc_undefined
:
3517 r
= info
->callbacks
->undefined_symbol (info
, name
, input_bfd
,
3519 rel
->r_offset
, TRUE
);
3522 case bfd_reloc_outofrange
:
3524 msg
= _("relocation out of range");
3527 case bfd_reloc_notsupported
:
3529 msg
= _("unsupported relocation");
3532 case bfd_reloc_dangerous
:
3534 msg
= _("dangerous relocation");
3539 msg
= _("unknown error");
3545 r
= info
->callbacks
->warning
3546 (info
, msg
, name
, input_bfd
, input_section
, rel
->r_offset
);
3554 /* Implement elf-backend_section_flags:
3555 Convert NIOS2 specific section flags to bfd internal section flags. */
3557 nios2_elf32_section_flags (flagword
*flags
, const Elf_Internal_Shdr
*hdr
)
3559 if (hdr
->sh_flags
& SHF_NIOS2_GPREL
)
3560 *flags
|= SEC_SMALL_DATA
;
3565 /* Implement elf_backend_fake_sections:
3566 Set the correct type for an NIOS2 ELF section. We do this by the
3567 section name, which is a hack, but ought to work. */
3569 nios2_elf32_fake_sections (bfd
*abfd ATTRIBUTE_UNUSED
,
3570 Elf_Internal_Shdr
*hdr
, asection
*sec
)
3572 register const char *name
= bfd_get_section_name (abfd
, sec
);
3574 if ((sec
->flags
& SEC_SMALL_DATA
)
3575 || strcmp (name
, ".sdata") == 0
3576 || strcmp (name
, ".sbss") == 0
3577 || strcmp (name
, ".lit4") == 0 || strcmp (name
, ".lit8") == 0)
3578 hdr
->sh_flags
|= SHF_NIOS2_GPREL
;
3583 /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
3584 shortcuts to them in our hash table. */
3586 create_got_section (bfd
*dynobj
, struct bfd_link_info
*info
)
3588 struct elf32_nios2_link_hash_table
*htab
;
3589 struct elf_link_hash_entry
*h
;
3591 htab
= elf32_nios2_hash_table (info
);
3593 if (! _bfd_elf_create_got_section (dynobj
, info
))
3596 /* In order for the two loads in .PLTresolve to share the same %hiadj,
3597 _GLOBAL_OFFSET_TABLE_ must be aligned to a 16-byte boundary. */
3598 if (!bfd_set_section_alignment (dynobj
, htab
->root
.sgotplt
, 4))
3601 /* The Nios II ABI specifies that GOT-relative relocations are relative
3602 to the linker-created symbol _gp_got, rather than using
3603 _GLOBAL_OFFSET_TABLE_ directly. In particular, the latter always
3604 points to the base of the GOT while _gp_got may include a bias. */
3605 h
= _bfd_elf_define_linkage_sym (dynobj
, info
, htab
->root
.sgotplt
,
3607 elf32_nios2_hash_table (info
)->h_gp_got
= h
;
3614 /* Implement elf_backend_create_dynamic_sections:
3615 Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
3616 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
3619 nios2_elf32_create_dynamic_sections (bfd
*dynobj
, struct bfd_link_info
*info
)
3621 struct elf32_nios2_link_hash_table
*htab
;
3623 htab
= elf32_nios2_hash_table (info
);
3624 if (!htab
->root
.sgot
&& !create_got_section (dynobj
, info
))
3627 _bfd_elf_create_dynamic_sections (dynobj
, info
);
3629 /* In order for the two loads in a shared object .PLTresolve to share the
3630 same %hiadj, the start of the PLT (as well as the GOT) must be aligned
3631 to a 16-byte boundary. This is because the addresses for these loads
3632 include the -(.plt+4) PIC correction. */
3633 if (!bfd_set_section_alignment (dynobj
, htab
->root
.splt
, 4))
3636 htab
->sdynbss
= bfd_get_linker_section (dynobj
, ".dynbss");
3641 htab
->srelbss
= bfd_get_linker_section (dynobj
, ".rela.bss");
3649 /* Implement elf_backend_copy_indirect_symbol:
3650 Copy the extra info we tack onto an elf_link_hash_entry. */
3652 nios2_elf32_copy_indirect_symbol (struct bfd_link_info
*info
,
3653 struct elf_link_hash_entry
*dir
,
3654 struct elf_link_hash_entry
*ind
)
3656 struct elf32_nios2_link_hash_entry
*edir
, *eind
;
3658 edir
= (struct elf32_nios2_link_hash_entry
*) dir
;
3659 eind
= (struct elf32_nios2_link_hash_entry
*) ind
;
3661 if (eind
->dyn_relocs
!= NULL
)
3663 if (edir
->dyn_relocs
!= NULL
)
3665 struct elf32_nios2_dyn_relocs
**pp
;
3666 struct elf32_nios2_dyn_relocs
*p
;
3668 /* Add reloc counts against the indirect sym to the direct sym
3669 list. Merge any entries against the same section. */
3670 for (pp
= &eind
->dyn_relocs
; (p
= *pp
) != NULL
; )
3672 struct elf32_nios2_dyn_relocs
*q
;
3674 for (q
= edir
->dyn_relocs
; q
!= NULL
; q
= q
->next
)
3675 if (q
->sec
== p
->sec
)
3677 q
->pc_count
+= p
->pc_count
;
3678 q
->count
+= p
->count
;
3685 *pp
= edir
->dyn_relocs
;
3688 edir
->dyn_relocs
= eind
->dyn_relocs
;
3689 eind
->dyn_relocs
= NULL
;
3692 if (ind
->root
.type
== bfd_link_hash_indirect
3693 && dir
->got
.refcount
<= 0)
3695 edir
->tls_type
= eind
->tls_type
;
3696 eind
->tls_type
= GOT_UNKNOWN
;
3699 edir
->got_types_used
|= eind
->got_types_used
;
3701 _bfd_elf_link_hash_copy_indirect (info
, dir
, ind
);
3704 /* Implement elf_backend_check_relocs:
3705 Look through the relocs for a section during the first phase. */
3707 nios2_elf32_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
,
3708 asection
*sec
, const Elf_Internal_Rela
*relocs
)
3711 Elf_Internal_Shdr
*symtab_hdr
;
3712 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
3713 const Elf_Internal_Rela
*rel
;
3714 const Elf_Internal_Rela
*rel_end
;
3715 struct elf32_nios2_link_hash_table
*htab
;
3718 asection
*sreloc
= NULL
;
3719 bfd_signed_vma
*local_got_refcounts
;
3721 if (info
->relocatable
)
3724 dynobj
= elf_hash_table (info
)->dynobj
;
3725 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3726 sym_hashes
= elf_sym_hashes (abfd
);
3727 sym_hashes_end
= (sym_hashes
3728 + symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
));
3729 if (!elf_bad_symtab (abfd
))
3730 sym_hashes_end
-= symtab_hdr
->sh_info
;
3731 local_got_refcounts
= elf_local_got_refcounts (abfd
);
3733 htab
= elf32_nios2_hash_table (info
);
3734 sgot
= htab
->root
.sgot
;
3735 srelgot
= htab
->root
.srelgot
;
3737 rel_end
= relocs
+ sec
->reloc_count
;
3738 for (rel
= relocs
; rel
< rel_end
; rel
++)
3740 unsigned int r_type
;
3741 struct elf_link_hash_entry
*h
;
3742 unsigned long r_symndx
;
3744 r_symndx
= ELF32_R_SYM (rel
->r_info
);
3745 if (r_symndx
< symtab_hdr
->sh_info
)
3749 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
3750 while (h
->root
.type
== bfd_link_hash_indirect
3751 || h
->root
.type
== bfd_link_hash_warning
)
3752 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3754 /* PR15323, ref flags aren't set for references in the same
3756 h
->root
.non_ir_ref
= 1;
3759 r_type
= ELF32_R_TYPE (rel
->r_info
);
3764 case R_NIOS2_GOT_LO
:
3765 case R_NIOS2_GOT_HA
:
3766 case R_NIOS2_CALL16
:
3767 case R_NIOS2_CALL_LO
:
3768 case R_NIOS2_CALL_HA
:
3769 case R_NIOS2_TLS_GD16
:
3770 case R_NIOS2_TLS_IE16
:
3771 /* This symbol requires a global offset table entry. */
3773 int tls_type
, old_tls_type
;
3779 case R_NIOS2_GOT_LO
:
3780 case R_NIOS2_GOT_HA
:
3781 case R_NIOS2_CALL16
:
3782 case R_NIOS2_CALL_LO
:
3783 case R_NIOS2_CALL_HA
:
3784 tls_type
= GOT_NORMAL
;
3786 case R_NIOS2_TLS_GD16
:
3787 tls_type
= GOT_TLS_GD
;
3789 case R_NIOS2_TLS_IE16
:
3790 tls_type
= GOT_TLS_IE
;
3796 /* Create the .got section. */
3797 elf_hash_table (info
)->dynobj
= dynobj
= abfd
;
3798 nios2_elf32_create_dynamic_sections (dynobj
, info
);
3803 sgot
= htab
->root
.sgot
;
3804 BFD_ASSERT (sgot
!= NULL
);
3808 && (h
!= NULL
|| info
->shared
))
3810 srelgot
= htab
->root
.srelgot
;
3811 BFD_ASSERT (srelgot
!= NULL
);
3816 struct elf32_nios2_link_hash_entry
*eh
3817 = (struct elf32_nios2_link_hash_entry
*)h
;
3819 old_tls_type
= elf32_nios2_hash_entry(h
)->tls_type
;
3820 if (r_type
== R_NIOS2_CALL16
3821 || r_type
== R_NIOS2_CALL_LO
3822 || r_type
== R_NIOS2_CALL_HA
)
3824 /* Make sure a plt entry is created for this symbol if
3825 it turns out to be a function defined by a dynamic
3830 eh
->got_types_used
|= CALL_USED
;
3833 eh
->got_types_used
|= GOT_USED
;
3837 /* This is a global offset table entry for a local symbol. */
3838 if (local_got_refcounts
== NULL
)
3842 size
= symtab_hdr
->sh_info
;
3843 size
*= (sizeof (bfd_signed_vma
) + sizeof (char));
3845 = ((bfd_signed_vma
*) bfd_zalloc (abfd
, size
));
3846 if (local_got_refcounts
== NULL
)
3848 elf_local_got_refcounts (abfd
) = local_got_refcounts
;
3849 elf32_nios2_local_got_tls_type (abfd
)
3850 = (char *) (local_got_refcounts
+ symtab_hdr
->sh_info
);
3852 local_got_refcounts
[r_symndx
]++;
3853 old_tls_type
= elf32_nios2_local_got_tls_type (abfd
) [r_symndx
];
3856 /* We will already have issued an error message if there is a
3857 TLS / non-TLS mismatch, based on the symbol type. We don't
3858 support any linker relaxations. So just combine any TLS
3860 if (old_tls_type
!= GOT_UNKNOWN
&& old_tls_type
!= GOT_NORMAL
3861 && tls_type
!= GOT_NORMAL
)
3862 tls_type
|= old_tls_type
;
3864 if (old_tls_type
!= tls_type
)
3867 elf32_nios2_hash_entry (h
)->tls_type
= tls_type
;
3869 elf32_nios2_local_got_tls_type (abfd
) [r_symndx
] = tls_type
;
3873 case R_NIOS2_TLS_LDM16
:
3874 if (r_type
== R_NIOS2_TLS_LDM16
)
3875 htab
->tls_ldm_got
.refcount
++;
3877 if (htab
->root
.sgot
== NULL
)
3879 if (htab
->root
.dynobj
== NULL
)
3880 htab
->root
.dynobj
= abfd
;
3881 if (!create_got_section (htab
->root
.dynobj
, info
))
3886 /* This relocation describes the C++ object vtable hierarchy.
3887 Reconstruct it for later use during GC. */
3888 case R_NIOS2_GNU_VTINHERIT
:
3889 if (!bfd_elf_gc_record_vtinherit (abfd
, sec
, h
, rel
->r_offset
))
3893 /* This relocation describes which C++ vtable entries are actually
3894 used. Record for later use during GC. */
3895 case R_NIOS2_GNU_VTENTRY
:
3896 if (!bfd_elf_gc_record_vtentry (abfd
, sec
, h
, rel
->r_addend
))
3900 case R_NIOS2_BFD_RELOC_32
:
3901 case R_NIOS2_CALL26
:
3902 case R_NIOS2_CALL26_NOAT
:
3903 case R_NIOS2_HIADJ16
:
3908 /* If this reloc is in a read-only section, we might
3909 need a copy reloc. We can't check reliably at this
3910 stage whether the section is read-only, as input
3911 sections have not yet been mapped to output sections.
3912 Tentatively set the flag for now, and correct in
3913 adjust_dynamic_symbol. */
3917 /* Make sure a plt entry is created for this symbol if it
3918 turns out to be a function defined by a dynamic object. */
3921 if (r_type
== R_NIOS2_CALL26
|| r_type
== R_NIOS2_CALL26_NOAT
)
3925 /* If we are creating a shared library, we need to copy the
3926 reloc into the shared library. */
3928 && (sec
->flags
& SEC_ALLOC
) != 0
3929 && (r_type
== R_NIOS2_BFD_RELOC_32
3930 || (h
!= NULL
&& ! h
->needs_plt
3931 && (! info
->symbolic
|| ! h
->def_regular
))))
3933 struct elf32_nios2_dyn_relocs
*p
;
3934 struct elf32_nios2_dyn_relocs
**head
;
3936 /* When creating a shared object, we must copy these
3937 reloc types into the output file. We create a reloc
3938 section in dynobj and make room for this reloc. */
3941 sreloc
= _bfd_elf_make_dynamic_reloc_section
3942 (sec
, dynobj
, 2, abfd
, TRUE
);
3947 /* If this is a global symbol, we count the number of
3948 relocations we need for this symbol. */
3950 head
= &((struct elf32_nios2_link_hash_entry
*) h
)->dyn_relocs
;
3953 /* Track dynamic relocs needed for local syms too.
3954 We really need local syms available to do this
3959 Elf_Internal_Sym
*isym
;
3961 isym
= bfd_sym_from_r_symndx (&htab
->sym_cache
,
3966 s
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
3970 vpp
= &elf_section_data (s
)->local_dynrel
;
3971 head
= (struct elf32_nios2_dyn_relocs
**) vpp
;
3975 if (p
== NULL
|| p
->sec
!= sec
)
3977 bfd_size_type amt
= sizeof *p
;
3978 p
= ((struct elf32_nios2_dyn_relocs
*)
3979 bfd_alloc (htab
->root
.dynobj
, amt
));
4000 /* Implement elf_backend_gc_mark_hook:
4001 Return the section that should be marked against GC for a given
4004 nios2_elf32_gc_mark_hook (asection
*sec
,
4005 struct bfd_link_info
*info
,
4006 Elf_Internal_Rela
*rel
,
4007 struct elf_link_hash_entry
*h
,
4008 Elf_Internal_Sym
*sym
)
4011 switch (ELF32_R_TYPE (rel
->r_info
))
4013 case R_NIOS2_GNU_VTINHERIT
:
4014 case R_NIOS2_GNU_VTENTRY
:
4017 return _bfd_elf_gc_mark_hook (sec
, info
, rel
, h
, sym
);
4020 /* Implement elf_backend_gc_sweep_hook:
4021 Update the got entry reference counts for the section being removed. */
4023 nios2_elf32_gc_sweep_hook (bfd
*abfd
,
4024 struct bfd_link_info
*info
,
4026 const Elf_Internal_Rela
*relocs
)
4028 Elf_Internal_Shdr
*symtab_hdr
;
4029 struct elf_link_hash_entry
**sym_hashes
;
4030 bfd_signed_vma
*local_got_refcounts
;
4031 const Elf_Internal_Rela
*rel
, *relend
;
4034 if (info
->relocatable
)
4037 elf_section_data (sec
)->local_dynrel
= NULL
;
4039 dynobj
= elf_hash_table (info
)->dynobj
;
4043 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4044 sym_hashes
= elf_sym_hashes (abfd
);
4045 local_got_refcounts
= elf_local_got_refcounts (abfd
);
4047 relend
= relocs
+ sec
->reloc_count
;
4048 for (rel
= relocs
; rel
< relend
; rel
++)
4050 unsigned long r_symndx
;
4051 struct elf_link_hash_entry
*h
= NULL
;
4054 r_symndx
= ELF32_R_SYM (rel
->r_info
);
4055 if (r_symndx
>= symtab_hdr
->sh_info
)
4057 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
4058 while (h
->root
.type
== bfd_link_hash_indirect
4059 || h
->root
.type
== bfd_link_hash_warning
)
4060 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4063 r_type
= ELF32_R_TYPE (rel
->r_info
);
4067 case R_NIOS2_GOT_LO
:
4068 case R_NIOS2_GOT_HA
:
4069 case R_NIOS2_CALL16
:
4070 case R_NIOS2_CALL_LO
:
4071 case R_NIOS2_CALL_HA
:
4074 if (h
->got
.refcount
> 0)
4077 else if (local_got_refcounts
!= NULL
)
4079 if (local_got_refcounts
[r_symndx
] > 0)
4080 --local_got_refcounts
[r_symndx
];
4084 case R_NIOS2_PCREL_LO
:
4085 case R_NIOS2_PCREL_HA
:
4086 case R_NIOS2_BFD_RELOC_32
:
4087 case R_NIOS2_CALL26
:
4088 case R_NIOS2_CALL26_NOAT
:
4091 struct elf32_nios2_link_hash_entry
*eh
;
4092 struct elf32_nios2_dyn_relocs
**pp
;
4093 struct elf32_nios2_dyn_relocs
*p
;
4095 eh
= (struct elf32_nios2_link_hash_entry
*) h
;
4097 if (h
->plt
.refcount
> 0)
4100 if (r_type
== R_NIOS2_PCREL_LO
|| r_type
== R_NIOS2_PCREL_HA
4101 || r_type
== R_NIOS2_BFD_RELOC_32
)
4103 for (pp
= &eh
->dyn_relocs
; (p
= *pp
) != NULL
;
4124 /* Implement elf_backend_finish_dynamic_symbols:
4125 Finish up dynamic symbol handling. We set the contents of various
4126 dynamic sections here. */
4128 nios2_elf32_finish_dynamic_symbol (bfd
*output_bfd
,
4129 struct bfd_link_info
*info
,
4130 struct elf_link_hash_entry
*h
,
4131 Elf_Internal_Sym
*sym
)
4133 struct elf32_nios2_link_hash_table
*htab
;
4134 struct elf32_nios2_link_hash_entry
*eh
4135 = (struct elf32_nios2_link_hash_entry
*)h
;
4138 htab
= elf32_nios2_hash_table (info
);
4140 if (h
->plt
.offset
!= (bfd_vma
) -1)
4147 Elf_Internal_Rela rela
;
4149 bfd_vma got_address
;
4151 /* This symbol has an entry in the procedure linkage table. Set
4153 BFD_ASSERT (h
->dynindx
!= -1);
4154 splt
= htab
->root
.splt
;
4155 sgotplt
= htab
->root
.sgotplt
;
4156 srela
= htab
->root
.srelplt
;
4157 BFD_ASSERT (splt
!= NULL
&& sgotplt
!= NULL
&& srela
!= NULL
);
4159 /* Emit the PLT entry. */
4162 nios2_elf32_install_data (splt
, nios2_so_plt_entry
, h
->plt
.offset
,
4164 plt_index
= (h
->plt
.offset
- 24) / 12;
4165 got_offset
= (plt_index
+ 3) * 4;
4166 nios2_elf32_install_imm16 (splt
, h
->plt
.offset
,
4167 hiadj(plt_index
* 4));
4168 nios2_elf32_install_imm16 (splt
, h
->plt
.offset
+ 4,
4169 (plt_index
* 4) & 0xffff);
4170 nios2_elf32_install_imm16 (splt
, h
->plt
.offset
+ 8,
4171 0xfff4 - h
->plt
.offset
);
4172 got_address
= (sgotplt
->output_section
->vma
+ sgotplt
->output_offset
4175 /* Fill in the entry in the global offset table. There are no
4176 res_n slots for a shared object PLT, instead the .got.plt entries
4177 point to the PLT entries. */
4178 bfd_put_32 (output_bfd
,
4179 splt
->output_section
->vma
+ splt
->output_offset
4180 + h
->plt
.offset
, sgotplt
->contents
+ got_offset
);
4184 plt_index
= (h
->plt
.offset
- 28 - htab
->res_n_size
) / 12;
4185 got_offset
= (plt_index
+ 3) * 4;
4187 nios2_elf32_install_data (splt
, nios2_plt_entry
, h
->plt
.offset
, 3);
4188 got_address
= (sgotplt
->output_section
->vma
+ sgotplt
->output_offset
4190 nios2_elf32_install_imm16 (splt
, h
->plt
.offset
, hiadj(got_address
));
4191 nios2_elf32_install_imm16 (splt
, h
->plt
.offset
+ 4,
4192 got_address
& 0xffff);
4194 /* Fill in the entry in the global offset table. */
4195 bfd_put_32 (output_bfd
,
4196 splt
->output_section
->vma
+ splt
->output_offset
4197 + plt_index
* 4, sgotplt
->contents
+ got_offset
);
4200 /* Fill in the entry in the .rela.plt section. */
4201 rela
.r_offset
= got_address
;
4202 rela
.r_info
= ELF32_R_INFO (h
->dynindx
, R_NIOS2_JUMP_SLOT
);
4204 loc
= srela
->contents
+ plt_index
* sizeof (Elf32_External_Rela
);
4205 bfd_elf32_swap_reloca_out (output_bfd
, &rela
, loc
);
4207 if (!h
->def_regular
)
4209 /* Mark the symbol as undefined, rather than as defined in
4210 the .plt section. Leave the value alone. */
4211 sym
->st_shndx
= SHN_UNDEF
;
4212 /* If the symbol is weak, we do need to clear the value.
4213 Otherwise, the PLT entry would provide a definition for
4214 the symbol even if the symbol wasn't defined anywhere,
4215 and so the symbol would never be NULL. */
4216 if (!h
->ref_regular_nonweak
)
4221 use_plt
= (eh
->got_types_used
== CALL_USED
4222 && h
->plt
.offset
!= (bfd_vma
) -1);
4224 if (!use_plt
&& h
->got
.offset
!= (bfd_vma
) -1
4225 && (elf32_nios2_hash_entry (h
)->tls_type
& GOT_TLS_GD
) == 0
4226 && (elf32_nios2_hash_entry (h
)->tls_type
& GOT_TLS_IE
) == 0)
4230 Elf_Internal_Rela rela
;
4234 /* This symbol has an entry in the global offset table. Set it
4236 sgot
= htab
->root
.sgot
;
4237 srela
= htab
->root
.srelgot
;
4238 BFD_ASSERT (sgot
!= NULL
&& srela
!= NULL
);
4240 offset
= (h
->got
.offset
& ~(bfd_vma
) 1);
4241 rela
.r_offset
= (sgot
->output_section
->vma
4242 + sgot
->output_offset
+ offset
);
4244 /* If this is a -Bsymbolic link, and the symbol is defined
4245 locally, we just want to emit a RELATIVE reloc. Likewise if
4246 the symbol was forced to be local because of a version file.
4247 The entry in the global offset table will already have been
4248 initialized in the relocate_section function. */
4250 if (info
->shared
&& SYMBOL_REFERENCES_LOCAL (info
, h
))
4252 rela
.r_info
= ELF32_R_INFO (0, R_NIOS2_RELATIVE
);
4253 rela
.r_addend
= bfd_get_signed_32 (output_bfd
,
4254 (sgot
->contents
+ offset
));
4255 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
+ offset
);
4259 bfd_put_32 (output_bfd
, (bfd_vma
) 0,
4260 sgot
->contents
+ offset
);
4261 rela
.r_info
= ELF32_R_INFO (h
->dynindx
, R_NIOS2_GLOB_DAT
);
4265 loc
= srela
->contents
;
4266 loc
+= srela
->reloc_count
++ * sizeof (Elf32_External_Rela
);
4267 bfd_elf32_swap_reloca_out (output_bfd
, &rela
, loc
);
4270 if (use_plt
&& h
->got
.offset
!= (bfd_vma
) -1)
4272 bfd_vma offset
= (h
->got
.offset
& ~(bfd_vma
) 1);
4273 asection
*sgot
= htab
->root
.sgot
;
4274 asection
*splt
= htab
->root
.splt
;
4275 bfd_put_32 (output_bfd
, (splt
->output_section
->vma
+ splt
->output_offset
4277 sgot
->contents
+ offset
);
4283 Elf_Internal_Rela rela
;
4286 /* This symbol needs a copy reloc. Set it up. */
4287 BFD_ASSERT (h
->dynindx
!= -1
4288 && (h
->root
.type
== bfd_link_hash_defined
4289 || h
->root
.type
== bfd_link_hash_defweak
));
4292 BFD_ASSERT (s
!= NULL
);
4294 rela
.r_offset
= (h
->root
.u
.def
.value
4295 + h
->root
.u
.def
.section
->output_section
->vma
4296 + h
->root
.u
.def
.section
->output_offset
);
4297 rela
.r_info
= ELF32_R_INFO (h
->dynindx
, R_NIOS2_COPY
);
4299 loc
= s
->contents
+ s
->reloc_count
++ * sizeof (Elf32_External_Rela
);
4300 bfd_elf32_swap_reloca_out (output_bfd
, &rela
, loc
);
4303 /* Mark _DYNAMIC, _GLOBAL_OFFSET_TABLE_, and _gp_got as absolute. */
4304 if (strcmp (h
->root
.root
.string
, "_DYNAMIC") == 0
4305 || h
== elf_hash_table (info
)->hgot
4306 || h
== elf32_nios2_hash_table (info
)->h_gp_got
)
4307 sym
->st_shndx
= SHN_ABS
;
4312 /* Implement elf_backend_finish_dynamic_sections. */
4314 nios2_elf32_finish_dynamic_sections (bfd
*output_bfd
,
4315 struct bfd_link_info
*info
)
4320 struct elf32_nios2_link_hash_table
*htab
;
4322 htab
= elf32_nios2_hash_table (info
);
4323 dynobj
= elf_hash_table (info
)->dynobj
;
4324 sgotplt
= htab
->root
.sgotplt
;
4325 BFD_ASSERT (sgotplt
!= NULL
);
4326 sdyn
= bfd_get_linker_section (dynobj
, ".dynamic");
4328 if (elf_hash_table (info
)->dynamic_sections_created
)
4331 Elf32_External_Dyn
*dyncon
, *dynconend
;
4333 splt
= htab
->root
.splt
;
4334 BFD_ASSERT (splt
!= NULL
&& sdyn
!= NULL
);
4336 dyncon
= (Elf32_External_Dyn
*) sdyn
->contents
;
4337 dynconend
= (Elf32_External_Dyn
*) (sdyn
->contents
+ sdyn
->size
);
4338 for (; dyncon
< dynconend
; dyncon
++)
4340 Elf_Internal_Dyn dyn
;
4343 bfd_elf32_swap_dyn_in (dynobj
, dyncon
, &dyn
);
4351 s
= htab
->root
.sgot
;
4352 BFD_ASSERT (s
!= NULL
);
4353 dyn
.d_un
.d_ptr
= s
->output_section
->vma
;
4354 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
4358 s
= htab
->root
.srelplt
;
4359 BFD_ASSERT (s
!= NULL
);
4360 dyn
.d_un
.d_ptr
= s
->output_section
->vma
;
4361 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
4365 s
= htab
->root
.srelplt
;
4366 BFD_ASSERT (s
!= NULL
);
4367 dyn
.d_un
.d_val
= s
->size
;
4368 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
4372 /* The procedure linkage table relocs (DT_JMPREL) should
4373 not be included in the overall relocs (DT_RELA).
4374 Therefore, we override the DT_RELASZ entry here to
4375 make it not include the JMPREL relocs. Since the
4376 linker script arranges for .rela.plt to follow all
4377 other relocation sections, we don't have to worry
4378 about changing the DT_RELA entry. */
4379 s
= htab
->root
.srelplt
;
4381 dyn
.d_un
.d_val
-= s
->size
;
4382 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
4386 s
= htab
->root
.sgot
;
4387 BFD_ASSERT (s
!= NULL
);
4388 dyn
.d_un
.d_ptr
= s
->output_section
->vma
+ 0x7ff0;
4389 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
4394 /* Fill in the first entry in the procedure linkage table. */
4397 bfd_vma got_address
= (sgotplt
->output_section
->vma
4398 + sgotplt
->output_offset
);
4401 bfd_vma corrected
= got_address
- (splt
->output_section
->vma
4402 + splt
->output_offset
+ 4);
4403 nios2_elf32_install_data (splt
, nios2_so_plt0_entry
, 0, 6);
4404 nios2_elf32_install_imm16 (splt
, 4, hiadj (corrected
));
4405 nios2_elf32_install_imm16 (splt
, 12, (corrected
& 0xffff) + 4);
4406 nios2_elf32_install_imm16 (splt
, 16, (corrected
& 0xffff) + 8);
4410 /* Divide by 4 here, not 3 because we already corrected for the
4412 bfd_vma res_size
= (splt
->size
- 28) / 4;
4413 bfd_vma res_start
= (splt
->output_section
->vma
4414 + splt
->output_offset
);
4417 for (res_offset
= 0; res_offset
< res_size
; res_offset
+= 4)
4418 bfd_put_32 (output_bfd
,
4419 6 | ((res_size
- (res_offset
+ 4)) << 6),
4420 splt
->contents
+ res_offset
);
4422 nios2_elf32_install_data (splt
, nios2_plt0_entry
, res_size
, 7);
4423 nios2_elf32_install_imm16 (splt
, res_size
, hiadj (res_start
));
4424 nios2_elf32_install_imm16 (splt
, res_size
+ 4,
4425 res_start
& 0xffff);
4426 nios2_elf32_install_imm16 (splt
, res_size
+ 12,
4427 hiadj (got_address
));
4428 nios2_elf32_install_imm16 (splt
, res_size
+ 16,
4429 (got_address
& 0xffff) + 4);
4430 nios2_elf32_install_imm16 (splt
, res_size
+ 20,
4431 (got_address
& 0xffff) + 8);
4435 /* Fill in the first three entries in the global offset table. */
4436 if (sgotplt
->size
> 0)
4439 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgotplt
->contents
);
4441 bfd_put_32 (output_bfd
,
4442 sdyn
->output_section
->vma
+ sdyn
->output_offset
,
4444 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgotplt
->contents
+ 4);
4445 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgotplt
->contents
+ 8);
4448 elf_section_data (sgotplt
->output_section
)->this_hdr
.sh_entsize
= 4;
4453 /* Implement elf_backend_adjust_dynamic_symbol:
4454 Adjust a symbol defined by a dynamic object and referenced by a
4455 regular object. The current definition is in some section of the
4456 dynamic object, but we're not including those sections. We have to
4457 change the definition to something the rest of the link can
4460 nios2_elf32_adjust_dynamic_symbol (struct bfd_link_info
*info
,
4461 struct elf_link_hash_entry
*h
)
4463 struct elf32_nios2_link_hash_table
*htab
;
4468 htab
= elf32_nios2_hash_table (info
);
4469 dynobj
= elf_hash_table (info
)->dynobj
;
4471 /* Make sure we know what is going on here. */
4472 BFD_ASSERT (dynobj
!= NULL
4474 || h
->u
.weakdef
!= NULL
4477 && !h
->def_regular
)));
4479 /* If this is a function, put it in the procedure linkage table. We
4480 will fill in the contents of the procedure linkage table later,
4481 when we know the address of the .got section. */
4482 if (h
->type
== STT_FUNC
|| h
->needs_plt
)
4484 if (h
->plt
.refcount
<= 0
4485 || SYMBOL_CALLS_LOCAL (info
, h
)
4486 || (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
4487 && h
->root
.type
== bfd_link_hash_undefweak
))
4489 /* This case can occur if we saw a PLT reloc in an input
4490 file, but the symbol was never referred to by a dynamic
4491 object, or if all references were garbage collected. In
4492 such a case, we don't actually need to build a procedure
4493 linkage table, and we can just do a PCREL reloc instead. */
4494 h
->plt
.offset
= (bfd_vma
) -1;
4501 /* Reinitialize the plt offset now that it is not used as a reference
4503 h
->plt
.offset
= (bfd_vma
) -1;
4505 /* If this is a weak symbol, and there is a real definition, the
4506 processor independent code will have arranged for us to see the
4507 real definition first, and we can just use the same value. */
4508 if (h
->u
.weakdef
!= NULL
)
4510 BFD_ASSERT (h
->u
.weakdef
->root
.type
== bfd_link_hash_defined
4511 || h
->u
.weakdef
->root
.type
== bfd_link_hash_defweak
);
4512 h
->root
.u
.def
.section
= h
->u
.weakdef
->root
.u
.def
.section
;
4513 h
->root
.u
.def
.value
= h
->u
.weakdef
->root
.u
.def
.value
;
4517 /* If there are no non-GOT references, we do not need a copy
4519 if (!h
->non_got_ref
)
4522 /* This is a reference to a symbol defined by a dynamic object which
4524 If we are creating a shared library, we must presume that the
4525 only references to the symbol are via the global offset table.
4526 For such cases we need not do anything here; the relocations will
4527 be handled correctly by relocate_section. */
4533 (*_bfd_error_handler
) (_("dynamic variable `%s' is zero size"),
4534 h
->root
.root
.string
);
4538 /* We must allocate the symbol in our .dynbss section, which will
4539 become part of the .bss section of the executable. There will be
4540 an entry for this symbol in the .dynsym section. The dynamic
4541 object will contain position independent code, so all references
4542 from the dynamic object to this symbol will go through the global
4543 offset table. The dynamic linker will use the .dynsym entry to
4544 determine the address it must put in the global offset table, so
4545 both the dynamic object and the regular object will refer to the
4546 same memory location for the variable. */
4548 BFD_ASSERT (s
!= NULL
);
4550 /* We must generate a R_NIOS2_COPY reloc to tell the dynamic linker to
4551 copy the initial value out of the dynamic object and into the
4552 runtime process image. We need to remember the offset into the
4553 .rela.bss section we are going to use. */
4554 if ((h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0)
4558 srel
= htab
->srelbss
;
4559 BFD_ASSERT (srel
!= NULL
);
4560 srel
->size
+= sizeof (Elf32_External_Rela
);
4564 align2
= bfd_log2 (h
->size
);
4565 if (align2
> h
->root
.u
.def
.section
->alignment_power
)
4566 align2
= h
->root
.u
.def
.section
->alignment_power
;
4569 s
->size
= BFD_ALIGN (s
->size
, (bfd_size_type
)1 << align2
);
4570 if (align2
> bfd_get_section_alignment (dynobj
, s
)
4571 && !bfd_set_section_alignment (dynobj
, s
, align2
))
4574 /* Define the symbol as being at this point in the section. */
4575 h
->root
.u
.def
.section
= s
;
4576 h
->root
.u
.def
.value
= s
->size
;
4578 /* Increment the section size to make room for the symbol. */
4584 /* Worker function for nios2_elf32_size_dynamic_sections. */
4586 adjust_dynrelocs (struct elf_link_hash_entry
*h
, PTR inf
)
4588 struct bfd_link_info
*info
;
4589 struct elf32_nios2_link_hash_table
*htab
;
4591 if (h
->root
.type
== bfd_link_hash_indirect
)
4594 if (h
->root
.type
== bfd_link_hash_warning
)
4595 /* When warning symbols are created, they **replace** the "real"
4596 entry in the hash table, thus we never get to see the real
4597 symbol in a hash traversal. So look at it now. */
4598 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4600 info
= (struct bfd_link_info
*) inf
;
4601 htab
= elf32_nios2_hash_table (info
);
4603 if (h
->plt
.offset
!= (bfd_vma
)-1)
4604 h
->plt
.offset
+= htab
->res_n_size
;
4605 if (htab
->root
.splt
== h
->root
.u
.def
.section
)
4606 h
->root
.u
.def
.value
+= htab
->res_n_size
;
4611 /* Another worker function for nios2_elf32_size_dynamic_sections.
4612 Allocate space in .plt, .got and associated reloc sections for
4615 allocate_dynrelocs (struct elf_link_hash_entry
*h
, PTR inf
)
4617 struct bfd_link_info
*info
;
4618 struct elf32_nios2_link_hash_table
*htab
;
4619 struct elf32_nios2_link_hash_entry
*eh
;
4620 struct elf32_nios2_dyn_relocs
*p
;
4623 if (h
->root
.type
== bfd_link_hash_indirect
)
4626 if (h
->root
.type
== bfd_link_hash_warning
)
4627 /* When warning symbols are created, they **replace** the "real"
4628 entry in the hash table, thus we never get to see the real
4629 symbol in a hash traversal. So look at it now. */
4630 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4632 info
= (struct bfd_link_info
*) inf
;
4633 htab
= elf32_nios2_hash_table (info
);
4635 if (htab
->root
.dynamic_sections_created
4636 && h
->plt
.refcount
> 0)
4638 /* Make sure this symbol is output as a dynamic symbol.
4639 Undefined weak syms won't yet be marked as dynamic. */
4640 if (h
->dynindx
== -1
4642 && !bfd_elf_link_record_dynamic_symbol (info
, h
))
4645 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info
->shared
, h
))
4647 asection
*s
= htab
->root
.splt
;
4649 /* Allocate room for the header. */
4658 h
->plt
.offset
= s
->size
;
4660 /* If this symbol is not defined in a regular file, and we are
4661 not generating a shared library, then set the symbol to this
4662 location in the .plt. This is required to make function
4663 pointers compare as equal between the normal executable and
4664 the shared library. */
4668 h
->root
.u
.def
.section
= s
;
4669 h
->root
.u
.def
.value
= h
->plt
.offset
;
4672 /* Make room for this entry. */
4675 /* We also need to make an entry in the .rela.plt section. */
4676 htab
->root
.srelplt
->size
+= sizeof (Elf32_External_Rela
);
4678 /* And the .got.plt section. */
4679 htab
->root
.sgotplt
->size
+= 4;
4683 h
->plt
.offset
= (bfd_vma
) -1;
4689 h
->plt
.offset
= (bfd_vma
) -1;
4693 eh
= (struct elf32_nios2_link_hash_entry
*) h
;
4694 use_plt
= (eh
->got_types_used
== CALL_USED
4695 && h
->plt
.offset
!= (bfd_vma
) -1);
4697 if (h
->got
.refcount
> 0)
4701 int tls_type
= eh
->tls_type
;
4704 /* Make sure this symbol is output as a dynamic symbol.
4705 Undefined weak syms won't yet be marked as dynamic. */
4706 if (h
->dynindx
== -1
4708 && !bfd_elf_link_record_dynamic_symbol (info
, h
))
4711 s
= htab
->root
.sgot
;
4712 h
->got
.offset
= s
->size
;
4714 if (tls_type
== GOT_UNKNOWN
)
4717 if (tls_type
== GOT_NORMAL
)
4718 /* Non-TLS symbols need one GOT slot. */
4722 if (tls_type
& GOT_TLS_GD
)
4723 /* R_NIOS2_TLS_GD16 needs 2 consecutive GOT slots. */
4725 if (tls_type
& GOT_TLS_IE
)
4726 /* R_NIOS2_TLS_IE16 needs one GOT slot. */
4730 dyn
= htab
->root
.dynamic_sections_created
;
4733 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, info
->shared
, h
)
4735 || !SYMBOL_REFERENCES_LOCAL (info
, h
)))
4738 if (tls_type
!= GOT_NORMAL
4739 && (info
->shared
|| indx
!= 0)
4740 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
4741 || h
->root
.type
!= bfd_link_hash_undefweak
))
4743 if (tls_type
& GOT_TLS_IE
)
4744 htab
->root
.srelgot
->size
+= sizeof (Elf32_External_Rela
);
4746 if (tls_type
& GOT_TLS_GD
)
4747 htab
->root
.srelgot
->size
+= sizeof (Elf32_External_Rela
);
4749 if ((tls_type
& GOT_TLS_GD
) && indx
!= 0)
4750 htab
->root
.srelgot
->size
+= sizeof (Elf32_External_Rela
);
4752 else if ((ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
4753 || h
->root
.type
!= bfd_link_hash_undefweak
)
4756 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, 0, h
)))
4757 htab
->root
.srelgot
->size
+= sizeof (Elf32_External_Rela
);
4760 h
->got
.offset
= (bfd_vma
) -1;
4762 if (eh
->dyn_relocs
== NULL
)
4765 /* In the shared -Bsymbolic case, discard space allocated for
4766 dynamic pc-relative relocs against symbols which turn out to be
4767 defined in regular objects. For the normal shared case, discard
4768 space for pc-relative relocs that have become local due to symbol
4769 visibility changes. */
4774 && (h
->forced_local
|| info
->symbolic
))
4776 struct elf32_nios2_dyn_relocs
**pp
;
4778 for (pp
= &eh
->dyn_relocs
; (p
= *pp
) != NULL
; )
4780 p
->count
-= p
->pc_count
;
4789 /* Also discard relocs on undefined weak syms with non-default
4791 if (eh
->dyn_relocs
!= NULL
4792 && h
->root
.type
== bfd_link_hash_undefweak
)
4794 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
4795 eh
->dyn_relocs
= NULL
;
4797 /* Make sure undefined weak symbols are output as a dynamic
4799 else if (h
->dynindx
== -1
4801 && !bfd_elf_link_record_dynamic_symbol (info
, h
))
4807 /* For the non-shared case, discard space for relocs against
4808 symbols which turn out to need copy relocs or are not
4812 && ((h
->def_dynamic
&& !h
->def_regular
)
4813 || (htab
->root
.dynamic_sections_created
4814 && (h
->root
.type
== bfd_link_hash_undefweak
4815 || h
->root
.type
== bfd_link_hash_undefined
))))
4817 /* Make sure this symbol is output as a dynamic symbol.
4818 Undefined weak syms won't yet be marked as dynamic. */
4819 if (h
->dynindx
== -1
4821 && !bfd_elf_link_record_dynamic_symbol (info
, h
))
4824 /* If that succeeded, we know we'll be keeping all the
4826 if (h
->dynindx
!= -1)
4830 eh
->dyn_relocs
= NULL
;
4835 /* Finally, allocate space. */
4836 for (p
= eh
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
4838 asection
*sreloc
= elf_section_data (p
->sec
)->sreloc
;
4839 sreloc
->size
+= p
->count
* sizeof (Elf32_External_Rela
);
4845 /* Implement elf_backend_size_dynamic_sections:
4846 Set the sizes of the dynamic sections. */
4848 nios2_elf32_size_dynamic_sections (bfd
*output_bfd ATTRIBUTE_UNUSED
,
4849 struct bfd_link_info
*info
)
4857 struct elf32_nios2_link_hash_table
*htab
;
4859 htab
= elf32_nios2_hash_table (info
);
4860 dynobj
= elf_hash_table (info
)->dynobj
;
4861 BFD_ASSERT (dynobj
!= NULL
);
4863 htab
->res_n_size
= 0;
4864 if (elf_hash_table (info
)->dynamic_sections_created
)
4866 /* Set the contents of the .interp section to the interpreter. */
4867 if (info
->executable
)
4869 s
= bfd_get_linker_section (dynobj
, ".interp");
4870 BFD_ASSERT (s
!= NULL
);
4871 s
->size
= sizeof ELF_DYNAMIC_INTERPRETER
;
4872 s
->contents
= (unsigned char *) ELF_DYNAMIC_INTERPRETER
;
4877 /* We may have created entries in the .rela.got section.
4878 However, if we are not creating the dynamic sections, we will
4879 not actually use these entries. Reset the size of .rela.got,
4880 which will cause it to get stripped from the output file
4882 s
= htab
->root
.srelgot
;
4887 /* Set up .got offsets for local syms, and space for local dynamic
4889 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link_next
)
4891 bfd_signed_vma
*local_got
;
4892 bfd_signed_vma
*end_local_got
;
4893 char *local_tls_type
;
4894 bfd_size_type locsymcount
;
4895 Elf_Internal_Shdr
*symtab_hdr
;
4898 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
4901 for (s
= ibfd
->sections
; s
!= NULL
; s
= s
->next
)
4903 struct elf32_nios2_dyn_relocs
*p
;
4905 for (p
= elf_section_data (s
)->local_dynrel
; p
!= NULL
; p
= p
->next
)
4907 if (!bfd_is_abs_section (p
->sec
)
4908 && bfd_is_abs_section (p
->sec
->output_section
))
4910 /* Input section has been discarded, either because
4911 it is a copy of a linkonce section or due to
4912 linker script /DISCARD/, so we'll be discarding
4915 else if (p
->count
!= 0)
4917 srel
= elf_section_data (p
->sec
)->sreloc
;
4918 srel
->size
+= p
->count
* sizeof (Elf32_External_Rela
);
4919 if ((p
->sec
->output_section
->flags
& SEC_READONLY
) != 0)
4920 info
->flags
|= DF_TEXTREL
;
4925 local_got
= elf_local_got_refcounts (ibfd
);
4929 symtab_hdr
= &elf_tdata (ibfd
)->symtab_hdr
;
4930 locsymcount
= symtab_hdr
->sh_info
;
4931 end_local_got
= local_got
+ locsymcount
;
4932 local_tls_type
= elf32_nios2_local_got_tls_type (ibfd
);
4933 s
= htab
->root
.sgot
;
4934 srel
= htab
->root
.srelgot
;
4935 for (; local_got
< end_local_got
; ++local_got
, ++local_tls_type
)
4939 *local_got
= s
->size
;
4940 if (*local_tls_type
& GOT_TLS_GD
)
4941 /* TLS_GD relocs need an 8-byte structure in the GOT. */
4943 if (*local_tls_type
& GOT_TLS_IE
)
4945 if (*local_tls_type
== GOT_NORMAL
)
4948 if (info
->shared
|| *local_tls_type
== GOT_TLS_GD
)
4949 srel
->size
+= sizeof (Elf32_External_Rela
);
4952 *local_got
= (bfd_vma
) -1;
4956 if (htab
->tls_ldm_got
.refcount
> 0)
4958 /* Allocate two GOT entries and one dynamic relocation (if necessary)
4959 for R_NIOS2_TLS_LDM16 relocations. */
4960 htab
->tls_ldm_got
.offset
= htab
->root
.sgot
->size
;
4961 htab
->root
.sgot
->size
+= 8;
4963 htab
->root
.srelgot
->size
+= sizeof (Elf32_External_Rela
);
4966 htab
->tls_ldm_got
.offset
= -1;
4968 /* Allocate global sym .plt and .got entries, and space for global
4969 sym dynamic relocs. */
4970 elf_link_hash_traverse (& htab
->root
, allocate_dynrelocs
, info
);
4972 if (elf_hash_table (info
)->dynamic_sections_created
)
4974 /* If the .got section is more than 0x8000 bytes, we add
4975 0x8000 to the value of _gp_got, so that 16-bit relocations
4976 have a greater chance of working. */
4977 if (htab
->root
.sgot
->size
>= 0x8000
4978 && elf32_nios2_hash_table (info
)->h_gp_got
->root
.u
.def
.value
== 0)
4979 elf32_nios2_hash_table (info
)->h_gp_got
->root
.u
.def
.value
= 0x8000;
4982 /* The check_relocs and adjust_dynamic_symbol entry points have
4983 determined the sizes of the various dynamic sections. Allocate
4988 for (s
= dynobj
->sections
; s
!= NULL
; s
= s
->next
)
4992 if ((s
->flags
& SEC_LINKER_CREATED
) == 0)
4995 /* It's OK to base decisions on the section name, because none
4996 of the dynobj section names depend upon the input files. */
4997 name
= bfd_get_section_name (dynobj
, s
);
4999 if (strcmp (name
, ".plt") == 0)
5001 /* Remember whether there is a PLT. */
5004 /* Correct for the number of res_N branches. */
5005 if (plt
&& !info
->shared
)
5007 htab
->res_n_size
= (s
->size
-28) / 3;
5008 s
->size
+= htab
->res_n_size
;
5011 else if (CONST_STRNEQ (name
, ".rela"))
5017 /* We use the reloc_count field as a counter if we need
5018 to copy relocs into the output file. */
5022 else if (CONST_STRNEQ (name
, ".got"))
5024 else if (strcmp (name
, ".dynbss") != 0)
5025 /* It's not one of our sections, so don't allocate space. */
5030 /* If we don't need this section, strip it from the
5031 output file. This is mostly to handle .rela.bss and
5032 .rela.plt. We must create both sections in
5033 create_dynamic_sections, because they must be created
5034 before the linker maps input sections to output
5035 sections. The linker does that before
5036 adjust_dynamic_symbol is called, and it is that
5037 function which decides whether anything needs to go
5038 into these sections. */
5039 s
->flags
|= SEC_EXCLUDE
;
5043 if ((s
->flags
& SEC_HAS_CONTENTS
) == 0)
5046 /* Allocate memory for the section contents. */
5047 /* FIXME: This should be a call to bfd_alloc not bfd_zalloc.
5048 Unused entries should be reclaimed before the section's contents
5049 are written out, but at the moment this does not happen. Thus in
5050 order to prevent writing out garbage, we initialize the section's
5051 contents to zero. */
5052 s
->contents
= (bfd_byte
*) bfd_zalloc (dynobj
, s
->size
);
5053 if (s
->contents
== NULL
)
5057 /* Adjust dynamic symbols that point to the plt to account for the
5058 now-known number of resN slots. */
5059 if (htab
->res_n_size
)
5060 elf_link_hash_traverse (& htab
->root
, adjust_dynrelocs
, info
);
5062 if (elf_hash_table (info
)->dynamic_sections_created
)
5064 /* Add some entries to the .dynamic section. We fill in the
5065 values later, in elf_nios2_finish_dynamic_sections, but we
5066 must add the entries now so that we get the correct size for
5067 the .dynamic section. The DT_DEBUG entry is filled in by the
5068 dynamic linker and used by the debugger. */
5069 #define add_dynamic_entry(TAG, VAL) \
5070 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
5072 if (!info
->shared
&& !add_dynamic_entry (DT_DEBUG
, 0))
5075 if (got
&& !add_dynamic_entry (DT_PLTGOT
, 0))
5079 && (!add_dynamic_entry (DT_PLTRELSZ
, 0)
5080 || !add_dynamic_entry (DT_PLTREL
, DT_RELA
)
5081 || !add_dynamic_entry (DT_JMPREL
, 0)))
5085 && (!add_dynamic_entry (DT_RELA
, 0)
5086 || !add_dynamic_entry (DT_RELASZ
, 0)
5087 || !add_dynamic_entry (DT_RELAENT
, sizeof (Elf32_External_Rela
))))
5090 if (!info
->shared
&& !add_dynamic_entry (DT_NIOS2_GP
, 0))
5093 if ((info
->flags
& DF_TEXTREL
) != 0
5094 && !add_dynamic_entry (DT_TEXTREL
, 0))
5097 #undef add_dynamic_entry
5102 /* Implement bfd_elf32_bfd_link_hash_table_create. */
5103 static struct bfd_link_hash_table
*
5104 nios2_elf32_link_hash_table_create (bfd
*abfd
)
5106 struct elf32_nios2_link_hash_table
*ret
;
5107 bfd_size_type amt
= sizeof (struct elf32_nios2_link_hash_table
);
5109 ret
= bfd_zmalloc (amt
);
5113 if (!_bfd_elf_link_hash_table_init (&ret
->root
, abfd
,
5116 elf32_nios2_link_hash_entry
),
5123 /* Init the stub hash table too. */
5124 if (!bfd_hash_table_init (&ret
->bstab
, stub_hash_newfunc
,
5125 sizeof (struct elf32_nios2_stub_hash_entry
)))
5128 return &ret
->root
.root
;
5131 /* Free the derived linker hash table. */
5133 nios2_elf32_link_hash_table_free (struct bfd_link_hash_table
*btab
)
5135 struct elf32_nios2_link_hash_table
*htab
5136 = (struct elf32_nios2_link_hash_table
*) btab
;
5138 bfd_hash_table_free (&htab
->bstab
);
5139 _bfd_elf_link_hash_table_free (btab
);
5142 /* Implement elf_backend_reloc_type_class. */
5143 static enum elf_reloc_type_class
5144 nios2_elf32_reloc_type_class (const struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
5145 const asection
*rel_sec ATTRIBUTE_UNUSED
,
5146 const Elf_Internal_Rela
*rela
)
5148 switch ((int) ELF32_R_TYPE (rela
->r_info
))
5150 case R_NIOS2_RELATIVE
:
5151 return reloc_class_relative
;
5152 case R_NIOS2_JUMP_SLOT
:
5153 return reloc_class_plt
;
5155 return reloc_class_copy
;
5157 return reloc_class_normal
;
5161 /* Return 1 if target is one of ours. */
5163 is_nios2_elf_target (const struct bfd_target
*targ
)
5165 return (targ
== &bfd_elf32_littlenios2_vec
5166 || targ
== &bfd_elf32_bignios2_vec
);
5169 /* Implement elf_backend_add_symbol_hook.
5170 This hook is called by the linker when adding symbols from an object
5171 file. We use it to put .comm items in .sbss, and not .bss. */
5173 nios2_elf_add_symbol_hook (bfd
*abfd
,
5174 struct bfd_link_info
*info
,
5175 Elf_Internal_Sym
*sym
,
5176 const char **namep ATTRIBUTE_UNUSED
,
5177 flagword
*flagsp ATTRIBUTE_UNUSED
,
5183 if (sym
->st_shndx
== SHN_COMMON
5184 && !info
->relocatable
5185 && sym
->st_size
<= elf_gp_size (abfd
)
5186 && is_nios2_elf_target (info
->output_bfd
->xvec
))
5188 /* Common symbols less than or equal to -G nn bytes are automatically
5190 struct elf32_nios2_link_hash_table
*htab
;
5192 htab
= elf32_nios2_hash_table (info
);
5193 if (htab
->sbss
== NULL
)
5195 flagword flags
= SEC_IS_COMMON
| SEC_LINKER_CREATED
;
5197 dynobj
= elf_hash_table (info
)->dynobj
;
5201 htab
->sbss
= bfd_make_section_anyway_with_flags (dynobj
, ".sbss",
5203 if (htab
->sbss
== NULL
)
5208 *valp
= sym
->st_size
;
5214 /* Implement elf_backend_can_make_relative_eh_frame:
5215 Decide whether to attempt to turn absptr or lsda encodings in
5216 shared libraries into pcrel within the given input section. */
5218 nios2_elf32_can_make_relative_eh_frame (bfd
*input_bfd ATTRIBUTE_UNUSED
,
5219 struct bfd_link_info
*info
5221 asection
*eh_frame_section
5224 /* We can't use PC-relative encodings in the .eh_frame section. */
5228 /* Implement elf_backend_special_sections. */
5229 const struct bfd_elf_special_section elf32_nios2_special_sections
[] =
5231 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS
,
5232 SHF_ALLOC
+ SHF_WRITE
+ SHF_NIOS2_GPREL
},
5233 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS
,
5234 SHF_ALLOC
+ SHF_WRITE
+ SHF_NIOS2_GPREL
},
5235 { NULL
, 0, 0, 0, 0 }
5238 #define ELF_ARCH bfd_arch_nios2
5239 #define ELF_TARGET_ID NIOS2_ELF_DATA
5240 #define ELF_MACHINE_CODE EM_ALTERA_NIOS2
5242 /* The Nios II MMU uses a 4K page size. */
5244 #define ELF_MAXPAGESIZE 0x1000
5246 #define bfd_elf32_bfd_link_hash_table_create \
5247 nios2_elf32_link_hash_table_create
5248 #define bfd_elf32_bfd_link_hash_table_free \
5249 nios2_elf32_link_hash_table_free
5251 /* Relocation table lookup macros. */
5253 #define bfd_elf32_bfd_reloc_type_lookup nios2_elf32_bfd_reloc_type_lookup
5254 #define bfd_elf32_bfd_reloc_name_lookup nios2_elf32_bfd_reloc_name_lookup
5256 /* JUMP_TABLE_LINK macros. */
5258 /* elf_info_to_howto (using RELA relocations). */
5260 #define elf_info_to_howto nios2_elf32_info_to_howto
5262 /* elf backend functions. */
5264 #define elf_backend_can_gc_sections 1
5265 #define elf_backend_can_refcount 1
5266 #define elf_backend_plt_readonly 1
5267 #define elf_backend_want_got_plt 1
5268 #define elf_backend_rela_normal 1
5270 #define elf_backend_relocate_section nios2_elf32_relocate_section
5271 #define elf_backend_section_flags nios2_elf32_section_flags
5272 #define elf_backend_fake_sections nios2_elf32_fake_sections
5273 #define elf_backend_check_relocs nios2_elf32_check_relocs
5275 #define elf_backend_gc_mark_hook nios2_elf32_gc_mark_hook
5276 #define elf_backend_gc_sweep_hook nios2_elf32_gc_sweep_hook
5277 #define elf_backend_create_dynamic_sections \
5278 nios2_elf32_create_dynamic_sections
5279 #define elf_backend_finish_dynamic_symbol nios2_elf32_finish_dynamic_symbol
5280 #define elf_backend_finish_dynamic_sections \
5281 nios2_elf32_finish_dynamic_sections
5282 #define elf_backend_adjust_dynamic_symbol nios2_elf32_adjust_dynamic_symbol
5283 #define elf_backend_reloc_type_class nios2_elf32_reloc_type_class
5284 #define elf_backend_size_dynamic_sections nios2_elf32_size_dynamic_sections
5285 #define elf_backend_add_symbol_hook nios2_elf_add_symbol_hook
5286 #define elf_backend_copy_indirect_symbol nios2_elf32_copy_indirect_symbol
5288 #define elf_backend_grok_prstatus nios2_grok_prstatus
5289 #define elf_backend_grok_psinfo nios2_grok_psinfo
5291 #undef elf_backend_can_make_relative_eh_frame
5292 #define elf_backend_can_make_relative_eh_frame \
5293 nios2_elf32_can_make_relative_eh_frame
5295 #define elf_backend_special_sections elf32_nios2_special_sections
5297 #define TARGET_LITTLE_SYM bfd_elf32_littlenios2_vec
5298 #define TARGET_LITTLE_NAME "elf32-littlenios2"
5299 #define TARGET_BIG_SYM bfd_elf32_bignios2_vec
5300 #define TARGET_BIG_NAME "elf32-bignios2"
5302 #define elf_backend_got_header_size 12
5304 #include "elf32-target.h"