Coverage Report

Created: 2026-10-02 09:53

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/binutils-gdb/bfd/elfcore.h
Line
Count
Source
1
/* ELF core file support for BFD.
2
   Copyright (C) 1995-2026 Free Software Foundation, Inc.
3
4
   This file is part of BFD, the Binary File Descriptor library.
5
6
   This program is free software; you can redistribute it and/or modify
7
   it under the terms of the GNU General Public License as published by
8
   the Free Software Foundation; either version 3 of the License, or
9
   (at your option) any later version.
10
11
   This program is distributed in the hope that it will be useful,
12
   but WITHOUT ANY WARRANTY; without even the implied warranty of
13
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
   GNU General Public License for more details.
15
16
   You should have received a copy of the GNU General Public License
17
   along with this program; if not, write to the Free Software
18
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19
   MA 02110-1301, USA.  */
20
21
char*
22
elf_core_file_failing_command (bfd *abfd)
23
0
{
24
0
  return elf_tdata (abfd)->core->command;
25
0
}
Unexecuted instantiation: bfd_elf64_core_file_failing_command
Unexecuted instantiation: bfd_elf32_core_file_failing_command
26
27
int
28
elf_core_file_failing_signal (bfd *abfd)
29
0
{
30
0
  return elf_tdata (abfd)->core->signal;
31
0
}
Unexecuted instantiation: bfd_elf64_core_file_failing_signal
Unexecuted instantiation: bfd_elf32_core_file_failing_signal
32
33
int
34
elf_core_file_pid (bfd *abfd)
35
0
{
36
0
  return elf_tdata (abfd)->core->pid;
37
0
}
Unexecuted instantiation: bfd_elf64_core_file_pid
Unexecuted instantiation: bfd_elf32_core_file_pid
38
39
bool
40
elf_core_file_matches_executable_p (bfd *core_bfd, bfd *exec_bfd)
41
0
{
42
0
  char* corename;
43
44
  /* xvecs must match if both are ELF files for the same target.  */
45
46
0
  if (core_bfd->xvec != exec_bfd->xvec)
47
0
    {
48
0
      bfd_set_error (bfd_error_system_call);
49
0
      return false;
50
0
    }
51
52
  /* If both BFDs have identical build-ids, then they match.  */
53
0
  if (core_bfd->build_id != NULL
54
0
      && exec_bfd->build_id != NULL
55
0
      && core_bfd->build_id->size == exec_bfd->build_id->size
56
0
      && memcmp (core_bfd->build_id->data, exec_bfd->build_id->data,
57
0
     core_bfd->build_id->size) == 0)
58
0
    return true;
59
60
  /* See if the name in the corefile matches the executable name.  */
61
0
  corename = elf_tdata (core_bfd)->core->program;
62
0
  if (corename != NULL)
63
0
    {
64
0
      const char* execname = strrchr (bfd_get_filename (exec_bfd), '/');
65
66
0
      execname = execname ? execname + 1 : bfd_get_filename (exec_bfd);
67
68
0
      if (strcmp (execname, corename) != 0)
69
0
  return false;
70
0
    }
71
72
0
  return true;
73
0
}
Unexecuted instantiation: bfd_elf64_core_file_matches_executable_p
Unexecuted instantiation: bfd_elf32_core_file_matches_executable_p
74
75
/*  Core files are simply standard ELF formatted files that partition
76
    the file using the execution view of the file (program header table)
77
    rather than the linking view.  In fact, there is no section header
78
    table in a core file.
79
80
    The process status information (including the contents of the general
81
    register set) and the floating point register set are stored in a
82
    segment of type PT_NOTE.  We handcraft a couple of extra bfd sections
83
    that allow standard bfd access to the general registers (.reg) and the
84
    floating point registers (.reg2).  */
85
86
bfd_cleanup
87
elf_core_file_p (bfd *abfd)
88
3.15M
{
89
3.15M
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.  */
90
3.15M
  Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form.  */
91
3.15M
  Elf_Internal_Phdr *i_phdrp; /* Elf program header, internal form.  */
92
3.15M
  unsigned int phindex;
93
3.15M
  bfd_size_type amt;
94
3.15M
  ufile_ptr filesize;
95
96
  /* Read in the ELF header in external format.  */
97
3.15M
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
98
129k
    {
99
129k
      if (bfd_get_error () != bfd_error_system_call)
100
129k
  goto wrong;
101
344
      else
102
344
  goto fail;
103
129k
    }
104
105
  /* Check the magic number.  */
106
3.02M
  if (! elf_file_p (&x_ehdr))
107
428k
    goto wrong;
108
109
  /* FIXME: Check EI_VERSION here !  */
110
111
  /* Check the address size ("class").  */
112
2.59M
  if (x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
113
695k
    goto wrong;
114
115
  /* Check the byteorder.  */
116
1.89M
  switch (x_ehdr.e_ident[EI_DATA])
117
1.89M
    {
118
1.40M
    case ELFDATA2MSB:   /* Big-endian.  */
119
1.40M
      if (! bfd_big_endian (abfd))
120
698k
  goto wrong;
121
707k
      break;
122
707k
    case ELFDATA2LSB:   /* Little-endian.  */
123
490k
      if (! bfd_little_endian (abfd))
124
242k
  goto wrong;
125
247k
      break;
126
247k
    default:
127
792
      goto wrong;
128
1.89M
    }
129
130
  /* Give abfd an elf_obj_tdata.  */
131
955k
  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
955k
  i_ehdrp = elf_elfheader (abfd);
136
955k
  elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
137
138
#if DEBUG & 1
139
  elf_debug_file (i_ehdrp);
140
#endif
141
142
955k
  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
955k
  if (ebd->elf_machine_code != i_ehdrp->e_machine
148
921k
      && (ebd->elf_machine_alt1 == 0
149
279k
    || i_ehdrp->e_machine != ebd->elf_machine_alt1)
150
920k
      && (ebd->elf_machine_alt2 == 0
151
48.9k
    || i_ehdrp->e_machine != ebd->elf_machine_alt2)
152
920k
      && ebd->elf_machine_code != EM_NONE)
153
904k
    goto wrong;
154
155
50.2k
  if (ebd->osabi_exact && i_ehdrp->e_ident[EI_OSABI] != ebd->elf_osabi)
156
2.05k
    goto wrong;
157
158
  /* If there is no program header, or the type is not a core file, then
159
     we are hosed.  */
160
48.1k
  if (i_ehdrp->e_phoff == 0 || i_ehdrp->e_type != ET_CORE)
161
5.48k
    goto wrong;
162
163
  /* Does BFD's idea of the phdr size match the size
164
     recorded in the file? */
165
42.7k
  if (i_ehdrp->e_phentsize != sizeof (Elf_External_Phdr))
166
0
    goto wrong;
167
168
  /* If the program header count is PN_XNUM(0xffff), the actual
169
     count is in the first section header.  */
170
42.7k
  if (i_ehdrp->e_shoff != 0 && i_ehdrp->e_phnum == PN_XNUM)
171
103
    {
172
103
      Elf_External_Shdr x_shdr;
173
103
      Elf_Internal_Shdr i_shdr;
174
103
      file_ptr where = (file_ptr) i_ehdrp->e_shoff;
175
176
103
      if (i_ehdrp->e_shoff < sizeof (x_ehdr))
177
3
  goto wrong;
178
179
      /* Seek to the section header table in the file.  */
180
100
      if (bfd_seek (abfd, where, SEEK_SET) != 0)
181
3
  goto fail;
182
183
      /* Read the first section header at index 0, and convert to internal
184
   form.  */
185
97
      if (bfd_read (&x_shdr, sizeof (x_shdr), abfd) != sizeof (x_shdr))
186
4
  goto fail;
187
93
      elf_swap_shdr_in (abfd, &x_shdr, &i_shdr);
188
189
93
      if (i_shdr.sh_info != 0)
190
85
  {
191
85
    i_ehdrp->e_phnum = i_shdr.sh_info;
192
85
    if (i_ehdrp->e_phnum != i_shdr.sh_info)
193
0
      goto wrong;
194
85
  }
195
93
    }
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
42.7k
  if (i_ehdrp->e_phnum > 1)
200
42.1k
    {
201
42.1k
      Elf_External_Phdr x_phdr;
202
42.1k
      Elf_Internal_Phdr i_phdr;
203
42.1k
      file_ptr where;
204
205
      /* Check that we don't have a totally silly number of
206
   program headers.  */
207
42.1k
      if (i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (x_phdr)
208
42.1k
    || i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (i_phdr))
209
24
  goto wrong;
210
211
42.1k
      where = (file_ptr)(i_ehdrp->e_phoff + (i_ehdrp->e_phnum - 1) * sizeof (x_phdr));
212
42.1k
      if ((bfd_size_type) where <= i_ehdrp->e_phoff)
213
0
  goto wrong;
214
215
42.1k
      if (bfd_seek (abfd, where, SEEK_SET) != 0)
216
0
  goto fail;
217
42.1k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
218
21
  goto fail;
219
42.1k
    }
220
221
  /* Move to the start of the program headers.  */
222
42.6k
  if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0)
223
1
    goto wrong;
224
225
  /* Allocate space for the program headers.  */
226
42.6k
  amt = sizeof (*i_phdrp) * i_ehdrp->e_phnum;
227
42.6k
  i_phdrp = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
228
42.6k
  if (!i_phdrp)
229
0
    goto fail;
230
231
42.6k
  elf_tdata (abfd)->phdr = i_phdrp;
232
233
  /* Read and convert to internal form.  */
234
1.03M
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
235
995k
    {
236
995k
      Elf_External_Phdr x_phdr;
237
238
995k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
239
0
  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
42.6k
  if (! bfd_default_set_arch_mach (abfd, ebd->arch, 0)
248
      /* It's OK if this fails for the generic target.  */
249
11.7k
      && 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
42.6k
  if (ebd->elf_backend_object_p != NULL
258
29.1k
      && ! ebd->elf_backend_object_p (abfd))
259
5.28k
    goto wrong;
260
261
  /* Process each program header.  */
262
903k
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
263
866k
    if (! bfd_section_from_phdr (abfd, i_phdrp + phindex, (int) phindex))
264
849
      goto fail;
265
266
  /* Check for core truncation.  */
267
36.5k
  filesize = bfd_get_file_size (abfd);
268
36.5k
  if (filesize != 0)
269
36.5k
    {
270
49.8k
      for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
271
49.2k
  {
272
49.2k
    Elf_Internal_Phdr *p = i_phdrp + phindex;
273
49.2k
    if (p->p_filesz
274
46.0k
        && (p->p_offset >= filesize
275
18.3k
      || p->p_filesz > filesize - p->p_offset))
276
35.9k
      {
277
35.9k
        _bfd_error_handler (_("warning: %pB has a segment "
278
35.9k
            "extending past end of file"), abfd);
279
35.9k
        abfd->read_only = 1;
280
35.9k
        break;
281
35.9k
      }
282
49.2k
      }
283
36.5k
  }
284
285
  /* Save the entry point from the ELF header.  */
286
36.5k
  abfd->start_address = i_ehdrp->e_entry;
287
36.5k
  return _bfd_no_cleanup;
288
289
3.11M
 wrong:
290
3.11M
  bfd_set_error (bfd_error_wrong_format);
291
3.11M
 fail:
292
3.11M
  return NULL;
293
3.11M
}
bfd_elf64_core_file_p
Line
Count
Source
88
731k
{
89
731k
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.  */
90
731k
  Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form.  */
91
731k
  Elf_Internal_Phdr *i_phdrp; /* Elf program header, internal form.  */
92
731k
  unsigned int phindex;
93
731k
  bfd_size_type amt;
94
731k
  ufile_ptr filesize;
95
96
  /* Read in the ELF header in external format.  */
97
731k
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
98
122k
    {
99
122k
      if (bfd_get_error () != bfd_error_system_call)
100
122k
  goto wrong;
101
80
      else
102
80
  goto fail;
103
122k
    }
104
105
  /* Check the magic number.  */
106
609k
  if (! elf_file_p (&x_ehdr))
107
96.6k
    goto wrong;
108
109
  /* FIXME: Check EI_VERSION here !  */
110
111
  /* Check the address size ("class").  */
112
512k
  if (x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
113
431k
    goto wrong;
114
115
  /* Check the byteorder.  */
116
81.3k
  switch (x_ehdr.e_ident[EI_DATA])
117
81.3k
    {
118
4.06k
    case ELFDATA2MSB:   /* Big-endian.  */
119
4.06k
      if (! bfd_big_endian (abfd))
120
2.14k
  goto wrong;
121
1.91k
      break;
122
77.3k
    case ELFDATA2LSB:   /* Little-endian.  */
123
77.3k
      if (! bfd_little_endian (abfd))
124
36.1k
  goto wrong;
125
41.1k
      break;
126
41.1k
    default:
127
0
      goto wrong;
128
81.3k
    }
129
130
  /* Give abfd an elf_obj_tdata.  */
131
43.1k
  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
43.1k
  i_ehdrp = elf_elfheader (abfd);
136
43.1k
  elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
137
138
#if DEBUG & 1
139
  elf_debug_file (i_ehdrp);
140
#endif
141
142
43.1k
  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
43.1k
  if (ebd->elf_machine_code != i_ehdrp->e_machine
148
38.3k
      && (ebd->elf_machine_alt1 == 0
149
3.96k
    || i_ehdrp->e_machine != ebd->elf_machine_alt1)
150
38.3k
      && (ebd->elf_machine_alt2 == 0
151
3.53k
    || i_ehdrp->e_machine != ebd->elf_machine_alt2)
152
38.2k
      && ebd->elf_machine_code != EM_NONE)
153
36.3k
    goto wrong;
154
155
6.73k
  if (ebd->osabi_exact && i_ehdrp->e_ident[EI_OSABI] != ebd->elf_osabi)
156
810
    goto wrong;
157
158
  /* If there is no program header, or the type is not a core file, then
159
     we are hosed.  */
160
5.92k
  if (i_ehdrp->e_phoff == 0 || i_ehdrp->e_type != ET_CORE)
161
1.82k
    goto wrong;
162
163
  /* Does BFD's idea of the phdr size match the size
164
     recorded in the file? */
165
4.10k
  if (i_ehdrp->e_phentsize != sizeof (Elf_External_Phdr))
166
0
    goto wrong;
167
168
  /* If the program header count is PN_XNUM(0xffff), the actual
169
     count is in the first section header.  */
170
4.10k
  if (i_ehdrp->e_shoff != 0 && i_ehdrp->e_phnum == PN_XNUM)
171
49
    {
172
49
      Elf_External_Shdr x_shdr;
173
49
      Elf_Internal_Shdr i_shdr;
174
49
      file_ptr where = (file_ptr) i_ehdrp->e_shoff;
175
176
49
      if (i_ehdrp->e_shoff < sizeof (x_ehdr))
177
3
  goto wrong;
178
179
      /* Seek to the section header table in the file.  */
180
46
      if (bfd_seek (abfd, where, SEEK_SET) != 0)
181
3
  goto fail;
182
183
      /* Read the first section header at index 0, and convert to internal
184
   form.  */
185
43
      if (bfd_read (&x_shdr, sizeof (x_shdr), abfd) != sizeof (x_shdr))
186
4
  goto fail;
187
39
      elf_swap_shdr_in (abfd, &x_shdr, &i_shdr);
188
189
39
      if (i_shdr.sh_info != 0)
190
31
  {
191
31
    i_ehdrp->e_phnum = i_shdr.sh_info;
192
31
    if (i_ehdrp->e_phnum != i_shdr.sh_info)
193
0
      goto wrong;
194
31
  }
195
39
    }
196
197
  /* Sanity check that we can read all of the program headers.
198
     It ought to be good enough to just read the last one.  */
199
4.09k
  if (i_ehdrp->e_phnum > 1)
200
3.58k
    {
201
3.58k
      Elf_External_Phdr x_phdr;
202
3.58k
      Elf_Internal_Phdr i_phdr;
203
3.58k
      file_ptr where;
204
205
      /* Check that we don't have a totally silly number of
206
   program headers.  */
207
3.58k
      if (i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (x_phdr)
208
3.57k
    || i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (i_phdr))
209
20
  goto wrong;
210
211
3.56k
      where = (file_ptr)(i_ehdrp->e_phoff + (i_ehdrp->e_phnum - 1) * sizeof (x_phdr));
212
3.56k
      if ((bfd_size_type) where <= i_ehdrp->e_phoff)
213
0
  goto wrong;
214
215
3.56k
      if (bfd_seek (abfd, where, SEEK_SET) != 0)
216
0
  goto fail;
217
3.56k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
218
15
  goto fail;
219
3.56k
    }
220
221
  /* Move to the start of the program headers.  */
222
4.05k
  if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0)
223
1
    goto wrong;
224
225
  /* Allocate space for the program headers.  */
226
4.05k
  amt = sizeof (*i_phdrp) * i_ehdrp->e_phnum;
227
4.05k
  i_phdrp = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
228
4.05k
  if (!i_phdrp)
229
0
    goto fail;
230
231
4.05k
  elf_tdata (abfd)->phdr = i_phdrp;
232
233
  /* Read and convert to internal form.  */
234
117k
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
235
113k
    {
236
113k
      Elf_External_Phdr x_phdr;
237
238
113k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
239
0
  goto fail;
240
241
113k
      elf_swap_phdr_in (abfd, &x_phdr, i_phdrp + phindex);
242
113k
    }
243
244
  /* Set the machine architecture.  Do this before processing the
245
     program headers since we need to know the architecture type
246
     when processing the notes of some systems' core files.  */
247
4.05k
  if (! bfd_default_set_arch_mach (abfd, ebd->arch, 0)
248
      /* It's OK if this fails for the generic target.  */
249
1.18k
      && ebd->elf_machine_code != EM_NONE)
250
0
    goto fail;
251
252
  /* Let the backend double check the format and override global
253
     information.  We do this before processing the program headers
254
     to allow the correct machine (as opposed to just the default
255
     machine) to be set, making it possible for grok_prstatus and
256
     grok_psinfo to rely on the mach setting.  */
257
4.05k
  if (ebd->elf_backend_object_p != NULL
258
2.72k
      && ! ebd->elf_backend_object_p (abfd))
259
0
    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
672
      goto fail;
265
266
  /* Check for core truncation.  */
267
3.38k
  filesize = bfd_get_file_size (abfd);
268
3.38k
  if (filesize != 0)
269
3.38k
    {
270
6.82k
      for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
271
6.31k
  {
272
6.31k
    Elf_Internal_Phdr *p = i_phdrp + phindex;
273
6.31k
    if (p->p_filesz
274
5.34k
        && (p->p_offset >= filesize
275
3.24k
      || p->p_filesz > filesize - p->p_offset))
276
2.86k
      {
277
2.86k
        _bfd_error_handler (_("warning: %pB has a segment "
278
2.86k
            "extending past end of file"), abfd);
279
2.86k
        abfd->read_only = 1;
280
2.86k
        break;
281
2.86k
      }
282
6.31k
      }
283
3.38k
  }
284
285
  /* Save the entry point from the ELF header.  */
286
3.38k
  abfd->start_address = i_ehdrp->e_entry;
287
3.38k
  return _bfd_no_cleanup;
288
289
727k
 wrong:
290
727k
  bfd_set_error (bfd_error_wrong_format);
291
728k
 fail:
292
  return NULL;
293
727k
}
bfd_elf32_core_file_p
Line
Count
Source
88
2.41M
{
89
2.41M
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.  */
90
2.41M
  Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form.  */
91
2.41M
  Elf_Internal_Phdr *i_phdrp; /* Elf program header, internal form.  */
92
2.41M
  unsigned int phindex;
93
2.41M
  bfd_size_type amt;
94
2.41M
  ufile_ptr filesize;
95
96
  /* Read in the ELF header in external format.  */
97
2.41M
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
98
7.26k
    {
99
7.26k
      if (bfd_get_error () != bfd_error_system_call)
100
6.99k
  goto wrong;
101
264
      else
102
264
  goto fail;
103
7.26k
    }
104
105
  /* Check the magic number.  */
106
2.41M
  if (! elf_file_p (&x_ehdr))
107
331k
    goto wrong;
108
109
  /* FIXME: Check EI_VERSION here !  */
110
111
  /* Check the address size ("class").  */
112
2.08M
  if (x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
113
264k
    goto wrong;
114
115
  /* Check the byteorder.  */
116
1.81M
  switch (x_ehdr.e_ident[EI_DATA])
117
1.81M
    {
118
1.40M
    case ELFDATA2MSB:   /* Big-endian.  */
119
1.40M
      if (! bfd_big_endian (abfd))
120
696k
  goto wrong;
121
705k
      break;
122
705k
    case ELFDATA2LSB:   /* Little-endian.  */
123
412k
      if (! bfd_little_endian (abfd))
124
206k
  goto wrong;
125
206k
      break;
126
206k
    default:
127
792
      goto wrong;
128
1.81M
    }
129
130
  /* Give abfd an elf_obj_tdata.  */
131
912k
  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
912k
  i_ehdrp = elf_elfheader (abfd);
136
912k
  elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
137
138
#if DEBUG & 1
139
  elf_debug_file (i_ehdrp);
140
#endif
141
142
912k
  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
912k
  if (ebd->elf_machine_code != i_ehdrp->e_machine
148
883k
      && (ebd->elf_machine_alt1 == 0
149
275k
    || i_ehdrp->e_machine != ebd->elf_machine_alt1)
150
882k
      && (ebd->elf_machine_alt2 == 0
151
45.4k
    || i_ehdrp->e_machine != ebd->elf_machine_alt2)
152
881k
      && ebd->elf_machine_code != EM_NONE)
153
868k
    goto wrong;
154
155
43.5k
  if (ebd->osabi_exact && i_ehdrp->e_ident[EI_OSABI] != ebd->elf_osabi)
156
1.24k
    goto wrong;
157
158
  /* If there is no program header, or the type is not a core file, then
159
     we are hosed.  */
160
42.2k
  if (i_ehdrp->e_phoff == 0 || i_ehdrp->e_type != ET_CORE)
161
3.66k
    goto wrong;
162
163
  /* Does BFD's idea of the phdr size match the size
164
     recorded in the file? */
165
38.6k
  if (i_ehdrp->e_phentsize != sizeof (Elf_External_Phdr))
166
0
    goto wrong;
167
168
  /* If the program header count is PN_XNUM(0xffff), the actual
169
     count is in the first section header.  */
170
38.6k
  if (i_ehdrp->e_shoff != 0 && i_ehdrp->e_phnum == PN_XNUM)
171
54
    {
172
54
      Elf_External_Shdr x_shdr;
173
54
      Elf_Internal_Shdr i_shdr;
174
54
      file_ptr where = (file_ptr) i_ehdrp->e_shoff;
175
176
54
      if (i_ehdrp->e_shoff < sizeof (x_ehdr))
177
0
  goto wrong;
178
179
      /* Seek to the section header table in the file.  */
180
54
      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
54
      if (bfd_read (&x_shdr, sizeof (x_shdr), abfd) != sizeof (x_shdr))
186
0
  goto fail;
187
54
      elf_swap_shdr_in (abfd, &x_shdr, &i_shdr);
188
189
54
      if (i_shdr.sh_info != 0)
190
54
  {
191
54
    i_ehdrp->e_phnum = i_shdr.sh_info;
192
54
    if (i_ehdrp->e_phnum != i_shdr.sh_info)
193
0
      goto wrong;
194
54
  }
195
54
    }
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
38.6k
  if (i_ehdrp->e_phnum > 1)
200
38.6k
    {
201
38.6k
      Elf_External_Phdr x_phdr;
202
38.6k
      Elf_Internal_Phdr i_phdr;
203
38.6k
      file_ptr where;
204
205
      /* Check that we don't have a totally silly number of
206
   program headers.  */
207
38.6k
      if (i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (x_phdr)
208
38.6k
    || i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (i_phdr))
209
4
  goto wrong;
210
211
38.6k
      where = (file_ptr)(i_ehdrp->e_phoff + (i_ehdrp->e_phnum - 1) * sizeof (x_phdr));
212
38.6k
      if ((bfd_size_type) where <= i_ehdrp->e_phoff)
213
0
  goto wrong;
214
215
38.6k
      if (bfd_seek (abfd, where, SEEK_SET) != 0)
216
0
  goto fail;
217
38.6k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
218
6
  goto fail;
219
38.6k
    }
220
221
  /* Move to the start of the program headers.  */
222
38.6k
  if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0)
223
0
    goto wrong;
224
225
  /* Allocate space for the program headers.  */
226
38.6k
  amt = sizeof (*i_phdrp) * i_ehdrp->e_phnum;
227
38.6k
  i_phdrp = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
228
38.6k
  if (!i_phdrp)
229
0
    goto fail;
230
231
38.6k
  elf_tdata (abfd)->phdr = i_phdrp;
232
233
  /* Read and convert to internal form.  */
234
920k
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
235
882k
    {
236
882k
      Elf_External_Phdr x_phdr;
237
238
882k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
239
0
  goto fail;
240
241
882k
      elf_swap_phdr_in (abfd, &x_phdr, i_phdrp + phindex);
242
882k
    }
243
244
  /* Set the machine architecture.  Do this before processing the
245
     program headers since we need to know the architecture type
246
     when processing the notes of some systems' core files.  */
247
38.6k
  if (! bfd_default_set_arch_mach (abfd, ebd->arch, 0)
248
      /* It's OK if this fails for the generic target.  */
249
10.5k
      && 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
38.6k
  if (ebd->elf_backend_object_p != NULL
258
26.4k
      && ! ebd->elf_backend_object_p (abfd))
259
5.28k
    goto wrong;
260
261
  /* Process each program header.  */
262
793k
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
263
760k
    if (! bfd_section_from_phdr (abfd, i_phdrp + phindex, (int) phindex))
264
177
      goto fail;
265
266
  /* Check for core truncation.  */
267
33.1k
  filesize = bfd_get_file_size (abfd);
268
33.1k
  if (filesize != 0)
269
33.1k
    {
270
43.0k
      for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
271
42.9k
  {
272
42.9k
    Elf_Internal_Phdr *p = i_phdrp + phindex;
273
42.9k
    if (p->p_filesz
274
40.7k
        && (p->p_offset >= filesize
275
15.0k
      || p->p_filesz > filesize - p->p_offset))
276
33.0k
      {
277
33.0k
        _bfd_error_handler (_("warning: %pB has a segment "
278
33.0k
            "extending past end of file"), abfd);
279
33.0k
        abfd->read_only = 1;
280
33.0k
        break;
281
33.0k
      }
282
42.9k
      }
283
33.1k
  }
284
285
  /* Save the entry point from the ELF header.  */
286
33.1k
  abfd->start_address = i_ehdrp->e_entry;
287
33.1k
  return _bfd_no_cleanup;
288
289
2.38M
 wrong:
290
2.38M
  bfd_set_error (bfd_error_wrong_format);
291
2.38M
 fail:
292
  return NULL;
293
2.38M
}
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
12.2k
{
304
12.2k
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.   */
305
12.2k
  Elf_Internal_Ehdr i_ehdr; /* Elf file header, internal form.   */
306
12.2k
  Elf_Internal_Phdr *i_phdr;
307
12.2k
  unsigned int i;
308
12.2k
  size_t amt;
309
310
  /* Seek to the position of the segment at OFFSET.  */
311
12.2k
  if (bfd_seek (abfd, offset, SEEK_SET) != 0)
312
1.10k
    goto fail;
313
314
  /* Read in the ELF header in external format.  */
315
11.1k
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
316
3.51k
    {
317
3.51k
      if (bfd_get_error () != bfd_error_system_call)
318
3.51k
  goto wrong;
319
0
      else
320
0
  goto fail;
321
3.51k
    }
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
7.65k
  if (! elf_file_p (&x_ehdr)
329
3.84k
      || x_ehdr.e_ident[EI_VERSION] != EV_CURRENT
330
3.38k
      || x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
331
4.29k
    goto wrong;
332
333
  /* Check that file's byte order matches xvec's.  */
334
3.36k
  switch (x_ehdr.e_ident[EI_DATA])
335
3.36k
    {
336
835
    case ELFDATA2MSB:   /* Big-endian.  */
337
835
      if (! bfd_header_big_endian (abfd))
338
21
  goto wrong;
339
814
      break;
340
2.49k
    case ELFDATA2LSB:   /* Little-endian.  */
341
2.49k
      if (! bfd_header_little_endian (abfd))
342
0
  goto wrong;
343
2.49k
      break;
344
2.49k
    case ELFDATANONE:   /* No data encoding specified.  */
345
39
    default:      /* Unknown data encoding specified . */
346
39
      goto wrong;
347
3.36k
    }
348
349
3.30k
  elf_swap_ehdr_in (abfd, &x_ehdr, &i_ehdr);
350
#if DEBUG & 1
351
  elf_debug_file (&i_ehdr);
352
#endif
353
354
3.30k
  if (i_ehdr.e_phentsize != sizeof (Elf_External_Phdr) || i_ehdr.e_phnum == 0)
355
55
    goto fail;
356
357
  /* Read in program headers.  */
358
3.25k
  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.25k
  i_phdr = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
364
3.25k
  if (i_phdr == NULL)
365
0
    goto fail;
366
367
3.25k
  if (bfd_seek (abfd, offset + i_ehdr.e_phoff, SEEK_SET) != 0)
368
18
    goto fail;
369
370
  /* Read in program headers and parse notes.  */
371
400k
  for (i = 0; i < i_ehdr.e_phnum; ++i, ++i_phdr)
372
397k
    {
373
397k
      Elf_External_Phdr x_phdr;
374
375
397k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
376
272
  goto fail;
377
397k
      elf_swap_phdr_in (abfd, &x_phdr, i_phdr);
378
379
397k
      if (i_phdr->p_type == PT_NOTE && i_phdr->p_filesz > 0)
380
3.33k
  {
381
3.33k
    _bfd_elf_read_notes (abfd, offset + i_phdr->p_offset,
382
3.33k
             i_phdr->p_filesz, i_phdr->p_align);
383
384
    /* Make sure ABFD returns to processing the program headers.  */
385
3.33k
    if (bfd_seek (abfd,
386
3.33k
      offset + i_ehdr.e_phoff + (i + 1) * sizeof (x_phdr),
387
3.33k
      SEEK_SET) != 0)
388
0
      goto fail;
389
390
3.33k
    if (abfd->build_id != NULL)
391
8
      return true;
392
3.33k
  }
393
397k
    }
394
395
  /* Having gotten this far, we have a valid ELF section, but no
396
     build-id was found.  */
397
2.95k
  goto fail;
398
399
7.87k
 wrong:
400
7.87k
  bfd_set_error (bfd_error_wrong_format);
401
12.2k
 fail:
402
  return false;
403
7.87k
}
_bfd_elf64_core_find_build_id
Line
Count
Source
303
6.20k
{
304
6.20k
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.   */
305
6.20k
  Elf_Internal_Ehdr i_ehdr; /* Elf file header, internal form.   */
306
6.20k
  Elf_Internal_Phdr *i_phdr;
307
6.20k
  unsigned int i;
308
6.20k
  size_t amt;
309
310
  /* Seek to the position of the segment at OFFSET.  */
311
6.20k
  if (bfd_seek (abfd, offset, SEEK_SET) != 0)
312
1.10k
    goto fail;
313
314
  /* Read in the ELF header in external format.  */
315
5.09k
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
316
1.59k
    {
317
1.59k
      if (bfd_get_error () != bfd_error_system_call)
318
1.59k
  goto wrong;
319
0
      else
320
0
  goto fail;
321
1.59k
    }
322
323
  /* Now check to see if we have a valid ELF file, and one that BFD can
324
     make use of.  The magic number must match, the address size ('class')
325
     and byte-swapping must match our XVEC entry, and it must have a
326
     section header table (FIXME: See comments re sections at top of this
327
     file).  */
328
3.50k
  if (! elf_file_p (&x_ehdr)
329
2.65k
      || x_ehdr.e_ident[EI_VERSION] != EV_CURRENT
330
2.21k
      || x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
331
1.29k
    goto wrong;
332
333
  /* Check that file's byte order matches xvec's.  */
334
2.20k
  switch (x_ehdr.e_ident[EI_DATA])
335
2.20k
    {
336
0
    case ELFDATA2MSB:   /* Big-endian.  */
337
0
      if (! bfd_header_big_endian (abfd))
338
0
  goto wrong;
339
0
      break;
340
2.18k
    case ELFDATA2LSB:   /* Little-endian.  */
341
2.18k
      if (! bfd_header_little_endian (abfd))
342
0
  goto wrong;
343
2.18k
      break;
344
2.18k
    case ELFDATANONE:   /* No data encoding specified.  */
345
25
    default:      /* Unknown data encoding specified . */
346
25
      goto wrong;
347
2.20k
    }
348
349
2.18k
  elf_swap_ehdr_in (abfd, &x_ehdr, &i_ehdr);
350
#if DEBUG & 1
351
  elf_debug_file (&i_ehdr);
352
#endif
353
354
2.18k
  if (i_ehdr.e_phentsize != sizeof (Elf_External_Phdr) || i_ehdr.e_phnum == 0)
355
35
    goto fail;
356
357
  /* Read in program headers.  */
358
2.14k
  if (_bfd_mul_overflow (i_ehdr.e_phnum, sizeof (*i_phdr), &amt))
359
0
    {
360
0
      bfd_set_error (bfd_error_file_too_big);
361
0
      goto fail;
362
0
    }
363
2.14k
  i_phdr = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
364
2.14k
  if (i_phdr == NULL)
365
0
    goto fail;
366
367
2.14k
  if (bfd_seek (abfd, offset + i_ehdr.e_phoff, SEEK_SET) != 0)
368
18
    goto fail;
369
370
  /* Read in program headers and parse notes.  */
371
223k
  for (i = 0; i < i_ehdr.e_phnum; ++i, ++i_phdr)
372
221k
    {
373
221k
      Elf_External_Phdr x_phdr;
374
375
221k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
376
26
  goto fail;
377
221k
      elf_swap_phdr_in (abfd, &x_phdr, i_phdr);
378
379
221k
      if (i_phdr->p_type == PT_NOTE && i_phdr->p_filesz > 0)
380
2.61k
  {
381
2.61k
    _bfd_elf_read_notes (abfd, offset + i_phdr->p_offset,
382
2.61k
             i_phdr->p_filesz, i_phdr->p_align);
383
384
    /* Make sure ABFD returns to processing the program headers.  */
385
2.61k
    if (bfd_seek (abfd,
386
2.61k
      offset + i_ehdr.e_phoff + (i + 1) * sizeof (x_phdr),
387
2.61k
      SEEK_SET) != 0)
388
0
      goto fail;
389
390
2.61k
    if (abfd->build_id != NULL)
391
8
      return true;
392
2.61k
  }
393
221k
    }
394
395
  /* Having gotten this far, we have a valid ELF section, but no
396
     build-id was found.  */
397
2.09k
  goto fail;
398
399
2.91k
 wrong:
400
2.91k
  bfd_set_error (bfd_error_wrong_format);
401
6.19k
 fail:
402
  return false;
403
2.91k
}
_bfd_elf32_core_find_build_id
Line
Count
Source
303
6.07k
{
304
6.07k
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.   */
305
6.07k
  Elf_Internal_Ehdr i_ehdr; /* Elf file header, internal form.   */
306
6.07k
  Elf_Internal_Phdr *i_phdr;
307
6.07k
  unsigned int i;
308
6.07k
  size_t amt;
309
310
  /* Seek to the position of the segment at OFFSET.  */
311
6.07k
  if (bfd_seek (abfd, offset, SEEK_SET) != 0)
312
0
    goto fail;
313
314
  /* Read in the ELF header in external format.  */
315
6.07k
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
316
1.92k
    {
317
1.92k
      if (bfd_get_error () != bfd_error_system_call)
318
1.92k
  goto wrong;
319
0
      else
320
0
  goto fail;
321
1.92k
    }
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.15k
  if (! elf_file_p (&x_ehdr)
329
1.19k
      || x_ehdr.e_ident[EI_VERSION] != EV_CURRENT
330
1.17k
      || x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
331
3.00k
    goto wrong;
332
333
  /* Check that file's byte order matches xvec's.  */
334
1.15k
  switch (x_ehdr.e_ident[EI_DATA])
335
1.15k
    {
336
835
    case ELFDATA2MSB:   /* Big-endian.  */
337
835
      if (! bfd_header_big_endian (abfd))
338
21
  goto wrong;
339
814
      break;
340
814
    case ELFDATA2LSB:   /* Little-endian.  */
341
307
      if (! bfd_header_little_endian (abfd))
342
0
  goto wrong;
343
307
      break;
344
307
    case ELFDATANONE:   /* No data encoding specified.  */
345
14
    default:      /* Unknown data encoding specified . */
346
14
      goto wrong;
347
1.15k
    }
348
349
1.12k
  elf_swap_ehdr_in (abfd, &x_ehdr, &i_ehdr);
350
#if DEBUG & 1
351
  elf_debug_file (&i_ehdr);
352
#endif
353
354
1.12k
  if (i_ehdr.e_phentsize != sizeof (Elf_External_Phdr) || i_ehdr.e_phnum == 0)
355
20
    goto fail;
356
357
  /* Read in program headers.  */
358
1.10k
  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.10k
  i_phdr = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
364
1.10k
  if (i_phdr == NULL)
365
0
    goto fail;
366
367
1.10k
  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
177k
  for (i = 0; i < i_ehdr.e_phnum; ++i, ++i_phdr)
372
176k
    {
373
176k
      Elf_External_Phdr x_phdr;
374
375
176k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
376
246
  goto fail;
377
176k
      elf_swap_phdr_in (abfd, &x_phdr, i_phdr);
378
379
176k
      if (i_phdr->p_type == PT_NOTE && i_phdr->p_filesz > 0)
380
725
  {
381
725
    _bfd_elf_read_notes (abfd, offset + i_phdr->p_offset,
382
725
             i_phdr->p_filesz, i_phdr->p_align);
383
384
    /* Make sure ABFD returns to processing the program headers.  */
385
725
    if (bfd_seek (abfd,
386
725
      offset + i_ehdr.e_phoff + (i + 1) * sizeof (x_phdr),
387
725
      SEEK_SET) != 0)
388
0
      goto fail;
389
390
725
    if (abfd->build_id != NULL)
391
0
      return true;
392
725
  }
393
176k
    }
394
395
  /* Having gotten this far, we have a valid ELF section, but no
396
     build-id was found.  */
397
855
  goto fail;
398
399
4.95k
 wrong:
400
4.95k
  bfd_set_error (bfd_error_wrong_format);
401
6.07k
 fail:
402
  return false;
403
4.95k
}