Thu Oct 13 15:51:20 1994 Jason Merrill (jason@phydeaux.cygnus.com)
[deliverable/binutils-gdb.git] / bfd / elf32-sparc.c
1 /* SPARC-specific support for 32-bit ELF
2 Copyright 1993 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., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20 #include "bfd.h"
21 #include "sysdep.h"
22 #include "bfdlink.h"
23 #include "libbfd.h"
24 #include "libelf.h"
25
26 static CONST struct reloc_howto_struct *bfd_elf32_bfd_reloc_type_lookup
27 PARAMS ((bfd *, bfd_reloc_code_real_type));
28 static void elf_info_to_howto
29 PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
30 static boolean elf32_sparc_create_dynamic_sections
31 PARAMS ((bfd *, struct bfd_link_info *));
32 static boolean elf32_sparc_check_relocs
33 PARAMS ((bfd *, struct bfd_link_info *, asection *,
34 const Elf_Internal_Rela *));
35 static boolean elf32_sparc_adjust_dynamic_symbol
36 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
37 static boolean elf32_sparc_size_dynamic_sections
38 PARAMS ((bfd *, struct bfd_link_info *));
39 static boolean elf32_sparc_relocate_section
40 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
41 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **, char *));
42 static boolean elf32_sparc_finish_dynamic_symbol
43 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
44 Elf_Internal_Sym *));
45 static boolean elf32_sparc_finish_dynamic_sections
46 PARAMS ((bfd *, struct bfd_link_info *));
47
48 enum 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
67 static 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
85 static reloc_howto_type elf_sparc_howto_table[] =
86 {
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),
100 HOWTO(R_SPARC_GOT10, 0,2,10,false,0,complain_overflow_dont, bfd_elf_generic_reloc,"R_SPARC_GOT10", false,0,0x000003ff,true),
101 HOWTO(R_SPARC_GOT13, 0,2,13,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,"R_SPARC_GOT13", false,0,0x00001fff,true),
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),
104 HOWTO(R_SPARC_PC22, 0,2,22,true, 0,complain_overflow_bitfield,bfd_elf_generic_reloc,"R_SPARC_PC22", false,0,0x003fffff,true),
105 HOWTO(R_SPARC_WPLT30, 0,0,00,true, 0,complain_overflow_dont, bfd_elf_generic_reloc,"R_SPARC_WPLT30", false,0,0x00000000,true),
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),
111 };
112
113 struct elf_reloc_map {
114 unsigned char bfd_reloc_val;
115 unsigned char elf_reloc_val;
116 };
117
118 static 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 },
143 /*{ BFD_RELOC_SPARC_UA32, R_SPARC_UA32 }, not used?? */
144 };
145
146 static CONST struct reloc_howto_struct *
147 bfd_elf32_bfd_reloc_type_lookup (abfd, code)
148 bfd *abfd;
149 bfd_reloc_code_real_type code;
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
160 static void
161 elf_info_to_howto (abfd, cache_ptr, dst)
162 bfd *abfd;
163 arelent *cache_ptr;
164 Elf_Internal_Rela *dst;
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
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
198 /* Create dynamic sections when linking against a dynamic object. */
199
200 static boolean
201 elf32_sparc_create_dynamic_sections (abfd, info)
202 bfd *abfd;
203 struct bfd_link_info *info;
204 {
205 flagword flags;
206 register asection *s;
207 struct elf_link_hash_entry *h;
208
209 /* We need to create .plt, .rela.plt, .got, .dynbss, and .rela.bss
210 sections. */
211
212 flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY;
213
214 s = bfd_make_section (abfd, ".plt");
215 if (s == NULL
216 || ! bfd_set_section_flags (abfd, s, flags | SEC_CODE)
217 || ! bfd_set_section_alignment (abfd, s, 2))
218 return false;
219
220 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
221 .plt section. */
222 h = NULL;
223 if (! (_bfd_generic_link_add_one_symbol
224 (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s, (bfd_vma) 0,
225 (const char *) NULL, false, get_elf_backend_data (abfd)->collect,
226 (struct bfd_link_hash_entry **) &h)))
227 return false;
228 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
229 h->type = STT_OBJECT;
230
231 if (info->shared
232 && ! bfd_elf32_link_record_dynamic_symbol (info, h))
233 return false;
234
235 /* The first four entries in .plt are reserved. */
236 s->_raw_size = 4 * PLT_ENTRY_SIZE;
237
238 s = bfd_make_section (abfd, ".rela.plt");
239 if (s == NULL
240 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
241 || ! bfd_set_section_alignment (abfd, s, 2))
242 return false;
243
244 s = bfd_make_section (abfd, ".got");
245 if (s == NULL
246 || ! bfd_set_section_flags (abfd, s, flags)
247 || ! bfd_set_section_alignment (abfd, s, 2))
248 return false;
249
250 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
251 section. We don't do this in the linker script because we don't
252 want to define the symbol if we are not creating a global offset
253 table. FIXME: The Solaris linker puts _GLOBAL_OFFSET_TABLE_ at
254 the start of the .got section, but when using the small PIC model
255 the .got is accessed using a signed 13 bit offset. Shouldn't
256 _GLOBAL_OFFSET_TABLE_ be located at .got + 4096? */
257 h = NULL;
258 if (! (_bfd_generic_link_add_one_symbol
259 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s, (bfd_vma) 0,
260 (const char *) NULL, false, get_elf_backend_data (abfd)->collect,
261 (struct bfd_link_hash_entry **) &h)))
262 return false;
263 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
264 h->type = STT_OBJECT;
265
266 if (info->shared
267 && ! bfd_elf32_link_record_dynamic_symbol (info, h))
268 return false;
269
270 /* The first global offset table entry is reserved. */
271 s->_raw_size += 4;
272
273 /* The .dynbss section is a place to put symbols which are defined
274 by dynamic objects, are referenced by regular objects, and are
275 not functions. We must allocate space for them in the process
276 image and use a R_SPARC_COPY reloc to tell the dynamic linker to
277 initialize them at run time. The linker script puts the .dynbss
278 section into the .bss section of the final image. */
279 s = bfd_make_section (abfd, ".dynbss");
280 if (s == NULL
281 || ! bfd_set_section_flags (abfd, s, SEC_ALLOC))
282 return false;
283
284 /* The .rela.bss section holds copy relocs. */
285 if (! info->shared)
286 {
287 s = bfd_make_section (abfd, ".rela.bss");
288 if (s == NULL
289 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
290 || ! bfd_set_section_alignment (abfd, s, 2))
291 return false;
292 }
293
294 return true;
295 }
296
297 /* Look through the relocs for a section during the first phase, and
298 allocate space in the global offset table or procedure linkage
299 table. */
300
301 static boolean
302 elf32_sparc_check_relocs (abfd, info, sec, relocs)
303 bfd *abfd;
304 struct bfd_link_info *info;
305 asection *sec;
306 const Elf_Internal_Rela *relocs;
307 {
308 bfd *dynobj;
309 Elf_Internal_Shdr *symtab_hdr;
310 struct elf_link_hash_entry **sym_hashes;
311 bfd_vma *local_got_offsets;
312 const Elf_Internal_Rela *rel;
313 const Elf_Internal_Rela *rel_end;
314 asection *sgot;
315 asection *srelgot;
316 asection *splt;
317 asection *srelplt;
318 asection *sreloc;
319
320 if (info->relocateable)
321 return true;
322
323 dynobj = elf_hash_table (info)->dynobj;
324 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
325 sym_hashes = elf_sym_hashes (abfd);
326 local_got_offsets = elf_local_got_offsets (abfd);
327
328 sgot = NULL;
329 srelgot = NULL;
330 splt = NULL;
331 srelplt = NULL;
332 sreloc = NULL;
333
334 rel_end = relocs + sec->reloc_count;
335 for (rel = relocs; rel < rel_end; rel++)
336 {
337 long r_symndx;
338 struct elf_link_hash_entry *h;
339
340 r_symndx = ELF32_R_SYM (rel->r_info);
341 if (r_symndx < symtab_hdr->sh_info)
342 h = NULL;
343 else
344 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
345
346 /* Some relocs require a global offset table. FIXME: If this is
347 a static link, we don't really need to create the full
348 dynamic linking information. */
349 if (dynobj == NULL)
350 {
351 switch (ELF32_R_TYPE (rel->r_info))
352 {
353 case R_SPARC_GOT10:
354 case R_SPARC_GOT13:
355 case R_SPARC_GOT22:
356 case R_SPARC_WPLT30:
357 elf_hash_table (info)->dynobj = dynobj = abfd;
358 if (! bfd_elf32_link_create_dynamic_sections (dynobj, info))
359 return false;
360 break;
361
362 default:
363 break;
364 }
365 }
366
367 switch (ELF32_R_TYPE (rel->r_info))
368 {
369 case R_SPARC_GOT10:
370 case R_SPARC_GOT13:
371 case R_SPARC_GOT22:
372 /* This symbol requires a global offset table entry. */
373
374 if (sgot == NULL)
375 {
376 sgot = bfd_get_section_by_name (dynobj, ".got");
377 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
378 if (srelgot == NULL)
379 {
380 srelgot = bfd_make_section (dynobj, ".rela.got");
381 if (srelgot == NULL
382 || ! bfd_set_section_flags (dynobj, srelgot,
383 (SEC_ALLOC
384 | SEC_LOAD
385 | SEC_HAS_CONTENTS
386 | SEC_IN_MEMORY
387 | SEC_READONLY)))
388 return false;
389 }
390 BFD_ASSERT (sgot != NULL && srelgot != NULL);
391 }
392
393 if (h != NULL)
394 {
395 if (h->got_offset != (bfd_vma) -1)
396 {
397 /* We have already allocated space in the .got. */
398 break;
399 }
400 h->got_offset = sgot->_raw_size;
401 }
402 else
403 {
404 /* This is a global offset table entry for a local
405 symbol. */
406 if (local_got_offsets == NULL)
407 {
408 size_t size;
409 register int i;
410
411 size = symtab_hdr->sh_info * sizeof (bfd_vma);
412 local_got_offsets = (bfd_vma *) bfd_alloc (abfd, size);
413 if (local_got_offsets == NULL)
414 {
415 bfd_set_error (bfd_error_no_memory);
416 return false;
417 }
418 elf_local_got_offsets (abfd) = local_got_offsets;
419 for (i = 0; i < symtab_hdr->sh_info; i++)
420 local_got_offsets[i] = (bfd_vma) -1;
421 }
422 if (local_got_offsets[r_symndx] != (bfd_vma) -1)
423 {
424 /* We have already allocated space in the .got. */
425 break;
426 }
427 local_got_offsets[r_symndx] = sgot->_raw_size;
428 }
429
430 sgot->_raw_size += 4;
431 srelgot->_raw_size += sizeof (Elf32_External_Rela);
432
433 break;
434
435 case R_SPARC_WPLT30:
436 /* This symbol requires a procedure linkage table entry. */
437
438 if (h == NULL)
439 {
440 /* It does not make sense to have a procedure linkage
441 table entry for a local symbol. */
442 bfd_set_error (bfd_error_bad_value);
443 return false;
444 }
445
446 if (h->plt_offset != (bfd_vma) -1)
447 {
448 /* There is already an entry for this symbol in the
449 procedure linkage table. */
450 break;
451 }
452
453 if (splt == NULL)
454 {
455 splt = bfd_get_section_by_name (dynobj, ".plt");
456 srelplt = bfd_get_section_by_name (dynobj, ".rela.plt");
457 BFD_ASSERT (splt != NULL && srelplt != NULL);
458 }
459
460 /* The procedure linkage table has a maximum size. */
461 if (splt->_raw_size >= 0x400000)
462 {
463 bfd_set_error (bfd_error_bad_value);
464 return false;
465 }
466
467 h->plt_offset = splt->_raw_size;
468
469 /* Make room for this entry. */
470 splt->_raw_size += PLT_ENTRY_SIZE;
471 srelplt->_raw_size += sizeof (Elf32_External_Rela);
472
473 break;
474
475 case R_SPARC_PC10:
476 case R_SPARC_PC22:
477 if (h != NULL
478 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
479 break;
480 /* Fall through. */
481 case R_SPARC_8:
482 case R_SPARC_16:
483 case R_SPARC_32:
484 case R_SPARC_DISP8:
485 case R_SPARC_DISP16:
486 case R_SPARC_DISP32:
487 case R_SPARC_WDISP30:
488 case R_SPARC_WDISP22:
489 case R_SPARC_HI22:
490 case R_SPARC_22:
491 case R_SPARC_13:
492 case R_SPARC_LO10:
493 case R_SPARC_UA32:
494 if (info->shared
495 && (sec->flags & SEC_ALLOC) != 0)
496 {
497 /* When creating a shared object, we must copy these
498 relocs into the output file. We create a reloc
499 section in dynobj and make room for the reloc. */
500 if (sreloc == NULL)
501 {
502 const char *name;
503
504 name = (elf_string_from_elf_section
505 (abfd,
506 elf_elfheader (abfd)->e_shstrndx,
507 elf_section_data (sec)->rel_hdr.sh_name));
508 if (name == NULL)
509 return false;
510
511 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
512 && strcmp (bfd_get_section_name (abfd, sec),
513 name + 5) == 0);
514
515 sreloc = bfd_get_section_by_name (dynobj, name);
516 if (sreloc == NULL)
517 {
518 sreloc = bfd_make_section (dynobj, name);
519 if (sreloc == NULL
520 || ! bfd_set_section_flags (dynobj, sreloc,
521 (SEC_ALLOC
522 | SEC_LOAD
523 | SEC_HAS_CONTENTS
524 | SEC_IN_MEMORY
525 | SEC_READONLY))
526 || ! bfd_set_section_alignment (dynobj, sreloc, 2))
527 return false;
528 }
529 }
530
531 sreloc->_raw_size += sizeof (Elf32_External_Rela);
532 }
533
534 break;
535
536 default:
537 break;
538 }
539 }
540
541 return true;
542 }
543
544 /* Adjust a symbol defined by a dynamic object and referenced by a
545 regular object. The current definition is in some section of the
546 dynamic object, but we're not including those sections. We have to
547 change the definition to something the rest of the link can
548 understand. */
549
550 static boolean
551 elf32_sparc_adjust_dynamic_symbol (info, h)
552 struct bfd_link_info *info;
553 struct elf_link_hash_entry *h;
554 {
555 bfd *dynobj;
556 asection *s;
557 unsigned int power_of_two;
558
559 dynobj = elf_hash_table (info)->dynobj;
560
561 /* Make sure we know what is going on here. */
562 BFD_ASSERT (dynobj != NULL
563 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
564 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0
565 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
566 && h->root.type == bfd_link_hash_defined
567 && (bfd_get_flavour (h->root.u.def.section->owner)
568 == bfd_target_elf_flavour)
569 && (elf_elfheader (h->root.u.def.section->owner)->e_type
570 == ET_DYN)
571 && h->root.u.def.section->output_section == NULL);
572
573 /* If this is a function, put it in the procedure linkage table. We
574 will fill in the contents of the procedure linkage table later
575 (although we could actually do it here). */
576 if (h->type == STT_FUNC)
577 {
578 if (h->plt_offset == (bfd_vma) -1)
579 {
580 s = bfd_get_section_by_name (dynobj, ".plt");
581 BFD_ASSERT (s != NULL);
582
583 /* The procedure linkage table has a maximum size. */
584 if (s->_raw_size >= 0x400000)
585 {
586 bfd_set_error (bfd_error_bad_value);
587 return false;
588 }
589
590 /* Set the symbol to this location in the .plt. */
591 h->root.u.def.section = s;
592 h->root.u.def.value = s->_raw_size;
593
594 h->plt_offset = s->_raw_size;
595
596 /* Make room for this entry. */
597 s->_raw_size += PLT_ENTRY_SIZE;
598
599 /* We also need to make an entry in the .rela.plt section. */
600
601 s = bfd_get_section_by_name (dynobj, ".rela.plt");
602 BFD_ASSERT (s != NULL);
603 s->_raw_size += sizeof (Elf32_External_Rela);
604 }
605
606 return true;
607 }
608
609 /* If this is a weak symbol, and there is a real definition, the
610 processor independent code will have arranged for us to see the
611 real definition first, and we can just use the same value. */
612 if (h->weakdef != NULL)
613 {
614 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined);
615 h->root.u.def.section = h->weakdef->root.u.def.section;
616 h->root.u.def.value = h->weakdef->root.u.def.value;
617 return true;
618 }
619
620 /* This is a reference to a symbol defined by a dynamic object which
621 is not a function. */
622
623 /* If we are creating a shared library, we must presume that the
624 only references to the symbol are via the global offset table.
625 For such cases we need not do anything here; the relocations will
626 be handled correctly by relocate_section. */
627 if (info->shared)
628 return true;
629
630 /* We must allocate the symbol in our .dynbss section, which will
631 become part of the .bss section of the executable. There will be
632 an entry for this symbol in the .dynsym section. The dynamic
633 object will contain position independent code, so all references
634 from the dynamic object to this symbol will go through the global
635 offset table. The dynamic linker will use the .dynsym entry to
636 determine the address it must put in the global offset table, so
637 both the dynamic object and the regular object will refer to the
638 same memory location for the variable. */
639
640 s = bfd_get_section_by_name (dynobj, ".dynbss");
641 BFD_ASSERT (s != NULL);
642
643 /* If the symbol is currently defined in the .bss section of the
644 dynamic object, then it is OK to simply initialize it to zero.
645 If the symbol is in some other section, we must generate a
646 R_SPARC_COPY reloc to tell the dynamic linker to copy the initial
647 value out of the dynamic object and into the runtime process
648 image. We need to remember the offset into the .rel.bss section
649 we are going to use. */
650 if ((h->root.u.def.section->flags & SEC_LOAD) != 0)
651 {
652 asection *srel;
653
654 srel = bfd_get_section_by_name (dynobj, ".rela.bss");
655 BFD_ASSERT (srel != NULL);
656 srel->_raw_size += sizeof (Elf32_External_Rela);
657 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
658 }
659
660 /* We need to figure out the alignment required for this symbol. I
661 have no idea how ELF linkers handle this. */
662 power_of_two = bfd_log2 (h->size);
663 if (power_of_two > 3)
664 power_of_two = 3;
665
666 /* Apply the required alignment. */
667 s->_raw_size = BFD_ALIGN (s->_raw_size,
668 (bfd_size_type) (1 << power_of_two));
669 if (power_of_two > bfd_get_section_alignment (dynobj, s))
670 {
671 if (! bfd_set_section_alignment (dynobj, s, power_of_two))
672 return false;
673 }
674
675 /* Define the symbol as being at this point in the section. */
676 h->root.u.def.section = s;
677 h->root.u.def.value = s->_raw_size;
678
679 /* Increment the section size to make room for the symbol. */
680 s->_raw_size += h->size;
681
682 return true;
683 }
684
685 /* Set the sizes of the dynamic sections. */
686
687 static boolean
688 elf32_sparc_size_dynamic_sections (output_bfd, info)
689 bfd *output_bfd;
690 struct bfd_link_info *info;
691 {
692 bfd *dynobj;
693 asection *s;
694 boolean reltext;
695
696 dynobj = elf_hash_table (info)->dynobj;
697 BFD_ASSERT (dynobj != NULL);
698
699 /* Set the contents of the .interp section to the interpreter. */
700 if (! info->shared)
701 {
702 s = bfd_get_section_by_name (dynobj, ".interp");
703 BFD_ASSERT (s != NULL);
704 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
705 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
706 }
707
708 /* Make space for the trailing nop in .plt. */
709 s = bfd_get_section_by_name (dynobj, ".plt");
710 BFD_ASSERT (s != NULL);
711 s->_raw_size += 4;
712
713 /* The check_relocs and adjust_dynamic_symbol entry points have
714 determined the sizes of the various dynamic sections. Allocate
715 memory for them. */
716 reltext = false;
717 for (s = dynobj->sections; s != NULL; s = s->next)
718 {
719 const char *name;
720
721 if ((s->flags & SEC_IN_MEMORY) == 0)
722 continue;
723
724 /* It's OK to base decisions on the section name, because none
725 of the dynobj section names depend upon the input files. */
726 name = bfd_get_section_name (dynobj, s);
727
728 if (strncmp (name, ".rela", 5) == 0
729 && s->_raw_size > 0)
730 {
731 asection *target;
732
733 /* If this relocation section applies to a read only
734 section, then we probably need a DT_TEXTREL entry. */
735 target = bfd_get_section_by_name (output_bfd, name + 5);
736 if (target != NULL
737 && (target->flags & SEC_READONLY) != 0)
738 reltext = true;
739
740 /* We use the reloc_count field as a counter if we need to
741 copy relocs into the output file. */
742 s->reloc_count = 0;
743 }
744 else if (strcmp (name, ".plt") != 0
745 && strcmp (name, ".got") != 0)
746 {
747 /* It's not one of our sections, so don't allocate space. */
748 continue;
749 }
750
751 /* Allocate memory for the section contents. */
752 s->contents = (bfd_byte *) bfd_alloc (dynobj, s->_raw_size);
753 if (s->contents == NULL && s->_raw_size != 0)
754 {
755 bfd_set_error (bfd_error_no_memory);
756 return false;
757 }
758 }
759
760 /* Add some entries to the .dynamic section. We fill in the values
761 later, in elf32_sparc_finish_dynamic_sections, but we must add
762 the entries now so that we get the correct size for the .dynamic
763 section. The DT_DEBUG entry is filled in by the dynamic linker
764 and used by the debugger. */
765 if (! info->shared)
766 {
767 if (! bfd_elf32_add_dynamic_entry (info, DT_DEBUG, 0))
768 return false;
769 }
770
771 if (! bfd_elf32_add_dynamic_entry (info, DT_PLTGOT, 0)
772 || ! bfd_elf32_add_dynamic_entry (info, DT_PLTRELSZ, 0)
773 || ! bfd_elf32_add_dynamic_entry (info, DT_PLTREL, DT_RELA)
774 || ! bfd_elf32_add_dynamic_entry (info, DT_JMPREL, 0)
775 || ! bfd_elf32_add_dynamic_entry (info, DT_RELA, 0)
776 || ! bfd_elf32_add_dynamic_entry (info, DT_RELASZ, 0)
777 || ! bfd_elf32_add_dynamic_entry (info, DT_RELAENT,
778 sizeof (Elf32_External_Rela)))
779 return false;
780
781 if (reltext)
782 {
783 if (! bfd_elf32_add_dynamic_entry (info, DT_TEXTREL, 0))
784 return false;
785 }
786
787 return true;
788 }
789
790 /* Relocate a SPARC ELF section. */
791
792 static boolean
793 elf32_sparc_relocate_section (output_bfd, info, input_bfd, input_section,
794 contents, relocs, local_syms, local_sections,
795 output_names)
796 bfd *output_bfd;
797 struct bfd_link_info *info;
798 bfd *input_bfd;
799 asection *input_section;
800 bfd_byte *contents;
801 Elf_Internal_Rela *relocs;
802 Elf_Internal_Sym *local_syms;
803 asection **local_sections;
804 char *output_names;
805 {
806 bfd *dynobj;
807 Elf_Internal_Shdr *symtab_hdr;
808 struct elf_link_hash_entry **sym_hashes;
809 bfd_vma *local_got_offsets;
810 asection *sgot;
811 asection *splt;
812 asection *sreloc;
813 Elf_Internal_Rela *rel;
814 Elf_Internal_Rela *relend;
815
816 dynobj = elf_hash_table (info)->dynobj;
817 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
818 sym_hashes = elf_sym_hashes (input_bfd);
819 local_got_offsets = elf_local_got_offsets (input_bfd);
820
821 sgot = NULL;
822 splt = NULL;
823 sreloc = NULL;
824
825 rel = relocs;
826 relend = relocs + input_section->reloc_count;
827 for (; rel < relend; rel++)
828 {
829 int r_type;
830 const reloc_howto_type *howto;
831 long r_symndx;
832 struct elf_link_hash_entry *h;
833 Elf_Internal_Sym *sym;
834 asection *sec;
835 bfd_vma relocation;
836 bfd_reloc_status_type r;
837
838 r_type = ELF32_R_TYPE (rel->r_info);
839 if (r_type < 0 || r_type >= (int) R_SPARC_max)
840 {
841 bfd_set_error (bfd_error_bad_value);
842 return false;
843 }
844 howto = elf_sparc_howto_table + r_type;
845
846 r_symndx = ELF32_R_SYM (rel->r_info);
847
848 if (info->relocateable)
849 {
850 /* This is a relocateable link. We don't have to change
851 anything, unless the reloc is against a section symbol,
852 in which case we have to adjust according to where the
853 section symbol winds up in the output section. */
854 if (r_symndx < symtab_hdr->sh_info)
855 {
856 sym = local_syms + r_symndx;
857 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
858 {
859 sec = local_sections[r_symndx];
860 rel->r_addend += sec->output_offset + sym->st_value;
861 }
862 }
863
864 continue;
865 }
866
867 /* This is a final link. */
868 h = NULL;
869 sym = NULL;
870 sec = NULL;
871 if (r_symndx < symtab_hdr->sh_info)
872 {
873 sym = local_syms + r_symndx;
874 sec = local_sections[r_symndx];
875 relocation = (sec->output_section->vma
876 + sec->output_offset
877 + sym->st_value);
878 }
879 else
880 {
881 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
882 if (h->root.type == bfd_link_hash_defined)
883 {
884 sec = h->root.u.def.section;
885 relocation = (h->root.u.def.value
886 + sec->output_section->vma
887 + sec->output_offset);
888 }
889 else if (h->root.type == bfd_link_hash_weak)
890 relocation = 0;
891 else if (info->shared)
892 relocation = 0;
893 else
894 {
895 if (! ((*info->callbacks->undefined_symbol)
896 (info, h->root.root.string, input_bfd,
897 input_section, rel->r_offset)))
898 return false;
899 relocation = 0;
900 }
901 }
902
903 switch (r_type)
904 {
905 case R_SPARC_GOT10:
906 case R_SPARC_GOT13:
907 case R_SPARC_GOT22:
908 /* Relocation is to the entry for this symbol in the global
909 offset table. */
910 if (sgot == NULL)
911 {
912 sgot = bfd_get_section_by_name (dynobj, ".got");
913 BFD_ASSERT (sgot != NULL);
914 }
915
916 if (h != NULL)
917 {
918 BFD_ASSERT (h->got_offset != (bfd_vma) -1);
919 relocation = sgot->output_offset + h->got_offset;
920 }
921 else
922 {
923 bfd_vma off;
924
925 BFD_ASSERT (local_got_offsets != NULL
926 && local_got_offsets[r_symndx] != (bfd_vma) -1);
927
928 off = local_got_offsets[r_symndx];
929
930 /* The offset must always be a multiple of 4. We use
931 the least significant bit to record whether we have
932 already generated the necessary reloc. */
933 if ((off & 1) != 0)
934 off &= ~1;
935 else
936 {
937 asection *srelgot;
938 Elf_Internal_Rela outrel;
939
940 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
941
942 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
943 BFD_ASSERT (srelgot != NULL);
944
945 outrel.r_offset = (sgot->output_section->vma
946 + sgot->output_offset
947 + off);
948 outrel.r_info = ELF32_R_INFO (0, R_SPARC_RELATIVE);
949 outrel.r_addend = 0;
950 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
951 (((Elf32_External_Rela *)
952 srelgot->contents)
953 + srelgot->reloc_count));
954 ++srelgot->reloc_count;
955
956 local_got_offsets[r_symndx] |= 1;
957 }
958
959 relocation = sgot->output_offset + off;
960 }
961
962 break;
963
964 case R_SPARC_WPLT30:
965 /* Relocation is to the entry for this symbol in the
966 procedure linkage table. */
967 if (splt == NULL)
968 {
969 splt = bfd_get_section_by_name (dynobj, ".plt");
970 BFD_ASSERT (splt != NULL);
971 }
972
973 BFD_ASSERT (h != NULL && h->plt_offset != (bfd_vma) -1);
974 relocation = (splt->output_section->vma
975 + splt->output_offset
976 + h->plt_offset);
977 break;
978
979 case R_SPARC_PC10:
980 case R_SPARC_PC22:
981 if (h != NULL
982 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
983 break;
984 /* Fall through. */
985 case R_SPARC_8:
986 case R_SPARC_16:
987 case R_SPARC_32:
988 case R_SPARC_DISP8:
989 case R_SPARC_DISP16:
990 case R_SPARC_DISP32:
991 case R_SPARC_WDISP30:
992 case R_SPARC_WDISP22:
993 case R_SPARC_HI22:
994 case R_SPARC_22:
995 case R_SPARC_13:
996 case R_SPARC_LO10:
997 case R_SPARC_UA32:
998 if (info->shared
999 && (input_section->flags & SEC_ALLOC) != 0)
1000 {
1001 Elf_Internal_Rela outrel;
1002
1003 /* When generating a shared object, these relocations
1004 are copied into the output file to be resolved at run
1005 time. */
1006
1007 if (sreloc == NULL)
1008 {
1009 const char *name;
1010
1011 name = (elf_string_from_elf_section
1012 (input_bfd,
1013 elf_elfheader (input_bfd)->e_shstrndx,
1014 elf_section_data (input_section)->rel_hdr.sh_name));
1015 if (name == NULL)
1016 return false;
1017
1018 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
1019 && strcmp (bfd_get_section_name (input_bfd,
1020 input_section),
1021 name + 5) == 0);
1022
1023 sreloc = bfd_get_section_by_name (dynobj, name);
1024 BFD_ASSERT (sreloc != NULL);
1025 }
1026
1027 outrel.r_offset = (rel->r_offset
1028 + input_section->output_section->vma
1029 + input_section->output_offset);
1030 if (h != NULL)
1031 {
1032 BFD_ASSERT (h->dynindx != (bfd_vma) -1);
1033 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
1034 outrel.r_addend = 0;
1035 }
1036 else
1037 {
1038 long indx;
1039
1040 sym = local_syms + r_symndx;
1041
1042 /* If this isn't a section symbol, we need to map it
1043 to something that is going to be put into the
1044 dynamic symbols. The case will probably never
1045 arise. */
1046 BFD_ASSERT (ELF_ST_TYPE (sym->st_info) == STT_SECTION);
1047
1048 sec = local_sections[r_symndx];
1049 if (sec != NULL && bfd_is_abs_section (sec))
1050 indx = 0;
1051 else if (sec == NULL || sec->owner == NULL)
1052 {
1053 bfd_set_error (bfd_error_bad_value);
1054 return false;
1055 }
1056 else
1057 {
1058 indx = sec->output_section->target_index;
1059 if (indx == 0)
1060 abort ();
1061 }
1062
1063 outrel.r_info = ELF32_R_INFO (indx, r_type);
1064 outrel.r_addend = sec->output_offset + sym->st_value;
1065 }
1066
1067 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1068 (((Elf32_External_Rela *)
1069 sreloc->contents)
1070 + sreloc->reloc_count));
1071 ++sreloc->reloc_count;
1072
1073 /* This reloc will be computed at runtime, so there's no
1074 need to do anything now. */
1075 continue;
1076 }
1077
1078 default:
1079 break;
1080 }
1081
1082 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1083 contents, rel->r_offset,
1084 relocation, rel->r_addend);
1085
1086 if (r != bfd_reloc_ok)
1087 {
1088 switch (r)
1089 {
1090 default:
1091 case bfd_reloc_outofrange:
1092 abort ();
1093 case bfd_reloc_overflow:
1094 {
1095 const char *name;
1096
1097 if (h != NULL)
1098 name = h->root.root.string;
1099 else
1100 {
1101 name = output_names + sym->st_name;
1102 if (name == NULL)
1103 return false;
1104 if (*name == '\0')
1105 name = bfd_section_name (input_bfd, sec);
1106 }
1107 if (! ((*info->callbacks->reloc_overflow)
1108 (info, name, howto->name, (bfd_vma) 0,
1109 input_bfd, input_section, rel->r_offset)))
1110 return false;
1111 }
1112 break;
1113 }
1114 }
1115 }
1116
1117 return true;
1118 }
1119
1120 /* Finish up dynamic symbol handling. We set the contents of various
1121 dynamic sections here. */
1122
1123 static boolean
1124 elf32_sparc_finish_dynamic_symbol (output_bfd, info, h, sym)
1125 bfd *output_bfd;
1126 struct bfd_link_info *info;
1127 struct elf_link_hash_entry *h;
1128 Elf_Internal_Sym *sym;
1129 {
1130 bfd *dynobj;
1131
1132 dynobj = elf_hash_table (info)->dynobj;
1133
1134 if (h->plt_offset != (bfd_vma) -1)
1135 {
1136 asection *splt;
1137 asection *srela;
1138 Elf_Internal_Rela rela;
1139
1140 /* This symbol has an entry in the procedure linkage table. Set
1141 it up. */
1142
1143 BFD_ASSERT (h->dynindx != -1);
1144
1145 splt = bfd_get_section_by_name (dynobj, ".plt");
1146 srela = bfd_get_section_by_name (dynobj, ".rela.plt");
1147 BFD_ASSERT (splt != NULL && srela != NULL);
1148
1149 /* Fill in the entry in the procedure linkage table. */
1150 bfd_put_32 (output_bfd,
1151 PLT_ENTRY_WORD0 + h->plt_offset,
1152 splt->contents + h->plt_offset);
1153 bfd_put_32 (output_bfd,
1154 (PLT_ENTRY_WORD1
1155 + (((- (h->plt_offset + 4)) >> 2) & 0x3fffff)),
1156 splt->contents + h->plt_offset + 4);
1157 bfd_put_32 (output_bfd, PLT_ENTRY_WORD2,
1158 splt->contents + h->plt_offset + 8);
1159
1160 /* Fill in the entry in the .rela.plt section. */
1161 rela.r_offset = (splt->output_section->vma
1162 + splt->output_offset
1163 + h->plt_offset);
1164 rela.r_info = ELF32_R_INFO (h->dynindx, R_SPARC_JMP_SLOT);
1165 rela.r_addend = 0;
1166 bfd_elf32_swap_reloca_out (output_bfd, &rela,
1167 ((Elf32_External_Rela *) srela->contents
1168 + h->plt_offset / PLT_ENTRY_SIZE - 4));
1169
1170 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1171 {
1172 /* Mark the symbol as undefined, rather than as defined in
1173 the .plt section. Leave the value alone. */
1174 sym->st_shndx = SHN_UNDEF;
1175 }
1176 }
1177
1178 if (h->got_offset != (bfd_vma) -1)
1179 {
1180 asection *sgot;
1181 asection *srela;
1182 Elf_Internal_Rela rela;
1183
1184 /* This symbol has an entry in the global offset table. Set it
1185 up. */
1186
1187 BFD_ASSERT (h->dynindx != -1);
1188
1189 sgot = bfd_get_section_by_name (dynobj, ".got");
1190 srela = bfd_get_section_by_name (dynobj, ".rela.got");
1191 BFD_ASSERT (sgot != NULL && srela != NULL);
1192
1193 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got_offset);
1194
1195 rela.r_offset = (sgot->output_section->vma
1196 + sgot->output_offset
1197 + h->got_offset);
1198 rela.r_info = ELF32_R_INFO (h->dynindx, R_SPARC_GLOB_DAT);
1199 rela.r_addend = 0;
1200 bfd_elf32_swap_reloca_out (output_bfd, &rela,
1201 ((Elf32_External_Rela *) srela->contents
1202 + srela->reloc_count));
1203 ++srela->reloc_count;
1204 }
1205
1206 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
1207 {
1208 asection *s;
1209 Elf_Internal_Rela rela;
1210
1211 /* This symbols needs a copy reloc. Set it up. */
1212
1213 BFD_ASSERT (h->dynindx != -1);
1214
1215 s = bfd_get_section_by_name (h->root.u.def.section->owner,
1216 ".rela.bss");
1217 BFD_ASSERT (s != NULL);
1218
1219 rela.r_offset = (h->root.u.def.value
1220 + h->root.u.def.section->output_section->vma
1221 + h->root.u.def.section->output_offset);
1222 rela.r_info = ELF32_R_INFO (h->dynindx, R_SPARC_COPY);
1223 rela.r_addend = 0;
1224 bfd_elf32_swap_reloca_out (output_bfd, &rela,
1225 ((Elf32_External_Rela *) s->contents
1226 + s->reloc_count));
1227 ++s->reloc_count;
1228 }
1229
1230 /* Mark some specially defined symbols as absolute. */
1231 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
1232 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
1233 || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0)
1234 sym->st_shndx = SHN_ABS;
1235
1236 return true;
1237 }
1238
1239 /* Finish up the dynamic sections. */
1240
1241 static boolean
1242 elf32_sparc_finish_dynamic_sections (output_bfd, info)
1243 bfd *output_bfd;
1244 struct bfd_link_info *info;
1245 {
1246 bfd *dynobj;
1247 asection *splt;
1248 asection *sgot;
1249 asection *sdyn;
1250 Elf32_External_Dyn *dyncon, *dynconend;
1251
1252 dynobj = elf_hash_table (info)->dynobj;
1253
1254 splt = bfd_get_section_by_name (dynobj, ".plt");
1255 sgot = bfd_get_section_by_name (dynobj, ".got");
1256 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
1257 BFD_ASSERT (splt != NULL && sgot != NULL && sdyn != NULL);
1258
1259 dyncon = (Elf32_External_Dyn *) sdyn->contents;
1260 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
1261 for (; dyncon < dynconend; dyncon++)
1262 {
1263 Elf_Internal_Dyn dyn;
1264 const char *name;
1265 boolean size;
1266
1267 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
1268
1269 switch (dyn.d_tag)
1270 {
1271 case DT_PLTGOT: name = ".plt"; size = false; break;
1272 case DT_PLTRELSZ: name = ".rela.plt"; size = true; break;
1273 case DT_JMPREL: name = ".rela.plt"; size = false; break;
1274 default: name = NULL; size = false; break;
1275 }
1276
1277 if (name != NULL)
1278 {
1279 asection *s;
1280
1281 s = bfd_get_section_by_name (output_bfd, name);
1282 BFD_ASSERT (s != NULL);
1283 if (! size)
1284 dyn.d_un.d_ptr = s->vma;
1285 else
1286 {
1287 if (s->_cooked_size != 0)
1288 dyn.d_un.d_val = s->_cooked_size;
1289 else
1290 dyn.d_un.d_val = s->_raw_size;
1291 }
1292 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1293 }
1294 }
1295
1296 /* Clear the first four entries in the procedure linkage table, and
1297 put a nop in the last four bytes. */
1298 if (splt->_raw_size > 0)
1299 {
1300 memset (splt->contents, 0, 4 * PLT_ENTRY_SIZE);
1301 bfd_put_32 (output_bfd, SPARC_NOP,
1302 splt->contents + splt->_raw_size - 4);
1303 }
1304
1305 /* Set the first entry in the global offset table to the address of
1306 the dynamic section. */
1307 if (sgot->_raw_size > 0)
1308 bfd_put_32 (output_bfd,
1309 sdyn->output_section->vma + sdyn->output_offset,
1310 sgot->contents);
1311
1312 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
1313 elf_section_data (splt->output_section)->this_hdr.sh_entsize =
1314 PLT_ENTRY_SIZE;
1315
1316 return true;
1317 }
1318
1319 #define TARGET_BIG_SYM bfd_elf32_sparc_vec
1320 #define TARGET_BIG_NAME "elf32-sparc"
1321 #define ELF_ARCH bfd_arch_sparc
1322 #define ELF_MACHINE_CODE EM_SPARC
1323 #define ELF_MAXPAGESIZE 0x10000
1324 #define elf_backend_create_dynamic_sections \
1325 elf32_sparc_create_dynamic_sections
1326 #define elf_backend_check_relocs elf32_sparc_check_relocs
1327 #define elf_backend_adjust_dynamic_symbol \
1328 elf32_sparc_adjust_dynamic_symbol
1329 #define elf_backend_size_dynamic_sections \
1330 elf32_sparc_size_dynamic_sections
1331 #define elf_backend_relocate_section elf32_sparc_relocate_section
1332 #define elf_backend_finish_dynamic_symbol \
1333 elf32_sparc_finish_dynamic_symbol
1334 #define elf_backend_finish_dynamic_sections \
1335 elf32_sparc_finish_dynamic_sections
1336
1337 #include "elf32-target.h"
This page took 0.07506 seconds and 4 git commands to generate.