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. */
895 /* GOT pointer symbol _gp_got. */
896 struct elf_link_hash_entry
*h_gp_got
;
899 bfd_signed_vma refcount
;
903 /* Small local sym cache. */
904 struct sym_cache sym_cache
;
909 struct nios2_elf32_obj_tdata
911 struct elf_obj_tdata root
;
913 /* tls_type for each local got entry. */
914 char *local_got_tls_type
;
916 /* TRUE if TLS GD relocs have been seen for this object. */
917 bfd_boolean has_tlsgd
;
920 #define elf32_nios2_tdata(abfd) \
921 ((struct nios2_elf32_obj_tdata *) (abfd)->tdata.any)
923 #define elf32_nios2_local_got_tls_type(abfd) \
924 (elf32_nios2_tdata (abfd)->local_got_tls_type)
926 /* The name of the dynamic interpreter. This is put in the .interp
928 #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
930 /* PLT implementation for position-dependent code. */
931 static const bfd_vma nios2_plt_entry
[] = { /* .PLTn: */
932 0x03c00034, /* movhi r15, %hiadj(plt_got_slot_address) */
933 0x7bc00017, /* ldw r15, %lo(plt_got_slot_address)(r15) */
934 0x7800683a /* jmp r15 */
937 static const bfd_vma nios2_plt0_entry
[] = { /* .PLTresolve */
938 0x03800034, /* movhi r14, %hiadj(res_0) */
939 0x73800004, /* addi r14, r14, %lo(res_0) */
940 0x7b9fc83a, /* sub r15, r15, r14 */
941 0x03400034, /* movhi r13, %hiadj(_GLOBAL_OFFSET_TABLE_) */
942 0x6b800017, /* ldw r14, %lo(_GLOBAL_OFFSET_TABLE_+4)(r13) */
943 0x6b400017, /* ldw r13, %lo(_GLOBAL_OFFSET_TABLE_+8)(r13) */
944 0x6800683a /* jmp r13 */
947 /* PLT implementation for position-independent code. */
948 static const bfd_vma nios2_so_plt_entry
[] = { /* .PLTn */
949 0x03c00034, /* movhi r15, %hiadj(index * 4) */
950 0x7bc00004, /* addi r15, r15, %lo(index * 4) */
951 0x00000006 /* br .PLTresolve */
954 static const bfd_vma nios2_so_plt0_entry
[] = { /* .PLTresolve */
955 0x001ce03a, /* nextpc r14 */
956 0x03400034, /* movhi r13, %hiadj(_GLOBAL_OFFSET_TABLE_) */
957 0x6b9b883a, /* add r13, r13, r14 */
958 0x6b800017, /* ldw r14, %lo(_GLOBAL_OFFSET_TABLE_+4)(r13) */
959 0x6b400017, /* ldw r13, %lo(_GLOBAL_OFFSET_TABLE_+8)(r13) */
960 0x6800683a /* jmp r13 */
964 static const bfd_vma nios2_call26_stub_entry
[] = {
965 0x00400034, /* orhi at, r0, %hiadj(dest) */
966 0x08400004, /* addi at, at, %lo(dest) */
967 0x0800683a /* jmp at */
970 /* Install 16-bit immediate value VALUE at offset OFFSET into section SEC. */
972 nios2_elf32_install_imm16 (asection
*sec
, bfd_vma offset
, bfd_vma value
)
974 bfd_vma word
= bfd_get_32 (sec
->owner
, sec
->contents
+ offset
);
976 BFD_ASSERT(value
<= 0xffff);
978 bfd_put_32 (sec
->owner
, word
| ((value
& 0xffff) << 6),
979 sec
->contents
+ offset
);
982 /* Install COUNT 32-bit values DATA starting at offset OFFSET into
985 nios2_elf32_install_data (asection
*sec
, const bfd_vma
*data
, bfd_vma offset
,
990 bfd_put_32 (sec
->owner
, *data
, sec
->contents
+ offset
);
996 /* The usual way of loading a 32-bit constant into a Nios II register is to
997 load the high 16 bits in one instruction and then add the low 16 bits with
998 a signed add. This means that the high halfword needs to be adjusted to
999 compensate for the sign bit of the low halfword. This function returns the
1000 adjusted high halfword for a given 32-bit constant. */
1002 bfd_vma
hiadj (bfd_vma symbol_value
)
1004 return ((symbol_value
+ 0x8000) >> 16) & 0xffff;
1007 /* Implement elf_backend_grok_prstatus:
1008 Support for core dump NOTE sections. */
1010 nios2_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
1015 switch (note
->descsz
)
1020 case 212: /* Linux/Nios II */
1022 elf_tdata (abfd
)->core
->signal
= bfd_get_16 (abfd
, note
->descdata
+ 12);
1025 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, note
->descdata
+ 24);
1034 /* Make a ".reg/999" section. */
1035 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
1036 size
, note
->descpos
+ offset
);
1039 /* Implement elf_backend_grok_psinfo. */
1041 nios2_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
1043 switch (note
->descsz
)
1048 case 124: /* Linux/Nios II elf_prpsinfo */
1049 elf_tdata (abfd
)->core
->program
1050 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 28, 16);
1051 elf_tdata (abfd
)->core
->command
1052 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 44, 80);
1055 /* Note that for some reason, a spurious space is tacked
1056 onto the end of the args in some (at least one anyway)
1057 implementations, so strip it off if it exists. */
1060 char *command
= elf_tdata (abfd
)->core
->command
;
1061 int n
= strlen (command
);
1063 if (0 < n
&& command
[n
- 1] == ' ')
1064 command
[n
- 1] = '\0';
1070 /* Assorted hash table functions. */
1072 /* Initialize an entry in the stub hash table. */
1073 static struct bfd_hash_entry
*
1074 stub_hash_newfunc (struct bfd_hash_entry
*entry
,
1075 struct bfd_hash_table
*table
,
1078 /* Allocate the structure if it has not already been allocated by a
1082 entry
= bfd_hash_allocate (table
,
1083 sizeof (struct elf32_nios2_stub_hash_entry
));
1088 /* Call the allocation method of the superclass. */
1089 entry
= bfd_hash_newfunc (entry
, table
, string
);
1092 struct elf32_nios2_stub_hash_entry
*hsh
;
1094 /* Initialize the local fields. */
1095 hsh
= (struct elf32_nios2_stub_hash_entry
*) entry
;
1096 hsh
->stub_sec
= NULL
;
1097 hsh
->stub_offset
= 0;
1098 hsh
->target_value
= 0;
1099 hsh
->target_section
= NULL
;
1100 hsh
->stub_type
= nios2_stub_none
;
1108 /* Create an entry in a Nios II ELF linker hash table. */
1109 static struct bfd_hash_entry
*
1110 link_hash_newfunc (struct bfd_hash_entry
*entry
,
1111 struct bfd_hash_table
*table
, const char *string
)
1113 /* Allocate the structure if it has not already been allocated by a
1117 entry
= bfd_hash_allocate (table
,
1118 sizeof (struct elf32_nios2_link_hash_entry
));
1123 /* Call the allocation method of the superclass. */
1124 entry
= _bfd_elf_link_hash_newfunc (entry
, table
, string
);
1127 struct elf32_nios2_link_hash_entry
*eh
;
1129 eh
= (struct elf32_nios2_link_hash_entry
*) entry
;
1130 eh
->hsh_cache
= NULL
;
1131 eh
->dyn_relocs
= NULL
;
1132 eh
->tls_type
= GOT_UNKNOWN
;
1133 eh
->got_types_used
= 0;
1139 /* Section name for stubs is the associated section name plus this
1141 #define STUB_SUFFIX ".stub"
1143 /* Build a name for an entry in the stub hash table. */
1145 nios2_stub_name (const asection
*input_section
,
1146 const asection
*sym_sec
,
1147 const struct elf32_nios2_link_hash_entry
*hh
,
1148 const Elf_Internal_Rela
*rel
,
1149 enum elf32_nios2_stub_type stub_type
)
1153 char stubpos
= (stub_type
== nios2_stub_call26_before
) ? 'b' : 'a';
1157 len
= 8 + 1 + 1 + 1+ strlen (hh
->root
.root
.root
.string
) + 1 + 8 + 1;
1158 stub_name
= bfd_malloc (len
);
1159 if (stub_name
!= NULL
)
1161 sprintf (stub_name
, "%08x_%c_%s+%x",
1162 input_section
->id
& 0xffffffff,
1164 hh
->root
.root
.root
.string
,
1165 (int) rel
->r_addend
& 0xffffffff);
1170 len
= 8 + 1 + 1 + 1+ 8 + 1 + 8 + 1 + 8 + 1;
1171 stub_name
= bfd_malloc (len
);
1172 if (stub_name
!= NULL
)
1174 sprintf (stub_name
, "%08x_%c_%x:%x+%x",
1175 input_section
->id
& 0xffffffff,
1177 sym_sec
->id
& 0xffffffff,
1178 (int) ELF32_R_SYM (rel
->r_info
) & 0xffffffff,
1179 (int) rel
->r_addend
& 0xffffffff);
1185 /* Look up an entry in the stub hash. Stub entries are cached because
1186 creating the stub name takes a bit of time. */
1187 static struct elf32_nios2_stub_hash_entry
*
1188 nios2_get_stub_entry (const asection
*input_section
,
1189 const asection
*sym_sec
,
1190 struct elf32_nios2_link_hash_entry
*hh
,
1191 const Elf_Internal_Rela
*rel
,
1192 struct elf32_nios2_link_hash_table
*htab
,
1193 enum elf32_nios2_stub_type stub_type
)
1195 struct elf32_nios2_stub_hash_entry
*hsh
;
1196 const asection
*id_sec
;
1198 /* If this input section is part of a group of sections sharing one
1199 stub section, then use the id of the first/last section in the group,
1200 depending on the stub section placement relative to the group.
1201 Stub names need to include a section id, as there may well be
1202 more than one stub used to reach say, printf, and we need to
1203 distinguish between them. */
1204 if (stub_type
== nios2_stub_call26_before
)
1205 id_sec
= htab
->stub_group
[input_section
->id
].first_sec
;
1207 id_sec
= htab
->stub_group
[input_section
->id
].last_sec
;
1209 if (hh
!= NULL
&& hh
->hsh_cache
!= NULL
1210 && hh
->hsh_cache
->hh
== hh
1211 && hh
->hsh_cache
->id_sec
== id_sec
1212 && hh
->hsh_cache
->stub_type
== stub_type
)
1214 hsh
= hh
->hsh_cache
;
1220 stub_name
= nios2_stub_name (id_sec
, sym_sec
, hh
, rel
, stub_type
);
1221 if (stub_name
== NULL
)
1224 hsh
= nios2_stub_hash_lookup (&htab
->bstab
,
1225 stub_name
, FALSE
, FALSE
);
1228 hh
->hsh_cache
= hsh
;
1236 /* Add a new stub entry to the stub hash. Not all fields of the new
1237 stub entry are initialised. */
1238 static struct elf32_nios2_stub_hash_entry
*
1239 nios2_add_stub (const char *stub_name
,
1241 struct elf32_nios2_link_hash_table
*htab
,
1242 enum elf32_nios2_stub_type stub_type
)
1246 asection
**secptr
, **linkptr
;
1247 struct elf32_nios2_stub_hash_entry
*hsh
;
1250 if (stub_type
== nios2_stub_call26_before
)
1252 link_sec
= htab
->stub_group
[section
->id
].first_sec
;
1253 secptr
= &(htab
->stub_group
[section
->id
].first_stub_sec
);
1254 linkptr
= &(htab
->stub_group
[link_sec
->id
].first_stub_sec
);
1259 link_sec
= htab
->stub_group
[section
->id
].last_sec
;
1260 secptr
= &(htab
->stub_group
[section
->id
].last_stub_sec
);
1261 linkptr
= &(htab
->stub_group
[link_sec
->id
].last_stub_sec
);
1265 if (stub_sec
== NULL
)
1267 stub_sec
= *linkptr
;
1268 if (stub_sec
== NULL
)
1274 namelen
= strlen (link_sec
->name
);
1275 len
= namelen
+ sizeof (STUB_SUFFIX
);
1276 s_name
= bfd_alloc (htab
->stub_bfd
, len
);
1280 memcpy (s_name
, link_sec
->name
, namelen
);
1281 memcpy (s_name
+ namelen
, STUB_SUFFIX
, sizeof (STUB_SUFFIX
));
1283 stub_sec
= (*htab
->add_stub_section
) (s_name
, link_sec
, afterp
);
1284 if (stub_sec
== NULL
)
1286 *linkptr
= stub_sec
;
1291 /* Enter this entry into the linker stub hash table. */
1292 hsh
= nios2_stub_hash_lookup (&htab
->bstab
, stub_name
,
1296 (*_bfd_error_handler
) (_("%B: cannot create stub entry %s"),
1302 hsh
->stub_sec
= stub_sec
;
1303 hsh
->stub_offset
= 0;
1304 hsh
->id_sec
= link_sec
;
1308 /* Set up various things so that we can make a list of input sections
1309 for each output section included in the link. Returns -1 on error,
1310 0 when no stubs will be needed, and 1 on success. */
1312 nios2_elf32_setup_section_lists (bfd
*output_bfd
, struct bfd_link_info
*info
)
1315 unsigned int bfd_count
;
1316 int top_id
, top_index
;
1318 asection
**input_list
, **list
;
1320 struct elf32_nios2_link_hash_table
*htab
= elf32_nios2_hash_table (info
);
1322 /* Count the number of input BFDs and find the top input section id. */
1323 for (input_bfd
= info
->input_bfds
, bfd_count
= 0, top_id
= 0;
1325 input_bfd
= input_bfd
->link_next
)
1328 for (section
= input_bfd
->sections
;
1330 section
= section
->next
)
1332 if (top_id
< section
->id
)
1333 top_id
= section
->id
;
1337 htab
->bfd_count
= bfd_count
;
1339 amt
= sizeof (struct map_stub
) * (top_id
+ 1);
1340 htab
->stub_group
= bfd_zmalloc (amt
);
1341 if (htab
->stub_group
== NULL
)
1344 /* We can't use output_bfd->section_count here to find the top output
1345 section index as some sections may have been removed, and
1346 strip_excluded_output_sections doesn't renumber the indices. */
1347 for (section
= output_bfd
->sections
, top_index
= 0;
1349 section
= section
->next
)
1351 if (top_index
< section
->index
)
1352 top_index
= section
->index
;
1355 htab
->top_index
= top_index
;
1356 amt
= sizeof (asection
*) * (top_index
+ 1);
1357 input_list
= bfd_malloc (amt
);
1358 htab
->input_list
= input_list
;
1359 if (input_list
== NULL
)
1362 /* For sections we aren't interested in, mark their entries with a
1363 value we can check later. */
1364 list
= input_list
+ top_index
;
1366 *list
= bfd_abs_section_ptr
;
1367 while (list
-- != input_list
);
1369 for (section
= output_bfd
->sections
;
1371 section
= section
->next
)
1373 /* FIXME: This is a bit of hack. Currently our .ctors and .dtors
1374 * have PC relative relocs in them but no code flag set. */
1375 if (((section
->flags
& SEC_CODE
) != 0) ||
1376 strcmp(".ctors", section
->name
) ||
1377 strcmp(".dtors", section
->name
))
1378 input_list
[section
->index
] = NULL
;
1384 /* The linker repeatedly calls this function for each input section,
1385 in the order that input sections are linked into output sections.
1386 Build lists of input sections to determine groupings between which
1387 we may insert linker stubs. */
1389 nios2_elf32_next_input_section (struct bfd_link_info
*info
, asection
*isec
)
1391 struct elf32_nios2_link_hash_table
*htab
= elf32_nios2_hash_table (info
);
1393 if (isec
->output_section
->index
<= htab
->top_index
)
1395 asection
**list
= htab
->input_list
+ isec
->output_section
->index
;
1396 if (*list
!= bfd_abs_section_ptr
)
1398 /* Steal the last_sec pointer for our list.
1399 This happens to make the list in reverse order,
1400 which is what we want. */
1401 htab
->stub_group
[isec
->id
].last_sec
= *list
;
1407 /* Segment mask for CALL26 relocation relaxation. */
1408 #define CALL26_SEGMENT(x) ((x) & 0xf0000000)
1410 /* Fudge factor for approximate maximum size of all stubs that might
1411 be inserted by the linker. This does not actually limit the number
1412 of stubs that might be inserted, and only affects strategy for grouping
1413 and placement of stubs. Perhaps this should be computed based on number
1414 of relocations seen, or be specifiable on the command line. */
1415 #define MAX_STUB_SECTION_SIZE 0xffff
1417 /* See whether we can group stub sections together. Grouping stub
1418 sections may result in fewer stubs. More importantly, we need to
1419 put all .init* and .fini* stubs at the end of the .init or
1420 .fini output sections respectively, because glibc splits the
1421 _init and _fini functions into multiple parts. Putting a stub in
1422 the middle of a function is not a good idea.
1423 Rather than computing groups of a maximum fixed size, for Nios II
1424 CALL26 relaxation it makes more sense to compute the groups based on
1425 sections that fit within a 256MB address segment. Also do not allow
1426 a group to span more than one output section, since different output
1427 sections might correspond to different memory banks on a bare-metal
1430 group_sections (struct elf32_nios2_link_hash_table
*htab
)
1432 asection
**list
= htab
->input_list
+ htab
->top_index
;
1435 /* The list is in reverse order so we'll search backwards looking
1436 for the first section that begins in the same memory segment,
1437 marking sections along the way to point at the tail for this
1439 asection
*tail
= *list
;
1440 if (tail
== bfd_abs_section_ptr
)
1442 while (tail
!= NULL
)
1444 bfd_vma start
= tail
->output_section
->vma
+ tail
->output_offset
;
1445 bfd_vma end
= start
+ tail
->size
;
1446 bfd_vma segment
= CALL26_SEGMENT (end
);
1449 if (segment
!= CALL26_SEGMENT (start
)
1450 || segment
!= CALL26_SEGMENT (end
+ MAX_STUB_SECTION_SIZE
))
1451 /* This section spans more than one memory segment, or is
1452 close enough to the end of the segment that adding stub
1453 sections before it might cause it to move so that it
1454 spans memory segments, or that stubs added at the end of
1455 this group might overflow into the next memory segment.
1456 Put it in a group by itself to localize the effects. */
1458 prev
= htab
->stub_group
[tail
->id
].last_sec
;
1459 htab
->stub_group
[tail
->id
].last_sec
= tail
;
1460 htab
->stub_group
[tail
->id
].first_sec
= tail
;
1463 /* Collect more sections for this group. */
1465 asection
*curr
, *first
;
1466 for (curr
= tail
; ; curr
= prev
)
1468 prev
= htab
->stub_group
[curr
->id
].last_sec
;
1470 || tail
->output_section
!= prev
->output_section
1471 || (CALL26_SEGMENT (prev
->output_section
->vma
1472 + prev
->output_offset
)
1477 for (curr
= tail
; ; curr
= prev
)
1479 prev
= htab
->stub_group
[curr
->id
].last_sec
;
1480 htab
->stub_group
[curr
->id
].last_sec
= tail
;
1481 htab
->stub_group
[curr
->id
].first_sec
= first
;
1487 /* Reset tail for the next group. */
1491 while (list
-- != htab
->input_list
);
1492 free (htab
->input_list
);
1495 /* Determine the type of stub needed, if any, for a call. */
1496 static enum elf32_nios2_stub_type
1497 nios2_type_of_stub (asection
*input_sec
,
1498 const Elf_Internal_Rela
*rel
,
1499 struct elf32_nios2_link_hash_entry
*hh
,
1500 struct elf32_nios2_link_hash_table
*htab
,
1501 bfd_vma destination
,
1502 struct bfd_link_info
*info ATTRIBUTE_UNUSED
)
1504 bfd_vma location
, segment
, start
, end
;
1505 asection
*s0
, *s1
, *s
;
1508 !(hh
->root
.root
.type
== bfd_link_hash_defined
1509 || hh
->root
.root
.type
== bfd_link_hash_defweak
))
1510 return nios2_stub_none
;
1512 /* Determine where the call point is. */
1513 location
= (input_sec
->output_section
->vma
1514 + input_sec
->output_offset
+ rel
->r_offset
);
1515 segment
= CALL26_SEGMENT (location
);
1517 /* Nios II CALL and JMPI instructions can transfer control to addresses
1518 within the same 256MB segment as the PC. */
1519 if (segment
== CALL26_SEGMENT (destination
))
1520 return nios2_stub_none
;
1522 /* Find the start and end addresses of the stub group. Also account for
1523 any already-created stub sections for this group. Note that for stubs
1524 in the end section, only the first instruction of the last stub
1525 (12 bytes long) needs to be within range. */
1526 s0
= htab
->stub_group
[input_sec
->id
].first_sec
;
1527 s
= htab
->stub_group
[s0
->id
].first_stub_sec
;
1528 if (s
!= NULL
&& s
->size
> 0)
1529 start
= s
->output_section
->vma
+ s
->output_offset
;
1531 start
= s0
->output_section
->vma
+ s0
->output_offset
;
1533 s1
= htab
->stub_group
[input_sec
->id
].last_sec
;
1534 s
= htab
->stub_group
[s1
->id
].last_stub_sec
;
1535 if (s
!= NULL
&& s
->size
> 0)
1536 end
= s
->output_section
->vma
+ s
->output_offset
+ s
->size
- 8;
1538 end
= s1
->output_section
->vma
+ s1
->output_offset
+ s1
->size
;
1540 BFD_ASSERT (start
< end
);
1541 BFD_ASSERT (start
<= location
);
1542 BFD_ASSERT (location
< end
);
1544 /* Put stubs at the end of the group unless that is not a valid
1545 location and the beginning of the group is. It might be that
1546 neither the beginning nor end works if we have an input section
1547 so large that it spans multiple segment boundaries. In that
1548 case, punt; the end result will be a relocation overflow error no
1549 matter what we do here.
1551 Note that adding stubs pushes up the addresses of all subsequent
1552 sections, so that stubs allocated on one pass through the
1553 relaxation loop may not be valid on the next pass. (E.g., we may
1554 allocate a stub at the beginning of the section on one pass and
1555 find that the call site has been bumped into the next memory
1556 segment on the next pass.) The important thing to note is that
1557 we never try to reclaim the space allocated to such unused stubs,
1558 so code size and section addresses can only increase with each
1559 iteration. Accounting for the start and end addresses of the
1560 already-created stub sections ensures that when the algorithm
1561 converges, it converges accurately, with the entire appropriate
1562 stub section accessible from the call site and not just the
1563 address at the start or end of the stub group proper. */
1565 if (segment
== CALL26_SEGMENT (end
))
1566 return nios2_stub_call26_after
;
1567 else if (segment
== CALL26_SEGMENT (start
))
1568 return nios2_stub_call26_before
;
1570 /* Perhaps this should be a dedicated error code. */
1571 return nios2_stub_none
;
1575 nios2_build_one_stub (struct bfd_hash_entry
*gen_entry
, void *in_arg ATTRIBUTE_UNUSED
)
1577 struct elf32_nios2_stub_hash_entry
*hsh
1578 = (struct elf32_nios2_stub_hash_entry
*) gen_entry
;
1579 asection
*stub_sec
= hsh
->stub_sec
;
1582 /* Make a note of the offset within the stubs for this entry. */
1583 hsh
->stub_offset
= stub_sec
->size
;
1585 switch (hsh
->stub_type
)
1587 case nios2_stub_call26_before
:
1588 case nios2_stub_call26_after
:
1589 /* A call26 stub looks like:
1590 orhi at, %hiadj(dest)
1591 addi at, at, %lo(dest)
1593 Note that call/jmpi instructions can't be used in PIC code
1594 so there is no reason for the stub to be PIC, either. */
1595 sym_value
= (hsh
->target_value
1596 + hsh
->target_section
->output_offset
1597 + hsh
->target_section
->output_section
->vma
1600 nios2_elf32_install_data (stub_sec
, nios2_call26_stub_entry
,
1601 hsh
->stub_offset
, 3);
1602 nios2_elf32_install_imm16 (stub_sec
, hsh
->stub_offset
,
1604 nios2_elf32_install_imm16 (stub_sec
, hsh
->stub_offset
+ 4,
1605 (sym_value
& 0xffff));
1606 stub_sec
->size
+= 12;
1616 /* As above, but don't actually build the stub. Just bump offset so
1617 we know stub section sizes. */
1619 nios2_size_one_stub (struct bfd_hash_entry
*gen_entry
, void *in_arg ATTRIBUTE_UNUSED
)
1621 struct elf32_nios2_stub_hash_entry
*hsh
1622 = (struct elf32_nios2_stub_hash_entry
*) gen_entry
;
1624 switch (hsh
->stub_type
)
1626 case nios2_stub_call26_before
:
1627 case nios2_stub_call26_after
:
1628 hsh
->stub_sec
->size
+= 12;
1637 /* Read in all local syms for all input bfds.
1638 Returns -1 on error, 0 otherwise. */
1641 get_local_syms (bfd
*output_bfd ATTRIBUTE_UNUSED
, bfd
*input_bfd
,
1642 struct bfd_link_info
*info
)
1644 unsigned int bfd_indx
;
1645 Elf_Internal_Sym
*local_syms
, **all_local_syms
;
1646 struct elf32_nios2_link_hash_table
*htab
= elf32_nios2_hash_table (info
);
1648 /* We want to read in symbol extension records only once. To do this
1649 we need to read in the local symbols in parallel and save them for
1650 later use; so hold pointers to the local symbols in an array. */
1651 bfd_size_type amt
= sizeof (Elf_Internal_Sym
*) * htab
->bfd_count
;
1652 all_local_syms
= bfd_zmalloc (amt
);
1653 htab
->all_local_syms
= all_local_syms
;
1654 if (all_local_syms
== NULL
)
1657 /* Walk over all the input BFDs, swapping in local symbols. */
1660 input_bfd
= input_bfd
->link_next
, bfd_indx
++)
1662 Elf_Internal_Shdr
*symtab_hdr
;
1664 /* We'll need the symbol table in a second. */
1665 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
1666 if (symtab_hdr
->sh_info
== 0)
1669 /* We need an array of the local symbols attached to the input bfd. */
1670 local_syms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
1671 if (local_syms
== NULL
)
1673 local_syms
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
,
1674 symtab_hdr
->sh_info
, 0,
1676 /* Cache them for elf_link_input_bfd. */
1677 symtab_hdr
->contents
= (unsigned char *) local_syms
;
1679 if (local_syms
== NULL
)
1682 all_local_syms
[bfd_indx
] = local_syms
;
1688 /* Determine and set the size of the stub section for a final link. */
1690 nios2_elf32_size_stubs (bfd
*output_bfd
, bfd
*stub_bfd
,
1691 struct bfd_link_info
*info
,
1692 asection
*(*add_stub_section
) (const char *,
1693 asection
*, bfd_boolean
),
1694 void (*layout_sections_again
) (void))
1696 bfd_boolean stub_changed
= FALSE
;
1697 struct elf32_nios2_link_hash_table
*htab
= elf32_nios2_hash_table (info
);
1699 /* Stash our params away. */
1700 htab
->stub_bfd
= stub_bfd
;
1701 htab
->add_stub_section
= add_stub_section
;
1702 htab
->layout_sections_again
= layout_sections_again
;
1704 /* FIXME: We only compute the section groups once. This could cause
1705 problems if adding a large stub section causes following sections,
1706 or parts of them, to move into another segment. However, this seems
1707 to be consistent with the way other back ends handle this.... */
1708 group_sections (htab
);
1710 if (get_local_syms (output_bfd
, info
->input_bfds
, info
))
1712 if (htab
->all_local_syms
)
1713 goto error_ret_free_local
;
1720 unsigned int bfd_indx
;
1723 for (input_bfd
= info
->input_bfds
, bfd_indx
= 0;
1725 input_bfd
= input_bfd
->link_next
, bfd_indx
++)
1727 Elf_Internal_Shdr
*symtab_hdr
;
1729 Elf_Internal_Sym
*local_syms
;
1731 /* We'll need the symbol table in a second. */
1732 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
1733 if (symtab_hdr
->sh_info
== 0)
1736 local_syms
= htab
->all_local_syms
[bfd_indx
];
1738 /* Walk over each section attached to the input bfd. */
1739 for (section
= input_bfd
->sections
;
1741 section
= section
->next
)
1743 Elf_Internal_Rela
*internal_relocs
, *irelaend
, *irela
;
1745 /* If there aren't any relocs, then there's nothing more
1747 if ((section
->flags
& SEC_RELOC
) == 0
1748 || section
->reloc_count
== 0)
1751 /* If this section is a link-once section that will be
1752 discarded, then don't create any stubs. */
1753 if (section
->output_section
== NULL
1754 || section
->output_section
->owner
!= output_bfd
)
1757 /* Get the relocs. */
1759 = _bfd_elf_link_read_relocs (input_bfd
, section
, NULL
, NULL
,
1761 if (internal_relocs
== NULL
)
1762 goto error_ret_free_local
;
1764 /* Now examine each relocation. */
1765 irela
= internal_relocs
;
1766 irelaend
= irela
+ section
->reloc_count
;
1767 for (; irela
< irelaend
; irela
++)
1769 unsigned int r_type
, r_indx
;
1770 enum elf32_nios2_stub_type stub_type
;
1771 struct elf32_nios2_stub_hash_entry
*hsh
;
1774 bfd_vma destination
;
1775 struct elf32_nios2_link_hash_entry
*hh
;
1777 const asection
*id_sec
;
1779 r_type
= ELF32_R_TYPE (irela
->r_info
);
1780 r_indx
= ELF32_R_SYM (irela
->r_info
);
1782 if (r_type
>= (unsigned int) R_NIOS2_ILLEGAL
)
1784 bfd_set_error (bfd_error_bad_value
);
1785 error_ret_free_internal
:
1786 if (elf_section_data (section
)->relocs
== NULL
)
1787 free (internal_relocs
);
1788 goto error_ret_free_local
;
1791 /* Only look for stubs on CALL and JMPI instructions. */
1792 if (r_type
!= (unsigned int) R_NIOS2_CALL26
)
1795 /* Now determine the call target, its name, value,
1801 if (r_indx
< symtab_hdr
->sh_info
)
1803 /* It's a local symbol. */
1804 Elf_Internal_Sym
*sym
;
1805 Elf_Internal_Shdr
*hdr
;
1808 sym
= local_syms
+ r_indx
;
1809 if (ELF_ST_TYPE (sym
->st_info
) != STT_SECTION
)
1810 sym_value
= sym
->st_value
;
1811 shndx
= sym
->st_shndx
;
1812 if (shndx
< elf_numsections (input_bfd
))
1814 hdr
= elf_elfsections (input_bfd
)[shndx
];
1815 sym_sec
= hdr
->bfd_section
;
1816 destination
= (sym_value
+ irela
->r_addend
1817 + sym_sec
->output_offset
1818 + sym_sec
->output_section
->vma
);
1823 /* It's an external symbol. */
1826 e_indx
= r_indx
- symtab_hdr
->sh_info
;
1827 hh
= ((struct elf32_nios2_link_hash_entry
*)
1828 elf_sym_hashes (input_bfd
)[e_indx
]);
1830 while (hh
->root
.root
.type
== bfd_link_hash_indirect
1831 || hh
->root
.root
.type
== bfd_link_hash_warning
)
1832 hh
= ((struct elf32_nios2_link_hash_entry
*)
1833 hh
->root
.root
.u
.i
.link
);
1835 if (hh
->root
.root
.type
== bfd_link_hash_defined
1836 || hh
->root
.root
.type
== bfd_link_hash_defweak
)
1838 sym_sec
= hh
->root
.root
.u
.def
.section
;
1839 sym_value
= hh
->root
.root
.u
.def
.value
;
1841 if (sym_sec
->output_section
!= NULL
)
1842 destination
= (sym_value
+ irela
->r_addend
1843 + sym_sec
->output_offset
1844 + sym_sec
->output_section
->vma
);
1848 else if (hh
->root
.root
.type
== bfd_link_hash_undefweak
)
1853 else if (hh
->root
.root
.type
== bfd_link_hash_undefined
)
1855 if (! (info
->unresolved_syms_in_objects
== RM_IGNORE
1856 && (ELF_ST_VISIBILITY (hh
->root
.other
)
1862 bfd_set_error (bfd_error_bad_value
);
1863 goto error_ret_free_internal
;
1867 /* Determine what (if any) linker stub is needed. */
1868 stub_type
= nios2_type_of_stub (section
, irela
, hh
, htab
,
1870 if (stub_type
== nios2_stub_none
)
1873 /* Support for grouping stub sections. */
1874 if (stub_type
== nios2_stub_call26_before
)
1875 id_sec
= htab
->stub_group
[section
->id
].first_sec
;
1877 id_sec
= htab
->stub_group
[section
->id
].last_sec
;
1879 /* Get the name of this stub. */
1880 stub_name
= nios2_stub_name (id_sec
, sym_sec
, hh
, irela
,
1883 goto error_ret_free_internal
;
1885 hsh
= nios2_stub_hash_lookup (&htab
->bstab
,
1890 /* The proper stub has already been created. */
1895 hsh
= nios2_add_stub (stub_name
, section
, htab
, stub_type
);
1899 goto error_ret_free_internal
;
1901 hsh
->target_value
= sym_value
;
1902 hsh
->target_section
= sym_sec
;
1903 hsh
->stub_type
= stub_type
;
1905 hsh
->addend
= irela
->r_addend
;
1906 stub_changed
= TRUE
;
1909 /* We're done with the internal relocs, free them. */
1910 if (elf_section_data (section
)->relocs
== NULL
)
1911 free (internal_relocs
);
1918 /* OK, we've added some stubs. Find out the new size of the
1920 for (stub_sec
= htab
->stub_bfd
->sections
;
1922 stub_sec
= stub_sec
->next
)
1925 bfd_hash_traverse (&htab
->bstab
, nios2_size_one_stub
, htab
);
1927 /* Ask the linker to do its stuff. */
1928 (*htab
->layout_sections_again
) ();
1929 stub_changed
= FALSE
;
1932 free (htab
->all_local_syms
);
1935 error_ret_free_local
:
1936 free (htab
->all_local_syms
);
1940 /* Build all the stubs associated with the current output file. The
1941 stubs are kept in a hash table attached to the main linker hash
1942 table. This function is called via nios2elf_finish in the linker. */
1944 nios2_elf32_build_stubs (struct bfd_link_info
*info
)
1947 struct bfd_hash_table
*table
;
1948 struct elf32_nios2_link_hash_table
*htab
;
1950 htab
= elf32_nios2_hash_table (info
);
1952 for (stub_sec
= htab
->stub_bfd
->sections
;
1954 stub_sec
= stub_sec
->next
)
1958 /* Allocate memory to hold the linker stubs. */
1959 size
= stub_sec
->size
;
1960 stub_sec
->contents
= bfd_zalloc (htab
->stub_bfd
, size
);
1961 if (stub_sec
->contents
== NULL
&& size
!= 0)
1966 /* Build the stubs as directed by the stub hash table. */
1967 table
= &htab
->bstab
;
1968 bfd_hash_traverse (table
, nios2_build_one_stub
, info
);
1974 /* Implement bfd_elf32_bfd_reloc_type_lookup:
1975 Given a BFD reloc type, return a howto structure. */
1976 static reloc_howto_type
*
1977 nios2_elf32_bfd_reloc_type_lookup (bfd
*abfd ATTRIBUTE_UNUSED
,
1978 bfd_reloc_code_real_type code
)
1982 i
< (int) (sizeof (nios2_reloc_map
) / sizeof (struct elf_reloc_map
));
1984 if (nios2_reloc_map
[i
].bfd_val
== code
)
1985 return &elf_nios2_howto_table_rel
[(int) nios2_reloc_map
[i
].elf_val
];
1989 /* Implement bfd_elf32_bfd_reloc_name_lookup:
1990 Given a reloc name, return a howto structure. */
1991 static reloc_howto_type
*
1992 nios2_elf32_bfd_reloc_name_lookup (bfd
*abfd ATTRIBUTE_UNUSED
,
1997 i
< (sizeof (elf_nios2_howto_table_rel
)
1998 / sizeof (elf_nios2_howto_table_rel
[0]));
2000 if (elf_nios2_howto_table_rel
[i
].name
2001 && strcasecmp (elf_nios2_howto_table_rel
[i
].name
, r_name
) == 0)
2002 return &elf_nios2_howto_table_rel
[i
];
2007 /* Implement elf_info_to_howto:
2008 Given a ELF32 relocation, fill in a arelent structure. */
2010 nios2_elf32_info_to_howto (bfd
*abfd ATTRIBUTE_UNUSED
, arelent
*cache_ptr
,
2011 Elf_Internal_Rela
*dst
)
2013 unsigned int r_type
;
2015 r_type
= ELF32_R_TYPE (dst
->r_info
);
2016 BFD_ASSERT (r_type
< R_NIOS2_ILLEGAL
);
2017 cache_ptr
->howto
= &elf_nios2_howto_table_rel
[r_type
];
2020 /* Return the base VMA address which should be subtracted from real addresses
2021 when resolving @dtpoff relocation.
2022 This is PT_TLS segment p_vaddr. */
2024 dtpoff_base (struct bfd_link_info
*info
)
2026 /* If tls_sec is NULL, we should have signalled an error already. */
2027 if (elf_hash_table (info
)->tls_sec
== NULL
)
2029 return elf_hash_table (info
)->tls_sec
->vma
;
2032 /* Return the relocation value for @tpoff relocation
2033 if STT_TLS virtual address is ADDRESS. */
2035 tpoff (struct bfd_link_info
*info
, bfd_vma address
)
2037 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
2039 /* If tls_sec is NULL, we should have signalled an error already. */
2040 if (htab
->tls_sec
== NULL
)
2042 return address
- htab
->tls_sec
->vma
;
2045 /* Set the GP value for OUTPUT_BFD. Returns FALSE if this is a
2046 dangerous relocation. */
2048 nios2_elf_assign_gp (bfd
*output_bfd
, bfd_vma
*pgp
, struct bfd_link_info
*info
)
2051 bfd_boolean gp_found
;
2052 struct bfd_hash_entry
*h
;
2053 struct bfd_link_hash_entry
*lh
;
2055 /* If we've already figured out what GP will be, just return it. */
2056 *pgp
= _bfd_get_gp_value (output_bfd
);
2060 h
= bfd_hash_lookup (&info
->hash
->table
, "_gp", FALSE
, FALSE
);
2061 lh
= (struct bfd_link_hash_entry
*) h
;
2067 case bfd_link_hash_undefined
:
2068 case bfd_link_hash_undefweak
:
2069 case bfd_link_hash_common
:
2072 case bfd_link_hash_defined
:
2073 case bfd_link_hash_defweak
:
2075 *pgp
= lh
->u
.def
.value
;
2077 case bfd_link_hash_indirect
:
2078 case bfd_link_hash_warning
:
2080 /* @@FIXME ignoring warning for now */
2082 case bfd_link_hash_new
:
2092 /* Only get the error once. */
2094 _bfd_set_gp_value (output_bfd
, *pgp
);
2098 _bfd_set_gp_value (output_bfd
, *pgp
);
2103 /* Retrieve the previously cached _gp pointer, returning bfd_reloc_dangerous
2104 if it's not available as we don't have a link_info pointer available here
2105 to look it up in the output symbol table. We don't need to adjust the
2106 symbol value for an external symbol if we are producing relocatable
2108 static bfd_reloc_status_type
2109 nios2_elf_final_gp (bfd
*output_bfd
, asymbol
*symbol
, bfd_boolean relocatable
,
2110 char **error_message
, bfd_vma
*pgp
)
2112 if (bfd_is_und_section (symbol
->section
) && !relocatable
)
2115 return bfd_reloc_undefined
;
2118 *pgp
= _bfd_get_gp_value (output_bfd
);
2119 if (*pgp
== 0 && (!relocatable
|| (symbol
->flags
& BSF_SECTION_SYM
) != 0))
2123 /* Make up a value. */
2124 *pgp
= symbol
->section
->output_section
->vma
+ 0x4000;
2125 _bfd_set_gp_value (output_bfd
, *pgp
);
2130 = (char *) _("global pointer relative relocation when _gp not defined");
2131 return bfd_reloc_dangerous
;
2135 return bfd_reloc_ok
;
2138 /* Do the relocations that require special handling. */
2139 static bfd_reloc_status_type
2140 nios2_elf32_do_hi16_relocate (bfd
*abfd
, reloc_howto_type
*howto
,
2141 asection
*input_section
,
2142 bfd_byte
*data
, bfd_vma offset
,
2143 bfd_vma symbol_value
, bfd_vma addend
)
2145 symbol_value
= symbol_value
+ addend
;
2147 symbol_value
= (symbol_value
>> 16) & 0xffff;
2148 return _bfd_final_link_relocate (howto
, abfd
, input_section
,
2149 data
, offset
, symbol_value
, addend
);
2152 static bfd_reloc_status_type
2153 nios2_elf32_do_lo16_relocate (bfd
*abfd
, reloc_howto_type
*howto
,
2154 asection
*input_section
,
2155 bfd_byte
*data
, bfd_vma offset
,
2156 bfd_vma symbol_value
, bfd_vma addend
)
2158 symbol_value
= symbol_value
+ addend
;
2160 symbol_value
= symbol_value
& 0xffff;
2161 return _bfd_final_link_relocate (howto
, abfd
, input_section
,
2162 data
, offset
, symbol_value
, addend
);
2165 static bfd_reloc_status_type
2166 nios2_elf32_do_hiadj16_relocate (bfd
*abfd
, reloc_howto_type
*howto
,
2167 asection
*input_section
,
2168 bfd_byte
*data
, bfd_vma offset
,
2169 bfd_vma symbol_value
, bfd_vma addend
)
2171 symbol_value
= symbol_value
+ addend
;
2173 symbol_value
= hiadj(symbol_value
);
2174 return _bfd_final_link_relocate (howto
, abfd
, input_section
, data
, offset
,
2175 symbol_value
, addend
);
2178 static bfd_reloc_status_type
2179 nios2_elf32_do_pcrel_lo16_relocate (bfd
*abfd
, reloc_howto_type
*howto
,
2180 asection
*input_section
,
2181 bfd_byte
*data
, bfd_vma offset
,
2182 bfd_vma symbol_value
, bfd_vma addend
)
2184 symbol_value
= symbol_value
+ addend
;
2186 symbol_value
= symbol_value
& 0xffff;
2187 return _bfd_final_link_relocate (howto
, abfd
, input_section
,
2188 data
, offset
, symbol_value
, addend
);
2191 static bfd_reloc_status_type
2192 nios2_elf32_do_pcrel_hiadj16_relocate (bfd
*abfd
, reloc_howto_type
*howto
,
2193 asection
*input_section
,
2194 bfd_byte
*data
, bfd_vma offset
,
2195 bfd_vma symbol_value
, bfd_vma addend
)
2197 symbol_value
= symbol_value
+ addend
;
2198 symbol_value
-= (input_section
->output_section
->vma
2199 + input_section
->output_offset
);
2200 symbol_value
-= offset
;
2202 symbol_value
= hiadj(symbol_value
);
2203 return _bfd_final_link_relocate (howto
, abfd
, input_section
, data
, offset
,
2204 symbol_value
, addend
);
2207 static bfd_reloc_status_type
2208 nios2_elf32_do_pcrel16_relocate (bfd
*abfd
, reloc_howto_type
*howto
,
2209 asection
*input_section
,
2210 bfd_byte
*data
, bfd_vma offset
,
2211 bfd_vma symbol_value
, bfd_vma addend
)
2213 /* NIOS2 pc relative relocations are relative to the next 32-bit instruction
2214 so we need to subtract 4 before doing a final_link_relocate. */
2215 symbol_value
= symbol_value
+ addend
- 4;
2217 return _bfd_final_link_relocate (howto
, abfd
, input_section
,
2218 data
, offset
, symbol_value
, addend
);
2221 static bfd_reloc_status_type
2222 nios2_elf32_do_call26_relocate (bfd
*abfd
, reloc_howto_type
*howto
,
2223 asection
*input_section
,
2224 bfd_byte
*data
, bfd_vma offset
,
2225 bfd_vma symbol_value
, bfd_vma addend
)
2227 /* Check that the relocation is in the same page as the current address. */
2228 if (CALL26_SEGMENT (symbol_value
+ addend
)
2229 != CALL26_SEGMENT (input_section
->output_section
->vma
2230 + input_section
->output_offset
2232 return bfd_reloc_overflow
;
2234 return _bfd_final_link_relocate (howto
, abfd
, input_section
,
2235 data
, offset
, symbol_value
, addend
);
2238 static bfd_reloc_status_type
2239 nios2_elf32_do_gprel_relocate (bfd
*abfd
, reloc_howto_type
*howto
,
2240 asection
*input_section
,
2241 bfd_byte
*data
, bfd_vma offset
,
2242 bfd_vma symbol_value
, bfd_vma addend
)
2244 /* Because we need the output_bfd, the special handling is done
2245 in nios2_elf32_relocate_section or in nios2_elf32_gprel_relocate. */
2246 return _bfd_final_link_relocate (howto
, abfd
, input_section
,
2247 data
, offset
, symbol_value
, addend
);
2250 static bfd_reloc_status_type
2251 nios2_elf32_do_ujmp_relocate (bfd
*abfd
, reloc_howto_type
*howto
,
2252 asection
*input_section
,
2253 bfd_byte
*data
, bfd_vma offset
,
2254 bfd_vma symbol_value
, bfd_vma addend
)
2256 bfd_vma symbol_lo16
, symbol_hi16
;
2257 bfd_reloc_status_type r
;
2258 symbol_value
= symbol_value
+ addend
;
2260 symbol_hi16
= (symbol_value
>> 16) & 0xffff;
2261 symbol_lo16
= symbol_value
& 0xffff;
2263 r
= _bfd_final_link_relocate (howto
, abfd
, input_section
,
2264 data
, offset
, symbol_hi16
, addend
);
2266 if (r
== bfd_reloc_ok
)
2267 return _bfd_final_link_relocate (howto
, abfd
, input_section
,
2268 data
, offset
+ 4, symbol_lo16
, addend
);
2273 static bfd_reloc_status_type
2274 nios2_elf32_do_cjmp_relocate (bfd
*abfd
, reloc_howto_type
*howto
,
2275 asection
*input_section
,
2276 bfd_byte
*data
, bfd_vma offset
,
2277 bfd_vma symbol_value
, bfd_vma addend
)
2279 bfd_vma symbol_lo16
, symbol_hi16
;
2280 bfd_reloc_status_type r
;
2281 symbol_value
= symbol_value
+ addend
;
2283 symbol_hi16
= (symbol_value
>> 16) & 0xffff;
2284 symbol_lo16
= symbol_value
& 0xffff;
2286 r
= _bfd_final_link_relocate (howto
, abfd
, input_section
,
2287 data
, offset
, symbol_hi16
, addend
);
2289 if (r
== bfd_reloc_ok
)
2290 return _bfd_final_link_relocate (howto
, abfd
, input_section
,
2291 data
, offset
+ 4, symbol_lo16
, addend
);
2296 static bfd_reloc_status_type
2297 nios2_elf32_do_callr_relocate (bfd
*abfd
, reloc_howto_type
*howto
,
2298 asection
*input_section
,
2299 bfd_byte
*data
, bfd_vma offset
,
2300 bfd_vma symbol_value
, bfd_vma addend
)
2302 bfd_vma symbol_lo16
, symbol_hi16
;
2303 bfd_reloc_status_type r
;
2304 symbol_value
= symbol_value
+ addend
;
2306 symbol_hi16
= (symbol_value
>> 16) & 0xffff;
2307 symbol_lo16
= symbol_value
& 0xffff;
2309 r
= _bfd_final_link_relocate (howto
, abfd
, input_section
,
2310 data
, offset
, symbol_hi16
, addend
);
2312 if (r
== bfd_reloc_ok
)
2313 return _bfd_final_link_relocate (howto
, abfd
, input_section
,
2314 data
, offset
+ 4, symbol_lo16
, addend
);
2319 /* HOWTO handlers for relocations that require special handling. */
2321 /* This is for relocations used only when relaxing to ensure
2322 changes in size of section don't screw up .align. */
2323 static bfd_reloc_status_type
2324 nios2_elf32_ignore_reloc (bfd
*abfd ATTRIBUTE_UNUSED
, arelent
*reloc_entry
,
2325 asymbol
*symbol ATTRIBUTE_UNUSED
,
2326 void *data ATTRIBUTE_UNUSED
, asection
*input_section
,
2328 char **error_message ATTRIBUTE_UNUSED
)
2330 if (output_bfd
!= NULL
)
2331 reloc_entry
->address
+= input_section
->output_offset
;
2332 return bfd_reloc_ok
;
2335 static bfd_reloc_status_type
2336 nios2_elf32_hi16_relocate (bfd
*abfd
, arelent
*reloc_entry
, asymbol
*symbol
,
2337 void *data
, asection
*input_section
,
2339 char **error_message ATTRIBUTE_UNUSED
)
2341 /* This part is from bfd_elf_generic_reloc. */
2342 if (output_bfd
!= NULL
2343 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
2344 && (!reloc_entry
->howto
->partial_inplace
|| reloc_entry
->addend
== 0))
2346 reloc_entry
->address
+= input_section
->output_offset
;
2347 return bfd_reloc_ok
;
2350 if (output_bfd
!= NULL
)
2351 /* FIXME: See bfd_perform_relocation. Is this right? */
2352 return bfd_reloc_continue
;
2354 return nios2_elf32_do_hi16_relocate (abfd
, reloc_entry
->howto
,
2356 data
, reloc_entry
->address
,
2358 + symbol
->section
->output_section
->vma
2359 + symbol
->section
->output_offset
),
2360 reloc_entry
->addend
);
2363 static bfd_reloc_status_type
2364 nios2_elf32_lo16_relocate (bfd
*abfd
, arelent
*reloc_entry
, asymbol
*symbol
,
2365 void *data
, asection
*input_section
,
2367 char **error_message ATTRIBUTE_UNUSED
)
2369 /* This part is from bfd_elf_generic_reloc. */
2370 if (output_bfd
!= NULL
2371 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
2372 && (!reloc_entry
->howto
->partial_inplace
|| reloc_entry
->addend
== 0))
2374 reloc_entry
->address
+= input_section
->output_offset
;
2375 return bfd_reloc_ok
;
2378 if (output_bfd
!= NULL
)
2379 /* FIXME: See bfd_perform_relocation. Is this right? */
2380 return bfd_reloc_continue
;
2382 return nios2_elf32_do_lo16_relocate (abfd
, reloc_entry
->howto
,
2384 data
, reloc_entry
->address
,
2386 + symbol
->section
->output_section
->vma
2387 + symbol
->section
->output_offset
),
2388 reloc_entry
->addend
);
2391 static bfd_reloc_status_type
2392 nios2_elf32_hiadj16_relocate (bfd
*abfd
, arelent
*reloc_entry
, asymbol
*symbol
,
2393 void *data
, asection
*input_section
,
2395 char **error_message ATTRIBUTE_UNUSED
)
2397 /* This part is from bfd_elf_generic_reloc. */
2398 if (output_bfd
!= NULL
2399 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
2400 && (!reloc_entry
->howto
->partial_inplace
|| reloc_entry
->addend
== 0))
2402 reloc_entry
->address
+= input_section
->output_offset
;
2403 return bfd_reloc_ok
;
2406 if (output_bfd
!= NULL
)
2407 /* FIXME: See bfd_perform_relocation. Is this right? */
2408 return bfd_reloc_continue
;
2410 return nios2_elf32_do_hiadj16_relocate (abfd
, reloc_entry
->howto
,
2412 data
, reloc_entry
->address
,
2414 + symbol
->section
->output_section
->vma
2415 + symbol
->section
->output_offset
),
2416 reloc_entry
->addend
);
2419 static bfd_reloc_status_type
2420 nios2_elf32_pcrel_lo16_relocate (bfd
*abfd
, arelent
*reloc_entry
,
2421 asymbol
*symbol
, void *data
,
2422 asection
*input_section
, bfd
*output_bfd
,
2423 char **error_message ATTRIBUTE_UNUSED
)
2425 /* This part is from bfd_elf_generic_reloc. */
2426 if (output_bfd
!= NULL
2427 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
2428 && (!reloc_entry
->howto
->partial_inplace
|| reloc_entry
->addend
== 0))
2430 reloc_entry
->address
+= input_section
->output_offset
;
2431 return bfd_reloc_ok
;
2434 if (output_bfd
!= NULL
)
2435 /* FIXME: See bfd_perform_relocation. Is this right? */
2436 return bfd_reloc_continue
;
2438 return nios2_elf32_do_pcrel_lo16_relocate (
2439 abfd
, reloc_entry
->howto
, input_section
, data
, reloc_entry
->address
,
2440 (symbol
->value
+ symbol
->section
->output_section
->vma
2441 + symbol
->section
->output_offset
),
2442 reloc_entry
->addend
);
2445 static bfd_reloc_status_type
2446 nios2_elf32_pcrel_hiadj16_relocate (bfd
*abfd
, arelent
*reloc_entry
,
2447 asymbol
*symbol
, void *data
,
2448 asection
*input_section
, bfd
*output_bfd
,
2449 char **error_message ATTRIBUTE_UNUSED
)
2451 /* This part is from bfd_elf_generic_reloc. */
2452 if (output_bfd
!= NULL
2453 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
2454 && (!reloc_entry
->howto
->partial_inplace
|| reloc_entry
->addend
== 0))
2456 reloc_entry
->address
+= input_section
->output_offset
;
2457 return bfd_reloc_ok
;
2460 if (output_bfd
!= NULL
)
2461 /* FIXME: See bfd_perform_relocation. Is this right? */
2462 return bfd_reloc_continue
;
2464 return nios2_elf32_do_pcrel_hiadj16_relocate (
2465 abfd
, reloc_entry
->howto
, input_section
, data
, reloc_entry
->address
,
2466 (symbol
->value
+ symbol
->section
->output_section
->vma
2467 + symbol
->section
->output_offset
),
2468 reloc_entry
->addend
);
2471 static bfd_reloc_status_type
2472 nios2_elf32_pcrel16_relocate (bfd
*abfd
, arelent
*reloc_entry
, asymbol
*symbol
,
2473 void *data
, asection
*input_section
,
2475 char **error_message ATTRIBUTE_UNUSED
)
2477 /* This part is from bfd_elf_generic_reloc. */
2478 if (output_bfd
!= NULL
2479 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
2480 && (!reloc_entry
->howto
->partial_inplace
|| reloc_entry
->addend
== 0))
2482 reloc_entry
->address
+= input_section
->output_offset
;
2483 return bfd_reloc_ok
;
2486 if (output_bfd
!= NULL
)
2487 /* FIXME: See bfd_perform_relocation. Is this right? */
2488 return bfd_reloc_continue
;
2490 return nios2_elf32_do_pcrel16_relocate (abfd
, reloc_entry
->howto
,
2492 data
, reloc_entry
->address
,
2494 + symbol
->section
->output_section
->vma
2495 + symbol
->section
->output_offset
),
2496 reloc_entry
->addend
);
2499 static bfd_reloc_status_type
2500 nios2_elf32_call26_relocate (bfd
*abfd
, arelent
*reloc_entry
, asymbol
*symbol
,
2501 void *data
, asection
*input_section
,
2503 char **error_message ATTRIBUTE_UNUSED
)
2505 /* This part is from bfd_elf_generic_reloc. */
2506 if (output_bfd
!= NULL
2507 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
2508 && (!reloc_entry
->howto
->partial_inplace
|| reloc_entry
->addend
== 0))
2510 reloc_entry
->address
+= input_section
->output_offset
;
2511 return bfd_reloc_ok
;
2514 if (output_bfd
!= NULL
)
2515 /* FIXME: See bfd_perform_relocation. Is this right? */
2516 return bfd_reloc_continue
;
2518 return nios2_elf32_do_call26_relocate (abfd
, reloc_entry
->howto
,
2520 data
, reloc_entry
->address
,
2522 + symbol
->section
->output_section
->vma
2523 + symbol
->section
->output_offset
),
2524 reloc_entry
->addend
);
2527 static bfd_reloc_status_type
2528 nios2_elf32_gprel_relocate (bfd
*abfd
, arelent
*reloc_entry
, asymbol
*symbol
,
2529 void *data
, asection
*input_section
,
2530 bfd
*output_bfd
, char **msg
)
2534 bfd_reloc_status_type r
;
2537 /* This part is from bfd_elf_generic_reloc. */
2538 if (output_bfd
!= NULL
2539 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
2540 && (!reloc_entry
->howto
->partial_inplace
|| reloc_entry
->addend
== 0))
2542 reloc_entry
->address
+= input_section
->output_offset
;
2543 return bfd_reloc_ok
;
2546 if (output_bfd
!= NULL
)
2547 /* FIXME: See bfd_perform_relocation. Is this right? */
2548 return bfd_reloc_continue
;
2550 relocation
= (symbol
->value
2551 + symbol
->section
->output_section
->vma
2552 + symbol
->section
->output_offset
);
2554 /* This assumes we've already cached the _gp symbol. */
2555 r
= nios2_elf_final_gp (abfd
, symbol
, FALSE
, msg
, &gp
);
2556 if (r
== bfd_reloc_ok
)
2558 relocation
= relocation
+ reloc_entry
->addend
- gp
;
2559 reloc_entry
->addend
= 0;
2560 if ((signed) relocation
< -32768 || (signed) relocation
> 32767)
2562 *msg
= _("global pointer relative address out of range");
2563 r
= bfd_reloc_outofrange
;
2566 r
= nios2_elf32_do_gprel_relocate (abfd
, reloc_entry
->howto
,
2568 data
, reloc_entry
->address
,
2569 relocation
, reloc_entry
->addend
);
2575 static bfd_reloc_status_type
2576 nios2_elf32_ujmp_relocate (bfd
*abfd
, arelent
*reloc_entry
, asymbol
*symbol
,
2577 void *data
, asection
*input_section
,
2578 bfd
*output_bfd
, char **msg ATTRIBUTE_UNUSED
)
2580 /* This part is from bfd_elf_generic_reloc. */
2581 if (output_bfd
!= NULL
2582 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
2583 && (!reloc_entry
->howto
->partial_inplace
|| reloc_entry
->addend
== 0))
2585 reloc_entry
->address
+= input_section
->output_offset
;
2586 return bfd_reloc_ok
;
2589 if (output_bfd
!= NULL
)
2590 /* FIXME: See bfd_perform_relocation. Is this right? */
2591 return bfd_reloc_continue
;
2593 return nios2_elf32_do_ujmp_relocate (abfd
, reloc_entry
->howto
,
2595 data
, reloc_entry
->address
,
2597 + symbol
->section
->output_section
->vma
2598 + symbol
->section
->output_offset
),
2599 reloc_entry
->addend
);
2602 static bfd_reloc_status_type
2603 nios2_elf32_cjmp_relocate (bfd
*abfd
, arelent
*reloc_entry
, asymbol
*symbol
,
2604 void *data
, asection
*input_section
,
2605 bfd
*output_bfd
, char **msg ATTRIBUTE_UNUSED
)
2607 /* This part is from bfd_elf_generic_reloc. */
2608 if (output_bfd
!= NULL
2609 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
2610 && (!reloc_entry
->howto
->partial_inplace
|| reloc_entry
->addend
== 0))
2612 reloc_entry
->address
+= input_section
->output_offset
;
2613 return bfd_reloc_ok
;
2616 if (output_bfd
!= NULL
)
2617 /* FIXME: See bfd_perform_relocation. Is this right? */
2618 return bfd_reloc_continue
;
2620 return nios2_elf32_do_cjmp_relocate (abfd
, reloc_entry
->howto
,
2622 data
, reloc_entry
->address
,
2624 + symbol
->section
->output_section
->vma
2625 + symbol
->section
->output_offset
),
2626 reloc_entry
->addend
);
2629 static bfd_reloc_status_type
2630 nios2_elf32_callr_relocate (bfd
*abfd
, arelent
*reloc_entry
, asymbol
*symbol
,
2631 void *data
, asection
*input_section
,
2632 bfd
*output_bfd
, char **msg ATTRIBUTE_UNUSED
)
2634 /* This part is from bfd_elf_generic_reloc. */
2635 if (output_bfd
!= NULL
2636 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
2637 && (!reloc_entry
->howto
->partial_inplace
|| reloc_entry
->addend
== 0))
2639 reloc_entry
->address
+= input_section
->output_offset
;
2640 return bfd_reloc_ok
;
2643 if (output_bfd
!= NULL
)
2644 /* FIXME: See bfd_perform_relocation. Is this right? */
2645 return bfd_reloc_continue
;
2647 return nios2_elf32_do_callr_relocate (abfd
, reloc_entry
->howto
,
2649 data
, reloc_entry
->address
,
2651 + symbol
->section
->output_section
->vma
2652 + symbol
->section
->output_offset
),
2653 reloc_entry
->addend
);
2657 /* Implement elf_backend_relocate_section. */
2659 nios2_elf32_relocate_section (bfd
*output_bfd
,
2660 struct bfd_link_info
*info
,
2662 asection
*input_section
,
2664 Elf_Internal_Rela
*relocs
,
2665 Elf_Internal_Sym
*local_syms
,
2666 asection
**local_sections
)
2668 Elf_Internal_Shdr
*symtab_hdr
;
2669 struct elf_link_hash_entry
**sym_hashes
;
2670 Elf_Internal_Rela
*rel
;
2671 Elf_Internal_Rela
*relend
;
2672 struct elf32_nios2_link_hash_table
*htab
;
2675 asection
*sreloc
= NULL
;
2676 bfd_vma
*local_got_offsets
;
2679 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
2680 sym_hashes
= elf_sym_hashes (input_bfd
);
2681 relend
= relocs
+ input_section
->reloc_count
;
2683 htab
= elf32_nios2_hash_table (info
);
2684 sgot
= htab
->root
.sgot
;
2685 splt
= htab
->root
.splt
;
2686 local_got_offsets
= elf_local_got_offsets (input_bfd
);
2688 if (elf32_nios2_hash_table (info
)->h_gp_got
== NULL
)
2691 got_base
= elf32_nios2_hash_table (info
)->h_gp_got
->root
.u
.def
.value
;
2693 for (rel
= relocs
; rel
< relend
; rel
++)
2695 reloc_howto_type
*howto
;
2696 unsigned long r_symndx
;
2697 Elf_Internal_Sym
*sym
;
2699 struct elf_link_hash_entry
*h
;
2700 struct elf32_nios2_link_hash_entry
*eh
;
2703 bfd_vma reloc_address
;
2704 bfd_reloc_status_type r
= bfd_reloc_ok
;
2705 const char *name
= NULL
;
2709 const char* msg
= (const char*) NULL
;
2710 bfd_boolean unresolved_reloc
;
2714 r_type
= ELF32_R_TYPE (rel
->r_info
);
2715 r_symndx
= ELF32_R_SYM (rel
->r_info
);
2717 howto
= lookup_howto ((unsigned) ELF32_R_TYPE (rel
->r_info
));
2722 if (r_symndx
< symtab_hdr
->sh_info
)
2724 sym
= local_syms
+ r_symndx
;
2725 sec
= local_sections
[r_symndx
];
2726 relocation
= _bfd_elf_rela_local_sym (output_bfd
, sym
, &sec
, rel
);
2730 bfd_boolean warned
, ignored
;
2732 RELOC_FOR_GLOBAL_SYMBOL (info
, input_bfd
, input_section
, rel
,
2733 r_symndx
, symtab_hdr
, sym_hashes
,
2735 unresolved_reloc
, warned
, ignored
);
2738 if (sec
&& discarded_section (sec
))
2739 RELOC_AGAINST_DISCARDED_SECTION (info
, input_bfd
, input_section
,
2740 rel
, 1, relend
, howto
, 0, contents
);
2742 /* Nothing more to do unless this is a final link. */
2743 if (info
->relocatable
)
2746 if (sec
&& sec
->output_section
)
2747 reloc_address
= (sec
->output_section
->vma
+ sec
->output_offset
2754 switch (howto
->type
)
2757 r
= nios2_elf32_do_hi16_relocate (input_bfd
, howto
,
2759 contents
, rel
->r_offset
,
2760 relocation
, rel
->r_addend
);
2763 r
= nios2_elf32_do_lo16_relocate (input_bfd
, howto
,
2765 contents
, rel
->r_offset
,
2766 relocation
, rel
->r_addend
);
2768 case R_NIOS2_PCREL_LO
:
2769 r
= nios2_elf32_do_pcrel_lo16_relocate (input_bfd
, howto
,
2776 case R_NIOS2_HIADJ16
:
2777 r
= nios2_elf32_do_hiadj16_relocate (input_bfd
, howto
,
2778 input_section
, contents
,
2779 rel
->r_offset
, relocation
,
2782 case R_NIOS2_PCREL_HA
:
2783 r
= nios2_elf32_do_pcrel_hiadj16_relocate (input_bfd
, howto
,
2790 case R_NIOS2_PCREL16
:
2791 r
= nios2_elf32_do_pcrel16_relocate (input_bfd
, howto
,
2792 input_section
, contents
,
2793 rel
->r_offset
, relocation
,
2797 /* Turns an absolute address into a gp-relative address. */
2798 if (!nios2_elf_assign_gp (output_bfd
, &gp
, info
))
2800 format
= _("global pointer relative relocation at address "
2801 "0x%08x when _gp not defined\n");
2802 sprintf (msgbuf
, format
, reloc_address
);
2804 r
= bfd_reloc_dangerous
;
2808 bfd_vma symbol_address
= rel
->r_addend
+ relocation
;
2809 relocation
= relocation
+ rel
->r_addend
- gp
;
2811 if (((signed) relocation
< -32768
2812 || (signed) relocation
> 32767)
2814 || h
->root
.type
== bfd_link_hash_defined
2815 || h
->root
.type
== bfd_link_hash_defweak
))
2817 format
= _("Unable to reach %s (at 0x%08x) from the "
2818 "global pointer (at 0x%08x) because the "
2819 "offset (%d) is out of the allowed range, "
2820 "-32678 to 32767.\n" );
2821 sprintf (msgbuf
, format
, name
, symbol_address
, gp
,
2822 (signed)relocation
);
2824 r
= bfd_reloc_outofrange
;
2827 r
= _bfd_final_link_relocate (howto
, input_bfd
,
2828 input_section
, contents
,
2829 rel
->r_offset
, relocation
,
2835 r
= nios2_elf32_do_ujmp_relocate (input_bfd
, howto
,
2837 contents
, rel
->r_offset
,
2838 relocation
, rel
->r_addend
);
2841 r
= nios2_elf32_do_cjmp_relocate (input_bfd
, howto
,
2843 contents
, rel
->r_offset
,
2844 relocation
, rel
->r_addend
);
2847 r
= nios2_elf32_do_callr_relocate (input_bfd
, howto
,
2848 input_section
, contents
,
2849 rel
->r_offset
, relocation
,
2852 case R_NIOS2_CALL26
:
2853 case R_NIOS2_CALL26_NOAT
:
2854 /* If we have a call to an undefined weak symbol, we just want
2855 to stuff a zero in the bits of the call instruction and
2856 bypass the normal call26 relocation handling, because it'll
2857 diagnose an overflow error if address 0 isn't in the same
2858 256MB segment as the call site. Presumably the call
2859 should be guarded by a null check anyway. */
2860 if (h
!= NULL
&& h
->root
.type
== bfd_link_hash_undefweak
)
2862 BFD_ASSERT (relocation
== 0 && rel
->r_addend
== 0);
2863 r
= _bfd_final_link_relocate (howto
, input_bfd
,
2864 input_section
, contents
,
2865 rel
->r_offset
, relocation
,
2869 /* Handle relocations which should use the PLT entry.
2870 NIOS2_BFD_RELOC_32 relocations will use the symbol's value,
2871 which may point to a PLT entry, but we don't need to handle
2872 that here. If we created a PLT entry, all branches in this
2873 object should go to it. */
2874 if (h
!= NULL
&& splt
!= NULL
&& h
->plt
.offset
!= (bfd_vma
) -1)
2876 /* If we've created a .plt section, and assigned a PLT entry
2877 to this function, it should not be known to bind locally.
2878 If it were, we would have cleared the PLT entry. */
2879 BFD_ASSERT (!SYMBOL_CALLS_LOCAL (info
, h
));
2881 relocation
= (splt
->output_section
->vma
2882 + splt
->output_offset
2885 unresolved_reloc
= FALSE
;
2887 /* Detect R_NIOS2_CALL26 relocations that would overflow the
2888 256MB segment. Replace the target with a reference to a
2890 Note that htab->stub_group is null if relaxation has been
2891 disabled by the --no-relax linker command-line option, so
2892 we can use that to skip this processing entirely. */
2893 if (howto
->type
== R_NIOS2_CALL26
&& htab
->stub_group
)
2895 bfd_vma dest
= relocation
+ rel
->r_addend
;
2896 enum elf32_nios2_stub_type stub_type
;
2898 eh
= (struct elf32_nios2_link_hash_entry
*)h
;
2899 stub_type
= nios2_type_of_stub (input_section
, rel
, eh
,
2902 if (stub_type
!= nios2_stub_none
)
2904 struct elf32_nios2_stub_hash_entry
*hsh
;
2906 hsh
= nios2_get_stub_entry (input_section
, sec
,
2907 eh
, rel
, htab
, stub_type
);
2910 r
= bfd_reloc_undefined
;
2914 dest
= (hsh
->stub_offset
2915 + hsh
->stub_sec
->output_offset
2916 + hsh
->stub_sec
->output_section
->vma
);
2917 r
= nios2_elf32_do_call26_relocate (input_bfd
, howto
,
2927 r
= nios2_elf32_do_call26_relocate (input_bfd
, howto
,
2928 input_section
, contents
,
2929 rel
->r_offset
, relocation
,
2934 /* For symmetry this would be
2935 r = nios2_elf32_do_ignore_reloc (input_bfd, howto,
2936 input_section, contents,
2937 rel->r_offset, relocation,
2939 but do_ignore_reloc would do no more than return
2944 case R_NIOS2_CALL16
:
2945 /* Relocation is to the entry for this symbol in the
2946 global offset table. */
2949 r
= bfd_reloc_notsupported
;
2959 eh
= (struct elf32_nios2_link_hash_entry
*)h
;
2960 use_plt
= (eh
->got_types_used
== CALL16_USED
2961 && h
->plt
.offset
!= (bfd_vma
) -1);
2963 off
= h
->got
.offset
;
2964 BFD_ASSERT (off
!= (bfd_vma
) -1);
2965 dyn
= elf_hash_table (info
)->dynamic_sections_created
;
2966 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, info
->shared
, h
)
2968 && SYMBOL_REFERENCES_LOCAL (info
, h
))
2969 || (ELF_ST_VISIBILITY (h
->other
)
2970 && h
->root
.type
== bfd_link_hash_undefweak
))
2972 /* This is actually a static link, or it is a -Bsymbolic
2973 link and the symbol is defined locally. We must
2974 initialize this entry in the global offset table.
2975 Since the offset must always be a multiple of 4, we
2976 use the least significant bit to record whether we
2977 have initialized it already.
2979 When doing a dynamic link, we create a .rela.got
2980 relocation entry to initialize the value. This is
2981 done in the finish_dynamic_symbol routine. */
2986 bfd_put_32 (output_bfd
, relocation
,
2987 sgot
->contents
+ off
);
2992 unresolved_reloc
= FALSE
;
2996 BFD_ASSERT (local_got_offsets
!= NULL
2997 && local_got_offsets
[r_symndx
] != (bfd_vma
) -1);
2999 off
= local_got_offsets
[r_symndx
];
3001 /* The offset must always be a multiple of 4. We use the
3002 least significant bit to record whether we have already
3003 generated the necessary reloc. */
3008 bfd_put_32 (output_bfd
, relocation
,
3009 sgot
->contents
+ off
);
3014 Elf_Internal_Rela outrel
;
3017 srelgot
= htab
->root
.srelgot
;
3018 BFD_ASSERT (srelgot
!= NULL
);
3020 outrel
.r_addend
= relocation
;
3021 outrel
.r_offset
= (sgot
->output_section
->vma
3022 + sgot
->output_offset
3024 outrel
.r_info
= ELF32_R_INFO (0, R_NIOS2_RELATIVE
);
3025 loc
= srelgot
->contents
;
3026 loc
+= (srelgot
->reloc_count
++ *
3027 sizeof (Elf32_External_Rela
));
3028 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
, loc
);
3031 local_got_offsets
[r_symndx
] |= 1;
3035 if (use_plt
&& info
->shared
)
3037 off
= ((h
->plt
.offset
- 24) / 12 + 3) * 4;
3038 relocation
= (htab
->root
.sgotplt
->output_offset
+ off
3042 relocation
= sgot
->output_offset
+ off
- got_base
;
3044 /* This relocation does not use the addend. */
3047 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
3048 contents
, rel
->r_offset
,
3049 relocation
, rel
->r_addend
);
3052 case R_NIOS2_GOTOFF_LO
:
3053 case R_NIOS2_GOTOFF_HA
:
3054 case R_NIOS2_GOTOFF
:
3055 /* Relocation is relative to the global offset table pointer. */
3057 BFD_ASSERT (sgot
!= NULL
);
3060 r
= bfd_reloc_notsupported
;
3064 /* Adjust the relocation to be relative to the GOT pointer. */
3065 relocation
-= (sgot
->output_section
->vma
3066 + sgot
->output_offset
- got_base
);
3068 switch (howto
->type
)
3070 case R_NIOS2_GOTOFF_LO
:
3071 r
= nios2_elf32_do_lo16_relocate (input_bfd
, howto
,
3072 input_section
, contents
,
3073 rel
->r_offset
, relocation
,
3076 case R_NIOS2_GOTOFF_HA
:
3077 r
= nios2_elf32_do_hiadj16_relocate (input_bfd
, howto
,
3078 input_section
, contents
,
3084 r
= _bfd_final_link_relocate (howto
, input_bfd
,
3085 input_section
, contents
,
3086 rel
->r_offset
, relocation
,
3092 case R_NIOS2_TLS_LDO16
:
3093 relocation
-= dtpoff_base (info
) + DTP_OFFSET
;
3095 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
3096 contents
, rel
->r_offset
,
3097 relocation
, rel
->r_addend
);
3099 case R_NIOS2_TLS_LDM16
:
3100 if (htab
->root
.sgot
== NULL
)
3103 off
= htab
->tls_ldm_got
.offset
;
3109 /* If we don't know the module number, create a relocation
3113 Elf_Internal_Rela outrel
;
3116 if (htab
->root
.srelgot
== NULL
)
3119 outrel
.r_addend
= 0;
3120 outrel
.r_offset
= (htab
->root
.sgot
->output_section
->vma
3121 + htab
->root
.sgot
->output_offset
3123 outrel
.r_info
= ELF32_R_INFO (0, R_NIOS2_TLS_DTPMOD
);
3125 loc
= htab
->root
.srelgot
->contents
;
3126 loc
+= (htab
->root
.srelgot
->reloc_count
++
3127 * sizeof (Elf32_External_Rela
));
3128 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
, loc
);
3131 bfd_put_32 (output_bfd
, 1,
3132 htab
->root
.sgot
->contents
+ off
);
3134 htab
->tls_ldm_got
.offset
|= 1;
3137 relocation
= htab
->root
.sgot
->output_offset
+ off
- got_base
;
3139 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
3140 contents
, rel
->r_offset
,
3141 relocation
, rel
->r_addend
);
3144 case R_NIOS2_TLS_GD16
:
3145 case R_NIOS2_TLS_IE16
:
3150 if (htab
->root
.sgot
== NULL
)
3157 dyn
= htab
->root
.dynamic_sections_created
;
3158 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, info
->shared
, h
)
3160 || !SYMBOL_REFERENCES_LOCAL (info
, h
)))
3162 unresolved_reloc
= FALSE
;
3165 off
= h
->got
.offset
;
3166 tls_type
= (((struct elf32_nios2_link_hash_entry
*) h
)
3171 if (local_got_offsets
== NULL
)
3173 off
= local_got_offsets
[r_symndx
];
3174 tls_type
= (elf32_nios2_local_got_tls_type (input_bfd
)
3178 if (tls_type
== GOT_UNKNOWN
)
3185 bfd_boolean need_relocs
= FALSE
;
3186 Elf_Internal_Rela outrel
;
3187 bfd_byte
*loc
= NULL
;
3190 /* The GOT entries have not been initialized yet. Do it
3191 now, and emit any relocations. If both an IE GOT and a
3192 GD GOT are necessary, we emit the GD first. */
3194 if ((info
->shared
|| indx
!= 0)
3196 || ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
3197 || h
->root
.type
!= bfd_link_hash_undefweak
))
3200 if (htab
->root
.srelgot
== NULL
)
3202 loc
= htab
->root
.srelgot
->contents
;
3203 loc
+= (htab
->root
.srelgot
->reloc_count
*
3204 sizeof (Elf32_External_Rela
));
3207 if (tls_type
& GOT_TLS_GD
)
3211 outrel
.r_addend
= 0;
3212 outrel
.r_offset
= (htab
->root
.sgot
->output_section
->vma
3213 + htab
->root
.sgot
->output_offset
3215 outrel
.r_info
= ELF32_R_INFO (indx
,
3216 R_NIOS2_TLS_DTPMOD
);
3218 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
,
3220 htab
->root
.srelgot
->reloc_count
++;
3221 loc
+= sizeof (Elf32_External_Rela
);
3224 bfd_put_32 (output_bfd
,
3225 (relocation
- dtpoff_base (info
) -
3227 htab
->root
.sgot
->contents
+ cur_off
+ 4);
3230 outrel
.r_addend
= 0;
3231 outrel
.r_info
= ELF32_R_INFO (indx
,
3232 R_NIOS2_TLS_DTPREL
);
3233 outrel
.r_offset
+= 4;
3235 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
,
3237 htab
->root
.srelgot
->reloc_count
++;
3238 loc
+= sizeof (Elf32_External_Rela
);
3243 /* If we are not emitting relocations for a
3244 general dynamic reference, then we must be in a
3245 static link or an executable link with the
3246 symbol binding locally. Mark it as belonging
3247 to module 1, the executable. */
3248 bfd_put_32 (output_bfd
, 1,
3249 htab
->root
.sgot
->contents
+ cur_off
);
3250 bfd_put_32 (output_bfd
, (relocation
-
3251 dtpoff_base (info
) -
3253 htab
->root
.sgot
->contents
+ cur_off
+ 4);
3259 if (tls_type
& GOT_TLS_IE
)
3264 outrel
.r_addend
= (relocation
-
3265 dtpoff_base (info
));
3267 outrel
.r_addend
= 0;
3268 outrel
.r_offset
= (htab
->root
.sgot
->output_section
->vma
3269 + htab
->root
.sgot
->output_offset
3271 outrel
.r_info
= ELF32_R_INFO (indx
,
3274 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
,
3276 htab
->root
.srelgot
->reloc_count
++;
3277 loc
+= sizeof (Elf32_External_Rela
);
3280 bfd_put_32 (output_bfd
, (tpoff (info
, relocation
)
3282 htab
->root
.sgot
->contents
+ cur_off
);
3289 local_got_offsets
[r_symndx
] |= 1;
3292 if ((tls_type
& GOT_TLS_GD
) && r_type
!= R_NIOS2_TLS_GD16
)
3294 relocation
= htab
->root
.sgot
->output_offset
+ off
- got_base
;
3296 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
3297 contents
, rel
->r_offset
,
3298 relocation
, rel
->r_addend
);
3302 case R_NIOS2_TLS_LE16
:
3303 if (info
->shared
&& !info
->pie
)
3305 (*_bfd_error_handler
)
3306 (_("%B(%A+0x%lx): R_NIOS2_TLS_LE16 relocation not "
3307 "permitted in shared object"),
3308 input_bfd
, input_section
,
3309 (long) rel
->r_offset
, howto
->name
);
3313 relocation
= tpoff (info
, relocation
) - TP_OFFSET
;
3315 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
3316 contents
, rel
->r_offset
,
3317 relocation
, rel
->r_addend
);
3320 case R_NIOS2_BFD_RELOC_32
:
3322 && (input_section
->flags
& SEC_ALLOC
) != 0
3324 || ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
3325 || h
->root
.type
!= bfd_link_hash_undefweak
))
3327 Elf_Internal_Rela outrel
;
3329 bfd_boolean skip
, relocate
;
3331 /* When generating a shared object, these relocations
3332 are copied into the output file to be resolved at run
3339 = _bfd_elf_section_offset (output_bfd
, info
,
3340 input_section
, rel
->r_offset
);
3341 if (outrel
.r_offset
== (bfd_vma
) -1)
3343 else if (outrel
.r_offset
== (bfd_vma
) -2)
3344 skip
= TRUE
, relocate
= TRUE
;
3345 outrel
.r_offset
+= (input_section
->output_section
->vma
3346 + input_section
->output_offset
);
3349 memset (&outrel
, 0, sizeof outrel
);
3354 || !h
->def_regular
))
3356 outrel
.r_info
= ELF32_R_INFO (h
->dynindx
, r_type
);
3357 outrel
.r_addend
= rel
->r_addend
;
3361 /* This symbol is local, or marked to become local. */
3362 outrel
.r_addend
= relocation
+ rel
->r_addend
;
3364 outrel
.r_info
= ELF32_R_INFO (0, R_NIOS2_RELATIVE
);
3367 sreloc
= elf_section_data (input_section
)->sreloc
;
3371 loc
= sreloc
->contents
;
3372 loc
+= sreloc
->reloc_count
++ * sizeof (Elf32_External_Rela
);
3373 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
, loc
);
3375 /* This reloc will be computed at runtime, so there's no
3376 need to do anything now, except for R_NIOS2_BFD_RELOC_32
3377 relocations that have been turned into
3378 R_NIOS2_RELATIVE. */
3383 r
= _bfd_final_link_relocate (howto
, input_bfd
,
3384 input_section
, contents
,
3385 rel
->r_offset
, relocation
,
3389 case R_NIOS2_TLS_DTPREL
:
3390 relocation
-= dtpoff_base (info
);
3394 r
= _bfd_final_link_relocate (howto
, input_bfd
,
3395 input_section
, contents
,
3396 rel
->r_offset
, relocation
,
3402 r
= bfd_reloc_notsupported
;
3404 if (r
!= bfd_reloc_ok
)
3407 name
= h
->root
.root
.string
;
3410 name
= bfd_elf_string_from_elf_section (input_bfd
,
3411 symtab_hdr
->sh_link
,
3413 if (name
== NULL
|| *name
== '\0')
3414 name
= bfd_section_name (input_bfd
, sec
);
3419 case bfd_reloc_overflow
:
3420 r
= info
->callbacks
->reloc_overflow (info
, NULL
, name
,
3421 howto
->name
, (bfd_vma
) 0,
3422 input_bfd
, input_section
,
3426 case bfd_reloc_undefined
:
3427 r
= info
->callbacks
->undefined_symbol (info
, name
, input_bfd
,
3429 rel
->r_offset
, TRUE
);
3432 case bfd_reloc_outofrange
:
3434 msg
= _("relocation out of range");
3437 case bfd_reloc_notsupported
:
3439 msg
= _("unsupported relocation");
3442 case bfd_reloc_dangerous
:
3444 msg
= _("dangerous relocation");
3449 msg
= _("unknown error");
3455 r
= info
->callbacks
->warning
3456 (info
, msg
, name
, input_bfd
, input_section
, rel
->r_offset
);
3464 /* Implement elf-backend_section_flags:
3465 Convert NIOS2 specific section flags to bfd internal section flags. */
3467 nios2_elf32_section_flags (flagword
*flags
, const Elf_Internal_Shdr
*hdr
)
3469 if (hdr
->sh_flags
& SHF_NIOS2_GPREL
)
3470 *flags
|= SEC_SMALL_DATA
;
3475 /* Implement elf_backend_fake_sections:
3476 Set the correct type for an NIOS2 ELF section. We do this by the
3477 section name, which is a hack, but ought to work. */
3479 nios2_elf32_fake_sections (bfd
*abfd ATTRIBUTE_UNUSED
,
3480 Elf_Internal_Shdr
*hdr
, asection
*sec
)
3482 register const char *name
= bfd_get_section_name (abfd
, sec
);
3484 if ((sec
->flags
& SEC_SMALL_DATA
)
3485 || strcmp (name
, ".sdata") == 0
3486 || strcmp (name
, ".sbss") == 0
3487 || strcmp (name
, ".lit4") == 0 || strcmp (name
, ".lit8") == 0)
3488 hdr
->sh_flags
|= SHF_NIOS2_GPREL
;
3493 /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
3494 shortcuts to them in our hash table. */
3496 create_got_section (bfd
*dynobj
, struct bfd_link_info
*info
)
3498 struct elf32_nios2_link_hash_table
*htab
;
3499 struct elf_link_hash_entry
*h
;
3501 htab
= elf32_nios2_hash_table (info
);
3503 if (! _bfd_elf_create_got_section (dynobj
, info
))
3506 /* In order for the two loads in .PLTresolve to share the same %hiadj,
3507 _GLOBAL_OFFSET_TABLE_ must be aligned to a 16-byte boundary. */
3508 if (!bfd_set_section_alignment (dynobj
, htab
->root
.sgotplt
, 4))
3511 /* The Nios II ABI specifies that GOT-relative relocations are relative
3512 to the linker-created symbol _gp_got, rather than using
3513 _GLOBAL_OFFSET_TABLE_ directly. In particular, the latter always
3514 points to the base of the GOT while _gp_got may include a bias. */
3515 h
= _bfd_elf_define_linkage_sym (dynobj
, info
, htab
->root
.sgotplt
,
3517 elf32_nios2_hash_table (info
)->h_gp_got
= h
;
3524 /* Implement elf_backend_create_dynamic_sections:
3525 Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
3526 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
3529 nios2_elf32_create_dynamic_sections (bfd
*dynobj
, struct bfd_link_info
*info
)
3531 struct elf32_nios2_link_hash_table
*htab
;
3533 htab
= elf32_nios2_hash_table (info
);
3534 if (!htab
->root
.sgot
&& !create_got_section (dynobj
, info
))
3537 _bfd_elf_create_dynamic_sections (dynobj
, info
);
3539 /* In order for the two loads in a shared object .PLTresolve to share the
3540 same %hiadj, the start of the PLT (as well as the GOT) must be aligned
3541 to a 16-byte boundary. This is because the addresses for these loads
3542 include the -(.plt+4) PIC correction. */
3543 if (!bfd_set_section_alignment (dynobj
, htab
->root
.splt
, 4))
3546 htab
->sdynbss
= bfd_get_linker_section (dynobj
, ".dynbss");
3551 htab
->srelbss
= bfd_get_linker_section (dynobj
, ".rela.bss");
3559 /* Implement elf_backend_copy_indirect_symbol:
3560 Copy the extra info we tack onto an elf_link_hash_entry. */
3562 nios2_elf32_copy_indirect_symbol (struct bfd_link_info
*info
,
3563 struct elf_link_hash_entry
*dir
,
3564 struct elf_link_hash_entry
*ind
)
3566 struct elf32_nios2_link_hash_entry
*edir
, *eind
;
3568 edir
= (struct elf32_nios2_link_hash_entry
*) dir
;
3569 eind
= (struct elf32_nios2_link_hash_entry
*) ind
;
3571 if (eind
->dyn_relocs
!= NULL
)
3573 if (edir
->dyn_relocs
!= NULL
)
3575 struct elf32_nios2_dyn_relocs
**pp
;
3576 struct elf32_nios2_dyn_relocs
*p
;
3578 /* Add reloc counts against the indirect sym to the direct sym
3579 list. Merge any entries against the same section. */
3580 for (pp
= &eind
->dyn_relocs
; (p
= *pp
) != NULL
; )
3582 struct elf32_nios2_dyn_relocs
*q
;
3584 for (q
= edir
->dyn_relocs
; q
!= NULL
; q
= q
->next
)
3585 if (q
->sec
== p
->sec
)
3587 q
->pc_count
+= p
->pc_count
;
3588 q
->count
+= p
->count
;
3595 *pp
= edir
->dyn_relocs
;
3598 edir
->dyn_relocs
= eind
->dyn_relocs
;
3599 eind
->dyn_relocs
= NULL
;
3602 if (ind
->root
.type
== bfd_link_hash_indirect
3603 && dir
->got
.refcount
<= 0)
3605 edir
->tls_type
= eind
->tls_type
;
3606 eind
->tls_type
= GOT_UNKNOWN
;
3609 edir
->got_types_used
|= eind
->got_types_used
;
3611 _bfd_elf_link_hash_copy_indirect (info
, dir
, ind
);
3614 /* Implement elf_backend_check_relocs:
3615 Look through the relocs for a section during the first phase. */
3617 nios2_elf32_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
,
3618 asection
*sec
, const Elf_Internal_Rela
*relocs
)
3621 Elf_Internal_Shdr
*symtab_hdr
;
3622 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
3623 const Elf_Internal_Rela
*rel
;
3624 const Elf_Internal_Rela
*rel_end
;
3625 struct elf32_nios2_link_hash_table
*htab
;
3628 asection
*sreloc
= NULL
;
3629 bfd_signed_vma
*local_got_refcounts
;
3631 if (info
->relocatable
)
3634 dynobj
= elf_hash_table (info
)->dynobj
;
3635 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3636 sym_hashes
= elf_sym_hashes (abfd
);
3637 sym_hashes_end
= (sym_hashes
3638 + symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
));
3639 if (!elf_bad_symtab (abfd
))
3640 sym_hashes_end
-= symtab_hdr
->sh_info
;
3641 local_got_refcounts
= elf_local_got_refcounts (abfd
);
3643 htab
= elf32_nios2_hash_table (info
);
3644 sgot
= htab
->root
.sgot
;
3645 srelgot
= htab
->root
.srelgot
;
3647 rel_end
= relocs
+ sec
->reloc_count
;
3648 for (rel
= relocs
; rel
< rel_end
; rel
++)
3650 unsigned int r_type
;
3651 struct elf_link_hash_entry
*h
;
3652 unsigned long r_symndx
;
3654 r_symndx
= ELF32_R_SYM (rel
->r_info
);
3655 if (r_symndx
< symtab_hdr
->sh_info
)
3659 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
3660 while (h
->root
.type
== bfd_link_hash_indirect
3661 || h
->root
.type
== bfd_link_hash_warning
)
3662 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3664 /* PR15323, ref flags aren't set for references in the same
3666 h
->root
.non_ir_ref
= 1;
3669 r_type
= ELF32_R_TYPE (rel
->r_info
);
3674 case R_NIOS2_CALL16
:
3675 case R_NIOS2_TLS_GD16
:
3676 case R_NIOS2_TLS_IE16
:
3677 /* This symbol requires a global offset table entry. */
3679 int tls_type
, old_tls_type
;
3685 case R_NIOS2_CALL16
:
3686 tls_type
= GOT_NORMAL
;
3688 case R_NIOS2_TLS_GD16
:
3689 tls_type
= GOT_TLS_GD
;
3691 case R_NIOS2_TLS_IE16
:
3692 tls_type
= GOT_TLS_IE
;
3698 /* Create the .got section. */
3699 elf_hash_table (info
)->dynobj
= dynobj
= abfd
;
3700 nios2_elf32_create_dynamic_sections (dynobj
, info
);
3705 sgot
= htab
->root
.sgot
;
3706 BFD_ASSERT (sgot
!= NULL
);
3710 && (h
!= NULL
|| info
->shared
))
3712 srelgot
= htab
->root
.srelgot
;
3713 BFD_ASSERT (srelgot
!= NULL
);
3718 struct elf32_nios2_link_hash_entry
*eh
3719 = (struct elf32_nios2_link_hash_entry
*)h
;
3721 old_tls_type
= elf32_nios2_hash_entry(h
)->tls_type
;
3722 if (r_type
== R_NIOS2_CALL16
)
3724 /* Make sure a plt entry is created for this symbol if
3725 it turns out to be a function defined by a dynamic
3730 eh
->got_types_used
|= CALL16_USED
;
3733 eh
->got_types_used
|= GOT16_USED
;
3737 /* This is a global offset table entry for a local symbol. */
3738 if (local_got_refcounts
== NULL
)
3742 size
= symtab_hdr
->sh_info
;
3743 size
*= (sizeof (bfd_signed_vma
) + sizeof (char));
3745 = ((bfd_signed_vma
*) bfd_zalloc (abfd
, size
));
3746 if (local_got_refcounts
== NULL
)
3748 elf_local_got_refcounts (abfd
) = local_got_refcounts
;
3749 elf32_nios2_local_got_tls_type (abfd
)
3750 = (char *) (local_got_refcounts
+ symtab_hdr
->sh_info
);
3752 local_got_refcounts
[r_symndx
]++;
3753 old_tls_type
= elf32_nios2_local_got_tls_type (abfd
) [r_symndx
];
3756 /* We will already have issued an error message if there is a
3757 TLS / non-TLS mismatch, based on the symbol type. We don't
3758 support any linker relaxations. So just combine any TLS
3760 if (old_tls_type
!= GOT_UNKNOWN
&& old_tls_type
!= GOT_NORMAL
3761 && tls_type
!= GOT_NORMAL
)
3762 tls_type
|= old_tls_type
;
3764 if (old_tls_type
!= tls_type
)
3767 elf32_nios2_hash_entry (h
)->tls_type
= tls_type
;
3769 elf32_nios2_local_got_tls_type (abfd
) [r_symndx
] = tls_type
;
3773 case R_NIOS2_TLS_LDM16
:
3774 if (r_type
== R_NIOS2_TLS_LDM16
)
3775 htab
->tls_ldm_got
.refcount
++;
3777 if (htab
->root
.sgot
== NULL
)
3779 if (htab
->root
.dynobj
== NULL
)
3780 htab
->root
.dynobj
= abfd
;
3781 if (!create_got_section (htab
->root
.dynobj
, info
))
3786 /* This relocation describes the C++ object vtable hierarchy.
3787 Reconstruct it for later use during GC. */
3788 case R_NIOS2_GNU_VTINHERIT
:
3789 if (!bfd_elf_gc_record_vtinherit (abfd
, sec
, h
, rel
->r_offset
))
3793 /* This relocation describes which C++ vtable entries are actually
3794 used. Record for later use during GC. */
3795 case R_NIOS2_GNU_VTENTRY
:
3796 if (!bfd_elf_gc_record_vtentry (abfd
, sec
, h
, rel
->r_addend
))
3800 case R_NIOS2_BFD_RELOC_32
:
3801 case R_NIOS2_CALL26
:
3802 case R_NIOS2_CALL26_NOAT
:
3803 case R_NIOS2_HIADJ16
:
3808 /* If this reloc is in a read-only section, we might
3809 need a copy reloc. We can't check reliably at this
3810 stage whether the section is read-only, as input
3811 sections have not yet been mapped to output sections.
3812 Tentatively set the flag for now, and correct in
3813 adjust_dynamic_symbol. */
3817 /* Make sure a plt entry is created for this symbol if it
3818 turns out to be a function defined by a dynamic object. */
3821 if (r_type
== R_NIOS2_CALL26
|| r_type
== R_NIOS2_CALL26_NOAT
)
3825 /* If we are creating a shared library, we need to copy the
3826 reloc into the shared library. */
3828 && (sec
->flags
& SEC_ALLOC
) != 0
3829 && (r_type
== R_NIOS2_BFD_RELOC_32
3830 || (h
!= NULL
&& ! h
->needs_plt
3831 && (! info
->symbolic
|| ! h
->def_regular
))))
3833 struct elf32_nios2_dyn_relocs
*p
;
3834 struct elf32_nios2_dyn_relocs
**head
;
3836 /* When creating a shared object, we must copy these
3837 reloc types into the output file. We create a reloc
3838 section in dynobj and make room for this reloc. */
3841 sreloc
= _bfd_elf_make_dynamic_reloc_section
3842 (sec
, dynobj
, 2, abfd
, TRUE
);
3847 /* If this is a global symbol, we count the number of
3848 relocations we need for this symbol. */
3850 head
= &((struct elf32_nios2_link_hash_entry
*) h
)->dyn_relocs
;
3853 /* Track dynamic relocs needed for local syms too.
3854 We really need local syms available to do this
3859 Elf_Internal_Sym
*isym
;
3861 isym
= bfd_sym_from_r_symndx (&htab
->sym_cache
,
3866 s
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
3870 vpp
= &elf_section_data (s
)->local_dynrel
;
3871 head
= (struct elf32_nios2_dyn_relocs
**) vpp
;
3875 if (p
== NULL
|| p
->sec
!= sec
)
3877 bfd_size_type amt
= sizeof *p
;
3878 p
= ((struct elf32_nios2_dyn_relocs
*)
3879 bfd_alloc (htab
->root
.dynobj
, amt
));
3900 /* Implement elf_backend_gc_mark_hook:
3901 Return the section that should be marked against GC for a given
3904 nios2_elf32_gc_mark_hook (asection
*sec
,
3905 struct bfd_link_info
*info
,
3906 Elf_Internal_Rela
*rel
,
3907 struct elf_link_hash_entry
*h
,
3908 Elf_Internal_Sym
*sym
)
3911 switch (ELF32_R_TYPE (rel
->r_info
))
3913 case R_NIOS2_GNU_VTINHERIT
:
3914 case R_NIOS2_GNU_VTENTRY
:
3917 return _bfd_elf_gc_mark_hook (sec
, info
, rel
, h
, sym
);
3920 /* Implement elf_backend_gc_sweep_hook:
3921 Update the got entry reference counts for the section being removed. */
3923 nios2_elf32_gc_sweep_hook (bfd
*abfd
,
3924 struct bfd_link_info
*info
,
3926 const Elf_Internal_Rela
*relocs
)
3928 Elf_Internal_Shdr
*symtab_hdr
;
3929 struct elf_link_hash_entry
**sym_hashes
;
3930 bfd_signed_vma
*local_got_refcounts
;
3931 const Elf_Internal_Rela
*rel
, *relend
;
3934 if (info
->relocatable
)
3937 elf_section_data (sec
)->local_dynrel
= NULL
;
3939 dynobj
= elf_hash_table (info
)->dynobj
;
3943 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3944 sym_hashes
= elf_sym_hashes (abfd
);
3945 local_got_refcounts
= elf_local_got_refcounts (abfd
);
3947 relend
= relocs
+ sec
->reloc_count
;
3948 for (rel
= relocs
; rel
< relend
; rel
++)
3950 unsigned long r_symndx
;
3951 struct elf_link_hash_entry
*h
= NULL
;
3954 r_symndx
= ELF32_R_SYM (rel
->r_info
);
3955 if (r_symndx
>= symtab_hdr
->sh_info
)
3957 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
3958 while (h
->root
.type
== bfd_link_hash_indirect
3959 || h
->root
.type
== bfd_link_hash_warning
)
3960 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3963 r_type
= ELF32_R_TYPE (rel
->r_info
);
3967 case R_NIOS2_CALL16
:
3970 if (h
->got
.refcount
> 0)
3973 else if (local_got_refcounts
!= NULL
)
3975 if (local_got_refcounts
[r_symndx
] > 0)
3976 --local_got_refcounts
[r_symndx
];
3980 case R_NIOS2_PCREL_LO
:
3981 case R_NIOS2_PCREL_HA
:
3982 case R_NIOS2_BFD_RELOC_32
:
3983 case R_NIOS2_CALL26
:
3984 case R_NIOS2_CALL26_NOAT
:
3987 struct elf32_nios2_link_hash_entry
*eh
;
3988 struct elf32_nios2_dyn_relocs
**pp
;
3989 struct elf32_nios2_dyn_relocs
*p
;
3991 eh
= (struct elf32_nios2_link_hash_entry
*) h
;
3993 if (h
->plt
.refcount
> 0)
3996 if (r_type
== R_NIOS2_PCREL_LO
|| r_type
== R_NIOS2_PCREL_HA
3997 || r_type
== R_NIOS2_BFD_RELOC_32
)
3999 for (pp
= &eh
->dyn_relocs
; (p
= *pp
) != NULL
;
4020 /* Implement elf_backend_finish_dynamic_symbols:
4021 Finish up dynamic symbol handling. We set the contents of various
4022 dynamic sections here. */
4024 nios2_elf32_finish_dynamic_symbol (bfd
*output_bfd
,
4025 struct bfd_link_info
*info
,
4026 struct elf_link_hash_entry
*h
,
4027 Elf_Internal_Sym
*sym
)
4029 struct elf32_nios2_link_hash_table
*htab
;
4030 struct elf32_nios2_link_hash_entry
*eh
4031 = (struct elf32_nios2_link_hash_entry
*)h
;
4034 htab
= elf32_nios2_hash_table (info
);
4036 if (h
->plt
.offset
!= (bfd_vma
) -1)
4043 Elf_Internal_Rela rela
;
4045 bfd_vma got_address
;
4047 /* This symbol has an entry in the procedure linkage table. Set
4049 BFD_ASSERT (h
->dynindx
!= -1);
4050 splt
= htab
->root
.splt
;
4051 sgotplt
= htab
->root
.sgotplt
;
4052 srela
= htab
->root
.srelplt
;
4053 BFD_ASSERT (splt
!= NULL
&& sgotplt
!= NULL
&& srela
!= NULL
);
4055 /* Emit the PLT entry. */
4058 nios2_elf32_install_data (splt
, nios2_so_plt_entry
, h
->plt
.offset
,
4060 plt_index
= (h
->plt
.offset
- 24) / 12;
4061 got_offset
= (plt_index
+ 3) * 4;
4062 nios2_elf32_install_imm16 (splt
, h
->plt
.offset
,
4063 hiadj(plt_index
* 4));
4064 nios2_elf32_install_imm16 (splt
, h
->plt
.offset
+ 4,
4065 (plt_index
* 4) & 0xffff);
4066 nios2_elf32_install_imm16 (splt
, h
->plt
.offset
+ 8,
4067 0xfff4 - h
->plt
.offset
);
4068 got_address
= (sgotplt
->output_section
->vma
+ sgotplt
->output_offset
4071 /* Fill in the entry in the global offset table. There are no
4072 res_n slots for a shared object PLT, instead the .got.plt entries
4073 point to the PLT entries. */
4074 bfd_put_32 (output_bfd
,
4075 splt
->output_section
->vma
+ splt
->output_offset
4076 + h
->plt
.offset
, sgotplt
->contents
+ got_offset
);
4080 plt_index
= (h
->plt
.offset
- 28 - htab
->res_n_size
) / 12;
4081 got_offset
= (plt_index
+ 3) * 4;
4083 nios2_elf32_install_data (splt
, nios2_plt_entry
, h
->plt
.offset
, 3);
4084 got_address
= (sgotplt
->output_section
->vma
+ sgotplt
->output_offset
4086 nios2_elf32_install_imm16 (splt
, h
->plt
.offset
, hiadj(got_address
));
4087 nios2_elf32_install_imm16 (splt
, h
->plt
.offset
+ 4,
4088 got_address
& 0xffff);
4090 /* Fill in the entry in the global offset table. */
4091 bfd_put_32 (output_bfd
,
4092 splt
->output_section
->vma
+ splt
->output_offset
4093 + plt_index
* 4, sgotplt
->contents
+ got_offset
);
4096 /* Fill in the entry in the .rela.plt section. */
4097 rela
.r_offset
= got_address
;
4098 rela
.r_info
= ELF32_R_INFO (h
->dynindx
, R_NIOS2_JUMP_SLOT
);
4100 loc
= srela
->contents
+ plt_index
* sizeof (Elf32_External_Rela
);
4101 bfd_elf32_swap_reloca_out (output_bfd
, &rela
, loc
);
4103 if (!h
->def_regular
)
4105 /* Mark the symbol as undefined, rather than as defined in
4106 the .plt section. Leave the value alone. */
4107 sym
->st_shndx
= SHN_UNDEF
;
4108 /* If the symbol is weak, we do need to clear the value.
4109 Otherwise, the PLT entry would provide a definition for
4110 the symbol even if the symbol wasn't defined anywhere,
4111 and so the symbol would never be NULL. */
4112 if (!h
->ref_regular_nonweak
)
4117 use_plt
= (eh
->got_types_used
== CALL16_USED
4118 && h
->plt
.offset
!= (bfd_vma
) -1);
4120 if (!use_plt
&& h
->got
.offset
!= (bfd_vma
) -1
4121 && (elf32_nios2_hash_entry (h
)->tls_type
& GOT_TLS_GD
) == 0
4122 && (elf32_nios2_hash_entry (h
)->tls_type
& GOT_TLS_IE
) == 0)
4126 Elf_Internal_Rela rela
;
4130 /* This symbol has an entry in the global offset table. Set it
4132 sgot
= htab
->root
.sgot
;
4133 srela
= htab
->root
.srelgot
;
4134 BFD_ASSERT (sgot
!= NULL
&& srela
!= NULL
);
4136 offset
= (h
->got
.offset
& ~(bfd_vma
) 1);
4137 rela
.r_offset
= (sgot
->output_section
->vma
4138 + sgot
->output_offset
+ offset
);
4140 /* If this is a -Bsymbolic link, and the symbol is defined
4141 locally, we just want to emit a RELATIVE reloc. Likewise if
4142 the symbol was forced to be local because of a version file.
4143 The entry in the global offset table will already have been
4144 initialized in the relocate_section function. */
4146 if (info
->shared
&& SYMBOL_REFERENCES_LOCAL (info
, h
))
4148 rela
.r_info
= ELF32_R_INFO (0, R_NIOS2_RELATIVE
);
4149 rela
.r_addend
= bfd_get_signed_32 (output_bfd
,
4150 (sgot
->contents
+ offset
));
4151 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
+ offset
);
4155 bfd_put_32 (output_bfd
, (bfd_vma
) 0,
4156 sgot
->contents
+ offset
);
4157 rela
.r_info
= ELF32_R_INFO (h
->dynindx
, R_NIOS2_GLOB_DAT
);
4161 loc
= srela
->contents
;
4162 loc
+= srela
->reloc_count
++ * sizeof (Elf32_External_Rela
);
4163 bfd_elf32_swap_reloca_out (output_bfd
, &rela
, loc
);
4166 if (use_plt
&& h
->got
.offset
!= (bfd_vma
) -1)
4168 bfd_vma offset
= (h
->got
.offset
& ~(bfd_vma
) 1);
4169 asection
*sgot
= htab
->root
.sgot
;
4170 asection
*splt
= htab
->root
.splt
;
4171 bfd_put_32 (output_bfd
, (splt
->output_section
->vma
+ splt
->output_offset
4173 sgot
->contents
+ offset
);
4179 Elf_Internal_Rela rela
;
4182 /* This symbol needs a copy reloc. Set it up. */
4183 BFD_ASSERT (h
->dynindx
!= -1
4184 && (h
->root
.type
== bfd_link_hash_defined
4185 || h
->root
.type
== bfd_link_hash_defweak
));
4188 BFD_ASSERT (s
!= NULL
);
4190 rela
.r_offset
= (h
->root
.u
.def
.value
4191 + h
->root
.u
.def
.section
->output_section
->vma
4192 + h
->root
.u
.def
.section
->output_offset
);
4193 rela
.r_info
= ELF32_R_INFO (h
->dynindx
, R_NIOS2_COPY
);
4195 loc
= s
->contents
+ s
->reloc_count
++ * sizeof (Elf32_External_Rela
);
4196 bfd_elf32_swap_reloca_out (output_bfd
, &rela
, loc
);
4199 /* Mark _DYNAMIC, _GLOBAL_OFFSET_TABLE_, and _gp_got as absolute. */
4200 if (strcmp (h
->root
.root
.string
, "_DYNAMIC") == 0
4201 || h
== elf_hash_table (info
)->hgot
4202 || h
== elf32_nios2_hash_table (info
)->h_gp_got
)
4203 sym
->st_shndx
= SHN_ABS
;
4208 /* Implement elf_backend_finish_dynamic_sections. */
4210 nios2_elf32_finish_dynamic_sections (bfd
*output_bfd
,
4211 struct bfd_link_info
*info
)
4216 struct elf32_nios2_link_hash_table
*htab
;
4218 htab
= elf32_nios2_hash_table (info
);
4219 dynobj
= elf_hash_table (info
)->dynobj
;
4220 sgotplt
= htab
->root
.sgotplt
;
4221 BFD_ASSERT (sgotplt
!= NULL
);
4222 sdyn
= bfd_get_linker_section (dynobj
, ".dynamic");
4224 if (elf_hash_table (info
)->dynamic_sections_created
)
4227 Elf32_External_Dyn
*dyncon
, *dynconend
;
4229 splt
= htab
->root
.splt
;
4230 BFD_ASSERT (splt
!= NULL
&& sdyn
!= NULL
);
4232 dyncon
= (Elf32_External_Dyn
*) sdyn
->contents
;
4233 dynconend
= (Elf32_External_Dyn
*) (sdyn
->contents
+ sdyn
->size
);
4234 for (; dyncon
< dynconend
; dyncon
++)
4236 Elf_Internal_Dyn dyn
;
4239 bfd_elf32_swap_dyn_in (dynobj
, dyncon
, &dyn
);
4247 s
= htab
->root
.sgot
;
4248 BFD_ASSERT (s
!= NULL
);
4249 dyn
.d_un
.d_ptr
= s
->output_section
->vma
;
4250 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
4254 s
= htab
->root
.srelplt
;
4255 BFD_ASSERT (s
!= NULL
);
4256 dyn
.d_un
.d_ptr
= s
->output_section
->vma
;
4257 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
4261 s
= htab
->root
.srelplt
;
4262 BFD_ASSERT (s
!= NULL
);
4263 dyn
.d_un
.d_val
= s
->size
;
4264 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
4268 /* The procedure linkage table relocs (DT_JMPREL) should
4269 not be included in the overall relocs (DT_RELA).
4270 Therefore, we override the DT_RELASZ entry here to
4271 make it not include the JMPREL relocs. Since the
4272 linker script arranges for .rela.plt to follow all
4273 other relocation sections, we don't have to worry
4274 about changing the DT_RELA entry. */
4275 s
= htab
->root
.srelplt
;
4277 dyn
.d_un
.d_val
-= s
->size
;
4278 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
4282 s
= htab
->root
.sgot
;
4283 BFD_ASSERT (s
!= NULL
);
4284 dyn
.d_un
.d_ptr
= s
->output_section
->vma
+ 0x7ff0;
4285 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
4290 /* Fill in the first entry in the procedure linkage table. */
4293 bfd_vma got_address
= (sgotplt
->output_section
->vma
4294 + sgotplt
->output_offset
);
4297 bfd_vma corrected
= got_address
- (splt
->output_section
->vma
4298 + splt
->output_offset
+ 4);
4299 nios2_elf32_install_data (splt
, nios2_so_plt0_entry
, 0, 6);
4300 nios2_elf32_install_imm16 (splt
, 4, hiadj (corrected
));
4301 nios2_elf32_install_imm16 (splt
, 12, (corrected
& 0xffff) + 4);
4302 nios2_elf32_install_imm16 (splt
, 16, (corrected
& 0xffff) + 8);
4306 /* Divide by 4 here, not 3 because we already corrected for the
4308 bfd_vma res_size
= (splt
->size
- 28) / 4;
4309 bfd_vma res_start
= (splt
->output_section
->vma
4310 + splt
->output_offset
);
4313 for (res_offset
= 0; res_offset
< res_size
; res_offset
+= 4)
4314 bfd_put_32 (output_bfd
,
4315 6 | ((res_size
- (res_offset
+ 4)) << 6),
4316 splt
->contents
+ res_offset
);
4318 nios2_elf32_install_data (splt
, nios2_plt0_entry
, res_size
, 7);
4319 nios2_elf32_install_imm16 (splt
, res_size
, hiadj (res_start
));
4320 nios2_elf32_install_imm16 (splt
, res_size
+ 4,
4321 res_start
& 0xffff);
4322 nios2_elf32_install_imm16 (splt
, res_size
+ 12,
4323 hiadj (got_address
));
4324 nios2_elf32_install_imm16 (splt
, res_size
+ 16,
4325 (got_address
& 0xffff) + 4);
4326 nios2_elf32_install_imm16 (splt
, res_size
+ 20,
4327 (got_address
& 0xffff) + 8);
4331 /* Fill in the first three entries in the global offset table. */
4332 if (sgotplt
->size
> 0)
4335 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgotplt
->contents
);
4337 bfd_put_32 (output_bfd
,
4338 sdyn
->output_section
->vma
+ sdyn
->output_offset
,
4340 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgotplt
->contents
+ 4);
4341 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgotplt
->contents
+ 8);
4344 elf_section_data (sgotplt
->output_section
)->this_hdr
.sh_entsize
= 4;
4349 /* Implement elf_backend_adjust_dynamic_symbol:
4350 Adjust a symbol defined by a dynamic object and referenced by a
4351 regular object. The current definition is in some section of the
4352 dynamic object, but we're not including those sections. We have to
4353 change the definition to something the rest of the link can
4356 nios2_elf32_adjust_dynamic_symbol (struct bfd_link_info
*info
,
4357 struct elf_link_hash_entry
*h
)
4359 struct elf32_nios2_link_hash_table
*htab
;
4364 htab
= elf32_nios2_hash_table (info
);
4365 dynobj
= elf_hash_table (info
)->dynobj
;
4367 /* Make sure we know what is going on here. */
4368 BFD_ASSERT (dynobj
!= NULL
4370 || h
->u
.weakdef
!= NULL
4373 && !h
->def_regular
)));
4375 /* If this is a function, put it in the procedure linkage table. We
4376 will fill in the contents of the procedure linkage table later,
4377 when we know the address of the .got section. */
4378 if (h
->type
== STT_FUNC
|| h
->needs_plt
)
4380 if (h
->plt
.refcount
<= 0
4381 || SYMBOL_CALLS_LOCAL (info
, h
)
4382 || (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
4383 && h
->root
.type
== bfd_link_hash_undefweak
))
4385 /* This case can occur if we saw a PLT reloc in an input
4386 file, but the symbol was never referred to by a dynamic
4387 object, or if all references were garbage collected. In
4388 such a case, we don't actually need to build a procedure
4389 linkage table, and we can just do a PCREL reloc instead. */
4390 h
->plt
.offset
= (bfd_vma
) -1;
4397 /* Reinitialize the plt offset now that it is not used as a reference
4399 h
->plt
.offset
= (bfd_vma
) -1;
4401 /* If this is a weak symbol, and there is a real definition, the
4402 processor independent code will have arranged for us to see the
4403 real definition first, and we can just use the same value. */
4404 if (h
->u
.weakdef
!= NULL
)
4406 BFD_ASSERT (h
->u
.weakdef
->root
.type
== bfd_link_hash_defined
4407 || h
->u
.weakdef
->root
.type
== bfd_link_hash_defweak
);
4408 h
->root
.u
.def
.section
= h
->u
.weakdef
->root
.u
.def
.section
;
4409 h
->root
.u
.def
.value
= h
->u
.weakdef
->root
.u
.def
.value
;
4413 /* If there are no non-GOT references, we do not need a copy
4415 if (!h
->non_got_ref
)
4418 /* This is a reference to a symbol defined by a dynamic object which
4420 If we are creating a shared library, we must presume that the
4421 only references to the symbol are via the global offset table.
4422 For such cases we need not do anything here; the relocations will
4423 be handled correctly by relocate_section. */
4429 (*_bfd_error_handler
) (_("dynamic variable `%s' is zero size"),
4430 h
->root
.root
.string
);
4434 /* We must allocate the symbol in our .dynbss section, which will
4435 become part of the .bss section of the executable. There will be
4436 an entry for this symbol in the .dynsym section. The dynamic
4437 object will contain position independent code, so all references
4438 from the dynamic object to this symbol will go through the global
4439 offset table. The dynamic linker will use the .dynsym entry to
4440 determine the address it must put in the global offset table, so
4441 both the dynamic object and the regular object will refer to the
4442 same memory location for the variable. */
4444 BFD_ASSERT (s
!= NULL
);
4446 /* We must generate a R_NIOS2_COPY reloc to tell the dynamic linker to
4447 copy the initial value out of the dynamic object and into the
4448 runtime process image. We need to remember the offset into the
4449 .rela.bss section we are going to use. */
4450 if ((h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0)
4454 srel
= htab
->srelbss
;
4455 BFD_ASSERT (srel
!= NULL
);
4456 srel
->size
+= sizeof (Elf32_External_Rela
);
4460 align2
= bfd_log2 (h
->size
);
4461 if (align2
> h
->root
.u
.def
.section
->alignment_power
)
4462 align2
= h
->root
.u
.def
.section
->alignment_power
;
4465 s
->size
= BFD_ALIGN (s
->size
, (bfd_size_type
)1 << align2
);
4466 if (align2
> bfd_get_section_alignment (dynobj
, s
)
4467 && !bfd_set_section_alignment (dynobj
, s
, align2
))
4470 /* Define the symbol as being at this point in the section. */
4471 h
->root
.u
.def
.section
= s
;
4472 h
->root
.u
.def
.value
= s
->size
;
4474 /* Increment the section size to make room for the symbol. */
4480 /* Worker function for nios2_elf32_size_dynamic_sections. */
4482 adjust_dynrelocs (struct elf_link_hash_entry
*h
, PTR inf
)
4484 struct bfd_link_info
*info
;
4485 struct elf32_nios2_link_hash_table
*htab
;
4487 if (h
->root
.type
== bfd_link_hash_indirect
)
4490 if (h
->root
.type
== bfd_link_hash_warning
)
4491 /* When warning symbols are created, they **replace** the "real"
4492 entry in the hash table, thus we never get to see the real
4493 symbol in a hash traversal. So look at it now. */
4494 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4496 info
= (struct bfd_link_info
*) inf
;
4497 htab
= elf32_nios2_hash_table (info
);
4499 if (h
->plt
.offset
!= (bfd_vma
)-1)
4500 h
->plt
.offset
+= htab
->res_n_size
;
4501 if (htab
->root
.splt
== h
->root
.u
.def
.section
)
4502 h
->root
.u
.def
.value
+= htab
->res_n_size
;
4507 /* Another worker function for nios2_elf32_size_dynamic_sections.
4508 Allocate space in .plt, .got and associated reloc sections for
4511 allocate_dynrelocs (struct elf_link_hash_entry
*h
, PTR inf
)
4513 struct bfd_link_info
*info
;
4514 struct elf32_nios2_link_hash_table
*htab
;
4515 struct elf32_nios2_link_hash_entry
*eh
;
4516 struct elf32_nios2_dyn_relocs
*p
;
4519 if (h
->root
.type
== bfd_link_hash_indirect
)
4522 if (h
->root
.type
== bfd_link_hash_warning
)
4523 /* When warning symbols are created, they **replace** the "real"
4524 entry in the hash table, thus we never get to see the real
4525 symbol in a hash traversal. So look at it now. */
4526 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4528 info
= (struct bfd_link_info
*) inf
;
4529 htab
= elf32_nios2_hash_table (info
);
4531 if (htab
->root
.dynamic_sections_created
4532 && h
->plt
.refcount
> 0)
4534 /* Make sure this symbol is output as a dynamic symbol.
4535 Undefined weak syms won't yet be marked as dynamic. */
4536 if (h
->dynindx
== -1
4538 && !bfd_elf_link_record_dynamic_symbol (info
, h
))
4541 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info
->shared
, h
))
4543 asection
*s
= htab
->root
.splt
;
4545 /* Allocate room for the header. */
4554 h
->plt
.offset
= s
->size
;
4556 /* If this symbol is not defined in a regular file, and we are
4557 not generating a shared library, then set the symbol to this
4558 location in the .plt. This is required to make function
4559 pointers compare as equal between the normal executable and
4560 the shared library. */
4564 h
->root
.u
.def
.section
= s
;
4565 h
->root
.u
.def
.value
= h
->plt
.offset
;
4568 /* Make room for this entry. */
4571 /* We also need to make an entry in the .rela.plt section. */
4572 htab
->root
.srelplt
->size
+= sizeof (Elf32_External_Rela
);
4574 /* And the .got.plt section. */
4575 htab
->root
.sgotplt
->size
+= 4;
4579 h
->plt
.offset
= (bfd_vma
) -1;
4585 h
->plt
.offset
= (bfd_vma
) -1;
4589 eh
= (struct elf32_nios2_link_hash_entry
*) h
;
4590 use_plt
= (eh
->got_types_used
== CALL16_USED
4591 && h
->plt
.offset
!= (bfd_vma
) -1);
4593 if (h
->got
.refcount
> 0)
4597 int tls_type
= eh
->tls_type
;
4600 /* Make sure this symbol is output as a dynamic symbol.
4601 Undefined weak syms won't yet be marked as dynamic. */
4602 if (h
->dynindx
== -1
4604 && !bfd_elf_link_record_dynamic_symbol (info
, h
))
4607 s
= htab
->root
.sgot
;
4608 h
->got
.offset
= s
->size
;
4610 if (tls_type
== GOT_UNKNOWN
)
4613 if (tls_type
== GOT_NORMAL
)
4614 /* Non-TLS symbols need one GOT slot. */
4618 if (tls_type
& GOT_TLS_GD
)
4619 /* R_NIOS2_TLS_GD16 needs 2 consecutive GOT slots. */
4621 if (tls_type
& GOT_TLS_IE
)
4622 /* R_NIOS2_TLS_IE16 needs one GOT slot. */
4626 dyn
= htab
->root
.dynamic_sections_created
;
4629 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, info
->shared
, h
)
4631 || !SYMBOL_REFERENCES_LOCAL (info
, h
)))
4634 if (tls_type
!= GOT_NORMAL
4635 && (info
->shared
|| indx
!= 0)
4636 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
4637 || h
->root
.type
!= bfd_link_hash_undefweak
))
4639 if (tls_type
& GOT_TLS_IE
)
4640 htab
->root
.srelgot
->size
+= sizeof (Elf32_External_Rela
);
4642 if (tls_type
& GOT_TLS_GD
)
4643 htab
->root
.srelgot
->size
+= sizeof (Elf32_External_Rela
);
4645 if ((tls_type
& GOT_TLS_GD
) && indx
!= 0)
4646 htab
->root
.srelgot
->size
+= sizeof (Elf32_External_Rela
);
4648 else if ((ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
4649 || h
->root
.type
!= bfd_link_hash_undefweak
)
4652 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, 0, h
)))
4653 htab
->root
.srelgot
->size
+= sizeof (Elf32_External_Rela
);
4656 h
->got
.offset
= (bfd_vma
) -1;
4658 if (eh
->dyn_relocs
== NULL
)
4661 /* In the shared -Bsymbolic case, discard space allocated for
4662 dynamic pc-relative relocs against symbols which turn out to be
4663 defined in regular objects. For the normal shared case, discard
4664 space for pc-relative relocs that have become local due to symbol
4665 visibility changes. */
4670 && (h
->forced_local
|| info
->symbolic
))
4672 struct elf32_nios2_dyn_relocs
**pp
;
4674 for (pp
= &eh
->dyn_relocs
; (p
= *pp
) != NULL
; )
4676 p
->count
-= p
->pc_count
;
4685 /* Also discard relocs on undefined weak syms with non-default
4687 if (eh
->dyn_relocs
!= NULL
4688 && h
->root
.type
== bfd_link_hash_undefweak
)
4690 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
4691 eh
->dyn_relocs
= NULL
;
4693 /* Make sure undefined weak symbols are output as a dynamic
4695 else if (h
->dynindx
== -1
4697 && !bfd_elf_link_record_dynamic_symbol (info
, h
))
4703 /* For the non-shared case, discard space for relocs against
4704 symbols which turn out to need copy relocs or are not
4708 && ((h
->def_dynamic
&& !h
->def_regular
)
4709 || (htab
->root
.dynamic_sections_created
4710 && (h
->root
.type
== bfd_link_hash_undefweak
4711 || h
->root
.type
== bfd_link_hash_undefined
))))
4713 /* Make sure this symbol is output as a dynamic symbol.
4714 Undefined weak syms won't yet be marked as dynamic. */
4715 if (h
->dynindx
== -1
4717 && !bfd_elf_link_record_dynamic_symbol (info
, h
))
4720 /* If that succeeded, we know we'll be keeping all the
4722 if (h
->dynindx
!= -1)
4726 eh
->dyn_relocs
= NULL
;
4731 /* Finally, allocate space. */
4732 for (p
= eh
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
4734 asection
*sreloc
= elf_section_data (p
->sec
)->sreloc
;
4735 sreloc
->size
+= p
->count
* sizeof (Elf32_External_Rela
);
4741 /* Implement elf_backend_size_dynamic_sections:
4742 Set the sizes of the dynamic sections. */
4744 nios2_elf32_size_dynamic_sections (bfd
*output_bfd ATTRIBUTE_UNUSED
,
4745 struct bfd_link_info
*info
)
4753 struct elf32_nios2_link_hash_table
*htab
;
4755 htab
= elf32_nios2_hash_table (info
);
4756 dynobj
= elf_hash_table (info
)->dynobj
;
4757 BFD_ASSERT (dynobj
!= NULL
);
4759 htab
->res_n_size
= 0;
4760 if (elf_hash_table (info
)->dynamic_sections_created
)
4762 /* Set the contents of the .interp section to the interpreter. */
4763 if (info
->executable
)
4765 s
= bfd_get_linker_section (dynobj
, ".interp");
4766 BFD_ASSERT (s
!= NULL
);
4767 s
->size
= sizeof ELF_DYNAMIC_INTERPRETER
;
4768 s
->contents
= (unsigned char *) ELF_DYNAMIC_INTERPRETER
;
4773 /* We may have created entries in the .rela.got section.
4774 However, if we are not creating the dynamic sections, we will
4775 not actually use these entries. Reset the size of .rela.got,
4776 which will cause it to get stripped from the output file
4778 s
= htab
->root
.srelgot
;
4783 /* Set up .got offsets for local syms, and space for local dynamic
4785 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link_next
)
4787 bfd_signed_vma
*local_got
;
4788 bfd_signed_vma
*end_local_got
;
4789 char *local_tls_type
;
4790 bfd_size_type locsymcount
;
4791 Elf_Internal_Shdr
*symtab_hdr
;
4794 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
4797 for (s
= ibfd
->sections
; s
!= NULL
; s
= s
->next
)
4799 struct elf32_nios2_dyn_relocs
*p
;
4801 for (p
= elf_section_data (s
)->local_dynrel
; p
!= NULL
; p
= p
->next
)
4803 if (!bfd_is_abs_section (p
->sec
)
4804 && bfd_is_abs_section (p
->sec
->output_section
))
4806 /* Input section has been discarded, either because
4807 it is a copy of a linkonce section or due to
4808 linker script /DISCARD/, so we'll be discarding
4811 else if (p
->count
!= 0)
4813 srel
= elf_section_data (p
->sec
)->sreloc
;
4814 srel
->size
+= p
->count
* sizeof (Elf32_External_Rela
);
4815 if ((p
->sec
->output_section
->flags
& SEC_READONLY
) != 0)
4816 info
->flags
|= DF_TEXTREL
;
4821 local_got
= elf_local_got_refcounts (ibfd
);
4825 symtab_hdr
= &elf_tdata (ibfd
)->symtab_hdr
;
4826 locsymcount
= symtab_hdr
->sh_info
;
4827 end_local_got
= local_got
+ locsymcount
;
4828 local_tls_type
= elf32_nios2_local_got_tls_type (ibfd
);
4829 s
= htab
->root
.sgot
;
4830 srel
= htab
->root
.srelgot
;
4831 for (; local_got
< end_local_got
; ++local_got
, ++local_tls_type
)
4835 *local_got
= s
->size
;
4836 if (*local_tls_type
& GOT_TLS_GD
)
4837 /* TLS_GD relocs need an 8-byte structure in the GOT. */
4839 if (*local_tls_type
& GOT_TLS_IE
)
4841 if (*local_tls_type
== GOT_NORMAL
)
4844 if (info
->shared
|| *local_tls_type
== GOT_TLS_GD
)
4845 srel
->size
+= sizeof (Elf32_External_Rela
);
4848 *local_got
= (bfd_vma
) -1;
4852 if (htab
->tls_ldm_got
.refcount
> 0)
4854 /* Allocate two GOT entries and one dynamic relocation (if necessary)
4855 for R_NIOS2_TLS_LDM16 relocations. */
4856 htab
->tls_ldm_got
.offset
= htab
->root
.sgot
->size
;
4857 htab
->root
.sgot
->size
+= 8;
4859 htab
->root
.srelgot
->size
+= sizeof (Elf32_External_Rela
);
4862 htab
->tls_ldm_got
.offset
= -1;
4864 /* Allocate global sym .plt and .got entries, and space for global
4865 sym dynamic relocs. */
4866 elf_link_hash_traverse (& htab
->root
, allocate_dynrelocs
, info
);
4868 if (elf_hash_table (info
)->dynamic_sections_created
)
4870 /* If the .got section is more than 0x8000 bytes, we add
4871 0x8000 to the value of _gp_got, so that 16-bit relocations
4872 have a greater chance of working. */
4873 if (htab
->root
.sgot
->size
>= 0x8000
4874 && elf32_nios2_hash_table (info
)->h_gp_got
->root
.u
.def
.value
== 0)
4875 elf32_nios2_hash_table (info
)->h_gp_got
->root
.u
.def
.value
= 0x8000;
4878 /* The check_relocs and adjust_dynamic_symbol entry points have
4879 determined the sizes of the various dynamic sections. Allocate
4884 for (s
= dynobj
->sections
; s
!= NULL
; s
= s
->next
)
4888 if ((s
->flags
& SEC_LINKER_CREATED
) == 0)
4891 /* It's OK to base decisions on the section name, because none
4892 of the dynobj section names depend upon the input files. */
4893 name
= bfd_get_section_name (dynobj
, s
);
4895 if (strcmp (name
, ".plt") == 0)
4897 /* Remember whether there is a PLT. */
4900 /* Correct for the number of res_N branches. */
4901 if (plt
&& !info
->shared
)
4903 htab
->res_n_size
= (s
->size
-28) / 3;
4904 s
->size
+= htab
->res_n_size
;
4907 else if (CONST_STRNEQ (name
, ".rela"))
4913 /* We use the reloc_count field as a counter if we need
4914 to copy relocs into the output file. */
4918 else if (CONST_STRNEQ (name
, ".got"))
4920 else if (strcmp (name
, ".dynbss") != 0)
4921 /* It's not one of our sections, so don't allocate space. */
4926 /* If we don't need this section, strip it from the
4927 output file. This is mostly to handle .rela.bss and
4928 .rela.plt. We must create both sections in
4929 create_dynamic_sections, because they must be created
4930 before the linker maps input sections to output
4931 sections. The linker does that before
4932 adjust_dynamic_symbol is called, and it is that
4933 function which decides whether anything needs to go
4934 into these sections. */
4935 s
->flags
|= SEC_EXCLUDE
;
4939 if ((s
->flags
& SEC_HAS_CONTENTS
) == 0)
4942 /* Allocate memory for the section contents. */
4943 /* FIXME: This should be a call to bfd_alloc not bfd_zalloc.
4944 Unused entries should be reclaimed before the section's contents
4945 are written out, but at the moment this does not happen. Thus in
4946 order to prevent writing out garbage, we initialize the section's
4947 contents to zero. */
4948 s
->contents
= (bfd_byte
*) bfd_zalloc (dynobj
, s
->size
);
4949 if (s
->contents
== NULL
)
4953 /* Adjust dynamic symbols that point to the plt to account for the
4954 now-known number of resN slots. */
4955 if (htab
->res_n_size
)
4956 elf_link_hash_traverse (& htab
->root
, adjust_dynrelocs
, info
);
4958 if (elf_hash_table (info
)->dynamic_sections_created
)
4960 /* Add some entries to the .dynamic section. We fill in the
4961 values later, in elf_nios2_finish_dynamic_sections, but we
4962 must add the entries now so that we get the correct size for
4963 the .dynamic section. The DT_DEBUG entry is filled in by the
4964 dynamic linker and used by the debugger. */
4965 #define add_dynamic_entry(TAG, VAL) \
4966 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
4968 if (!info
->shared
&& !add_dynamic_entry (DT_DEBUG
, 0))
4971 if (got
&& !add_dynamic_entry (DT_PLTGOT
, 0))
4975 && (!add_dynamic_entry (DT_PLTRELSZ
, 0)
4976 || !add_dynamic_entry (DT_PLTREL
, DT_RELA
)
4977 || !add_dynamic_entry (DT_JMPREL
, 0)))
4981 && (!add_dynamic_entry (DT_RELA
, 0)
4982 || !add_dynamic_entry (DT_RELASZ
, 0)
4983 || !add_dynamic_entry (DT_RELAENT
, sizeof (Elf32_External_Rela
))))
4986 if (!info
->shared
&& !add_dynamic_entry (DT_NIOS2_GP
, 0))
4989 if ((info
->flags
& DF_TEXTREL
) != 0
4990 && !add_dynamic_entry (DT_TEXTREL
, 0))
4993 #undef add_dynamic_entry
4998 /* Implement bfd_elf32_bfd_link_hash_table_create. */
4999 static struct bfd_link_hash_table
*
5000 nios2_elf32_link_hash_table_create (bfd
*abfd
)
5002 struct elf32_nios2_link_hash_table
*ret
;
5003 bfd_size_type amt
= sizeof (struct elf32_nios2_link_hash_table
);
5005 ret
= bfd_zmalloc (amt
);
5009 if (!_bfd_elf_link_hash_table_init (&ret
->root
, abfd
,
5012 elf32_nios2_link_hash_entry
),
5019 /* Init the stub hash table too. */
5020 if (!bfd_hash_table_init (&ret
->bstab
, stub_hash_newfunc
,
5021 sizeof (struct elf32_nios2_stub_hash_entry
)))
5024 return &ret
->root
.root
;
5027 /* Free the derived linker hash table. */
5029 nios2_elf32_link_hash_table_free (struct bfd_link_hash_table
*btab
)
5031 struct elf32_nios2_link_hash_table
*htab
5032 = (struct elf32_nios2_link_hash_table
*) btab
;
5034 bfd_hash_table_free (&htab
->bstab
);
5035 _bfd_elf_link_hash_table_free (btab
);
5038 /* Implement elf_backend_reloc_type_class. */
5039 static enum elf_reloc_type_class
5040 nios2_elf32_reloc_type_class (const struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
5041 const asection
*rel_sec ATTRIBUTE_UNUSED
,
5042 const Elf_Internal_Rela
*rela
)
5044 switch ((int) ELF32_R_TYPE (rela
->r_info
))
5046 case R_NIOS2_RELATIVE
:
5047 return reloc_class_relative
;
5048 case R_NIOS2_JUMP_SLOT
:
5049 return reloc_class_plt
;
5051 return reloc_class_copy
;
5053 return reloc_class_normal
;
5057 /* Return 1 if target is one of ours. */
5059 is_nios2_elf_target (const struct bfd_target
*targ
)
5061 return (targ
== &bfd_elf32_littlenios2_vec
5062 || targ
== &bfd_elf32_bignios2_vec
);
5065 /* Implement elf_backend_add_symbol_hook.
5066 This hook is called by the linker when adding symbols from an object
5067 file. We use it to put .comm items in .sbss, and not .bss. */
5069 nios2_elf_add_symbol_hook (bfd
*abfd
,
5070 struct bfd_link_info
*info
,
5071 Elf_Internal_Sym
*sym
,
5072 const char **namep ATTRIBUTE_UNUSED
,
5073 flagword
*flagsp ATTRIBUTE_UNUSED
,
5079 if (sym
->st_shndx
== SHN_COMMON
5080 && !info
->relocatable
5081 && sym
->st_size
<= elf_gp_size (abfd
)
5082 && is_nios2_elf_target (info
->output_bfd
->xvec
))
5084 /* Common symbols less than or equal to -G nn bytes are automatically
5086 struct elf32_nios2_link_hash_table
*htab
;
5088 htab
= elf32_nios2_hash_table (info
);
5089 if (htab
->sbss
== NULL
)
5091 flagword flags
= SEC_IS_COMMON
| SEC_LINKER_CREATED
;
5093 dynobj
= elf_hash_table (info
)->dynobj
;
5097 htab
->sbss
= bfd_make_section_anyway_with_flags (dynobj
, ".sbss",
5099 if (htab
->sbss
== NULL
)
5104 *valp
= sym
->st_size
;
5110 /* Implement elf_backend_can_make_relative_eh_frame:
5111 Decide whether to attempt to turn absptr or lsda encodings in
5112 shared libraries into pcrel within the given input section. */
5114 nios2_elf32_can_make_relative_eh_frame (bfd
*input_bfd ATTRIBUTE_UNUSED
,
5115 struct bfd_link_info
*info
5117 asection
*eh_frame_section
5120 /* We can't use PC-relative encodings in the .eh_frame section. */
5124 /* Implement elf_backend_special_sections. */
5125 const struct bfd_elf_special_section elf32_nios2_special_sections
[] =
5127 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS
,
5128 SHF_ALLOC
+ SHF_WRITE
+ SHF_NIOS2_GPREL
},
5129 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS
,
5130 SHF_ALLOC
+ SHF_WRITE
+ SHF_NIOS2_GPREL
},
5131 { NULL
, 0, 0, 0, 0 }
5134 #define ELF_ARCH bfd_arch_nios2
5135 #define ELF_TARGET_ID NIOS2_ELF_DATA
5136 #define ELF_MACHINE_CODE EM_ALTERA_NIOS2
5138 /* The Nios II MMU uses a 4K page size. */
5140 #define ELF_MAXPAGESIZE 0x1000
5142 #define bfd_elf32_bfd_link_hash_table_create \
5143 nios2_elf32_link_hash_table_create
5144 #define bfd_elf32_bfd_link_hash_table_free \
5145 nios2_elf32_link_hash_table_free
5147 /* Relocation table lookup macros. */
5149 #define bfd_elf32_bfd_reloc_type_lookup nios2_elf32_bfd_reloc_type_lookup
5150 #define bfd_elf32_bfd_reloc_name_lookup nios2_elf32_bfd_reloc_name_lookup
5152 /* JUMP_TABLE_LINK macros. */
5154 /* elf_info_to_howto (using RELA relocations). */
5156 #define elf_info_to_howto nios2_elf32_info_to_howto
5158 /* elf backend functions. */
5160 #define elf_backend_can_gc_sections 1
5161 #define elf_backend_can_refcount 1
5162 #define elf_backend_plt_readonly 1
5163 #define elf_backend_want_got_plt 1
5164 #define elf_backend_rela_normal 1
5166 #define elf_backend_relocate_section nios2_elf32_relocate_section
5167 #define elf_backend_section_flags nios2_elf32_section_flags
5168 #define elf_backend_fake_sections nios2_elf32_fake_sections
5169 #define elf_backend_check_relocs nios2_elf32_check_relocs
5171 #define elf_backend_gc_mark_hook nios2_elf32_gc_mark_hook
5172 #define elf_backend_gc_sweep_hook nios2_elf32_gc_sweep_hook
5173 #define elf_backend_create_dynamic_sections \
5174 nios2_elf32_create_dynamic_sections
5175 #define elf_backend_finish_dynamic_symbol nios2_elf32_finish_dynamic_symbol
5176 #define elf_backend_finish_dynamic_sections \
5177 nios2_elf32_finish_dynamic_sections
5178 #define elf_backend_adjust_dynamic_symbol nios2_elf32_adjust_dynamic_symbol
5179 #define elf_backend_reloc_type_class nios2_elf32_reloc_type_class
5180 #define elf_backend_size_dynamic_sections nios2_elf32_size_dynamic_sections
5181 #define elf_backend_add_symbol_hook nios2_elf_add_symbol_hook
5182 #define elf_backend_copy_indirect_symbol nios2_elf32_copy_indirect_symbol
5184 #define elf_backend_grok_prstatus nios2_grok_prstatus
5185 #define elf_backend_grok_psinfo nios2_grok_psinfo
5187 #undef elf_backend_can_make_relative_eh_frame
5188 #define elf_backend_can_make_relative_eh_frame \
5189 nios2_elf32_can_make_relative_eh_frame
5191 #define elf_backend_special_sections elf32_nios2_special_sections
5193 #define TARGET_LITTLE_SYM bfd_elf32_littlenios2_vec
5194 #define TARGET_LITTLE_NAME "elf32-littlenios2"
5195 #define TARGET_BIG_SYM bfd_elf32_bignios2_vec
5196 #define TARGET_BIG_NAME "elf32-bignios2"
5198 #define elf_backend_got_header_size 12
5200 #include "elf32-target.h"