Coverage Report

Created: 2026-11-21 09:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/binutils-gdb/bfd/elf32-sparc.c
Line
Count
Source
1
/* SPARC-specific support for 32-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 "bfd.h"
23
#include "bfdlink.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
#include "elf-vxworks.h"
31
32
/* Support for core dump NOTE sections.  */
33
34
static bool
35
elf32_sparc_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
36
0
{
37
0
  int offset;
38
0
  size_t size;
39
40
0
  switch (note->descsz)
41
0
    {
42
0
    default:
43
0
      return false;
44
0
    case 228:
45
      /* pr_cursig */
46
0
      elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
47
48
      /* pr_pid */
49
0
      elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
50
51
      /* pr_reg */
52
0
      offset = 72;
53
0
      size = 152;
54
55
0
      break;
56
0
    }
57
58
  /* Make a ".reg/999" section.  */
59
0
  return _bfd_elfcore_make_pseudosection (abfd, ".reg", size, note->descpos + offset);
60
0
}
61
62
static bool
63
elf32_sparc_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
64
9
{
65
9
  switch (note->descsz)
66
9
    {
67
9
    default:
68
9
      return false;
69
70
0
    case 260:     /* Solaris prpsinfo_t.  */
71
0
      elf_tdata (abfd)->core->program
72
0
  = _bfd_elfcore_strndup (abfd, note->descdata + 84, 16);
73
0
      elf_tdata (abfd)->core->command
74
0
  = _bfd_elfcore_strndup (abfd, note->descdata + 100, 80);
75
0
      break;
76
77
0
    case 336:     /* Solaris psinfo_t.  */
78
0
      elf_tdata (abfd)->core->program
79
0
  = _bfd_elfcore_strndup (abfd, note->descdata + 88, 16);
80
0
      elf_tdata (abfd)->core->command
81
0
  = _bfd_elfcore_strndup (abfd, note->descdata + 104, 80);
82
0
      break;
83
9
    }
84
85
0
  return true;
86
9
}
87
88
/* Functions for dealing with the e_flags field.
89
90
   We don't define set_private_flags or copy_private_bfd_data because
91
   the only currently defined values are based on the bfd mach number,
92
   so we use the latter instead and defer setting e_flags until the
93
   file is written out.  */
94
95
/* Merge backend specific data from an object file to the output
96
   object file when linking.  */
97
98
static bool
99
elf32_sparc_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
100
0
{
101
0
  bfd *obfd = info->output_bfd;
102
0
  bool error;
103
0
  unsigned long ibfd_mach;
104
  /* FIXME: This should not be static.  */
105
0
  static unsigned long previous_ibfd_e_flags = (unsigned long) -1;
106
107
0
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
108
0
    return true;
109
110
0
  error = false;
111
112
0
  ibfd_mach = bfd_get_mach (ibfd);
113
0
  if (bfd_mach_sparc_64bit_p (ibfd_mach))
114
0
    {
115
0
      error = true;
116
0
      _bfd_error_handler
117
0
  (_("%pB: compiled for a 64 bit system and target is 32 bit"), ibfd);
118
0
    }
119
0
  else if ((ibfd->flags & DYNAMIC) == 0)
120
0
    {
121
0
      if (bfd_get_mach (obfd) < ibfd_mach)
122
0
  bfd_set_arch_mach (obfd, bfd_arch_sparc, ibfd_mach);
123
0
    }
124
125
0
  if (((elf_elfheader (ibfd)->e_flags & EF_SPARC_LEDATA)
126
0
       != previous_ibfd_e_flags)
127
0
      && previous_ibfd_e_flags != (unsigned long) -1)
128
0
    {
129
0
      _bfd_error_handler
130
0
  (_("%pB: linking little endian files with big endian files"), ibfd);
131
0
      error = true;
132
0
    }
133
0
  previous_ibfd_e_flags = elf_elfheader (ibfd)->e_flags & EF_SPARC_LEDATA;
134
135
0
  if (error)
136
0
    {
137
0
      bfd_set_error (bfd_error_bad_value);
138
0
      return false;
139
0
    }
140
141
0
  return _bfd_sparc_elf_merge_private_bfd_data (ibfd, info);
142
0
}
143
144
/* The final processing done just before writing out the object file.
145
   We need to set the e_machine field appropriately.  */
146
147
static void
148
sparc_final_write_processing (bfd *abfd)
149
6
{
150
6
  switch (bfd_get_mach (abfd))
151
6
    {
152
1
    case bfd_mach_sparc:
153
1
    case bfd_mach_sparc_sparclet:
154
1
    case bfd_mach_sparc_sparclite:
155
1
      break; /* nothing to do */
156
1
    case bfd_mach_sparc_v8plus:
157
1
      elf_elfheader (abfd)->e_machine = EM_SPARC32PLUS;
158
1
      elf_elfheader (abfd)->e_flags &=~ EF_SPARC_32PLUS_MASK;
159
1
      elf_elfheader (abfd)->e_flags |= EF_SPARC_32PLUS;
160
1
      break;
161
2
    case bfd_mach_sparc_v8plusa:
162
2
      elf_elfheader (abfd)->e_machine = EM_SPARC32PLUS;
163
2
      elf_elfheader (abfd)->e_flags &=~ EF_SPARC_32PLUS_MASK;
164
2
      elf_elfheader (abfd)->e_flags |= EF_SPARC_32PLUS | EF_SPARC_SUN_US1;
165
2
      break;
166
2
    case bfd_mach_sparc_v8plusb:
167
2
    case bfd_mach_sparc_v8plusc:
168
2
    case bfd_mach_sparc_v8plusd:
169
2
    case bfd_mach_sparc_v8pluse:
170
2
    case bfd_mach_sparc_v8plusv:
171
2
    case bfd_mach_sparc_v8plusm:
172
2
    case bfd_mach_sparc_v8plusm8:
173
2
      elf_elfheader (abfd)->e_machine = EM_SPARC32PLUS;
174
2
      elf_elfheader (abfd)->e_flags &=~ EF_SPARC_32PLUS_MASK;
175
2
      elf_elfheader (abfd)->e_flags |= EF_SPARC_32PLUS | EF_SPARC_SUN_US1
176
2
               | EF_SPARC_SUN_US3;
177
2
      break;
178
0
    case bfd_mach_sparc_sparclite_le:
179
0
      elf_elfheader (abfd)->e_flags |= EF_SPARC_LEDATA;
180
0
      break;
181
0
    case 0: /* A non-sparc architecture - ignore.  */
182
0
      break;
183
0
    default:
184
0
      _bfd_error_handler
185
0
  (_("%pB: unhandled sparc machine value '%lu' detected during write processing"),
186
0
   abfd, (long) bfd_get_mach (abfd));
187
0
      break;
188
6
    }
189
6
}
190
191
static bool
192
elf32_sparc_final_write_processing (bfd *abfd)
193
6
{
194
6
  sparc_final_write_processing (abfd);
195
6
  return _bfd_elf_final_write_processing (abfd);
196
6
}
197
198
/* Used to decide how to sort relocs in an optimal manner for the
199
   dynamic linker, before writing them out.  */
200
201
static enum elf_reloc_type_class
202
elf32_sparc_reloc_type_class (const struct bfd_link_info *info,
203
            const asection *rel_sec ATTRIBUTE_UNUSED,
204
            const Elf_Internal_Rela *rela)
205
0
{
206
0
  bfd *abfd = info->output_bfd;
207
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
208
0
  struct _bfd_sparc_elf_link_hash_table *htab
209
0
    = _bfd_sparc_elf_hash_table (info);
210
0
  BFD_ASSERT (htab != NULL);
211
212
0
  if (htab->elf.dynsym != NULL
213
0
      && htab->elf.dynsym->contents != NULL)
214
0
    {
215
      /* Check relocation against STT_GNU_IFUNC symbol if there are
216
   dynamic symbols.  */
217
0
      unsigned long r_symndx = htab->r_symndx (rela->r_info);
218
0
      if (r_symndx != STN_UNDEF)
219
0
  {
220
0
    Elf_Internal_Sym sym;
221
0
    if (!bed->s->swap_symbol_in (abfd,
222
0
               (htab->elf.dynsym->contents
223
0
          + r_symndx * bed->s->sizeof_sym),
224
0
               0, &sym))
225
0
      abort ();
226
227
0
    if (ELF_ST_TYPE (sym.st_info) == STT_GNU_IFUNC)
228
0
      return reloc_class_ifunc;
229
0
  }
230
0
    }
231
232
0
  switch ((int) ELF32_R_TYPE (rela->r_info))
233
0
    {
234
0
    case R_SPARC_IRELATIVE:
235
0
      return reloc_class_ifunc;
236
0
    case R_SPARC_RELATIVE:
237
0
      return reloc_class_relative;
238
0
    case R_SPARC_JMP_SLOT:
239
0
      return reloc_class_plt;
240
0
    case R_SPARC_COPY:
241
0
      return reloc_class_copy;
242
0
    default:
243
0
      return reloc_class_normal;
244
0
    }
245
0
}
246
247
#define TARGET_BIG_SYM  sparc_elf32_vec
248
#define TARGET_BIG_NAME "elf32-sparc"
249
#define ELF_ARCH  bfd_arch_sparc
250
#define ELF_TARGET_ID SPARC_ELF_DATA
251
#define ELF_MACHINE_CODE EM_SPARC
252
#define ELF_MACHINE_ALT1 EM_SPARC32PLUS
253
#define ELF_OSABI ELFOSABI_GNU
254
#define ELF_MAXPAGESIZE 0x10000
255
#define ELF_COMMONPAGESIZE 0x2000
256
257
#define bfd_elf32_bfd_merge_private_bfd_data \
258
          elf32_sparc_merge_private_bfd_data
259
#define elf_backend_final_write_processing \
260
          elf32_sparc_final_write_processing
261
#define elf_backend_grok_prstatus elf32_sparc_grok_prstatus
262
#define elf_backend_grok_psinfo   elf32_sparc_grok_psinfo
263
#define elf_backend_reloc_type_class  elf32_sparc_reloc_type_class
264
265
#define elf_info_to_howto   _bfd_sparc_elf_info_to_howto
266
#define bfd_elf32_bfd_reloc_type_lookup _bfd_sparc_elf_reloc_type_lookup
267
#define bfd_elf32_bfd_reloc_name_lookup \
268
  _bfd_sparc_elf_reloc_name_lookup
269
#define bfd_elf32_bfd_link_hash_table_create \
270
          _bfd_sparc_elf_link_hash_table_create
271
#define bfd_elf32_bfd_relax_section _bfd_sparc_elf_relax_section
272
#define bfd_elf32_new_section_hook  _bfd_sparc_elf_new_section_hook
273
#define elf_backend_copy_indirect_symbol \
274
          _bfd_sparc_elf_copy_indirect_symbol
275
#define elf_backend_create_dynamic_sections \
276
          _bfd_sparc_elf_create_dynamic_sections
277
#define elf_backend_check_relocs  _bfd_sparc_elf_check_relocs
278
#define elf_backend_adjust_dynamic_symbol \
279
          _bfd_sparc_elf_adjust_dynamic_symbol
280
#define elf_backend_omit_section_dynsym _bfd_sparc_elf_omit_section_dynsym
281
#define elf_backend_late_size_sections  _bfd_sparc_elf_late_size_sections
282
#define elf_backend_relocate_section  _bfd_sparc_elf_relocate_section
283
#define elf_backend_finish_dynamic_symbol \
284
          _bfd_sparc_elf_finish_dynamic_symbol
285
#define elf_backend_finish_dynamic_sections \
286
          _bfd_sparc_elf_finish_dynamic_sections
287
#define bfd_elf32_mkobject    _bfd_sparc_elf_mkobject
288
#define elf_backend_object_p    _bfd_sparc_elf_object_p
289
#define elf_backend_gc_mark_hook  _bfd_sparc_elf_gc_mark_hook
290
#define elf_backend_plt_sym_val   _bfd_sparc_elf_plt_sym_val
291
#define elf_backend_init_index_section  _bfd_elf_init_1_index_section
292
#define elf_backend_fixup_symbol  _bfd_sparc_elf_fixup_symbol
293
294
#define elf_backend_can_gc_sections 1
295
#define elf_backend_can_refcount 1
296
#define elf_backend_want_got_plt 0
297
#define elf_backend_plt_readonly 0
298
#define elf_backend_want_plt_sym 1
299
#define elf_backend_got_header_size 4
300
#define elf_backend_want_dynrelro 1
301
#define elf_backend_rela_normal 1
302
303
#define elf_backend_linux_prpsinfo32_ugid16 true
304
305
#include "elf32-target.h"
306
307
#ifdef OBJ_MAYBE_ELF_SOLARIS2
308
309
/* Solaris 2.  */
310
311
#undef  TARGET_BIG_SYM
312
#define TARGET_BIG_SYM        sparc_elf32_sol2_vec
313
#undef  TARGET_BIG_NAME
314
#define TARGET_BIG_NAME       "elf32-sparc-sol2"
315
316
#undef  ELF_TARGET_OS
317
#define ELF_TARGET_OS       is_solaris
318
#undef  ELF_OSABI
319
#define ELF_OSABI       ELFOSABI_SOLARIS
320
#undef  ELF_OSABI_EXACT
321
322
#undef  elf32_bed
323
#define elf32_bed       elf32_sparc_sol2_bed
324
325
/* The 32-bit static TLS arena size is rounded to the nearest 8-byte
326
   boundary.  */
327
#undef  elf_backend_static_tls_alignment
328
#define elf_backend_static_tls_alignment  8
329
330
#undef  elf_backend_add_symbol_hook
331
#define elf_backend_add_symbol_hook   elf_solaris2_add_symbol_hook
332
333
static bool
334
elf32_sparc_copy_solaris_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED,
335
             bfd *obfd ATTRIBUTE_UNUSED,
336
             const Elf_Internal_Shdr *isection ATTRIBUTE_UNUSED,
337
             Elf_Internal_Shdr *osection ATTRIBUTE_UNUSED)
338
0
{
339
  /* PR 19938: FIXME: Need to add code for setting the sh_info
340
     and sh_link fields of Solaris specific section types.  */
341
0
  return false;
342
0
}
343
344
#undef  elf_backend_copy_special_section_fields
345
#define elf_backend_copy_special_section_fields elf32_sparc_copy_solaris_special_section_fields
346
347
#include "elf32-target.h"
348
349
#endif /* OBJ_MAYBE_ELF_SOLARIS2 */
350
351
#ifdef OBJ_MAYBE_ELF_VXWORKS
352
353
/* A final_write_processing hook that does both the SPARC- and VxWorks-
354
   specific handling.  */
355
356
static bool
357
elf32_sparc_vxworks_final_write_processing (bfd *abfd)
358
0
{
359
0
  sparc_final_write_processing (abfd);
360
0
  return elf_vxworks_final_write_processing (abfd);
361
0
}
362
363
#undef  TARGET_BIG_SYM
364
#define TARGET_BIG_SYM  sparc_elf32_vxworks_vec
365
#undef  TARGET_BIG_NAME
366
#define TARGET_BIG_NAME "elf32-sparc-vxworks"
367
368
#undef  ELF_MINPAGESIZE
369
#define ELF_MINPAGESIZE 0x1000
370
371
#undef  ELF_TARGET_OS
372
#define ELF_TARGET_OS is_vxworks
373
#undef  ELF_OSABI
374
#undef  ELF_OSABI_EXACT
375
376
#undef  elf_backend_want_got_plt
377
#define elf_backend_want_got_plt    1
378
#undef  elf_backend_plt_readonly
379
#define elf_backend_plt_readonly    1
380
#undef  elf_backend_got_header_size
381
#define elf_backend_got_header_size   12
382
#undef  elf_backend_dtrel_excludes_plt
383
#define elf_backend_dtrel_excludes_plt    1
384
#undef  elf_backend_add_symbol_hook
385
#define elf_backend_add_symbol_hook \
386
  elf_vxworks_add_symbol_hook
387
#undef  elf_backend_link_output_symbol_hook
388
#define elf_backend_link_output_symbol_hook \
389
  elf_vxworks_link_output_symbol_hook
390
#undef  elf_backend_emit_relocs
391
#define elf_backend_emit_relocs \
392
  elf_vxworks_emit_relocs
393
#undef  elf_backend_final_write_processing
394
#define elf_backend_final_write_processing \
395
  elf32_sparc_vxworks_final_write_processing
396
#undef  elf_backend_static_tls_alignment
397
#undef  elf_backend_copy_special_section_fields
398
399
#undef  elf32_bed
400
#define elf32_bed       sparc_elf_vxworks_bed
401
402
#include "elf32-target.h"
403
404
#endif /* OBJ_MAYBE_ELF_VXWORKS */