Coverage Report

Created: 2026-07-25 10:20

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
31
{
41
31
  size_t count, raw;
42
43
31
  count = sec->reloc_count;
44
31
  if (count >= LONG_MAX / 2 / sizeof (arelent *)
45
31
      || _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
31
  if (!bfd_write_p (abfd))
51
31
    {
52
31
      ufile_ptr filesize = bfd_get_file_size (abfd);
53
31
      if (filesize != 0 && raw > filesize)
54
0
  {
55
0
    bfd_set_error (bfd_error_file_truncated);
56
0
    return -1;
57
0
  }
58
31
    }
59
31
  return (count * 2 + 1) * sizeof (arelent *);
60
31
}
61
62
static long
63
elf64_sparc_get_dynamic_reloc_upper_bound (bfd *abfd)
64
6
{
65
6
  long ret = _bfd_elf_get_dynamic_reloc_upper_bound (abfd);
66
6
  if (ret > LONG_MAX / 2)
67
0
    {
68
0
      bfd_set_error (bfd_error_file_too_big);
69
0
      ret = -1;
70
0
    }
71
6
  else if (ret > 0)
72
0
    ret *= 2;
73
6
  return ret;
74
6
}
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
30
{
86
30
  void * allocated = NULL;
87
30
  bfd_byte *native_relocs;
88
30
  arelent *relent;
89
30
  unsigned int i;
90
30
  int entsize;
91
30
  bfd_size_type count;
92
30
  arelent *relents;
93
94
30
  if (bfd_seek (abfd, rel_hdr->sh_offset, SEEK_SET) != 0)
95
0
    return false;
96
30
  allocated = _bfd_malloc_and_read (abfd, rel_hdr->sh_size, rel_hdr->sh_size);
97
30
  if (allocated == NULL)
98
1
    return false;
99
100
29
  native_relocs = (bfd_byte *) allocated;
101
102
29
  relents = asect->relocation + canon_reloc_count (asect);
103
104
29
  entsize = rel_hdr->sh_entsize;
105
29
  BFD_ASSERT (entsize == sizeof (Elf64_External_Rela));
106
107
29
  count = rel_hdr->sh_size / entsize;
108
109
3.29k
  for (i = 0, relent = relents; i < count;
110
3.26k
       i++, relent++, native_relocs += entsize)
111
3.28k
    {
112
3.28k
      Elf_Internal_Rela rela;
113
3.28k
      unsigned int r_type;
114
115
3.28k
      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
3.28k
      if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0 || dynamic)
122
3.28k
  relent->address = rela.r_offset;
123
0
      else
124
0
  relent->address = rela.r_offset - asect->vma;
125
126
3.28k
      if (ELF64_R_SYM (rela.r_info) == STN_UNDEF)
127
239
  relent->sym_ptr_ptr = &bfd_abs_section_ptr->symbol;
128
3.04k
      else if (/* PR 17512: file: 996185f8.  */
129
3.04k
         ELF64_R_SYM (rela.r_info) > (dynamic
130
3.04k
              ? bfd_get_dynamic_symcount (abfd)
131
3.04k
              : bfd_get_symcount (abfd)))
132
2.15k
  {
133
2.15k
    _bfd_error_handler
134
      /* xgettext:c-format */
135
2.15k
      (_("%pB(%pA): relocation %d has invalid symbol index %ld"),
136
2.15k
       abfd, asect, i, (long) ELF64_R_SYM (rela.r_info));
137
2.15k
    bfd_set_error (bfd_error_bad_value);
138
2.15k
    relent->sym_ptr_ptr = &bfd_abs_section_ptr->symbol;
139
2.15k
  }
140
893
      else
141
893
  {
142
893
    asymbol **ps, *s;
143
144
893
    ps = symbols + ELF64_R_SYM (rela.r_info) - 1;
145
893
    s = *ps;
146
147
    /* Canonicalize ELF section symbols.  FIXME: Why?  */
148
893
    if ((s->flags & BSF_SECTION_SYM) == 0)
149
707
      relent->sym_ptr_ptr = ps;
150
186
    else
151
186
      relent->sym_ptr_ptr = &s->section->symbol;
152
893
  }
153
154
3.28k
      relent->addend = rela.r_addend;
155
156
3.28k
      r_type = ELF64_R_TYPE_ID (rela.r_info);
157
3.28k
      if (r_type == R_SPARC_OLO10)
158
10
  {
159
10
    relent->howto = _bfd_sparc_elf_info_to_howto_ptr (abfd, R_SPARC_LO10);
160
10
    relent[1].address = relent->address;
161
10
    relent++;
162
10
    relent->sym_ptr_ptr = &bfd_abs_section_ptr->symbol;
163
10
    relent->addend = ELF64_R_TYPE_DATA (rela.r_info);
164
10
    relent->howto = _bfd_sparc_elf_info_to_howto_ptr (abfd, R_SPARC_13);
165
10
  }
166
3.27k
      else
167
3.27k
  {
168
3.27k
    relent->howto = _bfd_sparc_elf_info_to_howto_ptr (abfd, r_type);
169
3.27k
    if (relent->howto == NULL)
170
24
      goto error_return;
171
3.27k
  }
172
3.28k
    }
173
174
5
  canon_reloc_count (asect) += relent - relents;
175
176
5
  free (allocated);
177
5
  return true;
178
179
24
 error_return:
180
24
  free (allocated);
181
24
  return false;
182
29
}
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
31
{
190
31
  struct bfd_elf_section_data * const d = elf_section_data (asect);
191
31
  Elf_Internal_Shdr *rel_hdr;
192
31
  Elf_Internal_Shdr *rel_hdr2;
193
31
  bfd_size_type amt;
194
195
31
  if (asect->relocation != NULL)
196
0
    return true;
197
198
31
  if (! dynamic)
199
31
    {
200
31
      if ((asect->flags & SEC_RELOC) == 0
201
30
    || asect->reloc_count == 0)
202
1
  return true;
203
204
30
      rel_hdr = d->rel.hdr;
205
30
      rel_hdr2 = d->rela.hdr;
206
207
30
      BFD_ASSERT ((rel_hdr && asect->rel_filepos == rel_hdr->sh_offset)
208
30
      || (rel_hdr2 && asect->rel_filepos == rel_hdr2->sh_offset));
209
30
    }
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
30
  amt = asect->reloc_count;
225
30
  amt *= 2 * sizeof (arelent);
226
30
  asect->relocation = (arelent *) bfd_alloc (abfd, amt);
227
30
  if (asect->relocation == NULL)
228
0
    return false;
229
230
  /* The elf64_sparc_slurp_one_reloc_table routine increments
231
     canon_reloc_count.  */
232
30
  canon_reloc_count (asect) = 0;
233
234
30
  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
30
  if (rel_hdr2
240
30
      && !elf64_sparc_slurp_one_reloc_table (abfd, asect, rel_hdr2, symbols,
241
30
               dynamic))
242
25
    return false;
243
244
5
  return true;
245
30
}
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
31
{
253
31
  arelent *tblptr;
254
31
  unsigned int i;
255
31
  elf_backend_data *bed = get_elf_backend_data (abfd);
256
257
31
  if (! bed->s->slurp_reloc_table (abfd, section, symbols, false))
258
25
    return -1;
259
260
6
  tblptr = section->relocation;
261
1.27k
  for (i = 0; i < canon_reloc_count (section); i++)
262
1.26k
    *relptr++ = tblptr++;
263
264
6
  *relptr = NULL;
265
266
6
  return canon_reloc_count (section);
267
31
}
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
14
{
324
14
  asect->orelocation = location;
325
14
  canon_reloc_count (asect) = count;
326
14
  if (count != 0)
327
1
    asect->flags |= SEC_RELOC;
328
13
  else
329
13
    asect->flags &= ~SEC_RELOC;
330
14
  return true;
331
14
}
332
333
/* Write out the relocs.  */
334
335
static void
336
elf64_sparc_write_relocs (bfd *abfd, asection *sec, void * data)
337
8
{
338
8
  bool *failedp = (bool *) data;
339
8
  Elf_Internal_Shdr *rela_hdr;
340
8
  bfd_vma addr_offset;
341
8
  Elf64_External_Rela *outbound_relocas, *src_rela;
342
8
  unsigned int idx, count;
343
8
  asymbol *last_sym = 0;
344
8
  int last_sym_idx = 0;
345
346
  /* If we have already failed, don't do anything.  */
347
8
  if (*failedp)
348
0
    return;
349
350
8
  if ((sec->flags & SEC_RELOC) == 0)
351
7
    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
1
  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
1
  count = 0;
364
254
  for (idx = 0; idx < canon_reloc_count (sec); idx++)
365
253
    {
366
253
      bfd_vma addr;
367
368
253
      ++count;
369
370
253
      addr = sec->orelocation[idx]->address;
371
253
      if (sec->orelocation[idx]->howto->type == R_SPARC_LO10
372
1
    && idx < canon_reloc_count (sec) - 1)
373
1
  {
374
1
    arelent *r = sec->orelocation[idx + 1];
375
376
1
    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
1
  }
382
253
    }
383
384
1
  rela_hdr = elf_section_data (sec)->rela.hdr;
385
386
1
  rela_hdr->sh_size = rela_hdr->sh_entsize * count;
387
1
  rela_hdr->contents = bfd_alloc (abfd, rela_hdr->sh_size);
388
1
  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
1
  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
1
  addr_offset = 0;
402
1
  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
1
  outbound_relocas = (Elf64_External_Rela *) rela_hdr->contents;
407
1
  src_rela = outbound_relocas;
408
409
254
  for (idx = 0; idx < canon_reloc_count (sec); idx++)
410
253
    {
411
253
      Elf_Internal_Rela dst_rela;
412
253
      arelent *ptr;
413
253
      asymbol *sym;
414
253
      int n;
415
416
253
      ptr = sec->orelocation[idx];
417
253
      sym = *ptr->sym_ptr_ptr;
418
253
      if (sym == last_sym)
419
1
  n = last_sym_idx;
420
252
      else if (bfd_is_abs_section (sym->section) && sym->value == 0)
421
230
  n = STN_UNDEF;
422
22
      else
423
22
  {
424
22
    last_sym = sym;
425
22
    n = _bfd_elf_symbol_from_bfd_symbol (abfd, &sym);
426
22
    if (n < 0)
427
0
      {
428
0
        *failedp = true;
429
0
        return;
430
0
      }
431
22
    last_sym_idx = n;
432
22
  }
433
434
253
      if ((*ptr->sym_ptr_ptr)->the_bfd != NULL
435
23
    && (*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
253
      if (ptr->howto->type == R_SPARC_LO10
443
1
    && idx < canon_reloc_count (sec) - 1)
444
1
  {
445
1
    arelent *r = sec->orelocation[idx + 1];
446
447
1
    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
1
    else
458
1
      dst_rela.r_info = ELF64_R_INFO (n, R_SPARC_LO10);
459
1
  }
460
252
      else
461
252
  dst_rela.r_info = ELF64_R_INFO (n, ptr->howto->type);
462
463
253
      dst_rela.r_offset = ptr->address + addr_offset;
464
253
      dst_rela.r_addend = ptr->addend;
465
466
253
      bfd_elf64_swap_reloca_out (abfd, &dst_rela, (bfd_byte *) src_rela);
467
253
      ++src_rela;
468
253
    }
469
1
}
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 (struct bfd_link_info *info,
601
            void * flaginfo,
602
            int (*func) (void *, const char *,
603
             Elf_Internal_Sym *,
604
             asection *,
605
             struct elf_link_hash_entry *))
606
0
{
607
0
  int reg;
608
0
  struct _bfd_sparc_elf_app_reg *app_regs =
609
0
    _bfd_sparc_elf_hash_table(info)->app_regs;
610
0
  Elf_Internal_Sym sym;
611
612
0
  for (reg = 0; reg < 4; reg++)
613
0
    if (app_regs [reg].name != NULL)
614
0
      {
615
0
  if (info->strip == strip_some
616
0
      && bfd_hash_lookup (info->keep_hash,
617
0
        app_regs [reg].name,
618
0
        false, false) == NULL)
619
0
    continue;
620
621
0
  sym.st_value = reg < 2 ? reg + 2 : reg + 4;
622
0
  sym.st_size = 0;
623
0
  sym.st_other = 0;
624
0
  sym.st_info = ELF_ST_INFO (app_regs [reg].bind, STT_REGISTER);
625
0
  sym.st_shndx = app_regs [reg].shndx;
626
0
  sym.st_target_internal = 0;
627
0
  if ((*func) (flaginfo, app_regs [reg].name, &sym,
628
0
         sym.st_shndx == SHN_ABS
629
0
         ? bfd_abs_section_ptr : bfd_und_section_ptr,
630
0
         NULL) != 1)
631
0
    return false;
632
0
      }
633
634
0
  return true;
635
0
}
636
637
static int
638
elf64_sparc_get_symbol_type (Elf_Internal_Sym *elf_sym, int type)
639
5
{
640
5
  if (ELF_ST_TYPE (elf_sym->st_info) == STT_REGISTER)
641
0
    return STT_REGISTER;
642
5
  else
643
5
    return type;
644
5
}
645
646
/* A STB_GLOBAL,STT_REGISTER symbol should be BSF_GLOBAL
647
   even in SHN_UNDEF section.  */
648
649
static void
650
elf64_sparc_symbol_processing (bfd *abfd ATTRIBUTE_UNUSED, asymbol *asym)
651
275
{
652
275
  elf_symbol_type *elfsym;
653
654
275
  elfsym = (elf_symbol_type *) asym;
655
275
  if (elfsym->internal_elf_sym.st_info
656
275
      == ELF_ST_INFO (STB_GLOBAL, STT_REGISTER))
657
0
    {
658
0
      asym->flags |= BSF_GLOBAL;
659
0
    }
660
275
}
661
662

663
/* Functions for dealing with the e_flags field.  */
664
665
/* Merge backend specific data from an object file to the output
666
   object file when linking.  */
667
668
static bool
669
elf64_sparc_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
670
0
{
671
0
  bfd *obfd = info->output_bfd;
672
0
  bool error;
673
0
  flagword new_flags, old_flags;
674
0
  int new_mm, old_mm;
675
676
0
  if (bfd_get_flavour (ibfd) != 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
14
{
759
14
  const char *name;
760
761
14
  name = bfd_section_name (sec);
762
763
14
  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
14
  return true;
770
14
}
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
  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
static 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_OSABI ELFOSABI_GNU
893
#define ELF_MAXPAGESIZE 0x100000
894
#define ELF_COMMONPAGESIZE 0x2000
895
896
/* This is the official ABI value.  */
897
#define ELF_MACHINE_CODE EM_SPARCV9
898
899
/* This is the value that we used before the ABI was released.  */
900
#define ELF_MACHINE_ALT1 EM_OLD_SPARCV9
901
902
#define elf_backend_reloc_type_class \
903
  elf64_sparc_reloc_type_class
904
#define bfd_elf64_get_reloc_upper_bound \
905
  elf64_sparc_get_reloc_upper_bound
906
#define bfd_elf64_get_dynamic_reloc_upper_bound \
907
  elf64_sparc_get_dynamic_reloc_upper_bound
908
#define bfd_elf64_canonicalize_reloc \
909
  elf64_sparc_canonicalize_reloc
910
#define bfd_elf64_canonicalize_dynamic_reloc \
911
  elf64_sparc_canonicalize_dynamic_reloc
912
#define bfd_elf64_finalize_section_relocs \
913
  elf64_sparc_finalize_section_relocs
914
#define elf_backend_add_symbol_hook \
915
  elf64_sparc_add_symbol_hook
916
#define elf_backend_get_symbol_type \
917
  elf64_sparc_get_symbol_type
918
#define elf_backend_symbol_processing \
919
  elf64_sparc_symbol_processing
920
#define elf_backend_print_symbol_all \
921
  elf64_sparc_print_symbol_all
922
#define elf_backend_output_arch_syms \
923
  elf64_sparc_output_arch_syms
924
#define bfd_elf64_bfd_merge_private_bfd_data \
925
  elf64_sparc_merge_private_bfd_data
926
#define elf_backend_fake_sections \
927
  elf64_sparc_fake_sections
928
#define elf_backend_size_info \
929
  elf64_sparc_size_info
930
931
#define elf_backend_plt_sym_val \
932
  _bfd_sparc_elf_plt_sym_val
933
#define bfd_elf64_bfd_link_hash_table_create \
934
  _bfd_sparc_elf_link_hash_table_create
935
#define elf_info_to_howto \
936
  _bfd_sparc_elf_info_to_howto
937
#define elf_backend_copy_indirect_symbol \
938
  _bfd_sparc_elf_copy_indirect_symbol
939
#define bfd_elf64_bfd_reloc_type_lookup \
940
  _bfd_sparc_elf_reloc_type_lookup
941
#define bfd_elf64_bfd_reloc_name_lookup \
942
  _bfd_sparc_elf_reloc_name_lookup
943
#define bfd_elf64_bfd_relax_section \
944
  _bfd_sparc_elf_relax_section
945
#define bfd_elf64_new_section_hook \
946
  _bfd_sparc_elf_new_section_hook
947
948
#define elf_backend_create_dynamic_sections \
949
  _bfd_sparc_elf_create_dynamic_sections
950
#define elf_backend_relocs_compatible \
951
  _bfd_elf_relocs_compatible
952
#define elf_backend_check_relocs \
953
  _bfd_sparc_elf_check_relocs
954
#define elf_backend_adjust_dynamic_symbol \
955
  _bfd_sparc_elf_adjust_dynamic_symbol
956
#define elf_backend_omit_section_dynsym \
957
  _bfd_sparc_elf_omit_section_dynsym
958
#define elf_backend_late_size_sections \
959
  _bfd_sparc_elf_late_size_sections
960
#define elf_backend_relocate_section \
961
  _bfd_sparc_elf_relocate_section
962
#define elf_backend_finish_dynamic_symbol \
963
  _bfd_sparc_elf_finish_dynamic_symbol
964
#define elf_backend_finish_dynamic_sections \
965
  _bfd_sparc_elf_finish_dynamic_sections
966
#define elf_backend_fixup_symbol \
967
  _bfd_sparc_elf_fixup_symbol
968
969
#define bfd_elf64_mkobject \
970
  _bfd_sparc_elf_mkobject
971
#define elf_backend_object_p \
972
  _bfd_sparc_elf_object_p
973
#define elf_backend_gc_mark_hook \
974
  _bfd_sparc_elf_gc_mark_hook
975
#define elf_backend_init_index_section \
976
  _bfd_elf_init_1_index_section
977
978
#define elf_backend_can_gc_sections 1
979
#define elf_backend_can_refcount 1
980
#define elf_backend_want_got_plt 0
981
#define elf_backend_plt_readonly 0
982
#define elf_backend_want_plt_sym 1
983
#define elf_backend_got_header_size 8
984
#define elf_backend_want_dynrelro 1
985
#define elf_backend_rela_normal 1
986
987
/* Section 5.2.4 of the ABI specifies a 256-byte boundary for the table.  */
988
#define elf_backend_plt_alignment 8
989
990
#include "elf64-target.h"
991
992
/* FreeBSD support */
993
#undef  TARGET_BIG_SYM
994
#define TARGET_BIG_SYM sparc_elf64_fbsd_vec
995
#undef  TARGET_BIG_NAME
996
#define TARGET_BIG_NAME "elf64-sparc-freebsd"
997
#undef  ELF_OSABI
998
#define ELF_OSABI ELFOSABI_FREEBSD
999
#undef  ELF_OSABI_EXACT
1000
#define ELF_OSABI_EXACT 1
1001
1002
#undef  elf64_bed
1003
#define elf64_bed       elf64_sparc_fbsd_bed
1004
1005
#include "elf64-target.h"
1006
1007
#ifdef OBJ_MAYBE_ELF_SOLARIS2
1008
1009
/* Solaris 2.  */
1010
1011
static bool
1012
elf64_sparc_solaris2_add_symbol_hook (bfd *abfd,
1013
              struct bfd_link_info *info,
1014
              Elf_Internal_Sym *sym,
1015
              const char **namep,
1016
              flagword *flagsp,
1017
              asection **secp,
1018
              bfd_vma *valp)
1019
0
{
1020
0
  return (elf64_sparc_add_symbol_hook (abfd, info, sym, namep,
1021
0
               flagsp, secp, valp)
1022
0
    && elf_solaris2_add_symbol_hook (abfd, info, sym, namep,
1023
0
             flagsp, secp, valp));
1024
0
}
1025
1026
#undef  TARGET_BIG_SYM
1027
#define TARGET_BIG_SYM        sparc_elf64_sol2_vec
1028
#undef  TARGET_BIG_NAME
1029
#define TARGET_BIG_NAME       "elf64-sparc-sol2"
1030
1031
#undef  ELF_TARGET_OS
1032
#define ELF_TARGET_OS       is_solaris
1033
#undef  ELF_OSABI
1034
#define ELF_OSABI       ELFOSABI_SOLARIS
1035
#undef  ELF_OSABI_EXACT
1036
1037
#undef  elf64_bed
1038
#define elf64_bed       elf64_sparc_sol2_bed
1039
1040
/* The 64-bit static TLS arena size is rounded to the nearest 16-byte
1041
   boundary.  */
1042
#undef  elf_backend_static_tls_alignment
1043
#define elf_backend_static_tls_alignment  16
1044
1045
#undef  elf_backend_add_symbol_hook
1046
#define elf_backend_add_symbol_hook elf64_sparc_solaris2_add_symbol_hook
1047
1048
static bool
1049
elf64_sparc_copy_solaris_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED,
1050
                                  bfd *obfd ATTRIBUTE_UNUSED,
1051
                                  const Elf_Internal_Shdr *isection ATTRIBUTE_UNUSED,
1052
                                  Elf_Internal_Shdr *osection ATTRIBUTE_UNUSED)
1053
0
{
1054
  /* PR 19938: FIXME: Need to add code for setting the sh_info
1055
     and sh_link fields of Solaris specific section types.  */
1056
  return false;
1057
0
}
1058
1059
#undef  elf_backend_copy_special_section_fields
1060
#define elf_backend_copy_special_section_fields elf64_sparc_copy_solaris_special_section_fields
1061
1062
#include "elf64-target.h"
1063
1064
#endif /* OBJ_MAYBE_ELF_SOLARIS2 */