* subsegs.c (section_symbol): Use the symbol, not the section, name.
[binutils.git] / bfd / elf32-sparc.c
blob07137baef33d169d618a2b243d316906d94e5042
1 /* SPARC-specific support for 32-bit ELF
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
3 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 #include "bfd.h"
22 #include "sysdep.h"
23 #include "bfdlink.h"
24 #include "libbfd.h"
25 #include "elf-bfd.h"
26 #include "elf/sparc.h"
27 #include "opcode/sparc.h"
29 static reloc_howto_type *elf32_sparc_reloc_type_lookup
30 PARAMS ((bfd *, bfd_reloc_code_real_type));
31 static void elf32_sparc_info_to_howto
32 PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
33 static bfd_boolean elf32_sparc_check_relocs
34 PARAMS ((bfd *, struct bfd_link_info *, asection *,
35 const Elf_Internal_Rela *));
36 static bfd_boolean elf32_sparc_adjust_dynamic_symbol
37 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
38 static bfd_boolean elf32_sparc_size_dynamic_sections
39 PARAMS ((bfd *, struct bfd_link_info *));
40 static bfd_boolean elf32_sparc_relax_section
41 PARAMS ((bfd *, asection *, struct bfd_link_info *, bfd_boolean *));
42 static bfd_boolean elf32_sparc_relocate_section
43 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
44 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
45 static bfd_boolean elf32_sparc_finish_dynamic_symbol
46 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
47 Elf_Internal_Sym *));
48 static bfd_boolean elf32_sparc_finish_dynamic_sections
49 PARAMS ((bfd *, struct bfd_link_info *));
50 static bfd_boolean elf32_sparc_merge_private_bfd_data
51 PARAMS ((bfd *, bfd *));
52 static bfd_boolean elf32_sparc_object_p
53 PARAMS ((bfd *));
54 static void elf32_sparc_final_write_processing
55 PARAMS ((bfd *, bfd_boolean));
56 static enum elf_reloc_type_class elf32_sparc_reloc_type_class
57 PARAMS ((const Elf_Internal_Rela *));
58 static asection * elf32_sparc_gc_mark_hook
59 PARAMS ((asection *, struct bfd_link_info *, Elf_Internal_Rela *,
60 struct elf_link_hash_entry *, Elf_Internal_Sym *));
61 static bfd_boolean elf32_sparc_gc_sweep_hook
62 PARAMS ((bfd *, struct bfd_link_info *, asection *,
63 const Elf_Internal_Rela *));
65 /* The relocation "howto" table. */
67 static bfd_reloc_status_type sparc_elf_notsupported_reloc
68 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
69 static bfd_reloc_status_type sparc_elf_wdisp16_reloc
70 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
72 reloc_howto_type _bfd_sparc_elf_howto_table[] =
74 HOWTO(R_SPARC_NONE, 0,0, 0,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_NONE", FALSE,0,0x00000000,TRUE),
75 HOWTO(R_SPARC_8, 0,0, 8,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_8", FALSE,0,0x000000ff,TRUE),
76 HOWTO(R_SPARC_16, 0,1,16,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_16", FALSE,0,0x0000ffff,TRUE),
77 HOWTO(R_SPARC_32, 0,2,32,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_32", FALSE,0,0xffffffff,TRUE),
78 HOWTO(R_SPARC_DISP8, 0,0, 8,TRUE, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_DISP8", FALSE,0,0x000000ff,TRUE),
79 HOWTO(R_SPARC_DISP16, 0,1,16,TRUE, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_DISP16", FALSE,0,0x0000ffff,TRUE),
80 HOWTO(R_SPARC_DISP32, 0,2,32,TRUE, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_DISP32", FALSE,0,0xffffffff,TRUE),
81 HOWTO(R_SPARC_WDISP30, 2,2,30,TRUE, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_WDISP30", FALSE,0,0x3fffffff,TRUE),
82 HOWTO(R_SPARC_WDISP22, 2,2,22,TRUE, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_WDISP22", FALSE,0,0x003fffff,TRUE),
83 HOWTO(R_SPARC_HI22, 10,2,22,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_HI22", FALSE,0,0x003fffff,TRUE),
84 HOWTO(R_SPARC_22, 0,2,22,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_22", FALSE,0,0x003fffff,TRUE),
85 HOWTO(R_SPARC_13, 0,2,13,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_13", FALSE,0,0x00001fff,TRUE),
86 HOWTO(R_SPARC_LO10, 0,2,10,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_LO10", FALSE,0,0x000003ff,TRUE),
87 HOWTO(R_SPARC_GOT10, 0,2,10,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_GOT10", FALSE,0,0x000003ff,TRUE),
88 HOWTO(R_SPARC_GOT13, 0,2,13,FALSE,0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_GOT13", FALSE,0,0x00001fff,TRUE),
89 HOWTO(R_SPARC_GOT22, 10,2,22,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_GOT22", FALSE,0,0x003fffff,TRUE),
90 HOWTO(R_SPARC_PC10, 0,2,10,TRUE, 0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_PC10", FALSE,0,0x000003ff,TRUE),
91 HOWTO(R_SPARC_PC22, 10,2,22,TRUE, 0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_PC22", FALSE,0,0x003fffff,TRUE),
92 HOWTO(R_SPARC_WPLT30, 2,2,30,TRUE, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_WPLT30", FALSE,0,0x3fffffff,TRUE),
93 HOWTO(R_SPARC_COPY, 0,0,00,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_COPY", FALSE,0,0x00000000,TRUE),
94 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),
95 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),
96 HOWTO(R_SPARC_RELATIVE, 0,0,00,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_RELATIVE",FALSE,0,0x00000000,TRUE),
97 HOWTO(R_SPARC_UA32, 0,2,32,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_UA32", FALSE,0,0xffffffff,TRUE),
98 HOWTO(R_SPARC_PLT32, 0,0,00,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_PLT32", FALSE,0,0xffffffff,TRUE),
99 HOWTO(R_SPARC_HIPLT22, 0,0,00,FALSE,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_HIPLT22", FALSE,0,0x00000000,TRUE),
100 HOWTO(R_SPARC_LOPLT10, 0,0,00,FALSE,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_LOPLT10", FALSE,0,0x00000000,TRUE),
101 HOWTO(R_SPARC_PCPLT32, 0,0,00,FALSE,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_PCPLT32", FALSE,0,0x00000000,TRUE),
102 HOWTO(R_SPARC_PCPLT22, 0,0,00,FALSE,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_PCPLT22", FALSE,0,0x00000000,TRUE),
103 HOWTO(R_SPARC_PCPLT10, 0,0,00,FALSE,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_PCPLT10", FALSE,0,0x00000000,TRUE),
104 HOWTO(R_SPARC_10, 0,2,10,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_10", FALSE,0,0x000003ff,TRUE),
105 HOWTO(R_SPARC_11, 0,2,11,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_11", FALSE,0,0x000007ff,TRUE),
106 /* These are for sparc64 in a 64 bit environment.
107 Values need to be here because the table is indexed by reloc number. */
108 HOWTO(R_SPARC_64, 0,0,00,FALSE,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_64", FALSE,0,0x00000000,TRUE),
109 HOWTO(R_SPARC_OLO10, 0,0,00,FALSE,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_OLO10", FALSE,0,0x00000000,TRUE),
110 HOWTO(R_SPARC_HH22, 0,0,00,FALSE,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_HH22", FALSE,0,0x00000000,TRUE),
111 HOWTO(R_SPARC_HM10, 0,0,00,FALSE,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_HM10", FALSE,0,0x00000000,TRUE),
112 HOWTO(R_SPARC_LM22, 0,0,00,FALSE,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_LM22", FALSE,0,0x00000000,TRUE),
113 HOWTO(R_SPARC_PC_HH22, 0,0,00,FALSE,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_PC_HH22", FALSE,0,0x00000000,TRUE),
114 HOWTO(R_SPARC_PC_HM10, 0,0,00,FALSE,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_PC_HM10", FALSE,0,0x00000000,TRUE),
115 HOWTO(R_SPARC_PC_LM22, 0,0,00,FALSE,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_PC_LM22", FALSE,0,0x00000000,TRUE),
116 /* End sparc64 in 64 bit environment values.
117 The following are for sparc64 in a 32 bit environment. */
118 HOWTO(R_SPARC_WDISP16, 2,2,16,TRUE, 0,complain_overflow_signed, sparc_elf_wdisp16_reloc,"R_SPARC_WDISP16", FALSE,0,0x00000000,TRUE),
119 HOWTO(R_SPARC_WDISP19, 2,2,19,TRUE, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_WDISP19", FALSE,0,0x0007ffff,TRUE),
120 HOWTO(R_SPARC_UNUSED_42, 0,0, 0,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_UNUSED_42",FALSE,0,0x00000000,TRUE),
121 HOWTO(R_SPARC_7, 0,2, 7,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_7", FALSE,0,0x0000007f,TRUE),
122 HOWTO(R_SPARC_5, 0,2, 5,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_5", FALSE,0,0x0000001f,TRUE),
123 HOWTO(R_SPARC_6, 0,2, 6,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_6", FALSE,0,0x0000003f,TRUE),
124 HOWTO(R_SPARC_NONE, 0,0, 0,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_NONE", FALSE,0,0x00000000,TRUE),
125 HOWTO(R_SPARC_NONE, 0,0, 0,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_NONE", FALSE,0,0x00000000,TRUE),
126 HOWTO(R_SPARC_NONE, 0,0, 0,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_NONE", FALSE,0,0x00000000,TRUE),
127 HOWTO(R_SPARC_NONE, 0,0, 0,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_NONE", FALSE,0,0x00000000,TRUE),
128 HOWTO(R_SPARC_NONE, 0,0, 0,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_NONE", FALSE,0,0x00000000,TRUE),
129 HOWTO(R_SPARC_NONE, 0,0, 0,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_NONE", FALSE,0,0x00000000,TRUE),
130 HOWTO(R_SPARC_NONE, 0,0, 0,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_NONE", FALSE,0,0x00000000,TRUE),
131 HOWTO(R_SPARC_NONE, 0,0, 0,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_NONE", FALSE,0,0x00000000,TRUE),
132 HOWTO(R_SPARC_UA64, 0,0, 0,FALSE,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_UA64", FALSE,0,0x00000000,TRUE),
133 HOWTO(R_SPARC_UA16, 0,1,16,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_UA16", FALSE,0,0x0000ffff,TRUE),
134 HOWTO(R_SPARC_REV32, 0,2,32,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_REV32", FALSE,0,0xffffffff,TRUE),
136 static reloc_howto_type elf32_sparc_vtinherit_howto =
137 HOWTO (R_SPARC_GNU_VTINHERIT, 0,2,0,FALSE,0,complain_overflow_dont, NULL, "R_SPARC_GNU_VTINHERIT", FALSE,0, 0, FALSE);
138 static reloc_howto_type elf32_sparc_vtentry_howto =
139 HOWTO (R_SPARC_GNU_VTENTRY, 0,2,0,FALSE,0,complain_overflow_dont, _bfd_elf_rel_vtable_reloc_fn,"R_SPARC_GNU_VTENTRY", FALSE,0,0, FALSE);
141 struct elf_reloc_map {
142 bfd_reloc_code_real_type bfd_reloc_val;
143 unsigned char elf_reloc_val;
146 static const struct elf_reloc_map sparc_reloc_map[] =
148 { BFD_RELOC_NONE, R_SPARC_NONE, },
149 { BFD_RELOC_16, R_SPARC_16, },
150 { BFD_RELOC_16_PCREL, R_SPARC_DISP16 },
151 { BFD_RELOC_8, R_SPARC_8 },
152 { BFD_RELOC_8_PCREL, R_SPARC_DISP8 },
153 { BFD_RELOC_CTOR, R_SPARC_32 },
154 { BFD_RELOC_32, R_SPARC_32 },
155 { BFD_RELOC_32_PCREL, R_SPARC_DISP32 },
156 { BFD_RELOC_HI22, R_SPARC_HI22 },
157 { BFD_RELOC_LO10, R_SPARC_LO10, },
158 { BFD_RELOC_32_PCREL_S2, R_SPARC_WDISP30 },
159 { BFD_RELOC_SPARC_PLT32, R_SPARC_PLT32 },
160 { BFD_RELOC_SPARC22, R_SPARC_22 },
161 { BFD_RELOC_SPARC13, R_SPARC_13 },
162 { BFD_RELOC_SPARC_GOT10, R_SPARC_GOT10 },
163 { BFD_RELOC_SPARC_GOT13, R_SPARC_GOT13 },
164 { BFD_RELOC_SPARC_GOT22, R_SPARC_GOT22 },
165 { BFD_RELOC_SPARC_PC10, R_SPARC_PC10 },
166 { BFD_RELOC_SPARC_PC22, R_SPARC_PC22 },
167 { BFD_RELOC_SPARC_WPLT30, R_SPARC_WPLT30 },
168 { BFD_RELOC_SPARC_COPY, R_SPARC_COPY },
169 { BFD_RELOC_SPARC_GLOB_DAT, R_SPARC_GLOB_DAT },
170 { BFD_RELOC_SPARC_JMP_SLOT, R_SPARC_JMP_SLOT },
171 { BFD_RELOC_SPARC_RELATIVE, R_SPARC_RELATIVE },
172 { BFD_RELOC_SPARC_WDISP22, R_SPARC_WDISP22 },
173 { BFD_RELOC_SPARC_UA16, R_SPARC_UA16 },
174 { BFD_RELOC_SPARC_UA32, R_SPARC_UA32 },
175 { BFD_RELOC_SPARC_UA64, R_SPARC_UA64 },
176 { BFD_RELOC_SPARC_10, R_SPARC_10 },
177 { BFD_RELOC_SPARC_11, R_SPARC_11 },
178 { BFD_RELOC_SPARC_64, R_SPARC_64 },
179 { BFD_RELOC_SPARC_OLO10, R_SPARC_OLO10 },
180 { BFD_RELOC_SPARC_HH22, R_SPARC_HH22 },
181 { BFD_RELOC_SPARC_HM10, R_SPARC_HM10 },
182 { BFD_RELOC_SPARC_LM22, R_SPARC_LM22 },
183 { BFD_RELOC_SPARC_PC_HH22, R_SPARC_PC_HH22 },
184 { BFD_RELOC_SPARC_PC_HM10, R_SPARC_PC_HM10 },
185 { BFD_RELOC_SPARC_PC_LM22, R_SPARC_PC_LM22 },
186 { BFD_RELOC_SPARC_WDISP16, R_SPARC_WDISP16 },
187 { BFD_RELOC_SPARC_WDISP19, R_SPARC_WDISP19 },
188 { BFD_RELOC_SPARC_7, R_SPARC_7 },
189 { BFD_RELOC_SPARC_5, R_SPARC_5 },
190 { BFD_RELOC_SPARC_6, R_SPARC_6 },
191 { BFD_RELOC_SPARC_REV32, R_SPARC_REV32 },
192 { BFD_RELOC_VTABLE_INHERIT, R_SPARC_GNU_VTINHERIT },
193 { BFD_RELOC_VTABLE_ENTRY, R_SPARC_GNU_VTENTRY },
196 static reloc_howto_type *
197 elf32_sparc_reloc_type_lookup (abfd, code)
198 bfd *abfd ATTRIBUTE_UNUSED;
199 bfd_reloc_code_real_type code;
201 unsigned int i;
203 switch (code)
205 case BFD_RELOC_VTABLE_INHERIT:
206 return &elf32_sparc_vtinherit_howto;
208 case BFD_RELOC_VTABLE_ENTRY:
209 return &elf32_sparc_vtentry_howto;
211 default:
212 for (i = 0; i < sizeof (sparc_reloc_map) / sizeof (struct elf_reloc_map); i++)
214 if (sparc_reloc_map[i].bfd_reloc_val == code)
215 return &_bfd_sparc_elf_howto_table[(int) sparc_reloc_map[i].elf_reloc_val];
218 bfd_set_error (bfd_error_bad_value);
219 return NULL;
222 /* We need to use ELF32_R_TYPE so we have our own copy of this function,
223 and elf64-sparc.c has its own copy. */
225 static void
226 elf32_sparc_info_to_howto (abfd, cache_ptr, dst)
227 bfd *abfd ATTRIBUTE_UNUSED;
228 arelent *cache_ptr;
229 Elf_Internal_Rela *dst;
231 switch (ELF32_R_TYPE(dst->r_info))
233 case R_SPARC_GNU_VTINHERIT:
234 cache_ptr->howto = &elf32_sparc_vtinherit_howto;
235 break;
237 case R_SPARC_GNU_VTENTRY:
238 cache_ptr->howto = &elf32_sparc_vtentry_howto;
239 break;
241 default:
242 BFD_ASSERT (ELF32_R_TYPE(dst->r_info) < (unsigned int) R_SPARC_max_std);
243 cache_ptr->howto = &_bfd_sparc_elf_howto_table[ELF32_R_TYPE(dst->r_info)];
247 /* For unsupported relocs. */
249 static bfd_reloc_status_type
250 sparc_elf_notsupported_reloc (abfd,
251 reloc_entry,
252 symbol,
253 data,
254 input_section,
255 output_bfd,
256 error_message)
257 bfd *abfd ATTRIBUTE_UNUSED;
258 arelent *reloc_entry ATTRIBUTE_UNUSED;
259 asymbol *symbol ATTRIBUTE_UNUSED;
260 PTR data ATTRIBUTE_UNUSED;
261 asection *input_section ATTRIBUTE_UNUSED;
262 bfd *output_bfd ATTRIBUTE_UNUSED;
263 char **error_message ATTRIBUTE_UNUSED;
265 return bfd_reloc_notsupported;
268 /* Handle the WDISP16 reloc. */
270 static bfd_reloc_status_type
271 sparc_elf_wdisp16_reloc (abfd,
272 reloc_entry,
273 symbol,
274 data,
275 input_section,
276 output_bfd,
277 error_message)
278 bfd *abfd;
279 arelent *reloc_entry;
280 asymbol *symbol;
281 PTR data;
282 asection *input_section;
283 bfd *output_bfd;
284 char **error_message ATTRIBUTE_UNUSED;
286 bfd_vma relocation;
287 bfd_vma x;
289 if (output_bfd != (bfd *) NULL
290 && (symbol->flags & BSF_SECTION_SYM) == 0
291 && (! reloc_entry->howto->partial_inplace
292 || reloc_entry->addend == 0))
294 reloc_entry->address += input_section->output_offset;
295 return bfd_reloc_ok;
298 if (output_bfd != NULL)
299 return bfd_reloc_continue;
301 if (reloc_entry->address > input_section->_cooked_size)
302 return bfd_reloc_outofrange;
304 relocation = (symbol->value
305 + symbol->section->output_section->vma
306 + symbol->section->output_offset);
307 relocation += reloc_entry->addend;
308 relocation -= (input_section->output_section->vma
309 + input_section->output_offset);
310 relocation -= reloc_entry->address;
312 x = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
313 x |= ((((relocation >> 2) & 0xc000) << 6)
314 | ((relocation >> 2) & 0x3fff));
315 bfd_put_32 (abfd, x, (bfd_byte *) data + reloc_entry->address);
317 if ((bfd_signed_vma) relocation < - 0x40000
318 || (bfd_signed_vma) relocation > 0x3ffff)
319 return bfd_reloc_overflow;
320 else
321 return bfd_reloc_ok;
324 /* Functions for the SPARC ELF linker. */
326 /* The name of the dynamic interpreter. This is put in the .interp
327 section. */
329 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
331 /* The nop opcode we use. */
333 #define SPARC_NOP 0x01000000
335 /* The size in bytes of an entry in the procedure linkage table. */
337 #define PLT_ENTRY_SIZE 12
339 /* The first four entries in a procedure linkage table are reserved,
340 and the initial contents are unimportant (we zero them out).
341 Subsequent entries look like this. See the SVR4 ABI SPARC
342 supplement to see how this works. */
344 /* sethi %hi(.-.plt0),%g1. We fill in the address later. */
345 #define PLT_ENTRY_WORD0 0x03000000
346 /* b,a .plt0. We fill in the offset later. */
347 #define PLT_ENTRY_WORD1 0x30800000
348 /* nop. */
349 #define PLT_ENTRY_WORD2 SPARC_NOP
351 /* Look through the relocs for a section during the first phase, and
352 allocate space in the global offset table or procedure linkage
353 table. */
355 static bfd_boolean
356 elf32_sparc_check_relocs (abfd, info, sec, relocs)
357 bfd *abfd;
358 struct bfd_link_info *info;
359 asection *sec;
360 const Elf_Internal_Rela *relocs;
362 bfd *dynobj;
363 Elf_Internal_Shdr *symtab_hdr;
364 struct elf_link_hash_entry **sym_hashes;
365 bfd_vma *local_got_offsets;
366 const Elf_Internal_Rela *rel;
367 const Elf_Internal_Rela *rel_end;
368 asection *sgot;
369 asection *srelgot;
370 asection *sreloc;
372 if (info->relocateable)
373 return TRUE;
375 dynobj = elf_hash_table (info)->dynobj;
376 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
377 sym_hashes = elf_sym_hashes (abfd);
378 local_got_offsets = elf_local_got_offsets (abfd);
380 sgot = NULL;
381 srelgot = NULL;
382 sreloc = NULL;
384 rel_end = relocs + sec->reloc_count;
385 for (rel = relocs; rel < rel_end; rel++)
387 unsigned long r_symndx;
388 struct elf_link_hash_entry *h;
390 r_symndx = ELF32_R_SYM (rel->r_info);
391 if (r_symndx < symtab_hdr->sh_info)
392 h = NULL;
393 else
394 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
396 switch (ELF32_R_TYPE (rel->r_info))
398 case R_SPARC_GOT10:
399 case R_SPARC_GOT13:
400 case R_SPARC_GOT22:
401 /* This symbol requires a global offset table entry. */
403 if (dynobj == NULL)
405 /* Create the .got section. */
406 elf_hash_table (info)->dynobj = dynobj = abfd;
407 if (! _bfd_elf_create_got_section (dynobj, info))
408 return FALSE;
411 if (sgot == NULL)
413 sgot = bfd_get_section_by_name (dynobj, ".got");
414 BFD_ASSERT (sgot != NULL);
417 if (srelgot == NULL
418 && (h != NULL || info->shared))
420 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
421 if (srelgot == NULL)
423 srelgot = bfd_make_section (dynobj, ".rela.got");
424 if (srelgot == NULL
425 || ! bfd_set_section_flags (dynobj, srelgot,
426 (SEC_ALLOC
427 | SEC_LOAD
428 | SEC_HAS_CONTENTS
429 | SEC_IN_MEMORY
430 | SEC_LINKER_CREATED
431 | SEC_READONLY))
432 || ! bfd_set_section_alignment (dynobj, srelgot, 2))
433 return FALSE;
437 if (h != NULL)
439 if (h->got.offset != (bfd_vma) -1)
441 /* We have already allocated space in the .got. */
442 break;
444 h->got.offset = sgot->_raw_size;
446 /* Make sure this symbol is output as a dynamic symbol. */
447 if (h->dynindx == -1)
449 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
450 return FALSE;
453 srelgot->_raw_size += sizeof (Elf32_External_Rela);
455 else
457 /* This is a global offset table entry for a local
458 symbol. */
459 if (local_got_offsets == NULL)
461 bfd_size_type size;
462 register unsigned int i;
464 size = symtab_hdr->sh_info;
465 size *= sizeof (bfd_vma);
466 local_got_offsets = (bfd_vma *) bfd_alloc (abfd, size);
467 if (local_got_offsets == NULL)
468 return FALSE;
469 elf_local_got_offsets (abfd) = local_got_offsets;
470 for (i = 0; i < symtab_hdr->sh_info; i++)
471 local_got_offsets[i] = (bfd_vma) -1;
473 if (local_got_offsets[r_symndx] != (bfd_vma) -1)
475 /* We have already allocated space in the .got. */
476 break;
478 local_got_offsets[r_symndx] = sgot->_raw_size;
480 if (info->shared)
482 /* If we are generating a shared object, we need to
483 output a R_SPARC_RELATIVE reloc so that the
484 dynamic linker can adjust this GOT entry. */
485 srelgot->_raw_size += sizeof (Elf32_External_Rela);
489 sgot->_raw_size += 4;
491 /* If the .got section is more than 0x1000 bytes, we add
492 0x1000 to the value of _GLOBAL_OFFSET_TABLE_, so that 13
493 bit relocations have a greater chance of working. */
494 if (sgot->_raw_size >= 0x1000
495 && elf_hash_table (info)->hgot->root.u.def.value == 0)
496 elf_hash_table (info)->hgot->root.u.def.value = 0x1000;
498 break;
500 case R_SPARC_PLT32:
501 case R_SPARC_WPLT30:
502 /* This symbol requires a procedure linkage table entry. We
503 actually build the entry in adjust_dynamic_symbol,
504 because this might be a case of linking PIC code without
505 linking in any dynamic objects, in which case we don't
506 need to generate a procedure linkage table after all. */
508 if (h == NULL)
510 /* The Solaris native assembler will generate a WPLT30
511 reloc for a local symbol if you assemble a call from
512 one section to another when using -K pic. We treat
513 it as WDISP30. */
514 if (ELF32_R_TYPE (rel->r_info) != R_SPARC_WPLT30)
515 goto r_sparc_plt32;
516 break;
519 /* Make sure this symbol is output as a dynamic symbol. */
520 if (h->dynindx == -1)
522 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
523 return FALSE;
526 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
528 if (ELF32_R_TYPE (rel->r_info) != R_SPARC_WPLT30)
529 goto r_sparc_plt32;
530 break;
532 case R_SPARC_PC10:
533 case R_SPARC_PC22:
534 if (h != NULL)
535 h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
537 if (h != NULL
538 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
539 break;
540 /* Fall through. */
541 case R_SPARC_DISP8:
542 case R_SPARC_DISP16:
543 case R_SPARC_DISP32:
544 case R_SPARC_WDISP30:
545 case R_SPARC_WDISP22:
546 case R_SPARC_WDISP19:
547 case R_SPARC_WDISP16:
548 if (h != NULL)
549 h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
551 /* If we are linking with -Bsymbolic, we do not need to copy
552 a PC relative reloc against a global symbol which is
553 defined in an object we are including in the link (i.e.,
554 DEF_REGULAR is set). FIXME: At this point we have not
555 seen all the input files, so it is possible that
556 DEF_REGULAR is not set now but will be set later (it is
557 never cleared). This needs to be handled as in
558 elf32-i386.c. */
559 if (h == NULL
560 || (info->symbolic
561 && (h->elf_link_hash_flags
562 & ELF_LINK_HASH_DEF_REGULAR) != 0))
563 break;
564 /* Fall through. */
565 case R_SPARC_8:
566 case R_SPARC_16:
567 case R_SPARC_32:
568 case R_SPARC_HI22:
569 case R_SPARC_22:
570 case R_SPARC_13:
571 case R_SPARC_LO10:
572 case R_SPARC_UA16:
573 case R_SPARC_UA32:
574 if (h != NULL)
575 h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
577 r_sparc_plt32:
578 if (info->shared && (sec->flags & SEC_ALLOC))
580 /* When creating a shared object, we must copy these
581 relocs into the output file. We create a reloc
582 section in dynobj and make room for the reloc. */
583 if (sreloc == NULL)
585 const char *name;
587 name = (bfd_elf_string_from_elf_section
588 (abfd,
589 elf_elfheader (abfd)->e_shstrndx,
590 elf_section_data (sec)->rel_hdr.sh_name));
591 if (name == NULL)
592 return FALSE;
594 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
595 && strcmp (bfd_get_section_name (abfd, sec),
596 name + 5) == 0);
598 sreloc = bfd_get_section_by_name (dynobj, name);
599 if (sreloc == NULL)
601 flagword flags;
603 sreloc = bfd_make_section (dynobj, name);
604 flags = (SEC_HAS_CONTENTS | SEC_READONLY
605 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
606 if ((sec->flags & SEC_ALLOC) != 0)
607 flags |= SEC_ALLOC | SEC_LOAD;
608 if (sreloc == NULL
609 || ! bfd_set_section_flags (dynobj, sreloc, flags)
610 || ! bfd_set_section_alignment (dynobj, sreloc, 2))
611 return FALSE;
613 if (sec->flags & SEC_READONLY)
614 info->flags |= DF_TEXTREL;
617 sreloc->_raw_size += sizeof (Elf32_External_Rela);
620 break;
622 case R_SPARC_GNU_VTINHERIT:
623 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
624 return FALSE;
625 break;
627 case R_SPARC_GNU_VTENTRY:
628 if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend))
629 return FALSE;
630 break;
632 default:
633 break;
637 return TRUE;
640 static asection *
641 elf32_sparc_gc_mark_hook (sec, info, rel, h, sym)
642 asection *sec;
643 struct bfd_link_info *info ATTRIBUTE_UNUSED;
644 Elf_Internal_Rela *rel;
645 struct elf_link_hash_entry *h;
646 Elf_Internal_Sym *sym;
648 if (h != NULL)
650 switch (ELF32_R_TYPE (rel->r_info))
652 case R_SPARC_GNU_VTINHERIT:
653 case R_SPARC_GNU_VTENTRY:
654 break;
656 default:
657 switch (h->root.type)
659 case bfd_link_hash_defined:
660 case bfd_link_hash_defweak:
661 return h->root.u.def.section;
663 case bfd_link_hash_common:
664 return h->root.u.c.p->section;
666 default:
667 break;
671 else
672 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
674 return NULL;
677 /* Update the got entry reference counts for the section being removed. */
678 static bfd_boolean
679 elf32_sparc_gc_sweep_hook (abfd, info, sec, relocs)
680 bfd *abfd;
681 struct bfd_link_info *info ATTRIBUTE_UNUSED;
682 asection *sec;
683 const Elf_Internal_Rela *relocs;
686 Elf_Internal_Shdr *symtab_hdr;
687 struct elf_link_hash_entry **sym_hashes;
688 bfd_signed_vma *local_got_refcounts;
689 const Elf_Internal_Rela *rel, *relend;
690 unsigned long r_symndx;
691 struct elf_link_hash_entry *h;
693 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
694 sym_hashes = elf_sym_hashes (abfd);
695 local_got_refcounts = elf_local_got_refcounts (abfd);
697 relend = relocs + sec->reloc_count;
698 for (rel = relocs; rel < relend; rel++)
699 switch (ELF32_R_TYPE (rel->r_info))
701 case R_SPARC_GOT10:
702 case R_SPARC_GOT13:
703 case R_SPARC_GOT22:
704 r_symndx = ELF32_R_SYM (rel->r_info);
705 if (r_symndx >= symtab_hdr->sh_info)
707 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
708 if (h->got.refcount > 0)
709 h->got.refcount--;
711 else
713 if (local_got_refcounts[r_symndx] > 0)
714 local_got_refcounts[r_symndx]--;
716 break;
718 case R_SPARC_PLT32:
719 case R_SPARC_HIPLT22:
720 case R_SPARC_LOPLT10:
721 case R_SPARC_PCPLT32:
722 case R_SPARC_PCPLT10:
723 r_symndx = ELF32_R_SYM (rel->r_info);
724 if (r_symndx >= symtab_hdr->sh_info)
726 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
727 if (h->plt.refcount > 0)
728 h->plt.refcount--;
730 break;
732 default:
733 break;
736 return TRUE;
739 /* Adjust a symbol defined by a dynamic object and referenced by a
740 regular object. The current definition is in some section of the
741 dynamic object, but we're not including those sections. We have to
742 change the definition to something the rest of the link can
743 understand. */
745 static bfd_boolean
746 elf32_sparc_adjust_dynamic_symbol (info, h)
747 struct bfd_link_info *info;
748 struct elf_link_hash_entry *h;
750 bfd *dynobj;
751 asection *s;
752 unsigned int power_of_two;
754 dynobj = elf_hash_table (info)->dynobj;
756 /* Make sure we know what is going on here. */
757 BFD_ASSERT (dynobj != NULL
758 && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
759 || h->weakdef != NULL
760 || ((h->elf_link_hash_flags
761 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
762 && (h->elf_link_hash_flags
763 & ELF_LINK_HASH_REF_REGULAR) != 0
764 && (h->elf_link_hash_flags
765 & ELF_LINK_HASH_DEF_REGULAR) == 0)));
767 /* If this is a function, put it in the procedure linkage table. We
768 will fill in the contents of the procedure linkage table later
769 (although we could actually do it here). The STT_NOTYPE
770 condition is a hack specifically for the Oracle libraries
771 delivered for Solaris; for some inexplicable reason, they define
772 some of their functions as STT_NOTYPE when they really should be
773 STT_FUNC. */
774 if (h->type == STT_FUNC
775 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0
776 || (h->type == STT_NOTYPE
777 && (h->root.type == bfd_link_hash_defined
778 || h->root.type == bfd_link_hash_defweak)
779 && (h->root.u.def.section->flags & SEC_CODE) != 0))
781 if (! elf_hash_table (info)->dynamic_sections_created
782 || ((!info->shared || info->symbolic || h->dynindx == -1)
783 && (h->elf_link_hash_flags
784 & ELF_LINK_HASH_DEF_REGULAR) != 0))
786 /* This case can occur if we saw a WPLT30 reloc in an input
787 file, but none of the input files were dynamic objects.
788 Or, when linking the main application or a -Bsymbolic
789 shared library against PIC code. Or when a global symbol
790 has been made private, e.g. via versioning.
792 In these cases we know what value the symbol will resolve
793 to, so we don't actually need to build a procedure linkage
794 table, and we can just do a WDISP30 reloc instead. */
796 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
797 return TRUE;
800 s = bfd_get_section_by_name (dynobj, ".plt");
801 BFD_ASSERT (s != NULL);
803 /* The first four entries in .plt are reserved. */
804 if (s->_raw_size == 0)
805 s->_raw_size = 4 * PLT_ENTRY_SIZE;
807 /* The procedure linkage table has a maximum size. */
808 if (s->_raw_size >= 0x400000)
810 bfd_set_error (bfd_error_bad_value);
811 return FALSE;
814 /* If this symbol is not defined in a regular file, and we are
815 not generating a shared library, then set the symbol to this
816 location in the .plt. This is required to make function
817 pointers compare as equal between the normal executable and
818 the shared library. */
819 if (! info->shared
820 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
822 h->root.u.def.section = s;
823 h->root.u.def.value = s->_raw_size;
826 h->plt.offset = s->_raw_size;
828 /* Make room for this entry. */
829 s->_raw_size += PLT_ENTRY_SIZE;
831 /* We also need to make an entry in the .rela.plt section. */
833 s = bfd_get_section_by_name (dynobj, ".rela.plt");
834 BFD_ASSERT (s != NULL);
835 s->_raw_size += sizeof (Elf32_External_Rela);
837 return TRUE;
840 /* If this is a weak symbol, and there is a real definition, the
841 processor independent code will have arranged for us to see the
842 real definition first, and we can just use the same value. */
843 if (h->weakdef != NULL)
845 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
846 || h->weakdef->root.type == bfd_link_hash_defweak);
847 h->root.u.def.section = h->weakdef->root.u.def.section;
848 h->root.u.def.value = h->weakdef->root.u.def.value;
849 return TRUE;
852 /* This is a reference to a symbol defined by a dynamic object which
853 is not a function. */
855 /* If we are creating a shared library, we must presume that the
856 only references to the symbol are via the global offset table.
857 For such cases we need not do anything here; the relocations will
858 be handled correctly by relocate_section. */
859 if (info->shared)
860 return TRUE;
862 /* If there are no references to this symbol that do not use the
863 GOT, we don't need to generate a copy reloc. */
864 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
865 return TRUE;
867 /* We must allocate the symbol in our .dynbss section, which will
868 become part of the .bss section of the executable. There will be
869 an entry for this symbol in the .dynsym section. The dynamic
870 object will contain position independent code, so all references
871 from the dynamic object to this symbol will go through the global
872 offset table. The dynamic linker will use the .dynsym entry to
873 determine the address it must put in the global offset table, so
874 both the dynamic object and the regular object will refer to the
875 same memory location for the variable. */
877 s = bfd_get_section_by_name (dynobj, ".dynbss");
878 BFD_ASSERT (s != NULL);
880 /* We must generate a R_SPARC_COPY reloc to tell the dynamic linker
881 to copy the initial value out of the dynamic object and into the
882 runtime process image. We need to remember the offset into the
883 .rel.bss section we are going to use. */
884 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
886 asection *srel;
888 srel = bfd_get_section_by_name (dynobj, ".rela.bss");
889 BFD_ASSERT (srel != NULL);
890 srel->_raw_size += sizeof (Elf32_External_Rela);
891 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
894 /* We need to figure out the alignment required for this symbol. I
895 have no idea how ELF linkers handle this. */
896 power_of_two = bfd_log2 (h->size);
897 if (power_of_two > 3)
898 power_of_two = 3;
900 /* Apply the required alignment. */
901 s->_raw_size = BFD_ALIGN (s->_raw_size,
902 (bfd_size_type) (1 << power_of_two));
903 if (power_of_two > bfd_get_section_alignment (dynobj, s))
905 if (! bfd_set_section_alignment (dynobj, s, power_of_two))
906 return FALSE;
909 /* Define the symbol as being at this point in the section. */
910 h->root.u.def.section = s;
911 h->root.u.def.value = s->_raw_size;
913 /* Increment the section size to make room for the symbol. */
914 s->_raw_size += h->size;
916 return TRUE;
919 /* Set the sizes of the dynamic sections. */
921 static bfd_boolean
922 elf32_sparc_size_dynamic_sections (output_bfd, info)
923 bfd *output_bfd ATTRIBUTE_UNUSED;
924 struct bfd_link_info *info;
926 bfd *dynobj;
927 asection *s;
928 bfd_boolean relplt;
930 dynobj = elf_hash_table (info)->dynobj;
931 BFD_ASSERT (dynobj != NULL);
933 if (elf_hash_table (info)->dynamic_sections_created)
935 /* Set the contents of the .interp section to the interpreter. */
936 if (! info->shared)
938 s = bfd_get_section_by_name (dynobj, ".interp");
939 BFD_ASSERT (s != NULL);
940 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
941 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
944 /* Make space for the trailing nop in .plt. */
945 s = bfd_get_section_by_name (dynobj, ".plt");
946 BFD_ASSERT (s != NULL);
947 if (s->_raw_size > 0)
948 s->_raw_size += 4;
950 else
952 /* We may have created entries in the .rela.got section.
953 However, if we are not creating the dynamic sections, we will
954 not actually use these entries. Reset the size of .rela.got,
955 which will cause it to get stripped from the output file
956 below. */
957 s = bfd_get_section_by_name (dynobj, ".rela.got");
958 if (s != NULL)
959 s->_raw_size = 0;
962 /* The check_relocs and adjust_dynamic_symbol entry points have
963 determined the sizes of the various dynamic sections. Allocate
964 memory for them. */
965 relplt = FALSE;
966 for (s = dynobj->sections; s != NULL; s = s->next)
968 const char *name;
969 bfd_boolean strip;
971 if ((s->flags & SEC_LINKER_CREATED) == 0)
972 continue;
974 /* It's OK to base decisions on the section name, because none
975 of the dynobj section names depend upon the input files. */
976 name = bfd_get_section_name (dynobj, s);
978 strip = FALSE;
980 if (strncmp (name, ".rela", 5) == 0)
982 if (s->_raw_size == 0)
984 /* If we don't need this section, strip it from the
985 output file. This is to handle .rela.bss and
986 .rel.plt. We must create it in
987 create_dynamic_sections, because it must be created
988 before the linker maps input sections to output
989 sections. The linker does that before
990 adjust_dynamic_symbol is called, and it is that
991 function which decides whether anything needs to go
992 into these sections. */
993 strip = TRUE;
995 else
997 if (strcmp (name, ".rela.plt") == 0)
998 relplt = TRUE;
1000 /* We use the reloc_count field as a counter if we need
1001 to copy relocs into the output file. */
1002 s->reloc_count = 0;
1005 else if (strcmp (name, ".plt") != 0
1006 && strcmp (name, ".got") != 0)
1008 /* It's not one of our sections, so don't allocate space. */
1009 continue;
1012 if (strip)
1014 _bfd_strip_section_from_output (info, s);
1015 continue;
1018 /* Allocate memory for the section contents. */
1019 /* FIXME: This should be a call to bfd_alloc not bfd_zalloc.
1020 Unused entries should be reclaimed before the section's contents
1021 are written out, but at the moment this does not happen. Thus in
1022 order to prevent writing out garbage, we initialise the section's
1023 contents to zero. */
1024 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
1025 if (s->contents == NULL && s->_raw_size != 0)
1026 return FALSE;
1029 if (elf_hash_table (info)->dynamic_sections_created)
1031 /* Add some entries to the .dynamic section. We fill in the
1032 values later, in elf32_sparc_finish_dynamic_sections, but we
1033 must add the entries now so that we get the correct size for
1034 the .dynamic section. The DT_DEBUG entry is filled in by the
1035 dynamic linker and used by the debugger. */
1036 #define add_dynamic_entry(TAG, VAL) \
1037 bfd_elf32_add_dynamic_entry (info, (bfd_vma) (TAG), (bfd_vma) (VAL))
1039 if (!info->shared)
1041 if (!add_dynamic_entry (DT_DEBUG, 0))
1042 return FALSE;
1045 if (relplt)
1047 if (!add_dynamic_entry (DT_PLTGOT, 0)
1048 || !add_dynamic_entry (DT_PLTRELSZ, 0)
1049 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
1050 || !add_dynamic_entry (DT_JMPREL, 0))
1051 return FALSE;
1054 if (!add_dynamic_entry (DT_RELA, 0)
1055 || !add_dynamic_entry (DT_RELASZ, 0)
1056 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
1057 return FALSE;
1059 if (info->flags & DF_TEXTREL)
1061 if (!add_dynamic_entry (DT_TEXTREL, 0))
1062 return FALSE;
1065 #undef add_dynamic_entry
1067 return TRUE;
1070 #define SET_SEC_DO_RELAX(section) do { elf_section_data(section)->tdata = (void *)1; } while (0)
1071 #define SEC_DO_RELAX(section) (elf_section_data(section)->tdata == (void *)1)
1073 static bfd_boolean
1074 elf32_sparc_relax_section (abfd, section, link_info, again)
1075 bfd *abfd ATTRIBUTE_UNUSED;
1076 asection *section ATTRIBUTE_UNUSED;
1077 struct bfd_link_info *link_info ATTRIBUTE_UNUSED;
1078 bfd_boolean *again;
1080 *again = FALSE;
1081 SET_SEC_DO_RELAX (section);
1082 return TRUE;
1085 /* This is the condition under which finish_dynamic_symbol will be called
1086 from elflink.h. If elflink.h doesn't call our finish_dynamic_symbol
1087 routine, we'll need to do something about initializing any .plt and .got
1088 entries in relocate_section. */
1089 #define WILL_CALL_FINISH_DYNAMIC_SYMBOL(DYN, INFO, H) \
1090 ((DYN) \
1091 && ((INFO)->shared \
1092 || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0) \
1093 && ((H)->dynindx != -1 \
1094 || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0))
1096 /* Relocate a SPARC ELF section. */
1098 static bfd_boolean
1099 elf32_sparc_relocate_section (output_bfd, info, input_bfd, input_section,
1100 contents, relocs, local_syms, local_sections)
1101 bfd *output_bfd;
1102 struct bfd_link_info *info;
1103 bfd *input_bfd;
1104 asection *input_section;
1105 bfd_byte *contents;
1106 Elf_Internal_Rela *relocs;
1107 Elf_Internal_Sym *local_syms;
1108 asection **local_sections;
1110 bfd *dynobj;
1111 Elf_Internal_Shdr *symtab_hdr;
1112 struct elf_link_hash_entry **sym_hashes;
1113 bfd_vma *local_got_offsets;
1114 bfd_vma got_base;
1115 asection *sgot;
1116 asection *splt;
1117 asection *sreloc;
1118 Elf_Internal_Rela *rel;
1119 Elf_Internal_Rela *relend;
1121 if (info->relocateable)
1122 return TRUE;
1124 dynobj = elf_hash_table (info)->dynobj;
1125 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1126 sym_hashes = elf_sym_hashes (input_bfd);
1127 local_got_offsets = elf_local_got_offsets (input_bfd);
1129 if (elf_hash_table (info)->hgot == NULL)
1130 got_base = 0;
1131 else
1132 got_base = elf_hash_table (info)->hgot->root.u.def.value;
1134 sgot = NULL;
1135 splt = NULL;
1136 sreloc = NULL;
1138 rel = relocs;
1139 relend = relocs + input_section->reloc_count;
1140 for (; rel < relend; rel++)
1142 int r_type;
1143 reloc_howto_type *howto;
1144 unsigned long r_symndx;
1145 struct elf_link_hash_entry *h;
1146 Elf_Internal_Sym *sym;
1147 asection *sec;
1148 bfd_vma relocation, off;
1149 bfd_reloc_status_type r;
1150 bfd_boolean is_plt = FALSE;
1151 bfd_boolean unresolved_reloc;
1153 r_type = ELF32_R_TYPE (rel->r_info);
1155 if (r_type == R_SPARC_GNU_VTINHERIT
1156 || r_type == R_SPARC_GNU_VTENTRY)
1157 continue;
1159 if (r_type < 0 || r_type >= (int) R_SPARC_max_std)
1161 bfd_set_error (bfd_error_bad_value);
1162 return FALSE;
1164 howto = _bfd_sparc_elf_howto_table + r_type;
1166 /* This is a final link. */
1167 r_symndx = ELF32_R_SYM (rel->r_info);
1168 h = NULL;
1169 sym = NULL;
1170 sec = NULL;
1171 unresolved_reloc = FALSE;
1172 if (r_symndx < symtab_hdr->sh_info)
1174 sym = local_syms + r_symndx;
1175 sec = local_sections[r_symndx];
1176 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, sec, rel);
1178 else
1180 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1181 while (h->root.type == bfd_link_hash_indirect
1182 || h->root.type == bfd_link_hash_warning)
1183 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1185 relocation = 0;
1186 if (h->root.type == bfd_link_hash_defined
1187 || h->root.type == bfd_link_hash_defweak)
1189 sec = h->root.u.def.section;
1190 if (sec->output_section == NULL)
1191 /* Set a flag that will be cleared later if we find a
1192 relocation value for this symbol. output_section
1193 is typically NULL for symbols satisfied by a shared
1194 library. */
1195 unresolved_reloc = TRUE;
1196 else
1197 relocation = (h->root.u.def.value
1198 + sec->output_section->vma
1199 + sec->output_offset);
1201 else if (h->root.type == bfd_link_hash_undefweak)
1203 else if (info->shared
1204 && (!info->symbolic || info->allow_shlib_undefined)
1205 && !info->no_undefined
1206 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
1208 else
1210 if (! ((*info->callbacks->undefined_symbol)
1211 (info, h->root.root.string, input_bfd,
1212 input_section, rel->r_offset,
1213 (!info->shared || info->no_undefined
1214 || ELF_ST_VISIBILITY (h->other)))))
1215 return FALSE;
1219 switch (r_type)
1221 case R_SPARC_GOT10:
1222 case R_SPARC_GOT13:
1223 case R_SPARC_GOT22:
1224 /* Relocation is to the entry for this symbol in the global
1225 offset table. */
1226 if (sgot == NULL)
1228 sgot = bfd_get_section_by_name (dynobj, ".got");
1229 BFD_ASSERT (sgot != NULL);
1232 if (h != NULL)
1234 bfd_boolean dyn;
1236 off = h->got.offset;
1237 BFD_ASSERT (off != (bfd_vma) -1);
1238 dyn = elf_hash_table (info)->dynamic_sections_created;
1240 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info, h)
1241 || (info->shared
1242 && (info->symbolic
1243 || h->dynindx == -1
1244 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL))
1245 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
1247 /* This is actually a static link, or it is a
1248 -Bsymbolic link and the symbol is defined
1249 locally, or the symbol was forced to be local
1250 because of a version file. We must initialize
1251 this entry in the global offset table. Since the
1252 offset must always be a multiple of 4, we use the
1253 least significant bit to record whether we have
1254 initialized it already.
1256 When doing a dynamic link, we create a .rela.got
1257 relocation entry to initialize the value. This
1258 is done in the finish_dynamic_symbol routine. */
1259 if ((off & 1) != 0)
1260 off &= ~1;
1261 else
1263 bfd_put_32 (output_bfd, relocation,
1264 sgot->contents + off);
1265 h->got.offset |= 1;
1268 else
1269 unresolved_reloc = FALSE;
1271 else
1273 BFD_ASSERT (local_got_offsets != NULL
1274 && local_got_offsets[r_symndx] != (bfd_vma) -1);
1276 off = local_got_offsets[r_symndx];
1278 /* The offset must always be a multiple of 4. We use
1279 the least significant bit to record whether we have
1280 already processed this entry. */
1281 if ((off & 1) != 0)
1282 off &= ~1;
1283 else
1286 if (info->shared)
1288 asection *s;
1289 Elf_Internal_Rela outrel;
1290 bfd_byte *loc;
1292 /* We need to generate a R_SPARC_RELATIVE reloc
1293 for the dynamic linker. */
1294 s = bfd_get_section_by_name (dynobj, ".rela.got");
1295 BFD_ASSERT (s != NULL);
1297 outrel.r_offset = (sgot->output_section->vma
1298 + sgot->output_offset
1299 + off);
1300 outrel.r_info = ELF32_R_INFO (0, R_SPARC_RELATIVE);
1301 outrel.r_addend = relocation;
1302 relocation = 0;
1303 loc = s->contents;
1304 loc += s->reloc_count++ * sizeof (Elf32_External_Rela);
1305 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1308 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
1309 local_got_offsets[r_symndx] |= 1;
1312 relocation = sgot->output_offset + off - got_base;
1313 break;
1315 case R_SPARC_PLT32:
1316 if (h == NULL || h->plt.offset == (bfd_vma) -1)
1318 r_type = R_SPARC_32;
1319 goto r_sparc_plt32;
1321 /* Fall through. */
1322 case R_SPARC_WPLT30:
1323 /* Relocation is to the entry for this symbol in the
1324 procedure linkage table. */
1326 /* The Solaris native assembler will generate a WPLT30 reloc
1327 for a local symbol if you assemble a call from one
1328 section to another when using -K pic. We treat it as
1329 WDISP30. */
1330 if (h == NULL)
1331 break;
1333 if (h->plt.offset == (bfd_vma) -1)
1335 /* We didn't make a PLT entry for this symbol. This
1336 happens when statically linking PIC code, or when
1337 using -Bsymbolic. */
1338 break;
1341 if (splt == NULL)
1343 splt = bfd_get_section_by_name (dynobj, ".plt");
1344 BFD_ASSERT (splt != NULL);
1347 relocation = (splt->output_section->vma
1348 + splt->output_offset
1349 + h->plt.offset);
1350 unresolved_reloc = FALSE;
1351 if (r_type == R_SPARC_PLT32)
1353 r_type = R_SPARC_32;
1354 is_plt = TRUE;
1355 goto r_sparc_plt32;
1357 break;
1359 case R_SPARC_PC10:
1360 case R_SPARC_PC22:
1361 if (h != NULL
1362 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
1363 break;
1364 /* Fall through. */
1365 case R_SPARC_DISP8:
1366 case R_SPARC_DISP16:
1367 case R_SPARC_DISP32:
1368 case R_SPARC_WDISP30:
1369 case R_SPARC_WDISP22:
1370 case R_SPARC_WDISP19:
1371 case R_SPARC_WDISP16:
1372 if (h == NULL
1373 || (info->symbolic
1374 && (h->elf_link_hash_flags
1375 & ELF_LINK_HASH_DEF_REGULAR) != 0))
1376 break;
1377 /* Fall through. */
1378 case R_SPARC_8:
1379 case R_SPARC_16:
1380 case R_SPARC_32:
1381 case R_SPARC_HI22:
1382 case R_SPARC_22:
1383 case R_SPARC_13:
1384 case R_SPARC_LO10:
1385 case R_SPARC_UA16:
1386 case R_SPARC_UA32:
1387 r_sparc_plt32:
1388 if (info->shared
1389 && r_symndx != 0
1390 && (input_section->flags & SEC_ALLOC))
1392 Elf_Internal_Rela outrel;
1393 bfd_byte *loc;
1394 bfd_boolean skip, relocate = FALSE;
1396 /* When generating a shared object, these relocations
1397 are copied into the output file to be resolved at run
1398 time. */
1400 if (sreloc == NULL)
1402 const char *name;
1404 name = (bfd_elf_string_from_elf_section
1405 (input_bfd,
1406 elf_elfheader (input_bfd)->e_shstrndx,
1407 elf_section_data (input_section)->rel_hdr.sh_name));
1408 if (name == NULL)
1409 return FALSE;
1411 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
1412 && strcmp (bfd_get_section_name (input_bfd,
1413 input_section),
1414 name + 5) == 0);
1416 sreloc = bfd_get_section_by_name (dynobj, name);
1417 BFD_ASSERT (sreloc != NULL);
1420 skip = FALSE;
1422 outrel.r_offset =
1423 _bfd_elf_section_offset (output_bfd, info, input_section,
1424 rel->r_offset);
1425 if (outrel.r_offset == (bfd_vma) -1)
1426 skip = TRUE;
1427 else if (outrel.r_offset == (bfd_vma) -2)
1428 skip = TRUE, relocate = TRUE;
1429 outrel.r_offset += (input_section->output_section->vma
1430 + input_section->output_offset);
1432 /* Optimize unaligned reloc usage now that we know where
1433 it finally resides. */
1434 switch (r_type)
1436 case R_SPARC_16:
1437 if (outrel.r_offset & 1)
1438 r_type = R_SPARC_UA16;
1439 break;
1440 case R_SPARC_UA16:
1441 if (!(outrel.r_offset & 1))
1442 r_type = R_SPARC_16;
1443 break;
1444 case R_SPARC_32:
1445 if (outrel.r_offset & 3)
1446 r_type = R_SPARC_UA32;
1447 break;
1448 case R_SPARC_UA32:
1449 if (!(outrel.r_offset & 3))
1450 r_type = R_SPARC_32;
1451 break;
1452 case R_SPARC_DISP8:
1453 case R_SPARC_DISP16:
1454 case R_SPARC_DISP32:
1455 /* If the symbol is not dynamic, we should not keep
1456 a dynamic relocation. But an .rela.* slot has been
1457 allocated for it, output R_SPARC_NONE.
1458 FIXME: Add code tracking needed dynamic relocs as
1459 e.g. i386 has. */
1460 if (h->dynindx == -1)
1461 skip = TRUE, relocate = TRUE;
1462 break;
1465 if (skip)
1466 memset (&outrel, 0, sizeof outrel);
1467 /* h->dynindx may be -1 if the symbol was marked to
1468 become local. */
1469 else if (h != NULL && ! is_plt
1470 && ((! info->symbolic && h->dynindx != -1)
1471 || (h->elf_link_hash_flags
1472 & ELF_LINK_HASH_DEF_REGULAR) == 0))
1474 BFD_ASSERT (h->dynindx != -1);
1475 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
1476 outrel.r_addend = rel->r_addend;
1478 else
1480 if (r_type == R_SPARC_32)
1482 outrel.r_info = ELF32_R_INFO (0, R_SPARC_RELATIVE);
1483 outrel.r_addend = relocation + rel->r_addend;
1485 else
1487 long indx;
1489 if (is_plt)
1490 sec = splt;
1491 else if (h == NULL)
1492 sec = local_sections[r_symndx];
1493 else
1495 BFD_ASSERT (h->root.type == bfd_link_hash_defined
1496 || (h->root.type
1497 == bfd_link_hash_defweak));
1498 sec = h->root.u.def.section;
1500 if (sec != NULL && bfd_is_abs_section (sec))
1501 indx = 0;
1502 else if (sec == NULL || sec->owner == NULL)
1504 bfd_set_error (bfd_error_bad_value);
1505 return FALSE;
1507 else
1509 asection *osec;
1511 osec = sec->output_section;
1512 indx = elf_section_data (osec)->dynindx;
1514 /* FIXME: we really should be able to link non-pic
1515 shared libraries. */
1516 if (indx == 0)
1518 BFD_FAIL ();
1519 (*_bfd_error_handler)
1520 (_("%s: probably compiled without -fPIC?"),
1521 bfd_archive_filename (input_bfd));
1522 bfd_set_error (bfd_error_bad_value);
1523 return FALSE;
1527 outrel.r_info = ELF32_R_INFO (indx, r_type);
1528 outrel.r_addend = relocation + rel->r_addend;
1532 loc = sreloc->contents;
1533 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
1534 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1536 /* This reloc will be computed at runtime, so there's no
1537 need to do anything now. */
1538 if (! relocate)
1539 continue;
1541 break;
1543 default:
1544 break;
1547 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
1548 because such sections are not SEC_ALLOC and thus ld.so will
1549 not process them. */
1550 if (unresolved_reloc
1551 && !((input_section->flags & SEC_DEBUGGING) != 0
1552 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0))
1553 (*_bfd_error_handler)
1554 (_("%s(%s+0x%lx): unresolvable relocation against symbol `%s'"),
1555 bfd_archive_filename (input_bfd),
1556 bfd_get_section_name (input_bfd, input_section),
1557 (long) rel->r_offset,
1558 h->root.root.string);
1560 r = bfd_reloc_continue;
1561 if (r_type == R_SPARC_WDISP16)
1563 bfd_vma x;
1565 relocation += rel->r_addend;
1566 relocation -= (input_section->output_section->vma
1567 + input_section->output_offset);
1568 relocation -= rel->r_offset;
1570 x = bfd_get_32 (input_bfd, contents + rel->r_offset);
1571 x |= ((((relocation >> 2) & 0xc000) << 6)
1572 | ((relocation >> 2) & 0x3fff));
1573 bfd_put_32 (input_bfd, x, contents + rel->r_offset);
1575 if ((bfd_signed_vma) relocation < - 0x40000
1576 || (bfd_signed_vma) relocation > 0x3ffff)
1577 r = bfd_reloc_overflow;
1578 else
1579 r = bfd_reloc_ok;
1581 else if (r_type == R_SPARC_REV32)
1583 bfd_vma x;
1585 relocation = relocation + rel->r_addend;
1587 x = bfd_get_32 (input_bfd, contents + rel->r_offset);
1588 x = x + relocation;
1589 bfd_putl32 (/*input_bfd,*/ x, contents + rel->r_offset);
1590 r = bfd_reloc_ok;
1592 else if ((r_type == R_SPARC_WDISP30 || r_type == R_SPARC_WPLT30)
1593 && SEC_DO_RELAX (input_section)
1594 && rel->r_offset + 4 < input_section->_raw_size)
1596 #define G0 0
1597 #define O7 15
1598 #define XCC (2 << 20)
1599 #define COND(x) (((x)&0xf)<<25)
1600 #define CONDA COND(0x8)
1601 #define INSN_BPA (F2(0,1) | CONDA | BPRED | XCC)
1602 #define INSN_BA (F2(0,2) | CONDA)
1603 #define INSN_OR F3(2, 0x2, 0)
1604 #define INSN_NOP F2(0,4)
1606 bfd_vma x, y;
1608 /* If the instruction is a call with either:
1609 restore
1610 arithmetic instruction with rd == %o7
1611 where rs1 != %o7 and rs2 if it is register != %o7
1612 then we can optimize if the call destination is near
1613 by changing the call into a branch always. */
1614 x = bfd_get_32 (input_bfd, contents + rel->r_offset);
1615 y = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
1616 if ((x & OP(~0)) == OP(1) && (y & OP(~0)) == OP(2))
1618 if (((y & OP3(~0)) == OP3(0x3d) /* restore */
1619 || ((y & OP3(0x28)) == 0 /* arithmetic */
1620 && (y & RD(~0)) == RD(O7)))
1621 && (y & RS1(~0)) != RS1(O7)
1622 && ((y & F3I(~0))
1623 || (y & RS2(~0)) != RS2(O7)))
1625 bfd_vma reloc;
1627 reloc = relocation + rel->r_addend - rel->r_offset;
1628 reloc -= (input_section->output_section->vma
1629 + input_section->output_offset);
1631 /* Ensure the reloc fits into simm22. */
1632 if ((reloc & 3) == 0
1633 && ((reloc & ~(bfd_vma)0x7fffff) == 0
1634 || ((reloc | 0x7fffff) == ~(bfd_vma)0)))
1636 reloc >>= 2;
1638 /* Check whether it fits into simm19 on v9. */
1639 if (((reloc & 0x3c0000) == 0
1640 || (reloc & 0x3c0000) == 0x3c0000)
1641 && (elf_elfheader (output_bfd)->e_flags & EF_SPARC_32PLUS))
1642 x = INSN_BPA | (reloc & 0x7ffff); /* ba,pt %xcc */
1643 else
1644 x = INSN_BA | (reloc & 0x3fffff); /* ba */
1645 bfd_put_32 (input_bfd, x, contents + rel->r_offset);
1646 r = bfd_reloc_ok;
1647 if (rel->r_offset >= 4
1648 && (y & (0xffffffff ^ RS1(~0)))
1649 == (INSN_OR | RD(O7) | RS2(G0)))
1651 bfd_vma z;
1652 unsigned int reg;
1654 z = bfd_get_32 (input_bfd,
1655 contents + rel->r_offset - 4);
1656 if ((z & (0xffffffff ^ RD(~0)))
1657 != (INSN_OR | RS1(O7) | RS2(G0)))
1658 break;
1660 /* The sequence was
1661 or %o7, %g0, %rN
1662 call foo
1663 or %rN, %g0, %o7
1665 If call foo was replaced with ba, replace
1666 or %rN, %g0, %o7 with nop. */
1668 reg = (y & RS1(~0)) >> 14;
1669 if (reg != ((z & RD(~0)) >> 25)
1670 || reg == G0 || reg == O7)
1671 break;
1673 bfd_put_32 (input_bfd, (bfd_vma) INSN_NOP,
1674 contents + rel->r_offset + 4);
1682 if (r == bfd_reloc_continue)
1683 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1684 contents, rel->r_offset,
1685 relocation, rel->r_addend);
1687 if (r != bfd_reloc_ok)
1689 switch (r)
1691 default:
1692 case bfd_reloc_outofrange:
1693 abort ();
1694 case bfd_reloc_overflow:
1696 const char *name;
1698 if (h != NULL)
1699 name = h->root.root.string;
1700 else
1702 name = bfd_elf_string_from_elf_section (input_bfd,
1703 symtab_hdr->sh_link,
1704 sym->st_name);
1705 if (name == NULL)
1706 return FALSE;
1707 if (*name == '\0')
1708 name = bfd_section_name (input_bfd, sec);
1710 if (! ((*info->callbacks->reloc_overflow)
1711 (info, name, howto->name, (bfd_vma) 0,
1712 input_bfd, input_section, rel->r_offset)))
1713 return FALSE;
1715 break;
1720 return TRUE;
1723 /* Finish up dynamic symbol handling. We set the contents of various
1724 dynamic sections here. */
1726 static bfd_boolean
1727 elf32_sparc_finish_dynamic_symbol (output_bfd, info, h, sym)
1728 bfd *output_bfd;
1729 struct bfd_link_info *info;
1730 struct elf_link_hash_entry *h;
1731 Elf_Internal_Sym *sym;
1733 bfd *dynobj;
1735 dynobj = elf_hash_table (info)->dynobj;
1737 if (h->plt.offset != (bfd_vma) -1)
1739 asection *splt;
1740 asection *srela;
1741 Elf_Internal_Rela rela;
1742 bfd_byte *loc;
1744 /* This symbol has an entry in the procedure linkage table. Set
1745 it up. */
1747 BFD_ASSERT (h->dynindx != -1);
1749 splt = bfd_get_section_by_name (dynobj, ".plt");
1750 srela = bfd_get_section_by_name (dynobj, ".rela.plt");
1751 BFD_ASSERT (splt != NULL && srela != NULL);
1753 /* Fill in the entry in the procedure linkage table. */
1754 bfd_put_32 (output_bfd,
1755 PLT_ENTRY_WORD0 + h->plt.offset,
1756 splt->contents + h->plt.offset);
1757 bfd_put_32 (output_bfd,
1758 (PLT_ENTRY_WORD1
1759 + (((- (h->plt.offset + 4)) >> 2) & 0x3fffff)),
1760 splt->contents + h->plt.offset + 4);
1761 bfd_put_32 (output_bfd, (bfd_vma) PLT_ENTRY_WORD2,
1762 splt->contents + h->plt.offset + 8);
1764 /* Fill in the entry in the .rela.plt section. */
1765 rela.r_offset = (splt->output_section->vma
1766 + splt->output_offset
1767 + h->plt.offset);
1768 rela.r_info = ELF32_R_INFO (h->dynindx, R_SPARC_JMP_SLOT);
1769 rela.r_addend = 0;
1770 loc = srela->contents;
1771 loc += (h->plt.offset / PLT_ENTRY_SIZE - 4) * sizeof (Elf32_External_Rela);
1772 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
1774 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1776 /* Mark the symbol as undefined, rather than as defined in
1777 the .plt section. Leave the value alone. */
1778 sym->st_shndx = SHN_UNDEF;
1779 /* If the symbol is weak, we do need to clear the value.
1780 Otherwise, the PLT entry would provide a definition for
1781 the symbol even if the symbol wasn't defined anywhere,
1782 and so the symbol would never be NULL. */
1783 if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR_NONWEAK)
1784 == 0)
1785 sym->st_value = 0;
1789 if (h->got.offset != (bfd_vma) -1)
1791 asection *sgot;
1792 asection *srela;
1793 Elf_Internal_Rela rela;
1794 bfd_byte *loc;
1796 /* This symbol has an entry in the global offset table. Set it
1797 up. */
1799 sgot = bfd_get_section_by_name (dynobj, ".got");
1800 srela = bfd_get_section_by_name (dynobj, ".rela.got");
1801 BFD_ASSERT (sgot != NULL && srela != NULL);
1803 rela.r_offset = (sgot->output_section->vma
1804 + sgot->output_offset
1805 + (h->got.offset &~ (bfd_vma) 1));
1807 /* If this is a -Bsymbolic link, and the symbol is defined
1808 locally, we just want to emit a RELATIVE reloc. Likewise if
1809 the symbol was forced to be local because of a version file.
1810 The entry in the global offset table will already have been
1811 initialized in the relocate_section function. */
1812 if (info->shared
1813 && (info->symbolic || h->dynindx == -1)
1814 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
1816 asection *sec = h->root.u.def.section;
1817 rela.r_info = ELF32_R_INFO (0, R_SPARC_RELATIVE);
1818 rela.r_addend = (h->root.u.def.value
1819 + sec->output_section->vma
1820 + sec->output_offset);
1822 else
1824 rela.r_info = ELF32_R_INFO (h->dynindx, R_SPARC_GLOB_DAT);
1825 rela.r_addend = 0;
1828 bfd_put_32 (output_bfd, (bfd_vma) 0,
1829 sgot->contents + (h->got.offset &~ (bfd_vma) 1));
1830 loc = srela->contents;
1831 loc += srela->reloc_count++ * sizeof (Elf32_External_Rela);
1832 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
1835 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
1837 asection *s;
1838 Elf_Internal_Rela rela;
1839 bfd_byte *loc;
1841 /* This symbols needs a copy reloc. Set it up. */
1843 BFD_ASSERT (h->dynindx != -1);
1845 s = bfd_get_section_by_name (h->root.u.def.section->owner,
1846 ".rela.bss");
1847 BFD_ASSERT (s != NULL);
1849 rela.r_offset = (h->root.u.def.value
1850 + h->root.u.def.section->output_section->vma
1851 + h->root.u.def.section->output_offset);
1852 rela.r_info = ELF32_R_INFO (h->dynindx, R_SPARC_COPY);
1853 rela.r_addend = 0;
1854 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
1855 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
1858 /* Mark some specially defined symbols as absolute. */
1859 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
1860 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
1861 || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0)
1862 sym->st_shndx = SHN_ABS;
1864 return TRUE;
1867 /* Finish up the dynamic sections. */
1869 static bfd_boolean
1870 elf32_sparc_finish_dynamic_sections (output_bfd, info)
1871 bfd *output_bfd;
1872 struct bfd_link_info *info;
1874 bfd *dynobj;
1875 asection *sdyn;
1876 asection *sgot;
1878 dynobj = elf_hash_table (info)->dynobj;
1880 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
1882 if (elf_hash_table (info)->dynamic_sections_created)
1884 asection *splt;
1885 Elf32_External_Dyn *dyncon, *dynconend;
1887 splt = bfd_get_section_by_name (dynobj, ".plt");
1888 BFD_ASSERT (splt != NULL && sdyn != NULL);
1890 dyncon = (Elf32_External_Dyn *) sdyn->contents;
1891 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
1892 for (; dyncon < dynconend; dyncon++)
1894 Elf_Internal_Dyn dyn;
1895 const char *name;
1896 bfd_boolean size;
1898 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
1900 switch (dyn.d_tag)
1902 case DT_PLTGOT: name = ".plt"; size = FALSE; break;
1903 case DT_PLTRELSZ: name = ".rela.plt"; size = TRUE; break;
1904 case DT_JMPREL: name = ".rela.plt"; size = FALSE; break;
1905 default: name = NULL; size = FALSE; break;
1908 if (name != NULL)
1910 asection *s;
1912 s = bfd_get_section_by_name (output_bfd, name);
1913 if (s == NULL)
1914 dyn.d_un.d_val = 0;
1915 else
1917 if (! size)
1918 dyn.d_un.d_ptr = s->vma;
1919 else
1921 if (s->_cooked_size != 0)
1922 dyn.d_un.d_val = s->_cooked_size;
1923 else
1924 dyn.d_un.d_val = s->_raw_size;
1927 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1931 /* Clear the first four entries in the procedure linkage table,
1932 and put a nop in the last four bytes. */
1933 if (splt->_raw_size > 0)
1935 memset (splt->contents, 0, 4 * PLT_ENTRY_SIZE);
1936 bfd_put_32 (output_bfd, (bfd_vma) SPARC_NOP,
1937 splt->contents + splt->_raw_size - 4);
1940 elf_section_data (splt->output_section)->this_hdr.sh_entsize =
1941 PLT_ENTRY_SIZE;
1944 /* Set the first entry in the global offset table to the address of
1945 the dynamic section. */
1946 sgot = bfd_get_section_by_name (dynobj, ".got");
1947 BFD_ASSERT (sgot != NULL);
1948 if (sgot->_raw_size > 0)
1950 if (sdyn == NULL)
1951 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
1952 else
1953 bfd_put_32 (output_bfd,
1954 sdyn->output_section->vma + sdyn->output_offset,
1955 sgot->contents);
1958 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
1960 return TRUE;
1963 /* Functions for dealing with the e_flags field.
1965 We don't define set_private_flags or copy_private_bfd_data because
1966 the only currently defined values are based on the bfd mach number,
1967 so we use the latter instead and defer setting e_flags until the
1968 file is written out. */
1970 /* Merge backend specific data from an object file to the output
1971 object file when linking. */
1973 static bfd_boolean
1974 elf32_sparc_merge_private_bfd_data (ibfd, obfd)
1975 bfd *ibfd;
1976 bfd *obfd;
1978 bfd_boolean error;
1979 /* FIXME: This should not be static. */
1980 static unsigned long previous_ibfd_e_flags = (unsigned long) -1;
1982 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1983 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1984 return TRUE;
1986 error = FALSE;
1988 if (bfd_get_mach (ibfd) >= bfd_mach_sparc_v9)
1990 error = TRUE;
1991 (*_bfd_error_handler)
1992 (_("%s: compiled for a 64 bit system and target is 32 bit"),
1993 bfd_archive_filename (ibfd));
1995 else if ((ibfd->flags & DYNAMIC) == 0)
1997 if (bfd_get_mach (obfd) < bfd_get_mach (ibfd))
1998 bfd_set_arch_mach (obfd, bfd_arch_sparc, bfd_get_mach (ibfd));
2001 if (((elf_elfheader (ibfd)->e_flags & EF_SPARC_LEDATA)
2002 != previous_ibfd_e_flags)
2003 && previous_ibfd_e_flags != (unsigned long) -1)
2005 (*_bfd_error_handler)
2006 (_("%s: linking little endian files with big endian files"),
2007 bfd_archive_filename (ibfd));
2008 error = TRUE;
2010 previous_ibfd_e_flags = elf_elfheader (ibfd)->e_flags & EF_SPARC_LEDATA;
2012 if (error)
2014 bfd_set_error (bfd_error_bad_value);
2015 return FALSE;
2018 return TRUE;
2021 /* Set the right machine number. */
2023 static bfd_boolean
2024 elf32_sparc_object_p (abfd)
2025 bfd *abfd;
2027 if (elf_elfheader (abfd)->e_machine == EM_SPARC32PLUS)
2029 if (elf_elfheader (abfd)->e_flags & EF_SPARC_SUN_US3)
2030 return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
2031 bfd_mach_sparc_v8plusb);
2032 else if (elf_elfheader (abfd)->e_flags & EF_SPARC_SUN_US1)
2033 return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
2034 bfd_mach_sparc_v8plusa);
2035 else if (elf_elfheader (abfd)->e_flags & EF_SPARC_32PLUS)
2036 return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
2037 bfd_mach_sparc_v8plus);
2038 else
2039 return FALSE;
2041 else if (elf_elfheader (abfd)->e_flags & EF_SPARC_LEDATA)
2042 return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
2043 bfd_mach_sparc_sparclite_le);
2044 else
2045 return bfd_default_set_arch_mach (abfd, bfd_arch_sparc, bfd_mach_sparc);
2048 /* The final processing done just before writing out the object file.
2049 We need to set the e_machine field appropriately. */
2051 static void
2052 elf32_sparc_final_write_processing (abfd, linker)
2053 bfd *abfd;
2054 bfd_boolean linker ATTRIBUTE_UNUSED;
2056 switch (bfd_get_mach (abfd))
2058 case bfd_mach_sparc :
2059 break; /* nothing to do */
2060 case bfd_mach_sparc_v8plus :
2061 elf_elfheader (abfd)->e_machine = EM_SPARC32PLUS;
2062 elf_elfheader (abfd)->e_flags &=~ EF_SPARC_32PLUS_MASK;
2063 elf_elfheader (abfd)->e_flags |= EF_SPARC_32PLUS;
2064 break;
2065 case bfd_mach_sparc_v8plusa :
2066 elf_elfheader (abfd)->e_machine = EM_SPARC32PLUS;
2067 elf_elfheader (abfd)->e_flags &=~ EF_SPARC_32PLUS_MASK;
2068 elf_elfheader (abfd)->e_flags |= EF_SPARC_32PLUS | EF_SPARC_SUN_US1;
2069 break;
2070 case bfd_mach_sparc_v8plusb :
2071 elf_elfheader (abfd)->e_machine = EM_SPARC32PLUS;
2072 elf_elfheader (abfd)->e_flags &=~ EF_SPARC_32PLUS_MASK;
2073 elf_elfheader (abfd)->e_flags |= EF_SPARC_32PLUS | EF_SPARC_SUN_US1
2074 | EF_SPARC_SUN_US3;
2075 break;
2076 case bfd_mach_sparc_sparclite_le :
2077 elf_elfheader (abfd)->e_machine = EM_SPARC;
2078 elf_elfheader (abfd)->e_flags |= EF_SPARC_LEDATA;
2079 break;
2080 default :
2081 abort ();
2082 break;
2086 static enum elf_reloc_type_class
2087 elf32_sparc_reloc_type_class (rela)
2088 const Elf_Internal_Rela *rela;
2090 switch ((int) ELF32_R_TYPE (rela->r_info))
2092 case R_SPARC_RELATIVE:
2093 return reloc_class_relative;
2094 case R_SPARC_JMP_SLOT:
2095 return reloc_class_plt;
2096 case R_SPARC_COPY:
2097 return reloc_class_copy;
2098 default:
2099 return reloc_class_normal;
2103 #define TARGET_BIG_SYM bfd_elf32_sparc_vec
2104 #define TARGET_BIG_NAME "elf32-sparc"
2105 #define ELF_ARCH bfd_arch_sparc
2106 #define ELF_MACHINE_CODE EM_SPARC
2107 #define ELF_MACHINE_ALT1 EM_SPARC32PLUS
2108 #define ELF_MAXPAGESIZE 0x10000
2110 #define bfd_elf32_bfd_reloc_type_lookup elf32_sparc_reloc_type_lookup
2111 #define bfd_elf32_bfd_relax_section elf32_sparc_relax_section
2112 #define elf_info_to_howto elf32_sparc_info_to_howto
2113 #define elf_backend_create_dynamic_sections \
2114 _bfd_elf_create_dynamic_sections
2115 #define elf_backend_check_relocs elf32_sparc_check_relocs
2116 #define elf_backend_adjust_dynamic_symbol \
2117 elf32_sparc_adjust_dynamic_symbol
2118 #define elf_backend_size_dynamic_sections \
2119 elf32_sparc_size_dynamic_sections
2120 #define elf_backend_relocate_section elf32_sparc_relocate_section
2121 #define elf_backend_finish_dynamic_symbol \
2122 elf32_sparc_finish_dynamic_symbol
2123 #define elf_backend_finish_dynamic_sections \
2124 elf32_sparc_finish_dynamic_sections
2125 #define bfd_elf32_bfd_merge_private_bfd_data \
2126 elf32_sparc_merge_private_bfd_data
2127 #define elf_backend_object_p elf32_sparc_object_p
2128 #define elf_backend_final_write_processing \
2129 elf32_sparc_final_write_processing
2130 #define elf_backend_gc_mark_hook elf32_sparc_gc_mark_hook
2131 #define elf_backend_gc_sweep_hook elf32_sparc_gc_sweep_hook
2132 #define elf_backend_reloc_type_class elf32_sparc_reloc_type_class
2134 #define elf_backend_can_gc_sections 1
2135 #define elf_backend_want_got_plt 0
2136 #define elf_backend_plt_readonly 0
2137 #define elf_backend_want_plt_sym 1
2138 #define elf_backend_got_header_size 4
2139 #define elf_backend_plt_header_size (4*PLT_ENTRY_SIZE)
2140 #define elf_backend_rela_normal 1
2142 #include "elf32-target.h"