* scripttempl/elf.sc: Move non-text .dynamic section before
[binutils.git] / bfd / elf32-sparc.c
blob76d0324ee5fd2ba10d950d8ccd40128fb79ed0d3
1 /* SPARC-specific support for 32-bit ELF
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001
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 boolean elf32_sparc_check_relocs
34 PARAMS ((bfd *, struct bfd_link_info *, asection *,
35 const Elf_Internal_Rela *));
36 static boolean elf32_sparc_adjust_dynamic_symbol
37 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
38 static boolean elf32_sparc_size_dynamic_sections
39 PARAMS ((bfd *, struct bfd_link_info *));
40 static boolean elf32_sparc_relax_section
41 PARAMS ((bfd *, asection *, struct bfd_link_info *, boolean *));
42 static 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 boolean elf32_sparc_finish_dynamic_symbol
46 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
47 Elf_Internal_Sym *));
48 static boolean elf32_sparc_finish_dynamic_sections
49 PARAMS ((bfd *, struct bfd_link_info *));
50 static boolean elf32_sparc_merge_private_bfd_data PARAMS ((bfd *, bfd *));
51 static boolean elf32_sparc_object_p
52 PARAMS ((bfd *));
53 static void elf32_sparc_final_write_processing
54 PARAMS ((bfd *, boolean));
56 /* The relocation "howto" table. */
58 static bfd_reloc_status_type sparc_elf_notsupported_reloc
59 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
60 static bfd_reloc_status_type sparc_elf_wdisp16_reloc
61 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
63 reloc_howto_type _bfd_sparc_elf_howto_table[] =
65 HOWTO(R_SPARC_NONE, 0,0, 0,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_NONE", false,0,0x00000000,true),
66 HOWTO(R_SPARC_8, 0,0, 8,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_8", false,0,0x000000ff,true),
67 HOWTO(R_SPARC_16, 0,1,16,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_16", false,0,0x0000ffff,true),
68 HOWTO(R_SPARC_32, 0,2,32,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_32", false,0,0xffffffff,true),
69 HOWTO(R_SPARC_DISP8, 0,0, 8,true, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_DISP8", false,0,0x000000ff,true),
70 HOWTO(R_SPARC_DISP16, 0,1,16,true, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_DISP16", false,0,0x0000ffff,true),
71 HOWTO(R_SPARC_DISP32, 0,2,32,true, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_DISP32", false,0,0x00ffffff,true),
72 HOWTO(R_SPARC_WDISP30, 2,2,30,true, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_WDISP30", false,0,0x3fffffff,true),
73 HOWTO(R_SPARC_WDISP22, 2,2,22,true, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_WDISP22", false,0,0x003fffff,true),
74 HOWTO(R_SPARC_HI22, 10,2,22,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_HI22", false,0,0x003fffff,true),
75 HOWTO(R_SPARC_22, 0,2,22,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_22", false,0,0x003fffff,true),
76 HOWTO(R_SPARC_13, 0,2,13,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_13", false,0,0x00001fff,true),
77 HOWTO(R_SPARC_LO10, 0,2,10,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_LO10", false,0,0x000003ff,true),
78 HOWTO(R_SPARC_GOT10, 0,2,10,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_GOT10", false,0,0x000003ff,true),
79 HOWTO(R_SPARC_GOT13, 0,2,13,false,0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_GOT13", false,0,0x00001fff,true),
80 HOWTO(R_SPARC_GOT22, 10,2,22,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_GOT22", false,0,0x003fffff,true),
81 HOWTO(R_SPARC_PC10, 0,2,10,true, 0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_PC10", false,0,0x000003ff,true),
82 HOWTO(R_SPARC_PC22, 10,2,22,true, 0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_PC22", false,0,0x003fffff,true),
83 HOWTO(R_SPARC_WPLT30, 2,2,30,true, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_WPLT30", false,0,0x3fffffff,true),
84 HOWTO(R_SPARC_COPY, 0,0,00,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_COPY", false,0,0x00000000,true),
85 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),
86 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),
87 HOWTO(R_SPARC_RELATIVE, 0,0,00,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_RELATIVE",false,0,0x00000000,true),
88 HOWTO(R_SPARC_UA32, 0,2,32,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_UA32", false,0,0xffffffff,true),
89 HOWTO(R_SPARC_PLT32, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_PLT32", false,0,0x00000000,true),
90 HOWTO(R_SPARC_HIPLT22, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_HIPLT22", false,0,0x00000000,true),
91 HOWTO(R_SPARC_LOPLT10, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_LOPLT10", false,0,0x00000000,true),
92 HOWTO(R_SPARC_PCPLT32, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_PCPLT32", false,0,0x00000000,true),
93 HOWTO(R_SPARC_PCPLT22, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_PCPLT22", false,0,0x00000000,true),
94 HOWTO(R_SPARC_PCPLT10, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_PCPLT10", false,0,0x00000000,true),
95 HOWTO(R_SPARC_10, 0,2,10,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_10", false,0,0x000003ff,true),
96 HOWTO(R_SPARC_11, 0,2,11,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_11", false,0,0x000007ff,true),
97 /* These are for sparc64 in a 64 bit environment.
98 Values need to be here because the table is indexed by reloc number. */
99 HOWTO(R_SPARC_64, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_64", false,0,0x00000000,true),
100 HOWTO(R_SPARC_OLO10, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_OLO10", false,0,0x00000000,true),
101 HOWTO(R_SPARC_HH22, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_HH22", false,0,0x00000000,true),
102 HOWTO(R_SPARC_HM10, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_HM10", false,0,0x00000000,true),
103 HOWTO(R_SPARC_LM22, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_LM22", false,0,0x00000000,true),
104 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),
105 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),
106 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),
107 /* End sparc64 in 64 bit environment values.
108 The following are for sparc64 in a 32 bit environment. */
109 HOWTO(R_SPARC_WDISP16, 2,2,16,true, 0,complain_overflow_signed, sparc_elf_wdisp16_reloc,"R_SPARC_WDISP16", false,0,0x00000000,true),
110 HOWTO(R_SPARC_WDISP19, 2,2,19,true, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_WDISP19", false,0,0x0007ffff,true),
111 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),
112 HOWTO(R_SPARC_7, 0,2, 7,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_7", false,0,0x0000007f,true),
113 HOWTO(R_SPARC_5, 0,2, 5,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_5", false,0,0x0000001f,true),
114 HOWTO(R_SPARC_6, 0,2, 6,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_6", false,0,0x0000003f,true),
115 HOWTO(R_SPARC_NONE, 0,0, 0,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_NONE", false,0,0x00000000,true),
116 HOWTO(R_SPARC_NONE, 0,0, 0,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_NONE", false,0,0x00000000,true),
117 HOWTO(R_SPARC_NONE, 0,0, 0,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_NONE", false,0,0x00000000,true),
118 HOWTO(R_SPARC_NONE, 0,0, 0,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_NONE", false,0,0x00000000,true),
119 HOWTO(R_SPARC_NONE, 0,0, 0,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_NONE", false,0,0x00000000,true),
120 HOWTO(R_SPARC_NONE, 0,0, 0,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_NONE", false,0,0x00000000,true),
121 HOWTO(R_SPARC_NONE, 0,0, 0,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_NONE", false,0,0x00000000,true),
122 HOWTO(R_SPARC_NONE, 0,0, 0,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_NONE", false,0,0x00000000,true),
123 HOWTO(R_SPARC_UA64, 0,0, 0,false,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_UA64", false,0,0x00000000,true),
124 HOWTO(R_SPARC_UA16, 0,1,16,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_UA16", false,0,0x0000ffff,true),
125 HOWTO(R_SPARC_REV32, 0,2,32,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_REV32", false,0,0xffffffff,true),
127 static reloc_howto_type elf32_sparc_vtinherit_howto =
128 HOWTO (R_SPARC_GNU_VTINHERIT, 0,2,0,false,0,complain_overflow_dont, NULL, "R_SPARC_GNU_VTINHERIT", false,0, 0, false);
129 static reloc_howto_type elf32_sparc_vtentry_howto =
130 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);
132 struct elf_reloc_map {
133 bfd_reloc_code_real_type bfd_reloc_val;
134 unsigned char elf_reloc_val;
137 static CONST struct elf_reloc_map sparc_reloc_map[] =
139 { BFD_RELOC_NONE, R_SPARC_NONE, },
140 { BFD_RELOC_16, R_SPARC_16, },
141 { BFD_RELOC_8, R_SPARC_8 },
142 { BFD_RELOC_8_PCREL, R_SPARC_DISP8 },
143 { BFD_RELOC_CTOR, R_SPARC_32 },
144 { BFD_RELOC_32, R_SPARC_32 },
145 { BFD_RELOC_32_PCREL, R_SPARC_DISP32 },
146 { BFD_RELOC_HI22, R_SPARC_HI22 },
147 { BFD_RELOC_LO10, R_SPARC_LO10, },
148 { BFD_RELOC_32_PCREL_S2, R_SPARC_WDISP30 },
149 { BFD_RELOC_SPARC22, R_SPARC_22 },
150 { BFD_RELOC_SPARC13, R_SPARC_13 },
151 { BFD_RELOC_SPARC_GOT10, R_SPARC_GOT10 },
152 { BFD_RELOC_SPARC_GOT13, R_SPARC_GOT13 },
153 { BFD_RELOC_SPARC_GOT22, R_SPARC_GOT22 },
154 { BFD_RELOC_SPARC_PC10, R_SPARC_PC10 },
155 { BFD_RELOC_SPARC_PC22, R_SPARC_PC22 },
156 { BFD_RELOC_SPARC_WPLT30, R_SPARC_WPLT30 },
157 { BFD_RELOC_SPARC_COPY, R_SPARC_COPY },
158 { BFD_RELOC_SPARC_GLOB_DAT, R_SPARC_GLOB_DAT },
159 { BFD_RELOC_SPARC_JMP_SLOT, R_SPARC_JMP_SLOT },
160 { BFD_RELOC_SPARC_RELATIVE, R_SPARC_RELATIVE },
161 { BFD_RELOC_SPARC_WDISP22, R_SPARC_WDISP22 },
162 { BFD_RELOC_SPARC_UA16, R_SPARC_UA16 },
163 { BFD_RELOC_SPARC_UA32, R_SPARC_UA32 },
164 { BFD_RELOC_SPARC_UA64, R_SPARC_UA64 },
165 { BFD_RELOC_SPARC_10, R_SPARC_10 },
166 { BFD_RELOC_SPARC_11, R_SPARC_11 },
167 { BFD_RELOC_SPARC_64, R_SPARC_64 },
168 { BFD_RELOC_SPARC_OLO10, R_SPARC_OLO10 },
169 { BFD_RELOC_SPARC_HH22, R_SPARC_HH22 },
170 { BFD_RELOC_SPARC_HM10, R_SPARC_HM10 },
171 { BFD_RELOC_SPARC_LM22, R_SPARC_LM22 },
172 { BFD_RELOC_SPARC_PC_HH22, R_SPARC_PC_HH22 },
173 { BFD_RELOC_SPARC_PC_HM10, R_SPARC_PC_HM10 },
174 { BFD_RELOC_SPARC_PC_LM22, R_SPARC_PC_LM22 },
175 { BFD_RELOC_SPARC_WDISP16, R_SPARC_WDISP16 },
176 { BFD_RELOC_SPARC_WDISP19, R_SPARC_WDISP19 },
177 { BFD_RELOC_SPARC_7, R_SPARC_7 },
178 { BFD_RELOC_SPARC_5, R_SPARC_5 },
179 { BFD_RELOC_SPARC_6, R_SPARC_6 },
180 { BFD_RELOC_SPARC_REV32, R_SPARC_REV32 },
181 { BFD_RELOC_VTABLE_INHERIT, R_SPARC_GNU_VTINHERIT },
182 { BFD_RELOC_VTABLE_ENTRY, R_SPARC_GNU_VTENTRY },
185 static reloc_howto_type *
186 elf32_sparc_reloc_type_lookup (abfd, code)
187 bfd *abfd ATTRIBUTE_UNUSED;
188 bfd_reloc_code_real_type code;
190 unsigned int i;
192 switch (code)
194 case BFD_RELOC_VTABLE_INHERIT:
195 return &elf32_sparc_vtinherit_howto;
197 case BFD_RELOC_VTABLE_ENTRY:
198 return &elf32_sparc_vtentry_howto;
200 default:
201 for (i = 0; i < sizeof (sparc_reloc_map) / sizeof (struct elf_reloc_map); i++)
203 if (sparc_reloc_map[i].bfd_reloc_val == code)
204 return &_bfd_sparc_elf_howto_table[(int) sparc_reloc_map[i].elf_reloc_val];
207 bfd_set_error (bfd_error_bad_value);
208 return NULL;
211 /* We need to use ELF32_R_TYPE so we have our own copy of this function,
212 and elf64-sparc.c has its own copy. */
214 static void
215 elf32_sparc_info_to_howto (abfd, cache_ptr, dst)
216 bfd *abfd ATTRIBUTE_UNUSED;
217 arelent *cache_ptr;
218 Elf_Internal_Rela *dst;
220 switch (ELF32_R_TYPE(dst->r_info))
222 case R_SPARC_GNU_VTINHERIT:
223 cache_ptr->howto = &elf32_sparc_vtinherit_howto;
224 break;
226 case R_SPARC_GNU_VTENTRY:
227 cache_ptr->howto = &elf32_sparc_vtentry_howto;
228 break;
230 default:
231 BFD_ASSERT (ELF32_R_TYPE(dst->r_info) < (unsigned int) R_SPARC_max_std);
232 cache_ptr->howto = &_bfd_sparc_elf_howto_table[ELF32_R_TYPE(dst->r_info)];
236 /* For unsupported relocs. */
238 static bfd_reloc_status_type
239 sparc_elf_notsupported_reloc (abfd,
240 reloc_entry,
241 symbol,
242 data,
243 input_section,
244 output_bfd,
245 error_message)
246 bfd *abfd ATTRIBUTE_UNUSED;
247 arelent *reloc_entry ATTRIBUTE_UNUSED;
248 asymbol *symbol ATTRIBUTE_UNUSED;
249 PTR data ATTRIBUTE_UNUSED;
250 asection *input_section ATTRIBUTE_UNUSED;
251 bfd *output_bfd ATTRIBUTE_UNUSED;
252 char **error_message ATTRIBUTE_UNUSED;
254 return bfd_reloc_notsupported;
257 /* Handle the WDISP16 reloc. */
259 static bfd_reloc_status_type
260 sparc_elf_wdisp16_reloc (abfd,
261 reloc_entry,
262 symbol,
263 data,
264 input_section,
265 output_bfd,
266 error_message)
267 bfd *abfd;
268 arelent *reloc_entry;
269 asymbol *symbol;
270 PTR data;
271 asection *input_section;
272 bfd *output_bfd;
273 char **error_message ATTRIBUTE_UNUSED;
275 bfd_vma relocation;
276 bfd_vma x;
278 if (output_bfd != (bfd *) NULL
279 && (symbol->flags & BSF_SECTION_SYM) == 0
280 && (! reloc_entry->howto->partial_inplace
281 || reloc_entry->addend == 0))
283 reloc_entry->address += input_section->output_offset;
284 return bfd_reloc_ok;
287 if (output_bfd != NULL)
288 return bfd_reloc_continue;
290 if (reloc_entry->address > input_section->_cooked_size)
291 return bfd_reloc_outofrange;
293 relocation = (symbol->value
294 + symbol->section->output_section->vma
295 + symbol->section->output_offset);
296 relocation += reloc_entry->addend;
297 relocation -= (input_section->output_section->vma
298 + input_section->output_offset);
299 relocation -= reloc_entry->address;
301 x = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
302 x |= ((((relocation >> 2) & 0xc000) << 6)
303 | ((relocation >> 2) & 0x3fff));
304 bfd_put_32 (abfd, x, (bfd_byte *) data + reloc_entry->address);
306 if ((bfd_signed_vma) relocation < - 0x40000
307 || (bfd_signed_vma) relocation > 0x3ffff)
308 return bfd_reloc_overflow;
309 else
310 return bfd_reloc_ok;
313 /* Functions for the SPARC ELF linker. */
315 /* The name of the dynamic interpreter. This is put in the .interp
316 section. */
318 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
320 /* The nop opcode we use. */
322 #define SPARC_NOP 0x01000000
324 /* The size in bytes of an entry in the procedure linkage table. */
326 #define PLT_ENTRY_SIZE 12
328 /* The first four entries in a procedure linkage table are reserved,
329 and the initial contents are unimportant (we zero them out).
330 Subsequent entries look like this. See the SVR4 ABI SPARC
331 supplement to see how this works. */
333 /* sethi %hi(.-.plt0),%g1. We fill in the address later. */
334 #define PLT_ENTRY_WORD0 0x03000000
335 /* b,a .plt0. We fill in the offset later. */
336 #define PLT_ENTRY_WORD1 0x30800000
337 /* nop. */
338 #define PLT_ENTRY_WORD2 SPARC_NOP
340 /* Look through the relocs for a section during the first phase, and
341 allocate space in the global offset table or procedure linkage
342 table. */
344 static boolean
345 elf32_sparc_check_relocs (abfd, info, sec, relocs)
346 bfd *abfd;
347 struct bfd_link_info *info;
348 asection *sec;
349 const Elf_Internal_Rela *relocs;
351 bfd *dynobj;
352 Elf_Internal_Shdr *symtab_hdr;
353 struct elf_link_hash_entry **sym_hashes;
354 bfd_vma *local_got_offsets;
355 const Elf_Internal_Rela *rel;
356 const Elf_Internal_Rela *rel_end;
357 asection *sgot;
358 asection *srelgot;
359 asection *sreloc;
361 if (info->relocateable)
362 return true;
364 dynobj = elf_hash_table (info)->dynobj;
365 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
366 sym_hashes = elf_sym_hashes (abfd);
367 local_got_offsets = elf_local_got_offsets (abfd);
369 sgot = NULL;
370 srelgot = NULL;
371 sreloc = NULL;
373 rel_end = relocs + sec->reloc_count;
374 for (rel = relocs; rel < rel_end; rel++)
376 unsigned long r_symndx;
377 struct elf_link_hash_entry *h;
379 r_symndx = ELF32_R_SYM (rel->r_info);
380 if (r_symndx < symtab_hdr->sh_info)
381 h = NULL;
382 else
383 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
385 switch (ELF32_R_TYPE (rel->r_info))
387 case R_SPARC_GOT10:
388 case R_SPARC_GOT13:
389 case R_SPARC_GOT22:
390 /* This symbol requires a global offset table entry. */
392 if (dynobj == NULL)
394 /* Create the .got section. */
395 elf_hash_table (info)->dynobj = dynobj = abfd;
396 if (! _bfd_elf_create_got_section (dynobj, info))
397 return false;
400 if (sgot == NULL)
402 sgot = bfd_get_section_by_name (dynobj, ".got");
403 BFD_ASSERT (sgot != NULL);
406 if (srelgot == NULL
407 && (h != NULL || info->shared))
409 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
410 if (srelgot == NULL)
412 srelgot = bfd_make_section (dynobj, ".rela.got");
413 if (srelgot == NULL
414 || ! bfd_set_section_flags (dynobj, srelgot,
415 (SEC_ALLOC
416 | SEC_LOAD
417 | SEC_HAS_CONTENTS
418 | SEC_IN_MEMORY
419 | SEC_LINKER_CREATED
420 | SEC_READONLY))
421 || ! bfd_set_section_alignment (dynobj, srelgot, 2))
422 return false;
426 if (h != NULL)
428 if (h->got.offset != (bfd_vma) -1)
430 /* We have already allocated space in the .got. */
431 break;
433 h->got.offset = sgot->_raw_size;
435 /* Make sure this symbol is output as a dynamic symbol. */
436 if (h->dynindx == -1)
438 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
439 return false;
442 srelgot->_raw_size += sizeof (Elf32_External_Rela);
444 else
446 /* This is a global offset table entry for a local
447 symbol. */
448 if (local_got_offsets == NULL)
450 size_t size;
451 register unsigned int i;
453 size = symtab_hdr->sh_info * sizeof (bfd_vma);
454 local_got_offsets = (bfd_vma *) bfd_alloc (abfd, size);
455 if (local_got_offsets == NULL)
456 return false;
457 elf_local_got_offsets (abfd) = local_got_offsets;
458 for (i = 0; i < symtab_hdr->sh_info; i++)
459 local_got_offsets[i] = (bfd_vma) -1;
461 if (local_got_offsets[r_symndx] != (bfd_vma) -1)
463 /* We have already allocated space in the .got. */
464 break;
466 local_got_offsets[r_symndx] = sgot->_raw_size;
468 if (info->shared)
470 /* If we are generating a shared object, we need to
471 output a R_SPARC_RELATIVE reloc so that the
472 dynamic linker can adjust this GOT entry. */
473 srelgot->_raw_size += sizeof (Elf32_External_Rela);
477 sgot->_raw_size += 4;
479 /* If the .got section is more than 0x1000 bytes, we add
480 0x1000 to the value of _GLOBAL_OFFSET_TABLE_, so that 13
481 bit relocations have a greater chance of working. */
482 if (sgot->_raw_size >= 0x1000
483 && elf_hash_table (info)->hgot->root.u.def.value == 0)
484 elf_hash_table (info)->hgot->root.u.def.value = 0x1000;
486 break;
488 case R_SPARC_WPLT30:
489 /* This symbol requires a procedure linkage table entry. We
490 actually build the entry in adjust_dynamic_symbol,
491 because this might be a case of linking PIC code without
492 linking in any dynamic objects, in which case we don't
493 need to generate a procedure linkage table after all. */
495 if (h == NULL)
497 /* The Solaris native assembler will generate a WPLT30
498 reloc for a local symbol if you assemble a call from
499 one section to another when using -K pic. We treat
500 it as WDISP30. */
501 break;
504 /* Make sure this symbol is output as a dynamic symbol. */
505 if (h->dynindx == -1)
507 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
508 return false;
511 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
513 break;
515 case R_SPARC_PC10:
516 case R_SPARC_PC22:
517 if (h != NULL)
518 h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
520 if (h != NULL
521 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
522 break;
523 /* Fall through. */
524 case R_SPARC_DISP8:
525 case R_SPARC_DISP16:
526 case R_SPARC_DISP32:
527 case R_SPARC_WDISP30:
528 case R_SPARC_WDISP22:
529 case R_SPARC_WDISP19:
530 case R_SPARC_WDISP16:
531 if (h != NULL)
532 h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
534 /* If we are linking with -Bsymbolic, we do not need to copy
535 a PC relative reloc against a global symbol which is
536 defined in an object we are including in the link (i.e.,
537 DEF_REGULAR is set). FIXME: At this point we have not
538 seen all the input files, so it is possible that
539 DEF_REGULAR is not set now but will be set later (it is
540 never cleared). This needs to be handled as in
541 elf32-i386.c. */
542 if (h == NULL
543 || (info->symbolic
544 && (h->elf_link_hash_flags
545 & ELF_LINK_HASH_DEF_REGULAR) != 0))
546 break;
547 /* Fall through. */
548 case R_SPARC_8:
549 case R_SPARC_16:
550 case R_SPARC_32:
551 case R_SPARC_HI22:
552 case R_SPARC_22:
553 case R_SPARC_13:
554 case R_SPARC_LO10:
555 case R_SPARC_UA16:
556 case R_SPARC_UA32:
557 if (h != NULL)
558 h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
560 if (info->shared && (sec->flags & SEC_ALLOC))
562 /* When creating a shared object, we must copy these
563 relocs into the output file. We create a reloc
564 section in dynobj and make room for the reloc. */
565 if (sreloc == NULL)
567 const char *name;
569 name = (bfd_elf_string_from_elf_section
570 (abfd,
571 elf_elfheader (abfd)->e_shstrndx,
572 elf_section_data (sec)->rel_hdr.sh_name));
573 if (name == NULL)
574 return false;
576 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
577 && strcmp (bfd_get_section_name (abfd, sec),
578 name + 5) == 0);
580 sreloc = bfd_get_section_by_name (dynobj, name);
581 if (sreloc == NULL)
583 flagword flags;
585 sreloc = bfd_make_section (dynobj, name);
586 flags = (SEC_HAS_CONTENTS | SEC_READONLY
587 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
588 if ((sec->flags & SEC_ALLOC) != 0)
589 flags |= SEC_ALLOC | SEC_LOAD;
590 if (sreloc == NULL
591 || ! bfd_set_section_flags (dynobj, sreloc, flags)
592 || ! bfd_set_section_alignment (dynobj, sreloc, 2))
593 return false;
597 sreloc->_raw_size += sizeof (Elf32_External_Rela);
600 break;
602 case R_SPARC_GNU_VTINHERIT:
603 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
604 return false;
605 break;
607 case R_SPARC_GNU_VTENTRY:
608 if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend))
609 return false;
610 break;
612 default:
613 break;
617 return true;
620 static asection *
621 elf32_sparc_gc_mark_hook (abfd, info, rel, h, sym)
622 bfd *abfd;
623 struct bfd_link_info *info ATTRIBUTE_UNUSED;
624 Elf_Internal_Rela *rel;
625 struct elf_link_hash_entry *h;
626 Elf_Internal_Sym *sym;
629 if (h != NULL)
631 switch (ELF32_R_TYPE (rel->r_info))
633 case R_SPARC_GNU_VTINHERIT:
634 case R_SPARC_GNU_VTENTRY:
635 break;
637 default:
638 switch (h->root.type)
640 case bfd_link_hash_defined:
641 case bfd_link_hash_defweak:
642 return h->root.u.def.section;
644 case bfd_link_hash_common:
645 return h->root.u.c.p->section;
647 default:
648 break;
652 else
654 if (!(elf_bad_symtab (abfd)
655 && ELF_ST_BIND (sym->st_info) != STB_LOCAL)
656 && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE)
657 && sym->st_shndx != SHN_COMMON))
659 return bfd_section_from_elf_index (abfd, sym->st_shndx);
663 return NULL;
666 /* Update the got entry reference counts for the section being removed. */
667 static boolean
668 elf32_sparc_gc_sweep_hook (abfd, info, sec, relocs)
669 bfd *abfd;
670 struct bfd_link_info *info ATTRIBUTE_UNUSED;
671 asection *sec;
672 const Elf_Internal_Rela *relocs;
675 Elf_Internal_Shdr *symtab_hdr;
676 struct elf_link_hash_entry **sym_hashes;
677 bfd_signed_vma *local_got_refcounts;
678 const Elf_Internal_Rela *rel, *relend;
679 unsigned long r_symndx;
680 struct elf_link_hash_entry *h;
682 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
683 sym_hashes = elf_sym_hashes (abfd);
684 local_got_refcounts = elf_local_got_refcounts (abfd);
686 relend = relocs + sec->reloc_count;
687 for (rel = relocs; rel < relend; rel++)
688 switch (ELF32_R_TYPE (rel->r_info))
690 case R_SPARC_GOT10:
691 case R_SPARC_GOT13:
692 case R_SPARC_GOT22:
693 r_symndx = ELF32_R_SYM (rel->r_info);
694 if (r_symndx >= symtab_hdr->sh_info)
696 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
697 if (h->got.refcount > 0)
698 h->got.refcount--;
700 else
702 if (local_got_refcounts[r_symndx] > 0)
703 local_got_refcounts[r_symndx]--;
705 break;
707 case R_SPARC_PLT32:
708 case R_SPARC_HIPLT22:
709 case R_SPARC_LOPLT10:
710 case R_SPARC_PCPLT32:
711 case R_SPARC_PCPLT10:
712 r_symndx = ELF32_R_SYM (rel->r_info);
713 if (r_symndx >= symtab_hdr->sh_info)
715 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
716 if (h->plt.refcount > 0)
717 h->plt.refcount--;
719 break;
721 default:
722 break;
725 return true;
728 /* Adjust a symbol defined by a dynamic object and referenced by a
729 regular object. The current definition is in some section of the
730 dynamic object, but we're not including those sections. We have to
731 change the definition to something the rest of the link can
732 understand. */
734 static boolean
735 elf32_sparc_adjust_dynamic_symbol (info, h)
736 struct bfd_link_info *info;
737 struct elf_link_hash_entry *h;
739 bfd *dynobj;
740 asection *s;
741 unsigned int power_of_two;
743 dynobj = elf_hash_table (info)->dynobj;
745 /* Make sure we know what is going on here. */
746 BFD_ASSERT (dynobj != NULL
747 && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
748 || h->weakdef != NULL
749 || ((h->elf_link_hash_flags
750 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
751 && (h->elf_link_hash_flags
752 & ELF_LINK_HASH_REF_REGULAR) != 0
753 && (h->elf_link_hash_flags
754 & ELF_LINK_HASH_DEF_REGULAR) == 0)));
756 /* If this is a function, put it in the procedure linkage table. We
757 will fill in the contents of the procedure linkage table later
758 (although we could actually do it here). The STT_NOTYPE
759 condition is a hack specifically for the Oracle libraries
760 delivered for Solaris; for some inexplicable reason, they define
761 some of their functions as STT_NOTYPE when they really should be
762 STT_FUNC. */
763 if (h->type == STT_FUNC
764 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0
765 || (h->type == STT_NOTYPE
766 && (h->root.type == bfd_link_hash_defined
767 || h->root.type == bfd_link_hash_defweak)
768 && (h->root.u.def.section->flags & SEC_CODE) != 0))
770 if (! elf_hash_table (info)->dynamic_sections_created
771 || ((!info->shared || info->symbolic || h->dynindx == -1)
772 && (h->elf_link_hash_flags
773 & ELF_LINK_HASH_DEF_REGULAR) != 0))
775 /* This case can occur if we saw a WPLT30 reloc in an input
776 file, but none of the input files were dynamic objects.
777 Or, when linking the main application or a -Bsymbolic
778 shared library against PIC code. Or when a global symbol
779 has been made private, e.g. via versioning.
781 In these cases we know what value the symbol will resolve
782 to, so we don't actually need to build a procedure linkage
783 table, and we can just do a WDISP30 reloc instead. */
785 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
786 return true;
789 s = bfd_get_section_by_name (dynobj, ".plt");
790 BFD_ASSERT (s != NULL);
792 /* The first four entries in .plt are reserved. */
793 if (s->_raw_size == 0)
794 s->_raw_size = 4 * PLT_ENTRY_SIZE;
796 /* The procedure linkage table has a maximum size. */
797 if (s->_raw_size >= 0x400000)
799 bfd_set_error (bfd_error_bad_value);
800 return false;
803 /* If this symbol is not defined in a regular file, and we are
804 not generating a shared library, then set the symbol to this
805 location in the .plt. This is required to make function
806 pointers compare as equal between the normal executable and
807 the shared library. */
808 if (! info->shared
809 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
811 h->root.u.def.section = s;
812 h->root.u.def.value = s->_raw_size;
815 h->plt.offset = s->_raw_size;
817 /* Make room for this entry. */
818 s->_raw_size += PLT_ENTRY_SIZE;
820 /* We also need to make an entry in the .rela.plt section. */
822 s = bfd_get_section_by_name (dynobj, ".rela.plt");
823 BFD_ASSERT (s != NULL);
824 s->_raw_size += sizeof (Elf32_External_Rela);
826 return true;
829 /* If this is a weak symbol, and there is a real definition, the
830 processor independent code will have arranged for us to see the
831 real definition first, and we can just use the same value. */
832 if (h->weakdef != NULL)
834 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
835 || h->weakdef->root.type == bfd_link_hash_defweak);
836 h->root.u.def.section = h->weakdef->root.u.def.section;
837 h->root.u.def.value = h->weakdef->root.u.def.value;
838 return true;
841 /* This is a reference to a symbol defined by a dynamic object which
842 is not a function. */
844 /* If we are creating a shared library, we must presume that the
845 only references to the symbol are via the global offset table.
846 For such cases we need not do anything here; the relocations will
847 be handled correctly by relocate_section. */
848 if (info->shared)
849 return true;
851 /* If there are no references to this symbol that do not use the
852 GOT, we don't need to generate a copy reloc. */
853 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
854 return true;
856 /* We must allocate the symbol in our .dynbss section, which will
857 become part of the .bss section of the executable. There will be
858 an entry for this symbol in the .dynsym section. The dynamic
859 object will contain position independent code, so all references
860 from the dynamic object to this symbol will go through the global
861 offset table. The dynamic linker will use the .dynsym entry to
862 determine the address it must put in the global offset table, so
863 both the dynamic object and the regular object will refer to the
864 same memory location for the variable. */
866 s = bfd_get_section_by_name (dynobj, ".dynbss");
867 BFD_ASSERT (s != NULL);
869 /* We must generate a R_SPARC_COPY reloc to tell the dynamic linker
870 to copy the initial value out of the dynamic object and into the
871 runtime process image. We need to remember the offset into the
872 .rel.bss section we are going to use. */
873 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
875 asection *srel;
877 srel = bfd_get_section_by_name (dynobj, ".rela.bss");
878 BFD_ASSERT (srel != NULL);
879 srel->_raw_size += sizeof (Elf32_External_Rela);
880 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
883 /* We need to figure out the alignment required for this symbol. I
884 have no idea how ELF linkers handle this. */
885 power_of_two = bfd_log2 (h->size);
886 if (power_of_two > 3)
887 power_of_two = 3;
889 /* Apply the required alignment. */
890 s->_raw_size = BFD_ALIGN (s->_raw_size,
891 (bfd_size_type) (1 << power_of_two));
892 if (power_of_two > bfd_get_section_alignment (dynobj, s))
894 if (! bfd_set_section_alignment (dynobj, s, power_of_two))
895 return false;
898 /* Define the symbol as being at this point in the section. */
899 h->root.u.def.section = s;
900 h->root.u.def.value = s->_raw_size;
902 /* Increment the section size to make room for the symbol. */
903 s->_raw_size += h->size;
905 return true;
908 /* Set the sizes of the dynamic sections. */
910 static boolean
911 elf32_sparc_size_dynamic_sections (output_bfd, info)
912 bfd *output_bfd;
913 struct bfd_link_info *info;
915 bfd *dynobj;
916 asection *s;
917 boolean reltext;
918 boolean relplt;
920 dynobj = elf_hash_table (info)->dynobj;
921 BFD_ASSERT (dynobj != NULL);
923 if (elf_hash_table (info)->dynamic_sections_created)
925 /* Set the contents of the .interp section to the interpreter. */
926 if (! info->shared)
928 s = bfd_get_section_by_name (dynobj, ".interp");
929 BFD_ASSERT (s != NULL);
930 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
931 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
934 /* Make space for the trailing nop in .plt. */
935 s = bfd_get_section_by_name (dynobj, ".plt");
936 BFD_ASSERT (s != NULL);
937 if (s->_raw_size > 0)
938 s->_raw_size += 4;
940 else
942 /* We may have created entries in the .rela.got section.
943 However, if we are not creating the dynamic sections, we will
944 not actually use these entries. Reset the size of .rela.got,
945 which will cause it to get stripped from the output file
946 below. */
947 s = bfd_get_section_by_name (dynobj, ".rela.got");
948 if (s != NULL)
949 s->_raw_size = 0;
952 /* The check_relocs and adjust_dynamic_symbol entry points have
953 determined the sizes of the various dynamic sections. Allocate
954 memory for them. */
955 reltext = false;
956 relplt = false;
957 for (s = dynobj->sections; s != NULL; s = s->next)
959 const char *name;
960 boolean strip;
962 if ((s->flags & SEC_LINKER_CREATED) == 0)
963 continue;
965 /* It's OK to base decisions on the section name, because none
966 of the dynobj section names depend upon the input files. */
967 name = bfd_get_section_name (dynobj, s);
969 strip = false;
971 if (strncmp (name, ".rela", 5) == 0)
973 if (s->_raw_size == 0)
975 /* If we don't need this section, strip it from the
976 output file. This is to handle .rela.bss and
977 .rel.plt. We must create it in
978 create_dynamic_sections, because it must be created
979 before the linker maps input sections to output
980 sections. The linker does that before
981 adjust_dynamic_symbol is called, and it is that
982 function which decides whether anything needs to go
983 into these sections. */
984 strip = true;
986 else
988 const char *outname;
989 asection *target;
991 /* If this relocation section applies to a read only
992 section, then we probably need a DT_TEXTREL entry. */
993 outname = bfd_get_section_name (output_bfd,
994 s->output_section);
995 target = bfd_get_section_by_name (output_bfd, outname + 5);
996 if (target != NULL
997 && (target->flags & SEC_READONLY) != 0
998 && (target->flags & SEC_ALLOC) != 0)
999 reltext = true;
1001 if (strcmp (name, ".rela.plt") == 0)
1002 relplt = true;
1004 /* We use the reloc_count field as a counter if we need
1005 to copy relocs into the output file. */
1006 s->reloc_count = 0;
1009 else if (strcmp (name, ".plt") != 0
1010 && strcmp (name, ".got") != 0)
1012 /* It's not one of our sections, so don't allocate space. */
1013 continue;
1016 if (strip)
1018 _bfd_strip_section_from_output (info, s);
1019 continue;
1022 /* Allocate memory for the section contents. */
1023 /* FIXME: This should be a call to bfd_alloc not bfd_zalloc.
1024 Unused entries should be reclaimed before the section's contents
1025 are written out, but at the moment this does not happen. Thus in
1026 order to prevent writing out garbage, we initialise the section's
1027 contents to zero. */
1028 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
1029 if (s->contents == NULL && s->_raw_size != 0)
1030 return false;
1033 if (elf_hash_table (info)->dynamic_sections_created)
1035 /* Add some entries to the .dynamic section. We fill in the
1036 values later, in elf32_sparc_finish_dynamic_sections, but we
1037 must add the entries now so that we get the correct size for
1038 the .dynamic section. The DT_DEBUG entry is filled in by the
1039 dynamic linker and used by the debugger. */
1040 if (! info->shared)
1042 if (! bfd_elf32_add_dynamic_entry (info, DT_DEBUG, 0))
1043 return false;
1046 if (relplt)
1048 if (! bfd_elf32_add_dynamic_entry (info, DT_PLTGOT, 0)
1049 || ! bfd_elf32_add_dynamic_entry (info, DT_PLTRELSZ, 0)
1050 || ! bfd_elf32_add_dynamic_entry (info, DT_PLTREL, DT_RELA)
1051 || ! bfd_elf32_add_dynamic_entry (info, DT_JMPREL, 0))
1052 return false;
1055 if (! bfd_elf32_add_dynamic_entry (info, DT_RELA, 0)
1056 || ! bfd_elf32_add_dynamic_entry (info, DT_RELASZ, 0)
1057 || ! bfd_elf32_add_dynamic_entry (info, DT_RELAENT,
1058 sizeof (Elf32_External_Rela)))
1059 return false;
1061 if (reltext)
1063 if (! bfd_elf32_add_dynamic_entry (info, DT_TEXTREL, 0))
1064 return false;
1065 info->flags |= DF_TEXTREL;
1069 return true;
1072 #define SET_SEC_DO_RELAX(section) do { elf_section_data(section)->tdata = (void *)1; } while (0)
1073 #define SEC_DO_RELAX(section) (elf_section_data(section)->tdata == (void *)1)
1075 static boolean
1076 elf32_sparc_relax_section (abfd, section, link_info, again)
1077 bfd *abfd ATTRIBUTE_UNUSED;
1078 asection *section ATTRIBUTE_UNUSED;
1079 struct bfd_link_info *link_info ATTRIBUTE_UNUSED;
1080 boolean *again;
1082 *again = false;
1083 SET_SEC_DO_RELAX (section);
1084 return true;
1087 /* Relocate a SPARC ELF section. */
1089 static boolean
1090 elf32_sparc_relocate_section (output_bfd, info, input_bfd, input_section,
1091 contents, relocs, local_syms, local_sections)
1092 bfd *output_bfd;
1093 struct bfd_link_info *info;
1094 bfd *input_bfd;
1095 asection *input_section;
1096 bfd_byte *contents;
1097 Elf_Internal_Rela *relocs;
1098 Elf_Internal_Sym *local_syms;
1099 asection **local_sections;
1101 bfd *dynobj;
1102 Elf_Internal_Shdr *symtab_hdr;
1103 struct elf_link_hash_entry **sym_hashes;
1104 bfd_vma *local_got_offsets;
1105 bfd_vma got_base;
1106 asection *sgot;
1107 asection *splt;
1108 asection *sreloc;
1109 Elf_Internal_Rela *rel;
1110 Elf_Internal_Rela *relend;
1112 dynobj = elf_hash_table (info)->dynobj;
1113 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1114 sym_hashes = elf_sym_hashes (input_bfd);
1115 local_got_offsets = elf_local_got_offsets (input_bfd);
1117 if (elf_hash_table (info)->hgot == NULL)
1118 got_base = 0;
1119 else
1120 got_base = elf_hash_table (info)->hgot->root.u.def.value;
1122 sgot = NULL;
1123 splt = NULL;
1124 sreloc = NULL;
1126 rel = relocs;
1127 relend = relocs + input_section->reloc_count;
1128 for (; rel < relend; rel++)
1130 int r_type;
1131 reloc_howto_type *howto;
1132 unsigned long r_symndx;
1133 struct elf_link_hash_entry *h;
1134 Elf_Internal_Sym *sym;
1135 asection *sec;
1136 bfd_vma relocation;
1137 bfd_reloc_status_type r;
1139 r_type = ELF32_R_TYPE (rel->r_info);
1141 if (r_type == R_SPARC_GNU_VTINHERIT
1142 || r_type == R_SPARC_GNU_VTENTRY)
1143 continue;
1145 if (r_type < 0 || r_type >= (int) R_SPARC_max_std)
1147 bfd_set_error (bfd_error_bad_value);
1148 return false;
1150 howto = _bfd_sparc_elf_howto_table + r_type;
1152 r_symndx = ELF32_R_SYM (rel->r_info);
1154 if (info->relocateable)
1156 /* This is a relocateable link. We don't have to change
1157 anything, unless the reloc is against a section symbol,
1158 in which case we have to adjust according to where the
1159 section symbol winds up in the output section. */
1160 if (r_symndx < symtab_hdr->sh_info)
1162 sym = local_syms + r_symndx;
1163 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
1165 sec = local_sections[r_symndx];
1166 rel->r_addend += sec->output_offset + sym->st_value;
1170 continue;
1173 /* This is a final link. */
1174 h = NULL;
1175 sym = NULL;
1176 sec = NULL;
1177 if (r_symndx < symtab_hdr->sh_info)
1179 sym = local_syms + r_symndx;
1180 sec = local_sections[r_symndx];
1181 relocation = (sec->output_section->vma
1182 + sec->output_offset
1183 + sym->st_value);
1185 else
1187 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1188 while (h->root.type == bfd_link_hash_indirect
1189 || h->root.type == bfd_link_hash_warning)
1190 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1191 if (h->root.type == bfd_link_hash_defined
1192 || h->root.type == bfd_link_hash_defweak)
1194 sec = h->root.u.def.section;
1195 if ((r_type == R_SPARC_WPLT30
1196 && h->plt.offset != (bfd_vma) -1)
1197 || ((r_type == R_SPARC_GOT10
1198 || r_type == R_SPARC_GOT13
1199 || r_type == R_SPARC_GOT22)
1200 && elf_hash_table (info)->dynamic_sections_created
1201 && (! info->shared
1202 || (! info->symbolic && h->dynindx != -1)
1203 || (h->elf_link_hash_flags
1204 & ELF_LINK_HASH_DEF_REGULAR) == 0))
1205 || (info->shared
1206 && ((! info->symbolic && h->dynindx != -1)
1207 || (h->elf_link_hash_flags
1208 & ELF_LINK_HASH_DEF_REGULAR) == 0)
1209 && (r_type == R_SPARC_8
1210 || r_type == R_SPARC_16
1211 || r_type == R_SPARC_32
1212 || r_type == R_SPARC_DISP8
1213 || r_type == R_SPARC_DISP16
1214 || r_type == R_SPARC_DISP32
1215 || r_type == R_SPARC_WDISP30
1216 || r_type == R_SPARC_WDISP22
1217 || r_type == R_SPARC_WDISP19
1218 || r_type == R_SPARC_WDISP16
1219 || r_type == R_SPARC_HI22
1220 || r_type == R_SPARC_22
1221 || r_type == R_SPARC_13
1222 || r_type == R_SPARC_LO10
1223 || r_type == R_SPARC_UA16
1224 || r_type == R_SPARC_UA32
1225 || ((r_type == R_SPARC_PC10
1226 || r_type == R_SPARC_PC22)
1227 && strcmp (h->root.root.string,
1228 "_GLOBAL_OFFSET_TABLE_") != 0))))
1230 /* In these cases, we don't need the relocation
1231 value. We check specially because in some
1232 obscure cases sec->output_section will be NULL. */
1233 relocation = 0;
1235 else
1236 relocation = (h->root.u.def.value
1237 + sec->output_section->vma
1238 + sec->output_offset);
1240 else if (h->root.type == bfd_link_hash_undefweak)
1241 relocation = 0;
1242 else if (info->shared && !info->symbolic
1243 && !info->no_undefined
1244 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
1245 relocation = 0;
1246 else
1248 if (! ((*info->callbacks->undefined_symbol)
1249 (info, h->root.root.string, input_bfd,
1250 input_section, rel->r_offset,
1251 (!info->shared || info->no_undefined
1252 || ELF_ST_VISIBILITY (h->other)))))
1253 return false;
1254 relocation = 0;
1258 switch (r_type)
1260 case R_SPARC_GOT10:
1261 case R_SPARC_GOT13:
1262 case R_SPARC_GOT22:
1263 /* Relocation is to the entry for this symbol in the global
1264 offset table. */
1265 if (sgot == NULL)
1267 sgot = bfd_get_section_by_name (dynobj, ".got");
1268 BFD_ASSERT (sgot != NULL);
1271 if (h != NULL)
1273 bfd_vma off;
1275 off = h->got.offset;
1276 BFD_ASSERT (off != (bfd_vma) -1);
1278 if (! elf_hash_table (info)->dynamic_sections_created
1279 || (info->shared
1280 && (info->symbolic || h->dynindx == -1)
1281 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
1283 /* This is actually a static link, or it is a
1284 -Bsymbolic link and the symbol is defined
1285 locally, or the symbol was forced to be local
1286 because of a version file. We must initialize
1287 this entry in the global offset table. Since the
1288 offset must always be a multiple of 4, we use the
1289 least significant bit to record whether we have
1290 initialized it already.
1292 When doing a dynamic link, we create a .rela.got
1293 relocation entry to initialize the value. This
1294 is done in the finish_dynamic_symbol routine. */
1295 if ((off & 1) != 0)
1296 off &= ~1;
1297 else
1299 bfd_put_32 (output_bfd, relocation,
1300 sgot->contents + off);
1301 h->got.offset |= 1;
1305 relocation = sgot->output_offset + off - got_base;
1307 else
1309 bfd_vma off;
1311 BFD_ASSERT (local_got_offsets != NULL
1312 && local_got_offsets[r_symndx] != (bfd_vma) -1);
1314 off = local_got_offsets[r_symndx];
1316 /* The offset must always be a multiple of 4. We use
1317 the least significant bit to record whether we have
1318 already processed this entry. */
1319 if ((off & 1) != 0)
1320 off &= ~1;
1321 else
1323 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
1325 if (info->shared)
1327 asection *srelgot;
1328 Elf_Internal_Rela outrel;
1330 /* We need to generate a R_SPARC_RELATIVE reloc
1331 for the dynamic linker. */
1332 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
1333 BFD_ASSERT (srelgot != NULL);
1335 outrel.r_offset = (sgot->output_section->vma
1336 + sgot->output_offset
1337 + off);
1338 outrel.r_info = ELF32_R_INFO (0, R_SPARC_RELATIVE);
1339 outrel.r_addend = 0;
1340 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1341 (((Elf32_External_Rela *)
1342 srelgot->contents)
1343 + srelgot->reloc_count));
1344 ++srelgot->reloc_count;
1347 local_got_offsets[r_symndx] |= 1;
1350 relocation = sgot->output_offset + off - got_base;
1353 break;
1355 case R_SPARC_WPLT30:
1356 /* Relocation is to the entry for this symbol in the
1357 procedure linkage table. */
1359 /* The Solaris native assembler will generate a WPLT30 reloc
1360 for a local symbol if you assemble a call from one
1361 section to another when using -K pic. We treat it as
1362 WDISP30. */
1363 if (h == NULL)
1364 break;
1366 if (h->plt.offset == (bfd_vma) -1)
1368 /* We didn't make a PLT entry for this symbol. This
1369 happens when statically linking PIC code, or when
1370 using -Bsymbolic. */
1371 break;
1374 if (splt == NULL)
1376 splt = bfd_get_section_by_name (dynobj, ".plt");
1377 BFD_ASSERT (splt != NULL);
1380 relocation = (splt->output_section->vma
1381 + splt->output_offset
1382 + h->plt.offset);
1383 break;
1385 case R_SPARC_PC10:
1386 case R_SPARC_PC22:
1387 if (h != NULL
1388 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
1389 break;
1390 /* Fall through. */
1391 case R_SPARC_DISP8:
1392 case R_SPARC_DISP16:
1393 case R_SPARC_DISP32:
1394 case R_SPARC_WDISP30:
1395 case R_SPARC_WDISP22:
1396 case R_SPARC_WDISP19:
1397 case R_SPARC_WDISP16:
1398 if (h == NULL
1399 || (info->symbolic
1400 && (h->elf_link_hash_flags
1401 & ELF_LINK_HASH_DEF_REGULAR) != 0))
1402 break;
1403 /* Fall through. */
1404 case R_SPARC_8:
1405 case R_SPARC_16:
1406 case R_SPARC_32:
1407 case R_SPARC_HI22:
1408 case R_SPARC_22:
1409 case R_SPARC_13:
1410 case R_SPARC_LO10:
1411 case R_SPARC_UA16:
1412 case R_SPARC_UA32:
1413 if (info->shared && (input_section->flags & SEC_ALLOC))
1415 Elf_Internal_Rela outrel;
1416 boolean skip;
1418 /* When generating a shared object, these relocations
1419 are copied into the output file to be resolved at run
1420 time. */
1422 if (sreloc == NULL)
1424 const char *name;
1426 name = (bfd_elf_string_from_elf_section
1427 (input_bfd,
1428 elf_elfheader (input_bfd)->e_shstrndx,
1429 elf_section_data (input_section)->rel_hdr.sh_name));
1430 if (name == NULL)
1431 return false;
1433 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
1434 && strcmp (bfd_get_section_name (input_bfd,
1435 input_section),
1436 name + 5) == 0);
1438 sreloc = bfd_get_section_by_name (dynobj, name);
1439 BFD_ASSERT (sreloc != NULL);
1442 skip = false;
1444 if (elf_section_data (input_section)->stab_info == NULL)
1445 outrel.r_offset = rel->r_offset;
1446 else
1448 bfd_vma off;
1450 off = (_bfd_stab_section_offset
1451 (output_bfd, &elf_hash_table (info)->stab_info,
1452 input_section,
1453 &elf_section_data (input_section)->stab_info,
1454 rel->r_offset));
1455 if (off == (bfd_vma) -1)
1456 skip = true;
1457 outrel.r_offset = off;
1460 outrel.r_offset += (input_section->output_section->vma
1461 + input_section->output_offset);
1463 /* Optimize unaligned reloc usage now that we know where
1464 it finally resides. */
1465 switch (r_type)
1467 case R_SPARC_16:
1468 if (outrel.r_offset & 1)
1469 r_type = R_SPARC_UA16;
1470 break;
1471 case R_SPARC_UA16:
1472 if (!(outrel.r_offset & 1))
1473 r_type = R_SPARC_16;
1474 break;
1475 case R_SPARC_32:
1476 if (outrel.r_offset & 3)
1477 r_type = R_SPARC_UA32;
1478 break;
1479 case R_SPARC_UA32:
1480 if (!(outrel.r_offset & 3))
1481 r_type = R_SPARC_32;
1482 break;
1485 if (skip)
1486 memset (&outrel, 0, sizeof outrel);
1487 /* h->dynindx may be -1 if the symbol was marked to
1488 become local. */
1489 else if (h != NULL
1490 && ((! info->symbolic && h->dynindx != -1)
1491 || (h->elf_link_hash_flags
1492 & ELF_LINK_HASH_DEF_REGULAR) == 0))
1494 BFD_ASSERT (h->dynindx != -1);
1495 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
1496 outrel.r_addend = rel->r_addend;
1498 else
1500 if (r_type == R_SPARC_32)
1502 outrel.r_info = ELF32_R_INFO (0, R_SPARC_RELATIVE);
1503 outrel.r_addend = relocation + rel->r_addend;
1505 else
1507 long indx;
1509 if (h == NULL)
1510 sec = local_sections[r_symndx];
1511 else
1513 BFD_ASSERT (h->root.type == bfd_link_hash_defined
1514 || (h->root.type
1515 == bfd_link_hash_defweak));
1516 sec = h->root.u.def.section;
1518 if (sec != NULL && bfd_is_abs_section (sec))
1519 indx = 0;
1520 else if (sec == NULL || sec->owner == NULL)
1522 bfd_set_error (bfd_error_bad_value);
1523 return false;
1525 else
1527 asection *osec;
1529 osec = sec->output_section;
1530 indx = elf_section_data (osec)->dynindx;
1532 /* FIXME: we really should be able to link non-pic
1533 shared libraries. */
1534 if (indx == 0)
1536 BFD_FAIL ();
1537 (*_bfd_error_handler)
1538 (_("%s: probably compiled without -fPIC?"),
1539 bfd_get_filename (input_bfd));
1540 bfd_set_error (bfd_error_bad_value);
1541 return false;
1545 outrel.r_info = ELF32_R_INFO (indx, r_type);
1546 outrel.r_addend = relocation + rel->r_addend;
1550 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1551 (((Elf32_External_Rela *)
1552 sreloc->contents)
1553 + sreloc->reloc_count));
1554 ++sreloc->reloc_count;
1556 /* This reloc will be computed at runtime, so there's no
1557 need to do anything now. */
1558 continue;
1560 break;
1562 default:
1563 break;
1566 r = bfd_reloc_continue;
1567 if (r_type == R_SPARC_WDISP16)
1569 bfd_vma x;
1571 relocation += rel->r_addend;
1572 relocation -= (input_section->output_section->vma
1573 + input_section->output_offset);
1574 relocation -= rel->r_offset;
1576 x = bfd_get_32 (input_bfd, contents + rel->r_offset);
1577 x |= ((((relocation >> 2) & 0xc000) << 6)
1578 | ((relocation >> 2) & 0x3fff));
1579 bfd_put_32 (input_bfd, x, contents + rel->r_offset);
1581 if ((bfd_signed_vma) relocation < - 0x40000
1582 || (bfd_signed_vma) relocation > 0x3ffff)
1583 r = bfd_reloc_overflow;
1584 else
1585 r = bfd_reloc_ok;
1587 else if (r_type == R_SPARC_REV32)
1589 bfd_vma x;
1591 relocation = relocation + rel->r_addend;
1593 x = bfd_get_32 (input_bfd, contents + rel->r_offset);
1594 x = x + relocation;
1595 bfd_putl32 (/*input_bfd,*/ x, contents + rel->r_offset);
1596 r = bfd_reloc_ok;
1598 else if ((r_type == R_SPARC_WDISP30 || r_type == R_SPARC_WPLT30)
1599 && SEC_DO_RELAX (input_section)
1600 && rel->r_offset + 4 < input_section->_raw_size)
1602 #define G0 0
1603 #define O7 15
1604 #define XCC (2 << 20)
1605 #define COND(x) (((x)&0xf)<<25)
1606 #define CONDA COND(0x8)
1607 #define INSN_BPA (F2(0,1) | CONDA | BPRED | XCC)
1608 #define INSN_BA (F2(0,2) | CONDA)
1609 #define INSN_OR F3(2, 0x2, 0)
1610 #define INSN_NOP F2(0,4)
1612 bfd_vma x, y;
1614 /* If the instruction is a call with either:
1615 restore
1616 arithmetic instruction with rd == %o7
1617 where rs1 != %o7 and rs2 if it is register != %o7
1618 then we can optimize if the call destination is near
1619 by changing the call into a branch always. */
1620 x = bfd_get_32 (input_bfd, contents + rel->r_offset);
1621 y = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
1622 if ((x & OP(~0)) == OP(1) && (y & OP(~0)) == OP(2))
1624 if (((y & OP3(~0)) == OP3(0x3d) /* restore */
1625 || ((y & OP3(0x28)) == 0 /* arithmetic */
1626 && (y & RD(~0)) == RD(O7)))
1627 && (y & RS1(~0)) != RS1(O7)
1628 && ((y & F3I(~0))
1629 || (y & RS2(~0)) != RS2(O7)))
1631 bfd_vma reloc;
1633 reloc = relocation + rel->r_addend - rel->r_offset;
1634 reloc -= (input_section->output_section->vma
1635 + input_section->output_offset);
1637 /* Ensure the reloc fits into simm22. */
1638 if ((reloc & 3) == 0
1639 && ((reloc & ~(bfd_vma)0x7fffff) == 0
1640 || ((reloc | 0x7fffff) == ~(bfd_vma)0)))
1642 reloc >>= 2;
1644 /* Check whether it fits into simm19 on v9. */
1645 if (((reloc & 0x3c0000) == 0
1646 || (reloc & 0x3c0000) == 0x3c0000)
1647 && (elf_elfheader (output_bfd)->e_flags & EF_SPARC_32PLUS))
1648 x = INSN_BPA | (reloc & 0x7ffff); /* ba,pt %xcc */
1649 else
1650 x = INSN_BA | (reloc & 0x3fffff); /* ba */
1651 bfd_put_32 (input_bfd, x, contents + rel->r_offset);
1652 r = bfd_reloc_ok;
1653 if (rel->r_offset >= 4
1654 && (y & (0xffffffff ^ RS1(~0)))
1655 == (INSN_OR | RD(O7) | RS2(G0)))
1657 bfd_vma z;
1658 unsigned int reg;
1660 z = bfd_get_32 (input_bfd,
1661 contents + rel->r_offset - 4);
1662 if ((z & (0xffffffff ^ RD(~0)))
1663 != (INSN_OR | RS1(O7) | RS2(G0)))
1664 break;
1666 /* The sequence was
1667 or %o7, %g0, %rN
1668 call foo
1669 or %rN, %g0, %o7
1671 If call foo was replaced with ba, replace
1672 or %rN, %g0, %o7 with nop. */
1674 reg = (y & RS1(~0)) >> 14;
1675 if (reg != ((z & RD(~0)) >> 25)
1676 || reg == G0 || reg == O7)
1677 break;
1679 bfd_put_32 (input_bfd, INSN_NOP,
1680 contents + rel->r_offset + 4);
1688 if (r == bfd_reloc_continue)
1689 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1690 contents, rel->r_offset,
1691 relocation, rel->r_addend);
1693 if (r != bfd_reloc_ok)
1695 switch (r)
1697 default:
1698 case bfd_reloc_outofrange:
1699 abort ();
1700 case bfd_reloc_overflow:
1702 const char *name;
1704 if (h != NULL)
1705 name = h->root.root.string;
1706 else
1708 name = bfd_elf_string_from_elf_section (input_bfd,
1709 symtab_hdr->sh_link,
1710 sym->st_name);
1711 if (name == NULL)
1712 return false;
1713 if (*name == '\0')
1714 name = bfd_section_name (input_bfd, sec);
1716 if (! ((*info->callbacks->reloc_overflow)
1717 (info, name, howto->name, (bfd_vma) 0,
1718 input_bfd, input_section, rel->r_offset)))
1719 return false;
1721 break;
1726 return true;
1729 /* Finish up dynamic symbol handling. We set the contents of various
1730 dynamic sections here. */
1732 static boolean
1733 elf32_sparc_finish_dynamic_symbol (output_bfd, info, h, sym)
1734 bfd *output_bfd;
1735 struct bfd_link_info *info;
1736 struct elf_link_hash_entry *h;
1737 Elf_Internal_Sym *sym;
1739 bfd *dynobj;
1741 dynobj = elf_hash_table (info)->dynobj;
1743 if (h->plt.offset != (bfd_vma) -1)
1745 asection *splt;
1746 asection *srela;
1747 Elf_Internal_Rela rela;
1749 /* This symbol has an entry in the procedure linkage table. Set
1750 it up. */
1752 BFD_ASSERT (h->dynindx != -1);
1754 splt = bfd_get_section_by_name (dynobj, ".plt");
1755 srela = bfd_get_section_by_name (dynobj, ".rela.plt");
1756 BFD_ASSERT (splt != NULL && srela != NULL);
1758 /* Fill in the entry in the procedure linkage table. */
1759 bfd_put_32 (output_bfd,
1760 PLT_ENTRY_WORD0 + h->plt.offset,
1761 splt->contents + h->plt.offset);
1762 bfd_put_32 (output_bfd,
1763 (PLT_ENTRY_WORD1
1764 + (((- (h->plt.offset + 4)) >> 2) & 0x3fffff)),
1765 splt->contents + h->plt.offset + 4);
1766 bfd_put_32 (output_bfd, PLT_ENTRY_WORD2,
1767 splt->contents + h->plt.offset + 8);
1769 /* Fill in the entry in the .rela.plt section. */
1770 rela.r_offset = (splt->output_section->vma
1771 + splt->output_offset
1772 + h->plt.offset);
1773 rela.r_info = ELF32_R_INFO (h->dynindx, R_SPARC_JMP_SLOT);
1774 rela.r_addend = 0;
1775 bfd_elf32_swap_reloca_out (output_bfd, &rela,
1776 ((Elf32_External_Rela *) srela->contents
1777 + h->plt.offset / PLT_ENTRY_SIZE - 4));
1779 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1781 /* Mark the symbol as undefined, rather than as defined in
1782 the .plt section. Leave the value alone. */
1783 sym->st_shndx = SHN_UNDEF;
1784 /* If the symbol is weak, we do need to clear the value.
1785 Otherwise, the PLT entry would provide a definition for
1786 the symbol even if the symbol wasn't defined anywhere,
1787 and so the symbol would never be NULL. */
1788 if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR_NONWEAK)
1789 == 0)
1790 sym->st_value = 0;
1794 if (h->got.offset != (bfd_vma) -1)
1796 asection *sgot;
1797 asection *srela;
1798 Elf_Internal_Rela rela;
1800 /* This symbol has an entry in the global offset table. Set it
1801 up. */
1803 sgot = bfd_get_section_by_name (dynobj, ".got");
1804 srela = bfd_get_section_by_name (dynobj, ".rela.got");
1805 BFD_ASSERT (sgot != NULL && srela != NULL);
1807 rela.r_offset = (sgot->output_section->vma
1808 + sgot->output_offset
1809 + (h->got.offset &~ 1));
1811 /* If this is a -Bsymbolic link, and the symbol is defined
1812 locally, we just want to emit a RELATIVE reloc. Likewise if
1813 the symbol was forced to be local because of a version file.
1814 The entry in the global offset table will already have been
1815 initialized in the relocate_section function. */
1816 if (info->shared
1817 && (info->symbolic || h->dynindx == -1)
1818 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
1819 rela.r_info = ELF32_R_INFO (0, R_SPARC_RELATIVE);
1820 else
1822 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
1823 rela.r_info = ELF32_R_INFO (h->dynindx, R_SPARC_GLOB_DAT);
1826 rela.r_addend = 0;
1827 bfd_elf32_swap_reloca_out (output_bfd, &rela,
1828 ((Elf32_External_Rela *) srela->contents
1829 + srela->reloc_count));
1830 ++srela->reloc_count;
1833 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
1835 asection *s;
1836 Elf_Internal_Rela rela;
1838 /* This symbols needs a copy reloc. Set it up. */
1840 BFD_ASSERT (h->dynindx != -1);
1842 s = bfd_get_section_by_name (h->root.u.def.section->owner,
1843 ".rela.bss");
1844 BFD_ASSERT (s != NULL);
1846 rela.r_offset = (h->root.u.def.value
1847 + h->root.u.def.section->output_section->vma
1848 + h->root.u.def.section->output_offset);
1849 rela.r_info = ELF32_R_INFO (h->dynindx, R_SPARC_COPY);
1850 rela.r_addend = 0;
1851 bfd_elf32_swap_reloca_out (output_bfd, &rela,
1852 ((Elf32_External_Rela *) s->contents
1853 + s->reloc_count));
1854 ++s->reloc_count;
1857 /* Mark some specially defined symbols as absolute. */
1858 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
1859 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
1860 || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0)
1861 sym->st_shndx = SHN_ABS;
1863 return true;
1866 /* Finish up the dynamic sections. */
1868 static boolean
1869 elf32_sparc_finish_dynamic_sections (output_bfd, info)
1870 bfd *output_bfd;
1871 struct bfd_link_info *info;
1873 bfd *dynobj;
1874 asection *sdyn;
1875 asection *sgot;
1877 dynobj = elf_hash_table (info)->dynobj;
1879 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
1881 if (elf_hash_table (info)->dynamic_sections_created)
1883 asection *splt;
1884 Elf32_External_Dyn *dyncon, *dynconend;
1886 splt = bfd_get_section_by_name (dynobj, ".plt");
1887 BFD_ASSERT (splt != NULL && sdyn != NULL);
1889 dyncon = (Elf32_External_Dyn *) sdyn->contents;
1890 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
1891 for (; dyncon < dynconend; dyncon++)
1893 Elf_Internal_Dyn dyn;
1894 const char *name;
1895 boolean size;
1897 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
1899 switch (dyn.d_tag)
1901 case DT_PLTGOT: name = ".plt"; size = false; break;
1902 case DT_PLTRELSZ: name = ".rela.plt"; size = true; break;
1903 case DT_JMPREL: name = ".rela.plt"; size = false; break;
1904 default: name = NULL; size = false; break;
1907 if (name != NULL)
1909 asection *s;
1911 s = bfd_get_section_by_name (output_bfd, name);
1912 if (s == NULL)
1913 dyn.d_un.d_val = 0;
1914 else
1916 if (! size)
1917 dyn.d_un.d_ptr = s->vma;
1918 else
1920 if (s->_cooked_size != 0)
1921 dyn.d_un.d_val = s->_cooked_size;
1922 else
1923 dyn.d_un.d_val = s->_raw_size;
1926 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1930 /* Clear the first four entries in the procedure linkage table,
1931 and put a nop in the last four bytes. */
1932 if (splt->_raw_size > 0)
1934 memset (splt->contents, 0, 4 * PLT_ENTRY_SIZE);
1935 bfd_put_32 (output_bfd, SPARC_NOP,
1936 splt->contents + splt->_raw_size - 4);
1939 elf_section_data (splt->output_section)->this_hdr.sh_entsize =
1940 PLT_ENTRY_SIZE;
1943 /* Set the first entry in the global offset table to the address of
1944 the dynamic section. */
1945 sgot = bfd_get_section_by_name (dynobj, ".got");
1946 BFD_ASSERT (sgot != NULL);
1947 if (sgot->_raw_size > 0)
1949 if (sdyn == NULL)
1950 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
1951 else
1952 bfd_put_32 (output_bfd,
1953 sdyn->output_section->vma + sdyn->output_offset,
1954 sgot->contents);
1957 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
1959 return true;
1962 /* Functions for dealing with the e_flags field.
1964 We don't define set_private_flags or copy_private_bfd_data because
1965 the only currently defined values are based on the bfd mach number,
1966 so we use the latter instead and defer setting e_flags until the
1967 file is written out. */
1969 /* Merge backend specific data from an object file to the output
1970 object file when linking. */
1972 static boolean
1973 elf32_sparc_merge_private_bfd_data (ibfd, obfd)
1974 bfd *ibfd;
1975 bfd *obfd;
1977 boolean error;
1978 /* FIXME: This should not be static. */
1979 static unsigned long previous_ibfd_e_flags = (unsigned long) -1;
1981 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1982 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1983 return true;
1985 error = false;
1987 if (bfd_get_mach (ibfd) >= bfd_mach_sparc_v9)
1989 error = true;
1990 (*_bfd_error_handler)
1991 (_("%s: compiled for a 64 bit system and target is 32 bit"),
1992 bfd_get_filename (ibfd));
1994 else if ((ibfd->flags & DYNAMIC) == 0)
1996 if (bfd_get_mach (obfd) < bfd_get_mach (ibfd))
1997 bfd_set_arch_mach (obfd, bfd_arch_sparc, bfd_get_mach (ibfd));
2000 if (((elf_elfheader (ibfd)->e_flags & EF_SPARC_LEDATA)
2001 != previous_ibfd_e_flags)
2002 && previous_ibfd_e_flags != (unsigned long) -1)
2004 (*_bfd_error_handler)
2005 (_("%s: linking little endian files with big endian files"),
2006 bfd_get_filename (ibfd));
2007 error = true;
2009 previous_ibfd_e_flags = elf_elfheader (ibfd)->e_flags & EF_SPARC_LEDATA;
2011 if (error)
2013 bfd_set_error (bfd_error_bad_value);
2014 return false;
2017 return true;
2020 /* Set the right machine number. */
2022 static boolean
2023 elf32_sparc_object_p (abfd)
2024 bfd *abfd;
2026 if (elf_elfheader (abfd)->e_machine == EM_SPARC32PLUS)
2028 if (elf_elfheader (abfd)->e_flags & EF_SPARC_SUN_US3)
2029 return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
2030 bfd_mach_sparc_v8plusb);
2031 else if (elf_elfheader (abfd)->e_flags & EF_SPARC_SUN_US1)
2032 return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
2033 bfd_mach_sparc_v8plusa);
2034 else if (elf_elfheader (abfd)->e_flags & EF_SPARC_32PLUS)
2035 return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
2036 bfd_mach_sparc_v8plus);
2037 else
2038 return false;
2040 else if (elf_elfheader (abfd)->e_flags & EF_SPARC_LEDATA)
2041 return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
2042 bfd_mach_sparc_sparclite_le);
2043 else
2044 return bfd_default_set_arch_mach (abfd, bfd_arch_sparc, bfd_mach_sparc);
2047 /* The final processing done just before writing out the object file.
2048 We need to set the e_machine field appropriately. */
2050 static void
2051 elf32_sparc_final_write_processing (abfd, linker)
2052 bfd *abfd;
2053 boolean linker ATTRIBUTE_UNUSED;
2055 switch (bfd_get_mach (abfd))
2057 case bfd_mach_sparc :
2058 break; /* nothing to do */
2059 case bfd_mach_sparc_v8plus :
2060 elf_elfheader (abfd)->e_machine = EM_SPARC32PLUS;
2061 elf_elfheader (abfd)->e_flags &=~ EF_SPARC_32PLUS_MASK;
2062 elf_elfheader (abfd)->e_flags |= EF_SPARC_32PLUS;
2063 break;
2064 case bfd_mach_sparc_v8plusa :
2065 elf_elfheader (abfd)->e_machine = EM_SPARC32PLUS;
2066 elf_elfheader (abfd)->e_flags &=~ EF_SPARC_32PLUS_MASK;
2067 elf_elfheader (abfd)->e_flags |= EF_SPARC_32PLUS | EF_SPARC_SUN_US1;
2068 break;
2069 case bfd_mach_sparc_v8plusb :
2070 elf_elfheader (abfd)->e_machine = EM_SPARC32PLUS;
2071 elf_elfheader (abfd)->e_flags &=~ EF_SPARC_32PLUS_MASK;
2072 elf_elfheader (abfd)->e_flags |= EF_SPARC_32PLUS | EF_SPARC_SUN_US1
2073 | EF_SPARC_SUN_US3;
2074 break;
2075 case bfd_mach_sparc_sparclite_le :
2076 elf_elfheader (abfd)->e_machine = EM_SPARC;
2077 elf_elfheader (abfd)->e_flags |= EF_SPARC_LEDATA;
2078 break;
2079 default :
2080 abort ();
2081 break;
2085 #define TARGET_BIG_SYM bfd_elf32_sparc_vec
2086 #define TARGET_BIG_NAME "elf32-sparc"
2087 #define ELF_ARCH bfd_arch_sparc
2088 #define ELF_MACHINE_CODE EM_SPARC
2089 #define ELF_MACHINE_ALT1 EM_SPARC32PLUS
2090 #define ELF_MAXPAGESIZE 0x10000
2092 #define bfd_elf32_bfd_reloc_type_lookup elf32_sparc_reloc_type_lookup
2093 #define bfd_elf32_bfd_relax_section elf32_sparc_relax_section
2094 #define elf_info_to_howto elf32_sparc_info_to_howto
2095 #define elf_backend_create_dynamic_sections \
2096 _bfd_elf_create_dynamic_sections
2097 #define elf_backend_check_relocs elf32_sparc_check_relocs
2098 #define elf_backend_adjust_dynamic_symbol \
2099 elf32_sparc_adjust_dynamic_symbol
2100 #define elf_backend_size_dynamic_sections \
2101 elf32_sparc_size_dynamic_sections
2102 #define elf_backend_relocate_section elf32_sparc_relocate_section
2103 #define elf_backend_finish_dynamic_symbol \
2104 elf32_sparc_finish_dynamic_symbol
2105 #define elf_backend_finish_dynamic_sections \
2106 elf32_sparc_finish_dynamic_sections
2107 #define bfd_elf32_bfd_merge_private_bfd_data \
2108 elf32_sparc_merge_private_bfd_data
2109 #define elf_backend_object_p elf32_sparc_object_p
2110 #define elf_backend_final_write_processing \
2111 elf32_sparc_final_write_processing
2112 #define elf_backend_gc_mark_hook elf32_sparc_gc_mark_hook
2113 #define elf_backend_gc_sweep_hook elf32_sparc_gc_sweep_hook
2115 #define elf_backend_can_gc_sections 1
2116 #define elf_backend_want_got_plt 0
2117 #define elf_backend_plt_readonly 0
2118 #define elf_backend_want_plt_sym 1
2119 #define elf_backend_got_header_size 4
2120 #define elf_backend_plt_header_size (4*PLT_ENTRY_SIZE)
2122 #include "elf32-target.h"