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"
34 /* Use RELA relocations. */
43 /* Forward declarations. */
44 static bfd_reloc_status_type nios2_elf32_ignore_reloc
45 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
46 static bfd_reloc_status_type nios2_elf32_hi16_relocate
47 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
48 static bfd_reloc_status_type nios2_elf32_lo16_relocate
49 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
50 static bfd_reloc_status_type nios2_elf32_hiadj16_relocate
51 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
52 static bfd_reloc_status_type nios2_elf32_pcrel_lo16_relocate
53 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
54 static bfd_reloc_status_type nios2_elf32_pcrel_hiadj16_relocate
55 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
56 static bfd_reloc_status_type nios2_elf32_pcrel16_relocate
57 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
58 static bfd_reloc_status_type nios2_elf32_call26_relocate
59 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
60 static bfd_reloc_status_type nios2_elf32_gprel_relocate
61 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
62 static bfd_reloc_status_type nios2_elf32_ujmp_relocate
63 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
64 static bfd_reloc_status_type nios2_elf32_cjmp_relocate
65 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
66 static bfd_reloc_status_type nios2_elf32_callr_relocate
67 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
70 extern const bfd_target bfd_elf32_littlenios2_vec;
71 extern const bfd_target bfd_elf32_bignios2_vec;
73 /* Offset of tp and dtp pointers from start of TLS block. */
74 #define TP_OFFSET 0x7000
75 #define DTP_OFFSET 0x8000
77 /* The relocation table used for SHT_REL sections. */
78 static reloc_howto_type elf_nios2_howto_table_rel[] = {
80 HOWTO (R_NIOS2_NONE, /* type */
82 0, /* size (0 = byte, 1 = short, 2 = long) */
84 FALSE, /* pc_relative */
86 complain_overflow_dont, /* complain_on_overflow */
87 bfd_elf_generic_reloc, /* special_function */
88 "R_NIOS2_NONE", /* name */
89 FALSE, /* partial_inplace */
92 FALSE), /* pcrel_offset */
94 /* 16-bit signed immediate relocation. */
95 HOWTO (R_NIOS2_S16, /* type */
97 2, /* size (0 = byte, 1 = short, 2 = long) */
99 FALSE, /* pc_relative */
101 complain_overflow_signed, /* complain on overflow */
102 bfd_elf_generic_reloc, /* special function */
103 "R_NIOS2_S16", /* name */
104 FALSE, /* partial_inplace */
105 0x003fffc0, /* src_mask */
106 0x003fffc0, /* dest_mask */
107 FALSE), /* pcrel_offset */
109 /* 16-bit unsigned immediate relocation. */
110 HOWTO (R_NIOS2_U16, /* type */
112 2, /* size (0 = byte, 1 = short, 2 = long) */
114 FALSE, /* pc_relative */
116 complain_overflow_unsigned, /* complain on overflow */
117 bfd_elf_generic_reloc, /* special function */
118 "R_NIOS2_U16", /* name */
119 FALSE, /* partial_inplace */
120 0x003fffc0, /* src_mask */
121 0x003fffc0, /* dest_mask */
122 FALSE), /* pcrel_offset */
124 HOWTO (R_NIOS2_PCREL16, /* type */
126 2, /* size (0 = byte, 1 = short, 2 = long) */
128 TRUE, /* pc_relative */
130 complain_overflow_signed, /* complain on overflow */
131 nios2_elf32_pcrel16_relocate, /* special function */
132 "R_NIOS2_PCREL16", /* name */
133 FALSE, /* partial_inplace */
134 0x003fffc0, /* src_mask */
135 0x003fffc0, /* dest_mask */
136 TRUE), /* pcrel_offset */
138 HOWTO (R_NIOS2_CALL26, /* type */
140 2, /* size (0 = byte, 1 = short, 2 = long) */
142 FALSE, /* pc_relative */
144 complain_overflow_dont, /* complain on overflow */
145 nios2_elf32_call26_relocate, /* special function */
146 "R_NIOS2_CALL26", /* name */
147 FALSE, /* partial_inplace */
148 0xffffffc0, /* src_mask */
149 0xffffffc0, /* dst_mask */
150 FALSE), /* pcrel_offset */
158 complain_overflow_bitfield,
159 bfd_elf_generic_reloc,
166 HOWTO (R_NIOS2_CACHE_OPX,
172 complain_overflow_bitfield,
173 bfd_elf_generic_reloc,
186 complain_overflow_bitfield,
187 bfd_elf_generic_reloc,
200 complain_overflow_bitfield,
201 bfd_elf_generic_reloc,
214 complain_overflow_dont,
215 nios2_elf32_hi16_relocate,
228 complain_overflow_dont,
229 nios2_elf32_lo16_relocate,
236 HOWTO (R_NIOS2_HIADJ16,
242 complain_overflow_dont,
243 nios2_elf32_hiadj16_relocate,
250 HOWTO (R_NIOS2_BFD_RELOC_32,
256 complain_overflow_dont,
257 bfd_elf_generic_reloc,
258 "R_NIOS2_BFD_RELOC32",
264 HOWTO (R_NIOS2_BFD_RELOC_16,
270 complain_overflow_bitfield,
271 bfd_elf_generic_reloc,
272 "R_NIOS2_BFD_RELOC16",
278 HOWTO (R_NIOS2_BFD_RELOC_8,
284 complain_overflow_bitfield,
285 bfd_elf_generic_reloc,
286 "R_NIOS2_BFD_RELOC8",
292 HOWTO (R_NIOS2_GPREL,
298 complain_overflow_dont,
299 nios2_elf32_gprel_relocate,
306 HOWTO (R_NIOS2_GNU_VTINHERIT,
312 complain_overflow_dont,
314 "R_NIOS2_GNU_VTINHERIT",
320 HOWTO (R_NIOS2_GNU_VTENTRY,
326 complain_overflow_dont,
327 _bfd_elf_rel_vtable_reloc_fn,
328 "R_NIOS2_GNU_VTENTRY",
340 complain_overflow_dont,
341 nios2_elf32_ujmp_relocate,
354 complain_overflow_dont,
355 nios2_elf32_cjmp_relocate,
362 HOWTO (R_NIOS2_CALLR,
368 complain_overflow_dont,
369 nios2_elf32_callr_relocate,
376 HOWTO (R_NIOS2_ALIGN,
382 complain_overflow_dont,
383 nios2_elf32_ignore_reloc,
391 HOWTO (R_NIOS2_GOT16,
397 complain_overflow_bitfield,
398 bfd_elf_generic_reloc,
405 HOWTO (R_NIOS2_CALL16,
411 complain_overflow_bitfield,
412 bfd_elf_generic_reloc,
419 HOWTO (R_NIOS2_GOTOFF_LO,
425 complain_overflow_dont,
426 bfd_elf_generic_reloc,
433 HOWTO (R_NIOS2_GOTOFF_HA,
439 complain_overflow_dont,
440 bfd_elf_generic_reloc,
447 HOWTO (R_NIOS2_PCREL_LO,
453 complain_overflow_dont,
454 nios2_elf32_pcrel_lo16_relocate,
461 HOWTO (R_NIOS2_PCREL_HA,
465 FALSE, /* This is a PC-relative relocation, but we need to subtract
466 PC ourselves before the HIADJ. */
468 complain_overflow_dont,
469 nios2_elf32_pcrel_hiadj16_relocate,
476 HOWTO (R_NIOS2_TLS_GD16,
482 complain_overflow_bitfield,
483 bfd_elf_generic_reloc,
490 HOWTO (R_NIOS2_TLS_LDM16,
496 complain_overflow_bitfield,
497 bfd_elf_generic_reloc,
504 HOWTO (R_NIOS2_TLS_LDO16,
510 complain_overflow_bitfield,
511 bfd_elf_generic_reloc,
518 HOWTO (R_NIOS2_TLS_IE16,
524 complain_overflow_bitfield,
525 bfd_elf_generic_reloc,
532 HOWTO (R_NIOS2_TLS_LE16,
538 complain_overflow_bitfield,
539 bfd_elf_generic_reloc,
546 HOWTO (R_NIOS2_TLS_DTPMOD,
552 complain_overflow_dont,
553 bfd_elf_generic_reloc,
554 "R_NIOS2_TLS_DTPMOD",
560 HOWTO (R_NIOS2_TLS_DTPREL,
566 complain_overflow_dont,
567 bfd_elf_generic_reloc,
568 "R_NIOS2_TLS_DTPREL",
574 HOWTO (R_NIOS2_TLS_TPREL,
580 complain_overflow_dont,
581 bfd_elf_generic_reloc,
594 complain_overflow_dont,
595 bfd_elf_generic_reloc,
602 HOWTO (R_NIOS2_GLOB_DAT,
608 complain_overflow_dont,
609 bfd_elf_generic_reloc,
616 HOWTO (R_NIOS2_JUMP_SLOT,
622 complain_overflow_dont,
623 bfd_elf_generic_reloc,
630 HOWTO (R_NIOS2_RELATIVE,
636 complain_overflow_dont,
637 bfd_elf_generic_reloc,
644 HOWTO (R_NIOS2_GOTOFF,
650 complain_overflow_dont,
651 bfd_elf_generic_reloc,
658 /* Add other relocations here. */
661 static unsigned char elf_code_to_howto_index[R_NIOS2_ILLEGAL + 1];
663 /* Return the howto for relocation RTYPE. */
664 static reloc_howto_type *
665 lookup_howto (unsigned int rtype)
667 static int initialized = 0;
669 int howto_tbl_size = (int) (sizeof (elf_nios2_howto_table_rel)
670 / sizeof (elf_nios2_howto_table_rel[0]));
675 memset (elf_code_to_howto_index, 0xff,
676 sizeof (elf_code_to_howto_index));
677 for (i = 0; i < howto_tbl_size; i++)
678 elf_code_to_howto_index[elf_nios2_howto_table_rel[i].type] = i;
681 BFD_ASSERT (rtype <= R_NIOS2_ILLEGAL);
682 i = elf_code_to_howto_index[rtype];
683 if (i >= howto_tbl_size)
685 return elf_nios2_howto_table_rel + i;
688 /* Map for converting BFD reloc types to Nios II reloc types. */
691 bfd_reloc_code_real_type bfd_val;
692 enum elf_nios2_reloc_type elf_val;
695 static const struct elf_reloc_map nios2_reloc_map[] = {
696 {BFD_RELOC_NIOS2_S16, R_NIOS2_S16},
697 {BFD_RELOC_NIOS2_U16, R_NIOS2_U16},
698 {BFD_RELOC_16_PCREL, R_NIOS2_PCREL16},
699 {BFD_RELOC_NIOS2_CALL26, R_NIOS2_CALL26},
700 {BFD_RELOC_NIOS2_IMM5, R_NIOS2_IMM5},
701 {BFD_RELOC_NIOS2_CACHE_OPX, R_NIOS2_CACHE_OPX},
702 {BFD_RELOC_NIOS2_IMM6, R_NIOS2_IMM6},
703 {BFD_RELOC_NIOS2_IMM8, R_NIOS2_IMM8},
704 {BFD_RELOC_NIOS2_HI16, R_NIOS2_HI16},
705 {BFD_RELOC_NIOS2_LO16, R_NIOS2_LO16},
706 {BFD_RELOC_NIOS2_HIADJ16, R_NIOS2_HIADJ16},
707 {BFD_RELOC_32, R_NIOS2_BFD_RELOC_32},
708 {BFD_RELOC_16, R_NIOS2_BFD_RELOC_16},
709 {BFD_RELOC_8, R_NIOS2_BFD_RELOC_8},
710 {BFD_RELOC_NIOS2_GPREL, R_NIOS2_GPREL},
711 {BFD_RELOC_VTABLE_INHERIT, R_NIOS2_GNU_VTINHERIT},
712 {BFD_RELOC_VTABLE_ENTRY, R_NIOS2_GNU_VTENTRY},
713 {BFD_RELOC_NIOS2_UJMP, R_NIOS2_UJMP},
714 {BFD_RELOC_NIOS2_CJMP, R_NIOS2_CJMP},
715 {BFD_RELOC_NIOS2_CALLR, R_NIOS2_CALLR},
716 {BFD_RELOC_NIOS2_ALIGN, R_NIOS2_ALIGN},
717 {BFD_RELOC_NIOS2_GOT16, R_NIOS2_GOT16},
718 {BFD_RELOC_NIOS2_CALL16, R_NIOS2_CALL16},
719 {BFD_RELOC_NIOS2_GOTOFF_LO, R_NIOS2_GOTOFF_LO},
720 {BFD_RELOC_NIOS2_GOTOFF_HA, R_NIOS2_GOTOFF_HA},
721 {BFD_RELOC_NIOS2_PCREL_LO, R_NIOS2_PCREL_LO},
722 {BFD_RELOC_NIOS2_PCREL_HA, R_NIOS2_PCREL_HA},
723 {BFD_RELOC_NIOS2_TLS_GD16, R_NIOS2_TLS_GD16},
724 {BFD_RELOC_NIOS2_TLS_LDM16, R_NIOS2_TLS_LDM16},
725 {BFD_RELOC_NIOS2_TLS_LDO16, R_NIOS2_TLS_LDO16},
726 {BFD_RELOC_NIOS2_TLS_IE16, R_NIOS2_TLS_IE16},
727 {BFD_RELOC_NIOS2_TLS_LE16, R_NIOS2_TLS_LE16},
728 {BFD_RELOC_NIOS2_TLS_DTPMOD, R_NIOS2_TLS_DTPMOD},
729 {BFD_RELOC_NIOS2_TLS_DTPREL, R_NIOS2_TLS_DTPREL},
730 {BFD_RELOC_NIOS2_TLS_TPREL, R_NIOS2_TLS_TPREL},
731 {BFD_RELOC_NIOS2_COPY, R_NIOS2_COPY},
732 {BFD_RELOC_NIOS2_GLOB_DAT, R_NIOS2_GLOB_DAT},
733 {BFD_RELOC_NIOS2_JUMP_SLOT, R_NIOS2_JUMP_SLOT},
734 {BFD_RELOC_NIOS2_RELATIVE, R_NIOS2_RELATIVE},
735 {BFD_RELOC_NIOS2_GOTOFF, R_NIOS2_GOTOFF}
738 /* The Nios II linker needs to keep track of the number of relocs that it
739 decides to copy as dynamic relocs in check_relocs for each symbol.
740 This is so that it can later discard them if they are found to be
741 unnecessary. We store the information in a field extending the
742 regular ELF linker hash table. */
744 struct elf32_nios2_dyn_relocs
746 struct elf32_nios2_dyn_relocs *next;
748 /* The input section of the reloc. */
751 /* Total number of relocs copied for the input section. */
754 /* Number of pc-relative relocs copied for the input section. */
755 bfd_size_type pc_count;
758 /* Nios II ELF linker hash entry. */
760 struct elf32_nios2_link_hash_entry
762 struct elf_link_hash_entry root;
764 /* Track dynamic relocs copied for this symbol. */
765 struct elf32_nios2_dyn_relocs *dyn_relocs;
767 #define GOT_UNKNOWN 0
771 unsigned char tls_type;
773 /* We need to detect and take special action for symbols which are only
774 referenced with %call() and not with %got(). Such symbols do not need
775 a dynamic GOT reloc in shared objects, only a dynamic PLT reloc. Lazy
776 linking will not work if the dynamic GOT reloc exists.
777 To check for this condition efficiently, we compare got_types_used against
779 (got_types_used & (GOT16_USED | CALL16_USED)) == CALL16_USED. */
781 #define CALL16_USED 2
782 unsigned char got_types_used;
785 #define elf32_nios2_hash_entry(ent) \
786 ((struct elf32_nios2_link_hash_entry *) (ent))
788 /* Get the Nios II elf linker hash table from a link_info structure. */
789 #define elf32_nios2_hash_table(info) \
790 ((struct elf32_nios2_link_hash_table *) ((info)->hash))
792 /* Nios II ELF linker hash table. */
793 struct elf32_nios2_link_hash_table
795 /* The main hash table. */
796 struct elf_link_hash_table root;
798 /* Short-cuts to get to dynamic linker sections. */
804 bfd_signed_vma refcount;
808 /* Small local sym cache. */
809 struct sym_cache sym_cache;
814 struct nios2_elf32_obj_tdata
816 struct elf_obj_tdata root;
818 /* tls_type for each local got entry. */
819 char *local_got_tls_type;
821 /* TRUE if TLS GD relocs have been seen for this object. */
822 bfd_boolean has_tlsgd;
825 #define elf32_nios2_tdata(abfd) \
826 ((struct nios2_elf32_obj_tdata *) (abfd)->tdata.any)
828 #define elf32_nios2_local_got_tls_type(abfd) \
829 (elf32_nios2_tdata (abfd)->local_got_tls_type)
831 /* The name of the dynamic interpreter. This is put in the .interp
833 #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
835 /* PLT implementation for position-dependent code. */
836 static const bfd_vma nios2_plt_entry[] = { /* .PLTn: */
837 0x03c00034, /* movhi r15, %hiadj(plt_got_slot_address) */
838 0x7bc00017, /* ldw r15, %lo(plt_got_slot_address)(r15) */
839 0x7800683a /* jmp r15 */
842 static const bfd_vma nios2_plt0_entry[] = { /* .PLTresolve */
843 0x03800034, /* movhi r14, %hiadj(res_0) */
844 0x73800004, /* addi r14, r14, %lo(res_0) */
845 0x7b9fc83a, /* sub r15, r15, r14 */
846 0x03400034, /* movhi r13, %hiadj(_GLOBAL_OFFSET_TABLE_) */
847 0x6b800017, /* ldw r14, %lo(_GLOBAL_OFFSET_TABLE_+4)(r13) */
848 0x6b400017, /* ldw r13, %lo(_GLOBAL_OFFSET_TABLE_+8)(r13) */
849 0x6800683a /* jmp r13 */
852 /* PLT implementation for position-independent code. */
853 static const bfd_vma nios2_so_plt_entry[] = { /* .PLTn */
854 0x03c00034, /* movhi r15, %hiadj(index * 4) */
855 0x7bc00004, /* addi r15, r15, %lo(index * 4) */
856 0x00000006 /* br .PLTresolve */
859 static const bfd_vma nios2_so_plt0_entry[] = { /* .PLTresolve */
860 0x001ce03a, /* nextpc r14 */
861 0x03400034, /* movhi r13, %hiadj(_GLOBAL_OFFSET_TABLE_) */
862 0x6b9b883a, /* add r13, r13, r14 */
863 0x6b800017, /* ldw r14, %lo(_GLOBAL_OFFSET_TABLE_+4)(r13) */
864 0x6b400017, /* ldw r13, %lo(_GLOBAL_OFFSET_TABLE_+8)(r13) */
865 0x6800683a /* jmp r13 */
868 /* Implement elf_backend_grok_prstatus:
869 Support for core dump NOTE sections. */
871 nios2_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
876 switch (note->descsz)
881 case 212: /* Linux/Nios II */
883 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
886 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 24);
895 /* Make a ".reg/999" section. */
896 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
897 size, note->descpos + offset);
900 /* Implement elf_backend_grok_psinfo. */
902 nios2_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
904 switch (note->descsz)
909 case 124: /* Linux/Nios II elf_prpsinfo */
910 elf_tdata (abfd)->core->program
911 = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
912 elf_tdata (abfd)->core->command
913 = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
916 /* Note that for some reason, a spurious space is tacked
917 onto the end of the args in some (at least one anyway)
918 implementations, so strip it off if it exists. */
921 char *command = elf_tdata (abfd)->core->command;
922 int n = strlen (command);
924 if (0 < n && command[n - 1] == ' ')
925 command[n - 1] = '\0';
931 /* Create an entry in a Nios II ELF linker hash table. */
932 static struct bfd_hash_entry *
933 link_hash_newfunc (struct bfd_hash_entry *entry,
934 struct bfd_hash_table *table, const char *string)
936 /* Allocate the structure if it has not already been allocated by a
940 entry = bfd_hash_allocate (table,
941 sizeof (struct elf32_nios2_link_hash_entry));
946 /* Call the allocation method of the superclass. */
947 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
950 struct elf32_nios2_link_hash_entry *eh;
952 eh = (struct elf32_nios2_link_hash_entry *) entry;
953 eh->dyn_relocs = NULL;
954 eh->tls_type = GOT_UNKNOWN;
955 eh->got_types_used = 0;
961 /* Implement bfd_elf32_bfd_reloc_type_lookup:
962 Given a BFD reloc type, return a howto structure. */
963 static reloc_howto_type *
964 nios2_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
965 bfd_reloc_code_real_type code)
969 i < (int) (sizeof (nios2_reloc_map) / sizeof (struct elf_reloc_map));
971 if (nios2_reloc_map[i].bfd_val == code)
972 return &elf_nios2_howto_table_rel[(int) nios2_reloc_map[i].elf_val];
976 /* Implement bfd_elf32_bfd_reloc_name_lookup:
977 Given a reloc name, return a howto structure. */
978 static reloc_howto_type *
979 nios2_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
984 i < (sizeof (elf_nios2_howto_table_rel)
985 / sizeof (elf_nios2_howto_table_rel[0]));
987 if (elf_nios2_howto_table_rel[i].name
988 && strcasecmp (elf_nios2_howto_table_rel[i].name, r_name) == 0)
989 return &elf_nios2_howto_table_rel[i];
994 /* Implement elf_info_to_howto:
995 Given a ELF32 relocation, fill in a arelent structure. */
997 nios2_elf32_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
998 Elf_Internal_Rela *dst)
1000 unsigned int r_type;
1002 r_type = ELF32_R_TYPE (dst->r_info);
1003 BFD_ASSERT (r_type < R_NIOS2_ILLEGAL);
1004 cache_ptr->howto = &elf_nios2_howto_table_rel[r_type];
1007 /* Return the base VMA address which should be subtracted from real addresses
1008 when resolving @dtpoff relocation.
1009 This is PT_TLS segment p_vaddr. */
1011 dtpoff_base (struct bfd_link_info *info)
1013 /* If tls_sec is NULL, we should have signalled an error already. */
1014 if (elf_hash_table (info)->tls_sec == NULL)
1016 return elf_hash_table (info)->tls_sec->vma;
1019 /* Return the relocation value for @tpoff relocation
1020 if STT_TLS virtual address is ADDRESS. */
1022 tpoff (struct bfd_link_info *info, bfd_vma address)
1024 struct elf_link_hash_table *htab = elf_hash_table (info);
1026 /* If tls_sec is NULL, we should have signalled an error already. */
1027 if (htab->tls_sec == NULL)
1029 return address - htab->tls_sec->vma;
1032 /* Set the GP value for OUTPUT_BFD. Returns FALSE if this is a
1033 dangerous relocation. */
1035 nios2_elf_assign_gp (bfd *output_bfd, bfd_vma *pgp, struct bfd_link_info *info)
1038 bfd_boolean gp_found;
1039 struct bfd_hash_entry *h;
1040 struct bfd_link_hash_entry *lh;
1042 /* If we've already figured out what GP will be, just return it. */
1043 *pgp = _bfd_get_gp_value (output_bfd);
1047 h = bfd_hash_lookup (&info->hash->table, "_gp", FALSE, FALSE);
1048 lh = (struct bfd_link_hash_entry *) h;
1054 case bfd_link_hash_undefined:
1055 case bfd_link_hash_undefweak:
1056 case bfd_link_hash_common:
1059 case bfd_link_hash_defined:
1060 case bfd_link_hash_defweak:
1062 *pgp = lh->u.def.value;
1064 case bfd_link_hash_indirect:
1065 case bfd_link_hash_warning:
1067 /* @@FIXME ignoring warning for now */
1069 case bfd_link_hash_new:
1079 /* Only get the error once. */
1081 _bfd_set_gp_value (output_bfd, *pgp);
1085 _bfd_set_gp_value (output_bfd, *pgp);
1090 /* Retrieve the previously cached _gp pointer, returning bfd_reloc_dangerous
1091 if it's not available as we don't have a link_info pointer available here
1092 to look it up in the output symbol table. We don't need to adjust the
1093 symbol value for an external symbol if we are producing relocatable
1095 static bfd_reloc_status_type
1096 nios2_elf_final_gp (bfd *output_bfd, asymbol *symbol, bfd_boolean relocatable,
1097 char **error_message, bfd_vma *pgp)
1099 if (bfd_is_und_section (symbol->section) && !relocatable)
1102 return bfd_reloc_undefined;
1105 *pgp = _bfd_get_gp_value (output_bfd);
1106 if (*pgp == 0 && (!relocatable || (symbol->flags & BSF_SECTION_SYM) != 0))
1110 /* Make up a value. */
1111 *pgp = symbol->section->output_section->vma + 0x4000;
1112 _bfd_set_gp_value (output_bfd, *pgp);
1117 = (char *) _("global pointer relative relocation when _gp not defined");
1118 return bfd_reloc_dangerous;
1122 return bfd_reloc_ok;
1125 /* The usual way of loading a 32-bit constant into a Nios II register is to
1126 load the high 16 bits in one instruction and then add the low 16 bits with
1127 a signed add. This means that the high halfword needs to be adjusted to
1128 compensate for the sign bit of the low halfword. This function returns the
1129 adjusted high halfword for a given 32-bit constant. */
1131 bfd_vma hiadj (bfd_vma symbol_value)
1133 return ((symbol_value + 0x8000) >> 16) & 0xffff;
1136 /* Do the relocations that require special handling. */
1137 static bfd_reloc_status_type
1138 nios2_elf32_do_hi16_relocate (bfd *abfd, reloc_howto_type *howto,
1139 asection *input_section,
1140 bfd_byte *data, bfd_vma offset,
1141 bfd_vma symbol_value, bfd_vma addend)
1143 symbol_value = symbol_value + addend;
1145 symbol_value = (symbol_value >> 16) & 0xffff;
1146 return _bfd_final_link_relocate (howto, abfd, input_section,
1147 data, offset, symbol_value, addend);
1150 static bfd_reloc_status_type
1151 nios2_elf32_do_lo16_relocate (bfd *abfd, reloc_howto_type *howto,
1152 asection *input_section,
1153 bfd_byte *data, bfd_vma offset,
1154 bfd_vma symbol_value, bfd_vma addend)
1156 symbol_value = symbol_value + addend;
1158 symbol_value = symbol_value & 0xffff;
1159 return _bfd_final_link_relocate (howto, abfd, input_section,
1160 data, offset, symbol_value, addend);
1163 static bfd_reloc_status_type
1164 nios2_elf32_do_hiadj16_relocate (bfd *abfd, reloc_howto_type *howto,
1165 asection *input_section,
1166 bfd_byte *data, bfd_vma offset,
1167 bfd_vma symbol_value, bfd_vma addend)
1169 symbol_value = symbol_value + addend;
1171 symbol_value = hiadj(symbol_value);
1172 return _bfd_final_link_relocate (howto, abfd, input_section, data, offset,
1173 symbol_value, addend);
1176 static bfd_reloc_status_type
1177 nios2_elf32_do_pcrel_lo16_relocate (bfd *abfd, reloc_howto_type *howto,
1178 asection *input_section,
1179 bfd_byte *data, bfd_vma offset,
1180 bfd_vma symbol_value, bfd_vma addend)
1182 symbol_value = symbol_value + addend;
1184 symbol_value = symbol_value & 0xffff;
1185 return _bfd_final_link_relocate (howto, abfd, input_section,
1186 data, offset, symbol_value, addend);
1189 static bfd_reloc_status_type
1190 nios2_elf32_do_pcrel_hiadj16_relocate (bfd *abfd, reloc_howto_type *howto,
1191 asection *input_section,
1192 bfd_byte *data, bfd_vma offset,
1193 bfd_vma symbol_value, bfd_vma addend)
1195 symbol_value = symbol_value + addend;
1196 symbol_value -= (input_section->output_section->vma
1197 + input_section->output_offset);
1198 symbol_value -= offset;
1200 symbol_value = hiadj(symbol_value);
1201 return _bfd_final_link_relocate (howto, abfd, input_section, data, offset,
1202 symbol_value, addend);
1205 static bfd_reloc_status_type
1206 nios2_elf32_do_pcrel16_relocate (bfd *abfd, reloc_howto_type *howto,
1207 asection *input_section,
1208 bfd_byte *data, bfd_vma offset,
1209 bfd_vma symbol_value, bfd_vma addend)
1211 /* NIOS2 pc relative relocations are relative to the next 32-bit instruction
1212 so we need to subtract 4 before doing a final_link_relocate. */
1213 symbol_value = symbol_value + addend - 4;
1215 return _bfd_final_link_relocate (howto, abfd, input_section,
1216 data, offset, symbol_value, addend);
1219 static bfd_reloc_status_type
1220 nios2_elf32_do_call26_relocate (bfd *abfd, reloc_howto_type *howto,
1221 asection *input_section,
1222 bfd_byte *data, bfd_vma offset,
1223 bfd_vma symbol_value, bfd_vma addend)
1225 /* Check that the relocation is in the same page as the current address. */
1226 if (((symbol_value + addend) & 0xf0000000)
1227 != ((input_section->output_section->vma + offset) & 0xf0000000))
1228 return bfd_reloc_overflow;
1230 return _bfd_final_link_relocate (howto, abfd, input_section,
1231 data, offset, symbol_value, addend);
1234 static bfd_reloc_status_type
1235 nios2_elf32_do_gprel_relocate (bfd *abfd, reloc_howto_type *howto,
1236 asection *input_section,
1237 bfd_byte *data, bfd_vma offset,
1238 bfd_vma symbol_value, bfd_vma addend)
1240 /* Because we need the output_bfd, the special handling is done
1241 in nios2_elf32_relocate_section or in nios2_elf32_gprel_relocate. */
1242 return _bfd_final_link_relocate (howto, abfd, input_section,
1243 data, offset, symbol_value, addend);
1246 static bfd_reloc_status_type
1247 nios2_elf32_do_ujmp_relocate (bfd *abfd, reloc_howto_type *howto,
1248 asection *input_section,
1249 bfd_byte *data, bfd_vma offset,
1250 bfd_vma symbol_value, bfd_vma addend)
1252 bfd_vma symbol_lo16, symbol_hi16;
1253 bfd_reloc_status_type r;
1254 symbol_value = symbol_value + addend;
1256 symbol_hi16 = (symbol_value >> 16) & 0xffff;
1257 symbol_lo16 = symbol_value & 0xffff;
1259 r = _bfd_final_link_relocate (howto, abfd, input_section,
1260 data, offset, symbol_hi16, addend);
1262 if (r == bfd_reloc_ok)
1263 return _bfd_final_link_relocate (howto, abfd, input_section,
1264 data, offset + 4, symbol_lo16, addend);
1269 static bfd_reloc_status_type
1270 nios2_elf32_do_cjmp_relocate (bfd *abfd, reloc_howto_type *howto,
1271 asection *input_section,
1272 bfd_byte *data, bfd_vma offset,
1273 bfd_vma symbol_value, bfd_vma addend)
1275 bfd_vma symbol_lo16, symbol_hi16;
1276 bfd_reloc_status_type r;
1277 symbol_value = symbol_value + addend;
1279 symbol_hi16 = (symbol_value >> 16) & 0xffff;
1280 symbol_lo16 = symbol_value & 0xffff;
1282 r = _bfd_final_link_relocate (howto, abfd, input_section,
1283 data, offset, symbol_hi16, addend);
1285 if (r == bfd_reloc_ok)
1286 return _bfd_final_link_relocate (howto, abfd, input_section,
1287 data, offset + 4, symbol_lo16, addend);
1292 static bfd_reloc_status_type
1293 nios2_elf32_do_callr_relocate (bfd *abfd, reloc_howto_type *howto,
1294 asection *input_section,
1295 bfd_byte *data, bfd_vma offset,
1296 bfd_vma symbol_value, bfd_vma addend)
1298 bfd_vma symbol_lo16, symbol_hi16;
1299 bfd_reloc_status_type r;
1300 symbol_value = symbol_value + addend;
1302 symbol_hi16 = (symbol_value >> 16) & 0xffff;
1303 symbol_lo16 = symbol_value & 0xffff;
1305 r = _bfd_final_link_relocate (howto, abfd, input_section,
1306 data, offset, symbol_hi16, addend);
1308 if (r == bfd_reloc_ok)
1309 return _bfd_final_link_relocate (howto, abfd, input_section,
1310 data, offset + 4, symbol_lo16, addend);
1315 /* HOWTO handlers for relocations that require special handling. */
1317 /* This is for relocations used only when relaxing to ensure
1318 changes in size of section don't screw up .align. */
1319 static bfd_reloc_status_type
1320 nios2_elf32_ignore_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
1321 asymbol *symbol ATTRIBUTE_UNUSED,
1322 void *data ATTRIBUTE_UNUSED, asection *input_section,
1324 char **error_message ATTRIBUTE_UNUSED)
1326 if (output_bfd != NULL)
1327 reloc_entry->address += input_section->output_offset;
1328 return bfd_reloc_ok;
1331 static bfd_reloc_status_type
1332 nios2_elf32_hi16_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1333 void *data, asection *input_section,
1335 char **error_message ATTRIBUTE_UNUSED)
1337 /* This part is from bfd_elf_generic_reloc. */
1338 if (output_bfd != NULL
1339 && (symbol->flags & BSF_SECTION_SYM) == 0
1340 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
1342 reloc_entry->address += input_section->output_offset;
1343 return bfd_reloc_ok;
1346 if (output_bfd != NULL)
1347 /* FIXME: See bfd_perform_relocation. Is this right? */
1348 return bfd_reloc_continue;
1350 return nios2_elf32_do_hi16_relocate (abfd, reloc_entry->howto,
1352 data, reloc_entry->address,
1354 + symbol->section->output_section->vma
1355 + symbol->section->output_offset),
1356 reloc_entry->addend);
1359 static bfd_reloc_status_type
1360 nios2_elf32_lo16_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1361 void *data, asection *input_section,
1363 char **error_message ATTRIBUTE_UNUSED)
1365 /* This part is from bfd_elf_generic_reloc. */
1366 if (output_bfd != NULL
1367 && (symbol->flags & BSF_SECTION_SYM) == 0
1368 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
1370 reloc_entry->address += input_section->output_offset;
1371 return bfd_reloc_ok;
1374 if (output_bfd != NULL)
1375 /* FIXME: See bfd_perform_relocation. Is this right? */
1376 return bfd_reloc_continue;
1378 return nios2_elf32_do_lo16_relocate (abfd, reloc_entry->howto,
1380 data, reloc_entry->address,
1382 + symbol->section->output_section->vma
1383 + symbol->section->output_offset),
1384 reloc_entry->addend);
1387 static bfd_reloc_status_type
1388 nios2_elf32_hiadj16_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1389 void *data, asection *input_section,
1391 char **error_message ATTRIBUTE_UNUSED)
1393 /* This part is from bfd_elf_generic_reloc. */
1394 if (output_bfd != NULL
1395 && (symbol->flags & BSF_SECTION_SYM) == 0
1396 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
1398 reloc_entry->address += input_section->output_offset;
1399 return bfd_reloc_ok;
1402 if (output_bfd != NULL)
1403 /* FIXME: See bfd_perform_relocation. Is this right? */
1404 return bfd_reloc_continue;
1406 return nios2_elf32_do_hiadj16_relocate (abfd, reloc_entry->howto,
1408 data, reloc_entry->address,
1410 + symbol->section->output_section->vma
1411 + symbol->section->output_offset),
1412 reloc_entry->addend);
1415 static bfd_reloc_status_type
1416 nios2_elf32_pcrel_lo16_relocate (bfd *abfd, arelent *reloc_entry,
1417 asymbol *symbol, void *data,
1418 asection *input_section, bfd *output_bfd,
1419 char **error_message ATTRIBUTE_UNUSED)
1421 /* This part is from bfd_elf_generic_reloc. */
1422 if (output_bfd != NULL
1423 && (symbol->flags & BSF_SECTION_SYM) == 0
1424 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
1426 reloc_entry->address += input_section->output_offset;
1427 return bfd_reloc_ok;
1430 if (output_bfd != NULL)
1431 /* FIXME: See bfd_perform_relocation. Is this right? */
1432 return bfd_reloc_continue;
1434 return nios2_elf32_do_pcrel_lo16_relocate (
1435 abfd, reloc_entry->howto, input_section, data, reloc_entry->address,
1436 (symbol->value + symbol->section->output_section->vma
1437 + symbol->section->output_offset),
1438 reloc_entry->addend);
1441 static bfd_reloc_status_type
1442 nios2_elf32_pcrel_hiadj16_relocate (bfd *abfd, arelent *reloc_entry,
1443 asymbol *symbol, void *data,
1444 asection *input_section, bfd *output_bfd,
1445 char **error_message ATTRIBUTE_UNUSED)
1447 /* This part is from bfd_elf_generic_reloc. */
1448 if (output_bfd != NULL
1449 && (symbol->flags & BSF_SECTION_SYM) == 0
1450 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
1452 reloc_entry->address += input_section->output_offset;
1453 return bfd_reloc_ok;
1456 if (output_bfd != NULL)
1457 /* FIXME: See bfd_perform_relocation. Is this right? */
1458 return bfd_reloc_continue;
1460 return nios2_elf32_do_pcrel_hiadj16_relocate (
1461 abfd, reloc_entry->howto, input_section, data, reloc_entry->address,
1462 (symbol->value + symbol->section->output_section->vma
1463 + symbol->section->output_offset),
1464 reloc_entry->addend);
1467 static bfd_reloc_status_type
1468 nios2_elf32_pcrel16_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1469 void *data, asection *input_section,
1471 char **error_message ATTRIBUTE_UNUSED)
1473 /* This part is from bfd_elf_generic_reloc. */
1474 if (output_bfd != NULL
1475 && (symbol->flags & BSF_SECTION_SYM) == 0
1476 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
1478 reloc_entry->address += input_section->output_offset;
1479 return bfd_reloc_ok;
1482 if (output_bfd != NULL)
1483 /* FIXME: See bfd_perform_relocation. Is this right? */
1484 return bfd_reloc_continue;
1486 return nios2_elf32_do_pcrel16_relocate (abfd, reloc_entry->howto,
1488 data, reloc_entry->address,
1490 + symbol->section->output_section->vma
1491 + symbol->section->output_offset),
1492 reloc_entry->addend);
1495 static bfd_reloc_status_type
1496 nios2_elf32_call26_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1497 void *data, asection *input_section,
1499 char **error_message ATTRIBUTE_UNUSED)
1501 /* This part is from bfd_elf_generic_reloc. */
1502 if (output_bfd != NULL
1503 && (symbol->flags & BSF_SECTION_SYM) == 0
1504 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
1506 reloc_entry->address += input_section->output_offset;
1507 return bfd_reloc_ok;
1510 if (output_bfd != NULL)
1511 /* FIXME: See bfd_perform_relocation. Is this right? */
1512 return bfd_reloc_continue;
1514 return nios2_elf32_do_call26_relocate (abfd, reloc_entry->howto,
1516 data, reloc_entry->address,
1518 + symbol->section->output_section->vma
1519 + symbol->section->output_offset),
1520 reloc_entry->addend);
1523 static bfd_reloc_status_type
1524 nios2_elf32_gprel_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1525 void *data, asection *input_section,
1526 bfd *output_bfd, char **msg)
1530 bfd_reloc_status_type r;
1533 /* This part is from bfd_elf_generic_reloc. */
1534 if (output_bfd != NULL
1535 && (symbol->flags & BSF_SECTION_SYM) == 0
1536 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
1538 reloc_entry->address += input_section->output_offset;
1539 return bfd_reloc_ok;
1542 if (output_bfd != NULL)
1543 /* FIXME: See bfd_perform_relocation. Is this right? */
1544 return bfd_reloc_continue;
1546 relocation = (symbol->value
1547 + symbol->section->output_section->vma
1548 + symbol->section->output_offset);
1550 /* This assumes we've already cached the _gp symbol. */
1551 r = nios2_elf_final_gp (abfd, symbol, FALSE, msg, &gp);
1552 if (r == bfd_reloc_ok)
1554 relocation = relocation + reloc_entry->addend - gp;
1555 reloc_entry->addend = 0;
1556 if ((signed) relocation < -32768 || (signed) relocation > 32767)
1558 *msg = _("global pointer relative address out of range");
1559 r = bfd_reloc_outofrange;
1562 r = nios2_elf32_do_gprel_relocate (abfd, reloc_entry->howto,
1564 data, reloc_entry->address,
1565 relocation, reloc_entry->addend);
1571 static bfd_reloc_status_type
1572 nios2_elf32_ujmp_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1573 void *data, asection *input_section,
1574 bfd *output_bfd, char **msg ATTRIBUTE_UNUSED)
1576 /* This part is from bfd_elf_generic_reloc. */
1577 if (output_bfd != NULL
1578 && (symbol->flags & BSF_SECTION_SYM) == 0
1579 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
1581 reloc_entry->address += input_section->output_offset;
1582 return bfd_reloc_ok;
1585 if (output_bfd != NULL)
1586 /* FIXME: See bfd_perform_relocation. Is this right? */
1587 return bfd_reloc_continue;
1589 return nios2_elf32_do_ujmp_relocate (abfd, reloc_entry->howto,
1591 data, reloc_entry->address,
1593 + symbol->section->output_section->vma
1594 + symbol->section->output_offset),
1595 reloc_entry->addend);
1598 static bfd_reloc_status_type
1599 nios2_elf32_cjmp_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1600 void *data, asection *input_section,
1601 bfd *output_bfd, char **msg ATTRIBUTE_UNUSED)
1603 /* This part is from bfd_elf_generic_reloc. */
1604 if (output_bfd != NULL
1605 && (symbol->flags & BSF_SECTION_SYM) == 0
1606 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
1608 reloc_entry->address += input_section->output_offset;
1609 return bfd_reloc_ok;
1612 if (output_bfd != NULL)
1613 /* FIXME: See bfd_perform_relocation. Is this right? */
1614 return bfd_reloc_continue;
1616 return nios2_elf32_do_cjmp_relocate (abfd, reloc_entry->howto,
1618 data, reloc_entry->address,
1620 + symbol->section->output_section->vma
1621 + symbol->section->output_offset),
1622 reloc_entry->addend);
1625 static bfd_reloc_status_type
1626 nios2_elf32_callr_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1627 void *data, asection *input_section,
1628 bfd *output_bfd, char **msg ATTRIBUTE_UNUSED)
1630 /* This part is from bfd_elf_generic_reloc. */
1631 if (output_bfd != NULL
1632 && (symbol->flags & BSF_SECTION_SYM) == 0
1633 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
1635 reloc_entry->address += input_section->output_offset;
1636 return bfd_reloc_ok;
1639 if (output_bfd != NULL)
1640 /* FIXME: See bfd_perform_relocation. Is this right? */
1641 return bfd_reloc_continue;
1643 return nios2_elf32_do_callr_relocate (abfd, reloc_entry->howto,
1645 data, reloc_entry->address,
1647 + symbol->section->output_section->vma
1648 + symbol->section->output_offset),
1649 reloc_entry->addend);
1653 /* Implement elf_backend_relocate_section. */
1655 nios2_elf32_relocate_section (bfd *output_bfd,
1656 struct bfd_link_info *info,
1658 asection *input_section,
1660 Elf_Internal_Rela *relocs,
1661 Elf_Internal_Sym *local_syms,
1662 asection **local_sections)
1664 Elf_Internal_Shdr *symtab_hdr;
1665 struct elf_link_hash_entry **sym_hashes;
1666 Elf_Internal_Rela *rel;
1667 Elf_Internal_Rela *relend;
1668 struct elf32_nios2_link_hash_table *htab;
1671 asection *sreloc = NULL;
1672 bfd_vma *local_got_offsets;
1674 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1675 sym_hashes = elf_sym_hashes (input_bfd);
1676 relend = relocs + input_section->reloc_count;
1678 htab = elf32_nios2_hash_table (info);
1679 sgot = htab->root.sgot;
1680 splt = htab->root.splt;
1681 local_got_offsets = elf_local_got_offsets (input_bfd);
1683 for (rel = relocs; rel < relend; rel++)
1685 reloc_howto_type *howto;
1686 unsigned long r_symndx;
1687 Elf_Internal_Sym *sym;
1689 struct elf_link_hash_entry *h;
1690 struct elf32_nios2_link_hash_entry *eh;
1693 bfd_vma reloc_address;
1694 bfd_reloc_status_type r = bfd_reloc_ok;
1695 const char *name = NULL;
1699 const char* msg = (const char*) NULL;
1700 bfd_boolean unresolved_reloc;
1704 r_type = ELF32_R_TYPE (rel->r_info);
1705 r_symndx = ELF32_R_SYM (rel->r_info);
1707 howto = lookup_howto ((unsigned) ELF32_R_TYPE (rel->r_info));
1712 if (r_symndx < symtab_hdr->sh_info)
1714 sym = local_syms + r_symndx;
1715 sec = local_sections[r_symndx];
1716 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
1720 bfd_boolean warned, ignored;
1722 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
1723 r_symndx, symtab_hdr, sym_hashes,
1725 unresolved_reloc, warned, ignored);
1728 if (sec && discarded_section (sec))
1729 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
1730 rel, 1, relend, howto, 0, contents);
1732 /* Nothing more to do unless this is a final link. */
1733 if (info->relocatable)
1736 if (sec && sec->output_section)
1737 reloc_address = (sec->output_section->vma + sec->output_offset
1744 switch (howto->type)
1747 r = nios2_elf32_do_hi16_relocate (input_bfd, howto,
1749 contents, rel->r_offset,
1750 relocation, rel->r_addend);
1753 r = nios2_elf32_do_lo16_relocate (input_bfd, howto,
1755 contents, rel->r_offset,
1756 relocation, rel->r_addend);
1758 case R_NIOS2_PCREL_LO:
1759 r = nios2_elf32_do_pcrel_lo16_relocate (input_bfd, howto,
1766 case R_NIOS2_HIADJ16:
1767 r = nios2_elf32_do_hiadj16_relocate (input_bfd, howto,
1768 input_section, contents,
1769 rel->r_offset, relocation,
1772 case R_NIOS2_PCREL_HA:
1773 r = nios2_elf32_do_pcrel_hiadj16_relocate (input_bfd, howto,
1780 case R_NIOS2_PCREL16:
1781 r = nios2_elf32_do_pcrel16_relocate (input_bfd, howto,
1782 input_section, contents,
1783 rel->r_offset, relocation,
1787 /* Turns an absolute address into a gp-relative address. */
1788 if (!nios2_elf_assign_gp (output_bfd, &gp, info))
1790 format = _("global pointer relative relocation at address "
1791 "0x%08x when _gp not defined\n");
1792 sprintf (msgbuf, format, reloc_address);
1794 r = bfd_reloc_dangerous;
1798 bfd_vma symbol_address = rel->r_addend + relocation;
1799 relocation = relocation + rel->r_addend - gp;
1801 if (((signed) relocation < -32768
1802 || (signed) relocation > 32767)
1804 || h->root.type == bfd_link_hash_defined
1805 || h->root.type == bfd_link_hash_defweak))
1807 format = _("Unable to reach %s (at 0x%08x) from the "
1808 "global pointer (at 0x%08x) because the "
1809 "offset (%d) is out of the allowed range, "
1810 "-32678 to 32767.\n" );
1811 sprintf (msgbuf, format, name, symbol_address, gp,
1812 (signed)relocation);
1814 r = bfd_reloc_outofrange;
1817 r = _bfd_final_link_relocate (howto, input_bfd,
1818 input_section, contents,
1819 rel->r_offset, relocation,
1825 r = nios2_elf32_do_ujmp_relocate (input_bfd, howto,
1827 contents, rel->r_offset,
1828 relocation, rel->r_addend);
1831 r = nios2_elf32_do_cjmp_relocate (input_bfd, howto,
1833 contents, rel->r_offset,
1834 relocation, rel->r_addend);
1837 r = nios2_elf32_do_callr_relocate (input_bfd, howto,
1838 input_section, contents,
1839 rel->r_offset, relocation,
1842 case R_NIOS2_CALL26:
1843 /* If we have a call to an undefined weak symbol, we just want
1844 to stuff a zero in the bits of the call instruction and
1845 bypass the normal call26 relocation handling, because it'll
1846 diagnose an overflow error if address 0 isn't in the same
1847 256MB segment as the call site. Presumably the call
1848 should be guarded by a null check anyway. */
1849 if (h != NULL && h->root.type == bfd_link_hash_undefweak)
1851 BFD_ASSERT (relocation == 0 && rel->r_addend == 0);
1852 r = _bfd_final_link_relocate (howto, input_bfd,
1853 input_section, contents,
1854 rel->r_offset, relocation,
1858 /* Handle relocations which should use the PLT entry.
1859 NIOS2_BFD_RELOC_32 relocations will use the symbol's value,
1860 which may point to a PLT entry, but we don't need to handle
1861 that here. If we created a PLT entry, all branches in this
1862 object should go to it. */
1863 if (h != NULL && splt != NULL && h->plt.offset != (bfd_vma) -1)
1865 /* If we've created a .plt section, and assigned a PLT entry
1866 to this function, it should not be known to bind locally.
1867 If it were, we would have cleared the PLT entry. */
1868 BFD_ASSERT (!SYMBOL_CALLS_LOCAL (info, h));
1870 relocation = (splt->output_section->vma
1871 + splt->output_offset
1874 unresolved_reloc = FALSE;
1876 r = nios2_elf32_do_call26_relocate (input_bfd, howto,
1877 input_section, contents,
1878 rel->r_offset, relocation,
1883 /* For symmetry this would be
1884 r = nios2_elf32_do_ignore_reloc (input_bfd, howto,
1885 input_section, contents,
1886 rel->r_offset, relocation,
1888 but do_ignore_reloc would do no more than return
1893 case R_NIOS2_CALL16:
1894 /* Relocation is to the entry for this symbol in the
1895 global offset table. */
1898 r = bfd_reloc_notsupported;
1908 eh = (struct elf32_nios2_link_hash_entry *)h;
1909 use_plt = (eh->got_types_used == CALL16_USED
1910 && h->plt.offset != (bfd_vma) -1);
1912 off = h->got.offset;
1913 BFD_ASSERT (off != (bfd_vma) -1);
1914 dyn = elf_hash_table (info)->dynamic_sections_created;
1915 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
1917 && SYMBOL_REFERENCES_LOCAL (info, h))
1918 || (ELF_ST_VISIBILITY (h->other)
1919 && h->root.type == bfd_link_hash_undefweak))
1921 /* This is actually a static link, or it is a -Bsymbolic
1922 link and the symbol is defined locally. We must
1923 initialize this entry in the global offset table.
1924 Since the offset must always be a multiple of 4, we
1925 use the least significant bit to record whether we
1926 have initialized it already.
1928 When doing a dynamic link, we create a .rela.got
1929 relocation entry to initialize the value. This is
1930 done in the finish_dynamic_symbol routine. */
1935 bfd_put_32 (output_bfd, relocation,
1936 sgot->contents + off);
1941 unresolved_reloc = FALSE;
1945 BFD_ASSERT (local_got_offsets != NULL
1946 && local_got_offsets[r_symndx] != (bfd_vma) -1);
1948 off = local_got_offsets[r_symndx];
1950 /* The offset must always be a multiple of 4. We use the
1951 least significant bit to record whether we have already
1952 generated the necessary reloc. */
1957 bfd_put_32 (output_bfd, relocation,
1958 sgot->contents + off);
1963 Elf_Internal_Rela outrel;
1966 srelgot = htab->root.srelgot;
1967 BFD_ASSERT (srelgot != NULL);
1969 outrel.r_addend = relocation;
1970 outrel.r_offset = (sgot->output_section->vma
1971 + sgot->output_offset
1973 outrel.r_info = ELF32_R_INFO (0, R_NIOS2_RELATIVE);
1974 loc = srelgot->contents;
1975 loc += (srelgot->reloc_count++ *
1976 sizeof (Elf32_External_Rela));
1977 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1980 local_got_offsets[r_symndx] |= 1;
1984 if (use_plt && info->shared)
1986 off = ((h->plt.offset - 24) / 12 + 3) * 4;
1987 relocation = htab->root.sgotplt->output_offset + off;
1990 relocation = sgot->output_offset + off;
1992 /* This relocation does not use the addend. */
1995 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1996 contents, rel->r_offset,
1997 relocation, rel->r_addend);
2000 case R_NIOS2_GOTOFF_LO:
2001 case R_NIOS2_GOTOFF_HA:
2002 case R_NIOS2_GOTOFF:
2003 /* Relocation is relative to the start of the
2004 global offset table. */
2006 BFD_ASSERT (sgot != NULL);
2009 r = bfd_reloc_notsupported;
2013 /* Note that sgot->output_offset is not involved in this
2014 calculation. We always want the start of .got. If we
2015 define _GLOBAL_OFFSET_TABLE in a different way, as is
2016 permitted by the ABI, we might have to change this
2018 relocation -= sgot->output_section->vma;
2019 switch (howto->type)
2021 case R_NIOS2_GOTOFF_LO:
2022 r = nios2_elf32_do_lo16_relocate (input_bfd, howto,
2023 input_section, contents,
2024 rel->r_offset, relocation,
2027 case R_NIOS2_GOTOFF_HA:
2028 r = nios2_elf32_do_hiadj16_relocate (input_bfd, howto,
2029 input_section, contents,
2035 r = _bfd_final_link_relocate (howto, input_bfd,
2036 input_section, contents,
2037 rel->r_offset, relocation,
2043 case R_NIOS2_TLS_LDO16:
2044 relocation -= dtpoff_base (info) + DTP_OFFSET;
2046 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
2047 contents, rel->r_offset,
2048 relocation, rel->r_addend);
2050 case R_NIOS2_TLS_LDM16:
2051 if (htab->root.sgot == NULL)
2054 off = htab->tls_ldm_got.offset;
2060 /* If we don't know the module number, create a relocation
2064 Elf_Internal_Rela outrel;
2067 if (htab->root.srelgot == NULL)
2070 outrel.r_addend = 0;
2071 outrel.r_offset = (htab->root.sgot->output_section->vma
2072 + htab->root.sgot->output_offset
2074 outrel.r_info = ELF32_R_INFO (0, R_NIOS2_TLS_DTPMOD);
2076 loc = htab->root.srelgot->contents;
2077 loc += (htab->root.srelgot->reloc_count++
2078 * sizeof (Elf32_External_Rela));
2079 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
2082 bfd_put_32 (output_bfd, 1,
2083 htab->root.sgot->contents + off);
2085 htab->tls_ldm_got.offset |= 1;
2088 relocation = (htab->root.sgot->output_offset + off);
2090 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
2091 contents, rel->r_offset,
2092 relocation, rel->r_addend);
2095 case R_NIOS2_TLS_GD16:
2096 case R_NIOS2_TLS_IE16:
2101 if (htab->root.sgot == NULL)
2108 dyn = htab->root.dynamic_sections_created;
2109 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
2111 || !SYMBOL_REFERENCES_LOCAL (info, h)))
2113 unresolved_reloc = FALSE;
2116 off = h->got.offset;
2117 tls_type = (((struct elf32_nios2_link_hash_entry *) h)
2122 if (local_got_offsets == NULL)
2124 off = local_got_offsets[r_symndx];
2125 tls_type = (elf32_nios2_local_got_tls_type (input_bfd)
2129 if (tls_type == GOT_UNKNOWN)
2136 bfd_boolean need_relocs = FALSE;
2137 Elf_Internal_Rela outrel;
2138 bfd_byte *loc = NULL;
2141 /* The GOT entries have not been initialized yet. Do it
2142 now, and emit any relocations. If both an IE GOT and a
2143 GD GOT are necessary, we emit the GD first. */
2145 if ((info->shared || indx != 0)
2147 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2148 || h->root.type != bfd_link_hash_undefweak))
2151 if (htab->root.srelgot == NULL)
2153 loc = htab->root.srelgot->contents;
2154 loc += (htab->root.srelgot->reloc_count *
2155 sizeof (Elf32_External_Rela));
2158 if (tls_type & GOT_TLS_GD)
2162 outrel.r_addend = 0;
2163 outrel.r_offset = (htab->root.sgot->output_section->vma
2164 + htab->root.sgot->output_offset
2166 outrel.r_info = ELF32_R_INFO (indx,
2167 R_NIOS2_TLS_DTPMOD);
2169 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
2171 htab->root.srelgot->reloc_count++;
2172 loc += sizeof (Elf32_External_Rela);
2175 bfd_put_32 (output_bfd,
2176 (relocation - dtpoff_base (info) -
2178 htab->root.sgot->contents + cur_off + 4);
2181 outrel.r_addend = 0;
2182 outrel.r_info = ELF32_R_INFO (indx,
2183 R_NIOS2_TLS_DTPREL);
2184 outrel.r_offset += 4;
2186 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
2188 htab->root.srelgot->reloc_count++;
2189 loc += sizeof (Elf32_External_Rela);
2194 /* If we are not emitting relocations for a
2195 general dynamic reference, then we must be in a
2196 static link or an executable link with the
2197 symbol binding locally. Mark it as belonging
2198 to module 1, the executable. */
2199 bfd_put_32 (output_bfd, 1,
2200 htab->root.sgot->contents + cur_off);
2201 bfd_put_32 (output_bfd, (relocation -
2202 dtpoff_base (info) -
2204 htab->root.sgot->contents + cur_off + 4);
2210 if (tls_type & GOT_TLS_IE)
2215 outrel.r_addend = (relocation -
2216 dtpoff_base (info));
2218 outrel.r_addend = 0;
2219 outrel.r_offset = (htab->root.sgot->output_section->vma
2220 + htab->root.sgot->output_offset
2222 outrel.r_info = ELF32_R_INFO (indx,
2225 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
2227 htab->root.srelgot->reloc_count++;
2228 loc += sizeof (Elf32_External_Rela);
2231 bfd_put_32 (output_bfd, (tpoff (info, relocation)
2233 htab->root.sgot->contents + cur_off);
2240 local_got_offsets[r_symndx] |= 1;
2243 if ((tls_type & GOT_TLS_GD) && r_type != R_NIOS2_TLS_GD16)
2245 relocation = (htab->root.sgot->output_offset + off);
2247 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
2248 contents, rel->r_offset,
2249 relocation, rel->r_addend);
2253 case R_NIOS2_TLS_LE16:
2254 if (info->shared && !info->pie)
2256 (*_bfd_error_handler)
2257 (_("%B(%A+0x%lx): R_NIOS2_TLS_LE16 relocation not "
2258 "permitted in shared object"),
2259 input_bfd, input_section,
2260 (long) rel->r_offset, howto->name);
2264 relocation = tpoff (info, relocation) - TP_OFFSET;
2266 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
2267 contents, rel->r_offset,
2268 relocation, rel->r_addend);
2271 case R_NIOS2_BFD_RELOC_32:
2273 && (input_section->flags & SEC_ALLOC) != 0
2275 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2276 || h->root.type != bfd_link_hash_undefweak))
2278 Elf_Internal_Rela outrel;
2280 bfd_boolean skip, relocate;
2282 /* When generating a shared object, these relocations
2283 are copied into the output file to be resolved at run
2290 = _bfd_elf_section_offset (output_bfd, info,
2291 input_section, rel->r_offset);
2292 if (outrel.r_offset == (bfd_vma) -1)
2294 else if (outrel.r_offset == (bfd_vma) -2)
2295 skip = TRUE, relocate = TRUE;
2296 outrel.r_offset += (input_section->output_section->vma
2297 + input_section->output_offset);
2300 memset (&outrel, 0, sizeof outrel);
2305 || !h->def_regular))
2307 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
2308 outrel.r_addend = rel->r_addend;
2312 /* This symbol is local, or marked to become local. */
2313 outrel.r_addend = relocation + rel->r_addend;
2315 outrel.r_info = ELF32_R_INFO (0, R_NIOS2_RELATIVE);
2318 sreloc = elf_section_data (input_section)->sreloc;
2322 loc = sreloc->contents;
2323 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
2324 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
2326 /* This reloc will be computed at runtime, so there's no
2327 need to do anything now, except for R_NIOS2_BFD_RELOC_32
2328 relocations that have been turned into
2329 R_NIOS2_RELATIVE. */
2334 r = _bfd_final_link_relocate (howto, input_bfd,
2335 input_section, contents,
2336 rel->r_offset, relocation,
2340 case R_NIOS2_TLS_DTPREL:
2341 relocation -= dtpoff_base (info);
2345 r = _bfd_final_link_relocate (howto, input_bfd,
2346 input_section, contents,
2347 rel->r_offset, relocation,
2353 r = bfd_reloc_notsupported;
2355 if (r != bfd_reloc_ok)
2358 name = h->root.root.string;
2361 name = bfd_elf_string_from_elf_section (input_bfd,
2362 symtab_hdr->sh_link,
2364 if (name == NULL || *name == '\0')
2365 name = bfd_section_name (input_bfd, sec);
2370 case bfd_reloc_overflow:
2371 r = info->callbacks->reloc_overflow (info, NULL, name,
2372 howto->name, (bfd_vma) 0,
2373 input_bfd, input_section,
2377 case bfd_reloc_undefined:
2378 r = info->callbacks->undefined_symbol (info, name, input_bfd,
2380 rel->r_offset, TRUE);
2383 case bfd_reloc_outofrange:
2385 msg = _("relocation out of range");
2388 case bfd_reloc_notsupported:
2390 msg = _("unsupported relocation");
2393 case bfd_reloc_dangerous:
2395 msg = _("dangerous relocation");
2400 msg = _("unknown error");
2406 r = info->callbacks->warning
2407 (info, msg, name, input_bfd, input_section, rel->r_offset);
2415 /* Implement elf-backend_section_flags:
2416 Convert NIOS2 specific section flags to bfd internal section flags. */
2418 nios2_elf32_section_flags (flagword *flags, const Elf_Internal_Shdr *hdr)
2420 if (hdr->sh_flags & SHF_NIOS2_GPREL)
2421 *flags |= SEC_SMALL_DATA;
2426 /* Implement elf_backend_fake_sections:
2427 Set the correct type for an NIOS2 ELF section. We do this by the
2428 section name, which is a hack, but ought to work. */
2430 nios2_elf32_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
2431 Elf_Internal_Shdr *hdr, asection *sec)
2433 register const char *name = bfd_get_section_name (abfd, sec);
2435 if ((sec->flags & SEC_SMALL_DATA)
2436 || strcmp (name, ".sdata") == 0
2437 || strcmp (name, ".sbss") == 0
2438 || strcmp (name, ".lit4") == 0 || strcmp (name, ".lit8") == 0)
2439 hdr->sh_flags |= SHF_NIOS2_GPREL;
2444 /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
2445 shortcuts to them in our hash table. */
2447 create_got_section (bfd *dynobj, struct bfd_link_info *info)
2449 struct elf32_nios2_link_hash_table *htab;
2451 htab = elf32_nios2_hash_table (info);
2453 if (! _bfd_elf_create_got_section (dynobj, info))
2456 /* In order for the two loads in .PLTresolve to share the same %hiadj,
2457 _GLOBAL_OFFSET_TABLE_ must be aligned to a 16-byte boundary. */
2458 if (!bfd_set_section_alignment (dynobj, htab->root.sgotplt, 4))
2464 /* Implement elf_backend_create_dynamic_sections:
2465 Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
2466 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
2469 nios2_elf32_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
2471 struct elf32_nios2_link_hash_table *htab;
2473 htab = elf32_nios2_hash_table (info);
2474 if (!htab->root.sgot && !create_got_section (dynobj, info))
2477 _bfd_elf_create_dynamic_sections (dynobj, info);
2479 /* In order for the two loads in a shared object .PLTresolve to share the
2480 same %hiadj, the start of the PLT (as well as the GOT) must be aligned
2481 to a 16-byte boundary. This is because the addresses for these loads
2482 include the -(.plt+4) PIC correction. */
2483 if (!bfd_set_section_alignment (dynobj, htab->root.splt, 4))
2486 htab->sdynbss = bfd_get_linker_section (dynobj, ".dynbss");
2491 htab->srelbss = bfd_get_linker_section (dynobj, ".rela.bss");
2499 /* Implement elf_backend_copy_indirect_symbol:
2500 Copy the extra info we tack onto an elf_link_hash_entry. */
2502 nios2_elf32_copy_indirect_symbol (struct bfd_link_info *info,
2503 struct elf_link_hash_entry *dir,
2504 struct elf_link_hash_entry *ind)
2506 struct elf32_nios2_link_hash_entry *edir, *eind;
2508 edir = (struct elf32_nios2_link_hash_entry *) dir;
2509 eind = (struct elf32_nios2_link_hash_entry *) ind;
2511 if (eind->dyn_relocs != NULL)
2513 if (edir->dyn_relocs != NULL)
2515 struct elf32_nios2_dyn_relocs **pp;
2516 struct elf32_nios2_dyn_relocs *p;
2518 /* Add reloc counts against the indirect sym to the direct sym
2519 list. Merge any entries against the same section. */
2520 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
2522 struct elf32_nios2_dyn_relocs *q;
2524 for (q = edir->dyn_relocs; q != NULL; q = q->next)
2525 if (q->sec == p->sec)
2527 q->pc_count += p->pc_count;
2528 q->count += p->count;
2535 *pp = edir->dyn_relocs;
2538 edir->dyn_relocs = eind->dyn_relocs;
2539 eind->dyn_relocs = NULL;
2542 if (ind->root.type == bfd_link_hash_indirect
2543 && dir->got.refcount <= 0)
2545 edir->tls_type = eind->tls_type;
2546 eind->tls_type = GOT_UNKNOWN;
2549 edir->got_types_used |= eind->got_types_used;
2551 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
2554 /* Implement elf_backend_check_relocs:
2555 Look through the relocs for a section during the first phase. */
2557 nios2_elf32_check_relocs (bfd *abfd, struct bfd_link_info *info,
2558 asection *sec, const Elf_Internal_Rela *relocs)
2561 Elf_Internal_Shdr *symtab_hdr;
2562 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
2563 const Elf_Internal_Rela *rel;
2564 const Elf_Internal_Rela *rel_end;
2565 struct elf32_nios2_link_hash_table *htab;
2568 asection *sreloc = NULL;
2569 bfd_signed_vma *local_got_refcounts;
2571 if (info->relocatable)
2574 dynobj = elf_hash_table (info)->dynobj;
2575 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2576 sym_hashes = elf_sym_hashes (abfd);
2577 sym_hashes_end = (sym_hashes
2578 + symtab_hdr->sh_size / sizeof (Elf32_External_Sym));
2579 if (!elf_bad_symtab (abfd))
2580 sym_hashes_end -= symtab_hdr->sh_info;
2581 local_got_refcounts = elf_local_got_refcounts (abfd);
2583 htab = elf32_nios2_hash_table (info);
2584 sgot = htab->root.sgot;
2585 srelgot = htab->root.srelgot;
2587 rel_end = relocs + sec->reloc_count;
2588 for (rel = relocs; rel < rel_end; rel++)
2590 unsigned int r_type;
2591 struct elf_link_hash_entry *h;
2592 unsigned long r_symndx;
2594 r_symndx = ELF32_R_SYM (rel->r_info);
2595 if (r_symndx < symtab_hdr->sh_info)
2599 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2600 while (h->root.type == bfd_link_hash_indirect
2601 || h->root.type == bfd_link_hash_warning)
2602 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2604 /* PR15323, ref flags aren't set for references in the same
2606 h->root.non_ir_ref = 1;
2609 r_type = ELF32_R_TYPE (rel->r_info);
2614 case R_NIOS2_CALL16:
2615 case R_NIOS2_TLS_GD16:
2616 case R_NIOS2_TLS_IE16:
2617 /* This symbol requires a global offset table entry. */
2619 int tls_type, old_tls_type;
2625 case R_NIOS2_CALL16:
2626 tls_type = GOT_NORMAL;
2628 case R_NIOS2_TLS_GD16:
2629 tls_type = GOT_TLS_GD;
2631 case R_NIOS2_TLS_IE16:
2632 tls_type = GOT_TLS_IE;
2638 /* Create the .got section. */
2639 elf_hash_table (info)->dynobj = dynobj = abfd;
2640 nios2_elf32_create_dynamic_sections (dynobj, info);
2645 sgot = htab->root.sgot;
2646 BFD_ASSERT (sgot != NULL);
2650 && (h != NULL || info->shared))
2652 srelgot = htab->root.srelgot;
2653 BFD_ASSERT (srelgot != NULL);
2658 struct elf32_nios2_link_hash_entry *eh
2659 = (struct elf32_nios2_link_hash_entry *)h;
2661 old_tls_type = elf32_nios2_hash_entry(h)->tls_type;
2662 if (r_type == R_NIOS2_CALL16)
2664 /* Make sure a plt entry is created for this symbol if
2665 it turns out to be a function defined by a dynamic
2670 eh->got_types_used |= CALL16_USED;
2673 eh->got_types_used |= GOT16_USED;
2677 /* This is a global offset table entry for a local symbol. */
2678 if (local_got_refcounts == NULL)
2682 size = symtab_hdr->sh_info;
2683 size *= (sizeof (bfd_signed_vma) + sizeof (char));
2685 = ((bfd_signed_vma *) bfd_zalloc (abfd, size));
2686 if (local_got_refcounts == NULL)
2688 elf_local_got_refcounts (abfd) = local_got_refcounts;
2689 elf32_nios2_local_got_tls_type (abfd)
2690 = (char *) (local_got_refcounts + symtab_hdr->sh_info);
2692 local_got_refcounts[r_symndx]++;
2693 old_tls_type = elf32_nios2_local_got_tls_type (abfd) [r_symndx];
2696 /* We will already have issued an error message if there is a
2697 TLS / non-TLS mismatch, based on the symbol type. We don't
2698 support any linker relaxations. So just combine any TLS
2700 if (old_tls_type != GOT_UNKNOWN && old_tls_type != GOT_NORMAL
2701 && tls_type != GOT_NORMAL)
2702 tls_type |= old_tls_type;
2704 if (old_tls_type != tls_type)
2707 elf32_nios2_hash_entry (h)->tls_type = tls_type;
2709 elf32_nios2_local_got_tls_type (abfd) [r_symndx] = tls_type;
2713 case R_NIOS2_TLS_LDM16:
2714 if (r_type == R_NIOS2_TLS_LDM16)
2715 htab->tls_ldm_got.refcount++;
2717 if (htab->root.sgot == NULL)
2719 if (htab->root.dynobj == NULL)
2720 htab->root.dynobj = abfd;
2721 if (!create_got_section (htab->root.dynobj, info))
2726 /* This relocation describes the C++ object vtable hierarchy.
2727 Reconstruct it for later use during GC. */
2728 case R_NIOS2_GNU_VTINHERIT:
2729 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
2733 /* This relocation describes which C++ vtable entries are actually
2734 used. Record for later use during GC. */
2735 case R_NIOS2_GNU_VTENTRY:
2736 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
2740 case R_NIOS2_BFD_RELOC_32:
2741 case R_NIOS2_CALL26:
2742 case R_NIOS2_HIADJ16:
2747 /* If this reloc is in a read-only section, we might
2748 need a copy reloc. We can't check reliably at this
2749 stage whether the section is read-only, as input
2750 sections have not yet been mapped to output sections.
2751 Tentatively set the flag for now, and correct in
2752 adjust_dynamic_symbol. */
2756 /* Make sure a plt entry is created for this symbol if it
2757 turns out to be a function defined by a dynamic object. */
2760 if (r_type == R_NIOS2_CALL26)
2764 /* If we are creating a shared library, we need to copy the
2765 reloc into the shared library. */
2767 && (sec->flags & SEC_ALLOC) != 0
2768 && (r_type == R_NIOS2_BFD_RELOC_32
2769 || (h != NULL && ! h->needs_plt
2770 && (! info->symbolic || ! h->def_regular))))
2772 struct elf32_nios2_dyn_relocs *p;
2773 struct elf32_nios2_dyn_relocs **head;
2775 /* When creating a shared object, we must copy these
2776 reloc types into the output file. We create a reloc
2777 section in dynobj and make room for this reloc. */
2780 sreloc = _bfd_elf_make_dynamic_reloc_section
2781 (sec, dynobj, 2, abfd, TRUE);
2786 /* If this is a global symbol, we count the number of
2787 relocations we need for this symbol. */
2789 head = &((struct elf32_nios2_link_hash_entry *) h)->dyn_relocs;
2792 /* Track dynamic relocs needed for local syms too.
2793 We really need local syms available to do this
2798 Elf_Internal_Sym *isym;
2800 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
2805 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
2809 vpp = &elf_section_data (s)->local_dynrel;
2810 head = (struct elf32_nios2_dyn_relocs **) vpp;
2814 if (p == NULL || p->sec != sec)
2816 bfd_size_type amt = sizeof *p;
2817 p = ((struct elf32_nios2_dyn_relocs *)
2818 bfd_alloc (htab->root.dynobj, amt));
2839 /* Implement elf_backend_gc_mark_hook:
2840 Return the section that should be marked against GC for a given
2843 nios2_elf32_gc_mark_hook (asection *sec,
2844 struct bfd_link_info *info,
2845 Elf_Internal_Rela *rel,
2846 struct elf_link_hash_entry *h,
2847 Elf_Internal_Sym *sym)
2850 switch (ELF32_R_TYPE (rel->r_info))
2852 case R_NIOS2_GNU_VTINHERIT:
2853 case R_NIOS2_GNU_VTENTRY:
2856 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
2859 /* Implement elf_backend_gc_sweep_hook:
2860 Update the got entry reference counts for the section being removed. */
2862 nios2_elf32_gc_sweep_hook (bfd *abfd,
2863 struct bfd_link_info *info,
2865 const Elf_Internal_Rela *relocs)
2867 Elf_Internal_Shdr *symtab_hdr;
2868 struct elf_link_hash_entry **sym_hashes;
2869 bfd_signed_vma *local_got_refcounts;
2870 const Elf_Internal_Rela *rel, *relend;
2873 if (info->relocatable)
2876 elf_section_data (sec)->local_dynrel = NULL;
2878 dynobj = elf_hash_table (info)->dynobj;
2882 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2883 sym_hashes = elf_sym_hashes (abfd);
2884 local_got_refcounts = elf_local_got_refcounts (abfd);
2886 relend = relocs + sec->reloc_count;
2887 for (rel = relocs; rel < relend; rel++)
2889 unsigned long r_symndx;
2890 struct elf_link_hash_entry *h = NULL;
2893 r_symndx = ELF32_R_SYM (rel->r_info);
2894 if (r_symndx >= symtab_hdr->sh_info)
2896 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2897 while (h->root.type == bfd_link_hash_indirect
2898 || h->root.type == bfd_link_hash_warning)
2899 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2902 r_type = ELF32_R_TYPE (rel->r_info);
2906 case R_NIOS2_CALL16:
2909 if (h->got.refcount > 0)
2912 else if (local_got_refcounts != NULL)
2914 if (local_got_refcounts[r_symndx] > 0)
2915 --local_got_refcounts[r_symndx];
2919 case R_NIOS2_PCREL_LO:
2920 case R_NIOS2_PCREL_HA:
2921 case R_NIOS2_BFD_RELOC_32:
2922 case R_NIOS2_CALL26:
2925 struct elf32_nios2_link_hash_entry *eh;
2926 struct elf32_nios2_dyn_relocs **pp;
2927 struct elf32_nios2_dyn_relocs *p;
2929 eh = (struct elf32_nios2_link_hash_entry *) h;
2931 if (h->plt.refcount > 0)
2934 if (r_type == R_NIOS2_PCREL_LO || r_type == R_NIOS2_PCREL_HA
2935 || r_type == R_NIOS2_BFD_RELOC_32)
2937 for (pp = &eh->dyn_relocs; (p = *pp) != NULL;
2958 /* Install 16-bit immediate value VALUE at offset OFFSET into section SEC. */
2960 nios2_elf32_install_imm16 (asection *sec, bfd_vma offset, bfd_vma value)
2962 bfd_vma word = bfd_get_32 (sec->owner, sec->contents + offset);
2964 BFD_ASSERT(value <= 0xffff);
2966 bfd_put_32 (sec->owner, word | ((value & 0xffff) << 6),
2967 sec->contents + offset);
2970 /* Install COUNT 32-bit values DATA starting at offset OFFSET into
2973 nios2_elf32_install_data (asection *sec, const bfd_vma *data, bfd_vma offset,
2978 bfd_put_32 (sec->owner, *data, sec->contents + offset);
2984 /* Implement elf_backend_finish_dynamic_symbols:
2985 Finish up dynamic symbol handling. We set the contents of various
2986 dynamic sections here. */
2988 nios2_elf32_finish_dynamic_symbol (bfd *output_bfd,
2989 struct bfd_link_info *info,
2990 struct elf_link_hash_entry *h,
2991 Elf_Internal_Sym *sym)
2993 struct elf32_nios2_link_hash_table *htab;
2994 struct elf32_nios2_link_hash_entry *eh
2995 = (struct elf32_nios2_link_hash_entry *)h;
2998 htab = elf32_nios2_hash_table (info);
3000 if (h->plt.offset != (bfd_vma) -1)
3007 Elf_Internal_Rela rela;
3009 bfd_vma got_address;
3011 /* This symbol has an entry in the procedure linkage table. Set
3013 BFD_ASSERT (h->dynindx != -1);
3014 splt = htab->root.splt;
3015 sgotplt = htab->root.sgotplt;
3016 srela = htab->root.srelplt;
3017 BFD_ASSERT (splt != NULL && sgotplt != NULL && srela != NULL);
3019 /* Emit the PLT entry. */
3022 nios2_elf32_install_data (splt, nios2_so_plt_entry, h->plt.offset,
3024 plt_index = (h->plt.offset - 24) / 12;
3025 got_offset = (plt_index + 3) * 4;
3026 nios2_elf32_install_imm16 (splt, h->plt.offset,
3027 hiadj(plt_index * 4));
3028 nios2_elf32_install_imm16 (splt, h->plt.offset + 4,
3029 (plt_index * 4) & 0xffff);
3030 nios2_elf32_install_imm16 (splt, h->plt.offset + 8,
3031 0xfff4 - h->plt.offset);
3032 got_address = (sgotplt->output_section->vma + sgotplt->output_offset
3035 /* Fill in the entry in the global offset table. There are no
3036 res_n slots for a shared object PLT, instead the .got.plt entries
3037 point to the PLT entries. */
3038 bfd_put_32 (output_bfd,
3039 splt->output_section->vma + splt->output_offset
3040 + h->plt.offset, sgotplt->contents + got_offset);
3044 plt_index = (h->plt.offset - 28 - htab->res_n_size) / 12;
3045 got_offset = (plt_index + 3) * 4;
3047 nios2_elf32_install_data (splt, nios2_plt_entry, h->plt.offset, 3);
3048 got_address = (sgotplt->output_section->vma + sgotplt->output_offset
3050 nios2_elf32_install_imm16 (splt, h->plt.offset, hiadj(got_address));
3051 nios2_elf32_install_imm16 (splt, h->plt.offset + 4,
3052 got_address & 0xffff);
3054 /* Fill in the entry in the global offset table. */
3055 bfd_put_32 (output_bfd,
3056 splt->output_section->vma + splt->output_offset
3057 + plt_index * 4, sgotplt->contents + got_offset);
3060 /* Fill in the entry in the .rela.plt section. */
3061 rela.r_offset = got_address;
3062 rela.r_info = ELF32_R_INFO (h->dynindx, R_NIOS2_JUMP_SLOT);
3064 loc = srela->contents + plt_index * sizeof (Elf32_External_Rela);
3065 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
3067 if (!h->def_regular)
3069 /* Mark the symbol as undefined, rather than as defined in
3070 the .plt section. Leave the value alone. */
3071 sym->st_shndx = SHN_UNDEF;
3072 /* If the symbol is weak, we do need to clear the value.
3073 Otherwise, the PLT entry would provide a definition for
3074 the symbol even if the symbol wasn't defined anywhere,
3075 and so the symbol would never be NULL. */
3076 if (!h->ref_regular_nonweak)
3081 use_plt = (eh->got_types_used == CALL16_USED
3082 && h->plt.offset != (bfd_vma) -1);
3084 if (!use_plt && h->got.offset != (bfd_vma) -1
3085 && (elf32_nios2_hash_entry (h)->tls_type & GOT_TLS_GD) == 0
3086 && (elf32_nios2_hash_entry (h)->tls_type & GOT_TLS_IE) == 0)
3090 Elf_Internal_Rela rela;
3094 /* This symbol has an entry in the global offset table. Set it
3096 sgot = htab->root.sgot;
3097 srela = htab->root.srelgot;
3098 BFD_ASSERT (sgot != NULL && srela != NULL);
3100 offset = (h->got.offset & ~(bfd_vma) 1);
3101 rela.r_offset = (sgot->output_section->vma
3102 + sgot->output_offset + offset);
3104 /* If this is a -Bsymbolic link, and the symbol is defined
3105 locally, we just want to emit a RELATIVE reloc. Likewise if
3106 the symbol was forced to be local because of a version file.
3107 The entry in the global offset table will already have been
3108 initialized in the relocate_section function. */
3110 if (info->shared && SYMBOL_REFERENCES_LOCAL (info, h))
3112 rela.r_info = ELF32_R_INFO (0, R_NIOS2_RELATIVE);
3113 rela.r_addend = bfd_get_signed_32 (output_bfd,
3114 (sgot->contents + offset));
3115 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + offset);
3119 bfd_put_32 (output_bfd, (bfd_vma) 0,
3120 sgot->contents + offset);
3121 rela.r_info = ELF32_R_INFO (h->dynindx, R_NIOS2_GLOB_DAT);
3125 loc = srela->contents;
3126 loc += srela->reloc_count++ * sizeof (Elf32_External_Rela);
3127 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
3130 if (use_plt && h->got.offset != (bfd_vma) -1)
3132 bfd_vma offset = (h->got.offset & ~(bfd_vma) 1);
3133 asection *sgot = htab->root.sgot;
3134 asection *splt = htab->root.splt;
3135 bfd_put_32 (output_bfd, (splt->output_section->vma + splt->output_offset
3137 sgot->contents + offset);
3143 Elf_Internal_Rela rela;
3146 /* This symbol needs a copy reloc. Set it up. */
3147 BFD_ASSERT (h->dynindx != -1
3148 && (h->root.type == bfd_link_hash_defined
3149 || h->root.type == bfd_link_hash_defweak));
3152 BFD_ASSERT (s != NULL);
3154 rela.r_offset = (h->root.u.def.value
3155 + h->root.u.def.section->output_section->vma
3156 + h->root.u.def.section->output_offset);
3157 rela.r_info = ELF32_R_INFO (h->dynindx, R_NIOS2_COPY);
3159 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
3160 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
3163 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
3164 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
3165 || h == elf_hash_table (info)->hgot)
3166 sym->st_shndx = SHN_ABS;
3171 /* Implement elf_backend_finish_dynamic_sections. */
3173 nios2_elf32_finish_dynamic_sections (bfd *output_bfd,
3174 struct bfd_link_info *info)
3179 struct elf32_nios2_link_hash_table *htab;
3181 htab = elf32_nios2_hash_table (info);
3182 dynobj = elf_hash_table (info)->dynobj;
3183 sgotplt = htab->root.sgotplt;
3184 BFD_ASSERT (sgotplt != NULL);
3185 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3187 if (elf_hash_table (info)->dynamic_sections_created)
3190 Elf32_External_Dyn *dyncon, *dynconend;
3192 splt = htab->root.splt;
3193 BFD_ASSERT (splt != NULL && sdyn != NULL);
3195 dyncon = (Elf32_External_Dyn *) sdyn->contents;
3196 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
3197 for (; dyncon < dynconend; dyncon++)
3199 Elf_Internal_Dyn dyn;
3202 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
3210 s = htab->root.sgot;
3211 BFD_ASSERT (s != NULL);
3212 dyn.d_un.d_ptr = s->output_section->vma;
3213 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3217 s = htab->root.srelplt;
3218 BFD_ASSERT (s != NULL);
3219 dyn.d_un.d_ptr = s->output_section->vma;
3220 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3224 s = htab->root.srelplt;
3225 BFD_ASSERT (s != NULL);
3226 dyn.d_un.d_val = s->size;
3227 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3231 /* The procedure linkage table relocs (DT_JMPREL) should
3232 not be included in the overall relocs (DT_RELA).
3233 Therefore, we override the DT_RELASZ entry here to
3234 make it not include the JMPREL relocs. Since the
3235 linker script arranges for .rela.plt to follow all
3236 other relocation sections, we don't have to worry
3237 about changing the DT_RELA entry. */
3238 s = htab->root.srelplt;
3240 dyn.d_un.d_val -= s->size;
3241 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3245 s = htab->root.sgot;
3246 BFD_ASSERT (s != NULL);
3247 dyn.d_un.d_ptr = s->output_section->vma + 0x7ff0;
3248 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3253 /* Fill in the first entry in the procedure linkage table. */
3256 bfd_vma got_address = (sgotplt->output_section->vma
3257 + sgotplt->output_offset);
3260 bfd_vma corrected = got_address - (splt->output_section->vma
3261 + splt->output_offset + 4);
3262 nios2_elf32_install_data (splt, nios2_so_plt0_entry, 0, 6);
3263 nios2_elf32_install_imm16 (splt, 4, hiadj (corrected));
3264 nios2_elf32_install_imm16 (splt, 12, (corrected & 0xffff) + 4);
3265 nios2_elf32_install_imm16 (splt, 16, (corrected & 0xffff) + 8);
3269 /* Divide by 4 here, not 3 because we already corrected for the
3271 bfd_vma res_size = (splt->size - 28) / 4;
3272 bfd_vma res_start = (splt->output_section->vma
3273 + splt->output_offset);
3276 for (res_offset = 0; res_offset < res_size; res_offset += 4)
3277 bfd_put_32 (output_bfd,
3278 6 | ((res_size - (res_offset + 4)) << 6),
3279 splt->contents + res_offset);
3281 nios2_elf32_install_data (splt, nios2_plt0_entry, res_size, 7);
3282 nios2_elf32_install_imm16 (splt, res_size, hiadj (res_start));
3283 nios2_elf32_install_imm16 (splt, res_size + 4,
3284 res_start & 0xffff);
3285 nios2_elf32_install_imm16 (splt, res_size + 12,
3286 hiadj (got_address));
3287 nios2_elf32_install_imm16 (splt, res_size + 16,
3288 (got_address & 0xffff) + 4);
3289 nios2_elf32_install_imm16 (splt, res_size + 20,
3290 (got_address & 0xffff) + 8);
3294 /* Fill in the first three entries in the global offset table. */
3295 if (sgotplt->size > 0)
3298 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents);
3300 bfd_put_32 (output_bfd,
3301 sdyn->output_section->vma + sdyn->output_offset,
3303 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 4);
3304 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 8);
3307 elf_section_data (sgotplt->output_section)->this_hdr.sh_entsize = 4;
3312 /* Implement elf_backend_adjust_dynamic_symbol:
3313 Adjust a symbol defined by a dynamic object and referenced by a
3314 regular object. The current definition is in some section of the
3315 dynamic object, but we're not including those sections. We have to
3316 change the definition to something the rest of the link can
3319 nios2_elf32_adjust_dynamic_symbol (struct bfd_link_info *info,
3320 struct elf_link_hash_entry *h)
3322 struct elf32_nios2_link_hash_table *htab;
3327 htab = elf32_nios2_hash_table (info);
3328 dynobj = elf_hash_table (info)->dynobj;
3330 /* Make sure we know what is going on here. */
3331 BFD_ASSERT (dynobj != NULL
3333 || h->u.weakdef != NULL
3336 && !h->def_regular)));
3338 /* If this is a function, put it in the procedure linkage table. We
3339 will fill in the contents of the procedure linkage table later,
3340 when we know the address of the .got section. */
3341 if (h->type == STT_FUNC || h->needs_plt)
3343 if (h->plt.refcount <= 0
3344 || SYMBOL_CALLS_LOCAL (info, h)
3345 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3346 && h->root.type == bfd_link_hash_undefweak))
3348 /* This case can occur if we saw a PLT reloc in an input
3349 file, but the symbol was never referred to by a dynamic
3350 object, or if all references were garbage collected. In
3351 such a case, we don't actually need to build a procedure
3352 linkage table, and we can just do a PCREL reloc instead. */
3353 h->plt.offset = (bfd_vma) -1;
3360 /* Reinitialize the plt offset now that it is not used as a reference
3362 h->plt.offset = (bfd_vma) -1;
3364 /* If this is a weak symbol, and there is a real definition, the
3365 processor independent code will have arranged for us to see the
3366 real definition first, and we can just use the same value. */
3367 if (h->u.weakdef != NULL)
3369 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
3370 || h->u.weakdef->root.type == bfd_link_hash_defweak);
3371 h->root.u.def.section = h->u.weakdef->root.u.def.section;
3372 h->root.u.def.value = h->u.weakdef->root.u.def.value;
3376 /* If there are no non-GOT references, we do not need a copy
3378 if (!h->non_got_ref)
3381 /* This is a reference to a symbol defined by a dynamic object which
3383 If we are creating a shared library, we must presume that the
3384 only references to the symbol are via the global offset table.
3385 For such cases we need not do anything here; the relocations will
3386 be handled correctly by relocate_section. */
3392 (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
3393 h->root.root.string);
3397 /* We must allocate the symbol in our .dynbss section, which will
3398 become part of the .bss section of the executable. There will be
3399 an entry for this symbol in the .dynsym section. The dynamic
3400 object will contain position independent code, so all references
3401 from the dynamic object to this symbol will go through the global
3402 offset table. The dynamic linker will use the .dynsym entry to
3403 determine the address it must put in the global offset table, so
3404 both the dynamic object and the regular object will refer to the
3405 same memory location for the variable. */
3407 BFD_ASSERT (s != NULL);
3409 /* We must generate a R_NIOS2_COPY reloc to tell the dynamic linker to
3410 copy the initial value out of the dynamic object and into the
3411 runtime process image. We need to remember the offset into the
3412 .rela.bss section we are going to use. */
3413 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
3417 srel = htab->srelbss;
3418 BFD_ASSERT (srel != NULL);
3419 srel->size += sizeof (Elf32_External_Rela);
3423 align2 = bfd_log2 (h->size);
3424 if (align2 > h->root.u.def.section->alignment_power)
3425 align2 = h->root.u.def.section->alignment_power;
3428 s->size = BFD_ALIGN (s->size, (bfd_size_type)1 << align2);
3429 if (align2 > bfd_get_section_alignment (dynobj, s)
3430 && !bfd_set_section_alignment (dynobj, s, align2))
3433 /* Define the symbol as being at this point in the section. */
3434 h->root.u.def.section = s;
3435 h->root.u.def.value = s->size;
3437 /* Increment the section size to make room for the symbol. */
3443 /* Worker function for nios2_elf32_size_dynamic_sections. */
3445 adjust_dynrelocs (struct elf_link_hash_entry *h, PTR inf)
3447 struct bfd_link_info *info;
3448 struct elf32_nios2_link_hash_table *htab;
3450 if (h->root.type == bfd_link_hash_indirect)
3453 if (h->root.type == bfd_link_hash_warning)
3454 /* When warning symbols are created, they **replace** the "real"
3455 entry in the hash table, thus we never get to see the real
3456 symbol in a hash traversal. So look at it now. */
3457 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3459 info = (struct bfd_link_info *) inf;
3460 htab = elf32_nios2_hash_table (info);
3462 if (h->plt.offset != (bfd_vma)-1)
3463 h->plt.offset += htab->res_n_size;
3464 if (htab->root.splt == h->root.u.def.section)
3465 h->root.u.def.value += htab->res_n_size;
3470 /* Another worker function for nios2_elf32_size_dynamic_sections.
3471 Allocate space in .plt, .got and associated reloc sections for
3474 allocate_dynrelocs (struct elf_link_hash_entry *h, PTR inf)
3476 struct bfd_link_info *info;
3477 struct elf32_nios2_link_hash_table *htab;
3478 struct elf32_nios2_link_hash_entry *eh;
3479 struct elf32_nios2_dyn_relocs *p;
3482 if (h->root.type == bfd_link_hash_indirect)
3485 if (h->root.type == bfd_link_hash_warning)
3486 /* When warning symbols are created, they **replace** the "real"
3487 entry in the hash table, thus we never get to see the real
3488 symbol in a hash traversal. So look at it now. */
3489 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3491 info = (struct bfd_link_info *) inf;
3492 htab = elf32_nios2_hash_table (info);
3494 if (htab->root.dynamic_sections_created
3495 && h->plt.refcount > 0)
3497 /* Make sure this symbol is output as a dynamic symbol.
3498 Undefined weak syms won't yet be marked as dynamic. */
3499 if (h->dynindx == -1
3501 && !bfd_elf_link_record_dynamic_symbol (info, h))
3504 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
3506 asection *s = htab->root.splt;
3508 /* Allocate room for the header. */
3517 h->plt.offset = s->size;
3519 /* If this symbol is not defined in a regular file, and we are
3520 not generating a shared library, then set the symbol to this
3521 location in the .plt. This is required to make function
3522 pointers compare as equal between the normal executable and
3523 the shared library. */
3527 h->root.u.def.section = s;
3528 h->root.u.def.value = h->plt.offset;
3531 /* Make room for this entry. */
3534 /* We also need to make an entry in the .rela.plt section. */
3535 htab->root.srelplt->size += sizeof (Elf32_External_Rela);
3537 /* And the .got.plt section. */
3538 htab->root.sgotplt->size += 4;
3542 h->plt.offset = (bfd_vma) -1;
3548 h->plt.offset = (bfd_vma) -1;
3552 eh = (struct elf32_nios2_link_hash_entry *) h;
3553 use_plt = (eh->got_types_used == CALL16_USED
3554 && h->plt.offset != (bfd_vma) -1);
3556 if (h->got.refcount > 0)
3560 int tls_type = eh->tls_type;
3563 /* Make sure this symbol is output as a dynamic symbol.
3564 Undefined weak syms won't yet be marked as dynamic. */
3565 if (h->dynindx == -1
3567 && !bfd_elf_link_record_dynamic_symbol (info, h))
3570 s = htab->root.sgot;
3571 h->got.offset = s->size;
3573 if (tls_type == GOT_UNKNOWN)
3576 if (tls_type == GOT_NORMAL)
3577 /* Non-TLS symbols need one GOT slot. */
3581 if (tls_type & GOT_TLS_GD)
3582 /* R_NIOS2_TLS_GD16 needs 2 consecutive GOT slots. */
3584 if (tls_type & GOT_TLS_IE)
3585 /* R_NIOS2_TLS_IE16 needs one GOT slot. */
3589 dyn = htab->root.dynamic_sections_created;
3592 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
3594 || !SYMBOL_REFERENCES_LOCAL (info, h)))
3597 if (tls_type != GOT_NORMAL
3598 && (info->shared || indx != 0)
3599 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3600 || h->root.type != bfd_link_hash_undefweak))
3602 if (tls_type & GOT_TLS_IE)
3603 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
3605 if (tls_type & GOT_TLS_GD)
3606 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
3608 if ((tls_type & GOT_TLS_GD) && indx != 0)
3609 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
3611 else if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3612 || h->root.type != bfd_link_hash_undefweak)
3615 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
3616 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
3619 h->got.offset = (bfd_vma) -1;
3621 if (eh->dyn_relocs == NULL)
3624 /* In the shared -Bsymbolic case, discard space allocated for
3625 dynamic pc-relative relocs against symbols which turn out to be
3626 defined in regular objects. For the normal shared case, discard
3627 space for pc-relative relocs that have become local due to symbol
3628 visibility changes. */
3633 && (h->forced_local || info->symbolic))
3635 struct elf32_nios2_dyn_relocs **pp;
3637 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
3639 p->count -= p->pc_count;
3648 /* Also discard relocs on undefined weak syms with non-default
3650 if (eh->dyn_relocs != NULL
3651 && h->root.type == bfd_link_hash_undefweak)
3653 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
3654 eh->dyn_relocs = NULL;
3656 /* Make sure undefined weak symbols are output as a dynamic
3658 else if (h->dynindx == -1
3660 && !bfd_elf_link_record_dynamic_symbol (info, h))
3666 /* For the non-shared case, discard space for relocs against
3667 symbols which turn out to need copy relocs or are not
3671 && ((h->def_dynamic && !h->def_regular)
3672 || (htab->root.dynamic_sections_created
3673 && (h->root.type == bfd_link_hash_undefweak
3674 || h->root.type == bfd_link_hash_undefined))))
3676 /* Make sure this symbol is output as a dynamic symbol.
3677 Undefined weak syms won't yet be marked as dynamic. */
3678 if (h->dynindx == -1
3680 && !bfd_elf_link_record_dynamic_symbol (info, h))
3683 /* If that succeeded, we know we'll be keeping all the
3685 if (h->dynindx != -1)
3689 eh->dyn_relocs = NULL;
3694 /* Finally, allocate space. */
3695 for (p = eh->dyn_relocs; p != NULL; p = p->next)
3697 asection *sreloc = elf_section_data (p->sec)->sreloc;
3698 sreloc->size += p->count * sizeof (Elf32_External_Rela);
3704 /* Implement elf_backend_size_dynamic_sections:
3705 Set the sizes of the dynamic sections. */
3707 nios2_elf32_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
3708 struct bfd_link_info *info)
3716 struct elf32_nios2_link_hash_table *htab;
3718 htab = elf32_nios2_hash_table (info);
3719 dynobj = elf_hash_table (info)->dynobj;
3720 BFD_ASSERT (dynobj != NULL);
3722 htab->res_n_size = 0;
3723 if (elf_hash_table (info)->dynamic_sections_created)
3725 /* Set the contents of the .interp section to the interpreter. */
3726 if (info->executable)
3728 s = bfd_get_linker_section (dynobj, ".interp");
3729 BFD_ASSERT (s != NULL);
3730 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
3731 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
3736 /* We may have created entries in the .rela.got section.
3737 However, if we are not creating the dynamic sections, we will
3738 not actually use these entries. Reset the size of .rela.got,
3739 which will cause it to get stripped from the output file
3741 s = htab->root.srelgot;
3746 /* Set up .got offsets for local syms, and space for local dynamic
3748 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
3750 bfd_signed_vma *local_got;
3751 bfd_signed_vma *end_local_got;
3752 char *local_tls_type;
3753 bfd_size_type locsymcount;
3754 Elf_Internal_Shdr *symtab_hdr;
3757 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
3760 for (s = ibfd->sections; s != NULL; s = s->next)
3762 struct elf32_nios2_dyn_relocs *p;
3764 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
3766 if (!bfd_is_abs_section (p->sec)
3767 && bfd_is_abs_section (p->sec->output_section))
3769 /* Input section has been discarded, either because
3770 it is a copy of a linkonce section or due to
3771 linker script /DISCARD/, so we'll be discarding
3774 else if (p->count != 0)
3776 srel = elf_section_data (p->sec)->sreloc;
3777 srel->size += p->count * sizeof (Elf32_External_Rela);
3778 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
3779 info->flags |= DF_TEXTREL;
3784 local_got = elf_local_got_refcounts (ibfd);
3788 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
3789 locsymcount = symtab_hdr->sh_info;
3790 end_local_got = local_got + locsymcount;
3791 local_tls_type = elf32_nios2_local_got_tls_type (ibfd);
3792 s = htab->root.sgot;
3793 srel = htab->root.srelgot;
3794 for (; local_got < end_local_got; ++local_got, ++local_tls_type)
3798 *local_got = s->size;
3799 if (*local_tls_type & GOT_TLS_GD)
3800 /* TLS_GD relocs need an 8-byte structure in the GOT. */
3802 if (*local_tls_type & GOT_TLS_IE)
3804 if (*local_tls_type == GOT_NORMAL)
3807 if (info->shared || *local_tls_type == GOT_TLS_GD)
3808 srel->size += sizeof (Elf32_External_Rela);
3811 *local_got = (bfd_vma) -1;
3815 if (htab->tls_ldm_got.refcount > 0)
3817 /* Allocate two GOT entries and one dynamic relocation (if necessary)
3818 for R_NIOS2_TLS_LDM16 relocations. */
3819 htab->tls_ldm_got.offset = htab->root.sgot->size;
3820 htab->root.sgot->size += 8;
3822 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
3825 htab->tls_ldm_got.offset = -1;
3827 /* Allocate global sym .plt and .got entries, and space for global
3828 sym dynamic relocs. */
3829 elf_link_hash_traverse (& htab->root, allocate_dynrelocs, info);
3831 /* The check_relocs and adjust_dynamic_symbol entry points have
3832 determined the sizes of the various dynamic sections. Allocate
3837 for (s = dynobj->sections; s != NULL; s = s->next)
3841 if ((s->flags & SEC_LINKER_CREATED) == 0)
3844 /* It's OK to base decisions on the section name, because none
3845 of the dynobj section names depend upon the input files. */
3846 name = bfd_get_section_name (dynobj, s);
3848 if (strcmp (name, ".plt") == 0)
3850 /* Remember whether there is a PLT. */
3853 /* Correct for the number of res_N branches. */
3854 if (plt && !info->shared)
3856 htab->res_n_size = (s->size-28) / 3;
3857 s->size += htab->res_n_size;
3860 else if (CONST_STRNEQ (name, ".rela"))
3866 /* We use the reloc_count field as a counter if we need
3867 to copy relocs into the output file. */
3871 else if (CONST_STRNEQ (name, ".got"))
3873 else if (strcmp (name, ".dynbss") != 0)
3874 /* It's not one of our sections, so don't allocate space. */
3879 /* If we don't need this section, strip it from the
3880 output file. This is mostly to handle .rela.bss and
3881 .rela.plt. We must create both sections in
3882 create_dynamic_sections, because they must be created
3883 before the linker maps input sections to output
3884 sections. The linker does that before
3885 adjust_dynamic_symbol is called, and it is that
3886 function which decides whether anything needs to go
3887 into these sections. */
3888 s->flags |= SEC_EXCLUDE;
3892 if ((s->flags & SEC_HAS_CONTENTS) == 0)
3895 /* Allocate memory for the section contents. */
3896 /* FIXME: This should be a call to bfd_alloc not bfd_zalloc.
3897 Unused entries should be reclaimed before the section's contents
3898 are written out, but at the moment this does not happen. Thus in
3899 order to prevent writing out garbage, we initialize the section's
3900 contents to zero. */
3901 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
3902 if (s->contents == NULL)
3906 /* Adjust dynamic symbols that point to the plt to account for the
3907 now-known number of resN slots. */
3908 if (htab->res_n_size)
3909 elf_link_hash_traverse (& htab->root, adjust_dynrelocs, info);
3911 if (elf_hash_table (info)->dynamic_sections_created)
3913 /* Add some entries to the .dynamic section. We fill in the
3914 values later, in elf_nios2_finish_dynamic_sections, but we
3915 must add the entries now so that we get the correct size for
3916 the .dynamic section. The DT_DEBUG entry is filled in by the
3917 dynamic linker and used by the debugger. */
3918 #define add_dynamic_entry(TAG, VAL) \
3919 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
3921 if (!info->shared && !add_dynamic_entry (DT_DEBUG, 0))
3924 if (got && !add_dynamic_entry (DT_PLTGOT, 0))
3928 && (!add_dynamic_entry (DT_PLTRELSZ, 0)
3929 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
3930 || !add_dynamic_entry (DT_JMPREL, 0)))
3934 && (!add_dynamic_entry (DT_RELA, 0)
3935 || !add_dynamic_entry (DT_RELASZ, 0)
3936 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela))))
3939 if (!info->shared && !add_dynamic_entry (DT_NIOS2_GP, 0))
3942 if ((info->flags & DF_TEXTREL) != 0
3943 && !add_dynamic_entry (DT_TEXTREL, 0))
3946 #undef add_dynamic_entry
3951 /* Implement bfd_elf32_bfd_link_hash_table_create. */
3952 static struct bfd_link_hash_table *
3953 nios2_elf32_link_hash_table_create (bfd *abfd)
3955 struct elf32_nios2_link_hash_table *ret;
3956 bfd_size_type amt = sizeof (struct elf32_nios2_link_hash_table);
3958 ret = bfd_zmalloc (amt);
3962 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
3965 elf32_nios2_link_hash_entry),
3972 return &ret->root.root;
3975 /* Implement elf_backend_reloc_type_class. */
3976 static enum elf_reloc_type_class
3977 nios2_elf32_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
3978 const asection *rel_sec ATTRIBUTE_UNUSED,
3979 const Elf_Internal_Rela *rela)
3981 switch ((int) ELF32_R_TYPE (rela->r_info))
3983 case R_NIOS2_RELATIVE:
3984 return reloc_class_relative;
3985 case R_NIOS2_JUMP_SLOT:
3986 return reloc_class_plt;
3988 return reloc_class_copy;
3990 return reloc_class_normal;
3994 /* Return 1 if target is one of ours. */
3996 is_nios2_elf_target (const struct bfd_target *targ)
3998 return (targ == &bfd_elf32_littlenios2_vec
3999 || targ == &bfd_elf32_bignios2_vec);
4002 /* Implement elf_backend_add_symbol_hook.
4003 This hook is called by the linker when adding symbols from an object
4004 file. We use it to put .comm items in .sbss, and not .bss. */
4006 nios2_elf_add_symbol_hook (bfd *abfd,
4007 struct bfd_link_info *info,
4008 Elf_Internal_Sym *sym,
4009 const char **namep ATTRIBUTE_UNUSED,
4010 flagword *flagsp ATTRIBUTE_UNUSED,
4016 if (sym->st_shndx == SHN_COMMON
4017 && !info->relocatable
4018 && sym->st_size <= elf_gp_size (abfd)
4019 && is_nios2_elf_target (info->output_bfd->xvec))
4021 /* Common symbols less than or equal to -G nn bytes are automatically
4023 struct elf32_nios2_link_hash_table *htab;
4025 htab = elf32_nios2_hash_table (info);
4026 if (htab->sbss == NULL)
4028 flagword flags = SEC_IS_COMMON | SEC_LINKER_CREATED;
4030 dynobj = elf_hash_table (info)->dynobj;
4034 htab->sbss = bfd_make_section_anyway_with_flags (dynobj, ".sbss",
4036 if (htab->sbss == NULL)
4041 *valp = sym->st_size;
4047 /* Implement elf_backend_can_make_relative_eh_frame:
4048 Decide whether to attempt to turn absptr or lsda encodings in
4049 shared libraries into pcrel within the given input section. */
4051 nios2_elf32_can_make_relative_eh_frame (bfd *input_bfd ATTRIBUTE_UNUSED,
4052 struct bfd_link_info *info
4054 asection *eh_frame_section
4057 /* We can't use PC-relative encodings in the .eh_frame section. */
4061 /* Implement elf_backend_special_sections. */
4062 const struct bfd_elf_special_section elf32_nios2_special_sections[] =
4064 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS,
4065 SHF_ALLOC + SHF_WRITE + SHF_NIOS2_GPREL },
4066 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS,
4067 SHF_ALLOC + SHF_WRITE + SHF_NIOS2_GPREL },
4068 { NULL, 0, 0, 0, 0 }
4071 #define ELF_ARCH bfd_arch_nios2
4072 #define ELF_TARGET_ID NIOS2_ELF_DATA
4073 #define ELF_MACHINE_CODE EM_ALTERA_NIOS2
4075 /* The Nios II MMU uses a 4K page size. */
4077 #define ELF_MAXPAGESIZE 0x1000
4079 #define bfd_elf32_bfd_link_hash_table_create \
4080 nios2_elf32_link_hash_table_create
4082 /* Relocation table lookup macros. */
4084 #define bfd_elf32_bfd_reloc_type_lookup nios2_elf32_bfd_reloc_type_lookup
4085 #define bfd_elf32_bfd_reloc_name_lookup nios2_elf32_bfd_reloc_name_lookup
4087 /* JUMP_TABLE_LINK macros. */
4089 /* elf_info_to_howto (using RELA relocations). */
4091 #define elf_info_to_howto nios2_elf32_info_to_howto
4093 /* elf backend functions. */
4095 #define elf_backend_can_gc_sections 1
4096 #define elf_backend_can_refcount 1
4097 #define elf_backend_plt_readonly 1
4098 #define elf_backend_want_got_plt 1
4099 #define elf_backend_rela_normal 1
4101 #define elf_backend_relocate_section nios2_elf32_relocate_section
4102 #define elf_backend_section_flags nios2_elf32_section_flags
4103 #define elf_backend_fake_sections nios2_elf32_fake_sections
4104 #define elf_backend_check_relocs nios2_elf32_check_relocs
4106 #define elf_backend_gc_mark_hook nios2_elf32_gc_mark_hook
4107 #define elf_backend_gc_sweep_hook nios2_elf32_gc_sweep_hook
4108 #define elf_backend_create_dynamic_sections \
4109 nios2_elf32_create_dynamic_sections
4110 #define elf_backend_finish_dynamic_symbol nios2_elf32_finish_dynamic_symbol
4111 #define elf_backend_finish_dynamic_sections \
4112 nios2_elf32_finish_dynamic_sections
4113 #define elf_backend_adjust_dynamic_symbol nios2_elf32_adjust_dynamic_symbol
4114 #define elf_backend_reloc_type_class nios2_elf32_reloc_type_class
4115 #define elf_backend_size_dynamic_sections nios2_elf32_size_dynamic_sections
4116 #define elf_backend_add_symbol_hook nios2_elf_add_symbol_hook
4117 #define elf_backend_copy_indirect_symbol nios2_elf32_copy_indirect_symbol
4119 #define elf_backend_grok_prstatus nios2_grok_prstatus
4120 #define elf_backend_grok_psinfo nios2_grok_psinfo
4122 #undef elf_backend_can_make_relative_eh_frame
4123 #define elf_backend_can_make_relative_eh_frame \
4124 nios2_elf32_can_make_relative_eh_frame
4126 #define elf_backend_special_sections elf32_nios2_special_sections
4128 #define TARGET_LITTLE_SYM bfd_elf32_littlenios2_vec
4129 #define TARGET_LITTLE_NAME "elf32-littlenios2"
4130 #define TARGET_BIG_SYM bfd_elf32_bignios2_vec
4131 #define TARGET_BIG_NAME "elf32-bignios2"
4133 #define elf_backend_got_header_size 12
4135 #include "elf32-target.h"