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