daily update
[binutils.git] / bfd / elf32-sparc.c
blobbe2733d36bb7cf6919e132ba95b9f311bbf00b54
1 /* SPARC-specific support for 32-bit ELF
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
3 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 #include "bfd.h"
22 #include "sysdep.h"
23 #include "bfdlink.h"
24 #include "libbfd.h"
25 #include "elf-bfd.h"
26 #include "elf/sparc.h"
27 #include "opcode/sparc.h"
29 static reloc_howto_type *elf32_sparc_reloc_type_lookup
30 PARAMS ((bfd *, bfd_reloc_code_real_type));
31 static void elf32_sparc_info_to_howto
32 PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
33 static boolean elf32_sparc_check_relocs
34 PARAMS ((bfd *, struct bfd_link_info *, asection *,
35 const Elf_Internal_Rela *));
36 static boolean elf32_sparc_adjust_dynamic_symbol
37 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
38 static boolean elf32_sparc_size_dynamic_sections
39 PARAMS ((bfd *, struct bfd_link_info *));
40 static boolean elf32_sparc_relax_section
41 PARAMS ((bfd *, asection *, struct bfd_link_info *, boolean *));
42 static boolean elf32_sparc_relocate_section
43 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
44 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
45 static boolean elf32_sparc_finish_dynamic_symbol
46 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
47 Elf_Internal_Sym *));
48 static boolean elf32_sparc_finish_dynamic_sections
49 PARAMS ((bfd *, struct bfd_link_info *));
50 static boolean elf32_sparc_merge_private_bfd_data PARAMS ((bfd *, bfd *));
51 static boolean elf32_sparc_object_p
52 PARAMS ((bfd *));
53 static void elf32_sparc_final_write_processing
54 PARAMS ((bfd *, boolean));
55 static enum elf_reloc_type_class elf32_sparc_reloc_type_class
56 PARAMS ((const Elf_Internal_Rela *));
57 static asection * elf32_sparc_gc_mark_hook
58 PARAMS ((asection *, struct bfd_link_info *, Elf_Internal_Rela *,
59 struct elf_link_hash_entry *, Elf_Internal_Sym *));
60 static boolean elf32_sparc_gc_sweep_hook
61 PARAMS ((bfd *, struct bfd_link_info *, asection *,
62 const Elf_Internal_Rela *));
64 /* The relocation "howto" table. */
66 static bfd_reloc_status_type sparc_elf_notsupported_reloc
67 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
68 static bfd_reloc_status_type sparc_elf_wdisp16_reloc
69 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
71 reloc_howto_type _bfd_sparc_elf_howto_table[] =
73 HOWTO(R_SPARC_NONE, 0,0, 0,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_NONE", false,0,0x00000000,true),
74 HOWTO(R_SPARC_8, 0,0, 8,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_8", false,0,0x000000ff,true),
75 HOWTO(R_SPARC_16, 0,1,16,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_16", false,0,0x0000ffff,true),
76 HOWTO(R_SPARC_32, 0,2,32,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_32", false,0,0xffffffff,true),
77 HOWTO(R_SPARC_DISP8, 0,0, 8,true, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_DISP8", false,0,0x000000ff,true),
78 HOWTO(R_SPARC_DISP16, 0,1,16,true, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_DISP16", false,0,0x0000ffff,true),
79 HOWTO(R_SPARC_DISP32, 0,2,32,true, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_DISP32", false,0,0xffffffff,true),
80 HOWTO(R_SPARC_WDISP30, 2,2,30,true, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_WDISP30", false,0,0x3fffffff,true),
81 HOWTO(R_SPARC_WDISP22, 2,2,22,true, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_WDISP22", false,0,0x003fffff,true),
82 HOWTO(R_SPARC_HI22, 10,2,22,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_HI22", false,0,0x003fffff,true),
83 HOWTO(R_SPARC_22, 0,2,22,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_22", false,0,0x003fffff,true),
84 HOWTO(R_SPARC_13, 0,2,13,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_13", false,0,0x00001fff,true),
85 HOWTO(R_SPARC_LO10, 0,2,10,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_LO10", false,0,0x000003ff,true),
86 HOWTO(R_SPARC_GOT10, 0,2,10,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_GOT10", false,0,0x000003ff,true),
87 HOWTO(R_SPARC_GOT13, 0,2,13,false,0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_GOT13", false,0,0x00001fff,true),
88 HOWTO(R_SPARC_GOT22, 10,2,22,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_GOT22", false,0,0x003fffff,true),
89 HOWTO(R_SPARC_PC10, 0,2,10,true, 0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_PC10", false,0,0x000003ff,true),
90 HOWTO(R_SPARC_PC22, 10,2,22,true, 0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_PC22", false,0,0x003fffff,true),
91 HOWTO(R_SPARC_WPLT30, 2,2,30,true, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_WPLT30", false,0,0x3fffffff,true),
92 HOWTO(R_SPARC_COPY, 0,0,00,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_COPY", false,0,0x00000000,true),
93 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),
94 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),
95 HOWTO(R_SPARC_RELATIVE, 0,0,00,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_RELATIVE",false,0,0x00000000,true),
96 HOWTO(R_SPARC_UA32, 0,2,32,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_UA32", false,0,0xffffffff,true),
97 HOWTO(R_SPARC_PLT32, 0,0,00,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_PLT32", false,0,0xffffffff,true),
98 HOWTO(R_SPARC_HIPLT22, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_HIPLT22", false,0,0x00000000,true),
99 HOWTO(R_SPARC_LOPLT10, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_LOPLT10", false,0,0x00000000,true),
100 HOWTO(R_SPARC_PCPLT32, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_PCPLT32", false,0,0x00000000,true),
101 HOWTO(R_SPARC_PCPLT22, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_PCPLT22", false,0,0x00000000,true),
102 HOWTO(R_SPARC_PCPLT10, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_PCPLT10", false,0,0x00000000,true),
103 HOWTO(R_SPARC_10, 0,2,10,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_10", false,0,0x000003ff,true),
104 HOWTO(R_SPARC_11, 0,2,11,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_11", false,0,0x000007ff,true),
105 /* These are for sparc64 in a 64 bit environment.
106 Values need to be here because the table is indexed by reloc number. */
107 HOWTO(R_SPARC_64, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_64", false,0,0x00000000,true),
108 HOWTO(R_SPARC_OLO10, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_OLO10", false,0,0x00000000,true),
109 HOWTO(R_SPARC_HH22, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_HH22", false,0,0x00000000,true),
110 HOWTO(R_SPARC_HM10, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_HM10", false,0,0x00000000,true),
111 HOWTO(R_SPARC_LM22, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_LM22", false,0,0x00000000,true),
112 HOWTO(R_SPARC_PC_HH22, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_PC_HH22", false,0,0x00000000,true),
113 HOWTO(R_SPARC_PC_HM10, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_PC_HM10", false,0,0x00000000,true),
114 HOWTO(R_SPARC_PC_LM22, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_PC_LM22", false,0,0x00000000,true),
115 /* End sparc64 in 64 bit environment values.
116 The following are for sparc64 in a 32 bit environment. */
117 HOWTO(R_SPARC_WDISP16, 2,2,16,true, 0,complain_overflow_signed, sparc_elf_wdisp16_reloc,"R_SPARC_WDISP16", false,0,0x00000000,true),
118 HOWTO(R_SPARC_WDISP19, 2,2,19,true, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_WDISP19", false,0,0x0007ffff,true),
119 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),
120 HOWTO(R_SPARC_7, 0,2, 7,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_7", false,0,0x0000007f,true),
121 HOWTO(R_SPARC_5, 0,2, 5,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_5", false,0,0x0000001f,true),
122 HOWTO(R_SPARC_6, 0,2, 6,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_6", false,0,0x0000003f,true),
123 HOWTO(R_SPARC_NONE, 0,0, 0,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_NONE", false,0,0x00000000,true),
124 HOWTO(R_SPARC_NONE, 0,0, 0,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_NONE", false,0,0x00000000,true),
125 HOWTO(R_SPARC_NONE, 0,0, 0,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_NONE", false,0,0x00000000,true),
126 HOWTO(R_SPARC_NONE, 0,0, 0,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_NONE", false,0,0x00000000,true),
127 HOWTO(R_SPARC_NONE, 0,0, 0,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_NONE", false,0,0x00000000,true),
128 HOWTO(R_SPARC_NONE, 0,0, 0,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_NONE", false,0,0x00000000,true),
129 HOWTO(R_SPARC_NONE, 0,0, 0,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_NONE", false,0,0x00000000,true),
130 HOWTO(R_SPARC_NONE, 0,0, 0,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_NONE", false,0,0x00000000,true),
131 HOWTO(R_SPARC_UA64, 0,0, 0,false,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_UA64", false,0,0x00000000,true),
132 HOWTO(R_SPARC_UA16, 0,1,16,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_UA16", false,0,0x0000ffff,true),
133 HOWTO(R_SPARC_REV32, 0,2,32,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_REV32", false,0,0xffffffff,true),
135 static reloc_howto_type elf32_sparc_vtinherit_howto =
136 HOWTO (R_SPARC_GNU_VTINHERIT, 0,2,0,false,0,complain_overflow_dont, NULL, "R_SPARC_GNU_VTINHERIT", false,0, 0, false);
137 static reloc_howto_type elf32_sparc_vtentry_howto =
138 HOWTO (R_SPARC_GNU_VTENTRY, 0,2,0,false,0,complain_overflow_dont, _bfd_elf_rel_vtable_reloc_fn,"R_SPARC_GNU_VTENTRY", false,0,0, false);
140 struct elf_reloc_map {
141 bfd_reloc_code_real_type bfd_reloc_val;
142 unsigned char elf_reloc_val;
145 static const struct elf_reloc_map sparc_reloc_map[] =
147 { BFD_RELOC_NONE, R_SPARC_NONE, },
148 { BFD_RELOC_16, R_SPARC_16, },
149 { BFD_RELOC_16_PCREL, R_SPARC_DISP16 },
150 { BFD_RELOC_8, R_SPARC_8 },
151 { BFD_RELOC_8_PCREL, R_SPARC_DISP8 },
152 { BFD_RELOC_CTOR, R_SPARC_32 },
153 { BFD_RELOC_32, R_SPARC_32 },
154 { BFD_RELOC_32_PCREL, R_SPARC_DISP32 },
155 { BFD_RELOC_HI22, R_SPARC_HI22 },
156 { BFD_RELOC_LO10, R_SPARC_LO10, },
157 { BFD_RELOC_32_PCREL_S2, R_SPARC_WDISP30 },
158 { BFD_RELOC_SPARC_PLT32, R_SPARC_PLT32 },
159 { BFD_RELOC_SPARC22, R_SPARC_22 },
160 { BFD_RELOC_SPARC13, R_SPARC_13 },
161 { BFD_RELOC_SPARC_GOT10, R_SPARC_GOT10 },
162 { BFD_RELOC_SPARC_GOT13, R_SPARC_GOT13 },
163 { BFD_RELOC_SPARC_GOT22, R_SPARC_GOT22 },
164 { BFD_RELOC_SPARC_PC10, R_SPARC_PC10 },
165 { BFD_RELOC_SPARC_PC22, R_SPARC_PC22 },
166 { BFD_RELOC_SPARC_WPLT30, R_SPARC_WPLT30 },
167 { BFD_RELOC_SPARC_COPY, R_SPARC_COPY },
168 { BFD_RELOC_SPARC_GLOB_DAT, R_SPARC_GLOB_DAT },
169 { BFD_RELOC_SPARC_JMP_SLOT, R_SPARC_JMP_SLOT },
170 { BFD_RELOC_SPARC_RELATIVE, R_SPARC_RELATIVE },
171 { BFD_RELOC_SPARC_WDISP22, R_SPARC_WDISP22 },
172 { BFD_RELOC_SPARC_UA16, R_SPARC_UA16 },
173 { BFD_RELOC_SPARC_UA32, R_SPARC_UA32 },
174 { BFD_RELOC_SPARC_UA64, R_SPARC_UA64 },
175 { BFD_RELOC_SPARC_10, R_SPARC_10 },
176 { BFD_RELOC_SPARC_11, R_SPARC_11 },
177 { BFD_RELOC_SPARC_64, R_SPARC_64 },
178 { BFD_RELOC_SPARC_OLO10, R_SPARC_OLO10 },
179 { BFD_RELOC_SPARC_HH22, R_SPARC_HH22 },
180 { BFD_RELOC_SPARC_HM10, R_SPARC_HM10 },
181 { BFD_RELOC_SPARC_LM22, R_SPARC_LM22 },
182 { BFD_RELOC_SPARC_PC_HH22, R_SPARC_PC_HH22 },
183 { BFD_RELOC_SPARC_PC_HM10, R_SPARC_PC_HM10 },
184 { BFD_RELOC_SPARC_PC_LM22, R_SPARC_PC_LM22 },
185 { BFD_RELOC_SPARC_WDISP16, R_SPARC_WDISP16 },
186 { BFD_RELOC_SPARC_WDISP19, R_SPARC_WDISP19 },
187 { BFD_RELOC_SPARC_7, R_SPARC_7 },
188 { BFD_RELOC_SPARC_5, R_SPARC_5 },
189 { BFD_RELOC_SPARC_6, R_SPARC_6 },
190 { BFD_RELOC_SPARC_REV32, R_SPARC_REV32 },
191 { BFD_RELOC_VTABLE_INHERIT, R_SPARC_GNU_VTINHERIT },
192 { BFD_RELOC_VTABLE_ENTRY, R_SPARC_GNU_VTENTRY },
195 static reloc_howto_type *
196 elf32_sparc_reloc_type_lookup (abfd, code)
197 bfd *abfd ATTRIBUTE_UNUSED;
198 bfd_reloc_code_real_type code;
200 unsigned int i;
202 switch (code)
204 case BFD_RELOC_VTABLE_INHERIT:
205 return &elf32_sparc_vtinherit_howto;
207 case BFD_RELOC_VTABLE_ENTRY:
208 return &elf32_sparc_vtentry_howto;
210 default:
211 for (i = 0; i < sizeof (sparc_reloc_map) / sizeof (struct elf_reloc_map); i++)
213 if (sparc_reloc_map[i].bfd_reloc_val == code)
214 return &_bfd_sparc_elf_howto_table[(int) sparc_reloc_map[i].elf_reloc_val];
217 bfd_set_error (bfd_error_bad_value);
218 return NULL;
221 /* We need to use ELF32_R_TYPE so we have our own copy of this function,
222 and elf64-sparc.c has its own copy. */
224 static void
225 elf32_sparc_info_to_howto (abfd, cache_ptr, dst)
226 bfd *abfd ATTRIBUTE_UNUSED;
227 arelent *cache_ptr;
228 Elf_Internal_Rela *dst;
230 switch (ELF32_R_TYPE(dst->r_info))
232 case R_SPARC_GNU_VTINHERIT:
233 cache_ptr->howto = &elf32_sparc_vtinherit_howto;
234 break;
236 case R_SPARC_GNU_VTENTRY:
237 cache_ptr->howto = &elf32_sparc_vtentry_howto;
238 break;
240 default:
241 BFD_ASSERT (ELF32_R_TYPE(dst->r_info) < (unsigned int) R_SPARC_max_std);
242 cache_ptr->howto = &_bfd_sparc_elf_howto_table[ELF32_R_TYPE(dst->r_info)];
246 /* For unsupported relocs. */
248 static bfd_reloc_status_type
249 sparc_elf_notsupported_reloc (abfd,
250 reloc_entry,
251 symbol,
252 data,
253 input_section,
254 output_bfd,
255 error_message)
256 bfd *abfd ATTRIBUTE_UNUSED;
257 arelent *reloc_entry ATTRIBUTE_UNUSED;
258 asymbol *symbol ATTRIBUTE_UNUSED;
259 PTR data ATTRIBUTE_UNUSED;
260 asection *input_section ATTRIBUTE_UNUSED;
261 bfd *output_bfd ATTRIBUTE_UNUSED;
262 char **error_message ATTRIBUTE_UNUSED;
264 return bfd_reloc_notsupported;
267 /* Handle the WDISP16 reloc. */
269 static bfd_reloc_status_type
270 sparc_elf_wdisp16_reloc (abfd,
271 reloc_entry,
272 symbol,
273 data,
274 input_section,
275 output_bfd,
276 error_message)
277 bfd *abfd;
278 arelent *reloc_entry;
279 asymbol *symbol;
280 PTR data;
281 asection *input_section;
282 bfd *output_bfd;
283 char **error_message ATTRIBUTE_UNUSED;
285 bfd_vma relocation;
286 bfd_vma x;
288 if (output_bfd != (bfd *) NULL
289 && (symbol->flags & BSF_SECTION_SYM) == 0
290 && (! reloc_entry->howto->partial_inplace
291 || reloc_entry->addend == 0))
293 reloc_entry->address += input_section->output_offset;
294 return bfd_reloc_ok;
297 if (output_bfd != NULL)
298 return bfd_reloc_continue;
300 if (reloc_entry->address > input_section->_cooked_size)
301 return bfd_reloc_outofrange;
303 relocation = (symbol->value
304 + symbol->section->output_section->vma
305 + symbol->section->output_offset);
306 relocation += reloc_entry->addend;
307 relocation -= (input_section->output_section->vma
308 + input_section->output_offset);
309 relocation -= reloc_entry->address;
311 x = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
312 x |= ((((relocation >> 2) & 0xc000) << 6)
313 | ((relocation >> 2) & 0x3fff));
314 bfd_put_32 (abfd, x, (bfd_byte *) data + reloc_entry->address);
316 if ((bfd_signed_vma) relocation < - 0x40000
317 || (bfd_signed_vma) relocation > 0x3ffff)
318 return bfd_reloc_overflow;
319 else
320 return bfd_reloc_ok;
323 /* Functions for the SPARC ELF linker. */
325 /* The name of the dynamic interpreter. This is put in the .interp
326 section. */
328 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
330 /* The nop opcode we use. */
332 #define SPARC_NOP 0x01000000
334 /* The size in bytes of an entry in the procedure linkage table. */
336 #define PLT_ENTRY_SIZE 12
338 /* The first four entries in a procedure linkage table are reserved,
339 and the initial contents are unimportant (we zero them out).
340 Subsequent entries look like this. See the SVR4 ABI SPARC
341 supplement to see how this works. */
343 /* sethi %hi(.-.plt0),%g1. We fill in the address later. */
344 #define PLT_ENTRY_WORD0 0x03000000
345 /* b,a .plt0. We fill in the offset later. */
346 #define PLT_ENTRY_WORD1 0x30800000
347 /* nop. */
348 #define PLT_ENTRY_WORD2 SPARC_NOP
350 /* Look through the relocs for a section during the first phase, and
351 allocate space in the global offset table or procedure linkage
352 table. */
354 static boolean
355 elf32_sparc_check_relocs (abfd, info, sec, relocs)
356 bfd *abfd;
357 struct bfd_link_info *info;
358 asection *sec;
359 const Elf_Internal_Rela *relocs;
361 bfd *dynobj;
362 Elf_Internal_Shdr *symtab_hdr;
363 struct elf_link_hash_entry **sym_hashes;
364 bfd_vma *local_got_offsets;
365 const Elf_Internal_Rela *rel;
366 const Elf_Internal_Rela *rel_end;
367 asection *sgot;
368 asection *srelgot;
369 asection *sreloc;
371 if (info->relocateable)
372 return true;
374 dynobj = elf_hash_table (info)->dynobj;
375 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
376 sym_hashes = elf_sym_hashes (abfd);
377 local_got_offsets = elf_local_got_offsets (abfd);
379 sgot = NULL;
380 srelgot = NULL;
381 sreloc = NULL;
383 rel_end = relocs + sec->reloc_count;
384 for (rel = relocs; rel < rel_end; rel++)
386 unsigned long r_symndx;
387 struct elf_link_hash_entry *h;
389 r_symndx = ELF32_R_SYM (rel->r_info);
390 if (r_symndx < symtab_hdr->sh_info)
391 h = NULL;
392 else
393 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
395 switch (ELF32_R_TYPE (rel->r_info))
397 case R_SPARC_GOT10:
398 case R_SPARC_GOT13:
399 case R_SPARC_GOT22:
400 /* This symbol requires a global offset table entry. */
402 if (dynobj == NULL)
404 /* Create the .got section. */
405 elf_hash_table (info)->dynobj = dynobj = abfd;
406 if (! _bfd_elf_create_got_section (dynobj, info))
407 return false;
410 if (sgot == NULL)
412 sgot = bfd_get_section_by_name (dynobj, ".got");
413 BFD_ASSERT (sgot != NULL);
416 if (srelgot == NULL
417 && (h != NULL || info->shared))
419 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
420 if (srelgot == NULL)
422 srelgot = bfd_make_section (dynobj, ".rela.got");
423 if (srelgot == NULL
424 || ! bfd_set_section_flags (dynobj, srelgot,
425 (SEC_ALLOC
426 | SEC_LOAD
427 | SEC_HAS_CONTENTS
428 | SEC_IN_MEMORY
429 | SEC_LINKER_CREATED
430 | SEC_READONLY))
431 || ! bfd_set_section_alignment (dynobj, srelgot, 2))
432 return false;
436 if (h != NULL)
438 if (h->got.offset != (bfd_vma) -1)
440 /* We have already allocated space in the .got. */
441 break;
443 h->got.offset = sgot->_raw_size;
445 /* Make sure this symbol is output as a dynamic symbol. */
446 if (h->dynindx == -1)
448 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
449 return false;
452 srelgot->_raw_size += sizeof (Elf32_External_Rela);
454 else
456 /* This is a global offset table entry for a local
457 symbol. */
458 if (local_got_offsets == NULL)
460 bfd_size_type size;
461 register unsigned int i;
463 size = symtab_hdr->sh_info;
464 size *= sizeof (bfd_vma);
465 local_got_offsets = (bfd_vma *) bfd_alloc (abfd, size);
466 if (local_got_offsets == NULL)
467 return false;
468 elf_local_got_offsets (abfd) = local_got_offsets;
469 for (i = 0; i < symtab_hdr->sh_info; i++)
470 local_got_offsets[i] = (bfd_vma) -1;
472 if (local_got_offsets[r_symndx] != (bfd_vma) -1)
474 /* We have already allocated space in the .got. */
475 break;
477 local_got_offsets[r_symndx] = sgot->_raw_size;
479 if (info->shared)
481 /* If we are generating a shared object, we need to
482 output a R_SPARC_RELATIVE reloc so that the
483 dynamic linker can adjust this GOT entry. */
484 srelgot->_raw_size += sizeof (Elf32_External_Rela);
488 sgot->_raw_size += 4;
490 /* If the .got section is more than 0x1000 bytes, we add
491 0x1000 to the value of _GLOBAL_OFFSET_TABLE_, so that 13
492 bit relocations have a greater chance of working. */
493 if (sgot->_raw_size >= 0x1000
494 && elf_hash_table (info)->hgot->root.u.def.value == 0)
495 elf_hash_table (info)->hgot->root.u.def.value = 0x1000;
497 break;
499 case R_SPARC_PLT32:
500 case R_SPARC_WPLT30:
501 /* This symbol requires a procedure linkage table entry. We
502 actually build the entry in adjust_dynamic_symbol,
503 because this might be a case of linking PIC code without
504 linking in any dynamic objects, in which case we don't
505 need to generate a procedure linkage table after all. */
507 if (h == NULL)
509 /* The Solaris native assembler will generate a WPLT30
510 reloc for a local symbol if you assemble a call from
511 one section to another when using -K pic. We treat
512 it as WDISP30. */
513 if (ELF32_R_TYPE (rel->r_info) != R_SPARC_WPLT30)
514 goto r_sparc_plt32;
515 break;
518 /* Make sure this symbol is output as a dynamic symbol. */
519 if (h->dynindx == -1)
521 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
522 return false;
525 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
527 if (ELF32_R_TYPE (rel->r_info) != R_SPARC_WPLT30)
528 goto r_sparc_plt32;
529 break;
531 case R_SPARC_PC10:
532 case R_SPARC_PC22:
533 if (h != NULL)
534 h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
536 if (h != NULL
537 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
538 break;
539 /* Fall through. */
540 case R_SPARC_DISP8:
541 case R_SPARC_DISP16:
542 case R_SPARC_DISP32:
543 case R_SPARC_WDISP30:
544 case R_SPARC_WDISP22:
545 case R_SPARC_WDISP19:
546 case R_SPARC_WDISP16:
547 if (h != NULL)
548 h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
550 /* If we are linking with -Bsymbolic, we do not need to copy
551 a PC relative reloc against a global symbol which is
552 defined in an object we are including in the link (i.e.,
553 DEF_REGULAR is set). FIXME: At this point we have not
554 seen all the input files, so it is possible that
555 DEF_REGULAR is not set now but will be set later (it is
556 never cleared). This needs to be handled as in
557 elf32-i386.c. */
558 if (h == NULL
559 || (info->symbolic
560 && (h->elf_link_hash_flags
561 & ELF_LINK_HASH_DEF_REGULAR) != 0))
562 break;
563 /* Fall through. */
564 case R_SPARC_8:
565 case R_SPARC_16:
566 case R_SPARC_32:
567 case R_SPARC_HI22:
568 case R_SPARC_22:
569 case R_SPARC_13:
570 case R_SPARC_LO10:
571 case R_SPARC_UA16:
572 case R_SPARC_UA32:
573 if (h != NULL)
574 h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
576 r_sparc_plt32:
577 if (info->shared && (sec->flags & SEC_ALLOC))
579 /* When creating a shared object, we must copy these
580 relocs into the output file. We create a reloc
581 section in dynobj and make room for the reloc. */
582 if (sreloc == NULL)
584 const char *name;
586 name = (bfd_elf_string_from_elf_section
587 (abfd,
588 elf_elfheader (abfd)->e_shstrndx,
589 elf_section_data (sec)->rel_hdr.sh_name));
590 if (name == NULL)
591 return false;
593 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
594 && strcmp (bfd_get_section_name (abfd, sec),
595 name + 5) == 0);
597 sreloc = bfd_get_section_by_name (dynobj, name);
598 if (sreloc == NULL)
600 flagword flags;
602 sreloc = bfd_make_section (dynobj, name);
603 flags = (SEC_HAS_CONTENTS | SEC_READONLY
604 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
605 if ((sec->flags & SEC_ALLOC) != 0)
606 flags |= SEC_ALLOC | SEC_LOAD;
607 if (sreloc == NULL
608 || ! bfd_set_section_flags (dynobj, sreloc, flags)
609 || ! bfd_set_section_alignment (dynobj, sreloc, 2))
610 return false;
612 if (sec->flags & SEC_READONLY)
613 info->flags |= DF_TEXTREL;
616 sreloc->_raw_size += sizeof (Elf32_External_Rela);
619 break;
621 case R_SPARC_GNU_VTINHERIT:
622 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
623 return false;
624 break;
626 case R_SPARC_GNU_VTENTRY:
627 if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend))
628 return false;
629 break;
631 default:
632 break;
636 return true;
639 static asection *
640 elf32_sparc_gc_mark_hook (sec, info, rel, h, sym)
641 asection *sec;
642 struct bfd_link_info *info ATTRIBUTE_UNUSED;
643 Elf_Internal_Rela *rel;
644 struct elf_link_hash_entry *h;
645 Elf_Internal_Sym *sym;
647 if (h != NULL)
649 switch (ELF32_R_TYPE (rel->r_info))
651 case R_SPARC_GNU_VTINHERIT:
652 case R_SPARC_GNU_VTENTRY:
653 break;
655 default:
656 switch (h->root.type)
658 case bfd_link_hash_defined:
659 case bfd_link_hash_defweak:
660 return h->root.u.def.section;
662 case bfd_link_hash_common:
663 return h->root.u.c.p->section;
665 default:
666 break;
670 else
671 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
673 return NULL;
676 /* Update the got entry reference counts for the section being removed. */
677 static boolean
678 elf32_sparc_gc_sweep_hook (abfd, info, sec, relocs)
679 bfd *abfd;
680 struct bfd_link_info *info ATTRIBUTE_UNUSED;
681 asection *sec;
682 const Elf_Internal_Rela *relocs;
685 Elf_Internal_Shdr *symtab_hdr;
686 struct elf_link_hash_entry **sym_hashes;
687 bfd_signed_vma *local_got_refcounts;
688 const Elf_Internal_Rela *rel, *relend;
689 unsigned long r_symndx;
690 struct elf_link_hash_entry *h;
692 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
693 sym_hashes = elf_sym_hashes (abfd);
694 local_got_refcounts = elf_local_got_refcounts (abfd);
696 relend = relocs + sec->reloc_count;
697 for (rel = relocs; rel < relend; rel++)
698 switch (ELF32_R_TYPE (rel->r_info))
700 case R_SPARC_GOT10:
701 case R_SPARC_GOT13:
702 case R_SPARC_GOT22:
703 r_symndx = ELF32_R_SYM (rel->r_info);
704 if (r_symndx >= symtab_hdr->sh_info)
706 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
707 if (h->got.refcount > 0)
708 h->got.refcount--;
710 else
712 if (local_got_refcounts[r_symndx] > 0)
713 local_got_refcounts[r_symndx]--;
715 break;
717 case R_SPARC_PLT32:
718 case R_SPARC_HIPLT22:
719 case R_SPARC_LOPLT10:
720 case R_SPARC_PCPLT32:
721 case R_SPARC_PCPLT10:
722 r_symndx = ELF32_R_SYM (rel->r_info);
723 if (r_symndx >= symtab_hdr->sh_info)
725 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
726 if (h->plt.refcount > 0)
727 h->plt.refcount--;
729 break;
731 default:
732 break;
735 return true;
738 /* Adjust a symbol defined by a dynamic object and referenced by a
739 regular object. The current definition is in some section of the
740 dynamic object, but we're not including those sections. We have to
741 change the definition to something the rest of the link can
742 understand. */
744 static boolean
745 elf32_sparc_adjust_dynamic_symbol (info, h)
746 struct bfd_link_info *info;
747 struct elf_link_hash_entry *h;
749 bfd *dynobj;
750 asection *s;
751 unsigned int power_of_two;
753 dynobj = elf_hash_table (info)->dynobj;
755 /* Make sure we know what is going on here. */
756 BFD_ASSERT (dynobj != NULL
757 && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
758 || h->weakdef != NULL
759 || ((h->elf_link_hash_flags
760 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
761 && (h->elf_link_hash_flags
762 & ELF_LINK_HASH_REF_REGULAR) != 0
763 && (h->elf_link_hash_flags
764 & ELF_LINK_HASH_DEF_REGULAR) == 0)));
766 /* If this is a function, put it in the procedure linkage table. We
767 will fill in the contents of the procedure linkage table later
768 (although we could actually do it here). The STT_NOTYPE
769 condition is a hack specifically for the Oracle libraries
770 delivered for Solaris; for some inexplicable reason, they define
771 some of their functions as STT_NOTYPE when they really should be
772 STT_FUNC. */
773 if (h->type == STT_FUNC
774 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0
775 || (h->type == STT_NOTYPE
776 && (h->root.type == bfd_link_hash_defined
777 || h->root.type == bfd_link_hash_defweak)
778 && (h->root.u.def.section->flags & SEC_CODE) != 0))
780 if (! elf_hash_table (info)->dynamic_sections_created
781 || ((!info->shared || info->symbolic || h->dynindx == -1)
782 && (h->elf_link_hash_flags
783 & ELF_LINK_HASH_DEF_REGULAR) != 0))
785 /* This case can occur if we saw a WPLT30 reloc in an input
786 file, but none of the input files were dynamic objects.
787 Or, when linking the main application or a -Bsymbolic
788 shared library against PIC code. Or when a global symbol
789 has been made private, e.g. via versioning.
791 In these cases we know what value the symbol will resolve
792 to, so we don't actually need to build a procedure linkage
793 table, and we can just do a WDISP30 reloc instead. */
795 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
796 return true;
799 s = bfd_get_section_by_name (dynobj, ".plt");
800 BFD_ASSERT (s != NULL);
802 /* The first four entries in .plt are reserved. */
803 if (s->_raw_size == 0)
804 s->_raw_size = 4 * PLT_ENTRY_SIZE;
806 /* The procedure linkage table has a maximum size. */
807 if (s->_raw_size >= 0x400000)
809 bfd_set_error (bfd_error_bad_value);
810 return false;
813 /* If this symbol is not defined in a regular file, and we are
814 not generating a shared library, then set the symbol to this
815 location in the .plt. This is required to make function
816 pointers compare as equal between the normal executable and
817 the shared library. */
818 if (! info->shared
819 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
821 h->root.u.def.section = s;
822 h->root.u.def.value = s->_raw_size;
825 h->plt.offset = s->_raw_size;
827 /* Make room for this entry. */
828 s->_raw_size += PLT_ENTRY_SIZE;
830 /* We also need to make an entry in the .rela.plt section. */
832 s = bfd_get_section_by_name (dynobj, ".rela.plt");
833 BFD_ASSERT (s != NULL);
834 s->_raw_size += sizeof (Elf32_External_Rela);
836 return true;
839 /* If this is a weak symbol, and there is a real definition, the
840 processor independent code will have arranged for us to see the
841 real definition first, and we can just use the same value. */
842 if (h->weakdef != NULL)
844 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
845 || h->weakdef->root.type == bfd_link_hash_defweak);
846 h->root.u.def.section = h->weakdef->root.u.def.section;
847 h->root.u.def.value = h->weakdef->root.u.def.value;
848 return true;
851 /* This is a reference to a symbol defined by a dynamic object which
852 is not a function. */
854 /* If we are creating a shared library, we must presume that the
855 only references to the symbol are via the global offset table.
856 For such cases we need not do anything here; the relocations will
857 be handled correctly by relocate_section. */
858 if (info->shared)
859 return true;
861 /* If there are no references to this symbol that do not use the
862 GOT, we don't need to generate a copy reloc. */
863 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
864 return true;
866 /* We must allocate the symbol in our .dynbss section, which will
867 become part of the .bss section of the executable. There will be
868 an entry for this symbol in the .dynsym section. The dynamic
869 object will contain position independent code, so all references
870 from the dynamic object to this symbol will go through the global
871 offset table. The dynamic linker will use the .dynsym entry to
872 determine the address it must put in the global offset table, so
873 both the dynamic object and the regular object will refer to the
874 same memory location for the variable. */
876 s = bfd_get_section_by_name (dynobj, ".dynbss");
877 BFD_ASSERT (s != NULL);
879 /* We must generate a R_SPARC_COPY reloc to tell the dynamic linker
880 to copy the initial value out of the dynamic object and into the
881 runtime process image. We need to remember the offset into the
882 .rel.bss section we are going to use. */
883 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
885 asection *srel;
887 srel = bfd_get_section_by_name (dynobj, ".rela.bss");
888 BFD_ASSERT (srel != NULL);
889 srel->_raw_size += sizeof (Elf32_External_Rela);
890 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
893 /* We need to figure out the alignment required for this symbol. I
894 have no idea how ELF linkers handle this. */
895 power_of_two = bfd_log2 (h->size);
896 if (power_of_two > 3)
897 power_of_two = 3;
899 /* Apply the required alignment. */
900 s->_raw_size = BFD_ALIGN (s->_raw_size,
901 (bfd_size_type) (1 << power_of_two));
902 if (power_of_two > bfd_get_section_alignment (dynobj, s))
904 if (! bfd_set_section_alignment (dynobj, s, power_of_two))
905 return false;
908 /* Define the symbol as being at this point in the section. */
909 h->root.u.def.section = s;
910 h->root.u.def.value = s->_raw_size;
912 /* Increment the section size to make room for the symbol. */
913 s->_raw_size += h->size;
915 return true;
918 /* Set the sizes of the dynamic sections. */
920 static boolean
921 elf32_sparc_size_dynamic_sections (output_bfd, info)
922 bfd *output_bfd ATTRIBUTE_UNUSED;
923 struct bfd_link_info *info;
925 bfd *dynobj;
926 asection *s;
927 boolean relplt;
929 dynobj = elf_hash_table (info)->dynobj;
930 BFD_ASSERT (dynobj != NULL);
932 if (elf_hash_table (info)->dynamic_sections_created)
934 /* Set the contents of the .interp section to the interpreter. */
935 if (! info->shared)
937 s = bfd_get_section_by_name (dynobj, ".interp");
938 BFD_ASSERT (s != NULL);
939 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
940 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
943 /* Make space for the trailing nop in .plt. */
944 s = bfd_get_section_by_name (dynobj, ".plt");
945 BFD_ASSERT (s != NULL);
946 if (s->_raw_size > 0)
947 s->_raw_size += 4;
949 else
951 /* We may have created entries in the .rela.got section.
952 However, if we are not creating the dynamic sections, we will
953 not actually use these entries. Reset the size of .rela.got,
954 which will cause it to get stripped from the output file
955 below. */
956 s = bfd_get_section_by_name (dynobj, ".rela.got");
957 if (s != NULL)
958 s->_raw_size = 0;
961 /* The check_relocs and adjust_dynamic_symbol entry points have
962 determined the sizes of the various dynamic sections. Allocate
963 memory for them. */
964 relplt = false;
965 for (s = dynobj->sections; s != NULL; s = s->next)
967 const char *name;
968 boolean strip;
970 if ((s->flags & SEC_LINKER_CREATED) == 0)
971 continue;
973 /* It's OK to base decisions on the section name, because none
974 of the dynobj section names depend upon the input files. */
975 name = bfd_get_section_name (dynobj, s);
977 strip = false;
979 if (strncmp (name, ".rela", 5) == 0)
981 if (s->_raw_size == 0)
983 /* If we don't need this section, strip it from the
984 output file. This is to handle .rela.bss and
985 .rel.plt. We must create it in
986 create_dynamic_sections, because it must be created
987 before the linker maps input sections to output
988 sections. The linker does that before
989 adjust_dynamic_symbol is called, and it is that
990 function which decides whether anything needs to go
991 into these sections. */
992 strip = true;
994 else
996 if (strcmp (name, ".rela.plt") == 0)
997 relplt = true;
999 /* We use the reloc_count field as a counter if we need
1000 to copy relocs into the output file. */
1001 s->reloc_count = 0;
1004 else if (strcmp (name, ".plt") != 0
1005 && strcmp (name, ".got") != 0)
1007 /* It's not one of our sections, so don't allocate space. */
1008 continue;
1011 if (strip)
1013 _bfd_strip_section_from_output (info, s);
1014 continue;
1017 /* Allocate memory for the section contents. */
1018 /* FIXME: This should be a call to bfd_alloc not bfd_zalloc.
1019 Unused entries should be reclaimed before the section's contents
1020 are written out, but at the moment this does not happen. Thus in
1021 order to prevent writing out garbage, we initialise the section's
1022 contents to zero. */
1023 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
1024 if (s->contents == NULL && s->_raw_size != 0)
1025 return false;
1028 if (elf_hash_table (info)->dynamic_sections_created)
1030 /* Add some entries to the .dynamic section. We fill in the
1031 values later, in elf32_sparc_finish_dynamic_sections, but we
1032 must add the entries now so that we get the correct size for
1033 the .dynamic section. The DT_DEBUG entry is filled in by the
1034 dynamic linker and used by the debugger. */
1035 #define add_dynamic_entry(TAG, VAL) \
1036 bfd_elf32_add_dynamic_entry (info, (bfd_vma) (TAG), (bfd_vma) (VAL))
1038 if (!info->shared)
1040 if (!add_dynamic_entry (DT_DEBUG, 0))
1041 return false;
1044 if (relplt)
1046 if (!add_dynamic_entry (DT_PLTGOT, 0)
1047 || !add_dynamic_entry (DT_PLTRELSZ, 0)
1048 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
1049 || !add_dynamic_entry (DT_JMPREL, 0))
1050 return false;
1053 if (!add_dynamic_entry (DT_RELA, 0)
1054 || !add_dynamic_entry (DT_RELASZ, 0)
1055 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
1056 return false;
1058 if (info->flags & DF_TEXTREL)
1060 if (!add_dynamic_entry (DT_TEXTREL, 0))
1061 return false;
1064 #undef add_dynamic_entry
1066 return true;
1069 #define SET_SEC_DO_RELAX(section) do { elf_section_data(section)->tdata = (void *)1; } while (0)
1070 #define SEC_DO_RELAX(section) (elf_section_data(section)->tdata == (void *)1)
1072 static boolean
1073 elf32_sparc_relax_section (abfd, section, link_info, again)
1074 bfd *abfd ATTRIBUTE_UNUSED;
1075 asection *section ATTRIBUTE_UNUSED;
1076 struct bfd_link_info *link_info ATTRIBUTE_UNUSED;
1077 boolean *again;
1079 *again = false;
1080 SET_SEC_DO_RELAX (section);
1081 return true;
1084 /* This is the condition under which finish_dynamic_symbol will be called
1085 from elflink.h. If elflink.h doesn't call our finish_dynamic_symbol
1086 routine, we'll need to do something about initializing any .plt and .got
1087 entries in relocate_section. */
1088 #define WILL_CALL_FINISH_DYNAMIC_SYMBOL(DYN, INFO, H) \
1089 ((DYN) \
1090 && ((INFO)->shared \
1091 || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0) \
1092 && ((H)->dynindx != -1 \
1093 || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0))
1095 /* Relocate a SPARC ELF section. */
1097 static boolean
1098 elf32_sparc_relocate_section (output_bfd, info, input_bfd, input_section,
1099 contents, relocs, local_syms, local_sections)
1100 bfd *output_bfd;
1101 struct bfd_link_info *info;
1102 bfd *input_bfd;
1103 asection *input_section;
1104 bfd_byte *contents;
1105 Elf_Internal_Rela *relocs;
1106 Elf_Internal_Sym *local_syms;
1107 asection **local_sections;
1109 bfd *dynobj;
1110 Elf_Internal_Shdr *symtab_hdr;
1111 struct elf_link_hash_entry **sym_hashes;
1112 bfd_vma *local_got_offsets;
1113 bfd_vma got_base;
1114 asection *sgot;
1115 asection *splt;
1116 asection *sreloc;
1117 Elf_Internal_Rela *rel;
1118 Elf_Internal_Rela *relend;
1120 if (info->relocateable)
1121 return true;
1123 dynobj = elf_hash_table (info)->dynobj;
1124 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1125 sym_hashes = elf_sym_hashes (input_bfd);
1126 local_got_offsets = elf_local_got_offsets (input_bfd);
1128 if (elf_hash_table (info)->hgot == NULL)
1129 got_base = 0;
1130 else
1131 got_base = elf_hash_table (info)->hgot->root.u.def.value;
1133 sgot = NULL;
1134 splt = NULL;
1135 sreloc = NULL;
1137 rel = relocs;
1138 relend = relocs + input_section->reloc_count;
1139 for (; rel < relend; rel++)
1141 int r_type;
1142 reloc_howto_type *howto;
1143 unsigned long r_symndx;
1144 struct elf_link_hash_entry *h;
1145 Elf_Internal_Sym *sym;
1146 asection *sec;
1147 bfd_vma relocation, off;
1148 bfd_reloc_status_type r;
1149 boolean is_plt = false;
1150 boolean unresolved_reloc;
1152 r_type = ELF32_R_TYPE (rel->r_info);
1154 if (r_type == R_SPARC_GNU_VTINHERIT
1155 || r_type == R_SPARC_GNU_VTENTRY)
1156 continue;
1158 if (r_type < 0 || r_type >= (int) R_SPARC_max_std)
1160 bfd_set_error (bfd_error_bad_value);
1161 return false;
1163 howto = _bfd_sparc_elf_howto_table + r_type;
1165 /* This is a final link. */
1166 r_symndx = ELF32_R_SYM (rel->r_info);
1167 h = NULL;
1168 sym = NULL;
1169 sec = NULL;
1170 unresolved_reloc = false;
1171 if (r_symndx < symtab_hdr->sh_info)
1173 sym = local_syms + r_symndx;
1174 sec = local_sections[r_symndx];
1175 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, sec, rel);
1177 else
1179 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1180 while (h->root.type == bfd_link_hash_indirect
1181 || h->root.type == bfd_link_hash_warning)
1182 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1184 relocation = 0;
1185 if (h->root.type == bfd_link_hash_defined
1186 || h->root.type == bfd_link_hash_defweak)
1188 sec = h->root.u.def.section;
1189 if (sec->output_section == NULL)
1190 /* Set a flag that will be cleared later if we find a
1191 relocation value for this symbol. output_section
1192 is typically NULL for symbols satisfied by a shared
1193 library. */
1194 unresolved_reloc = true;
1195 else
1196 relocation = (h->root.u.def.value
1197 + sec->output_section->vma
1198 + sec->output_offset);
1200 else if (h->root.type == bfd_link_hash_undefweak)
1202 else if (info->shared
1203 && (!info->symbolic || info->allow_shlib_undefined)
1204 && !info->no_undefined
1205 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
1207 else
1209 if (! ((*info->callbacks->undefined_symbol)
1210 (info, h->root.root.string, input_bfd,
1211 input_section, rel->r_offset,
1212 (!info->shared || info->no_undefined
1213 || ELF_ST_VISIBILITY (h->other)))))
1214 return false;
1218 switch (r_type)
1220 case R_SPARC_GOT10:
1221 case R_SPARC_GOT13:
1222 case R_SPARC_GOT22:
1223 /* Relocation is to the entry for this symbol in the global
1224 offset table. */
1225 if (sgot == NULL)
1227 sgot = bfd_get_section_by_name (dynobj, ".got");
1228 BFD_ASSERT (sgot != NULL);
1231 if (h != NULL)
1233 boolean dyn;
1235 off = h->got.offset;
1236 BFD_ASSERT (off != (bfd_vma) -1);
1237 dyn = elf_hash_table (info)->dynamic_sections_created;
1239 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info, h)
1240 || (info->shared
1241 && (info->symbolic
1242 || h->dynindx == -1
1243 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL))
1244 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
1246 /* This is actually a static link, or it is a
1247 -Bsymbolic link and the symbol is defined
1248 locally, or the symbol was forced to be local
1249 because of a version file. We must initialize
1250 this entry in the global offset table. Since the
1251 offset must always be a multiple of 4, we use the
1252 least significant bit to record whether we have
1253 initialized it already.
1255 When doing a dynamic link, we create a .rela.got
1256 relocation entry to initialize the value. This
1257 is done in the finish_dynamic_symbol routine. */
1258 if ((off & 1) != 0)
1259 off &= ~1;
1260 else
1262 bfd_put_32 (output_bfd, relocation,
1263 sgot->contents + off);
1264 h->got.offset |= 1;
1267 else
1268 unresolved_reloc = false;
1270 else
1272 BFD_ASSERT (local_got_offsets != NULL
1273 && local_got_offsets[r_symndx] != (bfd_vma) -1);
1275 off = local_got_offsets[r_symndx];
1277 /* The offset must always be a multiple of 4. We use
1278 the least significant bit to record whether we have
1279 already processed this entry. */
1280 if ((off & 1) != 0)
1281 off &= ~1;
1282 else
1285 if (info->shared)
1287 asection *srelgot;
1288 Elf_Internal_Rela outrel;
1290 /* We need to generate a R_SPARC_RELATIVE reloc
1291 for the dynamic linker. */
1292 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
1293 BFD_ASSERT (srelgot != NULL);
1295 outrel.r_offset = (sgot->output_section->vma
1296 + sgot->output_offset
1297 + off);
1298 outrel.r_info = ELF32_R_INFO (0, R_SPARC_RELATIVE);
1299 outrel.r_addend = relocation;
1300 relocation = 0;
1301 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1302 (((Elf32_External_Rela *)
1303 srelgot->contents)
1304 + srelgot->reloc_count));
1305 ++srelgot->reloc_count;
1308 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
1309 local_got_offsets[r_symndx] |= 1;
1312 relocation = sgot->output_offset + off - got_base;
1313 break;
1315 case R_SPARC_PLT32:
1316 if (h == NULL || h->plt.offset == (bfd_vma) -1)
1318 r_type = R_SPARC_32;
1319 goto r_sparc_plt32;
1321 /* Fall through. */
1322 case R_SPARC_WPLT30:
1323 /* Relocation is to the entry for this symbol in the
1324 procedure linkage table. */
1326 /* The Solaris native assembler will generate a WPLT30 reloc
1327 for a local symbol if you assemble a call from one
1328 section to another when using -K pic. We treat it as
1329 WDISP30. */
1330 if (h == NULL)
1331 break;
1333 if (h->plt.offset == (bfd_vma) -1)
1335 /* We didn't make a PLT entry for this symbol. This
1336 happens when statically linking PIC code, or when
1337 using -Bsymbolic. */
1338 break;
1341 if (splt == NULL)
1343 splt = bfd_get_section_by_name (dynobj, ".plt");
1344 BFD_ASSERT (splt != NULL);
1347 relocation = (splt->output_section->vma
1348 + splt->output_offset
1349 + h->plt.offset);
1350 unresolved_reloc = false;
1351 if (r_type == R_SPARC_PLT32)
1353 r_type = R_SPARC_32;
1354 is_plt = true;
1355 goto r_sparc_plt32;
1357 break;
1359 case R_SPARC_PC10:
1360 case R_SPARC_PC22:
1361 if (h != NULL
1362 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
1363 break;
1364 /* Fall through. */
1365 case R_SPARC_DISP8:
1366 case R_SPARC_DISP16:
1367 case R_SPARC_DISP32:
1368 case R_SPARC_WDISP30:
1369 case R_SPARC_WDISP22:
1370 case R_SPARC_WDISP19:
1371 case R_SPARC_WDISP16:
1372 if (h == NULL
1373 || (info->symbolic
1374 && (h->elf_link_hash_flags
1375 & ELF_LINK_HASH_DEF_REGULAR) != 0))
1376 break;
1377 /* Fall through. */
1378 case R_SPARC_8:
1379 case R_SPARC_16:
1380 case R_SPARC_32:
1381 case R_SPARC_HI22:
1382 case R_SPARC_22:
1383 case R_SPARC_13:
1384 case R_SPARC_LO10:
1385 case R_SPARC_UA16:
1386 case R_SPARC_UA32:
1387 r_sparc_plt32:
1388 if (info->shared
1389 && r_symndx != 0
1390 && (input_section->flags & SEC_ALLOC))
1392 Elf_Internal_Rela outrel;
1393 boolean skip, relocate = false;
1395 /* When generating a shared object, these relocations
1396 are copied into the output file to be resolved at run
1397 time. */
1399 if (sreloc == NULL)
1401 const char *name;
1403 name = (bfd_elf_string_from_elf_section
1404 (input_bfd,
1405 elf_elfheader (input_bfd)->e_shstrndx,
1406 elf_section_data (input_section)->rel_hdr.sh_name));
1407 if (name == NULL)
1408 return false;
1410 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
1411 && strcmp (bfd_get_section_name (input_bfd,
1412 input_section),
1413 name + 5) == 0);
1415 sreloc = bfd_get_section_by_name (dynobj, name);
1416 BFD_ASSERT (sreloc != NULL);
1419 skip = false;
1421 outrel.r_offset =
1422 _bfd_elf_section_offset (output_bfd, info, input_section,
1423 rel->r_offset);
1424 if (outrel.r_offset == (bfd_vma) -1)
1425 skip = true;
1426 else if (outrel.r_offset == (bfd_vma) -2)
1427 skip = true, relocate = true;
1428 outrel.r_offset += (input_section->output_section->vma
1429 + input_section->output_offset);
1431 /* Optimize unaligned reloc usage now that we know where
1432 it finally resides. */
1433 switch (r_type)
1435 case R_SPARC_16:
1436 if (outrel.r_offset & 1)
1437 r_type = R_SPARC_UA16;
1438 break;
1439 case R_SPARC_UA16:
1440 if (!(outrel.r_offset & 1))
1441 r_type = R_SPARC_16;
1442 break;
1443 case R_SPARC_32:
1444 if (outrel.r_offset & 3)
1445 r_type = R_SPARC_UA32;
1446 break;
1447 case R_SPARC_UA32:
1448 if (!(outrel.r_offset & 3))
1449 r_type = R_SPARC_32;
1450 break;
1451 case R_SPARC_DISP8:
1452 case R_SPARC_DISP16:
1453 case R_SPARC_DISP32:
1454 /* If the symbol is not dynamic, we should not keep
1455 a dynamic relocation. But an .rela.* slot has been
1456 allocated for it, output R_SPARC_NONE.
1457 FIXME: Add code tracking needed dynamic relocs as
1458 e.g. i386 has. */
1459 if (h->dynindx == -1)
1460 skip = true, relocate = true;
1461 break;
1464 if (skip)
1465 memset (&outrel, 0, sizeof outrel);
1466 /* h->dynindx may be -1 if the symbol was marked to
1467 become local. */
1468 else if (h != NULL && ! is_plt
1469 && ((! info->symbolic && h->dynindx != -1)
1470 || (h->elf_link_hash_flags
1471 & ELF_LINK_HASH_DEF_REGULAR) == 0))
1473 BFD_ASSERT (h->dynindx != -1);
1474 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
1475 outrel.r_addend = rel->r_addend;
1477 else
1479 if (r_type == R_SPARC_32)
1481 outrel.r_info = ELF32_R_INFO (0, R_SPARC_RELATIVE);
1482 outrel.r_addend = relocation + rel->r_addend;
1484 else
1486 long indx;
1488 if (is_plt)
1489 sec = splt;
1490 else if (h == NULL)
1491 sec = local_sections[r_symndx];
1492 else
1494 BFD_ASSERT (h->root.type == bfd_link_hash_defined
1495 || (h->root.type
1496 == bfd_link_hash_defweak));
1497 sec = h->root.u.def.section;
1499 if (sec != NULL && bfd_is_abs_section (sec))
1500 indx = 0;
1501 else if (sec == NULL || sec->owner == NULL)
1503 bfd_set_error (bfd_error_bad_value);
1504 return false;
1506 else
1508 asection *osec;
1510 osec = sec->output_section;
1511 indx = elf_section_data (osec)->dynindx;
1513 /* FIXME: we really should be able to link non-pic
1514 shared libraries. */
1515 if (indx == 0)
1517 BFD_FAIL ();
1518 (*_bfd_error_handler)
1519 (_("%s: probably compiled without -fPIC?"),
1520 bfd_archive_filename (input_bfd));
1521 bfd_set_error (bfd_error_bad_value);
1522 return false;
1526 outrel.r_info = ELF32_R_INFO (indx, r_type);
1527 outrel.r_addend = relocation + rel->r_addend;
1531 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1532 (((Elf32_External_Rela *)
1533 sreloc->contents)
1534 + sreloc->reloc_count));
1535 ++sreloc->reloc_count;
1537 /* This reloc will be computed at runtime, so there's no
1538 need to do anything now. */
1539 if (! relocate)
1540 continue;
1542 break;
1544 default:
1545 break;
1548 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
1549 because such sections are not SEC_ALLOC and thus ld.so will
1550 not process them. */
1551 if (unresolved_reloc
1552 && !((input_section->flags & SEC_DEBUGGING) != 0
1553 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0))
1554 (*_bfd_error_handler)
1555 (_("%s(%s+0x%lx): unresolvable relocation against symbol `%s'"),
1556 bfd_archive_filename (input_bfd),
1557 bfd_get_section_name (input_bfd, input_section),
1558 (long) rel->r_offset,
1559 h->root.root.string);
1561 r = bfd_reloc_continue;
1562 if (r_type == R_SPARC_WDISP16)
1564 bfd_vma x;
1566 relocation += rel->r_addend;
1567 relocation -= (input_section->output_section->vma
1568 + input_section->output_offset);
1569 relocation -= rel->r_offset;
1571 x = bfd_get_32 (input_bfd, contents + rel->r_offset);
1572 x |= ((((relocation >> 2) & 0xc000) << 6)
1573 | ((relocation >> 2) & 0x3fff));
1574 bfd_put_32 (input_bfd, x, contents + rel->r_offset);
1576 if ((bfd_signed_vma) relocation < - 0x40000
1577 || (bfd_signed_vma) relocation > 0x3ffff)
1578 r = bfd_reloc_overflow;
1579 else
1580 r = bfd_reloc_ok;
1582 else if (r_type == R_SPARC_REV32)
1584 bfd_vma x;
1586 relocation = relocation + rel->r_addend;
1588 x = bfd_get_32 (input_bfd, contents + rel->r_offset);
1589 x = x + relocation;
1590 bfd_putl32 (/*input_bfd,*/ x, contents + rel->r_offset);
1591 r = bfd_reloc_ok;
1593 else if ((r_type == R_SPARC_WDISP30 || r_type == R_SPARC_WPLT30)
1594 && SEC_DO_RELAX (input_section)
1595 && rel->r_offset + 4 < input_section->_raw_size)
1597 #define G0 0
1598 #define O7 15
1599 #define XCC (2 << 20)
1600 #define COND(x) (((x)&0xf)<<25)
1601 #define CONDA COND(0x8)
1602 #define INSN_BPA (F2(0,1) | CONDA | BPRED | XCC)
1603 #define INSN_BA (F2(0,2) | CONDA)
1604 #define INSN_OR F3(2, 0x2, 0)
1605 #define INSN_NOP F2(0,4)
1607 bfd_vma x, y;
1609 /* If the instruction is a call with either:
1610 restore
1611 arithmetic instruction with rd == %o7
1612 where rs1 != %o7 and rs2 if it is register != %o7
1613 then we can optimize if the call destination is near
1614 by changing the call into a branch always. */
1615 x = bfd_get_32 (input_bfd, contents + rel->r_offset);
1616 y = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
1617 if ((x & OP(~0)) == OP(1) && (y & OP(~0)) == OP(2))
1619 if (((y & OP3(~0)) == OP3(0x3d) /* restore */
1620 || ((y & OP3(0x28)) == 0 /* arithmetic */
1621 && (y & RD(~0)) == RD(O7)))
1622 && (y & RS1(~0)) != RS1(O7)
1623 && ((y & F3I(~0))
1624 || (y & RS2(~0)) != RS2(O7)))
1626 bfd_vma reloc;
1628 reloc = relocation + rel->r_addend - rel->r_offset;
1629 reloc -= (input_section->output_section->vma
1630 + input_section->output_offset);
1632 /* Ensure the reloc fits into simm22. */
1633 if ((reloc & 3) == 0
1634 && ((reloc & ~(bfd_vma)0x7fffff) == 0
1635 || ((reloc | 0x7fffff) == ~(bfd_vma)0)))
1637 reloc >>= 2;
1639 /* Check whether it fits into simm19 on v9. */
1640 if (((reloc & 0x3c0000) == 0
1641 || (reloc & 0x3c0000) == 0x3c0000)
1642 && (elf_elfheader (output_bfd)->e_flags & EF_SPARC_32PLUS))
1643 x = INSN_BPA | (reloc & 0x7ffff); /* ba,pt %xcc */
1644 else
1645 x = INSN_BA | (reloc & 0x3fffff); /* ba */
1646 bfd_put_32 (input_bfd, x, contents + rel->r_offset);
1647 r = bfd_reloc_ok;
1648 if (rel->r_offset >= 4
1649 && (y & (0xffffffff ^ RS1(~0)))
1650 == (INSN_OR | RD(O7) | RS2(G0)))
1652 bfd_vma z;
1653 unsigned int reg;
1655 z = bfd_get_32 (input_bfd,
1656 contents + rel->r_offset - 4);
1657 if ((z & (0xffffffff ^ RD(~0)))
1658 != (INSN_OR | RS1(O7) | RS2(G0)))
1659 break;
1661 /* The sequence was
1662 or %o7, %g0, %rN
1663 call foo
1664 or %rN, %g0, %o7
1666 If call foo was replaced with ba, replace
1667 or %rN, %g0, %o7 with nop. */
1669 reg = (y & RS1(~0)) >> 14;
1670 if (reg != ((z & RD(~0)) >> 25)
1671 || reg == G0 || reg == O7)
1672 break;
1674 bfd_put_32 (input_bfd, (bfd_vma) INSN_NOP,
1675 contents + rel->r_offset + 4);
1683 if (r == bfd_reloc_continue)
1684 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1685 contents, rel->r_offset,
1686 relocation, rel->r_addend);
1688 if (r != bfd_reloc_ok)
1690 switch (r)
1692 default:
1693 case bfd_reloc_outofrange:
1694 abort ();
1695 case bfd_reloc_overflow:
1697 const char *name;
1699 if (h != NULL)
1700 name = h->root.root.string;
1701 else
1703 name = bfd_elf_string_from_elf_section (input_bfd,
1704 symtab_hdr->sh_link,
1705 sym->st_name);
1706 if (name == NULL)
1707 return false;
1708 if (*name == '\0')
1709 name = bfd_section_name (input_bfd, sec);
1711 if (! ((*info->callbacks->reloc_overflow)
1712 (info, name, howto->name, (bfd_vma) 0,
1713 input_bfd, input_section, rel->r_offset)))
1714 return false;
1716 break;
1721 return true;
1724 /* Finish up dynamic symbol handling. We set the contents of various
1725 dynamic sections here. */
1727 static boolean
1728 elf32_sparc_finish_dynamic_symbol (output_bfd, info, h, sym)
1729 bfd *output_bfd;
1730 struct bfd_link_info *info;
1731 struct elf_link_hash_entry *h;
1732 Elf_Internal_Sym *sym;
1734 bfd *dynobj;
1736 dynobj = elf_hash_table (info)->dynobj;
1738 if (h->plt.offset != (bfd_vma) -1)
1740 asection *splt;
1741 asection *srela;
1742 Elf_Internal_Rela rela;
1744 /* This symbol has an entry in the procedure linkage table. Set
1745 it up. */
1747 BFD_ASSERT (h->dynindx != -1);
1749 splt = bfd_get_section_by_name (dynobj, ".plt");
1750 srela = bfd_get_section_by_name (dynobj, ".rela.plt");
1751 BFD_ASSERT (splt != NULL && srela != NULL);
1753 /* Fill in the entry in the procedure linkage table. */
1754 bfd_put_32 (output_bfd,
1755 PLT_ENTRY_WORD0 + h->plt.offset,
1756 splt->contents + h->plt.offset);
1757 bfd_put_32 (output_bfd,
1758 (PLT_ENTRY_WORD1
1759 + (((- (h->plt.offset + 4)) >> 2) & 0x3fffff)),
1760 splt->contents + h->plt.offset + 4);
1761 bfd_put_32 (output_bfd, (bfd_vma) PLT_ENTRY_WORD2,
1762 splt->contents + h->plt.offset + 8);
1764 /* Fill in the entry in the .rela.plt section. */
1765 rela.r_offset = (splt->output_section->vma
1766 + splt->output_offset
1767 + h->plt.offset);
1768 rela.r_info = ELF32_R_INFO (h->dynindx, R_SPARC_JMP_SLOT);
1769 rela.r_addend = 0;
1770 bfd_elf32_swap_reloca_out (output_bfd, &rela,
1771 ((Elf32_External_Rela *) srela->contents
1772 + h->plt.offset / PLT_ENTRY_SIZE - 4));
1774 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1776 /* Mark the symbol as undefined, rather than as defined in
1777 the .plt section. Leave the value alone. */
1778 sym->st_shndx = SHN_UNDEF;
1779 /* If the symbol is weak, we do need to clear the value.
1780 Otherwise, the PLT entry would provide a definition for
1781 the symbol even if the symbol wasn't defined anywhere,
1782 and so the symbol would never be NULL. */
1783 if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR_NONWEAK)
1784 == 0)
1785 sym->st_value = 0;
1789 if (h->got.offset != (bfd_vma) -1)
1791 asection *sgot;
1792 asection *srela;
1793 Elf_Internal_Rela rela;
1795 /* This symbol has an entry in the global offset table. Set it
1796 up. */
1798 sgot = bfd_get_section_by_name (dynobj, ".got");
1799 srela = bfd_get_section_by_name (dynobj, ".rela.got");
1800 BFD_ASSERT (sgot != NULL && srela != NULL);
1802 rela.r_offset = (sgot->output_section->vma
1803 + sgot->output_offset
1804 + (h->got.offset &~ (bfd_vma) 1));
1806 /* If this is a -Bsymbolic link, and the symbol is defined
1807 locally, we just want to emit a RELATIVE reloc. Likewise if
1808 the symbol was forced to be local because of a version file.
1809 The entry in the global offset table will already have been
1810 initialized in the relocate_section function. */
1811 if (info->shared
1812 && (info->symbolic || h->dynindx == -1)
1813 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
1815 asection *sec = h->root.u.def.section;
1816 rela.r_info = ELF32_R_INFO (0, R_SPARC_RELATIVE);
1817 rela.r_addend = (h->root.u.def.value
1818 + sec->output_section->vma
1819 + sec->output_offset);
1821 else
1823 rela.r_info = ELF32_R_INFO (h->dynindx, R_SPARC_GLOB_DAT);
1824 rela.r_addend = 0;
1827 bfd_put_32 (output_bfd, (bfd_vma) 0,
1828 sgot->contents + (h->got.offset &~ (bfd_vma) 1));
1829 bfd_elf32_swap_reloca_out (output_bfd, &rela,
1830 ((Elf32_External_Rela *) srela->contents
1831 + srela->reloc_count));
1832 ++srela->reloc_count;
1835 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
1837 asection *s;
1838 Elf_Internal_Rela rela;
1840 /* This symbols needs a copy reloc. Set it up. */
1842 BFD_ASSERT (h->dynindx != -1);
1844 s = bfd_get_section_by_name (h->root.u.def.section->owner,
1845 ".rela.bss");
1846 BFD_ASSERT (s != NULL);
1848 rela.r_offset = (h->root.u.def.value
1849 + h->root.u.def.section->output_section->vma
1850 + h->root.u.def.section->output_offset);
1851 rela.r_info = ELF32_R_INFO (h->dynindx, R_SPARC_COPY);
1852 rela.r_addend = 0;
1853 bfd_elf32_swap_reloca_out (output_bfd, &rela,
1854 ((Elf32_External_Rela *) s->contents
1855 + s->reloc_count));
1856 ++s->reloc_count;
1859 /* Mark some specially defined symbols as absolute. */
1860 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
1861 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
1862 || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0)
1863 sym->st_shndx = SHN_ABS;
1865 return true;
1868 /* Finish up the dynamic sections. */
1870 static boolean
1871 elf32_sparc_finish_dynamic_sections (output_bfd, info)
1872 bfd *output_bfd;
1873 struct bfd_link_info *info;
1875 bfd *dynobj;
1876 asection *sdyn;
1877 asection *sgot;
1879 dynobj = elf_hash_table (info)->dynobj;
1881 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
1883 if (elf_hash_table (info)->dynamic_sections_created)
1885 asection *splt;
1886 Elf32_External_Dyn *dyncon, *dynconend;
1888 splt = bfd_get_section_by_name (dynobj, ".plt");
1889 BFD_ASSERT (splt != NULL && sdyn != NULL);
1891 dyncon = (Elf32_External_Dyn *) sdyn->contents;
1892 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
1893 for (; dyncon < dynconend; dyncon++)
1895 Elf_Internal_Dyn dyn;
1896 const char *name;
1897 boolean size;
1899 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
1901 switch (dyn.d_tag)
1903 case DT_PLTGOT: name = ".plt"; size = false; break;
1904 case DT_PLTRELSZ: name = ".rela.plt"; size = true; break;
1905 case DT_JMPREL: name = ".rela.plt"; size = false; break;
1906 default: name = NULL; size = false; break;
1909 if (name != NULL)
1911 asection *s;
1913 s = bfd_get_section_by_name (output_bfd, name);
1914 if (s == NULL)
1915 dyn.d_un.d_val = 0;
1916 else
1918 if (! size)
1919 dyn.d_un.d_ptr = s->vma;
1920 else
1922 if (s->_cooked_size != 0)
1923 dyn.d_un.d_val = s->_cooked_size;
1924 else
1925 dyn.d_un.d_val = s->_raw_size;
1928 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1932 /* Clear the first four entries in the procedure linkage table,
1933 and put a nop in the last four bytes. */
1934 if (splt->_raw_size > 0)
1936 memset (splt->contents, 0, 4 * PLT_ENTRY_SIZE);
1937 bfd_put_32 (output_bfd, (bfd_vma) SPARC_NOP,
1938 splt->contents + splt->_raw_size - 4);
1941 elf_section_data (splt->output_section)->this_hdr.sh_entsize =
1942 PLT_ENTRY_SIZE;
1945 /* Set the first entry in the global offset table to the address of
1946 the dynamic section. */
1947 sgot = bfd_get_section_by_name (dynobj, ".got");
1948 BFD_ASSERT (sgot != NULL);
1949 if (sgot->_raw_size > 0)
1951 if (sdyn == NULL)
1952 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
1953 else
1954 bfd_put_32 (output_bfd,
1955 sdyn->output_section->vma + sdyn->output_offset,
1956 sgot->contents);
1959 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
1961 return true;
1964 /* Functions for dealing with the e_flags field.
1966 We don't define set_private_flags or copy_private_bfd_data because
1967 the only currently defined values are based on the bfd mach number,
1968 so we use the latter instead and defer setting e_flags until the
1969 file is written out. */
1971 /* Merge backend specific data from an object file to the output
1972 object file when linking. */
1974 static boolean
1975 elf32_sparc_merge_private_bfd_data (ibfd, obfd)
1976 bfd *ibfd;
1977 bfd *obfd;
1979 boolean error;
1980 /* FIXME: This should not be static. */
1981 static unsigned long previous_ibfd_e_flags = (unsigned long) -1;
1983 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1984 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1985 return true;
1987 error = false;
1989 if (bfd_get_mach (ibfd) >= bfd_mach_sparc_v9)
1991 error = true;
1992 (*_bfd_error_handler)
1993 (_("%s: compiled for a 64 bit system and target is 32 bit"),
1994 bfd_archive_filename (ibfd));
1996 else if ((ibfd->flags & DYNAMIC) == 0)
1998 if (bfd_get_mach (obfd) < bfd_get_mach (ibfd))
1999 bfd_set_arch_mach (obfd, bfd_arch_sparc, bfd_get_mach (ibfd));
2002 if (((elf_elfheader (ibfd)->e_flags & EF_SPARC_LEDATA)
2003 != previous_ibfd_e_flags)
2004 && previous_ibfd_e_flags != (unsigned long) -1)
2006 (*_bfd_error_handler)
2007 (_("%s: linking little endian files with big endian files"),
2008 bfd_archive_filename (ibfd));
2009 error = true;
2011 previous_ibfd_e_flags = elf_elfheader (ibfd)->e_flags & EF_SPARC_LEDATA;
2013 if (error)
2015 bfd_set_error (bfd_error_bad_value);
2016 return false;
2019 return true;
2022 /* Set the right machine number. */
2024 static boolean
2025 elf32_sparc_object_p (abfd)
2026 bfd *abfd;
2028 if (elf_elfheader (abfd)->e_machine == EM_SPARC32PLUS)
2030 if (elf_elfheader (abfd)->e_flags & EF_SPARC_SUN_US3)
2031 return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
2032 bfd_mach_sparc_v8plusb);
2033 else if (elf_elfheader (abfd)->e_flags & EF_SPARC_SUN_US1)
2034 return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
2035 bfd_mach_sparc_v8plusa);
2036 else if (elf_elfheader (abfd)->e_flags & EF_SPARC_32PLUS)
2037 return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
2038 bfd_mach_sparc_v8plus);
2039 else
2040 return false;
2042 else if (elf_elfheader (abfd)->e_flags & EF_SPARC_LEDATA)
2043 return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
2044 bfd_mach_sparc_sparclite_le);
2045 else
2046 return bfd_default_set_arch_mach (abfd, bfd_arch_sparc, bfd_mach_sparc);
2049 /* The final processing done just before writing out the object file.
2050 We need to set the e_machine field appropriately. */
2052 static void
2053 elf32_sparc_final_write_processing (abfd, linker)
2054 bfd *abfd;
2055 boolean linker ATTRIBUTE_UNUSED;
2057 switch (bfd_get_mach (abfd))
2059 case bfd_mach_sparc :
2060 break; /* nothing to do */
2061 case bfd_mach_sparc_v8plus :
2062 elf_elfheader (abfd)->e_machine = EM_SPARC32PLUS;
2063 elf_elfheader (abfd)->e_flags &=~ EF_SPARC_32PLUS_MASK;
2064 elf_elfheader (abfd)->e_flags |= EF_SPARC_32PLUS;
2065 break;
2066 case bfd_mach_sparc_v8plusa :
2067 elf_elfheader (abfd)->e_machine = EM_SPARC32PLUS;
2068 elf_elfheader (abfd)->e_flags &=~ EF_SPARC_32PLUS_MASK;
2069 elf_elfheader (abfd)->e_flags |= EF_SPARC_32PLUS | EF_SPARC_SUN_US1;
2070 break;
2071 case bfd_mach_sparc_v8plusb :
2072 elf_elfheader (abfd)->e_machine = EM_SPARC32PLUS;
2073 elf_elfheader (abfd)->e_flags &=~ EF_SPARC_32PLUS_MASK;
2074 elf_elfheader (abfd)->e_flags |= EF_SPARC_32PLUS | EF_SPARC_SUN_US1
2075 | EF_SPARC_SUN_US3;
2076 break;
2077 case bfd_mach_sparc_sparclite_le :
2078 elf_elfheader (abfd)->e_machine = EM_SPARC;
2079 elf_elfheader (abfd)->e_flags |= EF_SPARC_LEDATA;
2080 break;
2081 default :
2082 abort ();
2083 break;
2087 static enum elf_reloc_type_class
2088 elf32_sparc_reloc_type_class (rela)
2089 const Elf_Internal_Rela *rela;
2091 switch ((int) ELF32_R_TYPE (rela->r_info))
2093 case R_SPARC_RELATIVE:
2094 return reloc_class_relative;
2095 case R_SPARC_JMP_SLOT:
2096 return reloc_class_plt;
2097 case R_SPARC_COPY:
2098 return reloc_class_copy;
2099 default:
2100 return reloc_class_normal;
2104 #define TARGET_BIG_SYM bfd_elf32_sparc_vec
2105 #define TARGET_BIG_NAME "elf32-sparc"
2106 #define ELF_ARCH bfd_arch_sparc
2107 #define ELF_MACHINE_CODE EM_SPARC
2108 #define ELF_MACHINE_ALT1 EM_SPARC32PLUS
2109 #define ELF_MAXPAGESIZE 0x10000
2111 #define bfd_elf32_bfd_reloc_type_lookup elf32_sparc_reloc_type_lookup
2112 #define bfd_elf32_bfd_relax_section elf32_sparc_relax_section
2113 #define elf_info_to_howto elf32_sparc_info_to_howto
2114 #define elf_backend_create_dynamic_sections \
2115 _bfd_elf_create_dynamic_sections
2116 #define elf_backend_check_relocs elf32_sparc_check_relocs
2117 #define elf_backend_adjust_dynamic_symbol \
2118 elf32_sparc_adjust_dynamic_symbol
2119 #define elf_backend_size_dynamic_sections \
2120 elf32_sparc_size_dynamic_sections
2121 #define elf_backend_relocate_section elf32_sparc_relocate_section
2122 #define elf_backend_finish_dynamic_symbol \
2123 elf32_sparc_finish_dynamic_symbol
2124 #define elf_backend_finish_dynamic_sections \
2125 elf32_sparc_finish_dynamic_sections
2126 #define bfd_elf32_bfd_merge_private_bfd_data \
2127 elf32_sparc_merge_private_bfd_data
2128 #define elf_backend_object_p elf32_sparc_object_p
2129 #define elf_backend_final_write_processing \
2130 elf32_sparc_final_write_processing
2131 #define elf_backend_gc_mark_hook elf32_sparc_gc_mark_hook
2132 #define elf_backend_gc_sweep_hook elf32_sparc_gc_sweep_hook
2133 #define elf_backend_reloc_type_class elf32_sparc_reloc_type_class
2135 #define elf_backend_can_gc_sections 1
2136 #define elf_backend_want_got_plt 0
2137 #define elf_backend_plt_readonly 0
2138 #define elf_backend_want_plt_sym 1
2139 #define elf_backend_got_header_size 4
2140 #define elf_backend_plt_header_size (4*PLT_ENTRY_SIZE)
2141 #define elf_backend_rela_normal 1
2143 #include "elf32-target.h"