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