Coverage Report

Created: 2023-08-28 06:31

/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-2023 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
336k
    {
100
336k
      if (bfd_get_error () != bfd_error_system_call)
101
335k
  goto wrong;
102
534
      else
103
534
  goto fail;
104
336k
    }
105
106
  /* Check the magic number.  */
107
2.59M
  if (! elf_file_p (&x_ehdr))
108
593k
    goto wrong;
109
110
  /* FIXME: Check EI_VERSION here !  */
111
112
  /* Check the address size ("class").  */
113
2.00M
  if (x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
114
673k
    goto wrong;
115
116
  /* Check the byteorder.  */
117
1.32M
  switch (x_ehdr.e_ident[EI_DATA])
118
1.32M
    {
119
1.03M
    case ELFDATA2MSB:   /* Big-endian.  */
120
1.03M
      if (! bfd_big_endian (abfd))
121
517k
  goto wrong;
122
522k
      break;
123
522k
    case ELFDATA2LSB:   /* Little-endian.  */
124
288k
      if (! bfd_little_endian (abfd))
125
139k
  goto wrong;
126
149k
      break;
127
149k
    default:
128
980
      goto wrong;
129
1.32M
    }
130
131
  /* Give abfd an elf_obj_tdata.  */
132
671k
  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
671k
  i_ehdrp = elf_elfheader (abfd);
137
671k
  elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
138
139
#if DEBUG & 1
140
  elf_debug_file (i_ehdrp);
141
#endif
142
143
671k
  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
671k
  if (ebd->elf_machine_code != i_ehdrp->e_machine
149
671k
      && (ebd->elf_machine_alt1 == 0
150
645k
    || i_ehdrp->e_machine != ebd->elf_machine_alt1)
151
671k
      && (ebd->elf_machine_alt2 == 0
152
644k
    || i_ehdrp->e_machine != ebd->elf_machine_alt2)
153
671k
      && ebd->elf_machine_code != EM_NONE)
154
633k
    goto wrong;
155
156
38.3k
  if (ebd->elf_machine_code != EM_NONE
157
38.3k
      && i_ehdrp->e_ident[EI_OSABI] != ebd->elf_osabi
158
38.3k
      && ebd->elf_osabi != ELFOSABI_NONE)
159
2.59k
    goto wrong;
160
161
  /* If there is no program header, or the type is not a core file, then
162
     we are hosed.  */
163
35.7k
  if (i_ehdrp->e_phoff == 0 || i_ehdrp->e_type != ET_CORE)
164
4.48k
    goto wrong;
165
166
  /* Does BFD's idea of the phdr size match the size
167
     recorded in the file? */
168
31.2k
  if (i_ehdrp->e_phentsize != sizeof (Elf_External_Phdr))
169
91
    goto wrong;
170
171
  /* If the program header count is PN_XNUM(0xffff), the actual
172
     count is in the first section header.  */
173
31.1k
  if (i_ehdrp->e_shoff != 0 && i_ehdrp->e_phnum == PN_XNUM)
174
315
    {
175
315
      Elf_External_Shdr x_shdr;
176
315
      Elf_Internal_Shdr i_shdr;
177
315
      file_ptr where = (file_ptr) i_ehdrp->e_shoff;
178
179
315
      if (i_ehdrp->e_shoff < sizeof (x_ehdr))
180
15
  goto wrong;
181
182
      /* Seek to the section header table in the file.  */
183
300
      if (bfd_seek (abfd, where, SEEK_SET) != 0)
184
20
  goto fail;
185
186
      /* Read the first section header at index 0, and convert to internal
187
   form.  */
188
280
      if (bfd_read (&x_shdr, sizeof (x_shdr), abfd) != sizeof (x_shdr))
189
47
  goto fail;
190
233
      elf_swap_shdr_in (abfd, &x_shdr, &i_shdr);
191
192
233
      if (i_shdr.sh_info != 0)
193
210
  {
194
210
    i_ehdrp->e_phnum = i_shdr.sh_info;
195
210
    if (i_ehdrp->e_phnum != i_shdr.sh_info)
196
0
      goto wrong;
197
210
  }
198
233
    }
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
31.1k
  if (i_ehdrp->e_phnum > 1)
203
29.3k
    {
204
29.3k
      Elf_External_Phdr x_phdr;
205
29.3k
      Elf_Internal_Phdr i_phdr;
206
29.3k
      file_ptr where;
207
208
      /* Check that we don't have a totally silly number of
209
   program headers.  */
210
29.3k
      if (i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (x_phdr)
211
29.3k
    || i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (i_phdr))
212
41
  goto wrong;
213
214
29.3k
      where = (file_ptr)(i_ehdrp->e_phoff + (i_ehdrp->e_phnum - 1) * sizeof (x_phdr));
215
29.3k
      if ((bfd_size_type) where <= i_ehdrp->e_phoff)
216
12
  goto wrong;
217
218
29.2k
      if (bfd_seek (abfd, where, SEEK_SET) != 0)
219
23
  goto fail;
220
29.2k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
221
138
  goto fail;
222
29.2k
    }
223
224
  /* Move to the start of the program headers.  */
225
30.9k
  if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0)
226
20
    goto wrong;
227
228
  /* Allocate space for the program headers.  */
229
30.8k
  amt = sizeof (*i_phdrp) * i_ehdrp->e_phnum;
230
30.8k
  i_phdrp = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
231
30.8k
  if (!i_phdrp)
232
0
    goto fail;
233
234
30.8k
  elf_tdata (abfd)->phdr = i_phdrp;
235
236
  /* Read and convert to internal form.  */
237
1.45M
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
238
1.42M
    {
239
1.42M
      Elf_External_Phdr x_phdr;
240
241
1.42M
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
242
34
  goto fail;
243
244
1.42M
      elf_swap_phdr_in (abfd, &x_phdr, i_phdrp + phindex);
245
1.42M
    }
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
30.8k
  if (! bfd_default_set_arch_mach (abfd, ebd->arch, 0)
251
      /* It's OK if this fails for the generic target.  */
252
30.8k
      && 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
30.8k
  if (ebd->elf_backend_object_p != NULL
261
30.8k
      && ! ebd->elf_backend_object_p (abfd))
262
2.84k
    goto wrong;
263
264
  /* Process each program header.  */
265
1.23M
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
266
1.20M
    if (! bfd_section_from_phdr (abfd, i_phdrp + phindex, (int) phindex))
267
1.04k
      goto fail;
268
269
  /* Check for core truncation.  */
270
26.9k
  filesize = bfd_get_file_size (abfd);
271
26.9k
  if (filesize != 0)
272
26.9k
    {
273
44.9k
      for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
274
43.4k
  {
275
43.4k
    Elf_Internal_Phdr *p = i_phdrp + phindex;
276
43.4k
    if (p->p_filesz
277
43.4k
        && (p->p_offset >= filesize
278
36.9k
      || p->p_filesz > filesize - p->p_offset))
279
25.4k
      {
280
25.4k
        _bfd_error_handler (_("warning: %pB has a segment "
281
25.4k
            "extending past end of file"), abfd);
282
25.4k
        abfd->read_only = 1;
283
25.4k
        break;
284
25.4k
      }
285
43.4k
      }
286
26.9k
  }
287
288
  /* Save the entry point from the ELF header.  */
289
26.9k
  abfd->start_address = i_ehdrp->e_entry;
290
26.9k
  return _bfd_no_cleanup;
291
292
2.90M
 wrong:
293
2.90M
  bfd_set_error (bfd_error_wrong_format);
294
2.90M
 fail:
295
2.90M
  return NULL;
296
2.90M
}
bfd_elf64_core_file_p
Line
Count
Source
88
724k
{
89
724k
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.  */
90
724k
  Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form.  */
91
724k
  Elf_Internal_Phdr *i_phdrp; /* Elf program header, internal form.  */
92
724k
  unsigned int phindex;
93
724k
  const struct elf_backend_data *ebd;
94
724k
  bfd_size_type amt;
95
724k
  ufile_ptr filesize;
96
97
  /* Read in the ELF header in external format.  */
98
724k
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
99
132k
    {
100
132k
      if (bfd_get_error () != bfd_error_system_call)
101
132k
  goto wrong;
102
132
      else
103
132
  goto fail;
104
132k
    }
105
106
  /* Check the magic number.  */
107
592k
  if (! elf_file_p (&x_ehdr))
108
137k
    goto wrong;
109
110
  /* FIXME: Check EI_VERSION here !  */
111
112
  /* Check the address size ("class").  */
113
454k
  if (x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
114
346k
    goto wrong;
115
116
  /* Check the byteorder.  */
117
108k
  switch (x_ehdr.e_ident[EI_DATA])
118
108k
    {
119
10.1k
    case ELFDATA2MSB:   /* Big-endian.  */
120
10.1k
      if (! bfd_big_endian (abfd))
121
5.54k
  goto wrong;
122
4.62k
      break;
123
97.8k
    case ELFDATA2LSB:   /* Little-endian.  */
124
97.8k
      if (! bfd_little_endian (abfd))
125
43.9k
  goto wrong;
126
53.8k
      break;
127
53.8k
    default:
128
176
      goto wrong;
129
108k
    }
130
131
  /* Give abfd an elf_obj_tdata.  */
132
58.4k
  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
58.4k
  i_ehdrp = elf_elfheader (abfd);
137
58.4k
  elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
138
139
#if DEBUG & 1
140
  elf_debug_file (i_ehdrp);
141
#endif
142
143
58.4k
  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
58.4k
  if (ebd->elf_machine_code != i_ehdrp->e_machine
149
58.4k
      && (ebd->elf_machine_alt1 == 0
150
52.4k
    || i_ehdrp->e_machine != ebd->elf_machine_alt1)
151
58.4k
      && (ebd->elf_machine_alt2 == 0
152
52.3k
    || i_ehdrp->e_machine != ebd->elf_machine_alt2)
153
58.4k
      && ebd->elf_machine_code != EM_NONE)
154
50.1k
    goto wrong;
155
156
8.37k
  if (ebd->elf_machine_code != EM_NONE
157
8.37k
      && i_ehdrp->e_ident[EI_OSABI] != ebd->elf_osabi
158
8.37k
      && ebd->elf_osabi != ELFOSABI_NONE)
159
1.53k
    goto wrong;
160
161
  /* If there is no program header, or the type is not a core file, then
162
     we are hosed.  */
163
6.84k
  if (i_ehdrp->e_phoff == 0 || i_ehdrp->e_type != ET_CORE)
164
2.36k
    goto wrong;
165
166
  /* Does BFD's idea of the phdr size match the size
167
     recorded in the file? */
168
4.48k
  if (i_ehdrp->e_phentsize != sizeof (Elf_External_Phdr))
169
40
    goto wrong;
170
171
  /* If the program header count is PN_XNUM(0xffff), the actual
172
     count is in the first section header.  */
173
4.44k
  if (i_ehdrp->e_shoff != 0 && i_ehdrp->e_phnum == PN_XNUM)
174
136
    {
175
136
      Elf_External_Shdr x_shdr;
176
136
      Elf_Internal_Shdr i_shdr;
177
136
      file_ptr where = (file_ptr) i_ehdrp->e_shoff;
178
179
136
      if (i_ehdrp->e_shoff < sizeof (x_ehdr))
180
8
  goto wrong;
181
182
      /* Seek to the section header table in the file.  */
183
128
      if (bfd_seek (abfd, where, SEEK_SET) != 0)
184
20
  goto fail;
185
186
      /* Read the first section header at index 0, and convert to internal
187
   form.  */
188
108
      if (bfd_read (&x_shdr, sizeof (x_shdr), abfd) != sizeof (x_shdr))
189
15
  goto fail;
190
93
      elf_swap_shdr_in (abfd, &x_shdr, &i_shdr);
191
192
93
      if (i_shdr.sh_info != 0)
193
82
  {
194
82
    i_ehdrp->e_phnum = i_shdr.sh_info;
195
82
    if (i_ehdrp->e_phnum != i_shdr.sh_info)
196
0
      goto wrong;
197
82
  }
198
93
    }
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
4.39k
  if (i_ehdrp->e_phnum > 1)
203
2.80k
    {
204
2.80k
      Elf_External_Phdr x_phdr;
205
2.80k
      Elf_Internal_Phdr i_phdr;
206
2.80k
      file_ptr where;
207
208
      /* Check that we don't have a totally silly number of
209
   program headers.  */
210
2.80k
      if (i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (x_phdr)
211
2.80k
    || i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (i_phdr))
212
16
  goto wrong;
213
214
2.78k
      where = (file_ptr)(i_ehdrp->e_phoff + (i_ehdrp->e_phnum - 1) * sizeof (x_phdr));
215
2.78k
      if ((bfd_size_type) where <= i_ehdrp->e_phoff)
216
12
  goto wrong;
217
218
2.77k
      if (bfd_seek (abfd, where, SEEK_SET) != 0)
219
23
  goto fail;
220
2.75k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
221
63
  goto fail;
222
2.75k
    }
223
224
  /* Move to the start of the program headers.  */
225
4.28k
  if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0)
226
20
    goto wrong;
227
228
  /* Allocate space for the program headers.  */
229
4.26k
  amt = sizeof (*i_phdrp) * i_ehdrp->e_phnum;
230
4.26k
  i_phdrp = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
231
4.26k
  if (!i_phdrp)
232
0
    goto fail;
233
234
4.26k
  elf_tdata (abfd)->phdr = i_phdrp;
235
236
  /* Read and convert to internal form.  */
237
125k
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
238
121k
    {
239
121k
      Elf_External_Phdr x_phdr;
240
241
121k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
242
24
  goto fail;
243
244
121k
      elf_swap_phdr_in (abfd, &x_phdr, i_phdrp + phindex);
245
121k
    }
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
4.23k
  if (! bfd_default_set_arch_mach (abfd, ebd->arch, 0)
251
      /* It's OK if this fails for the generic target.  */
252
4.23k
      && 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
4.23k
  if (ebd->elf_backend_object_p != NULL
261
4.23k
      && ! ebd->elf_backend_object_p (abfd))
262
2
    goto wrong;
263
264
  /* Process each program header.  */
265
99.4k
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
266
95.8k
    if (! bfd_section_from_phdr (abfd, i_phdrp + phindex, (int) phindex))
267
606
      goto fail;
268
269
  /* Check for core truncation.  */
270
3.63k
  filesize = bfd_get_file_size (abfd);
271
3.63k
  if (filesize != 0)
272
3.63k
    {
273
7.46k
      for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
274
6.11k
  {
275
6.11k
    Elf_Internal_Phdr *p = i_phdrp + phindex;
276
6.11k
    if (p->p_filesz
277
6.11k
        && (p->p_offset >= filesize
278
3.88k
      || p->p_filesz > filesize - p->p_offset))
279
2.28k
      {
280
2.28k
        _bfd_error_handler (_("warning: %pB has a segment "
281
2.28k
            "extending past end of file"), abfd);
282
2.28k
        abfd->read_only = 1;
283
2.28k
        break;
284
2.28k
      }
285
6.11k
      }
286
3.63k
  }
287
288
  /* Save the entry point from the ELF header.  */
289
3.63k
  abfd->start_address = i_ehdrp->e_entry;
290
3.63k
  return _bfd_no_cleanup;
291
292
719k
 wrong:
293
719k
  bfd_set_error (bfd_error_wrong_format);
294
720k
 fail:
295
720k
  return NULL;
296
719k
}
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
204k
    {
100
204k
      if (bfd_get_error () != bfd_error_system_call)
101
203k
  goto wrong;
102
402
      else
103
402
  goto fail;
104
204k
    }
105
106
  /* Check the magic number.  */
107
2.00M
  if (! elf_file_p (&x_ehdr))
108
455k
    goto wrong;
109
110
  /* FIXME: Check EI_VERSION here !  */
111
112
  /* Check the address size ("class").  */
113
1.54M
  if (x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
114
327k
    goto wrong;
115
116
  /* Check the byteorder.  */
117
1.22M
  switch (x_ehdr.e_ident[EI_DATA])
118
1.22M
    {
119
1.02M
    case ELFDATA2MSB:   /* Big-endian.  */
120
1.02M
      if (! bfd_big_endian (abfd))
121
511k
  goto wrong;
122
517k
      break;
123
517k
    case ELFDATA2LSB:   /* Little-endian.  */
124
190k
      if (! bfd_little_endian (abfd))
125
95.2k
  goto wrong;
126
95.3k
      break;
127
95.3k
    default:
128
804
      goto wrong;
129
1.22M
    }
130
131
  /* Give abfd an elf_obj_tdata.  */
132
613k
  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
613k
  i_ehdrp = elf_elfheader (abfd);
137
613k
  elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
138
139
#if DEBUG & 1
140
  elf_debug_file (i_ehdrp);
141
#endif
142
143
613k
  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
613k
  if (ebd->elf_machine_code != i_ehdrp->e_machine
149
613k
      && (ebd->elf_machine_alt1 == 0
150
593k
    || i_ehdrp->e_machine != ebd->elf_machine_alt1)
151
613k
      && (ebd->elf_machine_alt2 == 0
152
592k
    || i_ehdrp->e_machine != ebd->elf_machine_alt2)
153
613k
      && ebd->elf_machine_code != EM_NONE)
154
583k
    goto wrong;
155
156
29.9k
  if (ebd->elf_machine_code != EM_NONE
157
29.9k
      && i_ehdrp->e_ident[EI_OSABI] != ebd->elf_osabi
158
29.9k
      && ebd->elf_osabi != ELFOSABI_NONE)
159
1.06k
    goto wrong;
160
161
  /* If there is no program header, or the type is not a core file, then
162
     we are hosed.  */
163
28.9k
  if (i_ehdrp->e_phoff == 0 || i_ehdrp->e_type != ET_CORE)
164
2.12k
    goto wrong;
165
166
  /* Does BFD's idea of the phdr size match the size
167
     recorded in the file? */
168
26.8k
  if (i_ehdrp->e_phentsize != sizeof (Elf_External_Phdr))
169
51
    goto wrong;
170
171
  /* If the program header count is PN_XNUM(0xffff), the actual
172
     count is in the first section header.  */
173
26.7k
  if (i_ehdrp->e_shoff != 0 && i_ehdrp->e_phnum == PN_XNUM)
174
179
    {
175
179
      Elf_External_Shdr x_shdr;
176
179
      Elf_Internal_Shdr i_shdr;
177
179
      file_ptr where = (file_ptr) i_ehdrp->e_shoff;
178
179
179
      if (i_ehdrp->e_shoff < sizeof (x_ehdr))
180
7
  goto wrong;
181
182
      /* Seek to the section header table in the file.  */
183
172
      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
172
      if (bfd_read (&x_shdr, sizeof (x_shdr), abfd) != sizeof (x_shdr))
189
32
  goto fail;
190
140
      elf_swap_shdr_in (abfd, &x_shdr, &i_shdr);
191
192
140
      if (i_shdr.sh_info != 0)
193
128
  {
194
128
    i_ehdrp->e_phnum = i_shdr.sh_info;
195
128
    if (i_ehdrp->e_phnum != i_shdr.sh_info)
196
0
      goto wrong;
197
128
  }
198
140
    }
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
26.7k
  if (i_ehdrp->e_phnum > 1)
203
26.5k
    {
204
26.5k
      Elf_External_Phdr x_phdr;
205
26.5k
      Elf_Internal_Phdr i_phdr;
206
26.5k
      file_ptr where;
207
208
      /* Check that we don't have a totally silly number of
209
   program headers.  */
210
26.5k
      if (i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (x_phdr)
211
26.5k
    || i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (i_phdr))
212
25
  goto wrong;
213
214
26.5k
      where = (file_ptr)(i_ehdrp->e_phoff + (i_ehdrp->e_phnum - 1) * sizeof (x_phdr));
215
26.5k
      if ((bfd_size_type) where <= i_ehdrp->e_phoff)
216
0
  goto wrong;
217
218
26.5k
      if (bfd_seek (abfd, where, SEEK_SET) != 0)
219
0
  goto fail;
220
26.5k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
221
75
  goto fail;
222
26.5k
    }
223
224
  /* Move to the start of the program headers.  */
225
26.6k
  if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0)
226
0
    goto wrong;
227
228
  /* Allocate space for the program headers.  */
229
26.6k
  amt = sizeof (*i_phdrp) * i_ehdrp->e_phnum;
230
26.6k
  i_phdrp = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
231
26.6k
  if (!i_phdrp)
232
0
    goto fail;
233
234
26.6k
  elf_tdata (abfd)->phdr = i_phdrp;
235
236
  /* Read and convert to internal form.  */
237
1.32M
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
238
1.29M
    {
239
1.29M
      Elf_External_Phdr x_phdr;
240
241
1.29M
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
242
10
  goto fail;
243
244
1.29M
      elf_swap_phdr_in (abfd, &x_phdr, i_phdrp + phindex);
245
1.29M
    }
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
26.6k
  if (! bfd_default_set_arch_mach (abfd, ebd->arch, 0)
251
      /* It's OK if this fails for the generic target.  */
252
26.6k
      && 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
26.6k
  if (ebd->elf_backend_object_p != NULL
261
26.6k
      && ! ebd->elf_backend_object_p (abfd))
262
2.84k
    goto wrong;
263
264
  /* Process each program header.  */
265
1.13M
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
266
1.10M
    if (! bfd_section_from_phdr (abfd, i_phdrp + phindex, (int) phindex))
267
438
      goto fail;
268
269
  /* Check for core truncation.  */
270
23.3k
  filesize = bfd_get_file_size (abfd);
271
23.3k
  if (filesize != 0)
272
23.3k
    {
273
37.5k
      for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
274
37.3k
  {
275
37.3k
    Elf_Internal_Phdr *p = i_phdrp + phindex;
276
37.3k
    if (p->p_filesz
277
37.3k
        && (p->p_offset >= filesize
278
33.0k
      || p->p_filesz > filesize - p->p_offset))
279
23.1k
      {
280
23.1k
        _bfd_error_handler (_("warning: %pB has a segment "
281
23.1k
            "extending past end of file"), abfd);
282
23.1k
        abfd->read_only = 1;
283
23.1k
        break;
284
23.1k
      }
285
37.3k
      }
286
23.3k
  }
287
288
  /* Save the entry point from the ELF header.  */
289
23.3k
  abfd->start_address = i_ehdrp->e_entry;
290
23.3k
  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
12.6k
{
307
12.6k
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.   */
308
12.6k
  Elf_Internal_Ehdr i_ehdr; /* Elf file header, internal form.   */
309
12.6k
  Elf_Internal_Phdr *i_phdr;
310
12.6k
  unsigned int i;
311
12.6k
  size_t amt;
312
313
  /* Seek to the position of the segment at OFFSET.  */
314
12.6k
  if (bfd_seek (abfd, offset, SEEK_SET) != 0)
315
1.01k
    goto fail;
316
317
  /* Read in the ELF header in external format.  */
318
11.6k
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
319
4.19k
    {
320
4.19k
      if (bfd_get_error () != bfd_error_system_call)
321
4.19k
  goto wrong;
322
0
      else
323
0
  goto fail;
324
4.19k
    }
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
7.41k
  if (! elf_file_p (&x_ehdr)
332
7.41k
      || x_ehdr.e_ident[EI_VERSION] != EV_CURRENT
333
7.41k
      || x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
334
5.60k
    goto wrong;
335
336
  /* Check that file's byte order matches xvec's.  */
337
1.81k
  switch (x_ehdr.e_ident[EI_DATA])
338
1.81k
    {
339
975
    case ELFDATA2MSB:   /* Big-endian.  */
340
975
      if (! bfd_header_big_endian (abfd))
341
0
  goto wrong;
342
975
      break;
343
975
    case ELFDATA2LSB:   /* Little-endian.  */
344
841
      if (! bfd_header_little_endian (abfd))
345
0
  goto wrong;
346
841
      break;
347
841
    case ELFDATANONE:   /* No data encoding specified.  */
348
1
    default:      /* Unknown data encoding specified . */
349
1
      goto wrong;
350
1.81k
    }
351
352
1.81k
  elf_swap_ehdr_in (abfd, &x_ehdr, &i_ehdr);
353
#if DEBUG
354
  elf_debug_file (&i_ehdr);
355
#endif
356
357
1.81k
  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
1.81k
  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.81k
  i_phdr = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
367
1.81k
  if (i_phdr == NULL)
368
0
    goto fail;
369
370
1.81k
  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.42M
  for (i = 0; i < i_ehdr.e_phnum; ++i, ++i_phdr)
375
1.42M
    {
376
1.42M
      Elf_External_Phdr x_phdr;
377
378
1.42M
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
379
223
  goto fail;
380
1.42M
      elf_swap_phdr_in (abfd, &x_phdr, i_phdr);
381
382
1.42M
      if (i_phdr->p_type == PT_NOTE && i_phdr->p_filesz > 0)
383
1.78k
  {
384
1.78k
    elf_read_notes (abfd, offset + i_phdr->p_offset,
385
1.78k
        i_phdr->p_filesz, i_phdr->p_align);
386
387
    /* Make sure ABFD returns to processing the program headers.  */
388
1.78k
    if (bfd_seek (abfd,
389
1.78k
      offset + i_ehdr.e_phoff + (i + 1) * sizeof (x_phdr),
390
1.78k
      SEEK_SET) != 0)
391
0
      goto fail;
392
393
1.78k
    if (abfd->build_id != NULL)
394
4
      return true;
395
1.78k
  }
396
1.42M
    }
397
398
  /* Having gotten this far, we have a valid ELF section, but no
399
     build-id was found.  */
400
1.58k
  goto fail;
401
402
9.79k
 wrong:
403
9.79k
  bfd_set_error (bfd_error_wrong_format);
404
12.6k
 fail:
405
12.6k
  return false;
406
9.79k
}
_bfd_elf64_core_find_build_id
Line
Count
Source
306
2.61k
{
307
2.61k
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.   */
308
2.61k
  Elf_Internal_Ehdr i_ehdr; /* Elf file header, internal form.   */
309
2.61k
  Elf_Internal_Phdr *i_phdr;
310
2.61k
  unsigned int i;
311
2.61k
  size_t amt;
312
313
  /* Seek to the position of the segment at OFFSET.  */
314
2.61k
  if (bfd_seek (abfd, offset, SEEK_SET) != 0)
315
1.01k
    goto fail;
316
317
  /* Read in the ELF header in external format.  */
318
1.59k
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
319
491
    {
320
491
      if (bfd_get_error () != bfd_error_system_call)
321
491
  goto wrong;
322
0
      else
323
0
  goto fail;
324
491
    }
325
326
  /* Now check to see if we have a valid ELF file, and one that BFD can
327
     make use of.  The magic number must match, the address size ('class')
328
     and byte-swapping must match our XVEC entry, and it must have a
329
     section header table (FIXME: See comments re sections at top of this
330
     file).  */
331
1.10k
  if (! elf_file_p (&x_ehdr)
332
1.10k
      || x_ehdr.e_ident[EI_VERSION] != EV_CURRENT
333
1.10k
      || x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
334
566
    goto wrong;
335
336
  /* Check that file's byte order matches xvec's.  */
337
537
  switch (x_ehdr.e_ident[EI_DATA])
338
537
    {
339
0
    case ELFDATA2MSB:   /* Big-endian.  */
340
0
      if (! bfd_header_big_endian (abfd))
341
0
  goto wrong;
342
0
      break;
343
536
    case ELFDATA2LSB:   /* Little-endian.  */
344
536
      if (! bfd_header_little_endian (abfd))
345
0
  goto wrong;
346
536
      break;
347
536
    case ELFDATANONE:   /* No data encoding specified.  */
348
1
    default:      /* Unknown data encoding specified . */
349
1
      goto wrong;
350
537
    }
351
352
536
  elf_swap_ehdr_in (abfd, &x_ehdr, &i_ehdr);
353
#if DEBUG
354
  elf_debug_file (&i_ehdr);
355
#endif
356
357
536
  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
536
  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
536
  i_phdr = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
367
536
  if (i_phdr == NULL)
368
0
    goto fail;
369
370
536
  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
259k
  for (i = 0; i < i_ehdr.e_phnum; ++i, ++i_phdr)
375
258k
    {
376
258k
      Elf_External_Phdr x_phdr;
377
378
258k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
379
175
  goto fail;
380
258k
      elf_swap_phdr_in (abfd, &x_phdr, i_phdr);
381
382
258k
      if (i_phdr->p_type == PT_NOTE && i_phdr->p_filesz > 0)
383
1.25k
  {
384
1.25k
    elf_read_notes (abfd, offset + i_phdr->p_offset,
385
1.25k
        i_phdr->p_filesz, i_phdr->p_align);
386
387
    /* Make sure ABFD returns to processing the program headers.  */
388
1.25k
    if (bfd_seek (abfd,
389
1.25k
      offset + i_ehdr.e_phoff + (i + 1) * sizeof (x_phdr),
390
1.25k
      SEEK_SET) != 0)
391
0
      goto fail;
392
393
1.25k
    if (abfd->build_id != NULL)
394
4
      return true;
395
1.25k
  }
396
258k
    }
397
398
  /* Having gotten this far, we have a valid ELF section, but no
399
     build-id was found.  */
400
357
  goto fail;
401
402
1.05k
 wrong:
403
1.05k
  bfd_set_error (bfd_error_wrong_format);
404
2.60k
 fail:
405
2.60k
  return false;
406
1.05k
}
_bfd_elf32_core_find_build_id
Line
Count
Source
306
10.0k
{
307
10.0k
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.   */
308
10.0k
  Elf_Internal_Ehdr i_ehdr; /* Elf file header, internal form.   */
309
10.0k
  Elf_Internal_Phdr *i_phdr;
310
10.0k
  unsigned int i;
311
10.0k
  size_t amt;
312
313
  /* Seek to the position of the segment at OFFSET.  */
314
10.0k
  if (bfd_seek (abfd, offset, SEEK_SET) != 0)
315
0
    goto fail;
316
317
  /* Read in the ELF header in external format.  */
318
10.0k
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
319
3.69k
    {
320
3.69k
      if (bfd_get_error () != bfd_error_system_call)
321
3.69k
  goto wrong;
322
0
      else
323
0
  goto fail;
324
3.69k
    }
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
6.31k
  if (! elf_file_p (&x_ehdr)
332
6.31k
      || x_ehdr.e_ident[EI_VERSION] != EV_CURRENT
333
6.31k
      || x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
334
5.03k
    goto wrong;
335
336
  /* Check that file's byte order matches xvec's.  */
337
1.28k
  switch (x_ehdr.e_ident[EI_DATA])
338
1.28k
    {
339
975
    case ELFDATA2MSB:   /* Big-endian.  */
340
975
      if (! bfd_header_big_endian (abfd))
341
0
  goto wrong;
342
975
      break;
343
975
    case ELFDATA2LSB:   /* Little-endian.  */
344
305
      if (! bfd_header_little_endian (abfd))
345
0
  goto wrong;
346
305
      break;
347
305
    case ELFDATANONE:   /* No data encoding specified.  */
348
0
    default:      /* Unknown data encoding specified . */
349
0
      goto wrong;
350
1.28k
    }
351
352
1.28k
  elf_swap_ehdr_in (abfd, &x_ehdr, &i_ehdr);
353
#if DEBUG
354
  elf_debug_file (&i_ehdr);
355
#endif
356
357
1.28k
  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
1.28k
  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.28k
  i_phdr = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
367
1.28k
  if (i_phdr == NULL)
368
0
    goto fail;
369
370
1.28k
  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.16M
  for (i = 0; i < i_ehdr.e_phnum; ++i, ++i_phdr)
375
1.16M
    {
376
1.16M
      Elf_External_Phdr x_phdr;
377
378
1.16M
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
379
48
  goto fail;
380
1.16M
      elf_swap_phdr_in (abfd, &x_phdr, i_phdr);
381
382
1.16M
      if (i_phdr->p_type == PT_NOTE && i_phdr->p_filesz > 0)
383
527
  {
384
527
    elf_read_notes (abfd, offset + i_phdr->p_offset,
385
527
        i_phdr->p_filesz, i_phdr->p_align);
386
387
    /* Make sure ABFD returns to processing the program headers.  */
388
527
    if (bfd_seek (abfd,
389
527
      offset + i_ehdr.e_phoff + (i + 1) * sizeof (x_phdr),
390
527
      SEEK_SET) != 0)
391
0
      goto fail;
392
393
527
    if (abfd->build_id != NULL)
394
0
      return true;
395
527
  }
396
1.16M
    }
397
398
  /* Having gotten this far, we have a valid ELF section, but no
399
     build-id was found.  */
400
1.23k
  goto fail;
401
402
8.73k
 wrong:
403
8.73k
  bfd_set_error (bfd_error_wrong_format);
404
10.0k
 fail:
405
10.0k
  return false;
406
8.73k
}