Coverage Report

Created: 2026-07-25 10:20

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
4.90M
{
89
4.90M
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.  */
90
4.90M
  Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form.  */
91
4.90M
  Elf_Internal_Phdr *i_phdrp; /* Elf program header, internal form.  */
92
4.90M
  unsigned int phindex;
93
4.90M
  bfd_size_type amt;
94
4.90M
  ufile_ptr filesize;
95
96
  /* Read in the ELF header in external format.  */
97
4.90M
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
98
731k
    {
99
731k
      if (bfd_get_error () != bfd_error_system_call)
100
731k
  goto wrong;
101
0
      else
102
0
  goto fail;
103
731k
    }
104
105
  /* Check the magic number.  */
106
4.17M
  if (! elf_file_p (&x_ehdr))
107
1.23M
    goto wrong;
108
109
  /* FIXME: Check EI_VERSION here !  */
110
111
  /* Check the address size ("class").  */
112
2.93M
  if (x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
113
858k
    goto wrong;
114
115
  /* Check the byteorder.  */
116
2.08M
  switch (x_ehdr.e_ident[EI_DATA])
117
2.08M
    {
118
1.50M
    case ELFDATA2MSB:   /* Big-endian.  */
119
1.50M
      if (! bfd_big_endian (abfd))
120
749k
  goto wrong;
121
758k
      break;
122
758k
    case ELFDATA2LSB:   /* Little-endian.  */
123
571k
      if (! bfd_little_endian (abfd))
124
282k
  goto wrong;
125
289k
      break;
126
289k
    default:
127
516
      goto wrong;
128
2.08M
    }
129
130
  /* Give abfd an elf_obj_tdata.  */
131
1.04M
  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
1.04M
  i_ehdrp = elf_elfheader (abfd);
136
1.04M
  elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
137
138
#if DEBUG & 1
139
  elf_debug_file (i_ehdrp);
140
#endif
141
142
1.04M
  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
1.04M
  if (ebd->elf_machine_code != i_ehdrp->e_machine
148
1.01M
      && (ebd->elf_machine_alt1 == 0
149
303k
    || i_ehdrp->e_machine != ebd->elf_machine_alt1)
150
1.00M
      && (ebd->elf_machine_alt2 == 0
151
53.7k
    || i_ehdrp->e_machine != ebd->elf_machine_alt2)
152
1.00M
      && ebd->elf_machine_code != EM_NONE)
153
991k
    goto wrong;
154
155
56.8k
  if (ebd->osabi_exact && i_ehdrp->e_ident[EI_OSABI] != ebd->elf_osabi)
156
2.47k
    goto wrong;
157
158
  /* If there is no program header, or the type is not a core file, then
159
     we are hosed.  */
160
54.3k
  if (i_ehdrp->e_phoff == 0 || i_ehdrp->e_type != ET_CORE)
161
7.31k
    goto wrong;
162
163
  /* Does BFD's idea of the phdr size match the size
164
     recorded in the file? */
165
47.0k
  if (i_ehdrp->e_phentsize != sizeof (Elf_External_Phdr))
166
28
    goto wrong;
167
168
  /* If the program header count is PN_XNUM(0xffff), the actual
169
     count is in the first section header.  */
170
47.0k
  if (i_ehdrp->e_shoff != 0 && i_ehdrp->e_phnum == PN_XNUM)
171
275
    {
172
275
      Elf_External_Shdr x_shdr;
173
275
      Elf_Internal_Shdr i_shdr;
174
275
      file_ptr where = (file_ptr) i_ehdrp->e_shoff;
175
176
275
      if (i_ehdrp->e_shoff < sizeof (x_ehdr))
177
18
  goto wrong;
178
179
      /* Seek to the section header table in the file.  */
180
257
      if (bfd_seek (abfd, where, SEEK_SET) != 0)
181
32
  goto fail;
182
183
      /* Read the first section header at index 0, and convert to internal
184
   form.  */
185
225
      if (bfd_read (&x_shdr, sizeof (x_shdr), abfd) != sizeof (x_shdr))
186
36
  goto fail;
187
189
      elf_swap_shdr_in (abfd, &x_shdr, &i_shdr);
188
189
189
      if (i_shdr.sh_info != 0)
190
159
  {
191
159
    i_ehdrp->e_phnum = i_shdr.sh_info;
192
159
    if (i_ehdrp->e_phnum != i_shdr.sh_info)
193
0
      goto wrong;
194
159
  }
195
189
    }
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
46.9k
  if (i_ehdrp->e_phnum > 1)
200
45.2k
    {
201
45.2k
      Elf_External_Phdr x_phdr;
202
45.2k
      Elf_Internal_Phdr i_phdr;
203
45.2k
      file_ptr where;
204
205
      /* Check that we don't have a totally silly number of
206
   program headers.  */
207
45.2k
      if (i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (x_phdr)
208
45.1k
    || i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (i_phdr))
209
80
  goto wrong;
210
211
45.1k
      where = (file_ptr)(i_ehdrp->e_phoff + (i_ehdrp->e_phnum - 1) * sizeof (x_phdr));
212
45.1k
      if ((bfd_size_type) where <= i_ehdrp->e_phoff)
213
19
  goto wrong;
214
215
45.1k
      if (bfd_seek (abfd, where, SEEK_SET) != 0)
216
61
  goto fail;
217
45.0k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
218
129
  goto fail;
219
45.0k
    }
220
221
  /* Move to the start of the program headers.  */
222
46.6k
  if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0)
223
10
    goto wrong;
224
225
  /* Allocate space for the program headers.  */
226
46.6k
  amt = sizeof (*i_phdrp) * i_ehdrp->e_phnum;
227
46.6k
  i_phdrp = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
228
46.6k
  if (!i_phdrp)
229
0
    goto fail;
230
231
46.6k
  elf_tdata (abfd)->phdr = i_phdrp;
232
233
  /* Read and convert to internal form.  */
234
1.07M
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
235
1.02M
    {
236
1.02M
      Elf_External_Phdr x_phdr;
237
238
1.02M
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
239
33
  goto fail;
240
241
1.02M
      elf_swap_phdr_in (abfd, &x_phdr, i_phdrp + phindex);
242
1.02M
    }
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
46.6k
  if (! bfd_default_set_arch_mach (abfd, ebd->arch, 0)
248
      /* It's OK if this fails for the generic target.  */
249
12.9k
      && ebd->elf_machine_code != EM_NONE)
250
1
    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
46.6k
  if (ebd->elf_backend_object_p != NULL
258
31.7k
      && ! ebd->elf_backend_object_p (abfd))
259
5.48k
    goto wrong;
260
261
  /* Process each program header.  */
262
936k
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
263
896k
    if (! bfd_section_from_phdr (abfd, i_phdrp + phindex, (int) phindex))
264
1.29k
      goto fail;
265
266
  /* Check for core truncation.  */
267
39.8k
  filesize = bfd_get_file_size (abfd);
268
39.8k
  if (filesize != 0)
269
39.8k
    {
270
56.2k
      for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
271
54.8k
  {
272
54.8k
    Elf_Internal_Phdr *p = i_phdrp + phindex;
273
54.8k
    if (p->p_filesz
274
48.8k
        && (p->p_offset >= filesize
275
19.9k
      || p->p_filesz > filesize - p->p_offset))
276
38.4k
      {
277
38.4k
        _bfd_error_handler (_("warning: %pB has a segment "
278
38.4k
            "extending past end of file"), abfd);
279
38.4k
        abfd->read_only = 1;
280
38.4k
        break;
281
38.4k
      }
282
54.8k
      }
283
39.8k
  }
284
285
  /* Save the entry point from the ELF header.  */
286
39.8k
  abfd->start_address = i_ehdrp->e_entry;
287
39.8k
  return _bfd_no_cleanup;
288
289
4.86M
 wrong:
290
4.86M
  bfd_set_error (bfd_error_wrong_format);
291
4.86M
 fail:
292
4.86M
  return NULL;
293
4.86M
}
bfd_elf64_core_file_p
Line
Count
Source
88
1.13M
{
89
1.13M
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.  */
90
1.13M
  Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form.  */
91
1.13M
  Elf_Internal_Phdr *i_phdrp; /* Elf program header, internal form.  */
92
1.13M
  unsigned int phindex;
93
1.13M
  bfd_size_type amt;
94
1.13M
  ufile_ptr filesize;
95
96
  /* Read in the ELF header in external format.  */
97
1.13M
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
98
277k
    {
99
277k
      if (bfd_get_error () != bfd_error_system_call)
100
277k
  goto wrong;
101
0
      else
102
0
  goto fail;
103
277k
    }
104
105
  /* Check the magic number.  */
106
861k
  if (! elf_file_p (&x_ehdr))
107
265k
    goto wrong;
108
109
  /* FIXME: Check EI_VERSION here !  */
110
111
  /* Check the address size ("class").  */
112
596k
  if (x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
113
480k
    goto wrong;
114
115
  /* Check the byteorder.  */
116
115k
  switch (x_ehdr.e_ident[EI_DATA])
117
115k
    {
118
12.3k
    case ELFDATA2MSB:   /* Big-endian.  */
119
12.3k
      if (! bfd_big_endian (abfd))
120
6.49k
  goto wrong;
121
5.87k
      break;
122
103k
    case ELFDATA2LSB:   /* Little-endian.  */
123
103k
      if (! bfd_little_endian (abfd))
124
48.3k
  goto wrong;
125
54.9k
      break;
126
54.9k
    default:
127
120
      goto wrong;
128
115k
    }
129
130
  /* Give abfd an elf_obj_tdata.  */
131
60.8k
  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
60.8k
  i_ehdrp = elf_elfheader (abfd);
136
60.8k
  elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
137
138
#if DEBUG & 1
139
  elf_debug_file (i_ehdrp);
140
#endif
141
142
60.8k
  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
60.8k
  if (ebd->elf_machine_code != i_ehdrp->e_machine
148
55.0k
      && (ebd->elf_machine_alt1 == 0
149
6.30k
    || i_ehdrp->e_machine != ebd->elf_machine_alt1)
150
54.8k
      && (ebd->elf_machine_alt2 == 0
151
5.05k
    || i_ehdrp->e_machine != ebd->elf_machine_alt2)
152
54.8k
      && ebd->elf_machine_code != EM_NONE)
153
52.1k
    goto wrong;
154
155
8.69k
  if (ebd->osabi_exact && i_ehdrp->e_ident[EI_OSABI] != ebd->elf_osabi)
156
934
    goto wrong;
157
158
  /* If there is no program header, or the type is not a core file, then
159
     we are hosed.  */
160
7.75k
  if (i_ehdrp->e_phoff == 0 || i_ehdrp->e_type != ET_CORE)
161
2.46k
    goto wrong;
162
163
  /* Does BFD's idea of the phdr size match the size
164
     recorded in the file? */
165
5.29k
  if (i_ehdrp->e_phentsize != sizeof (Elf_External_Phdr))
166
10
    goto wrong;
167
168
  /* If the program header count is PN_XNUM(0xffff), the actual
169
     count is in the first section header.  */
170
5.28k
  if (i_ehdrp->e_shoff != 0 && i_ehdrp->e_phnum == PN_XNUM)
171
131
    {
172
131
      Elf_External_Shdr x_shdr;
173
131
      Elf_Internal_Shdr i_shdr;
174
131
      file_ptr where = (file_ptr) i_ehdrp->e_shoff;
175
176
131
      if (i_ehdrp->e_shoff < sizeof (x_ehdr))
177
6
  goto wrong;
178
179
      /* Seek to the section header table in the file.  */
180
125
      if (bfd_seek (abfd, where, SEEK_SET) != 0)
181
32
  goto fail;
182
183
      /* Read the first section header at index 0, and convert to internal
184
   form.  */
185
93
      if (bfd_read (&x_shdr, sizeof (x_shdr), abfd) != sizeof (x_shdr))
186
14
  goto fail;
187
79
      elf_swap_shdr_in (abfd, &x_shdr, &i_shdr);
188
189
79
      if (i_shdr.sh_info != 0)
190
61
  {
191
61
    i_ehdrp->e_phnum = i_shdr.sh_info;
192
61
    if (i_ehdrp->e_phnum != i_shdr.sh_info)
193
0
      goto wrong;
194
61
  }
195
79
    }
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
5.23k
  if (i_ehdrp->e_phnum > 1)
200
4.39k
    {
201
4.39k
      Elf_External_Phdr x_phdr;
202
4.39k
      Elf_Internal_Phdr i_phdr;
203
4.39k
      file_ptr where;
204
205
      /* Check that we don't have a totally silly number of
206
   program headers.  */
207
4.39k
      if (i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (x_phdr)
208
4.35k
    || i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (i_phdr))
209
48
  goto wrong;
210
211
4.34k
      where = (file_ptr)(i_ehdrp->e_phoff + (i_ehdrp->e_phnum - 1) * sizeof (x_phdr));
212
4.34k
      if ((bfd_size_type) where <= i_ehdrp->e_phoff)
213
19
  goto wrong;
214
215
4.32k
      if (bfd_seek (abfd, where, SEEK_SET) != 0)
216
61
  goto fail;
217
4.26k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
218
31
  goto fail;
219
4.26k
    }
220
221
  /* Move to the start of the program headers.  */
222
5.07k
  if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0)
223
10
    goto wrong;
224
225
  /* Allocate space for the program headers.  */
226
5.06k
  amt = sizeof (*i_phdrp) * i_ehdrp->e_phnum;
227
5.06k
  i_phdrp = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
228
5.06k
  if (!i_phdrp)
229
0
    goto fail;
230
231
5.06k
  elf_tdata (abfd)->phdr = i_phdrp;
232
233
  /* Read and convert to internal form.  */
234
144k
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
235
139k
    {
236
139k
      Elf_External_Phdr x_phdr;
237
238
139k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
239
13
  goto fail;
240
241
139k
      elf_swap_phdr_in (abfd, &x_phdr, i_phdrp + phindex);
242
139k
    }
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
5.04k
  if (! bfd_default_set_arch_mach (abfd, ebd->arch, 0)
248
      /* It's OK if this fails for the generic target.  */
249
1.52k
      && 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
5.04k
  if (ebd->elf_backend_object_p != NULL
258
3.35k
      && ! ebd->elf_backend_object_p (abfd))
259
0
    goto wrong;
260
261
  /* Process each program header.  */
262
136k
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
263
132k
    if (! bfd_section_from_phdr (abfd, i_phdrp + phindex, (int) phindex))
264
736
      goto fail;
265
266
  /* Check for core truncation.  */
267
4.31k
  filesize = bfd_get_file_size (abfd);
268
4.31k
  if (filesize != 0)
269
4.31k
    {
270
8.61k
      for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
271
8.01k
  {
272
8.01k
    Elf_Internal_Phdr *p = i_phdrp + phindex;
273
8.01k
    if (p->p_filesz
274
6.34k
        && (p->p_offset >= filesize
275
3.62k
      || p->p_filesz > filesize - p->p_offset))
276
3.71k
      {
277
3.71k
        _bfd_error_handler (_("warning: %pB has a segment "
278
3.71k
            "extending past end of file"), abfd);
279
3.71k
        abfd->read_only = 1;
280
3.71k
        break;
281
3.71k
      }
282
8.01k
      }
283
4.31k
  }
284
285
  /* Save the entry point from the ELF header.  */
286
4.31k
  abfd->start_address = i_ehdrp->e_entry;
287
4.31k
  return _bfd_no_cleanup;
288
289
1.13M
 wrong:
290
1.13M
  bfd_set_error (bfd_error_wrong_format);
291
1.13M
 fail:
292
  return NULL;
293
1.13M
}
bfd_elf32_core_file_p
Line
Count
Source
88
3.76M
{
89
3.76M
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.  */
90
3.76M
  Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form.  */
91
3.76M
  Elf_Internal_Phdr *i_phdrp; /* Elf program header, internal form.  */
92
3.76M
  unsigned int phindex;
93
3.76M
  bfd_size_type amt;
94
3.76M
  ufile_ptr filesize;
95
96
  /* Read in the ELF header in external format.  */
97
3.76M
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
98
453k
    {
99
453k
      if (bfd_get_error () != bfd_error_system_call)
100
453k
  goto wrong;
101
0
      else
102
0
  goto fail;
103
453k
    }
104
105
  /* Check the magic number.  */
106
3.31M
  if (! elf_file_p (&x_ehdr))
107
967k
    goto wrong;
108
109
  /* FIXME: Check EI_VERSION here !  */
110
111
  /* Check the address size ("class").  */
112
2.34M
  if (x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
113
377k
    goto wrong;
114
115
  /* Check the byteorder.  */
116
1.96M
  switch (x_ehdr.e_ident[EI_DATA])
117
1.96M
    {
118
1.49M
    case ELFDATA2MSB:   /* Big-endian.  */
119
1.49M
      if (! bfd_big_endian (abfd))
120
743k
  goto wrong;
121
753k
      break;
122
753k
    case ELFDATA2LSB:   /* Little-endian.  */
123
467k
      if (! bfd_little_endian (abfd))
124
233k
  goto wrong;
125
234k
      break;
126
234k
    default:
127
396
      goto wrong;
128
1.96M
    }
129
130
  /* Give abfd an elf_obj_tdata.  */
131
987k
  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
987k
  i_ehdrp = elf_elfheader (abfd);
136
987k
  elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
137
138
#if DEBUG & 1
139
  elf_debug_file (i_ehdrp);
140
#endif
141
142
987k
  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
987k
  if (ebd->elf_machine_code != i_ehdrp->e_machine
148
955k
      && (ebd->elf_machine_alt1 == 0
149
297k
    || i_ehdrp->e_machine != ebd->elf_machine_alt1)
150
954k
      && (ebd->elf_machine_alt2 == 0
151
48.6k
    || i_ehdrp->e_machine != ebd->elf_machine_alt2)
152
953k
      && ebd->elf_machine_code != EM_NONE)
153
939k
    goto wrong;
154
155
48.1k
  if (ebd->osabi_exact && i_ehdrp->e_ident[EI_OSABI] != ebd->elf_osabi)
156
1.54k
    goto wrong;
157
158
  /* If there is no program header, or the type is not a core file, then
159
     we are hosed.  */
160
46.6k
  if (i_ehdrp->e_phoff == 0 || i_ehdrp->e_type != ET_CORE)
161
4.84k
    goto wrong;
162
163
  /* Does BFD's idea of the phdr size match the size
164
     recorded in the file? */
165
41.7k
  if (i_ehdrp->e_phentsize != sizeof (Elf_External_Phdr))
166
18
    goto wrong;
167
168
  /* If the program header count is PN_XNUM(0xffff), the actual
169
     count is in the first section header.  */
170
41.7k
  if (i_ehdrp->e_shoff != 0 && i_ehdrp->e_phnum == PN_XNUM)
171
144
    {
172
144
      Elf_External_Shdr x_shdr;
173
144
      Elf_Internal_Shdr i_shdr;
174
144
      file_ptr where = (file_ptr) i_ehdrp->e_shoff;
175
176
144
      if (i_ehdrp->e_shoff < sizeof (x_ehdr))
177
12
  goto wrong;
178
179
      /* Seek to the section header table in the file.  */
180
132
      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
132
      if (bfd_read (&x_shdr, sizeof (x_shdr), abfd) != sizeof (x_shdr))
186
22
  goto fail;
187
110
      elf_swap_shdr_in (abfd, &x_shdr, &i_shdr);
188
189
110
      if (i_shdr.sh_info != 0)
190
98
  {
191
98
    i_ehdrp->e_phnum = i_shdr.sh_info;
192
98
    if (i_ehdrp->e_phnum != i_shdr.sh_info)
193
0
      goto wrong;
194
98
  }
195
110
    }
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
41.7k
  if (i_ehdrp->e_phnum > 1)
200
40.8k
    {
201
40.8k
      Elf_External_Phdr x_phdr;
202
40.8k
      Elf_Internal_Phdr i_phdr;
203
40.8k
      file_ptr where;
204
205
      /* Check that we don't have a totally silly number of
206
   program headers.  */
207
40.8k
      if (i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (x_phdr)
208
40.8k
    || i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (i_phdr))
209
32
  goto wrong;
210
211
40.8k
      where = (file_ptr)(i_ehdrp->e_phoff + (i_ehdrp->e_phnum - 1) * sizeof (x_phdr));
212
40.8k
      if ((bfd_size_type) where <= i_ehdrp->e_phoff)
213
0
  goto wrong;
214
215
40.8k
      if (bfd_seek (abfd, where, SEEK_SET) != 0)
216
0
  goto fail;
217
40.8k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
218
98
  goto fail;
219
40.8k
    }
220
221
  /* Move to the start of the program headers.  */
222
41.5k
  if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0)
223
0
    goto wrong;
224
225
  /* Allocate space for the program headers.  */
226
41.5k
  amt = sizeof (*i_phdrp) * i_ehdrp->e_phnum;
227
41.5k
  i_phdrp = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
228
41.5k
  if (!i_phdrp)
229
0
    goto fail;
230
231
41.5k
  elf_tdata (abfd)->phdr = i_phdrp;
232
233
  /* Read and convert to internal form.  */
234
926k
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
235
884k
    {
236
884k
      Elf_External_Phdr x_phdr;
237
238
884k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
239
20
  goto fail;
240
241
884k
      elf_swap_phdr_in (abfd, &x_phdr, i_phdrp + phindex);
242
884k
    }
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
41.5k
  if (! bfd_default_set_arch_mach (abfd, ebd->arch, 0)
248
      /* It's OK if this fails for the generic target.  */
249
11.4k
      && ebd->elf_machine_code != EM_NONE)
250
1
    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
41.5k
  if (ebd->elf_backend_object_p != NULL
258
28.3k
      && ! ebd->elf_backend_object_p (abfd))
259
5.48k
    goto wrong;
260
261
  /* Process each program header.  */
262
799k
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
263
764k
    if (! bfd_section_from_phdr (abfd, i_phdrp + phindex, (int) phindex))
264
560
      goto fail;
265
266
  /* Check for core truncation.  */
267
35.5k
  filesize = bfd_get_file_size (abfd);
268
35.5k
  if (filesize != 0)
269
35.5k
    {
270
47.6k
      for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
271
46.8k
  {
272
46.8k
    Elf_Internal_Phdr *p = i_phdrp + phindex;
273
46.8k
    if (p->p_filesz
274
42.5k
        && (p->p_offset >= filesize
275
16.3k
      || p->p_filesz > filesize - p->p_offset))
276
34.7k
      {
277
34.7k
        _bfd_error_handler (_("warning: %pB has a segment "
278
34.7k
            "extending past end of file"), abfd);
279
34.7k
        abfd->read_only = 1;
280
34.7k
        break;
281
34.7k
      }
282
46.8k
      }
283
35.5k
  }
284
285
  /* Save the entry point from the ELF header.  */
286
35.5k
  abfd->start_address = i_ehdrp->e_entry;
287
35.5k
  return _bfd_no_cleanup;
288
289
3.72M
 wrong:
290
3.72M
  bfd_set_error (bfd_error_wrong_format);
291
3.72M
 fail:
292
  return NULL;
293
3.72M
}
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
14.2k
{
304
14.2k
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.   */
305
14.2k
  Elf_Internal_Ehdr i_ehdr; /* Elf file header, internal form.   */
306
14.2k
  Elf_Internal_Phdr *i_phdr;
307
14.2k
  unsigned int i;
308
14.2k
  size_t amt;
309
310
  /* Seek to the position of the segment at OFFSET.  */
311
14.2k
  if (bfd_seek (abfd, offset, SEEK_SET) != 0)
312
1.64k
    goto fail;
313
314
  /* Read in the ELF header in external format.  */
315
12.6k
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
316
4.16k
    {
317
4.16k
      if (bfd_get_error () != bfd_error_system_call)
318
4.16k
  goto wrong;
319
0
      else
320
0
  goto fail;
321
4.16k
    }
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
8.46k
  if (! elf_file_p (&x_ehdr)
329
4.29k
      || x_ehdr.e_ident[EI_VERSION] != EV_CURRENT
330
3.73k
      || x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
331
4.77k
    goto wrong;
332
333
  /* Check that file's byte order matches xvec's.  */
334
3.68k
  switch (x_ehdr.e_ident[EI_DATA])
335
3.68k
    {
336
1.30k
    case ELFDATA2MSB:   /* Big-endian.  */
337
1.30k
      if (! bfd_header_big_endian (abfd))
338
23
  goto wrong;
339
1.27k
      break;
340
2.36k
    case ELFDATA2LSB:   /* Little-endian.  */
341
2.36k
      if (! bfd_header_little_endian (abfd))
342
0
  goto wrong;
343
2.36k
      break;
344
2.36k
    case ELFDATANONE:   /* No data encoding specified.  */
345
23
    default:      /* Unknown data encoding specified . */
346
23
      goto wrong;
347
3.68k
    }
348
349
3.64k
  elf_swap_ehdr_in (abfd, &x_ehdr, &i_ehdr);
350
#if DEBUG & 1
351
  elf_debug_file (&i_ehdr);
352
#endif
353
354
3.64k
  if (i_ehdr.e_phentsize != sizeof (Elf_External_Phdr) || i_ehdr.e_phnum == 0)
355
40
    goto fail;
356
357
  /* Read in program headers.  */
358
3.60k
  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
3.60k
  i_phdr = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
364
3.60k
  if (i_phdr == NULL)
365
0
    goto fail;
366
367
3.60k
  if (bfd_seek (abfd, offset + i_ehdr.e_phoff, SEEK_SET) != 0)
368
15
    goto fail;
369
370
  /* Read in program headers and parse notes.  */
371
402k
  for (i = 0; i < i_ehdr.e_phnum; ++i, ++i_phdr)
372
399k
    {
373
399k
      Elf_External_Phdr x_phdr;
374
375
399k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
376
262
  goto fail;
377
399k
      elf_swap_phdr_in (abfd, &x_phdr, i_phdr);
378
379
399k
      if (i_phdr->p_type == PT_NOTE && i_phdr->p_filesz > 0)
380
3.00k
  {
381
3.00k
    _bfd_elf_read_notes (abfd, offset + i_phdr->p_offset,
382
3.00k
             i_phdr->p_filesz, i_phdr->p_align);
383
384
    /* Make sure ABFD returns to processing the program headers.  */
385
3.00k
    if (bfd_seek (abfd,
386
3.00k
      offset + i_ehdr.e_phoff + (i + 1) * sizeof (x_phdr),
387
3.00k
      SEEK_SET) != 0)
388
0
      goto fail;
389
390
3.00k
    if (abfd->build_id != NULL)
391
14
      return true;
392
3.00k
  }
393
399k
    }
394
395
  /* Having gotten this far, we have a valid ELF section, but no
396
     build-id was found.  */
397
3.31k
  goto fail;
398
399
8.98k
 wrong:
400
8.98k
  bfd_set_error (bfd_error_wrong_format);
401
14.2k
 fail:
402
  return false;
403
8.98k
}
_bfd_elf64_core_find_build_id
Line
Count
Source
303
7.57k
{
304
7.57k
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.   */
305
7.57k
  Elf_Internal_Ehdr i_ehdr; /* Elf file header, internal form.   */
306
7.57k
  Elf_Internal_Phdr *i_phdr;
307
7.57k
  unsigned int i;
308
7.57k
  size_t amt;
309
310
  /* Seek to the position of the segment at OFFSET.  */
311
7.57k
  if (bfd_seek (abfd, offset, SEEK_SET) != 0)
312
1.64k
    goto fail;
313
314
  /* Read in the ELF header in external format.  */
315
5.92k
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
316
2.01k
    {
317
2.01k
      if (bfd_get_error () != bfd_error_system_call)
318
2.01k
  goto wrong;
319
0
      else
320
0
  goto fail;
321
2.01k
    }
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
3.90k
  if (! elf_file_p (&x_ehdr)
329
2.55k
      || x_ehdr.e_ident[EI_VERSION] != EV_CURRENT
330
2.12k
      || x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
331
1.80k
    goto wrong;
332
333
  /* Check that file's byte order matches xvec's.  */
334
2.09k
  switch (x_ehdr.e_ident[EI_DATA])
335
2.09k
    {
336
34
    case ELFDATA2MSB:   /* Big-endian.  */
337
34
      if (! bfd_header_big_endian (abfd))
338
0
  goto wrong;
339
34
      break;
340
2.04k
    case ELFDATA2LSB:   /* Little-endian.  */
341
2.04k
      if (! bfd_header_little_endian (abfd))
342
0
  goto wrong;
343
2.04k
      break;
344
2.04k
    case ELFDATANONE:   /* No data encoding specified.  */
345
18
    default:      /* Unknown data encoding specified . */
346
18
      goto wrong;
347
2.09k
    }
348
349
2.08k
  elf_swap_ehdr_in (abfd, &x_ehdr, &i_ehdr);
350
#if DEBUG & 1
351
  elf_debug_file (&i_ehdr);
352
#endif
353
354
2.08k
  if (i_ehdr.e_phentsize != sizeof (Elf_External_Phdr) || i_ehdr.e_phnum == 0)
355
24
    goto fail;
356
357
  /* Read in program headers.  */
358
2.05k
  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.05k
  i_phdr = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
364
2.05k
  if (i_phdr == NULL)
365
0
    goto fail;
366
367
2.05k
  if (bfd_seek (abfd, offset + i_ehdr.e_phoff, SEEK_SET) != 0)
368
15
    goto fail;
369
370
  /* Read in program headers and parse notes.  */
371
204k
  for (i = 0; i < i_ehdr.e_phnum; ++i, ++i_phdr)
372
202k
    {
373
202k
      Elf_External_Phdr x_phdr;
374
375
202k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
376
18
  goto fail;
377
202k
      elf_swap_phdr_in (abfd, &x_phdr, i_phdr);
378
379
202k
      if (i_phdr->p_type == PT_NOTE && i_phdr->p_filesz > 0)
380
2.31k
  {
381
2.31k
    _bfd_elf_read_notes (abfd, offset + i_phdr->p_offset,
382
2.31k
             i_phdr->p_filesz, i_phdr->p_align);
383
384
    /* Make sure ABFD returns to processing the program headers.  */
385
2.31k
    if (bfd_seek (abfd,
386
2.31k
      offset + i_ehdr.e_phoff + (i + 1) * sizeof (x_phdr),
387
2.31k
      SEEK_SET) != 0)
388
0
      goto fail;
389
390
2.31k
    if (abfd->build_id != NULL)
391
10
      return true;
392
2.31k
  }
393
202k
    }
394
395
  /* Having gotten this far, we have a valid ELF section, but no
396
     build-id was found.  */
397
2.01k
  goto fail;
398
399
3.84k
 wrong:
400
3.84k
  bfd_set_error (bfd_error_wrong_format);
401
7.56k
 fail:
402
  return false;
403
3.84k
}
_bfd_elf32_core_find_build_id
Line
Count
Source
303
6.70k
{
304
6.70k
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.   */
305
6.70k
  Elf_Internal_Ehdr i_ehdr; /* Elf file header, internal form.   */
306
6.70k
  Elf_Internal_Phdr *i_phdr;
307
6.70k
  unsigned int i;
308
6.70k
  size_t amt;
309
310
  /* Seek to the position of the segment at OFFSET.  */
311
6.70k
  if (bfd_seek (abfd, offset, SEEK_SET) != 0)
312
0
    goto fail;
313
314
  /* Read in the ELF header in external format.  */
315
6.70k
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
316
2.14k
    {
317
2.14k
      if (bfd_get_error () != bfd_error_system_call)
318
2.14k
  goto wrong;
319
0
      else
320
0
  goto fail;
321
2.14k
    }
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
4.55k
  if (! elf_file_p (&x_ehdr)
329
1.73k
      || x_ehdr.e_ident[EI_VERSION] != EV_CURRENT
330
1.61k
      || x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
331
2.96k
    goto wrong;
332
333
  /* Check that file's byte order matches xvec's.  */
334
1.59k
  switch (x_ehdr.e_ident[EI_DATA])
335
1.59k
    {
336
1.26k
    case ELFDATA2MSB:   /* Big-endian.  */
337
1.26k
      if (! bfd_header_big_endian (abfd))
338
23
  goto wrong;
339
1.24k
      break;
340
1.24k
    case ELFDATA2LSB:   /* Little-endian.  */
341
317
      if (! bfd_header_little_endian (abfd))
342
0
  goto wrong;
343
317
      break;
344
317
    case ELFDATANONE:   /* No data encoding specified.  */
345
5
    default:      /* Unknown data encoding specified . */
346
5
      goto wrong;
347
1.59k
    }
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
16
    goto fail;
356
357
  /* Read in program headers.  */
358
1.54k
  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.54k
  i_phdr = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
364
1.54k
  if (i_phdr == NULL)
365
0
    goto fail;
366
367
1.54k
  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
197k
  for (i = 0; i < i_ehdr.e_phnum; ++i, ++i_phdr)
372
196k
    {
373
196k
      Elf_External_Phdr x_phdr;
374
375
196k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
376
244
  goto fail;
377
196k
      elf_swap_phdr_in (abfd, &x_phdr, i_phdr);
378
379
196k
      if (i_phdr->p_type == PT_NOTE && i_phdr->p_filesz > 0)
380
690
  {
381
690
    _bfd_elf_read_notes (abfd, offset + i_phdr->p_offset,
382
690
             i_phdr->p_filesz, i_phdr->p_align);
383
384
    /* Make sure ABFD returns to processing the program headers.  */
385
690
    if (bfd_seek (abfd,
386
690
      offset + i_ehdr.e_phoff + (i + 1) * sizeof (x_phdr),
387
690
      SEEK_SET) != 0)
388
0
      goto fail;
389
390
690
    if (abfd->build_id != NULL)
391
4
      return true;
392
690
  }
393
196k
    }
394
395
  /* Having gotten this far, we have a valid ELF section, but no
396
     build-id was found.  */
397
1.29k
  goto fail;
398
399
5.14k
 wrong:
400
5.14k
  bfd_set_error (bfd_error_wrong_format);
401
6.70k
 fail:
402
  return false;
403
5.14k
}