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