Coverage Report

Created: 2026-11-21 09:00

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
4.84M
{
89
4.84M
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.  */
90
4.84M
  Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form.  */
91
4.84M
  Elf_Internal_Phdr *i_phdrp; /* Elf program header, internal form.  */
92
4.84M
  unsigned int phindex;
93
4.84M
  bfd_size_type amt;
94
4.84M
  ufile_ptr filesize;
95
96
  /* Read in the ELF header in external format.  */
97
4.84M
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
98
766k
    {
99
766k
      if (bfd_get_error () != bfd_error_system_call)
100
766k
  goto wrong;
101
0
      else
102
0
  goto fail;
103
766k
    }
104
105
  /* Check the magic number.  */
106
4.07M
  if (! elf_file_p (&x_ehdr))
107
1.16M
    goto wrong;
108
109
  /* FIXME: Check EI_VERSION here !  */
110
111
  /* Check the address size ("class").  */
112
2.90M
  if (x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
113
842k
    goto wrong;
114
115
  /* Check the byteorder.  */
116
2.06M
  switch (x_ehdr.e_ident[EI_DATA])
117
2.06M
    {
118
1.50M
    case ELFDATA2MSB:   /* Big-endian.  */
119
1.50M
      if (! bfd_big_endian (abfd))
120
749k
  goto wrong;
121
758k
      break;
122
758k
    case ELFDATA2LSB:   /* Little-endian.  */
123
558k
      if (! bfd_little_endian (abfd))
124
275k
  goto wrong;
125
282k
      break;
126
282k
    default:
127
344
      goto wrong;
128
2.06M
    }
129
130
  /* Give abfd an elf_obj_tdata.  */
131
1.04M
  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
1.04M
  i_ehdrp = elf_elfheader (abfd);
136
1.04M
  elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
137
138
#if DEBUG & 1
139
  elf_debug_file (i_ehdrp);
140
#endif
141
142
1.04M
  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
1.04M
  if (ebd->elf_machine_code != i_ehdrp->e_machine
148
1.00M
      && (ebd->elf_machine_alt1 == 0
149
302k
    || i_ehdrp->e_machine != ebd->elf_machine_alt1)
150
1.00M
      && (ebd->elf_machine_alt2 == 0
151
53.5k
    || i_ehdrp->e_machine != ebd->elf_machine_alt2)
152
1.00M
      && ebd->elf_machine_code != EM_NONE)
153
984k
    goto wrong;
154
155
56.6k
  if (ebd->osabi_exact && i_ehdrp->e_ident[EI_OSABI] != ebd->elf_osabi)
156
2.51k
    goto wrong;
157
158
  /* If there is no program header, or the type is not a core file, then
159
     we are hosed.  */
160
54.1k
  if (i_ehdrp->e_phoff == 0 || i_ehdrp->e_type != ET_CORE)
161
7.10k
    goto wrong;
162
163
  /* Does BFD's idea of the phdr size match the size
164
     recorded in the file? */
165
47.0k
  if (i_ehdrp->e_phentsize != sizeof (Elf_External_Phdr))
166
47
    goto wrong;
167
168
  /* If the program header count is PN_XNUM(0xffff), the actual
169
     count is in the first section header.  */
170
47.0k
  if (i_ehdrp->e_shoff != 0 && i_ehdrp->e_phnum == PN_XNUM)
171
291
    {
172
291
      Elf_External_Shdr x_shdr;
173
291
      Elf_Internal_Shdr i_shdr;
174
291
      file_ptr where = (file_ptr) i_ehdrp->e_shoff;
175
176
291
      if (i_ehdrp->e_shoff < sizeof (x_ehdr))
177
30
  goto wrong;
178
179
      /* Seek to the section header table in the file.  */
180
261
      if (bfd_seek (abfd, where, SEEK_SET) != 0)
181
25
  goto fail;
182
183
      /* Read the first section header at index 0, and convert to internal
184
   form.  */
185
236
      if (bfd_read (&x_shdr, sizeof (x_shdr), abfd) != sizeof (x_shdr))
186
34
  goto fail;
187
202
      elf_swap_shdr_in (abfd, &x_shdr, &i_shdr);
188
189
202
      if (i_shdr.sh_info != 0)
190
176
  {
191
176
    i_ehdrp->e_phnum = i_shdr.sh_info;
192
176
    if (i_ehdrp->e_phnum != i_shdr.sh_info)
193
0
      goto wrong;
194
176
  }
195
202
    }
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
46.9k
  if (i_ehdrp->e_phnum > 1)
200
45.0k
    {
201
45.0k
      Elf_External_Phdr x_phdr;
202
45.0k
      Elf_Internal_Phdr i_phdr;
203
45.0k
      file_ptr where;
204
205
      /* Check that we don't have a totally silly number of
206
   program headers.  */
207
45.0k
      if (i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (x_phdr)
208
44.9k
    || i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (i_phdr))
209
122
  goto wrong;
210
211
44.9k
      where = (file_ptr)(i_ehdrp->e_phoff + (i_ehdrp->e_phnum - 1) * sizeof (x_phdr));
212
44.9k
      if ((bfd_size_type) where <= i_ehdrp->e_phoff)
213
14
  goto wrong;
214
215
44.9k
      if (bfd_seek (abfd, where, SEEK_SET) != 0)
216
52
  goto fail;
217
44.8k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
218
149
  goto fail;
219
44.8k
    }
220
221
  /* Move to the start of the program headers.  */
222
46.5k
  if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0)
223
10
    goto wrong;
224
225
  /* Allocate space for the program headers.  */
226
46.5k
  amt = sizeof (*i_phdrp) * i_ehdrp->e_phnum;
227
46.5k
  i_phdrp = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
228
46.5k
  if (!i_phdrp)
229
0
    goto fail;
230
231
46.5k
  elf_tdata (abfd)->phdr = i_phdrp;
232
233
  /* Read and convert to internal form.  */
234
1.04M
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
235
995k
    {
236
995k
      Elf_External_Phdr x_phdr;
237
238
995k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
239
31
  goto fail;
240
241
995k
      elf_swap_phdr_in (abfd, &x_phdr, i_phdrp + phindex);
242
995k
    }
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
46.5k
  if (! bfd_default_set_arch_mach (abfd, ebd->arch, 0)
248
      /* It's OK if this fails for the generic target.  */
249
12.9k
      && ebd->elf_machine_code != EM_NONE)
250
2
    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
46.5k
  if (ebd->elf_backend_object_p != NULL
258
31.8k
      && ! ebd->elf_backend_object_p (abfd))
259
5.60k
    goto wrong;
260
261
  /* Process each program header.  */
262
917k
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
263
877k
    if (! bfd_section_from_phdr (abfd, i_phdrp + phindex, (int) phindex))
264
1.41k
      goto fail;
265
266
  /* Check for core truncation.  */
267
39.5k
  filesize = bfd_get_file_size (abfd);
268
39.5k
  if (filesize != 0)
269
39.5k
    {
270
55.5k
      for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
271
53.9k
  {
272
53.9k
    Elf_Internal_Phdr *p = i_phdrp + phindex;
273
53.9k
    if (p->p_filesz
274
48.5k
        && (p->p_offset >= filesize
275
20.1k
      || p->p_filesz > filesize - p->p_offset))
276
37.9k
      {
277
37.9k
        _bfd_error_handler (_("warning: %pB has a segment "
278
37.9k
            "extending past end of file"), abfd);
279
37.9k
        abfd->read_only = 1;
280
37.9k
        break;
281
37.9k
      }
282
53.9k
      }
283
39.5k
  }
284
285
  /* Save the entry point from the ELF header.  */
286
39.5k
  abfd->start_address = i_ehdrp->e_entry;
287
39.5k
  return _bfd_no_cleanup;
288
289
4.80M
 wrong:
290
4.80M
  bfd_set_error (bfd_error_wrong_format);
291
4.80M
 fail:
292
4.80M
  return NULL;
293
4.80M
}
bfd_elf64_core_file_p
Line
Count
Source
88
1.12M
{
89
1.12M
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.  */
90
1.12M
  Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form.  */
91
1.12M
  Elf_Internal_Phdr *i_phdrp; /* Elf program header, internal form.  */
92
1.12M
  unsigned int phindex;
93
1.12M
  bfd_size_type amt;
94
1.12M
  ufile_ptr filesize;
95
96
  /* Read in the ELF header in external format.  */
97
1.12M
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
98
288k
    {
99
288k
      if (bfd_get_error () != bfd_error_system_call)
100
288k
  goto wrong;
101
0
      else
102
0
  goto fail;
103
288k
    }
104
105
  /* Check the magic number.  */
106
837k
  if (! elf_file_p (&x_ehdr))
107
246k
    goto wrong;
108
109
  /* FIXME: Check EI_VERSION here !  */
110
111
  /* Check the address size ("class").  */
112
591k
  if (x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
113
480k
    goto wrong;
114
115
  /* Check the byteorder.  */
116
111k
  switch (x_ehdr.e_ident[EI_DATA])
117
111k
    {
118
11.5k
    case ELFDATA2MSB:   /* Big-endian.  */
119
11.5k
      if (! bfd_big_endian (abfd))
120
6.06k
  goto wrong;
121
5.47k
      break;
122
99.6k
    case ELFDATA2LSB:   /* Little-endian.  */
123
99.6k
      if (! bfd_little_endian (abfd))
124
46.5k
  goto wrong;
125
53.1k
      break;
126
53.1k
    default:
127
80
      goto wrong;
128
111k
    }
129
130
  /* Give abfd an elf_obj_tdata.  */
131
58.5k
  if (! (*abfd->xvec->_bfd_set_format[bfd_core]) (abfd))
132
0
    goto fail;
133
134
  /* Swap in the rest of the header, now that we have the byte order.  */
135
58.5k
  i_ehdrp = elf_elfheader (abfd);
136
58.5k
  elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
137
138
#if DEBUG & 1
139
  elf_debug_file (i_ehdrp);
140
#endif
141
142
58.5k
  elf_backend_data *ebd = get_elf_backend_data (abfd);
143
144
  /* Check that the ELF e_machine field matches what this particular
145
     BFD format expects.  */
146
147
58.5k
  if (ebd->elf_machine_code != i_ehdrp->e_machine
148
52.7k
      && (ebd->elf_machine_alt1 == 0
149
6.08k
    || i_ehdrp->e_machine != ebd->elf_machine_alt1)
150
52.6k
      && (ebd->elf_machine_alt2 == 0
151
4.90k
    || i_ehdrp->e_machine != ebd->elf_machine_alt2)
152
52.5k
      && ebd->elf_machine_code != EM_NONE)
153
50.0k
    goto wrong;
154
155
8.57k
  if (ebd->osabi_exact && i_ehdrp->e_ident[EI_OSABI] != ebd->elf_osabi)
156
951
    goto wrong;
157
158
  /* If there is no program header, or the type is not a core file, then
159
     we are hosed.  */
160
7.62k
  if (i_ehdrp->e_phoff == 0 || i_ehdrp->e_type != ET_CORE)
161
2.28k
    goto wrong;
162
163
  /* Does BFD's idea of the phdr size match the size
164
     recorded in the file? */
165
5.34k
  if (i_ehdrp->e_phentsize != sizeof (Elf_External_Phdr))
166
12
    goto wrong;
167
168
  /* If the program header count is PN_XNUM(0xffff), the actual
169
     count is in the first section header.  */
170
5.33k
  if (i_ehdrp->e_shoff != 0 && i_ehdrp->e_phnum == PN_XNUM)
171
126
    {
172
126
      Elf_External_Shdr x_shdr;
173
126
      Elf_Internal_Shdr i_shdr;
174
126
      file_ptr where = (file_ptr) i_ehdrp->e_shoff;
175
176
126
      if (i_ehdrp->e_shoff < sizeof (x_ehdr))
177
10
  goto wrong;
178
179
      /* Seek to the section header table in the file.  */
180
116
      if (bfd_seek (abfd, where, SEEK_SET) != 0)
181
25
  goto fail;
182
183
      /* Read the first section header at index 0, and convert to internal
184
   form.  */
185
91
      if (bfd_read (&x_shdr, sizeof (x_shdr), abfd) != sizeof (x_shdr))
186
14
  goto fail;
187
77
      elf_swap_shdr_in (abfd, &x_shdr, &i_shdr);
188
189
77
      if (i_shdr.sh_info != 0)
190
63
  {
191
63
    i_ehdrp->e_phnum = i_shdr.sh_info;
192
63
    if (i_ehdrp->e_phnum != i_shdr.sh_info)
193
0
      goto wrong;
194
63
  }
195
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
5.28k
  if (i_ehdrp->e_phnum > 1)
200
4.29k
    {
201
4.29k
      Elf_External_Phdr x_phdr;
202
4.29k
      Elf_Internal_Phdr i_phdr;
203
4.29k
      file_ptr where;
204
205
      /* Check that we don't have a totally silly number of
206
   program headers.  */
207
4.29k
      if (i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (x_phdr)
208
4.25k
    || i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (i_phdr))
209
58
  goto wrong;
210
211
4.24k
      where = (file_ptr)(i_ehdrp->e_phoff + (i_ehdrp->e_phnum - 1) * sizeof (x_phdr));
212
4.24k
      if ((bfd_size_type) where <= i_ehdrp->e_phoff)
213
14
  goto wrong;
214
215
4.22k
      if (bfd_seek (abfd, where, SEEK_SET) != 0)
216
52
  goto fail;
217
4.17k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
218
60
  goto fail;
219
4.17k
    }
220
221
  /* Move to the start of the program headers.  */
222
5.09k
  if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0)
223
10
    goto wrong;
224
225
  /* Allocate space for the program headers.  */
226
5.08k
  amt = sizeof (*i_phdrp) * i_ehdrp->e_phnum;
227
5.08k
  i_phdrp = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
228
5.08k
  if (!i_phdrp)
229
0
    goto fail;
230
231
5.08k
  elf_tdata (abfd)->phdr = i_phdrp;
232
233
  /* Read and convert to internal form.  */
234
115k
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
235
110k
    {
236
110k
      Elf_External_Phdr x_phdr;
237
238
110k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
239
10
  goto fail;
240
241
110k
      elf_swap_phdr_in (abfd, &x_phdr, i_phdrp + phindex);
242
110k
    }
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
5.07k
  if (! bfd_default_set_arch_mach (abfd, ebd->arch, 0)
248
      /* It's OK if this fails for the generic target.  */
249
1.47k
      && 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
5.07k
  if (ebd->elf_backend_object_p != NULL
258
3.45k
      && ! ebd->elf_backend_object_p (abfd))
259
3
    goto wrong;
260
261
  /* Process each program header.  */
262
109k
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
263
105k
    if (! bfd_section_from_phdr (abfd, i_phdrp + phindex, (int) phindex))
264
798
      goto fail;
265
266
  /* Check for core truncation.  */
267
4.27k
  filesize = bfd_get_file_size (abfd);
268
4.27k
  if (filesize != 0)
269
4.27k
    {
270
8.55k
      for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
271
7.88k
  {
272
7.88k
    Elf_Internal_Phdr *p = i_phdrp + phindex;
273
7.88k
    if (p->p_filesz
274
6.54k
        && (p->p_offset >= filesize
275
4.00k
      || p->p_filesz > filesize - p->p_offset))
276
3.60k
      {
277
3.60k
        _bfd_error_handler (_("warning: %pB has a segment "
278
3.60k
            "extending past end of file"), abfd);
279
3.60k
        abfd->read_only = 1;
280
3.60k
        break;
281
3.60k
      }
282
7.88k
      }
283
4.27k
  }
284
285
  /* Save the entry point from the ELF header.  */
286
4.27k
  abfd->start_address = i_ehdrp->e_entry;
287
4.27k
  return _bfd_no_cleanup;
288
289
1.12M
 wrong:
290
1.12M
  bfd_set_error (bfd_error_wrong_format);
291
1.12M
 fail:
292
  return NULL;
293
1.12M
}
bfd_elf32_core_file_p
Line
Count
Source
88
3.71M
{
89
3.71M
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.  */
90
3.71M
  Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form.  */
91
3.71M
  Elf_Internal_Phdr *i_phdrp; /* Elf program header, internal form.  */
92
3.71M
  unsigned int phindex;
93
3.71M
  bfd_size_type amt;
94
3.71M
  ufile_ptr filesize;
95
96
  /* Read in the ELF header in external format.  */
97
3.71M
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
98
478k
    {
99
478k
      if (bfd_get_error () != bfd_error_system_call)
100
478k
  goto wrong;
101
0
      else
102
0
  goto fail;
103
478k
    }
104
105
  /* Check the magic number.  */
106
3.24M
  if (! elf_file_p (&x_ehdr))
107
921k
    goto wrong;
108
109
  /* FIXME: Check EI_VERSION here !  */
110
111
  /* Check the address size ("class").  */
112
2.31M
  if (x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
113
362k
    goto wrong;
114
115
  /* Check the byteorder.  */
116
1.95M
  switch (x_ehdr.e_ident[EI_DATA])
117
1.95M
    {
118
1.49M
    case ELFDATA2MSB:   /* Big-endian.  */
119
1.49M
      if (! bfd_big_endian (abfd))
120
743k
  goto wrong;
121
753k
      break;
122
753k
    case ELFDATA2LSB:   /* Little-endian.  */
123
458k
      if (! bfd_little_endian (abfd))
124
229k
  goto wrong;
125
229k
      break;
126
229k
    default:
127
264
      goto wrong;
128
1.95M
    }
129
130
  /* Give abfd an elf_obj_tdata.  */
131
982k
  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
982k
  i_ehdrp = elf_elfheader (abfd);
136
982k
  elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
137
138
#if DEBUG & 1
139
  elf_debug_file (i_ehdrp);
140
#endif
141
142
982k
  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
982k
  if (ebd->elf_machine_code != i_ehdrp->e_machine
148
950k
      && (ebd->elf_machine_alt1 == 0
149
296k
    || i_ehdrp->e_machine != ebd->elf_machine_alt1)
150
950k
      && (ebd->elf_machine_alt2 == 0
151
48.6k
    || i_ehdrp->e_machine != ebd->elf_machine_alt2)
152
949k
      && ebd->elf_machine_code != EM_NONE)
153
934k
    goto wrong;
154
155
48.1k
  if (ebd->osabi_exact && i_ehdrp->e_ident[EI_OSABI] != ebd->elf_osabi)
156
1.56k
    goto wrong;
157
158
  /* If there is no program header, or the type is not a core file, then
159
     we are hosed.  */
160
46.5k
  if (i_ehdrp->e_phoff == 0 || i_ehdrp->e_type != ET_CORE)
161
4.82k
    goto wrong;
162
163
  /* Does BFD's idea of the phdr size match the size
164
     recorded in the file? */
165
41.7k
  if (i_ehdrp->e_phentsize != sizeof (Elf_External_Phdr))
166
35
    goto wrong;
167
168
  /* If the program header count is PN_XNUM(0xffff), the actual
169
     count is in the first section header.  */
170
41.6k
  if (i_ehdrp->e_shoff != 0 && i_ehdrp->e_phnum == PN_XNUM)
171
165
    {
172
165
      Elf_External_Shdr x_shdr;
173
165
      Elf_Internal_Shdr i_shdr;
174
165
      file_ptr where = (file_ptr) i_ehdrp->e_shoff;
175
176
165
      if (i_ehdrp->e_shoff < sizeof (x_ehdr))
177
20
  goto wrong;
178
179
      /* Seek to the section header table in the file.  */
180
145
      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
145
      if (bfd_read (&x_shdr, sizeof (x_shdr), abfd) != sizeof (x_shdr))
186
20
  goto fail;
187
125
      elf_swap_shdr_in (abfd, &x_shdr, &i_shdr);
188
189
125
      if (i_shdr.sh_info != 0)
190
113
  {
191
113
    i_ehdrp->e_phnum = i_shdr.sh_info;
192
113
    if (i_ehdrp->e_phnum != i_shdr.sh_info)
193
0
      goto wrong;
194
113
  }
195
125
    }
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
41.6k
  if (i_ehdrp->e_phnum > 1)
200
40.7k
    {
201
40.7k
      Elf_External_Phdr x_phdr;
202
40.7k
      Elf_Internal_Phdr i_phdr;
203
40.7k
      file_ptr where;
204
205
      /* Check that we don't have a totally silly number of
206
   program headers.  */
207
40.7k
      if (i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (x_phdr)
208
40.6k
    || i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (i_phdr))
209
64
  goto wrong;
210
211
40.6k
      where = (file_ptr)(i_ehdrp->e_phoff + (i_ehdrp->e_phnum - 1) * sizeof (x_phdr));
212
40.6k
      if ((bfd_size_type) where <= i_ehdrp->e_phoff)
213
0
  goto wrong;
214
215
40.6k
      if (bfd_seek (abfd, where, SEEK_SET) != 0)
216
0
  goto fail;
217
40.6k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
218
89
  goto fail;
219
40.6k
    }
220
221
  /* Move to the start of the program headers.  */
222
41.4k
  if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0)
223
0
    goto wrong;
224
225
  /* Allocate space for the program headers.  */
226
41.4k
  amt = sizeof (*i_phdrp) * i_ehdrp->e_phnum;
227
41.4k
  i_phdrp = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
228
41.4k
  if (!i_phdrp)
229
0
    goto fail;
230
231
41.4k
  elf_tdata (abfd)->phdr = i_phdrp;
232
233
  /* Read and convert to internal form.  */
234
926k
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
235
884k
    {
236
884k
      Elf_External_Phdr x_phdr;
237
238
884k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
239
21
  goto fail;
240
241
884k
      elf_swap_phdr_in (abfd, &x_phdr, i_phdrp + phindex);
242
884k
    }
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
41.4k
  if (! bfd_default_set_arch_mach (abfd, ebd->arch, 0)
248
      /* It's OK if this fails for the generic target.  */
249
11.4k
      && ebd->elf_machine_code != EM_NONE)
250
2
    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
41.4k
  if (ebd->elf_backend_object_p != NULL
258
28.3k
      && ! ebd->elf_backend_object_p (abfd))
259
5.60k
    goto wrong;
260
261
  /* Process each program header.  */
262
807k
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
263
771k
    if (! bfd_section_from_phdr (abfd, i_phdrp + phindex, (int) phindex))
264
616
      goto fail;
265
266
  /* Check for core truncation.  */
267
35.2k
  filesize = bfd_get_file_size (abfd);
268
35.2k
  if (filesize != 0)
269
35.2k
    {
270
46.9k
      for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
271
46.0k
  {
272
46.0k
    Elf_Internal_Phdr *p = i_phdrp + phindex;
273
46.0k
    if (p->p_filesz
274
42.0k
        && (p->p_offset >= filesize
275
16.1k
      || p->p_filesz > filesize - p->p_offset))
276
34.3k
      {
277
34.3k
        _bfd_error_handler (_("warning: %pB has a segment "
278
34.3k
            "extending past end of file"), abfd);
279
34.3k
        abfd->read_only = 1;
280
34.3k
        break;
281
34.3k
      }
282
46.0k
      }
283
35.2k
  }
284
285
  /* Save the entry point from the ELF header.  */
286
35.2k
  abfd->start_address = i_ehdrp->e_entry;
287
35.2k
  return _bfd_no_cleanup;
288
289
3.68M
 wrong:
290
3.68M
  bfd_set_error (bfd_error_wrong_format);
291
3.68M
 fail:
292
  return NULL;
293
3.68M
}
294
295
/* Attempt to find a build-id in a core file from the core file BFD.
296
   OFFSET is the file offset to a PT_LOAD segment that may contain
297
   the build-id note.  Returns TRUE upon success, FALSE otherwise.  */
298
299
bool
300
NAME(_bfd_elf, core_find_build_id)
301
  (bfd *abfd,
302
   bfd_vma offset)
303
13.9k
{
304
13.9k
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.   */
305
13.9k
  Elf_Internal_Ehdr i_ehdr; /* Elf file header, internal form.   */
306
13.9k
  Elf_Internal_Phdr *i_phdr;
307
13.9k
  unsigned int i;
308
13.9k
  size_t amt;
309
310
  /* Seek to the position of the segment at OFFSET.  */
311
13.9k
  if (bfd_seek (abfd, offset, SEEK_SET) != 0)
312
1.23k
    goto fail;
313
314
  /* Read in the ELF header in external format.  */
315
12.7k
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
316
3.78k
    {
317
3.78k
      if (bfd_get_error () != bfd_error_system_call)
318
3.78k
  goto wrong;
319
0
      else
320
0
  goto fail;
321
3.78k
    }
322
323
  /* Now check to see if we have a valid ELF file, and one that BFD can
324
     make use of.  The magic number must match, the address size ('class')
325
     and byte-swapping must match our XVEC entry, and it must have a
326
     section header table (FIXME: See comments re sections at top of this
327
     file).  */
328
8.95k
  if (! elf_file_p (&x_ehdr)
329
5.24k
      || x_ehdr.e_ident[EI_VERSION] != EV_CURRENT
330
4.60k
      || x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
331
4.38k
    goto wrong;
332
333
  /* Check that file's byte order matches xvec's.  */
334
4.56k
  switch (x_ehdr.e_ident[EI_DATA])
335
4.56k
    {
336
1.13k
    case ELFDATA2MSB:   /* Big-endian.  */
337
1.13k
      if (! bfd_header_big_endian (abfd))
338
21
  goto wrong;
339
1.11k
      break;
340
3.39k
    case ELFDATA2LSB:   /* Little-endian.  */
341
3.39k
      if (! bfd_header_little_endian (abfd))
342
0
  goto wrong;
343
3.39k
      break;
344
3.39k
    case ELFDATANONE:   /* No data encoding specified.  */
345
36
    default:      /* Unknown data encoding specified . */
346
36
      goto wrong;
347
4.56k
    }
348
349
4.51k
  elf_swap_ehdr_in (abfd, &x_ehdr, &i_ehdr);
350
#if DEBUG & 1
351
  elf_debug_file (&i_ehdr);
352
#endif
353
354
4.51k
  if (i_ehdr.e_phentsize != sizeof (Elf_External_Phdr) || i_ehdr.e_phnum == 0)
355
66
    goto fail;
356
357
  /* Read in program headers.  */
358
4.44k
  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
4.44k
  i_phdr = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
364
4.44k
  if (i_phdr == NULL)
365
0
    goto fail;
366
367
4.44k
  if (bfd_seek (abfd, offset + i_ehdr.e_phoff, SEEK_SET) != 0)
368
11
    goto fail;
369
370
  /* Read in program headers and parse notes.  */
371
510k
  for (i = 0; i < i_ehdr.e_phnum; ++i, ++i_phdr)
372
506k
    {
373
506k
      Elf_External_Phdr x_phdr;
374
375
506k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
376
231
  goto fail;
377
506k
      elf_swap_phdr_in (abfd, &x_phdr, i_phdr);
378
379
506k
      if (i_phdr->p_type == PT_NOTE && i_phdr->p_filesz > 0)
380
3.59k
  {
381
3.59k
    _bfd_elf_read_notes (abfd, offset + i_phdr->p_offset,
382
3.59k
             i_phdr->p_filesz, i_phdr->p_align);
383
384
    /* Make sure ABFD returns to processing the program headers.  */
385
3.59k
    if (bfd_seek (abfd,
386
3.59k
      offset + i_ehdr.e_phoff + (i + 1) * sizeof (x_phdr),
387
3.59k
      SEEK_SET) != 0)
388
0
      goto fail;
389
390
3.59k
    if (abfd->build_id != NULL)
391
22
      return true;
392
3.59k
  }
393
506k
    }
394
395
  /* Having gotten this far, we have a valid ELF section, but no
396
     build-id was found.  */
397
4.18k
  goto fail;
398
399
8.22k
 wrong:
400
8.22k
  bfd_set_error (bfd_error_wrong_format);
401
13.9k
 fail:
402
  return false;
403
8.22k
}
_bfd_elf64_core_find_build_id
Line
Count
Source
303
7.27k
{
304
7.27k
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.   */
305
7.27k
  Elf_Internal_Ehdr i_ehdr; /* Elf file header, internal form.   */
306
7.27k
  Elf_Internal_Phdr *i_phdr;
307
7.27k
  unsigned int i;
308
7.27k
  size_t amt;
309
310
  /* Seek to the position of the segment at OFFSET.  */
311
7.27k
  if (bfd_seek (abfd, offset, SEEK_SET) != 0)
312
1.23k
    goto fail;
313
314
  /* Read in the ELF header in external format.  */
315
6.03k
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
316
1.60k
    {
317
1.60k
      if (bfd_get_error () != bfd_error_system_call)
318
1.60k
  goto wrong;
319
0
      else
320
0
  goto fail;
321
1.60k
    }
322
323
  /* Now check to see if we have a valid ELF file, and one that BFD can
324
     make use of.  The magic number must match, the address size ('class')
325
     and byte-swapping must match our XVEC entry, and it must have a
326
     section header table (FIXME: See comments re sections at top of this
327
     file).  */
328
4.43k
  if (! elf_file_p (&x_ehdr)
329
3.60k
      || x_ehdr.e_ident[EI_VERSION] != EV_CURRENT
330
3.11k
      || x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
331
1.33k
    goto wrong;
332
333
  /* Check that file's byte order matches xvec's.  */
334
3.10k
  switch (x_ehdr.e_ident[EI_DATA])
335
3.10k
    {
336
18
    case ELFDATA2MSB:   /* Big-endian.  */
337
18
      if (! bfd_header_big_endian (abfd))
338
0
  goto wrong;
339
18
      break;
340
3.06k
    case ELFDATA2LSB:   /* Little-endian.  */
341
3.06k
      if (! bfd_header_little_endian (abfd))
342
0
  goto wrong;
343
3.06k
      break;
344
3.06k
    case ELFDATANONE:   /* No data encoding specified.  */
345
17
    default:      /* Unknown data encoding specified . */
346
17
      goto wrong;
347
3.10k
    }
348
349
3.08k
  elf_swap_ehdr_in (abfd, &x_ehdr, &i_ehdr);
350
#if DEBUG & 1
351
  elf_debug_file (&i_ehdr);
352
#endif
353
354
3.08k
  if (i_ehdr.e_phentsize != sizeof (Elf_External_Phdr) || i_ehdr.e_phnum == 0)
355
23
    goto fail;
356
357
  /* Read in program headers.  */
358
3.06k
  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.06k
  i_phdr = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
364
3.06k
  if (i_phdr == NULL)
365
0
    goto fail;
366
367
3.06k
  if (bfd_seek (abfd, offset + i_ehdr.e_phoff, SEEK_SET) != 0)
368
11
    goto fail;
369
370
  /* Read in program headers and parse notes.  */
371
339k
  for (i = 0; i < i_ehdr.e_phnum; ++i, ++i_phdr)
372
336k
    {
373
336k
      Elf_External_Phdr x_phdr;
374
375
336k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
376
19
  goto fail;
377
336k
      elf_swap_phdr_in (abfd, &x_phdr, i_phdr);
378
379
336k
      if (i_phdr->p_type == PT_NOTE && i_phdr->p_filesz > 0)
380
2.74k
  {
381
2.74k
    _bfd_elf_read_notes (abfd, offset + i_phdr->p_offset,
382
2.74k
             i_phdr->p_filesz, i_phdr->p_align);
383
384
    /* Make sure ABFD returns to processing the program headers.  */
385
2.74k
    if (bfd_seek (abfd,
386
2.74k
      offset + i_ehdr.e_phoff + (i + 1) * sizeof (x_phdr),
387
2.74k
      SEEK_SET) != 0)
388
0
      goto fail;
389
390
2.74k
    if (abfd->build_id != NULL)
391
18
      return true;
392
2.74k
  }
393
336k
    }
394
395
  /* Having gotten this far, we have a valid ELF section, but no
396
     build-id was found.  */
397
3.01k
  goto fail;
398
399
3.01k
 wrong:
400
2.95k
  bfd_set_error (bfd_error_wrong_format);
401
7.25k
 fail:
402
  return false;
403
2.95k
}
_bfd_elf32_core_find_build_id
Line
Count
Source
303
6.69k
{
304
6.69k
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.   */
305
6.69k
  Elf_Internal_Ehdr i_ehdr; /* Elf file header, internal form.   */
306
6.69k
  Elf_Internal_Phdr *i_phdr;
307
6.69k
  unsigned int i;
308
6.69k
  size_t amt;
309
310
  /* Seek to the position of the segment at OFFSET.  */
311
6.69k
  if (bfd_seek (abfd, offset, SEEK_SET) != 0)
312
0
    goto fail;
313
314
  /* Read in the ELF header in external format.  */
315
6.69k
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
316
2.17k
    {
317
2.17k
      if (bfd_get_error () != bfd_error_system_call)
318
2.17k
  goto wrong;
319
0
      else
320
0
  goto fail;
321
2.17k
    }
322
323
  /* Now check to see if we have a valid ELF file, and one that BFD can
324
     make use of.  The magic number must match, the address size ('class')
325
     and byte-swapping must match our XVEC entry, and it must have a
326
     section header table (FIXME: See comments re sections at top of this
327
     file).  */
328
4.51k
  if (! elf_file_p (&x_ehdr)
329
1.64k
      || x_ehdr.e_ident[EI_VERSION] != EV_CURRENT
330
1.48k
      || x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
331
3.05k
    goto wrong;
332
333
  /* Check that file's byte order matches xvec's.  */
334
1.46k
  switch (x_ehdr.e_ident[EI_DATA])
335
1.46k
    {
336
1.11k
    case ELFDATA2MSB:   /* Big-endian.  */
337
1.11k
      if (! bfd_header_big_endian (abfd))
338
21
  goto wrong;
339
1.09k
      break;
340
1.09k
    case ELFDATA2LSB:   /* Little-endian.  */
341
329
      if (! bfd_header_little_endian (abfd))
342
0
  goto wrong;
343
329
      break;
344
329
    case ELFDATANONE:   /* No data encoding specified.  */
345
19
    default:      /* Unknown data encoding specified . */
346
19
      goto wrong;
347
1.46k
    }
348
349
1.42k
  elf_swap_ehdr_in (abfd, &x_ehdr, &i_ehdr);
350
#if DEBUG & 1
351
  elf_debug_file (&i_ehdr);
352
#endif
353
354
1.42k
  if (i_ehdr.e_phentsize != sizeof (Elf_External_Phdr) || i_ehdr.e_phnum == 0)
355
43
    goto fail;
356
357
  /* Read in program headers.  */
358
1.38k
  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.38k
  i_phdr = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
364
1.38k
  if (i_phdr == NULL)
365
0
    goto fail;
366
367
1.38k
  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
171k
  for (i = 0; i < i_ehdr.e_phnum; ++i, ++i_phdr)
372
169k
    {
373
169k
      Elf_External_Phdr x_phdr;
374
375
169k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
376
212
  goto fail;
377
169k
      elf_swap_phdr_in (abfd, &x_phdr, i_phdr);
378
379
169k
      if (i_phdr->p_type == PT_NOTE && i_phdr->p_filesz > 0)
380
845
  {
381
845
    _bfd_elf_read_notes (abfd, offset + i_phdr->p_offset,
382
845
             i_phdr->p_filesz, i_phdr->p_align);
383
384
    /* Make sure ABFD returns to processing the program headers.  */
385
845
    if (bfd_seek (abfd,
386
845
      offset + i_ehdr.e_phoff + (i + 1) * sizeof (x_phdr),
387
845
      SEEK_SET) != 0)
388
0
      goto fail;
389
390
845
    if (abfd->build_id != NULL)
391
4
      return true;
392
845
  }
393
169k
    }
394
395
  /* Having gotten this far, we have a valid ELF section, but no
396
     build-id was found.  */
397
1.16k
  goto fail;
398
399
5.27k
 wrong:
400
5.27k
  bfd_set_error (bfd_error_wrong_format);
401
6.69k
 fail:
402
  return false;
403
5.27k
}