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_check_relocs
33 PARAMS ((bfd
*, struct bfd_link_info
*, asection
*,
34 const Elf_Internal_Rela
*));
35 static boolean elf32_sparc_adjust_dynamic_symbol
36 PARAMS ((struct bfd_link_info
*, struct elf_link_hash_entry
*));
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
**, char *));
42 static boolean elf32_sparc_finish_dynamic_symbol
43 PARAMS ((bfd
*, struct bfd_link_info
*, struct elf_link_hash_entry
*,
45 static boolean elf32_sparc_finish_dynamic_sections
46 PARAMS ((bfd
*, struct bfd_link_info
*));
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
,
60 R_SPARC_GLOB_DAT
, R_SPARC_JMP_SLOT
,
67 static CONST
char *CONST reloc_type_names
[] =
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",
79 "R_SPARC_GLOB_DAT", "R_SPARC_JMP_SLOT",
85 static reloc_howto_type elf_sparc_howto_table
[] =
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_bitfield
,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
, 0,2,22,true, 0,complain_overflow_bitfield
,bfd_elf_generic_reloc
,"R_SPARC_PC22", false,0,0x003fffff,true),
105 HOWTO(R_SPARC_WPLT30
, 0,0,00,true, 0,complain_overflow_dont
, bfd_elf_generic_reloc
,"R_SPARC_WPLT30", false,0,0x00000000,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),
113 struct elf_reloc_map
{
114 unsigned char bfd_reloc_val
;
115 unsigned char elf_reloc_val
;
118 static CONST
struct elf_reloc_map sparc_reloc_map
[] =
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?? */
146 static CONST
struct reloc_howto_struct
*
147 bfd_elf32_bfd_reloc_type_lookup (abfd
, code
)
149 bfd_reloc_code_real_type code
;
152 for (i
= 0; i
< sizeof (sparc_reloc_map
) / sizeof (struct elf_reloc_map
); i
++)
154 if (sparc_reloc_map
[i
].bfd_reloc_val
== code
)
155 return &elf_sparc_howto_table
[(int) sparc_reloc_map
[i
].elf_reloc_val
];
161 elf_info_to_howto (abfd
, cache_ptr
, dst
)
164 Elf_Internal_Rela
*dst
;
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
)];
171 /* Functions for the SPARC ELF linker. */
173 /* The name of the dynamic interpreter. This is put in the .interp
176 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
178 /* The nop opcode we use. */
180 #define SPARC_NOP 0x01000000
182 /* The size in bytes of an entry in the procedure linkage table. */
184 #define PLT_ENTRY_SIZE 12
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. */
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
196 #define PLT_ENTRY_WORD2 SPARC_NOP
198 /* Create dynamic sections when linking against a dynamic object. */
201 elf32_sparc_create_dynamic_sections (abfd
, info
)
203 struct bfd_link_info
*info
;
206 register asection
*s
;
207 struct elf_link_hash_entry
*h
;
209 /* We need to create .plt, .rela.plt, .got, .dynbss, and .rela.bss
212 flags
= SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
| SEC_IN_MEMORY
;
214 s
= bfd_make_section (abfd
, ".plt");
216 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_CODE
)
217 || ! bfd_set_section_alignment (abfd
, s
, 2))
220 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
223 if (! (_bfd_generic_link_add_one_symbol
224 (info
, abfd
, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL
, s
, (bfd_vma
) 0,
225 (const char *) NULL
, false, get_elf_backend_data (abfd
)->collect
,
226 (struct bfd_link_hash_entry
**) &h
)))
228 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
229 h
->type
= STT_OBJECT
;
232 && ! bfd_elf32_link_record_dynamic_symbol (info
, h
))
235 /* The first four entries in .plt are reserved. */
236 s
->_raw_size
= 4 * PLT_ENTRY_SIZE
;
238 s
= bfd_make_section (abfd
, ".rela.plt");
240 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
241 || ! bfd_set_section_alignment (abfd
, s
, 2))
244 s
= bfd_make_section (abfd
, ".got");
246 || ! bfd_set_section_flags (abfd
, s
, flags
)
247 || ! bfd_set_section_alignment (abfd
, s
, 2))
250 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
251 section. We don't do this in the linker script because we don't
252 want to define the symbol if we are not creating a global offset
253 table. FIXME: The Solaris linker puts _GLOBAL_OFFSET_TABLE_ at
254 the start of the .got section, but when using the small PIC model
255 the .got is accessed using a signed 13 bit offset. Shouldn't
256 _GLOBAL_OFFSET_TABLE_ be located at .got + 4096? */
258 if (! (_bfd_generic_link_add_one_symbol
259 (info
, abfd
, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL
, s
, (bfd_vma
) 0,
260 (const char *) NULL
, false, get_elf_backend_data (abfd
)->collect
,
261 (struct bfd_link_hash_entry
**) &h
)))
263 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
264 h
->type
= STT_OBJECT
;
267 && ! bfd_elf32_link_record_dynamic_symbol (info
, h
))
270 /* The first global offset table entry is reserved. */
273 /* The .dynbss section is a place to put symbols which are defined
274 by dynamic objects, are referenced by regular objects, and are
275 not functions. We must allocate space for them in the process
276 image and use a R_SPARC_COPY reloc to tell the dynamic linker to
277 initialize them at run time. The linker script puts the .dynbss
278 section into the .bss section of the final image. */
279 s
= bfd_make_section (abfd
, ".dynbss");
281 || ! bfd_set_section_flags (abfd
, s
, SEC_ALLOC
))
284 /* The .rela.bss section holds copy relocs. */
287 s
= bfd_make_section (abfd
, ".rela.bss");
289 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
290 || ! bfd_set_section_alignment (abfd
, s
, 2))
297 /* Look through the relocs for a section during the first phase, and
298 allocate space in the global offset table or procedure linkage
302 elf32_sparc_check_relocs (abfd
, info
, sec
, relocs
)
304 struct bfd_link_info
*info
;
306 const Elf_Internal_Rela
*relocs
;
309 Elf_Internal_Shdr
*symtab_hdr
;
310 struct elf_link_hash_entry
**sym_hashes
;
311 bfd_vma
*local_got_offsets
;
312 const Elf_Internal_Rela
*rel
;
313 const Elf_Internal_Rela
*rel_end
;
320 if (info
->relocateable
)
323 dynobj
= elf_hash_table (info
)->dynobj
;
324 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
325 sym_hashes
= elf_sym_hashes (abfd
);
326 local_got_offsets
= elf_local_got_offsets (abfd
);
334 rel_end
= relocs
+ sec
->reloc_count
;
335 for (rel
= relocs
; rel
< rel_end
; rel
++)
338 struct elf_link_hash_entry
*h
;
340 r_symndx
= ELF32_R_SYM (rel
->r_info
);
341 if (r_symndx
< symtab_hdr
->sh_info
)
344 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
346 /* Some relocs require a global offset table. FIXME: If this is
347 a static link, we don't really need to create the full
348 dynamic linking information. */
351 switch (ELF32_R_TYPE (rel
->r_info
))
357 elf_hash_table (info
)->dynobj
= dynobj
= abfd
;
358 if (! bfd_elf32_link_create_dynamic_sections (dynobj
, info
))
367 switch (ELF32_R_TYPE (rel
->r_info
))
372 /* This symbol requires a global offset table entry. */
376 sgot
= bfd_get_section_by_name (dynobj
, ".got");
377 srelgot
= bfd_get_section_by_name (dynobj
, ".rela.got");
380 srelgot
= bfd_make_section (dynobj
, ".rela.got");
382 || ! bfd_set_section_flags (dynobj
, srelgot
,
390 BFD_ASSERT (sgot
!= NULL
&& srelgot
!= NULL
);
395 if (h
->got_offset
!= (bfd_vma
) -1)
397 /* We have already allocated space in the .got. */
400 h
->got_offset
= sgot
->_raw_size
;
404 /* This is a global offset table entry for a local
406 if (local_got_offsets
== NULL
)
411 size
= symtab_hdr
->sh_info
* sizeof (bfd_vma
);
412 local_got_offsets
= (bfd_vma
*) bfd_alloc (abfd
, size
);
413 if (local_got_offsets
== NULL
)
415 bfd_set_error (bfd_error_no_memory
);
418 elf_local_got_offsets (abfd
) = local_got_offsets
;
419 for (i
= 0; i
< symtab_hdr
->sh_info
; i
++)
420 local_got_offsets
[i
] = (bfd_vma
) -1;
422 if (local_got_offsets
[r_symndx
] != (bfd_vma
) -1)
424 /* We have already allocated space in the .got. */
427 local_got_offsets
[r_symndx
] = sgot
->_raw_size
;
430 sgot
->_raw_size
+= 4;
431 srelgot
->_raw_size
+= sizeof (Elf32_External_Rela
);
436 /* This symbol requires a procedure linkage table entry. */
440 /* It does not make sense to have a procedure linkage
441 table entry for a local symbol. */
442 bfd_set_error (bfd_error_bad_value
);
446 if (h
->plt_offset
!= (bfd_vma
) -1)
448 /* There is already an entry for this symbol in the
449 procedure linkage table. */
455 splt
= bfd_get_section_by_name (dynobj
, ".plt");
456 srelplt
= bfd_get_section_by_name (dynobj
, ".rela.plt");
457 BFD_ASSERT (splt
!= NULL
&& srelplt
!= NULL
);
460 /* The procedure linkage table has a maximum size. */
461 if (splt
->_raw_size
>= 0x400000)
463 bfd_set_error (bfd_error_bad_value
);
467 h
->plt_offset
= splt
->_raw_size
;
469 /* Make room for this entry. */
470 splt
->_raw_size
+= PLT_ENTRY_SIZE
;
471 srelplt
->_raw_size
+= sizeof (Elf32_External_Rela
);
478 && strcmp (h
->root
.root
.string
, "_GLOBAL_OFFSET_TABLE_") == 0)
487 case R_SPARC_WDISP30
:
488 case R_SPARC_WDISP22
:
495 && (sec
->flags
& SEC_ALLOC
) != 0)
497 /* When creating a shared object, we must copy these
498 relocs into the output file. We create a reloc
499 section in dynobj and make room for the reloc. */
504 name
= (elf_string_from_elf_section
506 elf_elfheader (abfd
)->e_shstrndx
,
507 elf_section_data (sec
)->rel_hdr
.sh_name
));
511 BFD_ASSERT (strncmp (name
, ".rela", 5) == 0
512 && strcmp (bfd_get_section_name (abfd
, sec
),
515 sreloc
= bfd_get_section_by_name (dynobj
, name
);
518 sreloc
= bfd_make_section (dynobj
, name
);
520 || ! bfd_set_section_flags (dynobj
, sreloc
,
526 || ! bfd_set_section_alignment (dynobj
, sreloc
, 2))
531 sreloc
->_raw_size
+= sizeof (Elf32_External_Rela
);
544 /* Adjust a symbol defined by a dynamic object and referenced by a
545 regular object. The current definition is in some section of the
546 dynamic object, but we're not including those sections. We have to
547 change the definition to something the rest of the link can
551 elf32_sparc_adjust_dynamic_symbol (info
, h
)
552 struct bfd_link_info
*info
;
553 struct elf_link_hash_entry
*h
;
557 unsigned int power_of_two
;
559 dynobj
= elf_hash_table (info
)->dynobj
;
561 /* Make sure we know what is going on here. */
562 BFD_ASSERT (dynobj
!= NULL
563 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
564 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) != 0
565 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
566 && h
->root
.type
== bfd_link_hash_defined
567 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
568 == bfd_target_elf_flavour
)
569 && (elf_elfheader (h
->root
.u
.def
.section
->owner
)->e_type
571 && h
->root
.u
.def
.section
->output_section
== NULL
);
573 /* If this is a function, put it in the procedure linkage table. We
574 will fill in the contents of the procedure linkage table later
575 (although we could actually do it here). */
576 if (h
->type
== STT_FUNC
)
578 if (h
->plt_offset
== (bfd_vma
) -1)
580 s
= bfd_get_section_by_name (dynobj
, ".plt");
581 BFD_ASSERT (s
!= NULL
);
583 /* The procedure linkage table has a maximum size. */
584 if (s
->_raw_size
>= 0x400000)
586 bfd_set_error (bfd_error_bad_value
);
590 /* Set the symbol to this location in the .plt. */
591 h
->root
.u
.def
.section
= s
;
592 h
->root
.u
.def
.value
= s
->_raw_size
;
594 h
->plt_offset
= s
->_raw_size
;
596 /* Make room for this entry. */
597 s
->_raw_size
+= PLT_ENTRY_SIZE
;
599 /* We also need to make an entry in the .rela.plt section. */
601 s
= bfd_get_section_by_name (dynobj
, ".rela.plt");
602 BFD_ASSERT (s
!= NULL
);
603 s
->_raw_size
+= sizeof (Elf32_External_Rela
);
609 /* If this is a weak symbol, and there is a real definition, the
610 processor independent code will have arranged for us to see the
611 real definition first, and we can just use the same value. */
612 if (h
->weakdef
!= NULL
)
614 BFD_ASSERT (h
->weakdef
->root
.type
== bfd_link_hash_defined
);
615 h
->root
.u
.def
.section
= h
->weakdef
->root
.u
.def
.section
;
616 h
->root
.u
.def
.value
= h
->weakdef
->root
.u
.def
.value
;
620 /* This is a reference to a symbol defined by a dynamic object which
621 is not a function. */
623 /* If we are creating a shared library, we must presume that the
624 only references to the symbol are via the global offset table.
625 For such cases we need not do anything here; the relocations will
626 be handled correctly by relocate_section. */
630 /* We must allocate the symbol in our .dynbss section, which will
631 become part of the .bss section of the executable. There will be
632 an entry for this symbol in the .dynsym section. The dynamic
633 object will contain position independent code, so all references
634 from the dynamic object to this symbol will go through the global
635 offset table. The dynamic linker will use the .dynsym entry to
636 determine the address it must put in the global offset table, so
637 both the dynamic object and the regular object will refer to the
638 same memory location for the variable. */
640 s
= bfd_get_section_by_name (dynobj
, ".dynbss");
641 BFD_ASSERT (s
!= NULL
);
643 /* If the symbol is currently defined in the .bss section of the
644 dynamic object, then it is OK to simply initialize it to zero.
645 If the symbol is in some other section, we must generate a
646 R_SPARC_COPY reloc to tell the dynamic linker to copy the initial
647 value out of the dynamic object and into the runtime process
648 image. We need to remember the offset into the .rel.bss section
649 we are going to use. */
650 if ((h
->root
.u
.def
.section
->flags
& SEC_LOAD
) != 0)
654 srel
= bfd_get_section_by_name (dynobj
, ".rela.bss");
655 BFD_ASSERT (srel
!= NULL
);
656 srel
->_raw_size
+= sizeof (Elf32_External_Rela
);
657 h
->elf_link_hash_flags
|= ELF_LINK_HASH_NEEDS_COPY
;
660 /* We need to figure out the alignment required for this symbol. I
661 have no idea how ELF linkers handle this. */
662 power_of_two
= bfd_log2 (h
->size
);
663 if (power_of_two
> 3)
666 /* Apply the required alignment. */
667 s
->_raw_size
= BFD_ALIGN (s
->_raw_size
,
668 (bfd_size_type
) (1 << power_of_two
));
669 if (power_of_two
> bfd_get_section_alignment (dynobj
, s
))
671 if (! bfd_set_section_alignment (dynobj
, s
, power_of_two
))
675 /* Define the symbol as being at this point in the section. */
676 h
->root
.u
.def
.section
= s
;
677 h
->root
.u
.def
.value
= s
->_raw_size
;
679 /* Increment the section size to make room for the symbol. */
680 s
->_raw_size
+= h
->size
;
685 /* Set the sizes of the dynamic sections. */
688 elf32_sparc_size_dynamic_sections (output_bfd
, info
)
690 struct bfd_link_info
*info
;
696 dynobj
= elf_hash_table (info
)->dynobj
;
697 BFD_ASSERT (dynobj
!= NULL
);
699 /* Set the contents of the .interp section to the interpreter. */
702 s
= bfd_get_section_by_name (dynobj
, ".interp");
703 BFD_ASSERT (s
!= NULL
);
704 s
->_raw_size
= sizeof ELF_DYNAMIC_INTERPRETER
;
705 s
->contents
= (unsigned char *) ELF_DYNAMIC_INTERPRETER
;
708 /* Make space for the trailing nop in .plt. */
709 s
= bfd_get_section_by_name (dynobj
, ".plt");
710 BFD_ASSERT (s
!= NULL
);
713 /* The check_relocs and adjust_dynamic_symbol entry points have
714 determined the sizes of the various dynamic sections. Allocate
717 for (s
= dynobj
->sections
; s
!= NULL
; s
= s
->next
)
721 if ((s
->flags
& SEC_IN_MEMORY
) == 0)
724 /* It's OK to base decisions on the section name, because none
725 of the dynobj section names depend upon the input files. */
726 name
= bfd_get_section_name (dynobj
, s
);
728 if (strncmp (name
, ".rela", 5) == 0
733 /* If this relocation section applies to a read only
734 section, then we probably need a DT_TEXTREL entry. */
735 target
= bfd_get_section_by_name (output_bfd
, name
+ 5);
737 && (target
->flags
& SEC_READONLY
) != 0)
740 /* We use the reloc_count field as a counter if we need to
741 copy relocs into the output file. */
744 else if (strcmp (name
, ".plt") != 0
745 && strcmp (name
, ".got") != 0)
747 /* It's not one of our sections, so don't allocate space. */
751 /* Allocate memory for the section contents. */
752 s
->contents
= (bfd_byte
*) bfd_alloc (dynobj
, s
->_raw_size
);
753 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
755 bfd_set_error (bfd_error_no_memory
);
760 /* Add some entries to the .dynamic section. We fill in the values
761 later, in elf32_sparc_finish_dynamic_sections, but we must add
762 the entries now so that we get the correct size for the .dynamic
763 section. The DT_DEBUG entry is filled in by the dynamic linker
764 and used by the debugger. */
767 if (! bfd_elf32_add_dynamic_entry (info
, DT_DEBUG
, 0))
771 if (! bfd_elf32_add_dynamic_entry (info
, DT_PLTGOT
, 0)
772 || ! bfd_elf32_add_dynamic_entry (info
, DT_PLTRELSZ
, 0)
773 || ! bfd_elf32_add_dynamic_entry (info
, DT_PLTREL
, DT_RELA
)
774 || ! bfd_elf32_add_dynamic_entry (info
, DT_JMPREL
, 0)
775 || ! bfd_elf32_add_dynamic_entry (info
, DT_RELA
, 0)
776 || ! bfd_elf32_add_dynamic_entry (info
, DT_RELASZ
, 0)
777 || ! bfd_elf32_add_dynamic_entry (info
, DT_RELAENT
,
778 sizeof (Elf32_External_Rela
)))
783 if (! bfd_elf32_add_dynamic_entry (info
, DT_TEXTREL
, 0))
790 /* Relocate a SPARC ELF section. */
793 elf32_sparc_relocate_section (output_bfd
, info
, input_bfd
, input_section
,
794 contents
, relocs
, local_syms
, local_sections
,
797 struct bfd_link_info
*info
;
799 asection
*input_section
;
801 Elf_Internal_Rela
*relocs
;
802 Elf_Internal_Sym
*local_syms
;
803 asection
**local_sections
;
807 Elf_Internal_Shdr
*symtab_hdr
;
808 struct elf_link_hash_entry
**sym_hashes
;
809 bfd_vma
*local_got_offsets
;
813 Elf_Internal_Rela
*rel
;
814 Elf_Internal_Rela
*relend
;
816 dynobj
= elf_hash_table (info
)->dynobj
;
817 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
818 sym_hashes
= elf_sym_hashes (input_bfd
);
819 local_got_offsets
= elf_local_got_offsets (input_bfd
);
826 relend
= relocs
+ input_section
->reloc_count
;
827 for (; rel
< relend
; rel
++)
830 const reloc_howto_type
*howto
;
832 struct elf_link_hash_entry
*h
;
833 Elf_Internal_Sym
*sym
;
836 bfd_reloc_status_type r
;
838 r_type
= ELF32_R_TYPE (rel
->r_info
);
839 if (r_type
< 0 || r_type
>= (int) R_SPARC_max
)
841 bfd_set_error (bfd_error_bad_value
);
844 howto
= elf_sparc_howto_table
+ r_type
;
846 r_symndx
= ELF32_R_SYM (rel
->r_info
);
848 if (info
->relocateable
)
850 /* This is a relocateable link. We don't have to change
851 anything, unless the reloc is against a section symbol,
852 in which case we have to adjust according to where the
853 section symbol winds up in the output section. */
854 if (r_symndx
< symtab_hdr
->sh_info
)
856 sym
= local_syms
+ r_symndx
;
857 if (ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
)
859 sec
= local_sections
[r_symndx
];
860 rel
->r_addend
+= sec
->output_offset
+ sym
->st_value
;
867 /* This is a final link. */
871 if (r_symndx
< symtab_hdr
->sh_info
)
873 sym
= local_syms
+ r_symndx
;
874 sec
= local_sections
[r_symndx
];
875 relocation
= (sec
->output_section
->vma
881 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
882 if (h
->root
.type
== bfd_link_hash_defined
)
884 sec
= h
->root
.u
.def
.section
;
885 relocation
= (h
->root
.u
.def
.value
886 + sec
->output_section
->vma
887 + sec
->output_offset
);
889 else if (h
->root
.type
== bfd_link_hash_weak
)
891 else if (info
->shared
)
895 if (! ((*info
->callbacks
->undefined_symbol
)
896 (info
, h
->root
.root
.string
, input_bfd
,
897 input_section
, rel
->r_offset
)))
908 /* Relocation is to the entry for this symbol in the global
912 sgot
= bfd_get_section_by_name (dynobj
, ".got");
913 BFD_ASSERT (sgot
!= NULL
);
918 BFD_ASSERT (h
->got_offset
!= (bfd_vma
) -1);
919 relocation
= sgot
->output_offset
+ h
->got_offset
;
925 BFD_ASSERT (local_got_offsets
!= NULL
926 && local_got_offsets
[r_symndx
] != (bfd_vma
) -1);
928 off
= local_got_offsets
[r_symndx
];
930 /* The offset must always be a multiple of 4. We use
931 the least significant bit to record whether we have
932 already generated the necessary reloc. */
938 Elf_Internal_Rela outrel
;
940 bfd_put_32 (output_bfd
, relocation
, sgot
->contents
+ off
);
942 srelgot
= bfd_get_section_by_name (dynobj
, ".rela.got");
943 BFD_ASSERT (srelgot
!= NULL
);
945 outrel
.r_offset
= (sgot
->output_section
->vma
946 + sgot
->output_offset
948 outrel
.r_info
= ELF32_R_INFO (0, R_SPARC_RELATIVE
);
950 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
,
951 (((Elf32_External_Rela
*)
953 + srelgot
->reloc_count
));
954 ++srelgot
->reloc_count
;
956 local_got_offsets
[r_symndx
] |= 1;
959 relocation
= sgot
->output_offset
+ off
;
965 /* Relocation is to the entry for this symbol in the
966 procedure linkage table. */
969 splt
= bfd_get_section_by_name (dynobj
, ".plt");
970 BFD_ASSERT (splt
!= NULL
);
973 BFD_ASSERT (h
!= NULL
&& h
->plt_offset
!= (bfd_vma
) -1);
974 relocation
= (splt
->output_section
->vma
975 + splt
->output_offset
982 && strcmp (h
->root
.root
.string
, "_GLOBAL_OFFSET_TABLE_") == 0)
991 case R_SPARC_WDISP30
:
992 case R_SPARC_WDISP22
:
999 && (input_section
->flags
& SEC_ALLOC
) != 0)
1001 Elf_Internal_Rela outrel
;
1003 /* When generating a shared object, these relocations
1004 are copied into the output file to be resolved at run
1011 name
= (elf_string_from_elf_section
1013 elf_elfheader (input_bfd
)->e_shstrndx
,
1014 elf_section_data (input_section
)->rel_hdr
.sh_name
));
1018 BFD_ASSERT (strncmp (name
, ".rela", 5) == 0
1019 && strcmp (bfd_get_section_name (input_bfd
,
1023 sreloc
= bfd_get_section_by_name (dynobj
, name
);
1024 BFD_ASSERT (sreloc
!= NULL
);
1027 outrel
.r_offset
= (rel
->r_offset
1028 + input_section
->output_section
->vma
1029 + input_section
->output_offset
);
1032 BFD_ASSERT (h
->dynindx
!= (bfd_vma
) -1);
1033 outrel
.r_info
= ELF32_R_INFO (h
->dynindx
, r_type
);
1034 outrel
.r_addend
= 0;
1040 sym
= local_syms
+ r_symndx
;
1042 /* If this isn't a section symbol, we need to map it
1043 to something that is going to be put into the
1044 dynamic symbols. The case will probably never
1046 BFD_ASSERT (ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
);
1048 sec
= local_sections
[r_symndx
];
1049 if (sec
!= NULL
&& bfd_is_abs_section (sec
))
1051 else if (sec
== NULL
|| sec
->owner
== NULL
)
1053 bfd_set_error (bfd_error_bad_value
);
1058 indx
= sec
->output_section
->target_index
;
1063 outrel
.r_info
= ELF32_R_INFO (indx
, r_type
);
1064 outrel
.r_addend
= sec
->output_offset
+ sym
->st_value
;
1067 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
,
1068 (((Elf32_External_Rela
*)
1070 + sreloc
->reloc_count
));
1071 ++sreloc
->reloc_count
;
1073 /* This reloc will be computed at runtime, so there's no
1074 need to do anything now. */
1082 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
1083 contents
, rel
->r_offset
,
1084 relocation
, rel
->r_addend
);
1086 if (r
!= bfd_reloc_ok
)
1091 case bfd_reloc_outofrange
:
1093 case bfd_reloc_overflow
:
1098 name
= h
->root
.root
.string
;
1101 name
= output_names
+ sym
->st_name
;
1105 name
= bfd_section_name (input_bfd
, sec
);
1107 if (! ((*info
->callbacks
->reloc_overflow
)
1108 (info
, name
, howto
->name
, (bfd_vma
) 0,
1109 input_bfd
, input_section
, rel
->r_offset
)))
1120 /* Finish up dynamic symbol handling. We set the contents of various
1121 dynamic sections here. */
1124 elf32_sparc_finish_dynamic_symbol (output_bfd
, info
, h
, sym
)
1126 struct bfd_link_info
*info
;
1127 struct elf_link_hash_entry
*h
;
1128 Elf_Internal_Sym
*sym
;
1132 dynobj
= elf_hash_table (info
)->dynobj
;
1134 if (h
->plt_offset
!= (bfd_vma
) -1)
1138 Elf_Internal_Rela rela
;
1140 /* This symbol has an entry in the procedure linkage table. Set
1143 BFD_ASSERT (h
->dynindx
!= -1);
1145 splt
= bfd_get_section_by_name (dynobj
, ".plt");
1146 srela
= bfd_get_section_by_name (dynobj
, ".rela.plt");
1147 BFD_ASSERT (splt
!= NULL
&& srela
!= NULL
);
1149 /* Fill in the entry in the procedure linkage table. */
1150 bfd_put_32 (output_bfd
,
1151 PLT_ENTRY_WORD0
+ h
->plt_offset
,
1152 splt
->contents
+ h
->plt_offset
);
1153 bfd_put_32 (output_bfd
,
1155 + (((- (h
->plt_offset
+ 4)) >> 2) & 0x3fffff)),
1156 splt
->contents
+ h
->plt_offset
+ 4);
1157 bfd_put_32 (output_bfd
, PLT_ENTRY_WORD2
,
1158 splt
->contents
+ h
->plt_offset
+ 8);
1160 /* Fill in the entry in the .rela.plt section. */
1161 rela
.r_offset
= (splt
->output_section
->vma
1162 + splt
->output_offset
1164 rela
.r_info
= ELF32_R_INFO (h
->dynindx
, R_SPARC_JMP_SLOT
);
1166 bfd_elf32_swap_reloca_out (output_bfd
, &rela
,
1167 ((Elf32_External_Rela
*) srela
->contents
1168 + h
->plt_offset
/ PLT_ENTRY_SIZE
- 4));
1170 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
1172 /* Mark the symbol as undefined, rather than as defined in
1173 the .plt section. Leave the value alone. */
1174 sym
->st_shndx
= SHN_UNDEF
;
1178 if (h
->got_offset
!= (bfd_vma
) -1)
1182 Elf_Internal_Rela rela
;
1184 /* This symbol has an entry in the global offset table. Set it
1187 BFD_ASSERT (h
->dynindx
!= -1);
1189 sgot
= bfd_get_section_by_name (dynobj
, ".got");
1190 srela
= bfd_get_section_by_name (dynobj
, ".rela.got");
1191 BFD_ASSERT (sgot
!= NULL
&& srela
!= NULL
);
1193 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
+ h
->got_offset
);
1195 rela
.r_offset
= (sgot
->output_section
->vma
1196 + sgot
->output_offset
1198 rela
.r_info
= ELF32_R_INFO (h
->dynindx
, R_SPARC_GLOB_DAT
);
1200 bfd_elf32_swap_reloca_out (output_bfd
, &rela
,
1201 ((Elf32_External_Rela
*) srela
->contents
1202 + srela
->reloc_count
));
1203 ++srela
->reloc_count
;
1206 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_COPY
) != 0)
1209 Elf_Internal_Rela rela
;
1211 /* This symbols needs a copy reloc. Set it up. */
1213 BFD_ASSERT (h
->dynindx
!= -1);
1215 s
= bfd_get_section_by_name (h
->root
.u
.def
.section
->owner
,
1217 BFD_ASSERT (s
!= NULL
);
1219 rela
.r_offset
= (h
->root
.u
.def
.value
1220 + h
->root
.u
.def
.section
->output_section
->vma
1221 + h
->root
.u
.def
.section
->output_offset
);
1222 rela
.r_info
= ELF32_R_INFO (h
->dynindx
, R_SPARC_COPY
);
1224 bfd_elf32_swap_reloca_out (output_bfd
, &rela
,
1225 ((Elf32_External_Rela
*) s
->contents
1230 /* Mark some specially defined symbols as absolute. */
1231 if (strcmp (h
->root
.root
.string
, "_DYNAMIC") == 0
1232 || strcmp (h
->root
.root
.string
, "_GLOBAL_OFFSET_TABLE_") == 0
1233 || strcmp (h
->root
.root
.string
, "_PROCEDURE_LINKAGE_TABLE_") == 0)
1234 sym
->st_shndx
= SHN_ABS
;
1239 /* Finish up the dynamic sections. */
1242 elf32_sparc_finish_dynamic_sections (output_bfd
, info
)
1244 struct bfd_link_info
*info
;
1250 Elf32_External_Dyn
*dyncon
, *dynconend
;
1252 dynobj
= elf_hash_table (info
)->dynobj
;
1254 splt
= bfd_get_section_by_name (dynobj
, ".plt");
1255 sgot
= bfd_get_section_by_name (dynobj
, ".got");
1256 sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
1257 BFD_ASSERT (splt
!= NULL
&& sgot
!= NULL
&& sdyn
!= NULL
);
1259 dyncon
= (Elf32_External_Dyn
*) sdyn
->contents
;
1260 dynconend
= (Elf32_External_Dyn
*) (sdyn
->contents
+ sdyn
->_raw_size
);
1261 for (; dyncon
< dynconend
; dyncon
++)
1263 Elf_Internal_Dyn dyn
;
1267 bfd_elf32_swap_dyn_in (dynobj
, dyncon
, &dyn
);
1271 case DT_PLTGOT
: name
= ".plt"; size
= false; break;
1272 case DT_PLTRELSZ
: name
= ".rela.plt"; size
= true; break;
1273 case DT_JMPREL
: name
= ".rela.plt"; size
= false; break;
1274 default: name
= NULL
; size
= false; break;
1281 s
= bfd_get_section_by_name (output_bfd
, name
);
1282 BFD_ASSERT (s
!= NULL
);
1284 dyn
.d_un
.d_ptr
= s
->vma
;
1287 if (s
->_cooked_size
!= 0)
1288 dyn
.d_un
.d_val
= s
->_cooked_size
;
1290 dyn
.d_un
.d_val
= s
->_raw_size
;
1292 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
1296 /* Clear the first four entries in the procedure linkage table, and
1297 put a nop in the last four bytes. */
1298 if (splt
->_raw_size
> 0)
1300 memset (splt
->contents
, 0, 4 * PLT_ENTRY_SIZE
);
1301 bfd_put_32 (output_bfd
, SPARC_NOP
,
1302 splt
->contents
+ splt
->_raw_size
- 4);
1305 /* Set the first entry in the global offset table to the address of
1306 the dynamic section. */
1307 if (sgot
->_raw_size
> 0)
1308 bfd_put_32 (output_bfd
,
1309 sdyn
->output_section
->vma
+ sdyn
->output_offset
,
1312 elf_section_data (sgot
->output_section
)->this_hdr
.sh_entsize
= 4;
1313 elf_section_data (splt
->output_section
)->this_hdr
.sh_entsize
=
1319 #define TARGET_BIG_SYM bfd_elf32_sparc_vec
1320 #define TARGET_BIG_NAME "elf32-sparc"
1321 #define ELF_ARCH bfd_arch_sparc
1322 #define ELF_MACHINE_CODE EM_SPARC
1323 #define ELF_MAXPAGESIZE 0x10000
1324 #define elf_backend_create_dynamic_sections \
1325 elf32_sparc_create_dynamic_sections
1326 #define elf_backend_check_relocs elf32_sparc_check_relocs
1327 #define elf_backend_adjust_dynamic_symbol \
1328 elf32_sparc_adjust_dynamic_symbol
1329 #define elf_backend_size_dynamic_sections \
1330 elf32_sparc_size_dynamic_sections
1331 #define elf_backend_relocate_section elf32_sparc_relocate_section
1332 #define elf_backend_finish_dynamic_symbol \
1333 elf32_sparc_finish_dynamic_symbol
1334 #define elf_backend_finish_dynamic_sections \
1335 elf32_sparc_finish_dynamic_sections
1337 #include "elf32-target.h"