* elf32-mips.c (mips_elf_object_p): Unconditionally set
[deliverable/binutils-gdb.git] / bfd / elf32-sparc.c
CommitLineData
32090b8e 1/* SPARC-specific support for 32-bit ELF
f786a73f 2 Copyright 1993, 1994, 1995 Free Software Foundation, Inc.
32090b8e
KR
3
4This file is part of BFD, the Binary File Descriptor library.
5
6This program is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2 of the License, or
9(at your option) any later version.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with this program; if not, write to the Free Software
18Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20#include "bfd.h"
21#include "sysdep.h"
013dec1a 22#include "bfdlink.h"
32090b8e
KR
23#include "libbfd.h"
24#include "libelf.h"
25
f786a73f 26static reloc_howto_type *bfd_elf32_bfd_reloc_type_lookup
013dec1a
ILT
27 PARAMS ((bfd *, bfd_reloc_code_real_type));
28static void elf_info_to_howto
29 PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
30static boolean elf32_sparc_create_dynamic_sections
31 PARAMS ((bfd *, struct bfd_link_info *));
12662be4
ILT
32static boolean elf32_sparc_create_got_section
33 PARAMS ((bfd *, struct bfd_link_info *));
4fbc96ad
KR
34static boolean elf32_sparc_check_relocs
35 PARAMS ((bfd *, struct bfd_link_info *, asection *,
36 const Elf_Internal_Rela *));
013dec1a
ILT
37static boolean elf32_sparc_adjust_dynamic_symbol
38 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
14cac507
ILT
39static boolean elf32_sparc_adjust_dynindx
40 PARAMS ((struct elf_link_hash_entry *, PTR));
013dec1a
ILT
41static boolean elf32_sparc_size_dynamic_sections
42 PARAMS ((bfd *, struct bfd_link_info *));
43static boolean elf32_sparc_relocate_section
44 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
12662be4 45 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
013dec1a
ILT
46static boolean elf32_sparc_finish_dynamic_symbol
47 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
48 Elf_Internal_Sym *));
49static boolean elf32_sparc_finish_dynamic_sections
50 PARAMS ((bfd *, struct bfd_link_info *));
51
32090b8e
KR
52enum 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
71static 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
89static reloc_howto_type elf_sparc_howto_table[] =
90{
013dec1a
ILT
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),
4fbc96ad 104 HOWTO(R_SPARC_GOT10, 0,2,10,false,0,complain_overflow_dont, bfd_elf_generic_reloc,"R_SPARC_GOT10", false,0,0x000003ff,true),
30dc85f1 105 HOWTO(R_SPARC_GOT13, 0,2,13,false,0,complain_overflow_signed, bfd_elf_generic_reloc,"R_SPARC_GOT13", false,0,0x00001fff,true),
4fbc96ad
KR
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),
12662be4
ILT
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),
013dec1a
ILT
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),
32090b8e
KR
115};
116
117struct elf_reloc_map {
118 unsigned char bfd_reloc_val;
119 unsigned char elf_reloc_val;
120};
121
122static 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 },
4fbc96ad 147/*{ BFD_RELOC_SPARC_UA32, R_SPARC_UA32 }, not used?? */
32090b8e
KR
148};
149
f786a73f 150static reloc_howto_type *
013dec1a
ILT
151bfd_elf32_bfd_reloc_type_lookup (abfd, code)
152 bfd *abfd;
153 bfd_reloc_code_real_type code;
32090b8e
KR
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
164static void
013dec1a
ILT
165elf_info_to_howto (abfd, cache_ptr, dst)
166 bfd *abfd;
167 arelent *cache_ptr;
168 Elf_Internal_Rela *dst;
32090b8e
KR
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
013dec1a
ILT
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
204static boolean
205elf32_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;
4fbc96ad
KR
233 h->type = STT_OBJECT;
234
235 if (info->shared
236 && ! bfd_elf32_link_record_dynamic_symbol (info, h))
237 return false;
013dec1a 238
013dec1a
ILT
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
12662be4
ILT
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
274static boolean
275elf32_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
013dec1a
ILT
286 s = bfd_make_section (abfd, ".got");
287 if (s == NULL
12662be4
ILT
288 || ! bfd_set_section_flags (abfd, s,
289 (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
290 | SEC_IN_MEMORY))
013dec1a
ILT
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;
4fbc96ad
KR
308 h->type = STT_OBJECT;
309
310 if (info->shared
311 && ! bfd_elf32_link_record_dynamic_symbol (info, h))
312 return false;
013dec1a
ILT
313
314 /* The first global offset table entry is reserved. */
315 s->_raw_size += 4;
316
4fbc96ad 317 return true;
12662be4 318}
4fbc96ad
KR
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
324static boolean
325elf32_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;
4fbc96ad
KR
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;
4fbc96ad
KR
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
4fbc96ad
KR
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
12662be4
ILT
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
4fbc96ad
KR
380 if (sgot == NULL)
381 {
382 sgot = bfd_get_section_by_name (dynobj, ".got");
12662be4
ILT
383 BFD_ASSERT (sgot != NULL);
384 }
385
386 if (srelgot == NULL
387 && (h != NULL || info->shared))
388 {
4fbc96ad
KR
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
12662be4
ILT
399 | SEC_READONLY))
400 || ! bfd_set_section_alignment (dynobj, srelgot, 2))
4fbc96ad
KR
401 return false;
402 }
4fbc96ad
KR
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;
12662be4
ILT
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);
4fbc96ad
KR
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;
12662be4
ILT
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 }
4fbc96ad
KR
457 }
458
459 sgot->_raw_size += 4;
4fbc96ad
KR
460
461 break;
462
463 case R_SPARC_WPLT30:
12662be4
ILT
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. */
4fbc96ad
KR
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
12662be4
ILT
478 /* Make sure this symbol is output as a dynamic symbol. */
479 if (h->dynindx == -1)
4fbc96ad 480 {
12662be4
ILT
481 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
482 return false;
4fbc96ad
KR
483 }
484
12662be4 485 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
4fbc96ad
KR
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 }
013dec1a
ILT
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
564static boolean
565elf32_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;
013dec1a
ILT
572
573 dynobj = elf_hash_table (info)->dynobj;
574
575 /* Make sure we know what is going on here. */
3004a68c
ILT
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)));
013dec1a
ILT
584
585 /* If this is a function, put it in the procedure linkage table. We
586 will fill in the contents of the procedure linkage table later
587 (although we could actually do it here). */
12662be4
ILT
588 if (h->type == STT_FUNC
589 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
013dec1a 590 {
12662be4 591 if (! elf_hash_table (info)->dynamic_sections_created)
013dec1a 592 {
12662be4
ILT
593 /* This case can occur if we saw a WPLT30 reloc in an input
594 file, but none of the input files were dynamic objects.
595 In such a case, we don't actually need to build a
596 procedure linkage table, and we can just do a WDISP30
597 reloc instead. */
598 BFD_ASSERT ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0);
599 return true;
600 }
4fbc96ad 601
12662be4
ILT
602 s = bfd_get_section_by_name (dynobj, ".plt");
603 BFD_ASSERT (s != NULL);
013dec1a 604
12662be4
ILT
605 /* The first four entries in .plt are reserved. */
606 if (s->_raw_size == 0)
607 s->_raw_size = 4 * PLT_ENTRY_SIZE;
013dec1a 608
12662be4
ILT
609 /* The procedure linkage table has a maximum size. */
610 if (s->_raw_size >= 0x400000)
611 {
612 bfd_set_error (bfd_error_bad_value);
613 return false;
614 }
013dec1a 615
9b09a015
ILT
616 /* If this symbol is not defined in a regular file, and we are
617 not generating a shared library, then set the symbol to this
618 location in the .plt. This is required to make function
619 pointers compare as equal between the normal executable and
620 the shared library. */
621 if (! info->shared
622 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
14cac507
ILT
623 {
624 h->root.u.def.section = s;
625 h->root.u.def.value = s->_raw_size;
626 }
013dec1a 627
12662be4 628 h->plt_offset = s->_raw_size;
4fbc96ad 629
12662be4
ILT
630 /* Make room for this entry. */
631 s->_raw_size += PLT_ENTRY_SIZE;
632
633 /* We also need to make an entry in the .rela.plt section. */
634
635 s = bfd_get_section_by_name (dynobj, ".rela.plt");
636 BFD_ASSERT (s != NULL);
637 s->_raw_size += sizeof (Elf32_External_Rela);
013dec1a
ILT
638
639 return true;
640 }
641
642 /* If this is a weak symbol, and there is a real definition, the
643 processor independent code will have arranged for us to see the
644 real definition first, and we can just use the same value. */
645 if (h->weakdef != NULL)
646 {
f786a73f
ILT
647 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
648 || h->weakdef->root.type == bfd_link_hash_defweak);
013dec1a
ILT
649 h->root.u.def.section = h->weakdef->root.u.def.section;
650 h->root.u.def.value = h->weakdef->root.u.def.value;
013dec1a
ILT
651 return true;
652 }
653
654 /* This is a reference to a symbol defined by a dynamic object which
4fbc96ad
KR
655 is not a function. */
656
657 /* If we are creating a shared library, we must presume that the
658 only references to the symbol are via the global offset table.
659 For such cases we need not do anything here; the relocations will
660 be handled correctly by relocate_section. */
661 if (info->shared)
662 return true;
663
664 /* We must allocate the symbol in our .dynbss section, which will
665 become part of the .bss section of the executable. There will be
666 an entry for this symbol in the .dynsym section. The dynamic
667 object will contain position independent code, so all references
668 from the dynamic object to this symbol will go through the global
669 offset table. The dynamic linker will use the .dynsym entry to
670 determine the address it must put in the global offset table, so
671 both the dynamic object and the regular object will refer to the
672 same memory location for the variable. */
013dec1a
ILT
673
674 s = bfd_get_section_by_name (dynobj, ".dynbss");
675 BFD_ASSERT (s != NULL);
676
677 /* If the symbol is currently defined in the .bss section of the
678 dynamic object, then it is OK to simply initialize it to zero.
679 If the symbol is in some other section, we must generate a
680 R_SPARC_COPY reloc to tell the dynamic linker to copy the initial
681 value out of the dynamic object and into the runtime process
682 image. We need to remember the offset into the .rel.bss section
7c6da9ca 683 we are going to use. */
4fbc96ad 684 if ((h->root.u.def.section->flags & SEC_LOAD) != 0)
013dec1a
ILT
685 {
686 asection *srel;
687
688 srel = bfd_get_section_by_name (dynobj, ".rela.bss");
689 BFD_ASSERT (srel != NULL);
013dec1a 690 srel->_raw_size += sizeof (Elf32_External_Rela);
4fbc96ad 691 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
013dec1a
ILT
692 }
693
694 /* We need to figure out the alignment required for this symbol. I
695 have no idea how ELF linkers handle this. */
7c6da9ca
ILT
696 power_of_two = bfd_log2 (h->size);
697 if (power_of_two > 3)
698 power_of_two = 3;
013dec1a
ILT
699
700 /* Apply the required alignment. */
7c6da9ca
ILT
701 s->_raw_size = BFD_ALIGN (s->_raw_size,
702 (bfd_size_type) (1 << power_of_two));
013dec1a
ILT
703 if (power_of_two > bfd_get_section_alignment (dynobj, s))
704 {
705 if (! bfd_set_section_alignment (dynobj, s, power_of_two))
706 return false;
707 }
708
709 /* Define the symbol as being at this point in the section. */
710 h->root.u.def.section = s;
711 h->root.u.def.value = s->_raw_size;
712
713 /* Increment the section size to make room for the symbol. */
714 s->_raw_size += h->size;
715
716 return true;
717}
718
013dec1a
ILT
719/* Set the sizes of the dynamic sections. */
720
721static boolean
722elf32_sparc_size_dynamic_sections (output_bfd, info)
723 bfd *output_bfd;
724 struct bfd_link_info *info;
725{
726 bfd *dynobj;
727 asection *s;
4fbc96ad 728 boolean reltext;
f786a73f 729 boolean relplt;
013dec1a
ILT
730
731 dynobj = elf_hash_table (info)->dynobj;
732 BFD_ASSERT (dynobj != NULL);
733
12662be4 734 if (elf_hash_table (info)->dynamic_sections_created)
8af74670 735 {
12662be4
ILT
736 /* Set the contents of the .interp section to the interpreter. */
737 if (! info->shared)
738 {
739 s = bfd_get_section_by_name (dynobj, ".interp");
740 BFD_ASSERT (s != NULL);
741 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
742 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
743 }
744
745 /* Make space for the trailing nop in .plt. */
746 s = bfd_get_section_by_name (dynobj, ".plt");
8af74670 747 BFD_ASSERT (s != NULL);
12662be4
ILT
748 if (s->_raw_size > 0)
749 s->_raw_size += 4;
750 }
751 else
752 {
753 /* We may have created entries in the .rela.got section.
754 However, if we are not creating the dynamic sections, we will
755 not actually use these entries. Reset the size of .rela.got,
756 which will cause it to get stripped from the output file
757 below. */
758 s = bfd_get_section_by_name (dynobj, ".rela.got");
759 if (s != NULL)
760 s->_raw_size = 0;
8af74670 761 }
013dec1a 762
4fbc96ad
KR
763 /* The check_relocs and adjust_dynamic_symbol entry points have
764 determined the sizes of the various dynamic sections. Allocate
765 memory for them. */
766 reltext = false;
f786a73f 767 relplt = false;
4fbc96ad
KR
768 for (s = dynobj->sections; s != NULL; s = s->next)
769 {
770 const char *name;
12662be4 771 boolean strip;
4fbc96ad
KR
772
773 if ((s->flags & SEC_IN_MEMORY) == 0)
774 continue;
775
776 /* It's OK to base decisions on the section name, because none
777 of the dynobj section names depend upon the input files. */
778 name = bfd_get_section_name (dynobj, s);
779
12662be4
ILT
780 strip = false;
781
782 if (strncmp (name, ".rela", 5) == 0)
4fbc96ad 783 {
12662be4
ILT
784 if (s->_raw_size == 0)
785 {
786 /* If we don't need this section, strip it from the
787 output file. This is to handle .rela.bss and
788 .rel.plt. We must create it in
789 create_dynamic_sections, because it must be created
790 before the linker maps input sections to output
791 sections. The linker does that before
792 adjust_dynamic_symbol is called, and it is that
793 function which decides whether anything needs to go
794 into these sections. */
795 strip = true;
796 }
797 else
798 {
799 asection *target;
800
801 /* If this relocation section applies to a read only
802 section, then we probably need a DT_TEXTREL entry. */
803 target = bfd_get_section_by_name (output_bfd, name + 5);
804 if (target != NULL
805 && (target->flags & SEC_READONLY) != 0)
806 reltext = true;
807
f786a73f
ILT
808 if (strcmp (name, ".rela.plt") == 0)
809 relplt = true;
810
12662be4
ILT
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 }
4fbc96ad
KR
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 }
12662be4
ILT
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
4fbc96ad
KR
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 }
013dec1a 845
12662be4 846 if (elf_hash_table (info)->dynamic_sections_created)
4fbc96ad 847 {
12662be4
ILT
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 }
013dec1a 858
f786a73f
ILT
859 if (! bfd_elf32_add_dynamic_entry (info, DT_PLTGOT, 0))
860 return false;
861
862 if (relplt)
863 {
864 if (! bfd_elf32_add_dynamic_entry (info, DT_PLTRELSZ, 0)
865 || ! bfd_elf32_add_dynamic_entry (info, DT_PLTREL, DT_RELA)
866 || ! bfd_elf32_add_dynamic_entry (info, DT_JMPREL, 0))
867 return false;
868 }
869
870 if (! bfd_elf32_add_dynamic_entry (info, DT_RELA, 0)
12662be4
ILT
871 || ! bfd_elf32_add_dynamic_entry (info, DT_RELASZ, 0)
872 || ! bfd_elf32_add_dynamic_entry (info, DT_RELAENT,
873 sizeof (Elf32_External_Rela)))
4fbc96ad 874 return false;
12662be4
ILT
875
876 if (reltext)
877 {
878 if (! bfd_elf32_add_dynamic_entry (info, DT_TEXTREL, 0))
879 return false;
880 }
4fbc96ad
KR
881 }
882
14cac507
ILT
883 /* If we are generating a shared library, we generate a section
884 symbol for each output section. These are local symbols, which
885 means that they must come first in the dynamic symbol table.
886 That means we must increment the dynamic symbol index of every
887 other dynamic symbol. */
888 if (info->shared)
889 {
890 int c, i;
891
892 c = bfd_count_sections (output_bfd);
893 elf_link_hash_traverse (elf_hash_table (info),
894 elf32_sparc_adjust_dynindx,
895 (PTR) &c);
896 elf_hash_table (info)->dynsymcount += c;
897
898 for (i = 1, s = output_bfd->sections; s != NULL; s = s->next, i++)
899 {
900 elf_section_data (s)->dynindx = i;
901 /* These symbols will have no names, so we don't need to
902 fiddle with dynstr_index. */
903 }
904 }
905
906 return true;
907}
908
909/* Increment the index of a dynamic symbol by a given amount. Called
910 via elf_link_hash_traverse. */
911
912static boolean
913elf32_sparc_adjust_dynindx (h, cparg)
914 struct elf_link_hash_entry *h;
915 PTR cparg;
916{
917 int *cp = (int *) cparg;
918
919 if (h->dynindx != -1)
920 h->dynindx += *cp;
013dec1a
ILT
921 return true;
922}
923
924/* Relocate a SPARC ELF section. */
925
926static boolean
927elf32_sparc_relocate_section (output_bfd, info, input_bfd, input_section,
12662be4 928 contents, relocs, local_syms, local_sections)
013dec1a
ILT
929 bfd *output_bfd;
930 struct bfd_link_info *info;
931 bfd *input_bfd;
932 asection *input_section;
933 bfd_byte *contents;
934 Elf_Internal_Rela *relocs;
935 Elf_Internal_Sym *local_syms;
936 asection **local_sections;
937{
4fbc96ad 938 bfd *dynobj;
013dec1a
ILT
939 Elf_Internal_Shdr *symtab_hdr;
940 struct elf_link_hash_entry **sym_hashes;
4fbc96ad
KR
941 bfd_vma *local_got_offsets;
942 asection *sgot;
943 asection *splt;
944 asection *sreloc;
013dec1a
ILT
945 Elf_Internal_Rela *rel;
946 Elf_Internal_Rela *relend;
947
4fbc96ad 948 dynobj = elf_hash_table (info)->dynobj;
013dec1a
ILT
949 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
950 sym_hashes = elf_sym_hashes (input_bfd);
4fbc96ad
KR
951 local_got_offsets = elf_local_got_offsets (input_bfd);
952
953 sgot = NULL;
954 splt = NULL;
955 sreloc = NULL;
013dec1a
ILT
956
957 rel = relocs;
958 relend = relocs + input_section->reloc_count;
959 for (; rel < relend; rel++)
960 {
961 int r_type;
82b1edf7 962 reloc_howto_type *howto;
013dec1a
ILT
963 long r_symndx;
964 struct elf_link_hash_entry *h;
965 Elf_Internal_Sym *sym;
966 asection *sec;
967 bfd_vma relocation;
968 bfd_reloc_status_type r;
969
970 r_type = ELF32_R_TYPE (rel->r_info);
971 if (r_type < 0 || r_type >= (int) R_SPARC_max)
972 {
973 bfd_set_error (bfd_error_bad_value);
974 return false;
975 }
976 howto = elf_sparc_howto_table + r_type;
977
978 r_symndx = ELF32_R_SYM (rel->r_info);
979
980 if (info->relocateable)
981 {
982 /* This is a relocateable link. We don't have to change
983 anything, unless the reloc is against a section symbol,
984 in which case we have to adjust according to where the
985 section symbol winds up in the output section. */
986 if (r_symndx < symtab_hdr->sh_info)
987 {
988 sym = local_syms + r_symndx;
989 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
990 {
991 sec = local_sections[r_symndx];
992 rel->r_addend += sec->output_offset + sym->st_value;
993 }
994 }
995
996 continue;
997 }
998
999 /* This is a final link. */
1000 h = NULL;
1001 sym = NULL;
1002 sec = NULL;
1003 if (r_symndx < symtab_hdr->sh_info)
1004 {
1005 sym = local_syms + r_symndx;
1006 sec = local_sections[r_symndx];
1007 relocation = (sec->output_section->vma
1008 + sec->output_offset
1009 + sym->st_value);
1010 }
1011 else
1012 {
4fbc96ad 1013 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
f786a73f
ILT
1014 if (h->root.type == bfd_link_hash_defined
1015 || h->root.type == bfd_link_hash_defweak)
013dec1a
ILT
1016 {
1017 sec = h->root.u.def.section;
9b09a015
ILT
1018 if ((r_type == R_SPARC_WPLT30
1019 && h->plt_offset != (bfd_vma) -1)
1020 || ((r_type == R_SPARC_GOT10
1021 || r_type == R_SPARC_GOT13
1022 || r_type == R_SPARC_GOT22)
1023 && elf_hash_table (info)->dynamic_sections_created)
1024 || (info->shared
1025 && (input_section->flags & SEC_ALLOC) != 0
1026 && (r_type == R_SPARC_8
1027 || r_type == R_SPARC_16
1028 || r_type == R_SPARC_32
1029 || r_type == R_SPARC_DISP8
1030 || r_type == R_SPARC_DISP16
1031 || r_type == R_SPARC_DISP32
1032 || r_type == R_SPARC_WDISP30
1033 || r_type == R_SPARC_WDISP22
1034 || r_type == R_SPARC_HI22
1035 || r_type == R_SPARC_22
1036 || r_type == R_SPARC_13
1037 || r_type == R_SPARC_LO10
1038 || r_type == R_SPARC_UA32
1039 || ((r_type == R_SPARC_PC10
1040 || r_type == R_SPARC_PC22)
1041 && strcmp (h->root.root.string,
1042 "_GLOBAL_OFFSET_TABLE_") != 0))))
1043 {
1044 /* In these cases, we don't need the relocation
1045 value. We check specially because in some
1046 obscure cases sec->output_section will be NULL. */
1047 relocation = 0;
1048 }
1049 else
1050 relocation = (h->root.u.def.value
1051 + sec->output_section->vma
1052 + sec->output_offset);
013dec1a 1053 }
f786a73f 1054 else if (h->root.type == bfd_link_hash_undefweak)
013dec1a 1055 relocation = 0;
4fbc96ad
KR
1056 else if (info->shared)
1057 relocation = 0;
013dec1a
ILT
1058 else
1059 {
1060 if (! ((*info->callbacks->undefined_symbol)
1061 (info, h->root.root.string, input_bfd,
1062 input_section, rel->r_offset)))
1063 return false;
1064 relocation = 0;
1065 }
1066 }
1067
4fbc96ad
KR
1068 switch (r_type)
1069 {
1070 case R_SPARC_GOT10:
1071 case R_SPARC_GOT13:
1072 case R_SPARC_GOT22:
1073 /* Relocation is to the entry for this symbol in the global
1074 offset table. */
1075 if (sgot == NULL)
1076 {
1077 sgot = bfd_get_section_by_name (dynobj, ".got");
1078 BFD_ASSERT (sgot != NULL);
1079 }
1080
1081 if (h != NULL)
1082 {
12662be4
ILT
1083 bfd_vma off;
1084
1085 off = h->got_offset;
1086 BFD_ASSERT (off != (bfd_vma) -1);
1087
1088 if (! elf_hash_table (info)->dynamic_sections_created)
1089 {
1090 /* This is actually a static link. We must
1091 initialize this entry in the global offset table.
1092 Since the offset must always be a multiple of 4,
1093 we use the least significant bit to record
1094 whether we have initialized it already.
1095
1096 When doing a dynamic link, we create a .rela.got
1097 relocation entry to initialize the value. This
1098 is done in the finish_dynamic_symbol routine. */
1099 if ((off & 1) != 0)
1100 off &= ~1;
1101 else
1102 {
1103 bfd_put_32 (output_bfd, relocation,
1104 sgot->contents + off);
1105 h->got_offset |= 1;
1106 }
1107 }
1108
1109 relocation = sgot->output_offset + off;
4fbc96ad
KR
1110 }
1111 else
1112 {
1113 bfd_vma off;
1114
1115 BFD_ASSERT (local_got_offsets != NULL
1116 && local_got_offsets[r_symndx] != (bfd_vma) -1);
1117
1118 off = local_got_offsets[r_symndx];
1119
1120 /* The offset must always be a multiple of 4. We use
12662be4
ILT
1121 the least significant bit to record whether we have
1122 already processed this entry. */
4fbc96ad
KR
1123 if ((off & 1) != 0)
1124 off &= ~1;
1125 else
1126 {
4fbc96ad
KR
1127 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
1128
12662be4
ILT
1129 if (info->shared)
1130 {
1131 asection *srelgot;
1132 Elf_Internal_Rela outrel;
1133
1134 /* We need to generate a R_SPARC_RELATIVE reloc
1135 for the dynamic linker. */
1136 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
1137 BFD_ASSERT (srelgot != NULL);
1138
1139 outrel.r_offset = (sgot->output_section->vma
1140 + sgot->output_offset
1141 + off);
1142 outrel.r_info = ELF32_R_INFO (0, R_SPARC_RELATIVE);
1143 outrel.r_addend = 0;
1144 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1145 (((Elf32_External_Rela *)
1146 srelgot->contents)
1147 + srelgot->reloc_count));
1148 ++srelgot->reloc_count;
1149 }
4fbc96ad
KR
1150
1151 local_got_offsets[r_symndx] |= 1;
1152 }
1153
1154 relocation = sgot->output_offset + off;
1155 }
1156
1157 break;
1158
1159 case R_SPARC_WPLT30:
1160 /* Relocation is to the entry for this symbol in the
1161 procedure linkage table. */
12662be4
ILT
1162 BFD_ASSERT (h != NULL);
1163
1164 if (h->plt_offset == (bfd_vma) -1)
1165 {
1166 /* We didn't make a PLT entry for this symbol. This
1167 happens when statically linking PIC code. */
1168 break;
1169 }
1170
4fbc96ad
KR
1171 if (splt == NULL)
1172 {
1173 splt = bfd_get_section_by_name (dynobj, ".plt");
1174 BFD_ASSERT (splt != NULL);
1175 }
1176
4fbc96ad
KR
1177 relocation = (splt->output_section->vma
1178 + splt->output_offset
1179 + h->plt_offset);
1180 break;
1181
1182 case R_SPARC_PC10:
1183 case R_SPARC_PC22:
1184 if (h != NULL
1185 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
1186 break;
1187 /* Fall through. */
1188 case R_SPARC_8:
1189 case R_SPARC_16:
1190 case R_SPARC_32:
1191 case R_SPARC_DISP8:
1192 case R_SPARC_DISP16:
1193 case R_SPARC_DISP32:
1194 case R_SPARC_WDISP30:
1195 case R_SPARC_WDISP22:
1196 case R_SPARC_HI22:
1197 case R_SPARC_22:
1198 case R_SPARC_13:
1199 case R_SPARC_LO10:
1200 case R_SPARC_UA32:
1201 if (info->shared
1202 && (input_section->flags & SEC_ALLOC) != 0)
1203 {
1204 Elf_Internal_Rela outrel;
1205
1206 /* When generating a shared object, these relocations
1207 are copied into the output file to be resolved at run
1208 time. */
1209
1210 if (sreloc == NULL)
1211 {
1212 const char *name;
1213
1214 name = (elf_string_from_elf_section
1215 (input_bfd,
1216 elf_elfheader (input_bfd)->e_shstrndx,
1217 elf_section_data (input_section)->rel_hdr.sh_name));
1218 if (name == NULL)
1219 return false;
1220
1221 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
1222 && strcmp (bfd_get_section_name (input_bfd,
1223 input_section),
1224 name + 5) == 0);
1225
1226 sreloc = bfd_get_section_by_name (dynobj, name);
1227 BFD_ASSERT (sreloc != NULL);
1228 }
1229
1230 outrel.r_offset = (rel->r_offset
1231 + input_section->output_section->vma
1232 + input_section->output_offset);
1233 if (h != NULL)
1234 {
12662be4 1235 BFD_ASSERT (h->dynindx != -1);
4fbc96ad 1236 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
14cac507 1237 outrel.r_addend = rel->r_addend;
4fbc96ad
KR
1238 }
1239 else
1240 {
14cac507 1241 if (r_type == R_SPARC_32)
4fbc96ad 1242 {
14cac507
ILT
1243 outrel.r_info = ELF32_R_INFO (0, R_SPARC_RELATIVE);
1244 outrel.r_addend = relocation + rel->r_addend;
4fbc96ad
KR
1245 }
1246 else
1247 {
14cac507
ILT
1248 long indx;
1249
1250 sym = local_syms + r_symndx;
1251
1252 BFD_ASSERT (ELF_ST_TYPE (sym->st_info) == STT_SECTION);
1253
1254 sec = local_sections[r_symndx];
1255 if (sec != NULL && bfd_is_abs_section (sec))
1256 indx = 0;
1257 else if (sec == NULL || sec->owner == NULL)
1258 {
1259 bfd_set_error (bfd_error_bad_value);
1260 return false;
1261 }
1262 else
1263 {
1264 asection *osec;
1265
1266 osec = sec->output_section;
1267 indx = elf_section_data (osec)->dynindx;
1268 if (indx == 0)
1269 abort ();
1270 }
1271
1272 outrel.r_info = ELF32_R_INFO (indx, r_type);
1273 outrel.r_addend = relocation + rel->r_addend;
4fbc96ad 1274 }
4fbc96ad
KR
1275 }
1276
1277 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1278 (((Elf32_External_Rela *)
1279 sreloc->contents)
1280 + sreloc->reloc_count));
1281 ++sreloc->reloc_count;
1282
1283 /* This reloc will be computed at runtime, so there's no
1284 need to do anything now. */
1285 continue;
1286 }
1287
1288 default:
1289 break;
1290 }
1291
013dec1a
ILT
1292 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1293 contents, rel->r_offset,
1294 relocation, rel->r_addend);
1295
1296 if (r != bfd_reloc_ok)
1297 {
1298 switch (r)
1299 {
1300 default:
1301 case bfd_reloc_outofrange:
1302 abort ();
1303 case bfd_reloc_overflow:
1304 {
1305 const char *name;
1306
1307 if (h != NULL)
1308 name = h->root.root.string;
1309 else
1310 {
12662be4
ILT
1311 name = elf_string_from_elf_section (input_bfd,
1312 symtab_hdr->sh_link,
1313 sym->st_name);
013dec1a
ILT
1314 if (name == NULL)
1315 return false;
1316 if (*name == '\0')
1317 name = bfd_section_name (input_bfd, sec);
1318 }
1319 if (! ((*info->callbacks->reloc_overflow)
1320 (info, name, howto->name, (bfd_vma) 0,
1321 input_bfd, input_section, rel->r_offset)))
1322 return false;
1323 }
1324 break;
1325 }
1326 }
1327 }
1328
1329 return true;
1330}
1331
1332/* Finish up dynamic symbol handling. We set the contents of various
1333 dynamic sections here. */
1334
1335static boolean
1336elf32_sparc_finish_dynamic_symbol (output_bfd, info, h, sym)
1337 bfd *output_bfd;
1338 struct bfd_link_info *info;
1339 struct elf_link_hash_entry *h;
1340 Elf_Internal_Sym *sym;
1341{
4fbc96ad 1342 bfd *dynobj;
013dec1a 1343
4fbc96ad 1344 dynobj = elf_hash_table (info)->dynobj;
013dec1a 1345
4fbc96ad 1346 if (h->plt_offset != (bfd_vma) -1)
013dec1a
ILT
1347 {
1348 asection *splt;
1349 asection *srela;
1350 Elf_Internal_Rela rela;
1351
4fbc96ad
KR
1352 /* This symbol has an entry in the procedure linkage table. Set
1353 it up. */
1354
1355 BFD_ASSERT (h->dynindx != -1);
1356
1357 splt = bfd_get_section_by_name (dynobj, ".plt");
1358 srela = bfd_get_section_by_name (dynobj, ".rela.plt");
1359 BFD_ASSERT (splt != NULL && srela != NULL);
013dec1a
ILT
1360
1361 /* Fill in the entry in the procedure linkage table. */
1362 bfd_put_32 (output_bfd,
4fbc96ad
KR
1363 PLT_ENTRY_WORD0 + h->plt_offset,
1364 splt->contents + h->plt_offset);
013dec1a
ILT
1365 bfd_put_32 (output_bfd,
1366 (PLT_ENTRY_WORD1
4fbc96ad
KR
1367 + (((- (h->plt_offset + 4)) >> 2) & 0x3fffff)),
1368 splt->contents + h->plt_offset + 4);
013dec1a 1369 bfd_put_32 (output_bfd, PLT_ENTRY_WORD2,
4fbc96ad 1370 splt->contents + h->plt_offset + 8);
013dec1a
ILT
1371
1372 /* Fill in the entry in the .rela.plt section. */
1373 rela.r_offset = (splt->output_section->vma
1374 + splt->output_offset
4fbc96ad 1375 + h->plt_offset);
013dec1a
ILT
1376 rela.r_info = ELF32_R_INFO (h->dynindx, R_SPARC_JMP_SLOT);
1377 rela.r_addend = 0;
1378 bfd_elf32_swap_reloca_out (output_bfd, &rela,
1379 ((Elf32_External_Rela *) srela->contents
4fbc96ad
KR
1380 + h->plt_offset / PLT_ENTRY_SIZE - 4));
1381
1382 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1383 {
1384 /* Mark the symbol as undefined, rather than as defined in
1385 the .plt section. Leave the value alone. */
1386 sym->st_shndx = SHN_UNDEF;
1387 }
1388 }
1389
1390 if (h->got_offset != (bfd_vma) -1)
1391 {
1392 asection *sgot;
1393 asection *srela;
1394 Elf_Internal_Rela rela;
1395
1396 /* This symbol has an entry in the global offset table. Set it
1397 up. */
013dec1a 1398
4fbc96ad
KR
1399 BFD_ASSERT (h->dynindx != -1);
1400
1401 sgot = bfd_get_section_by_name (dynobj, ".got");
1402 srela = bfd_get_section_by_name (dynobj, ".rela.got");
1403 BFD_ASSERT (sgot != NULL && srela != NULL);
1404
1405 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got_offset);
1406
1407 rela.r_offset = (sgot->output_section->vma
1408 + sgot->output_offset
1409 + h->got_offset);
1410 rela.r_info = ELF32_R_INFO (h->dynindx, R_SPARC_GLOB_DAT);
1411 rela.r_addend = 0;
1412 bfd_elf32_swap_reloca_out (output_bfd, &rela,
1413 ((Elf32_External_Rela *) srela->contents
1414 + srela->reloc_count));
1415 ++srela->reloc_count;
013dec1a 1416 }
4fbc96ad
KR
1417
1418 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
013dec1a 1419 {
4fbc96ad
KR
1420 asection *s;
1421 Elf_Internal_Rela rela;
013dec1a 1422
4fbc96ad 1423 /* This symbols needs a copy reloc. Set it up. */
013dec1a 1424
4fbc96ad
KR
1425 BFD_ASSERT (h->dynindx != -1);
1426
1427 s = bfd_get_section_by_name (h->root.u.def.section->owner,
1428 ".rela.bss");
1429 BFD_ASSERT (s != NULL);
1430
1431 rela.r_offset = (h->root.u.def.value
1432 + h->root.u.def.section->output_section->vma
1433 + h->root.u.def.section->output_offset);
1434 rela.r_info = ELF32_R_INFO (h->dynindx, R_SPARC_COPY);
1435 rela.r_addend = 0;
1436 bfd_elf32_swap_reloca_out (output_bfd, &rela,
1437 ((Elf32_External_Rela *) s->contents
1438 + s->reloc_count));
1439 ++s->reloc_count;
013dec1a
ILT
1440 }
1441
4fbc96ad
KR
1442 /* Mark some specially defined symbols as absolute. */
1443 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
1444 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
1445 || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0)
1446 sym->st_shndx = SHN_ABS;
1447
013dec1a
ILT
1448 return true;
1449}
1450
1451/* Finish up the dynamic sections. */
1452
1453static boolean
1454elf32_sparc_finish_dynamic_sections (output_bfd, info)
1455 bfd *output_bfd;
1456 struct bfd_link_info *info;
1457{
4fbc96ad 1458 bfd *dynobj;
013dec1a 1459 asection *sdyn;
12662be4 1460 asection *sgot;
013dec1a 1461
4fbc96ad
KR
1462 dynobj = elf_hash_table (info)->dynobj;
1463
4fbc96ad 1464 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
013dec1a 1465
12662be4 1466 if (elf_hash_table (info)->dynamic_sections_created)
013dec1a 1467 {
12662be4
ILT
1468 asection *splt;
1469 Elf32_External_Dyn *dyncon, *dynconend;
013dec1a 1470
12662be4
ILT
1471 splt = bfd_get_section_by_name (dynobj, ".plt");
1472 BFD_ASSERT (splt != NULL && sdyn != NULL);
013dec1a 1473
12662be4
ILT
1474 dyncon = (Elf32_External_Dyn *) sdyn->contents;
1475 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
1476 for (; dyncon < dynconend; dyncon++)
013dec1a 1477 {
12662be4
ILT
1478 Elf_Internal_Dyn dyn;
1479 const char *name;
1480 boolean size;
013dec1a 1481
12662be4 1482 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
013dec1a 1483
12662be4
ILT
1484 switch (dyn.d_tag)
1485 {
1486 case DT_PLTGOT: name = ".plt"; size = false; break;
1487 case DT_PLTRELSZ: name = ".rela.plt"; size = true; break;
1488 case DT_JMPREL: name = ".rela.plt"; size = false; break;
1489 default: name = NULL; size = false; break;
1490 }
1491
1492 if (name != NULL)
013dec1a 1493 {
12662be4
ILT
1494 asection *s;
1495
1496 s = bfd_get_section_by_name (output_bfd, name);
14cac507
ILT
1497 if (s == NULL)
1498 dyn.d_un.d_val = 0;
013dec1a 1499 else
12662be4 1500 {
14cac507
ILT
1501 if (! size)
1502 dyn.d_un.d_ptr = s->vma;
12662be4 1503 else
14cac507
ILT
1504 {
1505 if (s->_cooked_size != 0)
1506 dyn.d_un.d_val = s->_cooked_size;
1507 else
1508 dyn.d_un.d_val = s->_raw_size;
1509 }
12662be4
ILT
1510 }
1511 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
013dec1a 1512 }
013dec1a 1513 }
013dec1a 1514
12662be4
ILT
1515 /* Clear the first four entries in the procedure linkage table,
1516 and put a nop in the last four bytes. */
1517 if (splt->_raw_size > 0)
1518 {
1519 memset (splt->contents, 0, 4 * PLT_ENTRY_SIZE);
1520 bfd_put_32 (output_bfd, SPARC_NOP,
1521 splt->contents + splt->_raw_size - 4);
1522 }
1523
1524 elf_section_data (splt->output_section)->this_hdr.sh_entsize =
1525 PLT_ENTRY_SIZE;
013dec1a
ILT
1526 }
1527
1528 /* Set the first entry in the global offset table to the address of
1529 the dynamic section. */
12662be4
ILT
1530 sgot = bfd_get_section_by_name (dynobj, ".got");
1531 BFD_ASSERT (sgot != NULL);
013dec1a 1532 if (sgot->_raw_size > 0)
12662be4
ILT
1533 {
1534 if (sdyn == NULL)
1535 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
1536 else
1537 bfd_put_32 (output_bfd,
1538 sdyn->output_section->vma + sdyn->output_offset,
1539 sgot->contents);
1540 }
013dec1a
ILT
1541
1542 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
013dec1a 1543
14cac507
ILT
1544 if (info->shared)
1545 {
1546 asection *sdynsym;
1547 asection *s;
1548 Elf_Internal_Sym sym;
1549
1550 /* Set up the section symbols for the output sections. */
1551
1552 sdynsym = bfd_get_section_by_name (dynobj, ".dynsym");
1553 BFD_ASSERT (sdynsym != NULL);
1554
1555 sym.st_size = 0;
1556 sym.st_name = 0;
1557 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
1558 sym.st_other = 0;
1559
1560 for (s = output_bfd->sections; s != NULL; s = s->next)
1561 {
1562 int indx;
1563
1564 sym.st_value = s->vma;
1565
1566 indx = elf_section_data (s)->this_idx;
1567 BFD_ASSERT (indx > 0);
1568 sym.st_shndx = indx;
1569
1570 bfd_elf32_swap_symbol_out (output_bfd, &sym,
1571 ((Elf32_External_Sym *) sdynsym->contents
1572 + elf_section_data (s)->dynindx));
1573 }
1574
1575 /* Set the sh_info field of the output .dynsym section to the
1576 index of the first global symbol. */
1577 elf_section_data (sdynsym->output_section)->this_hdr.sh_info =
1578 bfd_count_sections (output_bfd) + 1;
1579 }
1580
013dec1a
ILT
1581 return true;
1582}
1583
32090b8e
KR
1584#define TARGET_BIG_SYM bfd_elf32_sparc_vec
1585#define TARGET_BIG_NAME "elf32-sparc"
1586#define ELF_ARCH bfd_arch_sparc
013dec1a
ILT
1587#define ELF_MACHINE_CODE EM_SPARC
1588#define ELF_MAXPAGESIZE 0x10000
1589#define elf_backend_create_dynamic_sections \
1590 elf32_sparc_create_dynamic_sections
4fbc96ad 1591#define elf_backend_check_relocs elf32_sparc_check_relocs
013dec1a
ILT
1592#define elf_backend_adjust_dynamic_symbol \
1593 elf32_sparc_adjust_dynamic_symbol
1594#define elf_backend_size_dynamic_sections \
1595 elf32_sparc_size_dynamic_sections
1596#define elf_backend_relocate_section elf32_sparc_relocate_section
1597#define elf_backend_finish_dynamic_symbol \
1598 elf32_sparc_finish_dynamic_symbol
1599#define elf_backend_finish_dynamic_sections \
1600 elf32_sparc_finish_dynamic_sections
32090b8e
KR
1601
1602#include "elf32-target.h"
This page took 0.338537 seconds and 4 git commands to generate.