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