Jakub Jelinek <jj@ultra.linux.cz>
[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 return false;
1130 }
1131
1132 if (! bfd_elf64_add_dynamic_entry (info, DT_RELA, 0)
1133 || ! bfd_elf64_add_dynamic_entry (info, DT_RELASZ, 0)
1134 || ! bfd_elf64_add_dynamic_entry (info, DT_RELAENT,
1135 sizeof (Elf64_External_Rela)))
1136 return false;
1137
1138 if (reltext)
1139 {
1140 if (! bfd_elf64_add_dynamic_entry (info, DT_TEXTREL, 0))
1141 return false;
1142 }
1143 }
1144
1145 return true;
1146 }
1147 \f
1148 /* Relocate a SPARC64 ELF section. */
1149
1150 static boolean
1151 sparc64_elf_relocate_section (output_bfd, info, input_bfd, input_section,
1152 contents, relocs, local_syms, local_sections)
1153 bfd *output_bfd;
1154 struct bfd_link_info *info;
1155 bfd *input_bfd;
1156 asection *input_section;
1157 bfd_byte *contents;
1158 Elf_Internal_Rela *relocs;
1159 Elf_Internal_Sym *local_syms;
1160 asection **local_sections;
1161 {
1162 bfd *dynobj;
1163 Elf_Internal_Shdr *symtab_hdr;
1164 struct elf_link_hash_entry **sym_hashes;
1165 bfd_vma *local_got_offsets;
1166 bfd_vma got_base;
1167 asection *sgot;
1168 asection *splt;
1169 asection *sreloc;
1170 Elf_Internal_Rela *rel;
1171 Elf_Internal_Rela *relend;
1172
1173 dynobj = elf_hash_table (info)->dynobj;
1174 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1175 sym_hashes = elf_sym_hashes (input_bfd);
1176 local_got_offsets = elf_local_got_offsets (input_bfd);
1177
1178 if (elf_hash_table(info)->hgot == NULL)
1179 got_base = 0;
1180 else
1181 got_base = elf_hash_table (info)->hgot->root.u.def.value;
1182
1183 sgot = splt = sreloc = NULL;
1184
1185 rel = relocs;
1186 relend = relocs + input_section->reloc_count;
1187 for (; rel < relend; rel++)
1188 {
1189 int r_type;
1190 reloc_howto_type *howto;
1191 long r_symndx;
1192 struct elf_link_hash_entry *h;
1193 Elf_Internal_Sym *sym;
1194 asection *sec;
1195 bfd_vma relocation;
1196 bfd_reloc_status_type r;
1197
1198 r_type = ELF64_R_TYPE (rel->r_info);
1199 if (r_type < 0 || r_type >= (int) R_SPARC_max_std)
1200 {
1201 bfd_set_error (bfd_error_bad_value);
1202 return false;
1203 }
1204 howto = sparc64_elf_howto_table + r_type;
1205
1206 r_symndx = ELF64_R_SYM (rel->r_info);
1207
1208 if (info->relocateable)
1209 {
1210 /* This is a relocateable link. We don't have to change
1211 anything, unless the reloc is against a section symbol,
1212 in which case we have to adjust according to where the
1213 section symbol winds up in the output section. */
1214 if (r_symndx < symtab_hdr->sh_info)
1215 {
1216 sym = local_syms + r_symndx;
1217 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
1218 {
1219 sec = local_sections[r_symndx];
1220 rel->r_addend += sec->output_offset + sym->st_value;
1221 }
1222 }
1223
1224 continue;
1225 }
1226
1227 /* This is a final link. */
1228 h = NULL;
1229 sym = NULL;
1230 sec = NULL;
1231 if (r_symndx < symtab_hdr->sh_info)
1232 {
1233 sym = local_syms + r_symndx;
1234 sec = local_sections[r_symndx];
1235 relocation = (sec->output_section->vma
1236 + sec->output_offset
1237 + sym->st_value);
1238 }
1239 else
1240 {
1241 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1242 while (h->root.type == bfd_link_hash_indirect
1243 || h->root.type == bfd_link_hash_warning)
1244 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1245 if (h->root.type == bfd_link_hash_defined
1246 || h->root.type == bfd_link_hash_defweak)
1247 {
1248 boolean skip_it = false;
1249 sec = h->root.u.def.section;
1250
1251 switch (r_type)
1252 {
1253 case R_SPARC_WPLT30:
1254 case R_SPARC_PLT32:
1255 case R_SPARC_HIPLT22:
1256 case R_SPARC_LOPLT10:
1257 case R_SPARC_PCPLT32:
1258 case R_SPARC_PCPLT22:
1259 case R_SPARC_PCPLT10:
1260 case R_SPARC_PLT64:
1261 if (h->plt.offset != (bfd_vma) -1)
1262 skip_it = true;
1263 break;
1264
1265 case R_SPARC_GOT10:
1266 case R_SPARC_GOT13:
1267 case R_SPARC_GOT22:
1268 if (elf_hash_table(info)->dynamic_sections_created
1269 && (!info->shared
1270 || (!info->symbolic && h->dynindx != -1)
1271 || !(h->elf_link_hash_flags
1272 & ELF_LINK_HASH_DEF_REGULAR)))
1273 skip_it = true;
1274 break;
1275
1276 case R_SPARC_PC10:
1277 case R_SPARC_PC22:
1278 case R_SPARC_PC_HH22:
1279 case R_SPARC_PC_HM10:
1280 case R_SPARC_PC_LM22:
1281 if (!strcmp(h->root.root.string, "_GLOBAL_OFFSET_TABLE_"))
1282 break;
1283 /* FALLTHRU */
1284
1285 case R_SPARC_8:
1286 case R_SPARC_16:
1287 case R_SPARC_32:
1288 case R_SPARC_DISP8:
1289 case R_SPARC_DISP16:
1290 case R_SPARC_DISP32:
1291 case R_SPARC_WDISP30:
1292 case R_SPARC_WDISP22:
1293 case R_SPARC_HI22:
1294 case R_SPARC_22:
1295 case R_SPARC_13:
1296 case R_SPARC_LO10:
1297 case R_SPARC_UA32:
1298 case R_SPARC_10:
1299 case R_SPARC_11:
1300 case R_SPARC_64:
1301 case R_SPARC_OLO10:
1302 case R_SPARC_HH22:
1303 case R_SPARC_HM10:
1304 case R_SPARC_LM22:
1305 case R_SPARC_WDISP19:
1306 case R_SPARC_WDISP16:
1307 case R_SPARC_7:
1308 case R_SPARC_5:
1309 case R_SPARC_6:
1310 case R_SPARC_DISP64:
1311 case R_SPARC_HIX22:
1312 case R_SPARC_LOX10:
1313 case R_SPARC_H44:
1314 case R_SPARC_M44:
1315 case R_SPARC_L44:
1316 case R_SPARC_UA64:
1317 case R_SPARC_UA16:
1318 if (info->shared
1319 && ((!info->symbolic && h->dynindx != -1)
1320 || !(h->elf_link_hash_flags
1321 & ELF_LINK_HASH_DEF_REGULAR)))
1322 skip_it = true;
1323 break;
1324 }
1325
1326 if (skip_it)
1327 {
1328 /* In these cases, we don't need the relocation
1329 value. We check specially because in some
1330 obscure cases sec->output_section will be NULL. */
1331 relocation = 0;
1332 }
1333 else
1334 {
1335 relocation = (h->root.u.def.value
1336 + sec->output_section->vma
1337 + sec->output_offset);
1338 }
1339 }
1340 else if (h->root.type == bfd_link_hash_undefweak)
1341 relocation = 0;
1342 else if (info->shared && !info->symbolic && !info->no_undefined)
1343 relocation = 0;
1344 else
1345 {
1346 if (! ((*info->callbacks->undefined_symbol)
1347 (info, h->root.root.string, input_bfd,
1348 input_section, rel->r_offset)))
1349 return false;
1350 relocation = 0;
1351 }
1352 }
1353
1354 /* When generating a shared object, these relocations are copied
1355 into the output file to be resolved at run time. */
1356 if (info->shared && (input_section->flags & SEC_ALLOC))
1357 {
1358 switch (r_type)
1359 {
1360 case R_SPARC_PC10:
1361 case R_SPARC_PC22:
1362 case R_SPARC_PC_HH22:
1363 case R_SPARC_PC_HM10:
1364 case R_SPARC_PC_LM22:
1365 if (h != NULL
1366 && !strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_"))
1367 break;
1368 /* Fall through. */
1369 case R_SPARC_DISP8:
1370 case R_SPARC_DISP16:
1371 case R_SPARC_DISP32:
1372 case R_SPARC_WDISP30:
1373 case R_SPARC_WDISP22:
1374 case R_SPARC_WDISP19:
1375 case R_SPARC_WDISP16:
1376 case R_SPARC_DISP64:
1377 if (h == NULL)
1378 break;
1379 /* Fall through. */
1380 case R_SPARC_8:
1381 case R_SPARC_16:
1382 case R_SPARC_32:
1383 case R_SPARC_HI22:
1384 case R_SPARC_22:
1385 case R_SPARC_13:
1386 case R_SPARC_LO10:
1387 case R_SPARC_UA32:
1388 case R_SPARC_10:
1389 case R_SPARC_11:
1390 case R_SPARC_64:
1391 case R_SPARC_OLO10:
1392 case R_SPARC_HH22:
1393 case R_SPARC_HM10:
1394 case R_SPARC_LM22:
1395 case R_SPARC_7:
1396 case R_SPARC_5:
1397 case R_SPARC_6:
1398 case R_SPARC_HIX22:
1399 case R_SPARC_LOX10:
1400 case R_SPARC_H44:
1401 case R_SPARC_M44:
1402 case R_SPARC_L44:
1403 case R_SPARC_UA64:
1404 case R_SPARC_UA16:
1405 {
1406 Elf_Internal_Rela outrel;
1407 boolean skip;
1408
1409 if (sreloc == NULL)
1410 {
1411 const char *name =
1412 (bfd_elf_string_from_elf_section
1413 (input_bfd,
1414 elf_elfheader (input_bfd)->e_shstrndx,
1415 elf_section_data (input_section)->rel_hdr.sh_name));
1416
1417 if (name == NULL)
1418 return false;
1419
1420 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
1421 && strcmp (bfd_get_section_name(input_bfd,
1422 input_section),
1423 name + 5) == 0);
1424
1425 sreloc = bfd_get_section_by_name (dynobj, name);
1426 BFD_ASSERT (sreloc != NULL);
1427 }
1428
1429 skip = false;
1430
1431 if (elf_section_data (input_section)->stab_info == NULL)
1432 outrel.r_offset = rel->r_offset;
1433 else
1434 {
1435 bfd_vma off;
1436
1437 off = (_bfd_stab_section_offset
1438 (output_bfd, &elf_hash_table (info)->stab_info,
1439 input_section,
1440 &elf_section_data (input_section)->stab_info,
1441 rel->r_offset));
1442 if (off == MINUS_ONE)
1443 skip = true;
1444 outrel.r_offset = off;
1445 }
1446
1447 outrel.r_offset += (input_section->output_section->vma
1448 + input_section->output_offset);
1449
1450 /* Optimize unaligned reloc usage now that we know where
1451 it finally resides. */
1452 switch (r_type)
1453 {
1454 case R_SPARC_16:
1455 if (outrel.r_offset & 1) r_type = R_SPARC_UA16;
1456 break;
1457 case R_SPARC_UA16:
1458 if (!(outrel.r_offset & 1)) r_type = R_SPARC_16;
1459 break;
1460 case R_SPARC_32:
1461 if (outrel.r_offset & 3) r_type = R_SPARC_UA32;
1462 break;
1463 case R_SPARC_UA32:
1464 if (!(outrel.r_offset & 3)) r_type = R_SPARC_32;
1465 break;
1466 case R_SPARC_64:
1467 if (outrel.r_offset & 7) r_type = R_SPARC_UA64;
1468 break;
1469 case R_SPARC_UA64:
1470 if (!(outrel.r_offset & 7)) r_type = R_SPARC_64;
1471 break;
1472 }
1473
1474 if (skip)
1475 memset (&outrel, 0, sizeof outrel);
1476 /* h->dynindx may be -1 if the symbol was marked to
1477 become local. */
1478 else if (h != NULL
1479 && ((! info->symbolic && h->dynindx != -1)
1480 || (h->elf_link_hash_flags
1481 & ELF_LINK_HASH_DEF_REGULAR) == 0))
1482 {
1483 BFD_ASSERT (h->dynindx != -1);
1484 outrel.r_info = ELF64_R_INFO (h->dynindx, r_type);
1485 outrel.r_addend = rel->r_addend;
1486 }
1487 else
1488 {
1489 if (r_type == R_SPARC_64)
1490 {
1491 outrel.r_info = ELF64_R_INFO (0, R_SPARC_RELATIVE);
1492 outrel.r_addend = relocation + rel->r_addend;
1493 }
1494 else
1495 {
1496 long indx;
1497
1498 if (h == NULL)
1499 sec = local_sections[r_symndx];
1500 else
1501 {
1502 BFD_ASSERT (h->root.type == bfd_link_hash_defined
1503 || (h->root.type
1504 == bfd_link_hash_defweak));
1505 sec = h->root.u.def.section;
1506 }
1507 if (sec != NULL && bfd_is_abs_section (sec))
1508 indx = 0;
1509 else if (sec == NULL || sec->owner == NULL)
1510 {
1511 bfd_set_error (bfd_error_bad_value);
1512 return false;
1513 }
1514 else
1515 {
1516 asection *osec;
1517
1518 osec = sec->output_section;
1519 indx = elf_section_data (osec)->dynindx;
1520
1521 /* FIXME: we really should be able to link non-pic
1522 shared libraries. */
1523 if (indx == 0)
1524 {
1525 BFD_FAIL ();
1526 (*_bfd_error_handler)
1527 (_("%s: probably compiled without -fPIC?"),
1528 bfd_get_filename (input_bfd));
1529 bfd_set_error (bfd_error_bad_value);
1530 return false;
1531 }
1532 }
1533
1534 outrel.r_info = ELF64_R_INFO (indx, r_type);
1535
1536 /* For non-RELATIVE dynamic relocations, we keep the
1537 same symbol, and so generally the same addend. But
1538 we do need to adjust those relocations referencing
1539 sections. */
1540 outrel.r_addend = rel->r_addend;
1541 if (r_symndx < symtab_hdr->sh_info
1542 && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
1543 outrel.r_addend += sec->output_offset+sym->st_value;
1544 }
1545 }
1546
1547 bfd_elf64_swap_reloca_out (output_bfd, &outrel,
1548 (((Elf64_External_Rela *)
1549 sreloc->contents)
1550 + sreloc->reloc_count));
1551 ++sreloc->reloc_count;
1552
1553 /* This reloc will be computed at runtime, so there's no
1554 need to do anything now, unless this is a RELATIVE
1555 reloc in an unallocated section. */
1556 if (skip
1557 || (input_section->flags & SEC_ALLOC) != 0
1558 || ELF64_R_TYPE (outrel.r_info) != R_SPARC_RELATIVE)
1559 continue;
1560 }
1561 break;
1562 }
1563 }
1564
1565 switch (r_type)
1566 {
1567 case R_SPARC_GOT10:
1568 case R_SPARC_GOT13:
1569 case R_SPARC_GOT22:
1570 /* Relocation is to the entry for this symbol in the global
1571 offset table. */
1572 if (sgot == NULL)
1573 {
1574 sgot = bfd_get_section_by_name (dynobj, ".got");
1575 BFD_ASSERT (sgot != NULL);
1576 }
1577
1578 if (h != NULL)
1579 {
1580 bfd_vma off = h->got.offset;
1581 BFD_ASSERT (off != (bfd_vma) -1);
1582
1583 if (! elf_hash_table (info)->dynamic_sections_created
1584 || (info->shared
1585 && (info->symbolic || h->dynindx == -1)
1586 && (h->elf_link_hash_flags
1587 & ELF_LINK_HASH_DEF_REGULAR)))
1588 {
1589 /* This is actually a static link, or it is a -Bsymbolic
1590 link and the symbol is defined locally, or the symbol
1591 was forced to be local because of a version file. We
1592 must initialize this entry in the global offset table.
1593 Since the offset must always be a multiple of 8, we
1594 use the least significant bit to record whether we
1595 have initialized it already.
1596
1597 When doing a dynamic link, we create a .rela.got
1598 relocation entry to initialize the value. This is
1599 done in the finish_dynamic_symbol routine. */
1600
1601 if ((off & 1) != 0)
1602 off &= ~1;
1603 else
1604 {
1605 bfd_put_64 (output_bfd, relocation,
1606 sgot->contents + off);
1607 h->got.offset |= 1;
1608 }
1609 }
1610 relocation = sgot->output_offset + off - got_base;
1611 }
1612 else
1613 {
1614 bfd_vma off;
1615
1616 BFD_ASSERT (local_got_offsets != NULL);
1617 off = local_got_offsets[r_symndx];
1618 BFD_ASSERT (off != (bfd_vma) -1);
1619
1620 /* The offset must always be a multiple of 8. We use
1621 the least significant bit to record whether we have
1622 already processed this entry. */
1623 if ((off & 1) != 0)
1624 off &= ~1;
1625 else
1626 {
1627 bfd_put_64 (output_bfd, relocation, sgot->contents + off);
1628 local_got_offsets[r_symndx] |= 1;
1629
1630 if (info->shared)
1631 {
1632 asection *srelgot;
1633 Elf_Internal_Rela outrel;
1634
1635 /* We need to generate a R_SPARC_RELATIVE reloc
1636 for the dynamic linker. */
1637 srelgot = bfd_get_section_by_name(dynobj, ".rela.got");
1638 BFD_ASSERT (srelgot != NULL);
1639
1640 outrel.r_offset = (sgot->output_section->vma
1641 + sgot->output_offset
1642 + off);
1643 outrel.r_info = ELF64_R_INFO (0, R_SPARC_RELATIVE);
1644 outrel.r_addend = relocation;
1645 bfd_elf64_swap_reloca_out (output_bfd, &outrel,
1646 (((Elf64_External_Rela *)
1647 srelgot->contents)
1648 + srelgot->reloc_count));
1649 ++srelgot->reloc_count;
1650 }
1651 }
1652 relocation = sgot->output_offset + off - got_base;
1653 }
1654 goto do_default;
1655
1656 case R_SPARC_WPLT30:
1657 case R_SPARC_PLT32:
1658 case R_SPARC_HIPLT22:
1659 case R_SPARC_LOPLT10:
1660 case R_SPARC_PCPLT32:
1661 case R_SPARC_PCPLT22:
1662 case R_SPARC_PCPLT10:
1663 case R_SPARC_PLT64:
1664 /* Relocation is to the entry for this symbol in the
1665 procedure linkage table. */
1666 BFD_ASSERT (h != NULL);
1667
1668 if (h->plt.offset == (bfd_vma) -1)
1669 {
1670 /* We didn't make a PLT entry for this symbol. This
1671 happens when statically linking PIC code, or when
1672 using -Bsymbolic. */
1673 goto do_default;
1674 }
1675
1676 if (splt == NULL)
1677 {
1678 splt = bfd_get_section_by_name (dynobj, ".plt");
1679 BFD_ASSERT (splt != NULL);
1680 }
1681
1682 relocation = (splt->output_section->vma
1683 + splt->output_offset
1684 + sparc64_elf_plt_entry_offset (h->plt.offset));
1685 goto do_default;
1686
1687 case R_SPARC_OLO10:
1688 {
1689 bfd_vma x;
1690
1691 relocation += rel->r_addend;
1692 relocation = (relocation & 0x3ff) + ELF64_R_TYPE_DATA (rel->r_info);
1693
1694 x = bfd_get_32 (input_bfd, contents + rel->r_offset);
1695 x = (x & ~0x1fff) | (relocation & 0x1fff);
1696 bfd_put_32 (input_bfd, x, contents + rel->r_offset);
1697
1698 r = bfd_check_overflow (howto->complain_on_overflow,
1699 howto->bitsize, howto->rightshift,
1700 bfd_arch_bits_per_address (input_bfd),
1701 relocation);
1702 }
1703 break;
1704
1705 case R_SPARC_WDISP16:
1706 {
1707 bfd_vma x;
1708
1709 relocation += rel->r_addend;
1710 /* Adjust for pc-relative-ness. */
1711 relocation -= (input_section->output_section->vma
1712 + input_section->output_offset);
1713 relocation -= rel->r_offset;
1714
1715 x = bfd_get_32 (input_bfd, contents + rel->r_offset);
1716 x = (x & ~0x303fff) | ((((relocation >> 2) & 0xc000) << 6)
1717 | ((relocation >> 2) & 0x3fff));
1718 bfd_put_32 (input_bfd, x, contents + rel->r_offset);
1719
1720 r = bfd_check_overflow (howto->complain_on_overflow,
1721 howto->bitsize, howto->rightshift,
1722 bfd_arch_bits_per_address (input_bfd),
1723 relocation);
1724 }
1725 break;
1726
1727 case R_SPARC_HIX22:
1728 {
1729 bfd_vma x;
1730
1731 relocation += rel->r_addend;
1732 relocation = relocation ^ MINUS_ONE;
1733
1734 x = bfd_get_32 (input_bfd, contents + rel->r_offset);
1735 x = (x & ~0x3fffff) | ((relocation >> 10) & 0x3fffff);
1736 bfd_put_32 (input_bfd, x, contents + rel->r_offset);
1737
1738 r = bfd_check_overflow (howto->complain_on_overflow,
1739 howto->bitsize, howto->rightshift,
1740 bfd_arch_bits_per_address (input_bfd),
1741 relocation);
1742 }
1743 break;
1744
1745 case R_SPARC_LOX10:
1746 {
1747 bfd_vma x;
1748
1749 relocation += rel->r_addend;
1750 relocation = (relocation & 0x3ff) | 0x1c00;
1751
1752 x = bfd_get_32 (input_bfd, contents + rel->r_offset);
1753 x = (x & ~0x1fff) | relocation;
1754 bfd_put_32 (input_bfd, x, contents + rel->r_offset);
1755
1756 r = bfd_reloc_ok;
1757 }
1758 break;
1759
1760 default:
1761 do_default:
1762 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1763 contents, rel->r_offset,
1764 relocation, rel->r_addend);
1765 break;
1766 }
1767
1768 switch (r)
1769 {
1770 case bfd_reloc_ok:
1771 break;
1772
1773 default:
1774 case bfd_reloc_outofrange:
1775 abort ();
1776
1777 case bfd_reloc_overflow:
1778 {
1779 const char *name;
1780
1781 if (h != NULL)
1782 {
1783 if (h->root.type == bfd_link_hash_undefweak
1784 && howto->pc_relative)
1785 {
1786 /* Assume this is a call protected by other code that
1787 detect the symbol is undefined. If this is the case,
1788 we can safely ignore the overflow. If not, the
1789 program is hosed anyway, and a little warning isn't
1790 going to help. */
1791 break;
1792 }
1793
1794 name = h->root.root.string;
1795 }
1796 else
1797 {
1798 name = (bfd_elf_string_from_elf_section
1799 (input_bfd,
1800 symtab_hdr->sh_link,
1801 sym->st_name));
1802 if (name == NULL)
1803 return false;
1804 if (*name == '\0')
1805 name = bfd_section_name (input_bfd, sec);
1806 }
1807 if (! ((*info->callbacks->reloc_overflow)
1808 (info, name, howto->name, (bfd_vma) 0,
1809 input_bfd, input_section, rel->r_offset)))
1810 return false;
1811 }
1812 break;
1813 }
1814 }
1815
1816 return true;
1817 }
1818
1819 /* Finish up dynamic symbol handling. We set the contents of various
1820 dynamic sections here. */
1821
1822 static boolean
1823 sparc64_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
1824 bfd *output_bfd;
1825 struct bfd_link_info *info;
1826 struct elf_link_hash_entry *h;
1827 Elf_Internal_Sym *sym;
1828 {
1829 bfd *dynobj;
1830
1831 dynobj = elf_hash_table (info)->dynobj;
1832
1833 if (h->plt.offset != (bfd_vma) -1)
1834 {
1835 asection *splt;
1836 asection *srela;
1837 Elf_Internal_Rela rela;
1838
1839 /* This symbol has an entry in the PLT. Set it up. */
1840
1841 BFD_ASSERT (h->dynindx != -1);
1842
1843 splt = bfd_get_section_by_name (dynobj, ".plt");
1844 srela = bfd_get_section_by_name (dynobj, ".rela.plt");
1845 BFD_ASSERT (splt != NULL && srela != NULL);
1846
1847 /* Fill in the entry in the .rela.plt section. */
1848
1849 if (h->plt.offset < LARGE_PLT_THRESHOLD)
1850 {
1851 rela.r_offset = sparc64_elf_plt_entry_offset (h->plt.offset);
1852 rela.r_addend = 0;
1853 }
1854 else
1855 {
1856 int max = splt->_raw_size / PLT_ENTRY_SIZE;
1857 rela.r_offset = sparc64_elf_plt_ptr_offset (h->plt.offset, max);
1858 rela.r_addend = -(sparc64_elf_plt_entry_offset (h->plt.offset) + 4);
1859 }
1860 rela.r_offset += (splt->output_section->vma + splt->output_offset);
1861 rela.r_info = ELF64_R_INFO (h->dynindx, R_SPARC_JMP_SLOT);
1862
1863 bfd_elf64_swap_reloca_out (output_bfd, &rela,
1864 ((Elf64_External_Rela *) srela->contents
1865 + h->plt.offset));
1866
1867 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1868 {
1869 /* Mark the symbol as undefined, rather than as defined in
1870 the .plt section. Leave the value alone. */
1871 sym->st_shndx = SHN_UNDEF;
1872 }
1873 }
1874
1875 if (h->got.offset != (bfd_vma) -1)
1876 {
1877 asection *sgot;
1878 asection *srela;
1879 Elf_Internal_Rela rela;
1880
1881 /* This symbol has an entry in the GOT. Set it up. */
1882
1883 sgot = bfd_get_section_by_name (dynobj, ".got");
1884 srela = bfd_get_section_by_name (dynobj, ".rela.got");
1885 BFD_ASSERT (sgot != NULL && srela != NULL);
1886
1887 rela.r_offset = (sgot->output_section->vma
1888 + sgot->output_offset
1889 + (h->got.offset &~ 1));
1890
1891 /* If this is a -Bsymbolic link, and the symbol is defined
1892 locally, we just want to emit a RELATIVE reloc. Likewise if
1893 the symbol was forced to be local because of a version file.
1894 The entry in the global offset table will already have been
1895 initialized in the relocate_section function. */
1896 if (info->shared
1897 && (info->symbolic || h->dynindx == -1)
1898 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
1899 {
1900 asection *sec = h->root.u.def.section;
1901 rela.r_info = ELF64_R_INFO (0, R_SPARC_RELATIVE);
1902 rela.r_addend = (h->root.u.def.value
1903 + sec->output_section->vma
1904 + sec->output_offset);
1905 }
1906 else
1907 {
1908 bfd_put_64 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
1909 rela.r_info = ELF64_R_INFO (h->dynindx, R_SPARC_GLOB_DAT);
1910 rela.r_addend = 0;
1911 }
1912
1913 bfd_elf64_swap_reloca_out (output_bfd, &rela,
1914 ((Elf64_External_Rela *) srela->contents
1915 + srela->reloc_count));
1916 ++srela->reloc_count;
1917 }
1918
1919 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
1920 {
1921 asection *s;
1922 Elf_Internal_Rela rela;
1923
1924 /* This symbols needs a copy reloc. Set it up. */
1925
1926 BFD_ASSERT (h->dynindx != -1);
1927
1928 s = bfd_get_section_by_name (h->root.u.def.section->owner,
1929 ".rela.bss");
1930 BFD_ASSERT (s != NULL);
1931
1932 rela.r_offset = (h->root.u.def.value
1933 + h->root.u.def.section->output_section->vma
1934 + h->root.u.def.section->output_offset);
1935 rela.r_info = ELF64_R_INFO (h->dynindx, R_SPARC_COPY);
1936 rela.r_addend = 0;
1937 bfd_elf64_swap_reloca_out (output_bfd, &rela,
1938 ((Elf64_External_Rela *) s->contents
1939 + s->reloc_count));
1940 ++s->reloc_count;
1941 }
1942
1943 /* Mark some specially defined symbols as absolute. */
1944 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
1945 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
1946 || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0)
1947 sym->st_shndx = SHN_ABS;
1948
1949 return true;
1950 }
1951
1952 /* Finish up the dynamic sections. */
1953
1954 static boolean
1955 sparc64_elf_finish_dynamic_sections (output_bfd, info)
1956 bfd *output_bfd;
1957 struct bfd_link_info *info;
1958 {
1959 bfd *dynobj;
1960 asection *sdyn;
1961 asection *sgot;
1962
1963 dynobj = elf_hash_table (info)->dynobj;
1964
1965 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
1966
1967 if (elf_hash_table (info)->dynamic_sections_created)
1968 {
1969 asection *splt;
1970 Elf64_External_Dyn *dyncon, *dynconend;
1971
1972 splt = bfd_get_section_by_name (dynobj, ".plt");
1973 BFD_ASSERT (splt != NULL && sdyn != NULL);
1974
1975 dyncon = (Elf64_External_Dyn *) sdyn->contents;
1976 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
1977 for (; dyncon < dynconend; dyncon++)
1978 {
1979 Elf_Internal_Dyn dyn;
1980 const char *name;
1981 boolean size;
1982
1983 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
1984
1985 switch (dyn.d_tag)
1986 {
1987 case DT_PLTGOT: name = ".plt"; size = false; break;
1988 case DT_PLTRELSZ: name = ".rela.plt"; size = true; break;
1989 case DT_JMPREL: name = ".rela.plt"; size = false; break;
1990 default: name = NULL; size = false; break;
1991 }
1992
1993 if (name != NULL)
1994 {
1995 asection *s;
1996
1997 s = bfd_get_section_by_name (output_bfd, name);
1998 if (s == NULL)
1999 dyn.d_un.d_val = 0;
2000 else
2001 {
2002 if (! size)
2003 dyn.d_un.d_ptr = s->vma;
2004 else
2005 {
2006 if (s->_cooked_size != 0)
2007 dyn.d_un.d_val = s->_cooked_size;
2008 else
2009 dyn.d_un.d_val = s->_raw_size;
2010 }
2011 }
2012 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2013 }
2014 }
2015
2016 /* Initialize the contents of the .plt section. */
2017 if (splt->_raw_size > 0)
2018 {
2019 sparc64_elf_build_plt(output_bfd, splt->contents,
2020 splt->_raw_size / PLT_ENTRY_SIZE);
2021 }
2022
2023 elf_section_data (splt->output_section)->this_hdr.sh_entsize =
2024 PLT_ENTRY_SIZE;
2025 }
2026
2027 /* Set the first entry in the global offset table to the address of
2028 the dynamic section. */
2029 sgot = bfd_get_section_by_name (dynobj, ".got");
2030 BFD_ASSERT (sgot != NULL);
2031 if (sgot->_raw_size > 0)
2032 {
2033 if (sdyn == NULL)
2034 bfd_put_64 (output_bfd, (bfd_vma) 0, sgot->contents);
2035 else
2036 bfd_put_64 (output_bfd,
2037 sdyn->output_section->vma + sdyn->output_offset,
2038 sgot->contents);
2039 }
2040
2041 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 8;
2042
2043 return true;
2044 }
2045 \f
2046 /* Functions for dealing with the e_flags field. */
2047
2048 /* Merge backend specific data from an object file to the output
2049 object file when linking. */
2050
2051 static boolean
2052 sparc64_elf_merge_private_bfd_data (ibfd, obfd)
2053 bfd *ibfd;
2054 bfd *obfd;
2055 {
2056 boolean error;
2057 flagword new_flags, old_flags;
2058 int new_mm, old_mm;
2059
2060 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2061 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2062 return true;
2063
2064 new_flags = elf_elfheader (ibfd)->e_flags;
2065 old_flags = elf_elfheader (obfd)->e_flags;
2066
2067 if (!elf_flags_init (obfd)) /* First call, no flags set */
2068 {
2069 elf_flags_init (obfd) = true;
2070 elf_elfheader (obfd)->e_flags = new_flags;
2071 }
2072
2073 else if (new_flags == old_flags) /* Compatible flags are ok */
2074 ;
2075
2076 else /* Incompatible flags */
2077 {
2078 error = false;
2079
2080 old_flags |= (new_flags & (EF_SPARC_SUN_US1|EF_SPARC_HAL_R1));
2081 new_flags |= (old_flags & (EF_SPARC_SUN_US1|EF_SPARC_HAL_R1));
2082 if ((old_flags & (EF_SPARC_SUN_US1|EF_SPARC_HAL_R1)) ==
2083 (EF_SPARC_SUN_US1|EF_SPARC_HAL_R1))
2084 {
2085 error = true;
2086 (*_bfd_error_handler)
2087 (_("%s: linking UltraSPARC specific with HAL specific code"),
2088 bfd_get_filename (ibfd));
2089 }
2090
2091 /* Choose the most restrictive memory ordering */
2092 old_mm = (old_flags & EF_SPARCV9_MM);
2093 new_mm = (new_flags & EF_SPARCV9_MM);
2094 old_flags &= ~EF_SPARCV9_MM;
2095 new_flags &= ~EF_SPARCV9_MM;
2096 if (new_mm < old_mm) old_mm = new_mm;
2097 old_flags |= old_mm;
2098 new_flags |= old_mm;
2099
2100 /* Warn about any other mismatches */
2101 if (new_flags != old_flags)
2102 {
2103 error = true;
2104 (*_bfd_error_handler)
2105 (_("%s: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
2106 bfd_get_filename (ibfd), (long)new_flags, (long)old_flags);
2107 }
2108
2109 elf_elfheader (obfd)->e_flags = old_flags;
2110
2111 if (error)
2112 {
2113 bfd_set_error (bfd_error_bad_value);
2114 return false;
2115 }
2116 }
2117 return true;
2118 }
2119
2120 \f
2121 /* Set the right machine number for a SPARC64 ELF file. */
2122
2123 static boolean
2124 sparc64_elf_object_p (abfd)
2125 bfd *abfd;
2126 {
2127 unsigned long mach = bfd_mach_sparc_v9;
2128
2129 if (elf_elfheader (abfd)->e_flags & EF_SPARC_SUN_US1)
2130 mach = bfd_mach_sparc_v9a;
2131 return bfd_default_set_arch_mach (abfd, bfd_arch_sparc, mach);
2132 }
2133
2134 #define TARGET_BIG_SYM bfd_elf64_sparc_vec
2135 #define TARGET_BIG_NAME "elf64-sparc"
2136 #define ELF_ARCH bfd_arch_sparc
2137 #define ELF_MAXPAGESIZE 0x100000
2138
2139 /* This is the official ABI value. */
2140 #define ELF_MACHINE_CODE EM_SPARCV9
2141
2142 /* This is the value that we used before the ABI was released. */
2143 #define ELF_MACHINE_ALT1 EM_OLD_SPARCV9
2144
2145 #define elf_info_to_howto \
2146 sparc64_elf_info_to_howto
2147 #define bfd_elf64_bfd_reloc_type_lookup \
2148 sparc64_elf_reloc_type_lookup
2149
2150 #define elf_backend_create_dynamic_sections \
2151 _bfd_elf_create_dynamic_sections
2152 #define elf_backend_check_relocs \
2153 sparc64_elf_check_relocs
2154 #define elf_backend_adjust_dynamic_symbol \
2155 sparc64_elf_adjust_dynamic_symbol
2156 #define elf_backend_size_dynamic_sections \
2157 sparc64_elf_size_dynamic_sections
2158 #define elf_backend_relocate_section \
2159 sparc64_elf_relocate_section
2160 #define elf_backend_finish_dynamic_symbol \
2161 sparc64_elf_finish_dynamic_symbol
2162 #define elf_backend_finish_dynamic_sections \
2163 sparc64_elf_finish_dynamic_sections
2164
2165 #define bfd_elf64_bfd_merge_private_bfd_data \
2166 sparc64_elf_merge_private_bfd_data
2167
2168 #define elf_backend_object_p \
2169 sparc64_elf_object_p
2170
2171 #define elf_backend_want_got_plt 0
2172 #define elf_backend_plt_readonly 0
2173 #define elf_backend_want_plt_sym 1
2174
2175 /* Section 5.2.4 of the ABI specifies a 256-byte boundary for the table. */
2176 #define elf_backend_plt_alignment 8
2177
2178 #define elf_backend_got_header_size 8
2179 #define elf_backend_plt_header_size PLT_HEADER_SIZE
2180
2181 #include "elf64-target.h"
This page took 0.079928 seconds and 4 git commands to generate.