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