Coverage Report

Created: 2026-05-11 07:54

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.76M
{
89
1.76M
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.  */
90
1.76M
  Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form.  */
91
1.76M
  Elf_Internal_Phdr *i_phdrp; /* Elf program header, internal form.  */
92
1.76M
  unsigned int phindex;
93
1.76M
  bfd_size_type amt;
94
1.76M
  ufile_ptr filesize;
95
96
  /* Read in the ELF header in external format.  */
97
1.76M
  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
1.64M
  if (! elf_file_p (&x_ehdr))
107
194k
    goto wrong;
108
109
  /* FIXME: Check EI_VERSION here !  */
110
111
  /* Check the address size ("class").  */
112
1.44M
  if (x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
113
348k
    goto wrong;
114
115
  /* Check the byteorder.  */
116
1.09M
  switch (x_ehdr.e_ident[EI_DATA])
117
1.09M
    {
118
687k
    case ELFDATA2MSB:   /* Big-endian.  */
119
687k
      if (! bfd_big_endian (abfd))
120
341k
  goto wrong;
121
346k
      break;
122
411k
    case ELFDATA2LSB:   /* Little-endian.  */
123
411k
      if (! bfd_little_endian (abfd))
124
204k
  goto wrong;
125
206k
      break;
126
206k
    default:
127
264
      goto wrong;
128
1.09M
    }
129
130
  /* Give abfd an elf_obj_tdata.  */
131
552k
  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
552k
  i_ehdrp = elf_elfheader (abfd);
136
552k
  elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
137
138
#if DEBUG & 1
139
  elf_debug_file (i_ehdrp);
140
#endif
141
142
552k
  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
552k
  if (ebd->elf_machine_code != i_ehdrp->e_machine
148
535k
      && (ebd->elf_machine_alt1 == 0
149
159k
    || i_ehdrp->e_machine != ebd->elf_machine_alt1)
150
535k
      && (ebd->elf_machine_alt2 == 0
151
25.4k
    || i_ehdrp->e_machine != ebd->elf_machine_alt2)
152
534k
      && ebd->elf_machine_code != EM_NONE)
153
526k
    goto wrong;
154
155
26.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
25.4k
  if (i_ehdrp->e_phoff == 0 || i_ehdrp->e_type != ET_CORE)
161
4.29k
    goto wrong;
162
163
  /* Does BFD's idea of the phdr size match the size
164
     recorded in the file? */
165
21.1k
  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.1k
  if (i_ehdrp->e_shoff != 0 && i_ehdrp->e_phnum == PN_XNUM)
171
99
    {
172
99
      Elf_External_Shdr x_shdr;
173
99
      Elf_Internal_Shdr i_shdr;
174
99
      file_ptr where = (file_ptr) i_ehdrp->e_shoff;
175
176
99
      if (i_ehdrp->e_shoff < sizeof (x_ehdr))
177
0
  goto wrong;
178
179
      /* Seek to the section header table in the file.  */
180
99
      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
99
      if (bfd_read (&x_shdr, sizeof (x_shdr), abfd) != sizeof (x_shdr))
186
0
  goto fail;
187
99
      elf_swap_shdr_in (abfd, &x_shdr, &i_shdr);
188
189
99
      if (i_shdr.sh_info != 0)
190
93
  {
191
93
    i_ehdrp->e_phnum = i_shdr.sh_info;
192
93
    if (i_ehdrp->e_phnum != i_shdr.sh_info)
193
0
      goto wrong;
194
93
  }
195
99
    }
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.1k
  if (i_ehdrp->e_phnum > 1)
200
21.0k
    {
201
21.0k
      Elf_External_Phdr x_phdr;
202
21.0k
      Elf_Internal_Phdr i_phdr;
203
21.0k
      file_ptr where;
204
205
      /* Check that we don't have a totally silly number of
206
   program headers.  */
207
21.0k
      if (i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (x_phdr)
208
21.0k
    || i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (i_phdr))
209
7
  goto wrong;
210
211
21.0k
      where = (file_ptr)(i_ehdrp->e_phoff + (i_ehdrp->e_phnum - 1) * sizeof (x_phdr));
212
21.0k
      if ((bfd_size_type) where <= i_ehdrp->e_phoff)
213
0
  goto wrong;
214
215
21.0k
      if (bfd_seek (abfd, where, SEEK_SET) != 0)
216
0
  goto fail;
217
21.0k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
218
9
  goto fail;
219
21.0k
    }
220
221
  /* Move to the start of the program headers.  */
222
21.1k
  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.1k
  amt = sizeof (*i_phdrp) * i_ehdrp->e_phnum;
227
21.1k
  i_phdrp = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
228
21.1k
  if (!i_phdrp)
229
0
    goto fail;
230
231
21.1k
  elf_tdata (abfd)->phdr = i_phdrp;
232
233
  /* Read and convert to internal form.  */
234
701k
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
235
680k
    {
236
680k
      Elf_External_Phdr x_phdr;
237
238
680k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
239
0
  goto fail;
240
241
680k
      elf_swap_phdr_in (abfd, &x_phdr, i_phdrp + phindex);
242
680k
    }
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.1k
  if (! bfd_default_set_arch_mach (abfd, ebd->arch, 0)
248
      /* It's OK if this fails for the generic target.  */
249
5.65k
      && 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.1k
  if (ebd->elf_backend_object_p != NULL
258
14.8k
      && ! ebd->elf_backend_object_p (abfd))
259
2.71k
    goto wrong;
260
261
  /* Process each program header.  */
262
605k
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
263
587k
    if (! bfd_section_from_phdr (abfd, i_phdrp + phindex, (int) phindex))
264
320
      goto fail;
265
266
  /* Check for core truncation.  */
267
18.0k
  filesize = bfd_get_file_size (abfd);
268
18.0k
  if (filesize != 0)
269
18.0k
    {
270
24.7k
      for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
271
24.6k
  {
272
24.6k
    Elf_Internal_Phdr *p = i_phdrp + phindex;
273
24.6k
    if (p->p_filesz
274
22.5k
        && (p->p_offset >= filesize
275
8.46k
      || p->p_filesz > filesize - p->p_offset))
276
18.0k
      {
277
18.0k
        _bfd_error_handler (_("warning: %pB has a segment "
278
18.0k
            "extending past end of file"), abfd);
279
18.0k
        abfd->read_only = 1;
280
18.0k
        break;
281
18.0k
      }
282
24.6k
      }
283
18.0k
  }
284
285
  /* Save the entry point from the ELF header.  */
286
18.0k
  abfd->start_address = i_ehdrp->e_entry;
287
18.0k
  return _bfd_no_cleanup;
288
289
1.74M
 wrong:
290
1.74M
  bfd_set_error (bfd_error_wrong_format);
291
1.74M
 fail:
292
1.74M
  return NULL;
293
1.74M
}
bfd_elf64_core_file_p
Line
Count
Source
88
409k
{
89
409k
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.  */
90
409k
  Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form.  */
91
409k
  Elf_Internal_Phdr *i_phdrp; /* Elf program header, internal form.  */
92
409k
  unsigned int phindex;
93
409k
  bfd_size_type amt;
94
409k
  ufile_ptr filesize;
95
96
  /* Read in the ELF header in external format.  */
97
409k
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
98
114k
    {
99
114k
      if (bfd_get_error () != bfd_error_system_call)
100
114k
  goto wrong;
101
0
      else
102
0
  goto fail;
103
114k
    }
104
105
  /* Check the magic number.  */
106
294k
  if (! elf_file_p (&x_ehdr))
107
42.6k
    goto wrong;
108
109
  /* FIXME: Check EI_VERSION here !  */
110
111
  /* Check the address size ("class").  */
112
251k
  if (x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
113
209k
    goto wrong;
114
115
  /* Check the byteorder.  */
116
42.7k
  switch (x_ehdr.e_ident[EI_DATA])
117
42.7k
    {
118
240
    case ELFDATA2MSB:   /* Big-endian.  */
119
240
      if (! bfd_big_endian (abfd))
120
126
  goto wrong;
121
114
      break;
122
42.5k
    case ELFDATA2LSB:   /* Little-endian.  */
123
42.5k
      if (! bfd_little_endian (abfd))
124
19.9k
  goto wrong;
125
22.5k
      break;
126
22.5k
    default:
127
0
      goto wrong;
128
42.7k
    }
129
130
  /* Give abfd an elf_obj_tdata.  */
131
22.7k
  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
22.7k
  i_ehdrp = elf_elfheader (abfd);
136
22.7k
  elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
137
138
#if DEBUG & 1
139
  elf_debug_file (i_ehdrp);
140
#endif
141
142
22.7k
  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
22.7k
  if (ebd->elf_machine_code != i_ehdrp->e_machine
148
20.0k
      && (ebd->elf_machine_alt1 == 0
149
1.88k
    || i_ehdrp->e_machine != ebd->elf_machine_alt1)
150
20.0k
      && (ebd->elf_machine_alt2 == 0
151
1.83k
    || i_ehdrp->e_machine != ebd->elf_machine_alt2)
152
19.9k
      && ebd->elf_machine_code != EM_NONE)
153
18.9k
    goto wrong;
154
155
3.74k
  if (ebd->osabi_exact && i_ehdrp->e_ident[EI_OSABI] != ebd->elf_osabi)
156
531
    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.21k
  if (i_ehdrp->e_phoff == 0 || i_ehdrp->e_type != ET_CORE)
161
1.42k
    goto wrong;
162
163
  /* Does BFD's idea of the phdr size match the size
164
     recorded in the file? */
165
1.79k
  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.79k
  if (i_ehdrp->e_shoff != 0 && i_ehdrp->e_phnum == PN_XNUM)
171
46
    {
172
46
      Elf_External_Shdr x_shdr;
173
46
      Elf_Internal_Shdr i_shdr;
174
46
      file_ptr where = (file_ptr) i_ehdrp->e_shoff;
175
176
46
      if (i_ehdrp->e_shoff < sizeof (x_ehdr))
177
0
  goto wrong;
178
179
      /* Seek to the section header table in the file.  */
180
46
      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
46
      if (bfd_read (&x_shdr, sizeof (x_shdr), abfd) != sizeof (x_shdr))
186
0
  goto fail;
187
46
      elf_swap_shdr_in (abfd, &x_shdr, &i_shdr);
188
189
46
      if (i_shdr.sh_info != 0)
190
40
  {
191
40
    i_ehdrp->e_phnum = i_shdr.sh_info;
192
40
    if (i_ehdrp->e_phnum != i_shdr.sh_info)
193
0
      goto wrong;
194
40
  }
195
46
    }
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.79k
  if (i_ehdrp->e_phnum > 1)
200
1.73k
    {
201
1.73k
      Elf_External_Phdr x_phdr;
202
1.73k
      Elf_Internal_Phdr i_phdr;
203
1.73k
      file_ptr where;
204
205
      /* Check that we don't have a totally silly number of
206
   program headers.  */
207
1.73k
      if (i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (x_phdr)
208
1.73k
    || i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (i_phdr))
209
5
  goto wrong;
210
211
1.73k
      where = (file_ptr)(i_ehdrp->e_phoff + (i_ehdrp->e_phnum - 1) * sizeof (x_phdr));
212
1.73k
      if ((bfd_size_type) where <= i_ehdrp->e_phoff)
213
0
  goto wrong;
214
215
1.73k
      if (bfd_seek (abfd, where, SEEK_SET) != 0)
216
0
  goto fail;
217
1.73k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
218
9
  goto fail;
219
1.73k
    }
220
221
  /* Move to the start of the program headers.  */
222
1.77k
  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.77k
  amt = sizeof (*i_phdrp) * i_ehdrp->e_phnum;
227
1.77k
  i_phdrp = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
228
1.77k
  if (!i_phdrp)
229
0
    goto fail;
230
231
1.77k
  elf_tdata (abfd)->phdr = i_phdrp;
232
233
  /* Read and convert to internal form.  */
234
108k
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
235
106k
    {
236
106k
      Elf_External_Phdr x_phdr;
237
238
106k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
239
0
  goto fail;
240
241
106k
      elf_swap_phdr_in (abfd, &x_phdr, i_phdrp + phindex);
242
106k
    }
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.77k
  if (! bfd_default_set_arch_mach (abfd, ebd->arch, 0)
248
      /* It's OK if this fails for the generic target.  */
249
473
      && 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.77k
  if (ebd->elf_backend_object_p != NULL
258
1.29k
      && ! ebd->elf_backend_object_p (abfd))
259
0
    goto wrong;
260
261
  /* Process each program header.  */
262
100k
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
263
98.5k
    if (! bfd_section_from_phdr (abfd, i_phdrp + phindex, (int) phindex))
264
218
      goto fail;
265
266
  /* Check for core truncation.  */
267
1.56k
  filesize = bfd_get_file_size (abfd);
268
1.56k
  if (filesize != 0)
269
1.56k
    {
270
3.32k
      for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
271
3.26k
  {
272
3.26k
    Elf_Internal_Phdr *p = i_phdrp + phindex;
273
3.26k
    if (p->p_filesz
274
2.57k
        && (p->p_offset >= filesize
275
1.48k
      || p->p_filesz > filesize - p->p_offset))
276
1.50k
      {
277
1.50k
        _bfd_error_handler (_("warning: %pB has a segment "
278
1.50k
            "extending past end of file"), abfd);
279
1.50k
        abfd->read_only = 1;
280
1.50k
        break;
281
1.50k
      }
282
3.26k
      }
283
1.56k
  }
284
285
  /* Save the entry point from the ELF header.  */
286
1.56k
  abfd->start_address = i_ehdrp->e_entry;
287
1.56k
  return _bfd_no_cleanup;
288
289
407k
 wrong:
290
407k
  bfd_set_error (bfd_error_wrong_format);
291
407k
 fail:
292
  return NULL;
293
407k
}
bfd_elf32_core_file_p
Line
Count
Source
88
1.35M
{
89
1.35M
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.  */
90
1.35M
  Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form.  */
91
1.35M
  Elf_Internal_Phdr *i_phdrp; /* Elf program header, internal form.  */
92
1.35M
  unsigned int phindex;
93
1.35M
  bfd_size_type amt;
94
1.35M
  ufile_ptr filesize;
95
96
  /* Read in the ELF header in external format.  */
97
1.35M
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
98
5.54k
    {
99
5.54k
      if (bfd_get_error () != bfd_error_system_call)
100
5.54k
  goto wrong;
101
0
      else
102
0
  goto fail;
103
5.54k
    }
104
105
  /* Check the magic number.  */
106
1.34M
  if (! elf_file_p (&x_ehdr))
107
152k
    goto wrong;
108
109
  /* FIXME: Check EI_VERSION here !  */
110
111
  /* Check the address size ("class").  */
112
1.19M
  if (x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
113
139k
    goto wrong;
114
115
  /* Check the byteorder.  */
116
1.05M
  switch (x_ehdr.e_ident[EI_DATA])
117
1.05M
    {
118
687k
    case ELFDATA2MSB:   /* Big-endian.  */
119
687k
      if (! bfd_big_endian (abfd))
120
341k
  goto wrong;
121
345k
      break;
122
368k
    case ELFDATA2LSB:   /* Little-endian.  */
123
368k
      if (! bfd_little_endian (abfd))
124
184k
  goto wrong;
125
184k
      break;
126
184k
    default:
127
264
      goto wrong;
128
1.05M
    }
129
130
  /* Give abfd an elf_obj_tdata.  */
131
530k
  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
530k
  i_ehdrp = elf_elfheader (abfd);
136
530k
  elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
137
138
#if DEBUG & 1
139
  elf_debug_file (i_ehdrp);
140
#endif
141
142
530k
  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
530k
  if (ebd->elf_machine_code != i_ehdrp->e_machine
148
515k
      && (ebd->elf_machine_alt1 == 0
149
157k
    || i_ehdrp->e_machine != ebd->elf_machine_alt1)
150
515k
      && (ebd->elf_machine_alt2 == 0
151
23.5k
    || i_ehdrp->e_machine != ebd->elf_machine_alt2)
152
515k
      && ebd->elf_machine_code != EM_NONE)
153
507k
    goto wrong;
154
155
22.9k
  if (ebd->osabi_exact && i_ehdrp->e_ident[EI_OSABI] != ebd->elf_osabi)
156
732
    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.2k
  if (i_ehdrp->e_phoff == 0 || i_ehdrp->e_type != ET_CORE)
161
2.87k
    goto wrong;
162
163
  /* Does BFD's idea of the phdr size match the size
164
     recorded in the file? */
165
19.3k
  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.3k
  if (i_ehdrp->e_shoff != 0 && i_ehdrp->e_phnum == PN_XNUM)
171
53
    {
172
53
      Elf_External_Shdr x_shdr;
173
53
      Elf_Internal_Shdr i_shdr;
174
53
      file_ptr where = (file_ptr) i_ehdrp->e_shoff;
175
176
53
      if (i_ehdrp->e_shoff < sizeof (x_ehdr))
177
0
  goto wrong;
178
179
      /* Seek to the section header table in the file.  */
180
53
      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
53
      if (bfd_read (&x_shdr, sizeof (x_shdr), abfd) != sizeof (x_shdr))
186
0
  goto fail;
187
53
      elf_swap_shdr_in (abfd, &x_shdr, &i_shdr);
188
189
53
      if (i_shdr.sh_info != 0)
190
53
  {
191
53
    i_ehdrp->e_phnum = i_shdr.sh_info;
192
53
    if (i_ehdrp->e_phnum != i_shdr.sh_info)
193
0
      goto wrong;
194
53
  }
195
53
    }
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.3k
  if (i_ehdrp->e_phnum > 1)
200
19.3k
    {
201
19.3k
      Elf_External_Phdr x_phdr;
202
19.3k
      Elf_Internal_Phdr i_phdr;
203
19.3k
      file_ptr where;
204
205
      /* Check that we don't have a totally silly number of
206
   program headers.  */
207
19.3k
      if (i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (x_phdr)
208
19.3k
    || i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (i_phdr))
209
2
  goto wrong;
210
211
19.3k
      where = (file_ptr)(i_ehdrp->e_phoff + (i_ehdrp->e_phnum - 1) * sizeof (x_phdr));
212
19.3k
      if ((bfd_size_type) where <= i_ehdrp->e_phoff)
213
0
  goto wrong;
214
215
19.3k
      if (bfd_seek (abfd, where, SEEK_SET) != 0)
216
0
  goto fail;
217
19.3k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
218
0
  goto fail;
219
19.3k
    }
220
221
  /* Move to the start of the program headers.  */
222
19.3k
  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.3k
  amt = sizeof (*i_phdrp) * i_ehdrp->e_phnum;
227
19.3k
  i_phdrp = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
228
19.3k
  if (!i_phdrp)
229
0
    goto fail;
230
231
19.3k
  elf_tdata (abfd)->phdr = i_phdrp;
232
233
  /* Read and convert to internal form.  */
234
592k
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
235
573k
    {
236
573k
      Elf_External_Phdr x_phdr;
237
238
573k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
239
0
  goto fail;
240
241
573k
      elf_swap_phdr_in (abfd, &x_phdr, i_phdrp + phindex);
242
573k
    }
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.3k
  if (! bfd_default_set_arch_mach (abfd, ebd->arch, 0)
248
      /* It's OK if this fails for the generic target.  */
249
5.18k
      && 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.3k
  if (ebd->elf_backend_object_p != NULL
258
13.5k
      && ! ebd->elf_backend_object_p (abfd))
259
2.71k
    goto wrong;
260
261
  /* Process each program header.  */
262
505k
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
263
488k
    if (! bfd_section_from_phdr (abfd, i_phdrp + phindex, (int) phindex))
264
102
      goto fail;
265
266
  /* Check for core truncation.  */
267
16.5k
  filesize = bfd_get_file_size (abfd);
268
16.5k
  if (filesize != 0)
269
16.5k
    {
270
21.4k
      for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
271
21.3k
  {
272
21.3k
    Elf_Internal_Phdr *p = i_phdrp + phindex;
273
21.3k
    if (p->p_filesz
274
19.9k
        && (p->p_offset >= filesize
275
6.98k
      || p->p_filesz > filesize - p->p_offset))
276
16.5k
      {
277
16.5k
        _bfd_error_handler (_("warning: %pB has a segment "
278
16.5k
            "extending past end of file"), abfd);
279
16.5k
        abfd->read_only = 1;
280
16.5k
        break;
281
16.5k
      }
282
21.3k
      }
283
16.5k
  }
284
285
  /* Save the entry point from the ELF header.  */
286
16.5k
  abfd->start_address = i_ehdrp->e_entry;
287
16.5k
  return _bfd_no_cleanup;
288
289
1.33M
 wrong:
290
1.33M
  bfd_set_error (bfd_error_wrong_format);
291
1.33M
 fail:
292
  return NULL;
293
1.33M
}
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.72k
{
304
8.72k
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.   */
305
8.72k
  Elf_Internal_Ehdr i_ehdr; /* Elf file header, internal form.   */
306
8.72k
  Elf_Internal_Phdr *i_phdr;
307
8.72k
  unsigned int i;
308
8.72k
  size_t amt;
309
310
  /* Seek to the position of the segment at OFFSET.  */
311
8.72k
  if (bfd_seek (abfd, offset, SEEK_SET) != 0)
312
1.09k
    goto fail;
313
314
  /* Read in the ELF header in external format.  */
315
7.62k
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
316
1.98k
    {
317
1.98k
      if (bfd_get_error () != bfd_error_system_call)
318
1.98k
  goto wrong;
319
0
      else
320
0
  goto fail;
321
1.98k
    }
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.64k
  if (! elf_file_p (&x_ehdr)
329
2.83k
      || x_ehdr.e_ident[EI_VERSION] != EV_CURRENT
330
2.63k
      || 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.59k
  switch (x_ehdr.e_ident[EI_DATA])
335
2.59k
    {
336
636
    case ELFDATA2MSB:   /* Big-endian.  */
337
636
      if (! bfd_header_big_endian (abfd))
338
0
  goto wrong;
339
636
      break;
340
1.93k
    case ELFDATA2LSB:   /* Little-endian.  */
341
1.93k
      if (! bfd_header_little_endian (abfd))
342
0
  goto wrong;
343
1.93k
      break;
344
1.93k
    case ELFDATANONE:   /* No data encoding specified.  */
345
18
    default:      /* Unknown data encoding specified . */
346
18
      goto wrong;
347
2.59k
    }
348
349
2.57k
  elf_swap_ehdr_in (abfd, &x_ehdr, &i_ehdr);
350
#if DEBUG & 1
351
  elf_debug_file (&i_ehdr);
352
#endif
353
354
2.57k
  if (i_ehdr.e_phentsize != sizeof (Elf_External_Phdr) || i_ehdr.e_phnum == 0)
355
15
    goto fail;
356
357
  /* Read in program headers.  */
358
2.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
2.55k
  i_phdr = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
364
2.55k
  if (i_phdr == NULL)
365
0
    goto fail;
366
367
2.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
428k
  for (i = 0; i < i_ehdr.e_phnum; ++i, ++i_phdr)
372
425k
    {
373
425k
      Elf_External_Phdr x_phdr;
374
375
425k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
376
243
  goto fail;
377
425k
      elf_swap_phdr_in (abfd, &x_phdr, i_phdr);
378
379
425k
      if (i_phdr->p_type == PT_NOTE && i_phdr->p_filesz > 0)
380
2.56k
  {
381
2.56k
    _bfd_elf_read_notes (abfd, offset + i_phdr->p_offset,
382
2.56k
             i_phdr->p_filesz, i_phdr->p_align);
383
384
    /* Make sure ABFD returns to processing the program headers.  */
385
2.56k
    if (bfd_seek (abfd,
386
2.56k
      offset + i_ehdr.e_phoff + (i + 1) * sizeof (x_phdr),
387
2.56k
      SEEK_SET) != 0)
388
0
      goto fail;
389
390
2.56k
    if (abfd->build_id != NULL)
391
0
      return true;
392
2.56k
  }
393
425k
    }
394
395
  /* Having gotten this far, we have a valid ELF section, but no
396
     build-id was found.  */
397
2.31k
  goto fail;
398
399
5.05k
 wrong:
400
5.05k
  bfd_set_error (bfd_error_wrong_format);
401
8.72k
 fail:
402
  return false;
403
5.05k
}
_bfd_elf64_core_find_build_id
Line
Count
Source
303
5.08k
{
304
5.08k
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.   */
305
5.08k
  Elf_Internal_Ehdr i_ehdr; /* Elf file header, internal form.   */
306
5.08k
  Elf_Internal_Phdr *i_phdr;
307
5.08k
  unsigned int i;
308
5.08k
  size_t amt;
309
310
  /* Seek to the position of the segment at OFFSET.  */
311
5.08k
  if (bfd_seek (abfd, offset, SEEK_SET) != 0)
312
1.09k
    goto fail;
313
314
  /* Read in the ELF header in external format.  */
315
3.98k
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
316
970
    {
317
970
      if (bfd_get_error () != bfd_error_system_call)
318
970
  goto wrong;
319
0
      else
320
0
  goto fail;
321
970
    }
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.01k
  if (! elf_file_p (&x_ehdr)
329
1.91k
      || x_ehdr.e_ident[EI_VERSION] != EV_CURRENT
330
1.74k
      || x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
331
1.30k
    goto wrong;
332
333
  /* Check that file's byte order matches xvec's.  */
334
1.71k
  switch (x_ehdr.e_ident[EI_DATA])
335
1.71k
    {
336
0
    case ELFDATA2MSB:   /* Big-endian.  */
337
0
      if (! bfd_header_big_endian (abfd))
338
0
  goto wrong;
339
0
      break;
340
1.69k
    case ELFDATA2LSB:   /* Little-endian.  */
341
1.69k
      if (! bfd_header_little_endian (abfd))
342
0
  goto wrong;
343
1.69k
      break;
344
1.69k
    case ELFDATANONE:   /* No data encoding specified.  */
345
18
    default:      /* Unknown data encoding specified . */
346
18
      goto wrong;
347
1.71k
    }
348
349
1.69k
  elf_swap_ehdr_in (abfd, &x_ehdr, &i_ehdr);
350
#if DEBUG & 1
351
  elf_debug_file (&i_ehdr);
352
#endif
353
354
1.69k
  if (i_ehdr.e_phentsize != sizeof (Elf_External_Phdr) || i_ehdr.e_phnum == 0)
355
15
    goto fail;
356
357
  /* Read in program headers.  */
358
1.68k
  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.68k
  i_phdr = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
364
1.68k
  if (i_phdr == NULL)
365
0
    goto fail;
366
367
1.68k
  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
251k
  for (i = 0; i < i_ehdr.e_phnum; ++i, ++i_phdr)
372
249k
    {
373
249k
      Elf_External_Phdr x_phdr;
374
375
249k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
376
43
  goto fail;
377
249k
      elf_swap_phdr_in (abfd, &x_phdr, i_phdr);
378
379
249k
      if (i_phdr->p_type == PT_NOTE && i_phdr->p_filesz > 0)
380
1.86k
  {
381
1.86k
    _bfd_elf_read_notes (abfd, offset + i_phdr->p_offset,
382
1.86k
             i_phdr->p_filesz, i_phdr->p_align);
383
384
    /* Make sure ABFD returns to processing the program headers.  */
385
1.86k
    if (bfd_seek (abfd,
386
1.86k
      offset + i_ehdr.e_phoff + (i + 1) * sizeof (x_phdr),
387
1.86k
      SEEK_SET) != 0)
388
0
      goto fail;
389
390
1.86k
    if (abfd->build_id != NULL)
391
0
      return true;
392
1.86k
  }
393
249k
    }
394
395
  /* Having gotten this far, we have a valid ELF section, but no
396
     build-id was found.  */
397
1.63k
  goto fail;
398
399
2.29k
 wrong:
400
2.29k
  bfd_set_error (bfd_error_wrong_format);
401
5.08k
 fail:
402
  return false;
403
2.29k
}
_bfd_elf32_core_find_build_id
Line
Count
Source
303
3.63k
{
304
3.63k
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.   */
305
3.63k
  Elf_Internal_Ehdr i_ehdr; /* Elf file header, internal form.   */
306
3.63k
  Elf_Internal_Phdr *i_phdr;
307
3.63k
  unsigned int i;
308
3.63k
  size_t amt;
309
310
  /* Seek to the position of the segment at OFFSET.  */
311
3.63k
  if (bfd_seek (abfd, offset, SEEK_SET) != 0)
312
0
    goto fail;
313
314
  /* Read in the ELF header in external format.  */
315
3.63k
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
316
1.01k
    {
317
1.01k
      if (bfd_get_error () != bfd_error_system_call)
318
1.01k
  goto wrong;
319
0
      else
320
0
  goto fail;
321
1.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
2.62k
  if (! elf_file_p (&x_ehdr)
329
916
      || x_ehdr.e_ident[EI_VERSION] != EV_CURRENT
330
893
      || x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
331
1.74k
    goto wrong;
332
333
  /* Check that file's byte order matches xvec's.  */
334
878
  switch (x_ehdr.e_ident[EI_DATA])
335
878
    {
336
636
    case ELFDATA2MSB:   /* Big-endian.  */
337
636
      if (! bfd_header_big_endian (abfd))
338
0
  goto wrong;
339
636
      break;
340
636
    case ELFDATA2LSB:   /* Little-endian.  */
341
242
      if (! bfd_header_little_endian (abfd))
342
0
  goto wrong;
343
242
      break;
344
242
    case ELFDATANONE:   /* No data encoding specified.  */
345
0
    default:      /* Unknown data encoding specified . */
346
0
      goto wrong;
347
878
    }
348
349
878
  elf_swap_ehdr_in (abfd, &x_ehdr, &i_ehdr);
350
#if DEBUG & 1
351
  elf_debug_file (&i_ehdr);
352
#endif
353
354
878
  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
878
  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
878
  i_phdr = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
364
878
  if (i_phdr == NULL)
365
0
    goto fail;
366
367
878
  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
176k
  for (i = 0; i < i_ehdr.e_phnum; ++i, ++i_phdr)
372
175k
    {
373
175k
      Elf_External_Phdr x_phdr;
374
375
175k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
376
200
  goto fail;
377
175k
      elf_swap_phdr_in (abfd, &x_phdr, i_phdr);
378
379
175k
      if (i_phdr->p_type == PT_NOTE && i_phdr->p_filesz > 0)
380
694
  {
381
694
    _bfd_elf_read_notes (abfd, offset + i_phdr->p_offset,
382
694
             i_phdr->p_filesz, i_phdr->p_align);
383
384
    /* Make sure ABFD returns to processing the program headers.  */
385
694
    if (bfd_seek (abfd,
386
694
      offset + i_ehdr.e_phoff + (i + 1) * sizeof (x_phdr),
387
694
      SEEK_SET) != 0)
388
0
      goto fail;
389
390
694
    if (abfd->build_id != NULL)
391
0
      return true;
392
694
  }
393
175k
    }
394
395
  /* Having gotten this far, we have a valid ELF section, but no
396
     build-id was found.  */
397
678
  goto fail;
398
399
2.76k
 wrong:
400
2.76k
  bfd_set_error (bfd_error_wrong_format);
401
3.63k
 fail:
402
  return false;
403
2.76k
}