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 ATTRIBUTE_UNUSED,
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 ATTRIBUTE_UNUSED,
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 ATTRIBUTE_UNUSED,
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 ATTRIBUTE_UNUSED,
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
1193 bfd_byte *data, bfd_vma offset,
1194 bfd_vma symbol_value, bfd_vma addend)
1196 symbol_value = symbol_value + addend;
1197 symbol_value -= (input_section->output_section->vma
1198 + input_section->output_offset);
1199 symbol_value -= offset;
1201 symbol_value = hiadj(symbol_value);
1202 return _bfd_final_link_relocate (howto, abfd, input_section, data, offset,
1203 symbol_value, addend);
1206 static bfd_reloc_status_type
1207 nios2_elf32_do_pcrel16_relocate (bfd *abfd, reloc_howto_type *howto,
1208 asection *input_section ATTRIBUTE_UNUSED,
1209 bfd_byte *data, bfd_vma offset,
1210 bfd_vma symbol_value, bfd_vma addend)
1212 /* NIOS2 pc relative relocations are relative to the next 32-bit instruction
1213 so we need to subtract 4 before doing a final_link_relocate. */
1214 symbol_value = symbol_value + addend - 4;
1216 return _bfd_final_link_relocate (howto, abfd, input_section,
1217 data, offset, symbol_value, addend);
1220 static bfd_reloc_status_type
1221 nios2_elf32_do_call26_relocate (bfd *abfd, reloc_howto_type *howto,
1222 asection *input_section ATTRIBUTE_UNUSED,
1223 bfd_byte *data, bfd_vma offset,
1224 bfd_vma symbol_value, bfd_vma addend)
1226 /* Check that the relocation is in the same page as the current address. */
1227 if (((symbol_value + addend) & 0xf0000000)
1228 != ((input_section->output_section->vma + offset) & 0xf0000000))
1229 return bfd_reloc_overflow;
1231 return _bfd_final_link_relocate (howto, abfd, input_section,
1232 data, offset, symbol_value, addend);
1235 static bfd_reloc_status_type
1236 nios2_elf32_do_gprel_relocate (bfd *abfd, reloc_howto_type *howto,
1237 asection *input_section ATTRIBUTE_UNUSED,
1238 bfd_byte *data, bfd_vma offset,
1239 bfd_vma symbol_value, bfd_vma addend)
1241 /* Because we need the output_bfd, the special handling is done
1242 in nios2_elf32_relocate_section or in nios2_elf32_gprel_relocate. */
1243 return _bfd_final_link_relocate (howto, abfd, input_section,
1244 data, offset, symbol_value, addend);
1247 static bfd_reloc_status_type
1248 nios2_elf32_do_ujmp_relocate (bfd *abfd, reloc_howto_type *howto,
1249 asection *input_section ATTRIBUTE_UNUSED,
1250 bfd_byte *data, bfd_vma offset,
1251 bfd_vma symbol_value, bfd_vma addend)
1253 bfd_vma symbol_lo16, symbol_hi16;
1254 bfd_reloc_status_type r;
1255 symbol_value = symbol_value + addend;
1257 symbol_hi16 = (symbol_value >> 16) & 0xffff;
1258 symbol_lo16 = symbol_value & 0xffff;
1260 r = _bfd_final_link_relocate (howto, abfd, input_section,
1261 data, offset, symbol_hi16, addend);
1263 if (r == bfd_reloc_ok)
1264 return _bfd_final_link_relocate (howto, abfd, input_section,
1265 data, offset + 4, symbol_lo16, addend);
1270 static bfd_reloc_status_type
1271 nios2_elf32_do_cjmp_relocate (bfd *abfd, reloc_howto_type *howto,
1272 asection *input_section ATTRIBUTE_UNUSED,
1273 bfd_byte *data, bfd_vma offset,
1274 bfd_vma symbol_value, bfd_vma addend)
1276 bfd_vma symbol_lo16, symbol_hi16;
1277 bfd_reloc_status_type r;
1278 symbol_value = symbol_value + addend;
1280 symbol_hi16 = (symbol_value >> 16) & 0xffff;
1281 symbol_lo16 = symbol_value & 0xffff;
1283 r = _bfd_final_link_relocate (howto, abfd, input_section,
1284 data, offset, symbol_hi16, addend);
1286 if (r == bfd_reloc_ok)
1287 return _bfd_final_link_relocate (howto, abfd, input_section,
1288 data, offset + 4, symbol_lo16, addend);
1293 static bfd_reloc_status_type
1294 nios2_elf32_do_callr_relocate (bfd *abfd, reloc_howto_type *howto,
1295 asection *input_section ATTRIBUTE_UNUSED,
1296 bfd_byte *data, bfd_vma offset,
1297 bfd_vma symbol_value, bfd_vma addend)
1299 bfd_vma symbol_lo16, symbol_hi16;
1300 bfd_reloc_status_type r;
1301 symbol_value = symbol_value + addend;
1303 symbol_hi16 = (symbol_value >> 16) & 0xffff;
1304 symbol_lo16 = symbol_value & 0xffff;
1306 r = _bfd_final_link_relocate (howto, abfd, input_section,
1307 data, offset, symbol_hi16, addend);
1309 if (r == bfd_reloc_ok)
1310 return _bfd_final_link_relocate (howto, abfd, input_section,
1311 data, offset + 4, symbol_lo16, addend);
1316 /* HOWTO handlers for relocations that require special handling. */
1318 /* This is for relocations used only when relaxing to ensure
1319 changes in size of section don't screw up .align. */
1320 static bfd_reloc_status_type
1321 nios2_elf32_ignore_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
1322 asymbol *symbol ATTRIBUTE_UNUSED,
1323 void *data ATTRIBUTE_UNUSED, asection *input_section,
1325 char **error_message ATTRIBUTE_UNUSED)
1327 if (output_bfd != NULL)
1328 reloc_entry->address += input_section->output_offset;
1329 return bfd_reloc_ok;
1332 static bfd_reloc_status_type
1333 nios2_elf32_hi16_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1334 void *data, asection *input_section,
1336 char **error_message ATTRIBUTE_UNUSED)
1338 /* This part is from bfd_elf_generic_reloc. */
1339 if (output_bfd != NULL
1340 && (symbol->flags & BSF_SECTION_SYM) == 0
1341 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
1343 reloc_entry->address += input_section->output_offset;
1344 return bfd_reloc_ok;
1347 if (output_bfd != NULL)
1348 /* FIXME: See bfd_perform_relocation. Is this right? */
1349 return bfd_reloc_continue;
1351 return nios2_elf32_do_hi16_relocate (abfd, reloc_entry->howto,
1353 data, reloc_entry->address,
1355 + symbol->section->output_section->vma
1356 + symbol->section->output_offset),
1357 reloc_entry->addend);
1360 static bfd_reloc_status_type
1361 nios2_elf32_lo16_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1362 void *data, asection *input_section,
1364 char **error_message ATTRIBUTE_UNUSED)
1366 /* This part is from bfd_elf_generic_reloc. */
1367 if (output_bfd != NULL
1368 && (symbol->flags & BSF_SECTION_SYM) == 0
1369 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
1371 reloc_entry->address += input_section->output_offset;
1372 return bfd_reloc_ok;
1375 if (output_bfd != NULL)
1376 /* FIXME: See bfd_perform_relocation. Is this right? */
1377 return bfd_reloc_continue;
1379 return nios2_elf32_do_lo16_relocate (abfd, reloc_entry->howto,
1381 data, reloc_entry->address,
1383 + symbol->section->output_section->vma
1384 + symbol->section->output_offset),
1385 reloc_entry->addend);
1388 static bfd_reloc_status_type
1389 nios2_elf32_hiadj16_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1390 void *data, asection *input_section,
1392 char **error_message ATTRIBUTE_UNUSED)
1394 /* This part is from bfd_elf_generic_reloc. */
1395 if (output_bfd != NULL
1396 && (symbol->flags & BSF_SECTION_SYM) == 0
1397 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
1399 reloc_entry->address += input_section->output_offset;
1400 return bfd_reloc_ok;
1403 if (output_bfd != NULL)
1404 /* FIXME: See bfd_perform_relocation. Is this right? */
1405 return bfd_reloc_continue;
1407 return nios2_elf32_do_hiadj16_relocate (abfd, reloc_entry->howto,
1409 data, reloc_entry->address,
1411 + symbol->section->output_section->vma
1412 + symbol->section->output_offset),
1413 reloc_entry->addend);
1416 static bfd_reloc_status_type
1417 nios2_elf32_pcrel_lo16_relocate (bfd *abfd, arelent *reloc_entry,
1418 asymbol *symbol, void *data,
1419 asection *input_section, bfd *output_bfd,
1420 char **error_message ATTRIBUTE_UNUSED)
1422 /* This part is from bfd_elf_generic_reloc. */
1423 if (output_bfd != NULL
1424 && (symbol->flags & BSF_SECTION_SYM) == 0
1425 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
1427 reloc_entry->address += input_section->output_offset;
1428 return bfd_reloc_ok;
1431 if (output_bfd != NULL)
1432 /* FIXME: See bfd_perform_relocation. Is this right? */
1433 return bfd_reloc_continue;
1435 return nios2_elf32_do_pcrel_lo16_relocate (
1436 abfd, reloc_entry->howto, input_section, data, reloc_entry->address,
1437 (symbol->value + symbol->section->output_section->vma
1438 + symbol->section->output_offset),
1439 reloc_entry->addend);
1442 static bfd_reloc_status_type
1443 nios2_elf32_pcrel_hiadj16_relocate (bfd *abfd, arelent *reloc_entry,
1444 asymbol *symbol, void *data,
1445 asection *input_section, bfd *output_bfd,
1446 char **error_message ATTRIBUTE_UNUSED)
1448 /* This part is from bfd_elf_generic_reloc. */
1449 if (output_bfd != NULL
1450 && (symbol->flags & BSF_SECTION_SYM) == 0
1451 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
1453 reloc_entry->address += input_section->output_offset;
1454 return bfd_reloc_ok;
1457 if (output_bfd != NULL)
1458 /* FIXME: See bfd_perform_relocation. Is this right? */
1459 return bfd_reloc_continue;
1461 return nios2_elf32_do_pcrel_hiadj16_relocate (
1462 abfd, reloc_entry->howto, input_section, data, reloc_entry->address,
1463 (symbol->value + symbol->section->output_section->vma
1464 + symbol->section->output_offset),
1465 reloc_entry->addend);
1468 static bfd_reloc_status_type
1469 nios2_elf32_pcrel16_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1470 void *data, asection *input_section,
1472 char **error_message ATTRIBUTE_UNUSED)
1474 /* This part is from bfd_elf_generic_reloc. */
1475 if (output_bfd != NULL
1476 && (symbol->flags & BSF_SECTION_SYM) == 0
1477 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
1479 reloc_entry->address += input_section->output_offset;
1480 return bfd_reloc_ok;
1483 if (output_bfd != NULL)
1484 /* FIXME: See bfd_perform_relocation. Is this right? */
1485 return bfd_reloc_continue;
1487 return nios2_elf32_do_pcrel16_relocate (abfd, reloc_entry->howto,
1489 data, reloc_entry->address,
1491 + symbol->section->output_section->vma
1492 + symbol->section->output_offset),
1493 reloc_entry->addend);
1496 static bfd_reloc_status_type
1497 nios2_elf32_call26_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1498 void *data, asection *input_section,
1500 char **error_message ATTRIBUTE_UNUSED)
1502 /* This part is from bfd_elf_generic_reloc. */
1503 if (output_bfd != NULL
1504 && (symbol->flags & BSF_SECTION_SYM) == 0
1505 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
1507 reloc_entry->address += input_section->output_offset;
1508 return bfd_reloc_ok;
1511 if (output_bfd != NULL)
1512 /* FIXME: See bfd_perform_relocation. Is this right? */
1513 return bfd_reloc_continue;
1515 return nios2_elf32_do_call26_relocate (abfd, reloc_entry->howto,
1517 data, reloc_entry->address,
1519 + symbol->section->output_section->vma
1520 + symbol->section->output_offset),
1521 reloc_entry->addend);
1524 static bfd_reloc_status_type
1525 nios2_elf32_gprel_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1526 void *data, asection *input_section,
1527 bfd *output_bfd, char **msg)
1531 bfd_reloc_status_type r;
1534 /* This part is from bfd_elf_generic_reloc. */
1535 if (output_bfd != NULL
1536 && (symbol->flags & BSF_SECTION_SYM) == 0
1537 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
1539 reloc_entry->address += input_section->output_offset;
1540 return bfd_reloc_ok;
1543 if (output_bfd != NULL)
1544 /* FIXME: See bfd_perform_relocation. Is this right? */
1545 return bfd_reloc_continue;
1547 relocation = (symbol->value
1548 + symbol->section->output_section->vma
1549 + symbol->section->output_offset);
1551 /* This assumes we've already cached the _gp symbol. */
1552 r = nios2_elf_final_gp (abfd, symbol, FALSE, msg, &gp);
1553 if (r == bfd_reloc_ok)
1555 relocation = relocation + reloc_entry->addend - gp;
1556 reloc_entry->addend = 0;
1557 if ((signed) relocation < -32768 || (signed) relocation > 32767)
1559 *msg = _("global pointer relative address out of range");
1560 r = bfd_reloc_outofrange;
1563 r = nios2_elf32_do_gprel_relocate (abfd, reloc_entry->howto,
1565 data, reloc_entry->address,
1566 relocation, reloc_entry->addend);
1572 static bfd_reloc_status_type
1573 nios2_elf32_ujmp_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1574 void *data, asection *input_section,
1575 bfd *output_bfd, char **msg ATTRIBUTE_UNUSED)
1577 /* This part is from bfd_elf_generic_reloc. */
1578 if (output_bfd != NULL
1579 && (symbol->flags & BSF_SECTION_SYM) == 0
1580 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
1582 reloc_entry->address += input_section->output_offset;
1583 return bfd_reloc_ok;
1586 if (output_bfd != NULL)
1587 /* FIXME: See bfd_perform_relocation. Is this right? */
1588 return bfd_reloc_continue;
1590 return nios2_elf32_do_ujmp_relocate (abfd, reloc_entry->howto,
1592 data, reloc_entry->address,
1594 + symbol->section->output_section->vma
1595 + symbol->section->output_offset),
1596 reloc_entry->addend);
1599 static bfd_reloc_status_type
1600 nios2_elf32_cjmp_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1601 void *data, asection *input_section,
1602 bfd *output_bfd, char **msg ATTRIBUTE_UNUSED)
1604 /* This part is from bfd_elf_generic_reloc. */
1605 if (output_bfd != NULL
1606 && (symbol->flags & BSF_SECTION_SYM) == 0
1607 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
1609 reloc_entry->address += input_section->output_offset;
1610 return bfd_reloc_ok;
1613 if (output_bfd != NULL)
1614 /* FIXME: See bfd_perform_relocation. Is this right? */
1615 return bfd_reloc_continue;
1617 return nios2_elf32_do_cjmp_relocate (abfd, reloc_entry->howto,
1619 data, reloc_entry->address,
1621 + symbol->section->output_section->vma
1622 + symbol->section->output_offset),
1623 reloc_entry->addend);
1626 static bfd_reloc_status_type
1627 nios2_elf32_callr_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1628 void *data, asection *input_section,
1629 bfd *output_bfd, char **msg ATTRIBUTE_UNUSED)
1631 /* This part is from bfd_elf_generic_reloc. */
1632 if (output_bfd != NULL
1633 && (symbol->flags & BSF_SECTION_SYM) == 0
1634 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
1636 reloc_entry->address += input_section->output_offset;
1637 return bfd_reloc_ok;
1640 if (output_bfd != NULL)
1641 /* FIXME: See bfd_perform_relocation. Is this right? */
1642 return bfd_reloc_continue;
1644 return nios2_elf32_do_callr_relocate (abfd, reloc_entry->howto,
1646 data, reloc_entry->address,
1648 + symbol->section->output_section->vma
1649 + symbol->section->output_offset),
1650 reloc_entry->addend);
1654 /* Implement elf_backend_relocate_section. */
1656 nios2_elf32_relocate_section (bfd *output_bfd,
1657 struct bfd_link_info *info,
1659 asection *input_section,
1661 Elf_Internal_Rela *relocs,
1662 Elf_Internal_Sym *local_syms,
1663 asection **local_sections)
1665 Elf_Internal_Shdr *symtab_hdr;
1666 struct elf_link_hash_entry **sym_hashes;
1667 Elf_Internal_Rela *rel;
1668 Elf_Internal_Rela *relend;
1669 struct elf32_nios2_link_hash_table *htab;
1672 asection *sreloc = NULL;
1673 bfd_vma *local_got_offsets;
1675 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1676 sym_hashes = elf_sym_hashes (input_bfd);
1677 relend = relocs + input_section->reloc_count;
1679 htab = elf32_nios2_hash_table (info);
1680 sgot = htab->root.sgot;
1681 splt = htab->root.splt;
1682 local_got_offsets = elf_local_got_offsets (input_bfd);
1684 for (rel = relocs; rel < relend; rel++)
1686 reloc_howto_type *howto;
1687 unsigned long r_symndx;
1688 Elf_Internal_Sym *sym;
1690 struct elf_link_hash_entry *h;
1691 struct elf32_nios2_link_hash_entry *eh;
1694 bfd_vma reloc_address;
1695 bfd_reloc_status_type r = bfd_reloc_ok;
1696 const char *name = NULL;
1700 const char* msg = (const char*) NULL;
1701 bfd_boolean unresolved_reloc;
1705 r_type = ELF32_R_TYPE (rel->r_info);
1706 r_symndx = ELF32_R_SYM (rel->r_info);
1708 howto = lookup_howto ((unsigned) ELF32_R_TYPE (rel->r_info));
1713 if (r_symndx < symtab_hdr->sh_info)
1715 sym = local_syms + r_symndx;
1716 sec = local_sections[r_symndx];
1717 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
1723 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
1724 r_symndx, symtab_hdr, sym_hashes,
1726 unresolved_reloc, warned);
1729 if (sec && discarded_section (sec))
1730 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
1731 rel, 1, relend, howto, 0, contents);
1733 /* Nothing more to do unless this is a final link. */
1734 if (info->relocatable)
1737 if (sec && sec->output_section)
1738 reloc_address = (sec->output_section->vma + sec->output_offset
1745 switch (howto->type)
1748 r = nios2_elf32_do_hi16_relocate (input_bfd, howto,
1750 contents, rel->r_offset,
1751 relocation, rel->r_addend);
1754 r = nios2_elf32_do_lo16_relocate (input_bfd, howto,
1756 contents, rel->r_offset,
1757 relocation, rel->r_addend);
1759 case R_NIOS2_PCREL_LO:
1760 r = nios2_elf32_do_pcrel_lo16_relocate (input_bfd, howto,
1767 case R_NIOS2_HIADJ16:
1768 r = nios2_elf32_do_hiadj16_relocate (input_bfd, howto,
1769 input_section, contents,
1770 rel->r_offset, relocation,
1773 case R_NIOS2_PCREL_HA:
1774 r = nios2_elf32_do_pcrel_hiadj16_relocate (input_bfd, howto,
1781 case R_NIOS2_PCREL16:
1782 r = nios2_elf32_do_pcrel16_relocate (input_bfd, howto,
1783 input_section, contents,
1784 rel->r_offset, relocation,
1788 /* Turns an absolute address into a gp-relative address. */
1789 if (!nios2_elf_assign_gp (output_bfd, &gp, info))
1791 format = _("global pointer relative relocation at address "
1792 "0x%08x when _gp not defined\n");
1793 sprintf (msgbuf, format, reloc_address);
1795 r = bfd_reloc_dangerous;
1799 bfd_vma symbol_address = rel->r_addend + relocation;
1800 relocation = relocation + rel->r_addend - gp;
1802 if (((signed) relocation < -32768
1803 || (signed) relocation > 32767)
1805 || h->root.type == bfd_link_hash_defined
1806 || h->root.type == bfd_link_hash_defweak))
1808 format = _("Unable to reach %s (at 0x%08x) from the "
1809 "global pointer (at 0x%08x) because the "
1810 "offset (%d) is out of the allowed range, "
1811 "-32678 to 32767.\n" );
1812 sprintf (msgbuf, format, name, symbol_address, gp,
1813 (signed)relocation);
1815 r = bfd_reloc_outofrange;
1818 r = _bfd_final_link_relocate (howto, input_bfd,
1819 input_section, contents,
1820 rel->r_offset, relocation,
1826 r = nios2_elf32_do_ujmp_relocate (input_bfd, howto,
1828 contents, rel->r_offset,
1829 relocation, rel->r_addend);
1832 r = nios2_elf32_do_cjmp_relocate (input_bfd, howto,
1834 contents, rel->r_offset,
1835 relocation, rel->r_addend);
1838 r = nios2_elf32_do_callr_relocate (input_bfd, howto,
1839 input_section, contents,
1840 rel->r_offset, relocation,
1843 case R_NIOS2_CALL26:
1844 /* If we have a call to an undefined weak symbol, we just want
1845 to stuff a zero in the bits of the call instruction and
1846 bypass the normal call26 relocation handling, because it'll
1847 diagnose an overflow error if address 0 isn't in the same
1848 256MB segment as the call site. Presumably the call
1849 should be guarded by a null check anyway. */
1850 if (h != NULL && h->root.type == bfd_link_hash_undefweak)
1852 BFD_ASSERT (relocation == 0 && rel->r_addend == 0);
1853 r = _bfd_final_link_relocate (howto, input_bfd,
1854 input_section, contents,
1855 rel->r_offset, relocation,
1859 /* Handle relocations which should use the PLT entry.
1860 NIOS2_BFD_RELOC_32 relocations will use the symbol's value,
1861 which may point to a PLT entry, but we don't need to handle
1862 that here. If we created a PLT entry, all branches in this
1863 object should go to it. */
1864 if (h != NULL && splt != NULL && h->plt.offset != (bfd_vma) -1)
1866 /* If we've created a .plt section, and assigned a PLT entry
1867 to this function, it should not be known to bind locally.
1868 If it were, we would have cleared the PLT entry. */
1869 BFD_ASSERT (!SYMBOL_CALLS_LOCAL (info, h));
1871 relocation = (splt->output_section->vma
1872 + splt->output_offset
1875 unresolved_reloc = FALSE;
1877 r = nios2_elf32_do_call26_relocate (input_bfd, howto,
1878 input_section, contents,
1879 rel->r_offset, relocation,
1884 /* For symmetry this would be
1885 r = nios2_elf32_do_ignore_reloc (input_bfd, howto,
1886 input_section, contents,
1887 rel->r_offset, relocation,
1889 but do_ignore_reloc would do no more than return
1894 case R_NIOS2_CALL16:
1895 /* Relocation is to the entry for this symbol in the
1896 global offset table. */
1899 r = bfd_reloc_notsupported;
1909 eh = (struct elf32_nios2_link_hash_entry *)h;
1910 use_plt = (eh->got_types_used == CALL16_USED
1911 && h->plt.offset != (bfd_vma) -1);
1913 off = h->got.offset;
1914 BFD_ASSERT (off != (bfd_vma) -1);
1915 dyn = elf_hash_table (info)->dynamic_sections_created;
1916 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
1918 && SYMBOL_REFERENCES_LOCAL (info, h))
1919 || (ELF_ST_VISIBILITY (h->other)
1920 && h->root.type == bfd_link_hash_undefweak))
1922 /* This is actually a static link, or it is a -Bsymbolic
1923 link and the symbol is defined locally. We must
1924 initialize this entry in the global offset table.
1925 Since the offset must always be a multiple of 4, we
1926 use the least significant bit to record whether we
1927 have initialized it already.
1929 When doing a dynamic link, we create a .rela.got
1930 relocation entry to initialize the value. This is
1931 done in the finish_dynamic_symbol routine. */
1936 bfd_put_32 (output_bfd, relocation,
1937 sgot->contents + off);
1942 unresolved_reloc = FALSE;
1946 BFD_ASSERT (local_got_offsets != NULL
1947 && local_got_offsets[r_symndx] != (bfd_vma) -1);
1949 off = local_got_offsets[r_symndx];
1951 /* The offset must always be a multiple of 4. We use the
1952 least significant bit to record whether we have already
1953 generated the necessary reloc. */
1958 bfd_put_32 (output_bfd, relocation,
1959 sgot->contents + off);
1964 Elf_Internal_Rela outrel;
1967 srelgot = htab->root.srelgot;
1968 BFD_ASSERT (srelgot != NULL);
1970 outrel.r_addend = relocation;
1971 outrel.r_offset = (sgot->output_section->vma
1972 + sgot->output_offset
1974 outrel.r_info = ELF32_R_INFO (0, R_NIOS2_RELATIVE);
1975 loc = srelgot->contents;
1976 loc += (srelgot->reloc_count++ *
1977 sizeof (Elf32_External_Rela));
1978 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1981 local_got_offsets[r_symndx] |= 1;
1985 if (use_plt && info->shared)
1987 off = ((h->plt.offset - 24) / 12 + 3) * 4;
1988 relocation = htab->root.sgotplt->output_offset + off;
1991 relocation = sgot->output_offset + off;
1993 /* This relocation does not use the addend. */
1996 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1997 contents, rel->r_offset,
1998 relocation, rel->r_addend);
2001 case R_NIOS2_GOTOFF_LO:
2002 case R_NIOS2_GOTOFF_HA:
2003 case R_NIOS2_GOTOFF:
2004 /* Relocation is relative to the start of the
2005 global offset table. */
2007 BFD_ASSERT (sgot != NULL);
2010 r = bfd_reloc_notsupported;
2014 /* Note that sgot->output_offset is not involved in this
2015 calculation. We always want the start of .got. If we
2016 define _GLOBAL_OFFSET_TABLE in a different way, as is
2017 permitted by the ABI, we might have to change this
2019 relocation -= sgot->output_section->vma;
2020 switch (howto->type)
2022 case R_NIOS2_GOTOFF_LO:
2023 r = nios2_elf32_do_lo16_relocate (input_bfd, howto,
2024 input_section, contents,
2025 rel->r_offset, relocation,
2028 case R_NIOS2_GOTOFF_HA:
2029 r = nios2_elf32_do_hiadj16_relocate (input_bfd, howto,
2030 input_section, contents,
2036 r = _bfd_final_link_relocate (howto, input_bfd,
2037 input_section, contents,
2038 rel->r_offset, relocation,
2044 case R_NIOS2_TLS_LDO16:
2045 relocation -= dtpoff_base (info) + DTP_OFFSET;
2047 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
2048 contents, rel->r_offset,
2049 relocation, rel->r_addend);
2051 case R_NIOS2_TLS_LDM16:
2052 if (htab->root.sgot == NULL)
2055 off = htab->tls_ldm_got.offset;
2061 /* If we don't know the module number, create a relocation
2065 Elf_Internal_Rela outrel;
2068 if (htab->root.srelgot == NULL)
2071 outrel.r_addend = 0;
2072 outrel.r_offset = (htab->root.sgot->output_section->vma
2073 + htab->root.sgot->output_offset
2075 outrel.r_info = ELF32_R_INFO (0, R_NIOS2_TLS_DTPMOD);
2077 loc = htab->root.srelgot->contents;
2078 loc += (htab->root.srelgot->reloc_count++
2079 * sizeof (Elf32_External_Rela));
2080 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
2083 bfd_put_32 (output_bfd, 1,
2084 htab->root.sgot->contents + off);
2086 htab->tls_ldm_got.offset |= 1;
2089 relocation = (htab->root.sgot->output_offset + off);
2091 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
2092 contents, rel->r_offset,
2093 relocation, rel->r_addend);
2096 case R_NIOS2_TLS_GD16:
2097 case R_NIOS2_TLS_IE16:
2102 if (htab->root.sgot == NULL)
2109 dyn = htab->root.dynamic_sections_created;
2110 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
2112 || !SYMBOL_REFERENCES_LOCAL (info, h)))
2114 unresolved_reloc = FALSE;
2117 off = h->got.offset;
2118 tls_type = (((struct elf32_nios2_link_hash_entry *) h)
2123 if (local_got_offsets == NULL)
2125 off = local_got_offsets[r_symndx];
2126 tls_type = (elf32_nios2_local_got_tls_type (input_bfd)
2130 if (tls_type == GOT_UNKNOWN)
2137 bfd_boolean need_relocs = FALSE;
2138 Elf_Internal_Rela outrel;
2139 bfd_byte *loc = NULL;
2142 /* The GOT entries have not been initialized yet. Do it
2143 now, and emit any relocations. If both an IE GOT and a
2144 GD GOT are necessary, we emit the GD first. */
2146 if ((info->shared || indx != 0)
2148 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2149 || h->root.type != bfd_link_hash_undefweak))
2152 if (htab->root.srelgot == NULL)
2154 loc = htab->root.srelgot->contents;
2155 loc += (htab->root.srelgot->reloc_count *
2156 sizeof (Elf32_External_Rela));
2159 if (tls_type & GOT_TLS_GD)
2163 outrel.r_addend = 0;
2164 outrel.r_offset = (htab->root.sgot->output_section->vma
2165 + htab->root.sgot->output_offset
2167 outrel.r_info = ELF32_R_INFO (indx,
2168 R_NIOS2_TLS_DTPMOD);
2170 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
2172 htab->root.srelgot->reloc_count++;
2173 loc += sizeof (Elf32_External_Rela);
2176 bfd_put_32 (output_bfd,
2177 (relocation - dtpoff_base (info) -
2179 htab->root.sgot->contents + cur_off + 4);
2182 outrel.r_addend = 0;
2183 outrel.r_info = ELF32_R_INFO (indx,
2184 R_NIOS2_TLS_DTPREL);
2185 outrel.r_offset += 4;
2187 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
2189 htab->root.srelgot->reloc_count++;
2190 loc += sizeof (Elf32_External_Rela);
2195 /* If we are not emitting relocations for a
2196 general dynamic reference, then we must be in a
2197 static link or an executable link with the
2198 symbol binding locally. Mark it as belonging
2199 to module 1, the executable. */
2200 bfd_put_32 (output_bfd, 1,
2201 htab->root.sgot->contents + cur_off);
2202 bfd_put_32 (output_bfd, (relocation -
2203 dtpoff_base (info) -
2205 htab->root.sgot->contents + cur_off + 4);
2211 if (tls_type & GOT_TLS_IE)
2216 outrel.r_addend = (relocation -
2217 dtpoff_base (info));
2219 outrel.r_addend = 0;
2220 outrel.r_offset = (htab->root.sgot->output_section->vma
2221 + htab->root.sgot->output_offset
2223 outrel.r_info = ELF32_R_INFO (indx,
2226 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
2228 htab->root.srelgot->reloc_count++;
2229 loc += sizeof (Elf32_External_Rela);
2232 bfd_put_32 (output_bfd, (tpoff (info, relocation)
2234 htab->root.sgot->contents + cur_off);
2241 local_got_offsets[r_symndx] |= 1;
2244 if ((tls_type & GOT_TLS_GD) && r_type != R_NIOS2_TLS_GD16)
2246 relocation = (htab->root.sgot->output_offset + off);
2248 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
2249 contents, rel->r_offset,
2250 relocation, rel->r_addend);
2254 case R_NIOS2_TLS_LE16:
2255 if (info->shared && !info->pie)
2257 (*_bfd_error_handler)
2258 (_("%B(%A+0x%lx): R_NIOS2_TLS_LE16 relocation not "
2259 "permitted in shared object"),
2260 input_bfd, input_section,
2261 (long) rel->r_offset, howto->name);
2265 relocation = tpoff (info, relocation) - TP_OFFSET;
2267 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
2268 contents, rel->r_offset,
2269 relocation, rel->r_addend);
2272 case R_NIOS2_BFD_RELOC_32:
2274 && (input_section->flags & SEC_ALLOC) != 0
2276 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2277 || h->root.type != bfd_link_hash_undefweak))
2279 Elf_Internal_Rela outrel;
2281 bfd_boolean skip, relocate;
2283 /* When generating a shared object, these relocations
2284 are copied into the output file to be resolved at run
2291 = _bfd_elf_section_offset (output_bfd, info,
2292 input_section, rel->r_offset);
2293 if (outrel.r_offset == (bfd_vma) -1)
2295 else if (outrel.r_offset == (bfd_vma) -2)
2296 skip = TRUE, relocate = TRUE;
2297 outrel.r_offset += (input_section->output_section->vma
2298 + input_section->output_offset);
2301 memset (&outrel, 0, sizeof outrel);
2306 || !h->def_regular))
2308 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
2309 outrel.r_addend = rel->r_addend;
2313 /* This symbol is local, or marked to become local. */
2314 outrel.r_addend = relocation + rel->r_addend;
2316 outrel.r_info = ELF32_R_INFO (0, R_NIOS2_RELATIVE);
2319 sreloc = elf_section_data (input_section)->sreloc;
2323 loc = sreloc->contents;
2324 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
2325 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
2327 /* This reloc will be computed at runtime, so there's no
2328 need to do anything now, except for R_NIOS2_BFD_RELOC_32
2329 relocations that have been turned into
2330 R_NIOS2_RELATIVE. */
2335 r = _bfd_final_link_relocate (howto, input_bfd,
2336 input_section, contents,
2337 rel->r_offset, relocation,
2341 case R_NIOS2_TLS_DTPREL:
2342 relocation -= dtpoff_base (info);
2346 r = _bfd_final_link_relocate (howto, input_bfd,
2347 input_section, contents,
2348 rel->r_offset, relocation,
2354 r = bfd_reloc_notsupported;
2356 if (r != bfd_reloc_ok)
2359 name = h->root.root.string;
2362 name = bfd_elf_string_from_elf_section (input_bfd,
2363 symtab_hdr->sh_link,
2365 if (name == NULL || *name == '\0')
2366 name = bfd_section_name (input_bfd, sec);
2371 case bfd_reloc_overflow:
2372 r = info->callbacks->reloc_overflow (info, NULL, name,
2373 howto->name, (bfd_vma) 0,
2374 input_bfd, input_section,
2378 case bfd_reloc_undefined:
2379 r = info->callbacks->undefined_symbol (info, name, input_bfd,
2381 rel->r_offset, TRUE);
2384 case bfd_reloc_outofrange:
2386 msg = _("relocation out of range");
2389 case bfd_reloc_notsupported:
2391 msg = _("unsupported relocation");
2394 case bfd_reloc_dangerous:
2396 msg = _("dangerous relocation");
2401 msg = _("unknown error");
2407 r = info->callbacks->warning
2408 (info, msg, name, input_bfd, input_section, rel->r_offset);
2416 /* Implement elf-backend_section_flags:
2417 Convert NIOS2 specific section flags to bfd internal section flags. */
2419 nios2_elf32_section_flags (flagword *flags, const Elf_Internal_Shdr *hdr)
2421 if (hdr->sh_flags & SHF_NIOS2_GPREL)
2422 *flags |= SEC_SMALL_DATA;
2427 /* Implement elf_backend_fake_sections:
2428 Set the correct type for an NIOS2 ELF section. We do this by the
2429 section name, which is a hack, but ought to work. */
2431 nios2_elf32_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
2432 Elf_Internal_Shdr *hdr, asection *sec)
2434 register const char *name = bfd_get_section_name (abfd, sec);
2436 if ((sec->flags & SEC_SMALL_DATA)
2437 || strcmp (name, ".sdata") == 0
2438 || strcmp (name, ".sbss") == 0
2439 || strcmp (name, ".lit4") == 0 || strcmp (name, ".lit8") == 0)
2440 hdr->sh_flags |= SHF_NIOS2_GPREL;
2445 /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
2446 shortcuts to them in our hash table. */
2448 create_got_section (bfd *dynobj, struct bfd_link_info *info)
2450 struct elf32_nios2_link_hash_table *htab;
2452 htab = elf32_nios2_hash_table (info);
2454 if (! _bfd_elf_create_got_section (dynobj, info))
2457 /* In order for the two loads in .PLTresolve to share the same %hiadj,
2458 _GLOBAL_OFFSET_TABLE_ must be aligned to a 16-byte boundary. */
2459 if (!bfd_set_section_alignment (dynobj, htab->root.sgotplt, 4))
2465 /* Implement elf_backend_create_dynamic_sections:
2466 Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
2467 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
2470 nios2_elf32_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
2472 struct elf32_nios2_link_hash_table *htab;
2474 htab = elf32_nios2_hash_table (info);
2475 if (!htab->root.sgot && !create_got_section (dynobj, info))
2478 _bfd_elf_create_dynamic_sections (dynobj, info);
2480 /* In order for the two loads in a shared object .PLTresolve to share the
2481 same %hiadj, the start of the PLT (as well as the GOT) must be aligned
2482 to a 16-byte boundary. This is because the addresses for these loads
2483 include the -(.plt+4) PIC correction. */
2484 if (!bfd_set_section_alignment (dynobj, htab->root.splt, 4))
2487 htab->sdynbss = bfd_get_linker_section (dynobj, ".dynbss");
2492 htab->srelbss = bfd_get_linker_section (dynobj, ".rela.bss");
2500 /* Implement elf_backend_copy_indirect_symbol:
2501 Copy the extra info we tack onto an elf_link_hash_entry. */
2503 nios2_elf32_copy_indirect_symbol (struct bfd_link_info *info,
2504 struct elf_link_hash_entry *dir,
2505 struct elf_link_hash_entry *ind)
2507 struct elf32_nios2_link_hash_entry *edir, *eind;
2509 edir = (struct elf32_nios2_link_hash_entry *) dir;
2510 eind = (struct elf32_nios2_link_hash_entry *) ind;
2512 if (eind->dyn_relocs != NULL)
2514 if (edir->dyn_relocs != NULL)
2516 struct elf32_nios2_dyn_relocs **pp;
2517 struct elf32_nios2_dyn_relocs *p;
2519 /* Add reloc counts against the indirect sym to the direct sym
2520 list. Merge any entries against the same section. */
2521 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
2523 struct elf32_nios2_dyn_relocs *q;
2525 for (q = edir->dyn_relocs; q != NULL; q = q->next)
2526 if (q->sec == p->sec)
2528 q->pc_count += p->pc_count;
2529 q->count += p->count;
2536 *pp = edir->dyn_relocs;
2539 edir->dyn_relocs = eind->dyn_relocs;
2540 eind->dyn_relocs = NULL;
2543 if (ind->root.type == bfd_link_hash_indirect
2544 && dir->got.refcount <= 0)
2546 edir->tls_type = eind->tls_type;
2547 eind->tls_type = GOT_UNKNOWN;
2550 edir->got_types_used |= eind->got_types_used;
2552 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
2555 /* Implement elf_backend_check_relocs:
2556 Look through the relocs for a section during the first phase. */
2558 nios2_elf32_check_relocs (bfd *abfd, struct bfd_link_info *info,
2559 asection *sec, const Elf_Internal_Rela *relocs)
2562 Elf_Internal_Shdr *symtab_hdr;
2563 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
2564 const Elf_Internal_Rela *rel;
2565 const Elf_Internal_Rela *rel_end;
2566 struct elf32_nios2_link_hash_table *htab;
2569 asection *sreloc = NULL;
2570 bfd_signed_vma *local_got_refcounts;
2572 if (info->relocatable)
2575 dynobj = elf_hash_table (info)->dynobj;
2576 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2577 sym_hashes = elf_sym_hashes (abfd);
2578 sym_hashes_end = (sym_hashes
2579 + symtab_hdr->sh_size / sizeof (Elf32_External_Sym));
2580 if (!elf_bad_symtab (abfd))
2581 sym_hashes_end -= symtab_hdr->sh_info;
2582 local_got_refcounts = elf_local_got_refcounts (abfd);
2584 htab = elf32_nios2_hash_table (info);
2585 sgot = htab->root.sgot;
2586 srelgot = htab->root.srelgot;
2588 rel_end = relocs + sec->reloc_count;
2589 for (rel = relocs; rel < rel_end; rel++)
2591 unsigned int r_type;
2592 struct elf_link_hash_entry *h;
2593 unsigned long r_symndx;
2595 r_symndx = ELF32_R_SYM (rel->r_info);
2596 if (r_symndx < symtab_hdr->sh_info)
2600 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2601 while (h->root.type == bfd_link_hash_indirect
2602 || h->root.type == bfd_link_hash_warning)
2603 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2605 /* PR15323, ref flags aren't set for references in the same
2607 h->root.non_ir_ref = 1;
2610 r_type = ELF32_R_TYPE (rel->r_info);
2615 case R_NIOS2_CALL16:
2616 case R_NIOS2_TLS_GD16:
2617 case R_NIOS2_TLS_IE16:
2618 /* This symbol requires a global offset table entry. */
2620 int tls_type, old_tls_type;
2626 case R_NIOS2_CALL16:
2627 tls_type = GOT_NORMAL;
2629 case R_NIOS2_TLS_GD16:
2630 tls_type = GOT_TLS_GD;
2632 case R_NIOS2_TLS_IE16:
2633 tls_type = GOT_TLS_IE;
2639 /* Create the .got section. */
2640 elf_hash_table (info)->dynobj = dynobj = abfd;
2641 nios2_elf32_create_dynamic_sections (dynobj, info);
2646 sgot = htab->root.sgot;
2647 BFD_ASSERT (sgot != NULL);
2651 && (h != NULL || info->shared))
2653 srelgot = htab->root.srelgot;
2654 BFD_ASSERT (srelgot != NULL);
2659 struct elf32_nios2_link_hash_entry *eh
2660 = (struct elf32_nios2_link_hash_entry *)h;
2662 old_tls_type = elf32_nios2_hash_entry(h)->tls_type;
2663 if (r_type == R_NIOS2_CALL16)
2665 /* Make sure a plt entry is created for this symbol if
2666 it turns out to be a function defined by a dynamic
2671 eh->got_types_used |= CALL16_USED;
2674 eh->got_types_used |= GOT16_USED;
2678 /* This is a global offset table entry for a local symbol. */
2679 if (local_got_refcounts == NULL)
2683 size = symtab_hdr->sh_info;
2684 size *= (sizeof (bfd_signed_vma) + sizeof (char));
2686 = ((bfd_signed_vma *) bfd_zalloc (abfd, size));
2687 if (local_got_refcounts == NULL)
2689 elf_local_got_refcounts (abfd) = local_got_refcounts;
2690 elf32_nios2_local_got_tls_type (abfd)
2691 = (char *) (local_got_refcounts + symtab_hdr->sh_info);
2693 local_got_refcounts[r_symndx]++;
2694 old_tls_type = elf32_nios2_local_got_tls_type (abfd) [r_symndx];
2697 /* We will already have issued an error message if there is a
2698 TLS / non-TLS mismatch, based on the symbol type. We don't
2699 support any linker relaxations. So just combine any TLS
2701 if (old_tls_type != GOT_UNKNOWN && old_tls_type != GOT_NORMAL
2702 && tls_type != GOT_NORMAL)
2703 tls_type |= old_tls_type;
2705 if (old_tls_type != tls_type)
2708 elf32_nios2_hash_entry (h)->tls_type = tls_type;
2710 elf32_nios2_local_got_tls_type (abfd) [r_symndx] = tls_type;
2714 case R_NIOS2_TLS_LDM16:
2715 if (r_type == R_NIOS2_TLS_LDM16)
2716 htab->tls_ldm_got.refcount++;
2718 if (htab->root.sgot == NULL)
2720 if (htab->root.dynobj == NULL)
2721 htab->root.dynobj = abfd;
2722 if (!create_got_section (htab->root.dynobj, info))
2727 /* This relocation describes the C++ object vtable hierarchy.
2728 Reconstruct it for later use during GC. */
2729 case R_NIOS2_GNU_VTINHERIT:
2730 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
2734 /* This relocation describes which C++ vtable entries are actually
2735 used. Record for later use during GC. */
2736 case R_NIOS2_GNU_VTENTRY:
2737 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
2741 case R_NIOS2_BFD_RELOC_32:
2742 case R_NIOS2_CALL26:
2743 case R_NIOS2_HIADJ16:
2748 /* If this reloc is in a read-only section, we might
2749 need a copy reloc. We can't check reliably at this
2750 stage whether the section is read-only, as input
2751 sections have not yet been mapped to output sections.
2752 Tentatively set the flag for now, and correct in
2753 adjust_dynamic_symbol. */
2757 /* Make sure a plt entry is created for this symbol if it
2758 turns out to be a function defined by a dynamic object. */
2761 if (r_type == R_NIOS2_CALL26)
2765 /* If we are creating a shared library, we need to copy the
2766 reloc into the shared library. */
2768 && (sec->flags & SEC_ALLOC) != 0
2769 && (r_type == R_NIOS2_BFD_RELOC_32
2770 || (h != NULL && ! h->needs_plt
2771 && (! info->symbolic || ! h->def_regular))))
2773 struct elf32_nios2_dyn_relocs *p;
2774 struct elf32_nios2_dyn_relocs **head;
2776 /* When creating a shared object, we must copy these
2777 reloc types into the output file. We create a reloc
2778 section in dynobj and make room for this reloc. */
2781 sreloc = _bfd_elf_make_dynamic_reloc_section
2782 (sec, dynobj, 2, abfd, TRUE);
2787 /* If this is a global symbol, we count the number of
2788 relocations we need for this symbol. */
2790 head = &((struct elf32_nios2_link_hash_entry *) h)->dyn_relocs;
2793 /* Track dynamic relocs needed for local syms too.
2794 We really need local syms available to do this
2799 Elf_Internal_Sym *isym;
2801 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
2806 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
2810 vpp = &elf_section_data (s)->local_dynrel;
2811 head = (struct elf32_nios2_dyn_relocs **) vpp;
2815 if (p == NULL || p->sec != sec)
2817 bfd_size_type amt = sizeof *p;
2818 p = ((struct elf32_nios2_dyn_relocs *)
2819 bfd_alloc (htab->root.dynobj, amt));
2840 /* Implement elf_backend_gc_mark_hook:
2841 Return the section that should be marked against GC for a given
2844 nios2_elf32_gc_mark_hook (asection *sec,
2845 struct bfd_link_info *info ATTRIBUTE_UNUSED,
2846 Elf_Internal_Rela *rel,
2847 struct elf_link_hash_entry *h,
2848 Elf_Internal_Sym *sym)
2851 switch (ELF32_R_TYPE (rel->r_info))
2853 case R_NIOS2_GNU_VTINHERIT:
2854 case R_NIOS2_GNU_VTENTRY:
2857 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
2860 /* Implement elf_backend_gc_sweep_hook:
2861 Update the got entry reference counts for the section being removed. */
2863 nios2_elf32_gc_sweep_hook (bfd *abfd,
2864 struct bfd_link_info *info,
2866 const Elf_Internal_Rela *relocs)
2868 Elf_Internal_Shdr *symtab_hdr;
2869 struct elf_link_hash_entry **sym_hashes;
2870 bfd_signed_vma *local_got_refcounts;
2871 const Elf_Internal_Rela *rel, *relend;
2874 if (info->relocatable)
2877 elf_section_data (sec)->local_dynrel = NULL;
2879 dynobj = elf_hash_table (info)->dynobj;
2883 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2884 sym_hashes = elf_sym_hashes (abfd);
2885 local_got_refcounts = elf_local_got_refcounts (abfd);
2887 relend = relocs + sec->reloc_count;
2888 for (rel = relocs; rel < relend; rel++)
2890 unsigned long r_symndx;
2891 struct elf_link_hash_entry *h = NULL;
2894 r_symndx = ELF32_R_SYM (rel->r_info);
2895 if (r_symndx >= symtab_hdr->sh_info)
2897 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2898 while (h->root.type == bfd_link_hash_indirect
2899 || h->root.type == bfd_link_hash_warning)
2900 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2903 r_type = ELF32_R_TYPE (rel->r_info);
2907 case R_NIOS2_CALL16:
2910 if (h->got.refcount > 0)
2913 else if (local_got_refcounts != NULL)
2915 if (local_got_refcounts[r_symndx] > 0)
2916 --local_got_refcounts[r_symndx];
2920 case R_NIOS2_PCREL_LO:
2921 case R_NIOS2_PCREL_HA:
2922 case R_NIOS2_BFD_RELOC_32:
2923 case R_NIOS2_CALL26:
2926 struct elf32_nios2_link_hash_entry *eh;
2927 struct elf32_nios2_dyn_relocs **pp;
2928 struct elf32_nios2_dyn_relocs *p;
2930 eh = (struct elf32_nios2_link_hash_entry *) h;
2932 if (h->plt.refcount > 0)
2935 if (r_type == R_NIOS2_PCREL_LO || r_type == R_NIOS2_PCREL_HA
2936 || r_type == R_NIOS2_BFD_RELOC_32)
2938 for (pp = &eh->dyn_relocs; (p = *pp) != NULL;
2959 /* Install 16-bit immediate value VALUE at offset OFFSET into section SEC. */
2961 nios2_elf32_install_imm16 (asection *sec, bfd_vma offset, bfd_vma value)
2963 bfd_vma word = bfd_get_32 (sec->owner, sec->contents + offset);
2965 BFD_ASSERT(value <= 0xffff);
2967 bfd_put_32 (sec->owner, word | ((value & 0xffff) << 6),
2968 sec->contents + offset);
2971 /* Install COUNT 32-bit values DATA starting at offset OFFSET into
2974 nios2_elf32_install_data (asection *sec, const bfd_vma *data, bfd_vma offset,
2979 bfd_put_32 (sec->owner, *data, sec->contents + offset);
2985 /* Implement elf_backend_finish_dynamic_symbols:
2986 Finish up dynamic symbol handling. We set the contents of various
2987 dynamic sections here. */
2989 nios2_elf32_finish_dynamic_symbol (bfd *output_bfd,
2990 struct bfd_link_info *info,
2991 struct elf_link_hash_entry *h,
2992 Elf_Internal_Sym *sym)
2994 struct elf32_nios2_link_hash_table *htab;
2995 struct elf32_nios2_link_hash_entry *eh
2996 = (struct elf32_nios2_link_hash_entry *)h;
2999 htab = elf32_nios2_hash_table (info);
3001 if (h->plt.offset != (bfd_vma) -1)
3008 Elf_Internal_Rela rela;
3010 bfd_vma got_address;
3012 /* This symbol has an entry in the procedure linkage table. Set
3014 BFD_ASSERT (h->dynindx != -1);
3015 splt = htab->root.splt;
3016 sgotplt = htab->root.sgotplt;
3017 srela = htab->root.srelplt;
3018 BFD_ASSERT (splt != NULL && sgotplt != NULL && srela != NULL);
3020 /* Emit the PLT entry. */
3023 nios2_elf32_install_data (splt, nios2_so_plt_entry, h->plt.offset,
3025 plt_index = (h->plt.offset - 24) / 12;
3026 got_offset = (plt_index + 3) * 4;
3027 nios2_elf32_install_imm16 (splt, h->plt.offset,
3028 hiadj(plt_index * 4));
3029 nios2_elf32_install_imm16 (splt, h->plt.offset + 4,
3030 (plt_index * 4) & 0xffff);
3031 nios2_elf32_install_imm16 (splt, h->plt.offset + 8,
3032 0xfff4 - h->plt.offset);
3033 got_address = (sgotplt->output_section->vma + sgotplt->output_offset
3036 /* Fill in the entry in the global offset table. There are no
3037 res_n slots for a shared object PLT, instead the .got.plt entries
3038 point to the PLT entries. */
3039 bfd_put_32 (output_bfd,
3040 splt->output_section->vma + splt->output_offset
3041 + h->plt.offset, sgotplt->contents + got_offset);
3045 plt_index = (h->plt.offset - 28 - htab->res_n_size) / 12;
3046 got_offset = (plt_index + 3) * 4;
3048 nios2_elf32_install_data (splt, nios2_plt_entry, h->plt.offset, 3);
3049 got_address = (sgotplt->output_section->vma + sgotplt->output_offset
3051 nios2_elf32_install_imm16 (splt, h->plt.offset, hiadj(got_address));
3052 nios2_elf32_install_imm16 (splt, h->plt.offset + 4,
3053 got_address & 0xffff);
3055 /* Fill in the entry in the global offset table. */
3056 bfd_put_32 (output_bfd,
3057 splt->output_section->vma + splt->output_offset
3058 + plt_index * 4, sgotplt->contents + got_offset);
3061 /* Fill in the entry in the .rela.plt section. */
3062 rela.r_offset = got_address;
3063 rela.r_info = ELF32_R_INFO (h->dynindx, R_NIOS2_JUMP_SLOT);
3065 loc = srela->contents + plt_index * sizeof (Elf32_External_Rela);
3066 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
3068 if (!h->def_regular)
3070 /* Mark the symbol as undefined, rather than as defined in
3071 the .plt section. Leave the value alone. */
3072 sym->st_shndx = SHN_UNDEF;
3073 /* If the symbol is weak, we do need to clear the value.
3074 Otherwise, the PLT entry would provide a definition for
3075 the symbol even if the symbol wasn't defined anywhere,
3076 and so the symbol would never be NULL. */
3077 if (!h->ref_regular_nonweak)
3082 use_plt = (eh->got_types_used == CALL16_USED
3083 && h->plt.offset != (bfd_vma) -1);
3085 if (!use_plt && h->got.offset != (bfd_vma) -1
3086 && (elf32_nios2_hash_entry (h)->tls_type & GOT_TLS_GD) == 0
3087 && (elf32_nios2_hash_entry (h)->tls_type & GOT_TLS_IE) == 0)
3091 Elf_Internal_Rela rela;
3095 /* This symbol has an entry in the global offset table. Set it
3097 sgot = htab->root.sgot;
3098 srela = htab->root.srelgot;
3099 BFD_ASSERT (sgot != NULL && srela != NULL);
3101 offset = (h->got.offset & ~(bfd_vma) 1);
3102 rela.r_offset = (sgot->output_section->vma
3103 + sgot->output_offset + offset);
3105 /* If this is a -Bsymbolic link, and the symbol is defined
3106 locally, we just want to emit a RELATIVE reloc. Likewise if
3107 the symbol was forced to be local because of a version file.
3108 The entry in the global offset table will already have been
3109 initialized in the relocate_section function. */
3111 if (info->shared && SYMBOL_REFERENCES_LOCAL (info, h))
3113 rela.r_info = ELF32_R_INFO (0, R_NIOS2_RELATIVE);
3114 rela.r_addend = bfd_get_signed_32 (output_bfd,
3115 (sgot->contents + offset));
3116 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + offset);
3120 bfd_put_32 (output_bfd, (bfd_vma) 0,
3121 sgot->contents + offset);
3122 rela.r_info = ELF32_R_INFO (h->dynindx, R_NIOS2_GLOB_DAT);
3126 loc = srela->contents;
3127 loc += srela->reloc_count++ * sizeof (Elf32_External_Rela);
3128 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
3131 if (use_plt && h->got.offset != (bfd_vma) -1)
3133 bfd_vma offset = (h->got.offset & ~(bfd_vma) 1);
3134 asection *sgot = htab->root.sgot;
3135 asection *splt = htab->root.splt;
3136 bfd_put_32 (output_bfd, (splt->output_section->vma + splt->output_offset
3138 sgot->contents + offset);
3144 Elf_Internal_Rela rela;
3147 /* This symbol needs a copy reloc. Set it up. */
3148 BFD_ASSERT (h->dynindx != -1
3149 && (h->root.type == bfd_link_hash_defined
3150 || h->root.type == bfd_link_hash_defweak));
3153 BFD_ASSERT (s != NULL);
3155 rela.r_offset = (h->root.u.def.value
3156 + h->root.u.def.section->output_section->vma
3157 + h->root.u.def.section->output_offset);
3158 rela.r_info = ELF32_R_INFO (h->dynindx, R_NIOS2_COPY);
3160 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
3161 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
3164 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
3165 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
3166 || h == elf_hash_table (info)->hgot)
3167 sym->st_shndx = SHN_ABS;
3172 /* Implement elf_backend_finish_dynamic_sections. */
3174 nios2_elf32_finish_dynamic_sections (bfd *output_bfd,
3175 struct bfd_link_info *info)
3180 struct elf32_nios2_link_hash_table *htab;
3182 htab = elf32_nios2_hash_table (info);
3183 dynobj = elf_hash_table (info)->dynobj;
3184 sgotplt = htab->root.sgotplt;
3185 BFD_ASSERT (sgotplt != NULL);
3186 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3188 if (elf_hash_table (info)->dynamic_sections_created)
3191 Elf32_External_Dyn *dyncon, *dynconend;
3193 splt = htab->root.splt;
3194 BFD_ASSERT (splt != NULL && sdyn != NULL);
3196 dyncon = (Elf32_External_Dyn *) sdyn->contents;
3197 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
3198 for (; dyncon < dynconend; dyncon++)
3200 Elf_Internal_Dyn dyn;
3203 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
3211 s = htab->root.sgot;
3212 BFD_ASSERT (s != NULL);
3213 dyn.d_un.d_ptr = s->output_section->vma;
3214 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3218 s = htab->root.srelplt;
3219 BFD_ASSERT (s != NULL);
3220 dyn.d_un.d_ptr = s->output_section->vma;
3221 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3225 s = htab->root.srelplt;
3226 BFD_ASSERT (s != NULL);
3227 dyn.d_un.d_val = s->size;
3228 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3232 /* The procedure linkage table relocs (DT_JMPREL) should
3233 not be included in the overall relocs (DT_RELA).
3234 Therefore, we override the DT_RELASZ entry here to
3235 make it not include the JMPREL relocs. Since the
3236 linker script arranges for .rela.plt to follow all
3237 other relocation sections, we don't have to worry
3238 about changing the DT_RELA entry. */
3239 s = htab->root.srelplt;
3241 dyn.d_un.d_val -= s->size;
3242 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3246 s = htab->root.sgot;
3247 BFD_ASSERT (s != NULL);
3248 dyn.d_un.d_ptr = s->output_section->vma + 0x7ff0;
3249 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3254 /* Fill in the first entry in the procedure linkage table. */
3257 bfd_vma got_address = (sgotplt->output_section->vma
3258 + sgotplt->output_offset);
3261 bfd_vma corrected = got_address - (splt->output_section->vma
3262 + splt->output_offset + 4);
3263 nios2_elf32_install_data (splt, nios2_so_plt0_entry, 0, 6);
3264 nios2_elf32_install_imm16 (splt, 4, hiadj (corrected));
3265 nios2_elf32_install_imm16 (splt, 12, (corrected & 0xffff) + 4);
3266 nios2_elf32_install_imm16 (splt, 16, (corrected & 0xffff) + 8);
3270 /* Divide by 4 here, not 3 because we already corrected for the
3272 bfd_vma res_size = (splt->size - 28) / 4;
3273 bfd_vma res_start = (splt->output_section->vma
3274 + splt->output_offset);
3277 for (res_offset = 0; res_offset < res_size; res_offset += 4)
3278 bfd_put_32 (output_bfd,
3279 6 | ((res_size - (res_offset + 4)) << 6),
3280 splt->contents + res_offset);
3282 nios2_elf32_install_data (splt, nios2_plt0_entry, res_size, 7);
3283 nios2_elf32_install_imm16 (splt, res_size, hiadj (res_start));
3284 nios2_elf32_install_imm16 (splt, res_size + 4,
3285 res_start & 0xffff);
3286 nios2_elf32_install_imm16 (splt, res_size + 12,
3287 hiadj (got_address));
3288 nios2_elf32_install_imm16 (splt, res_size + 16,
3289 (got_address & 0xffff) + 4);
3290 nios2_elf32_install_imm16 (splt, res_size + 20,
3291 (got_address & 0xffff) + 8);
3295 /* Fill in the first three entries in the global offset table. */
3296 if (sgotplt->size > 0)
3299 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents);
3301 bfd_put_32 (output_bfd,
3302 sdyn->output_section->vma + sdyn->output_offset,
3304 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 4);
3305 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 8);
3308 elf_section_data (sgotplt->output_section)->this_hdr.sh_entsize = 4;
3313 /* Implement elf_backend_adjust_dynamic_symbol:
3314 Adjust a symbol defined by a dynamic object and referenced by a
3315 regular object. The current definition is in some section of the
3316 dynamic object, but we're not including those sections. We have to
3317 change the definition to something the rest of the link can
3320 nios2_elf32_adjust_dynamic_symbol (struct bfd_link_info *info,
3321 struct elf_link_hash_entry *h)
3323 struct elf32_nios2_link_hash_table *htab;
3328 htab = elf32_nios2_hash_table (info);
3329 dynobj = elf_hash_table (info)->dynobj;
3331 /* Make sure we know what is going on here. */
3332 BFD_ASSERT (dynobj != NULL
3334 || h->u.weakdef != NULL
3337 && !h->def_regular)));
3339 /* If this is a function, put it in the procedure linkage table. We
3340 will fill in the contents of the procedure linkage table later,
3341 when we know the address of the .got section. */
3342 if (h->type == STT_FUNC || h->needs_plt)
3344 if (h->plt.refcount <= 0
3345 || SYMBOL_CALLS_LOCAL (info, h)
3346 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3347 && h->root.type == bfd_link_hash_undefweak))
3349 /* This case can occur if we saw a PLT reloc in an input
3350 file, but the symbol was never referred to by a dynamic
3351 object, or if all references were garbage collected. In
3352 such a case, we don't actually need to build a procedure
3353 linkage table, and we can just do a PCREL reloc instead. */
3354 h->plt.offset = (bfd_vma) -1;
3361 /* Reinitialize the plt offset now that it is not used as a reference
3363 h->plt.offset = (bfd_vma) -1;
3365 /* If this is a weak symbol, and there is a real definition, the
3366 processor independent code will have arranged for us to see the
3367 real definition first, and we can just use the same value. */
3368 if (h->u.weakdef != NULL)
3370 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
3371 || h->u.weakdef->root.type == bfd_link_hash_defweak);
3372 h->root.u.def.section = h->u.weakdef->root.u.def.section;
3373 h->root.u.def.value = h->u.weakdef->root.u.def.value;
3377 /* If there are no non-GOT references, we do not need a copy
3379 if (!h->non_got_ref)
3382 /* This is a reference to a symbol defined by a dynamic object which
3384 If we are creating a shared library, we must presume that the
3385 only references to the symbol are via the global offset table.
3386 For such cases we need not do anything here; the relocations will
3387 be handled correctly by relocate_section. */
3393 (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
3394 h->root.root.string);
3398 /* We must allocate the symbol in our .dynbss section, which will
3399 become part of the .bss section of the executable. There will be
3400 an entry for this symbol in the .dynsym section. The dynamic
3401 object will contain position independent code, so all references
3402 from the dynamic object to this symbol will go through the global
3403 offset table. The dynamic linker will use the .dynsym entry to
3404 determine the address it must put in the global offset table, so
3405 both the dynamic object and the regular object will refer to the
3406 same memory location for the variable. */
3408 BFD_ASSERT (s != NULL);
3410 /* We must generate a R_NIOS2_COPY reloc to tell the dynamic linker to
3411 copy the initial value out of the dynamic object and into the
3412 runtime process image. We need to remember the offset into the
3413 .rela.bss section we are going to use. */
3414 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
3418 srel = htab->srelbss;
3419 BFD_ASSERT (srel != NULL);
3420 srel->size += sizeof (Elf32_External_Rela);
3424 align2 = bfd_log2 (h->size);
3425 if (align2 > h->root.u.def.section->alignment_power)
3426 align2 = h->root.u.def.section->alignment_power;
3429 s->size = BFD_ALIGN (s->size, (bfd_size_type)1 << align2);
3430 if (align2 > bfd_get_section_alignment (dynobj, s)
3431 && !bfd_set_section_alignment (dynobj, s, align2))
3434 /* Define the symbol as being at this point in the section. */
3435 h->root.u.def.section = s;
3436 h->root.u.def.value = s->size;
3438 /* Increment the section size to make room for the symbol. */
3444 /* Worker function for nios2_elf32_size_dynamic_sections. */
3446 adjust_dynrelocs (struct elf_link_hash_entry *h, PTR inf)
3448 struct bfd_link_info *info;
3449 struct elf32_nios2_link_hash_table *htab;
3451 if (h->root.type == bfd_link_hash_indirect)
3454 if (h->root.type == bfd_link_hash_warning)
3455 /* When warning symbols are created, they **replace** the "real"
3456 entry in the hash table, thus we never get to see the real
3457 symbol in a hash traversal. So look at it now. */
3458 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3460 info = (struct bfd_link_info *) inf;
3461 htab = elf32_nios2_hash_table (info);
3463 if (h->plt.offset != (bfd_vma)-1)
3464 h->plt.offset += htab->res_n_size;
3465 if (htab->root.splt == h->root.u.def.section)
3466 h->root.u.def.value += htab->res_n_size;
3471 /* Another worker function for nios2_elf32_size_dynamic_sections.
3472 Allocate space in .plt, .got and associated reloc sections for
3475 allocate_dynrelocs (struct elf_link_hash_entry *h, PTR inf)
3477 struct bfd_link_info *info;
3478 struct elf32_nios2_link_hash_table *htab;
3479 struct elf32_nios2_link_hash_entry *eh;
3480 struct elf32_nios2_dyn_relocs *p;
3483 if (h->root.type == bfd_link_hash_indirect)
3486 if (h->root.type == bfd_link_hash_warning)
3487 /* When warning symbols are created, they **replace** the "real"
3488 entry in the hash table, thus we never get to see the real
3489 symbol in a hash traversal. So look at it now. */
3490 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3492 info = (struct bfd_link_info *) inf;
3493 htab = elf32_nios2_hash_table (info);
3495 if (htab->root.dynamic_sections_created
3496 && h->plt.refcount > 0)
3498 /* Make sure this symbol is output as a dynamic symbol.
3499 Undefined weak syms won't yet be marked as dynamic. */
3500 if (h->dynindx == -1
3502 && !bfd_elf_link_record_dynamic_symbol (info, h))
3505 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
3507 asection *s = htab->root.splt;
3509 /* Allocate room for the header. */
3518 h->plt.offset = s->size;
3520 /* If this symbol is not defined in a regular file, and we are
3521 not generating a shared library, then set the symbol to this
3522 location in the .plt. This is required to make function
3523 pointers compare as equal between the normal executable and
3524 the shared library. */
3528 h->root.u.def.section = s;
3529 h->root.u.def.value = h->plt.offset;
3532 /* Make room for this entry. */
3535 /* We also need to make an entry in the .rela.plt section. */
3536 htab->root.srelplt->size += sizeof (Elf32_External_Rela);
3538 /* And the .got.plt section. */
3539 htab->root.sgotplt->size += 4;
3543 h->plt.offset = (bfd_vma) -1;
3549 h->plt.offset = (bfd_vma) -1;
3553 eh = (struct elf32_nios2_link_hash_entry *) h;
3554 use_plt = (eh->got_types_used == CALL16_USED
3555 && h->plt.offset != (bfd_vma) -1);
3557 if (h->got.refcount > 0)
3561 int tls_type = eh->tls_type;
3564 /* Make sure this symbol is output as a dynamic symbol.
3565 Undefined weak syms won't yet be marked as dynamic. */
3566 if (h->dynindx == -1
3568 && !bfd_elf_link_record_dynamic_symbol (info, h))
3571 s = htab->root.sgot;
3572 h->got.offset = s->size;
3574 if (tls_type == GOT_UNKNOWN)
3577 if (tls_type == GOT_NORMAL)
3578 /* Non-TLS symbols need one GOT slot. */
3582 if (tls_type & GOT_TLS_GD)
3583 /* R_NIOS2_TLS_GD16 needs 2 consecutive GOT slots. */
3585 if (tls_type & GOT_TLS_IE)
3586 /* R_NIOS2_TLS_IE16 needs one GOT slot. */
3590 dyn = htab->root.dynamic_sections_created;
3593 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
3595 || !SYMBOL_REFERENCES_LOCAL (info, h)))
3598 if (tls_type != GOT_NORMAL
3599 && (info->shared || indx != 0)
3600 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3601 || h->root.type != bfd_link_hash_undefweak))
3603 if (tls_type & GOT_TLS_IE)
3604 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
3606 if (tls_type & GOT_TLS_GD)
3607 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
3609 if ((tls_type & GOT_TLS_GD) && indx != 0)
3610 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
3612 else if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3613 || h->root.type != bfd_link_hash_undefweak)
3616 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
3617 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
3620 h->got.offset = (bfd_vma) -1;
3622 if (eh->dyn_relocs == NULL)
3625 /* In the shared -Bsymbolic case, discard space allocated for
3626 dynamic pc-relative relocs against symbols which turn out to be
3627 defined in regular objects. For the normal shared case, discard
3628 space for pc-relative relocs that have become local due to symbol
3629 visibility changes. */
3634 && (h->forced_local || info->symbolic))
3636 struct elf32_nios2_dyn_relocs **pp;
3638 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
3640 p->count -= p->pc_count;
3649 /* Also discard relocs on undefined weak syms with non-default
3651 if (eh->dyn_relocs != NULL
3652 && h->root.type == bfd_link_hash_undefweak)
3654 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
3655 eh->dyn_relocs = NULL;
3657 /* Make sure undefined weak symbols are output as a dynamic
3659 else if (h->dynindx == -1
3661 && !bfd_elf_link_record_dynamic_symbol (info, h))
3667 /* For the non-shared case, discard space for relocs against
3668 symbols which turn out to need copy relocs or are not
3672 && ((h->def_dynamic && !h->def_regular)
3673 || (htab->root.dynamic_sections_created
3674 && (h->root.type == bfd_link_hash_undefweak
3675 || h->root.type == bfd_link_hash_undefined))))
3677 /* Make sure this symbol is output as a dynamic symbol.
3678 Undefined weak syms won't yet be marked as dynamic. */
3679 if (h->dynindx == -1
3681 && !bfd_elf_link_record_dynamic_symbol (info, h))
3684 /* If that succeeded, we know we'll be keeping all the
3686 if (h->dynindx != -1)
3690 eh->dyn_relocs = NULL;
3695 /* Finally, allocate space. */
3696 for (p = eh->dyn_relocs; p != NULL; p = p->next)
3698 asection *sreloc = elf_section_data (p->sec)->sreloc;
3699 sreloc->size += p->count * sizeof (Elf32_External_Rela);
3705 /* Implement elf_backend_size_dynamic_sections:
3706 Set the sizes of the dynamic sections. */
3708 nios2_elf32_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
3709 struct bfd_link_info *info)
3717 struct elf32_nios2_link_hash_table *htab;
3719 htab = elf32_nios2_hash_table (info);
3720 dynobj = elf_hash_table (info)->dynobj;
3721 BFD_ASSERT (dynobj != NULL);
3723 htab->res_n_size = 0;
3724 if (elf_hash_table (info)->dynamic_sections_created)
3726 /* Set the contents of the .interp section to the interpreter. */
3727 if (info->executable)
3729 s = bfd_get_linker_section (dynobj, ".interp");
3730 BFD_ASSERT (s != NULL);
3731 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
3732 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
3737 /* We may have created entries in the .rela.got section.
3738 However, if we are not creating the dynamic sections, we will
3739 not actually use these entries. Reset the size of .rela.got,
3740 which will cause it to get stripped from the output file
3742 s = htab->root.srelgot;
3747 /* Set up .got offsets for local syms, and space for local dynamic
3749 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
3751 bfd_signed_vma *local_got;
3752 bfd_signed_vma *end_local_got;
3753 char *local_tls_type;
3754 bfd_size_type locsymcount;
3755 Elf_Internal_Shdr *symtab_hdr;
3758 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
3761 for (s = ibfd->sections; s != NULL; s = s->next)
3763 struct elf32_nios2_dyn_relocs *p;
3765 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
3767 if (!bfd_is_abs_section (p->sec)
3768 && bfd_is_abs_section (p->sec->output_section))
3770 /* Input section has been discarded, either because
3771 it is a copy of a linkonce section or due to
3772 linker script /DISCARD/, so we'll be discarding
3775 else if (p->count != 0)
3777 srel = elf_section_data (p->sec)->sreloc;
3778 srel->size += p->count * sizeof (Elf32_External_Rela);
3779 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
3780 info->flags |= DF_TEXTREL;
3785 local_got = elf_local_got_refcounts (ibfd);
3789 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
3790 locsymcount = symtab_hdr->sh_info;
3791 end_local_got = local_got + locsymcount;
3792 local_tls_type = elf32_nios2_local_got_tls_type (ibfd);
3793 s = htab->root.sgot;
3794 srel = htab->root.srelgot;
3795 for (; local_got < end_local_got; ++local_got, ++local_tls_type)
3799 *local_got = s->size;
3800 if (*local_tls_type & GOT_TLS_GD)
3801 /* TLS_GD relocs need an 8-byte structure in the GOT. */
3803 if (*local_tls_type & GOT_TLS_IE)
3805 if (*local_tls_type == GOT_NORMAL)
3808 if (info->shared || *local_tls_type == GOT_TLS_GD)
3809 srel->size += sizeof (Elf32_External_Rela);
3812 *local_got = (bfd_vma) -1;
3816 if (htab->tls_ldm_got.refcount > 0)
3818 /* Allocate two GOT entries and one dynamic relocation (if necessary)
3819 for R_NIOS2_TLS_LDM16 relocations. */
3820 htab->tls_ldm_got.offset = htab->root.sgot->size;
3821 htab->root.sgot->size += 8;
3823 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
3826 htab->tls_ldm_got.offset = -1;
3828 /* Allocate global sym .plt and .got entries, and space for global
3829 sym dynamic relocs. */
3830 elf_link_hash_traverse (& htab->root, allocate_dynrelocs, info);
3832 /* The check_relocs and adjust_dynamic_symbol entry points have
3833 determined the sizes of the various dynamic sections. Allocate
3838 for (s = dynobj->sections; s != NULL; s = s->next)
3842 if ((s->flags & SEC_LINKER_CREATED) == 0)
3845 /* It's OK to base decisions on the section name, because none
3846 of the dynobj section names depend upon the input files. */
3847 name = bfd_get_section_name (dynobj, s);
3849 if (strcmp (name, ".plt") == 0)
3851 /* Remember whether there is a PLT. */
3854 /* Correct for the number of res_N branches. */
3855 if (plt && !info->shared)
3857 htab->res_n_size = (s->size-28) / 3;
3858 s->size += htab->res_n_size;
3861 else if (CONST_STRNEQ (name, ".rela"))
3867 /* We use the reloc_count field as a counter if we need
3868 to copy relocs into the output file. */
3872 else if (CONST_STRNEQ (name, ".got"))
3874 else if (strcmp (name, ".dynbss") != 0)
3875 /* It's not one of our sections, so don't allocate space. */
3880 /* If we don't need this section, strip it from the
3881 output file. This is mostly to handle .rela.bss and
3882 .rela.plt. We must create both sections in
3883 create_dynamic_sections, because they must be created
3884 before the linker maps input sections to output
3885 sections. The linker does that before
3886 adjust_dynamic_symbol is called, and it is that
3887 function which decides whether anything needs to go
3888 into these sections. */
3889 s->flags |= SEC_EXCLUDE;
3893 if ((s->flags & SEC_HAS_CONTENTS) == 0)
3896 /* Allocate memory for the section contents. */
3897 /* FIXME: This should be a call to bfd_alloc not bfd_zalloc.
3898 Unused entries should be reclaimed before the section's contents
3899 are written out, but at the moment this does not happen. Thus in
3900 order to prevent writing out garbage, we initialize the section's
3901 contents to zero. */
3902 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
3903 if (s->contents == NULL)
3907 /* Adjust dynamic symbols that point to the plt to account for the
3908 now-known number of resN slots. */
3909 if (htab->res_n_size)
3910 elf_link_hash_traverse (& htab->root, adjust_dynrelocs, info);
3912 if (elf_hash_table (info)->dynamic_sections_created)
3914 /* Add some entries to the .dynamic section. We fill in the
3915 values later, in elf_nios2_finish_dynamic_sections, but we
3916 must add the entries now so that we get the correct size for
3917 the .dynamic section. The DT_DEBUG entry is filled in by the
3918 dynamic linker and used by the debugger. */
3919 #define add_dynamic_entry(TAG, VAL) \
3920 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
3922 if (!info->shared && !add_dynamic_entry (DT_DEBUG, 0))
3925 if (got && !add_dynamic_entry (DT_PLTGOT, 0))
3929 && (!add_dynamic_entry (DT_PLTRELSZ, 0)
3930 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
3931 || !add_dynamic_entry (DT_JMPREL, 0)))
3935 && (!add_dynamic_entry (DT_RELA, 0)
3936 || !add_dynamic_entry (DT_RELASZ, 0)
3937 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela))))
3940 if (!info->shared && !add_dynamic_entry (DT_NIOS2_GP, 0))
3943 if ((info->flags & DF_TEXTREL) != 0
3944 && !add_dynamic_entry (DT_TEXTREL, 0))
3947 #undef add_dynamic_entry
3952 /* Implement bfd_elf32_bfd_link_hash_table_create. */
3953 static struct bfd_link_hash_table *
3954 nios2_elf32_link_hash_table_create (bfd *abfd)
3956 struct elf32_nios2_link_hash_table *ret;
3957 bfd_size_type amt = sizeof (struct elf32_nios2_link_hash_table);
3959 ret = bfd_zmalloc (amt);
3963 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
3966 elf32_nios2_link_hash_entry),
3973 return &ret->root.root;
3976 /* Implement elf_backend_reloc_type_class. */
3977 static enum elf_reloc_type_class
3978 nios2_elf32_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
3979 const asection *rel_sec ATTRIBUTE_UNUSED,
3980 const Elf_Internal_Rela *rela)
3982 switch ((int) ELF32_R_TYPE (rela->r_info))
3984 case R_NIOS2_RELATIVE:
3985 return reloc_class_relative;
3986 case R_NIOS2_JUMP_SLOT:
3987 return reloc_class_plt;
3989 return reloc_class_copy;
3991 return reloc_class_normal;
3995 /* Return 1 if target is one of ours. */
3997 is_nios2_elf_target (const struct bfd_target *targ)
3999 return (targ == &bfd_elf32_littlenios2_vec
4000 || targ == &bfd_elf32_bignios2_vec);
4003 /* Implement elf_backend_add_symbol_hook.
4004 This hook is called by the linker when adding symbols from an object
4005 file. We use it to put .comm items in .sbss, and not .bss. */
4007 nios2_elf_add_symbol_hook (bfd *abfd,
4008 struct bfd_link_info *info,
4009 Elf_Internal_Sym *sym,
4010 const char **namep ATTRIBUTE_UNUSED,
4011 flagword *flagsp ATTRIBUTE_UNUSED,
4017 if (sym->st_shndx == SHN_COMMON
4018 && !info->relocatable
4019 && sym->st_size <= elf_gp_size (abfd)
4020 && is_nios2_elf_target (info->output_bfd->xvec))
4022 /* Common symbols less than or equal to -G nn bytes are automatically
4024 struct elf32_nios2_link_hash_table *htab;
4026 htab = elf32_nios2_hash_table (info);
4027 if (htab->sbss == NULL)
4029 flagword flags = SEC_IS_COMMON | SEC_LINKER_CREATED;
4031 dynobj = elf_hash_table (info)->dynobj;
4035 htab->sbss = bfd_make_section_anyway_with_flags (dynobj, ".sbss",
4037 if (htab->sbss == NULL)
4042 *valp = sym->st_size;
4048 /* Implement elf_backend_can_make_relative_eh_frame:
4049 Decide whether to attempt to turn absptr or lsda encodings in
4050 shared libraries into pcrel within the given input section. */
4052 nios2_elf32_can_make_relative_eh_frame (bfd *input_bfd ATTRIBUTE_UNUSED,
4053 struct bfd_link_info *info
4055 asection *eh_frame_section
4058 /* We can't use PC-relative encodings in the .eh_frame section. */
4062 /* Implement elf_backend_special_sections. */
4063 const struct bfd_elf_special_section elf32_nios2_special_sections[] =
4065 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS,
4066 SHF_ALLOC + SHF_WRITE + SHF_NIOS2_GPREL },
4067 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS,
4068 SHF_ALLOC + SHF_WRITE + SHF_NIOS2_GPREL },
4069 { NULL, 0, 0, 0, 0 }
4072 #define ELF_ARCH bfd_arch_nios2
4073 #define ELF_TARGET_ID NIOS2_ELF_DATA
4074 #define ELF_MACHINE_CODE EM_ALTERA_NIOS2
4076 /* The Nios II MMU uses a 4K page size. */
4078 #define ELF_MAXPAGESIZE 0x1000
4080 #define bfd_elf32_bfd_link_hash_table_create \
4081 nios2_elf32_link_hash_table_create
4083 /* Relocation table lookup macros. */
4085 #define bfd_elf32_bfd_reloc_type_lookup nios2_elf32_bfd_reloc_type_lookup
4086 #define bfd_elf32_bfd_reloc_name_lookup nios2_elf32_bfd_reloc_name_lookup
4088 /* JUMP_TABLE_LINK macros. */
4090 /* elf_info_to_howto (using RELA relocations). */
4092 #define elf_info_to_howto nios2_elf32_info_to_howto
4094 /* elf backend functions. */
4096 #define elf_backend_can_gc_sections 1
4097 #define elf_backend_can_refcount 1
4098 #define elf_backend_plt_readonly 1
4099 #define elf_backend_want_got_plt 1
4100 #define elf_backend_rela_normal 1
4102 #define elf_backend_relocate_section nios2_elf32_relocate_section
4103 #define elf_backend_section_flags nios2_elf32_section_flags
4104 #define elf_backend_fake_sections nios2_elf32_fake_sections
4105 #define elf_backend_check_relocs nios2_elf32_check_relocs
4107 #define elf_backend_gc_mark_hook nios2_elf32_gc_mark_hook
4108 #define elf_backend_gc_sweep_hook nios2_elf32_gc_sweep_hook
4109 #define elf_backend_create_dynamic_sections \
4110 nios2_elf32_create_dynamic_sections
4111 #define elf_backend_finish_dynamic_symbol nios2_elf32_finish_dynamic_symbol
4112 #define elf_backend_finish_dynamic_sections \
4113 nios2_elf32_finish_dynamic_sections
4114 #define elf_backend_adjust_dynamic_symbol nios2_elf32_adjust_dynamic_symbol
4115 #define elf_backend_reloc_type_class nios2_elf32_reloc_type_class
4116 #define elf_backend_size_dynamic_sections nios2_elf32_size_dynamic_sections
4117 #define elf_backend_add_symbol_hook nios2_elf_add_symbol_hook
4118 #define elf_backend_copy_indirect_symbol nios2_elf32_copy_indirect_symbol
4120 #define elf_backend_grok_prstatus nios2_grok_prstatus
4121 #define elf_backend_grok_psinfo nios2_grok_psinfo
4123 #undef elf_backend_can_make_relative_eh_frame
4124 #define elf_backend_can_make_relative_eh_frame \
4125 nios2_elf32_can_make_relative_eh_frame
4127 #define elf_backend_special_sections elf32_nios2_special_sections
4129 #define TARGET_LITTLE_SYM bfd_elf32_littlenios2_vec
4130 #define TARGET_LITTLE_NAME "elf32-littlenios2"
4131 #define TARGET_BIG_SYM bfd_elf32_bignios2_vec
4132 #define TARGET_BIG_NAME "elf32-bignios2"
4134 #define elf_backend_got_header_size 12
4136 #include "elf32-target.h"