]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - bfd/elf32-sparc.c
* elf-bfd.h (enum elf_reloc_type_class): New.
[thirdparty/binutils-gdb.git] / bfd / elf32-sparc.c
1 /* SPARC-specific support for 32-bit ELF
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
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.
11
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.
16
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. */
20
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"
28
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 ((int));
57 \f
58 /* The relocation "howto" table. */
59
60 static bfd_reloc_status_type sparc_elf_notsupported_reloc
61 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
62 static bfd_reloc_status_type sparc_elf_wdisp16_reloc
63 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
64
65 reloc_howto_type _bfd_sparc_elf_howto_table[] =
66 {
67 HOWTO(R_SPARC_NONE, 0,0, 0,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_NONE", false,0,0x00000000,true),
68 HOWTO(R_SPARC_8, 0,0, 8,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_8", false,0,0x000000ff,true),
69 HOWTO(R_SPARC_16, 0,1,16,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_16", false,0,0x0000ffff,true),
70 HOWTO(R_SPARC_32, 0,2,32,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_32", false,0,0xffffffff,true),
71 HOWTO(R_SPARC_DISP8, 0,0, 8,true, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_DISP8", false,0,0x000000ff,true),
72 HOWTO(R_SPARC_DISP16, 0,1,16,true, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_DISP16", false,0,0x0000ffff,true),
73 HOWTO(R_SPARC_DISP32, 0,2,32,true, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_DISP32", false,0,0x00ffffff,true),
74 HOWTO(R_SPARC_WDISP30, 2,2,30,true, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_WDISP30", false,0,0x3fffffff,true),
75 HOWTO(R_SPARC_WDISP22, 2,2,22,true, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_WDISP22", false,0,0x003fffff,true),
76 HOWTO(R_SPARC_HI22, 10,2,22,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_HI22", false,0,0x003fffff,true),
77 HOWTO(R_SPARC_22, 0,2,22,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_22", false,0,0x003fffff,true),
78 HOWTO(R_SPARC_13, 0,2,13,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_13", false,0,0x00001fff,true),
79 HOWTO(R_SPARC_LO10, 0,2,10,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_LO10", false,0,0x000003ff,true),
80 HOWTO(R_SPARC_GOT10, 0,2,10,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_GOT10", false,0,0x000003ff,true),
81 HOWTO(R_SPARC_GOT13, 0,2,13,false,0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_GOT13", false,0,0x00001fff,true),
82 HOWTO(R_SPARC_GOT22, 10,2,22,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_GOT22", false,0,0x003fffff,true),
83 HOWTO(R_SPARC_PC10, 0,2,10,true, 0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_PC10", false,0,0x000003ff,true),
84 HOWTO(R_SPARC_PC22, 10,2,22,true, 0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_PC22", false,0,0x003fffff,true),
85 HOWTO(R_SPARC_WPLT30, 2,2,30,true, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_WPLT30", false,0,0x3fffffff,true),
86 HOWTO(R_SPARC_COPY, 0,0,00,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_COPY", false,0,0x00000000,true),
87 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),
88 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),
89 HOWTO(R_SPARC_RELATIVE, 0,0,00,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_RELATIVE",false,0,0x00000000,true),
90 HOWTO(R_SPARC_UA32, 0,2,32,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_UA32", false,0,0xffffffff,true),
91 HOWTO(R_SPARC_PLT32, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_PLT32", false,0,0x00000000,true),
92 HOWTO(R_SPARC_HIPLT22, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_HIPLT22", false,0,0x00000000,true),
93 HOWTO(R_SPARC_LOPLT10, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_LOPLT10", false,0,0x00000000,true),
94 HOWTO(R_SPARC_PCPLT32, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_PCPLT32", false,0,0x00000000,true),
95 HOWTO(R_SPARC_PCPLT22, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_PCPLT22", false,0,0x00000000,true),
96 HOWTO(R_SPARC_PCPLT10, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_PCPLT10", false,0,0x00000000,true),
97 HOWTO(R_SPARC_10, 0,2,10,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_10", false,0,0x000003ff,true),
98 HOWTO(R_SPARC_11, 0,2,11,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_11", false,0,0x000007ff,true),
99 /* These are for sparc64 in a 64 bit environment.
100 Values need to be here because the table is indexed by reloc number. */
101 HOWTO(R_SPARC_64, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_64", false,0,0x00000000,true),
102 HOWTO(R_SPARC_OLO10, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_OLO10", false,0,0x00000000,true),
103 HOWTO(R_SPARC_HH22, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_HH22", false,0,0x00000000,true),
104 HOWTO(R_SPARC_HM10, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_HM10", false,0,0x00000000,true),
105 HOWTO(R_SPARC_LM22, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_LM22", false,0,0x00000000,true),
106 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),
107 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),
108 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),
109 /* End sparc64 in 64 bit environment values.
110 The following are for sparc64 in a 32 bit environment. */
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_NONE, 0,0, 0,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_NONE", false,0,0x00000000,true),
118 HOWTO(R_SPARC_NONE, 0,0, 0,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_NONE", false,0,0x00000000,true),
119 HOWTO(R_SPARC_NONE, 0,0, 0,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_NONE", false,0,0x00000000,true),
120 HOWTO(R_SPARC_NONE, 0,0, 0,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_NONE", false,0,0x00000000,true),
121 HOWTO(R_SPARC_NONE, 0,0, 0,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_NONE", false,0,0x00000000,true),
122 HOWTO(R_SPARC_NONE, 0,0, 0,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_NONE", false,0,0x00000000,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_UA64, 0,0, 0,false,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_UA64", false,0,0x00000000,true),
126 HOWTO(R_SPARC_UA16, 0,1,16,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_UA16", false,0,0x0000ffff,true),
127 HOWTO(R_SPARC_REV32, 0,2,32,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_REV32", false,0,0xffffffff,true),
128 };
129 static reloc_howto_type elf32_sparc_vtinherit_howto =
130 HOWTO (R_SPARC_GNU_VTINHERIT, 0,2,0,false,0,complain_overflow_dont, NULL, "R_SPARC_GNU_VTINHERIT", false,0, 0, false);
131 static reloc_howto_type elf32_sparc_vtentry_howto =
132 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);
133
134 struct elf_reloc_map {
135 bfd_reloc_code_real_type bfd_reloc_val;
136 unsigned char elf_reloc_val;
137 };
138
139 static CONST struct elf_reloc_map sparc_reloc_map[] =
140 {
141 { BFD_RELOC_NONE, R_SPARC_NONE, },
142 { BFD_RELOC_16, R_SPARC_16, },
143 { BFD_RELOC_8, R_SPARC_8 },
144 { BFD_RELOC_8_PCREL, R_SPARC_DISP8 },
145 { BFD_RELOC_CTOR, R_SPARC_32 },
146 { BFD_RELOC_32, R_SPARC_32 },
147 { BFD_RELOC_32_PCREL, R_SPARC_DISP32 },
148 { BFD_RELOC_HI22, R_SPARC_HI22 },
149 { BFD_RELOC_LO10, R_SPARC_LO10, },
150 { BFD_RELOC_32_PCREL_S2, R_SPARC_WDISP30 },
151 { BFD_RELOC_SPARC22, R_SPARC_22 },
152 { BFD_RELOC_SPARC13, R_SPARC_13 },
153 { BFD_RELOC_SPARC_GOT10, R_SPARC_GOT10 },
154 { BFD_RELOC_SPARC_GOT13, R_SPARC_GOT13 },
155 { BFD_RELOC_SPARC_GOT22, R_SPARC_GOT22 },
156 { BFD_RELOC_SPARC_PC10, R_SPARC_PC10 },
157 { BFD_RELOC_SPARC_PC22, R_SPARC_PC22 },
158 { BFD_RELOC_SPARC_WPLT30, R_SPARC_WPLT30 },
159 { BFD_RELOC_SPARC_COPY, R_SPARC_COPY },
160 { BFD_RELOC_SPARC_GLOB_DAT, R_SPARC_GLOB_DAT },
161 { BFD_RELOC_SPARC_JMP_SLOT, R_SPARC_JMP_SLOT },
162 { BFD_RELOC_SPARC_RELATIVE, R_SPARC_RELATIVE },
163 { BFD_RELOC_SPARC_WDISP22, R_SPARC_WDISP22 },
164 { BFD_RELOC_SPARC_UA16, R_SPARC_UA16 },
165 { BFD_RELOC_SPARC_UA32, R_SPARC_UA32 },
166 { BFD_RELOC_SPARC_UA64, R_SPARC_UA64 },
167 { BFD_RELOC_SPARC_10, R_SPARC_10 },
168 { BFD_RELOC_SPARC_11, R_SPARC_11 },
169 { BFD_RELOC_SPARC_64, R_SPARC_64 },
170 { BFD_RELOC_SPARC_OLO10, R_SPARC_OLO10 },
171 { BFD_RELOC_SPARC_HH22, R_SPARC_HH22 },
172 { BFD_RELOC_SPARC_HM10, R_SPARC_HM10 },
173 { BFD_RELOC_SPARC_LM22, R_SPARC_LM22 },
174 { BFD_RELOC_SPARC_PC_HH22, R_SPARC_PC_HH22 },
175 { BFD_RELOC_SPARC_PC_HM10, R_SPARC_PC_HM10 },
176 { BFD_RELOC_SPARC_PC_LM22, R_SPARC_PC_LM22 },
177 { BFD_RELOC_SPARC_WDISP16, R_SPARC_WDISP16 },
178 { BFD_RELOC_SPARC_WDISP19, R_SPARC_WDISP19 },
179 { BFD_RELOC_SPARC_7, R_SPARC_7 },
180 { BFD_RELOC_SPARC_5, R_SPARC_5 },
181 { BFD_RELOC_SPARC_6, R_SPARC_6 },
182 { BFD_RELOC_SPARC_REV32, R_SPARC_REV32 },
183 { BFD_RELOC_VTABLE_INHERIT, R_SPARC_GNU_VTINHERIT },
184 { BFD_RELOC_VTABLE_ENTRY, R_SPARC_GNU_VTENTRY },
185 };
186
187 static reloc_howto_type *
188 elf32_sparc_reloc_type_lookup (abfd, code)
189 bfd *abfd ATTRIBUTE_UNUSED;
190 bfd_reloc_code_real_type code;
191 {
192 unsigned int i;
193
194 switch (code)
195 {
196 case BFD_RELOC_VTABLE_INHERIT:
197 return &elf32_sparc_vtinherit_howto;
198
199 case BFD_RELOC_VTABLE_ENTRY:
200 return &elf32_sparc_vtentry_howto;
201
202 default:
203 for (i = 0; i < sizeof (sparc_reloc_map) / sizeof (struct elf_reloc_map); i++)
204 {
205 if (sparc_reloc_map[i].bfd_reloc_val == code)
206 return &_bfd_sparc_elf_howto_table[(int) sparc_reloc_map[i].elf_reloc_val];
207 }
208 }
209 bfd_set_error (bfd_error_bad_value);
210 return NULL;
211 }
212
213 /* We need to use ELF32_R_TYPE so we have our own copy of this function,
214 and elf64-sparc.c has its own copy. */
215
216 static void
217 elf32_sparc_info_to_howto (abfd, cache_ptr, dst)
218 bfd *abfd ATTRIBUTE_UNUSED;
219 arelent *cache_ptr;
220 Elf_Internal_Rela *dst;
221 {
222 switch (ELF32_R_TYPE(dst->r_info))
223 {
224 case R_SPARC_GNU_VTINHERIT:
225 cache_ptr->howto = &elf32_sparc_vtinherit_howto;
226 break;
227
228 case R_SPARC_GNU_VTENTRY:
229 cache_ptr->howto = &elf32_sparc_vtentry_howto;
230 break;
231
232 default:
233 BFD_ASSERT (ELF32_R_TYPE(dst->r_info) < (unsigned int) R_SPARC_max_std);
234 cache_ptr->howto = &_bfd_sparc_elf_howto_table[ELF32_R_TYPE(dst->r_info)];
235 }
236 }
237 \f
238 /* For unsupported relocs. */
239
240 static bfd_reloc_status_type
241 sparc_elf_notsupported_reloc (abfd,
242 reloc_entry,
243 symbol,
244 data,
245 input_section,
246 output_bfd,
247 error_message)
248 bfd *abfd ATTRIBUTE_UNUSED;
249 arelent *reloc_entry ATTRIBUTE_UNUSED;
250 asymbol *symbol ATTRIBUTE_UNUSED;
251 PTR data ATTRIBUTE_UNUSED;
252 asection *input_section ATTRIBUTE_UNUSED;
253 bfd *output_bfd ATTRIBUTE_UNUSED;
254 char **error_message ATTRIBUTE_UNUSED;
255 {
256 return bfd_reloc_notsupported;
257 }
258
259 /* Handle the WDISP16 reloc. */
260
261 static bfd_reloc_status_type
262 sparc_elf_wdisp16_reloc (abfd,
263 reloc_entry,
264 symbol,
265 data,
266 input_section,
267 output_bfd,
268 error_message)
269 bfd *abfd;
270 arelent *reloc_entry;
271 asymbol *symbol;
272 PTR data;
273 asection *input_section;
274 bfd *output_bfd;
275 char **error_message ATTRIBUTE_UNUSED;
276 {
277 bfd_vma relocation;
278 bfd_vma x;
279
280 if (output_bfd != (bfd *) NULL
281 && (symbol->flags & BSF_SECTION_SYM) == 0
282 && (! reloc_entry->howto->partial_inplace
283 || reloc_entry->addend == 0))
284 {
285 reloc_entry->address += input_section->output_offset;
286 return bfd_reloc_ok;
287 }
288
289 if (output_bfd != NULL)
290 return bfd_reloc_continue;
291
292 if (reloc_entry->address > input_section->_cooked_size)
293 return bfd_reloc_outofrange;
294
295 relocation = (symbol->value
296 + symbol->section->output_section->vma
297 + symbol->section->output_offset);
298 relocation += reloc_entry->addend;
299 relocation -= (input_section->output_section->vma
300 + input_section->output_offset);
301 relocation -= reloc_entry->address;
302
303 x = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
304 x |= ((((relocation >> 2) & 0xc000) << 6)
305 | ((relocation >> 2) & 0x3fff));
306 bfd_put_32 (abfd, x, (bfd_byte *) data + reloc_entry->address);
307
308 if ((bfd_signed_vma) relocation < - 0x40000
309 || (bfd_signed_vma) relocation > 0x3ffff)
310 return bfd_reloc_overflow;
311 else
312 return bfd_reloc_ok;
313 }
314 \f
315 /* Functions for the SPARC ELF linker. */
316
317 /* The name of the dynamic interpreter. This is put in the .interp
318 section. */
319
320 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
321
322 /* The nop opcode we use. */
323
324 #define SPARC_NOP 0x01000000
325
326 /* The size in bytes of an entry in the procedure linkage table. */
327
328 #define PLT_ENTRY_SIZE 12
329
330 /* The first four entries in a procedure linkage table are reserved,
331 and the initial contents are unimportant (we zero them out).
332 Subsequent entries look like this. See the SVR4 ABI SPARC
333 supplement to see how this works. */
334
335 /* sethi %hi(.-.plt0),%g1. We fill in the address later. */
336 #define PLT_ENTRY_WORD0 0x03000000
337 /* b,a .plt0. We fill in the offset later. */
338 #define PLT_ENTRY_WORD1 0x30800000
339 /* nop. */
340 #define PLT_ENTRY_WORD2 SPARC_NOP
341
342 /* Look through the relocs for a section during the first phase, and
343 allocate space in the global offset table or procedure linkage
344 table. */
345
346 static boolean
347 elf32_sparc_check_relocs (abfd, info, sec, relocs)
348 bfd *abfd;
349 struct bfd_link_info *info;
350 asection *sec;
351 const Elf_Internal_Rela *relocs;
352 {
353 bfd *dynobj;
354 Elf_Internal_Shdr *symtab_hdr;
355 struct elf_link_hash_entry **sym_hashes;
356 bfd_vma *local_got_offsets;
357 const Elf_Internal_Rela *rel;
358 const Elf_Internal_Rela *rel_end;
359 asection *sgot;
360 asection *srelgot;
361 asection *sreloc;
362
363 if (info->relocateable)
364 return true;
365
366 dynobj = elf_hash_table (info)->dynobj;
367 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
368 sym_hashes = elf_sym_hashes (abfd);
369 local_got_offsets = elf_local_got_offsets (abfd);
370
371 sgot = NULL;
372 srelgot = NULL;
373 sreloc = NULL;
374
375 rel_end = relocs + sec->reloc_count;
376 for (rel = relocs; rel < rel_end; rel++)
377 {
378 unsigned long r_symndx;
379 struct elf_link_hash_entry *h;
380
381 r_symndx = ELF32_R_SYM (rel->r_info);
382 if (r_symndx < symtab_hdr->sh_info)
383 h = NULL;
384 else
385 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
386
387 switch (ELF32_R_TYPE (rel->r_info))
388 {
389 case R_SPARC_GOT10:
390 case R_SPARC_GOT13:
391 case R_SPARC_GOT22:
392 /* This symbol requires a global offset table entry. */
393
394 if (dynobj == NULL)
395 {
396 /* Create the .got section. */
397 elf_hash_table (info)->dynobj = dynobj = abfd;
398 if (! _bfd_elf_create_got_section (dynobj, info))
399 return false;
400 }
401
402 if (sgot == NULL)
403 {
404 sgot = bfd_get_section_by_name (dynobj, ".got");
405 BFD_ASSERT (sgot != NULL);
406 }
407
408 if (srelgot == NULL
409 && (h != NULL || info->shared))
410 {
411 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
412 if (srelgot == NULL)
413 {
414 srelgot = bfd_make_section (dynobj, ".rela.got");
415 if (srelgot == NULL
416 || ! bfd_set_section_flags (dynobj, srelgot,
417 (SEC_ALLOC
418 | SEC_LOAD
419 | SEC_HAS_CONTENTS
420 | SEC_IN_MEMORY
421 | SEC_LINKER_CREATED
422 | SEC_READONLY))
423 || ! bfd_set_section_alignment (dynobj, srelgot, 2))
424 return false;
425 }
426 }
427
428 if (h != NULL)
429 {
430 if (h->got.offset != (bfd_vma) -1)
431 {
432 /* We have already allocated space in the .got. */
433 break;
434 }
435 h->got.offset = sgot->_raw_size;
436
437 /* Make sure this symbol is output as a dynamic symbol. */
438 if (h->dynindx == -1)
439 {
440 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
441 return false;
442 }
443
444 srelgot->_raw_size += sizeof (Elf32_External_Rela);
445 }
446 else
447 {
448 /* This is a global offset table entry for a local
449 symbol. */
450 if (local_got_offsets == NULL)
451 {
452 size_t size;
453 register unsigned int i;
454
455 size = symtab_hdr->sh_info * sizeof (bfd_vma);
456 local_got_offsets = (bfd_vma *) bfd_alloc (abfd, size);
457 if (local_got_offsets == NULL)
458 return false;
459 elf_local_got_offsets (abfd) = local_got_offsets;
460 for (i = 0; i < symtab_hdr->sh_info; i++)
461 local_got_offsets[i] = (bfd_vma) -1;
462 }
463 if (local_got_offsets[r_symndx] != (bfd_vma) -1)
464 {
465 /* We have already allocated space in the .got. */
466 break;
467 }
468 local_got_offsets[r_symndx] = sgot->_raw_size;
469
470 if (info->shared)
471 {
472 /* If we are generating a shared object, we need to
473 output a R_SPARC_RELATIVE reloc so that the
474 dynamic linker can adjust this GOT entry. */
475 srelgot->_raw_size += sizeof (Elf32_External_Rela);
476 }
477 }
478
479 sgot->_raw_size += 4;
480
481 /* If the .got section is more than 0x1000 bytes, we add
482 0x1000 to the value of _GLOBAL_OFFSET_TABLE_, so that 13
483 bit relocations have a greater chance of working. */
484 if (sgot->_raw_size >= 0x1000
485 && elf_hash_table (info)->hgot->root.u.def.value == 0)
486 elf_hash_table (info)->hgot->root.u.def.value = 0x1000;
487
488 break;
489
490 case R_SPARC_WPLT30:
491 /* This symbol requires a procedure linkage table entry. We
492 actually build the entry in adjust_dynamic_symbol,
493 because this might be a case of linking PIC code without
494 linking in any dynamic objects, in which case we don't
495 need to generate a procedure linkage table after all. */
496
497 if (h == NULL)
498 {
499 /* The Solaris native assembler will generate a WPLT30
500 reloc for a local symbol if you assemble a call from
501 one section to another when using -K pic. We treat
502 it as WDISP30. */
503 break;
504 }
505
506 /* Make sure this symbol is output as a dynamic symbol. */
507 if (h->dynindx == -1)
508 {
509 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
510 return false;
511 }
512
513 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
514
515 break;
516
517 case R_SPARC_PC10:
518 case R_SPARC_PC22:
519 if (h != NULL)
520 h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
521
522 if (h != NULL
523 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
524 break;
525 /* Fall through. */
526 case R_SPARC_DISP8:
527 case R_SPARC_DISP16:
528 case R_SPARC_DISP32:
529 case R_SPARC_WDISP30:
530 case R_SPARC_WDISP22:
531 case R_SPARC_WDISP19:
532 case R_SPARC_WDISP16:
533 if (h != NULL)
534 h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
535
536 /* If we are linking with -Bsymbolic, we do not need to copy
537 a PC relative reloc against a global symbol which is
538 defined in an object we are including in the link (i.e.,
539 DEF_REGULAR is set). FIXME: At this point we have not
540 seen all the input files, so it is possible that
541 DEF_REGULAR is not set now but will be set later (it is
542 never cleared). This needs to be handled as in
543 elf32-i386.c. */
544 if (h == NULL
545 || (info->symbolic
546 && (h->elf_link_hash_flags
547 & ELF_LINK_HASH_DEF_REGULAR) != 0))
548 break;
549 /* Fall through. */
550 case R_SPARC_8:
551 case R_SPARC_16:
552 case R_SPARC_32:
553 case R_SPARC_HI22:
554 case R_SPARC_22:
555 case R_SPARC_13:
556 case R_SPARC_LO10:
557 case R_SPARC_UA16:
558 case R_SPARC_UA32:
559 if (h != NULL)
560 h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
561
562 if (info->shared && (sec->flags & SEC_ALLOC))
563 {
564 /* When creating a shared object, we must copy these
565 relocs into the output file. We create a reloc
566 section in dynobj and make room for the reloc. */
567 if (sreloc == NULL)
568 {
569 const char *name;
570
571 name = (bfd_elf_string_from_elf_section
572 (abfd,
573 elf_elfheader (abfd)->e_shstrndx,
574 elf_section_data (sec)->rel_hdr.sh_name));
575 if (name == NULL)
576 return false;
577
578 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
579 && strcmp (bfd_get_section_name (abfd, sec),
580 name + 5) == 0);
581
582 sreloc = bfd_get_section_by_name (dynobj, name);
583 if (sreloc == NULL)
584 {
585 flagword flags;
586
587 sreloc = bfd_make_section (dynobj, name);
588 flags = (SEC_HAS_CONTENTS | SEC_READONLY
589 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
590 if ((sec->flags & SEC_ALLOC) != 0)
591 flags |= SEC_ALLOC | SEC_LOAD;
592 if (sreloc == NULL
593 || ! bfd_set_section_flags (dynobj, sreloc, flags)
594 || ! bfd_set_section_alignment (dynobj, sreloc, 2))
595 return false;
596 }
597 if (sec->flags & SEC_READONLY)
598 info->flags |= DF_TEXTREL;
599 }
600
601 sreloc->_raw_size += sizeof (Elf32_External_Rela);
602 }
603
604 break;
605
606 case R_SPARC_GNU_VTINHERIT:
607 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
608 return false;
609 break;
610
611 case R_SPARC_GNU_VTENTRY:
612 if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend))
613 return false;
614 break;
615
616 default:
617 break;
618 }
619 }
620
621 return true;
622 }
623
624 static asection *
625 elf32_sparc_gc_mark_hook (abfd, info, rel, h, sym)
626 bfd *abfd;
627 struct bfd_link_info *info ATTRIBUTE_UNUSED;
628 Elf_Internal_Rela *rel;
629 struct elf_link_hash_entry *h;
630 Elf_Internal_Sym *sym;
631 {
632
633 if (h != NULL)
634 {
635 switch (ELF32_R_TYPE (rel->r_info))
636 {
637 case R_SPARC_GNU_VTINHERIT:
638 case R_SPARC_GNU_VTENTRY:
639 break;
640
641 default:
642 switch (h->root.type)
643 {
644 case bfd_link_hash_defined:
645 case bfd_link_hash_defweak:
646 return h->root.u.def.section;
647
648 case bfd_link_hash_common:
649 return h->root.u.c.p->section;
650
651 default:
652 break;
653 }
654 }
655 }
656 else
657 {
658 if (!(elf_bad_symtab (abfd)
659 && ELF_ST_BIND (sym->st_info) != STB_LOCAL)
660 && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE)
661 && sym->st_shndx != SHN_COMMON))
662 {
663 return bfd_section_from_elf_index (abfd, sym->st_shndx);
664 }
665 }
666
667 return NULL;
668 }
669
670 /* Update the got entry reference counts for the section being removed. */
671 static boolean
672 elf32_sparc_gc_sweep_hook (abfd, info, sec, relocs)
673 bfd *abfd;
674 struct bfd_link_info *info ATTRIBUTE_UNUSED;
675 asection *sec;
676 const Elf_Internal_Rela *relocs;
677 {
678
679 Elf_Internal_Shdr *symtab_hdr;
680 struct elf_link_hash_entry **sym_hashes;
681 bfd_signed_vma *local_got_refcounts;
682 const Elf_Internal_Rela *rel, *relend;
683 unsigned long r_symndx;
684 struct elf_link_hash_entry *h;
685
686 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
687 sym_hashes = elf_sym_hashes (abfd);
688 local_got_refcounts = elf_local_got_refcounts (abfd);
689
690 relend = relocs + sec->reloc_count;
691 for (rel = relocs; rel < relend; rel++)
692 switch (ELF32_R_TYPE (rel->r_info))
693 {
694 case R_SPARC_GOT10:
695 case R_SPARC_GOT13:
696 case R_SPARC_GOT22:
697 r_symndx = ELF32_R_SYM (rel->r_info);
698 if (r_symndx >= symtab_hdr->sh_info)
699 {
700 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
701 if (h->got.refcount > 0)
702 h->got.refcount--;
703 }
704 else
705 {
706 if (local_got_refcounts[r_symndx] > 0)
707 local_got_refcounts[r_symndx]--;
708 }
709 break;
710
711 case R_SPARC_PLT32:
712 case R_SPARC_HIPLT22:
713 case R_SPARC_LOPLT10:
714 case R_SPARC_PCPLT32:
715 case R_SPARC_PCPLT10:
716 r_symndx = ELF32_R_SYM (rel->r_info);
717 if (r_symndx >= symtab_hdr->sh_info)
718 {
719 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
720 if (h->plt.refcount > 0)
721 h->plt.refcount--;
722 }
723 break;
724
725 default:
726 break;
727 }
728
729 return true;
730 }
731
732 /* Adjust a symbol defined by a dynamic object and referenced by a
733 regular object. The current definition is in some section of the
734 dynamic object, but we're not including those sections. We have to
735 change the definition to something the rest of the link can
736 understand. */
737
738 static boolean
739 elf32_sparc_adjust_dynamic_symbol (info, h)
740 struct bfd_link_info *info;
741 struct elf_link_hash_entry *h;
742 {
743 bfd *dynobj;
744 asection *s;
745 unsigned int power_of_two;
746
747 dynobj = elf_hash_table (info)->dynobj;
748
749 /* Make sure we know what is going on here. */
750 BFD_ASSERT (dynobj != NULL
751 && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
752 || h->weakdef != NULL
753 || ((h->elf_link_hash_flags
754 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
755 && (h->elf_link_hash_flags
756 & ELF_LINK_HASH_REF_REGULAR) != 0
757 && (h->elf_link_hash_flags
758 & ELF_LINK_HASH_DEF_REGULAR) == 0)));
759
760 /* If this is a function, put it in the procedure linkage table. We
761 will fill in the contents of the procedure linkage table later
762 (although we could actually do it here). The STT_NOTYPE
763 condition is a hack specifically for the Oracle libraries
764 delivered for Solaris; for some inexplicable reason, they define
765 some of their functions as STT_NOTYPE when they really should be
766 STT_FUNC. */
767 if (h->type == STT_FUNC
768 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0
769 || (h->type == STT_NOTYPE
770 && (h->root.type == bfd_link_hash_defined
771 || h->root.type == bfd_link_hash_defweak)
772 && (h->root.u.def.section->flags & SEC_CODE) != 0))
773 {
774 if (! elf_hash_table (info)->dynamic_sections_created
775 || ((!info->shared || info->symbolic || h->dynindx == -1)
776 && (h->elf_link_hash_flags
777 & ELF_LINK_HASH_DEF_REGULAR) != 0))
778 {
779 /* This case can occur if we saw a WPLT30 reloc in an input
780 file, but none of the input files were dynamic objects.
781 Or, when linking the main application or a -Bsymbolic
782 shared library against PIC code. Or when a global symbol
783 has been made private, e.g. via versioning.
784
785 In these cases we know what value the symbol will resolve
786 to, so we don't actually need to build a procedure linkage
787 table, and we can just do a WDISP30 reloc instead. */
788
789 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
790 return true;
791 }
792
793 s = bfd_get_section_by_name (dynobj, ".plt");
794 BFD_ASSERT (s != NULL);
795
796 /* The first four entries in .plt are reserved. */
797 if (s->_raw_size == 0)
798 s->_raw_size = 4 * PLT_ENTRY_SIZE;
799
800 /* The procedure linkage table has a maximum size. */
801 if (s->_raw_size >= 0x400000)
802 {
803 bfd_set_error (bfd_error_bad_value);
804 return false;
805 }
806
807 /* If this symbol is not defined in a regular file, and we are
808 not generating a shared library, then set the symbol to this
809 location in the .plt. This is required to make function
810 pointers compare as equal between the normal executable and
811 the shared library. */
812 if (! info->shared
813 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
814 {
815 h->root.u.def.section = s;
816 h->root.u.def.value = s->_raw_size;
817 }
818
819 h->plt.offset = s->_raw_size;
820
821 /* Make room for this entry. */
822 s->_raw_size += PLT_ENTRY_SIZE;
823
824 /* We also need to make an entry in the .rela.plt section. */
825
826 s = bfd_get_section_by_name (dynobj, ".rela.plt");
827 BFD_ASSERT (s != NULL);
828 s->_raw_size += sizeof (Elf32_External_Rela);
829
830 return true;
831 }
832
833 /* If this is a weak symbol, and there is a real definition, the
834 processor independent code will have arranged for us to see the
835 real definition first, and we can just use the same value. */
836 if (h->weakdef != NULL)
837 {
838 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
839 || h->weakdef->root.type == bfd_link_hash_defweak);
840 h->root.u.def.section = h->weakdef->root.u.def.section;
841 h->root.u.def.value = h->weakdef->root.u.def.value;
842 return true;
843 }
844
845 /* This is a reference to a symbol defined by a dynamic object which
846 is not a function. */
847
848 /* If we are creating a shared library, we must presume that the
849 only references to the symbol are via the global offset table.
850 For such cases we need not do anything here; the relocations will
851 be handled correctly by relocate_section. */
852 if (info->shared)
853 return true;
854
855 /* If there are no references to this symbol that do not use the
856 GOT, we don't need to generate a copy reloc. */
857 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
858 return true;
859
860 /* We must allocate the symbol in our .dynbss section, which will
861 become part of the .bss section of the executable. There will be
862 an entry for this symbol in the .dynsym section. The dynamic
863 object will contain position independent code, so all references
864 from the dynamic object to this symbol will go through the global
865 offset table. The dynamic linker will use the .dynsym entry to
866 determine the address it must put in the global offset table, so
867 both the dynamic object and the regular object will refer to the
868 same memory location for the variable. */
869
870 s = bfd_get_section_by_name (dynobj, ".dynbss");
871 BFD_ASSERT (s != NULL);
872
873 /* We must generate a R_SPARC_COPY reloc to tell the dynamic linker
874 to copy the initial value out of the dynamic object and into the
875 runtime process image. We need to remember the offset into the
876 .rel.bss section we are going to use. */
877 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
878 {
879 asection *srel;
880
881 srel = bfd_get_section_by_name (dynobj, ".rela.bss");
882 BFD_ASSERT (srel != NULL);
883 srel->_raw_size += sizeof (Elf32_External_Rela);
884 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
885 }
886
887 /* We need to figure out the alignment required for this symbol. I
888 have no idea how ELF linkers handle this. */
889 power_of_two = bfd_log2 (h->size);
890 if (power_of_two > 3)
891 power_of_two = 3;
892
893 /* Apply the required alignment. */
894 s->_raw_size = BFD_ALIGN (s->_raw_size,
895 (bfd_size_type) (1 << power_of_two));
896 if (power_of_two > bfd_get_section_alignment (dynobj, s))
897 {
898 if (! bfd_set_section_alignment (dynobj, s, power_of_two))
899 return false;
900 }
901
902 /* Define the symbol as being at this point in the section. */
903 h->root.u.def.section = s;
904 h->root.u.def.value = s->_raw_size;
905
906 /* Increment the section size to make room for the symbol. */
907 s->_raw_size += h->size;
908
909 return true;
910 }
911
912 /* Set the sizes of the dynamic sections. */
913
914 static boolean
915 elf32_sparc_size_dynamic_sections (output_bfd, info)
916 bfd *output_bfd ATTRIBUTE_UNUSED;
917 struct bfd_link_info *info;
918 {
919 bfd *dynobj;
920 asection *s;
921 boolean relplt;
922
923 dynobj = elf_hash_table (info)->dynobj;
924 BFD_ASSERT (dynobj != NULL);
925
926 if (elf_hash_table (info)->dynamic_sections_created)
927 {
928 /* Set the contents of the .interp section to the interpreter. */
929 if (! info->shared)
930 {
931 s = bfd_get_section_by_name (dynobj, ".interp");
932 BFD_ASSERT (s != NULL);
933 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
934 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
935 }
936
937 /* Make space for the trailing nop in .plt. */
938 s = bfd_get_section_by_name (dynobj, ".plt");
939 BFD_ASSERT (s != NULL);
940 if (s->_raw_size > 0)
941 s->_raw_size += 4;
942 }
943 else
944 {
945 /* We may have created entries in the .rela.got section.
946 However, if we are not creating the dynamic sections, we will
947 not actually use these entries. Reset the size of .rela.got,
948 which will cause it to get stripped from the output file
949 below. */
950 s = bfd_get_section_by_name (dynobj, ".rela.got");
951 if (s != NULL)
952 s->_raw_size = 0;
953 }
954
955 /* The check_relocs and adjust_dynamic_symbol entry points have
956 determined the sizes of the various dynamic sections. Allocate
957 memory for them. */
958 relplt = false;
959 for (s = dynobj->sections; s != NULL; s = s->next)
960 {
961 const char *name;
962 boolean strip;
963
964 if ((s->flags & SEC_LINKER_CREATED) == 0)
965 continue;
966
967 /* It's OK to base decisions on the section name, because none
968 of the dynobj section names depend upon the input files. */
969 name = bfd_get_section_name (dynobj, s);
970
971 strip = false;
972
973 if (strncmp (name, ".rela", 5) == 0)
974 {
975 if (s->_raw_size == 0)
976 {
977 /* If we don't need this section, strip it from the
978 output file. This is to handle .rela.bss and
979 .rel.plt. We must create it in
980 create_dynamic_sections, because it must be created
981 before the linker maps input sections to output
982 sections. The linker does that before
983 adjust_dynamic_symbol is called, and it is that
984 function which decides whether anything needs to go
985 into these sections. */
986 strip = true;
987 }
988 else
989 {
990 if (strcmp (name, ".rela.plt") == 0)
991 relplt = true;
992
993 /* We use the reloc_count field as a counter if we need
994 to copy relocs into the output file. */
995 s->reloc_count = 0;
996 }
997 }
998 else if (strcmp (name, ".plt") != 0
999 && strcmp (name, ".got") != 0)
1000 {
1001 /* It's not one of our sections, so don't allocate space. */
1002 continue;
1003 }
1004
1005 if (strip)
1006 {
1007 _bfd_strip_section_from_output (info, s);
1008 continue;
1009 }
1010
1011 /* Allocate memory for the section contents. */
1012 /* FIXME: This should be a call to bfd_alloc not bfd_zalloc.
1013 Unused entries should be reclaimed before the section's contents
1014 are written out, but at the moment this does not happen. Thus in
1015 order to prevent writing out garbage, we initialise the section's
1016 contents to zero. */
1017 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
1018 if (s->contents == NULL && s->_raw_size != 0)
1019 return false;
1020 }
1021
1022 if (elf_hash_table (info)->dynamic_sections_created)
1023 {
1024 /* Add some entries to the .dynamic section. We fill in the
1025 values later, in elf32_sparc_finish_dynamic_sections, but we
1026 must add the entries now so that we get the correct size for
1027 the .dynamic section. The DT_DEBUG entry is filled in by the
1028 dynamic linker and used by the debugger. */
1029 if (! info->shared)
1030 {
1031 if (! bfd_elf32_add_dynamic_entry (info, DT_DEBUG, 0))
1032 return false;
1033 }
1034
1035 if (relplt)
1036 {
1037 if (! bfd_elf32_add_dynamic_entry (info, DT_PLTGOT, 0)
1038 || ! bfd_elf32_add_dynamic_entry (info, DT_PLTRELSZ, 0)
1039 || ! bfd_elf32_add_dynamic_entry (info, DT_PLTREL, DT_RELA)
1040 || ! bfd_elf32_add_dynamic_entry (info, DT_JMPREL, 0))
1041 return false;
1042 }
1043
1044 if (! bfd_elf32_add_dynamic_entry (info, DT_RELA, 0)
1045 || ! bfd_elf32_add_dynamic_entry (info, DT_RELASZ, 0)
1046 || ! bfd_elf32_add_dynamic_entry (info, DT_RELAENT,
1047 sizeof (Elf32_External_Rela)))
1048 return false;
1049
1050 if (info->flags & DF_TEXTREL)
1051 {
1052 if (! bfd_elf32_add_dynamic_entry (info, DT_TEXTREL, 0))
1053 return false;
1054 }
1055 }
1056
1057 return true;
1058 }
1059
1060 #define SET_SEC_DO_RELAX(section) do { elf_section_data(section)->tdata = (void *)1; } while (0)
1061 #define SEC_DO_RELAX(section) (elf_section_data(section)->tdata == (void *)1)
1062
1063 static boolean
1064 elf32_sparc_relax_section (abfd, section, link_info, again)
1065 bfd *abfd ATTRIBUTE_UNUSED;
1066 asection *section ATTRIBUTE_UNUSED;
1067 struct bfd_link_info *link_info ATTRIBUTE_UNUSED;
1068 boolean *again;
1069 {
1070 *again = false;
1071 SET_SEC_DO_RELAX (section);
1072 return true;
1073 }
1074
1075 /* Relocate a SPARC ELF section. */
1076
1077 static boolean
1078 elf32_sparc_relocate_section (output_bfd, info, input_bfd, input_section,
1079 contents, relocs, local_syms, local_sections)
1080 bfd *output_bfd;
1081 struct bfd_link_info *info;
1082 bfd *input_bfd;
1083 asection *input_section;
1084 bfd_byte *contents;
1085 Elf_Internal_Rela *relocs;
1086 Elf_Internal_Sym *local_syms;
1087 asection **local_sections;
1088 {
1089 bfd *dynobj;
1090 Elf_Internal_Shdr *symtab_hdr;
1091 struct elf_link_hash_entry **sym_hashes;
1092 bfd_vma *local_got_offsets;
1093 bfd_vma got_base;
1094 asection *sgot;
1095 asection *splt;
1096 asection *sreloc;
1097 Elf_Internal_Rela *rel;
1098 Elf_Internal_Rela *relend;
1099
1100 dynobj = elf_hash_table (info)->dynobj;
1101 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1102 sym_hashes = elf_sym_hashes (input_bfd);
1103 local_got_offsets = elf_local_got_offsets (input_bfd);
1104
1105 if (elf_hash_table (info)->hgot == NULL)
1106 got_base = 0;
1107 else
1108 got_base = elf_hash_table (info)->hgot->root.u.def.value;
1109
1110 sgot = NULL;
1111 splt = NULL;
1112 sreloc = NULL;
1113
1114 rel = relocs;
1115 relend = relocs + input_section->reloc_count;
1116 for (; rel < relend; rel++)
1117 {
1118 int r_type;
1119 reloc_howto_type *howto;
1120 unsigned long r_symndx;
1121 struct elf_link_hash_entry *h;
1122 Elf_Internal_Sym *sym;
1123 asection *sec;
1124 bfd_vma relocation;
1125 bfd_reloc_status_type r;
1126
1127 r_type = ELF32_R_TYPE (rel->r_info);
1128
1129 if (r_type == R_SPARC_GNU_VTINHERIT
1130 || r_type == R_SPARC_GNU_VTENTRY)
1131 continue;
1132
1133 if (r_type < 0 || r_type >= (int) R_SPARC_max_std)
1134 {
1135 bfd_set_error (bfd_error_bad_value);
1136 return false;
1137 }
1138 howto = _bfd_sparc_elf_howto_table + r_type;
1139
1140 r_symndx = ELF32_R_SYM (rel->r_info);
1141
1142 if (info->relocateable)
1143 {
1144 /* This is a relocateable link. We don't have to change
1145 anything, unless the reloc is against a section symbol,
1146 in which case we have to adjust according to where the
1147 section symbol winds up in the output section. */
1148 if (r_symndx < symtab_hdr->sh_info)
1149 {
1150 sym = local_syms + r_symndx;
1151 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
1152 {
1153 sec = local_sections[r_symndx];
1154 rel->r_addend += sec->output_offset + sym->st_value;
1155 }
1156 }
1157
1158 continue;
1159 }
1160
1161 /* This is a final link. */
1162 h = NULL;
1163 sym = NULL;
1164 sec = NULL;
1165 if (r_symndx < symtab_hdr->sh_info)
1166 {
1167 sym = local_syms + r_symndx;
1168 sec = local_sections[r_symndx];
1169 relocation = (sec->output_section->vma
1170 + sec->output_offset
1171 + sym->st_value);
1172 }
1173 else
1174 {
1175 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1176 while (h->root.type == bfd_link_hash_indirect
1177 || h->root.type == bfd_link_hash_warning)
1178 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1179 if (h->root.type == bfd_link_hash_defined
1180 || h->root.type == bfd_link_hash_defweak)
1181 {
1182 sec = h->root.u.def.section;
1183 if ((r_type == R_SPARC_WPLT30
1184 && h->plt.offset != (bfd_vma) -1)
1185 || ((r_type == R_SPARC_GOT10
1186 || r_type == R_SPARC_GOT13
1187 || r_type == R_SPARC_GOT22)
1188 && elf_hash_table (info)->dynamic_sections_created
1189 && (! info->shared
1190 || (! info->symbolic && h->dynindx != -1)
1191 || (h->elf_link_hash_flags
1192 & ELF_LINK_HASH_DEF_REGULAR) == 0))
1193 || (info->shared
1194 && ((! info->symbolic && h->dynindx != -1)
1195 || (h->elf_link_hash_flags
1196 & ELF_LINK_HASH_DEF_REGULAR) == 0)
1197 && (r_type == R_SPARC_8
1198 || r_type == R_SPARC_16
1199 || r_type == R_SPARC_32
1200 || r_type == R_SPARC_DISP8
1201 || r_type == R_SPARC_DISP16
1202 || r_type == R_SPARC_DISP32
1203 || r_type == R_SPARC_WDISP30
1204 || r_type == R_SPARC_WDISP22
1205 || r_type == R_SPARC_WDISP19
1206 || r_type == R_SPARC_WDISP16
1207 || r_type == R_SPARC_HI22
1208 || r_type == R_SPARC_22
1209 || r_type == R_SPARC_13
1210 || r_type == R_SPARC_LO10
1211 || r_type == R_SPARC_UA16
1212 || r_type == R_SPARC_UA32
1213 || ((r_type == R_SPARC_PC10
1214 || r_type == R_SPARC_PC22)
1215 && strcmp (h->root.root.string,
1216 "_GLOBAL_OFFSET_TABLE_") != 0))))
1217 {
1218 /* In these cases, we don't need the relocation
1219 value. We check specially because in some
1220 obscure cases sec->output_section will be NULL. */
1221 relocation = 0;
1222 }
1223 else
1224 relocation = (h->root.u.def.value
1225 + sec->output_section->vma
1226 + sec->output_offset);
1227 }
1228 else if (h->root.type == bfd_link_hash_undefweak)
1229 relocation = 0;
1230 else if (info->shared && !info->symbolic
1231 && !info->no_undefined
1232 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
1233 relocation = 0;
1234 else
1235 {
1236 if (! ((*info->callbacks->undefined_symbol)
1237 (info, h->root.root.string, input_bfd,
1238 input_section, rel->r_offset,
1239 (!info->shared || info->no_undefined
1240 || ELF_ST_VISIBILITY (h->other)))))
1241 return false;
1242 relocation = 0;
1243 }
1244 }
1245
1246 switch (r_type)
1247 {
1248 case R_SPARC_GOT10:
1249 case R_SPARC_GOT13:
1250 case R_SPARC_GOT22:
1251 /* Relocation is to the entry for this symbol in the global
1252 offset table. */
1253 if (sgot == NULL)
1254 {
1255 sgot = bfd_get_section_by_name (dynobj, ".got");
1256 BFD_ASSERT (sgot != NULL);
1257 }
1258
1259 if (h != NULL)
1260 {
1261 bfd_vma off;
1262
1263 off = h->got.offset;
1264 BFD_ASSERT (off != (bfd_vma) -1);
1265
1266 if (! elf_hash_table (info)->dynamic_sections_created
1267 || (info->shared
1268 && (info->symbolic || h->dynindx == -1)
1269 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
1270 {
1271 /* This is actually a static link, or it is a
1272 -Bsymbolic link and the symbol is defined
1273 locally, or the symbol was forced to be local
1274 because of a version file. We must initialize
1275 this entry in the global offset table. Since the
1276 offset must always be a multiple of 4, we use the
1277 least significant bit to record whether we have
1278 initialized it already.
1279
1280 When doing a dynamic link, we create a .rela.got
1281 relocation entry to initialize the value. This
1282 is done in the finish_dynamic_symbol routine. */
1283 if ((off & 1) != 0)
1284 off &= ~1;
1285 else
1286 {
1287 bfd_put_32 (output_bfd, relocation,
1288 sgot->contents + off);
1289 h->got.offset |= 1;
1290 }
1291 }
1292
1293 relocation = sgot->output_offset + off - got_base;
1294 }
1295 else
1296 {
1297 bfd_vma off;
1298
1299 BFD_ASSERT (local_got_offsets != NULL
1300 && local_got_offsets[r_symndx] != (bfd_vma) -1);
1301
1302 off = local_got_offsets[r_symndx];
1303
1304 /* The offset must always be a multiple of 4. We use
1305 the least significant bit to record whether we have
1306 already processed this entry. */
1307 if ((off & 1) != 0)
1308 off &= ~1;
1309 else
1310 {
1311 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
1312
1313 if (info->shared)
1314 {
1315 asection *srelgot;
1316 Elf_Internal_Rela outrel;
1317
1318 /* We need to generate a R_SPARC_RELATIVE reloc
1319 for the dynamic linker. */
1320 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
1321 BFD_ASSERT (srelgot != NULL);
1322
1323 outrel.r_offset = (sgot->output_section->vma
1324 + sgot->output_offset
1325 + off);
1326 outrel.r_info = ELF32_R_INFO (0, R_SPARC_RELATIVE);
1327 outrel.r_addend = 0;
1328 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1329 (((Elf32_External_Rela *)
1330 srelgot->contents)
1331 + srelgot->reloc_count));
1332 ++srelgot->reloc_count;
1333 }
1334
1335 local_got_offsets[r_symndx] |= 1;
1336 }
1337
1338 relocation = sgot->output_offset + off - got_base;
1339 }
1340
1341 break;
1342
1343 case R_SPARC_WPLT30:
1344 /* Relocation is to the entry for this symbol in the
1345 procedure linkage table. */
1346
1347 /* The Solaris native assembler will generate a WPLT30 reloc
1348 for a local symbol if you assemble a call from one
1349 section to another when using -K pic. We treat it as
1350 WDISP30. */
1351 if (h == NULL)
1352 break;
1353
1354 if (h->plt.offset == (bfd_vma) -1)
1355 {
1356 /* We didn't make a PLT entry for this symbol. This
1357 happens when statically linking PIC code, or when
1358 using -Bsymbolic. */
1359 break;
1360 }
1361
1362 if (splt == NULL)
1363 {
1364 splt = bfd_get_section_by_name (dynobj, ".plt");
1365 BFD_ASSERT (splt != NULL);
1366 }
1367
1368 relocation = (splt->output_section->vma
1369 + splt->output_offset
1370 + h->plt.offset);
1371 break;
1372
1373 case R_SPARC_PC10:
1374 case R_SPARC_PC22:
1375 if (h != NULL
1376 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
1377 break;
1378 /* Fall through. */
1379 case R_SPARC_DISP8:
1380 case R_SPARC_DISP16:
1381 case R_SPARC_DISP32:
1382 case R_SPARC_WDISP30:
1383 case R_SPARC_WDISP22:
1384 case R_SPARC_WDISP19:
1385 case R_SPARC_WDISP16:
1386 if (h == NULL
1387 || (info->symbolic
1388 && (h->elf_link_hash_flags
1389 & ELF_LINK_HASH_DEF_REGULAR) != 0))
1390 break;
1391 /* Fall through. */
1392 case R_SPARC_8:
1393 case R_SPARC_16:
1394 case R_SPARC_32:
1395 case R_SPARC_HI22:
1396 case R_SPARC_22:
1397 case R_SPARC_13:
1398 case R_SPARC_LO10:
1399 case R_SPARC_UA16:
1400 case R_SPARC_UA32:
1401 if (info->shared && (input_section->flags & SEC_ALLOC))
1402 {
1403 Elf_Internal_Rela outrel;
1404 boolean skip;
1405
1406 /* When generating a shared object, these relocations
1407 are copied into the output file to be resolved at run
1408 time. */
1409
1410 if (sreloc == NULL)
1411 {
1412 const char *name;
1413
1414 name = (bfd_elf_string_from_elf_section
1415 (input_bfd,
1416 elf_elfheader (input_bfd)->e_shstrndx,
1417 elf_section_data (input_section)->rel_hdr.sh_name));
1418 if (name == NULL)
1419 return false;
1420
1421 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
1422 && strcmp (bfd_get_section_name (input_bfd,
1423 input_section),
1424 name + 5) == 0);
1425
1426 sreloc = bfd_get_section_by_name (dynobj, name);
1427 BFD_ASSERT (sreloc != NULL);
1428 }
1429
1430 skip = false;
1431
1432 if (elf_section_data (input_section)->stab_info == NULL)
1433 outrel.r_offset = rel->r_offset;
1434 else
1435 {
1436 bfd_vma off;
1437
1438 off = (_bfd_stab_section_offset
1439 (output_bfd, &elf_hash_table (info)->stab_info,
1440 input_section,
1441 &elf_section_data (input_section)->stab_info,
1442 rel->r_offset));
1443 if (off == (bfd_vma) -1)
1444 skip = true;
1445 outrel.r_offset = off;
1446 }
1447
1448 outrel.r_offset += (input_section->output_section->vma
1449 + input_section->output_offset);
1450
1451 /* Optimize unaligned reloc usage now that we know where
1452 it finally resides. */
1453 switch (r_type)
1454 {
1455 case R_SPARC_16:
1456 if (outrel.r_offset & 1)
1457 r_type = R_SPARC_UA16;
1458 break;
1459 case R_SPARC_UA16:
1460 if (!(outrel.r_offset & 1))
1461 r_type = R_SPARC_16;
1462 break;
1463 case R_SPARC_32:
1464 if (outrel.r_offset & 3)
1465 r_type = R_SPARC_UA32;
1466 break;
1467 case R_SPARC_UA32:
1468 if (!(outrel.r_offset & 3))
1469 r_type = R_SPARC_32;
1470 break;
1471 }
1472
1473 if (skip)
1474 memset (&outrel, 0, sizeof outrel);
1475 /* h->dynindx may be -1 if the symbol was marked to
1476 become local. */
1477 else if (h != NULL
1478 && ((! info->symbolic && h->dynindx != -1)
1479 || (h->elf_link_hash_flags
1480 & ELF_LINK_HASH_DEF_REGULAR) == 0))
1481 {
1482 BFD_ASSERT (h->dynindx != -1);
1483 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
1484 outrel.r_addend = rel->r_addend;
1485 }
1486 else
1487 {
1488 if (r_type == R_SPARC_32)
1489 {
1490 outrel.r_info = ELF32_R_INFO (0, R_SPARC_RELATIVE);
1491 outrel.r_addend = relocation + rel->r_addend;
1492 }
1493 else
1494 {
1495 long indx;
1496
1497 if (h == NULL)
1498 sec = local_sections[r_symndx];
1499 else
1500 {
1501 BFD_ASSERT (h->root.type == bfd_link_hash_defined
1502 || (h->root.type
1503 == bfd_link_hash_defweak));
1504 sec = h->root.u.def.section;
1505 }
1506 if (sec != NULL && bfd_is_abs_section (sec))
1507 indx = 0;
1508 else if (sec == NULL || sec->owner == NULL)
1509 {
1510 bfd_set_error (bfd_error_bad_value);
1511 return false;
1512 }
1513 else
1514 {
1515 asection *osec;
1516
1517 osec = sec->output_section;
1518 indx = elf_section_data (osec)->dynindx;
1519
1520 /* FIXME: we really should be able to link non-pic
1521 shared libraries. */
1522 if (indx == 0)
1523 {
1524 BFD_FAIL ();
1525 (*_bfd_error_handler)
1526 (_("%s: probably compiled without -fPIC?"),
1527 bfd_get_filename (input_bfd));
1528 bfd_set_error (bfd_error_bad_value);
1529 return false;
1530 }
1531 }
1532
1533 outrel.r_info = ELF32_R_INFO (indx, r_type);
1534 outrel.r_addend = relocation + rel->r_addend;
1535 }
1536 }
1537
1538 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1539 (((Elf32_External_Rela *)
1540 sreloc->contents)
1541 + sreloc->reloc_count));
1542 ++sreloc->reloc_count;
1543
1544 /* This reloc will be computed at runtime, so there's no
1545 need to do anything now. */
1546 continue;
1547 }
1548 break;
1549
1550 default:
1551 break;
1552 }
1553
1554 r = bfd_reloc_continue;
1555 if (r_type == R_SPARC_WDISP16)
1556 {
1557 bfd_vma x;
1558
1559 relocation += rel->r_addend;
1560 relocation -= (input_section->output_section->vma
1561 + input_section->output_offset);
1562 relocation -= rel->r_offset;
1563
1564 x = bfd_get_32 (input_bfd, contents + rel->r_offset);
1565 x |= ((((relocation >> 2) & 0xc000) << 6)
1566 | ((relocation >> 2) & 0x3fff));
1567 bfd_put_32 (input_bfd, x, contents + rel->r_offset);
1568
1569 if ((bfd_signed_vma) relocation < - 0x40000
1570 || (bfd_signed_vma) relocation > 0x3ffff)
1571 r = bfd_reloc_overflow;
1572 else
1573 r = bfd_reloc_ok;
1574 }
1575 else if (r_type == R_SPARC_REV32)
1576 {
1577 bfd_vma x;
1578
1579 relocation = relocation + rel->r_addend;
1580
1581 x = bfd_get_32 (input_bfd, contents + rel->r_offset);
1582 x = x + relocation;
1583 bfd_putl32 (/*input_bfd,*/ x, contents + rel->r_offset);
1584 r = bfd_reloc_ok;
1585 }
1586 else if ((r_type == R_SPARC_WDISP30 || r_type == R_SPARC_WPLT30)
1587 && SEC_DO_RELAX (input_section)
1588 && rel->r_offset + 4 < input_section->_raw_size)
1589 {
1590 #define G0 0
1591 #define O7 15
1592 #define XCC (2 << 20)
1593 #define COND(x) (((x)&0xf)<<25)
1594 #define CONDA COND(0x8)
1595 #define INSN_BPA (F2(0,1) | CONDA | BPRED | XCC)
1596 #define INSN_BA (F2(0,2) | CONDA)
1597 #define INSN_OR F3(2, 0x2, 0)
1598 #define INSN_NOP F2(0,4)
1599
1600 bfd_vma x, y;
1601
1602 /* If the instruction is a call with either:
1603 restore
1604 arithmetic instruction with rd == %o7
1605 where rs1 != %o7 and rs2 if it is register != %o7
1606 then we can optimize if the call destination is near
1607 by changing the call into a branch always. */
1608 x = bfd_get_32 (input_bfd, contents + rel->r_offset);
1609 y = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
1610 if ((x & OP(~0)) == OP(1) && (y & OP(~0)) == OP(2))
1611 {
1612 if (((y & OP3(~0)) == OP3(0x3d) /* restore */
1613 || ((y & OP3(0x28)) == 0 /* arithmetic */
1614 && (y & RD(~0)) == RD(O7)))
1615 && (y & RS1(~0)) != RS1(O7)
1616 && ((y & F3I(~0))
1617 || (y & RS2(~0)) != RS2(O7)))
1618 {
1619 bfd_vma reloc;
1620
1621 reloc = relocation + rel->r_addend - rel->r_offset;
1622 reloc -= (input_section->output_section->vma
1623 + input_section->output_offset);
1624
1625 /* Ensure the reloc fits into simm22. */
1626 if ((reloc & 3) == 0
1627 && ((reloc & ~(bfd_vma)0x7fffff) == 0
1628 || ((reloc | 0x7fffff) == ~(bfd_vma)0)))
1629 {
1630 reloc >>= 2;
1631
1632 /* Check whether it fits into simm19 on v9. */
1633 if (((reloc & 0x3c0000) == 0
1634 || (reloc & 0x3c0000) == 0x3c0000)
1635 && (elf_elfheader (output_bfd)->e_flags & EF_SPARC_32PLUS))
1636 x = INSN_BPA | (reloc & 0x7ffff); /* ba,pt %xcc */
1637 else
1638 x = INSN_BA | (reloc & 0x3fffff); /* ba */
1639 bfd_put_32 (input_bfd, x, contents + rel->r_offset);
1640 r = bfd_reloc_ok;
1641 if (rel->r_offset >= 4
1642 && (y & (0xffffffff ^ RS1(~0)))
1643 == (INSN_OR | RD(O7) | RS2(G0)))
1644 {
1645 bfd_vma z;
1646 unsigned int reg;
1647
1648 z = bfd_get_32 (input_bfd,
1649 contents + rel->r_offset - 4);
1650 if ((z & (0xffffffff ^ RD(~0)))
1651 != (INSN_OR | RS1(O7) | RS2(G0)))
1652 break;
1653
1654 /* The sequence was
1655 or %o7, %g0, %rN
1656 call foo
1657 or %rN, %g0, %o7
1658
1659 If call foo was replaced with ba, replace
1660 or %rN, %g0, %o7 with nop. */
1661
1662 reg = (y & RS1(~0)) >> 14;
1663 if (reg != ((z & RD(~0)) >> 25)
1664 || reg == G0 || reg == O7)
1665 break;
1666
1667 bfd_put_32 (input_bfd, INSN_NOP,
1668 contents + rel->r_offset + 4);
1669 }
1670
1671 }
1672 }
1673 }
1674 }
1675
1676 if (r == bfd_reloc_continue)
1677 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1678 contents, rel->r_offset,
1679 relocation, rel->r_addend);
1680
1681 if (r != bfd_reloc_ok)
1682 {
1683 switch (r)
1684 {
1685 default:
1686 case bfd_reloc_outofrange:
1687 abort ();
1688 case bfd_reloc_overflow:
1689 {
1690 const char *name;
1691
1692 if (h != NULL)
1693 name = h->root.root.string;
1694 else
1695 {
1696 name = bfd_elf_string_from_elf_section (input_bfd,
1697 symtab_hdr->sh_link,
1698 sym->st_name);
1699 if (name == NULL)
1700 return false;
1701 if (*name == '\0')
1702 name = bfd_section_name (input_bfd, sec);
1703 }
1704 if (! ((*info->callbacks->reloc_overflow)
1705 (info, name, howto->name, (bfd_vma) 0,
1706 input_bfd, input_section, rel->r_offset)))
1707 return false;
1708 }
1709 break;
1710 }
1711 }
1712 }
1713
1714 return true;
1715 }
1716
1717 /* Finish up dynamic symbol handling. We set the contents of various
1718 dynamic sections here. */
1719
1720 static boolean
1721 elf32_sparc_finish_dynamic_symbol (output_bfd, info, h, sym)
1722 bfd *output_bfd;
1723 struct bfd_link_info *info;
1724 struct elf_link_hash_entry *h;
1725 Elf_Internal_Sym *sym;
1726 {
1727 bfd *dynobj;
1728
1729 dynobj = elf_hash_table (info)->dynobj;
1730
1731 if (h->plt.offset != (bfd_vma) -1)
1732 {
1733 asection *splt;
1734 asection *srela;
1735 Elf_Internal_Rela rela;
1736
1737 /* This symbol has an entry in the procedure linkage table. Set
1738 it up. */
1739
1740 BFD_ASSERT (h->dynindx != -1);
1741
1742 splt = bfd_get_section_by_name (dynobj, ".plt");
1743 srela = bfd_get_section_by_name (dynobj, ".rela.plt");
1744 BFD_ASSERT (splt != NULL && srela != NULL);
1745
1746 /* Fill in the entry in the procedure linkage table. */
1747 bfd_put_32 (output_bfd,
1748 PLT_ENTRY_WORD0 + h->plt.offset,
1749 splt->contents + h->plt.offset);
1750 bfd_put_32 (output_bfd,
1751 (PLT_ENTRY_WORD1
1752 + (((- (h->plt.offset + 4)) >> 2) & 0x3fffff)),
1753 splt->contents + h->plt.offset + 4);
1754 bfd_put_32 (output_bfd, PLT_ENTRY_WORD2,
1755 splt->contents + h->plt.offset + 8);
1756
1757 /* Fill in the entry in the .rela.plt section. */
1758 rela.r_offset = (splt->output_section->vma
1759 + splt->output_offset
1760 + h->plt.offset);
1761 rela.r_info = ELF32_R_INFO (h->dynindx, R_SPARC_JMP_SLOT);
1762 rela.r_addend = 0;
1763 bfd_elf32_swap_reloca_out (output_bfd, &rela,
1764 ((Elf32_External_Rela *) srela->contents
1765 + h->plt.offset / PLT_ENTRY_SIZE - 4));
1766
1767 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1768 {
1769 /* Mark the symbol as undefined, rather than as defined in
1770 the .plt section. Leave the value alone. */
1771 sym->st_shndx = SHN_UNDEF;
1772 /* If the symbol is weak, we do need to clear the value.
1773 Otherwise, the PLT entry would provide a definition for
1774 the symbol even if the symbol wasn't defined anywhere,
1775 and so the symbol would never be NULL. */
1776 if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR_NONWEAK)
1777 == 0)
1778 sym->st_value = 0;
1779 }
1780 }
1781
1782 if (h->got.offset != (bfd_vma) -1)
1783 {
1784 asection *sgot;
1785 asection *srela;
1786 Elf_Internal_Rela rela;
1787
1788 /* This symbol has an entry in the global offset table. Set it
1789 up. */
1790
1791 sgot = bfd_get_section_by_name (dynobj, ".got");
1792 srela = bfd_get_section_by_name (dynobj, ".rela.got");
1793 BFD_ASSERT (sgot != NULL && srela != NULL);
1794
1795 rela.r_offset = (sgot->output_section->vma
1796 + sgot->output_offset
1797 + (h->got.offset &~ 1));
1798
1799 /* If this is a -Bsymbolic link, and the symbol is defined
1800 locally, we just want to emit a RELATIVE reloc. Likewise if
1801 the symbol was forced to be local because of a version file.
1802 The entry in the global offset table will already have been
1803 initialized in the relocate_section function. */
1804 if (info->shared
1805 && (info->symbolic || h->dynindx == -1)
1806 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
1807 rela.r_info = ELF32_R_INFO (0, R_SPARC_RELATIVE);
1808 else
1809 {
1810 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
1811 rela.r_info = ELF32_R_INFO (h->dynindx, R_SPARC_GLOB_DAT);
1812 }
1813
1814 rela.r_addend = 0;
1815 bfd_elf32_swap_reloca_out (output_bfd, &rela,
1816 ((Elf32_External_Rela *) srela->contents
1817 + srela->reloc_count));
1818 ++srela->reloc_count;
1819 }
1820
1821 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
1822 {
1823 asection *s;
1824 Elf_Internal_Rela rela;
1825
1826 /* This symbols needs a copy reloc. Set it up. */
1827
1828 BFD_ASSERT (h->dynindx != -1);
1829
1830 s = bfd_get_section_by_name (h->root.u.def.section->owner,
1831 ".rela.bss");
1832 BFD_ASSERT (s != NULL);
1833
1834 rela.r_offset = (h->root.u.def.value
1835 + h->root.u.def.section->output_section->vma
1836 + h->root.u.def.section->output_offset);
1837 rela.r_info = ELF32_R_INFO (h->dynindx, R_SPARC_COPY);
1838 rela.r_addend = 0;
1839 bfd_elf32_swap_reloca_out (output_bfd, &rela,
1840 ((Elf32_External_Rela *) s->contents
1841 + s->reloc_count));
1842 ++s->reloc_count;
1843 }
1844
1845 /* Mark some specially defined symbols as absolute. */
1846 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
1847 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
1848 || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0)
1849 sym->st_shndx = SHN_ABS;
1850
1851 return true;
1852 }
1853
1854 /* Finish up the dynamic sections. */
1855
1856 static boolean
1857 elf32_sparc_finish_dynamic_sections (output_bfd, info)
1858 bfd *output_bfd;
1859 struct bfd_link_info *info;
1860 {
1861 bfd *dynobj;
1862 asection *sdyn;
1863 asection *sgot;
1864
1865 dynobj = elf_hash_table (info)->dynobj;
1866
1867 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
1868
1869 if (elf_hash_table (info)->dynamic_sections_created)
1870 {
1871 asection *splt;
1872 Elf32_External_Dyn *dyncon, *dynconend;
1873
1874 splt = bfd_get_section_by_name (dynobj, ".plt");
1875 BFD_ASSERT (splt != NULL && sdyn != NULL);
1876
1877 dyncon = (Elf32_External_Dyn *) sdyn->contents;
1878 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
1879 for (; dyncon < dynconend; dyncon++)
1880 {
1881 Elf_Internal_Dyn dyn;
1882 const char *name;
1883 boolean size;
1884
1885 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
1886
1887 switch (dyn.d_tag)
1888 {
1889 case DT_PLTGOT: name = ".plt"; size = false; break;
1890 case DT_PLTRELSZ: name = ".rela.plt"; size = true; break;
1891 case DT_JMPREL: name = ".rela.plt"; size = false; break;
1892 default: name = NULL; size = false; break;
1893 }
1894
1895 if (name != NULL)
1896 {
1897 asection *s;
1898
1899 s = bfd_get_section_by_name (output_bfd, name);
1900 if (s == NULL)
1901 dyn.d_un.d_val = 0;
1902 else
1903 {
1904 if (! size)
1905 dyn.d_un.d_ptr = s->vma;
1906 else
1907 {
1908 if (s->_cooked_size != 0)
1909 dyn.d_un.d_val = s->_cooked_size;
1910 else
1911 dyn.d_un.d_val = s->_raw_size;
1912 }
1913 }
1914 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1915 }
1916 }
1917
1918 /* Clear the first four entries in the procedure linkage table,
1919 and put a nop in the last four bytes. */
1920 if (splt->_raw_size > 0)
1921 {
1922 memset (splt->contents, 0, 4 * PLT_ENTRY_SIZE);
1923 bfd_put_32 (output_bfd, SPARC_NOP,
1924 splt->contents + splt->_raw_size - 4);
1925 }
1926
1927 elf_section_data (splt->output_section)->this_hdr.sh_entsize =
1928 PLT_ENTRY_SIZE;
1929 }
1930
1931 /* Set the first entry in the global offset table to the address of
1932 the dynamic section. */
1933 sgot = bfd_get_section_by_name (dynobj, ".got");
1934 BFD_ASSERT (sgot != NULL);
1935 if (sgot->_raw_size > 0)
1936 {
1937 if (sdyn == NULL)
1938 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
1939 else
1940 bfd_put_32 (output_bfd,
1941 sdyn->output_section->vma + sdyn->output_offset,
1942 sgot->contents);
1943 }
1944
1945 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
1946
1947 return true;
1948 }
1949 \f
1950 /* Functions for dealing with the e_flags field.
1951
1952 We don't define set_private_flags or copy_private_bfd_data because
1953 the only currently defined values are based on the bfd mach number,
1954 so we use the latter instead and defer setting e_flags until the
1955 file is written out. */
1956
1957 /* Merge backend specific data from an object file to the output
1958 object file when linking. */
1959
1960 static boolean
1961 elf32_sparc_merge_private_bfd_data (ibfd, obfd)
1962 bfd *ibfd;
1963 bfd *obfd;
1964 {
1965 boolean error;
1966 /* FIXME: This should not be static. */
1967 static unsigned long previous_ibfd_e_flags = (unsigned long) -1;
1968
1969 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1970 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1971 return true;
1972
1973 error = false;
1974
1975 if (bfd_get_mach (ibfd) >= bfd_mach_sparc_v9)
1976 {
1977 error = true;
1978 (*_bfd_error_handler)
1979 (_("%s: compiled for a 64 bit system and target is 32 bit"),
1980 bfd_get_filename (ibfd));
1981 }
1982 else if ((ibfd->flags & DYNAMIC) == 0)
1983 {
1984 if (bfd_get_mach (obfd) < bfd_get_mach (ibfd))
1985 bfd_set_arch_mach (obfd, bfd_arch_sparc, bfd_get_mach (ibfd));
1986 }
1987
1988 if (((elf_elfheader (ibfd)->e_flags & EF_SPARC_LEDATA)
1989 != previous_ibfd_e_flags)
1990 && previous_ibfd_e_flags != (unsigned long) -1)
1991 {
1992 (*_bfd_error_handler)
1993 (_("%s: linking little endian files with big endian files"),
1994 bfd_get_filename (ibfd));
1995 error = true;
1996 }
1997 previous_ibfd_e_flags = elf_elfheader (ibfd)->e_flags & EF_SPARC_LEDATA;
1998
1999 if (error)
2000 {
2001 bfd_set_error (bfd_error_bad_value);
2002 return false;
2003 }
2004
2005 return true;
2006 }
2007 \f
2008 /* Set the right machine number. */
2009
2010 static boolean
2011 elf32_sparc_object_p (abfd)
2012 bfd *abfd;
2013 {
2014 if (elf_elfheader (abfd)->e_machine == EM_SPARC32PLUS)
2015 {
2016 if (elf_elfheader (abfd)->e_flags & EF_SPARC_SUN_US3)
2017 return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
2018 bfd_mach_sparc_v8plusb);
2019 else if (elf_elfheader (abfd)->e_flags & EF_SPARC_SUN_US1)
2020 return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
2021 bfd_mach_sparc_v8plusa);
2022 else if (elf_elfheader (abfd)->e_flags & EF_SPARC_32PLUS)
2023 return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
2024 bfd_mach_sparc_v8plus);
2025 else
2026 return false;
2027 }
2028 else if (elf_elfheader (abfd)->e_flags & EF_SPARC_LEDATA)
2029 return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
2030 bfd_mach_sparc_sparclite_le);
2031 else
2032 return bfd_default_set_arch_mach (abfd, bfd_arch_sparc, bfd_mach_sparc);
2033 }
2034
2035 /* The final processing done just before writing out the object file.
2036 We need to set the e_machine field appropriately. */
2037
2038 static void
2039 elf32_sparc_final_write_processing (abfd, linker)
2040 bfd *abfd;
2041 boolean linker ATTRIBUTE_UNUSED;
2042 {
2043 switch (bfd_get_mach (abfd))
2044 {
2045 case bfd_mach_sparc :
2046 break; /* nothing to do */
2047 case bfd_mach_sparc_v8plus :
2048 elf_elfheader (abfd)->e_machine = EM_SPARC32PLUS;
2049 elf_elfheader (abfd)->e_flags &=~ EF_SPARC_32PLUS_MASK;
2050 elf_elfheader (abfd)->e_flags |= EF_SPARC_32PLUS;
2051 break;
2052 case bfd_mach_sparc_v8plusa :
2053 elf_elfheader (abfd)->e_machine = EM_SPARC32PLUS;
2054 elf_elfheader (abfd)->e_flags &=~ EF_SPARC_32PLUS_MASK;
2055 elf_elfheader (abfd)->e_flags |= EF_SPARC_32PLUS | EF_SPARC_SUN_US1;
2056 break;
2057 case bfd_mach_sparc_v8plusb :
2058 elf_elfheader (abfd)->e_machine = EM_SPARC32PLUS;
2059 elf_elfheader (abfd)->e_flags &=~ EF_SPARC_32PLUS_MASK;
2060 elf_elfheader (abfd)->e_flags |= EF_SPARC_32PLUS | EF_SPARC_SUN_US1
2061 | EF_SPARC_SUN_US3;
2062 break;
2063 case bfd_mach_sparc_sparclite_le :
2064 elf_elfheader (abfd)->e_machine = EM_SPARC;
2065 elf_elfheader (abfd)->e_flags |= EF_SPARC_LEDATA;
2066 break;
2067 default :
2068 abort ();
2069 break;
2070 }
2071 }
2072
2073 static enum elf_reloc_type_class
2074 elf32_sparc_reloc_type_class (type)
2075 int type;
2076 {
2077 switch (type)
2078 {
2079 case R_SPARC_RELATIVE:
2080 return reloc_class_relative;
2081 case R_SPARC_JMP_SLOT:
2082 return reloc_class_plt;
2083 case R_SPARC_COPY:
2084 return reloc_class_copy;
2085 default:
2086 return reloc_class_normal;
2087 }
2088 }
2089 \f
2090 #define TARGET_BIG_SYM bfd_elf32_sparc_vec
2091 #define TARGET_BIG_NAME "elf32-sparc"
2092 #define ELF_ARCH bfd_arch_sparc
2093 #define ELF_MACHINE_CODE EM_SPARC
2094 #define ELF_MACHINE_ALT1 EM_SPARC32PLUS
2095 #define ELF_MAXPAGESIZE 0x10000
2096
2097 #define bfd_elf32_bfd_reloc_type_lookup elf32_sparc_reloc_type_lookup
2098 #define bfd_elf32_bfd_relax_section elf32_sparc_relax_section
2099 #define elf_info_to_howto elf32_sparc_info_to_howto
2100 #define elf_backend_create_dynamic_sections \
2101 _bfd_elf_create_dynamic_sections
2102 #define elf_backend_check_relocs elf32_sparc_check_relocs
2103 #define elf_backend_adjust_dynamic_symbol \
2104 elf32_sparc_adjust_dynamic_symbol
2105 #define elf_backend_size_dynamic_sections \
2106 elf32_sparc_size_dynamic_sections
2107 #define elf_backend_relocate_section elf32_sparc_relocate_section
2108 #define elf_backend_finish_dynamic_symbol \
2109 elf32_sparc_finish_dynamic_symbol
2110 #define elf_backend_finish_dynamic_sections \
2111 elf32_sparc_finish_dynamic_sections
2112 #define bfd_elf32_bfd_merge_private_bfd_data \
2113 elf32_sparc_merge_private_bfd_data
2114 #define elf_backend_object_p elf32_sparc_object_p
2115 #define elf_backend_final_write_processing \
2116 elf32_sparc_final_write_processing
2117 #define elf_backend_gc_mark_hook elf32_sparc_gc_mark_hook
2118 #define elf_backend_gc_sweep_hook elf32_sparc_gc_sweep_hook
2119 #define elf_backend_reloc_type_class elf32_sparc_reloc_type_class
2120
2121 #define elf_backend_can_gc_sections 1
2122 #define elf_backend_want_got_plt 0
2123 #define elf_backend_plt_readonly 0
2124 #define elf_backend_want_plt_sym 1
2125 #define elf_backend_got_header_size 4
2126 #define elf_backend_plt_header_size (4*PLT_ENTRY_SIZE)
2127
2128 #include "elf32-target.h"