Coverage Report

Created: 2026-04-04 08:16

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/binutils-gdb/bfd/elfcore.h
Line
Count
Source
1
/* ELF core file support for BFD.
2
   Copyright (C) 1995-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
char*
22
elf_core_file_failing_command (bfd *abfd)
23
0
{
24
0
  return elf_tdata (abfd)->core->command;
25
0
}
Unexecuted instantiation: bfd_elf64_core_file_failing_command
Unexecuted instantiation: bfd_elf32_core_file_failing_command
26
27
int
28
elf_core_file_failing_signal (bfd *abfd)
29
0
{
30
0
  return elf_tdata (abfd)->core->signal;
31
0
}
Unexecuted instantiation: bfd_elf64_core_file_failing_signal
Unexecuted instantiation: bfd_elf32_core_file_failing_signal
32
33
int
34
elf_core_file_pid (bfd *abfd)
35
0
{
36
0
  return elf_tdata (abfd)->core->pid;
37
0
}
Unexecuted instantiation: bfd_elf64_core_file_pid
Unexecuted instantiation: bfd_elf32_core_file_pid
38
39
bool
40
elf_core_file_matches_executable_p (bfd *core_bfd, bfd *exec_bfd)
41
0
{
42
0
  char* corename;
43
44
  /* xvecs must match if both are ELF files for the same target.  */
45
46
0
  if (core_bfd->xvec != exec_bfd->xvec)
47
0
    {
48
0
      bfd_set_error (bfd_error_system_call);
49
0
      return false;
50
0
    }
51
52
  /* If both BFDs have identical build-ids, then they match.  */
53
0
  if (core_bfd->build_id != NULL
54
0
      && exec_bfd->build_id != NULL
55
0
      && core_bfd->build_id->size == exec_bfd->build_id->size
56
0
      && memcmp (core_bfd->build_id->data, exec_bfd->build_id->data,
57
0
     core_bfd->build_id->size) == 0)
58
0
    return true;
59
60
  /* See if the name in the corefile matches the executable name.  */
61
0
  corename = elf_tdata (core_bfd)->core->program;
62
0
  if (corename != NULL)
63
0
    {
64
0
      const char* execname = strrchr (bfd_get_filename (exec_bfd), '/');
65
66
0
      execname = execname ? execname + 1 : bfd_get_filename (exec_bfd);
67
68
0
      if (strcmp (execname, corename) != 0)
69
0
  return false;
70
0
    }
71
72
0
  return true;
73
0
}
Unexecuted instantiation: bfd_elf64_core_file_matches_executable_p
Unexecuted instantiation: bfd_elf32_core_file_matches_executable_p
74
75
/*  Core files are simply standard ELF formatted files that partition
76
    the file using the execution view of the file (program header table)
77
    rather than the linking view.  In fact, there is no section header
78
    table in a core file.
79
80
    The process status information (including the contents of the general
81
    register set) and the floating point register set are stored in a
82
    segment of type PT_NOTE.  We handcraft a couple of extra bfd sections
83
    that allow standard bfd access to the general registers (.reg) and the
84
    floating point registers (.reg2).  */
85
86
bfd_cleanup
87
elf_core_file_p (bfd *abfd)
88
1.83M
{
89
1.83M
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.  */
90
1.83M
  Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form.  */
91
1.83M
  Elf_Internal_Phdr *i_phdrp; /* Elf program header, internal form.  */
92
1.83M
  unsigned int phindex;
93
1.83M
  bfd_size_type amt;
94
1.83M
  ufile_ptr filesize;
95
96
  /* Read in the ELF header in external format.  */
97
1.83M
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
98
127k
    {
99
127k
      if (bfd_get_error () != bfd_error_system_call)
100
127k
  goto wrong;
101
0
      else
102
0
  goto fail;
103
127k
    }
104
105
  /* Check the magic number.  */
106
1.71M
  if (! elf_file_p (&x_ehdr))
107
163k
    goto wrong;
108
109
  /* FIXME: Check EI_VERSION here !  */
110
111
  /* Check the address size ("class").  */
112
1.54M
  if (x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
113
377k
    goto wrong;
114
115
  /* Check the byteorder.  */
116
1.16M
  switch (x_ehdr.e_ident[EI_DATA])
117
1.16M
    {
118
741k
    case ELFDATA2MSB:   /* Big-endian.  */
119
741k
      if (! bfd_big_endian (abfd))
120
368k
  goto wrong;
121
372k
      break;
122
427k
    case ELFDATA2LSB:   /* Little-endian.  */
123
427k
      if (! bfd_little_endian (abfd))
124
212k
  goto wrong;
125
215k
      break;
126
215k
    default:
127
528
      goto wrong;
128
1.16M
    }
129
130
  /* Give abfd an elf_obj_tdata.  */
131
588k
  if (! (*abfd->xvec->_bfd_set_format[bfd_core]) (abfd))
132
0
    goto fail;
133
134
  /* Swap in the rest of the header, now that we have the byte order.  */
135
588k
  i_ehdrp = elf_elfheader (abfd);
136
588k
  elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
137
138
#if DEBUG & 1
139
  elf_debug_file (i_ehdrp);
140
#endif
141
142
588k
  elf_backend_data *ebd = get_elf_backend_data (abfd);
143
144
  /* Check that the ELF e_machine field matches what this particular
145
     BFD format expects.  */
146
147
588k
  if (ebd->elf_machine_code != i_ehdrp->e_machine
148
570k
      && (ebd->elf_machine_alt1 == 0
149
170k
    || i_ehdrp->e_machine != ebd->elf_machine_alt1)
150
570k
      && (ebd->elf_machine_alt2 == 0
151
27.3k
    || i_ehdrp->e_machine != ebd->elf_machine_alt2)
152
569k
      && ebd->elf_machine_code != EM_NONE)
153
560k
    goto wrong;
154
155
27.6k
  if (ebd->osabi_exact && i_ehdrp->e_ident[EI_OSABI] != ebd->elf_osabi)
156
1.26k
    goto wrong;
157
158
  /* If there is no program header, or the type is not a core file, then
159
     we are hosed.  */
160
26.3k
  if (i_ehdrp->e_phoff == 0 || i_ehdrp->e_type != ET_CORE)
161
4.59k
    goto wrong;
162
163
  /* Does BFD's idea of the phdr size match the size
164
     recorded in the file? */
165
21.7k
  if (i_ehdrp->e_phentsize != sizeof (Elf_External_Phdr))
166
0
    goto wrong;
167
168
  /* If the program header count is PN_XNUM(0xffff), the actual
169
     count is in the first section header.  */
170
21.7k
  if (i_ehdrp->e_shoff != 0 && i_ehdrp->e_phnum == PN_XNUM)
171
113
    {
172
113
      Elf_External_Shdr x_shdr;
173
113
      Elf_Internal_Shdr i_shdr;
174
113
      file_ptr where = (file_ptr) i_ehdrp->e_shoff;
175
176
113
      if (i_ehdrp->e_shoff < sizeof (x_ehdr))
177
0
  goto wrong;
178
179
      /* Seek to the section header table in the file.  */
180
113
      if (bfd_seek (abfd, where, SEEK_SET) != 0)
181
0
  goto fail;
182
183
      /* Read the first section header at index 0, and convert to internal
184
   form.  */
185
113
      if (bfd_read (&x_shdr, sizeof (x_shdr), abfd) != sizeof (x_shdr))
186
0
  goto fail;
187
113
      elf_swap_shdr_in (abfd, &x_shdr, &i_shdr);
188
189
113
      if (i_shdr.sh_info != 0)
190
105
  {
191
105
    i_ehdrp->e_phnum = i_shdr.sh_info;
192
105
    if (i_ehdrp->e_phnum != i_shdr.sh_info)
193
0
      goto wrong;
194
105
  }
195
113
    }
196
197
  /* Sanity check that we can read all of the program headers.
198
     It ought to be good enough to just read the last one.  */
199
21.7k
  if (i_ehdrp->e_phnum > 1)
200
21.7k
    {
201
21.7k
      Elf_External_Phdr x_phdr;
202
21.7k
      Elf_Internal_Phdr i_phdr;
203
21.7k
      file_ptr where;
204
205
      /* Check that we don't have a totally silly number of
206
   program headers.  */
207
21.7k
      if (i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (x_phdr)
208
21.7k
    || i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (i_phdr))
209
17
  goto wrong;
210
211
21.6k
      where = (file_ptr)(i_ehdrp->e_phoff + (i_ehdrp->e_phnum - 1) * sizeof (x_phdr));
212
21.6k
      if ((bfd_size_type) where <= i_ehdrp->e_phoff)
213
0
  goto wrong;
214
215
21.6k
      if (bfd_seek (abfd, where, SEEK_SET) != 0)
216
0
  goto fail;
217
21.6k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
218
8
  goto fail;
219
21.6k
    }
220
221
  /* Move to the start of the program headers.  */
222
21.7k
  if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0)
223
0
    goto wrong;
224
225
  /* Allocate space for the program headers.  */
226
21.7k
  amt = sizeof (*i_phdrp) * i_ehdrp->e_phnum;
227
21.7k
  i_phdrp = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
228
21.7k
  if (!i_phdrp)
229
0
    goto fail;
230
231
21.7k
  elf_tdata (abfd)->phdr = i_phdrp;
232
233
  /* Read and convert to internal form.  */
234
665k
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
235
644k
    {
236
644k
      Elf_External_Phdr x_phdr;
237
238
644k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
239
0
  goto fail;
240
241
644k
      elf_swap_phdr_in (abfd, &x_phdr, i_phdrp + phindex);
242
644k
    }
243
244
  /* Set the machine architecture.  Do this before processing the
245
     program headers since we need to know the architecture type
246
     when processing the notes of some systems' core files.  */
247
21.7k
  if (! bfd_default_set_arch_mach (abfd, ebd->arch, 0)
248
      /* It's OK if this fails for the generic target.  */
249
5.90k
      && ebd->elf_machine_code != EM_NONE)
250
0
    goto fail;
251
252
  /* Let the backend double check the format and override global
253
     information.  We do this before processing the program headers
254
     to allow the correct machine (as opposed to just the default
255
     machine) to be set, making it possible for grok_prstatus and
256
     grok_psinfo to rely on the mach setting.  */
257
21.7k
  if (ebd->elf_backend_object_p != NULL
258
15.1k
      && ! ebd->elf_backend_object_p (abfd))
259
2.69k
    goto wrong;
260
261
  /* Process each program header.  */
262
575k
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
263
556k
    if (! bfd_section_from_phdr (abfd, i_phdrp + phindex, (int) phindex))
264
295
      goto fail;
265
266
  /* Check for core truncation.  */
267
18.7k
  filesize = bfd_get_file_size (abfd);
268
18.7k
  if (filesize != 0)
269
18.7k
    {
270
24.9k
      for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
271
24.8k
  {
272
24.8k
    Elf_Internal_Phdr *p = i_phdrp + phindex;
273
24.8k
    if (p->p_filesz
274
23.1k
        && (p->p_offset >= filesize
275
8.48k
      || p->p_filesz > filesize - p->p_offset))
276
18.6k
      {
277
18.6k
        _bfd_error_handler (_("warning: %pB has a segment "
278
18.6k
            "extending past end of file"), abfd);
279
18.6k
        abfd->read_only = 1;
280
18.6k
        break;
281
18.6k
      }
282
24.8k
      }
283
18.7k
  }
284
285
  /* Save the entry point from the ELF header.  */
286
18.7k
  abfd->start_address = i_ehdrp->e_entry;
287
18.7k
  return _bfd_no_cleanup;
288
289
1.81M
 wrong:
290
1.81M
  bfd_set_error (bfd_error_wrong_format);
291
1.81M
 fail:
292
1.81M
  return NULL;
293
1.81M
}
bfd_elf64_core_file_p
Line
Count
Source
88
426k
{
89
426k
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.  */
90
426k
  Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form.  */
91
426k
  Elf_Internal_Phdr *i_phdrp; /* Elf program header, internal form.  */
92
426k
  unsigned int phindex;
93
426k
  bfd_size_type amt;
94
426k
  ufile_ptr filesize;
95
96
  /* Read in the ELF header in external format.  */
97
426k
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
98
120k
    {
99
120k
      if (bfd_get_error () != bfd_error_system_call)
100
120k
  goto wrong;
101
0
      else
102
0
  goto fail;
103
120k
    }
104
105
  /* Check the magic number.  */
106
306k
  if (! elf_file_p (&x_ehdr))
107
35.2k
    goto wrong;
108
109
  /* FIXME: Check EI_VERSION here !  */
110
111
  /* Check the address size ("class").  */
112
271k
  if (x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
113
223k
    goto wrong;
114
115
  /* Check the byteorder.  */
116
47.2k
  switch (x_ehdr.e_ident[EI_DATA])
117
47.2k
    {
118
3.28k
    case ELFDATA2MSB:   /* Big-endian.  */
119
3.28k
      if (! bfd_big_endian (abfd))
120
1.72k
  goto wrong;
121
1.55k
      break;
122
43.9k
    case ELFDATA2LSB:   /* Little-endian.  */
123
43.9k
      if (! bfd_little_endian (abfd))
124
20.5k
  goto wrong;
125
23.3k
      break;
126
23.3k
    default:
127
0
      goto wrong;
128
47.2k
    }
129
130
  /* Give abfd an elf_obj_tdata.  */
131
24.9k
  if (! (*abfd->xvec->_bfd_set_format[bfd_core]) (abfd))
132
0
    goto fail;
133
134
  /* Swap in the rest of the header, now that we have the byte order.  */
135
24.9k
  i_ehdrp = elf_elfheader (abfd);
136
24.9k
  elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
137
138
#if DEBUG & 1
139
  elf_debug_file (i_ehdrp);
140
#endif
141
142
24.9k
  elf_backend_data *ebd = get_elf_backend_data (abfd);
143
144
  /* Check that the ELF e_machine field matches what this particular
145
     BFD format expects.  */
146
147
24.9k
  if (ebd->elf_machine_code != i_ehdrp->e_machine
148
22.1k
      && (ebd->elf_machine_alt1 == 0
149
2.42k
    || i_ehdrp->e_machine != ebd->elf_machine_alt1)
150
22.0k
      && (ebd->elf_machine_alt2 == 0
151
2.07k
    || i_ehdrp->e_machine != ebd->elf_machine_alt2)
152
22.0k
      && ebd->elf_machine_code != EM_NONE)
153
20.9k
    goto wrong;
154
155
3.99k
  if (ebd->osabi_exact && i_ehdrp->e_ident[EI_OSABI] != ebd->elf_osabi)
156
575
    goto wrong;
157
158
  /* If there is no program header, or the type is not a core file, then
159
     we are hosed.  */
160
3.41k
  if (i_ehdrp->e_phoff == 0 || i_ehdrp->e_type != ET_CORE)
161
1.46k
    goto wrong;
162
163
  /* Does BFD's idea of the phdr size match the size
164
     recorded in the file? */
165
1.95k
  if (i_ehdrp->e_phentsize != sizeof (Elf_External_Phdr))
166
0
    goto wrong;
167
168
  /* If the program header count is PN_XNUM(0xffff), the actual
169
     count is in the first section header.  */
170
1.95k
  if (i_ehdrp->e_shoff != 0 && i_ehdrp->e_phnum == PN_XNUM)
171
62
    {
172
62
      Elf_External_Shdr x_shdr;
173
62
      Elf_Internal_Shdr i_shdr;
174
62
      file_ptr where = (file_ptr) i_ehdrp->e_shoff;
175
176
62
      if (i_ehdrp->e_shoff < sizeof (x_ehdr))
177
0
  goto wrong;
178
179
      /* Seek to the section header table in the file.  */
180
62
      if (bfd_seek (abfd, where, SEEK_SET) != 0)
181
0
  goto fail;
182
183
      /* Read the first section header at index 0, and convert to internal
184
   form.  */
185
62
      if (bfd_read (&x_shdr, sizeof (x_shdr), abfd) != sizeof (x_shdr))
186
0
  goto fail;
187
62
      elf_swap_shdr_in (abfd, &x_shdr, &i_shdr);
188
189
62
      if (i_shdr.sh_info != 0)
190
54
  {
191
54
    i_ehdrp->e_phnum = i_shdr.sh_info;
192
54
    if (i_ehdrp->e_phnum != i_shdr.sh_info)
193
0
      goto wrong;
194
54
  }
195
62
    }
196
197
  /* Sanity check that we can read all of the program headers.
198
     It ought to be good enough to just read the last one.  */
199
1.95k
  if (i_ehdrp->e_phnum > 1)
200
1.88k
    {
201
1.88k
      Elf_External_Phdr x_phdr;
202
1.88k
      Elf_Internal_Phdr i_phdr;
203
1.88k
      file_ptr where;
204
205
      /* Check that we don't have a totally silly number of
206
   program headers.  */
207
1.88k
      if (i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (x_phdr)
208
1.87k
    || i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (i_phdr))
209
17
  goto wrong;
210
211
1.87k
      where = (file_ptr)(i_ehdrp->e_phoff + (i_ehdrp->e_phnum - 1) * sizeof (x_phdr));
212
1.87k
      if ((bfd_size_type) where <= i_ehdrp->e_phoff)
213
0
  goto wrong;
214
215
1.87k
      if (bfd_seek (abfd, where, SEEK_SET) != 0)
216
0
  goto fail;
217
1.87k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
218
8
  goto fail;
219
1.87k
    }
220
221
  /* Move to the start of the program headers.  */
222
1.92k
  if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0)
223
0
    goto wrong;
224
225
  /* Allocate space for the program headers.  */
226
1.92k
  amt = sizeof (*i_phdrp) * i_ehdrp->e_phnum;
227
1.92k
  i_phdrp = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
228
1.92k
  if (!i_phdrp)
229
0
    goto fail;
230
231
1.92k
  elf_tdata (abfd)->phdr = i_phdrp;
232
233
  /* Read and convert to internal form.  */
234
105k
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
235
103k
    {
236
103k
      Elf_External_Phdr x_phdr;
237
238
103k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
239
0
  goto fail;
240
241
103k
      elf_swap_phdr_in (abfd, &x_phdr, i_phdrp + phindex);
242
103k
    }
243
244
  /* Set the machine architecture.  Do this before processing the
245
     program headers since we need to know the architecture type
246
     when processing the notes of some systems' core files.  */
247
1.92k
  if (! bfd_default_set_arch_mach (abfd, ebd->arch, 0)
248
      /* It's OK if this fails for the generic target.  */
249
510
      && ebd->elf_machine_code != EM_NONE)
250
0
    goto fail;
251
252
  /* Let the backend double check the format and override global
253
     information.  We do this before processing the program headers
254
     to allow the correct machine (as opposed to just the default
255
     machine) to be set, making it possible for grok_prstatus and
256
     grok_psinfo to rely on the mach setting.  */
257
1.92k
  if (ebd->elf_backend_object_p != NULL
258
1.39k
      && ! ebd->elf_backend_object_p (abfd))
259
0
    goto wrong;
260
261
  /* Process each program header.  */
262
98.3k
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
263
96.6k
    if (! bfd_section_from_phdr (abfd, i_phdrp + phindex, (int) phindex))
264
182
      goto fail;
265
266
  /* Check for core truncation.  */
267
1.74k
  filesize = bfd_get_file_size (abfd);
268
1.74k
  if (filesize != 0)
269
1.74k
    {
270
3.45k
      for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
271
3.39k
  {
272
3.39k
    Elf_Internal_Phdr *p = i_phdrp + phindex;
273
3.39k
    if (p->p_filesz
274
2.81k
        && (p->p_offset >= filesize
275
1.53k
      || p->p_filesz > filesize - p->p_offset))
276
1.68k
      {
277
1.68k
        _bfd_error_handler (_("warning: %pB has a segment "
278
1.68k
            "extending past end of file"), abfd);
279
1.68k
        abfd->read_only = 1;
280
1.68k
        break;
281
1.68k
      }
282
3.39k
      }
283
1.74k
  }
284
285
  /* Save the entry point from the ELF header.  */
286
1.74k
  abfd->start_address = i_ehdrp->e_entry;
287
1.74k
  return _bfd_no_cleanup;
288
289
425k
 wrong:
290
425k
  bfd_set_error (bfd_error_wrong_format);
291
425k
 fail:
292
  return NULL;
293
425k
}
bfd_elf32_core_file_p
Line
Count
Source
88
1.41M
{
89
1.41M
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.  */
90
1.41M
  Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form.  */
91
1.41M
  Elf_Internal_Phdr *i_phdrp; /* Elf program header, internal form.  */
92
1.41M
  unsigned int phindex;
93
1.41M
  bfd_size_type amt;
94
1.41M
  ufile_ptr filesize;
95
96
  /* Read in the ELF header in external format.  */
97
1.41M
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
98
7.12k
    {
99
7.12k
      if (bfd_get_error () != bfd_error_system_call)
100
7.12k
  goto wrong;
101
0
      else
102
0
  goto fail;
103
7.12k
    }
104
105
  /* Check the magic number.  */
106
1.40M
  if (! elf_file_p (&x_ehdr))
107
127k
    goto wrong;
108
109
  /* FIXME: Check EI_VERSION here !  */
110
111
  /* Check the address size ("class").  */
112
1.27M
  if (x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
113
153k
    goto wrong;
114
115
  /* Check the byteorder.  */
116
1.12M
  switch (x_ehdr.e_ident[EI_DATA])
117
1.12M
    {
118
737k
    case ELFDATA2MSB:   /* Big-endian.  */
119
737k
      if (! bfd_big_endian (abfd))
120
366k
  goto wrong;
121
371k
      break;
122
383k
    case ELFDATA2LSB:   /* Little-endian.  */
123
383k
      if (! bfd_little_endian (abfd))
124
191k
  goto wrong;
125
192k
      break;
126
192k
    default:
127
528
      goto wrong;
128
1.12M
    }
129
130
  /* Give abfd an elf_obj_tdata.  */
131
563k
  if (! (*abfd->xvec->_bfd_set_format[bfd_core]) (abfd))
132
0
    goto fail;
133
134
  /* Swap in the rest of the header, now that we have the byte order.  */
135
563k
  i_ehdrp = elf_elfheader (abfd);
136
563k
  elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
137
138
#if DEBUG & 1
139
  elf_debug_file (i_ehdrp);
140
#endif
141
142
563k
  elf_backend_data *ebd = get_elf_backend_data (abfd);
143
144
  /* Check that the ELF e_machine field matches what this particular
145
     BFD format expects.  */
146
147
563k
  if (ebd->elf_machine_code != i_ehdrp->e_machine
148
548k
      && (ebd->elf_machine_alt1 == 0
149
167k
    || i_ehdrp->e_machine != ebd->elf_machine_alt1)
150
548k
      && (ebd->elf_machine_alt2 == 0
151
25.2k
    || i_ehdrp->e_machine != ebd->elf_machine_alt2)
152
547k
      && ebd->elf_machine_code != EM_NONE)
153
539k
    goto wrong;
154
155
23.6k
  if (ebd->osabi_exact && i_ehdrp->e_ident[EI_OSABI] != ebd->elf_osabi)
156
690
    goto wrong;
157
158
  /* If there is no program header, or the type is not a core file, then
159
     we are hosed.  */
160
22.9k
  if (i_ehdrp->e_phoff == 0 || i_ehdrp->e_type != ET_CORE)
161
3.13k
    goto wrong;
162
163
  /* Does BFD's idea of the phdr size match the size
164
     recorded in the file? */
165
19.8k
  if (i_ehdrp->e_phentsize != sizeof (Elf_External_Phdr))
166
0
    goto wrong;
167
168
  /* If the program header count is PN_XNUM(0xffff), the actual
169
     count is in the first section header.  */
170
19.8k
  if (i_ehdrp->e_shoff != 0 && i_ehdrp->e_phnum == PN_XNUM)
171
51
    {
172
51
      Elf_External_Shdr x_shdr;
173
51
      Elf_Internal_Shdr i_shdr;
174
51
      file_ptr where = (file_ptr) i_ehdrp->e_shoff;
175
176
51
      if (i_ehdrp->e_shoff < sizeof (x_ehdr))
177
0
  goto wrong;
178
179
      /* Seek to the section header table in the file.  */
180
51
      if (bfd_seek (abfd, where, SEEK_SET) != 0)
181
0
  goto fail;
182
183
      /* Read the first section header at index 0, and convert to internal
184
   form.  */
185
51
      if (bfd_read (&x_shdr, sizeof (x_shdr), abfd) != sizeof (x_shdr))
186
0
  goto fail;
187
51
      elf_swap_shdr_in (abfd, &x_shdr, &i_shdr);
188
189
51
      if (i_shdr.sh_info != 0)
190
51
  {
191
51
    i_ehdrp->e_phnum = i_shdr.sh_info;
192
51
    if (i_ehdrp->e_phnum != i_shdr.sh_info)
193
0
      goto wrong;
194
51
  }
195
51
    }
196
197
  /* Sanity check that we can read all of the program headers.
198
     It ought to be good enough to just read the last one.  */
199
19.8k
  if (i_ehdrp->e_phnum > 1)
200
19.8k
    {
201
19.8k
      Elf_External_Phdr x_phdr;
202
19.8k
      Elf_Internal_Phdr i_phdr;
203
19.8k
      file_ptr where;
204
205
      /* Check that we don't have a totally silly number of
206
   program headers.  */
207
19.8k
      if (i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (x_phdr)
208
19.8k
    || i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (i_phdr))
209
0
  goto wrong;
210
211
19.8k
      where = (file_ptr)(i_ehdrp->e_phoff + (i_ehdrp->e_phnum - 1) * sizeof (x_phdr));
212
19.8k
      if ((bfd_size_type) where <= i_ehdrp->e_phoff)
213
0
  goto wrong;
214
215
19.8k
      if (bfd_seek (abfd, where, SEEK_SET) != 0)
216
0
  goto fail;
217
19.8k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
218
0
  goto fail;
219
19.8k
    }
220
221
  /* Move to the start of the program headers.  */
222
19.8k
  if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0)
223
0
    goto wrong;
224
225
  /* Allocate space for the program headers.  */
226
19.8k
  amt = sizeof (*i_phdrp) * i_ehdrp->e_phnum;
227
19.8k
  i_phdrp = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
228
19.8k
  if (!i_phdrp)
229
0
    goto fail;
230
231
19.8k
  elf_tdata (abfd)->phdr = i_phdrp;
232
233
  /* Read and convert to internal form.  */
234
560k
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
235
541k
    {
236
541k
      Elf_External_Phdr x_phdr;
237
238
541k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
239
0
  goto fail;
240
241
541k
      elf_swap_phdr_in (abfd, &x_phdr, i_phdrp + phindex);
242
541k
    }
243
244
  /* Set the machine architecture.  Do this before processing the
245
     program headers since we need to know the architecture type
246
     when processing the notes of some systems' core files.  */
247
19.8k
  if (! bfd_default_set_arch_mach (abfd, ebd->arch, 0)
248
      /* It's OK if this fails for the generic target.  */
249
5.39k
      && ebd->elf_machine_code != EM_NONE)
250
0
    goto fail;
251
252
  /* Let the backend double check the format and override global
253
     information.  We do this before processing the program headers
254
     to allow the correct machine (as opposed to just the default
255
     machine) to be set, making it possible for grok_prstatus and
256
     grok_psinfo to rely on the mach setting.  */
257
19.8k
  if (ebd->elf_backend_object_p != NULL
258
13.7k
      && ! ebd->elf_backend_object_p (abfd))
259
2.69k
    goto wrong;
260
261
  /* Process each program header.  */
262
476k
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
263
459k
    if (! bfd_section_from_phdr (abfd, i_phdrp + phindex, (int) phindex))
264
113
      goto fail;
265
266
  /* Check for core truncation.  */
267
17.0k
  filesize = bfd_get_file_size (abfd);
268
17.0k
  if (filesize != 0)
269
17.0k
    {
270
21.5k
      for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
271
21.5k
  {
272
21.5k
    Elf_Internal_Phdr *p = i_phdrp + phindex;
273
21.5k
    if (p->p_filesz
274
20.3k
        && (p->p_offset >= filesize
275
6.94k
      || p->p_filesz > filesize - p->p_offset))
276
16.9k
      {
277
16.9k
        _bfd_error_handler (_("warning: %pB has a segment "
278
16.9k
            "extending past end of file"), abfd);
279
16.9k
        abfd->read_only = 1;
280
16.9k
        break;
281
16.9k
      }
282
21.5k
      }
283
17.0k
  }
284
285
  /* Save the entry point from the ELF header.  */
286
17.0k
  abfd->start_address = i_ehdrp->e_entry;
287
17.0k
  return _bfd_no_cleanup;
288
289
1.39M
 wrong:
290
1.39M
  bfd_set_error (bfd_error_wrong_format);
291
1.39M
 fail:
292
  return NULL;
293
1.39M
}
294
295
/* Attempt to find a build-id in a core file from the core file BFD.
296
   OFFSET is the file offset to a PT_LOAD segment that may contain
297
   the build-id note.  Returns TRUE upon success, FALSE otherwise.  */
298
299
bool
300
NAME(_bfd_elf, core_find_build_id)
301
  (bfd *abfd,
302
   bfd_vma offset)
303
8.08k
{
304
8.08k
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.   */
305
8.08k
  Elf_Internal_Ehdr i_ehdr; /* Elf file header, internal form.   */
306
8.08k
  Elf_Internal_Phdr *i_phdr;
307
8.08k
  unsigned int i;
308
8.08k
  size_t amt;
309
310
  /* Seek to the position of the segment at OFFSET.  */
311
8.08k
  if (bfd_seek (abfd, offset, SEEK_SET) != 0)
312
956
    goto fail;
313
314
  /* Read in the ELF header in external format.  */
315
7.13k
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
316
1.69k
    {
317
1.69k
      if (bfd_get_error () != bfd_error_system_call)
318
1.69k
  goto wrong;
319
0
      else
320
0
  goto fail;
321
1.69k
    }
322
323
  /* Now check to see if we have a valid ELF file, and one that BFD can
324
     make use of.  The magic number must match, the address size ('class')
325
     and byte-swapping must match our XVEC entry, and it must have a
326
     section header table (FIXME: See comments re sections at top of this
327
     file).  */
328
5.44k
  if (! elf_file_p (&x_ehdr)
329
2.52k
      || x_ehdr.e_ident[EI_VERSION] != EV_CURRENT
330
2.44k
      || x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
331
3.05k
    goto wrong;
332
333
  /* Check that file's byte order matches xvec's.  */
334
2.38k
  switch (x_ehdr.e_ident[EI_DATA])
335
2.38k
    {
336
565
    case ELFDATA2MSB:   /* Big-endian.  */
337
565
      if (! bfd_header_big_endian (abfd))
338
0
  goto wrong;
339
565
      break;
340
1.81k
    case ELFDATA2LSB:   /* Little-endian.  */
341
1.81k
      if (! bfd_header_little_endian (abfd))
342
0
  goto wrong;
343
1.81k
      break;
344
1.81k
    case ELFDATANONE:   /* No data encoding specified.  */
345
8
    default:      /* Unknown data encoding specified . */
346
8
      goto wrong;
347
2.38k
    }
348
349
2.38k
  elf_swap_ehdr_in (abfd, &x_ehdr, &i_ehdr);
350
#if DEBUG & 1
351
  elf_debug_file (&i_ehdr);
352
#endif
353
354
2.38k
  if (i_ehdr.e_phentsize != sizeof (Elf_External_Phdr) || i_ehdr.e_phnum == 0)
355
10
    goto fail;
356
357
  /* Read in program headers.  */
358
2.37k
  if (_bfd_mul_overflow (i_ehdr.e_phnum, sizeof (*i_phdr), &amt))
359
0
    {
360
0
      bfd_set_error (bfd_error_file_too_big);
361
0
      goto fail;
362
0
    }
363
2.37k
  i_phdr = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
364
2.37k
  if (i_phdr == NULL)
365
0
    goto fail;
366
367
2.37k
  if (bfd_seek (abfd, offset + i_ehdr.e_phoff, SEEK_SET) != 0)
368
0
    goto fail;
369
370
  /* Read in program headers and parse notes.  */
371
419k
  for (i = 0; i < i_ehdr.e_phnum; ++i, ++i_phdr)
372
416k
    {
373
416k
      Elf_External_Phdr x_phdr;
374
375
416k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
376
295
  goto fail;
377
416k
      elf_swap_phdr_in (abfd, &x_phdr, i_phdr);
378
379
416k
      if (i_phdr->p_type == PT_NOTE && i_phdr->p_filesz > 0)
380
2.53k
  {
381
2.53k
    _bfd_elf_read_notes (abfd, offset + i_phdr->p_offset,
382
2.53k
             i_phdr->p_filesz, i_phdr->p_align);
383
384
    /* Make sure ABFD returns to processing the program headers.  */
385
2.53k
    if (bfd_seek (abfd,
386
2.53k
      offset + i_ehdr.e_phoff + (i + 1) * sizeof (x_phdr),
387
2.53k
      SEEK_SET) != 0)
388
0
      goto fail;
389
390
2.53k
    if (abfd->build_id != NULL)
391
4
      return true;
392
2.53k
  }
393
416k
    }
394
395
  /* Having gotten this far, we have a valid ELF section, but no
396
     build-id was found.  */
397
2.07k
  goto fail;
398
399
4.75k
 wrong:
400
4.75k
  bfd_set_error (bfd_error_wrong_format);
401
8.08k
 fail:
402
  return false;
403
4.75k
}
_bfd_elf64_core_find_build_id
Line
Count
Source
303
4.42k
{
304
4.42k
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.   */
305
4.42k
  Elf_Internal_Ehdr i_ehdr; /* Elf file header, internal form.   */
306
4.42k
  Elf_Internal_Phdr *i_phdr;
307
4.42k
  unsigned int i;
308
4.42k
  size_t amt;
309
310
  /* Seek to the position of the segment at OFFSET.  */
311
4.42k
  if (bfd_seek (abfd, offset, SEEK_SET) != 0)
312
956
    goto fail;
313
314
  /* Read in the ELF header in external format.  */
315
3.47k
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
316
850
    {
317
850
      if (bfd_get_error () != bfd_error_system_call)
318
850
  goto wrong;
319
0
      else
320
0
  goto fail;
321
850
    }
322
323
  /* Now check to see if we have a valid ELF file, and one that BFD can
324
     make use of.  The magic number must match, the address size ('class')
325
     and byte-swapping must match our XVEC entry, and it must have a
326
     section header table (FIXME: See comments re sections at top of this
327
     file).  */
328
2.62k
  if (! elf_file_p (&x_ehdr)
329
1.66k
      || x_ehdr.e_ident[EI_VERSION] != EV_CURRENT
330
1.61k
      || x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
331
1.04k
    goto wrong;
332
333
  /* Check that file's byte order matches xvec's.  */
334
1.57k
  switch (x_ehdr.e_ident[EI_DATA])
335
1.57k
    {
336
0
    case ELFDATA2MSB:   /* Big-endian.  */
337
0
      if (! bfd_header_big_endian (abfd))
338
0
  goto wrong;
339
0
      break;
340
1.56k
    case ELFDATA2LSB:   /* Little-endian.  */
341
1.56k
      if (! bfd_header_little_endian (abfd))
342
0
  goto wrong;
343
1.56k
      break;
344
1.56k
    case ELFDATANONE:   /* No data encoding specified.  */
345
8
    default:      /* Unknown data encoding specified . */
346
8
      goto wrong;
347
1.57k
    }
348
349
1.56k
  elf_swap_ehdr_in (abfd, &x_ehdr, &i_ehdr);
350
#if DEBUG & 1
351
  elf_debug_file (&i_ehdr);
352
#endif
353
354
1.56k
  if (i_ehdr.e_phentsize != sizeof (Elf_External_Phdr) || i_ehdr.e_phnum == 0)
355
10
    goto fail;
356
357
  /* Read in program headers.  */
358
1.55k
  if (_bfd_mul_overflow (i_ehdr.e_phnum, sizeof (*i_phdr), &amt))
359
0
    {
360
0
      bfd_set_error (bfd_error_file_too_big);
361
0
      goto fail;
362
0
    }
363
1.55k
  i_phdr = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
364
1.55k
  if (i_phdr == NULL)
365
0
    goto fail;
366
367
1.55k
  if (bfd_seek (abfd, offset + i_ehdr.e_phoff, SEEK_SET) != 0)
368
0
    goto fail;
369
370
  /* Read in program headers and parse notes.  */
371
236k
  for (i = 0; i < i_ehdr.e_phnum; ++i, ++i_phdr)
372
234k
    {
373
234k
      Elf_External_Phdr x_phdr;
374
375
234k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
376
47
  goto fail;
377
234k
      elf_swap_phdr_in (abfd, &x_phdr, i_phdr);
378
379
234k
      if (i_phdr->p_type == PT_NOTE && i_phdr->p_filesz > 0)
380
1.75k
  {
381
1.75k
    _bfd_elf_read_notes (abfd, offset + i_phdr->p_offset,
382
1.75k
             i_phdr->p_filesz, i_phdr->p_align);
383
384
    /* Make sure ABFD returns to processing the program headers.  */
385
1.75k
    if (bfd_seek (abfd,
386
1.75k
      offset + i_ehdr.e_phoff + (i + 1) * sizeof (x_phdr),
387
1.75k
      SEEK_SET) != 0)
388
0
      goto fail;
389
390
1.75k
    if (abfd->build_id != NULL)
391
4
      return true;
392
1.75k
  }
393
234k
    }
394
395
  /* Having gotten this far, we have a valid ELF section, but no
396
     build-id was found.  */
397
1.50k
  goto fail;
398
399
1.90k
 wrong:
400
1.90k
  bfd_set_error (bfd_error_wrong_format);
401
4.42k
 fail:
402
  return false;
403
1.90k
}
_bfd_elf32_core_find_build_id
Line
Count
Source
303
3.66k
{
304
3.66k
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.   */
305
3.66k
  Elf_Internal_Ehdr i_ehdr; /* Elf file header, internal form.   */
306
3.66k
  Elf_Internal_Phdr *i_phdr;
307
3.66k
  unsigned int i;
308
3.66k
  size_t amt;
309
310
  /* Seek to the position of the segment at OFFSET.  */
311
3.66k
  if (bfd_seek (abfd, offset, SEEK_SET) != 0)
312
0
    goto fail;
313
314
  /* Read in the ELF header in external format.  */
315
3.66k
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
316
841
    {
317
841
      if (bfd_get_error () != bfd_error_system_call)
318
841
  goto wrong;
319
0
      else
320
0
  goto fail;
321
841
    }
322
323
  /* Now check to see if we have a valid ELF file, and one that BFD can
324
     make use of.  The magic number must match, the address size ('class')
325
     and byte-swapping must match our XVEC entry, and it must have a
326
     section header table (FIXME: See comments re sections at top of this
327
     file).  */
328
2.82k
  if (! elf_file_p (&x_ehdr)
329
855
      || x_ehdr.e_ident[EI_VERSION] != EV_CURRENT
330
828
      || x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
331
2.00k
    goto wrong;
332
333
  /* Check that file's byte order matches xvec's.  */
334
813
  switch (x_ehdr.e_ident[EI_DATA])
335
813
    {
336
565
    case ELFDATA2MSB:   /* Big-endian.  */
337
565
      if (! bfd_header_big_endian (abfd))
338
0
  goto wrong;
339
565
      break;
340
565
    case ELFDATA2LSB:   /* Little-endian.  */
341
248
      if (! bfd_header_little_endian (abfd))
342
0
  goto wrong;
343
248
      break;
344
248
    case ELFDATANONE:   /* No data encoding specified.  */
345
0
    default:      /* Unknown data encoding specified . */
346
0
      goto wrong;
347
813
    }
348
349
813
  elf_swap_ehdr_in (abfd, &x_ehdr, &i_ehdr);
350
#if DEBUG & 1
351
  elf_debug_file (&i_ehdr);
352
#endif
353
354
813
  if (i_ehdr.e_phentsize != sizeof (Elf_External_Phdr) || i_ehdr.e_phnum == 0)
355
0
    goto fail;
356
357
  /* Read in program headers.  */
358
813
  if (_bfd_mul_overflow (i_ehdr.e_phnum, sizeof (*i_phdr), &amt))
359
0
    {
360
0
      bfd_set_error (bfd_error_file_too_big);
361
0
      goto fail;
362
0
    }
363
813
  i_phdr = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
364
813
  if (i_phdr == NULL)
365
0
    goto fail;
366
367
813
  if (bfd_seek (abfd, offset + i_ehdr.e_phoff, SEEK_SET) != 0)
368
0
    goto fail;
369
370
  /* Read in program headers and parse notes.  */
371
182k
  for (i = 0; i < i_ehdr.e_phnum; ++i, ++i_phdr)
372
182k
    {
373
182k
      Elf_External_Phdr x_phdr;
374
375
182k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
376
248
  goto fail;
377
181k
      elf_swap_phdr_in (abfd, &x_phdr, i_phdr);
378
379
181k
      if (i_phdr->p_type == PT_NOTE && i_phdr->p_filesz > 0)
380
772
  {
381
772
    _bfd_elf_read_notes (abfd, offset + i_phdr->p_offset,
382
772
             i_phdr->p_filesz, i_phdr->p_align);
383
384
    /* Make sure ABFD returns to processing the program headers.  */
385
772
    if (bfd_seek (abfd,
386
772
      offset + i_ehdr.e_phoff + (i + 1) * sizeof (x_phdr),
387
772
      SEEK_SET) != 0)
388
0
      goto fail;
389
390
772
    if (abfd->build_id != NULL)
391
0
      return true;
392
772
  }
393
181k
    }
394
395
  /* Having gotten this far, we have a valid ELF section, but no
396
     build-id was found.  */
397
565
  goto fail;
398
399
2.84k
 wrong:
400
2.84k
  bfd_set_error (bfd_error_wrong_format);
401
3.66k
 fail:
402
  return false;
403
2.84k
}