Commit | Line | Data |
---|---|---|
d3366d23 | 1 | /* SPARC-specific support for 64-bit ELF |
3869b11f | 2 | Copyright (C) 1993, 1995, 1996, 1997 Free Software Foundation, Inc. |
789776d7 KR |
3 | |
4 | This file is part of BFD, the Binary File Descriptor library. | |
5 | ||
6 | This program is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 2 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | This program is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with this program; if not, write to the Free Software | |
d3366d23 DE |
18 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
19 | ||
789776d7 KR |
20 | #include "bfd.h" |
21 | #include "sysdep.h" | |
22 | #include "libbfd.h" | |
d3366d23 DE |
23 | #include "elf-bfd.h" |
24 | ||
70e818d4 DE |
25 | /* This is defined if one wants to build upward compatible binaries |
26 | with the original sparc64-elf toolchain. The support is kept in for | |
27 | now but is turned off by default. dje 970930 */ | |
28 | /*#define SPARC64_OLD_RELOCS*/ | |
29 | ||
3869b11f DE |
30 | #include "elf/sparc.h" |
31 | ||
70e818d4 DE |
32 | /* In case we're on a 32-bit machine, construct a 64-bit "-1" value. */ |
33 | #define MINUS_ONE (~ (bfd_vma) 0) | |
34 | ||
3869b11f DE |
35 | static reloc_howto_type *sparc64_elf_reloc_type_lookup |
36 | PARAMS ((bfd *, bfd_reloc_code_real_type)); | |
37 | static void sparc64_elf_info_to_howto | |
38 | PARAMS ((bfd *, arelent *, Elf_Internal_Rela *)); | |
39 | ||
65c2dd6e RH |
40 | static boolean sparc64_elf_check_relocs |
41 | PARAMS((bfd *, struct bfd_link_info *, asection *sec, | |
42 | const Elf_Internal_Rela *)); | |
43 | static boolean sparc64_elf_adjust_dynamic_symbol | |
44 | PARAMS((struct bfd_link_info *, struct elf_link_hash_entry *)); | |
45 | static boolean sparc64_elf_size_dynamic_sections | |
46 | PARAMS((bfd *, struct bfd_link_info *)); | |
47 | static boolean sparc64_elf_adjust_dynindx | |
48 | PARAMS((struct elf_link_hash_entry *, PTR)); | |
49 | ||
ccbab31d RH |
50 | static boolean sparc64_elf_merge_private_bfd_data |
51 | PARAMS ((bfd *, bfd *)); | |
52 | ||
d3366d23 DE |
53 | static boolean sparc64_elf_relocate_section |
54 | PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *, | |
55 | Elf_Internal_Rela *, Elf_Internal_Sym *, asection **)); | |
56 | static boolean sparc64_elf_object_p PARAMS ((bfd *)); | |
70e818d4 DE |
57 | \f |
58 | /* The relocation "howto" table. */ | |
789776d7 | 59 | |
70e818d4 DE |
60 | static bfd_reloc_status_type sparc_elf_notsup_reloc |
61 | PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **)); | |
3869b11f DE |
62 | static bfd_reloc_status_type sparc_elf_wdisp16_reloc |
63 | PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **)); | |
70e818d4 DE |
64 | static bfd_reloc_status_type sparc_elf_hix22_reloc |
65 | PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **)); | |
66 | static bfd_reloc_status_type sparc_elf_lox10_reloc | |
67 | PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **)); | |
3869b11f | 68 | |
65c2dd6e | 69 | static reloc_howto_type sparc64_elf_howto_table[] = |
789776d7 | 70 | { |
3869b11f DE |
71 | HOWTO(R_SPARC_NONE, 0,0, 0,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_NONE", false,0,0x00000000,true), |
72 | HOWTO(R_SPARC_8, 0,0, 8,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_8", false,0,0x000000ff,true), | |
73 | HOWTO(R_SPARC_16, 0,1,16,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_16", false,0,0x0000ffff,true), | |
74 | HOWTO(R_SPARC_32, 0,2,32,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_32", false,0,0xffffffff,true), | |
75 | HOWTO(R_SPARC_DISP8, 0,0, 8,true, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_DISP8", false,0,0x000000ff,true), | |
76 | HOWTO(R_SPARC_DISP16, 0,1,16,true, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_DISP16", false,0,0x0000ffff,true), | |
77 | HOWTO(R_SPARC_DISP32, 0,2,32,true, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_DISP32", false,0,0x00ffffff,true), | |
78 | HOWTO(R_SPARC_WDISP30, 2,2,30,true, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_WDISP30", false,0,0x3fffffff,true), | |
79 | HOWTO(R_SPARC_WDISP22, 2,2,22,true, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_WDISP22", false,0,0x003fffff,true), | |
80 | HOWTO(R_SPARC_HI22, 10,2,22,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_HI22", false,0,0x003fffff,true), | |
81 | HOWTO(R_SPARC_22, 0,2,22,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_22", false,0,0x003fffff,true), | |
82 | HOWTO(R_SPARC_13, 0,2,13,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_13", false,0,0x00001fff,true), | |
83 | HOWTO(R_SPARC_LO10, 0,2,10,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_LO10", false,0,0x000003ff,true), | |
84 | HOWTO(R_SPARC_GOT10, 0,2,10,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_GOT10", false,0,0x000003ff,true), | |
85 | HOWTO(R_SPARC_GOT13, 0,2,13,false,0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_GOT13", false,0,0x00001fff,true), | |
86 | HOWTO(R_SPARC_GOT22, 10,2,22,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_GOT22", false,0,0x003fffff,true), | |
87 | HOWTO(R_SPARC_PC10, 0,2,10,true, 0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_PC10", false,0,0x000003ff,true), | |
88 | HOWTO(R_SPARC_PC22, 10,2,22,true, 0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_PC22", false,0,0x003fffff,true), | |
89 | HOWTO(R_SPARC_WPLT30, 2,2,30,true, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_WPLT30", false,0,0x3fffffff,true), | |
90 | HOWTO(R_SPARC_COPY, 0,0,00,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_COPY", false,0,0x00000000,true), | |
91 | HOWTO(R_SPARC_GLOB_DAT, 0,0,00,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_GLOB_DAT",false,0,0x00000000,true), | |
92 | HOWTO(R_SPARC_JMP_SLOT, 0,0,00,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_JMP_SLOT",false,0,0x00000000,true), | |
93 | HOWTO(R_SPARC_RELATIVE, 0,0,00,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_RELATIVE",false,0,0x00000000,true), | |
94 | HOWTO(R_SPARC_UA32, 0,0,00,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_UA32", false,0,0x00000000,true), | |
70e818d4 DE |
95 | #ifndef SPARC64_OLD_RELOCS |
96 | /* These aren't implemented yet. */ | |
97 | HOWTO(R_SPARC_PLT32, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsup_reloc, "R_SPARC_PLT32", false,0,0x00000000,true), | |
98 | HOWTO(R_SPARC_HIPLT22, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsup_reloc, "R_SPARC_HIPLT22", false,0,0x00000000,true), | |
99 | HOWTO(R_SPARC_LOPLT10, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsup_reloc, "R_SPARC_LOPLT10", false,0,0x00000000,true), | |
100 | HOWTO(R_SPARC_PCPLT32, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsup_reloc, "R_SPARC_PCPLT32", false,0,0x00000000,true), | |
101 | HOWTO(R_SPARC_PCPLT22, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsup_reloc, "R_SPARC_PCPLT22", false,0,0x00000000,true), | |
102 | HOWTO(R_SPARC_PCPLT10, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsup_reloc, "R_SPARC_PCPLT10", false,0,0x00000000,true), | |
789776d7 | 103 | #endif |
3869b11f DE |
104 | HOWTO(R_SPARC_10, 0,2,10,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_10", false,0,0x000003ff,true), |
105 | HOWTO(R_SPARC_11, 0,2,11,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_11", false,0,0x000007ff,true), | |
70e818d4 DE |
106 | HOWTO(R_SPARC_64, 0,4,64,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_64", false,0,MINUS_ONE, true), |
107 | HOWTO(R_SPARC_OLO10, 0,2,13,false,0,complain_overflow_signed, sparc_elf_notsup_reloc, "R_SPARC_OLO10", false,0,0x00001fff,true), | |
108 | HOWTO(R_SPARC_HH22, 42,2,22,false,0,complain_overflow_unsigned,bfd_elf_generic_reloc, "R_SPARC_HH22", false,0,0x003fffff,true), | |
3869b11f DE |
109 | HOWTO(R_SPARC_HM10, 32,2,10,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_HM10", false,0,0x000003ff,true), |
110 | HOWTO(R_SPARC_LM22, 10,2,22,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_LM22", false,0,0x003fffff,true), | |
65c2dd6e RH |
111 | HOWTO(R_SPARC_PC_HH22, 42,2,22,true, 0,complain_overflow_unsigned,bfd_elf_generic_reloc, "R_SPARC_PC_HH22", false,0,0x003fffff,true), |
112 | HOWTO(R_SPARC_PC_HM10, 32,2,10,true, 0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_PC_HM10", false,0,0x000003ff,true), | |
113 | HOWTO(R_SPARC_PC_LM22, 10,2,22,true, 0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_PC_LM22", false,0,0x003fffff,true), | |
3869b11f | 114 | HOWTO(R_SPARC_WDISP16, 2,2,16,true, 0,complain_overflow_signed, sparc_elf_wdisp16_reloc,"R_SPARC_WDISP16", false,0,0x00000000,true), |
70e818d4 | 115 | HOWTO(R_SPARC_WDISP19, 2,2,19,true, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_WDISP19", false,0,0x0007ffff,true), |
3869b11f DE |
116 | HOWTO(R_SPARC_UNUSED_42, 0,0, 0,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_UNUSED_42",false,0,0x00000000,true), |
117 | HOWTO(R_SPARC_7, 0,2, 7,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_7", false,0,0x0000007f,true), | |
118 | HOWTO(R_SPARC_5, 0,2, 5,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_5", false,0,0x0000001f,true), | |
119 | HOWTO(R_SPARC_6, 0,2, 6,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_6", false,0,0x0000003f,true), | |
70e818d4 DE |
120 | HOWTO(R_SPARC_DISP64, 0,4,64,true, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_DISP64", false,0,MINUS_ONE, true), |
121 | HOWTO(R_SPARC_PLT64, 0,4,64,false,0,complain_overflow_bitfield,sparc_elf_notsup_reloc, "R_SPARC_PLT64", false,0,MINUS_ONE, false), | |
122 | HOWTO(R_SPARC_HIX22, 0,4, 0,false,0,complain_overflow_bitfield,sparc_elf_hix22_reloc, "R_SPARC_HIX22", false,0,MINUS_ONE, false), | |
123 | HOWTO(R_SPARC_LOX10, 0,4, 0,false,0,complain_overflow_dont, sparc_elf_lox10_reloc, "R_SPARC_LOX10", false,0,MINUS_ONE, false), | |
124 | HOWTO(R_SPARC_H44, 22,2,22,false,0,complain_overflow_unsigned,bfd_elf_generic_reloc, "R_SPARC_H44", false,0,0x003fffff,false), | |
125 | HOWTO(R_SPARC_M44, 12,2,10,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_M44", false,0,0x000003ff,false), | |
126 | HOWTO(R_SPARC_L44, 0,2,13,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_L44", false,0,0x00000fff,false), | |
d87ad0dc RH |
127 | HOWTO(R_SPARC_REGISTER, 0,4, 0,false,0,complain_overflow_bitfield,sparc_elf_notsup_reloc, "R_SPARC_REGISTER",false,0,MINUS_ONE, false), |
128 | HOWTO(R_SPARC_UA64, 0,4,64,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_UA64", false,0,MINUS_ONE, true), | |
129 | HOWTO(R_SPARC_UA16, 0,1,16,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_UA16", false,0,0x0000ffff,true) | |
3869b11f | 130 | }; |
789776d7 | 131 | |
3869b11f DE |
132 | struct elf_reloc_map { |
133 | unsigned char bfd_reloc_val; | |
134 | unsigned char elf_reloc_val; | |
135 | }; | |
789776d7 | 136 | |
3869b11f | 137 | static CONST struct elf_reloc_map sparc_reloc_map[] = |
789776d7 | 138 | { |
3869b11f DE |
139 | { BFD_RELOC_NONE, R_SPARC_NONE, }, |
140 | { BFD_RELOC_16, R_SPARC_16, }, | |
141 | { BFD_RELOC_8, R_SPARC_8 }, | |
142 | { BFD_RELOC_8_PCREL, R_SPARC_DISP8 }, | |
70e818d4 | 143 | { BFD_RELOC_CTOR, R_SPARC_64 }, |
3869b11f DE |
144 | { BFD_RELOC_32, R_SPARC_32 }, |
145 | { BFD_RELOC_32_PCREL, R_SPARC_DISP32 }, | |
146 | { BFD_RELOC_HI22, R_SPARC_HI22 }, | |
147 | { BFD_RELOC_LO10, R_SPARC_LO10, }, | |
148 | { BFD_RELOC_32_PCREL_S2, R_SPARC_WDISP30 }, | |
149 | { BFD_RELOC_SPARC22, R_SPARC_22 }, | |
150 | { BFD_RELOC_SPARC13, R_SPARC_13 }, | |
151 | { BFD_RELOC_SPARC_GOT10, R_SPARC_GOT10 }, | |
152 | { BFD_RELOC_SPARC_GOT13, R_SPARC_GOT13 }, | |
153 | { BFD_RELOC_SPARC_GOT22, R_SPARC_GOT22 }, | |
154 | { BFD_RELOC_SPARC_PC10, R_SPARC_PC10 }, | |
155 | { BFD_RELOC_SPARC_PC22, R_SPARC_PC22 }, | |
156 | { BFD_RELOC_SPARC_WPLT30, R_SPARC_WPLT30 }, | |
157 | { BFD_RELOC_SPARC_COPY, R_SPARC_COPY }, | |
158 | { BFD_RELOC_SPARC_GLOB_DAT, R_SPARC_GLOB_DAT }, | |
159 | { BFD_RELOC_SPARC_JMP_SLOT, R_SPARC_JMP_SLOT }, | |
160 | { BFD_RELOC_SPARC_RELATIVE, R_SPARC_RELATIVE }, | |
161 | { BFD_RELOC_SPARC_WDISP22, R_SPARC_WDISP22 }, | |
162 | /* ??? Doesn't dwarf use this? */ | |
163 | /*{ BFD_RELOC_SPARC_UA32, R_SPARC_UA32 }, not used?? */ | |
164 | {BFD_RELOC_SPARC_10, R_SPARC_10}, | |
165 | {BFD_RELOC_SPARC_11, R_SPARC_11}, | |
166 | {BFD_RELOC_SPARC_64, R_SPARC_64}, | |
167 | {BFD_RELOC_SPARC_OLO10, R_SPARC_OLO10}, | |
168 | {BFD_RELOC_SPARC_HH22, R_SPARC_HH22}, | |
169 | {BFD_RELOC_SPARC_HM10, R_SPARC_HM10}, | |
170 | {BFD_RELOC_SPARC_LM22, R_SPARC_LM22}, | |
171 | {BFD_RELOC_SPARC_PC_HH22, R_SPARC_PC_HH22}, | |
172 | {BFD_RELOC_SPARC_PC_HM10, R_SPARC_PC_HM10}, | |
173 | {BFD_RELOC_SPARC_PC_LM22, R_SPARC_PC_LM22}, | |
174 | {BFD_RELOC_SPARC_WDISP16, R_SPARC_WDISP16}, | |
175 | {BFD_RELOC_SPARC_WDISP19, R_SPARC_WDISP19}, | |
176 | {BFD_RELOC_SPARC_7, R_SPARC_7}, | |
177 | {BFD_RELOC_SPARC_5, R_SPARC_5}, | |
178 | {BFD_RELOC_SPARC_6, R_SPARC_6}, | |
70e818d4 DE |
179 | {BFD_RELOC_SPARC_DISP64, R_SPARC_DISP64}, |
180 | {BFD_RELOC_SPARC_PLT64, R_SPARC_PLT64}, | |
181 | {BFD_RELOC_SPARC_HIX22, R_SPARC_HIX22}, | |
182 | {BFD_RELOC_SPARC_LOX10, R_SPARC_LOX10}, | |
183 | {BFD_RELOC_SPARC_H44, R_SPARC_H44}, | |
184 | {BFD_RELOC_SPARC_M44, R_SPARC_M44}, | |
185 | {BFD_RELOC_SPARC_L44, R_SPARC_L44}, | |
186 | {BFD_RELOC_SPARC_REGISTER, R_SPARC_REGISTER} | |
789776d7 KR |
187 | }; |
188 | ||
3869b11f DE |
189 | static reloc_howto_type * |
190 | sparc64_elf_reloc_type_lookup (abfd, code) | |
191 | bfd *abfd; | |
192 | bfd_reloc_code_real_type code; | |
193 | { | |
194 | unsigned int i; | |
195 | for (i = 0; i < sizeof (sparc_reloc_map) / sizeof (struct elf_reloc_map); i++) | |
196 | { | |
197 | if (sparc_reloc_map[i].bfd_reloc_val == code) | |
198 | return &sparc64_elf_howto_table[(int) sparc_reloc_map[i].elf_reloc_val]; | |
199 | } | |
200 | return 0; | |
201 | } | |
202 | ||
203 | static void | |
204 | sparc64_elf_info_to_howto (abfd, cache_ptr, dst) | |
205 | bfd *abfd; | |
206 | arelent *cache_ptr; | |
207 | Elf64_Internal_Rela *dst; | |
208 | { | |
209 | BFD_ASSERT (ELF64_R_TYPE (dst->r_info) < (unsigned int) R_SPARC_max); | |
210 | cache_ptr->howto = &sparc64_elf_howto_table[ELF64_R_TYPE (dst->r_info)]; | |
211 | } | |
212 | \f | |
70e818d4 DE |
213 | /* Utility for performing the standard initial work of an instruction |
214 | relocation. | |
215 | *PRELOCATION will contain the relocated item. | |
216 | *PINSN will contain the instruction from the input stream. | |
217 | If the result is `bfd_reloc_other' the caller can continue with | |
218 | performing the relocation. Otherwise it must stop and return the | |
219 | value to its caller. */ | |
d3366d23 DE |
220 | |
221 | static bfd_reloc_status_type | |
70e818d4 DE |
222 | init_insn_reloc (abfd, |
223 | reloc_entry, | |
224 | symbol, | |
225 | data, | |
226 | input_section, | |
227 | output_bfd, | |
228 | prelocation, | |
229 | pinsn) | |
d3366d23 DE |
230 | bfd *abfd; |
231 | arelent *reloc_entry; | |
232 | asymbol *symbol; | |
233 | PTR data; | |
234 | asection *input_section; | |
235 | bfd *output_bfd; | |
70e818d4 DE |
236 | bfd_vma *prelocation; |
237 | bfd_vma *pinsn; | |
d3366d23 DE |
238 | { |
239 | bfd_vma relocation; | |
70e818d4 | 240 | reloc_howto_type *howto = reloc_entry->howto; |
d3366d23 DE |
241 | |
242 | if (output_bfd != (bfd *) NULL | |
243 | && (symbol->flags & BSF_SECTION_SYM) == 0 | |
70e818d4 | 244 | && (! howto->partial_inplace |
d3366d23 DE |
245 | || reloc_entry->addend == 0)) |
246 | { | |
247 | reloc_entry->address += input_section->output_offset; | |
248 | return bfd_reloc_ok; | |
249 | } | |
250 | ||
70e818d4 | 251 | /* This works because partial_inplace == false. */ |
d3366d23 DE |
252 | if (output_bfd != NULL) |
253 | return bfd_reloc_continue; | |
254 | ||
255 | if (reloc_entry->address > input_section->_cooked_size) | |
256 | return bfd_reloc_outofrange; | |
257 | ||
258 | relocation = (symbol->value | |
259 | + symbol->section->output_section->vma | |
260 | + symbol->section->output_offset); | |
261 | relocation += reloc_entry->addend; | |
70e818d4 DE |
262 | if (howto->pc_relative) |
263 | { | |
264 | relocation -= (input_section->output_section->vma | |
265 | + input_section->output_offset); | |
266 | relocation -= reloc_entry->address; | |
267 | } | |
268 | ||
269 | *prelocation = relocation; | |
270 | *pinsn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address); | |
271 | return bfd_reloc_other; | |
272 | } | |
d3366d23 | 273 | |
70e818d4 DE |
274 | /* For unsupported relocs. */ |
275 | ||
276 | static bfd_reloc_status_type | |
277 | sparc_elf_notsup_reloc (abfd, | |
278 | reloc_entry, | |
279 | symbol, | |
280 | data, | |
281 | input_section, | |
282 | output_bfd, | |
283 | error_message) | |
284 | bfd *abfd; | |
285 | arelent *reloc_entry; | |
286 | asymbol *symbol; | |
287 | PTR data; | |
288 | asection *input_section; | |
289 | bfd *output_bfd; | |
290 | char **error_message; | |
291 | { | |
292 | return bfd_reloc_notsupported; | |
293 | } | |
294 | ||
295 | /* Handle the WDISP16 reloc. */ | |
296 | ||
297 | static bfd_reloc_status_type | |
65c2dd6e RH |
298 | sparc_elf_wdisp16_reloc (abfd, reloc_entry, symbol, data, input_section, |
299 | output_bfd, error_message) | |
70e818d4 DE |
300 | bfd *abfd; |
301 | arelent *reloc_entry; | |
302 | asymbol *symbol; | |
303 | PTR data; | |
304 | asection *input_section; | |
305 | bfd *output_bfd; | |
306 | char **error_message; | |
307 | { | |
308 | bfd_vma relocation; | |
309 | bfd_vma insn; | |
310 | bfd_reloc_status_type status; | |
311 | ||
312 | status = init_insn_reloc (abfd, reloc_entry, symbol, data, | |
313 | input_section, output_bfd, &relocation, &insn); | |
314 | if (status != bfd_reloc_other) | |
315 | return status; | |
316 | ||
317 | insn = (insn & ~0x303fff) | ((((relocation >> 2) & 0xc000) << 6) | |
318 | | ((relocation >> 2) & 0x3fff)); | |
319 | bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address); | |
d3366d23 DE |
320 | |
321 | if ((bfd_signed_vma) relocation < - 0x40000 | |
322 | || (bfd_signed_vma) relocation > 0x3ffff) | |
323 | return bfd_reloc_overflow; | |
324 | else | |
325 | return bfd_reloc_ok; | |
326 | } | |
70e818d4 DE |
327 | |
328 | /* Handle the HIX22 reloc. */ | |
329 | ||
330 | static bfd_reloc_status_type | |
331 | sparc_elf_hix22_reloc (abfd, | |
332 | reloc_entry, | |
333 | symbol, | |
334 | data, | |
335 | input_section, | |
336 | output_bfd, | |
337 | error_message) | |
338 | bfd *abfd; | |
339 | arelent *reloc_entry; | |
340 | asymbol *symbol; | |
341 | PTR data; | |
342 | asection *input_section; | |
343 | bfd *output_bfd; | |
344 | char **error_message; | |
345 | { | |
346 | bfd_vma relocation; | |
347 | bfd_vma insn; | |
348 | bfd_reloc_status_type status; | |
349 | ||
350 | status = init_insn_reloc (abfd, reloc_entry, symbol, data, | |
351 | input_section, output_bfd, &relocation, &insn); | |
352 | if (status != bfd_reloc_other) | |
353 | return status; | |
354 | ||
355 | relocation ^= MINUS_ONE; | |
356 | insn = (insn & ~0x3fffff) | ((relocation >> 10) & 0x3fffff); | |
357 | bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address); | |
358 | ||
359 | if ((relocation & ~ (bfd_vma) 0xffffffff) != 0) | |
360 | return bfd_reloc_overflow; | |
361 | else | |
362 | return bfd_reloc_ok; | |
363 | } | |
364 | ||
365 | /* Handle the LOX10 reloc. */ | |
366 | ||
367 | static bfd_reloc_status_type | |
368 | sparc_elf_lox10_reloc (abfd, | |
369 | reloc_entry, | |
370 | symbol, | |
371 | data, | |
372 | input_section, | |
373 | output_bfd, | |
374 | error_message) | |
375 | bfd *abfd; | |
376 | arelent *reloc_entry; | |
377 | asymbol *symbol; | |
378 | PTR data; | |
379 | asection *input_section; | |
380 | bfd *output_bfd; | |
381 | char **error_message; | |
382 | { | |
383 | bfd_vma relocation; | |
384 | bfd_vma insn; | |
385 | bfd_reloc_status_type status; | |
386 | ||
387 | status = init_insn_reloc (abfd, reloc_entry, symbol, data, | |
388 | input_section, output_bfd, &relocation, &insn); | |
389 | if (status != bfd_reloc_other) | |
390 | return status; | |
391 | ||
392 | insn = (insn & ~0x1fff) | 0x1c00 | (relocation & 0x3ff); | |
393 | bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address); | |
394 | ||
395 | return bfd_reloc_ok; | |
396 | } | |
65c2dd6e RH |
397 | \f |
398 | /* PLT/GOT stuff */ | |
399 | ||
400 | /* FIXME: Do Delta Doc PLT entries. */ | |
401 | ||
402 | /* We use five different formats, chosing an optimal form for the | |
403 | code model used by the application/library. All of which provide | |
404 | the exact same interface to ld.so. | |
405 | FIXME, well, only three actually used right now -- fix up medlow | |
406 | and fullany later. */ | |
407 | ||
408 | /* Both the headers and the entries are icache aligned. */ | |
409 | #define PLT_HEADER_SIZE 32 | |
410 | #define PLT_ENTRY_SIZE 32 | |
411 | #define GOT_RESERVED_ENTRIES 3 | |
412 | ||
413 | #define ELF_DYNAMIC_INTERPRETER "/usr/lib/sparcv9/ld.so.1" | |
414 | ||
415 | /* Values for fixups: | |
416 | 0 == got0 | |
417 | 1 == got0 - plt0 | |
418 | 2 == gotN | |
419 | 3 == gotN - pltN | |
420 | 4 == relN | |
421 | 5 == gotN@got | |
422 | */ | |
423 | ||
424 | struct plt_template | |
425 | { | |
426 | unsigned int insns[8]; | |
427 | struct | |
428 | { | |
429 | unsigned char reloc; | |
430 | unsigned char value; | |
431 | } fixup[8]; | |
432 | }; | |
433 | ||
434 | static const struct plt_template plt_medlow_header = | |
435 | { | |
436 | { | |
437 | 0x07000000, /* sethi %hi(got0),%g3 */ | |
438 | 0x8610E000, /* or %g3,%lo(got0),%g3 */ | |
439 | 0xC258E008, /* ldx [%g3+8],%g1 */ | |
440 | 0x81C04000, /* jmp %g1 */ | |
441 | 0xC258E010, /* ldx [%g3+16],%g1 */ | |
442 | 0x01000000, /* nop */ | |
443 | 0x01000000, /* nop */ | |
444 | 0x01000000 /* nop */ | |
445 | }, | |
446 | { | |
447 | { R_SPARC_HI22, 0 }, | |
448 | { R_SPARC_LO10, 0 }, | |
449 | /* Rest null */ | |
450 | } | |
451 | }; | |
452 | ||
453 | static const struct plt_template plt_medlow_entry = | |
454 | { | |
455 | { | |
456 | 0x03000000, /* sethi %hi(gotN),%g1 */ | |
457 | 0x05000000, /* sethi %hi(relN),%g2 */ | |
458 | 0xC2586000, /* ldx [%g1+%lo(gotN)],%g1 */ | |
459 | 0x8410A000, /* or %g2,%lo(relN),%g2 */ | |
460 | 0x81C04000, /* jmp %g1 */ | |
461 | 0x01000000, /* nop */ | |
462 | 0x01000000, /* nop */ | |
463 | 0x01000000 /* nop */ | |
464 | }, | |
465 | { | |
466 | { R_SPARC_HI22, 2 }, | |
467 | { R_SPARC_HI22, 4 }, | |
468 | { R_SPARC_LO10, 2 }, | |
469 | { R_SPARC_LO10, 2 } | |
470 | } | |
471 | }; | |
472 | ||
473 | static const struct plt_template plt_medany_header = | |
474 | { | |
475 | { | |
476 | 0x07000000, /* sethi %hi(got0),%g3 */ | |
477 | 0x8610E000, /* or %g3,%lo(got0),%g3 */ | |
478 | 0x8600C004, /* add %g3,%g4,%g3 */ | |
479 | 0xC258E008, /* ldx [%g3+8],%g1 */ | |
480 | 0x81C04000, /* jmp %g1 */ | |
481 | 0xC258E010, /* ldx [%g3+16],%g1 */ | |
482 | 0x01000000, /* nop */ | |
483 | 0x01000000, /* nop */ | |
484 | }, | |
485 | { | |
486 | { R_SPARC_HI22, 0 }, | |
487 | { R_SPARC_LO10, 0 } | |
488 | } | |
489 | }; | |
490 | ||
491 | static const struct plt_template plt_medany_entry = | |
492 | { | |
493 | { | |
494 | 0x03000000, /* sethi %hi(gotN),%g1 */ | |
495 | 0x82106000, /* or %g1,%lo(gotN),%g1 */ | |
496 | 0xC2584004, /* ldx [%g1+%g4],%g1 */ | |
497 | 0x05000000, /* sethi %hi(relN),%g2 */ | |
498 | 0x81C04000, /* jmp %g1 */ | |
499 | 0x8410A000, /* or %g2,%lo(relN),%g2 */ | |
500 | 0x01000000, /* nop */ | |
501 | 0x01000000 /* nop */ | |
502 | }, | |
503 | { | |
504 | { R_SPARC_HI22, 2 }, | |
505 | { R_SPARC_LO10, 2 }, | |
506 | { R_SPARC_NONE, 0 }, | |
507 | { R_SPARC_HI22, 4 }, | |
508 | { R_SPARC_NONE, 0 }, | |
509 | { R_SPARC_LO10, 4 } | |
510 | } | |
511 | }; | |
512 | ||
513 | static const struct plt_template plt_fullany_header = | |
514 | { | |
515 | { | |
516 | 0x07000000, /* sethi %hi(got0-plt0),%g3 */ | |
517 | 0x8610E000, /* or %g3,%lo(got0-plt0),%g3 */ | |
518 | 0x86004003, /* add %g1,%g3,%g3 */ | |
519 | 0xC258C008, /* ldx [%g3+8],%g1 */ | |
520 | 0x81C04000, /* jmp %g1 */ | |
521 | 0xC258E010, /* ldx [%g3+16],%g1 */ | |
522 | 0x01000000, /* nop */ | |
523 | 0x01000000 /* nop */ | |
524 | }, | |
525 | { | |
526 | { R_SPARC_HI22, 1 }, | |
527 | { R_SPARC_LO10, 1 } | |
528 | } | |
529 | }; | |
530 | ||
531 | static const struct plt_template plt_fullany_entry = | |
532 | { | |
533 | { | |
534 | 0x83414000, /* rd %pc,%g1 */ | |
535 | 0x07000000, /* sethi %hi(gotN-pltN),%g3 */ | |
536 | 0x05000000, /* sethi %hi(relN),%g2 */ | |
537 | 0x8610E000, /* or %g3,%lo(gotN-pltN),%g3 */ | |
538 | 0xC2584003, /* ldx [%g1+%g3],%g1 */ | |
539 | 0x81C04000, /* jmp %g1 */ | |
540 | 0x8410A000, /* or %g2,%lo(relN),%g2 */ | |
541 | 0x01000000 /* nop */ | |
542 | }, | |
543 | { | |
544 | { R_SPARC_NONE, 0 }, | |
545 | { R_SPARC_HI22, 3 }, | |
546 | { R_SPARC_HI22, 4 }, | |
547 | { R_SPARC_LO10, 3 }, | |
548 | { R_SPARC_NONE, 0 }, | |
549 | { R_SPARC_NONE, 0 }, | |
550 | { R_SPARC_LO10, 4 } | |
551 | } | |
552 | }; | |
553 | ||
554 | static const struct plt_template plt_pic_header = | |
555 | { | |
556 | { | |
557 | 0xC25DE008, /* ldx [%l7+8],%g1 */ | |
558 | 0x81C04000, /* jmp %g1 */ | |
559 | 0xC25DE010, /* ldx [%l7+16],%g1 */ | |
560 | 0x01000000, /* nop */ | |
561 | 0x01000000, /* nop */ | |
562 | 0x01000000, /* nop */ | |
563 | 0x01000000, /* nop */ | |
564 | 0x01000000 /* nop */ | |
565 | }, | |
566 | { } | |
567 | }; | |
568 | ||
569 | static const struct plt_template plt_pic_small_entry = | |
570 | { | |
571 | { | |
572 | 0xC25DE000, /* ldx [%l7+gotN@got],%g1 */ | |
573 | 0x05000000, /* sethi %hi(relN),%g2 */ | |
574 | 0x81C04017, /* jmp %g1+%l7 */ | |
575 | 0x8410A000, /* or %g2,%lo(relN),%g2 */ | |
576 | 0x01000000, /* nop */ | |
577 | 0x01000000, /* nop */ | |
578 | 0x01000000, /* nop */ | |
579 | 0x01000000 /* nop */ | |
580 | }, | |
581 | { | |
582 | { R_SPARC_13, 5 }, /* R_SPARC_GOT13 */ | |
583 | { R_SPARC_HI22, 4 }, | |
584 | { R_SPARC_NONE, 0 }, | |
585 | { R_SPARC_LO10, 4 } | |
586 | } | |
587 | }; | |
588 | ||
589 | static const struct plt_template plt_pic_large_entry = | |
590 | { | |
591 | { | |
592 | 0x03000000, /* sethi %hi(gotN@got),%g1 */ | |
593 | 0x82106000, /* or %g1,%lo(gotN@got),%g1 */ | |
594 | 0xC2584017, /* ldx [%g1+%l7],%g1 */ | |
595 | 0x05000000, /* sethi %hi(relN),%g2 */ | |
596 | 0x81C04000, /* jmp %g1 */ | |
597 | 0x8410A000, /* or %g2,%lo(relN),%g2 */ | |
598 | 0x01000000, /* nop */ | |
599 | 0x01000000 /* nop */ | |
600 | }, | |
601 | { | |
602 | { R_SPARC_HI22, 5 }, /* R_SPARC_GOT22 */ | |
603 | { R_SPARC_LO10, 5 }, /* R_SPARC_GOT10 */ | |
604 | { R_SPARC_NONE, 0 }, | |
605 | { R_SPARC_HI22, 4 }, | |
606 | { R_SPARC_NONE, 0 }, | |
607 | { R_SPARC_LO10, 4 } | |
608 | } | |
609 | }; | |
610 | ||
611 | /* Build a plt entry given a template and values. */ | |
612 | ||
613 | static boolean sparc64_elf_build_plt_entry(output_bfd, loc, tmpl, values) | |
614 | bfd *output_bfd; | |
615 | unsigned char *loc; | |
616 | const struct plt_template *tmpl; | |
617 | bfd_vma values[]; | |
618 | { | |
619 | int i; | |
620 | for (i = 0; i < 8; ++i) | |
621 | { | |
622 | unsigned int insn = tmpl->insns[i]; | |
623 | bfd_vma value = values[tmpl->fixup[i].value]; | |
624 | ||
625 | #if 1 | |
626 | switch (tmpl->fixup[i].reloc) | |
627 | { | |
628 | case R_SPARC_NONE: | |
629 | break; | |
630 | case R_SPARC_HI22: | |
631 | insn |= (value >> 10) & 0x3fffff; | |
632 | break; | |
633 | case R_SPARC_LO10: | |
634 | insn |= value & 0x3ff; | |
635 | break; | |
636 | case R_SPARC_13: | |
637 | if ((bfd_signed_vma)value > 0xfff | |
638 | || (bfd_signed_vma)value < -0x1000) | |
639 | return false; | |
640 | insn |= value & 0x1fff; | |
641 | break; | |
642 | default: | |
643 | abort(); | |
644 | } | |
645 | #else | |
646 | /* FIXME -- possibly use _bfd_final_link_relocate? */ | |
647 | ||
648 | howto = sparc64_elf_howto_table + tmpl->fixups[i].reloc; | |
649 | r = _bfd_final_link_relocate (howto, input_bfd, input_section, | |
650 | &insn, 0, value, 0); | |
651 | if (r != bfd_reloc_ok) | |
652 | return false; | |
653 | #endif | |
654 | ||
655 | bfd_put_32(output_bfd, insn, loc); | |
656 | loc += 4; | |
657 | } | |
658 | ||
659 | return true; | |
660 | } | |
661 | \f | |
662 | /* Look through the relocs for a section during the first phase, and | |
663 | allocate space in the global offset table or procedure linkage | |
664 | table. */ | |
665 | ||
666 | static boolean | |
667 | sparc64_elf_check_relocs (abfd, info, sec, relocs) | |
668 | bfd *abfd; | |
669 | struct bfd_link_info *info; | |
670 | asection *sec; | |
671 | const Elf_Internal_Rela *relocs; | |
672 | { | |
673 | bfd *dynobj; | |
674 | Elf_Internal_Shdr *symtab_hdr; | |
675 | struct elf_link_hash_entry **sym_hashes; | |
676 | bfd_vma *local_got_offsets; | |
677 | const Elf_Internal_Rela *rel; | |
678 | const Elf_Internal_Rela *rel_end; | |
679 | asection *sgot; | |
680 | asection *srelgot; | |
681 | asection *sreloc; | |
682 | ||
683 | if (info->relocateable || (sec->flags & SEC_DEBUGGING)) | |
684 | return true; | |
685 | ||
686 | dynobj = elf_hash_table (info)->dynobj; | |
687 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
688 | sym_hashes = elf_sym_hashes (abfd); | |
689 | local_got_offsets = elf_local_got_offsets (abfd); | |
690 | ||
691 | sgot = NULL; | |
692 | srelgot = NULL; | |
693 | sreloc = NULL; | |
694 | ||
695 | rel_end = relocs + sec->reloc_count; | |
696 | for (rel = relocs; rel < rel_end; rel++) | |
697 | { | |
698 | unsigned long r_symndx; | |
699 | struct elf_link_hash_entry *h; | |
700 | ||
701 | r_symndx = ELF64_R_SYM (rel->r_info); | |
702 | if (r_symndx < symtab_hdr->sh_info) | |
703 | h = NULL; | |
704 | else | |
705 | h = sym_hashes[r_symndx - symtab_hdr->sh_info]; | |
706 | ||
707 | switch (ELF64_R_TYPE (rel->r_info)) | |
708 | { | |
709 | case R_SPARC_GOT10: | |
710 | case R_SPARC_GOT13: | |
711 | case R_SPARC_GOT22: | |
712 | /* This symbol requires a global offset table entry. */ | |
713 | ||
714 | if (dynobj == NULL) | |
715 | { | |
716 | /* Create the .got section. */ | |
717 | elf_hash_table (info)->dynobj = dynobj = abfd; | |
718 | if (! _bfd_elf_create_got_section (dynobj, info)) | |
719 | return false; | |
720 | } | |
721 | ||
722 | if (sgot == NULL) | |
723 | { | |
724 | sgot = bfd_get_section_by_name (dynobj, ".got"); | |
725 | BFD_ASSERT (sgot != NULL); | |
726 | } | |
727 | ||
728 | if (srelgot == NULL && (h != NULL || info->shared)) | |
729 | { | |
730 | srelgot = bfd_get_section_by_name (dynobj, ".rela.got"); | |
731 | if (srelgot == NULL) | |
732 | { | |
733 | srelgot = bfd_make_section (dynobj, ".rela.got"); | |
734 | if (srelgot == NULL | |
735 | || ! bfd_set_section_flags (dynobj, srelgot, | |
736 | (SEC_ALLOC | |
737 | | SEC_LOAD | |
738 | | SEC_HAS_CONTENTS | |
739 | | SEC_IN_MEMORY | |
740 | | SEC_LINKER_CREATED | |
741 | | SEC_READONLY)) | |
742 | || ! bfd_set_section_alignment (dynobj, srelgot, 3)) | |
743 | return false; | |
744 | } | |
745 | } | |
746 | ||
747 | if (h != NULL) | |
748 | { | |
749 | if (h->got_offset != (bfd_vma) -1) | |
750 | { | |
751 | /* We have already allocated space in the .got. */ | |
752 | break; | |
753 | } | |
754 | h->got_offset = sgot->_raw_size; | |
755 | ||
756 | /* Make sure this symbol is output as a dynamic symbol. */ | |
757 | if (h->dynindx == -1) | |
758 | { | |
759 | if (! bfd_elf64_link_record_dynamic_symbol (info, h)) | |
760 | return false; | |
761 | } | |
762 | ||
763 | srelgot->_raw_size += sizeof (Elf64_External_Rela); | |
764 | } | |
765 | else | |
766 | { | |
767 | /* This is a global offset table entry for a local | |
768 | symbol. */ | |
769 | if (local_got_offsets == NULL) | |
770 | { | |
771 | size_t size; | |
772 | register unsigned int i; | |
773 | ||
774 | size = symtab_hdr->sh_info * sizeof (bfd_vma); | |
775 | local_got_offsets = (bfd_vma *) bfd_alloc (abfd, size); | |
776 | if (local_got_offsets == NULL) | |
777 | return false; | |
778 | elf_local_got_offsets (abfd) = local_got_offsets; | |
779 | for (i = 0; i < symtab_hdr->sh_info; i++) | |
780 | local_got_offsets[i] = (bfd_vma) -1; | |
781 | } | |
782 | if (local_got_offsets[r_symndx] != (bfd_vma) -1) | |
783 | { | |
784 | /* We have already allocated space in the .got. */ | |
785 | break; | |
786 | } | |
787 | local_got_offsets[r_symndx] = sgot->_raw_size; | |
788 | ||
789 | if (info->shared) | |
790 | { | |
791 | /* If we are generating a shared object, we need to | |
792 | output a R_SPARC_RELATIVE reloc so that the | |
793 | dynamic linker can adjust this GOT entry. */ | |
794 | srelgot->_raw_size += sizeof (Elf64_External_Rela); | |
795 | } | |
796 | } | |
797 | ||
798 | sgot->_raw_size += 8; | |
799 | ||
800 | #if 0 | |
801 | /* Doesn't work for 64-bit -fPIC, since sethi/or builds | |
802 | unsigned numbers. If we permit ourselves to modify | |
803 | code so we get sethi/xor, this could work. | |
804 | Question: do we consider conditionally re-enabling | |
805 | this for -fpic, once we know about object code models? */ | |
806 | /* If the .got section is more than 0x1000 bytes, we add | |
807 | 0x1000 to the value of _GLOBAL_OFFSET_TABLE_, so that 13 | |
808 | bit relocations have a greater chance of working. */ | |
809 | if (sgot->_raw_size >= 0x1000 | |
810 | && elf_hash_table (info)->hgot->root.u.def.value == 0) | |
811 | elf_hash_table (info)->hgot->root.u.def.value = 0x1000; | |
812 | #endif | |
813 | ||
814 | break; | |
815 | ||
816 | case R_SPARC_WPLT30: | |
817 | case R_SPARC_PLT32: | |
818 | case R_SPARC_HIPLT22: | |
819 | case R_SPARC_LOPLT10: | |
820 | case R_SPARC_PCPLT32: | |
821 | case R_SPARC_PCPLT22: | |
822 | case R_SPARC_PCPLT10: | |
823 | case R_SPARC_PLT64: | |
824 | /* This symbol requires a procedure linkage table entry. We | |
825 | actually build the entry in adjust_dynamic_symbol, | |
826 | because this might be a case of linking PIC code without | |
827 | linking in any dynamic objects, in which case we don't | |
828 | need to generate a procedure linkage table after all. */ | |
829 | ||
830 | if (h == NULL) | |
831 | { | |
832 | /* It does not make sense to have a procedure linkage | |
833 | table entry for a local symbol. */ | |
834 | bfd_set_error (bfd_error_bad_value); | |
835 | return false; | |
836 | } | |
837 | ||
838 | /* Make sure this symbol is output as a dynamic symbol. */ | |
839 | if (h->dynindx == -1) | |
840 | { | |
841 | if (! bfd_elf64_link_record_dynamic_symbol (info, h)) | |
842 | return false; | |
843 | } | |
844 | ||
845 | h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT; | |
846 | break; | |
847 | ||
848 | case R_SPARC_PC10: | |
849 | case R_SPARC_PC22: | |
850 | case R_SPARC_PC_HH22: | |
851 | case R_SPARC_PC_HM10: | |
852 | case R_SPARC_PC_LM22: | |
853 | if (h != NULL | |
854 | && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0) | |
855 | break; | |
856 | /* Fall through. */ | |
857 | case R_SPARC_DISP8: | |
858 | case R_SPARC_DISP16: | |
859 | case R_SPARC_DISP32: | |
860 | case R_SPARC_DISP64: | |
861 | case R_SPARC_WDISP30: | |
862 | case R_SPARC_WDISP22: | |
863 | case R_SPARC_WDISP19: | |
864 | case R_SPARC_WDISP16: | |
865 | if (h == NULL) | |
866 | break; | |
867 | /* Fall through. */ | |
868 | case R_SPARC_8: | |
869 | case R_SPARC_16: | |
870 | case R_SPARC_32: | |
871 | case R_SPARC_HI22: | |
872 | case R_SPARC_22: | |
873 | case R_SPARC_13: | |
874 | case R_SPARC_LO10: | |
875 | case R_SPARC_UA32: | |
876 | case R_SPARC_10: | |
877 | case R_SPARC_11: | |
878 | case R_SPARC_64: | |
879 | case R_SPARC_OLO10: | |
880 | case R_SPARC_HH22: | |
881 | case R_SPARC_HM10: | |
882 | case R_SPARC_LM22: | |
883 | case R_SPARC_7: | |
884 | case R_SPARC_5: | |
885 | case R_SPARC_6: | |
886 | case R_SPARC_HIX22: | |
887 | case R_SPARC_LOX10: | |
888 | case R_SPARC_H44: | |
889 | case R_SPARC_M44: | |
890 | case R_SPARC_L44: | |
d87ad0dc RH |
891 | case R_SPARC_UA64: |
892 | case R_SPARC_UA16: | |
65c2dd6e RH |
893 | /* When creating a shared object, we must copy these relocs |
894 | into the output file. We create a reloc section in | |
895 | dynobj and make room for the reloc. | |
896 | ||
897 | But don't do this for debugging sections -- this shows up | |
898 | with DWARF2 -- first because they are not loaded, and | |
899 | second because DWARF sez the debug info is not to be | |
900 | biased by the load address. */ | |
901 | if (info->shared && !(sec->flags & SEC_DEBUGGING)) | |
902 | { | |
903 | if (sreloc == NULL) | |
904 | { | |
905 | const char *name; | |
906 | ||
907 | name = (bfd_elf_string_from_elf_section | |
908 | (abfd, | |
909 | elf_elfheader (abfd)->e_shstrndx, | |
910 | elf_section_data (sec)->rel_hdr.sh_name)); | |
911 | if (name == NULL) | |
912 | return false; | |
913 | ||
914 | BFD_ASSERT (strncmp (name, ".rela", 5) == 0 | |
915 | && strcmp (bfd_get_section_name (abfd, sec), | |
916 | name + 5) == 0); | |
917 | ||
918 | sreloc = bfd_get_section_by_name (dynobj, name); | |
919 | if (sreloc == NULL) | |
920 | { | |
921 | flagword flags; | |
922 | ||
923 | sreloc = bfd_make_section (dynobj, name); | |
924 | flags = (SEC_HAS_CONTENTS | SEC_READONLY | |
925 | | SEC_IN_MEMORY | SEC_LINKER_CREATED); | |
926 | if ((sec->flags & SEC_ALLOC) != 0) | |
927 | flags |= SEC_ALLOC | SEC_LOAD; | |
928 | if (sreloc == NULL | |
929 | || ! bfd_set_section_flags (dynobj, sreloc, flags) | |
930 | || ! bfd_set_section_alignment (dynobj, sreloc, 3)) | |
931 | return false; | |
932 | } | |
933 | } | |
934 | ||
935 | sreloc->_raw_size += sizeof (Elf64_External_Rela); | |
936 | } | |
937 | break; | |
938 | ||
939 | case R_SPARC_REGISTER: | |
940 | /* Nothing to do. */ | |
941 | break; | |
942 | ||
943 | default: | |
944 | (*_bfd_error_handler)("%s: check_relocs: unhandled reloc type %d", | |
945 | bfd_get_filename(abfd), | |
946 | ELF64_R_TYPE (rel->r_info)); | |
947 | return false; | |
948 | } | |
949 | } | |
950 | ||
951 | return true; | |
952 | } | |
953 | ||
954 | /* Adjust a symbol defined by a dynamic object and referenced by a | |
955 | regular object. The current definition is in some section of the | |
956 | dynamic object, but we're not including those sections. We have to | |
957 | change the definition to something the rest of the link can | |
958 | understand. */ | |
959 | ||
960 | static boolean | |
961 | sparc64_elf_adjust_dynamic_symbol (info, h) | |
962 | struct bfd_link_info *info; | |
963 | struct elf_link_hash_entry *h; | |
964 | { | |
965 | bfd *dynobj; | |
966 | asection *s; | |
967 | unsigned int power_of_two; | |
968 | ||
969 | dynobj = elf_hash_table (info)->dynobj; | |
970 | ||
971 | /* Make sure we know what is going on here. */ | |
972 | BFD_ASSERT (dynobj != NULL | |
973 | && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) | |
974 | || h->weakdef != NULL | |
975 | || ((h->elf_link_hash_flags | |
976 | & ELF_LINK_HASH_DEF_DYNAMIC) != 0 | |
977 | && (h->elf_link_hash_flags | |
978 | & ELF_LINK_HASH_REF_REGULAR) != 0 | |
979 | && (h->elf_link_hash_flags | |
980 | & ELF_LINK_HASH_DEF_REGULAR) == 0))); | |
981 | ||
982 | /* If this is a function, put it in the procedure linkage table. We | |
983 | will fill in the contents of the procedure linkage table later | |
984 | (although we could actually do it here). The STT_NOTYPE | |
985 | condition is a hack specifically for the Oracle libraries | |
986 | delivered for Solaris; for some inexplicable reason, they define | |
987 | some of their functions as STT_NOTYPE when they really should be | |
988 | STT_FUNC. */ | |
989 | if (h->type == STT_FUNC | |
990 | || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0 | |
991 | || (h->type == STT_NOTYPE | |
992 | && (h->root.type == bfd_link_hash_defined | |
993 | || h->root.type == bfd_link_hash_defweak) | |
994 | && (h->root.u.def.section->flags & SEC_CODE) != 0)) | |
995 | { | |
996 | if (! elf_hash_table (info)->dynamic_sections_created) | |
997 | { | |
998 | /* This case can occur if we saw a WPLT30 reloc in an input | |
999 | file, but none of the input files were dynamic objects. | |
1000 | In such a case, we don't actually need to build a | |
1001 | procedure linkage table, and we can just do a WDISP30 | |
1002 | reloc instead. */ | |
1003 | BFD_ASSERT ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0); | |
1004 | return true; | |
1005 | } | |
1006 | ||
1007 | s = bfd_get_section_by_name (dynobj, ".plt"); | |
1008 | BFD_ASSERT (s != NULL); | |
1009 | ||
1010 | /* The first four bit in .plt is reserved. */ | |
1011 | if (s->_raw_size == 0) | |
1012 | s->_raw_size = PLT_HEADER_SIZE; | |
1013 | ||
1014 | /* If this symbol is not defined in a regular file, and we are | |
1015 | not generating a shared library, then set the symbol to this | |
1016 | location in the .plt. This is required to make function | |
1017 | pointers compare as equal between the normal executable and | |
1018 | the shared library. */ | |
1019 | if (! info->shared | |
1020 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0) | |
1021 | { | |
1022 | h->root.u.def.section = s; | |
1023 | h->root.u.def.value = s->_raw_size; | |
1024 | } | |
1025 | ||
1026 | h->plt_offset = s->_raw_size; | |
1027 | ||
1028 | /* Make room for this entry. */ | |
1029 | s->_raw_size += PLT_ENTRY_SIZE; | |
1030 | ||
1031 | /* We also need to make an entry in the .got.plt section, which | |
1032 | will be placed in the .got section by the linker script. */ | |
1033 | ||
1034 | s = bfd_get_section_by_name (dynobj, ".got.plt"); | |
1035 | BFD_ASSERT (s != NULL); | |
1036 | s->_raw_size += 8; | |
1037 | ||
1038 | /* We also need to make an entry in the .rela.plt section. */ | |
1039 | ||
1040 | s = bfd_get_section_by_name (dynobj, ".rela.plt"); | |
1041 | BFD_ASSERT (s != NULL); | |
1042 | s->_raw_size += sizeof (Elf64_External_Rela); | |
1043 | ||
1044 | /* The procedure linkage table size is bounded by the magnitude | |
1045 | of the offset we can describe in the entry. */ | |
1046 | if (s->_raw_size >= (bfd_vma)1 << 32) | |
1047 | { | |
1048 | bfd_set_error (bfd_error_bad_value); | |
1049 | return false; | |
1050 | } | |
1051 | ||
1052 | return true; | |
1053 | } | |
1054 | ||
1055 | /* If this is a weak symbol, and there is a real definition, the | |
1056 | processor independent code will have arranged for us to see the | |
1057 | real definition first, and we can just use the same value. */ | |
1058 | if (h->weakdef != NULL) | |
1059 | { | |
1060 | BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined | |
1061 | || h->weakdef->root.type == bfd_link_hash_defweak); | |
1062 | h->root.u.def.section = h->weakdef->root.u.def.section; | |
1063 | h->root.u.def.value = h->weakdef->root.u.def.value; | |
1064 | return true; | |
1065 | } | |
1066 | ||
1067 | /* This is a reference to a symbol defined by a dynamic object which | |
1068 | is not a function. */ | |
1069 | ||
1070 | /* If we are creating a shared library, we must presume that the | |
1071 | only references to the symbol are via the global offset table. | |
1072 | For such cases we need not do anything here; the relocations will | |
1073 | be handled correctly by relocate_section. */ | |
1074 | if (info->shared) | |
1075 | return true; | |
1076 | ||
1077 | /* We must allocate the symbol in our .dynbss section, which will | |
1078 | become part of the .bss section of the executable. There will be | |
1079 | an entry for this symbol in the .dynsym section. The dynamic | |
1080 | object will contain position independent code, so all references | |
1081 | from the dynamic object to this symbol will go through the global | |
1082 | offset table. The dynamic linker will use the .dynsym entry to | |
1083 | determine the address it must put in the global offset table, so | |
1084 | both the dynamic object and the regular object will refer to the | |
1085 | same memory location for the variable. */ | |
1086 | ||
1087 | s = bfd_get_section_by_name (dynobj, ".dynbss"); | |
1088 | BFD_ASSERT (s != NULL); | |
1089 | ||
1090 | /* We must generate a R_SPARC_COPY reloc to tell the dynamic linker | |
1091 | to copy the initial value out of the dynamic object and into the | |
1092 | runtime process image. We need to remember the offset into the | |
1093 | .rel.bss section we are going to use. */ | |
1094 | if ((h->root.u.def.section->flags & SEC_ALLOC) != 0) | |
1095 | { | |
1096 | asection *srel; | |
1097 | ||
1098 | srel = bfd_get_section_by_name (dynobj, ".rela.bss"); | |
1099 | BFD_ASSERT (srel != NULL); | |
1100 | srel->_raw_size += sizeof (Elf64_External_Rela); | |
1101 | h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY; | |
1102 | } | |
1103 | ||
1104 | /* We need to figure out the alignment required for this symbol. I | |
1105 | have no idea how ELF linkers handle this. 16-bytes is the size | |
1106 | of the largest type that requires hard alignment -- long double. */ | |
1107 | power_of_two = bfd_log2 (h->size); | |
1108 | if (power_of_two > 4) | |
1109 | power_of_two = 4; | |
1110 | ||
1111 | /* Apply the required alignment. */ | |
1112 | s->_raw_size = BFD_ALIGN (s->_raw_size, | |
1113 | (bfd_size_type) (1 << power_of_two)); | |
1114 | if (power_of_two > bfd_get_section_alignment (dynobj, s)) | |
1115 | { | |
1116 | if (! bfd_set_section_alignment (dynobj, s, power_of_two)) | |
1117 | return false; | |
1118 | } | |
1119 | ||
1120 | /* Define the symbol as being at this point in the section. */ | |
1121 | h->root.u.def.section = s; | |
1122 | h->root.u.def.value = s->_raw_size; | |
1123 | ||
1124 | /* Increment the section size to make room for the symbol. */ | |
1125 | s->_raw_size += h->size; | |
1126 | ||
1127 | return true; | |
1128 | } | |
1129 | ||
1130 | /* Set the sizes of the dynamic sections. */ | |
1131 | ||
1132 | static boolean | |
1133 | sparc64_elf_size_dynamic_sections (output_bfd, info) | |
1134 | bfd *output_bfd; | |
1135 | struct bfd_link_info *info; | |
1136 | { | |
1137 | bfd *dynobj; | |
1138 | asection *s; | |
1139 | boolean reltext; | |
1140 | boolean relplt; | |
1141 | ||
1142 | dynobj = elf_hash_table (info)->dynobj; | |
1143 | BFD_ASSERT (dynobj != NULL); | |
1144 | ||
1145 | if (elf_hash_table (info)->dynamic_sections_created) | |
1146 | { | |
1147 | /* Set the contents of the .interp section to the interpreter. */ | |
1148 | if (! info->shared) | |
1149 | { | |
1150 | s = bfd_get_section_by_name (dynobj, ".interp"); | |
1151 | BFD_ASSERT (s != NULL); | |
1152 | s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER; | |
1153 | s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER; | |
1154 | } | |
1155 | } | |
1156 | else | |
1157 | { | |
1158 | /* We may have created entries in the .rela.got section. | |
1159 | However, if we are not creating the dynamic sections, we will | |
1160 | not actually use these entries. Reset the size of .rela.got, | |
1161 | which will cause it to get stripped from the output file | |
1162 | below. */ | |
1163 | s = bfd_get_section_by_name (dynobj, ".rela.got"); | |
1164 | if (s != NULL) | |
1165 | s->_raw_size = 0; | |
1166 | } | |
1167 | ||
1168 | /* The check_relocs and adjust_dynamic_symbol entry points have | |
1169 | determined the sizes of the various dynamic sections. Allocate | |
1170 | memory for them. */ | |
1171 | reltext = false; | |
1172 | relplt = false; | |
1173 | for (s = dynobj->sections; s != NULL; s = s->next) | |
1174 | { | |
1175 | const char *name; | |
1176 | boolean strip; | |
1177 | ||
1178 | if ((s->flags & SEC_LINKER_CREATED) == 0) | |
1179 | continue; | |
1180 | ||
1181 | /* It's OK to base decisions on the section name, because none | |
1182 | of the dynobj section names depend upon the input files. */ | |
1183 | name = bfd_get_section_name (dynobj, s); | |
1184 | ||
1185 | strip = false; | |
1186 | ||
1187 | if (strncmp (name, ".rela", 5) == 0) | |
1188 | { | |
1189 | if (s->_raw_size == 0) | |
1190 | { | |
1191 | /* If we don't need this section, strip it from the | |
1192 | output file. This is to handle .rela.bss and | |
1193 | .rel.plt. We must create it in | |
1194 | create_dynamic_sections, because it must be created | |
1195 | before the linker maps input sections to output | |
1196 | sections. The linker does that before | |
1197 | adjust_dynamic_symbol is called, and it is that | |
1198 | function which decides whether anything needs to go | |
1199 | into these sections. */ | |
1200 | strip = true; | |
1201 | } | |
1202 | else | |
1203 | { | |
1204 | const char *outname; | |
1205 | asection *target; | |
1206 | ||
1207 | /* If this relocation section applies to a read only | |
1208 | section, then we probably need a DT_TEXTREL entry. */ | |
1209 | outname = bfd_get_section_name (output_bfd, | |
1210 | s->output_section); | |
1211 | target = bfd_get_section_by_name (output_bfd, outname + 5); | |
1212 | if (target != NULL | |
1213 | && (target->flags & SEC_READONLY) != 0) | |
1214 | reltext = true; | |
1215 | ||
1216 | if (strcmp (name, ".rela.plt") == 0) | |
1217 | relplt = true; | |
1218 | ||
1219 | /* We use the reloc_count field as a counter if we need | |
1220 | to copy relocs into the output file. */ | |
1221 | s->reloc_count = 0; | |
1222 | } | |
1223 | } | |
1224 | else if (strcmp (name, ".plt") != 0 | |
1225 | && strncmp (name, ".got", 4) != 0) | |
1226 | { | |
1227 | /* It's not one of our sections, so don't allocate space. */ | |
1228 | continue; | |
1229 | } | |
1230 | ||
1231 | if (strip) | |
1232 | { | |
1233 | asection **spp; | |
1234 | ||
1235 | for (spp = &s->output_section->owner->sections; | |
1236 | *spp != s->output_section; | |
1237 | spp = &(*spp)->next) | |
1238 | ; | |
1239 | *spp = s->output_section->next; | |
1240 | --s->output_section->owner->section_count; | |
1241 | ||
1242 | continue; | |
1243 | } | |
1244 | ||
1245 | /* Allocate memory for the section contents. */ | |
1246 | s->contents = (bfd_byte *) bfd_alloc (dynobj, s->_raw_size); | |
1247 | if (s->contents == NULL && s->_raw_size != 0) | |
1248 | return false; | |
1249 | } | |
1250 | ||
1251 | if (elf_hash_table (info)->dynamic_sections_created) | |
1252 | { | |
1253 | /* Add some entries to the .dynamic section. We fill in the | |
1254 | values later, in sparc64_elf_finish_dynamic_sections, but we | |
1255 | must add the entries now so that we get the correct size for | |
1256 | the .dynamic section. The DT_DEBUG entry is filled in by the | |
1257 | dynamic linker and used by the debugger. */ | |
1258 | if (! info->shared) | |
1259 | { | |
1260 | if (! bfd_elf64_add_dynamic_entry (info, DT_DEBUG, 0)) | |
1261 | return false; | |
1262 | } | |
1263 | ||
1264 | if (relplt) | |
1265 | { | |
1266 | if (! bfd_elf64_add_dynamic_entry (info, DT_PLTGOT, 0) | |
1267 | || ! bfd_elf64_add_dynamic_entry (info, DT_PLTRELSZ, 0) | |
1268 | || ! bfd_elf64_add_dynamic_entry (info, DT_PLTREL, DT_RELA) | |
1269 | || ! bfd_elf64_add_dynamic_entry (info, DT_JMPREL, 0) | |
1270 | || ! bfd_elf64_add_dynamic_entry (info, DT_SPARC_PLTFMT, | |
1271 | (info->shared != 0) + 1)) | |
1272 | return false; | |
1273 | } | |
1274 | ||
1275 | if (! bfd_elf64_add_dynamic_entry (info, DT_RELA, 0) | |
1276 | || ! bfd_elf64_add_dynamic_entry (info, DT_RELASZ, 0) | |
1277 | || ! bfd_elf64_add_dynamic_entry (info, DT_RELAENT, | |
1278 | sizeof (Elf64_External_Rela))) | |
1279 | return false; | |
1280 | ||
1281 | if (reltext) | |
1282 | { | |
1283 | if (! bfd_elf64_add_dynamic_entry (info, DT_TEXTREL, 0)) | |
1284 | return false; | |
1285 | } | |
1286 | } | |
1287 | ||
1288 | /* If we are generating a shared library, we generate a section | |
1289 | symbol for each output section for which we might need to copy | |
1290 | relocs. These are local symbols, which means that they must come | |
1291 | first in the dynamic symbol table. That means we must increment | |
1292 | the dynamic symbol index of every other dynamic symbol. */ | |
1293 | if (info->shared) | |
1294 | { | |
1295 | int c; | |
1296 | ||
1297 | c = 0; | |
1298 | for (s = output_bfd->sections; s != NULL; s = s->next) | |
1299 | { | |
1300 | if ((s->flags & SEC_LINKER_CREATED) != 0 | |
1301 | || (s->flags & SEC_ALLOC) == 0) | |
1302 | continue; | |
1303 | ||
1304 | elf_section_data (s)->dynindx = c + 1; | |
1305 | ||
1306 | /* These symbols will have no names, so we don't need to | |
1307 | fiddle with dynstr_index. */ | |
1308 | ||
1309 | ++c; | |
1310 | } | |
1311 | ||
1312 | elf_link_hash_traverse (elf_hash_table (info), | |
1313 | sparc64_elf_adjust_dynindx, | |
1314 | (PTR) &c); | |
1315 | elf_hash_table (info)->dynsymcount += c; | |
1316 | } | |
1317 | ||
1318 | return true; | |
1319 | } | |
1320 | ||
1321 | /* Increment the index of a dynamic symbol by a given amount. Called | |
1322 | via elf_link_hash_traverse. */ | |
1323 | ||
1324 | static boolean | |
1325 | sparc64_elf_adjust_dynindx (h, cparg) | |
1326 | struct elf_link_hash_entry *h; | |
1327 | PTR cparg; | |
1328 | { | |
1329 | int *cp = (int *) cparg; | |
1330 | ||
1331 | if (h->dynindx != -1) | |
1332 | h->dynindx += *cp; | |
1333 | return true; | |
1334 | } | |
1335 | ||
3869b11f | 1336 | \f |
d3366d23 DE |
1337 | /* Relocate a SPARC64 ELF section. */ |
1338 | ||
1339 | static boolean | |
1340 | sparc64_elf_relocate_section (output_bfd, info, input_bfd, input_section, | |
1341 | contents, relocs, local_syms, local_sections) | |
1342 | bfd *output_bfd; | |
1343 | struct bfd_link_info *info; | |
1344 | bfd *input_bfd; | |
1345 | asection *input_section; | |
1346 | bfd_byte *contents; | |
1347 | Elf_Internal_Rela *relocs; | |
1348 | Elf_Internal_Sym *local_syms; | |
1349 | asection **local_sections; | |
789776d7 | 1350 | { |
65c2dd6e | 1351 | bfd *dynobj; |
d3366d23 DE |
1352 | Elf_Internal_Shdr *symtab_hdr; |
1353 | struct elf_link_hash_entry **sym_hashes; | |
65c2dd6e RH |
1354 | bfd_vma *local_got_offsets; |
1355 | bfd_vma got_base; | |
1356 | asection *sgot; | |
1357 | asection *splt; | |
1358 | asection *sreloc; | |
d3366d23 DE |
1359 | Elf_Internal_Rela *rel; |
1360 | Elf_Internal_Rela *relend; | |
1361 | ||
65c2dd6e | 1362 | dynobj = elf_hash_table (info)->dynobj; |
d3366d23 DE |
1363 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; |
1364 | sym_hashes = elf_sym_hashes (input_bfd); | |
65c2dd6e RH |
1365 | local_got_offsets = elf_local_got_offsets (input_bfd); |
1366 | ||
1367 | if (elf_hash_table(info)->hgot == NULL) | |
1368 | got_base = 0; | |
1369 | else | |
1370 | got_base = elf_hash_table (info)->hgot->root.u.def.value; | |
1371 | ||
1372 | sgot = splt = sreloc = NULL; | |
d3366d23 DE |
1373 | |
1374 | rel = relocs; | |
1375 | relend = relocs + input_section->reloc_count; | |
1376 | for (; rel < relend; rel++) | |
1377 | { | |
1378 | int r_type; | |
1379 | reloc_howto_type *howto; | |
1380 | long r_symndx; | |
1381 | struct elf_link_hash_entry *h; | |
1382 | Elf_Internal_Sym *sym; | |
1383 | asection *sec; | |
1384 | bfd_vma relocation; | |
1385 | bfd_reloc_status_type r; | |
1386 | ||
1387 | r_type = ELF64_R_TYPE (rel->r_info); | |
1388 | if (r_type < 0 || r_type >= (int) R_SPARC_max) | |
1389 | { | |
1390 | bfd_set_error (bfd_error_bad_value); | |
1391 | return false; | |
1392 | } | |
3869b11f | 1393 | howto = sparc64_elf_howto_table + r_type; |
d3366d23 DE |
1394 | |
1395 | r_symndx = ELF64_R_SYM (rel->r_info); | |
1396 | ||
1397 | if (info->relocateable) | |
1398 | { | |
1399 | /* This is a relocateable link. We don't have to change | |
1400 | anything, unless the reloc is against a section symbol, | |
1401 | in which case we have to adjust according to where the | |
1402 | section symbol winds up in the output section. */ | |
1403 | if (r_symndx < symtab_hdr->sh_info) | |
1404 | { | |
1405 | sym = local_syms + r_symndx; | |
1406 | if (ELF_ST_TYPE (sym->st_info) == STT_SECTION) | |
1407 | { | |
1408 | sec = local_sections[r_symndx]; | |
1409 | rel->r_addend += sec->output_offset + sym->st_value; | |
1410 | } | |
1411 | } | |
1412 | ||
1413 | continue; | |
1414 | } | |
1415 | ||
1416 | /* This is a final link. */ | |
1417 | h = NULL; | |
1418 | sym = NULL; | |
1419 | sec = NULL; | |
1420 | if (r_symndx < symtab_hdr->sh_info) | |
1421 | { | |
1422 | sym = local_syms + r_symndx; | |
1423 | sec = local_sections[r_symndx]; | |
1424 | relocation = (sec->output_section->vma | |
1425 | + sec->output_offset | |
1426 | + sym->st_value); | |
1427 | } | |
1428 | else | |
1429 | { | |
1430 | h = sym_hashes[r_symndx - symtab_hdr->sh_info]; | |
3869b11f DE |
1431 | while (h->root.type == bfd_link_hash_indirect |
1432 | || h->root.type == bfd_link_hash_warning) | |
1433 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
d3366d23 DE |
1434 | if (h->root.type == bfd_link_hash_defined |
1435 | || h->root.type == bfd_link_hash_defweak) | |
1436 | { | |
65c2dd6e | 1437 | boolean skip_it = false; |
d3366d23 | 1438 | sec = h->root.u.def.section; |
65c2dd6e RH |
1439 | |
1440 | switch (r_type) | |
1441 | { | |
1442 | case R_SPARC_WPLT30: | |
1443 | case R_SPARC_PLT32: | |
1444 | case R_SPARC_HIPLT22: | |
1445 | case R_SPARC_LOPLT10: | |
1446 | case R_SPARC_PCPLT32: | |
1447 | case R_SPARC_PCPLT22: | |
1448 | case R_SPARC_PCPLT10: | |
1449 | case R_SPARC_PLT64: | |
1450 | if (h->plt_offset != (bfd_vma) -1) | |
1451 | skip_it = true; | |
1452 | break; | |
1453 | ||
1454 | case R_SPARC_GOT10: | |
1455 | case R_SPARC_GOT13: | |
1456 | case R_SPARC_GOT22: | |
1457 | if (elf_hash_table(info)->dynamic_sections_created | |
1458 | && (!info->shared | |
1459 | || (!info->symbolic && h->dynindx != -1) | |
1460 | || !(h->elf_link_hash_flags | |
1461 | & ELF_LINK_HASH_DEF_REGULAR))) | |
1462 | skip_it = true; | |
1463 | break; | |
1464 | ||
1465 | case R_SPARC_PC10: | |
1466 | case R_SPARC_PC22: | |
1467 | case R_SPARC_PC_HH22: | |
1468 | case R_SPARC_PC_HM10: | |
1469 | case R_SPARC_PC_LM22: | |
1470 | if (!strcmp(h->root.root.string, "_GLOBAL_OFFSET_TABLE_")) | |
1471 | break; | |
1472 | /* FALLTHRU */ | |
1473 | ||
1474 | case R_SPARC_8: | |
1475 | case R_SPARC_16: | |
1476 | case R_SPARC_32: | |
1477 | case R_SPARC_DISP8: | |
1478 | case R_SPARC_DISP16: | |
1479 | case R_SPARC_DISP32: | |
1480 | case R_SPARC_WDISP30: | |
1481 | case R_SPARC_WDISP22: | |
1482 | case R_SPARC_HI22: | |
1483 | case R_SPARC_22: | |
1484 | case R_SPARC_13: | |
1485 | case R_SPARC_LO10: | |
1486 | case R_SPARC_UA32: | |
1487 | case R_SPARC_10: | |
1488 | case R_SPARC_11: | |
1489 | case R_SPARC_64: | |
1490 | case R_SPARC_OLO10: | |
1491 | case R_SPARC_HH22: | |
1492 | case R_SPARC_HM10: | |
1493 | case R_SPARC_LM22: | |
1494 | case R_SPARC_WDISP19: | |
1495 | case R_SPARC_WDISP16: | |
1496 | case R_SPARC_7: | |
1497 | case R_SPARC_5: | |
1498 | case R_SPARC_6: | |
1499 | case R_SPARC_DISP64: | |
1500 | case R_SPARC_HIX22: | |
1501 | case R_SPARC_LOX10: | |
1502 | case R_SPARC_H44: | |
1503 | case R_SPARC_M44: | |
1504 | case R_SPARC_L44: | |
d87ad0dc RH |
1505 | case R_SPARC_UA64: |
1506 | case R_SPARC_UA16: | |
65c2dd6e RH |
1507 | if (info->shared |
1508 | && ((!info->symbolic && h->dynindx != -1) | |
1509 | || !(h->elf_link_hash_flags | |
1510 | & ELF_LINK_HASH_DEF_REGULAR))) | |
1511 | skip_it = true; | |
1512 | break; | |
1513 | } | |
1514 | ||
1515 | if (skip_it) | |
1516 | { | |
1517 | /* In these cases, we don't need the relocation | |
1518 | value. We check specially because in some | |
1519 | obscure cases sec->output_section will be NULL. */ | |
1520 | relocation = 0; | |
1521 | } | |
1522 | else | |
1523 | { | |
1524 | relocation = (h->root.u.def.value | |
1525 | + sec->output_section->vma | |
1526 | + sec->output_offset); | |
1527 | } | |
d3366d23 DE |
1528 | } |
1529 | else if (h->root.type == bfd_link_hash_undefweak) | |
1530 | relocation = 0; | |
65c2dd6e RH |
1531 | else if (info->shared && !info->symbolic) |
1532 | relocation = 0; | |
d3366d23 DE |
1533 | else |
1534 | { | |
1535 | if (! ((*info->callbacks->undefined_symbol) | |
1536 | (info, h->root.root.string, input_bfd, | |
1537 | input_section, rel->r_offset))) | |
1538 | return false; | |
1539 | relocation = 0; | |
1540 | } | |
1541 | } | |
1542 | ||
65c2dd6e RH |
1543 | /* When generating a shared object, these relocations are copied |
1544 | into the output file to be resolved at run time. */ | |
1545 | if (info->shared && !(input_section->flags & SEC_DEBUGGING)) | |
1546 | { | |
1547 | switch (r_type) | |
1548 | { | |
1549 | case R_SPARC_PC10: | |
1550 | case R_SPARC_PC22: | |
1551 | case R_SPARC_PC_HH22: | |
1552 | case R_SPARC_PC_HM10: | |
1553 | case R_SPARC_PC_LM22: | |
1554 | if (h != NULL | |
1555 | && !strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_")) | |
1556 | break; | |
1557 | /* Fall through. */ | |
1558 | case R_SPARC_DISP8: | |
1559 | case R_SPARC_DISP16: | |
1560 | case R_SPARC_DISP32: | |
1561 | case R_SPARC_WDISP30: | |
1562 | case R_SPARC_WDISP22: | |
1563 | case R_SPARC_WDISP19: | |
1564 | case R_SPARC_WDISP16: | |
1565 | case R_SPARC_DISP64: | |
1566 | if (h == NULL) | |
1567 | break; | |
1568 | /* Fall through. */ | |
1569 | case R_SPARC_8: | |
1570 | case R_SPARC_16: | |
1571 | case R_SPARC_32: | |
1572 | case R_SPARC_HI22: | |
1573 | case R_SPARC_22: | |
1574 | case R_SPARC_13: | |
1575 | case R_SPARC_LO10: | |
1576 | case R_SPARC_UA32: | |
1577 | case R_SPARC_10: | |
1578 | case R_SPARC_11: | |
1579 | case R_SPARC_64: | |
1580 | case R_SPARC_OLO10: | |
1581 | case R_SPARC_HH22: | |
1582 | case R_SPARC_HM10: | |
1583 | case R_SPARC_LM22: | |
1584 | case R_SPARC_7: | |
1585 | case R_SPARC_5: | |
1586 | case R_SPARC_6: | |
1587 | case R_SPARC_HIX22: | |
1588 | case R_SPARC_LOX10: | |
1589 | case R_SPARC_H44: | |
1590 | case R_SPARC_M44: | |
1591 | case R_SPARC_L44: | |
d87ad0dc RH |
1592 | case R_SPARC_UA64: |
1593 | case R_SPARC_UA16: | |
65c2dd6e RH |
1594 | { |
1595 | Elf_Internal_Rela outrel; | |
1596 | boolean skip; | |
1597 | ||
1598 | if (sreloc == NULL) | |
1599 | { | |
1600 | const char *name = | |
1601 | (bfd_elf_string_from_elf_section | |
1602 | (input_bfd, | |
1603 | elf_elfheader (input_bfd)->e_shstrndx, | |
1604 | elf_section_data (input_section)->rel_hdr.sh_name)); | |
1605 | ||
1606 | if (name == NULL) | |
1607 | return false; | |
1608 | ||
1609 | BFD_ASSERT (strncmp (name, ".rela", 5) == 0 | |
1610 | && strcmp (bfd_get_section_name(input_bfd, | |
1611 | input_section), | |
1612 | name + 5) == 0); | |
1613 | ||
1614 | sreloc = bfd_get_section_by_name (dynobj, name); | |
1615 | BFD_ASSERT (sreloc != NULL); | |
1616 | } | |
1617 | ||
1618 | skip = false; | |
1619 | ||
1620 | if (elf_section_data (input_section)->stab_info == NULL) | |
1621 | outrel.r_offset = rel->r_offset; | |
1622 | else | |
1623 | { | |
1624 | bfd_vma off; | |
1625 | ||
1626 | off = (_bfd_stab_section_offset | |
1627 | (output_bfd, &elf_hash_table (info)->stab_info, | |
1628 | input_section, | |
1629 | &elf_section_data (input_section)->stab_info, | |
1630 | rel->r_offset)); | |
1631 | if (off == MINUS_ONE) | |
1632 | skip = true; | |
1633 | outrel.r_offset = off; | |
1634 | } | |
1635 | ||
1636 | outrel.r_offset += (input_section->output_section->vma | |
1637 | + input_section->output_offset); | |
1638 | ||
d87ad0dc RH |
1639 | /* Optimize unaligned reloc usage now that we know where |
1640 | it finally resides. */ | |
1641 | switch (r_type) | |
1642 | { | |
1643 | case R_SPARC_16: | |
1644 | if (outrel.r_offset & 1) r_type = R_SPARC_UA16; | |
1645 | break; | |
1646 | case R_SPARC_UA16: | |
1647 | if (!(outrel.r_offset & 1)) r_type = R_SPARC_16; | |
1648 | break; | |
1649 | case R_SPARC_32: | |
1650 | if (outrel.r_offset & 3) r_type = R_SPARC_UA32; | |
1651 | break; | |
1652 | case R_SPARC_UA32: | |
1653 | if (!(outrel.r_offset & 3)) r_type = R_SPARC_32; | |
1654 | break; | |
1655 | case R_SPARC_64: | |
1656 | if (outrel.r_offset & 7) r_type = R_SPARC_UA64; | |
1657 | break; | |
1658 | case R_SPARC_UA64: | |
1659 | if (!(outrel.r_offset & 7)) r_type = R_SPARC_64; | |
1660 | break; | |
1661 | } | |
1662 | ||
65c2dd6e RH |
1663 | if (skip) |
1664 | memset (&outrel, 0, sizeof outrel); | |
1665 | /* h->dynindx may be -1 if the symbol was marked to | |
1666 | become local. */ | |
1667 | else if (h != NULL | |
1668 | && ((! info->symbolic && h->dynindx != -1) | |
1669 | || (h->elf_link_hash_flags | |
1670 | & ELF_LINK_HASH_DEF_REGULAR) == 0)) | |
1671 | { | |
1672 | BFD_ASSERT (h->dynindx != -1); | |
1673 | outrel.r_info = ELF64_R_INFO (h->dynindx, r_type); | |
1674 | outrel.r_addend = rel->r_addend; | |
1675 | } | |
1676 | else | |
1677 | { | |
1678 | if (r_type == R_SPARC_64) | |
1679 | { | |
1680 | outrel.r_info = ELF64_R_INFO (0, R_SPARC_RELATIVE); | |
1681 | outrel.r_addend = relocation + rel->r_addend; | |
1682 | } | |
1683 | else | |
1684 | { | |
1685 | long indx; | |
1686 | ||
1687 | if (h == NULL) | |
1688 | sec = local_sections[r_symndx]; | |
1689 | else | |
1690 | { | |
1691 | BFD_ASSERT (h->root.type == bfd_link_hash_defined | |
1692 | || (h->root.type | |
1693 | == bfd_link_hash_defweak)); | |
1694 | sec = h->root.u.def.section; | |
1695 | } | |
1696 | if (sec != NULL && bfd_is_abs_section (sec)) | |
1697 | indx = 0; | |
1698 | else if (sec == NULL || sec->owner == NULL) | |
1699 | { | |
1700 | bfd_set_error (bfd_error_bad_value); | |
1701 | return false; | |
1702 | } | |
1703 | else | |
1704 | { | |
1705 | asection *osec; | |
1706 | ||
1707 | osec = sec->output_section; | |
1708 | indx = elf_section_data (osec)->dynindx; | |
1709 | BFD_ASSERT (indx > 0); | |
1710 | } | |
1711 | ||
1712 | outrel.r_info = ELF64_R_INFO (indx, r_type); | |
1713 | outrel.r_addend = relocation + rel->r_addend; | |
1714 | } | |
1715 | } | |
1716 | ||
1717 | bfd_elf64_swap_reloca_out (output_bfd, &outrel, | |
1718 | (((Elf64_External_Rela *) | |
1719 | sreloc->contents) | |
1720 | + sreloc->reloc_count)); | |
1721 | ++sreloc->reloc_count; | |
1722 | ||
1723 | /* This reloc will be computed at runtime, so there's no | |
1724 | need to do anything now, unless this is a RELATIVE | |
1725 | reloc in an unallocated section. */ | |
1726 | if (skip | |
1727 | || (input_section->flags & SEC_ALLOC) != 0 | |
1728 | || ELF64_R_TYPE (outrel.r_info) != R_SPARC_RELATIVE) | |
1729 | continue; | |
1730 | } | |
1731 | break; | |
1732 | } | |
1733 | } | |
1734 | ||
70e818d4 | 1735 | switch (r_type) |
d3366d23 | 1736 | { |
65c2dd6e RH |
1737 | case R_SPARC_GOT10: |
1738 | case R_SPARC_GOT13: | |
1739 | case R_SPARC_GOT22: | |
1740 | /* Relocation is to the entry for this symbol in the global | |
1741 | offset table. */ | |
1742 | if (sgot == NULL) | |
1743 | { | |
1744 | sgot = bfd_get_section_by_name (dynobj, ".got"); | |
1745 | BFD_ASSERT (sgot != NULL); | |
1746 | } | |
1747 | ||
1748 | if (h != NULL) | |
1749 | { | |
1750 | bfd_vma off = h->got_offset; | |
1751 | BFD_ASSERT (off != (bfd_vma) -1); | |
1752 | ||
1753 | if (! elf_hash_table (info)->dynamic_sections_created | |
1754 | || (info->shared | |
1755 | && (info->symbolic || h->dynindx == -1) | |
1756 | && (h->elf_link_hash_flags | |
1757 | & ELF_LINK_HASH_DEF_REGULAR))) | |
1758 | { | |
1759 | /* This is actually a static link, or it is a -Bsymbolic | |
1760 | link and the symbol is defined locally, or the symbol | |
1761 | was forced to be local because of a version file. We | |
1762 | must initialize this entry in the global offset table. | |
1763 | Since the offset must always be a multiple of 8, we | |
1764 | use the least significant bit to record whether we | |
1765 | have initialized it already. | |
1766 | ||
1767 | When doing a dynamic link, we create a .rela.got | |
1768 | relocation entry to initialize the value. This is | |
1769 | done in the finish_dynamic_symbol routine. */ | |
1770 | ||
1771 | if ((off & 1) != 0) | |
1772 | off &= ~1; | |
1773 | else | |
1774 | { | |
1775 | bfd_put_64 (output_bfd, relocation, | |
1776 | sgot->contents + off); | |
1777 | h->got_offset |= 1; | |
1778 | } | |
1779 | } | |
1780 | relocation = sgot->output_offset + off - got_base; | |
1781 | } | |
1782 | else | |
1783 | { | |
1784 | bfd_vma off; | |
1785 | ||
1786 | BFD_ASSERT (local_got_offsets != NULL); | |
1787 | off = local_got_offsets[r_symndx]; | |
1788 | BFD_ASSERT (off != (bfd_vma) -1); | |
1789 | ||
1790 | /* The offset must always be a multiple of 8. We use | |
1791 | the least significant bit to record whether we have | |
1792 | already processed this entry. */ | |
1793 | if ((off & 1) != 0) | |
1794 | off &= ~1; | |
1795 | else | |
1796 | { | |
1797 | bfd_put_64 (output_bfd, relocation, sgot->contents + off); | |
1798 | local_got_offsets[r_symndx] |= 1; | |
1799 | ||
1800 | if (info->shared) | |
1801 | { | |
1802 | asection *srelgot; | |
1803 | Elf_Internal_Rela outrel; | |
1804 | ||
1805 | /* We need to generate a R_SPARC_RELATIVE reloc | |
1806 | for the dynamic linker. */ | |
1807 | srelgot = bfd_get_section_by_name(dynobj, ".rela.got"); | |
1808 | BFD_ASSERT (srelgot != NULL); | |
1809 | ||
1810 | outrel.r_offset = (sgot->output_section->vma | |
1811 | + sgot->output_offset | |
1812 | + off); | |
1813 | outrel.r_info = ELF64_R_INFO (0, R_SPARC_RELATIVE); | |
1814 | outrel.r_addend = relocation; | |
1815 | bfd_elf64_swap_reloca_out (output_bfd, &outrel, | |
1816 | (((Elf64_External_Rela *) | |
1817 | srelgot->contents) | |
1818 | + srelgot->reloc_count)); | |
1819 | ++srelgot->reloc_count; | |
1820 | } | |
1821 | } | |
1822 | relocation = sgot->output_offset + off - got_base; | |
1823 | } | |
1824 | goto do_default; | |
1825 | ||
1826 | case R_SPARC_WPLT30: | |
1827 | case R_SPARC_PLT32: | |
1828 | case R_SPARC_HIPLT22: | |
1829 | case R_SPARC_LOPLT10: | |
1830 | case R_SPARC_PCPLT32: | |
1831 | case R_SPARC_PCPLT22: | |
1832 | case R_SPARC_PCPLT10: | |
1833 | case R_SPARC_PLT64: | |
1834 | /* Relocation is to the entry for this symbol in the | |
1835 | procedure linkage table. */ | |
1836 | BFD_ASSERT (h != NULL); | |
1837 | ||
1838 | if (h->plt_offset == (bfd_vma) -1) | |
1839 | { | |
1840 | /* We didn't make a PLT entry for this symbol. This | |
1841 | happens when statically linking PIC code, or when | |
1842 | using -Bsymbolic. */ | |
1843 | goto do_default; | |
1844 | } | |
1845 | ||
1846 | if (splt == NULL) | |
1847 | { | |
1848 | splt = bfd_get_section_by_name (dynobj, ".plt"); | |
1849 | BFD_ASSERT (splt != NULL); | |
1850 | } | |
1851 | ||
1852 | relocation = (splt->output_section->vma | |
1853 | + splt->output_offset | |
1854 | + h->plt_offset); | |
1855 | goto do_default; | |
1856 | ||
70e818d4 DE |
1857 | case R_SPARC_OLO10: |
1858 | { | |
1859 | bfd_vma x; | |
1860 | ||
1861 | relocation += rel->r_addend; | |
1862 | relocation = (relocation & 0x3ff) + ELF64_R_TYPE_DATA (rel->r_info); | |
1863 | ||
1864 | x = bfd_get_32 (input_bfd, contents + rel->r_offset); | |
1865 | x = (x & ~0x1fff) | (relocation & 0x1fff); | |
1866 | bfd_put_32 (input_bfd, x, contents + rel->r_offset); | |
1867 | ||
1868 | r = bfd_check_overflow (howto->complain_on_overflow, | |
1869 | howto->bitsize, howto->rightshift, | |
1870 | relocation); | |
1871 | } | |
1872 | break; | |
1873 | ||
1874 | case R_SPARC_WDISP16: | |
1875 | { | |
1876 | bfd_vma x; | |
1877 | ||
1878 | relocation += rel->r_addend; | |
1879 | /* Adjust for pc-relative-ness. */ | |
1880 | relocation -= (input_section->output_section->vma | |
1881 | + input_section->output_offset); | |
1882 | relocation -= rel->r_offset; | |
1883 | ||
1884 | x = bfd_get_32 (input_bfd, contents + rel->r_offset); | |
1885 | x = (x & ~0x303fff) | ((((relocation >> 2) & 0xc000) << 6) | |
1886 | | ((relocation >> 2) & 0x3fff)); | |
1887 | bfd_put_32 (input_bfd, x, contents + rel->r_offset); | |
1888 | ||
1889 | r = bfd_check_overflow (howto->complain_on_overflow, | |
1890 | howto->bitsize, howto->rightshift, | |
1891 | relocation); | |
1892 | } | |
1893 | break; | |
1894 | ||
1895 | case R_SPARC_HIX22: | |
1896 | { | |
1897 | bfd_vma x; | |
1898 | ||
1899 | relocation += rel->r_addend; | |
1900 | relocation = relocation ^ MINUS_ONE; | |
1901 | ||
1902 | x = bfd_get_32 (input_bfd, contents + rel->r_offset); | |
1903 | x = (x & ~0x3fffff) | ((relocation >> 10) & 0x3fffff); | |
1904 | bfd_put_32 (input_bfd, x, contents + rel->r_offset); | |
1905 | ||
1906 | r = bfd_check_overflow (howto->complain_on_overflow, | |
1907 | howto->bitsize, howto->rightshift, | |
1908 | relocation); | |
1909 | } | |
1910 | break; | |
1911 | ||
1912 | case R_SPARC_LOX10: | |
1913 | { | |
1914 | bfd_vma x; | |
1915 | ||
1916 | relocation += rel->r_addend; | |
1917 | relocation = (relocation & 0x3ff) | 0x1c00; | |
1918 | ||
1919 | x = bfd_get_32 (input_bfd, contents + rel->r_offset); | |
1920 | x = (x & ~0x1fff) | relocation; | |
1921 | bfd_put_32 (input_bfd, x, contents + rel->r_offset); | |
d3366d23 | 1922 | |
d3366d23 | 1923 | r = bfd_reloc_ok; |
70e818d4 DE |
1924 | } |
1925 | break; | |
1926 | ||
1927 | default: | |
65c2dd6e | 1928 | do_default: |
70e818d4 DE |
1929 | r = _bfd_final_link_relocate (howto, input_bfd, input_section, |
1930 | contents, rel->r_offset, | |
1931 | relocation, rel->r_addend); | |
1932 | break; | |
d3366d23 DE |
1933 | } |
1934 | ||
65c2dd6e | 1935 | switch (r) |
d3366d23 | 1936 | { |
65c2dd6e RH |
1937 | case bfd_reloc_ok: |
1938 | break; | |
d3366d23 | 1939 | |
65c2dd6e RH |
1940 | default: |
1941 | case bfd_reloc_outofrange: | |
1942 | abort (); | |
1943 | ||
1944 | case bfd_reloc_overflow: | |
1945 | { | |
1946 | const char *name; | |
1947 | ||
1948 | if (h != NULL) | |
1949 | { | |
1950 | if (h->root.type == bfd_link_hash_undefweak | |
1951 | && howto->pc_relative) | |
d3366d23 | 1952 | { |
65c2dd6e RH |
1953 | /* Assume this is a call protected by other code that |
1954 | detect the symbol is undefined. If this is the case, | |
1955 | we can safely ignore the overflow. If not, the | |
1956 | program is hosed anyway, and a little warning isn't | |
1957 | going to help. */ | |
1958 | break; | |
d3366d23 | 1959 | } |
65c2dd6e RH |
1960 | |
1961 | name = h->root.root.string; | |
1962 | } | |
1963 | else | |
1964 | { | |
1965 | name = (bfd_elf_string_from_elf_section | |
1966 | (input_bfd, | |
1967 | symtab_hdr->sh_link, | |
1968 | sym->st_name)); | |
1969 | if (name == NULL) | |
d3366d23 | 1970 | return false; |
65c2dd6e RH |
1971 | if (*name == '\0') |
1972 | name = bfd_section_name (input_bfd, sec); | |
d3366d23 | 1973 | } |
65c2dd6e RH |
1974 | if (! ((*info->callbacks->reloc_overflow) |
1975 | (info, name, howto->name, (bfd_vma) 0, | |
1976 | input_bfd, input_section, rel->r_offset))) | |
1977 | return false; | |
1978 | } | |
1979 | break; | |
1980 | } | |
1981 | } | |
1982 | ||
1983 | return true; | |
1984 | } | |
1985 | ||
1986 | /* Finish up dynamic symbol handling. We set the contents of various | |
1987 | dynamic sections here. */ | |
1988 | ||
1989 | static boolean | |
1990 | sparc64_elf_finish_dynamic_symbol (output_bfd, info, h, sym) | |
1991 | bfd *output_bfd; | |
1992 | struct bfd_link_info *info; | |
1993 | struct elf_link_hash_entry *h; | |
1994 | Elf_Internal_Sym *sym; | |
1995 | { | |
1996 | bfd *dynobj; | |
1997 | ||
1998 | dynobj = elf_hash_table (info)->dynobj; | |
1999 | ||
2000 | if (h->plt_offset != (bfd_vma) -1) | |
2001 | { | |
2002 | asection *splt; | |
2003 | asection *sgotplt; | |
2004 | asection *srela; | |
2005 | Elf_Internal_Rela rela; | |
2006 | bfd_vma values[6]; | |
2007 | bfd_vma plt0, pltN, got0, gotN, plt_index, got_offset; | |
2008 | const struct plt_template *plt_tmpl; | |
2009 | ||
2010 | /* This symbol has an entry in the procedure linkage table. Set | |
2011 | it up. */ | |
2012 | ||
2013 | BFD_ASSERT (h->dynindx != -1); | |
2014 | ||
2015 | splt = bfd_get_section_by_name (dynobj, ".plt"); | |
2016 | sgotplt = bfd_get_section_by_name (dynobj, ".got.plt"); | |
2017 | srela = bfd_get_section_by_name (dynobj, ".rela.plt"); | |
2018 | BFD_ASSERT (splt != NULL && sgotplt != NULL && srela != NULL); | |
2019 | ||
2020 | /* Fill in the various values the plt entry might care about, | |
2021 | as detailed above. */ | |
2022 | ||
2023 | plt0 = splt->output_section->vma + splt->output_offset; | |
2024 | pltN = plt0 + h->plt_offset; | |
2025 | plt_index = h->plt_offset / PLT_ENTRY_SIZE - 1; | |
2026 | got0 = sgotplt->output_section->vma + sgotplt->output_offset; | |
2027 | got_offset = (plt_index + GOT_RESERVED_ENTRIES) * 8; | |
2028 | gotN = got0 + got_offset; | |
2029 | ||
2030 | values[0] = got0; | |
2031 | values[1] = got0 - plt0; | |
2032 | values[5] = got_offset; | |
2033 | values[2] = gotN; | |
2034 | values[3] = gotN - pltN; | |
2035 | values[4] = plt_index * sizeof(Elf64_External_Rela); | |
2036 | ||
2037 | /* Fill in the entry in the procedure linkage table. */ | |
2038 | ||
2039 | if (info->shared) | |
2040 | if (got_offset < 0x1000) | |
2041 | plt_tmpl = &plt_pic_small_entry; | |
2042 | else | |
2043 | plt_tmpl = &plt_pic_large_entry; | |
2044 | else | |
2045 | /* FIXME -- learn how to select code models here. */ | |
2046 | plt_tmpl = &plt_medany_entry; | |
2047 | ||
2048 | sparc64_elf_build_plt_entry(output_bfd, splt->contents + h->plt_offset, | |
2049 | plt_tmpl, values); | |
2050 | ||
2051 | if (plt_index == 0) | |
2052 | { | |
2053 | /* We also need to fill in the plt header, but we only need to | |
2054 | do it once. Choose to do it while we do the first plt entry. */ | |
2055 | ||
2056 | sparc64_elf_build_plt_entry(output_bfd, splt->contents, | |
2057 | (info->shared | |
2058 | ? &plt_pic_header | |
2059 | : &plt_medany_header), | |
2060 | values); | |
2061 | } | |
2062 | ||
2063 | /* Fill in the entry in the .got.plt section. */ | |
2064 | ||
2065 | bfd_put_64 (output_bfd, (info->shared ? plt0-got0 : plt0), | |
2066 | sgotplt->contents + got_offset); | |
2067 | ||
2068 | /* Fill in the entry in the .rela.plt section. */ | |
2069 | ||
2070 | rela.r_offset = gotN; | |
2071 | rela.r_info = ELF64_R_INFO (h->dynindx, R_SPARC_JMP_SLOT); | |
2072 | rela.r_addend = 0; | |
2073 | bfd_elf64_swap_reloca_out (output_bfd, &rela, | |
2074 | ((Elf64_External_Rela *) srela->contents | |
2075 | + plt_index)); | |
2076 | ||
2077 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0) | |
2078 | { | |
2079 | /* Mark the symbol as undefined, rather than as defined in | |
2080 | the .plt section. Leave the value alone. */ | |
2081 | sym->st_shndx = SHN_UNDEF; | |
2082 | } | |
2083 | } | |
2084 | ||
2085 | if (h->got_offset != (bfd_vma) -1) | |
2086 | { | |
2087 | asection *sgot; | |
2088 | asection *srela; | |
2089 | Elf_Internal_Rela rela; | |
2090 | ||
2091 | /* This symbol has an entry in the global offset table. Set it | |
2092 | up. */ | |
2093 | ||
2094 | sgot = bfd_get_section_by_name (dynobj, ".got"); | |
2095 | srela = bfd_get_section_by_name (dynobj, ".rela.got"); | |
2096 | BFD_ASSERT (sgot != NULL && srela != NULL); | |
2097 | ||
2098 | rela.r_offset = (sgot->output_section->vma | |
2099 | + sgot->output_offset | |
2100 | + (h->got_offset &~ 1)); | |
2101 | ||
2102 | /* If this is a -Bsymbolic link, and the symbol is defined | |
2103 | locally, we just want to emit a RELATIVE reloc. Likewise if | |
2104 | the symbol was forced to be local because of a version file. | |
2105 | The entry in the global offset table will already have been | |
2106 | initialized in the relocate_section function. */ | |
2107 | if (info->shared | |
2108 | && (info->symbolic || h->dynindx == -1) | |
2109 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)) | |
2110 | { | |
2111 | asection *sec = h->root.u.def.section; | |
2112 | rela.r_info = ELF64_R_INFO (0, R_SPARC_RELATIVE); | |
2113 | rela.r_addend = (h->root.u.def.value | |
2114 | + sec->output_section->vma | |
2115 | + sec->output_offset); | |
2116 | } | |
2117 | else | |
2118 | { | |
2119 | bfd_put_64 (output_bfd, (bfd_vma) 0, sgot->contents + h->got_offset); | |
2120 | rela.r_info = ELF64_R_INFO (h->dynindx, R_SPARC_GLOB_DAT); | |
2121 | rela.r_addend = 0; | |
2122 | } | |
2123 | ||
2124 | bfd_elf64_swap_reloca_out (output_bfd, &rela, | |
2125 | ((Elf64_External_Rela *) srela->contents | |
2126 | + srela->reloc_count)); | |
2127 | ++srela->reloc_count; | |
2128 | } | |
2129 | ||
2130 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0) | |
2131 | { | |
2132 | asection *s; | |
2133 | Elf_Internal_Rela rela; | |
2134 | ||
2135 | /* This symbols needs a copy reloc. Set it up. */ | |
2136 | ||
2137 | BFD_ASSERT (h->dynindx != -1); | |
2138 | ||
2139 | s = bfd_get_section_by_name (h->root.u.def.section->owner, | |
2140 | ".rela.bss"); | |
2141 | BFD_ASSERT (s != NULL); | |
2142 | ||
2143 | rela.r_offset = (h->root.u.def.value | |
2144 | + h->root.u.def.section->output_section->vma | |
2145 | + h->root.u.def.section->output_offset); | |
2146 | rela.r_info = ELF64_R_INFO (h->dynindx, R_SPARC_COPY); | |
2147 | rela.r_addend = 0; | |
2148 | bfd_elf64_swap_reloca_out (output_bfd, &rela, | |
2149 | ((Elf64_External_Rela *) s->contents | |
2150 | + s->reloc_count)); | |
2151 | ++s->reloc_count; | |
2152 | } | |
2153 | ||
2154 | /* Mark some specially defined symbols as absolute. */ | |
2155 | if (strcmp (h->root.root.string, "_DYNAMIC") == 0 | |
2156 | || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0 | |
2157 | || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0) | |
2158 | sym->st_shndx = SHN_ABS; | |
2159 | ||
2160 | return true; | |
2161 | } | |
2162 | ||
2163 | /* Finish up the dynamic sections. */ | |
2164 | ||
2165 | static boolean | |
2166 | sparc64_elf_finish_dynamic_sections (output_bfd, info) | |
2167 | bfd *output_bfd; | |
2168 | struct bfd_link_info *info; | |
2169 | { | |
2170 | bfd *dynobj; | |
2171 | asection *sdyn; | |
2172 | asection *sgot; | |
2173 | ||
2174 | dynobj = elf_hash_table (info)->dynobj; | |
2175 | ||
2176 | sdyn = bfd_get_section_by_name (dynobj, ".dynamic"); | |
2177 | ||
2178 | if (elf_hash_table (info)->dynamic_sections_created) | |
2179 | { | |
2180 | asection *splt; | |
2181 | Elf64_External_Dyn *dyncon, *dynconend; | |
2182 | ||
2183 | splt = bfd_get_section_by_name (dynobj, ".plt"); | |
2184 | BFD_ASSERT (splt != NULL && sdyn != NULL); | |
2185 | ||
2186 | dyncon = (Elf64_External_Dyn *) sdyn->contents; | |
2187 | dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->_raw_size); | |
2188 | for (; dyncon < dynconend; dyncon++) | |
2189 | { | |
2190 | Elf_Internal_Dyn dyn; | |
2191 | const char *name; | |
2192 | boolean size; | |
2193 | ||
2194 | bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn); | |
2195 | ||
2196 | switch (dyn.d_tag) | |
2197 | { | |
2198 | case DT_PLTGOT: name = ".got"; size = false; break; | |
2199 | case DT_PLTRELSZ: name = ".rela.plt"; size = true; break; | |
2200 | case DT_JMPREL: name = ".rela.plt"; size = false; break; | |
2201 | default: name = NULL; size = false; break; | |
2202 | } | |
2203 | ||
2204 | if (name != NULL) | |
2205 | { | |
2206 | asection *s; | |
2207 | ||
2208 | s = bfd_get_section_by_name (output_bfd, name); | |
2209 | if (s == NULL) | |
2210 | dyn.d_un.d_val = 0; | |
2211 | else | |
2212 | { | |
2213 | if (! size) | |
2214 | dyn.d_un.d_ptr = s->vma; | |
2215 | else | |
2216 | { | |
2217 | if (s->_cooked_size != 0) | |
2218 | dyn.d_un.d_val = s->_cooked_size; | |
2219 | else | |
2220 | dyn.d_un.d_val = s->_raw_size; | |
2221 | } | |
2222 | } | |
2223 | bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon); | |
d3366d23 DE |
2224 | } |
2225 | } | |
65c2dd6e RH |
2226 | |
2227 | elf_section_data (splt->output_section)->this_hdr.sh_entsize = | |
2228 | PLT_ENTRY_SIZE; | |
2229 | } | |
2230 | ||
2231 | /* Set the first entry in the global offset table to the address of | |
2232 | the dynamic section. */ | |
2233 | sgot = bfd_get_section_by_name (dynobj, ".got.plt"); | |
2234 | BFD_ASSERT (sgot != NULL); | |
2235 | if (sgot->_raw_size > 0) | |
2236 | { | |
2237 | if (sdyn == NULL) | |
2238 | bfd_put_64 (output_bfd, (bfd_vma) 0, sgot->contents); | |
2239 | else | |
2240 | bfd_put_64 (output_bfd, | |
2241 | sdyn->output_section->vma + sdyn->output_offset, | |
2242 | sgot->contents); | |
2243 | } | |
2244 | ||
2245 | elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 8; | |
2246 | ||
2247 | if (info->shared) | |
2248 | { | |
2249 | asection *sdynsym; | |
2250 | asection *s; | |
2251 | Elf_Internal_Sym sym; | |
2252 | int c; | |
2253 | ||
2254 | /* Set up the section symbols for the output sections. */ | |
2255 | ||
2256 | sdynsym = bfd_get_section_by_name (dynobj, ".dynsym"); | |
2257 | BFD_ASSERT (sdynsym != NULL); | |
2258 | ||
2259 | sym.st_size = 0; | |
2260 | sym.st_name = 0; | |
2261 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
2262 | sym.st_other = 0; | |
2263 | ||
2264 | c = 0; | |
2265 | for (s = output_bfd->sections; s != NULL; s = s->next) | |
2266 | { | |
2267 | int indx; | |
2268 | ||
2269 | if (elf_section_data (s)->dynindx == 0) | |
2270 | continue; | |
2271 | ||
2272 | sym.st_value = s->vma; | |
2273 | ||
2274 | indx = elf_section_data (s)->this_idx; | |
2275 | BFD_ASSERT (indx > 0); | |
2276 | sym.st_shndx = indx; | |
2277 | ||
2278 | bfd_elf64_swap_symbol_out (output_bfd, &sym, | |
2279 | (PTR) (((Elf64_External_Sym *) | |
2280 | sdynsym->contents) | |
2281 | + elf_section_data (s)->dynindx)); | |
2282 | ||
2283 | ++c; | |
2284 | } | |
2285 | ||
2286 | /* Set the sh_info field of the output .dynsym section to the | |
2287 | index of the first global symbol. */ | |
2288 | elf_section_data (sdynsym->output_section)->this_hdr.sh_info = c + 1; | |
d3366d23 DE |
2289 | } |
2290 | ||
2291 | return true; | |
2292 | } | |
ccbab31d RH |
2293 | \f |
2294 | /* Functions for dealing with the e_flags field. */ | |
2295 | ||
2296 | /* Merge backend specific data from an object file to the output | |
2297 | object file when linking. */ | |
2298 | ||
2299 | static boolean | |
2300 | sparc64_elf_merge_private_bfd_data (ibfd, obfd) | |
2301 | bfd *ibfd; | |
2302 | bfd *obfd; | |
2303 | { | |
2304 | boolean error; | |
2305 | flagword new_flags, old_flags; | |
2306 | int new_mm, old_mm; | |
2307 | ||
2308 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
2309 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
2310 | return true; | |
2311 | ||
2312 | new_flags = elf_elfheader (ibfd)->e_flags; | |
2313 | old_flags = elf_elfheader (obfd)->e_flags; | |
2314 | ||
2315 | if (!elf_flags_init (obfd)) /* First call, no flags set */ | |
2316 | { | |
2317 | elf_flags_init (obfd) = true; | |
2318 | elf_elfheader (obfd)->e_flags = new_flags; | |
2319 | } | |
2320 | ||
2321 | else if (new_flags == old_flags) /* Compatible flags are ok */ | |
2322 | ; | |
2323 | ||
2324 | else /* Incompatible flags */ | |
2325 | { | |
2326 | error = false; | |
2327 | ||
2328 | old_flags |= (new_flags & (EF_SPARC_SUN_US1|EF_SPARC_HAL_R1)); | |
2329 | new_flags |= (old_flags & (EF_SPARC_SUN_US1|EF_SPARC_HAL_R1)); | |
2330 | if ((old_flags & (EF_SPARC_SUN_US1|EF_SPARC_HAL_R1)) == | |
2331 | (EF_SPARC_SUN_US1|EF_SPARC_HAL_R1)) | |
2332 | { | |
2333 | error = true; | |
2334 | (*_bfd_error_handler) | |
2335 | ("%s: linking UltraSPARC specific with HAL specific code", | |
2336 | bfd_get_filename (ibfd)); | |
2337 | } | |
2338 | ||
2339 | /* Choose the most restrictive memory ordering */ | |
2340 | old_mm = (old_flags & EF_SPARCV9_MM); | |
2341 | new_mm = (new_flags & EF_SPARCV9_MM); | |
2342 | old_flags &= ~EF_SPARCV9_MM; | |
2343 | new_flags &= ~EF_SPARCV9_MM; | |
2344 | if (new_mm < old_mm) old_mm = new_mm; | |
2345 | old_flags |= old_mm; | |
2346 | new_flags |= old_mm; | |
2347 | ||
2348 | /* Warn about any other mismatches */ | |
2349 | if (new_flags != old_flags) | |
2350 | { | |
2351 | error = true; | |
2352 | (*_bfd_error_handler) | |
2353 | ("%s: uses different e_flags (0x%lx) fields than previous modules (0x%lx)", | |
2354 | bfd_get_filename (ibfd), (long)new_flags, (long)old_flags); | |
2355 | } | |
2356 | ||
2357 | elf_elfheader (obfd)->e_flags = old_flags; | |
2358 | ||
2359 | if (error) | |
2360 | { | |
2361 | bfd_set_error (bfd_error_bad_value); | |
2362 | return false; | |
2363 | } | |
2364 | } | |
2365 | return true; | |
2366 | } | |
d3366d23 | 2367 | |
65c2dd6e | 2368 | \f |
d3366d23 DE |
2369 | /* Set the right machine number for a SPARC64 ELF file. */ |
2370 | ||
2371 | static boolean | |
2372 | sparc64_elf_object_p (abfd) | |
2373 | bfd *abfd; | |
2374 | { | |
ccbab31d RH |
2375 | unsigned long mach = bfd_mach_sparc_v9; |
2376 | ||
2377 | if (elf_elfheader (abfd)->e_flags & EF_SPARC_SUN_US1) | |
2378 | mach = bfd_mach_sparc_v9a; | |
2379 | return bfd_default_set_arch_mach (abfd, bfd_arch_sparc, mach); | |
789776d7 KR |
2380 | } |
2381 | ||
2382 | #define TARGET_BIG_SYM bfd_elf64_sparc_vec | |
2383 | #define TARGET_BIG_NAME "elf64-sparc" | |
2384 | #define ELF_ARCH bfd_arch_sparc | |
d3366d23 DE |
2385 | #define ELF_MACHINE_CODE EM_SPARC64 |
2386 | #define ELF_MAXPAGESIZE 0x100000 | |
2387 | ||
65c2dd6e RH |
2388 | #define elf_info_to_howto \ |
2389 | sparc64_elf_info_to_howto | |
2390 | #define bfd_elf64_bfd_reloc_type_lookup \ | |
2391 | sparc64_elf_reloc_type_lookup | |
2392 | ||
2393 | #define elf_backend_create_dynamic_sections \ | |
2394 | _bfd_elf_create_dynamic_sections | |
2395 | #define elf_backend_check_relocs \ | |
2396 | sparc64_elf_check_relocs | |
2397 | #define elf_backend_adjust_dynamic_symbol \ | |
2398 | sparc64_elf_adjust_dynamic_symbol | |
2399 | #define elf_backend_size_dynamic_sections \ | |
2400 | sparc64_elf_size_dynamic_sections | |
2401 | #define elf_backend_relocate_section \ | |
2402 | sparc64_elf_relocate_section | |
2403 | #define elf_backend_finish_dynamic_symbol \ | |
2404 | sparc64_elf_finish_dynamic_symbol | |
2405 | #define elf_backend_finish_dynamic_sections \ | |
2406 | sparc64_elf_finish_dynamic_sections | |
2407 | ||
ccbab31d RH |
2408 | #define bfd_elf64_bfd_merge_private_bfd_data \ |
2409 | sparc64_elf_merge_private_bfd_data | |
2410 | ||
65c2dd6e RH |
2411 | #define elf_backend_object_p \ |
2412 | sparc64_elf_object_p | |
2413 | ||
2414 | #define elf_backend_want_got_plt 1 | |
2415 | #define elf_backend_plt_readonly 1 | |
2416 | #define elf_backend_want_plt_sym 1 | |
2417 | #define elf_backend_plt_alignment 5 | |
789776d7 KR |
2418 | |
2419 | #include "elf64-target.h" |