Update address for bug reports.
[binutils.git] / bfd / elf32-sparc.c
blob4e9c6c2823354ddcc2d4b883ee632ce67b04aa14
1 /* SPARC-specific support for 32-bit ELF
2 Copyright (C) 1993, 94, 95, 96, 97, 98, 99, 2000
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,0,00,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_UA32", false,0,0x00000000,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_NONE, 0,0, 0,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_NONE", false,0,0x00000000,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_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 /* ??? Doesn't dwarf use this? */
163 /*{ BFD_RELOC_SPARC_UA32, R_SPARC_UA32 }, not used?? */
164 {BFD_RELOC_SPARC_10, R_SPARC_10},
165 {BFD_RELOC_SPARC_11, R_SPARC_11},
166 {BFD_RELOC_SPARC_64, R_SPARC_64},
167 {BFD_RELOC_SPARC_OLO10, R_SPARC_OLO10},
168 {BFD_RELOC_SPARC_HH22, R_SPARC_HH22},
169 {BFD_RELOC_SPARC_HM10, R_SPARC_HM10},
170 {BFD_RELOC_SPARC_LM22, R_SPARC_LM22},
171 {BFD_RELOC_SPARC_PC_HH22, R_SPARC_PC_HH22},
172 {BFD_RELOC_SPARC_PC_HM10, R_SPARC_PC_HM10},
173 {BFD_RELOC_SPARC_PC_LM22, R_SPARC_PC_LM22},
174 {BFD_RELOC_SPARC_WDISP16, R_SPARC_WDISP16},
175 {BFD_RELOC_SPARC_WDISP19, R_SPARC_WDISP19},
176 {BFD_RELOC_SPARC_7, R_SPARC_7},
177 {BFD_RELOC_SPARC_5, R_SPARC_5},
178 {BFD_RELOC_SPARC_6, R_SPARC_6},
179 {BFD_RELOC_SPARC_REV32, R_SPARC_REV32 },
180 {BFD_RELOC_VTABLE_INHERIT, R_SPARC_GNU_VTINHERIT},
181 {BFD_RELOC_VTABLE_ENTRY, R_SPARC_GNU_VTENTRY},
184 static reloc_howto_type *
185 elf32_sparc_reloc_type_lookup (abfd, code)
186 bfd *abfd ATTRIBUTE_UNUSED;
187 bfd_reloc_code_real_type code;
189 unsigned int i;
191 switch (code)
193 case BFD_RELOC_VTABLE_INHERIT:
194 return &elf32_sparc_vtinherit_howto;
196 case BFD_RELOC_VTABLE_ENTRY:
197 return &elf32_sparc_vtentry_howto;
199 default:
200 for (i = 0; i < sizeof (sparc_reloc_map) / sizeof (struct elf_reloc_map); i++)
202 if (sparc_reloc_map[i].bfd_reloc_val == code)
203 return &_bfd_sparc_elf_howto_table[(int) sparc_reloc_map[i].elf_reloc_val];
206 bfd_set_error (bfd_error_bad_value);
207 return NULL;
210 /* We need to use ELF32_R_TYPE so we have our own copy of this function,
211 and elf64-sparc.c has its own copy. */
213 static void
214 elf32_sparc_info_to_howto (abfd, cache_ptr, dst)
215 bfd *abfd ATTRIBUTE_UNUSED;
216 arelent *cache_ptr;
217 Elf_Internal_Rela *dst;
219 switch (ELF32_R_TYPE(dst->r_info))
221 case R_SPARC_GNU_VTINHERIT:
222 cache_ptr->howto = &elf32_sparc_vtinherit_howto;
223 break;
225 case R_SPARC_GNU_VTENTRY:
226 cache_ptr->howto = &elf32_sparc_vtentry_howto;
227 break;
229 default:
230 BFD_ASSERT (ELF32_R_TYPE(dst->r_info) < (unsigned int) R_SPARC_max_std);
231 cache_ptr->howto = &_bfd_sparc_elf_howto_table[ELF32_R_TYPE(dst->r_info)];
235 /* For unsupported relocs. */
237 static bfd_reloc_status_type
238 sparc_elf_notsupported_reloc (abfd,
239 reloc_entry,
240 symbol,
241 data,
242 input_section,
243 output_bfd,
244 error_message)
245 bfd *abfd ATTRIBUTE_UNUSED;
246 arelent *reloc_entry ATTRIBUTE_UNUSED;
247 asymbol *symbol ATTRIBUTE_UNUSED;
248 PTR data ATTRIBUTE_UNUSED;
249 asection *input_section ATTRIBUTE_UNUSED;
250 bfd *output_bfd ATTRIBUTE_UNUSED;
251 char **error_message ATTRIBUTE_UNUSED;
253 return bfd_reloc_notsupported;
256 /* Handle the WDISP16 reloc. */
258 static bfd_reloc_status_type
259 sparc_elf_wdisp16_reloc (abfd,
260 reloc_entry,
261 symbol,
262 data,
263 input_section,
264 output_bfd,
265 error_message)
266 bfd *abfd;
267 arelent *reloc_entry;
268 asymbol *symbol;
269 PTR data;
270 asection *input_section;
271 bfd *output_bfd;
272 char **error_message ATTRIBUTE_UNUSED;
274 bfd_vma relocation;
275 bfd_vma x;
277 if (output_bfd != (bfd *) NULL
278 && (symbol->flags & BSF_SECTION_SYM) == 0
279 && (! reloc_entry->howto->partial_inplace
280 || reloc_entry->addend == 0))
282 reloc_entry->address += input_section->output_offset;
283 return bfd_reloc_ok;
286 if (output_bfd != NULL)
287 return bfd_reloc_continue;
289 if (reloc_entry->address > input_section->_cooked_size)
290 return bfd_reloc_outofrange;
292 relocation = (symbol->value
293 + symbol->section->output_section->vma
294 + symbol->section->output_offset);
295 relocation += reloc_entry->addend;
296 relocation -= (input_section->output_section->vma
297 + input_section->output_offset);
298 relocation -= reloc_entry->address;
300 x = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
301 x |= ((((relocation >> 2) & 0xc000) << 6)
302 | ((relocation >> 2) & 0x3fff));
303 bfd_put_32 (abfd, x, (bfd_byte *) data + reloc_entry->address);
305 if ((bfd_signed_vma) relocation < - 0x40000
306 || (bfd_signed_vma) relocation > 0x3ffff)
307 return bfd_reloc_overflow;
308 else
309 return bfd_reloc_ok;
312 /* Functions for the SPARC ELF linker. */
314 /* The name of the dynamic interpreter. This is put in the .interp
315 section. */
317 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
319 /* The nop opcode we use. */
321 #define SPARC_NOP 0x01000000
323 /* The size in bytes of an entry in the procedure linkage table. */
325 #define PLT_ENTRY_SIZE 12
327 /* The first four entries in a procedure linkage table are reserved,
328 and the initial contents are unimportant (we zero them out).
329 Subsequent entries look like this. See the SVR4 ABI SPARC
330 supplement to see how this works. */
332 /* sethi %hi(.-.plt0),%g1. We fill in the address later. */
333 #define PLT_ENTRY_WORD0 0x03000000
334 /* b,a .plt0. We fill in the offset later. */
335 #define PLT_ENTRY_WORD1 0x30800000
336 /* nop. */
337 #define PLT_ENTRY_WORD2 SPARC_NOP
339 /* Look through the relocs for a section during the first phase, and
340 allocate space in the global offset table or procedure linkage
341 table. */
343 static boolean
344 elf32_sparc_check_relocs (abfd, info, sec, relocs)
345 bfd *abfd;
346 struct bfd_link_info *info;
347 asection *sec;
348 const Elf_Internal_Rela *relocs;
350 bfd *dynobj;
351 Elf_Internal_Shdr *symtab_hdr;
352 struct elf_link_hash_entry **sym_hashes;
353 bfd_vma *local_got_offsets;
354 const Elf_Internal_Rela *rel;
355 const Elf_Internal_Rela *rel_end;
356 asection *sgot;
357 asection *srelgot;
358 asection *sreloc;
360 if (info->relocateable)
361 return true;
363 dynobj = elf_hash_table (info)->dynobj;
364 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
365 sym_hashes = elf_sym_hashes (abfd);
366 local_got_offsets = elf_local_got_offsets (abfd);
368 sgot = NULL;
369 srelgot = NULL;
370 sreloc = NULL;
372 rel_end = relocs + sec->reloc_count;
373 for (rel = relocs; rel < rel_end; rel++)
375 unsigned long r_symndx;
376 struct elf_link_hash_entry *h;
378 r_symndx = ELF32_R_SYM (rel->r_info);
379 if (r_symndx < symtab_hdr->sh_info)
380 h = NULL;
381 else
382 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
384 switch (ELF32_R_TYPE (rel->r_info))
386 case R_SPARC_GOT10:
387 case R_SPARC_GOT13:
388 case R_SPARC_GOT22:
389 /* This symbol requires a global offset table entry. */
391 if (dynobj == NULL)
393 /* Create the .got section. */
394 elf_hash_table (info)->dynobj = dynobj = abfd;
395 if (! _bfd_elf_create_got_section (dynobj, info))
396 return false;
399 if (sgot == NULL)
401 sgot = bfd_get_section_by_name (dynobj, ".got");
402 BFD_ASSERT (sgot != NULL);
405 if (srelgot == NULL
406 && (h != NULL || info->shared))
408 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
409 if (srelgot == NULL)
411 srelgot = bfd_make_section (dynobj, ".rela.got");
412 if (srelgot == NULL
413 || ! bfd_set_section_flags (dynobj, srelgot,
414 (SEC_ALLOC
415 | SEC_LOAD
416 | SEC_HAS_CONTENTS
417 | SEC_IN_MEMORY
418 | SEC_LINKER_CREATED
419 | SEC_READONLY))
420 || ! bfd_set_section_alignment (dynobj, srelgot, 2))
421 return false;
425 if (h != NULL)
427 if (h->got.offset != (bfd_vma) -1)
429 /* We have already allocated space in the .got. */
430 break;
432 h->got.offset = sgot->_raw_size;
434 /* Make sure this symbol is output as a dynamic symbol. */
435 if (h->dynindx == -1)
437 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
438 return false;
441 srelgot->_raw_size += sizeof (Elf32_External_Rela);
443 else
445 /* This is a global offset table entry for a local
446 symbol. */
447 if (local_got_offsets == NULL)
449 size_t size;
450 register unsigned int i;
452 size = symtab_hdr->sh_info * sizeof (bfd_vma);
453 local_got_offsets = (bfd_vma *) bfd_alloc (abfd, size);
454 if (local_got_offsets == NULL)
455 return false;
456 elf_local_got_offsets (abfd) = local_got_offsets;
457 for (i = 0; i < symtab_hdr->sh_info; i++)
458 local_got_offsets[i] = (bfd_vma) -1;
460 if (local_got_offsets[r_symndx] != (bfd_vma) -1)
462 /* We have already allocated space in the .got. */
463 break;
465 local_got_offsets[r_symndx] = sgot->_raw_size;
467 if (info->shared)
469 /* If we are generating a shared object, we need to
470 output a R_SPARC_RELATIVE reloc so that the
471 dynamic linker can adjust this GOT entry. */
472 srelgot->_raw_size += sizeof (Elf32_External_Rela);
476 sgot->_raw_size += 4;
478 /* If the .got section is more than 0x1000 bytes, we add
479 0x1000 to the value of _GLOBAL_OFFSET_TABLE_, so that 13
480 bit relocations have a greater chance of working. */
481 if (sgot->_raw_size >= 0x1000
482 && elf_hash_table (info)->hgot->root.u.def.value == 0)
483 elf_hash_table (info)->hgot->root.u.def.value = 0x1000;
485 break;
487 case R_SPARC_WPLT30:
488 /* This symbol requires a procedure linkage table entry. We
489 actually build the entry in adjust_dynamic_symbol,
490 because this might be a case of linking PIC code without
491 linking in any dynamic objects, in which case we don't
492 need to generate a procedure linkage table after all. */
494 if (h == NULL)
496 /* The Solaris native assembler will generate a WPLT30
497 reloc for a local symbol if you assemble a call from
498 one section to another when using -K pic. We treat
499 it as WDISP30. */
500 break;
503 /* Make sure this symbol is output as a dynamic symbol. */
504 if (h->dynindx == -1)
506 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
507 return false;
510 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
512 break;
514 case R_SPARC_PC10:
515 case R_SPARC_PC22:
516 if (h != NULL)
517 h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
519 if (h != NULL
520 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
521 break;
522 /* Fall through. */
523 case R_SPARC_DISP8:
524 case R_SPARC_DISP16:
525 case R_SPARC_DISP32:
526 case R_SPARC_WDISP30:
527 case R_SPARC_WDISP22:
528 case R_SPARC_WDISP19:
529 case R_SPARC_WDISP16:
530 if (h != NULL)
531 h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
533 /* If we are linking with -Bsymbolic, we do not need to copy
534 a PC relative reloc against a global symbol which is
535 defined in an object we are including in the link (i.e.,
536 DEF_REGULAR is set). FIXME: At this point we have not
537 seen all the input files, so it is possible that
538 DEF_REGULAR is not set now but will be set later (it is
539 never cleared). This needs to be handled as in
540 elf32-i386.c. */
541 if (h == NULL
542 || (info->symbolic
543 && (h->elf_link_hash_flags
544 & ELF_LINK_HASH_DEF_REGULAR) != 0))
545 break;
546 /* Fall through. */
547 case R_SPARC_8:
548 case R_SPARC_16:
549 case R_SPARC_32:
550 case R_SPARC_HI22:
551 case R_SPARC_22:
552 case R_SPARC_13:
553 case R_SPARC_LO10:
554 case R_SPARC_UA32:
555 if (h != NULL)
556 h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
558 if (info->shared)
560 /* When creating a shared object, we must copy these
561 relocs into the output file. We create a reloc
562 section in dynobj and make room for the reloc. */
563 if (sreloc == NULL)
565 const char *name;
567 name = (bfd_elf_string_from_elf_section
568 (abfd,
569 elf_elfheader (abfd)->e_shstrndx,
570 elf_section_data (sec)->rel_hdr.sh_name));
571 if (name == NULL)
572 return false;
574 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
575 && strcmp (bfd_get_section_name (abfd, sec),
576 name + 5) == 0);
578 sreloc = bfd_get_section_by_name (dynobj, name);
579 if (sreloc == NULL)
581 flagword flags;
583 sreloc = bfd_make_section (dynobj, name);
584 flags = (SEC_HAS_CONTENTS | SEC_READONLY
585 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
586 if ((sec->flags & SEC_ALLOC) != 0)
587 flags |= SEC_ALLOC | SEC_LOAD;
588 if (sreloc == NULL
589 || ! bfd_set_section_flags (dynobj, sreloc, flags)
590 || ! bfd_set_section_alignment (dynobj, sreloc, 2))
591 return false;
595 sreloc->_raw_size += sizeof (Elf32_External_Rela);
598 break;
600 case R_SPARC_GNU_VTINHERIT:
601 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
602 return false;
603 break;
605 case R_SPARC_GNU_VTENTRY:
606 if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend))
607 return false;
608 break;
610 default:
611 break;
615 return true;
618 static asection *
619 elf32_sparc_gc_mark_hook (abfd, info, rel, h, sym)
620 bfd *abfd;
621 struct bfd_link_info *info ATTRIBUTE_UNUSED;
622 Elf_Internal_Rela *rel;
623 struct elf_link_hash_entry *h;
624 Elf_Internal_Sym *sym;
627 if (h != NULL)
629 switch (ELF32_R_TYPE (rel->r_info))
631 case R_SPARC_GNU_VTINHERIT:
632 case R_SPARC_GNU_VTENTRY:
633 break;
635 default:
636 switch (h->root.type)
638 case bfd_link_hash_defined:
639 case bfd_link_hash_defweak:
640 return h->root.u.def.section;
642 case bfd_link_hash_common:
643 return h->root.u.c.p->section;
645 default:
646 break;
650 else
652 if (!(elf_bad_symtab (abfd)
653 && ELF_ST_BIND (sym->st_info) != STB_LOCAL)
654 && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE)
655 && sym->st_shndx != SHN_COMMON))
657 return bfd_section_from_elf_index (abfd, sym->st_shndx);
661 return NULL;
664 /* Update the got entry reference counts for the section being removed. */
665 static boolean
666 elf32_sparc_gc_sweep_hook (abfd, info, sec, relocs)
667 bfd *abfd;
668 struct bfd_link_info *info ATTRIBUTE_UNUSED;
669 asection *sec;
670 const Elf_Internal_Rela *relocs;
673 Elf_Internal_Shdr *symtab_hdr;
674 struct elf_link_hash_entry **sym_hashes;
675 bfd_signed_vma *local_got_refcounts;
676 const Elf_Internal_Rela *rel, *relend;
677 unsigned long r_symndx;
678 struct elf_link_hash_entry *h;
680 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
681 sym_hashes = elf_sym_hashes (abfd);
682 local_got_refcounts = elf_local_got_refcounts (abfd);
684 relend = relocs + sec->reloc_count;
685 for (rel = relocs; rel < relend; rel++)
686 switch (ELF32_R_TYPE (rel->r_info))
688 case R_SPARC_GOT10:
689 case R_SPARC_GOT13:
690 case R_SPARC_GOT22:
691 r_symndx = ELF32_R_SYM (rel->r_info);
692 if (r_symndx >= symtab_hdr->sh_info)
694 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
695 if (h->got.refcount > 0)
696 h->got.refcount--;
698 else
700 if (local_got_refcounts[r_symndx] > 0)
701 local_got_refcounts[r_symndx]--;
703 break;
705 case R_SPARC_PLT32:
706 case R_SPARC_HIPLT22:
707 case R_SPARC_LOPLT10:
708 case R_SPARC_PCPLT32:
709 case R_SPARC_PCPLT10:
710 r_symndx = ELF32_R_SYM (rel->r_info);
711 if (r_symndx >= symtab_hdr->sh_info)
713 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
714 if (h->plt.refcount > 0)
715 h->plt.refcount--;
717 break;
719 default:
720 break;
723 return true;
726 /* Adjust a symbol defined by a dynamic object and referenced by a
727 regular object. The current definition is in some section of the
728 dynamic object, but we're not including those sections. We have to
729 change the definition to something the rest of the link can
730 understand. */
732 static boolean
733 elf32_sparc_adjust_dynamic_symbol (info, h)
734 struct bfd_link_info *info;
735 struct elf_link_hash_entry *h;
737 bfd *dynobj;
738 asection *s;
739 unsigned int power_of_two;
741 dynobj = elf_hash_table (info)->dynobj;
743 /* Make sure we know what is going on here. */
744 BFD_ASSERT (dynobj != NULL
745 && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
746 || h->weakdef != NULL
747 || ((h->elf_link_hash_flags
748 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
749 && (h->elf_link_hash_flags
750 & ELF_LINK_HASH_REF_REGULAR) != 0
751 && (h->elf_link_hash_flags
752 & ELF_LINK_HASH_DEF_REGULAR) == 0)));
754 /* If this is a function, put it in the procedure linkage table. We
755 will fill in the contents of the procedure linkage table later
756 (although we could actually do it here). The STT_NOTYPE
757 condition is a hack specifically for the Oracle libraries
758 delivered for Solaris; for some inexplicable reason, they define
759 some of their functions as STT_NOTYPE when they really should be
760 STT_FUNC. */
761 if (h->type == STT_FUNC
762 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0
763 || (h->type == STT_NOTYPE
764 && (h->root.type == bfd_link_hash_defined
765 || h->root.type == bfd_link_hash_defweak)
766 && (h->root.u.def.section->flags & SEC_CODE) != 0))
768 if (! elf_hash_table (info)->dynamic_sections_created
769 || ((!info->shared || info->symbolic || h->dynindx == -1)
770 && (h->elf_link_hash_flags
771 & ELF_LINK_HASH_DEF_REGULAR) != 0))
773 /* This case can occur if we saw a WPLT30 reloc in an input
774 file, but none of the input files were dynamic objects.
775 Or, when linking the main application or a -Bsymbolic
776 shared library against PIC code. Or when a global symbol
777 has been made private, e.g. via versioning.
779 In these cases we know what value the symbol will resolve
780 to, so we don't actually need to build a procedure linkage
781 table, and we can just do a WDISP30 reloc instead. */
783 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
784 return true;
787 s = bfd_get_section_by_name (dynobj, ".plt");
788 BFD_ASSERT (s != NULL);
790 /* The first four entries in .plt are reserved. */
791 if (s->_raw_size == 0)
792 s->_raw_size = 4 * PLT_ENTRY_SIZE;
794 /* The procedure linkage table has a maximum size. */
795 if (s->_raw_size >= 0x400000)
797 bfd_set_error (bfd_error_bad_value);
798 return false;
801 /* If this symbol is not defined in a regular file, and we are
802 not generating a shared library, then set the symbol to this
803 location in the .plt. This is required to make function
804 pointers compare as equal between the normal executable and
805 the shared library. */
806 if (! info->shared
807 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
809 h->root.u.def.section = s;
810 h->root.u.def.value = s->_raw_size;
813 h->plt.offset = s->_raw_size;
815 /* Make room for this entry. */
816 s->_raw_size += PLT_ENTRY_SIZE;
818 /* We also need to make an entry in the .rela.plt section. */
820 s = bfd_get_section_by_name (dynobj, ".rela.plt");
821 BFD_ASSERT (s != NULL);
822 s->_raw_size += sizeof (Elf32_External_Rela);
824 return true;
827 /* If this is a weak symbol, and there is a real definition, the
828 processor independent code will have arranged for us to see the
829 real definition first, and we can just use the same value. */
830 if (h->weakdef != NULL)
832 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
833 || h->weakdef->root.type == bfd_link_hash_defweak);
834 h->root.u.def.section = h->weakdef->root.u.def.section;
835 h->root.u.def.value = h->weakdef->root.u.def.value;
836 return true;
839 /* This is a reference to a symbol defined by a dynamic object which
840 is not a function. */
842 /* If we are creating a shared library, we must presume that the
843 only references to the symbol are via the global offset table.
844 For such cases we need not do anything here; the relocations will
845 be handled correctly by relocate_section. */
846 if (info->shared)
847 return true;
849 /* If there are no references to this symbol that do not use the
850 GOT, we don't need to generate a copy reloc. */
851 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
852 return true;
854 /* We must allocate the symbol in our .dynbss section, which will
855 become part of the .bss section of the executable. There will be
856 an entry for this symbol in the .dynsym section. The dynamic
857 object will contain position independent code, so all references
858 from the dynamic object to this symbol will go through the global
859 offset table. The dynamic linker will use the .dynsym entry to
860 determine the address it must put in the global offset table, so
861 both the dynamic object and the regular object will refer to the
862 same memory location for the variable. */
864 s = bfd_get_section_by_name (dynobj, ".dynbss");
865 BFD_ASSERT (s != NULL);
867 /* We must generate a R_SPARC_COPY reloc to tell the dynamic linker
868 to copy the initial value out of the dynamic object and into the
869 runtime process image. We need to remember the offset into the
870 .rel.bss section we are going to use. */
871 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
873 asection *srel;
875 srel = bfd_get_section_by_name (dynobj, ".rela.bss");
876 BFD_ASSERT (srel != NULL);
877 srel->_raw_size += sizeof (Elf32_External_Rela);
878 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
881 /* We need to figure out the alignment required for this symbol. I
882 have no idea how ELF linkers handle this. */
883 power_of_two = bfd_log2 (h->size);
884 if (power_of_two > 3)
885 power_of_two = 3;
887 /* Apply the required alignment. */
888 s->_raw_size = BFD_ALIGN (s->_raw_size,
889 (bfd_size_type) (1 << power_of_two));
890 if (power_of_two > bfd_get_section_alignment (dynobj, s))
892 if (! bfd_set_section_alignment (dynobj, s, power_of_two))
893 return false;
896 /* Define the symbol as being at this point in the section. */
897 h->root.u.def.section = s;
898 h->root.u.def.value = s->_raw_size;
900 /* Increment the section size to make room for the symbol. */
901 s->_raw_size += h->size;
903 return true;
906 /* Set the sizes of the dynamic sections. */
908 static boolean
909 elf32_sparc_size_dynamic_sections (output_bfd, info)
910 bfd *output_bfd;
911 struct bfd_link_info *info;
913 bfd *dynobj;
914 asection *s;
915 boolean reltext;
916 boolean relplt;
918 dynobj = elf_hash_table (info)->dynobj;
919 BFD_ASSERT (dynobj != NULL);
921 if (elf_hash_table (info)->dynamic_sections_created)
923 /* Set the contents of the .interp section to the interpreter. */
924 if (! info->shared)
926 s = bfd_get_section_by_name (dynobj, ".interp");
927 BFD_ASSERT (s != NULL);
928 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
929 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
932 /* Make space for the trailing nop in .plt. */
933 s = bfd_get_section_by_name (dynobj, ".plt");
934 BFD_ASSERT (s != NULL);
935 if (s->_raw_size > 0)
936 s->_raw_size += 4;
938 else
940 /* We may have created entries in the .rela.got section.
941 However, if we are not creating the dynamic sections, we will
942 not actually use these entries. Reset the size of .rela.got,
943 which will cause it to get stripped from the output file
944 below. */
945 s = bfd_get_section_by_name (dynobj, ".rela.got");
946 if (s != NULL)
947 s->_raw_size = 0;
950 /* The check_relocs and adjust_dynamic_symbol entry points have
951 determined the sizes of the various dynamic sections. Allocate
952 memory for them. */
953 reltext = false;
954 relplt = false;
955 for (s = dynobj->sections; s != NULL; s = s->next)
957 const char *name;
958 boolean strip;
960 if ((s->flags & SEC_LINKER_CREATED) == 0)
961 continue;
963 /* It's OK to base decisions on the section name, because none
964 of the dynobj section names depend upon the input files. */
965 name = bfd_get_section_name (dynobj, s);
967 strip = false;
969 if (strncmp (name, ".rela", 5) == 0)
971 if (s->_raw_size == 0)
973 /* If we don't need this section, strip it from the
974 output file. This is to handle .rela.bss and
975 .rel.plt. We must create it in
976 create_dynamic_sections, because it must be created
977 before the linker maps input sections to output
978 sections. The linker does that before
979 adjust_dynamic_symbol is called, and it is that
980 function which decides whether anything needs to go
981 into these sections. */
982 strip = true;
984 else
986 const char *outname;
987 asection *target;
989 /* If this relocation section applies to a read only
990 section, then we probably need a DT_TEXTREL entry. */
991 outname = bfd_get_section_name (output_bfd,
992 s->output_section);
993 target = bfd_get_section_by_name (output_bfd, outname + 5);
994 if (target != NULL
995 && (target->flags & SEC_READONLY) != 0
996 && (target->flags & SEC_ALLOC) != 0)
997 reltext = true;
999 if (strcmp (name, ".rela.plt") == 0)
1000 relplt = true;
1002 /* We use the reloc_count field as a counter if we need
1003 to copy relocs into the output file. */
1004 s->reloc_count = 0;
1007 else if (strcmp (name, ".plt") != 0
1008 && strcmp (name, ".got") != 0)
1010 /* It's not one of our sections, so don't allocate space. */
1011 continue;
1014 if (strip)
1016 _bfd_strip_section_from_output (info, s);
1017 continue;
1020 /* Allocate memory for the section contents. */
1021 /* FIXME: This should be a call to bfd_alloc not bfd_zalloc.
1022 Unused entries should be reclaimed before the section's contents
1023 are written out, but at the moment this does not happen. Thus in
1024 order to prevent writing out garbage, we initialise the section's
1025 contents to zero. */
1026 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
1027 if (s->contents == NULL && s->_raw_size != 0)
1028 return false;
1031 if (elf_hash_table (info)->dynamic_sections_created)
1033 /* Add some entries to the .dynamic section. We fill in the
1034 values later, in elf32_sparc_finish_dynamic_sections, but we
1035 must add the entries now so that we get the correct size for
1036 the .dynamic section. The DT_DEBUG entry is filled in by the
1037 dynamic linker and used by the debugger. */
1038 if (! info->shared)
1040 if (! bfd_elf32_add_dynamic_entry (info, DT_DEBUG, 0))
1041 return false;
1044 if (relplt)
1046 if (! bfd_elf32_add_dynamic_entry (info, DT_PLTGOT, 0)
1047 || ! bfd_elf32_add_dynamic_entry (info, DT_PLTRELSZ, 0)
1048 || ! bfd_elf32_add_dynamic_entry (info, DT_PLTREL, DT_RELA)
1049 || ! bfd_elf32_add_dynamic_entry (info, DT_JMPREL, 0))
1050 return false;
1053 if (! bfd_elf32_add_dynamic_entry (info, DT_RELA, 0)
1054 || ! bfd_elf32_add_dynamic_entry (info, DT_RELASZ, 0)
1055 || ! bfd_elf32_add_dynamic_entry (info, DT_RELAENT,
1056 sizeof (Elf32_External_Rela)))
1057 return false;
1059 if (reltext)
1061 if (! bfd_elf32_add_dynamic_entry (info, DT_TEXTREL, 0))
1062 return false;
1063 info->flags |= DF_TEXTREL;
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 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 boolean *again;
1080 *again = false;
1081 SET_SEC_DO_RELAX (section);
1082 return true;
1085 /* Relocate a SPARC ELF section. */
1087 static boolean
1088 elf32_sparc_relocate_section (output_bfd, info, input_bfd, input_section,
1089 contents, relocs, local_syms, local_sections)
1090 bfd *output_bfd;
1091 struct bfd_link_info *info;
1092 bfd *input_bfd;
1093 asection *input_section;
1094 bfd_byte *contents;
1095 Elf_Internal_Rela *relocs;
1096 Elf_Internal_Sym *local_syms;
1097 asection **local_sections;
1099 bfd *dynobj;
1100 Elf_Internal_Shdr *symtab_hdr;
1101 struct elf_link_hash_entry **sym_hashes;
1102 bfd_vma *local_got_offsets;
1103 bfd_vma got_base;
1104 asection *sgot;
1105 asection *splt;
1106 asection *sreloc;
1107 Elf_Internal_Rela *rel;
1108 Elf_Internal_Rela *relend;
1110 dynobj = elf_hash_table (info)->dynobj;
1111 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1112 sym_hashes = elf_sym_hashes (input_bfd);
1113 local_got_offsets = elf_local_got_offsets (input_bfd);
1115 if (elf_hash_table (info)->hgot == NULL)
1116 got_base = 0;
1117 else
1118 got_base = elf_hash_table (info)->hgot->root.u.def.value;
1120 sgot = NULL;
1121 splt = NULL;
1122 sreloc = NULL;
1124 rel = relocs;
1125 relend = relocs + input_section->reloc_count;
1126 for (; rel < relend; rel++)
1128 int r_type;
1129 reloc_howto_type *howto;
1130 unsigned long r_symndx;
1131 struct elf_link_hash_entry *h;
1132 Elf_Internal_Sym *sym;
1133 asection *sec;
1134 bfd_vma relocation;
1135 bfd_reloc_status_type r;
1137 r_type = ELF32_R_TYPE (rel->r_info);
1139 if (r_type == R_SPARC_GNU_VTINHERIT
1140 || r_type == R_SPARC_GNU_VTENTRY)
1141 continue;
1143 if (r_type < 0 || r_type >= (int) R_SPARC_max_std)
1145 bfd_set_error (bfd_error_bad_value);
1146 return false;
1148 howto = _bfd_sparc_elf_howto_table + r_type;
1150 r_symndx = ELF32_R_SYM (rel->r_info);
1152 if (info->relocateable)
1154 /* This is a relocateable link. We don't have to change
1155 anything, unless the reloc is against a section symbol,
1156 in which case we have to adjust according to where the
1157 section symbol winds up in the output section. */
1158 if (r_symndx < symtab_hdr->sh_info)
1160 sym = local_syms + r_symndx;
1161 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
1163 sec = local_sections[r_symndx];
1164 rel->r_addend += sec->output_offset + sym->st_value;
1168 continue;
1171 /* This is a final link. */
1172 h = NULL;
1173 sym = NULL;
1174 sec = NULL;
1175 if (r_symndx < symtab_hdr->sh_info)
1177 sym = local_syms + r_symndx;
1178 sec = local_sections[r_symndx];
1179 relocation = (sec->output_section->vma
1180 + sec->output_offset
1181 + sym->st_value);
1183 else
1185 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1186 while (h->root.type == bfd_link_hash_indirect
1187 || h->root.type == bfd_link_hash_warning)
1188 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1189 if (h->root.type == bfd_link_hash_defined
1190 || h->root.type == bfd_link_hash_defweak)
1192 sec = h->root.u.def.section;
1193 if ((r_type == R_SPARC_WPLT30
1194 && h->plt.offset != (bfd_vma) -1)
1195 || ((r_type == R_SPARC_GOT10
1196 || r_type == R_SPARC_GOT13
1197 || r_type == R_SPARC_GOT22)
1198 && elf_hash_table (info)->dynamic_sections_created
1199 && (! info->shared
1200 || (! info->symbolic && h->dynindx != -1)
1201 || (h->elf_link_hash_flags
1202 & ELF_LINK_HASH_DEF_REGULAR) == 0))
1203 || (info->shared
1204 && ((! info->symbolic && h->dynindx != -1)
1205 || (h->elf_link_hash_flags
1206 & ELF_LINK_HASH_DEF_REGULAR) == 0)
1207 && (r_type == R_SPARC_8
1208 || r_type == R_SPARC_16
1209 || r_type == R_SPARC_32
1210 || r_type == R_SPARC_DISP8
1211 || r_type == R_SPARC_DISP16
1212 || r_type == R_SPARC_DISP32
1213 || r_type == R_SPARC_WDISP30
1214 || r_type == R_SPARC_WDISP22
1215 || r_type == R_SPARC_WDISP19
1216 || r_type == R_SPARC_WDISP16
1217 || r_type == R_SPARC_HI22
1218 || r_type == R_SPARC_22
1219 || r_type == R_SPARC_13
1220 || r_type == R_SPARC_LO10
1221 || r_type == R_SPARC_UA32
1222 || ((r_type == R_SPARC_PC10
1223 || r_type == R_SPARC_PC22)
1224 && strcmp (h->root.root.string,
1225 "_GLOBAL_OFFSET_TABLE_") != 0))))
1227 /* In these cases, we don't need the relocation
1228 value. We check specially because in some
1229 obscure cases sec->output_section will be NULL. */
1230 relocation = 0;
1232 else
1233 relocation = (h->root.u.def.value
1234 + sec->output_section->vma
1235 + sec->output_offset);
1237 else if (h->root.type == bfd_link_hash_undefweak)
1238 relocation = 0;
1239 else if (info->shared && !info->symbolic
1240 && !info->no_undefined
1241 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
1242 relocation = 0;
1243 else
1245 if (! ((*info->callbacks->undefined_symbol)
1246 (info, h->root.root.string, input_bfd,
1247 input_section, rel->r_offset,
1248 (!info->shared || info->no_undefined
1249 || ELF_ST_VISIBILITY (h->other)))))
1250 return false;
1251 relocation = 0;
1255 switch (r_type)
1257 case R_SPARC_GOT10:
1258 case R_SPARC_GOT13:
1259 case R_SPARC_GOT22:
1260 /* Relocation is to the entry for this symbol in the global
1261 offset table. */
1262 if (sgot == NULL)
1264 sgot = bfd_get_section_by_name (dynobj, ".got");
1265 BFD_ASSERT (sgot != NULL);
1268 if (h != NULL)
1270 bfd_vma off;
1272 off = h->got.offset;
1273 BFD_ASSERT (off != (bfd_vma) -1);
1275 if (! elf_hash_table (info)->dynamic_sections_created
1276 || (info->shared
1277 && (info->symbolic || h->dynindx == -1)
1278 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
1280 /* This is actually a static link, or it is a
1281 -Bsymbolic link and the symbol is defined
1282 locally, or the symbol was forced to be local
1283 because of a version file. We must initialize
1284 this entry in the global offset table. Since the
1285 offset must always be a multiple of 4, we use the
1286 least significant bit to record whether we have
1287 initialized it already.
1289 When doing a dynamic link, we create a .rela.got
1290 relocation entry to initialize the value. This
1291 is done in the finish_dynamic_symbol routine. */
1292 if ((off & 1) != 0)
1293 off &= ~1;
1294 else
1296 bfd_put_32 (output_bfd, relocation,
1297 sgot->contents + off);
1298 h->got.offset |= 1;
1302 relocation = sgot->output_offset + off - got_base;
1304 else
1306 bfd_vma off;
1308 BFD_ASSERT (local_got_offsets != NULL
1309 && local_got_offsets[r_symndx] != (bfd_vma) -1);
1311 off = local_got_offsets[r_symndx];
1313 /* The offset must always be a multiple of 4. We use
1314 the least significant bit to record whether we have
1315 already processed this entry. */
1316 if ((off & 1) != 0)
1317 off &= ~1;
1318 else
1320 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
1322 if (info->shared)
1324 asection *srelgot;
1325 Elf_Internal_Rela outrel;
1327 /* We need to generate a R_SPARC_RELATIVE reloc
1328 for the dynamic linker. */
1329 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
1330 BFD_ASSERT (srelgot != NULL);
1332 outrel.r_offset = (sgot->output_section->vma
1333 + sgot->output_offset
1334 + off);
1335 outrel.r_info = ELF32_R_INFO (0, R_SPARC_RELATIVE);
1336 outrel.r_addend = 0;
1337 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1338 (((Elf32_External_Rela *)
1339 srelgot->contents)
1340 + srelgot->reloc_count));
1341 ++srelgot->reloc_count;
1344 local_got_offsets[r_symndx] |= 1;
1347 relocation = sgot->output_offset + off - got_base;
1350 break;
1352 case R_SPARC_WPLT30:
1353 /* Relocation is to the entry for this symbol in the
1354 procedure linkage table. */
1356 /* The Solaris native assembler will generate a WPLT30 reloc
1357 for a local symbol if you assemble a call from one
1358 section to another when using -K pic. We treat it as
1359 WDISP30. */
1360 if (h == NULL)
1361 break;
1363 if (h->plt.offset == (bfd_vma) -1)
1365 /* We didn't make a PLT entry for this symbol. This
1366 happens when statically linking PIC code, or when
1367 using -Bsymbolic. */
1368 break;
1371 if (splt == NULL)
1373 splt = bfd_get_section_by_name (dynobj, ".plt");
1374 BFD_ASSERT (splt != NULL);
1377 relocation = (splt->output_section->vma
1378 + splt->output_offset
1379 + h->plt.offset);
1380 break;
1382 case R_SPARC_PC10:
1383 case R_SPARC_PC22:
1384 if (h != NULL
1385 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
1386 break;
1387 /* Fall through. */
1388 case R_SPARC_DISP8:
1389 case R_SPARC_DISP16:
1390 case R_SPARC_DISP32:
1391 case R_SPARC_WDISP30:
1392 case R_SPARC_WDISP22:
1393 case R_SPARC_WDISP19:
1394 case R_SPARC_WDISP16:
1395 if (h == NULL
1396 || (info->symbolic
1397 && (h->elf_link_hash_flags
1398 & ELF_LINK_HASH_DEF_REGULAR) != 0))
1399 break;
1400 /* Fall through. */
1401 case R_SPARC_8:
1402 case R_SPARC_16:
1403 case R_SPARC_32:
1404 case R_SPARC_HI22:
1405 case R_SPARC_22:
1406 case R_SPARC_13:
1407 case R_SPARC_LO10:
1408 case R_SPARC_UA32:
1409 if (info->shared)
1411 Elf_Internal_Rela outrel;
1412 boolean skip;
1414 /* When generating a shared object, these relocations
1415 are copied into the output file to be resolved at run
1416 time. */
1418 if (sreloc == NULL)
1420 const char *name;
1422 name = (bfd_elf_string_from_elf_section
1423 (input_bfd,
1424 elf_elfheader (input_bfd)->e_shstrndx,
1425 elf_section_data (input_section)->rel_hdr.sh_name));
1426 if (name == NULL)
1427 return false;
1429 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
1430 && strcmp (bfd_get_section_name (input_bfd,
1431 input_section),
1432 name + 5) == 0);
1434 sreloc = bfd_get_section_by_name (dynobj, name);
1435 BFD_ASSERT (sreloc != NULL);
1438 skip = false;
1440 if (elf_section_data (input_section)->stab_info == NULL)
1441 outrel.r_offset = rel->r_offset;
1442 else
1444 bfd_vma off;
1446 off = (_bfd_stab_section_offset
1447 (output_bfd, &elf_hash_table (info)->stab_info,
1448 input_section,
1449 &elf_section_data (input_section)->stab_info,
1450 rel->r_offset));
1451 if (off == (bfd_vma) -1)
1452 skip = true;
1453 outrel.r_offset = off;
1456 outrel.r_offset += (input_section->output_section->vma
1457 + input_section->output_offset);
1459 if (skip)
1460 memset (&outrel, 0, sizeof outrel);
1461 /* h->dynindx may be -1 if the symbol was marked to
1462 become local. */
1463 else if (h != NULL
1464 && ((! info->symbolic && h->dynindx != -1)
1465 || (h->elf_link_hash_flags
1466 & ELF_LINK_HASH_DEF_REGULAR) == 0))
1468 BFD_ASSERT (h->dynindx != -1);
1469 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
1470 outrel.r_addend = rel->r_addend;
1472 else
1474 if (r_type == R_SPARC_32)
1476 outrel.r_info = ELF32_R_INFO (0, R_SPARC_RELATIVE);
1477 outrel.r_addend = relocation + rel->r_addend;
1479 else
1481 long indx;
1483 if (h == NULL)
1484 sec = local_sections[r_symndx];
1485 else
1487 BFD_ASSERT (h->root.type == bfd_link_hash_defined
1488 || (h->root.type
1489 == bfd_link_hash_defweak));
1490 sec = h->root.u.def.section;
1492 if (sec != NULL && bfd_is_abs_section (sec))
1493 indx = 0;
1494 else if (sec == NULL || sec->owner == NULL)
1496 bfd_set_error (bfd_error_bad_value);
1497 return false;
1499 else
1501 asection *osec;
1503 osec = sec->output_section;
1504 indx = elf_section_data (osec)->dynindx;
1506 /* FIXME: we really should be able to link non-pic
1507 shared libraries. */
1508 if (indx == 0)
1510 BFD_FAIL ();
1511 (*_bfd_error_handler)
1512 (_("%s: probably compiled without -fPIC?"),
1513 bfd_get_filename (input_bfd));
1514 bfd_set_error (bfd_error_bad_value);
1515 return false;
1519 outrel.r_info = ELF32_R_INFO (indx, r_type);
1520 outrel.r_addend = relocation + rel->r_addend;
1524 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1525 (((Elf32_External_Rela *)
1526 sreloc->contents)
1527 + sreloc->reloc_count));
1528 ++sreloc->reloc_count;
1530 /* This reloc will be computed at runtime, so there's no
1531 need to do anything now, unless this is a RELATIVE
1532 reloc in an unallocated section. */
1533 if (skip
1534 || (input_section->flags & SEC_ALLOC) != 0
1535 || ELF32_R_TYPE (outrel.r_info) != R_SPARC_RELATIVE)
1536 continue;
1538 break;
1540 default:
1541 break;
1544 r = bfd_reloc_continue;
1545 if (r_type == R_SPARC_WDISP16)
1547 bfd_vma x;
1549 relocation += rel->r_addend;
1550 relocation -= (input_section->output_section->vma
1551 + input_section->output_offset);
1552 relocation -= rel->r_offset;
1554 x = bfd_get_32 (input_bfd, contents + rel->r_offset);
1555 x |= ((((relocation >> 2) & 0xc000) << 6)
1556 | ((relocation >> 2) & 0x3fff));
1557 bfd_put_32 (input_bfd, x, contents + rel->r_offset);
1559 if ((bfd_signed_vma) relocation < - 0x40000
1560 || (bfd_signed_vma) relocation > 0x3ffff)
1561 r = bfd_reloc_overflow;
1562 else
1563 r = bfd_reloc_ok;
1565 else if (r_type == R_SPARC_REV32)
1567 bfd_vma x;
1569 relocation = relocation + rel->r_addend;
1571 x = bfd_get_32 (input_bfd, contents + rel->r_offset);
1572 x = x + relocation;
1573 bfd_putl32 (/*input_bfd,*/ x, contents + rel->r_offset);
1574 r = bfd_reloc_ok;
1576 else if ((r_type == R_SPARC_WDISP30 || r_type == R_SPARC_WPLT30)
1577 && SEC_DO_RELAX (input_section)
1578 && rel->r_offset + 4 < input_section->_raw_size)
1580 #define G0 0
1581 #define O7 15
1582 #define XCC (2 << 20)
1583 #define COND(x) (((x)&0xf)<<25)
1584 #define CONDA COND(0x8)
1585 #define INSN_BPA (F2(0,1) | CONDA | BPRED | XCC)
1586 #define INSN_BA (F2(0,2) | CONDA)
1587 #define INSN_OR F3(2, 0x2, 0)
1588 #define INSN_NOP F2(0,4)
1590 bfd_vma x, y;
1592 /* If the instruction is a call with either:
1593 restore
1594 arithmetic instruction with rd == %o7
1595 where rs1 != %o7 and rs2 if it is register != %o7
1596 then we can optimize if the call destination is near
1597 by changing the call into a branch always. */
1598 x = bfd_get_32 (input_bfd, contents + rel->r_offset);
1599 y = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
1600 if ((x & OP(~0)) == OP(1) && (y & OP(~0)) == OP(2))
1602 if (((y & OP3(~0)) == OP3(0x3d) /* restore */
1603 || ((y & OP3(0x28)) == 0 /* arithmetic */
1604 && (y & RD(~0)) == RD(O7)))
1605 && (y & RS1(~0)) != RS1(O7)
1606 && ((y & F3I(~0))
1607 || (y & RS2(~0)) != RS2(O7)))
1609 bfd_vma reloc;
1611 reloc = relocation + rel->r_addend - rel->r_offset;
1612 reloc -= (input_section->output_section->vma
1613 + input_section->output_offset);
1615 /* Ensure the reloc fits into simm22. */
1616 if ((reloc & 3) == 0
1617 && ((reloc & ~(bfd_vma)0x7fffff) == 0
1618 || ((reloc | 0x7fffff) == ~(bfd_vma)0)))
1620 reloc >>= 2;
1622 /* Check whether it fits into simm19 on v9. */
1623 if (((reloc & 0x3c0000) == 0
1624 || (reloc & 0x3c0000) == 0x3c0000)
1625 && (elf_elfheader (output_bfd)->e_flags & EF_SPARC_32PLUS))
1626 x = INSN_BPA | (reloc & 0x7ffff); /* ba,pt %xcc */
1627 else
1628 x = INSN_BA | (reloc & 0x3fffff); /* ba */
1629 bfd_put_32 (input_bfd, x, contents + rel->r_offset);
1630 r = bfd_reloc_ok;
1631 if (rel->r_offset >= 4
1632 && (y & (0xffffffff ^ RS1(~0)))
1633 == (INSN_OR | RD(O7) | RS2(G0)))
1635 bfd_vma z;
1636 unsigned int reg;
1638 z = bfd_get_32 (input_bfd,
1639 contents + rel->r_offset - 4);
1640 if ((z & (0xffffffff ^ RD(~0)))
1641 != (INSN_OR | RS1(O7) | RS2(G0)))
1642 break;
1644 /* The sequence was
1645 or %o7, %g0, %rN
1646 call foo
1647 or %rN, %g0, %o7
1649 If call foo was replaced with ba, replace
1650 or %rN, %g0, %o7 with nop. */
1652 reg = (y & RS1(~0)) >> 14;
1653 if (reg != ((z & RD(~0)) >> 25)
1654 || reg == G0 || reg == O7)
1655 break;
1657 bfd_put_32 (input_bfd, INSN_NOP,
1658 contents + rel->r_offset + 4);
1666 if (r == bfd_reloc_continue)
1667 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1668 contents, rel->r_offset,
1669 relocation, rel->r_addend);
1671 if (r != bfd_reloc_ok)
1673 switch (r)
1675 default:
1676 case bfd_reloc_outofrange:
1677 abort ();
1678 case bfd_reloc_overflow:
1680 const char *name;
1682 if (h != NULL)
1683 name = h->root.root.string;
1684 else
1686 name = bfd_elf_string_from_elf_section (input_bfd,
1687 symtab_hdr->sh_link,
1688 sym->st_name);
1689 if (name == NULL)
1690 return false;
1691 if (*name == '\0')
1692 name = bfd_section_name (input_bfd, sec);
1694 if (! ((*info->callbacks->reloc_overflow)
1695 (info, name, howto->name, (bfd_vma) 0,
1696 input_bfd, input_section, rel->r_offset)))
1697 return false;
1699 break;
1704 return true;
1707 /* Finish up dynamic symbol handling. We set the contents of various
1708 dynamic sections here. */
1710 static boolean
1711 elf32_sparc_finish_dynamic_symbol (output_bfd, info, h, sym)
1712 bfd *output_bfd;
1713 struct bfd_link_info *info;
1714 struct elf_link_hash_entry *h;
1715 Elf_Internal_Sym *sym;
1717 bfd *dynobj;
1719 dynobj = elf_hash_table (info)->dynobj;
1721 if (h->plt.offset != (bfd_vma) -1)
1723 asection *splt;
1724 asection *srela;
1725 Elf_Internal_Rela rela;
1727 /* This symbol has an entry in the procedure linkage table. Set
1728 it up. */
1730 BFD_ASSERT (h->dynindx != -1);
1732 splt = bfd_get_section_by_name (dynobj, ".plt");
1733 srela = bfd_get_section_by_name (dynobj, ".rela.plt");
1734 BFD_ASSERT (splt != NULL && srela != NULL);
1736 /* Fill in the entry in the procedure linkage table. */
1737 bfd_put_32 (output_bfd,
1738 PLT_ENTRY_WORD0 + h->plt.offset,
1739 splt->contents + h->plt.offset);
1740 bfd_put_32 (output_bfd,
1741 (PLT_ENTRY_WORD1
1742 + (((- (h->plt.offset + 4)) >> 2) & 0x3fffff)),
1743 splt->contents + h->plt.offset + 4);
1744 bfd_put_32 (output_bfd, PLT_ENTRY_WORD2,
1745 splt->contents + h->plt.offset + 8);
1747 /* Fill in the entry in the .rela.plt section. */
1748 rela.r_offset = (splt->output_section->vma
1749 + splt->output_offset
1750 + h->plt.offset);
1751 rela.r_info = ELF32_R_INFO (h->dynindx, R_SPARC_JMP_SLOT);
1752 rela.r_addend = 0;
1753 bfd_elf32_swap_reloca_out (output_bfd, &rela,
1754 ((Elf32_External_Rela *) srela->contents
1755 + h->plt.offset / PLT_ENTRY_SIZE - 4));
1757 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1759 /* Mark the symbol as undefined, rather than as defined in
1760 the .plt section. Leave the value alone. */
1761 sym->st_shndx = SHN_UNDEF;
1765 if (h->got.offset != (bfd_vma) -1)
1767 asection *sgot;
1768 asection *srela;
1769 Elf_Internal_Rela rela;
1771 /* This symbol has an entry in the global offset table. Set it
1772 up. */
1774 sgot = bfd_get_section_by_name (dynobj, ".got");
1775 srela = bfd_get_section_by_name (dynobj, ".rela.got");
1776 BFD_ASSERT (sgot != NULL && srela != NULL);
1778 rela.r_offset = (sgot->output_section->vma
1779 + sgot->output_offset
1780 + (h->got.offset &~ 1));
1782 /* If this is a -Bsymbolic link, and the symbol is defined
1783 locally, we just want to emit a RELATIVE reloc. Likewise if
1784 the symbol was forced to be local because of a version file.
1785 The entry in the global offset table will already have been
1786 initialized in the relocate_section function. */
1787 if (info->shared
1788 && (info->symbolic || h->dynindx == -1)
1789 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
1790 rela.r_info = ELF32_R_INFO (0, R_SPARC_RELATIVE);
1791 else
1793 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
1794 rela.r_info = ELF32_R_INFO (h->dynindx, R_SPARC_GLOB_DAT);
1797 rela.r_addend = 0;
1798 bfd_elf32_swap_reloca_out (output_bfd, &rela,
1799 ((Elf32_External_Rela *) srela->contents
1800 + srela->reloc_count));
1801 ++srela->reloc_count;
1804 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
1806 asection *s;
1807 Elf_Internal_Rela rela;
1809 /* This symbols needs a copy reloc. Set it up. */
1811 BFD_ASSERT (h->dynindx != -1);
1813 s = bfd_get_section_by_name (h->root.u.def.section->owner,
1814 ".rela.bss");
1815 BFD_ASSERT (s != NULL);
1817 rela.r_offset = (h->root.u.def.value
1818 + h->root.u.def.section->output_section->vma
1819 + h->root.u.def.section->output_offset);
1820 rela.r_info = ELF32_R_INFO (h->dynindx, R_SPARC_COPY);
1821 rela.r_addend = 0;
1822 bfd_elf32_swap_reloca_out (output_bfd, &rela,
1823 ((Elf32_External_Rela *) s->contents
1824 + s->reloc_count));
1825 ++s->reloc_count;
1828 /* Mark some specially defined symbols as absolute. */
1829 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
1830 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
1831 || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0)
1832 sym->st_shndx = SHN_ABS;
1834 return true;
1837 /* Finish up the dynamic sections. */
1839 static boolean
1840 elf32_sparc_finish_dynamic_sections (output_bfd, info)
1841 bfd *output_bfd;
1842 struct bfd_link_info *info;
1844 bfd *dynobj;
1845 asection *sdyn;
1846 asection *sgot;
1848 dynobj = elf_hash_table (info)->dynobj;
1850 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
1852 if (elf_hash_table (info)->dynamic_sections_created)
1854 asection *splt;
1855 Elf32_External_Dyn *dyncon, *dynconend;
1857 splt = bfd_get_section_by_name (dynobj, ".plt");
1858 BFD_ASSERT (splt != NULL && sdyn != NULL);
1860 dyncon = (Elf32_External_Dyn *) sdyn->contents;
1861 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
1862 for (; dyncon < dynconend; dyncon++)
1864 Elf_Internal_Dyn dyn;
1865 const char *name;
1866 boolean size;
1868 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
1870 switch (dyn.d_tag)
1872 case DT_PLTGOT: name = ".plt"; size = false; break;
1873 case DT_PLTRELSZ: name = ".rela.plt"; size = true; break;
1874 case DT_JMPREL: name = ".rela.plt"; size = false; break;
1875 default: name = NULL; size = false; break;
1878 if (name != NULL)
1880 asection *s;
1882 s = bfd_get_section_by_name (output_bfd, name);
1883 if (s == NULL)
1884 dyn.d_un.d_val = 0;
1885 else
1887 if (! size)
1888 dyn.d_un.d_ptr = s->vma;
1889 else
1891 if (s->_cooked_size != 0)
1892 dyn.d_un.d_val = s->_cooked_size;
1893 else
1894 dyn.d_un.d_val = s->_raw_size;
1897 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1901 /* Clear the first four entries in the procedure linkage table,
1902 and put a nop in the last four bytes. */
1903 if (splt->_raw_size > 0)
1905 memset (splt->contents, 0, 4 * PLT_ENTRY_SIZE);
1906 bfd_put_32 (output_bfd, SPARC_NOP,
1907 splt->contents + splt->_raw_size - 4);
1910 elf_section_data (splt->output_section)->this_hdr.sh_entsize =
1911 PLT_ENTRY_SIZE;
1914 /* Set the first entry in the global offset table to the address of
1915 the dynamic section. */
1916 sgot = bfd_get_section_by_name (dynobj, ".got");
1917 BFD_ASSERT (sgot != NULL);
1918 if (sgot->_raw_size > 0)
1920 if (sdyn == NULL)
1921 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
1922 else
1923 bfd_put_32 (output_bfd,
1924 sdyn->output_section->vma + sdyn->output_offset,
1925 sgot->contents);
1928 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
1930 return true;
1933 /* Functions for dealing with the e_flags field.
1935 We don't define set_private_flags or copy_private_bfd_data because
1936 the only currently defined values are based on the bfd mach number,
1937 so we use the latter instead and defer setting e_flags until the
1938 file is written out. */
1940 /* Merge backend specific data from an object file to the output
1941 object file when linking. */
1943 static boolean
1944 elf32_sparc_merge_private_bfd_data (ibfd, obfd)
1945 bfd *ibfd;
1946 bfd *obfd;
1948 boolean error;
1949 /* FIXME: This should not be static. */
1950 static unsigned long previous_ibfd_e_flags = (unsigned long) -1;
1952 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1953 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1954 return true;
1956 error = false;
1958 if (bfd_get_mach (ibfd) >= bfd_mach_sparc_v9)
1960 error = true;
1961 (*_bfd_error_handler)
1962 (_("%s: compiled for a 64 bit system and target is 32 bit"),
1963 bfd_get_filename (ibfd));
1965 else if ((ibfd->flags & DYNAMIC) == 0)
1967 if (bfd_get_mach (obfd) < bfd_get_mach (ibfd))
1968 bfd_set_arch_mach (obfd, bfd_arch_sparc, bfd_get_mach (ibfd));
1971 if (((elf_elfheader (ibfd)->e_flags & EF_SPARC_LEDATA)
1972 != previous_ibfd_e_flags)
1973 && previous_ibfd_e_flags != (unsigned long) -1)
1975 (*_bfd_error_handler)
1976 (_("%s: linking little endian files with big endian files"),
1977 bfd_get_filename (ibfd));
1978 error = true;
1980 previous_ibfd_e_flags = elf_elfheader (ibfd)->e_flags & EF_SPARC_LEDATA;
1982 if (error)
1984 bfd_set_error (bfd_error_bad_value);
1985 return false;
1988 return true;
1991 /* Set the right machine number. */
1993 static boolean
1994 elf32_sparc_object_p (abfd)
1995 bfd *abfd;
1997 if (elf_elfheader (abfd)->e_machine == EM_SPARC32PLUS)
1999 if (elf_elfheader (abfd)->e_flags & EF_SPARC_SUN_US3)
2000 return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
2001 bfd_mach_sparc_v8plusb);
2002 else if (elf_elfheader (abfd)->e_flags & EF_SPARC_SUN_US1)
2003 return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
2004 bfd_mach_sparc_v8plusa);
2005 else if (elf_elfheader (abfd)->e_flags & EF_SPARC_32PLUS)
2006 return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
2007 bfd_mach_sparc_v8plus);
2008 else
2009 return false;
2011 else if (elf_elfheader (abfd)->e_flags & EF_SPARC_LEDATA)
2012 return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
2013 bfd_mach_sparc_sparclite_le);
2014 else
2015 return bfd_default_set_arch_mach (abfd, bfd_arch_sparc, bfd_mach_sparc);
2018 /* The final processing done just before writing out the object file.
2019 We need to set the e_machine field appropriately. */
2021 static void
2022 elf32_sparc_final_write_processing (abfd, linker)
2023 bfd *abfd;
2024 boolean linker ATTRIBUTE_UNUSED;
2026 switch (bfd_get_mach (abfd))
2028 case bfd_mach_sparc :
2029 break; /* nothing to do */
2030 case bfd_mach_sparc_v8plus :
2031 elf_elfheader (abfd)->e_machine = EM_SPARC32PLUS;
2032 elf_elfheader (abfd)->e_flags &=~ EF_SPARC_32PLUS_MASK;
2033 elf_elfheader (abfd)->e_flags |= EF_SPARC_32PLUS;
2034 break;
2035 case bfd_mach_sparc_v8plusa :
2036 elf_elfheader (abfd)->e_machine = EM_SPARC32PLUS;
2037 elf_elfheader (abfd)->e_flags &=~ EF_SPARC_32PLUS_MASK;
2038 elf_elfheader (abfd)->e_flags |= EF_SPARC_32PLUS | EF_SPARC_SUN_US1;
2039 break;
2040 case bfd_mach_sparc_v8plusb :
2041 elf_elfheader (abfd)->e_machine = EM_SPARC32PLUS;
2042 elf_elfheader (abfd)->e_flags &=~ EF_SPARC_32PLUS_MASK;
2043 elf_elfheader (abfd)->e_flags |= EF_SPARC_32PLUS | EF_SPARC_SUN_US1
2044 | EF_SPARC_SUN_US3;
2045 break;
2046 case bfd_mach_sparc_sparclite_le :
2047 elf_elfheader (abfd)->e_machine = EM_SPARC;
2048 elf_elfheader (abfd)->e_flags |= EF_SPARC_LEDATA;
2049 break;
2050 default :
2051 abort ();
2052 break;
2056 #define TARGET_BIG_SYM bfd_elf32_sparc_vec
2057 #define TARGET_BIG_NAME "elf32-sparc"
2058 #define ELF_ARCH bfd_arch_sparc
2059 #define ELF_MACHINE_CODE EM_SPARC
2060 #define ELF_MACHINE_ALT1 EM_SPARC32PLUS
2061 #define ELF_MAXPAGESIZE 0x10000
2063 #define bfd_elf32_bfd_reloc_type_lookup elf32_sparc_reloc_type_lookup
2064 #define bfd_elf32_bfd_relax_section elf32_sparc_relax_section
2065 #define elf_info_to_howto elf32_sparc_info_to_howto
2066 #define elf_backend_create_dynamic_sections \
2067 _bfd_elf_create_dynamic_sections
2068 #define elf_backend_check_relocs elf32_sparc_check_relocs
2069 #define elf_backend_adjust_dynamic_symbol \
2070 elf32_sparc_adjust_dynamic_symbol
2071 #define elf_backend_size_dynamic_sections \
2072 elf32_sparc_size_dynamic_sections
2073 #define elf_backend_relocate_section elf32_sparc_relocate_section
2074 #define elf_backend_finish_dynamic_symbol \
2075 elf32_sparc_finish_dynamic_symbol
2076 #define elf_backend_finish_dynamic_sections \
2077 elf32_sparc_finish_dynamic_sections
2078 #define bfd_elf32_bfd_merge_private_bfd_data \
2079 elf32_sparc_merge_private_bfd_data
2080 #define elf_backend_object_p elf32_sparc_object_p
2081 #define elf_backend_final_write_processing \
2082 elf32_sparc_final_write_processing
2083 #define elf_backend_gc_mark_hook elf32_sparc_gc_mark_hook
2084 #define elf_backend_gc_sweep_hook elf32_sparc_gc_sweep_hook
2086 #define elf_backend_can_gc_sections 1
2087 #define elf_backend_want_got_plt 0
2088 #define elf_backend_plt_readonly 0
2089 #define elf_backend_want_plt_sym 1
2090 #define elf_backend_got_header_size 4
2091 #define elf_backend_plt_header_size (4*PLT_ENTRY_SIZE)
2093 #include "elf32-target.h"