]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elf64-sparc.c
Update year range in copyright notice of binutils files
[thirdparty/binutils-gdb.git] / bfd / elf64-sparc.c
CommitLineData
252b5132 1/* SPARC-specific support for 64-bit ELF
fd67aa11 2 Copyright (C) 1993-2024 Free Software Foundation, Inc.
252b5132 3
ae9a127f 4 This file is part of BFD, the Binary File Descriptor library.
252b5132 5
ae9a127f
NC
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
cd123cb7 8 the Free Software Foundation; either version 3 of the License, or
ae9a127f 9 (at your option) any later version.
252b5132 10
ae9a127f
NC
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.
252b5132 15
ae9a127f
NC
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
cd123cb7
NC
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
252b5132 20
252b5132 21#include "sysdep.h"
7a6e0d89 22#include <limits.h>
3db64b00 23#include "bfd.h"
252b5132
RH
24#include "libbfd.h"
25#include "elf-bfd.h"
252b5132 26#include "elf/sparc.h"
40937810 27#include "opcode/sparc.h"
22b75d0a 28#include "elfxx-sparc.h"
252b5132
RH
29
30/* In case we're on a 32-bit machine, construct a 64-bit "-1" value. */
31#define MINUS_ONE (~ (bfd_vma) 0)
32
f65054f7
RH
33/* Due to the way how we handle R_SPARC_OLO10, each entry in a SHT_RELA
34 section can represent up to two relocs, we must tell the user to allocate
35 more space. */
435b1e90 36
f65054f7 37static long
22b75d0a 38elf64_sparc_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
f65054f7 39{
a3eb71ad
AM
40 size_t count, raw;
41
42 count = sec->reloc_count;
43 if (count >= LONG_MAX / 2 / sizeof (arelent *)
44 || _bfd_mul_overflow (count, sizeof (Elf64_External_Rela), &raw))
7a6e0d89
AM
45 {
46 bfd_set_error (bfd_error_file_too_big);
47 return -1;
48 }
a3eb71ad
AM
49 if (!bfd_write_p (abfd))
50 {
51 ufile_ptr filesize = bfd_get_file_size (abfd);
52 if (filesize != 0 && raw > filesize)
53 {
54 bfd_set_error (bfd_error_file_truncated);
55 return -1;
56 }
57 }
58 return (count * 2 + 1) * sizeof (arelent *);
f65054f7
RH
59}
60
61static long
22b75d0a 62elf64_sparc_get_dynamic_reloc_upper_bound (bfd *abfd)
f65054f7 63{
7a6e0d89
AM
64 long ret = _bfd_elf_get_dynamic_reloc_upper_bound (abfd);
65 if (ret > LONG_MAX / 2)
66 {
67 bfd_set_error (bfd_error_file_too_big);
68 ret = -1;
69 }
70 else if (ret > 0)
71 ret *= 2;
72 return ret;
f65054f7
RH
73}
74
435b1e90 75/* Read relocations for ASECT from REL_HDR. There are RELOC_COUNT of
f65054f7
RH
76 them. We cannot use generic elf routines for this, because R_SPARC_OLO10
77 has secondary addend in ELF64_R_TYPE_DATA. We handle it as two relocations
78 for the same location, R_SPARC_LO10 and R_SPARC_13. */
79
0a1b45a2 80static bool
22b75d0a
DM
81elf64_sparc_slurp_one_reloc_table (bfd *abfd, asection *asect,
82 Elf_Internal_Shdr *rel_hdr,
0a1b45a2 83 asymbol **symbols, bool dynamic)
f65054f7 84{
2c3fc389 85 void * allocated = NULL;
f65054f7
RH
86 bfd_byte *native_relocs;
87 arelent *relent;
88 unsigned int i;
89 int entsize;
90 bfd_size_type count;
91 arelent *relents;
92
2bb3687b 93 if (bfd_seek (abfd, rel_hdr->sh_offset, SEEK_SET) != 0)
0a1b45a2 94 return false;
2bb3687b 95 allocated = _bfd_malloc_and_read (abfd, rel_hdr->sh_size, rel_hdr->sh_size);
f65054f7 96 if (allocated == NULL)
0a1b45a2 97 return false;
f65054f7
RH
98
99 native_relocs = (bfd_byte *) allocated;
100
3e1d7f19 101 relents = asect->relocation + canon_reloc_count (asect);
f65054f7
RH
102
103 entsize = rel_hdr->sh_entsize;
104 BFD_ASSERT (entsize == sizeof (Elf64_External_Rela));
435b1e90 105
f65054f7
RH
106 count = rel_hdr->sh_size / entsize;
107
108 for (i = 0, relent = relents; i < count;
109 i++, relent++, native_relocs += entsize)
110 {
111 Elf_Internal_Rela rela;
22b75d0a 112 unsigned int r_type;
f65054f7 113
947216bf 114 bfd_elf64_swap_reloca_in (abfd, native_relocs, &rela);
f65054f7
RH
115
116 /* The address of an ELF reloc is section relative for an object
117 file, and absolute for an executable file or shared library.
118 The address of a normal BFD reloc is always section relative,
119 and the address of a dynamic reloc is absolute.. */
120 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0 || dynamic)
121 relent->address = rela.r_offset;
122 else
123 relent->address = rela.r_offset - asect->vma;
124
8410d65b 125 if (ELF64_R_SYM (rela.r_info) == STN_UNDEF)
f65054f7 126 relent->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
8410d65b 127 else if (/* PR 17512: file: 996185f8. */
4b24dd1a
AM
128 ELF64_R_SYM (rela.r_info) > (dynamic
129 ? bfd_get_dynamic_symcount (abfd)
130 : bfd_get_symcount (abfd)))
131 {
132 _bfd_error_handler
8410d65b
JM
133 /* xgettext:c-format */
134 (_("%pB(%pA): relocation %d has invalid symbol index %ld"),
135 abfd, asect, i, (long) ELF64_R_SYM (rela.r_info));
136 bfd_set_error (bfd_error_bad_value);
137 relent->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
4b24dd1a 138 }
f65054f7
RH
139 else
140 {
141 asymbol **ps, *s;
142
143 ps = symbols + ELF64_R_SYM (rela.r_info) - 1;
144 s = *ps;
145
146 /* Canonicalize ELF section symbols. FIXME: Why? */
147 if ((s->flags & BSF_SECTION_SYM) == 0)
148 relent->sym_ptr_ptr = ps;
149 else
150 relent->sym_ptr_ptr = s->section->symbol_ptr_ptr;
151 }
152
153 relent->addend = rela.r_addend;
154
22b75d0a
DM
155 r_type = ELF64_R_TYPE_ID (rela.r_info);
156 if (r_type == R_SPARC_OLO10)
f65054f7 157 {
0aa13fee 158 relent->howto = _bfd_sparc_elf_info_to_howto_ptr (abfd, R_SPARC_LO10);
f65054f7
RH
159 relent[1].address = relent->address;
160 relent++;
161 relent->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
162 relent->addend = ELF64_R_TYPE_DATA (rela.r_info);
0aa13fee 163 relent->howto = _bfd_sparc_elf_info_to_howto_ptr (abfd, R_SPARC_13);
f65054f7
RH
164 }
165 else
f3185997
NC
166 {
167 relent->howto = _bfd_sparc_elf_info_to_howto_ptr (abfd, r_type);
168 if (relent->howto == NULL)
169 goto error_return;
170 }
f65054f7
RH
171 }
172
3e1d7f19 173 canon_reloc_count (asect) += relent - relents;
f65054f7 174
c9594989 175 free (allocated);
0a1b45a2 176 return true;
f65054f7
RH
177
178 error_return:
c9594989 179 free (allocated);
0a1b45a2 180 return false;
f65054f7
RH
181}
182
183/* Read in and swap the external relocs. */
184
0a1b45a2 185static bool
22b75d0a 186elf64_sparc_slurp_reloc_table (bfd *abfd, asection *asect,
0a1b45a2 187 asymbol **symbols, bool dynamic)
f65054f7
RH
188{
189 struct bfd_elf_section_data * const d = elf_section_data (asect);
190 Elf_Internal_Shdr *rel_hdr;
191 Elf_Internal_Shdr *rel_hdr2;
dc810e39 192 bfd_size_type amt;
f65054f7
RH
193
194 if (asect->relocation != NULL)
0a1b45a2 195 return true;
f65054f7
RH
196
197 if (! dynamic)
198 {
199 if ((asect->flags & SEC_RELOC) == 0
200 || asect->reloc_count == 0)
0a1b45a2 201 return true;
f65054f7 202
d4730f92
BS
203 rel_hdr = d->rel.hdr;
204 rel_hdr2 = d->rela.hdr;
f65054f7 205
d4730f92 206 BFD_ASSERT ((rel_hdr && asect->rel_filepos == rel_hdr->sh_offset)
f65054f7
RH
207 || (rel_hdr2 && asect->rel_filepos == rel_hdr2->sh_offset));
208 }
209 else
210 {
211 /* Note that ASECT->RELOC_COUNT tends not to be accurate in this
212 case because relocations against this section may use the
213 dynamic symbol table, and in that case bfd_section_from_shdr
214 in elf.c does not update the RELOC_COUNT. */
eea6121a 215 if (asect->size == 0)
0a1b45a2 216 return true;
f65054f7
RH
217
218 rel_hdr = &d->this_hdr;
d9bc7a44 219 asect->reloc_count = NUM_SHDR_ENTRIES (rel_hdr);
f65054f7
RH
220 rel_hdr2 = NULL;
221 }
222
dc810e39
AM
223 amt = asect->reloc_count;
224 amt *= 2 * sizeof (arelent);
225 asect->relocation = (arelent *) bfd_alloc (abfd, amt);
f65054f7 226 if (asect->relocation == NULL)
0a1b45a2 227 return false;
f65054f7 228
22b75d0a 229 /* The elf64_sparc_slurp_one_reloc_table routine increments
3e1d7f19
JJ
230 canon_reloc_count. */
231 canon_reloc_count (asect) = 0;
435b1e90 232
d4730f92
BS
233 if (rel_hdr
234 && !elf64_sparc_slurp_one_reloc_table (abfd, asect, rel_hdr, symbols,
235 dynamic))
0a1b45a2 236 return false;
435b1e90
KH
237
238 if (rel_hdr2
22b75d0a 239 && !elf64_sparc_slurp_one_reloc_table (abfd, asect, rel_hdr2, symbols,
f65054f7 240 dynamic))
0a1b45a2 241 return false;
f65054f7 242
0a1b45a2 243 return true;
f65054f7
RH
244}
245
3e1d7f19
JJ
246/* Canonicalize the relocs. */
247
248static long
22b75d0a
DM
249elf64_sparc_canonicalize_reloc (bfd *abfd, sec_ptr section,
250 arelent **relptr, asymbol **symbols)
3e1d7f19
JJ
251{
252 arelent *tblptr;
253 unsigned int i;
9c5bfbb7 254 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3e1d7f19 255
0a1b45a2 256 if (! bed->s->slurp_reloc_table (abfd, section, symbols, false))
3e1d7f19
JJ
257 return -1;
258
259 tblptr = section->relocation;
260 for (i = 0; i < canon_reloc_count (section); i++)
261 *relptr++ = tblptr++;
262
263 *relptr = NULL;
264
265 return canon_reloc_count (section);
266}
267
268
f65054f7
RH
269/* Canonicalize the dynamic relocation entries. Note that we return
270 the dynamic relocations as a single block, although they are
271 actually associated with particular sections; the interface, which
272 was designed for SunOS style shared libraries, expects that there
273 is only one set of dynamic relocs. Any section that was actually
274 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
275 the dynamic symbol table, is considered to be a dynamic reloc
276 section. */
277
278static long
22b75d0a
DM
279elf64_sparc_canonicalize_dynamic_reloc (bfd *abfd, arelent **storage,
280 asymbol **syms)
f65054f7
RH
281{
282 asection *s;
283 long ret;
284
285 if (elf_dynsymtab (abfd) == 0)
286 {
287 bfd_set_error (bfd_error_invalid_operation);
288 return -1;
289 }
290
291 ret = 0;
292 for (s = abfd->sections; s != NULL; s = s->next)
293 {
294 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
295 && (elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
296 {
297 arelent *p;
298 long count, i;
299
0a1b45a2 300 if (! elf64_sparc_slurp_reloc_table (abfd, s, syms, true))
f65054f7 301 return -1;
3e1d7f19 302 count = canon_reloc_count (s);
f65054f7
RH
303 p = s->relocation;
304 for (i = 0; i < count; i++)
305 *storage++ = p++;
306 ret += count;
307 }
308 }
309
310 *storage = NULL;
311
312 return ret;
313}
314
db84b98a
JM
315/* Install a new set of internal relocs. */
316
317static void
318elf64_sparc_set_reloc (bfd *abfd ATTRIBUTE_UNUSED,
07d6d2b8
AM
319 asection *asect,
320 arelent **location,
321 unsigned int count)
db84b98a
JM
322{
323 asect->orelocation = location;
324 canon_reloc_count (asect) = count;
4f5c4fce
AM
325 if (count != 0)
326 asect->flags |= SEC_RELOC;
327 else
328 asect->flags &= ~SEC_RELOC;
db84b98a
JM
329}
330
f65054f7
RH
331/* Write out the relocs. */
332
333static void
2c3fc389 334elf64_sparc_write_relocs (bfd *abfd, asection *sec, void * data)
f65054f7 335{
0a1b45a2 336 bool *failedp = (bool *) data;
f65054f7 337 Elf_Internal_Shdr *rela_hdr;
22b75d0a 338 bfd_vma addr_offset;
37fb6db1 339 Elf64_External_Rela *outbound_relocas, *src_rela;
f65054f7
RH
340 unsigned int idx, count;
341 asymbol *last_sym = 0;
342 int last_sym_idx = 0;
343
344 /* If we have already failed, don't do anything. */
345 if (*failedp)
346 return;
347
348 if ((sec->flags & SEC_RELOC) == 0)
349 return;
350
351 /* The linker backend writes the relocs out itself, and sets the
352 reloc_count field to zero to inhibit writing them here. Also,
353 sometimes the SEC_RELOC flag gets set even when there aren't any
354 relocs. */
db84b98a 355 if (canon_reloc_count (sec) == 0)
f65054f7
RH
356 return;
357
358 /* We can combine two relocs that refer to the same address
359 into R_SPARC_OLO10 if first one is R_SPARC_LO10 and the
360 latter is R_SPARC_13 with no associated symbol. */
361 count = 0;
db84b98a 362 for (idx = 0; idx < canon_reloc_count (sec); idx++)
f65054f7
RH
363 {
364 bfd_vma addr;
f65054f7
RH
365
366 ++count;
367
368 addr = sec->orelocation[idx]->address;
369 if (sec->orelocation[idx]->howto->type == R_SPARC_LO10
db84b98a 370 && idx < canon_reloc_count (sec) - 1)
f65054f7
RH
371 {
372 arelent *r = sec->orelocation[idx + 1];
373
374 if (r->howto->type == R_SPARC_13
375 && r->address == addr
376 && bfd_is_abs_section ((*r->sym_ptr_ptr)->section)
377 && (*r->sym_ptr_ptr)->value == 0)
378 ++idx;
379 }
380 }
381
d4730f92 382 rela_hdr = elf_section_data (sec)->rela.hdr;
f65054f7
RH
383
384 rela_hdr->sh_size = rela_hdr->sh_entsize * count;
2c3fc389 385 rela_hdr->contents = bfd_alloc (abfd, rela_hdr->sh_size);
f65054f7
RH
386 if (rela_hdr->contents == NULL)
387 {
0a1b45a2 388 *failedp = true;
f65054f7
RH
389 return;
390 }
391
392 /* Figure out whether the relocations are RELA or REL relocations. */
393 if (rela_hdr->sh_type != SHT_RELA)
394 abort ();
395
22b75d0a
DM
396 /* The address of an ELF reloc is section relative for an object
397 file, and absolute for an executable file or shared library.
398 The address of a BFD reloc is always section relative. */
399 addr_offset = 0;
400 if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
401 addr_offset = sec->vma;
402
435b1e90 403 /* orelocation has the data, reloc_count has the count... */
f65054f7 404 outbound_relocas = (Elf64_External_Rela *) rela_hdr->contents;
37fb6db1 405 src_rela = outbound_relocas;
f65054f7 406
db84b98a 407 for (idx = 0; idx < canon_reloc_count (sec); idx++)
f65054f7
RH
408 {
409 Elf_Internal_Rela dst_rela;
f65054f7
RH
410 arelent *ptr;
411 asymbol *sym;
412 int n;
413
414 ptr = sec->orelocation[idx];
f65054f7
RH
415 sym = *ptr->sym_ptr_ptr;
416 if (sym == last_sym)
417 n = last_sym_idx;
418 else if (bfd_is_abs_section (sym->section) && sym->value == 0)
419 n = STN_UNDEF;
420 else
421 {
422 last_sym = sym;
423 n = _bfd_elf_symbol_from_bfd_symbol (abfd, &sym);
424 if (n < 0)
425 {
0a1b45a2 426 *failedp = true;
f65054f7
RH
427 return;
428 }
429 last_sym_idx = n;
430 }
431
432 if ((*ptr->sym_ptr_ptr)->the_bfd != NULL
433 && (*ptr->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec
434 && ! _bfd_elf_validate_reloc (abfd, ptr))
435 {
0a1b45a2 436 *failedp = true;
f65054f7
RH
437 return;
438 }
439
440 if (ptr->howto->type == R_SPARC_LO10
db84b98a 441 && idx < canon_reloc_count (sec) - 1)
f65054f7
RH
442 {
443 arelent *r = sec->orelocation[idx + 1];
444
445 if (r->howto->type == R_SPARC_13
446 && r->address == ptr->address
447 && bfd_is_abs_section ((*r->sym_ptr_ptr)->section)
448 && (*r->sym_ptr_ptr)->value == 0)
449 {
450 idx++;
451 dst_rela.r_info
452 = ELF64_R_INFO (n, ELF64_R_TYPE_INFO (r->addend,
453 R_SPARC_OLO10));
454 }
455 else
456 dst_rela.r_info = ELF64_R_INFO (n, R_SPARC_LO10);
457 }
458 else
459 dst_rela.r_info = ELF64_R_INFO (n, ptr->howto->type);
460
22b75d0a 461 dst_rela.r_offset = ptr->address + addr_offset;
f65054f7 462 dst_rela.r_addend = ptr->addend;
22b75d0a 463
947216bf 464 bfd_elf64_swap_reloca_out (abfd, &dst_rela, (bfd_byte *) src_rela);
37fb6db1 465 ++src_rela;
f65054f7 466 }
252b5132 467}
587ff49e 468\f
22b75d0a
DM
469/* Hook called by the linker routine which adds symbols from an object
470 file. We use it for STT_REGISTER symbols. */
40937810 471
0a1b45a2 472static bool
22b75d0a
DM
473elf64_sparc_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
474 Elf_Internal_Sym *sym, const char **namep,
475 flagword *flagsp ATTRIBUTE_UNUSED,
476 asection **secp ATTRIBUTE_UNUSED,
477 bfd_vma *valp ATTRIBUTE_UNUSED)
40937810 478{
22b75d0a 479 static const char *const stt_types[] = { "NOTYPE", "OBJECT", "FUNCTION" };
40937810 480
22b75d0a
DM
481 if (ELF_ST_TYPE (sym->st_info) == STT_REGISTER)
482 {
483 int reg;
484 struct _bfd_sparc_elf_app_reg *p;
40937810 485
22b75d0a
DM
486 reg = (int)sym->st_value;
487 switch (reg & ~1)
488 {
489 case 2: reg -= 2; break;
490 case 6: reg -= 4; break;
491 default:
4eca0228 492 _bfd_error_handler
38f14ab8 493 (_("%pB: only registers %%g[2367] can be declared using STT_REGISTER"),
07d6d2b8 494 abfd);
0a1b45a2 495 return false;
22b75d0a 496 }
40937810 497
f13a99db 498 if (info->output_bfd->xvec != abfd->xvec
22b75d0a 499 || (abfd->flags & DYNAMIC) != 0)
07d6d2b8 500 {
22b75d0a
DM
501 /* STT_REGISTER only works when linking an elf64_sparc object.
502 If STT_REGISTER comes from a dynamic object, don't put it into
503 the output bfd. The dynamic linker will recheck it. */
504 *namep = NULL;
0a1b45a2 505 return true;
07d6d2b8 506 }
40937810 507
22b75d0a 508 p = _bfd_sparc_elf_hash_table(info)->app_regs + reg;
40937810 509
22b75d0a
DM
510 if (p->name != NULL && strcmp (p->name, *namep))
511 {
4eca0228 512 _bfd_error_handler
695344c0 513 /* xgettext:c-format */
38f14ab8 514 (_("register %%g%d used incompatibly: %s in %pB,"
871b3ab2 515 " previously %s in %pB"),
c08bb8dd
AM
516 (int) sym->st_value, **namep ? *namep : "#scratch", abfd,
517 *p->name ? p->name : "#scratch", p->abfd);
0a1b45a2 518 return false;
22b75d0a 519 }
40937810 520
22b75d0a
DM
521 if (p->name == NULL)
522 {
523 if (**namep)
524 {
525 struct elf_link_hash_entry *h;
40937810 526
22b75d0a 527 h = (struct elf_link_hash_entry *)
0a1b45a2 528 bfd_link_hash_lookup (info->hash, *namep, false, false, false);
40937810 529
22b75d0a
DM
530 if (h != NULL)
531 {
532 unsigned char type = h->type;
40937810 533
22b75d0a
DM
534 if (type > STT_FUNC)
535 type = 0;
4eca0228 536 _bfd_error_handler
695344c0 537 /* xgettext:c-format */
38f14ab8 538 (_("symbol `%s' has differing types: REGISTER in %pB,"
871b3ab2 539 " previously %s in %pB"),
c08bb8dd 540 *namep, abfd, stt_types[type], p->abfd);
0a1b45a2 541 return false;
22b75d0a 542 }
40937810 543
22b75d0a
DM
544 p->name = bfd_hash_allocate (&info->hash->table,
545 strlen (*namep) + 1);
546 if (!p->name)
0a1b45a2 547 return false;
40937810 548
22b75d0a
DM
549 strcpy (p->name, *namep);
550 }
551 else
552 p->name = "";
553 p->bind = ELF_ST_BIND (sym->st_info);
554 p->abfd = abfd;
555 p->shndx = sym->st_shndx;
556 }
557 else
558 {
559 if (p->bind == STB_WEAK
560 && ELF_ST_BIND (sym->st_info) == STB_GLOBAL)
561 {
562 p->bind = STB_GLOBAL;
563 p->abfd = abfd;
564 }
565 }
566 *namep = NULL;
0a1b45a2 567 return true;
22b75d0a
DM
568 }
569 else if (*namep && **namep
f13a99db 570 && info->output_bfd->xvec == abfd->xvec)
22b75d0a
DM
571 {
572 int i;
573 struct _bfd_sparc_elf_app_reg *p;
40937810 574
22b75d0a
DM
575 p = _bfd_sparc_elf_hash_table(info)->app_regs;
576 for (i = 0; i < 4; i++, p++)
577 if (p->name != NULL && ! strcmp (p->name, *namep))
578 {
579 unsigned char type = ELF_ST_TYPE (sym->st_info);
40937810 580
22b75d0a
DM
581 if (type > STT_FUNC)
582 type = 0;
4eca0228 583 _bfd_error_handler
695344c0 584 /* xgettext:c-format */
871b3ab2
AM
585 (_("Symbol `%s' has differing types: %s in %pB,"
586 " previously REGISTER in %pB"),
c08bb8dd 587 *namep, stt_types[type], abfd, p->abfd);
0a1b45a2 588 return false;
22b75d0a
DM
589 }
590 }
0a1b45a2 591 return true;
40937810
JJ
592}
593
22b75d0a
DM
594/* This function takes care of emitting STT_REGISTER symbols
595 which we cannot easily keep in the symbol hash table. */
587ff49e 596
0a1b45a2 597static bool
22b75d0a
DM
598elf64_sparc_output_arch_syms (bfd *output_bfd ATTRIBUTE_UNUSED,
599 struct bfd_link_info *info,
2c3fc389
NC
600 void * flaginfo,
601 int (*func) (void *, const char *,
6e0b88f1
AM
602 Elf_Internal_Sym *,
603 asection *,
604 struct elf_link_hash_entry *))
587ff49e 605{
22b75d0a
DM
606 int reg;
607 struct _bfd_sparc_elf_app_reg *app_regs =
608 _bfd_sparc_elf_hash_table(info)->app_regs;
609 Elf_Internal_Sym sym;
40937810 610
22b75d0a
DM
611 for (reg = 0; reg < 4; reg++)
612 if (app_regs [reg].name != NULL)
613 {
614 if (info->strip == strip_some
615 && bfd_hash_lookup (info->keep_hash,
616 app_regs [reg].name,
0a1b45a2 617 false, false) == NULL)
22b75d0a 618 continue;
587ff49e 619
22b75d0a
DM
620 sym.st_value = reg < 2 ? reg + 2 : reg + 4;
621 sym.st_size = 0;
622 sym.st_other = 0;
623 sym.st_info = ELF_ST_INFO (app_regs [reg].bind, STT_REGISTER);
624 sym.st_shndx = app_regs [reg].shndx;
35fc36a8 625 sym.st_target_internal = 0;
57402f1e 626 if ((*func) (flaginfo, app_regs [reg].name, &sym,
6e0b88f1
AM
627 sym.st_shndx == SHN_ABS
628 ? bfd_abs_section_ptr : bfd_und_section_ptr,
629 NULL) != 1)
0a1b45a2 630 return false;
22b75d0a 631 }
435b1e90 632
0a1b45a2 633 return true;
22b75d0a 634}
40937810 635
22b75d0a
DM
636static int
637elf64_sparc_get_symbol_type (Elf_Internal_Sym *elf_sym, int type)
40937810 638{
22b75d0a
DM
639 if (ELF_ST_TYPE (elf_sym->st_info) == STT_REGISTER)
640 return STT_REGISTER;
641 else
642 return type;
40937810
JJ
643}
644
22b75d0a
DM
645/* A STB_GLOBAL,STT_REGISTER symbol should be BSF_GLOBAL
646 even in SHN_UNDEF section. */
587ff49e 647
22b75d0a
DM
648static void
649elf64_sparc_symbol_processing (bfd *abfd ATTRIBUTE_UNUSED, asymbol *asym)
587ff49e 650{
22b75d0a 651 elf_symbol_type *elfsym;
587ff49e 652
22b75d0a
DM
653 elfsym = (elf_symbol_type *) asym;
654 if (elfsym->internal_elf_sym.st_info
655 == ELF_ST_INFO (STB_GLOBAL, STT_REGISTER))
587ff49e 656 {
22b75d0a 657 asym->flags |= BSF_GLOBAL;
587ff49e 658 }
587ff49e 659}
a51a7930 660
22b75d0a
DM
661\f
662/* Functions for dealing with the e_flags field. */
a51a7930 663
22b75d0a
DM
664/* Merge backend specific data from an object file to the output
665 object file when linking. */
a51a7930 666
0a1b45a2 667static bool
50e03d47 668elf64_sparc_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
a51a7930 669{
50e03d47 670 bfd *obfd = info->output_bfd;
0a1b45a2 671 bool error;
22b75d0a
DM
672 flagword new_flags, old_flags;
673 int new_mm, old_mm;
40937810 674
22b75d0a
DM
675 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
676 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
0a1b45a2 677 return true;
40937810 678
22b75d0a
DM
679 new_flags = elf_elfheader (ibfd)->e_flags;
680 old_flags = elf_elfheader (obfd)->e_flags;
40937810 681
22b75d0a
DM
682 if (!elf_flags_init (obfd)) /* First call, no flags set */
683 {
0a1b45a2 684 elf_flags_init (obfd) = true;
22b75d0a
DM
685 elf_elfheader (obfd)->e_flags = new_flags;
686 }
40937810 687
22b75d0a
DM
688 else if (new_flags == old_flags) /* Compatible flags are ok */
689 ;
40937810 690
07d6d2b8 691 else /* Incompatible flags */
40937810 692 {
0a1b45a2 693 error = false;
40937810 694
22b75d0a
DM
695#define EF_SPARC_ISA_EXTENSIONS \
696 (EF_SPARC_SUN_US1 | EF_SPARC_SUN_US3 | EF_SPARC_HAL_R1)
19f7b010 697
37fb6db1
ILT
698 if ((ibfd->flags & DYNAMIC) != 0)
699 {
700 /* We don't want dynamic objects memory ordering and
701 architecture to have any role. That's what dynamic linker
702 should do. */
19f7b010 703 new_flags &= ~(EF_SPARCV9_MM | EF_SPARC_ISA_EXTENSIONS);
6c08d697 704 new_flags |= (old_flags
19f7b010 705 & (EF_SPARCV9_MM | EF_SPARC_ISA_EXTENSIONS));
37fb6db1
ILT
706 }
707 else
708 {
709 /* Choose the highest architecture requirements. */
19f7b010
JJ
710 old_flags |= (new_flags & EF_SPARC_ISA_EXTENSIONS);
711 new_flags |= (old_flags & EF_SPARC_ISA_EXTENSIONS);
712 if ((old_flags & (EF_SPARC_SUN_US1 | EF_SPARC_SUN_US3))
713 && (old_flags & EF_SPARC_HAL_R1))
37fb6db1 714 {
0a1b45a2 715 error = true;
4eca0228 716 _bfd_error_handler
871b3ab2 717 (_("%pB: linking UltraSPARC specific with HAL specific code"),
d003868e 718 ibfd);
37fb6db1
ILT
719 }
720 /* Choose the most restrictive memory ordering. */
721 old_mm = (old_flags & EF_SPARCV9_MM);
722 new_mm = (new_flags & EF_SPARCV9_MM);
723 old_flags &= ~EF_SPARCV9_MM;
724 new_flags &= ~EF_SPARCV9_MM;
725 if (new_mm < old_mm)
726 old_mm = new_mm;
727 old_flags |= old_mm;
728 new_flags |= old_mm;
729 }
252b5132
RH
730
731 /* Warn about any other mismatches */
732 if (new_flags != old_flags)
07d6d2b8 733 {
0a1b45a2 734 error = true;
4eca0228 735 _bfd_error_handler
695344c0 736 /* xgettext:c-format */
871b3ab2 737 (_("%pB: uses different e_flags (%#x) fields than previous modules (%#x)"),
07d6d2b8
AM
738 ibfd, new_flags, old_flags);
739 }
252b5132
RH
740
741 elf_elfheader (obfd)->e_flags = old_flags;
742
743 if (error)
07d6d2b8
AM
744 {
745 bfd_set_error (bfd_error_bad_value);
0a1b45a2 746 return false;
07d6d2b8 747 }
252b5132 748 }
50e03d47 749 return _bfd_sparc_elf_merge_private_bfd_data (ibfd, info);
252b5132 750}
0594c12d
AM
751
752/* MARCO: Set the correct entry size for the .stab section. */
753
0a1b45a2 754static bool
22b75d0a
DM
755elf64_sparc_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
756 Elf_Internal_Shdr *hdr ATTRIBUTE_UNUSED,
757 asection *sec)
0594c12d
AM
758{
759 const char *name;
760
fd361982 761 name = bfd_section_name (sec);
0594c12d
AM
762
763 if (strcmp (name, ".stab") == 0)
764 {
765 /* Even in the 64bit case the stab entries are only 12 bytes long. */
766 elf_section_data (sec)->this_hdr.sh_entsize = 12;
767 }
b34976b6 768
0a1b45a2 769 return true;
0594c12d 770}
587ff49e
RH
771\f
772/* Print a STT_REGISTER symbol to file FILE. */
252b5132 773
587ff49e 774static const char *
2c3fc389 775elf64_sparc_print_symbol_all (bfd *abfd ATTRIBUTE_UNUSED, void * filep,
22b75d0a 776 asymbol *symbol)
587ff49e
RH
777{
778 FILE *file = (FILE *) filep;
779 int reg, type;
435b1e90 780
587ff49e
RH
781 if (ELF_ST_TYPE (((elf_symbol_type *) symbol)->internal_elf_sym.st_info)
782 != STT_REGISTER)
783 return NULL;
784
785 reg = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
786 type = symbol->flags;
787 fprintf (file, "REG_%c%c%11s%c%c R", "GOLI" [reg / 8], '0' + (reg & 7), "",
788 ((type & BSF_LOCAL)
789 ? (type & BSF_GLOBAL) ? '!' : 'l'
07d6d2b8
AM
790 : (type & BSF_GLOBAL) ? 'g' : ' '),
791 (type & BSF_WEAK) ? 'w' : ' ');
587ff49e
RH
792 if (symbol->name == NULL || symbol->name [0] == '\0')
793 return "#scratch";
794 else
795 return symbol->name;
796}
252b5132 797\f
b2abe1bd
EB
798/* Used to decide how to sort relocs in an optimal manner for the
799 dynamic linker, before writing them out. */
800
40937810 801static enum elf_reloc_type_class
b2abe1bd 802elf64_sparc_reloc_type_class (const struct bfd_link_info *info,
7e612e98
AM
803 const asection *rel_sec ATTRIBUTE_UNUSED,
804 const Elf_Internal_Rela *rela)
40937810 805{
b2abe1bd
EB
806 bfd *abfd = info->output_bfd;
807 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
808 struct _bfd_sparc_elf_link_hash_table *htab
809 = _bfd_sparc_elf_hash_table (info);
810 BFD_ASSERT (htab != NULL);
811
812 if (htab->elf.dynsym != NULL
813 && htab->elf.dynsym->contents != NULL)
814 {
815 /* Check relocation against STT_GNU_IFUNC symbol if there are
816 dynamic symbols. */
817 unsigned long r_symndx = htab->r_symndx (rela->r_info);
818 if (r_symndx != STN_UNDEF)
819 {
820 Elf_Internal_Sym sym;
821 if (!bed->s->swap_symbol_in (abfd,
822 (htab->elf.dynsym->contents
823 + r_symndx * bed->s->sizeof_sym),
824 0, &sym))
825 abort ();
826
827 if (ELF_ST_TYPE (sym.st_info) == STT_GNU_IFUNC)
828 return reloc_class_ifunc;
829 }
830 }
831
40937810
JJ
832 switch ((int) ELF64_R_TYPE (rela->r_info))
833 {
b2abe1bd
EB
834 case R_SPARC_IRELATIVE:
835 return reloc_class_ifunc;
40937810
JJ
836 case R_SPARC_RELATIVE:
837 return reloc_class_relative;
838 case R_SPARC_JMP_SLOT:
839 return reloc_class_plt;
840 case R_SPARC_COPY:
841 return reloc_class_copy;
842 default:
843 return reloc_class_normal;
844 }
845}
846
f65054f7
RH
847/* Relocations in the 64 bit SPARC ELF ABI are more complex than in
848 standard ELF, because R_SPARC_OLO10 has secondary addend in
849 ELF64_R_TYPE_DATA field. This structure is used to redirect the
850 relocation handling routines. */
851
22b75d0a 852const struct elf_size_info elf64_sparc_size_info =
f65054f7
RH
853{
854 sizeof (Elf64_External_Ehdr),
855 sizeof (Elf64_External_Phdr),
856 sizeof (Elf64_External_Shdr),
857 sizeof (Elf64_External_Rel),
858 sizeof (Elf64_External_Rela),
859 sizeof (Elf64_External_Sym),
860 sizeof (Elf64_External_Dyn),
861 sizeof (Elf_External_Note),
ae9a127f
NC
862 4, /* hash-table entry size. */
863 /* Internal relocations per external relocations.
f65054f7
RH
864 For link purposes we use just 1 internal per
865 1 external, for assembly and slurp symbol table
435b1e90 866 we use 2. */
f65054f7 867 1,
ae9a127f 868 64, /* arch_size. */
45d6a902 869 3, /* log_file_align. */
f65054f7
RH
870 ELFCLASS64,
871 EV_CURRENT,
872 bfd_elf64_write_out_phdrs,
873 bfd_elf64_write_shdrs_and_ehdr,
1489a3a0 874 bfd_elf64_checksum_contents,
22b75d0a 875 elf64_sparc_write_relocs,
73ff0d56 876 bfd_elf64_swap_symbol_in,
f65054f7 877 bfd_elf64_swap_symbol_out,
22b75d0a 878 elf64_sparc_slurp_reloc_table,
f65054f7
RH
879 bfd_elf64_slurp_symbol_table,
880 bfd_elf64_swap_dyn_in,
881 bfd_elf64_swap_dyn_out,
947216bf
AM
882 bfd_elf64_swap_reloc_in,
883 bfd_elf64_swap_reloc_out,
884 bfd_elf64_swap_reloca_in,
885 bfd_elf64_swap_reloca_out
f65054f7
RH
886};
887
6d00b590 888#define TARGET_BIG_SYM sparc_elf64_vec
252b5132
RH
889#define TARGET_BIG_NAME "elf64-sparc"
890#define ELF_ARCH bfd_arch_sparc
891#define ELF_MAXPAGESIZE 0x100000
24718e3b 892#define ELF_COMMONPAGESIZE 0x2000
252b5132
RH
893
894/* This is the official ABI value. */
895#define ELF_MACHINE_CODE EM_SPARCV9
896
897/* This is the value that we used before the ABI was released. */
898#define ELF_MACHINE_ALT1 EM_OLD_SPARCV9
899
22b75d0a
DM
900#define elf_backend_reloc_type_class \
901 elf64_sparc_reloc_type_class
f65054f7 902#define bfd_elf64_get_reloc_upper_bound \
22b75d0a 903 elf64_sparc_get_reloc_upper_bound
f65054f7 904#define bfd_elf64_get_dynamic_reloc_upper_bound \
22b75d0a 905 elf64_sparc_get_dynamic_reloc_upper_bound
3e1d7f19 906#define bfd_elf64_canonicalize_reloc \
22b75d0a 907 elf64_sparc_canonicalize_reloc
f65054f7 908#define bfd_elf64_canonicalize_dynamic_reloc \
22b75d0a 909 elf64_sparc_canonicalize_dynamic_reloc
db84b98a
JM
910#define bfd_elf64_set_reloc \
911 elf64_sparc_set_reloc
22b75d0a
DM
912#define elf_backend_add_symbol_hook \
913 elf64_sparc_add_symbol_hook
914#define elf_backend_get_symbol_type \
915 elf64_sparc_get_symbol_type
916#define elf_backend_symbol_processing \
917 elf64_sparc_symbol_processing
918#define elf_backend_print_symbol_all \
919 elf64_sparc_print_symbol_all
920#define elf_backend_output_arch_syms \
921 elf64_sparc_output_arch_syms
922#define bfd_elf64_bfd_merge_private_bfd_data \
923 elf64_sparc_merge_private_bfd_data
924#define elf_backend_fake_sections \
925 elf64_sparc_fake_sections
926#define elf_backend_size_info \
927 elf64_sparc_size_info
928
929#define elf_backend_plt_sym_val \
930 _bfd_sparc_elf_plt_sym_val
931#define bfd_elf64_bfd_link_hash_table_create \
932 _bfd_sparc_elf_link_hash_table_create
933#define elf_info_to_howto \
934 _bfd_sparc_elf_info_to_howto
935#define elf_backend_copy_indirect_symbol \
936 _bfd_sparc_elf_copy_indirect_symbol
252b5132 937#define bfd_elf64_bfd_reloc_type_lookup \
22b75d0a 938 _bfd_sparc_elf_reloc_type_lookup
157090f7
AM
939#define bfd_elf64_bfd_reloc_name_lookup \
940 _bfd_sparc_elf_reloc_name_lookup
f7775d95 941#define bfd_elf64_bfd_relax_section \
22b75d0a 942 _bfd_sparc_elf_relax_section
f0abc2a1 943#define bfd_elf64_new_section_hook \
22b75d0a 944 _bfd_sparc_elf_new_section_hook
252b5132
RH
945
946#define elf_backend_create_dynamic_sections \
22b75d0a 947 _bfd_sparc_elf_create_dynamic_sections
13285a1b
AM
948#define elf_backend_relocs_compatible \
949 _bfd_elf_relocs_compatible
252b5132 950#define elf_backend_check_relocs \
22b75d0a 951 _bfd_sparc_elf_check_relocs
252b5132 952#define elf_backend_adjust_dynamic_symbol \
22b75d0a 953 _bfd_sparc_elf_adjust_dynamic_symbol
151e5294 954#define elf_backend_omit_section_dynsym \
22b75d0a 955 _bfd_sparc_elf_omit_section_dynsym
252b5132 956#define elf_backend_size_dynamic_sections \
22b75d0a 957 _bfd_sparc_elf_size_dynamic_sections
252b5132 958#define elf_backend_relocate_section \
22b75d0a 959 _bfd_sparc_elf_relocate_section
252b5132 960#define elf_backend_finish_dynamic_symbol \
22b75d0a 961 _bfd_sparc_elf_finish_dynamic_symbol
252b5132 962#define elf_backend_finish_dynamic_sections \
22b75d0a 963 _bfd_sparc_elf_finish_dynamic_sections
bb1dd176
QZ
964#define elf_backend_fixup_symbol \
965 _bfd_sparc_elf_fixup_symbol
252b5132 966
40937810 967#define bfd_elf64_mkobject \
22b75d0a 968 _bfd_sparc_elf_mkobject
252b5132 969#define elf_backend_object_p \
22b75d0a 970 _bfd_sparc_elf_object_p
40937810 971#define elf_backend_gc_mark_hook \
22b75d0a 972 _bfd_sparc_elf_gc_mark_hook
74541ad4
AM
973#define elf_backend_init_index_section \
974 _bfd_elf_init_1_index_section
252b5132 975
40937810
JJ
976#define elf_backend_can_gc_sections 1
977#define elf_backend_can_refcount 1
252b5132
RH
978#define elf_backend_want_got_plt 0
979#define elf_backend_plt_readonly 0
980#define elf_backend_want_plt_sym 1
40937810 981#define elf_backend_got_header_size 8
5474d94f 982#define elf_backend_want_dynrelro 1
f0fe0e16 983#define elf_backend_rela_normal 1
252b5132
RH
984
985/* Section 5.2.4 of the ABI specifies a 256-byte boundary for the table. */
986#define elf_backend_plt_alignment 8
987
252b5132 988#include "elf64-target.h"
71a75f6f
MF
989
990/* FreeBSD support */
991#undef TARGET_BIG_SYM
6d00b590 992#define TARGET_BIG_SYM sparc_elf64_fbsd_vec
71a75f6f
MF
993#undef TARGET_BIG_NAME
994#define TARGET_BIG_NAME "elf64-sparc-freebsd"
d1036acb
L
995#undef ELF_OSABI
996#define ELF_OSABI ELFOSABI_FREEBSD
71a75f6f 997
71a75f6f
MF
998#undef elf64_bed
999#define elf64_bed elf64_sparc_fbsd_bed
1000
1001#include "elf64-target.h"
1002
1360ba76
RO
1003/* Solaris 2. */
1004
1005#undef TARGET_BIG_SYM
6d00b590 1006#define TARGET_BIG_SYM sparc_elf64_sol2_vec
1360ba76
RO
1007#undef TARGET_BIG_NAME
1008#define TARGET_BIG_NAME "elf64-sparc-sol2"
1009
1010/* Restore default: we cannot use ELFOSABI_SOLARIS, otherwise ELFOSABI_NONE
1011 objects won't be recognized. */
1012#undef ELF_OSABI
1013
1014#undef elf64_bed
1015#define elf64_bed elf64_sparc_sol2_bed
1016
1017/* The 64-bit static TLS arena size is rounded to the nearest 16-byte
1018 boundary. */
1019#undef elf_backend_static_tls_alignment
1020#define elf_backend_static_tls_alignment 16
1021
168bb188
LB
1022#undef elf_backend_strtab_flags
1023#define elf_backend_strtab_flags SHF_STRINGS
1024
0a1b45a2 1025static bool
168bb188
LB
1026elf64_sparc_copy_solaris_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED,
1027 bfd *obfd ATTRIBUTE_UNUSED,
1028 const Elf_Internal_Shdr *isection ATTRIBUTE_UNUSED,
1029 Elf_Internal_Shdr *osection ATTRIBUTE_UNUSED)
1030{
1031 /* PR 19938: FIXME: Need to add code for setting the sh_info
1032 and sh_link fields of Solaris specific section types. */
0a1b45a2 1033 return false;
168bb188
LB
1034}
1035
1036#undef elf_backend_copy_special_section_fields
1037#define elf_backend_copy_special_section_fields elf64_sparc_copy_solaris_special_section_fields
1038
1360ba76 1039#include "elf64-target.h"
168bb188
LB
1040
1041#undef elf_backend_strtab_flags
1042#undef elf_backend_copy_special_section_fields