Wed Oct 18 15:53:56 1995 steve chamberlain <sac@slash.cygnus.com>
[deliverable/binutils-gdb.git] / bfd / elf32-sparc.c
CommitLineData
32090b8e 1/* SPARC-specific support for 32-bit ELF
f786a73f 2 Copyright 1993, 1994, 1995 Free Software Foundation, Inc.
32090b8e
KR
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
943fbd5b 18Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
32090b8e
KR
19
20#include "bfd.h"
21#include "sysdep.h"
013dec1a 22#include "bfdlink.h"
32090b8e
KR
23#include "libbfd.h"
24#include "libelf.h"
25
f786a73f 26static reloc_howto_type *bfd_elf32_bfd_reloc_type_lookup
013dec1a
ILT
27 PARAMS ((bfd *, bfd_reloc_code_real_type));
28static void elf_info_to_howto
29 PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
4fbc96ad
KR
30static boolean elf32_sparc_check_relocs
31 PARAMS ((bfd *, struct bfd_link_info *, asection *,
32 const Elf_Internal_Rela *));
013dec1a
ILT
33static boolean elf32_sparc_adjust_dynamic_symbol
34 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
14cac507
ILT
35static boolean elf32_sparc_adjust_dynindx
36 PARAMS ((struct elf_link_hash_entry *, PTR));
013dec1a
ILT
37static boolean elf32_sparc_size_dynamic_sections
38 PARAMS ((bfd *, struct bfd_link_info *));
39static boolean elf32_sparc_relocate_section
40 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
12662be4 41 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
013dec1a
ILT
42static boolean elf32_sparc_finish_dynamic_symbol
43 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
44 Elf_Internal_Sym *));
45static boolean elf32_sparc_finish_dynamic_sections
46 PARAMS ((bfd *, struct bfd_link_info *));
47
32090b8e
KR
48enum reloc_type
49 {
50 R_SPARC_NONE = 0,
51 R_SPARC_8, R_SPARC_16, R_SPARC_32,
52 R_SPARC_DISP8, R_SPARC_DISP16, R_SPARC_DISP32,
53 R_SPARC_WDISP30, R_SPARC_WDISP22,
54 R_SPARC_HI22, R_SPARC_22,
55 R_SPARC_13, R_SPARC_LO10,
56 R_SPARC_GOT10, R_SPARC_GOT13, R_SPARC_GOT22,
57 R_SPARC_PC10, R_SPARC_PC22,
58 R_SPARC_WPLT30,
59 R_SPARC_COPY,
60 R_SPARC_GLOB_DAT, R_SPARC_JMP_SLOT,
61 R_SPARC_RELATIVE,
62 R_SPARC_UA32,
63 R_SPARC_max
64 };
65
66#if 0
67static CONST char *CONST reloc_type_names[] =
68{
69 "R_SPARC_NONE",
70 "R_SPARC_8", "R_SPARC_16", "R_SPARC_32",
71 "R_SPARC_DISP8", "R_SPARC_DISP16", "R_SPARC_DISP32",
72 "R_SPARC_WDISP30", "R_SPARC_WDISP22",
73 "R_SPARC_HI22", "R_SPARC_22",
74 "R_SPARC_13", "R_SPARC_LO10",
75 "R_SPARC_GOT10", "R_SPARC_GOT13", "R_SPARC_GOT22",
76 "R_SPARC_PC10", "R_SPARC_PC22",
77 "R_SPARC_WPLT30",
78 "R_SPARC_COPY",
79 "R_SPARC_GLOB_DAT", "R_SPARC_JMP_SLOT",
80 "R_SPARC_RELATIVE",
81 "R_SPARC_UA32",
82};
83#endif
84
85static reloc_howto_type elf_sparc_howto_table[] =
86{
013dec1a
ILT
87 HOWTO(R_SPARC_NONE, 0,0, 0,false,0,complain_overflow_dont, bfd_elf_generic_reloc,"R_SPARC_NONE", false,0,0x00000000,true),
88 HOWTO(R_SPARC_8, 0,0, 8,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,"R_SPARC_8", false,0,0x000000ff,true),
89 HOWTO(R_SPARC_16, 0,1,16,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,"R_SPARC_16", false,0,0x0000ffff,true),
90 HOWTO(R_SPARC_32, 0,2,32,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,"R_SPARC_32", false,0,0xffffffff,true),
91 HOWTO(R_SPARC_DISP8, 0,0, 8,true, 0,complain_overflow_signed, bfd_elf_generic_reloc,"R_SPARC_DISP8", false,0,0x000000ff,true),
92 HOWTO(R_SPARC_DISP16, 0,1,16,true, 0,complain_overflow_signed, bfd_elf_generic_reloc,"R_SPARC_DISP16", false,0,0x0000ffff,true),
93 HOWTO(R_SPARC_DISP32, 0,2,32,true, 0,complain_overflow_signed, bfd_elf_generic_reloc,"R_SPARC_DISP32", false,0,0x00ffffff,true),
94 HOWTO(R_SPARC_WDISP30, 2,2,30,true, 0,complain_overflow_signed, bfd_elf_generic_reloc,"R_SPARC_WDISP30", false,0,0x3fffffff,true),
95 HOWTO(R_SPARC_WDISP22, 2,2,22,true, 0,complain_overflow_signed, bfd_elf_generic_reloc,"R_SPARC_WDISP22", false,0,0x003fffff,true),
96 HOWTO(R_SPARC_HI22, 10,2,22,false,0,complain_overflow_dont, bfd_elf_generic_reloc,"R_SPARC_HI22", false,0,0x003fffff,true),
97 HOWTO(R_SPARC_22, 0,2,22,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,"R_SPARC_22", false,0,0x003fffff,true),
98 HOWTO(R_SPARC_13, 0,2,13,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,"R_SPARC_13", false,0,0x00001fff,true),
99 HOWTO(R_SPARC_LO10, 0,2,10,false,0,complain_overflow_dont, bfd_elf_generic_reloc,"R_SPARC_LO10", false,0,0x000003ff,true),
4fbc96ad 100 HOWTO(R_SPARC_GOT10, 0,2,10,false,0,complain_overflow_dont, bfd_elf_generic_reloc,"R_SPARC_GOT10", false,0,0x000003ff,true),
30dc85f1 101 HOWTO(R_SPARC_GOT13, 0,2,13,false,0,complain_overflow_signed, bfd_elf_generic_reloc,"R_SPARC_GOT13", false,0,0x00001fff,true),
4fbc96ad
KR
102 HOWTO(R_SPARC_GOT22, 10,2,22,false,0,complain_overflow_dont, bfd_elf_generic_reloc,"R_SPARC_GOT22", false,0,0x003fffff,true),
103 HOWTO(R_SPARC_PC10, 0,2,10,true, 0,complain_overflow_dont, bfd_elf_generic_reloc,"R_SPARC_PC10", false,0,0x000003ff,true),
12662be4
ILT
104 HOWTO(R_SPARC_PC22, 10,2,22,true, 0,complain_overflow_bitfield,bfd_elf_generic_reloc,"R_SPARC_PC22", false,0,0x003fffff,true),
105 HOWTO(R_SPARC_WPLT30, 2,2,30,true, 0,complain_overflow_signed, bfd_elf_generic_reloc,"R_SPARC_WPLT30", false,0,0x3fffffff,true),
013dec1a
ILT
106 HOWTO(R_SPARC_COPY, 0,0,00,false,0,complain_overflow_dont, bfd_elf_generic_reloc,"R_SPARC_COPY", false,0,0x00000000,true),
107 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),
108 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),
109 HOWTO(R_SPARC_RELATIVE,0,0,00,false,0,complain_overflow_dont, bfd_elf_generic_reloc,"R_SPARC_RELATIVE",false,0,0x00000000,true),
110 HOWTO(R_SPARC_UA32, 0,0,00,false,0,complain_overflow_dont, bfd_elf_generic_reloc,"R_SPARC_UA32", false,0,0x00000000,true),
32090b8e
KR
111};
112
113struct elf_reloc_map {
114 unsigned char bfd_reloc_val;
115 unsigned char elf_reloc_val;
116};
117
118static CONST struct elf_reloc_map sparc_reloc_map[] =
119{
120 { BFD_RELOC_NONE, R_SPARC_NONE, },
121 { BFD_RELOC_16, R_SPARC_16, },
122 { BFD_RELOC_8, R_SPARC_8 },
123 { BFD_RELOC_8_PCREL, R_SPARC_DISP8 },
124 { BFD_RELOC_CTOR, R_SPARC_32 }, /* @@ Assumes 32 bits. */
125 { BFD_RELOC_32, R_SPARC_32 },
126 { BFD_RELOC_32_PCREL, R_SPARC_DISP32 },
127 { BFD_RELOC_HI22, R_SPARC_HI22 },
128 { BFD_RELOC_LO10, R_SPARC_LO10, },
129 { BFD_RELOC_32_PCREL_S2, R_SPARC_WDISP30 },
130 { BFD_RELOC_SPARC22, R_SPARC_22 },
131 { BFD_RELOC_SPARC13, R_SPARC_13 },
132 { BFD_RELOC_SPARC_GOT10, R_SPARC_GOT10 },
133 { BFD_RELOC_SPARC_GOT13, R_SPARC_GOT13 },
134 { BFD_RELOC_SPARC_GOT22, R_SPARC_GOT22 },
135 { BFD_RELOC_SPARC_PC10, R_SPARC_PC10 },
136 { BFD_RELOC_SPARC_PC22, R_SPARC_PC22 },
137 { BFD_RELOC_SPARC_WPLT30, R_SPARC_WPLT30 },
138 { BFD_RELOC_SPARC_COPY, R_SPARC_COPY },
139 { BFD_RELOC_SPARC_GLOB_DAT, R_SPARC_GLOB_DAT },
140 { BFD_RELOC_SPARC_JMP_SLOT, R_SPARC_JMP_SLOT },
141 { BFD_RELOC_SPARC_RELATIVE, R_SPARC_RELATIVE },
142 { BFD_RELOC_SPARC_WDISP22, R_SPARC_WDISP22 },
4fbc96ad 143/*{ BFD_RELOC_SPARC_UA32, R_SPARC_UA32 }, not used?? */
32090b8e
KR
144};
145
f786a73f 146static reloc_howto_type *
013dec1a
ILT
147bfd_elf32_bfd_reloc_type_lookup (abfd, code)
148 bfd *abfd;
149 bfd_reloc_code_real_type code;
32090b8e
KR
150{
151 int i;
152 for (i = 0; i < sizeof (sparc_reloc_map) / sizeof (struct elf_reloc_map); i++)
153 {
154 if (sparc_reloc_map[i].bfd_reloc_val == code)
155 return &elf_sparc_howto_table[(int) sparc_reloc_map[i].elf_reloc_val];
156 }
157 return 0;
158}
159
160static void
013dec1a
ILT
161elf_info_to_howto (abfd, cache_ptr, dst)
162 bfd *abfd;
163 arelent *cache_ptr;
164 Elf_Internal_Rela *dst;
32090b8e
KR
165{
166 BFD_ASSERT (ELF32_R_TYPE(dst->r_info) < (unsigned int) R_SPARC_max);
167 cache_ptr->howto = &elf_sparc_howto_table[ELF32_R_TYPE(dst->r_info)];
168}
169
013dec1a
ILT
170\f
171/* Functions for the SPARC ELF linker. */
172
173/* The name of the dynamic interpreter. This is put in the .interp
174 section. */
175
176#define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
177
178/* The nop opcode we use. */
179
180#define SPARC_NOP 0x01000000
181
182/* The size in bytes of an entry in the procedure linkage table. */
183
184#define PLT_ENTRY_SIZE 12
185
186/* The first four entries in a procedure linkage table are reserved,
187 and the initial contents are unimportant (we zero them out).
188 Subsequent entries look like this. See the SVR4 ABI SPARC
189 supplement to see how this works. */
190
191/* sethi %hi(.-.plt0),%g1. We fill in the address later. */
192#define PLT_ENTRY_WORD0 0x03000000
193/* b,a .plt0. We fill in the offset later. */
194#define PLT_ENTRY_WORD1 0x30800000
195/* nop. */
196#define PLT_ENTRY_WORD2 SPARC_NOP
197
4fbc96ad
KR
198/* Look through the relocs for a section during the first phase, and
199 allocate space in the global offset table or procedure linkage
200 table. */
201
202static boolean
203elf32_sparc_check_relocs (abfd, info, sec, relocs)
204 bfd *abfd;
205 struct bfd_link_info *info;
206 asection *sec;
207 const Elf_Internal_Rela *relocs;
208{
209 bfd *dynobj;
210 Elf_Internal_Shdr *symtab_hdr;
211 struct elf_link_hash_entry **sym_hashes;
212 bfd_vma *local_got_offsets;
213 const Elf_Internal_Rela *rel;
214 const Elf_Internal_Rela *rel_end;
215 asection *sgot;
216 asection *srelgot;
4fbc96ad
KR
217 asection *sreloc;
218
219 if (info->relocateable)
220 return true;
221
222 dynobj = elf_hash_table (info)->dynobj;
223 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
224 sym_hashes = elf_sym_hashes (abfd);
225 local_got_offsets = elf_local_got_offsets (abfd);
226
227 sgot = NULL;
228 srelgot = NULL;
4fbc96ad
KR
229 sreloc = NULL;
230
231 rel_end = relocs + sec->reloc_count;
232 for (rel = relocs; rel < rel_end; rel++)
233 {
234 long r_symndx;
235 struct elf_link_hash_entry *h;
236
237 r_symndx = ELF32_R_SYM (rel->r_info);
238 if (r_symndx < symtab_hdr->sh_info)
239 h = NULL;
240 else
241 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
242
4fbc96ad
KR
243 switch (ELF32_R_TYPE (rel->r_info))
244 {
245 case R_SPARC_GOT10:
246 case R_SPARC_GOT13:
247 case R_SPARC_GOT22:
248 /* This symbol requires a global offset table entry. */
249
12662be4
ILT
250 if (dynobj == NULL)
251 {
252 /* Create the .got section. */
253 elf_hash_table (info)->dynobj = dynobj = abfd;
ede4eed4 254 if (! _bfd_elf_create_got_section (dynobj, info))
12662be4
ILT
255 return false;
256 }
257
4fbc96ad
KR
258 if (sgot == NULL)
259 {
260 sgot = bfd_get_section_by_name (dynobj, ".got");
12662be4
ILT
261 BFD_ASSERT (sgot != NULL);
262 }
263
264 if (srelgot == NULL
265 && (h != NULL || info->shared))
266 {
4fbc96ad
KR
267 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
268 if (srelgot == NULL)
269 {
270 srelgot = bfd_make_section (dynobj, ".rela.got");
271 if (srelgot == NULL
272 || ! bfd_set_section_flags (dynobj, srelgot,
273 (SEC_ALLOC
274 | SEC_LOAD
275 | SEC_HAS_CONTENTS
276 | SEC_IN_MEMORY
12662be4
ILT
277 | SEC_READONLY))
278 || ! bfd_set_section_alignment (dynobj, srelgot, 2))
4fbc96ad
KR
279 return false;
280 }
4fbc96ad
KR
281 }
282
283 if (h != NULL)
284 {
285 if (h->got_offset != (bfd_vma) -1)
286 {
287 /* We have already allocated space in the .got. */
288 break;
289 }
290 h->got_offset = sgot->_raw_size;
12662be4
ILT
291
292 /* Make sure this symbol is output as a dynamic symbol. */
293 if (h->dynindx == -1)
294 {
295 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
296 return false;
297 }
298
299 srelgot->_raw_size += sizeof (Elf32_External_Rela);
4fbc96ad
KR
300 }
301 else
302 {
303 /* This is a global offset table entry for a local
304 symbol. */
305 if (local_got_offsets == NULL)
306 {
307 size_t size;
308 register int i;
309
310 size = symtab_hdr->sh_info * sizeof (bfd_vma);
311 local_got_offsets = (bfd_vma *) bfd_alloc (abfd, size);
312 if (local_got_offsets == NULL)
313 {
314 bfd_set_error (bfd_error_no_memory);
315 return false;
316 }
317 elf_local_got_offsets (abfd) = local_got_offsets;
318 for (i = 0; i < symtab_hdr->sh_info; i++)
319 local_got_offsets[i] = (bfd_vma) -1;
320 }
321 if (local_got_offsets[r_symndx] != (bfd_vma) -1)
322 {
323 /* We have already allocated space in the .got. */
324 break;
325 }
326 local_got_offsets[r_symndx] = sgot->_raw_size;
12662be4
ILT
327
328 if (info->shared)
329 {
330 /* If we are generating a shared object, we need to
331 output a R_SPARC_RELATIVE reloc so that the
332 dynamic linker can adjust this GOT entry. */
333 srelgot->_raw_size += sizeof (Elf32_External_Rela);
334 }
4fbc96ad
KR
335 }
336
337 sgot->_raw_size += 4;
4fbc96ad
KR
338
339 break;
340
341 case R_SPARC_WPLT30:
12662be4
ILT
342 /* This symbol requires a procedure linkage table entry. We
343 actually build the entry in adjust_dynamic_symbol,
344 because this might be a case of linking PIC code without
345 linking in any dynamic objects, in which case we don't
346 need to generate a procedure linkage table after all. */
4fbc96ad
KR
347
348 if (h == NULL)
349 {
350 /* It does not make sense to have a procedure linkage
351 table entry for a local symbol. */
352 bfd_set_error (bfd_error_bad_value);
353 return false;
354 }
355
12662be4
ILT
356 /* Make sure this symbol is output as a dynamic symbol. */
357 if (h->dynindx == -1)
4fbc96ad 358 {
12662be4
ILT
359 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
360 return false;
4fbc96ad
KR
361 }
362
12662be4 363 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
4fbc96ad
KR
364
365 break;
366
367 case R_SPARC_PC10:
368 case R_SPARC_PC22:
369 if (h != NULL
370 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
371 break;
372 /* Fall through. */
373 case R_SPARC_8:
374 case R_SPARC_16:
375 case R_SPARC_32:
376 case R_SPARC_DISP8:
377 case R_SPARC_DISP16:
378 case R_SPARC_DISP32:
379 case R_SPARC_WDISP30:
380 case R_SPARC_WDISP22:
381 case R_SPARC_HI22:
382 case R_SPARC_22:
383 case R_SPARC_13:
384 case R_SPARC_LO10:
385 case R_SPARC_UA32:
386 if (info->shared
387 && (sec->flags & SEC_ALLOC) != 0)
388 {
389 /* When creating a shared object, we must copy these
390 relocs into the output file. We create a reloc
391 section in dynobj and make room for the reloc. */
392 if (sreloc == NULL)
393 {
394 const char *name;
395
ede4eed4 396 name = (bfd_elf_string_from_elf_section
4fbc96ad
KR
397 (abfd,
398 elf_elfheader (abfd)->e_shstrndx,
399 elf_section_data (sec)->rel_hdr.sh_name));
400 if (name == NULL)
401 return false;
402
403 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
404 && strcmp (bfd_get_section_name (abfd, sec),
405 name + 5) == 0);
406
407 sreloc = bfd_get_section_by_name (dynobj, name);
408 if (sreloc == NULL)
409 {
410 sreloc = bfd_make_section (dynobj, name);
411 if (sreloc == NULL
412 || ! bfd_set_section_flags (dynobj, sreloc,
413 (SEC_ALLOC
414 | SEC_LOAD
415 | SEC_HAS_CONTENTS
416 | SEC_IN_MEMORY
417 | SEC_READONLY))
418 || ! bfd_set_section_alignment (dynobj, sreloc, 2))
419 return false;
420 }
421 }
422
423 sreloc->_raw_size += sizeof (Elf32_External_Rela);
424 }
425
426 break;
427
428 default:
429 break;
430 }
431 }
013dec1a
ILT
432
433 return true;
434}
435
436/* Adjust a symbol defined by a dynamic object and referenced by a
437 regular object. The current definition is in some section of the
438 dynamic object, but we're not including those sections. We have to
439 change the definition to something the rest of the link can
440 understand. */
441
442static boolean
443elf32_sparc_adjust_dynamic_symbol (info, h)
444 struct bfd_link_info *info;
445 struct elf_link_hash_entry *h;
446{
447 bfd *dynobj;
448 asection *s;
449 unsigned int power_of_two;
013dec1a
ILT
450
451 dynobj = elf_hash_table (info)->dynobj;
452
453 /* Make sure we know what is going on here. */
3004a68c
ILT
454 BFD_ASSERT (dynobj != NULL
455 && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
456 || ((h->elf_link_hash_flags
457 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
458 && (h->elf_link_hash_flags
459 & ELF_LINK_HASH_REF_REGULAR) != 0
460 && (h->elf_link_hash_flags
461 & ELF_LINK_HASH_DEF_REGULAR) == 0)));
013dec1a
ILT
462
463 /* If this is a function, put it in the procedure linkage table. We
464 will fill in the contents of the procedure linkage table later
465 (although we could actually do it here). */
12662be4
ILT
466 if (h->type == STT_FUNC
467 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
013dec1a 468 {
12662be4 469 if (! elf_hash_table (info)->dynamic_sections_created)
013dec1a 470 {
12662be4
ILT
471 /* This case can occur if we saw a WPLT30 reloc in an input
472 file, but none of the input files were dynamic objects.
473 In such a case, we don't actually need to build a
474 procedure linkage table, and we can just do a WDISP30
475 reloc instead. */
476 BFD_ASSERT ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0);
477 return true;
478 }
4fbc96ad 479
12662be4
ILT
480 s = bfd_get_section_by_name (dynobj, ".plt");
481 BFD_ASSERT (s != NULL);
013dec1a 482
12662be4
ILT
483 /* The first four entries in .plt are reserved. */
484 if (s->_raw_size == 0)
485 s->_raw_size = 4 * PLT_ENTRY_SIZE;
013dec1a 486
12662be4
ILT
487 /* The procedure linkage table has a maximum size. */
488 if (s->_raw_size >= 0x400000)
489 {
490 bfd_set_error (bfd_error_bad_value);
491 return false;
492 }
013dec1a 493
9b09a015
ILT
494 /* If this symbol is not defined in a regular file, and we are
495 not generating a shared library, then set the symbol to this
496 location in the .plt. This is required to make function
497 pointers compare as equal between the normal executable and
498 the shared library. */
499 if (! info->shared
500 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
14cac507
ILT
501 {
502 h->root.u.def.section = s;
503 h->root.u.def.value = s->_raw_size;
504 }
013dec1a 505
12662be4 506 h->plt_offset = s->_raw_size;
4fbc96ad 507
12662be4
ILT
508 /* Make room for this entry. */
509 s->_raw_size += PLT_ENTRY_SIZE;
510
511 /* We also need to make an entry in the .rela.plt section. */
512
513 s = bfd_get_section_by_name (dynobj, ".rela.plt");
514 BFD_ASSERT (s != NULL);
515 s->_raw_size += sizeof (Elf32_External_Rela);
013dec1a
ILT
516
517 return true;
518 }
519
520 /* If this is a weak symbol, and there is a real definition, the
521 processor independent code will have arranged for us to see the
522 real definition first, and we can just use the same value. */
523 if (h->weakdef != NULL)
524 {
f786a73f
ILT
525 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
526 || h->weakdef->root.type == bfd_link_hash_defweak);
013dec1a
ILT
527 h->root.u.def.section = h->weakdef->root.u.def.section;
528 h->root.u.def.value = h->weakdef->root.u.def.value;
013dec1a
ILT
529 return true;
530 }
531
532 /* This is a reference to a symbol defined by a dynamic object which
4fbc96ad
KR
533 is not a function. */
534
535 /* If we are creating a shared library, we must presume that the
536 only references to the symbol are via the global offset table.
537 For such cases we need not do anything here; the relocations will
538 be handled correctly by relocate_section. */
539 if (info->shared)
540 return true;
541
542 /* We must allocate the symbol in our .dynbss section, which will
543 become part of the .bss section of the executable. There will be
544 an entry for this symbol in the .dynsym section. The dynamic
545 object will contain position independent code, so all references
546 from the dynamic object to this symbol will go through the global
547 offset table. The dynamic linker will use the .dynsym entry to
548 determine the address it must put in the global offset table, so
549 both the dynamic object and the regular object will refer to the
550 same memory location for the variable. */
013dec1a
ILT
551
552 s = bfd_get_section_by_name (dynobj, ".dynbss");
553 BFD_ASSERT (s != NULL);
554
555 /* If the symbol is currently defined in the .bss section of the
556 dynamic object, then it is OK to simply initialize it to zero.
557 If the symbol is in some other section, we must generate a
558 R_SPARC_COPY reloc to tell the dynamic linker to copy the initial
559 value out of the dynamic object and into the runtime process
560 image. We need to remember the offset into the .rel.bss section
7c6da9ca 561 we are going to use. */
4fbc96ad 562 if ((h->root.u.def.section->flags & SEC_LOAD) != 0)
013dec1a
ILT
563 {
564 asection *srel;
565
566 srel = bfd_get_section_by_name (dynobj, ".rela.bss");
567 BFD_ASSERT (srel != NULL);
013dec1a 568 srel->_raw_size += sizeof (Elf32_External_Rela);
4fbc96ad 569 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
013dec1a
ILT
570 }
571
572 /* We need to figure out the alignment required for this symbol. I
573 have no idea how ELF linkers handle this. */
7c6da9ca
ILT
574 power_of_two = bfd_log2 (h->size);
575 if (power_of_two > 3)
576 power_of_two = 3;
013dec1a
ILT
577
578 /* Apply the required alignment. */
7c6da9ca
ILT
579 s->_raw_size = BFD_ALIGN (s->_raw_size,
580 (bfd_size_type) (1 << power_of_two));
013dec1a
ILT
581 if (power_of_two > bfd_get_section_alignment (dynobj, s))
582 {
583 if (! bfd_set_section_alignment (dynobj, s, power_of_two))
584 return false;
585 }
586
587 /* Define the symbol as being at this point in the section. */
588 h->root.u.def.section = s;
589 h->root.u.def.value = s->_raw_size;
590
591 /* Increment the section size to make room for the symbol. */
592 s->_raw_size += h->size;
593
594 return true;
595}
596
013dec1a
ILT
597/* Set the sizes of the dynamic sections. */
598
599static boolean
600elf32_sparc_size_dynamic_sections (output_bfd, info)
601 bfd *output_bfd;
602 struct bfd_link_info *info;
603{
604 bfd *dynobj;
605 asection *s;
4fbc96ad 606 boolean reltext;
f786a73f 607 boolean relplt;
013dec1a
ILT
608
609 dynobj = elf_hash_table (info)->dynobj;
610 BFD_ASSERT (dynobj != NULL);
611
12662be4 612 if (elf_hash_table (info)->dynamic_sections_created)
8af74670 613 {
12662be4
ILT
614 /* Set the contents of the .interp section to the interpreter. */
615 if (! info->shared)
616 {
617 s = bfd_get_section_by_name (dynobj, ".interp");
618 BFD_ASSERT (s != NULL);
619 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
620 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
621 }
622
623 /* Make space for the trailing nop in .plt. */
624 s = bfd_get_section_by_name (dynobj, ".plt");
8af74670 625 BFD_ASSERT (s != NULL);
12662be4
ILT
626 if (s->_raw_size > 0)
627 s->_raw_size += 4;
628 }
629 else
630 {
631 /* We may have created entries in the .rela.got section.
632 However, if we are not creating the dynamic sections, we will
633 not actually use these entries. Reset the size of .rela.got,
634 which will cause it to get stripped from the output file
635 below. */
636 s = bfd_get_section_by_name (dynobj, ".rela.got");
637 if (s != NULL)
638 s->_raw_size = 0;
8af74670 639 }
013dec1a 640
4fbc96ad
KR
641 /* The check_relocs and adjust_dynamic_symbol entry points have
642 determined the sizes of the various dynamic sections. Allocate
643 memory for them. */
644 reltext = false;
f786a73f 645 relplt = false;
4fbc96ad
KR
646 for (s = dynobj->sections; s != NULL; s = s->next)
647 {
648 const char *name;
12662be4 649 boolean strip;
4fbc96ad
KR
650
651 if ((s->flags & SEC_IN_MEMORY) == 0)
652 continue;
653
654 /* It's OK to base decisions on the section name, because none
655 of the dynobj section names depend upon the input files. */
656 name = bfd_get_section_name (dynobj, s);
657
12662be4
ILT
658 strip = false;
659
660 if (strncmp (name, ".rela", 5) == 0)
4fbc96ad 661 {
12662be4
ILT
662 if (s->_raw_size == 0)
663 {
664 /* If we don't need this section, strip it from the
665 output file. This is to handle .rela.bss and
666 .rel.plt. We must create it in
667 create_dynamic_sections, because it must be created
668 before the linker maps input sections to output
669 sections. The linker does that before
670 adjust_dynamic_symbol is called, and it is that
671 function which decides whether anything needs to go
672 into these sections. */
673 strip = true;
674 }
675 else
676 {
677 asection *target;
678
679 /* If this relocation section applies to a read only
680 section, then we probably need a DT_TEXTREL entry. */
681 target = bfd_get_section_by_name (output_bfd, name + 5);
682 if (target != NULL
683 && (target->flags & SEC_READONLY) != 0)
684 reltext = true;
685
f786a73f
ILT
686 if (strcmp (name, ".rela.plt") == 0)
687 relplt = true;
688
12662be4
ILT
689 /* We use the reloc_count field as a counter if we need
690 to copy relocs into the output file. */
691 s->reloc_count = 0;
692 }
4fbc96ad
KR
693 }
694 else if (strcmp (name, ".plt") != 0
695 && strcmp (name, ".got") != 0)
696 {
697 /* It's not one of our sections, so don't allocate space. */
698 continue;
699 }
12662be4
ILT
700
701 if (strip)
702 {
703 asection **spp;
704
705 for (spp = &s->output_section->owner->sections;
706 *spp != s->output_section;
707 spp = &(*spp)->next)
708 ;
709 *spp = s->output_section->next;
710 --s->output_section->owner->section_count;
711
712 continue;
713 }
714
4fbc96ad
KR
715 /* Allocate memory for the section contents. */
716 s->contents = (bfd_byte *) bfd_alloc (dynobj, s->_raw_size);
717 if (s->contents == NULL && s->_raw_size != 0)
718 {
719 bfd_set_error (bfd_error_no_memory);
720 return false;
721 }
722 }
013dec1a 723
12662be4 724 if (elf_hash_table (info)->dynamic_sections_created)
4fbc96ad 725 {
12662be4
ILT
726 /* Add some entries to the .dynamic section. We fill in the
727 values later, in elf32_sparc_finish_dynamic_sections, but we
728 must add the entries now so that we get the correct size for
729 the .dynamic section. The DT_DEBUG entry is filled in by the
730 dynamic linker and used by the debugger. */
731 if (! info->shared)
732 {
733 if (! bfd_elf32_add_dynamic_entry (info, DT_DEBUG, 0))
734 return false;
735 }
013dec1a 736
f786a73f
ILT
737 if (! bfd_elf32_add_dynamic_entry (info, DT_PLTGOT, 0))
738 return false;
739
740 if (relplt)
741 {
742 if (! bfd_elf32_add_dynamic_entry (info, DT_PLTRELSZ, 0)
743 || ! bfd_elf32_add_dynamic_entry (info, DT_PLTREL, DT_RELA)
744 || ! bfd_elf32_add_dynamic_entry (info, DT_JMPREL, 0))
745 return false;
746 }
747
748 if (! bfd_elf32_add_dynamic_entry (info, DT_RELA, 0)
12662be4
ILT
749 || ! bfd_elf32_add_dynamic_entry (info, DT_RELASZ, 0)
750 || ! bfd_elf32_add_dynamic_entry (info, DT_RELAENT,
751 sizeof (Elf32_External_Rela)))
4fbc96ad 752 return false;
12662be4
ILT
753
754 if (reltext)
755 {
756 if (! bfd_elf32_add_dynamic_entry (info, DT_TEXTREL, 0))
757 return false;
758 }
4fbc96ad
KR
759 }
760
14cac507
ILT
761 /* If we are generating a shared library, we generate a section
762 symbol for each output section. These are local symbols, which
763 means that they must come first in the dynamic symbol table.
764 That means we must increment the dynamic symbol index of every
765 other dynamic symbol. */
766 if (info->shared)
767 {
768 int c, i;
769
770 c = bfd_count_sections (output_bfd);
771 elf_link_hash_traverse (elf_hash_table (info),
772 elf32_sparc_adjust_dynindx,
773 (PTR) &c);
774 elf_hash_table (info)->dynsymcount += c;
775
776 for (i = 1, s = output_bfd->sections; s != NULL; s = s->next, i++)
777 {
778 elf_section_data (s)->dynindx = i;
779 /* These symbols will have no names, so we don't need to
780 fiddle with dynstr_index. */
781 }
782 }
783
784 return true;
785}
786
787/* Increment the index of a dynamic symbol by a given amount. Called
788 via elf_link_hash_traverse. */
789
790static boolean
791elf32_sparc_adjust_dynindx (h, cparg)
792 struct elf_link_hash_entry *h;
793 PTR cparg;
794{
795 int *cp = (int *) cparg;
796
797 if (h->dynindx != -1)
798 h->dynindx += *cp;
013dec1a
ILT
799 return true;
800}
801
802/* Relocate a SPARC ELF section. */
803
804static boolean
805elf32_sparc_relocate_section (output_bfd, info, input_bfd, input_section,
12662be4 806 contents, relocs, local_syms, local_sections)
013dec1a
ILT
807 bfd *output_bfd;
808 struct bfd_link_info *info;
809 bfd *input_bfd;
810 asection *input_section;
811 bfd_byte *contents;
812 Elf_Internal_Rela *relocs;
813 Elf_Internal_Sym *local_syms;
814 asection **local_sections;
815{
4fbc96ad 816 bfd *dynobj;
013dec1a
ILT
817 Elf_Internal_Shdr *symtab_hdr;
818 struct elf_link_hash_entry **sym_hashes;
4fbc96ad
KR
819 bfd_vma *local_got_offsets;
820 asection *sgot;
821 asection *splt;
822 asection *sreloc;
013dec1a
ILT
823 Elf_Internal_Rela *rel;
824 Elf_Internal_Rela *relend;
825
4fbc96ad 826 dynobj = elf_hash_table (info)->dynobj;
013dec1a
ILT
827 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
828 sym_hashes = elf_sym_hashes (input_bfd);
4fbc96ad
KR
829 local_got_offsets = elf_local_got_offsets (input_bfd);
830
831 sgot = NULL;
832 splt = NULL;
833 sreloc = NULL;
013dec1a
ILT
834
835 rel = relocs;
836 relend = relocs + input_section->reloc_count;
837 for (; rel < relend; rel++)
838 {
839 int r_type;
82b1edf7 840 reloc_howto_type *howto;
013dec1a
ILT
841 long r_symndx;
842 struct elf_link_hash_entry *h;
843 Elf_Internal_Sym *sym;
844 asection *sec;
845 bfd_vma relocation;
846 bfd_reloc_status_type r;
847
848 r_type = ELF32_R_TYPE (rel->r_info);
849 if (r_type < 0 || r_type >= (int) R_SPARC_max)
850 {
851 bfd_set_error (bfd_error_bad_value);
852 return false;
853 }
854 howto = elf_sparc_howto_table + r_type;
855
856 r_symndx = ELF32_R_SYM (rel->r_info);
857
858 if (info->relocateable)
859 {
860 /* This is a relocateable link. We don't have to change
861 anything, unless the reloc is against a section symbol,
862 in which case we have to adjust according to where the
863 section symbol winds up in the output section. */
864 if (r_symndx < symtab_hdr->sh_info)
865 {
866 sym = local_syms + r_symndx;
867 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
868 {
869 sec = local_sections[r_symndx];
870 rel->r_addend += sec->output_offset + sym->st_value;
871 }
872 }
873
874 continue;
875 }
876
877 /* This is a final link. */
878 h = NULL;
879 sym = NULL;
880 sec = NULL;
881 if (r_symndx < symtab_hdr->sh_info)
882 {
883 sym = local_syms + r_symndx;
884 sec = local_sections[r_symndx];
885 relocation = (sec->output_section->vma
886 + sec->output_offset
887 + sym->st_value);
888 }
889 else
890 {
4fbc96ad 891 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
f786a73f
ILT
892 if (h->root.type == bfd_link_hash_defined
893 || h->root.type == bfd_link_hash_defweak)
013dec1a
ILT
894 {
895 sec = h->root.u.def.section;
9b09a015
ILT
896 if ((r_type == R_SPARC_WPLT30
897 && h->plt_offset != (bfd_vma) -1)
898 || ((r_type == R_SPARC_GOT10
899 || r_type == R_SPARC_GOT13
900 || r_type == R_SPARC_GOT22)
901 && elf_hash_table (info)->dynamic_sections_created)
902 || (info->shared
903 && (input_section->flags & SEC_ALLOC) != 0
904 && (r_type == R_SPARC_8
905 || r_type == R_SPARC_16
906 || r_type == R_SPARC_32
907 || r_type == R_SPARC_DISP8
908 || r_type == R_SPARC_DISP16
909 || r_type == R_SPARC_DISP32
910 || r_type == R_SPARC_WDISP30
911 || r_type == R_SPARC_WDISP22
912 || r_type == R_SPARC_HI22
913 || r_type == R_SPARC_22
914 || r_type == R_SPARC_13
915 || r_type == R_SPARC_LO10
916 || r_type == R_SPARC_UA32
917 || ((r_type == R_SPARC_PC10
918 || r_type == R_SPARC_PC22)
919 && strcmp (h->root.root.string,
920 "_GLOBAL_OFFSET_TABLE_") != 0))))
921 {
922 /* In these cases, we don't need the relocation
923 value. We check specially because in some
924 obscure cases sec->output_section will be NULL. */
925 relocation = 0;
926 }
927 else
928 relocation = (h->root.u.def.value
929 + sec->output_section->vma
930 + sec->output_offset);
013dec1a 931 }
f786a73f 932 else if (h->root.type == bfd_link_hash_undefweak)
013dec1a 933 relocation = 0;
4fbc96ad
KR
934 else if (info->shared)
935 relocation = 0;
013dec1a
ILT
936 else
937 {
938 if (! ((*info->callbacks->undefined_symbol)
939 (info, h->root.root.string, input_bfd,
940 input_section, rel->r_offset)))
941 return false;
942 relocation = 0;
943 }
944 }
945
4fbc96ad
KR
946 switch (r_type)
947 {
948 case R_SPARC_GOT10:
949 case R_SPARC_GOT13:
950 case R_SPARC_GOT22:
951 /* Relocation is to the entry for this symbol in the global
952 offset table. */
953 if (sgot == NULL)
954 {
955 sgot = bfd_get_section_by_name (dynobj, ".got");
956 BFD_ASSERT (sgot != NULL);
957 }
958
959 if (h != NULL)
960 {
12662be4
ILT
961 bfd_vma off;
962
963 off = h->got_offset;
964 BFD_ASSERT (off != (bfd_vma) -1);
965
966 if (! elf_hash_table (info)->dynamic_sections_created)
967 {
968 /* This is actually a static link. We must
969 initialize this entry in the global offset table.
970 Since the offset must always be a multiple of 4,
971 we use the least significant bit to record
972 whether we have initialized it already.
973
974 When doing a dynamic link, we create a .rela.got
975 relocation entry to initialize the value. This
976 is done in the finish_dynamic_symbol routine. */
977 if ((off & 1) != 0)
978 off &= ~1;
979 else
980 {
981 bfd_put_32 (output_bfd, relocation,
982 sgot->contents + off);
983 h->got_offset |= 1;
984 }
985 }
986
987 relocation = sgot->output_offset + off;
4fbc96ad
KR
988 }
989 else
990 {
991 bfd_vma off;
992
993 BFD_ASSERT (local_got_offsets != NULL
994 && local_got_offsets[r_symndx] != (bfd_vma) -1);
995
996 off = local_got_offsets[r_symndx];
997
998 /* The offset must always be a multiple of 4. We use
12662be4
ILT
999 the least significant bit to record whether we have
1000 already processed this entry. */
4fbc96ad
KR
1001 if ((off & 1) != 0)
1002 off &= ~1;
1003 else
1004 {
4fbc96ad
KR
1005 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
1006
12662be4
ILT
1007 if (info->shared)
1008 {
1009 asection *srelgot;
1010 Elf_Internal_Rela outrel;
1011
1012 /* We need to generate a R_SPARC_RELATIVE reloc
1013 for the dynamic linker. */
1014 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
1015 BFD_ASSERT (srelgot != NULL);
1016
1017 outrel.r_offset = (sgot->output_section->vma
1018 + sgot->output_offset
1019 + off);
1020 outrel.r_info = ELF32_R_INFO (0, R_SPARC_RELATIVE);
1021 outrel.r_addend = 0;
1022 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1023 (((Elf32_External_Rela *)
1024 srelgot->contents)
1025 + srelgot->reloc_count));
1026 ++srelgot->reloc_count;
1027 }
4fbc96ad
KR
1028
1029 local_got_offsets[r_symndx] |= 1;
1030 }
1031
1032 relocation = sgot->output_offset + off;
1033 }
1034
1035 break;
1036
1037 case R_SPARC_WPLT30:
1038 /* Relocation is to the entry for this symbol in the
1039 procedure linkage table. */
12662be4
ILT
1040 BFD_ASSERT (h != NULL);
1041
1042 if (h->plt_offset == (bfd_vma) -1)
1043 {
1044 /* We didn't make a PLT entry for this symbol. This
1045 happens when statically linking PIC code. */
1046 break;
1047 }
1048
4fbc96ad
KR
1049 if (splt == NULL)
1050 {
1051 splt = bfd_get_section_by_name (dynobj, ".plt");
1052 BFD_ASSERT (splt != NULL);
1053 }
1054
4fbc96ad
KR
1055 relocation = (splt->output_section->vma
1056 + splt->output_offset
1057 + h->plt_offset);
1058 break;
1059
1060 case R_SPARC_PC10:
1061 case R_SPARC_PC22:
1062 if (h != NULL
1063 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
1064 break;
1065 /* Fall through. */
1066 case R_SPARC_8:
1067 case R_SPARC_16:
1068 case R_SPARC_32:
1069 case R_SPARC_DISP8:
1070 case R_SPARC_DISP16:
1071 case R_SPARC_DISP32:
1072 case R_SPARC_WDISP30:
1073 case R_SPARC_WDISP22:
1074 case R_SPARC_HI22:
1075 case R_SPARC_22:
1076 case R_SPARC_13:
1077 case R_SPARC_LO10:
1078 case R_SPARC_UA32:
1079 if (info->shared
1080 && (input_section->flags & SEC_ALLOC) != 0)
1081 {
1082 Elf_Internal_Rela outrel;
1083
1084 /* When generating a shared object, these relocations
1085 are copied into the output file to be resolved at run
1086 time. */
1087
1088 if (sreloc == NULL)
1089 {
1090 const char *name;
1091
ede4eed4 1092 name = (bfd_elf_string_from_elf_section
4fbc96ad
KR
1093 (input_bfd,
1094 elf_elfheader (input_bfd)->e_shstrndx,
1095 elf_section_data (input_section)->rel_hdr.sh_name));
1096 if (name == NULL)
1097 return false;
1098
1099 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
1100 && strcmp (bfd_get_section_name (input_bfd,
1101 input_section),
1102 name + 5) == 0);
1103
1104 sreloc = bfd_get_section_by_name (dynobj, name);
1105 BFD_ASSERT (sreloc != NULL);
1106 }
1107
1108 outrel.r_offset = (rel->r_offset
1109 + input_section->output_section->vma
1110 + input_section->output_offset);
1111 if (h != NULL)
1112 {
12662be4 1113 BFD_ASSERT (h->dynindx != -1);
4fbc96ad 1114 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
14cac507 1115 outrel.r_addend = rel->r_addend;
4fbc96ad
KR
1116 }
1117 else
1118 {
14cac507 1119 if (r_type == R_SPARC_32)
4fbc96ad 1120 {
14cac507
ILT
1121 outrel.r_info = ELF32_R_INFO (0, R_SPARC_RELATIVE);
1122 outrel.r_addend = relocation + rel->r_addend;
4fbc96ad
KR
1123 }
1124 else
1125 {
14cac507
ILT
1126 long indx;
1127
1128 sym = local_syms + r_symndx;
1129
1130 BFD_ASSERT (ELF_ST_TYPE (sym->st_info) == STT_SECTION);
1131
1132 sec = local_sections[r_symndx];
1133 if (sec != NULL && bfd_is_abs_section (sec))
1134 indx = 0;
1135 else if (sec == NULL || sec->owner == NULL)
1136 {
1137 bfd_set_error (bfd_error_bad_value);
1138 return false;
1139 }
1140 else
1141 {
1142 asection *osec;
1143
1144 osec = sec->output_section;
1145 indx = elf_section_data (osec)->dynindx;
1146 if (indx == 0)
1147 abort ();
1148 }
1149
1150 outrel.r_info = ELF32_R_INFO (indx, r_type);
1151 outrel.r_addend = relocation + rel->r_addend;
4fbc96ad 1152 }
4fbc96ad
KR
1153 }
1154
1155 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1156 (((Elf32_External_Rela *)
1157 sreloc->contents)
1158 + sreloc->reloc_count));
1159 ++sreloc->reloc_count;
1160
1161 /* This reloc will be computed at runtime, so there's no
1162 need to do anything now. */
1163 continue;
1164 }
1165
1166 default:
1167 break;
1168 }
1169
013dec1a
ILT
1170 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1171 contents, rel->r_offset,
1172 relocation, rel->r_addend);
1173
1174 if (r != bfd_reloc_ok)
1175 {
1176 switch (r)
1177 {
1178 default:
1179 case bfd_reloc_outofrange:
1180 abort ();
1181 case bfd_reloc_overflow:
1182 {
1183 const char *name;
1184
1185 if (h != NULL)
1186 name = h->root.root.string;
1187 else
1188 {
ede4eed4
KR
1189 name = bfd_elf_string_from_elf_section (input_bfd,
1190 symtab_hdr->sh_link,
1191 sym->st_name);
013dec1a
ILT
1192 if (name == NULL)
1193 return false;
1194 if (*name == '\0')
1195 name = bfd_section_name (input_bfd, sec);
1196 }
1197 if (! ((*info->callbacks->reloc_overflow)
1198 (info, name, howto->name, (bfd_vma) 0,
1199 input_bfd, input_section, rel->r_offset)))
1200 return false;
1201 }
1202 break;
1203 }
1204 }
1205 }
1206
1207 return true;
1208}
1209
1210/* Finish up dynamic symbol handling. We set the contents of various
1211 dynamic sections here. */
1212
1213static boolean
1214elf32_sparc_finish_dynamic_symbol (output_bfd, info, h, sym)
1215 bfd *output_bfd;
1216 struct bfd_link_info *info;
1217 struct elf_link_hash_entry *h;
1218 Elf_Internal_Sym *sym;
1219{
4fbc96ad 1220 bfd *dynobj;
013dec1a 1221
4fbc96ad 1222 dynobj = elf_hash_table (info)->dynobj;
013dec1a 1223
4fbc96ad 1224 if (h->plt_offset != (bfd_vma) -1)
013dec1a
ILT
1225 {
1226 asection *splt;
1227 asection *srela;
1228 Elf_Internal_Rela rela;
1229
4fbc96ad
KR
1230 /* This symbol has an entry in the procedure linkage table. Set
1231 it up. */
1232
1233 BFD_ASSERT (h->dynindx != -1);
1234
1235 splt = bfd_get_section_by_name (dynobj, ".plt");
1236 srela = bfd_get_section_by_name (dynobj, ".rela.plt");
1237 BFD_ASSERT (splt != NULL && srela != NULL);
013dec1a
ILT
1238
1239 /* Fill in the entry in the procedure linkage table. */
1240 bfd_put_32 (output_bfd,
4fbc96ad
KR
1241 PLT_ENTRY_WORD0 + h->plt_offset,
1242 splt->contents + h->plt_offset);
013dec1a
ILT
1243 bfd_put_32 (output_bfd,
1244 (PLT_ENTRY_WORD1
4fbc96ad
KR
1245 + (((- (h->plt_offset + 4)) >> 2) & 0x3fffff)),
1246 splt->contents + h->plt_offset + 4);
013dec1a 1247 bfd_put_32 (output_bfd, PLT_ENTRY_WORD2,
4fbc96ad 1248 splt->contents + h->plt_offset + 8);
013dec1a
ILT
1249
1250 /* Fill in the entry in the .rela.plt section. */
1251 rela.r_offset = (splt->output_section->vma
1252 + splt->output_offset
4fbc96ad 1253 + h->plt_offset);
013dec1a
ILT
1254 rela.r_info = ELF32_R_INFO (h->dynindx, R_SPARC_JMP_SLOT);
1255 rela.r_addend = 0;
1256 bfd_elf32_swap_reloca_out (output_bfd, &rela,
1257 ((Elf32_External_Rela *) srela->contents
4fbc96ad
KR
1258 + h->plt_offset / PLT_ENTRY_SIZE - 4));
1259
1260 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1261 {
1262 /* Mark the symbol as undefined, rather than as defined in
1263 the .plt section. Leave the value alone. */
1264 sym->st_shndx = SHN_UNDEF;
1265 }
1266 }
1267
1268 if (h->got_offset != (bfd_vma) -1)
1269 {
1270 asection *sgot;
1271 asection *srela;
1272 Elf_Internal_Rela rela;
1273
1274 /* This symbol has an entry in the global offset table. Set it
1275 up. */
013dec1a 1276
4fbc96ad
KR
1277 BFD_ASSERT (h->dynindx != -1);
1278
1279 sgot = bfd_get_section_by_name (dynobj, ".got");
1280 srela = bfd_get_section_by_name (dynobj, ".rela.got");
1281 BFD_ASSERT (sgot != NULL && srela != NULL);
1282
1283 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got_offset);
1284
1285 rela.r_offset = (sgot->output_section->vma
1286 + sgot->output_offset
1287 + h->got_offset);
1288 rela.r_info = ELF32_R_INFO (h->dynindx, R_SPARC_GLOB_DAT);
1289 rela.r_addend = 0;
1290 bfd_elf32_swap_reloca_out (output_bfd, &rela,
1291 ((Elf32_External_Rela *) srela->contents
1292 + srela->reloc_count));
1293 ++srela->reloc_count;
013dec1a 1294 }
4fbc96ad
KR
1295
1296 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
013dec1a 1297 {
4fbc96ad
KR
1298 asection *s;
1299 Elf_Internal_Rela rela;
013dec1a 1300
4fbc96ad 1301 /* This symbols needs a copy reloc. Set it up. */
013dec1a 1302
4fbc96ad
KR
1303 BFD_ASSERT (h->dynindx != -1);
1304
1305 s = bfd_get_section_by_name (h->root.u.def.section->owner,
1306 ".rela.bss");
1307 BFD_ASSERT (s != NULL);
1308
1309 rela.r_offset = (h->root.u.def.value
1310 + h->root.u.def.section->output_section->vma
1311 + h->root.u.def.section->output_offset);
1312 rela.r_info = ELF32_R_INFO (h->dynindx, R_SPARC_COPY);
1313 rela.r_addend = 0;
1314 bfd_elf32_swap_reloca_out (output_bfd, &rela,
1315 ((Elf32_External_Rela *) s->contents
1316 + s->reloc_count));
1317 ++s->reloc_count;
013dec1a
ILT
1318 }
1319
4fbc96ad
KR
1320 /* Mark some specially defined symbols as absolute. */
1321 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
1322 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
1323 || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0)
1324 sym->st_shndx = SHN_ABS;
1325
013dec1a
ILT
1326 return true;
1327}
1328
1329/* Finish up the dynamic sections. */
1330
1331static boolean
1332elf32_sparc_finish_dynamic_sections (output_bfd, info)
1333 bfd *output_bfd;
1334 struct bfd_link_info *info;
1335{
4fbc96ad 1336 bfd *dynobj;
013dec1a 1337 asection *sdyn;
12662be4 1338 asection *sgot;
013dec1a 1339
4fbc96ad
KR
1340 dynobj = elf_hash_table (info)->dynobj;
1341
4fbc96ad 1342 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
013dec1a 1343
12662be4 1344 if (elf_hash_table (info)->dynamic_sections_created)
013dec1a 1345 {
12662be4
ILT
1346 asection *splt;
1347 Elf32_External_Dyn *dyncon, *dynconend;
013dec1a 1348
12662be4
ILT
1349 splt = bfd_get_section_by_name (dynobj, ".plt");
1350 BFD_ASSERT (splt != NULL && sdyn != NULL);
013dec1a 1351
12662be4
ILT
1352 dyncon = (Elf32_External_Dyn *) sdyn->contents;
1353 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
1354 for (; dyncon < dynconend; dyncon++)
013dec1a 1355 {
12662be4
ILT
1356 Elf_Internal_Dyn dyn;
1357 const char *name;
1358 boolean size;
013dec1a 1359
12662be4 1360 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
013dec1a 1361
12662be4
ILT
1362 switch (dyn.d_tag)
1363 {
1364 case DT_PLTGOT: name = ".plt"; size = false; break;
1365 case DT_PLTRELSZ: name = ".rela.plt"; size = true; break;
1366 case DT_JMPREL: name = ".rela.plt"; size = false; break;
1367 default: name = NULL; size = false; break;
1368 }
1369
1370 if (name != NULL)
013dec1a 1371 {
12662be4
ILT
1372 asection *s;
1373
1374 s = bfd_get_section_by_name (output_bfd, name);
14cac507
ILT
1375 if (s == NULL)
1376 dyn.d_un.d_val = 0;
013dec1a 1377 else
12662be4 1378 {
14cac507
ILT
1379 if (! size)
1380 dyn.d_un.d_ptr = s->vma;
12662be4 1381 else
14cac507
ILT
1382 {
1383 if (s->_cooked_size != 0)
1384 dyn.d_un.d_val = s->_cooked_size;
1385 else
1386 dyn.d_un.d_val = s->_raw_size;
1387 }
12662be4
ILT
1388 }
1389 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
013dec1a 1390 }
013dec1a 1391 }
013dec1a 1392
12662be4
ILT
1393 /* Clear the first four entries in the procedure linkage table,
1394 and put a nop in the last four bytes. */
1395 if (splt->_raw_size > 0)
1396 {
1397 memset (splt->contents, 0, 4 * PLT_ENTRY_SIZE);
1398 bfd_put_32 (output_bfd, SPARC_NOP,
1399 splt->contents + splt->_raw_size - 4);
1400 }
1401
1402 elf_section_data (splt->output_section)->this_hdr.sh_entsize =
1403 PLT_ENTRY_SIZE;
013dec1a
ILT
1404 }
1405
1406 /* Set the first entry in the global offset table to the address of
1407 the dynamic section. */
12662be4
ILT
1408 sgot = bfd_get_section_by_name (dynobj, ".got");
1409 BFD_ASSERT (sgot != NULL);
013dec1a 1410 if (sgot->_raw_size > 0)
12662be4
ILT
1411 {
1412 if (sdyn == NULL)
1413 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
1414 else
1415 bfd_put_32 (output_bfd,
1416 sdyn->output_section->vma + sdyn->output_offset,
1417 sgot->contents);
1418 }
013dec1a
ILT
1419
1420 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
013dec1a 1421
14cac507
ILT
1422 if (info->shared)
1423 {
1424 asection *sdynsym;
1425 asection *s;
1426 Elf_Internal_Sym sym;
1427
1428 /* Set up the section symbols for the output sections. */
1429
1430 sdynsym = bfd_get_section_by_name (dynobj, ".dynsym");
1431 BFD_ASSERT (sdynsym != NULL);
1432
1433 sym.st_size = 0;
1434 sym.st_name = 0;
1435 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
1436 sym.st_other = 0;
1437
1438 for (s = output_bfd->sections; s != NULL; s = s->next)
1439 {
1440 int indx;
1441
1442 sym.st_value = s->vma;
1443
1444 indx = elf_section_data (s)->this_idx;
1445 BFD_ASSERT (indx > 0);
1446 sym.st_shndx = indx;
1447
1448 bfd_elf32_swap_symbol_out (output_bfd, &sym,
ede4eed4 1449(char*) ((Elf32_External_Sym *) sdynsym->contents
14cac507
ILT
1450 + elf_section_data (s)->dynindx));
1451 }
1452
1453 /* Set the sh_info field of the output .dynsym section to the
1454 index of the first global symbol. */
1455 elf_section_data (sdynsym->output_section)->this_hdr.sh_info =
1456 bfd_count_sections (output_bfd) + 1;
1457 }
1458
013dec1a
ILT
1459 return true;
1460}
1461
32090b8e
KR
1462#define TARGET_BIG_SYM bfd_elf32_sparc_vec
1463#define TARGET_BIG_NAME "elf32-sparc"
1464#define ELF_ARCH bfd_arch_sparc
013dec1a
ILT
1465#define ELF_MACHINE_CODE EM_SPARC
1466#define ELF_MAXPAGESIZE 0x10000
1467#define elf_backend_create_dynamic_sections \
ede4eed4 1468 _bfd_elf_create_dynamic_sections
4fbc96ad 1469#define elf_backend_check_relocs elf32_sparc_check_relocs
013dec1a
ILT
1470#define elf_backend_adjust_dynamic_symbol \
1471 elf32_sparc_adjust_dynamic_symbol
1472#define elf_backend_size_dynamic_sections \
1473 elf32_sparc_size_dynamic_sections
1474#define elf_backend_relocate_section elf32_sparc_relocate_section
1475#define elf_backend_finish_dynamic_symbol \
1476 elf32_sparc_finish_dynamic_symbol
1477#define elf_backend_finish_dynamic_sections \
1478 elf32_sparc_finish_dynamic_sections
ede4eed4
KR
1479#define elf_backend_want_got_plt 0
1480#define elf_backend_plt_readonly 1
1481#define elf_backend_want_plt_sym 1
32090b8e
KR
1482
1483#include "elf32-target.h"
This page took 0.153526 seconds and 4 git commands to generate.