Coverage Report

Created: 2026-07-12 09:22

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.72M
{
89
3.72M
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.  */
90
3.72M
  Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form.  */
91
3.72M
  Elf_Internal_Phdr *i_phdrp; /* Elf program header, internal form.  */
92
3.72M
  unsigned int phindex;
93
3.72M
  bfd_size_type amt;
94
3.72M
  ufile_ptr filesize;
95
96
  /* Read in the ELF header in external format.  */
97
3.72M
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
98
662k
    {
99
662k
      if (bfd_get_error () != bfd_error_system_call)
100
662k
  goto wrong;
101
0
      else
102
0
  goto fail;
103
662k
    }
104
105
  /* Check the magic number.  */
106
3.06M
  if (! elf_file_p (&x_ehdr))
107
1.01M
    goto wrong;
108
109
  /* FIXME: Check EI_VERSION here !  */
110
111
  /* Check the address size ("class").  */
112
2.05M
  if (x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
113
574k
    goto wrong;
114
115
  /* Check the byteorder.  */
116
1.48M
  switch (x_ehdr.e_ident[EI_DATA])
117
1.48M
    {
118
968k
    case ELFDATA2MSB:   /* Big-endian.  */
119
968k
      if (! bfd_big_endian (abfd))
120
481k
  goto wrong;
121
487k
      break;
122
511k
    case ELFDATA2LSB:   /* Little-endian.  */
123
511k
      if (! bfd_little_endian (abfd))
124
253k
  goto wrong;
125
258k
      break;
126
258k
    default:
127
608
      goto wrong;
128
1.48M
    }
129
130
  /* Give abfd an elf_obj_tdata.  */
131
745k
  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
745k
  i_ehdrp = elf_elfheader (abfd);
136
745k
  elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
137
138
#if DEBUG & 1
139
  elf_debug_file (i_ehdrp);
140
#endif
141
142
745k
  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
745k
  if (ebd->elf_machine_code != i_ehdrp->e_machine
148
720k
      && (ebd->elf_machine_alt1 == 0
149
214k
    || i_ehdrp->e_machine != ebd->elf_machine_alt1)
150
719k
      && (ebd->elf_machine_alt2 == 0
151
35.8k
    || i_ehdrp->e_machine != ebd->elf_machine_alt2)
152
718k
      && ebd->elf_machine_code != EM_NONE)
153
706k
    goto wrong;
154
155
39.2k
  if (ebd->osabi_exact && i_ehdrp->e_ident[EI_OSABI] != ebd->elf_osabi)
156
1.93k
    goto wrong;
157
158
  /* If there is no program header, or the type is not a core file, then
159
     we are hosed.  */
160
37.3k
  if (i_ehdrp->e_phoff == 0 || i_ehdrp->e_type != ET_CORE)
161
6.22k
    goto wrong;
162
163
  /* Does BFD's idea of the phdr size match the size
164
     recorded in the file? */
165
31.1k
  if (i_ehdrp->e_phentsize != sizeof (Elf_External_Phdr))
166
42
    goto wrong;
167
168
  /* If the program header count is PN_XNUM(0xffff), the actual
169
     count is in the first section header.  */
170
31.0k
  if (i_ehdrp->e_shoff != 0 && i_ehdrp->e_phnum == PN_XNUM)
171
246
    {
172
246
      Elf_External_Shdr x_shdr;
173
246
      Elf_Internal_Shdr i_shdr;
174
246
      file_ptr where = (file_ptr) i_ehdrp->e_shoff;
175
176
246
      if (i_ehdrp->e_shoff < sizeof (x_ehdr))
177
18
  goto wrong;
178
179
      /* Seek to the section header table in the file.  */
180
228
      if (bfd_seek (abfd, where, SEEK_SET) != 0)
181
21
  goto fail;
182
183
      /* Read the first section header at index 0, and convert to internal
184
   form.  */
185
207
      if (bfd_read (&x_shdr, sizeof (x_shdr), abfd) != sizeof (x_shdr))
186
24
  goto fail;
187
183
      elf_swap_shdr_in (abfd, &x_shdr, &i_shdr);
188
189
183
      if (i_shdr.sh_info != 0)
190
161
  {
191
161
    i_ehdrp->e_phnum = i_shdr.sh_info;
192
161
    if (i_ehdrp->e_phnum != i_shdr.sh_info)
193
0
      goto wrong;
194
161
  }
195
183
    }
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
31.0k
  if (i_ehdrp->e_phnum > 1)
200
29.7k
    {
201
29.7k
      Elf_External_Phdr x_phdr;
202
29.7k
      Elf_Internal_Phdr i_phdr;
203
29.7k
      file_ptr where;
204
205
      /* Check that we don't have a totally silly number of
206
   program headers.  */
207
29.7k
      if (i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (x_phdr)
208
29.7k
    || i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (i_phdr))
209
79
  goto wrong;
210
211
29.6k
      where = (file_ptr)(i_ehdrp->e_phoff + (i_ehdrp->e_phnum - 1) * sizeof (x_phdr));
212
29.6k
      if ((bfd_size_type) where <= i_ehdrp->e_phoff)
213
15
  goto wrong;
214
215
29.6k
      if (bfd_seek (abfd, where, SEEK_SET) != 0)
216
36
  goto fail;
217
29.6k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
218
84
  goto fail;
219
29.6k
    }
220
221
  /* Move to the start of the program headers.  */
222
30.7k
  if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0)
223
13
    goto wrong;
224
225
  /* Allocate space for the program headers.  */
226
30.7k
  amt = sizeof (*i_phdrp) * i_ehdrp->e_phnum;
227
30.7k
  i_phdrp = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
228
30.7k
  if (!i_phdrp)
229
0
    goto fail;
230
231
30.7k
  elf_tdata (abfd)->phdr = i_phdrp;
232
233
  /* Read and convert to internal form.  */
234
831k
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
235
801k
    {
236
801k
      Elf_External_Phdr x_phdr;
237
238
801k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
239
45
  goto fail;
240
241
800k
      elf_swap_phdr_in (abfd, &x_phdr, i_phdrp + phindex);
242
800k
    }
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
30.7k
  if (! bfd_default_set_arch_mach (abfd, ebd->arch, 0)
248
      /* It's OK if this fails for the generic target.  */
249
8.39k
      && ebd->elf_machine_code != EM_NONE)
250
1
    goto fail;
251
252
  /* Let the backend double check the format and override global
253
     information.  We do this before processing the program headers
254
     to allow the correct machine (as opposed to just the default
255
     machine) to be set, making it possible for grok_prstatus and
256
     grok_psinfo to rely on the mach setting.  */
257
30.7k
  if (ebd->elf_backend_object_p != NULL
258
21.3k
      && ! ebd->elf_backend_object_p (abfd))
259
3.92k
    goto wrong;
260
261
  /* Process each program header.  */
262
722k
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
263
696k
    if (! bfd_section_from_phdr (abfd, i_phdrp + phindex, (int) phindex))
264
1.07k
      goto fail;
265
266
  /* Check for core truncation.  */
267
25.7k
  filesize = bfd_get_file_size (abfd);
268
25.7k
  if (filesize != 0)
269
25.7k
    {
270
37.4k
      for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
271
36.4k
  {
272
36.4k
    Elf_Internal_Phdr *p = i_phdrp + phindex;
273
36.4k
    if (p->p_filesz
274
31.5k
        && (p->p_offset >= filesize
275
12.1k
      || p->p_filesz > filesize - p->p_offset))
276
24.7k
      {
277
24.7k
        _bfd_error_handler (_("warning: %pB has a segment "
278
24.7k
            "extending past end of file"), abfd);
279
24.7k
        abfd->read_only = 1;
280
24.7k
        break;
281
24.7k
      }
282
36.4k
      }
283
25.7k
  }
284
285
  /* Save the entry point from the ELF header.  */
286
25.7k
  abfd->start_address = i_ehdrp->e_entry;
287
25.7k
  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
866k
{
89
866k
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.  */
90
866k
  Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form.  */
91
866k
  Elf_Internal_Phdr *i_phdrp; /* Elf program header, internal form.  */
92
866k
  unsigned int phindex;
93
866k
  bfd_size_type amt;
94
866k
  ufile_ptr filesize;
95
96
  /* Read in the ELF header in external format.  */
97
866k
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
98
255k
    {
99
255k
      if (bfd_get_error () != bfd_error_system_call)
100
255k
  goto wrong;
101
0
      else
102
0
  goto fail;
103
255k
    }
104
105
  /* Check the magic number.  */
106
610k
  if (! elf_file_p (&x_ehdr))
107
217k
    goto wrong;
108
109
  /* FIXME: Check EI_VERSION here !  */
110
111
  /* Check the address size ("class").  */
112
393k
  if (x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
113
313k
    goto wrong;
114
115
  /* Check the byteorder.  */
116
79.7k
  switch (x_ehdr.e_ident[EI_DATA])
117
79.7k
    {
118
5.67k
    case ELFDATA2MSB:   /* Big-endian.  */
119
5.67k
      if (! bfd_big_endian (abfd))
120
2.96k
  goto wrong;
121
2.70k
      break;
122
74.0k
    case ELFDATA2LSB:   /* Little-endian.  */
123
74.0k
      if (! bfd_little_endian (abfd))
124
34.7k
  goto wrong;
125
39.2k
      break;
126
39.2k
    default:
127
80
      goto wrong;
128
79.7k
    }
129
130
  /* Give abfd an elf_obj_tdata.  */
131
41.9k
  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
41.9k
  i_ehdrp = elf_elfheader (abfd);
136
41.9k
  elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
137
138
#if DEBUG & 1
139
  elf_debug_file (i_ehdrp);
140
#endif
141
142
41.9k
  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
41.9k
  if (ebd->elf_machine_code != i_ehdrp->e_machine
148
38.0k
      && (ebd->elf_machine_alt1 == 0
149
4.10k
    || i_ehdrp->e_machine != ebd->elf_machine_alt1)
150
37.8k
      && (ebd->elf_machine_alt2 == 0
151
3.51k
    || i_ehdrp->e_machine != ebd->elf_machine_alt2)
152
37.7k
      && ebd->elf_machine_code != EM_NONE)
153
35.8k
    goto wrong;
154
155
6.08k
  if (ebd->osabi_exact && i_ehdrp->e_ident[EI_OSABI] != ebd->elf_osabi)
156
742
    goto wrong;
157
158
  /* If there is no program header, or the type is not a core file, then
159
     we are hosed.  */
160
5.34k
  if (i_ehdrp->e_phoff == 0 || i_ehdrp->e_type != ET_CORE)
161
2.02k
    goto wrong;
162
163
  /* Does BFD's idea of the phdr size match the size
164
     recorded in the file? */
165
3.31k
  if (i_ehdrp->e_phentsize != sizeof (Elf_External_Phdr))
166
18
    goto wrong;
167
168
  /* If the program header count is PN_XNUM(0xffff), the actual
169
     count is in the first section header.  */
170
3.29k
  if (i_ehdrp->e_shoff != 0 && i_ehdrp->e_phnum == PN_XNUM)
171
114
    {
172
114
      Elf_External_Shdr x_shdr;
173
114
      Elf_Internal_Shdr i_shdr;
174
114
      file_ptr where = (file_ptr) i_ehdrp->e_shoff;
175
176
114
      if (i_ehdrp->e_shoff < sizeof (x_ehdr))
177
6
  goto wrong;
178
179
      /* Seek to the section header table in the file.  */
180
108
      if (bfd_seek (abfd, where, SEEK_SET) != 0)
181
21
  goto fail;
182
183
      /* Read the first section header at index 0, and convert to internal
184
   form.  */
185
87
      if (bfd_read (&x_shdr, sizeof (x_shdr), abfd) != sizeof (x_shdr))
186
10
  goto fail;
187
77
      elf_swap_shdr_in (abfd, &x_shdr, &i_shdr);
188
189
77
      if (i_shdr.sh_info != 0)
190
67
  {
191
67
    i_ehdrp->e_phnum = i_shdr.sh_info;
192
67
    if (i_ehdrp->e_phnum != i_shdr.sh_info)
193
0
      goto wrong;
194
67
  }
195
77
    }
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
3.25k
  if (i_ehdrp->e_phnum > 1)
200
2.78k
    {
201
2.78k
      Elf_External_Phdr x_phdr;
202
2.78k
      Elf_Internal_Phdr i_phdr;
203
2.78k
      file_ptr where;
204
205
      /* Check that we don't have a totally silly number of
206
   program headers.  */
207
2.78k
      if (i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (x_phdr)
208
2.73k
    || i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (i_phdr))
209
55
  goto wrong;
210
211
2.72k
      where = (file_ptr)(i_ehdrp->e_phoff + (i_ehdrp->e_phnum - 1) * sizeof (x_phdr));
212
2.72k
      if ((bfd_size_type) where <= i_ehdrp->e_phoff)
213
15
  goto wrong;
214
215
2.71k
      if (bfd_seek (abfd, where, SEEK_SET) != 0)
216
36
  goto fail;
217
2.67k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
218
26
  goto fail;
219
2.67k
    }
220
221
  /* Move to the start of the program headers.  */
222
3.12k
  if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0)
223
13
    goto wrong;
224
225
  /* Allocate space for the program headers.  */
226
3.11k
  amt = sizeof (*i_phdrp) * i_ehdrp->e_phnum;
227
3.11k
  i_phdrp = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
228
3.11k
  if (!i_phdrp)
229
0
    goto fail;
230
231
3.11k
  elf_tdata (abfd)->phdr = i_phdrp;
232
233
  /* Read and convert to internal form.  */
234
109k
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
235
106k
    {
236
106k
      Elf_External_Phdr x_phdr;
237
238
106k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
239
25
  goto fail;
240
241
106k
      elf_swap_phdr_in (abfd, &x_phdr, i_phdrp + phindex);
242
106k
    }
243
244
  /* Set the machine architecture.  Do this before processing the
245
     program headers since we need to know the architecture type
246
     when processing the notes of some systems' core files.  */
247
3.08k
  if (! bfd_default_set_arch_mach (abfd, ebd->arch, 0)
248
      /* It's OK if this fails for the generic target.  */
249
935
      && 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
3.08k
  if (ebd->elf_backend_object_p != NULL
258
2.09k
      && ! ebd->elf_backend_object_p (abfd))
259
0
    goto wrong;
260
261
  /* Process each program header.  */
262
101k
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
263
99.2k
    if (! bfd_section_from_phdr (abfd, i_phdrp + phindex, (int) phindex))
264
621
      goto fail;
265
266
  /* Check for core truncation.  */
267
2.46k
  filesize = bfd_get_file_size (abfd);
268
2.46k
  if (filesize != 0)
269
2.46k
    {
270
5.40k
      for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
271
5.16k
  {
272
5.16k
    Elf_Internal_Phdr *p = i_phdrp + phindex;
273
5.16k
    if (p->p_filesz
274
3.67k
        && (p->p_offset >= filesize
275
1.92k
      || p->p_filesz > filesize - p->p_offset))
276
2.22k
      {
277
2.22k
        _bfd_error_handler (_("warning: %pB has a segment "
278
2.22k
            "extending past end of file"), abfd);
279
2.22k
        abfd->read_only = 1;
280
2.22k
        break;
281
2.22k
      }
282
5.16k
      }
283
2.46k
  }
284
285
  /* Save the entry point from the ELF header.  */
286
2.46k
  abfd->start_address = i_ehdrp->e_entry;
287
2.46k
  return _bfd_no_cleanup;
288
289
863k
 wrong:
290
863k
  bfd_set_error (bfd_error_wrong_format);
291
863k
 fail:
292
  return NULL;
293
863k
}
bfd_elf32_core_file_p
Line
Count
Source
88
2.86M
{
89
2.86M
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.  */
90
2.86M
  Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form.  */
91
2.86M
  Elf_Internal_Phdr *i_phdrp; /* Elf program header, internal form.  */
92
2.86M
  unsigned int phindex;
93
2.86M
  bfd_size_type amt;
94
2.86M
  ufile_ptr filesize;
95
96
  /* Read in the ELF header in external format.  */
97
2.86M
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
98
406k
    {
99
406k
      if (bfd_get_error () != bfd_error_system_call)
100
406k
  goto wrong;
101
0
      else
102
0
  goto fail;
103
406k
    }
104
105
  /* Check the magic number.  */
106
2.45M
  if (! elf_file_p (&x_ehdr))
107
793k
    goto wrong;
108
109
  /* FIXME: Check EI_VERSION here !  */
110
111
  /* Check the address size ("class").  */
112
1.66M
  if (x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
113
260k
    goto wrong;
114
115
  /* Check the byteorder.  */
116
1.40M
  switch (x_ehdr.e_ident[EI_DATA])
117
1.40M
    {
118
963k
    case ELFDATA2MSB:   /* Big-endian.  */
119
963k
      if (! bfd_big_endian (abfd))
120
478k
  goto wrong;
121
484k
      break;
122
484k
    case ELFDATA2LSB:   /* Little-endian.  */
123
437k
      if (! bfd_little_endian (abfd))
124
218k
  goto wrong;
125
219k
      break;
126
219k
    default:
127
528
      goto wrong;
128
1.40M
    }
129
130
  /* Give abfd an elf_obj_tdata.  */
131
703k
  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
703k
  i_ehdrp = elf_elfheader (abfd);
136
703k
  elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
137
138
#if DEBUG & 1
139
  elf_debug_file (i_ehdrp);
140
#endif
141
142
703k
  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
703k
  if (ebd->elf_machine_code != i_ehdrp->e_machine
148
682k
      && (ebd->elf_machine_alt1 == 0
149
210k
    || i_ehdrp->e_machine != ebd->elf_machine_alt1)
150
681k
      && (ebd->elf_machine_alt2 == 0
151
32.3k
    || i_ehdrp->e_machine != ebd->elf_machine_alt2)
152
680k
      && ebd->elf_machine_code != EM_NONE)
153
670k
    goto wrong;
154
155
33.1k
  if (ebd->osabi_exact && i_ehdrp->e_ident[EI_OSABI] != ebd->elf_osabi)
156
1.19k
    goto wrong;
157
158
  /* If there is no program header, or the type is not a core file, then
159
     we are hosed.  */
160
31.9k
  if (i_ehdrp->e_phoff == 0 || i_ehdrp->e_type != ET_CORE)
161
4.19k
    goto wrong;
162
163
  /* Does BFD's idea of the phdr size match the size
164
     recorded in the file? */
165
27.7k
  if (i_ehdrp->e_phentsize != sizeof (Elf_External_Phdr))
166
24
    goto wrong;
167
168
  /* If the program header count is PN_XNUM(0xffff), the actual
169
     count is in the first section header.  */
170
27.7k
  if (i_ehdrp->e_shoff != 0 && i_ehdrp->e_phnum == PN_XNUM)
171
132
    {
172
132
      Elf_External_Shdr x_shdr;
173
132
      Elf_Internal_Shdr i_shdr;
174
132
      file_ptr where = (file_ptr) i_ehdrp->e_shoff;
175
176
132
      if (i_ehdrp->e_shoff < sizeof (x_ehdr))
177
12
  goto wrong;
178
179
      /* Seek to the section header table in the file.  */
180
120
      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
120
      if (bfd_read (&x_shdr, sizeof (x_shdr), abfd) != sizeof (x_shdr))
186
14
  goto fail;
187
106
      elf_swap_shdr_in (abfd, &x_shdr, &i_shdr);
188
189
106
      if (i_shdr.sh_info != 0)
190
94
  {
191
94
    i_ehdrp->e_phnum = i_shdr.sh_info;
192
94
    if (i_ehdrp->e_phnum != i_shdr.sh_info)
193
0
      goto wrong;
194
94
  }
195
106
    }
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
27.7k
  if (i_ehdrp->e_phnum > 1)
200
26.9k
    {
201
26.9k
      Elf_External_Phdr x_phdr;
202
26.9k
      Elf_Internal_Phdr i_phdr;
203
26.9k
      file_ptr where;
204
205
      /* Check that we don't have a totally silly number of
206
   program headers.  */
207
26.9k
      if (i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (x_phdr)
208
26.9k
    || i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (i_phdr))
209
24
  goto wrong;
210
211
26.9k
      where = (file_ptr)(i_ehdrp->e_phoff + (i_ehdrp->e_phnum - 1) * sizeof (x_phdr));
212
26.9k
      if ((bfd_size_type) where <= i_ehdrp->e_phoff)
213
0
  goto wrong;
214
215
26.9k
      if (bfd_seek (abfd, where, SEEK_SET) != 0)
216
0
  goto fail;
217
26.9k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
218
58
  goto fail;
219
26.9k
    }
220
221
  /* Move to the start of the program headers.  */
222
27.6k
  if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0)
223
0
    goto wrong;
224
225
  /* Allocate space for the program headers.  */
226
27.6k
  amt = sizeof (*i_phdrp) * i_ehdrp->e_phnum;
227
27.6k
  i_phdrp = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
228
27.6k
  if (!i_phdrp)
229
0
    goto fail;
230
231
27.6k
  elf_tdata (abfd)->phdr = i_phdrp;
232
233
  /* Read and convert to internal form.  */
234
722k
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
235
694k
    {
236
694k
      Elf_External_Phdr x_phdr;
237
238
694k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
239
20
  goto fail;
240
241
694k
      elf_swap_phdr_in (abfd, &x_phdr, i_phdrp + phindex);
242
694k
    }
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
27.6k
  if (! bfd_default_set_arch_mach (abfd, ebd->arch, 0)
248
      /* It's OK if this fails for the generic target.  */
249
7.46k
      && ebd->elf_machine_code != EM_NONE)
250
1
    goto fail;
251
252
  /* Let the backend double check the format and override global
253
     information.  We do this before processing the program headers
254
     to allow the correct machine (as opposed to just the default
255
     machine) to be set, making it possible for grok_prstatus and
256
     grok_psinfo to rely on the mach setting.  */
257
27.6k
  if (ebd->elf_backend_object_p != NULL
258
19.2k
      && ! ebd->elf_backend_object_p (abfd))
259
3.92k
    goto wrong;
260
261
  /* Process each program header.  */
262
620k
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
263
597k
    if (! bfd_section_from_phdr (abfd, i_phdrp + phindex, (int) phindex))
264
456
      goto fail;
265
266
  /* Check for core truncation.  */
267
23.2k
  filesize = bfd_get_file_size (abfd);
268
23.2k
  if (filesize != 0)
269
23.2k
    {
270
32.0k
      for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
271
31.2k
  {
272
31.2k
    Elf_Internal_Phdr *p = i_phdrp + phindex;
273
31.2k
    if (p->p_filesz
274
27.9k
        && (p->p_offset >= filesize
275
10.2k
      || p->p_filesz > filesize - p->p_offset))
276
22.5k
      {
277
22.5k
        _bfd_error_handler (_("warning: %pB has a segment "
278
22.5k
            "extending past end of file"), abfd);
279
22.5k
        abfd->read_only = 1;
280
22.5k
        break;
281
22.5k
      }
282
31.2k
      }
283
23.2k
  }
284
285
  /* Save the entry point from the ELF header.  */
286
23.2k
  abfd->start_address = i_ehdrp->e_entry;
287
23.2k
  return _bfd_no_cleanup;
288
289
2.83M
 wrong:
290
2.83M
  bfd_set_error (bfd_error_wrong_format);
291
2.83M
 fail:
292
  return NULL;
293
2.83M
}
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
10.9k
{
304
10.9k
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.   */
305
10.9k
  Elf_Internal_Ehdr i_ehdr; /* Elf file header, internal form.   */
306
10.9k
  Elf_Internal_Phdr *i_phdr;
307
10.9k
  unsigned int i;
308
10.9k
  size_t amt;
309
310
  /* Seek to the position of the segment at OFFSET.  */
311
10.9k
  if (bfd_seek (abfd, offset, SEEK_SET) != 0)
312
1.33k
    goto fail;
313
314
  /* Read in the ELF header in external format.  */
315
9.61k
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
316
2.77k
    {
317
2.77k
      if (bfd_get_error () != bfd_error_system_call)
318
2.77k
  goto wrong;
319
0
      else
320
0
  goto fail;
321
2.77k
    }
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
6.84k
  if (! elf_file_p (&x_ehdr)
329
3.53k
      || x_ehdr.e_ident[EI_VERSION] != EV_CURRENT
330
3.23k
      || x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
331
3.64k
    goto wrong;
332
333
  /* Check that file's byte order matches xvec's.  */
334
3.19k
  switch (x_ehdr.e_ident[EI_DATA])
335
3.19k
    {
336
1.07k
    case ELFDATA2MSB:   /* Big-endian.  */
337
1.07k
      if (! bfd_header_big_endian (abfd))
338
23
  goto wrong;
339
1.04k
      break;
340
2.09k
    case ELFDATA2LSB:   /* Little-endian.  */
341
2.09k
      if (! bfd_header_little_endian (abfd))
342
0
  goto wrong;
343
2.09k
      break;
344
2.09k
    case ELFDATANONE:   /* No data encoding specified.  */
345
23
    default:      /* Unknown data encoding specified . */
346
23
      goto wrong;
347
3.19k
    }
348
349
3.14k
  elf_swap_ehdr_in (abfd, &x_ehdr, &i_ehdr);
350
#if DEBUG & 1
351
  elf_debug_file (&i_ehdr);
352
#endif
353
354
3.14k
  if (i_ehdr.e_phentsize != sizeof (Elf_External_Phdr) || i_ehdr.e_phnum == 0)
355
33
    goto fail;
356
357
  /* Read in program headers.  */
358
3.11k
  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.11k
  i_phdr = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
364
3.11k
  if (i_phdr == NULL)
365
0
    goto fail;
366
367
3.11k
  if (bfd_seek (abfd, offset + i_ehdr.e_phoff, SEEK_SET) != 0)
368
4
    goto fail;
369
370
  /* Read in program headers and parse notes.  */
371
358k
  for (i = 0; i < i_ehdr.e_phnum; ++i, ++i_phdr)
372
355k
    {
373
355k
      Elf_External_Phdr x_phdr;
374
375
355k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
376
229
  goto fail;
377
355k
      elf_swap_phdr_in (abfd, &x_phdr, i_phdr);
378
379
355k
      if (i_phdr->p_type == PT_NOTE && i_phdr->p_filesz > 0)
380
2.75k
  {
381
2.75k
    _bfd_elf_read_notes (abfd, offset + i_phdr->p_offset,
382
2.75k
             i_phdr->p_filesz, i_phdr->p_align);
383
384
    /* Make sure ABFD returns to processing the program headers.  */
385
2.75k
    if (bfd_seek (abfd,
386
2.75k
      offset + i_ehdr.e_phoff + (i + 1) * sizeof (x_phdr),
387
2.75k
      SEEK_SET) != 0)
388
0
      goto fail;
389
390
2.75k
    if (abfd->build_id != NULL)
391
8
      return true;
392
2.75k
  }
393
355k
    }
394
395
  /* Having gotten this far, we have a valid ELF section, but no
396
     build-id was found.  */
397
2.87k
  goto fail;
398
399
6.46k
 wrong:
400
6.46k
  bfd_set_error (bfd_error_wrong_format);
401
10.9k
 fail:
402
  return false;
403
6.46k
}
_bfd_elf64_core_find_build_id
Line
Count
Source
303
5.66k
{
304
5.66k
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.   */
305
5.66k
  Elf_Internal_Ehdr i_ehdr; /* Elf file header, internal form.   */
306
5.66k
  Elf_Internal_Phdr *i_phdr;
307
5.66k
  unsigned int i;
308
5.66k
  size_t amt;
309
310
  /* Seek to the position of the segment at OFFSET.  */
311
5.66k
  if (bfd_seek (abfd, offset, SEEK_SET) != 0)
312
1.33k
    goto fail;
313
314
  /* Read in the ELF header in external format.  */
315
4.33k
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
316
1.24k
    {
317
1.24k
      if (bfd_get_error () != bfd_error_system_call)
318
1.24k
  goto wrong;
319
0
      else
320
0
  goto fail;
321
1.24k
    }
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.08k
  if (! elf_file_p (&x_ehdr)
329
2.12k
      || x_ehdr.e_ident[EI_VERSION] != EV_CURRENT
330
1.89k
      || x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
331
1.21k
    goto wrong;
332
333
  /* Check that file's byte order matches xvec's.  */
334
1.87k
  switch (x_ehdr.e_ident[EI_DATA])
335
1.87k
    {
336
33
    case ELFDATA2MSB:   /* Big-endian.  */
337
33
      if (! bfd_header_big_endian (abfd))
338
0
  goto wrong;
339
33
      break;
340
1.82k
    case ELFDATA2LSB:   /* Little-endian.  */
341
1.82k
      if (! bfd_header_little_endian (abfd))
342
0
  goto wrong;
343
1.82k
      break;
344
1.82k
    case ELFDATANONE:   /* No data encoding specified.  */
345
18
    default:      /* Unknown data encoding specified . */
346
18
      goto wrong;
347
1.87k
    }
348
349
1.85k
  elf_swap_ehdr_in (abfd, &x_ehdr, &i_ehdr);
350
#if DEBUG & 1
351
  elf_debug_file (&i_ehdr);
352
#endif
353
354
1.85k
  if (i_ehdr.e_phentsize != sizeof (Elf_External_Phdr) || i_ehdr.e_phnum == 0)
355
20
    goto fail;
356
357
  /* Read in program headers.  */
358
1.83k
  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.83k
  i_phdr = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
364
1.83k
  if (i_phdr == NULL)
365
0
    goto fail;
366
367
1.83k
  if (bfd_seek (abfd, offset + i_ehdr.e_phoff, SEEK_SET) != 0)
368
4
    goto fail;
369
370
  /* Read in program headers and parse notes.  */
371
191k
  for (i = 0; i < i_ehdr.e_phnum; ++i, ++i_phdr)
372
189k
    {
373
189k
      Elf_External_Phdr x_phdr;
374
375
189k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
376
21
  goto fail;
377
189k
      elf_swap_phdr_in (abfd, &x_phdr, i_phdr);
378
379
189k
      if (i_phdr->p_type == PT_NOTE && i_phdr->p_filesz > 0)
380
2.06k
  {
381
2.06k
    _bfd_elf_read_notes (abfd, offset + i_phdr->p_offset,
382
2.06k
             i_phdr->p_filesz, i_phdr->p_align);
383
384
    /* Make sure ABFD returns to processing the program headers.  */
385
2.06k
    if (bfd_seek (abfd,
386
2.06k
      offset + i_ehdr.e_phoff + (i + 1) * sizeof (x_phdr),
387
2.06k
      SEEK_SET) != 0)
388
0
      goto fail;
389
390
2.06k
    if (abfd->build_id != NULL)
391
4
      return true;
392
2.06k
  }
393
189k
    }
394
395
  /* Having gotten this far, we have a valid ELF section, but no
396
     build-id was found.  */
397
1.80k
  goto fail;
398
399
2.47k
 wrong:
400
2.47k
  bfd_set_error (bfd_error_wrong_format);
401
5.65k
 fail:
402
  return false;
403
2.47k
}
_bfd_elf32_core_find_build_id
Line
Count
Source
303
5.28k
{
304
5.28k
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.   */
305
5.28k
  Elf_Internal_Ehdr i_ehdr; /* Elf file header, internal form.   */
306
5.28k
  Elf_Internal_Phdr *i_phdr;
307
5.28k
  unsigned int i;
308
5.28k
  size_t amt;
309
310
  /* Seek to the position of the segment at OFFSET.  */
311
5.28k
  if (bfd_seek (abfd, offset, SEEK_SET) != 0)
312
0
    goto fail;
313
314
  /* Read in the ELF header in external format.  */
315
5.28k
  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.75k
  if (! elf_file_p (&x_ehdr)
329
1.40k
      || x_ehdr.e_ident[EI_VERSION] != EV_CURRENT
330
1.33k
      || x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
331
2.43k
    goto wrong;
332
333
  /* Check that file's byte order matches xvec's.  */
334
1.31k
  switch (x_ehdr.e_ident[EI_DATA])
335
1.31k
    {
336
1.03k
    case ELFDATA2MSB:   /* Big-endian.  */
337
1.03k
      if (! bfd_header_big_endian (abfd))
338
23
  goto wrong;
339
1.01k
      break;
340
1.01k
    case ELFDATA2LSB:   /* Little-endian.  */
341
275
      if (! bfd_header_little_endian (abfd))
342
0
  goto wrong;
343
275
      break;
344
275
    case ELFDATANONE:   /* No data encoding specified.  */
345
5
    default:      /* Unknown data encoding specified . */
346
5
      goto wrong;
347
1.31k
    }
348
349
1.29k
  elf_swap_ehdr_in (abfd, &x_ehdr, &i_ehdr);
350
#if DEBUG & 1
351
  elf_debug_file (&i_ehdr);
352
#endif
353
354
1.29k
  if (i_ehdr.e_phentsize != sizeof (Elf_External_Phdr) || i_ehdr.e_phnum == 0)
355
13
    goto fail;
356
357
  /* Read in program headers.  */
358
1.27k
  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.27k
  i_phdr = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
364
1.27k
  if (i_phdr == NULL)
365
0
    goto fail;
366
367
1.27k
  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
166k
  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
208
  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
690
  {
381
690
    _bfd_elf_read_notes (abfd, offset + i_phdr->p_offset,
382
690
             i_phdr->p_filesz, i_phdr->p_align);
383
384
    /* Make sure ABFD returns to processing the program headers.  */
385
690
    if (bfd_seek (abfd,
386
690
      offset + i_ehdr.e_phoff + (i + 1) * sizeof (x_phdr),
387
690
      SEEK_SET) != 0)
388
0
      goto fail;
389
390
690
    if (abfd->build_id != NULL)
391
4
      return true;
392
690
  }
393
165k
    }
394
395
  /* Having gotten this far, we have a valid ELF section, but no
396
     build-id was found.  */
397
1.06k
  goto fail;
398
399
3.99k
 wrong:
400
3.99k
  bfd_set_error (bfd_error_wrong_format);
401
5.27k
 fail:
402
  return false;
403
3.99k
}