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