Coverage Report

Created: 2025-07-08 11:15

/src/binutils-gdb/bfd/elfcore.h
Line
Count
Source (jump to first uncovered line)
1
/* ELF core file support for BFD.
2
   Copyright (C) 1995-2025 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.89M
{
89
1.89M
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.  */
90
1.89M
  Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form.  */
91
1.89M
  Elf_Internal_Phdr *i_phdrp; /* Elf program header, internal form.  */
92
1.89M
  unsigned int phindex;
93
1.89M
  const struct elf_backend_data *ebd;
94
1.89M
  bfd_size_type amt;
95
1.89M
  ufile_ptr filesize;
96
97
  /* Read in the ELF header in external format.  */
98
1.89M
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
99
421k
    {
100
421k
      if (bfd_get_error () != bfd_error_system_call)
101
420k
  goto wrong;
102
1.05k
      else
103
1.05k
  goto fail;
104
421k
    }
105
106
  /* Check the magic number.  */
107
1.47M
  if (! elf_file_p (&x_ehdr))
108
639k
    goto wrong;
109
110
  /* FIXME: Check EI_VERSION here !  */
111
112
  /* Check the address size ("class").  */
113
838k
  if (x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
114
317k
    goto wrong;
115
116
  /* Check the byteorder.  */
117
521k
  switch (x_ehdr.e_ident[EI_DATA])
118
521k
    {
119
311k
    case ELFDATA2MSB:   /* Big-endian.  */
120
311k
      if (! bfd_big_endian (abfd))
121
154k
  goto wrong;
122
156k
      break;
123
209k
    case ELFDATA2LSB:   /* Little-endian.  */
124
209k
      if (! bfd_little_endian (abfd))
125
101k
  goto wrong;
126
107k
      break;
127
107k
    default:
128
968
      goto wrong;
129
521k
    }
130
131
  /* Give abfd an elf_obj_tdata.  */
132
263k
  if (! (*abfd->xvec->_bfd_set_format[bfd_core]) (abfd))
133
0
    goto fail;
134
135
  /* Swap in the rest of the header, now that we have the byte order.  */
136
263k
  i_ehdrp = elf_elfheader (abfd);
137
263k
  elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
138
139
#if DEBUG & 1
140
  elf_debug_file (i_ehdrp);
141
#endif
142
143
263k
  ebd = get_elf_backend_data (abfd);
144
145
  /* Check that the ELF e_machine field matches what this particular
146
     BFD format expects.  */
147
148
263k
  if (ebd->elf_machine_code != i_ehdrp->e_machine
149
263k
      && (ebd->elf_machine_alt1 == 0
150
252k
    || i_ehdrp->e_machine != ebd->elf_machine_alt1)
151
263k
      && (ebd->elf_machine_alt2 == 0
152
252k
    || i_ehdrp->e_machine != ebd->elf_machine_alt2)
153
263k
      && ebd->elf_machine_code != EM_NONE)
154
247k
    goto wrong;
155
156
16.4k
  if (ebd->elf_machine_code != EM_NONE
157
16.4k
      && i_ehdrp->e_ident[EI_OSABI] != ebd->elf_osabi
158
16.4k
      && ebd->elf_osabi != ELFOSABI_NONE)
159
1.88k
    goto wrong;
160
161
  /* If there is no program header, or the type is not a core file, then
162
     we are hosed.  */
163
14.6k
  if (i_ehdrp->e_phoff == 0 || i_ehdrp->e_type != ET_CORE)
164
4.79k
    goto wrong;
165
166
  /* Does BFD's idea of the phdr size match the size
167
     recorded in the file? */
168
9.81k
  if (i_ehdrp->e_phentsize != sizeof (Elf_External_Phdr))
169
20
    goto wrong;
170
171
  /* If the program header count is PN_XNUM(0xffff), the actual
172
     count is in the first section header.  */
173
9.79k
  if (i_ehdrp->e_shoff != 0 && i_ehdrp->e_phnum == PN_XNUM)
174
244
    {
175
244
      Elf_External_Shdr x_shdr;
176
244
      Elf_Internal_Shdr i_shdr;
177
244
      file_ptr where = (file_ptr) i_ehdrp->e_shoff;
178
179
244
      if (i_ehdrp->e_shoff < sizeof (x_ehdr))
180
9
  goto wrong;
181
182
      /* Seek to the section header table in the file.  */
183
235
      if (bfd_seek (abfd, where, SEEK_SET) != 0)
184
9
  goto fail;
185
186
      /* Read the first section header at index 0, and convert to internal
187
   form.  */
188
226
      if (bfd_read (&x_shdr, sizeof (x_shdr), abfd) != sizeof (x_shdr))
189
20
  goto fail;
190
206
      elf_swap_shdr_in (abfd, &x_shdr, &i_shdr);
191
192
206
      if (i_shdr.sh_info != 0)
193
194
  {
194
194
    i_ehdrp->e_phnum = i_shdr.sh_info;
195
194
    if (i_ehdrp->e_phnum != i_shdr.sh_info)
196
0
      goto wrong;
197
194
  }
198
206
    }
199
200
  /* Sanity check that we can read all of the program headers.
201
     It ought to be good enough to just read the last one.  */
202
9.75k
  if (i_ehdrp->e_phnum > 1)
203
9.34k
    {
204
9.34k
      Elf_External_Phdr x_phdr;
205
9.34k
      Elf_Internal_Phdr i_phdr;
206
9.34k
      file_ptr where;
207
208
      /* Check that we don't have a totally silly number of
209
   program headers.  */
210
9.34k
      if (i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (x_phdr)
211
9.34k
    || i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (i_phdr))
212
21
  goto wrong;
213
214
9.32k
      where = (file_ptr)(i_ehdrp->e_phoff + (i_ehdrp->e_phnum - 1) * sizeof (x_phdr));
215
9.32k
      if ((bfd_size_type) where <= i_ehdrp->e_phoff)
216
10
  goto wrong;
217
218
9.31k
      if (bfd_seek (abfd, where, SEEK_SET) != 0)
219
13
  goto fail;
220
9.30k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
221
56
  goto fail;
222
9.30k
    }
223
224
  /* Move to the start of the program headers.  */
225
9.65k
  if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0)
226
10
    goto wrong;
227
228
  /* Allocate space for the program headers.  */
229
9.64k
  amt = sizeof (*i_phdrp) * i_ehdrp->e_phnum;
230
9.64k
  i_phdrp = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
231
9.64k
  if (!i_phdrp)
232
0
    goto fail;
233
234
9.64k
  elf_tdata (abfd)->phdr = i_phdrp;
235
236
  /* Read and convert to internal form.  */
237
562k
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
238
552k
    {
239
552k
      Elf_External_Phdr x_phdr;
240
241
552k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
242
14
  goto fail;
243
244
552k
      elf_swap_phdr_in (abfd, &x_phdr, i_phdrp + phindex);
245
552k
    }
246
247
  /* Set the machine architecture.  Do this before processing the
248
     program headers since we need to know the architecture type
249
     when processing the notes of some systems' core files.  */
250
9.63k
  if (! bfd_default_set_arch_mach (abfd, ebd->arch, 0)
251
      /* It's OK if this fails for the generic target.  */
252
9.63k
      && ebd->elf_machine_code != EM_NONE)
253
0
    goto fail;
254
255
  /* Let the backend double check the format and override global
256
     information.  We do this before processing the program headers
257
     to allow the correct machine (as opposed to just the default
258
     machine) to be set, making it possible for grok_prstatus and
259
     grok_psinfo to rely on the mach setting.  */
260
9.63k
  if (ebd->elf_backend_object_p != NULL
261
9.63k
      && ! ebd->elf_backend_object_p (abfd))
262
1.20k
    goto wrong;
263
264
  /* Process each program header.  */
265
471k
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
266
463k
    if (! bfd_section_from_phdr (abfd, i_phdrp + phindex, (int) phindex))
267
720
      goto fail;
268
269
  /* Check for core truncation.  */
270
7.70k
  filesize = bfd_get_file_size (abfd);
271
7.70k
  if (filesize != 0)
272
7.70k
    {
273
12.5k
      for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
274
12.3k
  {
275
12.3k
    Elf_Internal_Phdr *p = i_phdrp + phindex;
276
12.3k
    if (p->p_filesz
277
12.3k
        && (p->p_offset >= filesize
278
10.3k
      || p->p_filesz > filesize - p->p_offset))
279
7.49k
      {
280
7.49k
        _bfd_error_handler (_("warning: %pB has a segment "
281
7.49k
            "extending past end of file"), abfd);
282
7.49k
        abfd->read_only = 1;
283
7.49k
        break;
284
7.49k
      }
285
12.3k
      }
286
7.70k
  }
287
288
  /* Save the entry point from the ELF header.  */
289
7.70k
  abfd->start_address = i_ehdrp->e_entry;
290
7.70k
  return _bfd_no_cleanup;
291
292
1.88M
 wrong:
293
1.88M
  bfd_set_error (bfd_error_wrong_format);
294
1.89M
 fail:
295
1.89M
  return NULL;
296
1.88M
}
bfd_elf64_core_file_p
Line
Count
Source
88
474k
{
89
474k
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.  */
90
474k
  Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form.  */
91
474k
  Elf_Internal_Phdr *i_phdrp; /* Elf program header, internal form.  */
92
474k
  unsigned int phindex;
93
474k
  const struct elf_backend_data *ebd;
94
474k
  bfd_size_type amt;
95
474k
  ufile_ptr filesize;
96
97
  /* Read in the ELF header in external format.  */
98
474k
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
99
151k
    {
100
151k
      if (bfd_get_error () != bfd_error_system_call)
101
151k
  goto wrong;
102
264
      else
103
264
  goto fail;
104
151k
    }
105
106
  /* Check the magic number.  */
107
323k
  if (! elf_file_p (&x_ehdr))
108
145k
    goto wrong;
109
110
  /* FIXME: Check EI_VERSION here !  */
111
112
  /* Check the address size ("class").  */
113
177k
  if (x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
114
107k
    goto wrong;
115
116
  /* Check the byteorder.  */
117
69.9k
  switch (x_ehdr.e_ident[EI_DATA])
118
69.9k
    {
119
5.80k
    case ELFDATA2MSB:   /* Big-endian.  */
120
5.80k
      if (! bfd_big_endian (abfd))
121
3.16k
  goto wrong;
122
2.64k
      break;
123
64.0k
    case ELFDATA2LSB:   /* Little-endian.  */
124
64.0k
      if (! bfd_little_endian (abfd))
125
28.9k
  goto wrong;
126
35.0k
      break;
127
35.0k
    default:
128
176
      goto wrong;
129
69.9k
    }
130
131
  /* Give abfd an elf_obj_tdata.  */
132
37.7k
  if (! (*abfd->xvec->_bfd_set_format[bfd_core]) (abfd))
133
0
    goto fail;
134
135
  /* Swap in the rest of the header, now that we have the byte order.  */
136
37.7k
  i_ehdrp = elf_elfheader (abfd);
137
37.7k
  elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
138
139
#if DEBUG & 1
140
  elf_debug_file (i_ehdrp);
141
#endif
142
143
37.7k
  ebd = get_elf_backend_data (abfd);
144
145
  /* Check that the ELF e_machine field matches what this particular
146
     BFD format expects.  */
147
148
37.7k
  if (ebd->elf_machine_code != i_ehdrp->e_machine
149
37.7k
      && (ebd->elf_machine_alt1 == 0
150
33.2k
    || i_ehdrp->e_machine != ebd->elf_machine_alt1)
151
37.7k
      && (ebd->elf_machine_alt2 == 0
152
33.2k
    || i_ehdrp->e_machine != ebd->elf_machine_alt2)
153
37.7k
      && ebd->elf_machine_code != EM_NONE)
154
31.7k
    goto wrong;
155
156
5.99k
  if (ebd->elf_machine_code != EM_NONE
157
5.99k
      && i_ehdrp->e_ident[EI_OSABI] != ebd->elf_osabi
158
5.99k
      && ebd->elf_osabi != ELFOSABI_NONE)
159
1.39k
    goto wrong;
160
161
  /* If there is no program header, or the type is not a core file, then
162
     we are hosed.  */
163
4.59k
  if (i_ehdrp->e_phoff == 0 || i_ehdrp->e_type != ET_CORE)
164
3.11k
    goto wrong;
165
166
  /* Does BFD's idea of the phdr size match the size
167
     recorded in the file? */
168
1.48k
  if (i_ehdrp->e_phentsize != sizeof (Elf_External_Phdr))
169
8
    goto wrong;
170
171
  /* If the program header count is PN_XNUM(0xffff), the actual
172
     count is in the first section header.  */
173
1.47k
  if (i_ehdrp->e_shoff != 0 && i_ehdrp->e_phnum == PN_XNUM)
174
201
    {
175
201
      Elf_External_Shdr x_shdr;
176
201
      Elf_Internal_Shdr i_shdr;
177
201
      file_ptr where = (file_ptr) i_ehdrp->e_shoff;
178
179
201
      if (i_ehdrp->e_shoff < sizeof (x_ehdr))
180
8
  goto wrong;
181
182
      /* Seek to the section header table in the file.  */
183
193
      if (bfd_seek (abfd, where, SEEK_SET) != 0)
184
9
  goto fail;
185
186
      /* Read the first section header at index 0, and convert to internal
187
   form.  */
188
184
      if (bfd_read (&x_shdr, sizeof (x_shdr), abfd) != sizeof (x_shdr))
189
11
  goto fail;
190
173
      elf_swap_shdr_in (abfd, &x_shdr, &i_shdr);
191
192
173
      if (i_shdr.sh_info != 0)
193
166
  {
194
166
    i_ehdrp->e_phnum = i_shdr.sh_info;
195
166
    if (i_ehdrp->e_phnum != i_shdr.sh_info)
196
0
      goto wrong;
197
166
  }
198
173
    }
199
200
  /* Sanity check that we can read all of the program headers.
201
     It ought to be good enough to just read the last one.  */
202
1.44k
  if (i_ehdrp->e_phnum > 1)
203
1.24k
    {
204
1.24k
      Elf_External_Phdr x_phdr;
205
1.24k
      Elf_Internal_Phdr i_phdr;
206
1.24k
      file_ptr where;
207
208
      /* Check that we don't have a totally silly number of
209
   program headers.  */
210
1.24k
      if (i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (x_phdr)
211
1.24k
    || i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (i_phdr))
212
11
  goto wrong;
213
214
1.23k
      where = (file_ptr)(i_ehdrp->e_phoff + (i_ehdrp->e_phnum - 1) * sizeof (x_phdr));
215
1.23k
      if ((bfd_size_type) where <= i_ehdrp->e_phoff)
216
10
  goto wrong;
217
218
1.22k
      if (bfd_seek (abfd, where, SEEK_SET) != 0)
219
13
  goto fail;
220
1.21k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
221
27
  goto fail;
222
1.21k
    }
223
224
  /* Move to the start of the program headers.  */
225
1.38k
  if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0)
226
10
    goto wrong;
227
228
  /* Allocate space for the program headers.  */
229
1.37k
  amt = sizeof (*i_phdrp) * i_ehdrp->e_phnum;
230
1.37k
  i_phdrp = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
231
1.37k
  if (!i_phdrp)
232
0
    goto fail;
233
234
1.37k
  elf_tdata (abfd)->phdr = i_phdrp;
235
236
  /* Read and convert to internal form.  */
237
171k
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
238
170k
    {
239
170k
      Elf_External_Phdr x_phdr;
240
241
170k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
242
8
  goto fail;
243
244
170k
      elf_swap_phdr_in (abfd, &x_phdr, i_phdrp + phindex);
245
170k
    }
246
247
  /* Set the machine architecture.  Do this before processing the
248
     program headers since we need to know the architecture type
249
     when processing the notes of some systems' core files.  */
250
1.36k
  if (! bfd_default_set_arch_mach (abfd, ebd->arch, 0)
251
      /* It's OK if this fails for the generic target.  */
252
1.36k
      && ebd->elf_machine_code != EM_NONE)
253
0
    goto fail;
254
255
  /* Let the backend double check the format and override global
256
     information.  We do this before processing the program headers
257
     to allow the correct machine (as opposed to just the default
258
     machine) to be set, making it possible for grok_prstatus and
259
     grok_psinfo to rely on the mach setting.  */
260
1.36k
  if (ebd->elf_backend_object_p != NULL
261
1.36k
      && ! ebd->elf_backend_object_p (abfd))
262
1
    goto wrong;
263
264
  /* Process each program header.  */
265
152k
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
266
151k
    if (! bfd_section_from_phdr (abfd, i_phdrp + phindex, (int) phindex))
267
351
      goto fail;
268
269
  /* Check for core truncation.  */
270
1.01k
  filesize = bfd_get_file_size (abfd);
271
1.01k
  if (filesize != 0)
272
1.01k
    {
273
3.02k
      for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
274
2.92k
  {
275
2.92k
    Elf_Internal_Phdr *p = i_phdrp + phindex;
276
2.92k
    if (p->p_filesz
277
2.92k
        && (p->p_offset >= filesize
278
1.89k
      || p->p_filesz > filesize - p->p_offset))
279
917
      {
280
917
        _bfd_error_handler (_("warning: %pB has a segment "
281
917
            "extending past end of file"), abfd);
282
917
        abfd->read_only = 1;
283
917
        break;
284
917
      }
285
2.92k
      }
286
1.01k
  }
287
288
  /* Save the entry point from the ELF header.  */
289
1.01k
  abfd->start_address = i_ehdrp->e_entry;
290
1.01k
  return _bfd_no_cleanup;
291
292
473k
 wrong:
293
473k
  bfd_set_error (bfd_error_wrong_format);
294
473k
 fail:
295
473k
  return NULL;
296
473k
}
bfd_elf32_core_file_p
Line
Count
Source
88
1.42M
{
89
1.42M
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.  */
90
1.42M
  Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form.  */
91
1.42M
  Elf_Internal_Phdr *i_phdrp; /* Elf program header, internal form.  */
92
1.42M
  unsigned int phindex;
93
1.42M
  const struct elf_backend_data *ebd;
94
1.42M
  bfd_size_type amt;
95
1.42M
  ufile_ptr filesize;
96
97
  /* Read in the ELF header in external format.  */
98
1.42M
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
99
270k
    {
100
270k
      if (bfd_get_error () != bfd_error_system_call)
101
269k
  goto wrong;
102
792
      else
103
792
  goto fail;
104
270k
    }
105
106
  /* Check the magic number.  */
107
1.15M
  if (! elf_file_p (&x_ehdr))
108
493k
    goto wrong;
109
110
  /* FIXME: Check EI_VERSION here !  */
111
112
  /* Check the address size ("class").  */
113
661k
  if (x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
114
209k
    goto wrong;
115
116
  /* Check the byteorder.  */
117
451k
  switch (x_ehdr.e_ident[EI_DATA])
118
451k
    {
119
305k
    case ELFDATA2MSB:   /* Big-endian.  */
120
305k
      if (! bfd_big_endian (abfd))
121
151k
  goto wrong;
122
153k
      break;
123
153k
    case ELFDATA2LSB:   /* Little-endian.  */
124
145k
      if (! bfd_little_endian (abfd))
125
72.6k
  goto wrong;
126
72.6k
      break;
127
72.6k
    default:
128
792
      goto wrong;
129
451k
    }
130
131
  /* Give abfd an elf_obj_tdata.  */
132
226k
  if (! (*abfd->xvec->_bfd_set_format[bfd_core]) (abfd))
133
0
    goto fail;
134
135
  /* Swap in the rest of the header, now that we have the byte order.  */
136
226k
  i_ehdrp = elf_elfheader (abfd);
137
226k
  elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
138
139
#if DEBUG & 1
140
  elf_debug_file (i_ehdrp);
141
#endif
142
143
226k
  ebd = get_elf_backend_data (abfd);
144
145
  /* Check that the ELF e_machine field matches what this particular
146
     BFD format expects.  */
147
148
226k
  if (ebd->elf_machine_code != i_ehdrp->e_machine
149
226k
      && (ebd->elf_machine_alt1 == 0
150
219k
    || i_ehdrp->e_machine != ebd->elf_machine_alt1)
151
226k
      && (ebd->elf_machine_alt2 == 0
152
219k
    || i_ehdrp->e_machine != ebd->elf_machine_alt2)
153
226k
      && ebd->elf_machine_code != EM_NONE)
154
215k
    goto wrong;
155
156
10.4k
  if (ebd->elf_machine_code != EM_NONE
157
10.4k
      && i_ehdrp->e_ident[EI_OSABI] != ebd->elf_osabi
158
10.4k
      && ebd->elf_osabi != ELFOSABI_NONE)
159
487
    goto wrong;
160
161
  /* If there is no program header, or the type is not a core file, then
162
     we are hosed.  */
163
10.0k
  if (i_ehdrp->e_phoff == 0 || i_ehdrp->e_type != ET_CORE)
164
1.67k
    goto wrong;
165
166
  /* Does BFD's idea of the phdr size match the size
167
     recorded in the file? */
168
8.33k
  if (i_ehdrp->e_phentsize != sizeof (Elf_External_Phdr))
169
12
    goto wrong;
170
171
  /* If the program header count is PN_XNUM(0xffff), the actual
172
     count is in the first section header.  */
173
8.32k
  if (i_ehdrp->e_shoff != 0 && i_ehdrp->e_phnum == PN_XNUM)
174
43
    {
175
43
      Elf_External_Shdr x_shdr;
176
43
      Elf_Internal_Shdr i_shdr;
177
43
      file_ptr where = (file_ptr) i_ehdrp->e_shoff;
178
179
43
      if (i_ehdrp->e_shoff < sizeof (x_ehdr))
180
1
  goto wrong;
181
182
      /* Seek to the section header table in the file.  */
183
42
      if (bfd_seek (abfd, where, SEEK_SET) != 0)
184
0
  goto fail;
185
186
      /* Read the first section header at index 0, and convert to internal
187
   form.  */
188
42
      if (bfd_read (&x_shdr, sizeof (x_shdr), abfd) != sizeof (x_shdr))
189
9
  goto fail;
190
33
      elf_swap_shdr_in (abfd, &x_shdr, &i_shdr);
191
192
33
      if (i_shdr.sh_info != 0)
193
28
  {
194
28
    i_ehdrp->e_phnum = i_shdr.sh_info;
195
28
    if (i_ehdrp->e_phnum != i_shdr.sh_info)
196
0
      goto wrong;
197
28
  }
198
33
    }
199
200
  /* Sanity check that we can read all of the program headers.
201
     It ought to be good enough to just read the last one.  */
202
8.31k
  if (i_ehdrp->e_phnum > 1)
203
8.09k
    {
204
8.09k
      Elf_External_Phdr x_phdr;
205
8.09k
      Elf_Internal_Phdr i_phdr;
206
8.09k
      file_ptr where;
207
208
      /* Check that we don't have a totally silly number of
209
   program headers.  */
210
8.09k
      if (i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (x_phdr)
211
8.09k
    || i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (i_phdr))
212
10
  goto wrong;
213
214
8.08k
      where = (file_ptr)(i_ehdrp->e_phoff + (i_ehdrp->e_phnum - 1) * sizeof (x_phdr));
215
8.08k
      if ((bfd_size_type) where <= i_ehdrp->e_phoff)
216
0
  goto wrong;
217
218
8.08k
      if (bfd_seek (abfd, where, SEEK_SET) != 0)
219
0
  goto fail;
220
8.08k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
221
29
  goto fail;
222
8.08k
    }
223
224
  /* Move to the start of the program headers.  */
225
8.27k
  if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0)
226
0
    goto wrong;
227
228
  /* Allocate space for the program headers.  */
229
8.27k
  amt = sizeof (*i_phdrp) * i_ehdrp->e_phnum;
230
8.27k
  i_phdrp = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
231
8.27k
  if (!i_phdrp)
232
0
    goto fail;
233
234
8.27k
  elf_tdata (abfd)->phdr = i_phdrp;
235
236
  /* Read and convert to internal form.  */
237
390k
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
238
382k
    {
239
382k
      Elf_External_Phdr x_phdr;
240
241
382k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
242
6
  goto fail;
243
244
382k
      elf_swap_phdr_in (abfd, &x_phdr, i_phdrp + phindex);
245
382k
    }
246
247
  /* Set the machine architecture.  Do this before processing the
248
     program headers since we need to know the architecture type
249
     when processing the notes of some systems' core files.  */
250
8.26k
  if (! bfd_default_set_arch_mach (abfd, ebd->arch, 0)
251
      /* It's OK if this fails for the generic target.  */
252
8.26k
      && ebd->elf_machine_code != EM_NONE)
253
0
    goto fail;
254
255
  /* Let the backend double check the format and override global
256
     information.  We do this before processing the program headers
257
     to allow the correct machine (as opposed to just the default
258
     machine) to be set, making it possible for grok_prstatus and
259
     grok_psinfo to rely on the mach setting.  */
260
8.26k
  if (ebd->elf_backend_object_p != NULL
261
8.26k
      && ! ebd->elf_backend_object_p (abfd))
262
1.20k
    goto wrong;
263
264
  /* Process each program header.  */
265
318k
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
266
312k
    if (! bfd_section_from_phdr (abfd, i_phdrp + phindex, (int) phindex))
267
369
      goto fail;
268
269
  /* Check for core truncation.  */
270
6.69k
  filesize = bfd_get_file_size (abfd);
271
6.69k
  if (filesize != 0)
272
6.69k
    {
273
9.54k
      for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
274
9.43k
  {
275
9.43k
    Elf_Internal_Phdr *p = i_phdrp + phindex;
276
9.43k
    if (p->p_filesz
277
9.43k
        && (p->p_offset >= filesize
278
8.47k
      || p->p_filesz > filesize - p->p_offset))
279
6.58k
      {
280
6.58k
        _bfd_error_handler (_("warning: %pB has a segment "
281
6.58k
            "extending past end of file"), abfd);
282
6.58k
        abfd->read_only = 1;
283
6.58k
        break;
284
6.58k
      }
285
9.43k
      }
286
6.69k
  }
287
288
  /* Save the entry point from the ELF header.  */
289
6.69k
  abfd->start_address = i_ehdrp->e_entry;
290
6.69k
  return _bfd_no_cleanup;
291
292
1.41M
 wrong:
293
1.41M
  bfd_set_error (bfd_error_wrong_format);
294
1.41M
 fail:
295
1.41M
  return NULL;
296
1.41M
}
297
298
/* Attempt to find a build-id in a core file from the core file BFD.
299
   OFFSET is the file offset to a PT_LOAD segment that may contain
300
   the build-id note.  Returns TRUE upon success, FALSE otherwise.  */
301
302
bool
303
NAME(_bfd_elf, core_find_build_id)
304
  (bfd *abfd,
305
   bfd_vma offset)
306
4.63k
{
307
4.63k
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.   */
308
4.63k
  Elf_Internal_Ehdr i_ehdr; /* Elf file header, internal form.   */
309
4.63k
  Elf_Internal_Phdr *i_phdr;
310
4.63k
  unsigned int i;
311
4.63k
  size_t amt;
312
313
  /* Seek to the position of the segment at OFFSET.  */
314
4.63k
  if (bfd_seek (abfd, offset, SEEK_SET) != 0)
315
758
    goto fail;
316
317
  /* Read in the ELF header in external format.  */
318
3.87k
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
319
960
    {
320
960
      if (bfd_get_error () != bfd_error_system_call)
321
960
  goto wrong;
322
0
      else
323
0
  goto fail;
324
960
    }
325
326
  /* Now check to see if we have a valid ELF file, and one that BFD can
327
     make use of.  The magic number must match, the address size ('class')
328
     and byte-swapping must match our XVEC entry, and it must have a
329
     section header table (FIXME: See comments re sections at top of this
330
     file).  */
331
2.91k
  if (! elf_file_p (&x_ehdr)
332
2.91k
      || x_ehdr.e_ident[EI_VERSION] != EV_CURRENT
333
2.91k
      || x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
334
1.84k
    goto wrong;
335
336
  /* Check that file's byte order matches xvec's.  */
337
1.07k
  switch (x_ehdr.e_ident[EI_DATA])
338
1.07k
    {
339
469
    case ELFDATA2MSB:   /* Big-endian.  */
340
469
      if (! bfd_header_big_endian (abfd))
341
0
  goto wrong;
342
469
      break;
343
601
    case ELFDATA2LSB:   /* Little-endian.  */
344
601
      if (! bfd_header_little_endian (abfd))
345
0
  goto wrong;
346
601
      break;
347
601
    case ELFDATANONE:   /* No data encoding specified.  */
348
0
    default:      /* Unknown data encoding specified . */
349
0
      goto wrong;
350
1.07k
    }
351
352
1.07k
  elf_swap_ehdr_in (abfd, &x_ehdr, &i_ehdr);
353
#if DEBUG
354
  elf_debug_file (&i_ehdr);
355
#endif
356
357
1.07k
  if (i_ehdr.e_phentsize != sizeof (Elf_External_Phdr) || i_ehdr.e_phnum == 0)
358
4
    goto fail;
359
360
  /* Read in program headers.  */
361
1.06k
  if (_bfd_mul_overflow (i_ehdr.e_phnum, sizeof (*i_phdr), &amt))
362
0
    {
363
0
      bfd_set_error (bfd_error_file_too_big);
364
0
      goto fail;
365
0
    }
366
1.06k
  i_phdr = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
367
1.06k
  if (i_phdr == NULL)
368
0
    goto fail;
369
370
1.06k
  if (bfd_seek (abfd, offset + i_ehdr.e_phoff, SEEK_SET) != 0)
371
0
    goto fail;
372
373
  /* Read in program headers and parse notes.  */
374
1.07M
  for (i = 0; i < i_ehdr.e_phnum; ++i, ++i_phdr)
375
1.07M
    {
376
1.07M
      Elf_External_Phdr x_phdr;
377
378
1.07M
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
379
301
  goto fail;
380
1.07M
      elf_swap_phdr_in (abfd, &x_phdr, i_phdr);
381
382
1.07M
      if (i_phdr->p_type == PT_NOTE && i_phdr->p_filesz > 0)
383
3.11k
  {
384
3.11k
    elf_read_notes (abfd, offset + i_phdr->p_offset,
385
3.11k
        i_phdr->p_filesz, i_phdr->p_align);
386
387
    /* Make sure ABFD returns to processing the program headers.  */
388
3.11k
    if (bfd_seek (abfd,
389
3.11k
      offset + i_ehdr.e_phoff + (i + 1) * sizeof (x_phdr),
390
3.11k
      SEEK_SET) != 0)
391
0
      goto fail;
392
393
3.11k
    if (abfd->build_id != NULL)
394
18
      return true;
395
3.11k
  }
396
1.07M
    }
397
398
  /* Having gotten this far, we have a valid ELF section, but no
399
     build-id was found.  */
400
747
  goto fail;
401
402
2.80k
 wrong:
403
2.80k
  bfd_set_error (bfd_error_wrong_format);
404
4.61k
 fail:
405
4.61k
  return false;
406
2.80k
}
_bfd_elf64_core_find_build_id
Line
Count
Source
306
2.45k
{
307
2.45k
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.   */
308
2.45k
  Elf_Internal_Ehdr i_ehdr; /* Elf file header, internal form.   */
309
2.45k
  Elf_Internal_Phdr *i_phdr;
310
2.45k
  unsigned int i;
311
2.45k
  size_t amt;
312
313
  /* Seek to the position of the segment at OFFSET.  */
314
2.45k
  if (bfd_seek (abfd, offset, SEEK_SET) != 0)
315
758
    goto fail;
316
317
  /* Read in the ELF header in external format.  */
318
1.70k
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
319
285
    {
320
285
      if (bfd_get_error () != bfd_error_system_call)
321
285
  goto wrong;
322
0
      else
323
0
  goto fail;
324
285
    }
325
326
  /* Now check to see if we have a valid ELF file, and one that BFD can
327
     make use of.  The magic number must match, the address size ('class')
328
     and byte-swapping must match our XVEC entry, and it must have a
329
     section header table (FIXME: See comments re sections at top of this
330
     file).  */
331
1.41k
  if (! elf_file_p (&x_ehdr)
332
1.41k
      || x_ehdr.e_ident[EI_VERSION] != EV_CURRENT
333
1.41k
      || x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
334
871
    goto wrong;
335
336
  /* Check that file's byte order matches xvec's.  */
337
545
  switch (x_ehdr.e_ident[EI_DATA])
338
545
    {
339
0
    case ELFDATA2MSB:   /* Big-endian.  */
340
0
      if (! bfd_header_big_endian (abfd))
341
0
  goto wrong;
342
0
      break;
343
545
    case ELFDATA2LSB:   /* Little-endian.  */
344
545
      if (! bfd_header_little_endian (abfd))
345
0
  goto wrong;
346
545
      break;
347
545
    case ELFDATANONE:   /* No data encoding specified.  */
348
0
    default:      /* Unknown data encoding specified . */
349
0
      goto wrong;
350
545
    }
351
352
545
  elf_swap_ehdr_in (abfd, &x_ehdr, &i_ehdr);
353
#if DEBUG
354
  elf_debug_file (&i_ehdr);
355
#endif
356
357
545
  if (i_ehdr.e_phentsize != sizeof (Elf_External_Phdr) || i_ehdr.e_phnum == 0)
358
4
    goto fail;
359
360
  /* Read in program headers.  */
361
541
  if (_bfd_mul_overflow (i_ehdr.e_phnum, sizeof (*i_phdr), &amt))
362
0
    {
363
0
      bfd_set_error (bfd_error_file_too_big);
364
0
      goto fail;
365
0
    }
366
541
  i_phdr = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
367
541
  if (i_phdr == NULL)
368
0
    goto fail;
369
370
541
  if (bfd_seek (abfd, offset + i_ehdr.e_phoff, SEEK_SET) != 0)
371
0
    goto fail;
372
373
  /* Read in program headers and parse notes.  */
374
365k
  for (i = 0; i < i_ehdr.e_phnum; ++i, ++i_phdr)
375
364k
    {
376
364k
      Elf_External_Phdr x_phdr;
377
378
364k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
379
264
  goto fail;
380
364k
      elf_swap_phdr_in (abfd, &x_phdr, i_phdr);
381
382
364k
      if (i_phdr->p_type == PT_NOTE && i_phdr->p_filesz > 0)
383
1.00k
  {
384
1.00k
    elf_read_notes (abfd, offset + i_phdr->p_offset,
385
1.00k
        i_phdr->p_filesz, i_phdr->p_align);
386
387
    /* Make sure ABFD returns to processing the program headers.  */
388
1.00k
    if (bfd_seek (abfd,
389
1.00k
      offset + i_ehdr.e_phoff + (i + 1) * sizeof (x_phdr),
390
1.00k
      SEEK_SET) != 0)
391
0
      goto fail;
392
393
1.00k
    if (abfd->build_id != NULL)
394
12
      return true;
395
1.00k
  }
396
364k
    }
397
398
  /* Having gotten this far, we have a valid ELF section, but no
399
     build-id was found.  */
400
265
  goto fail;
401
402
1.15k
 wrong:
403
1.15k
  bfd_set_error (bfd_error_wrong_format);
404
2.44k
 fail:
405
2.44k
  return false;
406
1.15k
}
_bfd_elf32_core_find_build_id
Line
Count
Source
306
2.17k
{
307
2.17k
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.   */
308
2.17k
  Elf_Internal_Ehdr i_ehdr; /* Elf file header, internal form.   */
309
2.17k
  Elf_Internal_Phdr *i_phdr;
310
2.17k
  unsigned int i;
311
2.17k
  size_t amt;
312
313
  /* Seek to the position of the segment at OFFSET.  */
314
2.17k
  if (bfd_seek (abfd, offset, SEEK_SET) != 0)
315
0
    goto fail;
316
317
  /* Read in the ELF header in external format.  */
318
2.17k
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
319
675
    {
320
675
      if (bfd_get_error () != bfd_error_system_call)
321
675
  goto wrong;
322
0
      else
323
0
  goto fail;
324
675
    }
325
326
  /* Now check to see if we have a valid ELF file, and one that BFD can
327
     make use of.  The magic number must match, the address size ('class')
328
     and byte-swapping must match our XVEC entry, and it must have a
329
     section header table (FIXME: See comments re sections at top of this
330
     file).  */
331
1.49k
  if (! elf_file_p (&x_ehdr)
332
1.49k
      || x_ehdr.e_ident[EI_VERSION] != EV_CURRENT
333
1.49k
      || x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
334
972
    goto wrong;
335
336
  /* Check that file's byte order matches xvec's.  */
337
525
  switch (x_ehdr.e_ident[EI_DATA])
338
525
    {
339
469
    case ELFDATA2MSB:   /* Big-endian.  */
340
469
      if (! bfd_header_big_endian (abfd))
341
0
  goto wrong;
342
469
      break;
343
469
    case ELFDATA2LSB:   /* Little-endian.  */
344
56
      if (! bfd_header_little_endian (abfd))
345
0
  goto wrong;
346
56
      break;
347
56
    case ELFDATANONE:   /* No data encoding specified.  */
348
0
    default:      /* Unknown data encoding specified . */
349
0
      goto wrong;
350
525
    }
351
352
525
  elf_swap_ehdr_in (abfd, &x_ehdr, &i_ehdr);
353
#if DEBUG
354
  elf_debug_file (&i_ehdr);
355
#endif
356
357
525
  if (i_ehdr.e_phentsize != sizeof (Elf_External_Phdr) || i_ehdr.e_phnum == 0)
358
0
    goto fail;
359
360
  /* Read in program headers.  */
361
525
  if (_bfd_mul_overflow (i_ehdr.e_phnum, sizeof (*i_phdr), &amt))
362
0
    {
363
0
      bfd_set_error (bfd_error_file_too_big);
364
0
      goto fail;
365
0
    }
366
525
  i_phdr = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
367
525
  if (i_phdr == NULL)
368
0
    goto fail;
369
370
525
  if (bfd_seek (abfd, offset + i_ehdr.e_phoff, SEEK_SET) != 0)
371
0
    goto fail;
372
373
  /* Read in program headers and parse notes.  */
374
712k
  for (i = 0; i < i_ehdr.e_phnum; ++i, ++i_phdr)
375
711k
    {
376
711k
      Elf_External_Phdr x_phdr;
377
378
711k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
379
37
  goto fail;
380
711k
      elf_swap_phdr_in (abfd, &x_phdr, i_phdr);
381
382
711k
      if (i_phdr->p_type == PT_NOTE && i_phdr->p_filesz > 0)
383
2.10k
  {
384
2.10k
    elf_read_notes (abfd, offset + i_phdr->p_offset,
385
2.10k
        i_phdr->p_filesz, i_phdr->p_align);
386
387
    /* Make sure ABFD returns to processing the program headers.  */
388
2.10k
    if (bfd_seek (abfd,
389
2.10k
      offset + i_ehdr.e_phoff + (i + 1) * sizeof (x_phdr),
390
2.10k
      SEEK_SET) != 0)
391
0
      goto fail;
392
393
2.10k
    if (abfd->build_id != NULL)
394
6
      return true;
395
2.10k
  }
396
711k
    }
397
398
  /* Having gotten this far, we have a valid ELF section, but no
399
     build-id was found.  */
400
482
  goto fail;
401
402
1.64k
 wrong:
403
1.64k
  bfd_set_error (bfd_error_wrong_format);
404
2.16k
 fail:
405
2.16k
  return false;
406
1.64k
}