Coverage Report

Created: 2026-03-10 08:46

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