Coverage Report

Created: 2026-09-14 08:07

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
3.74M
{
89
3.74M
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.  */
90
3.74M
  Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form.  */
91
3.74M
  Elf_Internal_Phdr *i_phdrp; /* Elf program header, internal form.  */
92
3.74M
  unsigned int phindex;
93
3.74M
  bfd_size_type amt;
94
3.74M
  ufile_ptr filesize;
95
96
  /* Read in the ELF header in external format.  */
97
3.74M
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
98
127k
    {
99
127k
      if (bfd_get_error () != bfd_error_system_call)
100
127k
  goto wrong;
101
344
      else
102
344
  goto fail;
103
127k
    }
104
105
  /* Check the magic number.  */
106
3.61M
  if (! elf_file_p (&x_ehdr))
107
890k
    goto wrong;
108
109
  /* FIXME: Check EI_VERSION here !  */
110
111
  /* Check the address size ("class").  */
112
2.72M
  if (x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
113
756k
    goto wrong;
114
115
  /* Check the byteorder.  */
116
1.96M
  switch (x_ehdr.e_ident[EI_DATA])
117
1.96M
    {
118
1.43M
    case ELFDATA2MSB:   /* Big-endian.  */
119
1.43M
      if (! bfd_big_endian (abfd))
120
714k
  goto wrong;
121
723k
      break;
122
723k
    case ELFDATA2LSB:   /* Little-endian.  */
123
530k
      if (! bfd_little_endian (abfd))
124
262k
  goto wrong;
125
268k
      break;
126
268k
    default:
127
792
      goto wrong;
128
1.96M
    }
129
130
  /* Give abfd an elf_obj_tdata.  */
131
991k
  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
991k
  i_ehdrp = elf_elfheader (abfd);
136
991k
  elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
137
138
#if DEBUG & 1
139
  elf_debug_file (i_ehdrp);
140
#endif
141
142
991k
  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
991k
  if (ebd->elf_machine_code != i_ehdrp->e_machine
148
956k
      && (ebd->elf_machine_alt1 == 0
149
288k
    || i_ehdrp->e_machine != ebd->elf_machine_alt1)
150
955k
      && (ebd->elf_machine_alt2 == 0
151
50.6k
    || i_ehdrp->e_machine != ebd->elf_machine_alt2)
152
954k
      && ebd->elf_machine_code != EM_NONE)
153
938k
    goto wrong;
154
155
53.0k
  if (ebd->osabi_exact && i_ehdrp->e_ident[EI_OSABI] != ebd->elf_osabi)
156
2.30k
    goto wrong;
157
158
  /* If there is no program header, or the type is not a core file, then
159
     we are hosed.  */
160
50.7k
  if (i_ehdrp->e_phoff == 0 || i_ehdrp->e_type != ET_CORE)
161
6.59k
    goto wrong;
162
163
  /* Does BFD's idea of the phdr size match the size
164
     recorded in the file? */
165
44.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
44.1k
  if (i_ehdrp->e_shoff != 0 && i_ehdrp->e_phnum == PN_XNUM)
171
144
    {
172
144
      Elf_External_Shdr x_shdr;
173
144
      Elf_Internal_Shdr i_shdr;
174
144
      file_ptr where = (file_ptr) i_ehdrp->e_shoff;
175
176
144
      if (i_ehdrp->e_shoff < sizeof (x_ehdr))
177
4
  goto wrong;
178
179
      /* Seek to the section header table in the file.  */
180
140
      if (bfd_seek (abfd, where, SEEK_SET) != 0)
181
2
  goto fail;
182
183
      /* Read the first section header at index 0, and convert to internal
184
   form.  */
185
138
      if (bfd_read (&x_shdr, sizeof (x_shdr), abfd) != sizeof (x_shdr))
186
4
  goto fail;
187
134
      elf_swap_shdr_in (abfd, &x_shdr, &i_shdr);
188
189
134
      if (i_shdr.sh_info != 0)
190
120
  {
191
120
    i_ehdrp->e_phnum = i_shdr.sh_info;
192
120
    if (i_ehdrp->e_phnum != i_shdr.sh_info)
193
0
      goto wrong;
194
120
  }
195
134
    }
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
44.1k
  if (i_ehdrp->e_phnum > 1)
200
43.4k
    {
201
43.4k
      Elf_External_Phdr x_phdr;
202
43.4k
      Elf_Internal_Phdr i_phdr;
203
43.4k
      file_ptr where;
204
205
      /* Check that we don't have a totally silly number of
206
   program headers.  */
207
43.4k
      if (i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (x_phdr)
208
43.4k
    || i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (i_phdr))
209
52
  goto wrong;
210
211
43.4k
      where = (file_ptr)(i_ehdrp->e_phoff + (i_ehdrp->e_phnum - 1) * sizeof (x_phdr));
212
43.4k
      if ((bfd_size_type) where <= i_ehdrp->e_phoff)
213
3
  goto wrong;
214
215
43.4k
      if (bfd_seek (abfd, where, SEEK_SET) != 0)
216
10
  goto fail;
217
43.4k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
218
16
  goto fail;
219
43.4k
    }
220
221
  /* Move to the start of the program headers.  */
222
44.0k
  if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0)
223
0
    goto wrong;
224
225
  /* Allocate space for the program headers.  */
226
44.0k
  amt = sizeof (*i_phdrp) * i_ehdrp->e_phnum;
227
44.0k
  i_phdrp = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
228
44.0k
  if (!i_phdrp)
229
0
    goto fail;
230
231
44.0k
  elf_tdata (abfd)->phdr = i_phdrp;
232
233
  /* Read and convert to internal form.  */
234
1.03M
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
235
990k
    {
236
990k
      Elf_External_Phdr x_phdr;
237
238
990k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
239
0
  goto fail;
240
241
990k
      elf_swap_phdr_in (abfd, &x_phdr, i_phdrp + phindex);
242
990k
    }
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
44.0k
  if (! bfd_default_set_arch_mach (abfd, ebd->arch, 0)
248
      /* It's OK if this fails for the generic target.  */
249
12.1k
      && 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
44.0k
  if (ebd->elf_backend_object_p != NULL
258
30.1k
      && ! ebd->elf_backend_object_p (abfd))
259
5.43k
    goto wrong;
260
261
  /* Process each program header.  */
262
898k
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
263
860k
    if (! bfd_section_from_phdr (abfd, i_phdrp + phindex, (int) phindex))
264
1.38k
      goto fail;
265
266
  /* Check for core truncation.  */
267
37.2k
  filesize = bfd_get_file_size (abfd);
268
37.2k
  if (filesize != 0)
269
37.2k
    {
270
52.4k
      for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
271
51.5k
  {
272
51.5k
    Elf_Internal_Phdr *p = i_phdrp + phindex;
273
51.5k
    if (p->p_filesz
274
46.8k
        && (p->p_offset >= filesize
275
18.8k
      || p->p_filesz > filesize - p->p_offset))
276
36.3k
      {
277
36.3k
        _bfd_error_handler (_("warning: %pB has a segment "
278
36.3k
            "extending past end of file"), abfd);
279
36.3k
        abfd->read_only = 1;
280
36.3k
        break;
281
36.3k
      }
282
51.5k
      }
283
37.2k
  }
284
285
  /* Save the entry point from the ELF header.  */
286
37.2k
  abfd->start_address = i_ehdrp->e_entry;
287
37.2k
  return _bfd_no_cleanup;
288
289
3.70M
 wrong:
290
3.70M
  bfd_set_error (bfd_error_wrong_format);
291
3.70M
 fail:
292
3.70M
  return NULL;
293
3.70M
}
bfd_elf64_core_file_p
Line
Count
Source
88
869k
{
89
869k
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.  */
90
869k
  Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form.  */
91
869k
  Elf_Internal_Phdr *i_phdrp; /* Elf program header, internal form.  */
92
869k
  unsigned int phindex;
93
869k
  bfd_size_type amt;
94
869k
  ufile_ptr filesize;
95
96
  /* Read in the ELF header in external format.  */
97
869k
  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
80
      else
102
80
  goto fail;
103
120k
    }
104
105
  /* Check the magic number.  */
106
749k
  if (! elf_file_p (&x_ehdr))
107
204k
    goto wrong;
108
109
  /* FIXME: Check EI_VERSION here !  */
110
111
  /* Check the address size ("class").  */
112
545k
  if (x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
113
451k
    goto wrong;
114
115
  /* Check the byteorder.  */
116
93.9k
  switch (x_ehdr.e_ident[EI_DATA])
117
93.9k
    {
118
7.23k
    case ELFDATA2MSB:   /* Big-endian.  */
119
7.23k
      if (! bfd_big_endian (abfd))
120
3.80k
  goto wrong;
121
3.43k
      break;
122
86.7k
    case ELFDATA2LSB:   /* Little-endian.  */
123
86.7k
      if (! bfd_little_endian (abfd))
124
40.5k
  goto wrong;
125
46.1k
      break;
126
46.1k
    default:
127
0
      goto wrong;
128
93.9k
    }
129
130
  /* Give abfd an elf_obj_tdata.  */
131
49.5k
  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
49.5k
  i_ehdrp = elf_elfheader (abfd);
136
49.5k
  elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
137
138
#if DEBUG & 1
139
  elf_debug_file (i_ehdrp);
140
#endif
141
142
49.5k
  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
49.5k
  if (ebd->elf_machine_code != i_ehdrp->e_machine
148
44.4k
      && (ebd->elf_machine_alt1 == 0
149
4.85k
    || i_ehdrp->e_machine != ebd->elf_machine_alt1)
150
44.3k
      && (ebd->elf_machine_alt2 == 0
151
4.09k
    || i_ehdrp->e_machine != ebd->elf_machine_alt2)
152
44.3k
      && ebd->elf_machine_code != EM_NONE)
153
42.0k
    goto wrong;
154
155
7.50k
  if (ebd->osabi_exact && i_ehdrp->e_ident[EI_OSABI] != ebd->elf_osabi)
156
887
    goto wrong;
157
158
  /* If there is no program header, or the type is not a core file, then
159
     we are hosed.  */
160
6.61k
  if (i_ehdrp->e_phoff == 0 || i_ehdrp->e_type != ET_CORE)
161
2.05k
    goto wrong;
162
163
  /* Does BFD's idea of the phdr size match the size
164
     recorded in the file? */
165
4.56k
  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
4.56k
  if (i_ehdrp->e_shoff != 0 && i_ehdrp->e_phnum == PN_XNUM)
171
81
    {
172
81
      Elf_External_Shdr x_shdr;
173
81
      Elf_Internal_Shdr i_shdr;
174
81
      file_ptr where = (file_ptr) i_ehdrp->e_shoff;
175
176
81
      if (i_ehdrp->e_shoff < sizeof (x_ehdr))
177
4
  goto wrong;
178
179
      /* Seek to the section header table in the file.  */
180
77
      if (bfd_seek (abfd, where, SEEK_SET) != 0)
181
2
  goto fail;
182
183
      /* Read the first section header at index 0, and convert to internal
184
   form.  */
185
75
      if (bfd_read (&x_shdr, sizeof (x_shdr), abfd) != sizeof (x_shdr))
186
4
  goto fail;
187
71
      elf_swap_shdr_in (abfd, &x_shdr, &i_shdr);
188
189
71
      if (i_shdr.sh_info != 0)
190
57
  {
191
57
    i_ehdrp->e_phnum = i_shdr.sh_info;
192
57
    if (i_ehdrp->e_phnum != i_shdr.sh_info)
193
0
      goto wrong;
194
57
  }
195
71
    }
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
4.55k
  if (i_ehdrp->e_phnum > 1)
200
3.89k
    {
201
3.89k
      Elf_External_Phdr x_phdr;
202
3.89k
      Elf_Internal_Phdr i_phdr;
203
3.89k
      file_ptr where;
204
205
      /* Check that we don't have a totally silly number of
206
   program headers.  */
207
3.89k
      if (i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (x_phdr)
208
3.85k
    || i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (i_phdr))
209
49
  goto wrong;
210
211
3.84k
      where = (file_ptr)(i_ehdrp->e_phoff + (i_ehdrp->e_phnum - 1) * sizeof (x_phdr));
212
3.84k
      if ((bfd_size_type) where <= i_ehdrp->e_phoff)
213
3
  goto wrong;
214
215
3.84k
      if (bfd_seek (abfd, where, SEEK_SET) != 0)
216
10
  goto fail;
217
3.83k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
218
9
  goto fail;
219
3.83k
    }
220
221
  /* Move to the start of the program headers.  */
222
4.48k
  if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0)
223
0
    goto wrong;
224
225
  /* Allocate space for the program headers.  */
226
4.48k
  amt = sizeof (*i_phdrp) * i_ehdrp->e_phnum;
227
4.48k
  i_phdrp = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
228
4.48k
  if (!i_phdrp)
229
0
    goto fail;
230
231
4.48k
  elf_tdata (abfd)->phdr = i_phdrp;
232
233
  /* Read and convert to internal form.  */
234
111k
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
235
107k
    {
236
107k
      Elf_External_Phdr x_phdr;
237
238
107k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
239
0
  goto fail;
240
241
107k
      elf_swap_phdr_in (abfd, &x_phdr, i_phdrp + phindex);
242
107k
    }
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
4.48k
  if (! bfd_default_set_arch_mach (abfd, ebd->arch, 0)
248
      /* It's OK if this fails for the generic target.  */
249
1.34k
      && 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
4.48k
  if (ebd->elf_backend_object_p != NULL
258
3.01k
      && ! ebd->elf_backend_object_p (abfd))
259
1
    goto wrong;
260
261
  /* Process each program header.  */
262
104k
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
263
100k
    if (! bfd_section_from_phdr (abfd, i_phdrp + phindex, (int) phindex))
264
806
      goto fail;
265
266
  /* Check for core truncation.  */
267
3.67k
  filesize = bfd_get_file_size (abfd);
268
3.67k
  if (filesize != 0)
269
3.67k
    {
270
7.63k
      for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
271
7.04k
  {
272
7.04k
    Elf_Internal_Phdr *p = i_phdrp + phindex;
273
7.04k
    if (p->p_filesz
274
5.62k
        && (p->p_offset >= filesize
275
3.33k
      || p->p_filesz > filesize - p->p_offset))
276
3.07k
      {
277
3.07k
        _bfd_error_handler (_("warning: %pB has a segment "
278
3.07k
            "extending past end of file"), abfd);
279
3.07k
        abfd->read_only = 1;
280
3.07k
        break;
281
3.07k
      }
282
7.04k
      }
283
3.67k
  }
284
285
  /* Save the entry point from the ELF header.  */
286
3.67k
  abfd->start_address = i_ehdrp->e_entry;
287
3.67k
  return _bfd_no_cleanup;
288
289
865k
 wrong:
290
865k
  bfd_set_error (bfd_error_wrong_format);
291
866k
 fail:
292
  return NULL;
293
865k
}
bfd_elf32_core_file_p
Line
Count
Source
88
2.87M
{
89
2.87M
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.  */
90
2.87M
  Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form.  */
91
2.87M
  Elf_Internal_Phdr *i_phdrp; /* Elf program header, internal form.  */
92
2.87M
  unsigned int phindex;
93
2.87M
  bfd_size_type amt;
94
2.87M
  ufile_ptr filesize;
95
96
  /* Read in the ELF header in external format.  */
97
2.87M
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
98
7.39k
    {
99
7.39k
      if (bfd_get_error () != bfd_error_system_call)
100
7.12k
  goto wrong;
101
264
      else
102
264
  goto fail;
103
7.39k
    }
104
105
  /* Check the magic number.  */
106
2.86M
  if (! elf_file_p (&x_ehdr))
107
685k
    goto wrong;
108
109
  /* FIXME: Check EI_VERSION here !  */
110
111
  /* Check the address size ("class").  */
112
2.18M
  if (x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
113
305k
    goto wrong;
114
115
  /* Check the byteorder.  */
116
1.87M
  switch (x_ehdr.e_ident[EI_DATA])
117
1.87M
    {
118
1.43M
    case ELFDATA2MSB:   /* Big-endian.  */
119
1.43M
      if (! bfd_big_endian (abfd))
120
710k
  goto wrong;
121
720k
      break;
122
720k
    case ELFDATA2LSB:   /* Little-endian.  */
123
444k
      if (! bfd_little_endian (abfd))
124
222k
  goto wrong;
125
222k
      break;
126
222k
    default:
127
792
      goto wrong;
128
1.87M
    }
129
130
  /* Give abfd an elf_obj_tdata.  */
131
942k
  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
942k
  i_ehdrp = elf_elfheader (abfd);
136
942k
  elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
137
138
#if DEBUG & 1
139
  elf_debug_file (i_ehdrp);
140
#endif
141
142
942k
  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
942k
  if (ebd->elf_machine_code != i_ehdrp->e_machine
148
912k
      && (ebd->elf_machine_alt1 == 0
149
284k
    || i_ehdrp->e_machine != ebd->elf_machine_alt1)
150
911k
      && (ebd->elf_machine_alt2 == 0
151
46.5k
    || i_ehdrp->e_machine != ebd->elf_machine_alt2)
152
910k
      && ebd->elf_machine_code != EM_NONE)
153
896k
    goto wrong;
154
155
45.5k
  if (ebd->osabi_exact && i_ehdrp->e_ident[EI_OSABI] != ebd->elf_osabi)
156
1.42k
    goto wrong;
157
158
  /* If there is no program header, or the type is not a core file, then
159
     we are hosed.  */
160
44.1k
  if (i_ehdrp->e_phoff == 0 || i_ehdrp->e_type != ET_CORE)
161
4.54k
    goto wrong;
162
163
  /* Does BFD's idea of the phdr size match the size
164
     recorded in the file? */
165
39.5k
  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
39.5k
  if (i_ehdrp->e_shoff != 0 && i_ehdrp->e_phnum == PN_XNUM)
171
63
    {
172
63
      Elf_External_Shdr x_shdr;
173
63
      Elf_Internal_Shdr i_shdr;
174
63
      file_ptr where = (file_ptr) i_ehdrp->e_shoff;
175
176
63
      if (i_ehdrp->e_shoff < sizeof (x_ehdr))
177
0
  goto wrong;
178
179
      /* Seek to the section header table in the file.  */
180
63
      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
63
      if (bfd_read (&x_shdr, sizeof (x_shdr), abfd) != sizeof (x_shdr))
186
0
  goto fail;
187
63
      elf_swap_shdr_in (abfd, &x_shdr, &i_shdr);
188
189
63
      if (i_shdr.sh_info != 0)
190
63
  {
191
63
    i_ehdrp->e_phnum = i_shdr.sh_info;
192
63
    if (i_ehdrp->e_phnum != i_shdr.sh_info)
193
0
      goto wrong;
194
63
  }
195
63
    }
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
39.5k
  if (i_ehdrp->e_phnum > 1)
200
39.5k
    {
201
39.5k
      Elf_External_Phdr x_phdr;
202
39.5k
      Elf_Internal_Phdr i_phdr;
203
39.5k
      file_ptr where;
204
205
      /* Check that we don't have a totally silly number of
206
   program headers.  */
207
39.5k
      if (i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (x_phdr)
208
39.5k
    || i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (i_phdr))
209
3
  goto wrong;
210
211
39.5k
      where = (file_ptr)(i_ehdrp->e_phoff + (i_ehdrp->e_phnum - 1) * sizeof (x_phdr));
212
39.5k
      if ((bfd_size_type) where <= i_ehdrp->e_phoff)
213
0
  goto wrong;
214
215
39.5k
      if (bfd_seek (abfd, where, SEEK_SET) != 0)
216
0
  goto fail;
217
39.5k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
218
7
  goto fail;
219
39.5k
    }
220
221
  /* Move to the start of the program headers.  */
222
39.5k
  if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0)
223
0
    goto wrong;
224
225
  /* Allocate space for the program headers.  */
226
39.5k
  amt = sizeof (*i_phdrp) * i_ehdrp->e_phnum;
227
39.5k
  i_phdrp = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
228
39.5k
  if (!i_phdrp)
229
0
    goto fail;
230
231
39.5k
  elf_tdata (abfd)->phdr = i_phdrp;
232
233
  /* Read and convert to internal form.  */
234
922k
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
235
882k
    {
236
882k
      Elf_External_Phdr x_phdr;
237
238
882k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
239
0
  goto fail;
240
241
882k
      elf_swap_phdr_in (abfd, &x_phdr, i_phdrp + phindex);
242
882k
    }
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
39.5k
  if (! bfd_default_set_arch_mach (abfd, ebd->arch, 0)
248
      /* It's OK if this fails for the generic target.  */
249
10.8k
      && 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
39.5k
  if (ebd->elf_backend_object_p != NULL
258
27.1k
      && ! ebd->elf_backend_object_p (abfd))
259
5.43k
    goto wrong;
260
261
  /* Process each program header.  */
262
793k
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
263
760k
    if (! bfd_section_from_phdr (abfd, i_phdrp + phindex, (int) phindex))
264
582
      goto fail;
265
266
  /* Check for core truncation.  */
267
33.5k
  filesize = bfd_get_file_size (abfd);
268
33.5k
  if (filesize != 0)
269
33.5k
    {
270
44.8k
      for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
271
44.5k
  {
272
44.5k
    Elf_Internal_Phdr *p = i_phdrp + phindex;
273
44.5k
    if (p->p_filesz
274
41.2k
        && (p->p_offset >= filesize
275
15.4k
      || p->p_filesz > filesize - p->p_offset))
276
33.2k
      {
277
33.2k
        _bfd_error_handler (_("warning: %pB has a segment "
278
33.2k
            "extending past end of file"), abfd);
279
33.2k
        abfd->read_only = 1;
280
33.2k
        break;
281
33.2k
      }
282
44.5k
      }
283
33.5k
  }
284
285
  /* Save the entry point from the ELF header.  */
286
33.5k
  abfd->start_address = i_ehdrp->e_entry;
287
33.5k
  return _bfd_no_cleanup;
288
289
2.84M
 wrong:
290
2.84M
  bfd_set_error (bfd_error_wrong_format);
291
2.84M
 fail:
292
  return NULL;
293
2.84M
}
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
13.1k
{
304
13.1k
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.   */
305
13.1k
  Elf_Internal_Ehdr i_ehdr; /* Elf file header, internal form.   */
306
13.1k
  Elf_Internal_Phdr *i_phdr;
307
13.1k
  unsigned int i;
308
13.1k
  size_t amt;
309
310
  /* Seek to the position of the segment at OFFSET.  */
311
13.1k
  if (bfd_seek (abfd, offset, SEEK_SET) != 0)
312
1.20k
    goto fail;
313
314
  /* Read in the ELF header in external format.  */
315
11.9k
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
316
3.66k
    {
317
3.66k
      if (bfd_get_error () != bfd_error_system_call)
318
3.66k
  goto wrong;
319
0
      else
320
0
  goto fail;
321
3.66k
    }
322
323
  /* Now check to see if we have a valid ELF file, and one that BFD can
324
     make use of.  The magic number must match, the address size ('class')
325
     and byte-swapping must match our XVEC entry, and it must have a
326
     section header table (FIXME: See comments re sections at top of this
327
     file).  */
328
8.33k
  if (! elf_file_p (&x_ehdr)
329
4.38k
      || x_ehdr.e_ident[EI_VERSION] != EV_CURRENT
330
3.86k
      || x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
331
4.49k
    goto wrong;
332
333
  /* Check that file's byte order matches xvec's.  */
334
3.83k
  switch (x_ehdr.e_ident[EI_DATA])
335
3.83k
    {
336
1.22k
    case ELFDATA2MSB:   /* Big-endian.  */
337
1.22k
      if (! bfd_header_big_endian (abfd))
338
21
  goto wrong;
339
1.20k
      break;
340
2.58k
    case ELFDATA2LSB:   /* Little-endian.  */
341
2.58k
      if (! bfd_header_little_endian (abfd))
342
0
  goto wrong;
343
2.58k
      break;
344
2.58k
    case ELFDATANONE:   /* No data encoding specified.  */
345
27
    default:      /* Unknown data encoding specified . */
346
27
      goto wrong;
347
3.83k
    }
348
349
3.78k
  elf_swap_ehdr_in (abfd, &x_ehdr, &i_ehdr);
350
#if DEBUG & 1
351
  elf_debug_file (&i_ehdr);
352
#endif
353
354
3.78k
  if (i_ehdr.e_phentsize != sizeof (Elf_External_Phdr) || i_ehdr.e_phnum == 0)
355
53
    goto fail;
356
357
  /* Read in program headers.  */
358
3.73k
  if (_bfd_mul_overflow (i_ehdr.e_phnum, sizeof (*i_phdr), &amt))
359
0
    {
360
0
      bfd_set_error (bfd_error_file_too_big);
361
0
      goto fail;
362
0
    }
363
3.73k
  i_phdr = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
364
3.73k
  if (i_phdr == NULL)
365
0
    goto fail;
366
367
3.73k
  if (bfd_seek (abfd, offset + i_ehdr.e_phoff, SEEK_SET) != 0)
368
18
    goto fail;
369
370
  /* Read in program headers and parse notes.  */
371
408k
  for (i = 0; i < i_ehdr.e_phnum; ++i, ++i_phdr)
372
405k
    {
373
405k
      Elf_External_Phdr x_phdr;
374
375
405k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
376
236
  goto fail;
377
404k
      elf_swap_phdr_in (abfd, &x_phdr, i_phdr);
378
379
404k
      if (i_phdr->p_type == PT_NOTE && i_phdr->p_filesz > 0)
380
3.52k
  {
381
3.52k
    _bfd_elf_read_notes (abfd, offset + i_phdr->p_offset,
382
3.52k
             i_phdr->p_filesz, i_phdr->p_align);
383
384
    /* Make sure ABFD returns to processing the program headers.  */
385
3.52k
    if (bfd_seek (abfd,
386
3.52k
      offset + i_ehdr.e_phoff + (i + 1) * sizeof (x_phdr),
387
3.52k
      SEEK_SET) != 0)
388
0
      goto fail;
389
390
3.52k
    if (abfd->build_id != NULL)
391
11
      return true;
392
3.52k
  }
393
404k
    }
394
395
  /* Having gotten this far, we have a valid ELF section, but no
396
     build-id was found.  */
397
3.47k
  goto fail;
398
399
8.20k
 wrong:
400
8.20k
  bfd_set_error (bfd_error_wrong_format);
401
13.1k
 fail:
402
  return false;
403
8.20k
}
_bfd_elf64_core_find_build_id
Line
Count
Source
303
6.36k
{
304
6.36k
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.   */
305
6.36k
  Elf_Internal_Ehdr i_ehdr; /* Elf file header, internal form.   */
306
6.36k
  Elf_Internal_Phdr *i_phdr;
307
6.36k
  unsigned int i;
308
6.36k
  size_t amt;
309
310
  /* Seek to the position of the segment at OFFSET.  */
311
6.36k
  if (bfd_seek (abfd, offset, SEEK_SET) != 0)
312
1.20k
    goto fail;
313
314
  /* Read in the ELF header in external format.  */
315
5.16k
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
316
1.52k
    {
317
1.52k
      if (bfd_get_error () != bfd_error_system_call)
318
1.52k
  goto wrong;
319
0
      else
320
0
  goto fail;
321
1.52k
    }
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.63k
  if (! elf_file_p (&x_ehdr)
329
2.76k
      || x_ehdr.e_ident[EI_VERSION] != EV_CURRENT
330
2.37k
      || x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
331
1.27k
    goto wrong;
332
333
  /* Check that file's byte order matches xvec's.  */
334
2.36k
  switch (x_ehdr.e_ident[EI_DATA])
335
2.36k
    {
336
31
    case ELFDATA2MSB:   /* Big-endian.  */
337
31
      if (! bfd_header_big_endian (abfd))
338
0
  goto wrong;
339
31
      break;
340
2.31k
    case ELFDATA2LSB:   /* Little-endian.  */
341
2.31k
      if (! bfd_header_little_endian (abfd))
342
0
  goto wrong;
343
2.31k
      break;
344
2.31k
    case ELFDATANONE:   /* No data encoding specified.  */
345
22
    default:      /* Unknown data encoding specified . */
346
22
      goto wrong;
347
2.36k
    }
348
349
2.34k
  elf_swap_ehdr_in (abfd, &x_ehdr, &i_ehdr);
350
#if DEBUG & 1
351
  elf_debug_file (&i_ehdr);
352
#endif
353
354
2.34k
  if (i_ehdr.e_phentsize != sizeof (Elf_External_Phdr) || i_ehdr.e_phnum == 0)
355
31
    goto fail;
356
357
  /* Read in program headers.  */
358
2.31k
  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.31k
  i_phdr = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
364
2.31k
  if (i_phdr == NULL)
365
0
    goto fail;
366
367
2.31k
  if (bfd_seek (abfd, offset + i_ehdr.e_phoff, SEEK_SET) != 0)
368
18
    goto fail;
369
370
  /* Read in program headers and parse notes.  */
371
241k
  for (i = 0; i < i_ehdr.e_phnum; ++i, ++i_phdr)
372
239k
    {
373
239k
      Elf_External_Phdr x_phdr;
374
375
239k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
376
26
  goto fail;
377
239k
      elf_swap_phdr_in (abfd, &x_phdr, i_phdr);
378
379
239k
      if (i_phdr->p_type == PT_NOTE && i_phdr->p_filesz > 0)
380
2.61k
  {
381
2.61k
    _bfd_elf_read_notes (abfd, offset + i_phdr->p_offset,
382
2.61k
             i_phdr->p_filesz, i_phdr->p_align);
383
384
    /* Make sure ABFD returns to processing the program headers.  */
385
2.61k
    if (bfd_seek (abfd,
386
2.61k
      offset + i_ehdr.e_phoff + (i + 1) * sizeof (x_phdr),
387
2.61k
      SEEK_SET) != 0)
388
0
      goto fail;
389
390
2.61k
    if (abfd->build_id != NULL)
391
7
      return true;
392
2.61k
  }
393
239k
    }
394
395
  /* Having gotten this far, we have a valid ELF section, but no
396
     build-id was found.  */
397
2.26k
  goto fail;
398
399
2.81k
 wrong:
400
2.81k
  bfd_set_error (bfd_error_wrong_format);
401
6.35k
 fail:
402
  return false;
403
2.81k
}
_bfd_elf32_core_find_build_id
Line
Count
Source
303
6.83k
{
304
6.83k
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.   */
305
6.83k
  Elf_Internal_Ehdr i_ehdr; /* Elf file header, internal form.   */
306
6.83k
  Elf_Internal_Phdr *i_phdr;
307
6.83k
  unsigned int i;
308
6.83k
  size_t amt;
309
310
  /* Seek to the position of the segment at OFFSET.  */
311
6.83k
  if (bfd_seek (abfd, offset, SEEK_SET) != 0)
312
0
    goto fail;
313
314
  /* Read in the ELF header in external format.  */
315
6.83k
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
316
2.14k
    {
317
2.14k
      if (bfd_get_error () != bfd_error_system_call)
318
2.14k
  goto wrong;
319
0
      else
320
0
  goto fail;
321
2.14k
    }
322
323
  /* Now check to see if we have a valid ELF file, and one that BFD can
324
     make use of.  The magic number must match, the address size ('class')
325
     and byte-swapping must match our XVEC entry, and it must have a
326
     section header table (FIXME: See comments re sections at top of this
327
     file).  */
328
4.69k
  if (! elf_file_p (&x_ehdr)
329
1.61k
      || x_ehdr.e_ident[EI_VERSION] != EV_CURRENT
330
1.48k
      || x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
331
3.22k
    goto wrong;
332
333
  /* Check that file's byte order matches xvec's.  */
334
1.46k
  switch (x_ehdr.e_ident[EI_DATA])
335
1.46k
    {
336
1.19k
    case ELFDATA2MSB:   /* Big-endian.  */
337
1.19k
      if (! bfd_header_big_endian (abfd))
338
21
  goto wrong;
339
1.17k
      break;
340
1.17k
    case ELFDATA2LSB:   /* Little-endian.  */
341
271
      if (! bfd_header_little_endian (abfd))
342
0
  goto wrong;
343
271
      break;
344
271
    case ELFDATANONE:   /* No data encoding specified.  */
345
5
    default:      /* Unknown data encoding specified . */
346
5
      goto wrong;
347
1.46k
    }
348
349
1.44k
  elf_swap_ehdr_in (abfd, &x_ehdr, &i_ehdr);
350
#if DEBUG & 1
351
  elf_debug_file (&i_ehdr);
352
#endif
353
354
1.44k
  if (i_ehdr.e_phentsize != sizeof (Elf_External_Phdr) || i_ehdr.e_phnum == 0)
355
22
    goto fail;
356
357
  /* Read in program headers.  */
358
1.42k
  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.42k
  i_phdr = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
364
1.42k
  if (i_phdr == NULL)
365
0
    goto fail;
366
367
1.42k
  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
167k
  for (i = 0; i < i_ehdr.e_phnum; ++i, ++i_phdr)
372
165k
    {
373
165k
      Elf_External_Phdr x_phdr;
374
375
165k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
376
210
  goto fail;
377
165k
      elf_swap_phdr_in (abfd, &x_phdr, i_phdr);
378
379
165k
      if (i_phdr->p_type == PT_NOTE && i_phdr->p_filesz > 0)
380
908
  {
381
908
    _bfd_elf_read_notes (abfd, offset + i_phdr->p_offset,
382
908
             i_phdr->p_filesz, i_phdr->p_align);
383
384
    /* Make sure ABFD returns to processing the program headers.  */
385
908
    if (bfd_seek (abfd,
386
908
      offset + i_ehdr.e_phoff + (i + 1) * sizeof (x_phdr),
387
908
      SEEK_SET) != 0)
388
0
      goto fail;
389
390
908
    if (abfd->build_id != NULL)
391
4
      return true;
392
908
  }
393
165k
    }
394
395
  /* Having gotten this far, we have a valid ELF section, but no
396
     build-id was found.  */
397
1.20k
  goto fail;
398
399
5.39k
 wrong:
400
5.39k
  bfd_set_error (bfd_error_wrong_format);
401
6.83k
 fail:
402
  return false;
403
5.39k
}