1 /* 32-bit ELF support for Nios II.
2 Copyright (C) 2012, 2013 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 /* Add other relocations here. */
676 static unsigned char elf_code_to_howto_index
[R_NIOS2_ILLEGAL
+ 1];
678 /* Return the howto for relocation RTYPE. */
679 static reloc_howto_type
*
680 lookup_howto (unsigned int rtype
)
682 static int initialized
= 0;
684 int howto_tbl_size
= (int) (sizeof (elf_nios2_howto_table_rel
)
685 / sizeof (elf_nios2_howto_table_rel
[0]));
690 memset (elf_code_to_howto_index
, 0xff,
691 sizeof (elf_code_to_howto_index
));
692 for (i
= 0; i
< howto_tbl_size
; i
++)
693 elf_code_to_howto_index
[elf_nios2_howto_table_rel
[i
].type
] = i
;
696 BFD_ASSERT (rtype
<= R_NIOS2_ILLEGAL
);
697 i
= elf_code_to_howto_index
[rtype
];
698 if (i
>= howto_tbl_size
)
700 return elf_nios2_howto_table_rel
+ i
;
703 /* Map for converting BFD reloc types to Nios II reloc types. */
706 bfd_reloc_code_real_type bfd_val
;
707 enum elf_nios2_reloc_type elf_val
;
710 static const struct elf_reloc_map nios2_reloc_map
[] = {
711 {BFD_RELOC_NIOS2_S16
, R_NIOS2_S16
},
712 {BFD_RELOC_NIOS2_U16
, R_NIOS2_U16
},
713 {BFD_RELOC_16_PCREL
, R_NIOS2_PCREL16
},
714 {BFD_RELOC_NIOS2_CALL26
, R_NIOS2_CALL26
},
715 {BFD_RELOC_NIOS2_IMM5
, R_NIOS2_IMM5
},
716 {BFD_RELOC_NIOS2_CACHE_OPX
, R_NIOS2_CACHE_OPX
},
717 {BFD_RELOC_NIOS2_IMM6
, R_NIOS2_IMM6
},
718 {BFD_RELOC_NIOS2_IMM8
, R_NIOS2_IMM8
},
719 {BFD_RELOC_NIOS2_HI16
, R_NIOS2_HI16
},
720 {BFD_RELOC_NIOS2_LO16
, R_NIOS2_LO16
},
721 {BFD_RELOC_NIOS2_HIADJ16
, R_NIOS2_HIADJ16
},
722 {BFD_RELOC_32
, R_NIOS2_BFD_RELOC_32
},
723 {BFD_RELOC_16
, R_NIOS2_BFD_RELOC_16
},
724 {BFD_RELOC_8
, R_NIOS2_BFD_RELOC_8
},
725 {BFD_RELOC_NIOS2_GPREL
, R_NIOS2_GPREL
},
726 {BFD_RELOC_VTABLE_INHERIT
, R_NIOS2_GNU_VTINHERIT
},
727 {BFD_RELOC_VTABLE_ENTRY
, R_NIOS2_GNU_VTENTRY
},
728 {BFD_RELOC_NIOS2_UJMP
, R_NIOS2_UJMP
},
729 {BFD_RELOC_NIOS2_CJMP
, R_NIOS2_CJMP
},
730 {BFD_RELOC_NIOS2_CALLR
, R_NIOS2_CALLR
},
731 {BFD_RELOC_NIOS2_ALIGN
, R_NIOS2_ALIGN
},
732 {BFD_RELOC_NIOS2_GOT16
, R_NIOS2_GOT16
},
733 {BFD_RELOC_NIOS2_CALL16
, R_NIOS2_CALL16
},
734 {BFD_RELOC_NIOS2_GOTOFF_LO
, R_NIOS2_GOTOFF_LO
},
735 {BFD_RELOC_NIOS2_GOTOFF_HA
, R_NIOS2_GOTOFF_HA
},
736 {BFD_RELOC_NIOS2_PCREL_LO
, R_NIOS2_PCREL_LO
},
737 {BFD_RELOC_NIOS2_PCREL_HA
, R_NIOS2_PCREL_HA
},
738 {BFD_RELOC_NIOS2_TLS_GD16
, R_NIOS2_TLS_GD16
},
739 {BFD_RELOC_NIOS2_TLS_LDM16
, R_NIOS2_TLS_LDM16
},
740 {BFD_RELOC_NIOS2_TLS_LDO16
, R_NIOS2_TLS_LDO16
},
741 {BFD_RELOC_NIOS2_TLS_IE16
, R_NIOS2_TLS_IE16
},
742 {BFD_RELOC_NIOS2_TLS_LE16
, R_NIOS2_TLS_LE16
},
743 {BFD_RELOC_NIOS2_TLS_DTPMOD
, R_NIOS2_TLS_DTPMOD
},
744 {BFD_RELOC_NIOS2_TLS_DTPREL
, R_NIOS2_TLS_DTPREL
},
745 {BFD_RELOC_NIOS2_TLS_TPREL
, R_NIOS2_TLS_TPREL
},
746 {BFD_RELOC_NIOS2_COPY
, R_NIOS2_COPY
},
747 {BFD_RELOC_NIOS2_GLOB_DAT
, R_NIOS2_GLOB_DAT
},
748 {BFD_RELOC_NIOS2_JUMP_SLOT
, R_NIOS2_JUMP_SLOT
},
749 {BFD_RELOC_NIOS2_RELATIVE
, R_NIOS2_RELATIVE
},
750 {BFD_RELOC_NIOS2_GOTOFF
, R_NIOS2_GOTOFF
},
751 {BFD_RELOC_NIOS2_CALL26_NOAT
, R_NIOS2_CALL26_NOAT
},
754 enum elf32_nios2_stub_type
756 nios2_stub_call26_before
,
757 nios2_stub_call26_after
,
761 struct elf32_nios2_stub_hash_entry
763 /* Base hash table entry structure. */
764 struct bfd_hash_entry bh_root
;
766 /* The stub section. */
769 /* Offset within stub_sec of the beginning of this stub. */
772 /* Given the symbol's value and its section we can determine its final
773 value when building the stubs (so the stub knows where to jump. */
774 bfd_vma target_value
;
775 asection
*target_section
;
777 enum elf32_nios2_stub_type stub_type
;
779 /* The symbol table entry, if any, that this was derived from. */
780 struct elf32_nios2_link_hash_entry
*hh
;
782 /* And the reloc addend that this was derived from. */
785 /* Where this stub is being called from, or, in the case of combined
786 stub sections, the first input section in the group. */
790 #define nios2_stub_hash_entry(ent) \
791 ((struct elf32_nios2_stub_hash_entry *)(ent))
793 #define nios2_stub_hash_lookup(table, string, create, copy) \
794 ((struct elf32_nios2_stub_hash_entry *) \
795 bfd_hash_lookup ((table), (string), (create), (copy)))
798 /* The Nios II linker needs to keep track of the number of relocs that it
799 decides to copy as dynamic relocs in check_relocs for each symbol.
800 This is so that it can later discard them if they are found to be
801 unnecessary. We store the information in a field extending the
802 regular ELF linker hash table. */
804 struct elf32_nios2_dyn_relocs
806 struct elf32_nios2_dyn_relocs
*next
;
808 /* The input section of the reloc. */
811 /* Total number of relocs copied for the input section. */
814 /* Number of pc-relative relocs copied for the input section. */
815 bfd_size_type pc_count
;
818 /* Nios II ELF linker hash entry. */
820 struct elf32_nios2_link_hash_entry
822 struct elf_link_hash_entry root
;
824 /* A pointer to the most recently used stub hash entry against this
826 struct elf32_nios2_stub_hash_entry
*hsh_cache
;
828 /* Track dynamic relocs copied for this symbol. */
829 struct elf32_nios2_dyn_relocs
*dyn_relocs
;
831 #define GOT_UNKNOWN 0
835 unsigned char tls_type
;
837 /* We need to detect and take special action for symbols which are only
838 referenced with %call() and not with %got(). Such symbols do not need
839 a dynamic GOT reloc in shared objects, only a dynamic PLT reloc. Lazy
840 linking will not work if the dynamic GOT reloc exists.
841 To check for this condition efficiently, we compare got_types_used against
843 (got_types_used & (GOT16_USED | CALL16_USED)) == CALL16_USED. */
845 #define CALL16_USED 2
846 unsigned char got_types_used
;
849 #define elf32_nios2_hash_entry(ent) \
850 ((struct elf32_nios2_link_hash_entry *) (ent))
852 /* Get the Nios II elf linker hash table from a link_info structure. */
853 #define elf32_nios2_hash_table(info) \
854 ((struct elf32_nios2_link_hash_table *) ((info)->hash))
856 /* Nios II ELF linker hash table. */
857 struct elf32_nios2_link_hash_table
859 /* The main hash table. */
860 struct elf_link_hash_table root
;
862 /* The stub hash table. */
863 struct bfd_hash_table bstab
;
865 /* Linker stub bfd. */
868 /* Linker call-backs. */
869 asection
* (*add_stub_section
) (const char *, asection
*, bfd_boolean
);
870 void (*layout_sections_again
) (void);
872 /* Array to keep track of which stub sections have been created, and
873 information on stub grouping. */
876 /* These are the section to which stubs in the group will be
878 asection
*first_sec
, *last_sec
;
879 /* The stub sections. There might be stubs inserted either before
880 or after the real section.*/
881 asection
*first_stub_sec
, *last_stub_sec
;
884 /* Assorted information used by nios2_elf32_size_stubs. */
885 unsigned int bfd_count
;
887 asection
**input_list
;
888 Elf_Internal_Sym
**all_local_syms
;
890 /* Short-cuts to get to dynamic linker sections. */
896 bfd_signed_vma refcount
;
900 /* Small local sym cache. */
901 struct sym_cache sym_cache
;
906 struct nios2_elf32_obj_tdata
908 struct elf_obj_tdata root
;
910 /* tls_type for each local got entry. */
911 char *local_got_tls_type
;
913 /* TRUE if TLS GD relocs have been seen for this object. */
914 bfd_boolean has_tlsgd
;
917 #define elf32_nios2_tdata(abfd) \
918 ((struct nios2_elf32_obj_tdata *) (abfd)->tdata.any)
920 #define elf32_nios2_local_got_tls_type(abfd) \
921 (elf32_nios2_tdata (abfd)->local_got_tls_type)
923 /* The name of the dynamic interpreter. This is put in the .interp
925 #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
927 /* PLT implementation for position-dependent code. */
928 static const bfd_vma nios2_plt_entry
[] = { /* .PLTn: */
929 0x03c00034, /* movhi r15, %hiadj(plt_got_slot_address) */
930 0x7bc00017, /* ldw r15, %lo(plt_got_slot_address)(r15) */
931 0x7800683a /* jmp r15 */
934 static const bfd_vma nios2_plt0_entry
[] = { /* .PLTresolve */
935 0x03800034, /* movhi r14, %hiadj(res_0) */
936 0x73800004, /* addi r14, r14, %lo(res_0) */
937 0x7b9fc83a, /* sub r15, r15, r14 */
938 0x03400034, /* movhi r13, %hiadj(_GLOBAL_OFFSET_TABLE_) */
939 0x6b800017, /* ldw r14, %lo(_GLOBAL_OFFSET_TABLE_+4)(r13) */
940 0x6b400017, /* ldw r13, %lo(_GLOBAL_OFFSET_TABLE_+8)(r13) */
941 0x6800683a /* jmp r13 */
944 /* PLT implementation for position-independent code. */
945 static const bfd_vma nios2_so_plt_entry
[] = { /* .PLTn */
946 0x03c00034, /* movhi r15, %hiadj(index * 4) */
947 0x7bc00004, /* addi r15, r15, %lo(index * 4) */
948 0x00000006 /* br .PLTresolve */
951 static const bfd_vma nios2_so_plt0_entry
[] = { /* .PLTresolve */
952 0x001ce03a, /* nextpc r14 */
953 0x03400034, /* movhi r13, %hiadj(_GLOBAL_OFFSET_TABLE_) */
954 0x6b9b883a, /* add r13, r13, r14 */
955 0x6b800017, /* ldw r14, %lo(_GLOBAL_OFFSET_TABLE_+4)(r13) */
956 0x6b400017, /* ldw r13, %lo(_GLOBAL_OFFSET_TABLE_+8)(r13) */
957 0x6800683a /* jmp r13 */
961 static const bfd_vma nios2_call26_stub_entry
[] = {
962 0x00400034, /* orhi at, r0, %hiadj(dest) */
963 0x08400004, /* addi at, at, %lo(dest) */
964 0x0800683a /* jmp at */
967 /* Install 16-bit immediate value VALUE at offset OFFSET into section SEC. */
969 nios2_elf32_install_imm16 (asection
*sec
, bfd_vma offset
, bfd_vma value
)
971 bfd_vma word
= bfd_get_32 (sec
->owner
, sec
->contents
+ offset
);
973 BFD_ASSERT(value
<= 0xffff);
975 bfd_put_32 (sec
->owner
, word
| ((value
& 0xffff) << 6),
976 sec
->contents
+ offset
);
979 /* Install COUNT 32-bit values DATA starting at offset OFFSET into
982 nios2_elf32_install_data (asection
*sec
, const bfd_vma
*data
, bfd_vma offset
,
987 bfd_put_32 (sec
->owner
, *data
, sec
->contents
+ offset
);
993 /* The usual way of loading a 32-bit constant into a Nios II register is to
994 load the high 16 bits in one instruction and then add the low 16 bits with
995 a signed add. This means that the high halfword needs to be adjusted to
996 compensate for the sign bit of the low halfword. This function returns the
997 adjusted high halfword for a given 32-bit constant. */
999 bfd_vma
hiadj (bfd_vma symbol_value
)
1001 return ((symbol_value
+ 0x8000) >> 16) & 0xffff;
1004 /* Implement elf_backend_grok_prstatus:
1005 Support for core dump NOTE sections. */
1007 nios2_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
1012 switch (note
->descsz
)
1017 case 212: /* Linux/Nios II */
1019 elf_tdata (abfd
)->core
->signal
= bfd_get_16 (abfd
, note
->descdata
+ 12);
1022 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, note
->descdata
+ 24);
1031 /* Make a ".reg/999" section. */
1032 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
1033 size
, note
->descpos
+ offset
);
1036 /* Implement elf_backend_grok_psinfo. */
1038 nios2_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
1040 switch (note
->descsz
)
1045 case 124: /* Linux/Nios II elf_prpsinfo */
1046 elf_tdata (abfd
)->core
->program
1047 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 28, 16);
1048 elf_tdata (abfd
)->core
->command
1049 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 44, 80);
1052 /* Note that for some reason, a spurious space is tacked
1053 onto the end of the args in some (at least one anyway)
1054 implementations, so strip it off if it exists. */
1057 char *command
= elf_tdata (abfd
)->core
->command
;
1058 int n
= strlen (command
);
1060 if (0 < n
&& command
[n
- 1] == ' ')
1061 command
[n
- 1] = '\0';
1067 /* Assorted hash table functions. */
1069 /* Initialize an entry in the stub hash table. */
1070 static struct bfd_hash_entry
*
1071 stub_hash_newfunc (struct bfd_hash_entry
*entry
,
1072 struct bfd_hash_table
*table
,
1075 /* Allocate the structure if it has not already been allocated by a
1079 entry
= bfd_hash_allocate (table
,
1080 sizeof (struct elf32_nios2_stub_hash_entry
));
1085 /* Call the allocation method of the superclass. */
1086 entry
= bfd_hash_newfunc (entry
, table
, string
);
1089 struct elf32_nios2_stub_hash_entry
*hsh
;
1091 /* Initialize the local fields. */
1092 hsh
= (struct elf32_nios2_stub_hash_entry
*) entry
;
1093 hsh
->stub_sec
= NULL
;
1094 hsh
->stub_offset
= 0;
1095 hsh
->target_value
= 0;
1096 hsh
->target_section
= NULL
;
1097 hsh
->stub_type
= nios2_stub_none
;
1105 /* Create an entry in a Nios II ELF linker hash table. */
1106 static struct bfd_hash_entry
*
1107 link_hash_newfunc (struct bfd_hash_entry
*entry
,
1108 struct bfd_hash_table
*table
, const char *string
)
1110 /* Allocate the structure if it has not already been allocated by a
1114 entry
= bfd_hash_allocate (table
,
1115 sizeof (struct elf32_nios2_link_hash_entry
));
1120 /* Call the allocation method of the superclass. */
1121 entry
= _bfd_elf_link_hash_newfunc (entry
, table
, string
);
1124 struct elf32_nios2_link_hash_entry
*eh
;
1126 eh
= (struct elf32_nios2_link_hash_entry
*) entry
;
1127 eh
->hsh_cache
= NULL
;
1128 eh
->dyn_relocs
= NULL
;
1129 eh
->tls_type
= GOT_UNKNOWN
;
1130 eh
->got_types_used
= 0;
1136 /* Section name for stubs is the associated section name plus this
1138 #define STUB_SUFFIX ".stub"
1140 /* Build a name for an entry in the stub hash table. */
1142 nios2_stub_name (const asection
*input_section
,
1143 const asection
*sym_sec
,
1144 const struct elf32_nios2_link_hash_entry
*hh
,
1145 const Elf_Internal_Rela
*rel
,
1146 enum elf32_nios2_stub_type stub_type
)
1150 char stubpos
= (stub_type
== nios2_stub_call26_before
) ? 'b' : 'a';
1154 len
= 8 + 1 + 1 + 1+ strlen (hh
->root
.root
.root
.string
) + 1 + 8 + 1;
1155 stub_name
= bfd_malloc (len
);
1156 if (stub_name
!= NULL
)
1158 sprintf (stub_name
, "%08x_%c_%s+%x",
1159 input_section
->id
& 0xffffffff,
1161 hh
->root
.root
.root
.string
,
1162 (int) rel
->r_addend
& 0xffffffff);
1167 len
= 8 + 1 + 1 + 1+ 8 + 1 + 8 + 1 + 8 + 1;
1168 stub_name
= bfd_malloc (len
);
1169 if (stub_name
!= NULL
)
1171 sprintf (stub_name
, "%08x_%c_%x:%x+%x",
1172 input_section
->id
& 0xffffffff,
1174 sym_sec
->id
& 0xffffffff,
1175 (int) ELF32_R_SYM (rel
->r_info
) & 0xffffffff,
1176 (int) rel
->r_addend
& 0xffffffff);
1182 /* Look up an entry in the stub hash. Stub entries are cached because
1183 creating the stub name takes a bit of time. */
1184 static struct elf32_nios2_stub_hash_entry
*
1185 nios2_get_stub_entry (const asection
*input_section
,
1186 const asection
*sym_sec
,
1187 struct elf32_nios2_link_hash_entry
*hh
,
1188 const Elf_Internal_Rela
*rel
,
1189 struct elf32_nios2_link_hash_table
*htab
,
1190 enum elf32_nios2_stub_type stub_type
)
1192 struct elf32_nios2_stub_hash_entry
*hsh
;
1193 const asection
*id_sec
;
1195 /* If this input section is part of a group of sections sharing one
1196 stub section, then use the id of the first/last section in the group,
1197 depending on the stub section placement relative to the group.
1198 Stub names need to include a section id, as there may well be
1199 more than one stub used to reach say, printf, and we need to
1200 distinguish between them. */
1201 if (stub_type
== nios2_stub_call26_before
)
1202 id_sec
= htab
->stub_group
[input_section
->id
].first_sec
;
1204 id_sec
= htab
->stub_group
[input_section
->id
].last_sec
;
1206 if (hh
!= NULL
&& hh
->hsh_cache
!= NULL
1207 && hh
->hsh_cache
->hh
== hh
1208 && hh
->hsh_cache
->id_sec
== id_sec
1209 && hh
->hsh_cache
->stub_type
== stub_type
)
1211 hsh
= hh
->hsh_cache
;
1217 stub_name
= nios2_stub_name (id_sec
, sym_sec
, hh
, rel
, stub_type
);
1218 if (stub_name
== NULL
)
1221 hsh
= nios2_stub_hash_lookup (&htab
->bstab
,
1222 stub_name
, FALSE
, FALSE
);
1225 hh
->hsh_cache
= hsh
;
1233 /* Add a new stub entry to the stub hash. Not all fields of the new
1234 stub entry are initialised. */
1235 static struct elf32_nios2_stub_hash_entry
*
1236 nios2_add_stub (const char *stub_name
,
1238 struct elf32_nios2_link_hash_table
*htab
,
1239 enum elf32_nios2_stub_type stub_type
)
1243 asection
**secptr
, **linkptr
;
1244 struct elf32_nios2_stub_hash_entry
*hsh
;
1247 if (stub_type
== nios2_stub_call26_before
)
1249 link_sec
= htab
->stub_group
[section
->id
].first_sec
;
1250 secptr
= &(htab
->stub_group
[section
->id
].first_stub_sec
);
1251 linkptr
= &(htab
->stub_group
[link_sec
->id
].first_stub_sec
);
1256 link_sec
= htab
->stub_group
[section
->id
].last_sec
;
1257 secptr
= &(htab
->stub_group
[section
->id
].last_stub_sec
);
1258 linkptr
= &(htab
->stub_group
[link_sec
->id
].last_stub_sec
);
1262 if (stub_sec
== NULL
)
1264 stub_sec
= *linkptr
;
1265 if (stub_sec
== NULL
)
1271 namelen
= strlen (link_sec
->name
);
1272 len
= namelen
+ sizeof (STUB_SUFFIX
);
1273 s_name
= bfd_alloc (htab
->stub_bfd
, len
);
1277 memcpy (s_name
, link_sec
->name
, namelen
);
1278 memcpy (s_name
+ namelen
, STUB_SUFFIX
, sizeof (STUB_SUFFIX
));
1280 stub_sec
= (*htab
->add_stub_section
) (s_name
, link_sec
, afterp
);
1281 if (stub_sec
== NULL
)
1283 *linkptr
= stub_sec
;
1288 /* Enter this entry into the linker stub hash table. */
1289 hsh
= nios2_stub_hash_lookup (&htab
->bstab
, stub_name
,
1293 (*_bfd_error_handler
) (_("%B: cannot create stub entry %s"),
1299 hsh
->stub_sec
= stub_sec
;
1300 hsh
->stub_offset
= 0;
1301 hsh
->id_sec
= link_sec
;
1305 /* Set up various things so that we can make a list of input sections
1306 for each output section included in the link. Returns -1 on error,
1307 0 when no stubs will be needed, and 1 on success. */
1309 nios2_elf32_setup_section_lists (bfd
*output_bfd
, struct bfd_link_info
*info
)
1312 unsigned int bfd_count
;
1313 int top_id
, top_index
;
1315 asection
**input_list
, **list
;
1317 struct elf32_nios2_link_hash_table
*htab
= elf32_nios2_hash_table (info
);
1319 /* Count the number of input BFDs and find the top input section id. */
1320 for (input_bfd
= info
->input_bfds
, bfd_count
= 0, top_id
= 0;
1322 input_bfd
= input_bfd
->link_next
)
1325 for (section
= input_bfd
->sections
;
1327 section
= section
->next
)
1329 if (top_id
< section
->id
)
1330 top_id
= section
->id
;
1334 htab
->bfd_count
= bfd_count
;
1336 amt
= sizeof (struct map_stub
) * (top_id
+ 1);
1337 htab
->stub_group
= bfd_zmalloc (amt
);
1338 if (htab
->stub_group
== NULL
)
1341 /* We can't use output_bfd->section_count here to find the top output
1342 section index as some sections may have been removed, and
1343 strip_excluded_output_sections doesn't renumber the indices. */
1344 for (section
= output_bfd
->sections
, top_index
= 0;
1346 section
= section
->next
)
1348 if (top_index
< section
->index
)
1349 top_index
= section
->index
;
1352 htab
->top_index
= top_index
;
1353 amt
= sizeof (asection
*) * (top_index
+ 1);
1354 input_list
= bfd_malloc (amt
);
1355 htab
->input_list
= input_list
;
1356 if (input_list
== NULL
)
1359 /* For sections we aren't interested in, mark their entries with a
1360 value we can check later. */
1361 list
= input_list
+ top_index
;
1363 *list
= bfd_abs_section_ptr
;
1364 while (list
-- != input_list
);
1366 for (section
= output_bfd
->sections
;
1368 section
= section
->next
)
1370 /* FIXME: This is a bit of hack. Currently our .ctors and .dtors
1371 * have PC relative relocs in them but no code flag set. */
1372 if (((section
->flags
& SEC_CODE
) != 0) ||
1373 strcmp(".ctors", section
->name
) ||
1374 strcmp(".dtors", section
->name
))
1375 input_list
[section
->index
] = NULL
;
1381 /* The linker repeatedly calls this function for each input section,
1382 in the order that input sections are linked into output sections.
1383 Build lists of input sections to determine groupings between which
1384 we may insert linker stubs. */
1386 nios2_elf32_next_input_section (struct bfd_link_info
*info
, asection
*isec
)
1388 struct elf32_nios2_link_hash_table
*htab
= elf32_nios2_hash_table (info
);
1390 if (isec
->output_section
->index
<= htab
->top_index
)
1392 asection
**list
= htab
->input_list
+ isec
->output_section
->index
;
1393 if (*list
!= bfd_abs_section_ptr
)
1395 /* Steal the last_sec pointer for our list.
1396 This happens to make the list in reverse order,
1397 which is what we want. */
1398 htab
->stub_group
[isec
->id
].last_sec
= *list
;
1404 /* Segment mask for CALL26 relocation relaxation. */
1405 #define CALL26_SEGMENT(x) ((x) & 0xf0000000)
1407 /* Fudge factor for approximate maximum size of all stubs that might
1408 be inserted by the linker. This does not actually limit the number
1409 of stubs that might be inserted, and only affects strategy for grouping
1410 and placement of stubs. Perhaps this should be computed based on number
1411 of relocations seen, or be specifiable on the command line. */
1412 #define MAX_STUB_SECTION_SIZE 0xffff
1414 /* See whether we can group stub sections together. Grouping stub
1415 sections may result in fewer stubs. More importantly, we need to
1416 put all .init* and .fini* stubs at the end of the .init or
1417 .fini output sections respectively, because glibc splits the
1418 _init and _fini functions into multiple parts. Putting a stub in
1419 the middle of a function is not a good idea.
1420 Rather than computing groups of a maximum fixed size, for Nios II
1421 CALL26 relaxation it makes more sense to compute the groups based on
1422 sections that fit within a 256MB address segment. Also do not allow
1423 a group to span more than one output section, since different output
1424 sections might correspond to different memory banks on a bare-metal
1427 group_sections (struct elf32_nios2_link_hash_table
*htab
)
1429 asection
**list
= htab
->input_list
+ htab
->top_index
;
1432 /* The list is in reverse order so we'll search backwards looking
1433 for the first section that begins in the same memory segment,
1434 marking sections along the way to point at the tail for this
1436 asection
*tail
= *list
;
1437 if (tail
== bfd_abs_section_ptr
)
1439 while (tail
!= NULL
)
1441 bfd_vma start
= tail
->output_section
->vma
+ tail
->output_offset
;
1442 bfd_vma end
= start
+ tail
->size
;
1443 bfd_vma segment
= CALL26_SEGMENT (end
);
1446 if (segment
!= CALL26_SEGMENT (start
)
1447 || segment
!= CALL26_SEGMENT (end
+ MAX_STUB_SECTION_SIZE
))
1448 /* This section spans more than one memory segment, or is
1449 close enough to the end of the segment that adding stub
1450 sections before it might cause it to move so that it
1451 spans memory segments, or that stubs added at the end of
1452 this group might overflow into the next memory segment.
1453 Put it in a group by itself to localize the effects. */
1455 prev
= htab
->stub_group
[tail
->id
].last_sec
;
1456 htab
->stub_group
[tail
->id
].last_sec
= tail
;
1457 htab
->stub_group
[tail
->id
].first_sec
= tail
;
1460 /* Collect more sections for this group. */
1462 asection
*curr
, *first
;
1463 for (curr
= tail
; ; curr
= prev
)
1465 prev
= htab
->stub_group
[curr
->id
].last_sec
;
1467 || tail
->output_section
!= prev
->output_section
1468 || (CALL26_SEGMENT (prev
->output_section
->vma
1469 + prev
->output_offset
)
1474 for (curr
= tail
; ; curr
= prev
)
1476 prev
= htab
->stub_group
[curr
->id
].last_sec
;
1477 htab
->stub_group
[curr
->id
].last_sec
= tail
;
1478 htab
->stub_group
[curr
->id
].first_sec
= first
;
1484 /* Reset tail for the next group. */
1488 while (list
-- != htab
->input_list
);
1489 free (htab
->input_list
);
1492 /* Determine the type of stub needed, if any, for a call. */
1493 static enum elf32_nios2_stub_type
1494 nios2_type_of_stub (asection
*input_sec
,
1495 const Elf_Internal_Rela
*rel
,
1496 struct elf32_nios2_link_hash_entry
*hh
,
1497 struct elf32_nios2_link_hash_table
*htab
,
1498 bfd_vma destination
,
1499 struct bfd_link_info
*info ATTRIBUTE_UNUSED
)
1501 bfd_vma location
, segment
, start
, end
;
1502 asection
*s0
, *s1
, *s
;
1505 !(hh
->root
.root
.type
== bfd_link_hash_defined
1506 || hh
->root
.root
.type
== bfd_link_hash_defweak
))
1507 return nios2_stub_none
;
1509 /* Determine where the call point is. */
1510 location
= (input_sec
->output_section
->vma
1511 + input_sec
->output_offset
+ rel
->r_offset
);
1512 segment
= CALL26_SEGMENT (location
);
1514 /* Nios II CALL and JMPI instructions can transfer control to addresses
1515 within the same 256MB segment as the PC. */
1516 if (segment
== CALL26_SEGMENT (destination
))
1517 return nios2_stub_none
;
1519 /* Find the start and end addresses of the stub group. Also account for
1520 any already-created stub sections for this group. Note that for stubs
1521 in the end section, only the first instruction of the last stub
1522 (12 bytes long) needs to be within range. */
1523 s0
= htab
->stub_group
[input_sec
->id
].first_sec
;
1524 s
= htab
->stub_group
[s0
->id
].first_stub_sec
;
1525 if (s
!= NULL
&& s
->size
> 0)
1526 start
= s
->output_section
->vma
+ s
->output_offset
;
1528 start
= s0
->output_section
->vma
+ s0
->output_offset
;
1530 s1
= htab
->stub_group
[input_sec
->id
].last_sec
;
1531 s
= htab
->stub_group
[s1
->id
].last_stub_sec
;
1532 if (s
!= NULL
&& s
->size
> 0)
1533 end
= s
->output_section
->vma
+ s
->output_offset
+ s
->size
- 8;
1535 end
= s1
->output_section
->vma
+ s1
->output_offset
+ s1
->size
;
1537 BFD_ASSERT (start
< end
);
1538 BFD_ASSERT (start
<= location
);
1539 BFD_ASSERT (location
< end
);
1541 /* Put stubs at the end of the group unless that is not a valid
1542 location and the beginning of the group is. It might be that
1543 neither the beginning nor end works if we have an input section
1544 so large that it spans multiple segment boundaries. In that
1545 case, punt; the end result will be a relocation overflow error no
1546 matter what we do here.
1548 Note that adding stubs pushes up the addresses of all subsequent
1549 sections, so that stubs allocated on one pass through the
1550 relaxation loop may not be valid on the next pass. (E.g., we may
1551 allocate a stub at the beginning of the section on one pass and
1552 find that the call site has been bumped into the next memory
1553 segment on the next pass.) The important thing to note is that
1554 we never try to reclaim the space allocated to such unused stubs,
1555 so code size and section addresses can only increase with each
1556 iteration. Accounting for the start and end addresses of the
1557 already-created stub sections ensures that when the algorithm
1558 converges, it converges accurately, with the entire appropriate
1559 stub section accessible from the call site and not just the
1560 address at the start or end of the stub group proper. */
1562 if (segment
== CALL26_SEGMENT (end
))
1563 return nios2_stub_call26_after
;
1564 else if (segment
== CALL26_SEGMENT (start
))
1565 return nios2_stub_call26_before
;
1567 /* Perhaps this should be a dedicated error code. */
1568 return nios2_stub_none
;
1572 nios2_build_one_stub (struct bfd_hash_entry
*gen_entry
, void *in_arg ATTRIBUTE_UNUSED
)
1574 struct elf32_nios2_stub_hash_entry
*hsh
1575 = (struct elf32_nios2_stub_hash_entry
*) gen_entry
;
1576 asection
*stub_sec
= hsh
->stub_sec
;
1579 /* Make a note of the offset within the stubs for this entry. */
1580 hsh
->stub_offset
= stub_sec
->size
;
1582 switch (hsh
->stub_type
)
1584 case nios2_stub_call26_before
:
1585 case nios2_stub_call26_after
:
1586 /* A call26 stub looks like:
1587 orhi at, %hiadj(dest)
1588 addi at, at, %lo(dest)
1590 Note that call/jmpi instructions can't be used in PIC code
1591 so there is no reason for the stub to be PIC, either. */
1592 sym_value
= (hsh
->target_value
1593 + hsh
->target_section
->output_offset
1594 + hsh
->target_section
->output_section
->vma
1597 nios2_elf32_install_data (stub_sec
, nios2_call26_stub_entry
,
1598 hsh
->stub_offset
, 3);
1599 nios2_elf32_install_imm16 (stub_sec
, hsh
->stub_offset
,
1601 nios2_elf32_install_imm16 (stub_sec
, hsh
->stub_offset
+ 4,
1602 (sym_value
& 0xffff));
1603 stub_sec
->size
+= 12;
1613 /* As above, but don't actually build the stub. Just bump offset so
1614 we know stub section sizes. */
1616 nios2_size_one_stub (struct bfd_hash_entry
*gen_entry
, void *in_arg ATTRIBUTE_UNUSED
)
1618 struct elf32_nios2_stub_hash_entry
*hsh
1619 = (struct elf32_nios2_stub_hash_entry
*) gen_entry
;
1621 switch (hsh
->stub_type
)
1623 case nios2_stub_call26_before
:
1624 case nios2_stub_call26_after
:
1625 hsh
->stub_sec
->size
+= 12;
1634 /* Read in all local syms for all input bfds.
1635 Returns -1 on error, 0 otherwise. */
1638 get_local_syms (bfd
*output_bfd ATTRIBUTE_UNUSED
, bfd
*input_bfd
,
1639 struct bfd_link_info
*info
)
1641 unsigned int bfd_indx
;
1642 Elf_Internal_Sym
*local_syms
, **all_local_syms
;
1643 struct elf32_nios2_link_hash_table
*htab
= elf32_nios2_hash_table (info
);
1645 /* We want to read in symbol extension records only once. To do this
1646 we need to read in the local symbols in parallel and save them for
1647 later use; so hold pointers to the local symbols in an array. */
1648 bfd_size_type amt
= sizeof (Elf_Internal_Sym
*) * htab
->bfd_count
;
1649 all_local_syms
= bfd_zmalloc (amt
);
1650 htab
->all_local_syms
= all_local_syms
;
1651 if (all_local_syms
== NULL
)
1654 /* Walk over all the input BFDs, swapping in local symbols. */
1657 input_bfd
= input_bfd
->link_next
, bfd_indx
++)
1659 Elf_Internal_Shdr
*symtab_hdr
;
1661 /* We'll need the symbol table in a second. */
1662 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
1663 if (symtab_hdr
->sh_info
== 0)
1666 /* We need an array of the local symbols attached to the input bfd. */
1667 local_syms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
1668 if (local_syms
== NULL
)
1670 local_syms
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
,
1671 symtab_hdr
->sh_info
, 0,
1673 /* Cache them for elf_link_input_bfd. */
1674 symtab_hdr
->contents
= (unsigned char *) local_syms
;
1676 if (local_syms
== NULL
)
1679 all_local_syms
[bfd_indx
] = local_syms
;
1685 /* Determine and set the size of the stub section for a final link. */
1687 nios2_elf32_size_stubs (bfd
*output_bfd
, bfd
*stub_bfd
,
1688 struct bfd_link_info
*info
,
1689 asection
*(*add_stub_section
) (const char *,
1690 asection
*, bfd_boolean
),
1691 void (*layout_sections_again
) (void))
1693 bfd_boolean stub_changed
= FALSE
;
1694 struct elf32_nios2_link_hash_table
*htab
= elf32_nios2_hash_table (info
);
1696 /* Stash our params away. */
1697 htab
->stub_bfd
= stub_bfd
;
1698 htab
->add_stub_section
= add_stub_section
;
1699 htab
->layout_sections_again
= layout_sections_again
;
1701 /* FIXME: We only compute the section groups once. This could cause
1702 problems if adding a large stub section causes following sections,
1703 or parts of them, to move into another segment. However, this seems
1704 to be consistent with the way other back ends handle this.... */
1705 group_sections (htab
);
1707 if (get_local_syms (output_bfd
, info
->input_bfds
, info
))
1709 if (htab
->all_local_syms
)
1710 goto error_ret_free_local
;
1717 unsigned int bfd_indx
;
1720 for (input_bfd
= info
->input_bfds
, bfd_indx
= 0;
1722 input_bfd
= input_bfd
->link_next
, bfd_indx
++)
1724 Elf_Internal_Shdr
*symtab_hdr
;
1726 Elf_Internal_Sym
*local_syms
;
1728 /* We'll need the symbol table in a second. */
1729 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
1730 if (symtab_hdr
->sh_info
== 0)
1733 local_syms
= htab
->all_local_syms
[bfd_indx
];
1735 /* Walk over each section attached to the input bfd. */
1736 for (section
= input_bfd
->sections
;
1738 section
= section
->next
)
1740 Elf_Internal_Rela
*internal_relocs
, *irelaend
, *irela
;
1742 /* If there aren't any relocs, then there's nothing more
1744 if ((section
->flags
& SEC_RELOC
) == 0
1745 || section
->reloc_count
== 0)
1748 /* If this section is a link-once section that will be
1749 discarded, then don't create any stubs. */
1750 if (section
->output_section
== NULL
1751 || section
->output_section
->owner
!= output_bfd
)
1754 /* Get the relocs. */
1756 = _bfd_elf_link_read_relocs (input_bfd
, section
, NULL
, NULL
,
1758 if (internal_relocs
== NULL
)
1759 goto error_ret_free_local
;
1761 /* Now examine each relocation. */
1762 irela
= internal_relocs
;
1763 irelaend
= irela
+ section
->reloc_count
;
1764 for (; irela
< irelaend
; irela
++)
1766 unsigned int r_type
, r_indx
;
1767 enum elf32_nios2_stub_type stub_type
;
1768 struct elf32_nios2_stub_hash_entry
*hsh
;
1771 bfd_vma destination
;
1772 struct elf32_nios2_link_hash_entry
*hh
;
1774 const asection
*id_sec
;
1776 r_type
= ELF32_R_TYPE (irela
->r_info
);
1777 r_indx
= ELF32_R_SYM (irela
->r_info
);
1779 if (r_type
>= (unsigned int) R_NIOS2_ILLEGAL
)
1781 bfd_set_error (bfd_error_bad_value
);
1782 error_ret_free_internal
:
1783 if (elf_section_data (section
)->relocs
== NULL
)
1784 free (internal_relocs
);
1785 goto error_ret_free_local
;
1788 /* Only look for stubs on CALL and JMPI instructions. */
1789 if (r_type
!= (unsigned int) R_NIOS2_CALL26
)
1792 /* Now determine the call target, its name, value,
1798 if (r_indx
< symtab_hdr
->sh_info
)
1800 /* It's a local symbol. */
1801 Elf_Internal_Sym
*sym
;
1802 Elf_Internal_Shdr
*hdr
;
1805 sym
= local_syms
+ r_indx
;
1806 if (ELF_ST_TYPE (sym
->st_info
) != STT_SECTION
)
1807 sym_value
= sym
->st_value
;
1808 shndx
= sym
->st_shndx
;
1809 if (shndx
< elf_numsections (input_bfd
))
1811 hdr
= elf_elfsections (input_bfd
)[shndx
];
1812 sym_sec
= hdr
->bfd_section
;
1813 destination
= (sym_value
+ irela
->r_addend
1814 + sym_sec
->output_offset
1815 + sym_sec
->output_section
->vma
);
1820 /* It's an external symbol. */
1823 e_indx
= r_indx
- symtab_hdr
->sh_info
;
1824 hh
= ((struct elf32_nios2_link_hash_entry
*)
1825 elf_sym_hashes (input_bfd
)[e_indx
]);
1827 while (hh
->root
.root
.type
== bfd_link_hash_indirect
1828 || hh
->root
.root
.type
== bfd_link_hash_warning
)
1829 hh
= ((struct elf32_nios2_link_hash_entry
*)
1830 hh
->root
.root
.u
.i
.link
);
1832 if (hh
->root
.root
.type
== bfd_link_hash_defined
1833 || hh
->root
.root
.type
== bfd_link_hash_defweak
)
1835 sym_sec
= hh
->root
.root
.u
.def
.section
;
1836 sym_value
= hh
->root
.root
.u
.def
.value
;
1838 if (sym_sec
->output_section
!= NULL
)
1839 destination
= (sym_value
+ irela
->r_addend
1840 + sym_sec
->output_offset
1841 + sym_sec
->output_section
->vma
);
1845 else if (hh
->root
.root
.type
== bfd_link_hash_undefweak
)
1850 else if (hh
->root
.root
.type
== bfd_link_hash_undefined
)
1852 if (! (info
->unresolved_syms_in_objects
== RM_IGNORE
1853 && (ELF_ST_VISIBILITY (hh
->root
.other
)
1859 bfd_set_error (bfd_error_bad_value
);
1860 goto error_ret_free_internal
;
1864 /* Determine what (if any) linker stub is needed. */
1865 stub_type
= nios2_type_of_stub (section
, irela
, hh
, htab
,
1867 if (stub_type
== nios2_stub_none
)
1870 /* Support for grouping stub sections. */
1871 if (stub_type
== nios2_stub_call26_before
)
1872 id_sec
= htab
->stub_group
[section
->id
].first_sec
;
1874 id_sec
= htab
->stub_group
[section
->id
].last_sec
;
1876 /* Get the name of this stub. */
1877 stub_name
= nios2_stub_name (id_sec
, sym_sec
, hh
, irela
,
1880 goto error_ret_free_internal
;
1882 hsh
= nios2_stub_hash_lookup (&htab
->bstab
,
1887 /* The proper stub has already been created. */
1892 hsh
= nios2_add_stub (stub_name
, section
, htab
, stub_type
);
1896 goto error_ret_free_internal
;
1898 hsh
->target_value
= sym_value
;
1899 hsh
->target_section
= sym_sec
;
1900 hsh
->stub_type
= stub_type
;
1902 hsh
->addend
= irela
->r_addend
;
1903 stub_changed
= TRUE
;
1906 /* We're done with the internal relocs, free them. */
1907 if (elf_section_data (section
)->relocs
== NULL
)
1908 free (internal_relocs
);
1915 /* OK, we've added some stubs. Find out the new size of the
1917 for (stub_sec
= htab
->stub_bfd
->sections
;
1919 stub_sec
= stub_sec
->next
)
1922 bfd_hash_traverse (&htab
->bstab
, nios2_size_one_stub
, htab
);
1924 /* Ask the linker to do its stuff. */
1925 (*htab
->layout_sections_again
) ();
1926 stub_changed
= FALSE
;
1929 free (htab
->all_local_syms
);
1932 error_ret_free_local
:
1933 free (htab
->all_local_syms
);
1937 /* Build all the stubs associated with the current output file. The
1938 stubs are kept in a hash table attached to the main linker hash
1939 table. This function is called via nios2elf_finish in the linker. */
1941 nios2_elf32_build_stubs (struct bfd_link_info
*info
)
1944 struct bfd_hash_table
*table
;
1945 struct elf32_nios2_link_hash_table
*htab
;
1947 htab
= elf32_nios2_hash_table (info
);
1949 for (stub_sec
= htab
->stub_bfd
->sections
;
1951 stub_sec
= stub_sec
->next
)
1955 /* Allocate memory to hold the linker stubs. */
1956 size
= stub_sec
->size
;
1957 stub_sec
->contents
= bfd_zalloc (htab
->stub_bfd
, size
);
1958 if (stub_sec
->contents
== NULL
&& size
!= 0)
1963 /* Build the stubs as directed by the stub hash table. */
1964 table
= &htab
->bstab
;
1965 bfd_hash_traverse (table
, nios2_build_one_stub
, info
);
1971 /* Implement bfd_elf32_bfd_reloc_type_lookup:
1972 Given a BFD reloc type, return a howto structure. */
1973 static reloc_howto_type
*
1974 nios2_elf32_bfd_reloc_type_lookup (bfd
*abfd ATTRIBUTE_UNUSED
,
1975 bfd_reloc_code_real_type code
)
1979 i
< (int) (sizeof (nios2_reloc_map
) / sizeof (struct elf_reloc_map
));
1981 if (nios2_reloc_map
[i
].bfd_val
== code
)
1982 return &elf_nios2_howto_table_rel
[(int) nios2_reloc_map
[i
].elf_val
];
1986 /* Implement bfd_elf32_bfd_reloc_name_lookup:
1987 Given a reloc name, return a howto structure. */
1988 static reloc_howto_type
*
1989 nios2_elf32_bfd_reloc_name_lookup (bfd
*abfd ATTRIBUTE_UNUSED
,
1994 i
< (sizeof (elf_nios2_howto_table_rel
)
1995 / sizeof (elf_nios2_howto_table_rel
[0]));
1997 if (elf_nios2_howto_table_rel
[i
].name
1998 && strcasecmp (elf_nios2_howto_table_rel
[i
].name
, r_name
) == 0)
1999 return &elf_nios2_howto_table_rel
[i
];
2004 /* Implement elf_info_to_howto:
2005 Given a ELF32 relocation, fill in a arelent structure. */
2007 nios2_elf32_info_to_howto (bfd
*abfd ATTRIBUTE_UNUSED
, arelent
*cache_ptr
,
2008 Elf_Internal_Rela
*dst
)
2010 unsigned int r_type
;
2012 r_type
= ELF32_R_TYPE (dst
->r_info
);
2013 BFD_ASSERT (r_type
< R_NIOS2_ILLEGAL
);
2014 cache_ptr
->howto
= &elf_nios2_howto_table_rel
[r_type
];
2017 /* Return the base VMA address which should be subtracted from real addresses
2018 when resolving @dtpoff relocation.
2019 This is PT_TLS segment p_vaddr. */
2021 dtpoff_base (struct bfd_link_info
*info
)
2023 /* If tls_sec is NULL, we should have signalled an error already. */
2024 if (elf_hash_table (info
)->tls_sec
== NULL
)
2026 return elf_hash_table (info
)->tls_sec
->vma
;
2029 /* Return the relocation value for @tpoff relocation
2030 if STT_TLS virtual address is ADDRESS. */
2032 tpoff (struct bfd_link_info
*info
, bfd_vma address
)
2034 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
2036 /* If tls_sec is NULL, we should have signalled an error already. */
2037 if (htab
->tls_sec
== NULL
)
2039 return address
- htab
->tls_sec
->vma
;
2042 /* Set the GP value for OUTPUT_BFD. Returns FALSE if this is a
2043 dangerous relocation. */
2045 nios2_elf_assign_gp (bfd
*output_bfd
, bfd_vma
*pgp
, struct bfd_link_info
*info
)
2048 bfd_boolean gp_found
;
2049 struct bfd_hash_entry
*h
;
2050 struct bfd_link_hash_entry
*lh
;
2052 /* If we've already figured out what GP will be, just return it. */
2053 *pgp
= _bfd_get_gp_value (output_bfd
);
2057 h
= bfd_hash_lookup (&info
->hash
->table
, "_gp", FALSE
, FALSE
);
2058 lh
= (struct bfd_link_hash_entry
*) h
;
2064 case bfd_link_hash_undefined
:
2065 case bfd_link_hash_undefweak
:
2066 case bfd_link_hash_common
:
2069 case bfd_link_hash_defined
:
2070 case bfd_link_hash_defweak
:
2072 *pgp
= lh
->u
.def
.value
;
2074 case bfd_link_hash_indirect
:
2075 case bfd_link_hash_warning
:
2077 /* @@FIXME ignoring warning for now */
2079 case bfd_link_hash_new
:
2089 /* Only get the error once. */
2091 _bfd_set_gp_value (output_bfd
, *pgp
);
2095 _bfd_set_gp_value (output_bfd
, *pgp
);
2100 /* Retrieve the previously cached _gp pointer, returning bfd_reloc_dangerous
2101 if it's not available as we don't have a link_info pointer available here
2102 to look it up in the output symbol table. We don't need to adjust the
2103 symbol value for an external symbol if we are producing relocatable
2105 static bfd_reloc_status_type
2106 nios2_elf_final_gp (bfd
*output_bfd
, asymbol
*symbol
, bfd_boolean relocatable
,
2107 char **error_message
, bfd_vma
*pgp
)
2109 if (bfd_is_und_section (symbol
->section
) && !relocatable
)
2112 return bfd_reloc_undefined
;
2115 *pgp
= _bfd_get_gp_value (output_bfd
);
2116 if (*pgp
== 0 && (!relocatable
|| (symbol
->flags
& BSF_SECTION_SYM
) != 0))
2120 /* Make up a value. */
2121 *pgp
= symbol
->section
->output_section
->vma
+ 0x4000;
2122 _bfd_set_gp_value (output_bfd
, *pgp
);
2127 = (char *) _("global pointer relative relocation when _gp not defined");
2128 return bfd_reloc_dangerous
;
2132 return bfd_reloc_ok
;
2135 /* Do the relocations that require special handling. */
2136 static bfd_reloc_status_type
2137 nios2_elf32_do_hi16_relocate (bfd
*abfd
, reloc_howto_type
*howto
,
2138 asection
*input_section
,
2139 bfd_byte
*data
, bfd_vma offset
,
2140 bfd_vma symbol_value
, bfd_vma addend
)
2142 symbol_value
= symbol_value
+ addend
;
2144 symbol_value
= (symbol_value
>> 16) & 0xffff;
2145 return _bfd_final_link_relocate (howto
, abfd
, input_section
,
2146 data
, offset
, symbol_value
, addend
);
2149 static bfd_reloc_status_type
2150 nios2_elf32_do_lo16_relocate (bfd
*abfd
, reloc_howto_type
*howto
,
2151 asection
*input_section
,
2152 bfd_byte
*data
, bfd_vma offset
,
2153 bfd_vma symbol_value
, bfd_vma addend
)
2155 symbol_value
= symbol_value
+ addend
;
2157 symbol_value
= symbol_value
& 0xffff;
2158 return _bfd_final_link_relocate (howto
, abfd
, input_section
,
2159 data
, offset
, symbol_value
, addend
);
2162 static bfd_reloc_status_type
2163 nios2_elf32_do_hiadj16_relocate (bfd
*abfd
, reloc_howto_type
*howto
,
2164 asection
*input_section
,
2165 bfd_byte
*data
, bfd_vma offset
,
2166 bfd_vma symbol_value
, bfd_vma addend
)
2168 symbol_value
= symbol_value
+ addend
;
2170 symbol_value
= hiadj(symbol_value
);
2171 return _bfd_final_link_relocate (howto
, abfd
, input_section
, data
, offset
,
2172 symbol_value
, addend
);
2175 static bfd_reloc_status_type
2176 nios2_elf32_do_pcrel_lo16_relocate (bfd
*abfd
, reloc_howto_type
*howto
,
2177 asection
*input_section
,
2178 bfd_byte
*data
, bfd_vma offset
,
2179 bfd_vma symbol_value
, bfd_vma addend
)
2181 symbol_value
= symbol_value
+ addend
;
2183 symbol_value
= symbol_value
& 0xffff;
2184 return _bfd_final_link_relocate (howto
, abfd
, input_section
,
2185 data
, offset
, symbol_value
, addend
);
2188 static bfd_reloc_status_type
2189 nios2_elf32_do_pcrel_hiadj16_relocate (bfd
*abfd
, reloc_howto_type
*howto
,
2190 asection
*input_section
,
2191 bfd_byte
*data
, bfd_vma offset
,
2192 bfd_vma symbol_value
, bfd_vma addend
)
2194 symbol_value
= symbol_value
+ addend
;
2195 symbol_value
-= (input_section
->output_section
->vma
2196 + input_section
->output_offset
);
2197 symbol_value
-= offset
;
2199 symbol_value
= hiadj(symbol_value
);
2200 return _bfd_final_link_relocate (howto
, abfd
, input_section
, data
, offset
,
2201 symbol_value
, addend
);
2204 static bfd_reloc_status_type
2205 nios2_elf32_do_pcrel16_relocate (bfd
*abfd
, reloc_howto_type
*howto
,
2206 asection
*input_section
,
2207 bfd_byte
*data
, bfd_vma offset
,
2208 bfd_vma symbol_value
, bfd_vma addend
)
2210 /* NIOS2 pc relative relocations are relative to the next 32-bit instruction
2211 so we need to subtract 4 before doing a final_link_relocate. */
2212 symbol_value
= symbol_value
+ addend
- 4;
2214 return _bfd_final_link_relocate (howto
, abfd
, input_section
,
2215 data
, offset
, symbol_value
, addend
);
2218 static bfd_reloc_status_type
2219 nios2_elf32_do_call26_relocate (bfd
*abfd
, reloc_howto_type
*howto
,
2220 asection
*input_section
,
2221 bfd_byte
*data
, bfd_vma offset
,
2222 bfd_vma symbol_value
, bfd_vma addend
)
2224 /* Check that the relocation is in the same page as the current address. */
2225 if (CALL26_SEGMENT (symbol_value
+ addend
)
2226 != CALL26_SEGMENT (input_section
->output_section
->vma
2227 + input_section
->output_offset
2229 return bfd_reloc_overflow
;
2231 return _bfd_final_link_relocate (howto
, abfd
, input_section
,
2232 data
, offset
, symbol_value
, addend
);
2235 static bfd_reloc_status_type
2236 nios2_elf32_do_gprel_relocate (bfd
*abfd
, reloc_howto_type
*howto
,
2237 asection
*input_section
,
2238 bfd_byte
*data
, bfd_vma offset
,
2239 bfd_vma symbol_value
, bfd_vma addend
)
2241 /* Because we need the output_bfd, the special handling is done
2242 in nios2_elf32_relocate_section or in nios2_elf32_gprel_relocate. */
2243 return _bfd_final_link_relocate (howto
, abfd
, input_section
,
2244 data
, offset
, symbol_value
, addend
);
2247 static bfd_reloc_status_type
2248 nios2_elf32_do_ujmp_relocate (bfd
*abfd
, reloc_howto_type
*howto
,
2249 asection
*input_section
,
2250 bfd_byte
*data
, bfd_vma offset
,
2251 bfd_vma symbol_value
, bfd_vma addend
)
2253 bfd_vma symbol_lo16
, symbol_hi16
;
2254 bfd_reloc_status_type r
;
2255 symbol_value
= symbol_value
+ addend
;
2257 symbol_hi16
= (symbol_value
>> 16) & 0xffff;
2258 symbol_lo16
= symbol_value
& 0xffff;
2260 r
= _bfd_final_link_relocate (howto
, abfd
, input_section
,
2261 data
, offset
, symbol_hi16
, addend
);
2263 if (r
== bfd_reloc_ok
)
2264 return _bfd_final_link_relocate (howto
, abfd
, input_section
,
2265 data
, offset
+ 4, symbol_lo16
, addend
);
2270 static bfd_reloc_status_type
2271 nios2_elf32_do_cjmp_relocate (bfd
*abfd
, reloc_howto_type
*howto
,
2272 asection
*input_section
,
2273 bfd_byte
*data
, bfd_vma offset
,
2274 bfd_vma symbol_value
, bfd_vma addend
)
2276 bfd_vma symbol_lo16
, symbol_hi16
;
2277 bfd_reloc_status_type r
;
2278 symbol_value
= symbol_value
+ addend
;
2280 symbol_hi16
= (symbol_value
>> 16) & 0xffff;
2281 symbol_lo16
= symbol_value
& 0xffff;
2283 r
= _bfd_final_link_relocate (howto
, abfd
, input_section
,
2284 data
, offset
, symbol_hi16
, addend
);
2286 if (r
== bfd_reloc_ok
)
2287 return _bfd_final_link_relocate (howto
, abfd
, input_section
,
2288 data
, offset
+ 4, symbol_lo16
, addend
);
2293 static bfd_reloc_status_type
2294 nios2_elf32_do_callr_relocate (bfd
*abfd
, reloc_howto_type
*howto
,
2295 asection
*input_section
,
2296 bfd_byte
*data
, bfd_vma offset
,
2297 bfd_vma symbol_value
, bfd_vma addend
)
2299 bfd_vma symbol_lo16
, symbol_hi16
;
2300 bfd_reloc_status_type r
;
2301 symbol_value
= symbol_value
+ addend
;
2303 symbol_hi16
= (symbol_value
>> 16) & 0xffff;
2304 symbol_lo16
= symbol_value
& 0xffff;
2306 r
= _bfd_final_link_relocate (howto
, abfd
, input_section
,
2307 data
, offset
, symbol_hi16
, addend
);
2309 if (r
== bfd_reloc_ok
)
2310 return _bfd_final_link_relocate (howto
, abfd
, input_section
,
2311 data
, offset
+ 4, symbol_lo16
, addend
);
2316 /* HOWTO handlers for relocations that require special handling. */
2318 /* This is for relocations used only when relaxing to ensure
2319 changes in size of section don't screw up .align. */
2320 static bfd_reloc_status_type
2321 nios2_elf32_ignore_reloc (bfd
*abfd ATTRIBUTE_UNUSED
, arelent
*reloc_entry
,
2322 asymbol
*symbol ATTRIBUTE_UNUSED
,
2323 void *data ATTRIBUTE_UNUSED
, asection
*input_section
,
2325 char **error_message ATTRIBUTE_UNUSED
)
2327 if (output_bfd
!= NULL
)
2328 reloc_entry
->address
+= input_section
->output_offset
;
2329 return bfd_reloc_ok
;
2332 static bfd_reloc_status_type
2333 nios2_elf32_hi16_relocate (bfd
*abfd
, arelent
*reloc_entry
, asymbol
*symbol
,
2334 void *data
, asection
*input_section
,
2336 char **error_message ATTRIBUTE_UNUSED
)
2338 /* This part is from bfd_elf_generic_reloc. */
2339 if (output_bfd
!= NULL
2340 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
2341 && (!reloc_entry
->howto
->partial_inplace
|| reloc_entry
->addend
== 0))
2343 reloc_entry
->address
+= input_section
->output_offset
;
2344 return bfd_reloc_ok
;
2347 if (output_bfd
!= NULL
)
2348 /* FIXME: See bfd_perform_relocation. Is this right? */
2349 return bfd_reloc_continue
;
2351 return nios2_elf32_do_hi16_relocate (abfd
, reloc_entry
->howto
,
2353 data
, reloc_entry
->address
,
2355 + symbol
->section
->output_section
->vma
2356 + symbol
->section
->output_offset
),
2357 reloc_entry
->addend
);
2360 static bfd_reloc_status_type
2361 nios2_elf32_lo16_relocate (bfd
*abfd
, arelent
*reloc_entry
, asymbol
*symbol
,
2362 void *data
, asection
*input_section
,
2364 char **error_message ATTRIBUTE_UNUSED
)
2366 /* This part is from bfd_elf_generic_reloc. */
2367 if (output_bfd
!= NULL
2368 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
2369 && (!reloc_entry
->howto
->partial_inplace
|| reloc_entry
->addend
== 0))
2371 reloc_entry
->address
+= input_section
->output_offset
;
2372 return bfd_reloc_ok
;
2375 if (output_bfd
!= NULL
)
2376 /* FIXME: See bfd_perform_relocation. Is this right? */
2377 return bfd_reloc_continue
;
2379 return nios2_elf32_do_lo16_relocate (abfd
, reloc_entry
->howto
,
2381 data
, reloc_entry
->address
,
2383 + symbol
->section
->output_section
->vma
2384 + symbol
->section
->output_offset
),
2385 reloc_entry
->addend
);
2388 static bfd_reloc_status_type
2389 nios2_elf32_hiadj16_relocate (bfd
*abfd
, arelent
*reloc_entry
, asymbol
*symbol
,
2390 void *data
, asection
*input_section
,
2392 char **error_message ATTRIBUTE_UNUSED
)
2394 /* This part is from bfd_elf_generic_reloc. */
2395 if (output_bfd
!= NULL
2396 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
2397 && (!reloc_entry
->howto
->partial_inplace
|| reloc_entry
->addend
== 0))
2399 reloc_entry
->address
+= input_section
->output_offset
;
2400 return bfd_reloc_ok
;
2403 if (output_bfd
!= NULL
)
2404 /* FIXME: See bfd_perform_relocation. Is this right? */
2405 return bfd_reloc_continue
;
2407 return nios2_elf32_do_hiadj16_relocate (abfd
, reloc_entry
->howto
,
2409 data
, reloc_entry
->address
,
2411 + symbol
->section
->output_section
->vma
2412 + symbol
->section
->output_offset
),
2413 reloc_entry
->addend
);
2416 static bfd_reloc_status_type
2417 nios2_elf32_pcrel_lo16_relocate (bfd
*abfd
, arelent
*reloc_entry
,
2418 asymbol
*symbol
, void *data
,
2419 asection
*input_section
, bfd
*output_bfd
,
2420 char **error_message ATTRIBUTE_UNUSED
)
2422 /* This part is from bfd_elf_generic_reloc. */
2423 if (output_bfd
!= NULL
2424 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
2425 && (!reloc_entry
->howto
->partial_inplace
|| reloc_entry
->addend
== 0))
2427 reloc_entry
->address
+= input_section
->output_offset
;
2428 return bfd_reloc_ok
;
2431 if (output_bfd
!= NULL
)
2432 /* FIXME: See bfd_perform_relocation. Is this right? */
2433 return bfd_reloc_continue
;
2435 return nios2_elf32_do_pcrel_lo16_relocate (
2436 abfd
, reloc_entry
->howto
, input_section
, data
, reloc_entry
->address
,
2437 (symbol
->value
+ symbol
->section
->output_section
->vma
2438 + symbol
->section
->output_offset
),
2439 reloc_entry
->addend
);
2442 static bfd_reloc_status_type
2443 nios2_elf32_pcrel_hiadj16_relocate (bfd
*abfd
, arelent
*reloc_entry
,
2444 asymbol
*symbol
, void *data
,
2445 asection
*input_section
, bfd
*output_bfd
,
2446 char **error_message ATTRIBUTE_UNUSED
)
2448 /* This part is from bfd_elf_generic_reloc. */
2449 if (output_bfd
!= NULL
2450 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
2451 && (!reloc_entry
->howto
->partial_inplace
|| reloc_entry
->addend
== 0))
2453 reloc_entry
->address
+= input_section
->output_offset
;
2454 return bfd_reloc_ok
;
2457 if (output_bfd
!= NULL
)
2458 /* FIXME: See bfd_perform_relocation. Is this right? */
2459 return bfd_reloc_continue
;
2461 return nios2_elf32_do_pcrel_hiadj16_relocate (
2462 abfd
, reloc_entry
->howto
, input_section
, data
, reloc_entry
->address
,
2463 (symbol
->value
+ symbol
->section
->output_section
->vma
2464 + symbol
->section
->output_offset
),
2465 reloc_entry
->addend
);
2468 static bfd_reloc_status_type
2469 nios2_elf32_pcrel16_relocate (bfd
*abfd
, arelent
*reloc_entry
, asymbol
*symbol
,
2470 void *data
, asection
*input_section
,
2472 char **error_message ATTRIBUTE_UNUSED
)
2474 /* This part is from bfd_elf_generic_reloc. */
2475 if (output_bfd
!= NULL
2476 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
2477 && (!reloc_entry
->howto
->partial_inplace
|| reloc_entry
->addend
== 0))
2479 reloc_entry
->address
+= input_section
->output_offset
;
2480 return bfd_reloc_ok
;
2483 if (output_bfd
!= NULL
)
2484 /* FIXME: See bfd_perform_relocation. Is this right? */
2485 return bfd_reloc_continue
;
2487 return nios2_elf32_do_pcrel16_relocate (abfd
, reloc_entry
->howto
,
2489 data
, reloc_entry
->address
,
2491 + symbol
->section
->output_section
->vma
2492 + symbol
->section
->output_offset
),
2493 reloc_entry
->addend
);
2496 static bfd_reloc_status_type
2497 nios2_elf32_call26_relocate (bfd
*abfd
, arelent
*reloc_entry
, asymbol
*symbol
,
2498 void *data
, asection
*input_section
,
2500 char **error_message ATTRIBUTE_UNUSED
)
2502 /* This part is from bfd_elf_generic_reloc. */
2503 if (output_bfd
!= NULL
2504 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
2505 && (!reloc_entry
->howto
->partial_inplace
|| reloc_entry
->addend
== 0))
2507 reloc_entry
->address
+= input_section
->output_offset
;
2508 return bfd_reloc_ok
;
2511 if (output_bfd
!= NULL
)
2512 /* FIXME: See bfd_perform_relocation. Is this right? */
2513 return bfd_reloc_continue
;
2515 return nios2_elf32_do_call26_relocate (abfd
, reloc_entry
->howto
,
2517 data
, reloc_entry
->address
,
2519 + symbol
->section
->output_section
->vma
2520 + symbol
->section
->output_offset
),
2521 reloc_entry
->addend
);
2524 static bfd_reloc_status_type
2525 nios2_elf32_gprel_relocate (bfd
*abfd
, arelent
*reloc_entry
, asymbol
*symbol
,
2526 void *data
, asection
*input_section
,
2527 bfd
*output_bfd
, char **msg
)
2531 bfd_reloc_status_type r
;
2534 /* This part is from bfd_elf_generic_reloc. */
2535 if (output_bfd
!= NULL
2536 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
2537 && (!reloc_entry
->howto
->partial_inplace
|| reloc_entry
->addend
== 0))
2539 reloc_entry
->address
+= input_section
->output_offset
;
2540 return bfd_reloc_ok
;
2543 if (output_bfd
!= NULL
)
2544 /* FIXME: See bfd_perform_relocation. Is this right? */
2545 return bfd_reloc_continue
;
2547 relocation
= (symbol
->value
2548 + symbol
->section
->output_section
->vma
2549 + symbol
->section
->output_offset
);
2551 /* This assumes we've already cached the _gp symbol. */
2552 r
= nios2_elf_final_gp (abfd
, symbol
, FALSE
, msg
, &gp
);
2553 if (r
== bfd_reloc_ok
)
2555 relocation
= relocation
+ reloc_entry
->addend
- gp
;
2556 reloc_entry
->addend
= 0;
2557 if ((signed) relocation
< -32768 || (signed) relocation
> 32767)
2559 *msg
= _("global pointer relative address out of range");
2560 r
= bfd_reloc_outofrange
;
2563 r
= nios2_elf32_do_gprel_relocate (abfd
, reloc_entry
->howto
,
2565 data
, reloc_entry
->address
,
2566 relocation
, reloc_entry
->addend
);
2572 static bfd_reloc_status_type
2573 nios2_elf32_ujmp_relocate (bfd
*abfd
, arelent
*reloc_entry
, asymbol
*symbol
,
2574 void *data
, asection
*input_section
,
2575 bfd
*output_bfd
, char **msg ATTRIBUTE_UNUSED
)
2577 /* This part is from bfd_elf_generic_reloc. */
2578 if (output_bfd
!= NULL
2579 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
2580 && (!reloc_entry
->howto
->partial_inplace
|| reloc_entry
->addend
== 0))
2582 reloc_entry
->address
+= input_section
->output_offset
;
2583 return bfd_reloc_ok
;
2586 if (output_bfd
!= NULL
)
2587 /* FIXME: See bfd_perform_relocation. Is this right? */
2588 return bfd_reloc_continue
;
2590 return nios2_elf32_do_ujmp_relocate (abfd
, reloc_entry
->howto
,
2592 data
, reloc_entry
->address
,
2594 + symbol
->section
->output_section
->vma
2595 + symbol
->section
->output_offset
),
2596 reloc_entry
->addend
);
2599 static bfd_reloc_status_type
2600 nios2_elf32_cjmp_relocate (bfd
*abfd
, arelent
*reloc_entry
, asymbol
*symbol
,
2601 void *data
, asection
*input_section
,
2602 bfd
*output_bfd
, char **msg ATTRIBUTE_UNUSED
)
2604 /* This part is from bfd_elf_generic_reloc. */
2605 if (output_bfd
!= NULL
2606 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
2607 && (!reloc_entry
->howto
->partial_inplace
|| reloc_entry
->addend
== 0))
2609 reloc_entry
->address
+= input_section
->output_offset
;
2610 return bfd_reloc_ok
;
2613 if (output_bfd
!= NULL
)
2614 /* FIXME: See bfd_perform_relocation. Is this right? */
2615 return bfd_reloc_continue
;
2617 return nios2_elf32_do_cjmp_relocate (abfd
, reloc_entry
->howto
,
2619 data
, reloc_entry
->address
,
2621 + symbol
->section
->output_section
->vma
2622 + symbol
->section
->output_offset
),
2623 reloc_entry
->addend
);
2626 static bfd_reloc_status_type
2627 nios2_elf32_callr_relocate (bfd
*abfd
, arelent
*reloc_entry
, asymbol
*symbol
,
2628 void *data
, asection
*input_section
,
2629 bfd
*output_bfd
, char **msg ATTRIBUTE_UNUSED
)
2631 /* This part is from bfd_elf_generic_reloc. */
2632 if (output_bfd
!= NULL
2633 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
2634 && (!reloc_entry
->howto
->partial_inplace
|| reloc_entry
->addend
== 0))
2636 reloc_entry
->address
+= input_section
->output_offset
;
2637 return bfd_reloc_ok
;
2640 if (output_bfd
!= NULL
)
2641 /* FIXME: See bfd_perform_relocation. Is this right? */
2642 return bfd_reloc_continue
;
2644 return nios2_elf32_do_callr_relocate (abfd
, reloc_entry
->howto
,
2646 data
, reloc_entry
->address
,
2648 + symbol
->section
->output_section
->vma
2649 + symbol
->section
->output_offset
),
2650 reloc_entry
->addend
);
2654 /* Implement elf_backend_relocate_section. */
2656 nios2_elf32_relocate_section (bfd
*output_bfd
,
2657 struct bfd_link_info
*info
,
2659 asection
*input_section
,
2661 Elf_Internal_Rela
*relocs
,
2662 Elf_Internal_Sym
*local_syms
,
2663 asection
**local_sections
)
2665 Elf_Internal_Shdr
*symtab_hdr
;
2666 struct elf_link_hash_entry
**sym_hashes
;
2667 Elf_Internal_Rela
*rel
;
2668 Elf_Internal_Rela
*relend
;
2669 struct elf32_nios2_link_hash_table
*htab
;
2672 asection
*sreloc
= NULL
;
2673 bfd_vma
*local_got_offsets
;
2675 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
2676 sym_hashes
= elf_sym_hashes (input_bfd
);
2677 relend
= relocs
+ input_section
->reloc_count
;
2679 htab
= elf32_nios2_hash_table (info
);
2680 sgot
= htab
->root
.sgot
;
2681 splt
= htab
->root
.splt
;
2682 local_got_offsets
= elf_local_got_offsets (input_bfd
);
2684 for (rel
= relocs
; rel
< relend
; rel
++)
2686 reloc_howto_type
*howto
;
2687 unsigned long r_symndx
;
2688 Elf_Internal_Sym
*sym
;
2690 struct elf_link_hash_entry
*h
;
2691 struct elf32_nios2_link_hash_entry
*eh
;
2694 bfd_vma reloc_address
;
2695 bfd_reloc_status_type r
= bfd_reloc_ok
;
2696 const char *name
= NULL
;
2700 const char* msg
= (const char*) NULL
;
2701 bfd_boolean unresolved_reloc
;
2705 r_type
= ELF32_R_TYPE (rel
->r_info
);
2706 r_symndx
= ELF32_R_SYM (rel
->r_info
);
2708 howto
= lookup_howto ((unsigned) ELF32_R_TYPE (rel
->r_info
));
2713 if (r_symndx
< symtab_hdr
->sh_info
)
2715 sym
= local_syms
+ r_symndx
;
2716 sec
= local_sections
[r_symndx
];
2717 relocation
= _bfd_elf_rela_local_sym (output_bfd
, sym
, &sec
, rel
);
2721 bfd_boolean warned
, ignored
;
2723 RELOC_FOR_GLOBAL_SYMBOL (info
, input_bfd
, input_section
, rel
,
2724 r_symndx
, symtab_hdr
, sym_hashes
,
2726 unresolved_reloc
, warned
, ignored
);
2729 if (sec
&& discarded_section (sec
))
2730 RELOC_AGAINST_DISCARDED_SECTION (info
, input_bfd
, input_section
,
2731 rel
, 1, relend
, howto
, 0, contents
);
2733 /* Nothing more to do unless this is a final link. */
2734 if (info
->relocatable
)
2737 if (sec
&& sec
->output_section
)
2738 reloc_address
= (sec
->output_section
->vma
+ sec
->output_offset
2745 switch (howto
->type
)
2748 r
= nios2_elf32_do_hi16_relocate (input_bfd
, howto
,
2750 contents
, rel
->r_offset
,
2751 relocation
, rel
->r_addend
);
2754 r
= nios2_elf32_do_lo16_relocate (input_bfd
, howto
,
2756 contents
, rel
->r_offset
,
2757 relocation
, rel
->r_addend
);
2759 case R_NIOS2_PCREL_LO
:
2760 r
= nios2_elf32_do_pcrel_lo16_relocate (input_bfd
, howto
,
2767 case R_NIOS2_HIADJ16
:
2768 r
= nios2_elf32_do_hiadj16_relocate (input_bfd
, howto
,
2769 input_section
, contents
,
2770 rel
->r_offset
, relocation
,
2773 case R_NIOS2_PCREL_HA
:
2774 r
= nios2_elf32_do_pcrel_hiadj16_relocate (input_bfd
, howto
,
2781 case R_NIOS2_PCREL16
:
2782 r
= nios2_elf32_do_pcrel16_relocate (input_bfd
, howto
,
2783 input_section
, contents
,
2784 rel
->r_offset
, relocation
,
2788 /* Turns an absolute address into a gp-relative address. */
2789 if (!nios2_elf_assign_gp (output_bfd
, &gp
, info
))
2791 format
= _("global pointer relative relocation at address "
2792 "0x%08x when _gp not defined\n");
2793 sprintf (msgbuf
, format
, reloc_address
);
2795 r
= bfd_reloc_dangerous
;
2799 bfd_vma symbol_address
= rel
->r_addend
+ relocation
;
2800 relocation
= relocation
+ rel
->r_addend
- gp
;
2802 if (((signed) relocation
< -32768
2803 || (signed) relocation
> 32767)
2805 || h
->root
.type
== bfd_link_hash_defined
2806 || h
->root
.type
== bfd_link_hash_defweak
))
2808 format
= _("Unable to reach %s (at 0x%08x) from the "
2809 "global pointer (at 0x%08x) because the "
2810 "offset (%d) is out of the allowed range, "
2811 "-32678 to 32767.\n" );
2812 sprintf (msgbuf
, format
, name
, symbol_address
, gp
,
2813 (signed)relocation
);
2815 r
= bfd_reloc_outofrange
;
2818 r
= _bfd_final_link_relocate (howto
, input_bfd
,
2819 input_section
, contents
,
2820 rel
->r_offset
, relocation
,
2826 r
= nios2_elf32_do_ujmp_relocate (input_bfd
, howto
,
2828 contents
, rel
->r_offset
,
2829 relocation
, rel
->r_addend
);
2832 r
= nios2_elf32_do_cjmp_relocate (input_bfd
, howto
,
2834 contents
, rel
->r_offset
,
2835 relocation
, rel
->r_addend
);
2838 r
= nios2_elf32_do_callr_relocate (input_bfd
, howto
,
2839 input_section
, contents
,
2840 rel
->r_offset
, relocation
,
2843 case R_NIOS2_CALL26
:
2844 case R_NIOS2_CALL26_NOAT
:
2845 /* If we have a call to an undefined weak symbol, we just want
2846 to stuff a zero in the bits of the call instruction and
2847 bypass the normal call26 relocation handling, because it'll
2848 diagnose an overflow error if address 0 isn't in the same
2849 256MB segment as the call site. Presumably the call
2850 should be guarded by a null check anyway. */
2851 if (h
!= NULL
&& h
->root
.type
== bfd_link_hash_undefweak
)
2853 BFD_ASSERT (relocation
== 0 && rel
->r_addend
== 0);
2854 r
= _bfd_final_link_relocate (howto
, input_bfd
,
2855 input_section
, contents
,
2856 rel
->r_offset
, relocation
,
2860 /* Handle relocations which should use the PLT entry.
2861 NIOS2_BFD_RELOC_32 relocations will use the symbol's value,
2862 which may point to a PLT entry, but we don't need to handle
2863 that here. If we created a PLT entry, all branches in this
2864 object should go to it. */
2865 if (h
!= NULL
&& splt
!= NULL
&& h
->plt
.offset
!= (bfd_vma
) -1)
2867 /* If we've created a .plt section, and assigned a PLT entry
2868 to this function, it should not be known to bind locally.
2869 If it were, we would have cleared the PLT entry. */
2870 BFD_ASSERT (!SYMBOL_CALLS_LOCAL (info
, h
));
2872 relocation
= (splt
->output_section
->vma
2873 + splt
->output_offset
2876 unresolved_reloc
= FALSE
;
2878 /* Detect R_NIOS2_CALL26 relocations that would overflow the
2879 256MB segment. Replace the target with a reference to a
2881 Note that htab->stub_group is null if relaxation has been
2882 disabled by the --no-relax linker command-line option, so
2883 we can use that to skip this processing entirely. */
2884 if (howto
->type
== R_NIOS2_CALL26
&& htab
->stub_group
)
2886 bfd_vma dest
= relocation
+ rel
->r_addend
;
2887 enum elf32_nios2_stub_type stub_type
;
2889 eh
= (struct elf32_nios2_link_hash_entry
*)h
;
2890 stub_type
= nios2_type_of_stub (input_section
, rel
, eh
,
2893 if (stub_type
!= nios2_stub_none
)
2895 struct elf32_nios2_stub_hash_entry
*hsh
;
2897 hsh
= nios2_get_stub_entry (input_section
, sec
,
2898 eh
, rel
, htab
, stub_type
);
2901 r
= bfd_reloc_undefined
;
2905 dest
= (hsh
->stub_offset
2906 + hsh
->stub_sec
->output_offset
2907 + hsh
->stub_sec
->output_section
->vma
);
2908 r
= nios2_elf32_do_call26_relocate (input_bfd
, howto
,
2918 r
= nios2_elf32_do_call26_relocate (input_bfd
, howto
,
2919 input_section
, contents
,
2920 rel
->r_offset
, relocation
,
2925 /* For symmetry this would be
2926 r = nios2_elf32_do_ignore_reloc (input_bfd, howto,
2927 input_section, contents,
2928 rel->r_offset, relocation,
2930 but do_ignore_reloc would do no more than return
2935 case R_NIOS2_CALL16
:
2936 /* Relocation is to the entry for this symbol in the
2937 global offset table. */
2940 r
= bfd_reloc_notsupported
;
2950 eh
= (struct elf32_nios2_link_hash_entry
*)h
;
2951 use_plt
= (eh
->got_types_used
== CALL16_USED
2952 && h
->plt
.offset
!= (bfd_vma
) -1);
2954 off
= h
->got
.offset
;
2955 BFD_ASSERT (off
!= (bfd_vma
) -1);
2956 dyn
= elf_hash_table (info
)->dynamic_sections_created
;
2957 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, info
->shared
, h
)
2959 && SYMBOL_REFERENCES_LOCAL (info
, h
))
2960 || (ELF_ST_VISIBILITY (h
->other
)
2961 && h
->root
.type
== bfd_link_hash_undefweak
))
2963 /* This is actually a static link, or it is a -Bsymbolic
2964 link and the symbol is defined locally. We must
2965 initialize this entry in the global offset table.
2966 Since the offset must always be a multiple of 4, we
2967 use the least significant bit to record whether we
2968 have initialized it already.
2970 When doing a dynamic link, we create a .rela.got
2971 relocation entry to initialize the value. This is
2972 done in the finish_dynamic_symbol routine. */
2977 bfd_put_32 (output_bfd
, relocation
,
2978 sgot
->contents
+ off
);
2983 unresolved_reloc
= FALSE
;
2987 BFD_ASSERT (local_got_offsets
!= NULL
2988 && local_got_offsets
[r_symndx
] != (bfd_vma
) -1);
2990 off
= local_got_offsets
[r_symndx
];
2992 /* The offset must always be a multiple of 4. We use the
2993 least significant bit to record whether we have already
2994 generated the necessary reloc. */
2999 bfd_put_32 (output_bfd
, relocation
,
3000 sgot
->contents
+ off
);
3005 Elf_Internal_Rela outrel
;
3008 srelgot
= htab
->root
.srelgot
;
3009 BFD_ASSERT (srelgot
!= NULL
);
3011 outrel
.r_addend
= relocation
;
3012 outrel
.r_offset
= (sgot
->output_section
->vma
3013 + sgot
->output_offset
3015 outrel
.r_info
= ELF32_R_INFO (0, R_NIOS2_RELATIVE
);
3016 loc
= srelgot
->contents
;
3017 loc
+= (srelgot
->reloc_count
++ *
3018 sizeof (Elf32_External_Rela
));
3019 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
, loc
);
3022 local_got_offsets
[r_symndx
] |= 1;
3026 if (use_plt
&& info
->shared
)
3028 off
= ((h
->plt
.offset
- 24) / 12 + 3) * 4;
3029 relocation
= htab
->root
.sgotplt
->output_offset
+ off
;
3032 relocation
= sgot
->output_offset
+ off
;
3034 /* This relocation does not use the addend. */
3037 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
3038 contents
, rel
->r_offset
,
3039 relocation
, rel
->r_addend
);
3042 case R_NIOS2_GOTOFF_LO
:
3043 case R_NIOS2_GOTOFF_HA
:
3044 case R_NIOS2_GOTOFF
:
3045 /* Relocation is relative to the start of the
3046 global offset table. */
3048 BFD_ASSERT (sgot
!= NULL
);
3051 r
= bfd_reloc_notsupported
;
3055 /* Note that sgot->output_offset is not involved in this
3056 calculation. We always want the start of .got. If we
3057 define _GLOBAL_OFFSET_TABLE in a different way, as is
3058 permitted by the ABI, we might have to change this
3060 relocation
-= sgot
->output_section
->vma
;
3061 switch (howto
->type
)
3063 case R_NIOS2_GOTOFF_LO
:
3064 r
= nios2_elf32_do_lo16_relocate (input_bfd
, howto
,
3065 input_section
, contents
,
3066 rel
->r_offset
, relocation
,
3069 case R_NIOS2_GOTOFF_HA
:
3070 r
= nios2_elf32_do_hiadj16_relocate (input_bfd
, howto
,
3071 input_section
, contents
,
3077 r
= _bfd_final_link_relocate (howto
, input_bfd
,
3078 input_section
, contents
,
3079 rel
->r_offset
, relocation
,
3085 case R_NIOS2_TLS_LDO16
:
3086 relocation
-= dtpoff_base (info
) + DTP_OFFSET
;
3088 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
3089 contents
, rel
->r_offset
,
3090 relocation
, rel
->r_addend
);
3092 case R_NIOS2_TLS_LDM16
:
3093 if (htab
->root
.sgot
== NULL
)
3096 off
= htab
->tls_ldm_got
.offset
;
3102 /* If we don't know the module number, create a relocation
3106 Elf_Internal_Rela outrel
;
3109 if (htab
->root
.srelgot
== NULL
)
3112 outrel
.r_addend
= 0;
3113 outrel
.r_offset
= (htab
->root
.sgot
->output_section
->vma
3114 + htab
->root
.sgot
->output_offset
3116 outrel
.r_info
= ELF32_R_INFO (0, R_NIOS2_TLS_DTPMOD
);
3118 loc
= htab
->root
.srelgot
->contents
;
3119 loc
+= (htab
->root
.srelgot
->reloc_count
++
3120 * sizeof (Elf32_External_Rela
));
3121 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
, loc
);
3124 bfd_put_32 (output_bfd
, 1,
3125 htab
->root
.sgot
->contents
+ off
);
3127 htab
->tls_ldm_got
.offset
|= 1;
3130 relocation
= (htab
->root
.sgot
->output_offset
+ off
);
3132 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
3133 contents
, rel
->r_offset
,
3134 relocation
, rel
->r_addend
);
3137 case R_NIOS2_TLS_GD16
:
3138 case R_NIOS2_TLS_IE16
:
3143 if (htab
->root
.sgot
== NULL
)
3150 dyn
= htab
->root
.dynamic_sections_created
;
3151 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, info
->shared
, h
)
3153 || !SYMBOL_REFERENCES_LOCAL (info
, h
)))
3155 unresolved_reloc
= FALSE
;
3158 off
= h
->got
.offset
;
3159 tls_type
= (((struct elf32_nios2_link_hash_entry
*) h
)
3164 if (local_got_offsets
== NULL
)
3166 off
= local_got_offsets
[r_symndx
];
3167 tls_type
= (elf32_nios2_local_got_tls_type (input_bfd
)
3171 if (tls_type
== GOT_UNKNOWN
)
3178 bfd_boolean need_relocs
= FALSE
;
3179 Elf_Internal_Rela outrel
;
3180 bfd_byte
*loc
= NULL
;
3183 /* The GOT entries have not been initialized yet. Do it
3184 now, and emit any relocations. If both an IE GOT and a
3185 GD GOT are necessary, we emit the GD first. */
3187 if ((info
->shared
|| indx
!= 0)
3189 || ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
3190 || h
->root
.type
!= bfd_link_hash_undefweak
))
3193 if (htab
->root
.srelgot
== NULL
)
3195 loc
= htab
->root
.srelgot
->contents
;
3196 loc
+= (htab
->root
.srelgot
->reloc_count
*
3197 sizeof (Elf32_External_Rela
));
3200 if (tls_type
& GOT_TLS_GD
)
3204 outrel
.r_addend
= 0;
3205 outrel
.r_offset
= (htab
->root
.sgot
->output_section
->vma
3206 + htab
->root
.sgot
->output_offset
3208 outrel
.r_info
= ELF32_R_INFO (indx
,
3209 R_NIOS2_TLS_DTPMOD
);
3211 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
,
3213 htab
->root
.srelgot
->reloc_count
++;
3214 loc
+= sizeof (Elf32_External_Rela
);
3217 bfd_put_32 (output_bfd
,
3218 (relocation
- dtpoff_base (info
) -
3220 htab
->root
.sgot
->contents
+ cur_off
+ 4);
3223 outrel
.r_addend
= 0;
3224 outrel
.r_info
= ELF32_R_INFO (indx
,
3225 R_NIOS2_TLS_DTPREL
);
3226 outrel
.r_offset
+= 4;
3228 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
,
3230 htab
->root
.srelgot
->reloc_count
++;
3231 loc
+= sizeof (Elf32_External_Rela
);
3236 /* If we are not emitting relocations for a
3237 general dynamic reference, then we must be in a
3238 static link or an executable link with the
3239 symbol binding locally. Mark it as belonging
3240 to module 1, the executable. */
3241 bfd_put_32 (output_bfd
, 1,
3242 htab
->root
.sgot
->contents
+ cur_off
);
3243 bfd_put_32 (output_bfd
, (relocation
-
3244 dtpoff_base (info
) -
3246 htab
->root
.sgot
->contents
+ cur_off
+ 4);
3252 if (tls_type
& GOT_TLS_IE
)
3257 outrel
.r_addend
= (relocation
-
3258 dtpoff_base (info
));
3260 outrel
.r_addend
= 0;
3261 outrel
.r_offset
= (htab
->root
.sgot
->output_section
->vma
3262 + htab
->root
.sgot
->output_offset
3264 outrel
.r_info
= ELF32_R_INFO (indx
,
3267 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
,
3269 htab
->root
.srelgot
->reloc_count
++;
3270 loc
+= sizeof (Elf32_External_Rela
);
3273 bfd_put_32 (output_bfd
, (tpoff (info
, relocation
)
3275 htab
->root
.sgot
->contents
+ cur_off
);
3282 local_got_offsets
[r_symndx
] |= 1;
3285 if ((tls_type
& GOT_TLS_GD
) && r_type
!= R_NIOS2_TLS_GD16
)
3287 relocation
= (htab
->root
.sgot
->output_offset
+ off
);
3289 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
3290 contents
, rel
->r_offset
,
3291 relocation
, rel
->r_addend
);
3295 case R_NIOS2_TLS_LE16
:
3296 if (info
->shared
&& !info
->pie
)
3298 (*_bfd_error_handler
)
3299 (_("%B(%A+0x%lx): R_NIOS2_TLS_LE16 relocation not "
3300 "permitted in shared object"),
3301 input_bfd
, input_section
,
3302 (long) rel
->r_offset
, howto
->name
);
3306 relocation
= tpoff (info
, relocation
) - TP_OFFSET
;
3308 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
3309 contents
, rel
->r_offset
,
3310 relocation
, rel
->r_addend
);
3313 case R_NIOS2_BFD_RELOC_32
:
3315 && (input_section
->flags
& SEC_ALLOC
) != 0
3317 || ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
3318 || h
->root
.type
!= bfd_link_hash_undefweak
))
3320 Elf_Internal_Rela outrel
;
3322 bfd_boolean skip
, relocate
;
3324 /* When generating a shared object, these relocations
3325 are copied into the output file to be resolved at run
3332 = _bfd_elf_section_offset (output_bfd
, info
,
3333 input_section
, rel
->r_offset
);
3334 if (outrel
.r_offset
== (bfd_vma
) -1)
3336 else if (outrel
.r_offset
== (bfd_vma
) -2)
3337 skip
= TRUE
, relocate
= TRUE
;
3338 outrel
.r_offset
+= (input_section
->output_section
->vma
3339 + input_section
->output_offset
);
3342 memset (&outrel
, 0, sizeof outrel
);
3347 || !h
->def_regular
))
3349 outrel
.r_info
= ELF32_R_INFO (h
->dynindx
, r_type
);
3350 outrel
.r_addend
= rel
->r_addend
;
3354 /* This symbol is local, or marked to become local. */
3355 outrel
.r_addend
= relocation
+ rel
->r_addend
;
3357 outrel
.r_info
= ELF32_R_INFO (0, R_NIOS2_RELATIVE
);
3360 sreloc
= elf_section_data (input_section
)->sreloc
;
3364 loc
= sreloc
->contents
;
3365 loc
+= sreloc
->reloc_count
++ * sizeof (Elf32_External_Rela
);
3366 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
, loc
);
3368 /* This reloc will be computed at runtime, so there's no
3369 need to do anything now, except for R_NIOS2_BFD_RELOC_32
3370 relocations that have been turned into
3371 R_NIOS2_RELATIVE. */
3376 r
= _bfd_final_link_relocate (howto
, input_bfd
,
3377 input_section
, contents
,
3378 rel
->r_offset
, relocation
,
3382 case R_NIOS2_TLS_DTPREL
:
3383 relocation
-= dtpoff_base (info
);
3387 r
= _bfd_final_link_relocate (howto
, input_bfd
,
3388 input_section
, contents
,
3389 rel
->r_offset
, relocation
,
3395 r
= bfd_reloc_notsupported
;
3397 if (r
!= bfd_reloc_ok
)
3400 name
= h
->root
.root
.string
;
3403 name
= bfd_elf_string_from_elf_section (input_bfd
,
3404 symtab_hdr
->sh_link
,
3406 if (name
== NULL
|| *name
== '\0')
3407 name
= bfd_section_name (input_bfd
, sec
);
3412 case bfd_reloc_overflow
:
3413 r
= info
->callbacks
->reloc_overflow (info
, NULL
, name
,
3414 howto
->name
, (bfd_vma
) 0,
3415 input_bfd
, input_section
,
3419 case bfd_reloc_undefined
:
3420 r
= info
->callbacks
->undefined_symbol (info
, name
, input_bfd
,
3422 rel
->r_offset
, TRUE
);
3425 case bfd_reloc_outofrange
:
3427 msg
= _("relocation out of range");
3430 case bfd_reloc_notsupported
:
3432 msg
= _("unsupported relocation");
3435 case bfd_reloc_dangerous
:
3437 msg
= _("dangerous relocation");
3442 msg
= _("unknown error");
3448 r
= info
->callbacks
->warning
3449 (info
, msg
, name
, input_bfd
, input_section
, rel
->r_offset
);
3457 /* Implement elf-backend_section_flags:
3458 Convert NIOS2 specific section flags to bfd internal section flags. */
3460 nios2_elf32_section_flags (flagword
*flags
, const Elf_Internal_Shdr
*hdr
)
3462 if (hdr
->sh_flags
& SHF_NIOS2_GPREL
)
3463 *flags
|= SEC_SMALL_DATA
;
3468 /* Implement elf_backend_fake_sections:
3469 Set the correct type for an NIOS2 ELF section. We do this by the
3470 section name, which is a hack, but ought to work. */
3472 nios2_elf32_fake_sections (bfd
*abfd ATTRIBUTE_UNUSED
,
3473 Elf_Internal_Shdr
*hdr
, asection
*sec
)
3475 register const char *name
= bfd_get_section_name (abfd
, sec
);
3477 if ((sec
->flags
& SEC_SMALL_DATA
)
3478 || strcmp (name
, ".sdata") == 0
3479 || strcmp (name
, ".sbss") == 0
3480 || strcmp (name
, ".lit4") == 0 || strcmp (name
, ".lit8") == 0)
3481 hdr
->sh_flags
|= SHF_NIOS2_GPREL
;
3486 /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
3487 shortcuts to them in our hash table. */
3489 create_got_section (bfd
*dynobj
, struct bfd_link_info
*info
)
3491 struct elf32_nios2_link_hash_table
*htab
;
3493 htab
= elf32_nios2_hash_table (info
);
3495 if (! _bfd_elf_create_got_section (dynobj
, info
))
3498 /* In order for the two loads in .PLTresolve to share the same %hiadj,
3499 _GLOBAL_OFFSET_TABLE_ must be aligned to a 16-byte boundary. */
3500 if (!bfd_set_section_alignment (dynobj
, htab
->root
.sgotplt
, 4))
3506 /* Implement elf_backend_create_dynamic_sections:
3507 Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
3508 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
3511 nios2_elf32_create_dynamic_sections (bfd
*dynobj
, struct bfd_link_info
*info
)
3513 struct elf32_nios2_link_hash_table
*htab
;
3515 htab
= elf32_nios2_hash_table (info
);
3516 if (!htab
->root
.sgot
&& !create_got_section (dynobj
, info
))
3519 _bfd_elf_create_dynamic_sections (dynobj
, info
);
3521 /* In order for the two loads in a shared object .PLTresolve to share the
3522 same %hiadj, the start of the PLT (as well as the GOT) must be aligned
3523 to a 16-byte boundary. This is because the addresses for these loads
3524 include the -(.plt+4) PIC correction. */
3525 if (!bfd_set_section_alignment (dynobj
, htab
->root
.splt
, 4))
3528 htab
->sdynbss
= bfd_get_linker_section (dynobj
, ".dynbss");
3533 htab
->srelbss
= bfd_get_linker_section (dynobj
, ".rela.bss");
3541 /* Implement elf_backend_copy_indirect_symbol:
3542 Copy the extra info we tack onto an elf_link_hash_entry. */
3544 nios2_elf32_copy_indirect_symbol (struct bfd_link_info
*info
,
3545 struct elf_link_hash_entry
*dir
,
3546 struct elf_link_hash_entry
*ind
)
3548 struct elf32_nios2_link_hash_entry
*edir
, *eind
;
3550 edir
= (struct elf32_nios2_link_hash_entry
*) dir
;
3551 eind
= (struct elf32_nios2_link_hash_entry
*) ind
;
3553 if (eind
->dyn_relocs
!= NULL
)
3555 if (edir
->dyn_relocs
!= NULL
)
3557 struct elf32_nios2_dyn_relocs
**pp
;
3558 struct elf32_nios2_dyn_relocs
*p
;
3560 /* Add reloc counts against the indirect sym to the direct sym
3561 list. Merge any entries against the same section. */
3562 for (pp
= &eind
->dyn_relocs
; (p
= *pp
) != NULL
; )
3564 struct elf32_nios2_dyn_relocs
*q
;
3566 for (q
= edir
->dyn_relocs
; q
!= NULL
; q
= q
->next
)
3567 if (q
->sec
== p
->sec
)
3569 q
->pc_count
+= p
->pc_count
;
3570 q
->count
+= p
->count
;
3577 *pp
= edir
->dyn_relocs
;
3580 edir
->dyn_relocs
= eind
->dyn_relocs
;
3581 eind
->dyn_relocs
= NULL
;
3584 if (ind
->root
.type
== bfd_link_hash_indirect
3585 && dir
->got
.refcount
<= 0)
3587 edir
->tls_type
= eind
->tls_type
;
3588 eind
->tls_type
= GOT_UNKNOWN
;
3591 edir
->got_types_used
|= eind
->got_types_used
;
3593 _bfd_elf_link_hash_copy_indirect (info
, dir
, ind
);
3596 /* Implement elf_backend_check_relocs:
3597 Look through the relocs for a section during the first phase. */
3599 nios2_elf32_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
,
3600 asection
*sec
, const Elf_Internal_Rela
*relocs
)
3603 Elf_Internal_Shdr
*symtab_hdr
;
3604 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
3605 const Elf_Internal_Rela
*rel
;
3606 const Elf_Internal_Rela
*rel_end
;
3607 struct elf32_nios2_link_hash_table
*htab
;
3610 asection
*sreloc
= NULL
;
3611 bfd_signed_vma
*local_got_refcounts
;
3613 if (info
->relocatable
)
3616 dynobj
= elf_hash_table (info
)->dynobj
;
3617 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3618 sym_hashes
= elf_sym_hashes (abfd
);
3619 sym_hashes_end
= (sym_hashes
3620 + symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
));
3621 if (!elf_bad_symtab (abfd
))
3622 sym_hashes_end
-= symtab_hdr
->sh_info
;
3623 local_got_refcounts
= elf_local_got_refcounts (abfd
);
3625 htab
= elf32_nios2_hash_table (info
);
3626 sgot
= htab
->root
.sgot
;
3627 srelgot
= htab
->root
.srelgot
;
3629 rel_end
= relocs
+ sec
->reloc_count
;
3630 for (rel
= relocs
; rel
< rel_end
; rel
++)
3632 unsigned int r_type
;
3633 struct elf_link_hash_entry
*h
;
3634 unsigned long r_symndx
;
3636 r_symndx
= ELF32_R_SYM (rel
->r_info
);
3637 if (r_symndx
< symtab_hdr
->sh_info
)
3641 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
3642 while (h
->root
.type
== bfd_link_hash_indirect
3643 || h
->root
.type
== bfd_link_hash_warning
)
3644 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3646 /* PR15323, ref flags aren't set for references in the same
3648 h
->root
.non_ir_ref
= 1;
3651 r_type
= ELF32_R_TYPE (rel
->r_info
);
3656 case R_NIOS2_CALL16
:
3657 case R_NIOS2_TLS_GD16
:
3658 case R_NIOS2_TLS_IE16
:
3659 /* This symbol requires a global offset table entry. */
3661 int tls_type
, old_tls_type
;
3667 case R_NIOS2_CALL16
:
3668 tls_type
= GOT_NORMAL
;
3670 case R_NIOS2_TLS_GD16
:
3671 tls_type
= GOT_TLS_GD
;
3673 case R_NIOS2_TLS_IE16
:
3674 tls_type
= GOT_TLS_IE
;
3680 /* Create the .got section. */
3681 elf_hash_table (info
)->dynobj
= dynobj
= abfd
;
3682 nios2_elf32_create_dynamic_sections (dynobj
, info
);
3687 sgot
= htab
->root
.sgot
;
3688 BFD_ASSERT (sgot
!= NULL
);
3692 && (h
!= NULL
|| info
->shared
))
3694 srelgot
= htab
->root
.srelgot
;
3695 BFD_ASSERT (srelgot
!= NULL
);
3700 struct elf32_nios2_link_hash_entry
*eh
3701 = (struct elf32_nios2_link_hash_entry
*)h
;
3703 old_tls_type
= elf32_nios2_hash_entry(h
)->tls_type
;
3704 if (r_type
== R_NIOS2_CALL16
)
3706 /* Make sure a plt entry is created for this symbol if
3707 it turns out to be a function defined by a dynamic
3712 eh
->got_types_used
|= CALL16_USED
;
3715 eh
->got_types_used
|= GOT16_USED
;
3719 /* This is a global offset table entry for a local symbol. */
3720 if (local_got_refcounts
== NULL
)
3724 size
= symtab_hdr
->sh_info
;
3725 size
*= (sizeof (bfd_signed_vma
) + sizeof (char));
3727 = ((bfd_signed_vma
*) bfd_zalloc (abfd
, size
));
3728 if (local_got_refcounts
== NULL
)
3730 elf_local_got_refcounts (abfd
) = local_got_refcounts
;
3731 elf32_nios2_local_got_tls_type (abfd
)
3732 = (char *) (local_got_refcounts
+ symtab_hdr
->sh_info
);
3734 local_got_refcounts
[r_symndx
]++;
3735 old_tls_type
= elf32_nios2_local_got_tls_type (abfd
) [r_symndx
];
3738 /* We will already have issued an error message if there is a
3739 TLS / non-TLS mismatch, based on the symbol type. We don't
3740 support any linker relaxations. So just combine any TLS
3742 if (old_tls_type
!= GOT_UNKNOWN
&& old_tls_type
!= GOT_NORMAL
3743 && tls_type
!= GOT_NORMAL
)
3744 tls_type
|= old_tls_type
;
3746 if (old_tls_type
!= tls_type
)
3749 elf32_nios2_hash_entry (h
)->tls_type
= tls_type
;
3751 elf32_nios2_local_got_tls_type (abfd
) [r_symndx
] = tls_type
;
3755 case R_NIOS2_TLS_LDM16
:
3756 if (r_type
== R_NIOS2_TLS_LDM16
)
3757 htab
->tls_ldm_got
.refcount
++;
3759 if (htab
->root
.sgot
== NULL
)
3761 if (htab
->root
.dynobj
== NULL
)
3762 htab
->root
.dynobj
= abfd
;
3763 if (!create_got_section (htab
->root
.dynobj
, info
))
3768 /* This relocation describes the C++ object vtable hierarchy.
3769 Reconstruct it for later use during GC. */
3770 case R_NIOS2_GNU_VTINHERIT
:
3771 if (!bfd_elf_gc_record_vtinherit (abfd
, sec
, h
, rel
->r_offset
))
3775 /* This relocation describes which C++ vtable entries are actually
3776 used. Record for later use during GC. */
3777 case R_NIOS2_GNU_VTENTRY
:
3778 if (!bfd_elf_gc_record_vtentry (abfd
, sec
, h
, rel
->r_addend
))
3782 case R_NIOS2_BFD_RELOC_32
:
3783 case R_NIOS2_CALL26
:
3784 case R_NIOS2_CALL26_NOAT
:
3785 case R_NIOS2_HIADJ16
:
3790 /* If this reloc is in a read-only section, we might
3791 need a copy reloc. We can't check reliably at this
3792 stage whether the section is read-only, as input
3793 sections have not yet been mapped to output sections.
3794 Tentatively set the flag for now, and correct in
3795 adjust_dynamic_symbol. */
3799 /* Make sure a plt entry is created for this symbol if it
3800 turns out to be a function defined by a dynamic object. */
3803 if (r_type
== R_NIOS2_CALL26
|| r_type
== R_NIOS2_CALL26_NOAT
)
3807 /* If we are creating a shared library, we need to copy the
3808 reloc into the shared library. */
3810 && (sec
->flags
& SEC_ALLOC
) != 0
3811 && (r_type
== R_NIOS2_BFD_RELOC_32
3812 || (h
!= NULL
&& ! h
->needs_plt
3813 && (! info
->symbolic
|| ! h
->def_regular
))))
3815 struct elf32_nios2_dyn_relocs
*p
;
3816 struct elf32_nios2_dyn_relocs
**head
;
3818 /* When creating a shared object, we must copy these
3819 reloc types into the output file. We create a reloc
3820 section in dynobj and make room for this reloc. */
3823 sreloc
= _bfd_elf_make_dynamic_reloc_section
3824 (sec
, dynobj
, 2, abfd
, TRUE
);
3829 /* If this is a global symbol, we count the number of
3830 relocations we need for this symbol. */
3832 head
= &((struct elf32_nios2_link_hash_entry
*) h
)->dyn_relocs
;
3835 /* Track dynamic relocs needed for local syms too.
3836 We really need local syms available to do this
3841 Elf_Internal_Sym
*isym
;
3843 isym
= bfd_sym_from_r_symndx (&htab
->sym_cache
,
3848 s
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
3852 vpp
= &elf_section_data (s
)->local_dynrel
;
3853 head
= (struct elf32_nios2_dyn_relocs
**) vpp
;
3857 if (p
== NULL
|| p
->sec
!= sec
)
3859 bfd_size_type amt
= sizeof *p
;
3860 p
= ((struct elf32_nios2_dyn_relocs
*)
3861 bfd_alloc (htab
->root
.dynobj
, amt
));
3882 /* Implement elf_backend_gc_mark_hook:
3883 Return the section that should be marked against GC for a given
3886 nios2_elf32_gc_mark_hook (asection
*sec
,
3887 struct bfd_link_info
*info
,
3888 Elf_Internal_Rela
*rel
,
3889 struct elf_link_hash_entry
*h
,
3890 Elf_Internal_Sym
*sym
)
3893 switch (ELF32_R_TYPE (rel
->r_info
))
3895 case R_NIOS2_GNU_VTINHERIT
:
3896 case R_NIOS2_GNU_VTENTRY
:
3899 return _bfd_elf_gc_mark_hook (sec
, info
, rel
, h
, sym
);
3902 /* Implement elf_backend_gc_sweep_hook:
3903 Update the got entry reference counts for the section being removed. */
3905 nios2_elf32_gc_sweep_hook (bfd
*abfd
,
3906 struct bfd_link_info
*info
,
3908 const Elf_Internal_Rela
*relocs
)
3910 Elf_Internal_Shdr
*symtab_hdr
;
3911 struct elf_link_hash_entry
**sym_hashes
;
3912 bfd_signed_vma
*local_got_refcounts
;
3913 const Elf_Internal_Rela
*rel
, *relend
;
3916 if (info
->relocatable
)
3919 elf_section_data (sec
)->local_dynrel
= NULL
;
3921 dynobj
= elf_hash_table (info
)->dynobj
;
3925 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3926 sym_hashes
= elf_sym_hashes (abfd
);
3927 local_got_refcounts
= elf_local_got_refcounts (abfd
);
3929 relend
= relocs
+ sec
->reloc_count
;
3930 for (rel
= relocs
; rel
< relend
; rel
++)
3932 unsigned long r_symndx
;
3933 struct elf_link_hash_entry
*h
= NULL
;
3936 r_symndx
= ELF32_R_SYM (rel
->r_info
);
3937 if (r_symndx
>= symtab_hdr
->sh_info
)
3939 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
3940 while (h
->root
.type
== bfd_link_hash_indirect
3941 || h
->root
.type
== bfd_link_hash_warning
)
3942 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3945 r_type
= ELF32_R_TYPE (rel
->r_info
);
3949 case R_NIOS2_CALL16
:
3952 if (h
->got
.refcount
> 0)
3955 else if (local_got_refcounts
!= NULL
)
3957 if (local_got_refcounts
[r_symndx
] > 0)
3958 --local_got_refcounts
[r_symndx
];
3962 case R_NIOS2_PCREL_LO
:
3963 case R_NIOS2_PCREL_HA
:
3964 case R_NIOS2_BFD_RELOC_32
:
3965 case R_NIOS2_CALL26
:
3966 case R_NIOS2_CALL26_NOAT
:
3969 struct elf32_nios2_link_hash_entry
*eh
;
3970 struct elf32_nios2_dyn_relocs
**pp
;
3971 struct elf32_nios2_dyn_relocs
*p
;
3973 eh
= (struct elf32_nios2_link_hash_entry
*) h
;
3975 if (h
->plt
.refcount
> 0)
3978 if (r_type
== R_NIOS2_PCREL_LO
|| r_type
== R_NIOS2_PCREL_HA
3979 || r_type
== R_NIOS2_BFD_RELOC_32
)
3981 for (pp
= &eh
->dyn_relocs
; (p
= *pp
) != NULL
;
4002 /* Implement elf_backend_finish_dynamic_symbols:
4003 Finish up dynamic symbol handling. We set the contents of various
4004 dynamic sections here. */
4006 nios2_elf32_finish_dynamic_symbol (bfd
*output_bfd
,
4007 struct bfd_link_info
*info
,
4008 struct elf_link_hash_entry
*h
,
4009 Elf_Internal_Sym
*sym
)
4011 struct elf32_nios2_link_hash_table
*htab
;
4012 struct elf32_nios2_link_hash_entry
*eh
4013 = (struct elf32_nios2_link_hash_entry
*)h
;
4016 htab
= elf32_nios2_hash_table (info
);
4018 if (h
->plt
.offset
!= (bfd_vma
) -1)
4025 Elf_Internal_Rela rela
;
4027 bfd_vma got_address
;
4029 /* This symbol has an entry in the procedure linkage table. Set
4031 BFD_ASSERT (h
->dynindx
!= -1);
4032 splt
= htab
->root
.splt
;
4033 sgotplt
= htab
->root
.sgotplt
;
4034 srela
= htab
->root
.srelplt
;
4035 BFD_ASSERT (splt
!= NULL
&& sgotplt
!= NULL
&& srela
!= NULL
);
4037 /* Emit the PLT entry. */
4040 nios2_elf32_install_data (splt
, nios2_so_plt_entry
, h
->plt
.offset
,
4042 plt_index
= (h
->plt
.offset
- 24) / 12;
4043 got_offset
= (plt_index
+ 3) * 4;
4044 nios2_elf32_install_imm16 (splt
, h
->plt
.offset
,
4045 hiadj(plt_index
* 4));
4046 nios2_elf32_install_imm16 (splt
, h
->plt
.offset
+ 4,
4047 (plt_index
* 4) & 0xffff);
4048 nios2_elf32_install_imm16 (splt
, h
->plt
.offset
+ 8,
4049 0xfff4 - h
->plt
.offset
);
4050 got_address
= (sgotplt
->output_section
->vma
+ sgotplt
->output_offset
4053 /* Fill in the entry in the global offset table. There are no
4054 res_n slots for a shared object PLT, instead the .got.plt entries
4055 point to the PLT entries. */
4056 bfd_put_32 (output_bfd
,
4057 splt
->output_section
->vma
+ splt
->output_offset
4058 + h
->plt
.offset
, sgotplt
->contents
+ got_offset
);
4062 plt_index
= (h
->plt
.offset
- 28 - htab
->res_n_size
) / 12;
4063 got_offset
= (plt_index
+ 3) * 4;
4065 nios2_elf32_install_data (splt
, nios2_plt_entry
, h
->plt
.offset
, 3);
4066 got_address
= (sgotplt
->output_section
->vma
+ sgotplt
->output_offset
4068 nios2_elf32_install_imm16 (splt
, h
->plt
.offset
, hiadj(got_address
));
4069 nios2_elf32_install_imm16 (splt
, h
->plt
.offset
+ 4,
4070 got_address
& 0xffff);
4072 /* Fill in the entry in the global offset table. */
4073 bfd_put_32 (output_bfd
,
4074 splt
->output_section
->vma
+ splt
->output_offset
4075 + plt_index
* 4, sgotplt
->contents
+ got_offset
);
4078 /* Fill in the entry in the .rela.plt section. */
4079 rela
.r_offset
= got_address
;
4080 rela
.r_info
= ELF32_R_INFO (h
->dynindx
, R_NIOS2_JUMP_SLOT
);
4082 loc
= srela
->contents
+ plt_index
* sizeof (Elf32_External_Rela
);
4083 bfd_elf32_swap_reloca_out (output_bfd
, &rela
, loc
);
4085 if (!h
->def_regular
)
4087 /* Mark the symbol as undefined, rather than as defined in
4088 the .plt section. Leave the value alone. */
4089 sym
->st_shndx
= SHN_UNDEF
;
4090 /* If the symbol is weak, we do need to clear the value.
4091 Otherwise, the PLT entry would provide a definition for
4092 the symbol even if the symbol wasn't defined anywhere,
4093 and so the symbol would never be NULL. */
4094 if (!h
->ref_regular_nonweak
)
4099 use_plt
= (eh
->got_types_used
== CALL16_USED
4100 && h
->plt
.offset
!= (bfd_vma
) -1);
4102 if (!use_plt
&& h
->got
.offset
!= (bfd_vma
) -1
4103 && (elf32_nios2_hash_entry (h
)->tls_type
& GOT_TLS_GD
) == 0
4104 && (elf32_nios2_hash_entry (h
)->tls_type
& GOT_TLS_IE
) == 0)
4108 Elf_Internal_Rela rela
;
4112 /* This symbol has an entry in the global offset table. Set it
4114 sgot
= htab
->root
.sgot
;
4115 srela
= htab
->root
.srelgot
;
4116 BFD_ASSERT (sgot
!= NULL
&& srela
!= NULL
);
4118 offset
= (h
->got
.offset
& ~(bfd_vma
) 1);
4119 rela
.r_offset
= (sgot
->output_section
->vma
4120 + sgot
->output_offset
+ offset
);
4122 /* If this is a -Bsymbolic link, and the symbol is defined
4123 locally, we just want to emit a RELATIVE reloc. Likewise if
4124 the symbol was forced to be local because of a version file.
4125 The entry in the global offset table will already have been
4126 initialized in the relocate_section function. */
4128 if (info
->shared
&& SYMBOL_REFERENCES_LOCAL (info
, h
))
4130 rela
.r_info
= ELF32_R_INFO (0, R_NIOS2_RELATIVE
);
4131 rela
.r_addend
= bfd_get_signed_32 (output_bfd
,
4132 (sgot
->contents
+ offset
));
4133 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
+ offset
);
4137 bfd_put_32 (output_bfd
, (bfd_vma
) 0,
4138 sgot
->contents
+ offset
);
4139 rela
.r_info
= ELF32_R_INFO (h
->dynindx
, R_NIOS2_GLOB_DAT
);
4143 loc
= srela
->contents
;
4144 loc
+= srela
->reloc_count
++ * sizeof (Elf32_External_Rela
);
4145 bfd_elf32_swap_reloca_out (output_bfd
, &rela
, loc
);
4148 if (use_plt
&& h
->got
.offset
!= (bfd_vma
) -1)
4150 bfd_vma offset
= (h
->got
.offset
& ~(bfd_vma
) 1);
4151 asection
*sgot
= htab
->root
.sgot
;
4152 asection
*splt
= htab
->root
.splt
;
4153 bfd_put_32 (output_bfd
, (splt
->output_section
->vma
+ splt
->output_offset
4155 sgot
->contents
+ offset
);
4161 Elf_Internal_Rela rela
;
4164 /* This symbol needs a copy reloc. Set it up. */
4165 BFD_ASSERT (h
->dynindx
!= -1
4166 && (h
->root
.type
== bfd_link_hash_defined
4167 || h
->root
.type
== bfd_link_hash_defweak
));
4170 BFD_ASSERT (s
!= NULL
);
4172 rela
.r_offset
= (h
->root
.u
.def
.value
4173 + h
->root
.u
.def
.section
->output_section
->vma
4174 + h
->root
.u
.def
.section
->output_offset
);
4175 rela
.r_info
= ELF32_R_INFO (h
->dynindx
, R_NIOS2_COPY
);
4177 loc
= s
->contents
+ s
->reloc_count
++ * sizeof (Elf32_External_Rela
);
4178 bfd_elf32_swap_reloca_out (output_bfd
, &rela
, loc
);
4181 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
4182 if (strcmp (h
->root
.root
.string
, "_DYNAMIC") == 0
4183 || h
== elf_hash_table (info
)->hgot
)
4184 sym
->st_shndx
= SHN_ABS
;
4189 /* Implement elf_backend_finish_dynamic_sections. */
4191 nios2_elf32_finish_dynamic_sections (bfd
*output_bfd
,
4192 struct bfd_link_info
*info
)
4197 struct elf32_nios2_link_hash_table
*htab
;
4199 htab
= elf32_nios2_hash_table (info
);
4200 dynobj
= elf_hash_table (info
)->dynobj
;
4201 sgotplt
= htab
->root
.sgotplt
;
4202 BFD_ASSERT (sgotplt
!= NULL
);
4203 sdyn
= bfd_get_linker_section (dynobj
, ".dynamic");
4205 if (elf_hash_table (info
)->dynamic_sections_created
)
4208 Elf32_External_Dyn
*dyncon
, *dynconend
;
4210 splt
= htab
->root
.splt
;
4211 BFD_ASSERT (splt
!= NULL
&& sdyn
!= NULL
);
4213 dyncon
= (Elf32_External_Dyn
*) sdyn
->contents
;
4214 dynconend
= (Elf32_External_Dyn
*) (sdyn
->contents
+ sdyn
->size
);
4215 for (; dyncon
< dynconend
; dyncon
++)
4217 Elf_Internal_Dyn dyn
;
4220 bfd_elf32_swap_dyn_in (dynobj
, dyncon
, &dyn
);
4228 s
= htab
->root
.sgot
;
4229 BFD_ASSERT (s
!= NULL
);
4230 dyn
.d_un
.d_ptr
= s
->output_section
->vma
;
4231 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
4235 s
= htab
->root
.srelplt
;
4236 BFD_ASSERT (s
!= NULL
);
4237 dyn
.d_un
.d_ptr
= s
->output_section
->vma
;
4238 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
4242 s
= htab
->root
.srelplt
;
4243 BFD_ASSERT (s
!= NULL
);
4244 dyn
.d_un
.d_val
= s
->size
;
4245 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
4249 /* The procedure linkage table relocs (DT_JMPREL) should
4250 not be included in the overall relocs (DT_RELA).
4251 Therefore, we override the DT_RELASZ entry here to
4252 make it not include the JMPREL relocs. Since the
4253 linker script arranges for .rela.plt to follow all
4254 other relocation sections, we don't have to worry
4255 about changing the DT_RELA entry. */
4256 s
= htab
->root
.srelplt
;
4258 dyn
.d_un
.d_val
-= s
->size
;
4259 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
4263 s
= htab
->root
.sgot
;
4264 BFD_ASSERT (s
!= NULL
);
4265 dyn
.d_un
.d_ptr
= s
->output_section
->vma
+ 0x7ff0;
4266 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
4271 /* Fill in the first entry in the procedure linkage table. */
4274 bfd_vma got_address
= (sgotplt
->output_section
->vma
4275 + sgotplt
->output_offset
);
4278 bfd_vma corrected
= got_address
- (splt
->output_section
->vma
4279 + splt
->output_offset
+ 4);
4280 nios2_elf32_install_data (splt
, nios2_so_plt0_entry
, 0, 6);
4281 nios2_elf32_install_imm16 (splt
, 4, hiadj (corrected
));
4282 nios2_elf32_install_imm16 (splt
, 12, (corrected
& 0xffff) + 4);
4283 nios2_elf32_install_imm16 (splt
, 16, (corrected
& 0xffff) + 8);
4287 /* Divide by 4 here, not 3 because we already corrected for the
4289 bfd_vma res_size
= (splt
->size
- 28) / 4;
4290 bfd_vma res_start
= (splt
->output_section
->vma
4291 + splt
->output_offset
);
4294 for (res_offset
= 0; res_offset
< res_size
; res_offset
+= 4)
4295 bfd_put_32 (output_bfd
,
4296 6 | ((res_size
- (res_offset
+ 4)) << 6),
4297 splt
->contents
+ res_offset
);
4299 nios2_elf32_install_data (splt
, nios2_plt0_entry
, res_size
, 7);
4300 nios2_elf32_install_imm16 (splt
, res_size
, hiadj (res_start
));
4301 nios2_elf32_install_imm16 (splt
, res_size
+ 4,
4302 res_start
& 0xffff);
4303 nios2_elf32_install_imm16 (splt
, res_size
+ 12,
4304 hiadj (got_address
));
4305 nios2_elf32_install_imm16 (splt
, res_size
+ 16,
4306 (got_address
& 0xffff) + 4);
4307 nios2_elf32_install_imm16 (splt
, res_size
+ 20,
4308 (got_address
& 0xffff) + 8);
4312 /* Fill in the first three entries in the global offset table. */
4313 if (sgotplt
->size
> 0)
4316 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgotplt
->contents
);
4318 bfd_put_32 (output_bfd
,
4319 sdyn
->output_section
->vma
+ sdyn
->output_offset
,
4321 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgotplt
->contents
+ 4);
4322 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgotplt
->contents
+ 8);
4325 elf_section_data (sgotplt
->output_section
)->this_hdr
.sh_entsize
= 4;
4330 /* Implement elf_backend_adjust_dynamic_symbol:
4331 Adjust a symbol defined by a dynamic object and referenced by a
4332 regular object. The current definition is in some section of the
4333 dynamic object, but we're not including those sections. We have to
4334 change the definition to something the rest of the link can
4337 nios2_elf32_adjust_dynamic_symbol (struct bfd_link_info
*info
,
4338 struct elf_link_hash_entry
*h
)
4340 struct elf32_nios2_link_hash_table
*htab
;
4345 htab
= elf32_nios2_hash_table (info
);
4346 dynobj
= elf_hash_table (info
)->dynobj
;
4348 /* Make sure we know what is going on here. */
4349 BFD_ASSERT (dynobj
!= NULL
4351 || h
->u
.weakdef
!= NULL
4354 && !h
->def_regular
)));
4356 /* If this is a function, put it in the procedure linkage table. We
4357 will fill in the contents of the procedure linkage table later,
4358 when we know the address of the .got section. */
4359 if (h
->type
== STT_FUNC
|| h
->needs_plt
)
4361 if (h
->plt
.refcount
<= 0
4362 || SYMBOL_CALLS_LOCAL (info
, h
)
4363 || (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
4364 && h
->root
.type
== bfd_link_hash_undefweak
))
4366 /* This case can occur if we saw a PLT reloc in an input
4367 file, but the symbol was never referred to by a dynamic
4368 object, or if all references were garbage collected. In
4369 such a case, we don't actually need to build a procedure
4370 linkage table, and we can just do a PCREL reloc instead. */
4371 h
->plt
.offset
= (bfd_vma
) -1;
4378 /* Reinitialize the plt offset now that it is not used as a reference
4380 h
->plt
.offset
= (bfd_vma
) -1;
4382 /* If this is a weak symbol, and there is a real definition, the
4383 processor independent code will have arranged for us to see the
4384 real definition first, and we can just use the same value. */
4385 if (h
->u
.weakdef
!= NULL
)
4387 BFD_ASSERT (h
->u
.weakdef
->root
.type
== bfd_link_hash_defined
4388 || h
->u
.weakdef
->root
.type
== bfd_link_hash_defweak
);
4389 h
->root
.u
.def
.section
= h
->u
.weakdef
->root
.u
.def
.section
;
4390 h
->root
.u
.def
.value
= h
->u
.weakdef
->root
.u
.def
.value
;
4394 /* If there are no non-GOT references, we do not need a copy
4396 if (!h
->non_got_ref
)
4399 /* This is a reference to a symbol defined by a dynamic object which
4401 If we are creating a shared library, we must presume that the
4402 only references to the symbol are via the global offset table.
4403 For such cases we need not do anything here; the relocations will
4404 be handled correctly by relocate_section. */
4410 (*_bfd_error_handler
) (_("dynamic variable `%s' is zero size"),
4411 h
->root
.root
.string
);
4415 /* We must allocate the symbol in our .dynbss section, which will
4416 become part of the .bss section of the executable. There will be
4417 an entry for this symbol in the .dynsym section. The dynamic
4418 object will contain position independent code, so all references
4419 from the dynamic object to this symbol will go through the global
4420 offset table. The dynamic linker will use the .dynsym entry to
4421 determine the address it must put in the global offset table, so
4422 both the dynamic object and the regular object will refer to the
4423 same memory location for the variable. */
4425 BFD_ASSERT (s
!= NULL
);
4427 /* We must generate a R_NIOS2_COPY reloc to tell the dynamic linker to
4428 copy the initial value out of the dynamic object and into the
4429 runtime process image. We need to remember the offset into the
4430 .rela.bss section we are going to use. */
4431 if ((h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0)
4435 srel
= htab
->srelbss
;
4436 BFD_ASSERT (srel
!= NULL
);
4437 srel
->size
+= sizeof (Elf32_External_Rela
);
4441 align2
= bfd_log2 (h
->size
);
4442 if (align2
> h
->root
.u
.def
.section
->alignment_power
)
4443 align2
= h
->root
.u
.def
.section
->alignment_power
;
4446 s
->size
= BFD_ALIGN (s
->size
, (bfd_size_type
)1 << align2
);
4447 if (align2
> bfd_get_section_alignment (dynobj
, s
)
4448 && !bfd_set_section_alignment (dynobj
, s
, align2
))
4451 /* Define the symbol as being at this point in the section. */
4452 h
->root
.u
.def
.section
= s
;
4453 h
->root
.u
.def
.value
= s
->size
;
4455 /* Increment the section size to make room for the symbol. */
4461 /* Worker function for nios2_elf32_size_dynamic_sections. */
4463 adjust_dynrelocs (struct elf_link_hash_entry
*h
, PTR inf
)
4465 struct bfd_link_info
*info
;
4466 struct elf32_nios2_link_hash_table
*htab
;
4468 if (h
->root
.type
== bfd_link_hash_indirect
)
4471 if (h
->root
.type
== bfd_link_hash_warning
)
4472 /* When warning symbols are created, they **replace** the "real"
4473 entry in the hash table, thus we never get to see the real
4474 symbol in a hash traversal. So look at it now. */
4475 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4477 info
= (struct bfd_link_info
*) inf
;
4478 htab
= elf32_nios2_hash_table (info
);
4480 if (h
->plt
.offset
!= (bfd_vma
)-1)
4481 h
->plt
.offset
+= htab
->res_n_size
;
4482 if (htab
->root
.splt
== h
->root
.u
.def
.section
)
4483 h
->root
.u
.def
.value
+= htab
->res_n_size
;
4488 /* Another worker function for nios2_elf32_size_dynamic_sections.
4489 Allocate space in .plt, .got and associated reloc sections for
4492 allocate_dynrelocs (struct elf_link_hash_entry
*h
, PTR inf
)
4494 struct bfd_link_info
*info
;
4495 struct elf32_nios2_link_hash_table
*htab
;
4496 struct elf32_nios2_link_hash_entry
*eh
;
4497 struct elf32_nios2_dyn_relocs
*p
;
4500 if (h
->root
.type
== bfd_link_hash_indirect
)
4503 if (h
->root
.type
== bfd_link_hash_warning
)
4504 /* When warning symbols are created, they **replace** the "real"
4505 entry in the hash table, thus we never get to see the real
4506 symbol in a hash traversal. So look at it now. */
4507 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4509 info
= (struct bfd_link_info
*) inf
;
4510 htab
= elf32_nios2_hash_table (info
);
4512 if (htab
->root
.dynamic_sections_created
4513 && h
->plt
.refcount
> 0)
4515 /* Make sure this symbol is output as a dynamic symbol.
4516 Undefined weak syms won't yet be marked as dynamic. */
4517 if (h
->dynindx
== -1
4519 && !bfd_elf_link_record_dynamic_symbol (info
, h
))
4522 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info
->shared
, h
))
4524 asection
*s
= htab
->root
.splt
;
4526 /* Allocate room for the header. */
4535 h
->plt
.offset
= s
->size
;
4537 /* If this symbol is not defined in a regular file, and we are
4538 not generating a shared library, then set the symbol to this
4539 location in the .plt. This is required to make function
4540 pointers compare as equal between the normal executable and
4541 the shared library. */
4545 h
->root
.u
.def
.section
= s
;
4546 h
->root
.u
.def
.value
= h
->plt
.offset
;
4549 /* Make room for this entry. */
4552 /* We also need to make an entry in the .rela.plt section. */
4553 htab
->root
.srelplt
->size
+= sizeof (Elf32_External_Rela
);
4555 /* And the .got.plt section. */
4556 htab
->root
.sgotplt
->size
+= 4;
4560 h
->plt
.offset
= (bfd_vma
) -1;
4566 h
->plt
.offset
= (bfd_vma
) -1;
4570 eh
= (struct elf32_nios2_link_hash_entry
*) h
;
4571 use_plt
= (eh
->got_types_used
== CALL16_USED
4572 && h
->plt
.offset
!= (bfd_vma
) -1);
4574 if (h
->got
.refcount
> 0)
4578 int tls_type
= eh
->tls_type
;
4581 /* Make sure this symbol is output as a dynamic symbol.
4582 Undefined weak syms won't yet be marked as dynamic. */
4583 if (h
->dynindx
== -1
4585 && !bfd_elf_link_record_dynamic_symbol (info
, h
))
4588 s
= htab
->root
.sgot
;
4589 h
->got
.offset
= s
->size
;
4591 if (tls_type
== GOT_UNKNOWN
)
4594 if (tls_type
== GOT_NORMAL
)
4595 /* Non-TLS symbols need one GOT slot. */
4599 if (tls_type
& GOT_TLS_GD
)
4600 /* R_NIOS2_TLS_GD16 needs 2 consecutive GOT slots. */
4602 if (tls_type
& GOT_TLS_IE
)
4603 /* R_NIOS2_TLS_IE16 needs one GOT slot. */
4607 dyn
= htab
->root
.dynamic_sections_created
;
4610 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, info
->shared
, h
)
4612 || !SYMBOL_REFERENCES_LOCAL (info
, h
)))
4615 if (tls_type
!= GOT_NORMAL
4616 && (info
->shared
|| indx
!= 0)
4617 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
4618 || h
->root
.type
!= bfd_link_hash_undefweak
))
4620 if (tls_type
& GOT_TLS_IE
)
4621 htab
->root
.srelgot
->size
+= sizeof (Elf32_External_Rela
);
4623 if (tls_type
& GOT_TLS_GD
)
4624 htab
->root
.srelgot
->size
+= sizeof (Elf32_External_Rela
);
4626 if ((tls_type
& GOT_TLS_GD
) && indx
!= 0)
4627 htab
->root
.srelgot
->size
+= sizeof (Elf32_External_Rela
);
4629 else if ((ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
4630 || h
->root
.type
!= bfd_link_hash_undefweak
)
4633 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, 0, h
)))
4634 htab
->root
.srelgot
->size
+= sizeof (Elf32_External_Rela
);
4637 h
->got
.offset
= (bfd_vma
) -1;
4639 if (eh
->dyn_relocs
== NULL
)
4642 /* In the shared -Bsymbolic case, discard space allocated for
4643 dynamic pc-relative relocs against symbols which turn out to be
4644 defined in regular objects. For the normal shared case, discard
4645 space for pc-relative relocs that have become local due to symbol
4646 visibility changes. */
4651 && (h
->forced_local
|| info
->symbolic
))
4653 struct elf32_nios2_dyn_relocs
**pp
;
4655 for (pp
= &eh
->dyn_relocs
; (p
= *pp
) != NULL
; )
4657 p
->count
-= p
->pc_count
;
4666 /* Also discard relocs on undefined weak syms with non-default
4668 if (eh
->dyn_relocs
!= NULL
4669 && h
->root
.type
== bfd_link_hash_undefweak
)
4671 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
4672 eh
->dyn_relocs
= NULL
;
4674 /* Make sure undefined weak symbols are output as a dynamic
4676 else if (h
->dynindx
== -1
4678 && !bfd_elf_link_record_dynamic_symbol (info
, h
))
4684 /* For the non-shared case, discard space for relocs against
4685 symbols which turn out to need copy relocs or are not
4689 && ((h
->def_dynamic
&& !h
->def_regular
)
4690 || (htab
->root
.dynamic_sections_created
4691 && (h
->root
.type
== bfd_link_hash_undefweak
4692 || h
->root
.type
== bfd_link_hash_undefined
))))
4694 /* Make sure this symbol is output as a dynamic symbol.
4695 Undefined weak syms won't yet be marked as dynamic. */
4696 if (h
->dynindx
== -1
4698 && !bfd_elf_link_record_dynamic_symbol (info
, h
))
4701 /* If that succeeded, we know we'll be keeping all the
4703 if (h
->dynindx
!= -1)
4707 eh
->dyn_relocs
= NULL
;
4712 /* Finally, allocate space. */
4713 for (p
= eh
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
4715 asection
*sreloc
= elf_section_data (p
->sec
)->sreloc
;
4716 sreloc
->size
+= p
->count
* sizeof (Elf32_External_Rela
);
4722 /* Implement elf_backend_size_dynamic_sections:
4723 Set the sizes of the dynamic sections. */
4725 nios2_elf32_size_dynamic_sections (bfd
*output_bfd ATTRIBUTE_UNUSED
,
4726 struct bfd_link_info
*info
)
4734 struct elf32_nios2_link_hash_table
*htab
;
4736 htab
= elf32_nios2_hash_table (info
);
4737 dynobj
= elf_hash_table (info
)->dynobj
;
4738 BFD_ASSERT (dynobj
!= NULL
);
4740 htab
->res_n_size
= 0;
4741 if (elf_hash_table (info
)->dynamic_sections_created
)
4743 /* Set the contents of the .interp section to the interpreter. */
4744 if (info
->executable
)
4746 s
= bfd_get_linker_section (dynobj
, ".interp");
4747 BFD_ASSERT (s
!= NULL
);
4748 s
->size
= sizeof ELF_DYNAMIC_INTERPRETER
;
4749 s
->contents
= (unsigned char *) ELF_DYNAMIC_INTERPRETER
;
4754 /* We may have created entries in the .rela.got section.
4755 However, if we are not creating the dynamic sections, we will
4756 not actually use these entries. Reset the size of .rela.got,
4757 which will cause it to get stripped from the output file
4759 s
= htab
->root
.srelgot
;
4764 /* Set up .got offsets for local syms, and space for local dynamic
4766 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link_next
)
4768 bfd_signed_vma
*local_got
;
4769 bfd_signed_vma
*end_local_got
;
4770 char *local_tls_type
;
4771 bfd_size_type locsymcount
;
4772 Elf_Internal_Shdr
*symtab_hdr
;
4775 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
4778 for (s
= ibfd
->sections
; s
!= NULL
; s
= s
->next
)
4780 struct elf32_nios2_dyn_relocs
*p
;
4782 for (p
= elf_section_data (s
)->local_dynrel
; p
!= NULL
; p
= p
->next
)
4784 if (!bfd_is_abs_section (p
->sec
)
4785 && bfd_is_abs_section (p
->sec
->output_section
))
4787 /* Input section has been discarded, either because
4788 it is a copy of a linkonce section or due to
4789 linker script /DISCARD/, so we'll be discarding
4792 else if (p
->count
!= 0)
4794 srel
= elf_section_data (p
->sec
)->sreloc
;
4795 srel
->size
+= p
->count
* sizeof (Elf32_External_Rela
);
4796 if ((p
->sec
->output_section
->flags
& SEC_READONLY
) != 0)
4797 info
->flags
|= DF_TEXTREL
;
4802 local_got
= elf_local_got_refcounts (ibfd
);
4806 symtab_hdr
= &elf_tdata (ibfd
)->symtab_hdr
;
4807 locsymcount
= symtab_hdr
->sh_info
;
4808 end_local_got
= local_got
+ locsymcount
;
4809 local_tls_type
= elf32_nios2_local_got_tls_type (ibfd
);
4810 s
= htab
->root
.sgot
;
4811 srel
= htab
->root
.srelgot
;
4812 for (; local_got
< end_local_got
; ++local_got
, ++local_tls_type
)
4816 *local_got
= s
->size
;
4817 if (*local_tls_type
& GOT_TLS_GD
)
4818 /* TLS_GD relocs need an 8-byte structure in the GOT. */
4820 if (*local_tls_type
& GOT_TLS_IE
)
4822 if (*local_tls_type
== GOT_NORMAL
)
4825 if (info
->shared
|| *local_tls_type
== GOT_TLS_GD
)
4826 srel
->size
+= sizeof (Elf32_External_Rela
);
4829 *local_got
= (bfd_vma
) -1;
4833 if (htab
->tls_ldm_got
.refcount
> 0)
4835 /* Allocate two GOT entries and one dynamic relocation (if necessary)
4836 for R_NIOS2_TLS_LDM16 relocations. */
4837 htab
->tls_ldm_got
.offset
= htab
->root
.sgot
->size
;
4838 htab
->root
.sgot
->size
+= 8;
4840 htab
->root
.srelgot
->size
+= sizeof (Elf32_External_Rela
);
4843 htab
->tls_ldm_got
.offset
= -1;
4845 /* Allocate global sym .plt and .got entries, and space for global
4846 sym dynamic relocs. */
4847 elf_link_hash_traverse (& htab
->root
, allocate_dynrelocs
, info
);
4849 /* The check_relocs and adjust_dynamic_symbol entry points have
4850 determined the sizes of the various dynamic sections. Allocate
4855 for (s
= dynobj
->sections
; s
!= NULL
; s
= s
->next
)
4859 if ((s
->flags
& SEC_LINKER_CREATED
) == 0)
4862 /* It's OK to base decisions on the section name, because none
4863 of the dynobj section names depend upon the input files. */
4864 name
= bfd_get_section_name (dynobj
, s
);
4866 if (strcmp (name
, ".plt") == 0)
4868 /* Remember whether there is a PLT. */
4871 /* Correct for the number of res_N branches. */
4872 if (plt
&& !info
->shared
)
4874 htab
->res_n_size
= (s
->size
-28) / 3;
4875 s
->size
+= htab
->res_n_size
;
4878 else if (CONST_STRNEQ (name
, ".rela"))
4884 /* We use the reloc_count field as a counter if we need
4885 to copy relocs into the output file. */
4889 else if (CONST_STRNEQ (name
, ".got"))
4891 else if (strcmp (name
, ".dynbss") != 0)
4892 /* It's not one of our sections, so don't allocate space. */
4897 /* If we don't need this section, strip it from the
4898 output file. This is mostly to handle .rela.bss and
4899 .rela.plt. We must create both sections in
4900 create_dynamic_sections, because they must be created
4901 before the linker maps input sections to output
4902 sections. The linker does that before
4903 adjust_dynamic_symbol is called, and it is that
4904 function which decides whether anything needs to go
4905 into these sections. */
4906 s
->flags
|= SEC_EXCLUDE
;
4910 if ((s
->flags
& SEC_HAS_CONTENTS
) == 0)
4913 /* Allocate memory for the section contents. */
4914 /* FIXME: This should be a call to bfd_alloc not bfd_zalloc.
4915 Unused entries should be reclaimed before the section's contents
4916 are written out, but at the moment this does not happen. Thus in
4917 order to prevent writing out garbage, we initialize the section's
4918 contents to zero. */
4919 s
->contents
= (bfd_byte
*) bfd_zalloc (dynobj
, s
->size
);
4920 if (s
->contents
== NULL
)
4924 /* Adjust dynamic symbols that point to the plt to account for the
4925 now-known number of resN slots. */
4926 if (htab
->res_n_size
)
4927 elf_link_hash_traverse (& htab
->root
, adjust_dynrelocs
, info
);
4929 if (elf_hash_table (info
)->dynamic_sections_created
)
4931 /* Add some entries to the .dynamic section. We fill in the
4932 values later, in elf_nios2_finish_dynamic_sections, but we
4933 must add the entries now so that we get the correct size for
4934 the .dynamic section. The DT_DEBUG entry is filled in by the
4935 dynamic linker and used by the debugger. */
4936 #define add_dynamic_entry(TAG, VAL) \
4937 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
4939 if (!info
->shared
&& !add_dynamic_entry (DT_DEBUG
, 0))
4942 if (got
&& !add_dynamic_entry (DT_PLTGOT
, 0))
4946 && (!add_dynamic_entry (DT_PLTRELSZ
, 0)
4947 || !add_dynamic_entry (DT_PLTREL
, DT_RELA
)
4948 || !add_dynamic_entry (DT_JMPREL
, 0)))
4952 && (!add_dynamic_entry (DT_RELA
, 0)
4953 || !add_dynamic_entry (DT_RELASZ
, 0)
4954 || !add_dynamic_entry (DT_RELAENT
, sizeof (Elf32_External_Rela
))))
4957 if (!info
->shared
&& !add_dynamic_entry (DT_NIOS2_GP
, 0))
4960 if ((info
->flags
& DF_TEXTREL
) != 0
4961 && !add_dynamic_entry (DT_TEXTREL
, 0))
4964 #undef add_dynamic_entry
4969 /* Implement bfd_elf32_bfd_link_hash_table_create. */
4970 static struct bfd_link_hash_table
*
4971 nios2_elf32_link_hash_table_create (bfd
*abfd
)
4973 struct elf32_nios2_link_hash_table
*ret
;
4974 bfd_size_type amt
= sizeof (struct elf32_nios2_link_hash_table
);
4976 ret
= bfd_zmalloc (amt
);
4980 if (!_bfd_elf_link_hash_table_init (&ret
->root
, abfd
,
4983 elf32_nios2_link_hash_entry
),
4990 /* Init the stub hash table too. */
4991 if (!bfd_hash_table_init (&ret
->bstab
, stub_hash_newfunc
,
4992 sizeof (struct elf32_nios2_stub_hash_entry
)))
4995 return &ret
->root
.root
;
4998 /* Free the derived linker hash table. */
5000 nios2_elf32_link_hash_table_free (struct bfd_link_hash_table
*btab
)
5002 struct elf32_nios2_link_hash_table
*htab
5003 = (struct elf32_nios2_link_hash_table
*) btab
;
5005 bfd_hash_table_free (&htab
->bstab
);
5006 _bfd_elf_link_hash_table_free (btab
);
5009 /* Implement elf_backend_reloc_type_class. */
5010 static enum elf_reloc_type_class
5011 nios2_elf32_reloc_type_class (const struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
5012 const asection
*rel_sec ATTRIBUTE_UNUSED
,
5013 const Elf_Internal_Rela
*rela
)
5015 switch ((int) ELF32_R_TYPE (rela
->r_info
))
5017 case R_NIOS2_RELATIVE
:
5018 return reloc_class_relative
;
5019 case R_NIOS2_JUMP_SLOT
:
5020 return reloc_class_plt
;
5022 return reloc_class_copy
;
5024 return reloc_class_normal
;
5028 /* Return 1 if target is one of ours. */
5030 is_nios2_elf_target (const struct bfd_target
*targ
)
5032 return (targ
== &bfd_elf32_littlenios2_vec
5033 || targ
== &bfd_elf32_bignios2_vec
);
5036 /* Implement elf_backend_add_symbol_hook.
5037 This hook is called by the linker when adding symbols from an object
5038 file. We use it to put .comm items in .sbss, and not .bss. */
5040 nios2_elf_add_symbol_hook (bfd
*abfd
,
5041 struct bfd_link_info
*info
,
5042 Elf_Internal_Sym
*sym
,
5043 const char **namep ATTRIBUTE_UNUSED
,
5044 flagword
*flagsp ATTRIBUTE_UNUSED
,
5050 if (sym
->st_shndx
== SHN_COMMON
5051 && !info
->relocatable
5052 && sym
->st_size
<= elf_gp_size (abfd
)
5053 && is_nios2_elf_target (info
->output_bfd
->xvec
))
5055 /* Common symbols less than or equal to -G nn bytes are automatically
5057 struct elf32_nios2_link_hash_table
*htab
;
5059 htab
= elf32_nios2_hash_table (info
);
5060 if (htab
->sbss
== NULL
)
5062 flagword flags
= SEC_IS_COMMON
| SEC_LINKER_CREATED
;
5064 dynobj
= elf_hash_table (info
)->dynobj
;
5068 htab
->sbss
= bfd_make_section_anyway_with_flags (dynobj
, ".sbss",
5070 if (htab
->sbss
== NULL
)
5075 *valp
= sym
->st_size
;
5081 /* Implement elf_backend_can_make_relative_eh_frame:
5082 Decide whether to attempt to turn absptr or lsda encodings in
5083 shared libraries into pcrel within the given input section. */
5085 nios2_elf32_can_make_relative_eh_frame (bfd
*input_bfd ATTRIBUTE_UNUSED
,
5086 struct bfd_link_info
*info
5088 asection
*eh_frame_section
5091 /* We can't use PC-relative encodings in the .eh_frame section. */
5095 /* Implement elf_backend_special_sections. */
5096 const struct bfd_elf_special_section elf32_nios2_special_sections
[] =
5098 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS
,
5099 SHF_ALLOC
+ SHF_WRITE
+ SHF_NIOS2_GPREL
},
5100 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS
,
5101 SHF_ALLOC
+ SHF_WRITE
+ SHF_NIOS2_GPREL
},
5102 { NULL
, 0, 0, 0, 0 }
5105 #define ELF_ARCH bfd_arch_nios2
5106 #define ELF_TARGET_ID NIOS2_ELF_DATA
5107 #define ELF_MACHINE_CODE EM_ALTERA_NIOS2
5109 /* The Nios II MMU uses a 4K page size. */
5111 #define ELF_MAXPAGESIZE 0x1000
5113 #define bfd_elf32_bfd_link_hash_table_create \
5114 nios2_elf32_link_hash_table_create
5115 #define bfd_elf32_bfd_link_hash_table_free \
5116 nios2_elf32_link_hash_table_free
5118 /* Relocation table lookup macros. */
5120 #define bfd_elf32_bfd_reloc_type_lookup nios2_elf32_bfd_reloc_type_lookup
5121 #define bfd_elf32_bfd_reloc_name_lookup nios2_elf32_bfd_reloc_name_lookup
5123 /* JUMP_TABLE_LINK macros. */
5125 /* elf_info_to_howto (using RELA relocations). */
5127 #define elf_info_to_howto nios2_elf32_info_to_howto
5129 /* elf backend functions. */
5131 #define elf_backend_can_gc_sections 1
5132 #define elf_backend_can_refcount 1
5133 #define elf_backend_plt_readonly 1
5134 #define elf_backend_want_got_plt 1
5135 #define elf_backend_rela_normal 1
5137 #define elf_backend_relocate_section nios2_elf32_relocate_section
5138 #define elf_backend_section_flags nios2_elf32_section_flags
5139 #define elf_backend_fake_sections nios2_elf32_fake_sections
5140 #define elf_backend_check_relocs nios2_elf32_check_relocs
5142 #define elf_backend_gc_mark_hook nios2_elf32_gc_mark_hook
5143 #define elf_backend_gc_sweep_hook nios2_elf32_gc_sweep_hook
5144 #define elf_backend_create_dynamic_sections \
5145 nios2_elf32_create_dynamic_sections
5146 #define elf_backend_finish_dynamic_symbol nios2_elf32_finish_dynamic_symbol
5147 #define elf_backend_finish_dynamic_sections \
5148 nios2_elf32_finish_dynamic_sections
5149 #define elf_backend_adjust_dynamic_symbol nios2_elf32_adjust_dynamic_symbol
5150 #define elf_backend_reloc_type_class nios2_elf32_reloc_type_class
5151 #define elf_backend_size_dynamic_sections nios2_elf32_size_dynamic_sections
5152 #define elf_backend_add_symbol_hook nios2_elf_add_symbol_hook
5153 #define elf_backend_copy_indirect_symbol nios2_elf32_copy_indirect_symbol
5155 #define elf_backend_grok_prstatus nios2_grok_prstatus
5156 #define elf_backend_grok_psinfo nios2_grok_psinfo
5158 #undef elf_backend_can_make_relative_eh_frame
5159 #define elf_backend_can_make_relative_eh_frame \
5160 nios2_elf32_can_make_relative_eh_frame
5162 #define elf_backend_special_sections elf32_nios2_special_sections
5164 #define TARGET_LITTLE_SYM bfd_elf32_littlenios2_vec
5165 #define TARGET_LITTLE_NAME "elf32-littlenios2"
5166 #define TARGET_BIG_SYM bfd_elf32_bignios2_vec
5167 #define TARGET_BIG_NAME "elf32-bignios2"
5169 #define elf_backend_got_header_size 12
5171 #include "elf32-target.h"