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