Coverage Report

Created: 2023-06-29 07:13

/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.76M
{
89
2.76M
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.  */
90
2.76M
  Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form.  */
91
2.76M
  Elf_Internal_Phdr *i_phdrp; /* Elf program header, internal form.  */
92
2.76M
  unsigned int phindex;
93
2.76M
  const struct elf_backend_data *ebd;
94
2.76M
  bfd_size_type amt;
95
2.76M
  ufile_ptr filesize;
96
97
  /* Read in the ELF header in external format.  */
98
2.76M
  if (bfd_bread (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
99
436k
    {
100
436k
      if (bfd_get_error () != bfd_error_system_call)
101
435k
  goto wrong;
102
352
      else
103
352
  goto fail;
104
436k
    }
105
106
  /* Check the magic number.  */
107
2.33M
  if (! elf_file_p (&x_ehdr))
108
709k
    goto wrong;
109
110
  /* FIXME: Check EI_VERSION here !  */
111
112
  /* Check the address size ("class").  */
113
1.62M
  if (x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
114
610k
    goto wrong;
115
116
  /* Check the byteorder.  */
117
1.01M
  switch (x_ehdr.e_ident[EI_DATA])
118
1.01M
    {
119
740k
    case ELFDATA2MSB:   /* Big-endian.  */
120
740k
      if (! bfd_big_endian (abfd))
121
365k
  goto wrong;
122
374k
      break;
123
374k
    case ELFDATA2LSB:   /* Little-endian.  */
124
272k
      if (! bfd_little_endian (abfd))
125
132k
  goto wrong;
126
139k
      break;
127
139k
    default:
128
704
      goto wrong;
129
1.01M
    }
130
131
  /* Give abfd an elf_obj_tdata.  */
132
513k
  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
513k
  i_ehdrp = elf_elfheader (abfd);
137
513k
  elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
138
139
#if DEBUG & 1
140
  elf_debug_file (i_ehdrp);
141
#endif
142
143
513k
  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
513k
  if (ebd->elf_machine_code != i_ehdrp->e_machine
149
513k
      && (ebd->elf_machine_alt1 == 0
150
493k
    || i_ehdrp->e_machine != ebd->elf_machine_alt1)
151
513k
      && (ebd->elf_machine_alt2 == 0
152
492k
    || i_ehdrp->e_machine != ebd->elf_machine_alt2)
153
513k
      && ebd->elf_machine_code != EM_NONE)
154
483k
    goto wrong;
155
156
30.4k
  if (ebd->elf_machine_code != EM_NONE
157
30.4k
      && i_ehdrp->e_ident[EI_OSABI] != ebd->elf_osabi
158
30.4k
      && ebd->elf_osabi != ELFOSABI_NONE)
159
2.71k
    goto wrong;
160
161
  /* If there is no program header, or the type is not a core file, then
162
     we are hosed.  */
163
27.7k
  if (i_ehdrp->e_phoff == 0 || i_ehdrp->e_type != ET_CORE)
164
5.62k
    goto wrong;
165
166
  /* Does BFD's idea of the phdr size match the size
167
     recorded in the file? */
168
22.1k
  if (i_ehdrp->e_phentsize != sizeof (Elf_External_Phdr))
169
70
    goto wrong;
170
171
  /* If the program header count is PN_XNUM(0xffff), the actual
172
     count is in the first section header.  */
173
22.0k
  if (i_ehdrp->e_shoff != 0 && i_ehdrp->e_phnum == PN_XNUM)
174
376
    {
175
376
      Elf_External_Shdr x_shdr;
176
376
      Elf_Internal_Shdr i_shdr;
177
376
      file_ptr where = (file_ptr) i_ehdrp->e_shoff;
178
179
376
      if (i_ehdrp->e_shoff < sizeof (x_ehdr))
180
10
  goto wrong;
181
182
      /* Seek to the section header table in the file.  */
183
366
      if (bfd_seek (abfd, where, SEEK_SET) != 0)
184
21
  goto fail;
185
186
      /* Read the first section header at index 0, and convert to internal
187
   form.  */
188
345
      if (bfd_bread (&x_shdr, sizeof (x_shdr), abfd) != sizeof (x_shdr))
189
31
  goto fail;
190
314
      elf_swap_shdr_in (abfd, &x_shdr, &i_shdr);
191
192
314
      if (i_shdr.sh_info != 0)
193
281
  {
194
281
    i_ehdrp->e_phnum = i_shdr.sh_info;
195
281
    if (i_ehdrp->e_phnum != i_shdr.sh_info)
196
0
      goto wrong;
197
281
  }
198
314
    }
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
21.9k
  if (i_ehdrp->e_phnum > 1)
203
20.3k
    {
204
20.3k
      Elf_External_Phdr x_phdr;
205
20.3k
      Elf_Internal_Phdr i_phdr;
206
20.3k
      file_ptr where;
207
208
      /* Check that we don't have a totally silly number of
209
   program headers.  */
210
20.3k
      if (i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (x_phdr)
211
20.3k
    || i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (i_phdr))
212
74
  goto wrong;
213
214
20.3k
      where = (file_ptr)(i_ehdrp->e_phoff + (i_ehdrp->e_phnum - 1) * sizeof (x_phdr));
215
20.3k
      if ((bfd_size_type) where <= i_ehdrp->e_phoff)
216
12
  goto wrong;
217
218
20.3k
      if (bfd_seek (abfd, where, SEEK_SET) != 0)
219
41
  goto fail;
220
20.2k
      if (bfd_bread (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
221
180
  goto fail;
222
20.2k
    }
223
224
  /* Move to the start of the program headers.  */
225
21.6k
  if (bfd_seek (abfd, (file_ptr) i_ehdrp->e_phoff, SEEK_SET) != 0)
226
23
    goto wrong;
227
228
  /* Allocate space for the program headers.  */
229
21.6k
  amt = sizeof (*i_phdrp) * i_ehdrp->e_phnum;
230
21.6k
  i_phdrp = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
231
21.6k
  if (!i_phdrp)
232
0
    goto fail;
233
234
21.6k
  elf_tdata (abfd)->phdr = i_phdrp;
235
236
  /* Read and convert to internal form.  */
237
1.08M
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
238
1.06M
    {
239
1.06M
      Elf_External_Phdr x_phdr;
240
241
1.06M
      if (bfd_bread (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
242
41
  goto fail;
243
244
1.06M
      elf_swap_phdr_in (abfd, &x_phdr, i_phdrp + phindex);
245
1.06M
    }
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
21.5k
  if (! bfd_default_set_arch_mach (abfd, ebd->arch, 0)
251
      /* It's OK if this fails for the generic target.  */
252
21.5k
      && ebd->elf_machine_code != EM_NONE)
253
2
    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
21.5k
  if (ebd->elf_backend_object_p != NULL
261
21.5k
      && ! ebd->elf_backend_object_p (abfd))
262
1.69k
    goto wrong;
263
264
  /* Process each program header.  */
265
955k
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
266
936k
    if (! bfd_section_from_phdr (abfd, i_phdrp + phindex, (int) phindex))
267
1.30k
      goto fail;
268
269
  /* Check for core truncation.  */
270
18.5k
  filesize = bfd_get_file_size (abfd);
271
18.5k
  if (filesize != 0)
272
18.5k
    {
273
30.0k
      for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
274
28.8k
  {
275
28.8k
    Elf_Internal_Phdr *p = i_phdrp + phindex;
276
28.8k
    if (p->p_filesz
277
28.8k
        && (p->p_offset >= filesize
278
25.1k
      || p->p_filesz > filesize - p->p_offset))
279
17.4k
      {
280
17.4k
        _bfd_error_handler (_("warning: %pB has a segment "
281
17.4k
            "extending past end of file"), abfd);
282
17.4k
        abfd->read_only = 1;
283
17.4k
        break;
284
17.4k
      }
285
28.8k
      }
286
18.5k
  }
287
288
  /* Save the entry point from the ELF header.  */
289
18.5k
  abfd->start_address = i_ehdrp->e_entry;
290
18.5k
  return _bfd_no_cleanup;
291
292
2.74M
 wrong:
293
2.74M
  bfd_set_error (bfd_error_wrong_format);
294
2.75M
 fail:
295
2.75M
  return NULL;
296
2.74M
}
bfd_elf64_core_file_p
Line
Count
Source
88
676k
{
89
676k
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.  */
90
676k
  Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form.  */
91
676k
  Elf_Internal_Phdr *i_phdrp; /* Elf program header, internal form.  */
92
676k
  unsigned int phindex;
93
676k
  const struct elf_backend_data *ebd;
94
676k
  bfd_size_type amt;
95
676k
  ufile_ptr filesize;
96
97
  /* Read in the ELF header in external format.  */
98
676k
  if (bfd_bread (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
99
154k
    {
100
154k
      if (bfd_get_error () != bfd_error_system_call)
101
154k
  goto wrong;
102
86
      else
103
86
  goto fail;
104
154k
    }
105
106
  /* Check the magic number.  */
107
521k
  if (! elf_file_p (&x_ehdr))
108
161k
    goto wrong;
109
110
  /* FIXME: Check EI_VERSION here !  */
111
112
  /* Check the address size ("class").  */
113
360k
  if (x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
114
241k
    goto wrong;
115
116
  /* Check the byteorder.  */
117
119k
  switch (x_ehdr.e_ident[EI_DATA])
118
119k
    {
119
13.0k
    case ELFDATA2MSB:   /* Big-endian.  */
120
13.0k
      if (! bfd_big_endian (abfd))
121
6.96k
  goto wrong;
122
6.06k
      break;
123
106k
    case ELFDATA2LSB:   /* Little-endian.  */
124
106k
      if (! bfd_little_endian (abfd))
125
49.1k
  goto wrong;
126
57.3k
      break;
127
57.3k
    default:
128
172
      goto wrong;
129
119k
    }
130
131
  /* Give abfd an elf_obj_tdata.  */
132
63.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
63.4k
  i_ehdrp = elf_elfheader (abfd);
137
63.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
63.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
63.4k
  if (ebd->elf_machine_code != i_ehdrp->e_machine
149
63.4k
      && (ebd->elf_machine_alt1 == 0
150
56.7k
    || i_ehdrp->e_machine != ebd->elf_machine_alt1)
151
63.4k
      && (ebd->elf_machine_alt2 == 0
152
56.6k
    || i_ehdrp->e_machine != ebd->elf_machine_alt2)
153
63.4k
      && ebd->elf_machine_code != EM_NONE)
154
54.1k
    goto wrong;
155
156
9.30k
  if (ebd->elf_machine_code != EM_NONE
157
9.30k
      && i_ehdrp->e_ident[EI_OSABI] != ebd->elf_osabi
158
9.30k
      && ebd->elf_osabi != ELFOSABI_NONE)
159
1.97k
    goto wrong;
160
161
  /* If there is no program header, or the type is not a core file, then
162
     we are hosed.  */
163
7.33k
  if (i_ehdrp->e_phoff == 0 || i_ehdrp->e_type != ET_CORE)
164
3.42k
    goto wrong;
165
166
  /* Does BFD's idea of the phdr size match the size
167
     recorded in the file? */
168
3.91k
  if (i_ehdrp->e_phentsize != sizeof (Elf_External_Phdr))
169
28
    goto wrong;
170
171
  /* If the program header count is PN_XNUM(0xffff), the actual
172
     count is in the first section header.  */
173
3.88k
  if (i_ehdrp->e_shoff != 0 && i_ehdrp->e_phnum == PN_XNUM)
174
111
    {
175
111
      Elf_External_Shdr x_shdr;
176
111
      Elf_Internal_Shdr i_shdr;
177
111
      file_ptr where = (file_ptr) i_ehdrp->e_shoff;
178
179
111
      if (i_ehdrp->e_shoff < sizeof (x_ehdr))
180
5
  goto wrong;
181
182
      /* Seek to the section header table in the file.  */
183
106
      if (bfd_seek (abfd, where, SEEK_SET) != 0)
184
21
  goto fail;
185
186
      /* Read the first section header at index 0, and convert to internal
187
   form.  */
188
85
      if (bfd_bread (&x_shdr, sizeof (x_shdr), abfd) != sizeof (x_shdr))
189
10
  goto fail;
190
75
      elf_swap_shdr_in (abfd, &x_shdr, &i_shdr);
191
192
75
      if (i_shdr.sh_info != 0)
193
62
  {
194
62
    i_ehdrp->e_phnum = i_shdr.sh_info;
195
62
    if (i_ehdrp->e_phnum != i_shdr.sh_info)
196
0
      goto wrong;
197
62
  }
198
75
    }
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
3.84k
  if (i_ehdrp->e_phnum > 1)
203
2.52k
    {
204
2.52k
      Elf_External_Phdr x_phdr;
205
2.52k
      Elf_Internal_Phdr i_phdr;
206
2.52k
      file_ptr where;
207
208
      /* Check that we don't have a totally silly number of
209
   program headers.  */
210
2.52k
      if (i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (x_phdr)
211
2.52k
    || i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (i_phdr))
212
22
  goto wrong;
213
214
2.50k
      where = (file_ptr)(i_ehdrp->e_phoff + (i_ehdrp->e_phnum - 1) * sizeof (x_phdr));
215
2.50k
      if ((bfd_size_type) where <= i_ehdrp->e_phoff)
216
12
  goto wrong;
217
218
2.49k
      if (bfd_seek (abfd, where, SEEK_SET) != 0)
219
41
  goto fail;
220
2.45k
      if (bfd_bread (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
221
89
  goto fail;
222
2.45k
    }
223
224
  /* Move to the start of the program headers.  */
225
3.68k
  if (bfd_seek (abfd, (file_ptr) i_ehdrp->e_phoff, SEEK_SET) != 0)
226
23
    goto wrong;
227
228
  /* Allocate space for the program headers.  */
229
3.65k
  amt = sizeof (*i_phdrp) * i_ehdrp->e_phnum;
230
3.65k
  i_phdrp = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
231
3.65k
  if (!i_phdrp)
232
0
    goto fail;
233
234
3.65k
  elf_tdata (abfd)->phdr = i_phdrp;
235
236
  /* Read and convert to internal form.  */
237
88.0k
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
238
84.4k
    {
239
84.4k
      Elf_External_Phdr x_phdr;
240
241
84.4k
      if (bfd_bread (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
242
21
  goto fail;
243
244
84.4k
      elf_swap_phdr_in (abfd, &x_phdr, i_phdrp + phindex);
245
84.4k
    }
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
3.63k
  if (! bfd_default_set_arch_mach (abfd, ebd->arch, 0)
251
      /* It's OK if this fails for the generic target.  */
252
3.63k
      && ebd->elf_machine_code != EM_NONE)
253
0
    goto fail;
254
255
  /* Let the backend double check the format and override global
256
     information.  We do this before processing the program headers
257
     to allow the correct machine (as opposed to just the default
258
     machine) to be set, making it possible for grok_prstatus and
259
     grok_psinfo to rely on the mach setting.  */
260
3.63k
  if (ebd->elf_backend_object_p != NULL
261
3.63k
      && ! ebd->elf_backend_object_p (abfd))
262
3
    goto wrong;
263
264
  /* Process each program header.  */
265
76.2k
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
266
73.3k
    if (! bfd_section_from_phdr (abfd, i_phdrp + phindex, (int) phindex))
267
808
      goto fail;
268
269
  /* Check for core truncation.  */
270
2.82k
  filesize = bfd_get_file_size (abfd);
271
2.82k
  if (filesize != 0)
272
2.82k
    {
273
4.39k
      for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
274
3.39k
  {
275
3.39k
    Elf_Internal_Phdr *p = i_phdrp + phindex;
276
3.39k
    if (p->p_filesz
277
3.39k
        && (p->p_offset >= filesize
278
2.97k
      || p->p_filesz > filesize - p->p_offset))
279
1.82k
      {
280
1.82k
        _bfd_error_handler (_("warning: %pB has a segment "
281
1.82k
            "extending past end of file"), abfd);
282
1.82k
        abfd->read_only = 1;
283
1.82k
        break;
284
1.82k
      }
285
3.39k
      }
286
2.82k
  }
287
288
  /* Save the entry point from the ELF header.  */
289
2.82k
  abfd->start_address = i_ehdrp->e_entry;
290
2.82k
  return _bfd_no_cleanup;
291
292
672k
 wrong:
293
672k
  bfd_set_error (bfd_error_wrong_format);
294
673k
 fail:
295
673k
  return NULL;
296
672k
}
bfd_elf32_core_file_p
Line
Count
Source
88
2.09M
{
89
2.09M
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.  */
90
2.09M
  Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form.  */
91
2.09M
  Elf_Internal_Phdr *i_phdrp; /* Elf program header, internal form.  */
92
2.09M
  unsigned int phindex;
93
2.09M
  const struct elf_backend_data *ebd;
94
2.09M
  bfd_size_type amt;
95
2.09M
  ufile_ptr filesize;
96
97
  /* Read in the ELF header in external format.  */
98
2.09M
  if (bfd_bread (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
99
281k
    {
100
281k
      if (bfd_get_error () != bfd_error_system_call)
101
281k
  goto wrong;
102
266
      else
103
266
  goto fail;
104
281k
    }
105
106
  /* Check the magic number.  */
107
1.81M
  if (! elf_file_p (&x_ehdr))
108
548k
    goto wrong;
109
110
  /* FIXME: Check EI_VERSION here !  */
111
112
  /* Check the address size ("class").  */
113
1.26M
  if (x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
114
369k
    goto wrong;
115
116
  /* Check the byteorder.  */
117
893k
  switch (x_ehdr.e_ident[EI_DATA])
118
893k
    {
119
727k
    case ELFDATA2MSB:   /* Big-endian.  */
120
727k
      if (! bfd_big_endian (abfd))
121
358k
  goto wrong;
122
368k
      break;
123
368k
    case ELFDATA2LSB:   /* Little-endian.  */
124
165k
      if (! bfd_little_endian (abfd))
125
83.3k
  goto wrong;
126
82.2k
      break;
127
82.2k
    default:
128
532
      goto wrong;
129
893k
    }
130
131
  /* Give abfd an elf_obj_tdata.  */
132
450k
  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
450k
  i_ehdrp = elf_elfheader (abfd);
137
450k
  elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
138
139
#if DEBUG & 1
140
  elf_debug_file (i_ehdrp);
141
#endif
142
143
450k
  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
450k
  if (ebd->elf_machine_code != i_ehdrp->e_machine
149
450k
      && (ebd->elf_machine_alt1 == 0
150
436k
    || i_ehdrp->e_machine != ebd->elf_machine_alt1)
151
450k
      && (ebd->elf_machine_alt2 == 0
152
435k
    || i_ehdrp->e_machine != ebd->elf_machine_alt2)
153
450k
      && ebd->elf_machine_code != EM_NONE)
154
429k
    goto wrong;
155
156
21.1k
  if (ebd->elf_machine_code != EM_NONE
157
21.1k
      && i_ehdrp->e_ident[EI_OSABI] != ebd->elf_osabi
158
21.1k
      && ebd->elf_osabi != ELFOSABI_NONE)
159
739
    goto wrong;
160
161
  /* If there is no program header, or the type is not a core file, then
162
     we are hosed.  */
163
20.3k
  if (i_ehdrp->e_phoff == 0 || i_ehdrp->e_type != ET_CORE)
164
2.20k
    goto wrong;
165
166
  /* Does BFD's idea of the phdr size match the size
167
     recorded in the file? */
168
18.1k
  if (i_ehdrp->e_phentsize != sizeof (Elf_External_Phdr))
169
42
    goto wrong;
170
171
  /* If the program header count is PN_XNUM(0xffff), the actual
172
     count is in the first section header.  */
173
18.1k
  if (i_ehdrp->e_shoff != 0 && i_ehdrp->e_phnum == PN_XNUM)
174
265
    {
175
265
      Elf_External_Shdr x_shdr;
176
265
      Elf_Internal_Shdr i_shdr;
177
265
      file_ptr where = (file_ptr) i_ehdrp->e_shoff;
178
179
265
      if (i_ehdrp->e_shoff < sizeof (x_ehdr))
180
5
  goto wrong;
181
182
      /* Seek to the section header table in the file.  */
183
260
      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
260
      if (bfd_bread (&x_shdr, sizeof (x_shdr), abfd) != sizeof (x_shdr))
189
21
  goto fail;
190
239
      elf_swap_shdr_in (abfd, &x_shdr, &i_shdr);
191
192
239
      if (i_shdr.sh_info != 0)
193
219
  {
194
219
    i_ehdrp->e_phnum = i_shdr.sh_info;
195
219
    if (i_ehdrp->e_phnum != i_shdr.sh_info)
196
0
      goto wrong;
197
219
  }
198
239
    }
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
18.1k
  if (i_ehdrp->e_phnum > 1)
203
17.8k
    {
204
17.8k
      Elf_External_Phdr x_phdr;
205
17.8k
      Elf_Internal_Phdr i_phdr;
206
17.8k
      file_ptr where;
207
208
      /* Check that we don't have a totally silly number of
209
   program headers.  */
210
17.8k
      if (i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (x_phdr)
211
17.8k
    || i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (i_phdr))
212
52
  goto wrong;
213
214
17.8k
      where = (file_ptr)(i_ehdrp->e_phoff + (i_ehdrp->e_phnum - 1) * sizeof (x_phdr));
215
17.8k
      if ((bfd_size_type) where <= i_ehdrp->e_phoff)
216
0
  goto wrong;
217
218
17.8k
      if (bfd_seek (abfd, where, SEEK_SET) != 0)
219
0
  goto fail;
220
17.8k
      if (bfd_bread (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
221
91
  goto fail;
222
17.8k
    }
223
224
  /* Move to the start of the program headers.  */
225
17.9k
  if (bfd_seek (abfd, (file_ptr) i_ehdrp->e_phoff, SEEK_SET) != 0)
226
0
    goto wrong;
227
228
  /* Allocate space for the program headers.  */
229
17.9k
  amt = sizeof (*i_phdrp) * i_ehdrp->e_phnum;
230
17.9k
  i_phdrp = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
231
17.9k
  if (!i_phdrp)
232
0
    goto fail;
233
234
17.9k
  elf_tdata (abfd)->phdr = i_phdrp;
235
236
  /* Read and convert to internal form.  */
237
999k
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
238
981k
    {
239
981k
      Elf_External_Phdr x_phdr;
240
241
981k
      if (bfd_bread (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
242
20
  goto fail;
243
244
981k
      elf_swap_phdr_in (abfd, &x_phdr, i_phdrp + phindex);
245
981k
    }
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
17.9k
  if (! bfd_default_set_arch_mach (abfd, ebd->arch, 0)
251
      /* It's OK if this fails for the generic target.  */
252
17.9k
      && ebd->elf_machine_code != EM_NONE)
253
2
    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
17.9k
  if (ebd->elf_backend_object_p != NULL
261
17.9k
      && ! ebd->elf_backend_object_p (abfd))
262
1.69k
    goto wrong;
263
264
  /* Process each program header.  */
265
878k
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
266
863k
    if (! bfd_section_from_phdr (abfd, i_phdrp + phindex, (int) phindex))
267
496
      goto fail;
268
269
  /* Check for core truncation.  */
270
15.7k
  filesize = bfd_get_file_size (abfd);
271
15.7k
  if (filesize != 0)
272
15.7k
    {
273
25.6k
      for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
274
25.4k
  {
275
25.4k
    Elf_Internal_Phdr *p = i_phdrp + phindex;
276
25.4k
    if (p->p_filesz
277
25.4k
        && (p->p_offset >= filesize
278
22.1k
      || p->p_filesz > filesize - p->p_offset))
279
15.5k
      {
280
15.5k
        _bfd_error_handler (_("warning: %pB has a segment "
281
15.5k
            "extending past end of file"), abfd);
282
15.5k
        abfd->read_only = 1;
283
15.5k
        break;
284
15.5k
      }
285
25.4k
      }
286
15.7k
  }
287
288
  /* Save the entry point from the ELF header.  */
289
15.7k
  abfd->start_address = i_ehdrp->e_entry;
290
15.7k
  return _bfd_no_cleanup;
291
292
2.07M
 wrong:
293
2.07M
  bfd_set_error (bfd_error_wrong_format);
294
2.07M
 fail:
295
2.07M
  return NULL;
296
2.07M
}
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
11.9k
{
307
11.9k
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.   */
308
11.9k
  Elf_Internal_Ehdr i_ehdr; /* Elf file header, internal form.   */
309
11.9k
  Elf_Internal_Phdr *i_phdr;
310
11.9k
  unsigned int i;
311
11.9k
  size_t amt;
312
313
  /* Seek to the position of the segment at OFFSET.  */
314
11.9k
  if (bfd_seek (abfd, offset, SEEK_SET) != 0)
315
903
    goto fail;
316
317
  /* Read in the ELF header in external format.  */
318
11.0k
  if (bfd_bread (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
319
3.93k
    {
320
3.93k
      if (bfd_get_error () != bfd_error_system_call)
321
3.93k
  goto wrong;
322
0
      else
323
0
  goto fail;
324
3.93k
    }
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.08k
  if (! elf_file_p (&x_ehdr)
332
7.08k
      || x_ehdr.e_ident[EI_VERSION] != EV_CURRENT
333
7.08k
      || x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
334
4.95k
    goto wrong;
335
336
  /* Check that file's byte order matches xvec's.  */
337
2.12k
  switch (x_ehdr.e_ident[EI_DATA])
338
2.12k
    {
339
1.15k
    case ELFDATA2MSB:   /* Big-endian.  */
340
1.15k
      if (! bfd_header_big_endian (abfd))
341
0
  goto wrong;
342
1.15k
      break;
343
1.15k
    case ELFDATA2LSB:   /* Little-endian.  */
344
970
      if (! bfd_header_little_endian (abfd))
345
0
  goto wrong;
346
970
      break;
347
970
    case ELFDATANONE:   /* No data encoding specified.  */
348
1
    default:      /* Unknown data encoding specified . */
349
1
      goto wrong;
350
2.12k
    }
351
352
2.12k
  elf_swap_ehdr_in (abfd, &x_ehdr, &i_ehdr);
353
#if DEBUG
354
  elf_debug_file (&i_ehdr);
355
#endif
356
357
2.12k
  if (i_ehdr.e_phentsize != sizeof (Elf_External_Phdr) || i_ehdr.e_phnum == 0)
358
6
    goto fail;
359
360
  /* Read in program headers.  */
361
2.11k
  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
2.11k
  i_phdr = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
367
2.11k
  if (i_phdr == NULL)
368
0
    goto fail;
369
370
2.11k
  if (bfd_seek (abfd, (file_ptr) (offset + i_ehdr.e_phoff), SEEK_SET) != 0)
371
0
    goto fail;
372
373
  /* Read in program headers and parse notes.  */
374
1.31M
  for (i = 0; i < i_ehdr.e_phnum; ++i, ++i_phdr)
375
1.31M
    {
376
1.31M
      Elf_External_Phdr x_phdr;
377
378
1.31M
      if (bfd_bread (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
379
296
  goto fail;
380
1.31M
      elf_swap_phdr_in (abfd, &x_phdr, i_phdr);
381
382
1.31M
      if (i_phdr->p_type == PT_NOTE && i_phdr->p_filesz > 0)
383
2.98k
  {
384
2.98k
    elf_read_notes (abfd, offset + i_phdr->p_offset,
385
2.98k
        i_phdr->p_filesz, i_phdr->p_align);
386
387
    /* Make sure ABFD returns to processing the program headers.  */
388
2.98k
    if (bfd_seek (abfd, (file_ptr) (offset + i_ehdr.e_phoff
389
2.98k
            + (i + 1) * sizeof (x_phdr)),
390
2.98k
      SEEK_SET) != 0)
391
0
      goto fail;
392
393
2.98k
    if (abfd->build_id != NULL)
394
0
      return true;
395
2.98k
  }
396
1.31M
    }
397
398
  /* Having gotten this far, we have a valid ELF section, but no
399
     build-id was found.  */
400
1.82k
  goto fail;
401
402
8.89k
 wrong:
403
8.89k
  bfd_set_error (bfd_error_wrong_format);
404
11.9k
 fail:
405
11.9k
  return false;
406
8.89k
}
_bfd_elf64_core_find_build_id
Line
Count
Source
306
2.62k
{
307
2.62k
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.   */
308
2.62k
  Elf_Internal_Ehdr i_ehdr; /* Elf file header, internal form.   */
309
2.62k
  Elf_Internal_Phdr *i_phdr;
310
2.62k
  unsigned int i;
311
2.62k
  size_t amt;
312
313
  /* Seek to the position of the segment at OFFSET.  */
314
2.62k
  if (bfd_seek (abfd, offset, SEEK_SET) != 0)
315
903
    goto fail;
316
317
  /* Read in the ELF header in external format.  */
318
1.71k
  if (bfd_bread (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
319
325
    {
320
325
      if (bfd_get_error () != bfd_error_system_call)
321
325
  goto wrong;
322
0
      else
323
0
  goto fail;
324
325
    }
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.39k
  if (! elf_file_p (&x_ehdr)
332
1.39k
      || x_ehdr.e_ident[EI_VERSION] != EV_CURRENT
333
1.39k
      || x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
334
720
    goto wrong;
335
336
  /* Check that file's byte order matches xvec's.  */
337
674
  switch (x_ehdr.e_ident[EI_DATA])
338
674
    {
339
0
    case ELFDATA2MSB:   /* Big-endian.  */
340
0
      if (! bfd_header_big_endian (abfd))
341
0
  goto wrong;
342
0
      break;
343
673
    case ELFDATA2LSB:   /* Little-endian.  */
344
673
      if (! bfd_header_little_endian (abfd))
345
0
  goto wrong;
346
673
      break;
347
673
    case ELFDATANONE:   /* No data encoding specified.  */
348
1
    default:      /* Unknown data encoding specified . */
349
1
      goto wrong;
350
674
    }
351
352
673
  elf_swap_ehdr_in (abfd, &x_ehdr, &i_ehdr);
353
#if DEBUG
354
  elf_debug_file (&i_ehdr);
355
#endif
356
357
673
  if (i_ehdr.e_phentsize != sizeof (Elf_External_Phdr) || i_ehdr.e_phnum == 0)
358
6
    goto fail;
359
360
  /* Read in program headers.  */
361
667
  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
667
  i_phdr = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
367
667
  if (i_phdr == NULL)
368
0
    goto fail;
369
370
667
  if (bfd_seek (abfd, (file_ptr) (offset + i_ehdr.e_phoff), SEEK_SET) != 0)
371
0
    goto fail;
372
373
  /* Read in program headers and parse notes.  */
374
179k
  for (i = 0; i < i_ehdr.e_phnum; ++i, ++i_phdr)
375
178k
    {
376
178k
      Elf_External_Phdr x_phdr;
377
378
178k
      if (bfd_bread (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
379
71
  goto fail;
380
178k
      elf_swap_phdr_in (abfd, &x_phdr, i_phdr);
381
382
178k
      if (i_phdr->p_type == PT_NOTE && i_phdr->p_filesz > 0)
383
1.05k
  {
384
1.05k
    elf_read_notes (abfd, offset + i_phdr->p_offset,
385
1.05k
        i_phdr->p_filesz, i_phdr->p_align);
386
387
    /* Make sure ABFD returns to processing the program headers.  */
388
1.05k
    if (bfd_seek (abfd, (file_ptr) (offset + i_ehdr.e_phoff
389
1.05k
            + (i + 1) * sizeof (x_phdr)),
390
1.05k
      SEEK_SET) != 0)
391
0
      goto fail;
392
393
1.05k
    if (abfd->build_id != NULL)
394
0
      return true;
395
1.05k
  }
396
178k
    }
397
398
  /* Having gotten this far, we have a valid ELF section, but no
399
     build-id was found.  */
400
596
  goto fail;
401
402
1.04k
 wrong:
403
1.04k
  bfd_set_error (bfd_error_wrong_format);
404
2.62k
 fail:
405
2.62k
  return false;
406
1.04k
}
_bfd_elf32_core_find_build_id
Line
Count
Source
306
9.30k
{
307
9.30k
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.   */
308
9.30k
  Elf_Internal_Ehdr i_ehdr; /* Elf file header, internal form.   */
309
9.30k
  Elf_Internal_Phdr *i_phdr;
310
9.30k
  unsigned int i;
311
9.30k
  size_t amt;
312
313
  /* Seek to the position of the segment at OFFSET.  */
314
9.30k
  if (bfd_seek (abfd, offset, SEEK_SET) != 0)
315
0
    goto fail;
316
317
  /* Read in the ELF header in external format.  */
318
9.30k
  if (bfd_bread (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
319
3.61k
    {
320
3.61k
      if (bfd_get_error () != bfd_error_system_call)
321
3.61k
  goto wrong;
322
0
      else
323
0
  goto fail;
324
3.61k
    }
325
326
  /* Now check to see if we have a valid ELF file, and one that BFD can
327
     make use of.  The magic number must match, the address size ('class')
328
     and byte-swapping must match our XVEC entry, and it must have a
329
     section header table (FIXME: See comments re sections at top of this
330
     file).  */
331
5.68k
  if (! elf_file_p (&x_ehdr)
332
5.68k
      || x_ehdr.e_ident[EI_VERSION] != EV_CURRENT
333
5.68k
      || x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
334
4.23k
    goto wrong;
335
336
  /* Check that file's byte order matches xvec's.  */
337
1.45k
  switch (x_ehdr.e_ident[EI_DATA])
338
1.45k
    {
339
1.15k
    case ELFDATA2MSB:   /* Big-endian.  */
340
1.15k
      if (! bfd_header_big_endian (abfd))
341
0
  goto wrong;
342
1.15k
      break;
343
1.15k
    case ELFDATA2LSB:   /* Little-endian.  */
344
297
      if (! bfd_header_little_endian (abfd))
345
0
  goto wrong;
346
297
      break;
347
297
    case ELFDATANONE:   /* No data encoding specified.  */
348
0
    default:      /* Unknown data encoding specified . */
349
0
      goto wrong;
350
1.45k
    }
351
352
1.45k
  elf_swap_ehdr_in (abfd, &x_ehdr, &i_ehdr);
353
#if DEBUG
354
  elf_debug_file (&i_ehdr);
355
#endif
356
357
1.45k
  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.45k
  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.45k
  i_phdr = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
367
1.45k
  if (i_phdr == NULL)
368
0
    goto fail;
369
370
1.45k
  if (bfd_seek (abfd, (file_ptr) (offset + i_ehdr.e_phoff), SEEK_SET) != 0)
371
0
    goto fail;
372
373
  /* Read in program headers and parse notes.  */
374
1.13M
  for (i = 0; i < i_ehdr.e_phnum; ++i, ++i_phdr)
375
1.13M
    {
376
1.13M
      Elf_External_Phdr x_phdr;
377
378
1.13M
      if (bfd_bread (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
379
225
  goto fail;
380
1.13M
      elf_swap_phdr_in (abfd, &x_phdr, i_phdr);
381
382
1.13M
      if (i_phdr->p_type == PT_NOTE && i_phdr->p_filesz > 0)
383
1.93k
  {
384
1.93k
    elf_read_notes (abfd, offset + i_phdr->p_offset,
385
1.93k
        i_phdr->p_filesz, i_phdr->p_align);
386
387
    /* Make sure ABFD returns to processing the program headers.  */
388
1.93k
    if (bfd_seek (abfd, (file_ptr) (offset + i_ehdr.e_phoff
389
1.93k
            + (i + 1) * sizeof (x_phdr)),
390
1.93k
      SEEK_SET) != 0)
391
0
      goto fail;
392
393
1.93k
    if (abfd->build_id != NULL)
394
0
      return true;
395
1.93k
  }
396
1.13M
    }
397
398
  /* Having gotten this far, we have a valid ELF section, but no
399
     build-id was found.  */
400
1.22k
  goto fail;
401
402
7.85k
 wrong:
403
7.85k
  bfd_set_error (bfd_error_wrong_format);
404
9.30k
 fail:
405
9.30k
  return false;
406
7.85k
}