Coverage Report

Created: 2026-03-10 08:46

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