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