* dwarf2.c (read_abbrevs): Use _raw_size directly rather than
[deliverable/binutils-gdb.git] / bfd / elf32-sparc.c
CommitLineData
252b5132
RH
1/* SPARC-specific support for 32-bit ELF
2 Copyright (C) 1993, 94, 95, 96, 97, 98, 1999 Free Software Foundation, Inc.
3
4This file is part of BFD, the Binary File Descriptor library.
5
6This program is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2 of the License, or
9(at your option) any later version.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with this program; if not, write to the Free Software
18Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19
20#include "bfd.h"
21#include "sysdep.h"
22#include "bfdlink.h"
23#include "libbfd.h"
24#include "elf-bfd.h"
25#include "elf/sparc.h"
26
27static reloc_howto_type *elf32_sparc_reloc_type_lookup
28 PARAMS ((bfd *, bfd_reloc_code_real_type));
29static void elf32_sparc_info_to_howto
30 PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
31static boolean elf32_sparc_check_relocs
32 PARAMS ((bfd *, struct bfd_link_info *, asection *,
33 const Elf_Internal_Rela *));
34static boolean elf32_sparc_adjust_dynamic_symbol
35 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
252b5132
RH
36static boolean elf32_sparc_size_dynamic_sections
37 PARAMS ((bfd *, struct bfd_link_info *));
38static boolean elf32_sparc_relocate_section
39 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
40 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
41static boolean elf32_sparc_finish_dynamic_symbol
42 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
43 Elf_Internal_Sym *));
44static boolean elf32_sparc_finish_dynamic_sections
45 PARAMS ((bfd *, struct bfd_link_info *));
46static boolean elf32_sparc_merge_private_bfd_data PARAMS ((bfd *, bfd *));
47static boolean elf32_sparc_object_p
48 PARAMS ((bfd *));
49static void elf32_sparc_final_write_processing
50 PARAMS ((bfd *, boolean));
51\f
52/* The relocation "howto" table. */
53
54static bfd_reloc_status_type sparc_elf_notsupported_reloc
55 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
56static bfd_reloc_status_type sparc_elf_wdisp16_reloc
57 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
58
59reloc_howto_type _bfd_sparc_elf_howto_table[] =
60{
61 HOWTO(R_SPARC_NONE, 0,0, 0,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_NONE", false,0,0x00000000,true),
62 HOWTO(R_SPARC_8, 0,0, 8,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_8", false,0,0x000000ff,true),
63 HOWTO(R_SPARC_16, 0,1,16,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_16", false,0,0x0000ffff,true),
64 HOWTO(R_SPARC_32, 0,2,32,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_32", false,0,0xffffffff,true),
65 HOWTO(R_SPARC_DISP8, 0,0, 8,true, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_DISP8", false,0,0x000000ff,true),
66 HOWTO(R_SPARC_DISP16, 0,1,16,true, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_DISP16", false,0,0x0000ffff,true),
67 HOWTO(R_SPARC_DISP32, 0,2,32,true, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_DISP32", false,0,0x00ffffff,true),
68 HOWTO(R_SPARC_WDISP30, 2,2,30,true, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_WDISP30", false,0,0x3fffffff,true),
69 HOWTO(R_SPARC_WDISP22, 2,2,22,true, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_WDISP22", false,0,0x003fffff,true),
70 HOWTO(R_SPARC_HI22, 10,2,22,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_HI22", false,0,0x003fffff,true),
71 HOWTO(R_SPARC_22, 0,2,22,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_22", false,0,0x003fffff,true),
72 HOWTO(R_SPARC_13, 0,2,13,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_13", false,0,0x00001fff,true),
73 HOWTO(R_SPARC_LO10, 0,2,10,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_LO10", false,0,0x000003ff,true),
74 HOWTO(R_SPARC_GOT10, 0,2,10,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_GOT10", false,0,0x000003ff,true),
75 HOWTO(R_SPARC_GOT13, 0,2,13,false,0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_GOT13", false,0,0x00001fff,true),
76 HOWTO(R_SPARC_GOT22, 10,2,22,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_GOT22", false,0,0x003fffff,true),
77 HOWTO(R_SPARC_PC10, 0,2,10,true, 0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_PC10", false,0,0x000003ff,true),
78 HOWTO(R_SPARC_PC22, 10,2,22,true, 0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_PC22", false,0,0x003fffff,true),
79 HOWTO(R_SPARC_WPLT30, 2,2,30,true, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_WPLT30", false,0,0x3fffffff,true),
80 HOWTO(R_SPARC_COPY, 0,0,00,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_COPY", false,0,0x00000000,true),
81 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),
82 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),
83 HOWTO(R_SPARC_RELATIVE, 0,0,00,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_RELATIVE",false,0,0x00000000,true),
84 HOWTO(R_SPARC_UA32, 0,0,00,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_UA32", false,0,0x00000000,true),
85 HOWTO(R_SPARC_PLT32, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_PLT32", false,0,0x00000000,true),
86 HOWTO(R_SPARC_HIPLT22, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_HIPLT22", false,0,0x00000000,true),
87 HOWTO(R_SPARC_LOPLT10, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_LOPLT10", false,0,0x00000000,true),
88 HOWTO(R_SPARC_PCPLT32, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_PCPLT32", false,0,0x00000000,true),
89 HOWTO(R_SPARC_PCPLT22, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_PCPLT22", false,0,0x00000000,true),
90 HOWTO(R_SPARC_PCPLT10, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_PCPLT10", false,0,0x00000000,true),
91 HOWTO(R_SPARC_10, 0,2,10,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_10", false,0,0x000003ff,true),
92 HOWTO(R_SPARC_11, 0,2,11,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_11", false,0,0x000007ff,true),
93 /* These are for sparc64 in a 64 bit environment.
94 Values need to be here because the table is indexed by reloc number. */
95 HOWTO(R_SPARC_64, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_64", false,0,0x00000000,true),
96 HOWTO(R_SPARC_OLO10, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_OLO10", false,0,0x00000000,true),
97 HOWTO(R_SPARC_HH22, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_HH22", false,0,0x00000000,true),
98 HOWTO(R_SPARC_HM10, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_HM10", false,0,0x00000000,true),
99 HOWTO(R_SPARC_LM22, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_LM22", false,0,0x00000000,true),
100 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),
101 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),
102 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),
103 /* End sparc64 in 64 bit environment values.
104 The following are for sparc64 in a 32 bit environment. */
105 HOWTO(R_SPARC_WDISP16, 2,2,16,true, 0,complain_overflow_signed, sparc_elf_wdisp16_reloc,"R_SPARC_WDISP16", false,0,0x00000000,true),
106 HOWTO(R_SPARC_WDISP19, 2,2,19,true, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_WDISP19", false,0,0x0007ffff,true),
107 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),
108 HOWTO(R_SPARC_7, 0,2, 7,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_7", false,0,0x0000007f,true),
109 HOWTO(R_SPARC_5, 0,2, 5,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_5", false,0,0x0000001f,true),
110 HOWTO(R_SPARC_6, 0,2, 6,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_6", false,0,0x0000003f,true),
111 HOWTO(R_SPARC_NONE, 0,0, 0,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_NONE", false,0,0x00000000,true),
112 HOWTO(R_SPARC_NONE, 0,0, 0,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_NONE", false,0,0x00000000,true),
113 HOWTO(R_SPARC_NONE, 0,0, 0,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_NONE", false,0,0x00000000,true),
114 HOWTO(R_SPARC_NONE, 0,0, 0,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_NONE", false,0,0x00000000,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_REV32, 0,2,32,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_REV32", false,0,0xffffffff,true),
122};
123static reloc_howto_type elf32_sparc_vtinherit_howto =
124 HOWTO (R_SPARC_GNU_VTINHERIT, 0,2,0,false,0,complain_overflow_dont, NULL, "R_SPARC_GNU_VTINHERIT", false,0, 0, false);
125static reloc_howto_type elf32_sparc_vtentry_howto =
126 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);
127
128
129struct elf_reloc_map {
130 bfd_reloc_code_real_type bfd_reloc_val;
131 unsigned char elf_reloc_val;
132};
133
134static CONST struct elf_reloc_map sparc_reloc_map[] =
135{
136 { BFD_RELOC_NONE, R_SPARC_NONE, },
137 { BFD_RELOC_16, R_SPARC_16, },
138 { BFD_RELOC_8, R_SPARC_8 },
139 { BFD_RELOC_8_PCREL, R_SPARC_DISP8 },
140 { BFD_RELOC_CTOR, R_SPARC_32 },
141 { BFD_RELOC_32, R_SPARC_32 },
142 { BFD_RELOC_32_PCREL, R_SPARC_DISP32 },
143 { BFD_RELOC_HI22, R_SPARC_HI22 },
144 { BFD_RELOC_LO10, R_SPARC_LO10, },
145 { BFD_RELOC_32_PCREL_S2, R_SPARC_WDISP30 },
146 { BFD_RELOC_SPARC22, R_SPARC_22 },
147 { BFD_RELOC_SPARC13, R_SPARC_13 },
148 { BFD_RELOC_SPARC_GOT10, R_SPARC_GOT10 },
149 { BFD_RELOC_SPARC_GOT13, R_SPARC_GOT13 },
150 { BFD_RELOC_SPARC_GOT22, R_SPARC_GOT22 },
151 { BFD_RELOC_SPARC_PC10, R_SPARC_PC10 },
152 { BFD_RELOC_SPARC_PC22, R_SPARC_PC22 },
153 { BFD_RELOC_SPARC_WPLT30, R_SPARC_WPLT30 },
154 { BFD_RELOC_SPARC_COPY, R_SPARC_COPY },
155 { BFD_RELOC_SPARC_GLOB_DAT, R_SPARC_GLOB_DAT },
156 { BFD_RELOC_SPARC_JMP_SLOT, R_SPARC_JMP_SLOT },
157 { BFD_RELOC_SPARC_RELATIVE, R_SPARC_RELATIVE },
158 { BFD_RELOC_SPARC_WDISP22, R_SPARC_WDISP22 },
159 /* ??? Doesn't dwarf use this? */
160/*{ BFD_RELOC_SPARC_UA32, R_SPARC_UA32 }, not used?? */
161 {BFD_RELOC_SPARC_10, R_SPARC_10},
162 {BFD_RELOC_SPARC_11, R_SPARC_11},
163 {BFD_RELOC_SPARC_64, R_SPARC_64},
164 {BFD_RELOC_SPARC_OLO10, R_SPARC_OLO10},
165 {BFD_RELOC_SPARC_HH22, R_SPARC_HH22},
166 {BFD_RELOC_SPARC_HM10, R_SPARC_HM10},
167 {BFD_RELOC_SPARC_LM22, R_SPARC_LM22},
168 {BFD_RELOC_SPARC_PC_HH22, R_SPARC_PC_HH22},
169 {BFD_RELOC_SPARC_PC_HM10, R_SPARC_PC_HM10},
170 {BFD_RELOC_SPARC_PC_LM22, R_SPARC_PC_LM22},
171 {BFD_RELOC_SPARC_WDISP16, R_SPARC_WDISP16},
172 {BFD_RELOC_SPARC_WDISP19, R_SPARC_WDISP19},
173 {BFD_RELOC_SPARC_7, R_SPARC_7},
174 {BFD_RELOC_SPARC_5, R_SPARC_5},
175 {BFD_RELOC_SPARC_6, R_SPARC_6},
176 {BFD_RELOC_SPARC_REV32, R_SPARC_REV32 },
177 {BFD_RELOC_VTABLE_INHERIT, R_SPARC_GNU_VTINHERIT},
178 {BFD_RELOC_VTABLE_ENTRY, R_SPARC_GNU_VTENTRY},
179};
180
181static reloc_howto_type *
182elf32_sparc_reloc_type_lookup (abfd, code)
5f771d47 183 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
184 bfd_reloc_code_real_type code;
185{
186 unsigned int i;
187
188 switch (code)
189 {
190 case BFD_RELOC_VTABLE_INHERIT:
191 return &elf32_sparc_vtinherit_howto;
192
193 case BFD_RELOC_VTABLE_ENTRY:
194 return &elf32_sparc_vtentry_howto;
195
196 default:
197 for (i = 0; i < sizeof (sparc_reloc_map) / sizeof (struct elf_reloc_map); i++)
198 {
199 if (sparc_reloc_map[i].bfd_reloc_val == code)
200 return &_bfd_sparc_elf_howto_table[(int) sparc_reloc_map[i].elf_reloc_val];
201 }
202 }
203 bfd_set_error (bfd_error_bad_value);
204 return NULL;
205}
206
207/* We need to use ELF32_R_TYPE so we have our own copy of this function,
208 and elf64-sparc.c has its own copy. */
209
210static void
211elf32_sparc_info_to_howto (abfd, cache_ptr, dst)
5f771d47 212 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
213 arelent *cache_ptr;
214 Elf_Internal_Rela *dst;
215{
60dac299
RH
216 switch (ELF32_R_TYPE(dst->r_info))
217 {
218 case R_SPARC_GNU_VTINHERIT:
219 cache_ptr->howto = &elf32_sparc_vtinherit_howto;
220 break;
221
222 case R_SPARC_GNU_VTENTRY:
223 cache_ptr->howto = &elf32_sparc_vtentry_howto;
224 break;
225
226 default:
227 BFD_ASSERT (ELF32_R_TYPE(dst->r_info) < (unsigned int) R_SPARC_max_std);
228 cache_ptr->howto = &_bfd_sparc_elf_howto_table[ELF32_R_TYPE(dst->r_info)];
229 }
252b5132
RH
230}
231\f
232/* For unsupported relocs. */
233
234static bfd_reloc_status_type
235sparc_elf_notsupported_reloc (abfd,
236 reloc_entry,
237 symbol,
238 data,
239 input_section,
240 output_bfd,
241 error_message)
5f771d47
ILT
242 bfd *abfd ATTRIBUTE_UNUSED;
243 arelent *reloc_entry ATTRIBUTE_UNUSED;
244 asymbol *symbol ATTRIBUTE_UNUSED;
245 PTR data ATTRIBUTE_UNUSED;
246 asection *input_section ATTRIBUTE_UNUSED;
247 bfd *output_bfd ATTRIBUTE_UNUSED;
248 char **error_message ATTRIBUTE_UNUSED;
252b5132
RH
249{
250 return bfd_reloc_notsupported;
251}
252
253/* Handle the WDISP16 reloc. */
254
255static bfd_reloc_status_type
256sparc_elf_wdisp16_reloc (abfd,
257 reloc_entry,
258 symbol,
259 data,
260 input_section,
261 output_bfd,
262 error_message)
263 bfd *abfd;
264 arelent *reloc_entry;
265 asymbol *symbol;
266 PTR data;
267 asection *input_section;
268 bfd *output_bfd;
5f771d47 269 char **error_message ATTRIBUTE_UNUSED;
252b5132
RH
270{
271 bfd_vma relocation;
272 bfd_vma x;
273
274 if (output_bfd != (bfd *) NULL
275 && (symbol->flags & BSF_SECTION_SYM) == 0
276 && (! reloc_entry->howto->partial_inplace
277 || reloc_entry->addend == 0))
278 {
279 reloc_entry->address += input_section->output_offset;
280 return bfd_reloc_ok;
281 }
282
283 if (output_bfd != NULL)
284 return bfd_reloc_continue;
285
286 if (reloc_entry->address > input_section->_cooked_size)
287 return bfd_reloc_outofrange;
288
289 relocation = (symbol->value
290 + symbol->section->output_section->vma
291 + symbol->section->output_offset);
292 relocation += reloc_entry->addend;
293 relocation -= (input_section->output_section->vma
294 + input_section->output_offset);
295 relocation -= reloc_entry->address;
296
297 x = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
298 x |= ((((relocation >> 2) & 0xc000) << 6)
299 | ((relocation >> 2) & 0x3fff));
300 bfd_put_32 (abfd, x, (bfd_byte *) data + reloc_entry->address);
301
302 if ((bfd_signed_vma) relocation < - 0x40000
303 || (bfd_signed_vma) relocation > 0x3ffff)
304 return bfd_reloc_overflow;
305 else
306 return bfd_reloc_ok;
307}
308\f
309/* Functions for the SPARC ELF linker. */
310
311/* The name of the dynamic interpreter. This is put in the .interp
312 section. */
313
314#define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
315
316/* The nop opcode we use. */
317
318#define SPARC_NOP 0x01000000
319
320/* The size in bytes of an entry in the procedure linkage table. */
321
322#define PLT_ENTRY_SIZE 12
323
324/* The first four entries in a procedure linkage table are reserved,
325 and the initial contents are unimportant (we zero them out).
326 Subsequent entries look like this. See the SVR4 ABI SPARC
327 supplement to see how this works. */
328
329/* sethi %hi(.-.plt0),%g1. We fill in the address later. */
330#define PLT_ENTRY_WORD0 0x03000000
331/* b,a .plt0. We fill in the offset later. */
332#define PLT_ENTRY_WORD1 0x30800000
333/* nop. */
334#define PLT_ENTRY_WORD2 SPARC_NOP
335
336/* Look through the relocs for a section during the first phase, and
337 allocate space in the global offset table or procedure linkage
338 table. */
339
340static boolean
341elf32_sparc_check_relocs (abfd, info, sec, relocs)
342 bfd *abfd;
343 struct bfd_link_info *info;
344 asection *sec;
345 const Elf_Internal_Rela *relocs;
346{
347 bfd *dynobj;
348 Elf_Internal_Shdr *symtab_hdr;
349 struct elf_link_hash_entry **sym_hashes;
350 bfd_vma *local_got_offsets;
351 const Elf_Internal_Rela *rel;
352 const Elf_Internal_Rela *rel_end;
353 asection *sgot;
354 asection *srelgot;
355 asection *sreloc;
356
357 if (info->relocateable)
358 return true;
359
360 dynobj = elf_hash_table (info)->dynobj;
361 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
362 sym_hashes = elf_sym_hashes (abfd);
363 local_got_offsets = elf_local_got_offsets (abfd);
364
365 sgot = NULL;
366 srelgot = NULL;
367 sreloc = NULL;
368
369 rel_end = relocs + sec->reloc_count;
370 for (rel = relocs; rel < rel_end; rel++)
371 {
372 unsigned long r_symndx;
373 struct elf_link_hash_entry *h;
374
375 r_symndx = ELF32_R_SYM (rel->r_info);
376 if (r_symndx < symtab_hdr->sh_info)
377 h = NULL;
378 else
379 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
380
381 switch (ELF32_R_TYPE (rel->r_info))
382 {
383 case R_SPARC_GOT10:
384 case R_SPARC_GOT13:
385 case R_SPARC_GOT22:
386 /* This symbol requires a global offset table entry. */
387
388 if (dynobj == NULL)
389 {
390 /* Create the .got section. */
391 elf_hash_table (info)->dynobj = dynobj = abfd;
392 if (! _bfd_elf_create_got_section (dynobj, info))
393 return false;
394 }
395
396 if (sgot == NULL)
397 {
398 sgot = bfd_get_section_by_name (dynobj, ".got");
399 BFD_ASSERT (sgot != NULL);
400 }
401
402 if (srelgot == NULL
403 && (h != NULL || info->shared))
404 {
405 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
406 if (srelgot == NULL)
407 {
408 srelgot = bfd_make_section (dynobj, ".rela.got");
409 if (srelgot == NULL
410 || ! bfd_set_section_flags (dynobj, srelgot,
411 (SEC_ALLOC
412 | SEC_LOAD
413 | SEC_HAS_CONTENTS
414 | SEC_IN_MEMORY
415 | SEC_LINKER_CREATED
416 | SEC_READONLY))
417 || ! bfd_set_section_alignment (dynobj, srelgot, 2))
418 return false;
419 }
420 }
421
422 if (h != NULL)
423 {
424 if (h->got.offset != (bfd_vma) -1)
425 {
426 /* We have already allocated space in the .got. */
427 break;
428 }
429 h->got.offset = sgot->_raw_size;
430
431 /* Make sure this symbol is output as a dynamic symbol. */
432 if (h->dynindx == -1)
433 {
434 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
435 return false;
436 }
437
438 srelgot->_raw_size += sizeof (Elf32_External_Rela);
439 }
440 else
441 {
442 /* This is a global offset table entry for a local
443 symbol. */
444 if (local_got_offsets == NULL)
445 {
446 size_t size;
447 register unsigned int i;
448
449 size = symtab_hdr->sh_info * sizeof (bfd_vma);
450 local_got_offsets = (bfd_vma *) bfd_alloc (abfd, size);
451 if (local_got_offsets == NULL)
452 return false;
453 elf_local_got_offsets (abfd) = local_got_offsets;
454 for (i = 0; i < symtab_hdr->sh_info; i++)
455 local_got_offsets[i] = (bfd_vma) -1;
456 }
457 if (local_got_offsets[r_symndx] != (bfd_vma) -1)
458 {
459 /* We have already allocated space in the .got. */
460 break;
461 }
462 local_got_offsets[r_symndx] = sgot->_raw_size;
463
464 if (info->shared)
465 {
466 /* If we are generating a shared object, we need to
467 output a R_SPARC_RELATIVE reloc so that the
468 dynamic linker can adjust this GOT entry. */
469 srelgot->_raw_size += sizeof (Elf32_External_Rela);
470 }
471 }
472
473 sgot->_raw_size += 4;
474
475 /* If the .got section is more than 0x1000 bytes, we add
476 0x1000 to the value of _GLOBAL_OFFSET_TABLE_, so that 13
477 bit relocations have a greater chance of working. */
478 if (sgot->_raw_size >= 0x1000
479 && elf_hash_table (info)->hgot->root.u.def.value == 0)
480 elf_hash_table (info)->hgot->root.u.def.value = 0x1000;
481
482 break;
483
484 case R_SPARC_WPLT30:
485 /* This symbol requires a procedure linkage table entry. We
486 actually build the entry in adjust_dynamic_symbol,
487 because this might be a case of linking PIC code without
488 linking in any dynamic objects, in which case we don't
489 need to generate a procedure linkage table after all. */
490
491 if (h == NULL)
492 {
493 /* The Solaris native assembler will generate a WPLT30
494 reloc for a local symbol if you assemble a call from
495 one section to another when using -K pic. We treat
496 it as WDISP30. */
497 break;
498 }
499
500 /* Make sure this symbol is output as a dynamic symbol. */
501 if (h->dynindx == -1)
502 {
503 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
504 return false;
505 }
506
507 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
508
509 break;
510
511 case R_SPARC_PC10:
512 case R_SPARC_PC22:
7843f00e
ILT
513 if (h != NULL)
514 h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
515
252b5132
RH
516 if (h != NULL
517 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
518 break;
519 /* Fall through. */
520 case R_SPARC_DISP8:
521 case R_SPARC_DISP16:
522 case R_SPARC_DISP32:
523 case R_SPARC_WDISP30:
524 case R_SPARC_WDISP22:
525 case R_SPARC_WDISP19:
526 case R_SPARC_WDISP16:
7843f00e
ILT
527 if (h != NULL)
528 h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
529
252b5132
RH
530 /* If we are linking with -Bsymbolic, we do not need to copy
531 a PC relative reloc against a global symbol which is
532 defined in an object we are including in the link (i.e.,
533 DEF_REGULAR is set). FIXME: At this point we have not
534 seen all the input files, so it is possible that
535 DEF_REGULAR is not set now but will be set later (it is
536 never cleared). This needs to be handled as in
537 elf32-i386.c. */
538 if (h == NULL
539 || (info->symbolic
540 && (h->elf_link_hash_flags
541 & ELF_LINK_HASH_DEF_REGULAR) != 0))
542 break;
543 /* Fall through. */
544 case R_SPARC_8:
545 case R_SPARC_16:
546 case R_SPARC_32:
547 case R_SPARC_HI22:
548 case R_SPARC_22:
549 case R_SPARC_13:
550 case R_SPARC_LO10:
551 case R_SPARC_UA32:
7843f00e
ILT
552 if (h != NULL)
553 h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
554
252b5132
RH
555 if (info->shared)
556 {
557 /* When creating a shared object, we must copy these
558 relocs into the output file. We create a reloc
559 section in dynobj and make room for the reloc. */
560 if (sreloc == NULL)
561 {
562 const char *name;
563
564 name = (bfd_elf_string_from_elf_section
565 (abfd,
566 elf_elfheader (abfd)->e_shstrndx,
567 elf_section_data (sec)->rel_hdr.sh_name));
568 if (name == NULL)
569 return false;
570
571 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
572 && strcmp (bfd_get_section_name (abfd, sec),
573 name + 5) == 0);
574
575 sreloc = bfd_get_section_by_name (dynobj, name);
576 if (sreloc == NULL)
577 {
578 flagword flags;
579
580 sreloc = bfd_make_section (dynobj, name);
581 flags = (SEC_HAS_CONTENTS | SEC_READONLY
582 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
583 if ((sec->flags & SEC_ALLOC) != 0)
584 flags |= SEC_ALLOC | SEC_LOAD;
585 if (sreloc == NULL
586 || ! bfd_set_section_flags (dynobj, sreloc, flags)
587 || ! bfd_set_section_alignment (dynobj, sreloc, 2))
588 return false;
589 }
590 }
591
592 sreloc->_raw_size += sizeof (Elf32_External_Rela);
593 }
594
595 break;
596
597 case R_SPARC_GNU_VTINHERIT:
598 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
599 return false;
600 break;
601
602 case R_SPARC_GNU_VTENTRY:
603 if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend))
604 return false;
605 break;
606
607 default:
608 break;
609 }
610 }
611
612 return true;
613}
614
615static asection *
616elf32_sparc_gc_mark_hook (abfd, info, rel, h, sym)
617 bfd *abfd;
5f771d47 618 struct bfd_link_info *info ATTRIBUTE_UNUSED;
252b5132
RH
619 Elf_Internal_Rela *rel;
620 struct elf_link_hash_entry *h;
621 Elf_Internal_Sym *sym;
622{
623
624 if (h != NULL)
625 {
626 switch (ELF32_R_TYPE (rel->r_info))
627 {
628 case R_SPARC_GNU_VTINHERIT:
629 case R_SPARC_GNU_VTENTRY:
630 break;
631
632 default:
633 switch (h->root.type)
634 {
635 case bfd_link_hash_defined:
636 case bfd_link_hash_defweak:
637 return h->root.u.def.section;
638
639 case bfd_link_hash_common:
640 return h->root.u.c.p->section;
e049a0de
ILT
641
642 default:
643 break;
252b5132
RH
644 }
645 }
646 }
647 else
648 {
649 if (!(elf_bad_symtab (abfd)
650 && ELF_ST_BIND (sym->st_info) != STB_LOCAL)
651 && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE)
652 && sym->st_shndx != SHN_COMMON))
653 {
654 return bfd_section_from_elf_index (abfd, sym->st_shndx);
655 }
656 }
657
658 return NULL;
659}
660
661/* Update the got entry reference counts for the section being removed. */
662static boolean
663elf32_sparc_gc_sweep_hook (abfd, info, sec, relocs)
664 bfd *abfd;
5f771d47 665 struct bfd_link_info *info ATTRIBUTE_UNUSED;
252b5132
RH
666 asection *sec;
667 const Elf_Internal_Rela *relocs;
668{
669
670 Elf_Internal_Shdr *symtab_hdr;
671 struct elf_link_hash_entry **sym_hashes;
672 bfd_signed_vma *local_got_refcounts;
673 const Elf_Internal_Rela *rel, *relend;
674 unsigned long r_symndx;
675 struct elf_link_hash_entry *h;
676
677 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
678 sym_hashes = elf_sym_hashes (abfd);
679 local_got_refcounts = elf_local_got_refcounts (abfd);
680
681 relend = relocs + sec->reloc_count;
682 for (rel = relocs; rel < relend; rel++)
683 switch (ELF32_R_TYPE (rel->r_info))
684 {
685 case R_SPARC_GOT10:
686 case R_SPARC_GOT13:
687 case R_SPARC_GOT22:
688 r_symndx = ELF32_R_SYM (rel->r_info);
689 if (r_symndx >= symtab_hdr->sh_info)
690 {
691 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
692 if (h->got.refcount > 0)
693 h->got.refcount--;
694 }
695 else
696 {
697 if (local_got_refcounts[r_symndx] > 0)
698 local_got_refcounts[r_symndx]--;
699 }
700 break;
701
702 case R_SPARC_PLT32:
703 case R_SPARC_HIPLT22:
704 case R_SPARC_LOPLT10:
705 case R_SPARC_PCPLT32:
706 case R_SPARC_PCPLT10:
707 r_symndx = ELF32_R_SYM (rel->r_info);
708 if (r_symndx >= symtab_hdr->sh_info)
709 {
710 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
711 if (h->plt.refcount > 0)
712 h->plt.refcount--;
713 }
714 break;
715
716 default:
717 break;
718 }
719
720 return true;
721}
722
723/* Adjust a symbol defined by a dynamic object and referenced by a
724 regular object. The current definition is in some section of the
725 dynamic object, but we're not including those sections. We have to
726 change the definition to something the rest of the link can
727 understand. */
728
729static boolean
730elf32_sparc_adjust_dynamic_symbol (info, h)
731 struct bfd_link_info *info;
732 struct elf_link_hash_entry *h;
733{
734 bfd *dynobj;
735 asection *s;
736 unsigned int power_of_two;
737
738 dynobj = elf_hash_table (info)->dynobj;
739
740 /* Make sure we know what is going on here. */
741 BFD_ASSERT (dynobj != NULL
742 && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
743 || h->weakdef != NULL
744 || ((h->elf_link_hash_flags
745 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
746 && (h->elf_link_hash_flags
747 & ELF_LINK_HASH_REF_REGULAR) != 0
748 && (h->elf_link_hash_flags
749 & ELF_LINK_HASH_DEF_REGULAR) == 0)));
750
751 /* If this is a function, put it in the procedure linkage table. We
752 will fill in the contents of the procedure linkage table later
753 (although we could actually do it here). The STT_NOTYPE
754 condition is a hack specifically for the Oracle libraries
755 delivered for Solaris; for some inexplicable reason, they define
756 some of their functions as STT_NOTYPE when they really should be
757 STT_FUNC. */
758 if (h->type == STT_FUNC
759 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0
760 || (h->type == STT_NOTYPE
761 && (h->root.type == bfd_link_hash_defined
762 || h->root.type == bfd_link_hash_defweak)
763 && (h->root.u.def.section->flags & SEC_CODE) != 0))
764 {
765 if (! elf_hash_table (info)->dynamic_sections_created
766 || ((!info->shared || info->symbolic || h->dynindx == -1)
767 && (h->elf_link_hash_flags
768 & ELF_LINK_HASH_DEF_REGULAR) != 0))
769 {
770 /* This case can occur if we saw a WPLT30 reloc in an input
771 file, but none of the input files were dynamic objects.
772 Or, when linking the main application or a -Bsymbolic
773 shared library against PIC code. Or when a global symbol
774 has been made private, e.g. via versioning.
775
776 In these cases we know what value the symbol will resolve
777 to, so we don't actually need to build a procedure linkage
778 table, and we can just do a WDISP30 reloc instead. */
779
780 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
781 return true;
782 }
783
784 s = bfd_get_section_by_name (dynobj, ".plt");
785 BFD_ASSERT (s != NULL);
786
787 /* The first four entries in .plt are reserved. */
788 if (s->_raw_size == 0)
789 s->_raw_size = 4 * PLT_ENTRY_SIZE;
790
791 /* The procedure linkage table has a maximum size. */
792 if (s->_raw_size >= 0x400000)
793 {
794 bfd_set_error (bfd_error_bad_value);
795 return false;
796 }
797
798 /* If this symbol is not defined in a regular file, and we are
799 not generating a shared library, then set the symbol to this
800 location in the .plt. This is required to make function
801 pointers compare as equal between the normal executable and
802 the shared library. */
803 if (! info->shared
804 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
805 {
806 h->root.u.def.section = s;
807 h->root.u.def.value = s->_raw_size;
808 }
809
810 h->plt.offset = s->_raw_size;
811
812 /* Make room for this entry. */
813 s->_raw_size += PLT_ENTRY_SIZE;
814
815 /* We also need to make an entry in the .rela.plt section. */
816
817 s = bfd_get_section_by_name (dynobj, ".rela.plt");
818 BFD_ASSERT (s != NULL);
819 s->_raw_size += sizeof (Elf32_External_Rela);
820
821 return true;
822 }
823
824 /* If this is a weak symbol, and there is a real definition, the
825 processor independent code will have arranged for us to see the
826 real definition first, and we can just use the same value. */
827 if (h->weakdef != NULL)
828 {
829 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
830 || h->weakdef->root.type == bfd_link_hash_defweak);
831 h->root.u.def.section = h->weakdef->root.u.def.section;
832 h->root.u.def.value = h->weakdef->root.u.def.value;
833 return true;
834 }
835
836 /* This is a reference to a symbol defined by a dynamic object which
837 is not a function. */
838
839 /* If we are creating a shared library, we must presume that the
840 only references to the symbol are via the global offset table.
841 For such cases we need not do anything here; the relocations will
842 be handled correctly by relocate_section. */
843 if (info->shared)
844 return true;
845
7843f00e
ILT
846 /* If there are no references to this symbol that do not use the
847 GOT, we don't need to generate a copy reloc. */
848 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
849 return true;
850
252b5132
RH
851 /* We must allocate the symbol in our .dynbss section, which will
852 become part of the .bss section of the executable. There will be
853 an entry for this symbol in the .dynsym section. The dynamic
854 object will contain position independent code, so all references
855 from the dynamic object to this symbol will go through the global
856 offset table. The dynamic linker will use the .dynsym entry to
857 determine the address it must put in the global offset table, so
858 both the dynamic object and the regular object will refer to the
859 same memory location for the variable. */
860
861 s = bfd_get_section_by_name (dynobj, ".dynbss");
862 BFD_ASSERT (s != NULL);
863
864 /* We must generate a R_SPARC_COPY reloc to tell the dynamic linker
865 to copy the initial value out of the dynamic object and into the
866 runtime process image. We need to remember the offset into the
867 .rel.bss section we are going to use. */
868 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
869 {
870 asection *srel;
871
872 srel = bfd_get_section_by_name (dynobj, ".rela.bss");
873 BFD_ASSERT (srel != NULL);
874 srel->_raw_size += sizeof (Elf32_External_Rela);
875 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
876 }
877
878 /* We need to figure out the alignment required for this symbol. I
879 have no idea how ELF linkers handle this. */
880 power_of_two = bfd_log2 (h->size);
881 if (power_of_two > 3)
882 power_of_two = 3;
883
884 /* Apply the required alignment. */
885 s->_raw_size = BFD_ALIGN (s->_raw_size,
886 (bfd_size_type) (1 << power_of_two));
887 if (power_of_two > bfd_get_section_alignment (dynobj, s))
888 {
889 if (! bfd_set_section_alignment (dynobj, s, power_of_two))
890 return false;
891 }
892
893 /* Define the symbol as being at this point in the section. */
894 h->root.u.def.section = s;
895 h->root.u.def.value = s->_raw_size;
896
897 /* Increment the section size to make room for the symbol. */
898 s->_raw_size += h->size;
899
900 return true;
901}
902
903/* Set the sizes of the dynamic sections. */
904
905static boolean
906elf32_sparc_size_dynamic_sections (output_bfd, info)
907 bfd *output_bfd;
908 struct bfd_link_info *info;
909{
910 bfd *dynobj;
911 asection *s;
912 boolean reltext;
913 boolean relplt;
914
915 dynobj = elf_hash_table (info)->dynobj;
916 BFD_ASSERT (dynobj != NULL);
917
918 if (elf_hash_table (info)->dynamic_sections_created)
919 {
920 /* Set the contents of the .interp section to the interpreter. */
921 if (! info->shared)
922 {
923 s = bfd_get_section_by_name (dynobj, ".interp");
924 BFD_ASSERT (s != NULL);
925 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
926 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
927 }
928
929 /* Make space for the trailing nop in .plt. */
930 s = bfd_get_section_by_name (dynobj, ".plt");
931 BFD_ASSERT (s != NULL);
932 if (s->_raw_size > 0)
933 s->_raw_size += 4;
934 }
935 else
936 {
937 /* We may have created entries in the .rela.got section.
938 However, if we are not creating the dynamic sections, we will
939 not actually use these entries. Reset the size of .rela.got,
940 which will cause it to get stripped from the output file
941 below. */
942 s = bfd_get_section_by_name (dynobj, ".rela.got");
943 if (s != NULL)
944 s->_raw_size = 0;
945 }
946
947 /* The check_relocs and adjust_dynamic_symbol entry points have
948 determined the sizes of the various dynamic sections. Allocate
949 memory for them. */
950 reltext = false;
951 relplt = false;
952 for (s = dynobj->sections; s != NULL; s = s->next)
953 {
954 const char *name;
955 boolean strip;
956
957 if ((s->flags & SEC_LINKER_CREATED) == 0)
958 continue;
959
960 /* It's OK to base decisions on the section name, because none
961 of the dynobj section names depend upon the input files. */
962 name = bfd_get_section_name (dynobj, s);
963
964 strip = false;
965
966 if (strncmp (name, ".rela", 5) == 0)
967 {
968 if (s->_raw_size == 0)
969 {
970 /* If we don't need this section, strip it from the
971 output file. This is to handle .rela.bss and
972 .rel.plt. We must create it in
973 create_dynamic_sections, because it must be created
974 before the linker maps input sections to output
975 sections. The linker does that before
976 adjust_dynamic_symbol is called, and it is that
977 function which decides whether anything needs to go
978 into these sections. */
979 strip = true;
980 }
981 else
982 {
983 const char *outname;
984 asection *target;
985
986 /* If this relocation section applies to a read only
987 section, then we probably need a DT_TEXTREL entry. */
988 outname = bfd_get_section_name (output_bfd,
989 s->output_section);
990 target = bfd_get_section_by_name (output_bfd, outname + 5);
991 if (target != NULL
992 && (target->flags & SEC_READONLY) != 0
993 && (target->flags & SEC_ALLOC) != 0)
994 reltext = true;
995
996 if (strcmp (name, ".rela.plt") == 0)
997 relplt = true;
998
999 /* We use the reloc_count field as a counter if we need
1000 to copy relocs into the output file. */
1001 s->reloc_count = 0;
1002 }
1003 }
1004 else if (strcmp (name, ".plt") != 0
1005 && strcmp (name, ".got") != 0)
1006 {
1007 /* It's not one of our sections, so don't allocate space. */
1008 continue;
1009 }
1010
1011 if (strip)
1012 {
7f8d5fc9 1013 _bfd_strip_section_from_output (info, s);
252b5132
RH
1014 continue;
1015 }
1016
1017 /* Allocate memory for the section contents. */
1018 s->contents = (bfd_byte *) bfd_alloc (dynobj, s->_raw_size);
1019 if (s->contents == NULL && s->_raw_size != 0)
1020 return false;
1021 }
1022
1023 if (elf_hash_table (info)->dynamic_sections_created)
1024 {
1025 /* Add some entries to the .dynamic section. We fill in the
1026 values later, in elf32_sparc_finish_dynamic_sections, but we
1027 must add the entries now so that we get the correct size for
1028 the .dynamic section. The DT_DEBUG entry is filled in by the
1029 dynamic linker and used by the debugger. */
1030 if (! info->shared)
1031 {
1032 if (! bfd_elf32_add_dynamic_entry (info, DT_DEBUG, 0))
1033 return false;
1034 }
1035
1036 if (relplt)
1037 {
1038 if (! bfd_elf32_add_dynamic_entry (info, DT_PLTGOT, 0)
1039 || ! bfd_elf32_add_dynamic_entry (info, DT_PLTRELSZ, 0)
1040 || ! bfd_elf32_add_dynamic_entry (info, DT_PLTREL, DT_RELA)
1041 || ! bfd_elf32_add_dynamic_entry (info, DT_JMPREL, 0))
1042 return false;
1043 }
1044
1045 if (! bfd_elf32_add_dynamic_entry (info, DT_RELA, 0)
1046 || ! bfd_elf32_add_dynamic_entry (info, DT_RELASZ, 0)
1047 || ! bfd_elf32_add_dynamic_entry (info, DT_RELAENT,
1048 sizeof (Elf32_External_Rela)))
1049 return false;
1050
1051 if (reltext)
1052 {
1053 if (! bfd_elf32_add_dynamic_entry (info, DT_TEXTREL, 0))
1054 return false;
1055 }
1056 }
1057
252b5132
RH
1058 return true;
1059}
1060
1061/* Relocate a SPARC ELF section. */
1062
1063static boolean
1064elf32_sparc_relocate_section (output_bfd, info, input_bfd, input_section,
1065 contents, relocs, local_syms, local_sections)
1066 bfd *output_bfd;
1067 struct bfd_link_info *info;
1068 bfd *input_bfd;
1069 asection *input_section;
1070 bfd_byte *contents;
1071 Elf_Internal_Rela *relocs;
1072 Elf_Internal_Sym *local_syms;
1073 asection **local_sections;
1074{
1075 bfd *dynobj;
1076 Elf_Internal_Shdr *symtab_hdr;
1077 struct elf_link_hash_entry **sym_hashes;
1078 bfd_vma *local_got_offsets;
1079 bfd_vma got_base;
1080 asection *sgot;
1081 asection *splt;
1082 asection *sreloc;
1083 Elf_Internal_Rela *rel;
1084 Elf_Internal_Rela *relend;
1085
1086 dynobj = elf_hash_table (info)->dynobj;
1087 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1088 sym_hashes = elf_sym_hashes (input_bfd);
1089 local_got_offsets = elf_local_got_offsets (input_bfd);
1090
1091 if (elf_hash_table (info)->hgot == NULL)
1092 got_base = 0;
1093 else
1094 got_base = elf_hash_table (info)->hgot->root.u.def.value;
1095
1096 sgot = NULL;
1097 splt = NULL;
1098 sreloc = NULL;
1099
1100 rel = relocs;
1101 relend = relocs + input_section->reloc_count;
1102 for (; rel < relend; rel++)
1103 {
1104 int r_type;
1105 reloc_howto_type *howto;
1106 unsigned long r_symndx;
1107 struct elf_link_hash_entry *h;
1108 Elf_Internal_Sym *sym;
1109 asection *sec;
1110 bfd_vma relocation;
1111 bfd_reloc_status_type r;
1112
1113 r_type = ELF32_R_TYPE (rel->r_info);
1114
1115 if (r_type == R_SPARC_GNU_VTINHERIT
1116 || r_type == R_SPARC_GNU_VTENTRY)
1117 continue;
1118
60dac299 1119 if (r_type < 0 || r_type >= (int) R_SPARC_max_std)
252b5132
RH
1120 {
1121 bfd_set_error (bfd_error_bad_value);
1122 return false;
1123 }
1124 howto = _bfd_sparc_elf_howto_table + r_type;
1125
1126 r_symndx = ELF32_R_SYM (rel->r_info);
1127
1128 if (info->relocateable)
1129 {
1130 /* This is a relocateable link. We don't have to change
1131 anything, unless the reloc is against a section symbol,
1132 in which case we have to adjust according to where the
1133 section symbol winds up in the output section. */
1134 if (r_symndx < symtab_hdr->sh_info)
1135 {
1136 sym = local_syms + r_symndx;
1137 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
1138 {
1139 sec = local_sections[r_symndx];
1140 rel->r_addend += sec->output_offset + sym->st_value;
1141 }
1142 }
1143
1144 continue;
1145 }
1146
1147 /* This is a final link. */
1148 h = NULL;
1149 sym = NULL;
1150 sec = NULL;
1151 if (r_symndx < symtab_hdr->sh_info)
1152 {
1153 sym = local_syms + r_symndx;
1154 sec = local_sections[r_symndx];
1155 relocation = (sec->output_section->vma
1156 + sec->output_offset
1157 + sym->st_value);
1158 }
1159 else
1160 {
1161 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1162 while (h->root.type == bfd_link_hash_indirect
1163 || h->root.type == bfd_link_hash_warning)
1164 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1165 if (h->root.type == bfd_link_hash_defined
1166 || h->root.type == bfd_link_hash_defweak)
1167 {
1168 sec = h->root.u.def.section;
1169 if ((r_type == R_SPARC_WPLT30
1170 && h->plt.offset != (bfd_vma) -1)
1171 || ((r_type == R_SPARC_GOT10
1172 || r_type == R_SPARC_GOT13
1173 || r_type == R_SPARC_GOT22)
1174 && elf_hash_table (info)->dynamic_sections_created
1175 && (! info->shared
1176 || (! info->symbolic && h->dynindx != -1)
1177 || (h->elf_link_hash_flags
1178 & ELF_LINK_HASH_DEF_REGULAR) == 0))
1179 || (info->shared
1180 && ((! info->symbolic && h->dynindx != -1)
1181 || (h->elf_link_hash_flags
1182 & ELF_LINK_HASH_DEF_REGULAR) == 0)
1183 && (r_type == R_SPARC_8
1184 || r_type == R_SPARC_16
1185 || r_type == R_SPARC_32
1186 || r_type == R_SPARC_DISP8
1187 || r_type == R_SPARC_DISP16
1188 || r_type == R_SPARC_DISP32
1189 || r_type == R_SPARC_WDISP30
1190 || r_type == R_SPARC_WDISP22
1191 || r_type == R_SPARC_WDISP19
1192 || r_type == R_SPARC_WDISP16
1193 || r_type == R_SPARC_HI22
1194 || r_type == R_SPARC_22
1195 || r_type == R_SPARC_13
1196 || r_type == R_SPARC_LO10
1197 || r_type == R_SPARC_UA32
1198 || ((r_type == R_SPARC_PC10
1199 || r_type == R_SPARC_PC22)
1200 && strcmp (h->root.root.string,
1201 "_GLOBAL_OFFSET_TABLE_") != 0))))
1202 {
1203 /* In these cases, we don't need the relocation
1204 value. We check specially because in some
1205 obscure cases sec->output_section will be NULL. */
1206 relocation = 0;
1207 }
1208 else
1209 relocation = (h->root.u.def.value
1210 + sec->output_section->vma
1211 + sec->output_offset);
1212 }
1213 else if (h->root.type == bfd_link_hash_undefweak)
1214 relocation = 0;
1215 else if (info->shared && !info->symbolic && !info->no_undefined)
1216 relocation = 0;
1217 else
1218 {
1219 if (! ((*info->callbacks->undefined_symbol)
1220 (info, h->root.root.string, input_bfd,
1221 input_section, rel->r_offset)))
1222 return false;
1223 relocation = 0;
1224 }
1225 }
1226
1227 switch (r_type)
1228 {
1229 case R_SPARC_GOT10:
1230 case R_SPARC_GOT13:
1231 case R_SPARC_GOT22:
1232 /* Relocation is to the entry for this symbol in the global
1233 offset table. */
1234 if (sgot == NULL)
1235 {
1236 sgot = bfd_get_section_by_name (dynobj, ".got");
1237 BFD_ASSERT (sgot != NULL);
1238 }
1239
1240 if (h != NULL)
1241 {
1242 bfd_vma off;
1243
1244 off = h->got.offset;
1245 BFD_ASSERT (off != (bfd_vma) -1);
1246
1247 if (! elf_hash_table (info)->dynamic_sections_created
1248 || (info->shared
1249 && (info->symbolic || h->dynindx == -1)
1250 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
1251 {
1252 /* This is actually a static link, or it is a
1253 -Bsymbolic link and the symbol is defined
1254 locally, or the symbol was forced to be local
1255 because of a version file. We must initialize
1256 this entry in the global offset table. Since the
1257 offset must always be a multiple of 4, we use the
1258 least significant bit to record whether we have
1259 initialized it already.
1260
1261 When doing a dynamic link, we create a .rela.got
1262 relocation entry to initialize the value. This
1263 is done in the finish_dynamic_symbol routine. */
1264 if ((off & 1) != 0)
1265 off &= ~1;
1266 else
1267 {
1268 bfd_put_32 (output_bfd, relocation,
1269 sgot->contents + off);
1270 h->got.offset |= 1;
1271 }
1272 }
1273
1274 relocation = sgot->output_offset + off - got_base;
1275 }
1276 else
1277 {
1278 bfd_vma off;
1279
1280 BFD_ASSERT (local_got_offsets != NULL
1281 && local_got_offsets[r_symndx] != (bfd_vma) -1);
1282
1283 off = local_got_offsets[r_symndx];
1284
1285 /* The offset must always be a multiple of 4. We use
1286 the least significant bit to record whether we have
1287 already processed this entry. */
1288 if ((off & 1) != 0)
1289 off &= ~1;
1290 else
1291 {
1292 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
1293
1294 if (info->shared)
1295 {
1296 asection *srelgot;
1297 Elf_Internal_Rela outrel;
1298
1299 /* We need to generate a R_SPARC_RELATIVE reloc
1300 for the dynamic linker. */
1301 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
1302 BFD_ASSERT (srelgot != NULL);
1303
1304 outrel.r_offset = (sgot->output_section->vma
1305 + sgot->output_offset
1306 + off);
1307 outrel.r_info = ELF32_R_INFO (0, R_SPARC_RELATIVE);
1308 outrel.r_addend = 0;
1309 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1310 (((Elf32_External_Rela *)
1311 srelgot->contents)
1312 + srelgot->reloc_count));
1313 ++srelgot->reloc_count;
1314 }
1315
1316 local_got_offsets[r_symndx] |= 1;
1317 }
1318
1319 relocation = sgot->output_offset + off - got_base;
1320 }
1321
1322 break;
1323
1324 case R_SPARC_WPLT30:
1325 /* Relocation is to the entry for this symbol in the
1326 procedure linkage table. */
1327
1328 /* The Solaris native assembler will generate a WPLT30 reloc
1329 for a local symbol if you assemble a call from one
1330 section to another when using -K pic. We treat it as
1331 WDISP30. */
1332 if (h == NULL)
1333 break;
1334
1335 if (h->plt.offset == (bfd_vma) -1)
1336 {
1337 /* We didn't make a PLT entry for this symbol. This
1338 happens when statically linking PIC code, or when
1339 using -Bsymbolic. */
1340 break;
1341 }
1342
1343 if (splt == NULL)
1344 {
1345 splt = bfd_get_section_by_name (dynobj, ".plt");
1346 BFD_ASSERT (splt != NULL);
1347 }
1348
1349 relocation = (splt->output_section->vma
1350 + splt->output_offset
1351 + h->plt.offset);
1352 break;
1353
1354 case R_SPARC_PC10:
1355 case R_SPARC_PC22:
1356 if (h != NULL
1357 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
1358 break;
1359 /* Fall through. */
1360 case R_SPARC_DISP8:
1361 case R_SPARC_DISP16:
1362 case R_SPARC_DISP32:
1363 case R_SPARC_WDISP30:
1364 case R_SPARC_WDISP22:
1365 case R_SPARC_WDISP19:
1366 case R_SPARC_WDISP16:
1367 if (h == NULL
1368 || (info->symbolic
1369 && (h->elf_link_hash_flags
1370 & ELF_LINK_HASH_DEF_REGULAR) != 0))
1371 break;
1372 /* Fall through. */
1373 case R_SPARC_8:
1374 case R_SPARC_16:
1375 case R_SPARC_32:
1376 case R_SPARC_HI22:
1377 case R_SPARC_22:
1378 case R_SPARC_13:
1379 case R_SPARC_LO10:
1380 case R_SPARC_UA32:
1381 if (info->shared)
1382 {
1383 Elf_Internal_Rela outrel;
1384 boolean skip;
1385
1386 /* When generating a shared object, these relocations
1387 are copied into the output file to be resolved at run
1388 time. */
1389
1390 if (sreloc == NULL)
1391 {
1392 const char *name;
1393
1394 name = (bfd_elf_string_from_elf_section
1395 (input_bfd,
1396 elf_elfheader (input_bfd)->e_shstrndx,
1397 elf_section_data (input_section)->rel_hdr.sh_name));
1398 if (name == NULL)
1399 return false;
1400
1401 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
1402 && strcmp (bfd_get_section_name (input_bfd,
1403 input_section),
1404 name + 5) == 0);
1405
1406 sreloc = bfd_get_section_by_name (dynobj, name);
1407 BFD_ASSERT (sreloc != NULL);
1408 }
1409
1410 skip = false;
1411
1412 if (elf_section_data (input_section)->stab_info == NULL)
1413 outrel.r_offset = rel->r_offset;
1414 else
1415 {
1416 bfd_vma off;
1417
1418 off = (_bfd_stab_section_offset
1419 (output_bfd, &elf_hash_table (info)->stab_info,
1420 input_section,
1421 &elf_section_data (input_section)->stab_info,
1422 rel->r_offset));
1423 if (off == (bfd_vma) -1)
1424 skip = true;
1425 outrel.r_offset = off;
1426 }
1427
1428 outrel.r_offset += (input_section->output_section->vma
1429 + input_section->output_offset);
1430
1431 if (skip)
1432 memset (&outrel, 0, sizeof outrel);
1433 /* h->dynindx may be -1 if the symbol was marked to
1434 become local. */
1435 else if (h != NULL
1436 && ((! info->symbolic && h->dynindx != -1)
1437 || (h->elf_link_hash_flags
1438 & ELF_LINK_HASH_DEF_REGULAR) == 0))
1439 {
1440 BFD_ASSERT (h->dynindx != -1);
1441 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
1442 outrel.r_addend = rel->r_addend;
1443 }
1444 else
1445 {
1446 if (r_type == R_SPARC_32)
1447 {
1448 outrel.r_info = ELF32_R_INFO (0, R_SPARC_RELATIVE);
1449 outrel.r_addend = relocation + rel->r_addend;
1450 }
1451 else
1452 {
1453 long indx;
1454
1455 if (h == NULL)
1456 sec = local_sections[r_symndx];
1457 else
1458 {
1459 BFD_ASSERT (h->root.type == bfd_link_hash_defined
1460 || (h->root.type
1461 == bfd_link_hash_defweak));
1462 sec = h->root.u.def.section;
1463 }
1464 if (sec != NULL && bfd_is_abs_section (sec))
1465 indx = 0;
1466 else if (sec == NULL || sec->owner == NULL)
1467 {
1468 bfd_set_error (bfd_error_bad_value);
1469 return false;
1470 }
1471 else
1472 {
1473 asection *osec;
1474
1475 osec = sec->output_section;
1476 indx = elf_section_data (osec)->dynindx;
1477
1478 /* FIXME: we really should be able to link non-pic
1479 shared libraries. */
1480 if (indx == 0)
1481 {
1482 BFD_FAIL ();
1483 (*_bfd_error_handler)
1484 (_("%s: probably compiled without -fPIC?"),
1485 bfd_get_filename (input_bfd));
1486 bfd_set_error (bfd_error_bad_value);
1487 return false;
1488 }
1489 }
1490
1491 outrel.r_info = ELF32_R_INFO (indx, r_type);
840a9995 1492 outrel.r_addend = relocation + rel->r_addend;
252b5132
RH
1493 }
1494 }
1495
1496 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1497 (((Elf32_External_Rela *)
1498 sreloc->contents)
1499 + sreloc->reloc_count));
1500 ++sreloc->reloc_count;
1501
1502 /* This reloc will be computed at runtime, so there's no
1503 need to do anything now, unless this is a RELATIVE
1504 reloc in an unallocated section. */
1505 if (skip
1506 || (input_section->flags & SEC_ALLOC) != 0
1507 || ELF32_R_TYPE (outrel.r_info) != R_SPARC_RELATIVE)
1508 continue;
1509 }
1510 break;
1511
1512 default:
1513 break;
1514 }
1515
1516 if (r_type == R_SPARC_WDISP16)
1517 {
1518 bfd_vma x;
1519
1520 relocation += rel->r_addend;
1521 relocation -= (input_section->output_section->vma
1522 + input_section->output_offset);
1523 relocation -= rel->r_offset;
1524
1525 x = bfd_get_32 (input_bfd, contents + rel->r_offset);
1526 x |= ((((relocation >> 2) & 0xc000) << 6)
1527 | ((relocation >> 2) & 0x3fff));
1528 bfd_put_32 (input_bfd, x, contents + rel->r_offset);
1529
1530 if ((bfd_signed_vma) relocation < - 0x40000
1531 || (bfd_signed_vma) relocation > 0x3ffff)
1532 r = bfd_reloc_overflow;
1533 else
1534 r = bfd_reloc_ok;
1535 }
1536 else if (r_type == R_SPARC_REV32)
1537 {
1538 bfd_vma x;
1539
1540 relocation = relocation + rel->r_addend;
1541
1542 x = bfd_get_32 (input_bfd, contents + rel->r_offset);
1543 x = x + relocation;
1544 bfd_putl32 (/*input_bfd,*/ x, contents + rel->r_offset);
1545 r = bfd_reloc_ok;
1546 }
1547 else
1548 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1549 contents, rel->r_offset,
1550 relocation, rel->r_addend);
1551
1552
1553 if (r != bfd_reloc_ok)
1554 {
1555 switch (r)
1556 {
1557 default:
1558 case bfd_reloc_outofrange:
1559 abort ();
1560 case bfd_reloc_overflow:
1561 {
1562 const char *name;
1563
1564 if (h != NULL)
1565 name = h->root.root.string;
1566 else
1567 {
1568 name = bfd_elf_string_from_elf_section (input_bfd,
1569 symtab_hdr->sh_link,
1570 sym->st_name);
1571 if (name == NULL)
1572 return false;
1573 if (*name == '\0')
1574 name = bfd_section_name (input_bfd, sec);
1575 }
1576 if (! ((*info->callbacks->reloc_overflow)
1577 (info, name, howto->name, (bfd_vma) 0,
1578 input_bfd, input_section, rel->r_offset)))
1579 return false;
1580 }
1581 break;
1582 }
1583 }
1584 }
1585
1586 return true;
1587}
1588
1589/* Finish up dynamic symbol handling. We set the contents of various
1590 dynamic sections here. */
1591
1592static boolean
1593elf32_sparc_finish_dynamic_symbol (output_bfd, info, h, sym)
1594 bfd *output_bfd;
1595 struct bfd_link_info *info;
1596 struct elf_link_hash_entry *h;
1597 Elf_Internal_Sym *sym;
1598{
1599 bfd *dynobj;
1600
1601 dynobj = elf_hash_table (info)->dynobj;
1602
1603 if (h->plt.offset != (bfd_vma) -1)
1604 {
1605 asection *splt;
1606 asection *srela;
1607 Elf_Internal_Rela rela;
1608
1609 /* This symbol has an entry in the procedure linkage table. Set
1610 it up. */
1611
1612 BFD_ASSERT (h->dynindx != -1);
1613
1614 splt = bfd_get_section_by_name (dynobj, ".plt");
1615 srela = bfd_get_section_by_name (dynobj, ".rela.plt");
1616 BFD_ASSERT (splt != NULL && srela != NULL);
1617
1618 /* Fill in the entry in the procedure linkage table. */
1619 bfd_put_32 (output_bfd,
1620 PLT_ENTRY_WORD0 + h->plt.offset,
1621 splt->contents + h->plt.offset);
1622 bfd_put_32 (output_bfd,
1623 (PLT_ENTRY_WORD1
1624 + (((- (h->plt.offset + 4)) >> 2) & 0x3fffff)),
1625 splt->contents + h->plt.offset + 4);
1626 bfd_put_32 (output_bfd, PLT_ENTRY_WORD2,
1627 splt->contents + h->plt.offset + 8);
1628
1629 /* Fill in the entry in the .rela.plt section. */
1630 rela.r_offset = (splt->output_section->vma
1631 + splt->output_offset
1632 + h->plt.offset);
1633 rela.r_info = ELF32_R_INFO (h->dynindx, R_SPARC_JMP_SLOT);
1634 rela.r_addend = 0;
1635 bfd_elf32_swap_reloca_out (output_bfd, &rela,
1636 ((Elf32_External_Rela *) srela->contents
1637 + h->plt.offset / PLT_ENTRY_SIZE - 4));
1638
1639 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1640 {
1641 /* Mark the symbol as undefined, rather than as defined in
1642 the .plt section. Leave the value alone. */
1643 sym->st_shndx = SHN_UNDEF;
1644 }
1645 }
1646
1647 if (h->got.offset != (bfd_vma) -1)
1648 {
1649 asection *sgot;
1650 asection *srela;
1651 Elf_Internal_Rela rela;
1652
1653 /* This symbol has an entry in the global offset table. Set it
1654 up. */
1655
1656 sgot = bfd_get_section_by_name (dynobj, ".got");
1657 srela = bfd_get_section_by_name (dynobj, ".rela.got");
1658 BFD_ASSERT (sgot != NULL && srela != NULL);
1659
1660 rela.r_offset = (sgot->output_section->vma
1661 + sgot->output_offset
1662 + (h->got.offset &~ 1));
1663
1664 /* If this is a -Bsymbolic link, and the symbol is defined
1665 locally, we just want to emit a RELATIVE reloc. Likewise if
1666 the symbol was forced to be local because of a version file.
1667 The entry in the global offset table will already have been
1668 initialized in the relocate_section function. */
1669 if (info->shared
1670 && (info->symbolic || h->dynindx == -1)
1671 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
1672 rela.r_info = ELF32_R_INFO (0, R_SPARC_RELATIVE);
1673 else
1674 {
1675 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
1676 rela.r_info = ELF32_R_INFO (h->dynindx, R_SPARC_GLOB_DAT);
1677 }
1678
1679 rela.r_addend = 0;
1680 bfd_elf32_swap_reloca_out (output_bfd, &rela,
1681 ((Elf32_External_Rela *) srela->contents
1682 + srela->reloc_count));
1683 ++srela->reloc_count;
1684 }
1685
1686 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
1687 {
1688 asection *s;
1689 Elf_Internal_Rela rela;
1690
1691 /* This symbols needs a copy reloc. Set it up. */
1692
1693 BFD_ASSERT (h->dynindx != -1);
1694
1695 s = bfd_get_section_by_name (h->root.u.def.section->owner,
1696 ".rela.bss");
1697 BFD_ASSERT (s != NULL);
1698
1699 rela.r_offset = (h->root.u.def.value
1700 + h->root.u.def.section->output_section->vma
1701 + h->root.u.def.section->output_offset);
1702 rela.r_info = ELF32_R_INFO (h->dynindx, R_SPARC_COPY);
1703 rela.r_addend = 0;
1704 bfd_elf32_swap_reloca_out (output_bfd, &rela,
1705 ((Elf32_External_Rela *) s->contents
1706 + s->reloc_count));
1707 ++s->reloc_count;
1708 }
1709
1710 /* Mark some specially defined symbols as absolute. */
1711 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
1712 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
1713 || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0)
1714 sym->st_shndx = SHN_ABS;
1715
1716 return true;
1717}
1718
1719/* Finish up the dynamic sections. */
1720
1721static boolean
1722elf32_sparc_finish_dynamic_sections (output_bfd, info)
1723 bfd *output_bfd;
1724 struct bfd_link_info *info;
1725{
1726 bfd *dynobj;
1727 asection *sdyn;
1728 asection *sgot;
1729
1730 dynobj = elf_hash_table (info)->dynobj;
1731
1732 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
1733
1734 if (elf_hash_table (info)->dynamic_sections_created)
1735 {
1736 asection *splt;
1737 Elf32_External_Dyn *dyncon, *dynconend;
1738
1739 splt = bfd_get_section_by_name (dynobj, ".plt");
1740 BFD_ASSERT (splt != NULL && sdyn != NULL);
1741
1742 dyncon = (Elf32_External_Dyn *) sdyn->contents;
1743 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
1744 for (; dyncon < dynconend; dyncon++)
1745 {
1746 Elf_Internal_Dyn dyn;
1747 const char *name;
1748 boolean size;
1749
1750 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
1751
1752 switch (dyn.d_tag)
1753 {
1754 case DT_PLTGOT: name = ".plt"; size = false; break;
1755 case DT_PLTRELSZ: name = ".rela.plt"; size = true; break;
1756 case DT_JMPREL: name = ".rela.plt"; size = false; break;
1757 default: name = NULL; size = false; break;
1758 }
1759
1760 if (name != NULL)
1761 {
1762 asection *s;
1763
1764 s = bfd_get_section_by_name (output_bfd, name);
1765 if (s == NULL)
1766 dyn.d_un.d_val = 0;
1767 else
1768 {
1769 if (! size)
1770 dyn.d_un.d_ptr = s->vma;
1771 else
1772 {
1773 if (s->_cooked_size != 0)
1774 dyn.d_un.d_val = s->_cooked_size;
1775 else
1776 dyn.d_un.d_val = s->_raw_size;
1777 }
1778 }
1779 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1780 }
1781 }
1782
1783 /* Clear the first four entries in the procedure linkage table,
1784 and put a nop in the last four bytes. */
1785 if (splt->_raw_size > 0)
1786 {
1787 memset (splt->contents, 0, 4 * PLT_ENTRY_SIZE);
1788 bfd_put_32 (output_bfd, SPARC_NOP,
1789 splt->contents + splt->_raw_size - 4);
1790 }
1791
1792 elf_section_data (splt->output_section)->this_hdr.sh_entsize =
1793 PLT_ENTRY_SIZE;
1794 }
1795
1796 /* Set the first entry in the global offset table to the address of
1797 the dynamic section. */
1798 sgot = bfd_get_section_by_name (dynobj, ".got");
1799 BFD_ASSERT (sgot != NULL);
1800 if (sgot->_raw_size > 0)
1801 {
1802 if (sdyn == NULL)
1803 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
1804 else
1805 bfd_put_32 (output_bfd,
1806 sdyn->output_section->vma + sdyn->output_offset,
1807 sgot->contents);
1808 }
1809
1810 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
1811
252b5132
RH
1812 return true;
1813}
1814\f
1815/* Functions for dealing with the e_flags field.
1816
1817 We don't define set_private_flags or copy_private_bfd_data because
1818 the only currently defined values are based on the bfd mach number,
1819 so we use the latter instead and defer setting e_flags until the
1820 file is written out. */
1821
1822/* Merge backend specific data from an object file to the output
1823 object file when linking. */
1824
1825static boolean
1826elf32_sparc_merge_private_bfd_data (ibfd, obfd)
1827 bfd *ibfd;
1828 bfd *obfd;
1829{
1830 boolean error;
5f771d47
ILT
1831 /* FIXME: This should not be static. */
1832 static unsigned long previous_ibfd_e_flags = (unsigned long) -1;
252b5132
RH
1833
1834 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1835 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1836 return true;
1837
1838 error = false;
1839
1840#if 0
1841 /* ??? The native linker doesn't do this so we can't (otherwise gcc would
1842 have to know which linker is being used). Instead, the native linker
1843 bumps up the architecture level when it has to. However, I still think
1844 warnings like these are good, so it would be nice to have them turned on
1845 by some option. */
1846
1847 /* If the output machine is normal sparc, we can't allow v9 input files. */
1848 if (bfd_get_mach (obfd) == bfd_mach_sparc
1849 && (bfd_get_mach (ibfd) == bfd_mach_sparc_v8plus
1850 || bfd_get_mach (ibfd) == bfd_mach_sparc_v8plusa))
1851 {
1852 error = true;
1853 (*_bfd_error_handler)
1854 (_("%s: compiled for a v8plus system and target is v8"),
1855 bfd_get_filename (ibfd));
1856 }
1857 /* If the output machine is v9, we can't allow v9+vis input files. */
1858 if (bfd_get_mach (obfd) == bfd_mach_sparc_v8plus
1859 && bfd_get_mach (ibfd) == bfd_mach_sparc_v8plusa)
1860 {
1861 error = true;
1862 (*_bfd_error_handler)
1863 (_("%s: compiled for a v8plusa system and target is v8plus"),
1864 bfd_get_filename (ibfd));
1865 }
1866#else
1867 if (bfd_get_mach (ibfd) >= bfd_mach_sparc_v9)
1868 {
1869 error = true;
1870 (*_bfd_error_handler)
1871 (_("%s: compiled for a 64 bit system and target is 32 bit"),
1872 bfd_get_filename (ibfd));
1873 }
1874 else if (bfd_get_mach (obfd) < bfd_get_mach (ibfd))
1875 bfd_set_arch_mach (obfd, bfd_arch_sparc, bfd_get_mach (ibfd));
1876#endif
1877
1878 if (((elf_elfheader (ibfd)->e_flags & EF_SPARC_LEDATA)
1879 != previous_ibfd_e_flags)
5f771d47 1880 && previous_ibfd_e_flags != (unsigned long) -1)
252b5132
RH
1881 {
1882 (*_bfd_error_handler)
1883 (_("%s: linking little endian files with big endian files"),
1884 bfd_get_filename (ibfd));
1885 error = true;
1886 }
1887 previous_ibfd_e_flags = elf_elfheader (ibfd)->e_flags & EF_SPARC_LEDATA;
1888
1889 if (error)
1890 {
1891 bfd_set_error (bfd_error_bad_value);
1892 return false;
1893 }
1894
1895 return true;
1896}
1897\f
1898/* Set the right machine number. */
1899
1900static boolean
1901elf32_sparc_object_p (abfd)
1902 bfd *abfd;
1903{
1904 if (elf_elfheader (abfd)->e_machine == EM_SPARC32PLUS)
1905 {
1906 if (elf_elfheader (abfd)->e_flags & EF_SPARC_SUN_US1)
1907 return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
1908 bfd_mach_sparc_v8plusa);
1909 else if (elf_elfheader (abfd)->e_flags & EF_SPARC_32PLUS)
1910 return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
1911 bfd_mach_sparc_v8plus);
1912 else
1913 return false;
1914 }
1915 else if (elf_elfheader (abfd)->e_flags & EF_SPARC_LEDATA)
1916 return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
1917 bfd_mach_sparc_sparclite_le);
1918 else
1919 return bfd_default_set_arch_mach (abfd, bfd_arch_sparc, bfd_mach_sparc);
1920}
1921
1922/* The final processing done just before writing out the object file.
1923 We need to set the e_machine field appropriately. */
1924
1925static void
1926elf32_sparc_final_write_processing (abfd, linker)
1927 bfd *abfd;
5f771d47 1928 boolean linker ATTRIBUTE_UNUSED;
252b5132
RH
1929{
1930 switch (bfd_get_mach (abfd))
1931 {
1932 case bfd_mach_sparc :
1933 break; /* nothing to do */
1934 case bfd_mach_sparc_v8plus :
1935 elf_elfheader (abfd)->e_machine = EM_SPARC32PLUS;
1936 elf_elfheader (abfd)->e_flags &=~ EF_SPARC_32PLUS_MASK;
1937 elf_elfheader (abfd)->e_flags |= EF_SPARC_32PLUS;
1938 break;
1939 case bfd_mach_sparc_v8plusa :
1940 elf_elfheader (abfd)->e_machine = EM_SPARC32PLUS;
1941 elf_elfheader (abfd)->e_flags &=~ EF_SPARC_32PLUS_MASK;
1942 elf_elfheader (abfd)->e_flags |= EF_SPARC_32PLUS | EF_SPARC_SUN_US1;
1943 break;
1944 case bfd_mach_sparc_sparclite_le :
1945 elf_elfheader (abfd)->e_machine = EM_SPARC;
1946 elf_elfheader (abfd)->e_flags |= EF_SPARC_LEDATA;
1947 break;
1948 default :
1949 abort ();
1950 break;
1951 }
1952}
1953\f
1954#define TARGET_BIG_SYM bfd_elf32_sparc_vec
1955#define TARGET_BIG_NAME "elf32-sparc"
1956#define ELF_ARCH bfd_arch_sparc
1957#define ELF_MACHINE_CODE EM_SPARC
1958#define ELF_MACHINE_ALT1 EM_SPARC32PLUS
1959#define ELF_MAXPAGESIZE 0x10000
1960
1961#define bfd_elf32_bfd_reloc_type_lookup elf32_sparc_reloc_type_lookup
1962#define elf_info_to_howto elf32_sparc_info_to_howto
1963#define elf_backend_create_dynamic_sections \
1964 _bfd_elf_create_dynamic_sections
1965#define elf_backend_check_relocs elf32_sparc_check_relocs
1966#define elf_backend_adjust_dynamic_symbol \
1967 elf32_sparc_adjust_dynamic_symbol
1968#define elf_backend_size_dynamic_sections \
1969 elf32_sparc_size_dynamic_sections
1970#define elf_backend_relocate_section elf32_sparc_relocate_section
1971#define elf_backend_finish_dynamic_symbol \
1972 elf32_sparc_finish_dynamic_symbol
1973#define elf_backend_finish_dynamic_sections \
1974 elf32_sparc_finish_dynamic_sections
1975#define bfd_elf32_bfd_merge_private_bfd_data \
1976 elf32_sparc_merge_private_bfd_data
1977#define elf_backend_object_p elf32_sparc_object_p
1978#define elf_backend_final_write_processing \
1979 elf32_sparc_final_write_processing
1980#define elf_backend_gc_mark_hook elf32_sparc_gc_mark_hook
1981#define elf_backend_gc_sweep_hook elf32_sparc_gc_sweep_hook
1982
1983#define elf_backend_can_gc_sections 1
1984#define elf_backend_want_got_plt 0
1985#define elf_backend_plt_readonly 0
1986#define elf_backend_want_plt_sym 1
1987#define elf_backend_got_header_size 4
1988#define elf_backend_plt_header_size (4*PLT_ENTRY_SIZE)
1989
1990#include "elf32-target.h"
This page took 0.141821 seconds and 4 git commands to generate.