Commit | Line | Data |
---|---|---|
32090b8e KR |
1 | /* SPARC-specific support for 32-bit ELF |
2 | Copyright 1993 Free Software Foundation, Inc. | |
3 | ||
4 | This file is part of BFD, the Binary File Descriptor library. | |
5 | ||
6 | This program is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 2 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | This program is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with this program; if not, write to the Free Software | |
18 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
19 | ||
20 | #include "bfd.h" | |
21 | #include "sysdep.h" | |
013dec1a | 22 | #include "bfdlink.h" |
32090b8e KR |
23 | #include "libbfd.h" |
24 | #include "libelf.h" | |
25 | ||
013dec1a ILT |
26 | static CONST struct reloc_howto_struct *bfd_elf32_bfd_reloc_type_lookup |
27 | PARAMS ((bfd *, bfd_reloc_code_real_type)); | |
28 | static void elf_info_to_howto | |
29 | PARAMS ((bfd *, arelent *, Elf_Internal_Rela *)); | |
30 | static boolean elf32_sparc_create_dynamic_sections | |
31 | PARAMS ((bfd *, struct bfd_link_info *)); | |
12662be4 ILT |
32 | static boolean elf32_sparc_create_got_section |
33 | PARAMS ((bfd *, struct bfd_link_info *)); | |
4fbc96ad KR |
34 | static boolean elf32_sparc_check_relocs |
35 | PARAMS ((bfd *, struct bfd_link_info *, asection *, | |
36 | const Elf_Internal_Rela *)); | |
013dec1a ILT |
37 | static boolean elf32_sparc_adjust_dynamic_symbol |
38 | PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *)); | |
013dec1a ILT |
39 | static boolean elf32_sparc_size_dynamic_sections |
40 | PARAMS ((bfd *, struct bfd_link_info *)); | |
41 | static boolean elf32_sparc_relocate_section | |
42 | PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *, | |
12662be4 | 43 | Elf_Internal_Rela *, Elf_Internal_Sym *, asection **)); |
013dec1a ILT |
44 | static boolean elf32_sparc_finish_dynamic_symbol |
45 | PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *, | |
46 | Elf_Internal_Sym *)); | |
47 | static boolean elf32_sparc_finish_dynamic_sections | |
48 | PARAMS ((bfd *, struct bfd_link_info *)); | |
49 | ||
32090b8e KR |
50 | enum reloc_type |
51 | { | |
52 | R_SPARC_NONE = 0, | |
53 | R_SPARC_8, R_SPARC_16, R_SPARC_32, | |
54 | R_SPARC_DISP8, R_SPARC_DISP16, R_SPARC_DISP32, | |
55 | R_SPARC_WDISP30, R_SPARC_WDISP22, | |
56 | R_SPARC_HI22, R_SPARC_22, | |
57 | R_SPARC_13, R_SPARC_LO10, | |
58 | R_SPARC_GOT10, R_SPARC_GOT13, R_SPARC_GOT22, | |
59 | R_SPARC_PC10, R_SPARC_PC22, | |
60 | R_SPARC_WPLT30, | |
61 | R_SPARC_COPY, | |
62 | R_SPARC_GLOB_DAT, R_SPARC_JMP_SLOT, | |
63 | R_SPARC_RELATIVE, | |
64 | R_SPARC_UA32, | |
65 | R_SPARC_max | |
66 | }; | |
67 | ||
68 | #if 0 | |
69 | static CONST char *CONST reloc_type_names[] = | |
70 | { | |
71 | "R_SPARC_NONE", | |
72 | "R_SPARC_8", "R_SPARC_16", "R_SPARC_32", | |
73 | "R_SPARC_DISP8", "R_SPARC_DISP16", "R_SPARC_DISP32", | |
74 | "R_SPARC_WDISP30", "R_SPARC_WDISP22", | |
75 | "R_SPARC_HI22", "R_SPARC_22", | |
76 | "R_SPARC_13", "R_SPARC_LO10", | |
77 | "R_SPARC_GOT10", "R_SPARC_GOT13", "R_SPARC_GOT22", | |
78 | "R_SPARC_PC10", "R_SPARC_PC22", | |
79 | "R_SPARC_WPLT30", | |
80 | "R_SPARC_COPY", | |
81 | "R_SPARC_GLOB_DAT", "R_SPARC_JMP_SLOT", | |
82 | "R_SPARC_RELATIVE", | |
83 | "R_SPARC_UA32", | |
84 | }; | |
85 | #endif | |
86 | ||
87 | static reloc_howto_type elf_sparc_howto_table[] = | |
88 | { | |
013dec1a ILT |
89 | HOWTO(R_SPARC_NONE, 0,0, 0,false,0,complain_overflow_dont, bfd_elf_generic_reloc,"R_SPARC_NONE", false,0,0x00000000,true), |
90 | HOWTO(R_SPARC_8, 0,0, 8,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,"R_SPARC_8", false,0,0x000000ff,true), | |
91 | HOWTO(R_SPARC_16, 0,1,16,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,"R_SPARC_16", false,0,0x0000ffff,true), | |
92 | HOWTO(R_SPARC_32, 0,2,32,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,"R_SPARC_32", false,0,0xffffffff,true), | |
93 | HOWTO(R_SPARC_DISP8, 0,0, 8,true, 0,complain_overflow_signed, bfd_elf_generic_reloc,"R_SPARC_DISP8", false,0,0x000000ff,true), | |
94 | HOWTO(R_SPARC_DISP16, 0,1,16,true, 0,complain_overflow_signed, bfd_elf_generic_reloc,"R_SPARC_DISP16", false,0,0x0000ffff,true), | |
95 | HOWTO(R_SPARC_DISP32, 0,2,32,true, 0,complain_overflow_signed, bfd_elf_generic_reloc,"R_SPARC_DISP32", false,0,0x00ffffff,true), | |
96 | HOWTO(R_SPARC_WDISP30, 2,2,30,true, 0,complain_overflow_signed, bfd_elf_generic_reloc,"R_SPARC_WDISP30", false,0,0x3fffffff,true), | |
97 | HOWTO(R_SPARC_WDISP22, 2,2,22,true, 0,complain_overflow_signed, bfd_elf_generic_reloc,"R_SPARC_WDISP22", false,0,0x003fffff,true), | |
98 | HOWTO(R_SPARC_HI22, 10,2,22,false,0,complain_overflow_dont, bfd_elf_generic_reloc,"R_SPARC_HI22", false,0,0x003fffff,true), | |
99 | HOWTO(R_SPARC_22, 0,2,22,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,"R_SPARC_22", false,0,0x003fffff,true), | |
100 | HOWTO(R_SPARC_13, 0,2,13,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,"R_SPARC_13", false,0,0x00001fff,true), | |
101 | HOWTO(R_SPARC_LO10, 0,2,10,false,0,complain_overflow_dont, bfd_elf_generic_reloc,"R_SPARC_LO10", false,0,0x000003ff,true), | |
4fbc96ad | 102 | HOWTO(R_SPARC_GOT10, 0,2,10,false,0,complain_overflow_dont, bfd_elf_generic_reloc,"R_SPARC_GOT10", false,0,0x000003ff,true), |
013dec1a | 103 | HOWTO(R_SPARC_GOT13, 0,2,13,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,"R_SPARC_GOT13", false,0,0x00001fff,true), |
4fbc96ad KR |
104 | HOWTO(R_SPARC_GOT22, 10,2,22,false,0,complain_overflow_dont, bfd_elf_generic_reloc,"R_SPARC_GOT22", false,0,0x003fffff,true), |
105 | HOWTO(R_SPARC_PC10, 0,2,10,true, 0,complain_overflow_dont, bfd_elf_generic_reloc,"R_SPARC_PC10", false,0,0x000003ff,true), | |
12662be4 ILT |
106 | HOWTO(R_SPARC_PC22, 10,2,22,true, 0,complain_overflow_bitfield,bfd_elf_generic_reloc,"R_SPARC_PC22", false,0,0x003fffff,true), |
107 | HOWTO(R_SPARC_WPLT30, 2,2,30,true, 0,complain_overflow_signed, bfd_elf_generic_reloc,"R_SPARC_WPLT30", false,0,0x3fffffff,true), | |
013dec1a ILT |
108 | HOWTO(R_SPARC_COPY, 0,0,00,false,0,complain_overflow_dont, bfd_elf_generic_reloc,"R_SPARC_COPY", false,0,0x00000000,true), |
109 | 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), | |
110 | 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), | |
111 | HOWTO(R_SPARC_RELATIVE,0,0,00,false,0,complain_overflow_dont, bfd_elf_generic_reloc,"R_SPARC_RELATIVE",false,0,0x00000000,true), | |
112 | HOWTO(R_SPARC_UA32, 0,0,00,false,0,complain_overflow_dont, bfd_elf_generic_reloc,"R_SPARC_UA32", false,0,0x00000000,true), | |
32090b8e KR |
113 | }; |
114 | ||
115 | struct elf_reloc_map { | |
116 | unsigned char bfd_reloc_val; | |
117 | unsigned char elf_reloc_val; | |
118 | }; | |
119 | ||
120 | static CONST struct elf_reloc_map sparc_reloc_map[] = | |
121 | { | |
122 | { BFD_RELOC_NONE, R_SPARC_NONE, }, | |
123 | { BFD_RELOC_16, R_SPARC_16, }, | |
124 | { BFD_RELOC_8, R_SPARC_8 }, | |
125 | { BFD_RELOC_8_PCREL, R_SPARC_DISP8 }, | |
126 | { BFD_RELOC_CTOR, R_SPARC_32 }, /* @@ Assumes 32 bits. */ | |
127 | { BFD_RELOC_32, R_SPARC_32 }, | |
128 | { BFD_RELOC_32_PCREL, R_SPARC_DISP32 }, | |
129 | { BFD_RELOC_HI22, R_SPARC_HI22 }, | |
130 | { BFD_RELOC_LO10, R_SPARC_LO10, }, | |
131 | { BFD_RELOC_32_PCREL_S2, R_SPARC_WDISP30 }, | |
132 | { BFD_RELOC_SPARC22, R_SPARC_22 }, | |
133 | { BFD_RELOC_SPARC13, R_SPARC_13 }, | |
134 | { BFD_RELOC_SPARC_GOT10, R_SPARC_GOT10 }, | |
135 | { BFD_RELOC_SPARC_GOT13, R_SPARC_GOT13 }, | |
136 | { BFD_RELOC_SPARC_GOT22, R_SPARC_GOT22 }, | |
137 | { BFD_RELOC_SPARC_PC10, R_SPARC_PC10 }, | |
138 | { BFD_RELOC_SPARC_PC22, R_SPARC_PC22 }, | |
139 | { BFD_RELOC_SPARC_WPLT30, R_SPARC_WPLT30 }, | |
140 | { BFD_RELOC_SPARC_COPY, R_SPARC_COPY }, | |
141 | { BFD_RELOC_SPARC_GLOB_DAT, R_SPARC_GLOB_DAT }, | |
142 | { BFD_RELOC_SPARC_JMP_SLOT, R_SPARC_JMP_SLOT }, | |
143 | { BFD_RELOC_SPARC_RELATIVE, R_SPARC_RELATIVE }, | |
144 | { BFD_RELOC_SPARC_WDISP22, R_SPARC_WDISP22 }, | |
4fbc96ad | 145 | /*{ BFD_RELOC_SPARC_UA32, R_SPARC_UA32 }, not used?? */ |
32090b8e KR |
146 | }; |
147 | ||
148 | static CONST struct reloc_howto_struct * | |
013dec1a ILT |
149 | bfd_elf32_bfd_reloc_type_lookup (abfd, code) |
150 | bfd *abfd; | |
151 | bfd_reloc_code_real_type code; | |
32090b8e KR |
152 | { |
153 | int i; | |
154 | for (i = 0; i < sizeof (sparc_reloc_map) / sizeof (struct elf_reloc_map); i++) | |
155 | { | |
156 | if (sparc_reloc_map[i].bfd_reloc_val == code) | |
157 | return &elf_sparc_howto_table[(int) sparc_reloc_map[i].elf_reloc_val]; | |
158 | } | |
159 | return 0; | |
160 | } | |
161 | ||
162 | static void | |
013dec1a ILT |
163 | elf_info_to_howto (abfd, cache_ptr, dst) |
164 | bfd *abfd; | |
165 | arelent *cache_ptr; | |
166 | Elf_Internal_Rela *dst; | |
32090b8e KR |
167 | { |
168 | BFD_ASSERT (ELF32_R_TYPE(dst->r_info) < (unsigned int) R_SPARC_max); | |
169 | cache_ptr->howto = &elf_sparc_howto_table[ELF32_R_TYPE(dst->r_info)]; | |
170 | } | |
171 | ||
013dec1a ILT |
172 | \f |
173 | /* Functions for the SPARC ELF linker. */ | |
174 | ||
175 | /* The name of the dynamic interpreter. This is put in the .interp | |
176 | section. */ | |
177 | ||
178 | #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1" | |
179 | ||
180 | /* The nop opcode we use. */ | |
181 | ||
182 | #define SPARC_NOP 0x01000000 | |
183 | ||
184 | /* The size in bytes of an entry in the procedure linkage table. */ | |
185 | ||
186 | #define PLT_ENTRY_SIZE 12 | |
187 | ||
188 | /* The first four entries in a procedure linkage table are reserved, | |
189 | and the initial contents are unimportant (we zero them out). | |
190 | Subsequent entries look like this. See the SVR4 ABI SPARC | |
191 | supplement to see how this works. */ | |
192 | ||
193 | /* sethi %hi(.-.plt0),%g1. We fill in the address later. */ | |
194 | #define PLT_ENTRY_WORD0 0x03000000 | |
195 | /* b,a .plt0. We fill in the offset later. */ | |
196 | #define PLT_ENTRY_WORD1 0x30800000 | |
197 | /* nop. */ | |
198 | #define PLT_ENTRY_WORD2 SPARC_NOP | |
199 | ||
200 | /* Create dynamic sections when linking against a dynamic object. */ | |
201 | ||
202 | static boolean | |
203 | elf32_sparc_create_dynamic_sections (abfd, info) | |
204 | bfd *abfd; | |
205 | struct bfd_link_info *info; | |
206 | { | |
207 | flagword flags; | |
208 | register asection *s; | |
209 | struct elf_link_hash_entry *h; | |
210 | ||
211 | /* We need to create .plt, .rela.plt, .got, .dynbss, and .rela.bss | |
212 | sections. */ | |
213 | ||
214 | flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY; | |
215 | ||
216 | s = bfd_make_section (abfd, ".plt"); | |
217 | if (s == NULL | |
218 | || ! bfd_set_section_flags (abfd, s, flags | SEC_CODE) | |
219 | || ! bfd_set_section_alignment (abfd, s, 2)) | |
220 | return false; | |
221 | ||
222 | /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the | |
223 | .plt section. */ | |
224 | h = NULL; | |
225 | if (! (_bfd_generic_link_add_one_symbol | |
226 | (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s, (bfd_vma) 0, | |
227 | (const char *) NULL, false, get_elf_backend_data (abfd)->collect, | |
228 | (struct bfd_link_hash_entry **) &h))) | |
229 | return false; | |
230 | h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR; | |
4fbc96ad KR |
231 | h->type = STT_OBJECT; |
232 | ||
233 | if (info->shared | |
234 | && ! bfd_elf32_link_record_dynamic_symbol (info, h)) | |
235 | return false; | |
013dec1a | 236 | |
013dec1a ILT |
237 | s = bfd_make_section (abfd, ".rela.plt"); |
238 | if (s == NULL | |
239 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
240 | || ! bfd_set_section_alignment (abfd, s, 2)) | |
241 | return false; | |
242 | ||
12662be4 ILT |
243 | if (! elf32_sparc_create_got_section (abfd, info)) |
244 | return false; | |
245 | ||
246 | /* The .dynbss section is a place to put symbols which are defined | |
247 | by dynamic objects, are referenced by regular objects, and are | |
248 | not functions. We must allocate space for them in the process | |
249 | image and use a R_SPARC_COPY reloc to tell the dynamic linker to | |
250 | initialize them at run time. The linker script puts the .dynbss | |
251 | section into the .bss section of the final image. */ | |
252 | s = bfd_make_section (abfd, ".dynbss"); | |
253 | if (s == NULL | |
254 | || ! bfd_set_section_flags (abfd, s, SEC_ALLOC)) | |
255 | return false; | |
256 | ||
257 | /* The .rela.bss section holds copy relocs. */ | |
258 | if (! info->shared) | |
259 | { | |
260 | s = bfd_make_section (abfd, ".rela.bss"); | |
261 | if (s == NULL | |
262 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
263 | || ! bfd_set_section_alignment (abfd, s, 2)) | |
264 | return false; | |
265 | } | |
266 | ||
267 | return true; | |
268 | } | |
269 | ||
270 | /* Create the .got section to hold the global offset table. */ | |
271 | ||
272 | static boolean | |
273 | elf32_sparc_create_got_section (abfd, info) | |
274 | bfd *abfd; | |
275 | struct bfd_link_info *info; | |
276 | { | |
277 | register asection *s; | |
278 | struct elf_link_hash_entry *h; | |
279 | ||
280 | /* This function may be called more than once. */ | |
281 | if (bfd_get_section_by_name (abfd, ".got") != NULL) | |
282 | return true; | |
283 | ||
013dec1a ILT |
284 | s = bfd_make_section (abfd, ".got"); |
285 | if (s == NULL | |
12662be4 ILT |
286 | || ! bfd_set_section_flags (abfd, s, |
287 | (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | |
288 | | SEC_IN_MEMORY)) | |
013dec1a ILT |
289 | || ! bfd_set_section_alignment (abfd, s, 2)) |
290 | return false; | |
291 | ||
292 | /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got | |
293 | section. We don't do this in the linker script because we don't | |
294 | want to define the symbol if we are not creating a global offset | |
295 | table. FIXME: The Solaris linker puts _GLOBAL_OFFSET_TABLE_ at | |
296 | the start of the .got section, but when using the small PIC model | |
297 | the .got is accessed using a signed 13 bit offset. Shouldn't | |
298 | _GLOBAL_OFFSET_TABLE_ be located at .got + 4096? */ | |
299 | h = NULL; | |
300 | if (! (_bfd_generic_link_add_one_symbol | |
301 | (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s, (bfd_vma) 0, | |
302 | (const char *) NULL, false, get_elf_backend_data (abfd)->collect, | |
303 | (struct bfd_link_hash_entry **) &h))) | |
304 | return false; | |
305 | h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR; | |
4fbc96ad KR |
306 | h->type = STT_OBJECT; |
307 | ||
308 | if (info->shared | |
309 | && ! bfd_elf32_link_record_dynamic_symbol (info, h)) | |
310 | return false; | |
013dec1a ILT |
311 | |
312 | /* The first global offset table entry is reserved. */ | |
313 | s->_raw_size += 4; | |
314 | ||
4fbc96ad | 315 | return true; |
12662be4 | 316 | } |
4fbc96ad KR |
317 | |
318 | /* Look through the relocs for a section during the first phase, and | |
319 | allocate space in the global offset table or procedure linkage | |
320 | table. */ | |
321 | ||
322 | static boolean | |
323 | elf32_sparc_check_relocs (abfd, info, sec, relocs) | |
324 | bfd *abfd; | |
325 | struct bfd_link_info *info; | |
326 | asection *sec; | |
327 | const Elf_Internal_Rela *relocs; | |
328 | { | |
329 | bfd *dynobj; | |
330 | Elf_Internal_Shdr *symtab_hdr; | |
331 | struct elf_link_hash_entry **sym_hashes; | |
332 | bfd_vma *local_got_offsets; | |
333 | const Elf_Internal_Rela *rel; | |
334 | const Elf_Internal_Rela *rel_end; | |
335 | asection *sgot; | |
336 | asection *srelgot; | |
4fbc96ad KR |
337 | asection *sreloc; |
338 | ||
339 | if (info->relocateable) | |
340 | return true; | |
341 | ||
342 | dynobj = elf_hash_table (info)->dynobj; | |
343 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
344 | sym_hashes = elf_sym_hashes (abfd); | |
345 | local_got_offsets = elf_local_got_offsets (abfd); | |
346 | ||
347 | sgot = NULL; | |
348 | srelgot = NULL; | |
4fbc96ad KR |
349 | sreloc = NULL; |
350 | ||
351 | rel_end = relocs + sec->reloc_count; | |
352 | for (rel = relocs; rel < rel_end; rel++) | |
353 | { | |
354 | long r_symndx; | |
355 | struct elf_link_hash_entry *h; | |
356 | ||
357 | r_symndx = ELF32_R_SYM (rel->r_info); | |
358 | if (r_symndx < symtab_hdr->sh_info) | |
359 | h = NULL; | |
360 | else | |
361 | h = sym_hashes[r_symndx - symtab_hdr->sh_info]; | |
362 | ||
4fbc96ad KR |
363 | switch (ELF32_R_TYPE (rel->r_info)) |
364 | { | |
365 | case R_SPARC_GOT10: | |
366 | case R_SPARC_GOT13: | |
367 | case R_SPARC_GOT22: | |
368 | /* This symbol requires a global offset table entry. */ | |
369 | ||
12662be4 ILT |
370 | if (dynobj == NULL) |
371 | { | |
372 | /* Create the .got section. */ | |
373 | elf_hash_table (info)->dynobj = dynobj = abfd; | |
374 | if (! elf32_sparc_create_got_section (dynobj, info)) | |
375 | return false; | |
376 | } | |
377 | ||
4fbc96ad KR |
378 | if (sgot == NULL) |
379 | { | |
380 | sgot = bfd_get_section_by_name (dynobj, ".got"); | |
12662be4 ILT |
381 | BFD_ASSERT (sgot != NULL); |
382 | } | |
383 | ||
384 | if (srelgot == NULL | |
385 | && (h != NULL || info->shared)) | |
386 | { | |
4fbc96ad KR |
387 | srelgot = bfd_get_section_by_name (dynobj, ".rela.got"); |
388 | if (srelgot == NULL) | |
389 | { | |
390 | srelgot = bfd_make_section (dynobj, ".rela.got"); | |
391 | if (srelgot == NULL | |
392 | || ! bfd_set_section_flags (dynobj, srelgot, | |
393 | (SEC_ALLOC | |
394 | | SEC_LOAD | |
395 | | SEC_HAS_CONTENTS | |
396 | | SEC_IN_MEMORY | |
12662be4 ILT |
397 | | SEC_READONLY)) |
398 | || ! bfd_set_section_alignment (dynobj, srelgot, 2)) | |
4fbc96ad KR |
399 | return false; |
400 | } | |
4fbc96ad KR |
401 | } |
402 | ||
403 | if (h != NULL) | |
404 | { | |
405 | if (h->got_offset != (bfd_vma) -1) | |
406 | { | |
407 | /* We have already allocated space in the .got. */ | |
408 | break; | |
409 | } | |
410 | h->got_offset = sgot->_raw_size; | |
12662be4 ILT |
411 | |
412 | /* Make sure this symbol is output as a dynamic symbol. */ | |
413 | if (h->dynindx == -1) | |
414 | { | |
415 | if (! bfd_elf32_link_record_dynamic_symbol (info, h)) | |
416 | return false; | |
417 | } | |
418 | ||
419 | srelgot->_raw_size += sizeof (Elf32_External_Rela); | |
4fbc96ad KR |
420 | } |
421 | else | |
422 | { | |
423 | /* This is a global offset table entry for a local | |
424 | symbol. */ | |
425 | if (local_got_offsets == NULL) | |
426 | { | |
427 | size_t size; | |
428 | register int i; | |
429 | ||
430 | size = symtab_hdr->sh_info * sizeof (bfd_vma); | |
431 | local_got_offsets = (bfd_vma *) bfd_alloc (abfd, size); | |
432 | if (local_got_offsets == NULL) | |
433 | { | |
434 | bfd_set_error (bfd_error_no_memory); | |
435 | return false; | |
436 | } | |
437 | elf_local_got_offsets (abfd) = local_got_offsets; | |
438 | for (i = 0; i < symtab_hdr->sh_info; i++) | |
439 | local_got_offsets[i] = (bfd_vma) -1; | |
440 | } | |
441 | if (local_got_offsets[r_symndx] != (bfd_vma) -1) | |
442 | { | |
443 | /* We have already allocated space in the .got. */ | |
444 | break; | |
445 | } | |
446 | local_got_offsets[r_symndx] = sgot->_raw_size; | |
12662be4 ILT |
447 | |
448 | if (info->shared) | |
449 | { | |
450 | /* If we are generating a shared object, we need to | |
451 | output a R_SPARC_RELATIVE reloc so that the | |
452 | dynamic linker can adjust this GOT entry. */ | |
453 | srelgot->_raw_size += sizeof (Elf32_External_Rela); | |
454 | } | |
4fbc96ad KR |
455 | } |
456 | ||
457 | sgot->_raw_size += 4; | |
4fbc96ad KR |
458 | |
459 | break; | |
460 | ||
461 | case R_SPARC_WPLT30: | |
12662be4 ILT |
462 | /* This symbol requires a procedure linkage table entry. We |
463 | actually build the entry in adjust_dynamic_symbol, | |
464 | because this might be a case of linking PIC code without | |
465 | linking in any dynamic objects, in which case we don't | |
466 | need to generate a procedure linkage table after all. */ | |
4fbc96ad KR |
467 | |
468 | if (h == NULL) | |
469 | { | |
470 | /* It does not make sense to have a procedure linkage | |
471 | table entry for a local symbol. */ | |
472 | bfd_set_error (bfd_error_bad_value); | |
473 | return false; | |
474 | } | |
475 | ||
12662be4 ILT |
476 | /* Make sure this symbol is output as a dynamic symbol. */ |
477 | if (h->dynindx == -1) | |
4fbc96ad | 478 | { |
12662be4 ILT |
479 | if (! bfd_elf32_link_record_dynamic_symbol (info, h)) |
480 | return false; | |
4fbc96ad KR |
481 | } |
482 | ||
12662be4 | 483 | h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT; |
4fbc96ad KR |
484 | |
485 | break; | |
486 | ||
487 | case R_SPARC_PC10: | |
488 | case R_SPARC_PC22: | |
489 | if (h != NULL | |
490 | && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0) | |
491 | break; | |
492 | /* Fall through. */ | |
493 | case R_SPARC_8: | |
494 | case R_SPARC_16: | |
495 | case R_SPARC_32: | |
496 | case R_SPARC_DISP8: | |
497 | case R_SPARC_DISP16: | |
498 | case R_SPARC_DISP32: | |
499 | case R_SPARC_WDISP30: | |
500 | case R_SPARC_WDISP22: | |
501 | case R_SPARC_HI22: | |
502 | case R_SPARC_22: | |
503 | case R_SPARC_13: | |
504 | case R_SPARC_LO10: | |
505 | case R_SPARC_UA32: | |
506 | if (info->shared | |
507 | && (sec->flags & SEC_ALLOC) != 0) | |
508 | { | |
509 | /* When creating a shared object, we must copy these | |
510 | relocs into the output file. We create a reloc | |
511 | section in dynobj and make room for the reloc. */ | |
512 | if (sreloc == NULL) | |
513 | { | |
514 | const char *name; | |
515 | ||
516 | name = (elf_string_from_elf_section | |
517 | (abfd, | |
518 | elf_elfheader (abfd)->e_shstrndx, | |
519 | elf_section_data (sec)->rel_hdr.sh_name)); | |
520 | if (name == NULL) | |
521 | return false; | |
522 | ||
523 | BFD_ASSERT (strncmp (name, ".rela", 5) == 0 | |
524 | && strcmp (bfd_get_section_name (abfd, sec), | |
525 | name + 5) == 0); | |
526 | ||
527 | sreloc = bfd_get_section_by_name (dynobj, name); | |
528 | if (sreloc == NULL) | |
529 | { | |
530 | sreloc = bfd_make_section (dynobj, name); | |
531 | if (sreloc == NULL | |
532 | || ! bfd_set_section_flags (dynobj, sreloc, | |
533 | (SEC_ALLOC | |
534 | | SEC_LOAD | |
535 | | SEC_HAS_CONTENTS | |
536 | | SEC_IN_MEMORY | |
537 | | SEC_READONLY)) | |
538 | || ! bfd_set_section_alignment (dynobj, sreloc, 2)) | |
539 | return false; | |
540 | } | |
541 | } | |
542 | ||
543 | sreloc->_raw_size += sizeof (Elf32_External_Rela); | |
544 | } | |
545 | ||
546 | break; | |
547 | ||
548 | default: | |
549 | break; | |
550 | } | |
551 | } | |
013dec1a ILT |
552 | |
553 | return true; | |
554 | } | |
555 | ||
556 | /* Adjust a symbol defined by a dynamic object and referenced by a | |
557 | regular object. The current definition is in some section of the | |
558 | dynamic object, but we're not including those sections. We have to | |
559 | change the definition to something the rest of the link can | |
560 | understand. */ | |
561 | ||
562 | static boolean | |
563 | elf32_sparc_adjust_dynamic_symbol (info, h) | |
564 | struct bfd_link_info *info; | |
565 | struct elf_link_hash_entry *h; | |
566 | { | |
567 | bfd *dynobj; | |
568 | asection *s; | |
569 | unsigned int power_of_two; | |
013dec1a ILT |
570 | |
571 | dynobj = elf_hash_table (info)->dynobj; | |
572 | ||
573 | /* Make sure we know what is going on here. */ | |
574 | BFD_ASSERT (dynobj != NULL | |
013dec1a ILT |
575 | && h->root.type == bfd_link_hash_defined |
576 | && (bfd_get_flavour (h->root.u.def.section->owner) | |
577 | == bfd_target_elf_flavour) | |
12662be4 ILT |
578 | && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) |
579 | || ((h->elf_link_hash_flags | |
580 | & ELF_LINK_HASH_DEF_DYNAMIC) != 0 | |
581 | && (h->elf_link_hash_flags | |
582 | & ELF_LINK_HASH_REF_REGULAR) != 0 | |
583 | && (h->elf_link_hash_flags | |
584 | & ELF_LINK_HASH_DEF_REGULAR) == 0 | |
585 | && (elf_elfheader (h->root.u.def.section->owner)->e_type | |
586 | == ET_DYN) | |
587 | && h->root.u.def.section->output_section == NULL))); | |
013dec1a ILT |
588 | |
589 | /* If this is a function, put it in the procedure linkage table. We | |
590 | will fill in the contents of the procedure linkage table later | |
591 | (although we could actually do it here). */ | |
12662be4 ILT |
592 | if (h->type == STT_FUNC |
593 | || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0) | |
013dec1a | 594 | { |
12662be4 | 595 | if (! elf_hash_table (info)->dynamic_sections_created) |
013dec1a | 596 | { |
12662be4 ILT |
597 | /* This case can occur if we saw a WPLT30 reloc in an input |
598 | file, but none of the input files were dynamic objects. | |
599 | In such a case, we don't actually need to build a | |
600 | procedure linkage table, and we can just do a WDISP30 | |
601 | reloc instead. */ | |
602 | BFD_ASSERT ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0); | |
603 | return true; | |
604 | } | |
4fbc96ad | 605 | |
12662be4 ILT |
606 | s = bfd_get_section_by_name (dynobj, ".plt"); |
607 | BFD_ASSERT (s != NULL); | |
013dec1a | 608 | |
12662be4 ILT |
609 | /* The first four entries in .plt are reserved. */ |
610 | if (s->_raw_size == 0) | |
611 | s->_raw_size = 4 * PLT_ENTRY_SIZE; | |
013dec1a | 612 | |
12662be4 ILT |
613 | /* The procedure linkage table has a maximum size. */ |
614 | if (s->_raw_size >= 0x400000) | |
615 | { | |
616 | bfd_set_error (bfd_error_bad_value); | |
617 | return false; | |
618 | } | |
013dec1a | 619 | |
12662be4 ILT |
620 | /* Set the symbol to this location in the .plt. */ |
621 | h->root.u.def.section = s; | |
622 | h->root.u.def.value = s->_raw_size; | |
013dec1a | 623 | |
12662be4 | 624 | h->plt_offset = s->_raw_size; |
4fbc96ad | 625 | |
12662be4 ILT |
626 | /* Make room for this entry. */ |
627 | s->_raw_size += PLT_ENTRY_SIZE; | |
628 | ||
629 | /* We also need to make an entry in the .rela.plt section. */ | |
630 | ||
631 | s = bfd_get_section_by_name (dynobj, ".rela.plt"); | |
632 | BFD_ASSERT (s != NULL); | |
633 | s->_raw_size += sizeof (Elf32_External_Rela); | |
013dec1a ILT |
634 | |
635 | return true; | |
636 | } | |
637 | ||
638 | /* If this is a weak symbol, and there is a real definition, the | |
639 | processor independent code will have arranged for us to see the | |
640 | real definition first, and we can just use the same value. */ | |
641 | if (h->weakdef != NULL) | |
642 | { | |
643 | BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined); | |
644 | h->root.u.def.section = h->weakdef->root.u.def.section; | |
645 | h->root.u.def.value = h->weakdef->root.u.def.value; | |
013dec1a ILT |
646 | return true; |
647 | } | |
648 | ||
649 | /* This is a reference to a symbol defined by a dynamic object which | |
4fbc96ad KR |
650 | is not a function. */ |
651 | ||
652 | /* If we are creating a shared library, we must presume that the | |
653 | only references to the symbol are via the global offset table. | |
654 | For such cases we need not do anything here; the relocations will | |
655 | be handled correctly by relocate_section. */ | |
656 | if (info->shared) | |
657 | return true; | |
658 | ||
659 | /* We must allocate the symbol in our .dynbss section, which will | |
660 | become part of the .bss section of the executable. There will be | |
661 | an entry for this symbol in the .dynsym section. The dynamic | |
662 | object will contain position independent code, so all references | |
663 | from the dynamic object to this symbol will go through the global | |
664 | offset table. The dynamic linker will use the .dynsym entry to | |
665 | determine the address it must put in the global offset table, so | |
666 | both the dynamic object and the regular object will refer to the | |
667 | same memory location for the variable. */ | |
013dec1a ILT |
668 | |
669 | s = bfd_get_section_by_name (dynobj, ".dynbss"); | |
670 | BFD_ASSERT (s != NULL); | |
671 | ||
672 | /* If the symbol is currently defined in the .bss section of the | |
673 | dynamic object, then it is OK to simply initialize it to zero. | |
674 | If the symbol is in some other section, we must generate a | |
675 | R_SPARC_COPY reloc to tell the dynamic linker to copy the initial | |
676 | value out of the dynamic object and into the runtime process | |
677 | image. We need to remember the offset into the .rel.bss section | |
7c6da9ca | 678 | we are going to use. */ |
4fbc96ad | 679 | if ((h->root.u.def.section->flags & SEC_LOAD) != 0) |
013dec1a ILT |
680 | { |
681 | asection *srel; | |
682 | ||
683 | srel = bfd_get_section_by_name (dynobj, ".rela.bss"); | |
684 | BFD_ASSERT (srel != NULL); | |
013dec1a | 685 | srel->_raw_size += sizeof (Elf32_External_Rela); |
4fbc96ad | 686 | h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY; |
013dec1a ILT |
687 | } |
688 | ||
689 | /* We need to figure out the alignment required for this symbol. I | |
690 | have no idea how ELF linkers handle this. */ | |
7c6da9ca ILT |
691 | power_of_two = bfd_log2 (h->size); |
692 | if (power_of_two > 3) | |
693 | power_of_two = 3; | |
013dec1a ILT |
694 | |
695 | /* Apply the required alignment. */ | |
7c6da9ca ILT |
696 | s->_raw_size = BFD_ALIGN (s->_raw_size, |
697 | (bfd_size_type) (1 << power_of_two)); | |
013dec1a ILT |
698 | if (power_of_two > bfd_get_section_alignment (dynobj, s)) |
699 | { | |
700 | if (! bfd_set_section_alignment (dynobj, s, power_of_two)) | |
701 | return false; | |
702 | } | |
703 | ||
704 | /* Define the symbol as being at this point in the section. */ | |
705 | h->root.u.def.section = s; | |
706 | h->root.u.def.value = s->_raw_size; | |
707 | ||
708 | /* Increment the section size to make room for the symbol. */ | |
709 | s->_raw_size += h->size; | |
710 | ||
711 | return true; | |
712 | } | |
713 | ||
013dec1a ILT |
714 | /* Set the sizes of the dynamic sections. */ |
715 | ||
716 | static boolean | |
717 | elf32_sparc_size_dynamic_sections (output_bfd, info) | |
718 | bfd *output_bfd; | |
719 | struct bfd_link_info *info; | |
720 | { | |
721 | bfd *dynobj; | |
722 | asection *s; | |
4fbc96ad | 723 | boolean reltext; |
013dec1a ILT |
724 | |
725 | dynobj = elf_hash_table (info)->dynobj; | |
726 | BFD_ASSERT (dynobj != NULL); | |
727 | ||
12662be4 | 728 | if (elf_hash_table (info)->dynamic_sections_created) |
8af74670 | 729 | { |
12662be4 ILT |
730 | /* Set the contents of the .interp section to the interpreter. */ |
731 | if (! info->shared) | |
732 | { | |
733 | s = bfd_get_section_by_name (dynobj, ".interp"); | |
734 | BFD_ASSERT (s != NULL); | |
735 | s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER; | |
736 | s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER; | |
737 | } | |
738 | ||
739 | /* Make space for the trailing nop in .plt. */ | |
740 | s = bfd_get_section_by_name (dynobj, ".plt"); | |
8af74670 | 741 | BFD_ASSERT (s != NULL); |
12662be4 ILT |
742 | if (s->_raw_size > 0) |
743 | s->_raw_size += 4; | |
744 | } | |
745 | else | |
746 | { | |
747 | /* We may have created entries in the .rela.got section. | |
748 | However, if we are not creating the dynamic sections, we will | |
749 | not actually use these entries. Reset the size of .rela.got, | |
750 | which will cause it to get stripped from the output file | |
751 | below. */ | |
752 | s = bfd_get_section_by_name (dynobj, ".rela.got"); | |
753 | if (s != NULL) | |
754 | s->_raw_size = 0; | |
8af74670 | 755 | } |
013dec1a | 756 | |
4fbc96ad KR |
757 | /* The check_relocs and adjust_dynamic_symbol entry points have |
758 | determined the sizes of the various dynamic sections. Allocate | |
759 | memory for them. */ | |
760 | reltext = false; | |
761 | for (s = dynobj->sections; s != NULL; s = s->next) | |
762 | { | |
763 | const char *name; | |
12662be4 | 764 | boolean strip; |
4fbc96ad KR |
765 | |
766 | if ((s->flags & SEC_IN_MEMORY) == 0) | |
767 | continue; | |
768 | ||
769 | /* It's OK to base decisions on the section name, because none | |
770 | of the dynobj section names depend upon the input files. */ | |
771 | name = bfd_get_section_name (dynobj, s); | |
772 | ||
12662be4 ILT |
773 | strip = false; |
774 | ||
775 | if (strncmp (name, ".rela", 5) == 0) | |
4fbc96ad | 776 | { |
12662be4 ILT |
777 | if (s->_raw_size == 0) |
778 | { | |
779 | /* If we don't need this section, strip it from the | |
780 | output file. This is to handle .rela.bss and | |
781 | .rel.plt. We must create it in | |
782 | create_dynamic_sections, because it must be created | |
783 | before the linker maps input sections to output | |
784 | sections. The linker does that before | |
785 | adjust_dynamic_symbol is called, and it is that | |
786 | function which decides whether anything needs to go | |
787 | into these sections. */ | |
788 | strip = true; | |
789 | } | |
790 | else | |
791 | { | |
792 | asection *target; | |
793 | ||
794 | /* If this relocation section applies to a read only | |
795 | section, then we probably need a DT_TEXTREL entry. */ | |
796 | target = bfd_get_section_by_name (output_bfd, name + 5); | |
797 | if (target != NULL | |
798 | && (target->flags & SEC_READONLY) != 0) | |
799 | reltext = true; | |
800 | ||
801 | /* We use the reloc_count field as a counter if we need | |
802 | to copy relocs into the output file. */ | |
803 | s->reloc_count = 0; | |
804 | } | |
4fbc96ad KR |
805 | } |
806 | else if (strcmp (name, ".plt") != 0 | |
807 | && strcmp (name, ".got") != 0) | |
808 | { | |
809 | /* It's not one of our sections, so don't allocate space. */ | |
810 | continue; | |
811 | } | |
12662be4 ILT |
812 | |
813 | if (strip) | |
814 | { | |
815 | asection **spp; | |
816 | ||
817 | for (spp = &s->output_section->owner->sections; | |
818 | *spp != s->output_section; | |
819 | spp = &(*spp)->next) | |
820 | ; | |
821 | *spp = s->output_section->next; | |
822 | --s->output_section->owner->section_count; | |
823 | ||
824 | continue; | |
825 | } | |
826 | ||
4fbc96ad KR |
827 | /* Allocate memory for the section contents. */ |
828 | s->contents = (bfd_byte *) bfd_alloc (dynobj, s->_raw_size); | |
829 | if (s->contents == NULL && s->_raw_size != 0) | |
830 | { | |
831 | bfd_set_error (bfd_error_no_memory); | |
832 | return false; | |
833 | } | |
834 | } | |
013dec1a | 835 | |
12662be4 | 836 | if (elf_hash_table (info)->dynamic_sections_created) |
4fbc96ad | 837 | { |
12662be4 ILT |
838 | /* Add some entries to the .dynamic section. We fill in the |
839 | values later, in elf32_sparc_finish_dynamic_sections, but we | |
840 | must add the entries now so that we get the correct size for | |
841 | the .dynamic section. The DT_DEBUG entry is filled in by the | |
842 | dynamic linker and used by the debugger. */ | |
843 | if (! info->shared) | |
844 | { | |
845 | if (! bfd_elf32_add_dynamic_entry (info, DT_DEBUG, 0)) | |
846 | return false; | |
847 | } | |
013dec1a | 848 | |
12662be4 ILT |
849 | if (! bfd_elf32_add_dynamic_entry (info, DT_PLTGOT, 0) |
850 | || ! bfd_elf32_add_dynamic_entry (info, DT_PLTRELSZ, 0) | |
851 | || ! bfd_elf32_add_dynamic_entry (info, DT_PLTREL, DT_RELA) | |
852 | || ! bfd_elf32_add_dynamic_entry (info, DT_JMPREL, 0) | |
853 | || ! bfd_elf32_add_dynamic_entry (info, DT_RELA, 0) | |
854 | || ! bfd_elf32_add_dynamic_entry (info, DT_RELASZ, 0) | |
855 | || ! bfd_elf32_add_dynamic_entry (info, DT_RELAENT, | |
856 | sizeof (Elf32_External_Rela))) | |
4fbc96ad | 857 | return false; |
12662be4 ILT |
858 | |
859 | if (reltext) | |
860 | { | |
861 | if (! bfd_elf32_add_dynamic_entry (info, DT_TEXTREL, 0)) | |
862 | return false; | |
863 | } | |
4fbc96ad KR |
864 | } |
865 | ||
013dec1a ILT |
866 | return true; |
867 | } | |
868 | ||
869 | /* Relocate a SPARC ELF section. */ | |
870 | ||
871 | static boolean | |
872 | elf32_sparc_relocate_section (output_bfd, info, input_bfd, input_section, | |
12662be4 | 873 | contents, relocs, local_syms, local_sections) |
013dec1a ILT |
874 | bfd *output_bfd; |
875 | struct bfd_link_info *info; | |
876 | bfd *input_bfd; | |
877 | asection *input_section; | |
878 | bfd_byte *contents; | |
879 | Elf_Internal_Rela *relocs; | |
880 | Elf_Internal_Sym *local_syms; | |
881 | asection **local_sections; | |
882 | { | |
4fbc96ad | 883 | bfd *dynobj; |
013dec1a ILT |
884 | Elf_Internal_Shdr *symtab_hdr; |
885 | struct elf_link_hash_entry **sym_hashes; | |
4fbc96ad KR |
886 | bfd_vma *local_got_offsets; |
887 | asection *sgot; | |
888 | asection *splt; | |
889 | asection *sreloc; | |
013dec1a ILT |
890 | Elf_Internal_Rela *rel; |
891 | Elf_Internal_Rela *relend; | |
892 | ||
4fbc96ad | 893 | dynobj = elf_hash_table (info)->dynobj; |
013dec1a ILT |
894 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; |
895 | sym_hashes = elf_sym_hashes (input_bfd); | |
4fbc96ad KR |
896 | local_got_offsets = elf_local_got_offsets (input_bfd); |
897 | ||
898 | sgot = NULL; | |
899 | splt = NULL; | |
900 | sreloc = NULL; | |
013dec1a ILT |
901 | |
902 | rel = relocs; | |
903 | relend = relocs + input_section->reloc_count; | |
904 | for (; rel < relend; rel++) | |
905 | { | |
906 | int r_type; | |
907 | const reloc_howto_type *howto; | |
908 | long r_symndx; | |
909 | struct elf_link_hash_entry *h; | |
910 | Elf_Internal_Sym *sym; | |
911 | asection *sec; | |
912 | bfd_vma relocation; | |
913 | bfd_reloc_status_type r; | |
914 | ||
915 | r_type = ELF32_R_TYPE (rel->r_info); | |
916 | if (r_type < 0 || r_type >= (int) R_SPARC_max) | |
917 | { | |
918 | bfd_set_error (bfd_error_bad_value); | |
919 | return false; | |
920 | } | |
921 | howto = elf_sparc_howto_table + r_type; | |
922 | ||
923 | r_symndx = ELF32_R_SYM (rel->r_info); | |
924 | ||
925 | if (info->relocateable) | |
926 | { | |
927 | /* This is a relocateable link. We don't have to change | |
928 | anything, unless the reloc is against a section symbol, | |
929 | in which case we have to adjust according to where the | |
930 | section symbol winds up in the output section. */ | |
931 | if (r_symndx < symtab_hdr->sh_info) | |
932 | { | |
933 | sym = local_syms + r_symndx; | |
934 | if (ELF_ST_TYPE (sym->st_info) == STT_SECTION) | |
935 | { | |
936 | sec = local_sections[r_symndx]; | |
937 | rel->r_addend += sec->output_offset + sym->st_value; | |
938 | } | |
939 | } | |
940 | ||
941 | continue; | |
942 | } | |
943 | ||
944 | /* This is a final link. */ | |
945 | h = NULL; | |
946 | sym = NULL; | |
947 | sec = NULL; | |
948 | if (r_symndx < symtab_hdr->sh_info) | |
949 | { | |
950 | sym = local_syms + r_symndx; | |
951 | sec = local_sections[r_symndx]; | |
952 | relocation = (sec->output_section->vma | |
953 | + sec->output_offset | |
954 | + sym->st_value); | |
955 | } | |
956 | else | |
957 | { | |
4fbc96ad | 958 | h = sym_hashes[r_symndx - symtab_hdr->sh_info]; |
013dec1a ILT |
959 | if (h->root.type == bfd_link_hash_defined) |
960 | { | |
961 | sec = h->root.u.def.section; | |
962 | relocation = (h->root.u.def.value | |
963 | + sec->output_section->vma | |
964 | + sec->output_offset); | |
965 | } | |
966 | else if (h->root.type == bfd_link_hash_weak) | |
967 | relocation = 0; | |
4fbc96ad KR |
968 | else if (info->shared) |
969 | relocation = 0; | |
013dec1a ILT |
970 | else |
971 | { | |
972 | if (! ((*info->callbacks->undefined_symbol) | |
973 | (info, h->root.root.string, input_bfd, | |
974 | input_section, rel->r_offset))) | |
975 | return false; | |
976 | relocation = 0; | |
977 | } | |
978 | } | |
979 | ||
4fbc96ad KR |
980 | switch (r_type) |
981 | { | |
982 | case R_SPARC_GOT10: | |
983 | case R_SPARC_GOT13: | |
984 | case R_SPARC_GOT22: | |
985 | /* Relocation is to the entry for this symbol in the global | |
986 | offset table. */ | |
987 | if (sgot == NULL) | |
988 | { | |
989 | sgot = bfd_get_section_by_name (dynobj, ".got"); | |
990 | BFD_ASSERT (sgot != NULL); | |
991 | } | |
992 | ||
993 | if (h != NULL) | |
994 | { | |
12662be4 ILT |
995 | bfd_vma off; |
996 | ||
997 | off = h->got_offset; | |
998 | BFD_ASSERT (off != (bfd_vma) -1); | |
999 | ||
1000 | if (! elf_hash_table (info)->dynamic_sections_created) | |
1001 | { | |
1002 | /* This is actually a static link. We must | |
1003 | initialize this entry in the global offset table. | |
1004 | Since the offset must always be a multiple of 4, | |
1005 | we use the least significant bit to record | |
1006 | whether we have initialized it already. | |
1007 | ||
1008 | When doing a dynamic link, we create a .rela.got | |
1009 | relocation entry to initialize the value. This | |
1010 | is done in the finish_dynamic_symbol routine. */ | |
1011 | if ((off & 1) != 0) | |
1012 | off &= ~1; | |
1013 | else | |
1014 | { | |
1015 | bfd_put_32 (output_bfd, relocation, | |
1016 | sgot->contents + off); | |
1017 | h->got_offset |= 1; | |
1018 | } | |
1019 | } | |
1020 | ||
1021 | relocation = sgot->output_offset + off; | |
4fbc96ad KR |
1022 | } |
1023 | else | |
1024 | { | |
1025 | bfd_vma off; | |
1026 | ||
1027 | BFD_ASSERT (local_got_offsets != NULL | |
1028 | && local_got_offsets[r_symndx] != (bfd_vma) -1); | |
1029 | ||
1030 | off = local_got_offsets[r_symndx]; | |
1031 | ||
1032 | /* The offset must always be a multiple of 4. We use | |
12662be4 ILT |
1033 | the least significant bit to record whether we have |
1034 | already processed this entry. */ | |
4fbc96ad KR |
1035 | if ((off & 1) != 0) |
1036 | off &= ~1; | |
1037 | else | |
1038 | { | |
4fbc96ad KR |
1039 | bfd_put_32 (output_bfd, relocation, sgot->contents + off); |
1040 | ||
12662be4 ILT |
1041 | if (info->shared) |
1042 | { | |
1043 | asection *srelgot; | |
1044 | Elf_Internal_Rela outrel; | |
1045 | ||
1046 | /* We need to generate a R_SPARC_RELATIVE reloc | |
1047 | for the dynamic linker. */ | |
1048 | srelgot = bfd_get_section_by_name (dynobj, ".rela.got"); | |
1049 | BFD_ASSERT (srelgot != NULL); | |
1050 | ||
1051 | outrel.r_offset = (sgot->output_section->vma | |
1052 | + sgot->output_offset | |
1053 | + off); | |
1054 | outrel.r_info = ELF32_R_INFO (0, R_SPARC_RELATIVE); | |
1055 | outrel.r_addend = 0; | |
1056 | bfd_elf32_swap_reloca_out (output_bfd, &outrel, | |
1057 | (((Elf32_External_Rela *) | |
1058 | srelgot->contents) | |
1059 | + srelgot->reloc_count)); | |
1060 | ++srelgot->reloc_count; | |
1061 | } | |
4fbc96ad KR |
1062 | |
1063 | local_got_offsets[r_symndx] |= 1; | |
1064 | } | |
1065 | ||
1066 | relocation = sgot->output_offset + off; | |
1067 | } | |
1068 | ||
1069 | break; | |
1070 | ||
1071 | case R_SPARC_WPLT30: | |
1072 | /* Relocation is to the entry for this symbol in the | |
1073 | procedure linkage table. */ | |
12662be4 ILT |
1074 | BFD_ASSERT (h != NULL); |
1075 | ||
1076 | if (h->plt_offset == (bfd_vma) -1) | |
1077 | { | |
1078 | /* We didn't make a PLT entry for this symbol. This | |
1079 | happens when statically linking PIC code. */ | |
1080 | break; | |
1081 | } | |
1082 | ||
4fbc96ad KR |
1083 | if (splt == NULL) |
1084 | { | |
1085 | splt = bfd_get_section_by_name (dynobj, ".plt"); | |
1086 | BFD_ASSERT (splt != NULL); | |
1087 | } | |
1088 | ||
4fbc96ad KR |
1089 | relocation = (splt->output_section->vma |
1090 | + splt->output_offset | |
1091 | + h->plt_offset); | |
1092 | break; | |
1093 | ||
1094 | case R_SPARC_PC10: | |
1095 | case R_SPARC_PC22: | |
1096 | if (h != NULL | |
1097 | && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0) | |
1098 | break; | |
1099 | /* Fall through. */ | |
1100 | case R_SPARC_8: | |
1101 | case R_SPARC_16: | |
1102 | case R_SPARC_32: | |
1103 | case R_SPARC_DISP8: | |
1104 | case R_SPARC_DISP16: | |
1105 | case R_SPARC_DISP32: | |
1106 | case R_SPARC_WDISP30: | |
1107 | case R_SPARC_WDISP22: | |
1108 | case R_SPARC_HI22: | |
1109 | case R_SPARC_22: | |
1110 | case R_SPARC_13: | |
1111 | case R_SPARC_LO10: | |
1112 | case R_SPARC_UA32: | |
1113 | if (info->shared | |
1114 | && (input_section->flags & SEC_ALLOC) != 0) | |
1115 | { | |
1116 | Elf_Internal_Rela outrel; | |
1117 | ||
1118 | /* When generating a shared object, these relocations | |
1119 | are copied into the output file to be resolved at run | |
1120 | time. */ | |
1121 | ||
1122 | if (sreloc == NULL) | |
1123 | { | |
1124 | const char *name; | |
1125 | ||
1126 | name = (elf_string_from_elf_section | |
1127 | (input_bfd, | |
1128 | elf_elfheader (input_bfd)->e_shstrndx, | |
1129 | elf_section_data (input_section)->rel_hdr.sh_name)); | |
1130 | if (name == NULL) | |
1131 | return false; | |
1132 | ||
1133 | BFD_ASSERT (strncmp (name, ".rela", 5) == 0 | |
1134 | && strcmp (bfd_get_section_name (input_bfd, | |
1135 | input_section), | |
1136 | name + 5) == 0); | |
1137 | ||
1138 | sreloc = bfd_get_section_by_name (dynobj, name); | |
1139 | BFD_ASSERT (sreloc != NULL); | |
1140 | } | |
1141 | ||
1142 | outrel.r_offset = (rel->r_offset | |
1143 | + input_section->output_section->vma | |
1144 | + input_section->output_offset); | |
1145 | if (h != NULL) | |
1146 | { | |
12662be4 | 1147 | BFD_ASSERT (h->dynindx != -1); |
4fbc96ad KR |
1148 | outrel.r_info = ELF32_R_INFO (h->dynindx, r_type); |
1149 | outrel.r_addend = 0; | |
1150 | } | |
1151 | else | |
1152 | { | |
1153 | long indx; | |
1154 | ||
1155 | sym = local_syms + r_symndx; | |
1156 | ||
1157 | /* If this isn't a section symbol, we need to map it | |
1158 | to something that is going to be put into the | |
1159 | dynamic symbols. The case will probably never | |
1160 | arise. */ | |
1161 | BFD_ASSERT (ELF_ST_TYPE (sym->st_info) == STT_SECTION); | |
1162 | ||
1163 | sec = local_sections[r_symndx]; | |
1164 | if (sec != NULL && bfd_is_abs_section (sec)) | |
1165 | indx = 0; | |
1166 | else if (sec == NULL || sec->owner == NULL) | |
1167 | { | |
1168 | bfd_set_error (bfd_error_bad_value); | |
1169 | return false; | |
1170 | } | |
1171 | else | |
1172 | { | |
1173 | indx = sec->output_section->target_index; | |
1174 | if (indx == 0) | |
1175 | abort (); | |
1176 | } | |
1177 | ||
1178 | outrel.r_info = ELF32_R_INFO (indx, r_type); | |
1179 | outrel.r_addend = sec->output_offset + sym->st_value; | |
1180 | } | |
1181 | ||
1182 | bfd_elf32_swap_reloca_out (output_bfd, &outrel, | |
1183 | (((Elf32_External_Rela *) | |
1184 | sreloc->contents) | |
1185 | + sreloc->reloc_count)); | |
1186 | ++sreloc->reloc_count; | |
1187 | ||
1188 | /* This reloc will be computed at runtime, so there's no | |
1189 | need to do anything now. */ | |
1190 | continue; | |
1191 | } | |
1192 | ||
1193 | default: | |
1194 | break; | |
1195 | } | |
1196 | ||
013dec1a ILT |
1197 | r = _bfd_final_link_relocate (howto, input_bfd, input_section, |
1198 | contents, rel->r_offset, | |
1199 | relocation, rel->r_addend); | |
1200 | ||
1201 | if (r != bfd_reloc_ok) | |
1202 | { | |
1203 | switch (r) | |
1204 | { | |
1205 | default: | |
1206 | case bfd_reloc_outofrange: | |
1207 | abort (); | |
1208 | case bfd_reloc_overflow: | |
1209 | { | |
1210 | const char *name; | |
1211 | ||
1212 | if (h != NULL) | |
1213 | name = h->root.root.string; | |
1214 | else | |
1215 | { | |
12662be4 ILT |
1216 | name = elf_string_from_elf_section (input_bfd, |
1217 | symtab_hdr->sh_link, | |
1218 | sym->st_name); | |
013dec1a ILT |
1219 | if (name == NULL) |
1220 | return false; | |
1221 | if (*name == '\0') | |
1222 | name = bfd_section_name (input_bfd, sec); | |
1223 | } | |
1224 | if (! ((*info->callbacks->reloc_overflow) | |
1225 | (info, name, howto->name, (bfd_vma) 0, | |
1226 | input_bfd, input_section, rel->r_offset))) | |
1227 | return false; | |
1228 | } | |
1229 | break; | |
1230 | } | |
1231 | } | |
1232 | } | |
1233 | ||
1234 | return true; | |
1235 | } | |
1236 | ||
1237 | /* Finish up dynamic symbol handling. We set the contents of various | |
1238 | dynamic sections here. */ | |
1239 | ||
1240 | static boolean | |
1241 | elf32_sparc_finish_dynamic_symbol (output_bfd, info, h, sym) | |
1242 | bfd *output_bfd; | |
1243 | struct bfd_link_info *info; | |
1244 | struct elf_link_hash_entry *h; | |
1245 | Elf_Internal_Sym *sym; | |
1246 | { | |
4fbc96ad | 1247 | bfd *dynobj; |
013dec1a | 1248 | |
4fbc96ad | 1249 | dynobj = elf_hash_table (info)->dynobj; |
013dec1a | 1250 | |
4fbc96ad | 1251 | if (h->plt_offset != (bfd_vma) -1) |
013dec1a ILT |
1252 | { |
1253 | asection *splt; | |
1254 | asection *srela; | |
1255 | Elf_Internal_Rela rela; | |
1256 | ||
4fbc96ad KR |
1257 | /* This symbol has an entry in the procedure linkage table. Set |
1258 | it up. */ | |
1259 | ||
1260 | BFD_ASSERT (h->dynindx != -1); | |
1261 | ||
1262 | splt = bfd_get_section_by_name (dynobj, ".plt"); | |
1263 | srela = bfd_get_section_by_name (dynobj, ".rela.plt"); | |
1264 | BFD_ASSERT (splt != NULL && srela != NULL); | |
013dec1a ILT |
1265 | |
1266 | /* Fill in the entry in the procedure linkage table. */ | |
1267 | bfd_put_32 (output_bfd, | |
4fbc96ad KR |
1268 | PLT_ENTRY_WORD0 + h->plt_offset, |
1269 | splt->contents + h->plt_offset); | |
013dec1a ILT |
1270 | bfd_put_32 (output_bfd, |
1271 | (PLT_ENTRY_WORD1 | |
4fbc96ad KR |
1272 | + (((- (h->plt_offset + 4)) >> 2) & 0x3fffff)), |
1273 | splt->contents + h->plt_offset + 4); | |
013dec1a | 1274 | bfd_put_32 (output_bfd, PLT_ENTRY_WORD2, |
4fbc96ad | 1275 | splt->contents + h->plt_offset + 8); |
013dec1a ILT |
1276 | |
1277 | /* Fill in the entry in the .rela.plt section. */ | |
1278 | rela.r_offset = (splt->output_section->vma | |
1279 | + splt->output_offset | |
4fbc96ad | 1280 | + h->plt_offset); |
013dec1a ILT |
1281 | rela.r_info = ELF32_R_INFO (h->dynindx, R_SPARC_JMP_SLOT); |
1282 | rela.r_addend = 0; | |
1283 | bfd_elf32_swap_reloca_out (output_bfd, &rela, | |
1284 | ((Elf32_External_Rela *) srela->contents | |
4fbc96ad KR |
1285 | + h->plt_offset / PLT_ENTRY_SIZE - 4)); |
1286 | ||
1287 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0) | |
1288 | { | |
1289 | /* Mark the symbol as undefined, rather than as defined in | |
1290 | the .plt section. Leave the value alone. */ | |
1291 | sym->st_shndx = SHN_UNDEF; | |
1292 | } | |
1293 | } | |
1294 | ||
1295 | if (h->got_offset != (bfd_vma) -1) | |
1296 | { | |
1297 | asection *sgot; | |
1298 | asection *srela; | |
1299 | Elf_Internal_Rela rela; | |
1300 | ||
1301 | /* This symbol has an entry in the global offset table. Set it | |
1302 | up. */ | |
013dec1a | 1303 | |
4fbc96ad KR |
1304 | BFD_ASSERT (h->dynindx != -1); |
1305 | ||
1306 | sgot = bfd_get_section_by_name (dynobj, ".got"); | |
1307 | srela = bfd_get_section_by_name (dynobj, ".rela.got"); | |
1308 | BFD_ASSERT (sgot != NULL && srela != NULL); | |
1309 | ||
1310 | bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got_offset); | |
1311 | ||
1312 | rela.r_offset = (sgot->output_section->vma | |
1313 | + sgot->output_offset | |
1314 | + h->got_offset); | |
1315 | rela.r_info = ELF32_R_INFO (h->dynindx, R_SPARC_GLOB_DAT); | |
1316 | rela.r_addend = 0; | |
1317 | bfd_elf32_swap_reloca_out (output_bfd, &rela, | |
1318 | ((Elf32_External_Rela *) srela->contents | |
1319 | + srela->reloc_count)); | |
1320 | ++srela->reloc_count; | |
013dec1a | 1321 | } |
4fbc96ad KR |
1322 | |
1323 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0) | |
013dec1a | 1324 | { |
4fbc96ad KR |
1325 | asection *s; |
1326 | Elf_Internal_Rela rela; | |
013dec1a | 1327 | |
4fbc96ad | 1328 | /* This symbols needs a copy reloc. Set it up. */ |
013dec1a | 1329 | |
4fbc96ad KR |
1330 | BFD_ASSERT (h->dynindx != -1); |
1331 | ||
1332 | s = bfd_get_section_by_name (h->root.u.def.section->owner, | |
1333 | ".rela.bss"); | |
1334 | BFD_ASSERT (s != NULL); | |
1335 | ||
1336 | rela.r_offset = (h->root.u.def.value | |
1337 | + h->root.u.def.section->output_section->vma | |
1338 | + h->root.u.def.section->output_offset); | |
1339 | rela.r_info = ELF32_R_INFO (h->dynindx, R_SPARC_COPY); | |
1340 | rela.r_addend = 0; | |
1341 | bfd_elf32_swap_reloca_out (output_bfd, &rela, | |
1342 | ((Elf32_External_Rela *) s->contents | |
1343 | + s->reloc_count)); | |
1344 | ++s->reloc_count; | |
013dec1a ILT |
1345 | } |
1346 | ||
4fbc96ad KR |
1347 | /* Mark some specially defined symbols as absolute. */ |
1348 | if (strcmp (h->root.root.string, "_DYNAMIC") == 0 | |
1349 | || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0 | |
1350 | || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0) | |
1351 | sym->st_shndx = SHN_ABS; | |
1352 | ||
013dec1a ILT |
1353 | return true; |
1354 | } | |
1355 | ||
1356 | /* Finish up the dynamic sections. */ | |
1357 | ||
1358 | static boolean | |
1359 | elf32_sparc_finish_dynamic_sections (output_bfd, info) | |
1360 | bfd *output_bfd; | |
1361 | struct bfd_link_info *info; | |
1362 | { | |
4fbc96ad | 1363 | bfd *dynobj; |
013dec1a | 1364 | asection *sdyn; |
12662be4 | 1365 | asection *sgot; |
013dec1a | 1366 | |
4fbc96ad KR |
1367 | dynobj = elf_hash_table (info)->dynobj; |
1368 | ||
4fbc96ad | 1369 | sdyn = bfd_get_section_by_name (dynobj, ".dynamic"); |
013dec1a | 1370 | |
12662be4 | 1371 | if (elf_hash_table (info)->dynamic_sections_created) |
013dec1a | 1372 | { |
12662be4 ILT |
1373 | asection *splt; |
1374 | Elf32_External_Dyn *dyncon, *dynconend; | |
013dec1a | 1375 | |
12662be4 ILT |
1376 | splt = bfd_get_section_by_name (dynobj, ".plt"); |
1377 | BFD_ASSERT (splt != NULL && sdyn != NULL); | |
013dec1a | 1378 | |
12662be4 ILT |
1379 | dyncon = (Elf32_External_Dyn *) sdyn->contents; |
1380 | dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size); | |
1381 | for (; dyncon < dynconend; dyncon++) | |
013dec1a | 1382 | { |
12662be4 ILT |
1383 | Elf_Internal_Dyn dyn; |
1384 | const char *name; | |
1385 | boolean size; | |
013dec1a | 1386 | |
12662be4 | 1387 | bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn); |
013dec1a | 1388 | |
12662be4 ILT |
1389 | switch (dyn.d_tag) |
1390 | { | |
1391 | case DT_PLTGOT: name = ".plt"; size = false; break; | |
1392 | case DT_PLTRELSZ: name = ".rela.plt"; size = true; break; | |
1393 | case DT_JMPREL: name = ".rela.plt"; size = false; break; | |
1394 | default: name = NULL; size = false; break; | |
1395 | } | |
1396 | ||
1397 | if (name != NULL) | |
013dec1a | 1398 | { |
12662be4 ILT |
1399 | asection *s; |
1400 | ||
1401 | s = bfd_get_section_by_name (output_bfd, name); | |
1402 | BFD_ASSERT (s != NULL); | |
1403 | if (! size) | |
1404 | dyn.d_un.d_ptr = s->vma; | |
013dec1a | 1405 | else |
12662be4 ILT |
1406 | { |
1407 | if (s->_cooked_size != 0) | |
1408 | dyn.d_un.d_val = s->_cooked_size; | |
1409 | else | |
1410 | dyn.d_un.d_val = s->_raw_size; | |
1411 | } | |
1412 | bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon); | |
013dec1a | 1413 | } |
013dec1a | 1414 | } |
013dec1a | 1415 | |
12662be4 ILT |
1416 | /* Clear the first four entries in the procedure linkage table, |
1417 | and put a nop in the last four bytes. */ | |
1418 | if (splt->_raw_size > 0) | |
1419 | { | |
1420 | memset (splt->contents, 0, 4 * PLT_ENTRY_SIZE); | |
1421 | bfd_put_32 (output_bfd, SPARC_NOP, | |
1422 | splt->contents + splt->_raw_size - 4); | |
1423 | } | |
1424 | ||
1425 | elf_section_data (splt->output_section)->this_hdr.sh_entsize = | |
1426 | PLT_ENTRY_SIZE; | |
013dec1a ILT |
1427 | } |
1428 | ||
1429 | /* Set the first entry in the global offset table to the address of | |
1430 | the dynamic section. */ | |
12662be4 ILT |
1431 | sgot = bfd_get_section_by_name (dynobj, ".got"); |
1432 | BFD_ASSERT (sgot != NULL); | |
013dec1a | 1433 | if (sgot->_raw_size > 0) |
12662be4 ILT |
1434 | { |
1435 | if (sdyn == NULL) | |
1436 | bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents); | |
1437 | else | |
1438 | bfd_put_32 (output_bfd, | |
1439 | sdyn->output_section->vma + sdyn->output_offset, | |
1440 | sgot->contents); | |
1441 | } | |
013dec1a ILT |
1442 | |
1443 | elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4; | |
013dec1a ILT |
1444 | |
1445 | return true; | |
1446 | } | |
1447 | ||
32090b8e KR |
1448 | #define TARGET_BIG_SYM bfd_elf32_sparc_vec |
1449 | #define TARGET_BIG_NAME "elf32-sparc" | |
1450 | #define ELF_ARCH bfd_arch_sparc | |
013dec1a ILT |
1451 | #define ELF_MACHINE_CODE EM_SPARC |
1452 | #define ELF_MAXPAGESIZE 0x10000 | |
1453 | #define elf_backend_create_dynamic_sections \ | |
1454 | elf32_sparc_create_dynamic_sections | |
4fbc96ad | 1455 | #define elf_backend_check_relocs elf32_sparc_check_relocs |
013dec1a ILT |
1456 | #define elf_backend_adjust_dynamic_symbol \ |
1457 | elf32_sparc_adjust_dynamic_symbol | |
1458 | #define elf_backend_size_dynamic_sections \ | |
1459 | elf32_sparc_size_dynamic_sections | |
1460 | #define elf_backend_relocate_section elf32_sparc_relocate_section | |
1461 | #define elf_backend_finish_dynamic_symbol \ | |
1462 | elf32_sparc_finish_dynamic_symbol | |
1463 | #define elf_backend_finish_dynamic_sections \ | |
1464 | elf32_sparc_finish_dynamic_sections | |
32090b8e KR |
1465 | |
1466 | #include "elf32-target.h" |