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