1 /* SPARC-specific support for 32-bit ELF
2 Copyright 1993 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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. */
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 *));
32 static boolean elf32_sparc_create_got_section
33 PARAMS ((bfd *, struct bfd_link_info *));
34 static boolean elf32_sparc_check_relocs
35 PARAMS ((bfd *, struct bfd_link_info *, asection *,
36 const Elf_Internal_Rela *));
37 static boolean elf32_sparc_adjust_dynamic_symbol
38 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
39 static boolean elf32_sparc_adjust_dynindx
40 PARAMS ((struct elf_link_hash_entry *, PTR));
41 static boolean elf32_sparc_size_dynamic_sections
42 PARAMS ((bfd *, struct bfd_link_info *));
43 static boolean elf32_sparc_relocate_section
44 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
45 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
46 static boolean elf32_sparc_finish_dynamic_symbol
47 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
49 static boolean elf32_sparc_finish_dynamic_sections
50 PARAMS ((bfd *, struct bfd_link_info *));
55 R_SPARC_8, R_SPARC_16, R_SPARC_32,
56 R_SPARC_DISP8, R_SPARC_DISP16, R_SPARC_DISP32,
57 R_SPARC_WDISP30, R_SPARC_WDISP22,
58 R_SPARC_HI22, R_SPARC_22,
59 R_SPARC_13, R_SPARC_LO10,
60 R_SPARC_GOT10, R_SPARC_GOT13, R_SPARC_GOT22,
61 R_SPARC_PC10, R_SPARC_PC22,
64 R_SPARC_GLOB_DAT, R_SPARC_JMP_SLOT,
71 static CONST char *CONST reloc_type_names[] =
74 "R_SPARC_8", "R_SPARC_16", "R_SPARC_32",
75 "R_SPARC_DISP8", "R_SPARC_DISP16", "R_SPARC_DISP32",
76 "R_SPARC_WDISP30", "R_SPARC_WDISP22",
77 "R_SPARC_HI22", "R_SPARC_22",
78 "R_SPARC_13", "R_SPARC_LO10",
79 "R_SPARC_GOT10", "R_SPARC_GOT13", "R_SPARC_GOT22",
80 "R_SPARC_PC10", "R_SPARC_PC22",
83 "R_SPARC_GLOB_DAT", "R_SPARC_JMP_SLOT",
89 static reloc_howto_type elf_sparc_howto_table[] =
91 HOWTO(R_SPARC_NONE, 0,0, 0,false,0,complain_overflow_dont, bfd_elf_generic_reloc,"R_SPARC_NONE", false,0,0x00000000,true),
92 HOWTO(R_SPARC_8, 0,0, 8,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,"R_SPARC_8", false,0,0x000000ff,true),
93 HOWTO(R_SPARC_16, 0,1,16,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,"R_SPARC_16", false,0,0x0000ffff,true),
94 HOWTO(R_SPARC_32, 0,2,32,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,"R_SPARC_32", false,0,0xffffffff,true),
95 HOWTO(R_SPARC_DISP8, 0,0, 8,true, 0,complain_overflow_signed, bfd_elf_generic_reloc,"R_SPARC_DISP8", false,0,0x000000ff,true),
96 HOWTO(R_SPARC_DISP16, 0,1,16,true, 0,complain_overflow_signed, bfd_elf_generic_reloc,"R_SPARC_DISP16", false,0,0x0000ffff,true),
97 HOWTO(R_SPARC_DISP32, 0,2,32,true, 0,complain_overflow_signed, bfd_elf_generic_reloc,"R_SPARC_DISP32", false,0,0x00ffffff,true),
98 HOWTO(R_SPARC_WDISP30, 2,2,30,true, 0,complain_overflow_signed, bfd_elf_generic_reloc,"R_SPARC_WDISP30", false,0,0x3fffffff,true),
99 HOWTO(R_SPARC_WDISP22, 2,2,22,true, 0,complain_overflow_signed, bfd_elf_generic_reloc,"R_SPARC_WDISP22", false,0,0x003fffff,true),
100 HOWTO(R_SPARC_HI22, 10,2,22,false,0,complain_overflow_dont, bfd_elf_generic_reloc,"R_SPARC_HI22", false,0,0x003fffff,true),
101 HOWTO(R_SPARC_22, 0,2,22,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,"R_SPARC_22", false,0,0x003fffff,true),
102 HOWTO(R_SPARC_13, 0,2,13,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,"R_SPARC_13", false,0,0x00001fff,true),
103 HOWTO(R_SPARC_LO10, 0,2,10,false,0,complain_overflow_dont, bfd_elf_generic_reloc,"R_SPARC_LO10", false,0,0x000003ff,true),
104 HOWTO(R_SPARC_GOT10, 0,2,10,false,0,complain_overflow_dont, bfd_elf_generic_reloc,"R_SPARC_GOT10", false,0,0x000003ff,true),
105 HOWTO(R_SPARC_GOT13, 0,2,13,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,"R_SPARC_GOT13", false,0,0x00001fff,true),
106 HOWTO(R_SPARC_GOT22, 10,2,22,false,0,complain_overflow_dont, bfd_elf_generic_reloc,"R_SPARC_GOT22", false,0,0x003fffff,true),
107 HOWTO(R_SPARC_PC10, 0,2,10,true, 0,complain_overflow_dont, bfd_elf_generic_reloc,"R_SPARC_PC10", false,0,0x000003ff,true),
108 HOWTO(R_SPARC_PC22, 10,2,22,true, 0,complain_overflow_bitfield,bfd_elf_generic_reloc,"R_SPARC_PC22", false,0,0x003fffff,true),
109 HOWTO(R_SPARC_WPLT30, 2,2,30,true, 0,complain_overflow_signed, bfd_elf_generic_reloc,"R_SPARC_WPLT30", false,0,0x3fffffff,true),
110 HOWTO(R_SPARC_COPY, 0,0,00,false,0,complain_overflow_dont, bfd_elf_generic_reloc,"R_SPARC_COPY", false,0,0x00000000,true),
111 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),
112 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),
113 HOWTO(R_SPARC_RELATIVE,0,0,00,false,0,complain_overflow_dont, bfd_elf_generic_reloc,"R_SPARC_RELATIVE",false,0,0x00000000,true),
114 HOWTO(R_SPARC_UA32, 0,0,00,false,0,complain_overflow_dont, bfd_elf_generic_reloc,"R_SPARC_UA32", false,0,0x00000000,true),
117 struct elf_reloc_map {
118 unsigned char bfd_reloc_val;
119 unsigned char elf_reloc_val;
122 static CONST struct elf_reloc_map sparc_reloc_map[] =
124 { BFD_RELOC_NONE, R_SPARC_NONE, },
125 { BFD_RELOC_16, R_SPARC_16, },
126 { BFD_RELOC_8, R_SPARC_8 },
127 { BFD_RELOC_8_PCREL, R_SPARC_DISP8 },
128 { BFD_RELOC_CTOR, R_SPARC_32 }, /* @@ Assumes 32 bits. */
129 { BFD_RELOC_32, R_SPARC_32 },
130 { BFD_RELOC_32_PCREL, R_SPARC_DISP32 },
131 { BFD_RELOC_HI22, R_SPARC_HI22 },
132 { BFD_RELOC_LO10, R_SPARC_LO10, },
133 { BFD_RELOC_32_PCREL_S2, R_SPARC_WDISP30 },
134 { BFD_RELOC_SPARC22, R_SPARC_22 },
135 { BFD_RELOC_SPARC13, R_SPARC_13 },
136 { BFD_RELOC_SPARC_GOT10, R_SPARC_GOT10 },
137 { BFD_RELOC_SPARC_GOT13, R_SPARC_GOT13 },
138 { BFD_RELOC_SPARC_GOT22, R_SPARC_GOT22 },
139 { BFD_RELOC_SPARC_PC10, R_SPARC_PC10 },
140 { BFD_RELOC_SPARC_PC22, R_SPARC_PC22 },
141 { BFD_RELOC_SPARC_WPLT30, R_SPARC_WPLT30 },
142 { BFD_RELOC_SPARC_COPY, R_SPARC_COPY },
143 { BFD_RELOC_SPARC_GLOB_DAT, R_SPARC_GLOB_DAT },
144 { BFD_RELOC_SPARC_JMP_SLOT, R_SPARC_JMP_SLOT },
145 { BFD_RELOC_SPARC_RELATIVE, R_SPARC_RELATIVE },
146 { BFD_RELOC_SPARC_WDISP22, R_SPARC_WDISP22 },
147 /*{ BFD_RELOC_SPARC_UA32, R_SPARC_UA32 }, not used?? */
150 static CONST struct reloc_howto_struct *
151 bfd_elf32_bfd_reloc_type_lookup (abfd, code)
153 bfd_reloc_code_real_type code;
156 for (i = 0; i < sizeof (sparc_reloc_map) / sizeof (struct elf_reloc_map); i++)
158 if (sparc_reloc_map[i].bfd_reloc_val == code)
159 return &elf_sparc_howto_table[(int) sparc_reloc_map[i].elf_reloc_val];
165 elf_info_to_howto (abfd, cache_ptr, dst)
168 Elf_Internal_Rela *dst;
170 BFD_ASSERT (ELF32_R_TYPE(dst->r_info) < (unsigned int) R_SPARC_max);
171 cache_ptr->howto = &elf_sparc_howto_table[ELF32_R_TYPE(dst->r_info)];
175 /* Functions for the SPARC ELF linker. */
177 /* The name of the dynamic interpreter. This is put in the .interp
180 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
182 /* The nop opcode we use. */
184 #define SPARC_NOP 0x01000000
186 /* The size in bytes of an entry in the procedure linkage table. */
188 #define PLT_ENTRY_SIZE 12
190 /* The first four entries in a procedure linkage table are reserved,
191 and the initial contents are unimportant (we zero them out).
192 Subsequent entries look like this. See the SVR4 ABI SPARC
193 supplement to see how this works. */
195 /* sethi %hi(.-.plt0),%g1. We fill in the address later. */
196 #define PLT_ENTRY_WORD0 0x03000000
197 /* b,a .plt0. We fill in the offset later. */
198 #define PLT_ENTRY_WORD1 0x30800000
200 #define PLT_ENTRY_WORD2 SPARC_NOP
202 /* Create dynamic sections when linking against a dynamic object. */
205 elf32_sparc_create_dynamic_sections (abfd, info)
207 struct bfd_link_info *info;
210 register asection *s;
211 struct elf_link_hash_entry *h;
213 /* We need to create .plt, .rela.plt, .got, .dynbss, and .rela.bss
216 flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY;
218 s = bfd_make_section (abfd, ".plt");
220 || ! bfd_set_section_flags (abfd, s, flags | SEC_CODE)
221 || ! bfd_set_section_alignment (abfd, s, 2))
224 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
227 if (! (_bfd_generic_link_add_one_symbol
228 (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s, (bfd_vma) 0,
229 (const char *) NULL, false, get_elf_backend_data (abfd)->collect,
230 (struct bfd_link_hash_entry **) &h)))
232 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
233 h->type = STT_OBJECT;
236 && ! bfd_elf32_link_record_dynamic_symbol (info, h))
239 s = bfd_make_section (abfd, ".rela.plt");
241 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
242 || ! bfd_set_section_alignment (abfd, s, 2))
245 if (! elf32_sparc_create_got_section (abfd, info))
248 /* The .dynbss section is a place to put symbols which are defined
249 by dynamic objects, are referenced by regular objects, and are
250 not functions. We must allocate space for them in the process
251 image and use a R_SPARC_COPY reloc to tell the dynamic linker to
252 initialize them at run time. The linker script puts the .dynbss
253 section into the .bss section of the final image. */
254 s = bfd_make_section (abfd, ".dynbss");
256 || ! bfd_set_section_flags (abfd, s, SEC_ALLOC))
259 /* The .rela.bss section holds copy relocs. */
262 s = bfd_make_section (abfd, ".rela.bss");
264 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
265 || ! bfd_set_section_alignment (abfd, s, 2))
272 /* Create the .got section to hold the global offset table. */
275 elf32_sparc_create_got_section (abfd, info)
277 struct bfd_link_info *info;
279 register asection *s;
280 struct elf_link_hash_entry *h;
282 /* This function may be called more than once. */
283 if (bfd_get_section_by_name (abfd, ".got") != NULL)
286 s = bfd_make_section (abfd, ".got");
288 || ! bfd_set_section_flags (abfd, s,
289 (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
291 || ! bfd_set_section_alignment (abfd, s, 2))
294 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
295 section. We don't do this in the linker script because we don't
296 want to define the symbol if we are not creating a global offset
297 table. FIXME: The Solaris linker puts _GLOBAL_OFFSET_TABLE_ at
298 the start of the .got section, but when using the small PIC model
299 the .got is accessed using a signed 13 bit offset. Shouldn't
300 _GLOBAL_OFFSET_TABLE_ be located at .got + 4096? */
302 if (! (_bfd_generic_link_add_one_symbol
303 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s, (bfd_vma) 0,
304 (const char *) NULL, false, get_elf_backend_data (abfd)->collect,
305 (struct bfd_link_hash_entry **) &h)))
307 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
308 h->type = STT_OBJECT;
311 && ! bfd_elf32_link_record_dynamic_symbol (info, h))
314 /* The first global offset table entry is reserved. */
320 /* Look through the relocs for a section during the first phase, and
321 allocate space in the global offset table or procedure linkage
325 elf32_sparc_check_relocs (abfd, info, sec, relocs)
327 struct bfd_link_info *info;
329 const Elf_Internal_Rela *relocs;
332 Elf_Internal_Shdr *symtab_hdr;
333 struct elf_link_hash_entry **sym_hashes;
334 bfd_vma *local_got_offsets;
335 const Elf_Internal_Rela *rel;
336 const Elf_Internal_Rela *rel_end;
341 if (info->relocateable)
344 dynobj = elf_hash_table (info)->dynobj;
345 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
346 sym_hashes = elf_sym_hashes (abfd);
347 local_got_offsets = elf_local_got_offsets (abfd);
353 rel_end = relocs + sec->reloc_count;
354 for (rel = relocs; rel < rel_end; rel++)
357 struct elf_link_hash_entry *h;
359 r_symndx = ELF32_R_SYM (rel->r_info);
360 if (r_symndx < symtab_hdr->sh_info)
363 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
365 switch (ELF32_R_TYPE (rel->r_info))
370 /* This symbol requires a global offset table entry. */
374 /* Create the .got section. */
375 elf_hash_table (info)->dynobj = dynobj = abfd;
376 if (! elf32_sparc_create_got_section (dynobj, info))
382 sgot = bfd_get_section_by_name (dynobj, ".got");
383 BFD_ASSERT (sgot != NULL);
387 && (h != NULL || info->shared))
389 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
392 srelgot = bfd_make_section (dynobj, ".rela.got");
394 || ! bfd_set_section_flags (dynobj, srelgot,
400 || ! bfd_set_section_alignment (dynobj, srelgot, 2))
407 if (h->got_offset != (bfd_vma) -1)
409 /* We have already allocated space in the .got. */
412 h->got_offset = sgot->_raw_size;
414 /* Make sure this symbol is output as a dynamic symbol. */
415 if (h->dynindx == -1)
417 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
421 srelgot->_raw_size += sizeof (Elf32_External_Rela);
425 /* This is a global offset table entry for a local
427 if (local_got_offsets == NULL)
432 size = symtab_hdr->sh_info * sizeof (bfd_vma);
433 local_got_offsets = (bfd_vma *) bfd_alloc (abfd, size);
434 if (local_got_offsets == NULL)
436 bfd_set_error (bfd_error_no_memory);
439 elf_local_got_offsets (abfd) = local_got_offsets;
440 for (i = 0; i < symtab_hdr->sh_info; i++)
441 local_got_offsets[i] = (bfd_vma) -1;
443 if (local_got_offsets[r_symndx] != (bfd_vma) -1)
445 /* We have already allocated space in the .got. */
448 local_got_offsets[r_symndx] = sgot->_raw_size;
452 /* If we are generating a shared object, we need to
453 output a R_SPARC_RELATIVE reloc so that the
454 dynamic linker can adjust this GOT entry. */
455 srelgot->_raw_size += sizeof (Elf32_External_Rela);
459 sgot->_raw_size += 4;
464 /* This symbol requires a procedure linkage table entry. We
465 actually build the entry in adjust_dynamic_symbol,
466 because this might be a case of linking PIC code without
467 linking in any dynamic objects, in which case we don't
468 need to generate a procedure linkage table after all. */
472 /* It does not make sense to have a procedure linkage
473 table entry for a local symbol. */
474 bfd_set_error (bfd_error_bad_value);
478 /* Make sure this symbol is output as a dynamic symbol. */
479 if (h->dynindx == -1)
481 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
485 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
492 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
501 case R_SPARC_WDISP30:
502 case R_SPARC_WDISP22:
509 && (sec->flags & SEC_ALLOC) != 0)
511 /* When creating a shared object, we must copy these
512 relocs into the output file. We create a reloc
513 section in dynobj and make room for the reloc. */
518 name = (elf_string_from_elf_section
520 elf_elfheader (abfd)->e_shstrndx,
521 elf_section_data (sec)->rel_hdr.sh_name));
525 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
526 && strcmp (bfd_get_section_name (abfd, sec),
529 sreloc = bfd_get_section_by_name (dynobj, name);
532 sreloc = bfd_make_section (dynobj, name);
534 || ! bfd_set_section_flags (dynobj, sreloc,
540 || ! bfd_set_section_alignment (dynobj, sreloc, 2))
545 sreloc->_raw_size += sizeof (Elf32_External_Rela);
558 /* Adjust a symbol defined by a dynamic object and referenced by a
559 regular object. The current definition is in some section of the
560 dynamic object, but we're not including those sections. We have to
561 change the definition to something the rest of the link can
565 elf32_sparc_adjust_dynamic_symbol (info, h)
566 struct bfd_link_info *info;
567 struct elf_link_hash_entry *h;
571 unsigned int power_of_two;
573 dynobj = elf_hash_table (info)->dynobj;
575 /* Make sure we know what is going on here. */
576 BFD_ASSERT (dynobj != NULL
577 && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
578 || ((h->elf_link_hash_flags
579 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
580 && (h->elf_link_hash_flags
581 & ELF_LINK_HASH_REF_REGULAR) != 0
582 && (h->elf_link_hash_flags
583 & ELF_LINK_HASH_DEF_REGULAR) == 0
584 && h->root.type == bfd_link_hash_defined
585 && (bfd_get_flavour (h->root.u.def.section->owner)
586 == bfd_target_elf_flavour)
587 && (elf_elfheader (h->root.u.def.section->owner)->e_type
589 && h->root.u.def.section->output_section == NULL)));
591 /* If this is a function, put it in the procedure linkage table. We
592 will fill in the contents of the procedure linkage table later
593 (although we could actually do it here). */
594 if (h->type == STT_FUNC
595 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
597 if (! elf_hash_table (info)->dynamic_sections_created)
599 /* This case can occur if we saw a WPLT30 reloc in an input
600 file, but none of the input files were dynamic objects.
601 In such a case, we don't actually need to build a
602 procedure linkage table, and we can just do a WDISP30
604 BFD_ASSERT ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0);
608 s = bfd_get_section_by_name (dynobj, ".plt");
609 BFD_ASSERT (s != NULL);
611 /* The first four entries in .plt are reserved. */
612 if (s->_raw_size == 0)
613 s->_raw_size = 4 * PLT_ENTRY_SIZE;
615 /* The procedure linkage table has a maximum size. */
616 if (s->_raw_size >= 0x400000)
618 bfd_set_error (bfd_error_bad_value);
622 /* If we are not generating a shared library, or if the symbol
623 is not defined, set the symbol to this location in the .plt.
624 This is required to make function pointers compare as equal
625 between the normal executable and the shared library. */
626 if (! info->shared || h->root.type != bfd_link_hash_defined)
628 h->root.u.def.section = s;
629 h->root.u.def.value = s->_raw_size;
632 h->plt_offset = s->_raw_size;
634 /* Make room for this entry. */
635 s->_raw_size += PLT_ENTRY_SIZE;
637 /* We also need to make an entry in the .rela.plt section. */
639 s = bfd_get_section_by_name (dynobj, ".rela.plt");
640 BFD_ASSERT (s != NULL);
641 s->_raw_size += sizeof (Elf32_External_Rela);
646 /* If this is a weak symbol, and there is a real definition, the
647 processor independent code will have arranged for us to see the
648 real definition first, and we can just use the same value. */
649 if (h->weakdef != NULL)
651 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined);
652 h->root.u.def.section = h->weakdef->root.u.def.section;
653 h->root.u.def.value = h->weakdef->root.u.def.value;
657 /* This is a reference to a symbol defined by a dynamic object which
658 is not a function. */
660 /* If we are creating a shared library, we must presume that the
661 only references to the symbol are via the global offset table.
662 For such cases we need not do anything here; the relocations will
663 be handled correctly by relocate_section. */
667 /* We must allocate the symbol in our .dynbss section, which will
668 become part of the .bss section of the executable. There will be
669 an entry for this symbol in the .dynsym section. The dynamic
670 object will contain position independent code, so all references
671 from the dynamic object to this symbol will go through the global
672 offset table. The dynamic linker will use the .dynsym entry to
673 determine the address it must put in the global offset table, so
674 both the dynamic object and the regular object will refer to the
675 same memory location for the variable. */
677 s = bfd_get_section_by_name (dynobj, ".dynbss");
678 BFD_ASSERT (s != NULL);
680 /* If the symbol is currently defined in the .bss section of the
681 dynamic object, then it is OK to simply initialize it to zero.
682 If the symbol is in some other section, we must generate a
683 R_SPARC_COPY reloc to tell the dynamic linker to copy the initial
684 value out of the dynamic object and into the runtime process
685 image. We need to remember the offset into the .rel.bss section
686 we are going to use. */
687 if ((h->root.u.def.section->flags & SEC_LOAD) != 0)
691 srel = bfd_get_section_by_name (dynobj, ".rela.bss");
692 BFD_ASSERT (srel != NULL);
693 srel->_raw_size += sizeof (Elf32_External_Rela);
694 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
697 /* We need to figure out the alignment required for this symbol. I
698 have no idea how ELF linkers handle this. */
699 power_of_two = bfd_log2 (h->size);
700 if (power_of_two > 3)
703 /* Apply the required alignment. */
704 s->_raw_size = BFD_ALIGN (s->_raw_size,
705 (bfd_size_type) (1 << power_of_two));
706 if (power_of_two > bfd_get_section_alignment (dynobj, s))
708 if (! bfd_set_section_alignment (dynobj, s, power_of_two))
712 /* Define the symbol as being at this point in the section. */
713 h->root.u.def.section = s;
714 h->root.u.def.value = s->_raw_size;
716 /* Increment the section size to make room for the symbol. */
717 s->_raw_size += h->size;
722 /* Set the sizes of the dynamic sections. */
725 elf32_sparc_size_dynamic_sections (output_bfd, info)
727 struct bfd_link_info *info;
733 dynobj = elf_hash_table (info)->dynobj;
734 BFD_ASSERT (dynobj != NULL);
736 if (elf_hash_table (info)->dynamic_sections_created)
738 /* Set the contents of the .interp section to the interpreter. */
741 s = bfd_get_section_by_name (dynobj, ".interp");
742 BFD_ASSERT (s != NULL);
743 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
744 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
747 /* Make space for the trailing nop in .plt. */
748 s = bfd_get_section_by_name (dynobj, ".plt");
749 BFD_ASSERT (s != NULL);
750 if (s->_raw_size > 0)
755 /* We may have created entries in the .rela.got section.
756 However, if we are not creating the dynamic sections, we will
757 not actually use these entries. Reset the size of .rela.got,
758 which will cause it to get stripped from the output file
760 s = bfd_get_section_by_name (dynobj, ".rela.got");
765 /* The check_relocs and adjust_dynamic_symbol entry points have
766 determined the sizes of the various dynamic sections. Allocate
769 for (s = dynobj->sections; s != NULL; s = s->next)
774 if ((s->flags & SEC_IN_MEMORY) == 0)
777 /* It's OK to base decisions on the section name, because none
778 of the dynobj section names depend upon the input files. */
779 name = bfd_get_section_name (dynobj, s);
783 if (strncmp (name, ".rela", 5) == 0)
785 if (s->_raw_size == 0)
787 /* If we don't need this section, strip it from the
788 output file. This is to handle .rela.bss and
789 .rel.plt. We must create it in
790 create_dynamic_sections, because it must be created
791 before the linker maps input sections to output
792 sections. The linker does that before
793 adjust_dynamic_symbol is called, and it is that
794 function which decides whether anything needs to go
795 into these sections. */
802 /* If this relocation section applies to a read only
803 section, then we probably need a DT_TEXTREL entry. */
804 target = bfd_get_section_by_name (output_bfd, name + 5);
806 && (target->flags & SEC_READONLY) != 0)
809 /* We use the reloc_count field as a counter if we need
810 to copy relocs into the output file. */
814 else if (strcmp (name, ".plt") != 0
815 && strcmp (name, ".got") != 0)
817 /* It's not one of our sections, so don't allocate space. */
825 for (spp = &s->output_section->owner->sections;
826 *spp != s->output_section;
829 *spp = s->output_section->next;
830 --s->output_section->owner->section_count;
835 /* Allocate memory for the section contents. */
836 s->contents = (bfd_byte *) bfd_alloc (dynobj, s->_raw_size);
837 if (s->contents == NULL && s->_raw_size != 0)
839 bfd_set_error (bfd_error_no_memory);
844 if (elf_hash_table (info)->dynamic_sections_created)
846 /* Add some entries to the .dynamic section. We fill in the
847 values later, in elf32_sparc_finish_dynamic_sections, but we
848 must add the entries now so that we get the correct size for
849 the .dynamic section. The DT_DEBUG entry is filled in by the
850 dynamic linker and used by the debugger. */
853 if (! bfd_elf32_add_dynamic_entry (info, DT_DEBUG, 0))
857 if (! bfd_elf32_add_dynamic_entry (info, DT_PLTGOT, 0)
858 || ! bfd_elf32_add_dynamic_entry (info, DT_PLTRELSZ, 0)
859 || ! bfd_elf32_add_dynamic_entry (info, DT_PLTREL, DT_RELA)
860 || ! bfd_elf32_add_dynamic_entry (info, DT_JMPREL, 0)
861 || ! bfd_elf32_add_dynamic_entry (info, DT_RELA, 0)
862 || ! bfd_elf32_add_dynamic_entry (info, DT_RELASZ, 0)
863 || ! bfd_elf32_add_dynamic_entry (info, DT_RELAENT,
864 sizeof (Elf32_External_Rela)))
869 if (! bfd_elf32_add_dynamic_entry (info, DT_TEXTREL, 0))
874 /* If we are generating a shared library, we generate a section
875 symbol for each output section. These are local symbols, which
876 means that they must come first in the dynamic symbol table.
877 That means we must increment the dynamic symbol index of every
878 other dynamic symbol. */
883 c = bfd_count_sections (output_bfd);
884 elf_link_hash_traverse (elf_hash_table (info),
885 elf32_sparc_adjust_dynindx,
887 elf_hash_table (info)->dynsymcount += c;
889 for (i = 1, s = output_bfd->sections; s != NULL; s = s->next, i++)
891 elf_section_data (s)->dynindx = i;
892 /* These symbols will have no names, so we don't need to
893 fiddle with dynstr_index. */
900 /* Increment the index of a dynamic symbol by a given amount. Called
901 via elf_link_hash_traverse. */
904 elf32_sparc_adjust_dynindx (h, cparg)
905 struct elf_link_hash_entry *h;
908 int *cp = (int *) cparg;
910 if (h->dynindx != -1)
915 /* Relocate a SPARC ELF section. */
918 elf32_sparc_relocate_section (output_bfd, info, input_bfd, input_section,
919 contents, relocs, local_syms, local_sections)
921 struct bfd_link_info *info;
923 asection *input_section;
925 Elf_Internal_Rela *relocs;
926 Elf_Internal_Sym *local_syms;
927 asection **local_sections;
930 Elf_Internal_Shdr *symtab_hdr;
931 struct elf_link_hash_entry **sym_hashes;
932 bfd_vma *local_got_offsets;
936 Elf_Internal_Rela *rel;
937 Elf_Internal_Rela *relend;
939 dynobj = elf_hash_table (info)->dynobj;
940 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
941 sym_hashes = elf_sym_hashes (input_bfd);
942 local_got_offsets = elf_local_got_offsets (input_bfd);
949 relend = relocs + input_section->reloc_count;
950 for (; rel < relend; rel++)
953 const reloc_howto_type *howto;
955 struct elf_link_hash_entry *h;
956 Elf_Internal_Sym *sym;
959 bfd_reloc_status_type r;
961 r_type = ELF32_R_TYPE (rel->r_info);
962 if (r_type < 0 || r_type >= (int) R_SPARC_max)
964 bfd_set_error (bfd_error_bad_value);
967 howto = elf_sparc_howto_table + r_type;
969 r_symndx = ELF32_R_SYM (rel->r_info);
971 if (info->relocateable)
973 /* This is a relocateable link. We don't have to change
974 anything, unless the reloc is against a section symbol,
975 in which case we have to adjust according to where the
976 section symbol winds up in the output section. */
977 if (r_symndx < symtab_hdr->sh_info)
979 sym = local_syms + r_symndx;
980 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
982 sec = local_sections[r_symndx];
983 rel->r_addend += sec->output_offset + sym->st_value;
990 /* This is a final link. */
994 if (r_symndx < symtab_hdr->sh_info)
996 sym = local_syms + r_symndx;
997 sec = local_sections[r_symndx];
998 relocation = (sec->output_section->vma
1004 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1005 if (h->root.type == bfd_link_hash_defined)
1007 sec = h->root.u.def.section;
1008 relocation = (h->root.u.def.value
1009 + sec->output_section->vma
1010 + sec->output_offset);
1012 else if (h->root.type == bfd_link_hash_weak)
1014 else if (info->shared)
1018 if (! ((*info->callbacks->undefined_symbol)
1019 (info, h->root.root.string, input_bfd,
1020 input_section, rel->r_offset)))
1031 /* Relocation is to the entry for this symbol in the global
1035 sgot = bfd_get_section_by_name (dynobj, ".got");
1036 BFD_ASSERT (sgot != NULL);
1043 off = h->got_offset;
1044 BFD_ASSERT (off != (bfd_vma) -1);
1046 if (! elf_hash_table (info)->dynamic_sections_created)
1048 /* This is actually a static link. We must
1049 initialize this entry in the global offset table.
1050 Since the offset must always be a multiple of 4,
1051 we use the least significant bit to record
1052 whether we have initialized it already.
1054 When doing a dynamic link, we create a .rela.got
1055 relocation entry to initialize the value. This
1056 is done in the finish_dynamic_symbol routine. */
1061 bfd_put_32 (output_bfd, relocation,
1062 sgot->contents + off);
1067 relocation = sgot->output_offset + off;
1073 BFD_ASSERT (local_got_offsets != NULL
1074 && local_got_offsets[r_symndx] != (bfd_vma) -1);
1076 off = local_got_offsets[r_symndx];
1078 /* The offset must always be a multiple of 4. We use
1079 the least significant bit to record whether we have
1080 already processed this entry. */
1085 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
1090 Elf_Internal_Rela outrel;
1092 /* We need to generate a R_SPARC_RELATIVE reloc
1093 for the dynamic linker. */
1094 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
1095 BFD_ASSERT (srelgot != NULL);
1097 outrel.r_offset = (sgot->output_section->vma
1098 + sgot->output_offset
1100 outrel.r_info = ELF32_R_INFO (0, R_SPARC_RELATIVE);
1101 outrel.r_addend = 0;
1102 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1103 (((Elf32_External_Rela *)
1105 + srelgot->reloc_count));
1106 ++srelgot->reloc_count;
1109 local_got_offsets[r_symndx] |= 1;
1112 relocation = sgot->output_offset + off;
1117 case R_SPARC_WPLT30:
1118 /* Relocation is to the entry for this symbol in the
1119 procedure linkage table. */
1120 BFD_ASSERT (h != NULL);
1122 if (h->plt_offset == (bfd_vma) -1)
1124 /* We didn't make a PLT entry for this symbol. This
1125 happens when statically linking PIC code. */
1131 splt = bfd_get_section_by_name (dynobj, ".plt");
1132 BFD_ASSERT (splt != NULL);
1135 relocation = (splt->output_section->vma
1136 + splt->output_offset
1143 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
1150 case R_SPARC_DISP16:
1151 case R_SPARC_DISP32:
1152 case R_SPARC_WDISP30:
1153 case R_SPARC_WDISP22:
1160 && (input_section->flags & SEC_ALLOC) != 0)
1162 Elf_Internal_Rela outrel;
1164 /* When generating a shared object, these relocations
1165 are copied into the output file to be resolved at run
1172 name = (elf_string_from_elf_section
1174 elf_elfheader (input_bfd)->e_shstrndx,
1175 elf_section_data (input_section)->rel_hdr.sh_name));
1179 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
1180 && strcmp (bfd_get_section_name (input_bfd,
1184 sreloc = bfd_get_section_by_name (dynobj, name);
1185 BFD_ASSERT (sreloc != NULL);
1188 outrel.r_offset = (rel->r_offset
1189 + input_section->output_section->vma
1190 + input_section->output_offset);
1193 BFD_ASSERT (h->dynindx != -1);
1194 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
1195 outrel.r_addend = rel->r_addend;
1199 if (r_type == R_SPARC_32)
1201 outrel.r_info = ELF32_R_INFO (0, R_SPARC_RELATIVE);
1202 outrel.r_addend = relocation + rel->r_addend;
1208 sym = local_syms + r_symndx;
1210 BFD_ASSERT (ELF_ST_TYPE (sym->st_info) == STT_SECTION);
1212 sec = local_sections[r_symndx];
1213 if (sec != NULL && bfd_is_abs_section (sec))
1215 else if (sec == NULL || sec->owner == NULL)
1217 bfd_set_error (bfd_error_bad_value);
1224 osec = sec->output_section;
1225 indx = elf_section_data (osec)->dynindx;
1230 outrel.r_info = ELF32_R_INFO (indx, r_type);
1231 outrel.r_addend = relocation + rel->r_addend;
1235 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1236 (((Elf32_External_Rela *)
1238 + sreloc->reloc_count));
1239 ++sreloc->reloc_count;
1241 /* This reloc will be computed at runtime, so there's no
1242 need to do anything now. */
1250 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1251 contents, rel->r_offset,
1252 relocation, rel->r_addend);
1254 if (r != bfd_reloc_ok)
1259 case bfd_reloc_outofrange:
1261 case bfd_reloc_overflow:
1266 name = h->root.root.string;
1269 name = elf_string_from_elf_section (input_bfd,
1270 symtab_hdr->sh_link,
1275 name = bfd_section_name (input_bfd, sec);
1277 if (! ((*info->callbacks->reloc_overflow)
1278 (info, name, howto->name, (bfd_vma) 0,
1279 input_bfd, input_section, rel->r_offset)))
1290 /* Finish up dynamic symbol handling. We set the contents of various
1291 dynamic sections here. */
1294 elf32_sparc_finish_dynamic_symbol (output_bfd, info, h, sym)
1296 struct bfd_link_info *info;
1297 struct elf_link_hash_entry *h;
1298 Elf_Internal_Sym *sym;
1302 dynobj = elf_hash_table (info)->dynobj;
1304 if (h->plt_offset != (bfd_vma) -1)
1308 Elf_Internal_Rela rela;
1310 /* This symbol has an entry in the procedure linkage table. Set
1313 BFD_ASSERT (h->dynindx != -1);
1315 splt = bfd_get_section_by_name (dynobj, ".plt");
1316 srela = bfd_get_section_by_name (dynobj, ".rela.plt");
1317 BFD_ASSERT (splt != NULL && srela != NULL);
1319 /* Fill in the entry in the procedure linkage table. */
1320 bfd_put_32 (output_bfd,
1321 PLT_ENTRY_WORD0 + h->plt_offset,
1322 splt->contents + h->plt_offset);
1323 bfd_put_32 (output_bfd,
1325 + (((- (h->plt_offset + 4)) >> 2) & 0x3fffff)),
1326 splt->contents + h->plt_offset + 4);
1327 bfd_put_32 (output_bfd, PLT_ENTRY_WORD2,
1328 splt->contents + h->plt_offset + 8);
1330 /* Fill in the entry in the .rela.plt section. */
1331 rela.r_offset = (splt->output_section->vma
1332 + splt->output_offset
1334 rela.r_info = ELF32_R_INFO (h->dynindx, R_SPARC_JMP_SLOT);
1336 bfd_elf32_swap_reloca_out (output_bfd, &rela,
1337 ((Elf32_External_Rela *) srela->contents
1338 + h->plt_offset / PLT_ENTRY_SIZE - 4));
1340 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1342 /* Mark the symbol as undefined, rather than as defined in
1343 the .plt section. Leave the value alone. */
1344 sym->st_shndx = SHN_UNDEF;
1348 if (h->got_offset != (bfd_vma) -1)
1352 Elf_Internal_Rela rela;
1354 /* This symbol has an entry in the global offset table. Set it
1357 BFD_ASSERT (h->dynindx != -1);
1359 sgot = bfd_get_section_by_name (dynobj, ".got");
1360 srela = bfd_get_section_by_name (dynobj, ".rela.got");
1361 BFD_ASSERT (sgot != NULL && srela != NULL);
1363 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got_offset);
1365 rela.r_offset = (sgot->output_section->vma
1366 + sgot->output_offset
1368 rela.r_info = ELF32_R_INFO (h->dynindx, R_SPARC_GLOB_DAT);
1370 bfd_elf32_swap_reloca_out (output_bfd, &rela,
1371 ((Elf32_External_Rela *) srela->contents
1372 + srela->reloc_count));
1373 ++srela->reloc_count;
1376 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
1379 Elf_Internal_Rela rela;
1381 /* This symbols needs a copy reloc. Set it up. */
1383 BFD_ASSERT (h->dynindx != -1);
1385 s = bfd_get_section_by_name (h->root.u.def.section->owner,
1387 BFD_ASSERT (s != NULL);
1389 rela.r_offset = (h->root.u.def.value
1390 + h->root.u.def.section->output_section->vma
1391 + h->root.u.def.section->output_offset);
1392 rela.r_info = ELF32_R_INFO (h->dynindx, R_SPARC_COPY);
1394 bfd_elf32_swap_reloca_out (output_bfd, &rela,
1395 ((Elf32_External_Rela *) s->contents
1400 /* Mark some specially defined symbols as absolute. */
1401 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
1402 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
1403 || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0)
1404 sym->st_shndx = SHN_ABS;
1409 /* Finish up the dynamic sections. */
1412 elf32_sparc_finish_dynamic_sections (output_bfd, info)
1414 struct bfd_link_info *info;
1420 dynobj = elf_hash_table (info)->dynobj;
1422 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
1424 if (elf_hash_table (info)->dynamic_sections_created)
1427 Elf32_External_Dyn *dyncon, *dynconend;
1429 splt = bfd_get_section_by_name (dynobj, ".plt");
1430 BFD_ASSERT (splt != NULL && sdyn != NULL);
1432 dyncon = (Elf32_External_Dyn *) sdyn->contents;
1433 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
1434 for (; dyncon < dynconend; dyncon++)
1436 Elf_Internal_Dyn dyn;
1440 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
1444 case DT_PLTGOT: name = ".plt"; size = false; break;
1445 case DT_PLTRELSZ: name = ".rela.plt"; size = true; break;
1446 case DT_JMPREL: name = ".rela.plt"; size = false; break;
1447 default: name = NULL; size = false; break;
1454 s = bfd_get_section_by_name (output_bfd, name);
1460 dyn.d_un.d_ptr = s->vma;
1463 if (s->_cooked_size != 0)
1464 dyn.d_un.d_val = s->_cooked_size;
1466 dyn.d_un.d_val = s->_raw_size;
1469 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1473 /* Clear the first four entries in the procedure linkage table,
1474 and put a nop in the last four bytes. */
1475 if (splt->_raw_size > 0)
1477 memset (splt->contents, 0, 4 * PLT_ENTRY_SIZE);
1478 bfd_put_32 (output_bfd, SPARC_NOP,
1479 splt->contents + splt->_raw_size - 4);
1482 elf_section_data (splt->output_section)->this_hdr.sh_entsize =
1486 /* Set the first entry in the global offset table to the address of
1487 the dynamic section. */
1488 sgot = bfd_get_section_by_name (dynobj, ".got");
1489 BFD_ASSERT (sgot != NULL);
1490 if (sgot->_raw_size > 0)
1493 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
1495 bfd_put_32 (output_bfd,
1496 sdyn->output_section->vma + sdyn->output_offset,
1500 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
1506 Elf_Internal_Sym sym;
1508 /* Set up the section symbols for the output sections. */
1510 sdynsym = bfd_get_section_by_name (dynobj, ".dynsym");
1511 BFD_ASSERT (sdynsym != NULL);
1515 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
1518 for (s = output_bfd->sections; s != NULL; s = s->next)
1522 sym.st_value = s->vma;
1524 indx = elf_section_data (s)->this_idx;
1525 BFD_ASSERT (indx > 0);
1526 sym.st_shndx = indx;
1528 bfd_elf32_swap_symbol_out (output_bfd, &sym,
1529 ((Elf32_External_Sym *) sdynsym->contents
1530 + elf_section_data (s)->dynindx));
1533 /* Set the sh_info field of the output .dynsym section to the
1534 index of the first global symbol. */
1535 elf_section_data (sdynsym->output_section)->this_hdr.sh_info =
1536 bfd_count_sections (output_bfd) + 1;
1542 #define TARGET_BIG_SYM bfd_elf32_sparc_vec
1543 #define TARGET_BIG_NAME "elf32-sparc"
1544 #define ELF_ARCH bfd_arch_sparc
1545 #define ELF_MACHINE_CODE EM_SPARC
1546 #define ELF_MAXPAGESIZE 0x10000
1547 #define elf_backend_create_dynamic_sections \
1548 elf32_sparc_create_dynamic_sections
1549 #define elf_backend_check_relocs elf32_sparc_check_relocs
1550 #define elf_backend_adjust_dynamic_symbol \
1551 elf32_sparc_adjust_dynamic_symbol
1552 #define elf_backend_size_dynamic_sections \
1553 elf32_sparc_size_dynamic_sections
1554 #define elf_backend_relocate_section elf32_sparc_relocate_section
1555 #define elf_backend_finish_dynamic_symbol \
1556 elf32_sparc_finish_dynamic_symbol
1557 #define elf_backend_finish_dynamic_sections \
1558 elf32_sparc_finish_dynamic_sections
1560 #include "elf32-target.h"