Coverage Report

Created: 2025-06-24 06:45

/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
2.93M
{
89
2.93M
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.  */
90
2.93M
  Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form.  */
91
2.93M
  Elf_Internal_Phdr *i_phdrp; /* Elf program header, internal form.  */
92
2.93M
  unsigned int phindex;
93
2.93M
  const struct elf_backend_data *ebd;
94
2.93M
  bfd_size_type amt;
95
2.93M
  ufile_ptr filesize;
96
97
  /* Read in the ELF header in external format.  */
98
2.93M
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
99
544k
    {
100
544k
      if (bfd_get_error () != bfd_error_system_call)
101
542k
  goto wrong;
102
1.76k
      else
103
1.76k
  goto fail;
104
544k
    }
105
106
  /* Check the magic number.  */
107
2.39M
  if (! elf_file_p (&x_ehdr))
108
894k
    goto wrong;
109
110
  /* FIXME: Check EI_VERSION here !  */
111
112
  /* Check the address size ("class").  */
113
1.49M
  if (x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
114
520k
    goto wrong;
115
116
  /* Check the byteorder.  */
117
978k
  switch (x_ehdr.e_ident[EI_DATA])
118
978k
    {
119
715k
    case ELFDATA2MSB:   /* Big-endian.  */
120
715k
      if (! bfd_big_endian (abfd))
121
356k
  goto wrong;
122
359k
      break;
123
359k
    case ELFDATA2LSB:   /* Little-endian.  */
124
261k
      if (! bfd_little_endian (abfd))
125
126k
  goto wrong;
126
134k
      break;
127
134k
    default:
128
880
      goto wrong;
129
978k
    }
130
131
  /* Give abfd an elf_obj_tdata.  */
132
494k
  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
494k
  i_ehdrp = elf_elfheader (abfd);
137
494k
  elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
138
139
#if DEBUG & 1
140
  elf_debug_file (i_ehdrp);
141
#endif
142
143
494k
  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
494k
  if (ebd->elf_machine_code != i_ehdrp->e_machine
149
494k
      && (ebd->elf_machine_alt1 == 0
150
474k
    || i_ehdrp->e_machine != ebd->elf_machine_alt1)
151
494k
      && (ebd->elf_machine_alt2 == 0
152
473k
    || i_ehdrp->e_machine != ebd->elf_machine_alt2)
153
494k
      && ebd->elf_machine_code != EM_NONE)
154
464k
    goto wrong;
155
156
29.7k
  if (ebd->elf_machine_code != EM_NONE
157
29.7k
      && i_ehdrp->e_ident[EI_OSABI] != ebd->elf_osabi
158
29.7k
      && ebd->elf_osabi != ELFOSABI_NONE)
159
2.22k
    goto wrong;
160
161
  /* If there is no program header, or the type is not a core file, then
162
     we are hosed.  */
163
27.5k
  if (i_ehdrp->e_phoff == 0 || i_ehdrp->e_type != ET_CORE)
164
5.10k
    goto wrong;
165
166
  /* Does BFD's idea of the phdr size match the size
167
     recorded in the file? */
168
22.4k
  if (i_ehdrp->e_phentsize != sizeof (Elf_External_Phdr))
169
41
    goto wrong;
170
171
  /* If the program header count is PN_XNUM(0xffff), the actual
172
     count is in the first section header.  */
173
22.3k
  if (i_ehdrp->e_shoff != 0 && i_ehdrp->e_phnum == PN_XNUM)
174
340
    {
175
340
      Elf_External_Shdr x_shdr;
176
340
      Elf_Internal_Shdr i_shdr;
177
340
      file_ptr where = (file_ptr) i_ehdrp->e_shoff;
178
179
340
      if (i_ehdrp->e_shoff < sizeof (x_ehdr))
180
23
  goto wrong;
181
182
      /* Seek to the section header table in the file.  */
183
317
      if (bfd_seek (abfd, where, SEEK_SET) != 0)
184
23
  goto fail;
185
186
      /* Read the first section header at index 0, and convert to internal
187
   form.  */
188
294
      if (bfd_read (&x_shdr, sizeof (x_shdr), abfd) != sizeof (x_shdr))
189
32
  goto fail;
190
262
      elf_swap_shdr_in (abfd, &x_shdr, &i_shdr);
191
192
262
      if (i_shdr.sh_info != 0)
193
250
  {
194
250
    i_ehdrp->e_phnum = i_shdr.sh_info;
195
250
    if (i_ehdrp->e_phnum != i_shdr.sh_info)
196
0
      goto wrong;
197
250
  }
198
262
    }
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
22.2k
  if (i_ehdrp->e_phnum > 1)
203
21.5k
    {
204
21.5k
      Elf_External_Phdr x_phdr;
205
21.5k
      Elf_Internal_Phdr i_phdr;
206
21.5k
      file_ptr where;
207
208
      /* Check that we don't have a totally silly number of
209
   program headers.  */
210
21.5k
      if (i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (x_phdr)
211
21.5k
    || i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (i_phdr))
212
29
  goto wrong;
213
214
21.4k
      where = (file_ptr)(i_ehdrp->e_phoff + (i_ehdrp->e_phnum - 1) * sizeof (x_phdr));
215
21.4k
      if ((bfd_size_type) where <= i_ehdrp->e_phoff)
216
12
  goto wrong;
217
218
21.4k
      if (bfd_seek (abfd, where, SEEK_SET) != 0)
219
20
  goto fail;
220
21.4k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
221
68
  goto fail;
222
21.4k
    }
223
224
  /* Move to the start of the program headers.  */
225
22.1k
  if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0)
226
10
    goto wrong;
227
228
  /* Allocate space for the program headers.  */
229
22.1k
  amt = sizeof (*i_phdrp) * i_ehdrp->e_phnum;
230
22.1k
  i_phdrp = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
231
22.1k
  if (!i_phdrp)
232
0
    goto fail;
233
234
22.1k
  elf_tdata (abfd)->phdr = i_phdrp;
235
236
  /* Read and convert to internal form.  */
237
1.26M
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
238
1.24M
    {
239
1.24M
      Elf_External_Phdr x_phdr;
240
241
1.24M
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
242
31
  goto fail;
243
244
1.24M
      elf_swap_phdr_in (abfd, &x_phdr, i_phdrp + phindex);
245
1.24M
    }
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
22.1k
  if (! bfd_default_set_arch_mach (abfd, ebd->arch, 0)
251
      /* It's OK if this fails for the generic target.  */
252
22.1k
      && ebd->elf_machine_code != EM_NONE)
253
3
    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
22.1k
  if (ebd->elf_backend_object_p != NULL
261
22.1k
      && ! ebd->elf_backend_object_p (abfd))
262
2.87k
    goto wrong;
263
264
  /* Process each program header.  */
265
1.06M
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
266
1.04M
    if (! bfd_section_from_phdr (abfd, i_phdrp + phindex, (int) phindex))
267
1.33k
      goto fail;
268
269
  /* Check for core truncation.  */
270
17.9k
  filesize = bfd_get_file_size (abfd);
271
17.9k
  if (filesize != 0)
272
17.9k
    {
273
25.3k
      for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
274
24.9k
  {
275
24.9k
    Elf_Internal_Phdr *p = i_phdrp + phindex;
276
24.9k
    if (p->p_filesz
277
24.9k
        && (p->p_offset >= filesize
278
22.7k
      || p->p_filesz > filesize - p->p_offset))
279
17.5k
      {
280
17.5k
        _bfd_error_handler (_("warning: %pB has a segment "
281
17.5k
            "extending past end of file"), abfd);
282
17.5k
        abfd->read_only = 1;
283
17.5k
        break;
284
17.5k
      }
285
24.9k
      }
286
17.9k
  }
287
288
  /* Save the entry point from the ELF header.  */
289
17.9k
  abfd->start_address = i_ehdrp->e_entry;
290
17.9k
  return _bfd_no_cleanup;
291
292
2.91M
 wrong:
293
2.91M
  bfd_set_error (bfd_error_wrong_format);
294
2.91M
 fail:
295
2.91M
  return NULL;
296
2.91M
}
bfd_elf64_core_file_p
Line
Count
Source
88
733k
{
89
733k
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.  */
90
733k
  Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form.  */
91
733k
  Elf_Internal_Phdr *i_phdrp; /* Elf program header, internal form.  */
92
733k
  unsigned int phindex;
93
733k
  const struct elf_backend_data *ebd;
94
733k
  bfd_size_type amt;
95
733k
  ufile_ptr filesize;
96
97
  /* Read in the ELF header in external format.  */
98
733k
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
99
195k
    {
100
195k
      if (bfd_get_error () != bfd_error_system_call)
101
195k
  goto wrong;
102
440
      else
103
440
  goto fail;
104
195k
    }
105
106
  /* Check the magic number.  */
107
538k
  if (! elf_file_p (&x_ehdr))
108
202k
    goto wrong;
109
110
  /* FIXME: Check EI_VERSION here !  */
111
112
  /* Check the address size ("class").  */
113
335k
  if (x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
114
243k
    goto wrong;
115
116
  /* Check the byteorder.  */
117
92.8k
  switch (x_ehdr.e_ident[EI_DATA])
118
92.8k
    {
119
10.8k
    case ELFDATA2MSB:   /* Big-endian.  */
120
10.8k
      if (! bfd_big_endian (abfd))
121
5.92k
  goto wrong;
122
4.94k
      break;
123
81.8k
    case ELFDATA2LSB:   /* Little-endian.  */
124
81.8k
      if (! bfd_little_endian (abfd))
125
36.9k
  goto wrong;
126
44.9k
      break;
127
44.9k
    default:
128
88
      goto wrong;
129
92.8k
    }
130
131
  /* Give abfd an elf_obj_tdata.  */
132
49.8k
  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
49.8k
  i_ehdrp = elf_elfheader (abfd);
137
49.8k
  elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
138
139
#if DEBUG & 1
140
  elf_debug_file (i_ehdrp);
141
#endif
142
143
49.8k
  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
49.8k
  if (ebd->elf_machine_code != i_ehdrp->e_machine
149
49.8k
      && (ebd->elf_machine_alt1 == 0
150
44.9k
    || i_ehdrp->e_machine != ebd->elf_machine_alt1)
151
49.8k
      && (ebd->elf_machine_alt2 == 0
152
44.9k
    || i_ehdrp->e_machine != ebd->elf_machine_alt2)
153
49.8k
      && ebd->elf_machine_code != EM_NONE)
154
42.9k
    goto wrong;
155
156
6.92k
  if (ebd->elf_machine_code != EM_NONE
157
6.92k
      && i_ehdrp->e_ident[EI_OSABI] != ebd->elf_osabi
158
6.92k
      && ebd->elf_osabi != ELFOSABI_NONE)
159
1.24k
    goto wrong;
160
161
  /* If there is no program header, or the type is not a core file, then
162
     we are hosed.  */
163
5.67k
  if (i_ehdrp->e_phoff == 0 || i_ehdrp->e_type != ET_CORE)
164
2.95k
    goto wrong;
165
166
  /* Does BFD's idea of the phdr size match the size
167
     recorded in the file? */
168
2.72k
  if (i_ehdrp->e_phentsize != sizeof (Elf_External_Phdr))
169
17
    goto wrong;
170
171
  /* If the program header count is PN_XNUM(0xffff), the actual
172
     count is in the first section header.  */
173
2.70k
  if (i_ehdrp->e_shoff != 0 && i_ehdrp->e_phnum == PN_XNUM)
174
276
    {
175
276
      Elf_External_Shdr x_shdr;
176
276
      Elf_Internal_Shdr i_shdr;
177
276
      file_ptr where = (file_ptr) i_ehdrp->e_shoff;
178
179
276
      if (i_ehdrp->e_shoff < sizeof (x_ehdr))
180
11
  goto wrong;
181
182
      /* Seek to the section header table in the file.  */
183
265
      if (bfd_seek (abfd, where, SEEK_SET) != 0)
184
23
  goto fail;
185
186
      /* Read the first section header at index 0, and convert to internal
187
   form.  */
188
242
      if (bfd_read (&x_shdr, sizeof (x_shdr), abfd) != sizeof (x_shdr))
189
12
  goto fail;
190
230
      elf_swap_shdr_in (abfd, &x_shdr, &i_shdr);
191
192
230
      if (i_shdr.sh_info != 0)
193
224
  {
194
224
    i_ehdrp->e_phnum = i_shdr.sh_info;
195
224
    if (i_ehdrp->e_phnum != i_shdr.sh_info)
196
0
      goto wrong;
197
224
  }
198
230
    }
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
2.65k
  if (i_ehdrp->e_phnum > 1)
203
2.30k
    {
204
2.30k
      Elf_External_Phdr x_phdr;
205
2.30k
      Elf_Internal_Phdr i_phdr;
206
2.30k
      file_ptr where;
207
208
      /* Check that we don't have a totally silly number of
209
   program headers.  */
210
2.30k
      if (i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (x_phdr)
211
2.30k
    || i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (i_phdr))
212
12
  goto wrong;
213
214
2.29k
      where = (file_ptr)(i_ehdrp->e_phoff + (i_ehdrp->e_phnum - 1) * sizeof (x_phdr));
215
2.29k
      if ((bfd_size_type) where <= i_ehdrp->e_phoff)
216
12
  goto wrong;
217
218
2.27k
      if (bfd_seek (abfd, where, SEEK_SET) != 0)
219
20
  goto fail;
220
2.25k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
221
22
  goto fail;
222
2.25k
    }
223
224
  /* Move to the start of the program headers.  */
225
2.59k
  if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0)
226
10
    goto wrong;
227
228
  /* Allocate space for the program headers.  */
229
2.58k
  amt = sizeof (*i_phdrp) * i_ehdrp->e_phnum;
230
2.58k
  i_phdrp = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
231
2.58k
  if (!i_phdrp)
232
0
    goto fail;
233
234
2.58k
  elf_tdata (abfd)->phdr = i_phdrp;
235
236
  /* Read and convert to internal form.  */
237
349k
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
238
346k
    {
239
346k
      Elf_External_Phdr x_phdr;
240
241
346k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
242
11
  goto fail;
243
244
346k
      elf_swap_phdr_in (abfd, &x_phdr, i_phdrp + phindex);
245
346k
    }
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
2.57k
  if (! bfd_default_set_arch_mach (abfd, ebd->arch, 0)
251
      /* It's OK if this fails for the generic target.  */
252
2.57k
      && 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
2.57k
  if (ebd->elf_backend_object_p != NULL
261
2.57k
      && ! ebd->elf_backend_object_p (abfd))
262
1
    goto wrong;
263
264
  /* Process each program header.  */
265
321k
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
266
319k
    if (! bfd_section_from_phdr (abfd, i_phdrp + phindex, (int) phindex))
267
516
      goto fail;
268
269
  /* Check for core truncation.  */
270
2.05k
  filesize = bfd_get_file_size (abfd);
271
2.05k
  if (filesize != 0)
272
2.05k
    {
273
2.83k
      for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
274
2.68k
  {
275
2.68k
    Elf_Internal_Phdr *p = i_phdrp + phindex;
276
2.68k
    if (p->p_filesz
277
2.68k
        && (p->p_offset >= filesize
278
2.38k
      || p->p_filesz > filesize - p->p_offset))
279
1.89k
      {
280
1.89k
        _bfd_error_handler (_("warning: %pB has a segment "
281
1.89k
            "extending past end of file"), abfd);
282
1.89k
        abfd->read_only = 1;
283
1.89k
        break;
284
1.89k
      }
285
2.68k
      }
286
2.05k
  }
287
288
  /* Save the entry point from the ELF header.  */
289
2.05k
  abfd->start_address = i_ehdrp->e_entry;
290
2.05k
  return _bfd_no_cleanup;
291
292
730k
 wrong:
293
730k
  bfd_set_error (bfd_error_wrong_format);
294
731k
 fail:
295
731k
  return NULL;
296
730k
}
bfd_elf32_core_file_p
Line
Count
Source
88
2.20M
{
89
2.20M
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.  */
90
2.20M
  Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form.  */
91
2.20M
  Elf_Internal_Phdr *i_phdrp; /* Elf program header, internal form.  */
92
2.20M
  unsigned int phindex;
93
2.20M
  const struct elf_backend_data *ebd;
94
2.20M
  bfd_size_type amt;
95
2.20M
  ufile_ptr filesize;
96
97
  /* Read in the ELF header in external format.  */
98
2.20M
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
99
348k
    {
100
348k
      if (bfd_get_error () != bfd_error_system_call)
101
347k
  goto wrong;
102
1.32k
      else
103
1.32k
  goto fail;
104
348k
    }
105
106
  /* Check the magic number.  */
107
1.85M
  if (! elf_file_p (&x_ehdr))
108
691k
    goto wrong;
109
110
  /* FIXME: Check EI_VERSION here !  */
111
112
  /* Check the address size ("class").  */
113
1.16M
  if (x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
114
277k
    goto wrong;
115
116
  /* Check the byteorder.  */
117
885k
  switch (x_ehdr.e_ident[EI_DATA])
118
885k
    {
119
704k
    case ELFDATA2MSB:   /* Big-endian.  */
120
704k
      if (! bfd_big_endian (abfd))
121
350k
  goto wrong;
122
354k
      break;
123
354k
    case ELFDATA2LSB:   /* Little-endian.  */
124
179k
      if (! bfd_little_endian (abfd))
125
89.9k
  goto wrong;
126
89.9k
      break;
127
89.9k
    default:
128
792
      goto wrong;
129
885k
    }
130
131
  /* Give abfd an elf_obj_tdata.  */
132
444k
  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
444k
  i_ehdrp = elf_elfheader (abfd);
137
444k
  elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
138
139
#if DEBUG & 1
140
  elf_debug_file (i_ehdrp);
141
#endif
142
143
444k
  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
444k
  if (ebd->elf_machine_code != i_ehdrp->e_machine
149
444k
      && (ebd->elf_machine_alt1 == 0
150
429k
    || i_ehdrp->e_machine != ebd->elf_machine_alt1)
151
444k
      && (ebd->elf_machine_alt2 == 0
152
428k
    || i_ehdrp->e_machine != ebd->elf_machine_alt2)
153
444k
      && ebd->elf_machine_code != EM_NONE)
154
421k
    goto wrong;
155
156
22.8k
  if (ebd->elf_machine_code != EM_NONE
157
22.8k
      && i_ehdrp->e_ident[EI_OSABI] != ebd->elf_osabi
158
22.8k
      && ebd->elf_osabi != ELFOSABI_NONE)
159
975
    goto wrong;
160
161
  /* If there is no program header, or the type is not a core file, then
162
     we are hosed.  */
163
21.8k
  if (i_ehdrp->e_phoff == 0 || i_ehdrp->e_type != ET_CORE)
164
2.14k
    goto wrong;
165
166
  /* Does BFD's idea of the phdr size match the size
167
     recorded in the file? */
168
19.6k
  if (i_ehdrp->e_phentsize != sizeof (Elf_External_Phdr))
169
24
    goto wrong;
170
171
  /* If the program header count is PN_XNUM(0xffff), the actual
172
     count is in the first section header.  */
173
19.6k
  if (i_ehdrp->e_shoff != 0 && i_ehdrp->e_phnum == PN_XNUM)
174
64
    {
175
64
      Elf_External_Shdr x_shdr;
176
64
      Elf_Internal_Shdr i_shdr;
177
64
      file_ptr where = (file_ptr) i_ehdrp->e_shoff;
178
179
64
      if (i_ehdrp->e_shoff < sizeof (x_ehdr))
180
12
  goto wrong;
181
182
      /* Seek to the section header table in the file.  */
183
52
      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
52
      if (bfd_read (&x_shdr, sizeof (x_shdr), abfd) != sizeof (x_shdr))
189
20
  goto fail;
190
32
      elf_swap_shdr_in (abfd, &x_shdr, &i_shdr);
191
192
32
      if (i_shdr.sh_info != 0)
193
26
  {
194
26
    i_ehdrp->e_phnum = i_shdr.sh_info;
195
26
    if (i_ehdrp->e_phnum != i_shdr.sh_info)
196
0
      goto wrong;
197
26
  }
198
32
    }
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
19.6k
  if (i_ehdrp->e_phnum > 1)
203
19.2k
    {
204
19.2k
      Elf_External_Phdr x_phdr;
205
19.2k
      Elf_Internal_Phdr i_phdr;
206
19.2k
      file_ptr where;
207
208
      /* Check that we don't have a totally silly number of
209
   program headers.  */
210
19.2k
      if (i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (x_phdr)
211
19.2k
    || i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (i_phdr))
212
17
  goto wrong;
213
214
19.2k
      where = (file_ptr)(i_ehdrp->e_phoff + (i_ehdrp->e_phnum - 1) * sizeof (x_phdr));
215
19.2k
      if ((bfd_size_type) where <= i_ehdrp->e_phoff)
216
0
  goto wrong;
217
218
19.2k
      if (bfd_seek (abfd, where, SEEK_SET) != 0)
219
0
  goto fail;
220
19.2k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
221
46
  goto fail;
222
19.2k
    }
223
224
  /* Move to the start of the program headers.  */
225
19.5k
  if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0)
226
0
    goto wrong;
227
228
  /* Allocate space for the program headers.  */
229
19.5k
  amt = sizeof (*i_phdrp) * i_ehdrp->e_phnum;
230
19.5k
  i_phdrp = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
231
19.5k
  if (!i_phdrp)
232
0
    goto fail;
233
234
19.5k
  elf_tdata (abfd)->phdr = i_phdrp;
235
236
  /* Read and convert to internal form.  */
237
913k
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
238
894k
    {
239
894k
      Elf_External_Phdr x_phdr;
240
241
894k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
242
20
  goto fail;
243
244
894k
      elf_swap_phdr_in (abfd, &x_phdr, i_phdrp + phindex);
245
894k
    }
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
19.5k
  if (! bfd_default_set_arch_mach (abfd, ebd->arch, 0)
251
      /* It's OK if this fails for the generic target.  */
252
19.5k
      && ebd->elf_machine_code != EM_NONE)
253
3
    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
19.5k
  if (ebd->elf_backend_object_p != NULL
261
19.5k
      && ! ebd->elf_backend_object_p (abfd))
262
2.87k
    goto wrong;
263
264
  /* Process each program header.  */
265
746k
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
266
730k
    if (! bfd_section_from_phdr (abfd, i_phdrp + phindex, (int) phindex))
267
817
      goto fail;
268
269
  /* Check for core truncation.  */
270
15.8k
  filesize = bfd_get_file_size (abfd);
271
15.8k
  if (filesize != 0)
272
15.8k
    {
273
22.5k
      for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
274
22.3k
  {
275
22.3k
    Elf_Internal_Phdr *p = i_phdrp + phindex;
276
22.3k
    if (p->p_filesz
277
22.3k
        && (p->p_offset >= filesize
278
20.3k
      || p->p_filesz > filesize - p->p_offset))
279
15.6k
      {
280
15.6k
        _bfd_error_handler (_("warning: %pB has a segment "
281
15.6k
            "extending past end of file"), abfd);
282
15.6k
        abfd->read_only = 1;
283
15.6k
        break;
284
15.6k
      }
285
22.3k
      }
286
15.8k
  }
287
288
  /* Save the entry point from the ELF header.  */
289
15.8k
  abfd->start_address = i_ehdrp->e_entry;
290
15.8k
  return _bfd_no_cleanup;
291
292
2.18M
 wrong:
293
2.18M
  bfd_set_error (bfd_error_wrong_format);
294
2.18M
 fail:
295
2.18M
  return NULL;
296
2.18M
}
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
13.1k
{
307
13.1k
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.   */
308
13.1k
  Elf_Internal_Ehdr i_ehdr; /* Elf file header, internal form.   */
309
13.1k
  Elf_Internal_Phdr *i_phdr;
310
13.1k
  unsigned int i;
311
13.1k
  size_t amt;
312
313
  /* Seek to the position of the segment at OFFSET.  */
314
13.1k
  if (bfd_seek (abfd, offset, SEEK_SET) != 0)
315
1.74k
    goto fail;
316
317
  /* Read in the ELF header in external format.  */
318
11.4k
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
319
2.83k
    {
320
2.83k
      if (bfd_get_error () != bfd_error_system_call)
321
2.83k
  goto wrong;
322
0
      else
323
0
  goto fail;
324
2.83k
    }
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
8.56k
  if (! elf_file_p (&x_ehdr)
332
8.56k
      || x_ehdr.e_ident[EI_VERSION] != EV_CURRENT
333
8.56k
      || x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
334
6.81k
    goto wrong;
335
336
  /* Check that file's byte order matches xvec's.  */
337
1.75k
  switch (x_ehdr.e_ident[EI_DATA])
338
1.75k
    {
339
846
    case ELFDATA2MSB:   /* Big-endian.  */
340
846
      if (! bfd_header_big_endian (abfd))
341
9
  goto wrong;
342
837
      break;
343
908
    case ELFDATA2LSB:   /* Little-endian.  */
344
908
      if (! bfd_header_little_endian (abfd))
345
0
  goto wrong;
346
908
      break;
347
908
    case ELFDATANONE:   /* No data encoding specified.  */
348
0
    default:      /* Unknown data encoding specified . */
349
0
      goto wrong;
350
1.75k
    }
351
352
1.74k
  elf_swap_ehdr_in (abfd, &x_ehdr, &i_ehdr);
353
#if DEBUG
354
  elf_debug_file (&i_ehdr);
355
#endif
356
357
1.74k
  if (i_ehdr.e_phentsize != sizeof (Elf_External_Phdr) || i_ehdr.e_phnum == 0)
358
10
    goto fail;
359
360
  /* Read in program headers.  */
361
1.73k
  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.73k
  i_phdr = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
367
1.73k
  if (i_phdr == NULL)
368
0
    goto fail;
369
370
1.73k
  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.27M
  for (i = 0; i < i_ehdr.e_phnum; ++i, ++i_phdr)
375
1.27M
    {
376
1.27M
      Elf_External_Phdr x_phdr;
377
378
1.27M
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
379
469
  goto fail;
380
1.27M
      elf_swap_phdr_in (abfd, &x_phdr, i_phdr);
381
382
1.27M
      if (i_phdr->p_type == PT_NOTE && i_phdr->p_filesz > 0)
383
3.79k
  {
384
3.79k
    elf_read_notes (abfd, offset + i_phdr->p_offset,
385
3.79k
        i_phdr->p_filesz, i_phdr->p_align);
386
387
    /* Make sure ABFD returns to processing the program headers.  */
388
3.79k
    if (bfd_seek (abfd,
389
3.79k
      offset + i_ehdr.e_phoff + (i + 1) * sizeof (x_phdr),
390
3.79k
      SEEK_SET) != 0)
391
0
      goto fail;
392
393
3.79k
    if (abfd->build_id != NULL)
394
13
      return true;
395
3.79k
  }
396
1.27M
    }
397
398
  /* Having gotten this far, we have a valid ELF section, but no
399
     build-id was found.  */
400
1.25k
  goto fail;
401
402
9.65k
 wrong:
403
9.65k
  bfd_set_error (bfd_error_wrong_format);
404
13.1k
 fail:
405
13.1k
  return false;
406
9.65k
}
_bfd_elf64_core_find_build_id
Line
Count
Source
306
8.17k
{
307
8.17k
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.   */
308
8.17k
  Elf_Internal_Ehdr i_ehdr; /* Elf file header, internal form.   */
309
8.17k
  Elf_Internal_Phdr *i_phdr;
310
8.17k
  unsigned int i;
311
8.17k
  size_t amt;
312
313
  /* Seek to the position of the segment at OFFSET.  */
314
8.17k
  if (bfd_seek (abfd, offset, SEEK_SET) != 0)
315
1.74k
    goto fail;
316
317
  /* Read in the ELF header in external format.  */
318
6.42k
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
319
1.20k
    {
320
1.20k
      if (bfd_get_error () != bfd_error_system_call)
321
1.20k
  goto wrong;
322
0
      else
323
0
  goto fail;
324
1.20k
    }
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
5.21k
  if (! elf_file_p (&x_ehdr)
332
5.21k
      || x_ehdr.e_ident[EI_VERSION] != EV_CURRENT
333
5.21k
      || x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
334
4.37k
    goto wrong;
335
336
  /* Check that file's byte order matches xvec's.  */
337
840
  switch (x_ehdr.e_ident[EI_DATA])
338
840
    {
339
9
    case ELFDATA2MSB:   /* Big-endian.  */
340
9
      if (! bfd_header_big_endian (abfd))
341
9
  goto wrong;
342
0
      break;
343
831
    case ELFDATA2LSB:   /* Little-endian.  */
344
831
      if (! bfd_header_little_endian (abfd))
345
0
  goto wrong;
346
831
      break;
347
831
    case ELFDATANONE:   /* No data encoding specified.  */
348
0
    default:      /* Unknown data encoding specified . */
349
0
      goto wrong;
350
840
    }
351
352
831
  elf_swap_ehdr_in (abfd, &x_ehdr, &i_ehdr);
353
#if DEBUG
354
  elf_debug_file (&i_ehdr);
355
#endif
356
357
831
  if (i_ehdr.e_phentsize != sizeof (Elf_External_Phdr) || i_ehdr.e_phnum == 0)
358
10
    goto fail;
359
360
  /* Read in program headers.  */
361
821
  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
821
  i_phdr = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
367
821
  if (i_phdr == NULL)
368
0
    goto fail;
369
370
821
  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
601k
  for (i = 0; i < i_ehdr.e_phnum; ++i, ++i_phdr)
375
600k
    {
376
600k
      Elf_External_Phdr x_phdr;
377
378
600k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
379
412
  goto fail;
380
600k
      elf_swap_phdr_in (abfd, &x_phdr, i_phdr);
381
382
600k
      if (i_phdr->p_type == PT_NOTE && i_phdr->p_filesz > 0)
383
1.49k
  {
384
1.49k
    elf_read_notes (abfd, offset + i_phdr->p_offset,
385
1.49k
        i_phdr->p_filesz, i_phdr->p_align);
386
387
    /* Make sure ABFD returns to processing the program headers.  */
388
1.49k
    if (bfd_seek (abfd,
389
1.49k
      offset + i_ehdr.e_phoff + (i + 1) * sizeof (x_phdr),
390
1.49k
      SEEK_SET) != 0)
391
0
      goto fail;
392
393
1.49k
    if (abfd->build_id != NULL)
394
1
      return true;
395
1.49k
  }
396
600k
    }
397
398
  /* Having gotten this far, we have a valid ELF section, but no
399
     build-id was found.  */
400
408
  goto fail;
401
402
5.59k
 wrong:
403
5.59k
  bfd_set_error (bfd_error_wrong_format);
404
8.17k
 fail:
405
8.17k
  return false;
406
5.59k
}
_bfd_elf32_core_find_build_id
Line
Count
Source
306
4.97k
{
307
4.97k
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.   */
308
4.97k
  Elf_Internal_Ehdr i_ehdr; /* Elf file header, internal form.   */
309
4.97k
  Elf_Internal_Phdr *i_phdr;
310
4.97k
  unsigned int i;
311
4.97k
  size_t amt;
312
313
  /* Seek to the position of the segment at OFFSET.  */
314
4.97k
  if (bfd_seek (abfd, offset, SEEK_SET) != 0)
315
0
    goto fail;
316
317
  /* Read in the ELF header in external format.  */
318
4.97k
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
319
1.62k
    {
320
1.62k
      if (bfd_get_error () != bfd_error_system_call)
321
1.62k
  goto wrong;
322
0
      else
323
0
  goto fail;
324
1.62k
    }
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
3.35k
  if (! elf_file_p (&x_ehdr)
332
3.35k
      || x_ehdr.e_ident[EI_VERSION] != EV_CURRENT
333
3.35k
      || x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
334
2.43k
    goto wrong;
335
336
  /* Check that file's byte order matches xvec's.  */
337
914
  switch (x_ehdr.e_ident[EI_DATA])
338
914
    {
339
837
    case ELFDATA2MSB:   /* Big-endian.  */
340
837
      if (! bfd_header_big_endian (abfd))
341
0
  goto wrong;
342
837
      break;
343
837
    case ELFDATA2LSB:   /* Little-endian.  */
344
77
      if (! bfd_header_little_endian (abfd))
345
0
  goto wrong;
346
77
      break;
347
77
    case ELFDATANONE:   /* No data encoding specified.  */
348
0
    default:      /* Unknown data encoding specified . */
349
0
      goto wrong;
350
914
    }
351
352
914
  elf_swap_ehdr_in (abfd, &x_ehdr, &i_ehdr);
353
#if DEBUG
354
  elf_debug_file (&i_ehdr);
355
#endif
356
357
914
  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
914
  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
914
  i_phdr = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
367
914
  if (i_phdr == NULL)
368
0
    goto fail;
369
370
914
  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
674k
  for (i = 0; i < i_ehdr.e_phnum; ++i, ++i_phdr)
375
673k
    {
376
673k
      Elf_External_Phdr x_phdr;
377
378
673k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
379
57
  goto fail;
380
673k
      elf_swap_phdr_in (abfd, &x_phdr, i_phdr);
381
382
673k
      if (i_phdr->p_type == PT_NOTE && i_phdr->p_filesz > 0)
383
2.29k
  {
384
2.29k
    elf_read_notes (abfd, offset + i_phdr->p_offset,
385
2.29k
        i_phdr->p_filesz, i_phdr->p_align);
386
387
    /* Make sure ABFD returns to processing the program headers.  */
388
2.29k
    if (bfd_seek (abfd,
389
2.29k
      offset + i_ehdr.e_phoff + (i + 1) * sizeof (x_phdr),
390
2.29k
      SEEK_SET) != 0)
391
0
      goto fail;
392
393
2.29k
    if (abfd->build_id != NULL)
394
12
      return true;
395
2.29k
  }
396
673k
    }
397
398
  /* Having gotten this far, we have a valid ELF section, but no
399
     build-id was found.  */
400
845
  goto fail;
401
402
4.06k
 wrong:
403
4.06k
  bfd_set_error (bfd_error_wrong_format);
404
4.96k
 fail:
405
4.96k
  return false;
406
4.06k
}