1 /* SPARC-specific support for 64-bit ELF
2 Copyright (C) 1993, 1995, 1996, 1997 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
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*/
30 #include "elf/sparc.h"
32 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value. */
33 #define MINUS_ONE (~ (bfd_vma) 0)
35 static reloc_howto_type *sparc64_elf_reloc_type_lookup
36 PARAMS ((bfd *, bfd_reloc_code_real_type));
37 static void sparc64_elf_info_to_howto
38 PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
40 static boolean sparc64_elf_check_relocs
41 PARAMS((bfd *, struct bfd_link_info *, asection *sec,
42 const Elf_Internal_Rela *));
43 static boolean sparc64_elf_adjust_dynamic_symbol
44 PARAMS((struct bfd_link_info *, struct elf_link_hash_entry *));
45 static boolean sparc64_elf_size_dynamic_sections
46 PARAMS((bfd *, struct bfd_link_info *));
47 static boolean sparc64_elf_adjust_dynindx
48 PARAMS((struct elf_link_hash_entry *, PTR));
50 static boolean sparc64_elf_relocate_section
51 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
52 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
53 static boolean sparc64_elf_object_p PARAMS ((bfd *));
55 /* The relocation "howto" table. */
57 static bfd_reloc_status_type sparc_elf_notsup_reloc
58 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
59 static bfd_reloc_status_type sparc_elf_wdisp16_reloc
60 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
61 static bfd_reloc_status_type sparc_elf_hix22_reloc
62 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
63 static bfd_reloc_status_type sparc_elf_lox10_reloc
64 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
66 static reloc_howto_type sparc64_elf_howto_table[] =
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),
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),
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),
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),
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),
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),
111 HOWTO(R_SPARC_WDISP16, 2,2,16,true, 0,complain_overflow_signed, sparc_elf_wdisp16_reloc,"R_SPARC_WDISP16", false,0,0x00000000,true),
112 HOWTO(R_SPARC_WDISP19, 2,2,19,true, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_WDISP19", false,0,0x0007ffff,true),
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),
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)
127 struct elf_reloc_map {
128 unsigned char bfd_reloc_val;
129 unsigned char elf_reloc_val;
132 static CONST struct elf_reloc_map sparc_reloc_map[] =
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 },
138 { BFD_RELOC_CTOR, R_SPARC_64 },
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},
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}
184 static reloc_howto_type *
185 sparc64_elf_reloc_type_lookup (abfd, code)
187 bfd_reloc_code_real_type code;
190 for (i = 0; i < sizeof (sparc_reloc_map) / sizeof (struct elf_reloc_map); i++)
192 if (sparc_reloc_map[i].bfd_reloc_val == code)
193 return &sparc64_elf_howto_table[(int) sparc_reloc_map[i].elf_reloc_val];
199 sparc64_elf_info_to_howto (abfd, cache_ptr, dst)
202 Elf64_Internal_Rela *dst;
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)];
208 /* Utility for performing the standard initial work of an instruction
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. */
216 static bfd_reloc_status_type
217 init_insn_reloc (abfd,
226 arelent *reloc_entry;
229 asection *input_section;
231 bfd_vma *prelocation;
235 reloc_howto_type *howto = reloc_entry->howto;
237 if (output_bfd != (bfd *) NULL
238 && (symbol->flags & BSF_SECTION_SYM) == 0
239 && (! howto->partial_inplace
240 || reloc_entry->addend == 0))
242 reloc_entry->address += input_section->output_offset;
246 /* This works because partial_inplace == false. */
247 if (output_bfd != NULL)
248 return bfd_reloc_continue;
250 if (reloc_entry->address > input_section->_cooked_size)
251 return bfd_reloc_outofrange;
253 relocation = (symbol->value
254 + symbol->section->output_section->vma
255 + symbol->section->output_offset);
256 relocation += reloc_entry->addend;
257 if (howto->pc_relative)
259 relocation -= (input_section->output_section->vma
260 + input_section->output_offset);
261 relocation -= reloc_entry->address;
264 *prelocation = relocation;
265 *pinsn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
266 return bfd_reloc_other;
269 /* For unsupported relocs. */
271 static bfd_reloc_status_type
272 sparc_elf_notsup_reloc (abfd,
280 arelent *reloc_entry;
283 asection *input_section;
285 char **error_message;
287 return bfd_reloc_notsupported;
290 /* Handle the WDISP16 reloc. */
292 static bfd_reloc_status_type
293 sparc_elf_wdisp16_reloc (abfd, reloc_entry, symbol, data, input_section,
294 output_bfd, error_message)
296 arelent *reloc_entry;
299 asection *input_section;
301 char **error_message;
305 bfd_reloc_status_type status;
307 status = init_insn_reloc (abfd, reloc_entry, symbol, data,
308 input_section, output_bfd, &relocation, &insn);
309 if (status != bfd_reloc_other)
312 insn = (insn & ~0x303fff) | ((((relocation >> 2) & 0xc000) << 6)
313 | ((relocation >> 2) & 0x3fff));
314 bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
316 if ((bfd_signed_vma) relocation < - 0x40000
317 || (bfd_signed_vma) relocation > 0x3ffff)
318 return bfd_reloc_overflow;
323 /* Handle the HIX22 reloc. */
325 static bfd_reloc_status_type
326 sparc_elf_hix22_reloc (abfd,
334 arelent *reloc_entry;
337 asection *input_section;
339 char **error_message;
343 bfd_reloc_status_type status;
345 status = init_insn_reloc (abfd, reloc_entry, symbol, data,
346 input_section, output_bfd, &relocation, &insn);
347 if (status != bfd_reloc_other)
350 relocation ^= MINUS_ONE;
351 insn = (insn & ~0x3fffff) | ((relocation >> 10) & 0x3fffff);
352 bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
354 if ((relocation & ~ (bfd_vma) 0xffffffff) != 0)
355 return bfd_reloc_overflow;
360 /* Handle the LOX10 reloc. */
362 static bfd_reloc_status_type
363 sparc_elf_lox10_reloc (abfd,
371 arelent *reloc_entry;
374 asection *input_section;
376 char **error_message;
380 bfd_reloc_status_type status;
382 status = init_insn_reloc (abfd, reloc_entry, symbol, data,
383 input_section, output_bfd, &relocation, &insn);
384 if (status != bfd_reloc_other)
387 insn = (insn & ~0x1fff) | 0x1c00 | (relocation & 0x3ff);
388 bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
395 /* FIXME: Do Delta Doc PLT entries. */
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. */
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
408 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/sparcv9/ld.so.1"
410 /* Values for fixups:
421 unsigned int insns[8];
429 static const struct plt_template plt_medlow_header =
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 */
448 static const struct plt_template plt_medlow_entry =
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 */
468 static const struct plt_template plt_medany_header =
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 */
486 static const struct plt_template plt_medany_entry =
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 */
508 static const struct plt_template plt_fullany_header =
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 */
526 static const struct plt_template plt_fullany_entry =
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 */
549 static const struct plt_template plt_pic_header =
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 */
564 static const struct plt_template plt_pic_small_entry =
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 */
577 { R_SPARC_13, 5 }, /* R_SPARC_GOT13 */
584 static const struct plt_template plt_pic_large_entry =
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 */
597 { R_SPARC_HI22, 5 }, /* R_SPARC_GOT22 */
598 { R_SPARC_LO10, 5 }, /* R_SPARC_GOT10 */
606 /* Build a plt entry given a template and values. */
608 static boolean sparc64_elf_build_plt_entry(output_bfd, loc, tmpl, values)
611 const struct plt_template *tmpl;
615 for (i = 0; i < 8; ++i)
617 unsigned int insn = tmpl->insns[i];
618 bfd_vma value = values[tmpl->fixup[i].value];
621 switch (tmpl->fixup[i].reloc)
626 insn |= (value >> 10) & 0x3fffff;
629 insn |= value & 0x3ff;
632 if ((bfd_signed_vma)value > 0xfff
633 || (bfd_signed_vma)value < -0x1000)
635 insn |= value & 0x1fff;
641 /* FIXME -- possibly use _bfd_final_link_relocate? */
643 howto = sparc64_elf_howto_table + tmpl->fixups[i].reloc;
644 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
646 if (r != bfd_reloc_ok)
650 bfd_put_32(output_bfd, insn, loc);
657 /* Look through the relocs for a section during the first phase, and
658 allocate space in the global offset table or procedure linkage
662 sparc64_elf_check_relocs (abfd, info, sec, relocs)
664 struct bfd_link_info *info;
666 const Elf_Internal_Rela *relocs;
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;
678 if (info->relocateable || (sec->flags & SEC_DEBUGGING))
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);
690 rel_end = relocs + sec->reloc_count;
691 for (rel = relocs; rel < rel_end; rel++)
693 unsigned long r_symndx;
694 struct elf_link_hash_entry *h;
696 r_symndx = ELF64_R_SYM (rel->r_info);
697 if (r_symndx < symtab_hdr->sh_info)
700 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
702 switch (ELF64_R_TYPE (rel->r_info))
707 /* This symbol requires a global offset table entry. */
711 /* Create the .got section. */
712 elf_hash_table (info)->dynobj = dynobj = abfd;
713 if (! _bfd_elf_create_got_section (dynobj, info))
719 sgot = bfd_get_section_by_name (dynobj, ".got");
720 BFD_ASSERT (sgot != NULL);
723 if (srelgot == NULL && (h != NULL || info->shared))
725 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
728 srelgot = bfd_make_section (dynobj, ".rela.got");
730 || ! bfd_set_section_flags (dynobj, srelgot,
737 || ! bfd_set_section_alignment (dynobj, srelgot, 3))
744 if (h->got_offset != (bfd_vma) -1)
746 /* We have already allocated space in the .got. */
749 h->got_offset = sgot->_raw_size;
751 /* Make sure this symbol is output as a dynamic symbol. */
752 if (h->dynindx == -1)
754 if (! bfd_elf64_link_record_dynamic_symbol (info, h))
758 srelgot->_raw_size += sizeof (Elf64_External_Rela);
762 /* This is a global offset table entry for a local
764 if (local_got_offsets == NULL)
767 register unsigned int i;
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)
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;
777 if (local_got_offsets[r_symndx] != (bfd_vma) -1)
779 /* We have already allocated space in the .got. */
782 local_got_offsets[r_symndx] = sgot->_raw_size;
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);
793 sgot->_raw_size += 8;
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;
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:
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. */
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);
833 /* Make sure this symbol is output as a dynamic symbol. */
834 if (h->dynindx == -1)
836 if (! bfd_elf64_link_record_dynamic_symbol (info, h))
840 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
845 case R_SPARC_PC_HH22:
846 case R_SPARC_PC_HM10:
847 case R_SPARC_PC_LM22:
849 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
856 case R_SPARC_WDISP30:
857 case R_SPARC_WDISP22:
858 case R_SPARC_WDISP19:
859 case R_SPARC_WDISP16:
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.
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))
900 name = (bfd_elf_string_from_elf_section
902 elf_elfheader (abfd)->e_shstrndx,
903 elf_section_data (sec)->rel_hdr.sh_name));
907 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
908 && strcmp (bfd_get_section_name (abfd, sec),
911 sreloc = bfd_get_section_by_name (dynobj, name);
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;
922 || ! bfd_set_section_flags (dynobj, sreloc, flags)
923 || ! bfd_set_section_alignment (dynobj, sreloc, 3))
928 sreloc->_raw_size += sizeof (Elf64_External_Rela);
932 case R_SPARC_REGISTER:
937 (*_bfd_error_handler)("%s: check_relocs: unhandled reloc type %d",
938 bfd_get_filename(abfd),
939 ELF64_R_TYPE (rel->r_info));
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
954 sparc64_elf_adjust_dynamic_symbol (info, h)
955 struct bfd_link_info *info;
956 struct elf_link_hash_entry *h;
960 unsigned int power_of_two;
962 dynobj = elf_hash_table (info)->dynobj;
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)));
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
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))
989 if (! elf_hash_table (info)->dynamic_sections_created)
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
996 BFD_ASSERT ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0);
1000 s = bfd_get_section_by_name (dynobj, ".plt");
1001 BFD_ASSERT (s != NULL);
1003 /* The first four bit in .plt is reserved. */
1004 if (s->_raw_size == 0)
1005 s->_raw_size = PLT_HEADER_SIZE;
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. */
1013 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1015 h->root.u.def.section = s;
1016 h->root.u.def.value = s->_raw_size;
1019 h->plt_offset = s->_raw_size;
1021 /* Make room for this entry. */
1022 s->_raw_size += PLT_ENTRY_SIZE;
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. */
1027 s = bfd_get_section_by_name (dynobj, ".got.plt");
1028 BFD_ASSERT (s != NULL);
1031 /* We also need to make an entry in the .rela.plt section. */
1033 s = bfd_get_section_by_name (dynobj, ".rela.plt");
1034 BFD_ASSERT (s != NULL);
1035 s->_raw_size += sizeof (Elf64_External_Rela);
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)
1041 bfd_set_error (bfd_error_bad_value);
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)
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;
1060 /* This is a reference to a symbol defined by a dynamic object which
1061 is not a function. */
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. */
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. */
1080 s = bfd_get_section_by_name (dynobj, ".dynbss");
1081 BFD_ASSERT (s != NULL);
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)
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;
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)
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))
1109 if (! bfd_set_section_alignment (dynobj, s, power_of_two))
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;
1117 /* Increment the section size to make room for the symbol. */
1118 s->_raw_size += h->size;
1123 /* Set the sizes of the dynamic sections. */
1126 sparc64_elf_size_dynamic_sections (output_bfd, info)
1128 struct bfd_link_info *info;
1135 dynobj = elf_hash_table (info)->dynobj;
1136 BFD_ASSERT (dynobj != NULL);
1138 if (elf_hash_table (info)->dynamic_sections_created)
1140 /* Set the contents of the .interp section to the interpreter. */
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;
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
1156 s = bfd_get_section_by_name (dynobj, ".rela.got");
1161 /* The check_relocs and adjust_dynamic_symbol entry points have
1162 determined the sizes of the various dynamic sections. Allocate
1166 for (s = dynobj->sections; s != NULL; s = s->next)
1171 if ((s->flags & SEC_LINKER_CREATED) == 0)
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);
1180 if (strncmp (name, ".rela", 5) == 0)
1182 if (s->_raw_size == 0)
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. */
1197 const char *outname;
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,
1204 target = bfd_get_section_by_name (output_bfd, outname + 5);
1206 && (target->flags & SEC_READONLY) != 0)
1209 if (strcmp (name, ".rela.plt") == 0)
1212 /* We use the reloc_count field as a counter if we need
1213 to copy relocs into the output file. */
1217 else if (strcmp (name, ".plt") != 0
1218 && strncmp (name, ".got", 4) != 0)
1220 /* It's not one of our sections, so don't allocate space. */
1228 for (spp = &s->output_section->owner->sections;
1229 *spp != s->output_section;
1230 spp = &(*spp)->next)
1232 *spp = s->output_section->next;
1233 --s->output_section->owner->section_count;
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)
1244 if (elf_hash_table (info)->dynamic_sections_created)
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. */
1253 if (! bfd_elf64_add_dynamic_entry (info, DT_DEBUG, 0))
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))
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)))
1276 if (! bfd_elf64_add_dynamic_entry (info, DT_TEXTREL, 0))
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. */
1291 for (s = output_bfd->sections; s != NULL; s = s->next)
1293 if ((s->flags & SEC_LINKER_CREATED) != 0
1294 || (s->flags & SEC_ALLOC) == 0)
1297 elf_section_data (s)->dynindx = c + 1;
1299 /* These symbols will have no names, so we don't need to
1300 fiddle with dynstr_index. */
1305 elf_link_hash_traverse (elf_hash_table (info),
1306 sparc64_elf_adjust_dynindx,
1308 elf_hash_table (info)->dynsymcount += c;
1314 /* Increment the index of a dynamic symbol by a given amount. Called
1315 via elf_link_hash_traverse. */
1318 sparc64_elf_adjust_dynindx (h, cparg)
1319 struct elf_link_hash_entry *h;
1322 int *cp = (int *) cparg;
1324 if (h->dynindx != -1)
1330 /* Relocate a SPARC64 ELF section. */
1333 sparc64_elf_relocate_section (output_bfd, info, input_bfd, input_section,
1334 contents, relocs, local_syms, local_sections)
1336 struct bfd_link_info *info;
1338 asection *input_section;
1340 Elf_Internal_Rela *relocs;
1341 Elf_Internal_Sym *local_syms;
1342 asection **local_sections;
1345 Elf_Internal_Shdr *symtab_hdr;
1346 struct elf_link_hash_entry **sym_hashes;
1347 bfd_vma *local_got_offsets;
1352 Elf_Internal_Rela *rel;
1353 Elf_Internal_Rela *relend;
1355 dynobj = elf_hash_table (info)->dynobj;
1356 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1357 sym_hashes = elf_sym_hashes (input_bfd);
1358 local_got_offsets = elf_local_got_offsets (input_bfd);
1360 if (elf_hash_table(info)->hgot == NULL)
1363 got_base = elf_hash_table (info)->hgot->root.u.def.value;
1365 sgot = splt = sreloc = NULL;
1368 relend = relocs + input_section->reloc_count;
1369 for (; rel < relend; rel++)
1372 reloc_howto_type *howto;
1374 struct elf_link_hash_entry *h;
1375 Elf_Internal_Sym *sym;
1378 bfd_reloc_status_type r;
1380 r_type = ELF64_R_TYPE (rel->r_info);
1381 if (r_type < 0 || r_type >= (int) R_SPARC_max)
1383 bfd_set_error (bfd_error_bad_value);
1386 howto = sparc64_elf_howto_table + r_type;
1388 r_symndx = ELF64_R_SYM (rel->r_info);
1390 if (info->relocateable)
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)
1398 sym = local_syms + r_symndx;
1399 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
1401 sec = local_sections[r_symndx];
1402 rel->r_addend += sec->output_offset + sym->st_value;
1409 /* This is a final link. */
1413 if (r_symndx < symtab_hdr->sh_info)
1415 sym = local_syms + r_symndx;
1416 sec = local_sections[r_symndx];
1417 relocation = (sec->output_section->vma
1418 + sec->output_offset
1423 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
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;
1427 if (h->root.type == bfd_link_hash_defined
1428 || h->root.type == bfd_link_hash_defweak)
1430 boolean skip_it = false;
1431 sec = h->root.u.def.section;
1435 case R_SPARC_WPLT30:
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:
1443 if (h->plt_offset != (bfd_vma) -1)
1450 if (elf_hash_table(info)->dynamic_sections_created
1452 || (!info->symbolic && h->dynindx != -1)
1453 || !(h->elf_link_hash_flags
1454 & ELF_LINK_HASH_DEF_REGULAR)))
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_"))
1471 case R_SPARC_DISP16:
1472 case R_SPARC_DISP32:
1473 case R_SPARC_WDISP30:
1474 case R_SPARC_WDISP22:
1487 case R_SPARC_WDISP19:
1488 case R_SPARC_WDISP16:
1492 case R_SPARC_DISP64:
1499 && ((!info->symbolic && h->dynindx != -1)
1500 || !(h->elf_link_hash_flags
1501 & ELF_LINK_HASH_DEF_REGULAR)))
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. */
1515 relocation = (h->root.u.def.value
1516 + sec->output_section->vma
1517 + sec->output_offset);
1520 else if (h->root.type == bfd_link_hash_undefweak)
1522 else if (info->shared && !info->symbolic)
1526 if (! ((*info->callbacks->undefined_symbol)
1527 (info, h->root.root.string, input_bfd,
1528 input_section, rel->r_offset)))
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))
1542 case R_SPARC_PC_HH22:
1543 case R_SPARC_PC_HM10:
1544 case R_SPARC_PC_LM22:
1546 && !strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_"))
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:
1584 Elf_Internal_Rela outrel;
1590 (bfd_elf_string_from_elf_section
1592 elf_elfheader (input_bfd)->e_shstrndx,
1593 elf_section_data (input_section)->rel_hdr.sh_name));
1598 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
1599 && strcmp (bfd_get_section_name(input_bfd,
1603 sreloc = bfd_get_section_by_name (dynobj, name);
1604 BFD_ASSERT (sreloc != NULL);
1609 if (elf_section_data (input_section)->stab_info == NULL)
1610 outrel.r_offset = rel->r_offset;
1615 off = (_bfd_stab_section_offset
1616 (output_bfd, &elf_hash_table (info)->stab_info,
1618 &elf_section_data (input_section)->stab_info,
1620 if (off == MINUS_ONE)
1622 outrel.r_offset = off;
1625 outrel.r_offset += (input_section->output_section->vma
1626 + input_section->output_offset);
1629 memset (&outrel, 0, sizeof outrel);
1630 /* h->dynindx may be -1 if the symbol was marked to
1633 && ((! info->symbolic && h->dynindx != -1)
1634 || (h->elf_link_hash_flags
1635 & ELF_LINK_HASH_DEF_REGULAR) == 0))
1637 BFD_ASSERT (h->dynindx != -1);
1638 outrel.r_info = ELF64_R_INFO (h->dynindx, r_type);
1639 outrel.r_addend = rel->r_addend;
1643 if (r_type == R_SPARC_64)
1645 outrel.r_info = ELF64_R_INFO (0, R_SPARC_RELATIVE);
1646 outrel.r_addend = relocation + rel->r_addend;
1653 sec = local_sections[r_symndx];
1656 BFD_ASSERT (h->root.type == bfd_link_hash_defined
1658 == bfd_link_hash_defweak));
1659 sec = h->root.u.def.section;
1661 if (sec != NULL && bfd_is_abs_section (sec))
1663 else if (sec == NULL || sec->owner == NULL)
1665 bfd_set_error (bfd_error_bad_value);
1672 osec = sec->output_section;
1673 indx = elf_section_data (osec)->dynindx;
1674 BFD_ASSERT (indx > 0);
1677 outrel.r_info = ELF64_R_INFO (indx, r_type);
1678 outrel.r_addend = relocation + rel->r_addend;
1682 bfd_elf64_swap_reloca_out (output_bfd, &outrel,
1683 (((Elf64_External_Rela *)
1685 + sreloc->reloc_count));
1686 ++sreloc->reloc_count;
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. */
1692 || (input_section->flags & SEC_ALLOC) != 0
1693 || ELF64_R_TYPE (outrel.r_info) != R_SPARC_RELATIVE)
1705 /* Relocation is to the entry for this symbol in the global
1709 sgot = bfd_get_section_by_name (dynobj, ".got");
1710 BFD_ASSERT (sgot != NULL);
1715 bfd_vma off = h->got_offset;
1716 BFD_ASSERT (off != (bfd_vma) -1);
1718 if (! elf_hash_table (info)->dynamic_sections_created
1720 && (info->symbolic || h->dynindx == -1)
1721 && (h->elf_link_hash_flags
1722 & ELF_LINK_HASH_DEF_REGULAR)))
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.
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. */
1740 bfd_put_64 (output_bfd, relocation,
1741 sgot->contents + off);
1745 relocation = sgot->output_offset + off - got_base;
1751 BFD_ASSERT (local_got_offsets != NULL);
1752 off = local_got_offsets[r_symndx];
1753 BFD_ASSERT (off != (bfd_vma) -1);
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. */
1762 bfd_put_64 (output_bfd, relocation, sgot->contents + off);
1763 local_got_offsets[r_symndx] |= 1;
1768 Elf_Internal_Rela outrel;
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);
1775 outrel.r_offset = (sgot->output_section->vma
1776 + sgot->output_offset
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 *)
1783 + srelgot->reloc_count));
1784 ++srelgot->reloc_count;
1787 relocation = sgot->output_offset + off - got_base;
1791 case R_SPARC_WPLT30:
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:
1799 /* Relocation is to the entry for this symbol in the
1800 procedure linkage table. */
1801 BFD_ASSERT (h != NULL);
1803 if (h->plt_offset == (bfd_vma) -1)
1805 /* We didn't make a PLT entry for this symbol. This
1806 happens when statically linking PIC code, or when
1807 using -Bsymbolic. */
1813 splt = bfd_get_section_by_name (dynobj, ".plt");
1814 BFD_ASSERT (splt != NULL);
1817 relocation = (splt->output_section->vma
1818 + splt->output_offset
1826 relocation += rel->r_addend;
1827 relocation = (relocation & 0x3ff) + ELF64_R_TYPE_DATA (rel->r_info);
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);
1833 r = bfd_check_overflow (howto->complain_on_overflow,
1834 howto->bitsize, howto->rightshift,
1839 case R_SPARC_WDISP16:
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;
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);
1854 r = bfd_check_overflow (howto->complain_on_overflow,
1855 howto->bitsize, howto->rightshift,
1864 relocation += rel->r_addend;
1865 relocation = relocation ^ MINUS_ONE;
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);
1871 r = bfd_check_overflow (howto->complain_on_overflow,
1872 howto->bitsize, howto->rightshift,
1881 relocation += rel->r_addend;
1882 relocation = (relocation & 0x3ff) | 0x1c00;
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);
1894 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1895 contents, rel->r_offset,
1896 relocation, rel->r_addend);
1906 case bfd_reloc_outofrange:
1909 case bfd_reloc_overflow:
1915 if (h->root.type == bfd_link_hash_undefweak
1916 && howto->pc_relative)
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
1926 name = h->root.root.string;
1930 name = (bfd_elf_string_from_elf_section
1932 symtab_hdr->sh_link,
1937 name = bfd_section_name (input_bfd, sec);
1939 if (! ((*info->callbacks->reloc_overflow)
1940 (info, name, howto->name, (bfd_vma) 0,
1941 input_bfd, input_section, rel->r_offset)))
1951 /* Finish up dynamic symbol handling. We set the contents of various
1952 dynamic sections here. */
1955 sparc64_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
1957 struct bfd_link_info *info;
1958 struct elf_link_hash_entry *h;
1959 Elf_Internal_Sym *sym;
1963 dynobj = elf_hash_table (info)->dynobj;
1965 if (h->plt_offset != (bfd_vma) -1)
1970 Elf_Internal_Rela rela;
1972 bfd_vma plt0, pltN, got0, gotN, plt_index, got_offset;
1973 const struct plt_template *plt_tmpl;
1975 /* This symbol has an entry in the procedure linkage table. Set
1978 BFD_ASSERT (h->dynindx != -1);
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);
1985 /* Fill in the various values the plt entry might care about,
1986 as detailed above. */
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;
1996 values[1] = got0 - plt0;
1997 values[5] = got_offset;
1999 values[3] = gotN - pltN;
2000 values[4] = plt_index * sizeof(Elf64_External_Rela);
2002 /* Fill in the entry in the procedure linkage table. */
2005 if (got_offset < 0x1000)
2006 plt_tmpl = &plt_pic_small_entry;
2008 plt_tmpl = &plt_pic_large_entry;
2010 /* FIXME -- learn how to select code models here. */
2011 plt_tmpl = &plt_medany_entry;
2013 sparc64_elf_build_plt_entry(output_bfd, splt->contents + h->plt_offset,
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. */
2021 sparc64_elf_build_plt_entry(output_bfd, splt->contents,
2024 : &plt_medany_header),
2028 /* Fill in the entry in the .got.plt section. */
2030 bfd_put_64 (output_bfd, (info->shared ? plt0-got0 : plt0),
2031 sgotplt->contents + got_offset);
2033 /* Fill in the entry in the .rela.plt section. */
2035 rela.r_offset = gotN;
2036 rela.r_info = ELF64_R_INFO (h->dynindx, R_SPARC_JMP_SLOT);
2038 bfd_elf64_swap_reloca_out (output_bfd, &rela,
2039 ((Elf64_External_Rela *) srela->contents
2042 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
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;
2050 if (h->got_offset != (bfd_vma) -1)
2054 Elf_Internal_Rela rela;
2056 /* This symbol has an entry in the global offset table. Set it
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);
2063 rela.r_offset = (sgot->output_section->vma
2064 + sgot->output_offset
2065 + (h->got_offset &~ 1));
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. */
2073 && (info->symbolic || h->dynindx == -1)
2074 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
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);
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);
2089 bfd_elf64_swap_reloca_out (output_bfd, &rela,
2090 ((Elf64_External_Rela *) srela->contents
2091 + srela->reloc_count));
2092 ++srela->reloc_count;
2095 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
2098 Elf_Internal_Rela rela;
2100 /* This symbols needs a copy reloc. Set it up. */
2102 BFD_ASSERT (h->dynindx != -1);
2104 s = bfd_get_section_by_name (h->root.u.def.section->owner,
2106 BFD_ASSERT (s != NULL);
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);
2113 bfd_elf64_swap_reloca_out (output_bfd, &rela,
2114 ((Elf64_External_Rela *) s->contents
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;
2128 /* Finish up the dynamic sections. */
2131 sparc64_elf_finish_dynamic_sections (output_bfd, info)
2133 struct bfd_link_info *info;
2139 dynobj = elf_hash_table (info)->dynobj;
2141 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2143 if (elf_hash_table (info)->dynamic_sections_created)
2146 Elf64_External_Dyn *dyncon, *dynconend;
2148 splt = bfd_get_section_by_name (dynobj, ".plt");
2149 BFD_ASSERT (splt != NULL && sdyn != NULL);
2151 dyncon = (Elf64_External_Dyn *) sdyn->contents;
2152 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
2153 for (; dyncon < dynconend; dyncon++)
2155 Elf_Internal_Dyn dyn;
2159 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
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;
2173 s = bfd_get_section_by_name (output_bfd, name);
2179 dyn.d_un.d_ptr = s->vma;
2182 if (s->_cooked_size != 0)
2183 dyn.d_un.d_val = s->_cooked_size;
2185 dyn.d_un.d_val = s->_raw_size;
2188 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2192 elf_section_data (splt->output_section)->this_hdr.sh_entsize =
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)
2203 bfd_put_64 (output_bfd, (bfd_vma) 0, sgot->contents);
2205 bfd_put_64 (output_bfd,
2206 sdyn->output_section->vma + sdyn->output_offset,
2210 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 8;
2216 Elf_Internal_Sym sym;
2219 /* Set up the section symbols for the output sections. */
2221 sdynsym = bfd_get_section_by_name (dynobj, ".dynsym");
2222 BFD_ASSERT (sdynsym != NULL);
2226 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
2230 for (s = output_bfd->sections; s != NULL; s = s->next)
2234 if (elf_section_data (s)->dynindx == 0)
2237 sym.st_value = s->vma;
2239 indx = elf_section_data (s)->this_idx;
2240 BFD_ASSERT (indx > 0);
2241 sym.st_shndx = indx;
2243 bfd_elf64_swap_symbol_out (output_bfd, &sym,
2244 (PTR) (((Elf64_External_Sym *)
2246 + elf_section_data (s)->dynindx));
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;
2260 /* Set the right machine number for a SPARC64 ELF file. */
2263 sparc64_elf_object_p (abfd)
2266 return bfd_default_set_arch_mach (abfd, bfd_arch_sparc, bfd_mach_sparc_v9);
2269 #define TARGET_BIG_SYM bfd_elf64_sparc_vec
2270 #define TARGET_BIG_NAME "elf64-sparc"
2271 #define ELF_ARCH bfd_arch_sparc
2272 #define ELF_MACHINE_CODE EM_SPARC64
2273 #define ELF_MAXPAGESIZE 0x100000
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
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
2295 #define elf_backend_object_p \
2296 sparc64_elf_object_p
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
2303 #include "elf64-target.h"