Coverage Report

Created: 2025-06-24 06:45

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