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