Coverage Report

Created: 2026-03-10 08:46

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.60M
{
89
3.60M
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.  */
90
3.60M
  Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form.  */
91
3.60M
  Elf_Internal_Phdr *i_phdrp; /* Elf program header, internal form.  */
92
3.60M
  unsigned int phindex;
93
3.60M
  bfd_size_type amt;
94
3.60M
  ufile_ptr filesize;
95
96
  /* Read in the ELF header in external format.  */
97
3.60M
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
98
693k
    {
99
693k
      if (bfd_get_error () != bfd_error_system_call)
100
693k
  goto wrong;
101
0
      else
102
0
  goto fail;
103
693k
    }
104
105
  /* Check the magic number.  */
106
2.90M
  if (! elf_file_p (&x_ehdr))
107
1.08M
    goto wrong;
108
109
  /* FIXME: Check EI_VERSION here !  */
110
111
  /* Check the address size ("class").  */
112
1.82M
  if (x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
113
540k
    goto wrong;
114
115
  /* Check the byteorder.  */
116
1.28M
  switch (x_ehdr.e_ident[EI_DATA])
117
1.28M
    {
118
797k
    case ELFDATA2MSB:   /* Big-endian.  */
119
797k
      if (! bfd_big_endian (abfd))
120
396k
  goto wrong;
121
400k
      break;
122
482k
    case ELFDATA2LSB:   /* Little-endian.  */
123
482k
      if (! bfd_little_endian (abfd))
124
238k
  goto wrong;
125
243k
      break;
126
243k
    default:
127
436
      goto wrong;
128
1.28M
    }
129
130
  /* Give abfd an elf_obj_tdata.  */
131
644k
  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
644k
  i_ehdrp = elf_elfheader (abfd);
136
644k
  elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
137
138
#if DEBUG & 1
139
  elf_debug_file (i_ehdrp);
140
#endif
141
142
644k
  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
644k
  if (ebd->elf_machine_code != i_ehdrp->e_machine
148
622k
      && (ebd->elf_machine_alt1 == 0
149
182k
    || i_ehdrp->e_machine != ebd->elf_machine_alt1)
150
621k
      && (ebd->elf_machine_alt2 == 0
151
30.6k
    || i_ehdrp->e_machine != ebd->elf_machine_alt2)
152
621k
      && ebd->elf_machine_code != EM_NONE)
153
610k
    goto wrong;
154
155
34.2k
  if (ebd->osabi_exact && i_ehdrp->e_ident[EI_OSABI] != ebd->elf_osabi)
156
2.00k
    goto wrong;
157
158
  /* If there is no program header, or the type is not a core file, then
159
     we are hosed.  */
160
32.2k
  if (i_ehdrp->e_phoff == 0 || i_ehdrp->e_type != ET_CORE)
161
5.79k
    goto wrong;
162
163
  /* Does BFD's idea of the phdr size match the size
164
     recorded in the file? */
165
26.4k
  if (i_ehdrp->e_phentsize != sizeof (Elf_External_Phdr))
166
32
    goto wrong;
167
168
  /* If the program header count is PN_XNUM(0xffff), the actual
169
     count is in the first section header.  */
170
26.4k
  if (i_ehdrp->e_shoff != 0 && i_ehdrp->e_phnum == PN_XNUM)
171
209
    {
172
209
      Elf_External_Shdr x_shdr;
173
209
      Elf_Internal_Shdr i_shdr;
174
209
      file_ptr where = (file_ptr) i_ehdrp->e_shoff;
175
176
209
      if (i_ehdrp->e_shoff < sizeof (x_ehdr))
177
16
  goto wrong;
178
179
      /* Seek to the section header table in the file.  */
180
193
      if (bfd_seek (abfd, where, SEEK_SET) != 0)
181
13
  goto fail;
182
183
      /* Read the first section header at index 0, and convert to internal
184
   form.  */
185
180
      if (bfd_read (&x_shdr, sizeof (x_shdr), abfd) != sizeof (x_shdr))
186
24
  goto fail;
187
156
      elf_swap_shdr_in (abfd, &x_shdr, &i_shdr);
188
189
156
      if (i_shdr.sh_info != 0)
190
131
  {
191
131
    i_ehdrp->e_phnum = i_shdr.sh_info;
192
131
    if (i_ehdrp->e_phnum != i_shdr.sh_info)
193
0
      goto wrong;
194
131
  }
195
156
    }
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
26.3k
  if (i_ehdrp->e_phnum > 1)
200
24.8k
    {
201
24.8k
      Elf_External_Phdr x_phdr;
202
24.8k
      Elf_Internal_Phdr i_phdr;
203
24.8k
      file_ptr where;
204
205
      /* Check that we don't have a totally silly number of
206
   program headers.  */
207
24.8k
      if (i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (x_phdr)
208
24.8k
    || i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (i_phdr))
209
47
  goto wrong;
210
211
24.7k
      where = (file_ptr)(i_ehdrp->e_phoff + (i_ehdrp->e_phnum - 1) * sizeof (x_phdr));
212
24.7k
      if ((bfd_size_type) where <= i_ehdrp->e_phoff)
213
9
  goto wrong;
214
215
24.7k
      if (bfd_seek (abfd, where, SEEK_SET) != 0)
216
29
  goto fail;
217
24.7k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
218
144
  goto fail;
219
24.7k
    }
220
221
  /* Move to the start of the program headers.  */
222
26.1k
  if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0)
223
22
    goto wrong;
224
225
  /* Allocate space for the program headers.  */
226
26.1k
  amt = sizeof (*i_phdrp) * i_ehdrp->e_phnum;
227
26.1k
  i_phdrp = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
228
26.1k
  if (!i_phdrp)
229
0
    goto fail;
230
231
26.1k
  elf_tdata (abfd)->phdr = i_phdrp;
232
233
  /* Read and convert to internal form.  */
234
732k
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
235
706k
    {
236
706k
      Elf_External_Phdr x_phdr;
237
238
706k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
239
28
  goto fail;
240
241
706k
      elf_swap_phdr_in (abfd, &x_phdr, i_phdrp + phindex);
242
706k
    }
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
26.1k
  if (! bfd_default_set_arch_mach (abfd, ebd->arch, 0)
248
      /* It's OK if this fails for the generic target.  */
249
7.22k
      && ebd->elf_machine_code != EM_NONE)
250
1
    goto fail;
251
252
  /* Let the backend double check the format and override global
253
     information.  We do this before processing the program headers
254
     to allow the correct machine (as opposed to just the default
255
     machine) to be set, making it possible for grok_prstatus and
256
     grok_psinfo to rely on the mach setting.  */
257
26.1k
  if (ebd->elf_backend_object_p != NULL
258
18.0k
      && ! ebd->elf_backend_object_p (abfd))
259
3.12k
    goto wrong;
260
261
  /* Process each program header.  */
262
633k
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
263
611k
    if (! bfd_section_from_phdr (abfd, i_phdrp + phindex, (int) phindex))
264
1.25k
      goto fail;
265
266
  /* Check for core truncation.  */
267
21.7k
  filesize = bfd_get_file_size (abfd);
268
21.7k
  if (filesize != 0)
269
21.7k
    {
270
31.1k
      for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
271
30.2k
  {
272
30.2k
    Elf_Internal_Phdr *p = i_phdrp + phindex;
273
30.2k
    if (p->p_filesz
274
26.2k
        && (p->p_offset >= filesize
275
9.87k
      || p->p_filesz > filesize - p->p_offset))
276
20.8k
      {
277
20.8k
        _bfd_error_handler (_("warning: %pB has a segment "
278
20.8k
            "extending past end of file"), abfd);
279
20.8k
        abfd->read_only = 1;
280
20.8k
        break;
281
20.8k
      }
282
30.2k
      }
283
21.7k
  }
284
285
  /* Save the entry point from the ELF header.  */
286
21.7k
  abfd->start_address = i_ehdrp->e_entry;
287
21.7k
  return _bfd_no_cleanup;
288
289
3.57M
 wrong:
290
3.57M
  bfd_set_error (bfd_error_wrong_format);
291
3.58M
 fail:
292
3.58M
  return NULL;
293
3.57M
}
bfd_elf64_core_file_p
Line
Count
Source
88
837k
{
89
837k
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.  */
90
837k
  Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form.  */
91
837k
  Elf_Internal_Phdr *i_phdrp; /* Elf program header, internal form.  */
92
837k
  unsigned int phindex;
93
837k
  bfd_size_type amt;
94
837k
  ufile_ptr filesize;
95
96
  /* Read in the ELF header in external format.  */
97
837k
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
98
260k
    {
99
260k
      if (bfd_get_error () != bfd_error_system_call)
100
260k
  goto wrong;
101
0
      else
102
0
  goto fail;
103
260k
    }
104
105
  /* Check the magic number.  */
106
576k
  if (! elf_file_p (&x_ehdr))
107
231k
    goto wrong;
108
109
  /* FIXME: Check EI_VERSION here !  */
110
111
  /* Check the address size ("class").  */
112
344k
  if (x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
113
258k
    goto wrong;
114
115
  /* Check the byteorder.  */
116
86.3k
  switch (x_ehdr.e_ident[EI_DATA])
117
86.3k
    {
118
8.49k
    case ELFDATA2MSB:   /* Big-endian.  */
119
8.49k
      if (! bfd_big_endian (abfd))
120
4.45k
  goto wrong;
121
4.04k
      break;
122
77.8k
    case ELFDATA2LSB:   /* Little-endian.  */
123
77.8k
      if (! bfd_little_endian (abfd))
124
36.5k
  goto wrong;
125
41.2k
      break;
126
41.2k
    default:
127
40
      goto wrong;
128
86.3k
    }
129
130
  /* Give abfd an elf_obj_tdata.  */
131
45.3k
  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
45.3k
  i_ehdrp = elf_elfheader (abfd);
136
45.3k
  elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
137
138
#if DEBUG & 1
139
  elf_debug_file (i_ehdrp);
140
#endif
141
142
45.3k
  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
45.3k
  if (ebd->elf_machine_code != i_ehdrp->e_machine
148
40.7k
      && (ebd->elf_machine_alt1 == 0
149
4.67k
    || i_ehdrp->e_machine != ebd->elf_machine_alt1)
150
40.6k
      && (ebd->elf_machine_alt2 == 0
151
3.88k
    || i_ehdrp->e_machine != ebd->elf_machine_alt2)
152
40.5k
      && ebd->elf_machine_code != EM_NONE)
153
38.5k
    goto wrong;
154
155
6.77k
  if (ebd->osabi_exact && i_ehdrp->e_ident[EI_OSABI] != ebd->elf_osabi)
156
960
    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.81k
  if (i_ehdrp->e_phoff == 0 || i_ehdrp->e_type != ET_CORE)
161
2.11k
    goto wrong;
162
163
  /* Does BFD's idea of the phdr size match the size
164
     recorded in the file? */
165
3.70k
  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
3.69k
  if (i_ehdrp->e_shoff != 0 && i_ehdrp->e_phnum == PN_XNUM)
171
101
    {
172
101
      Elf_External_Shdr x_shdr;
173
101
      Elf_Internal_Shdr i_shdr;
174
101
      file_ptr where = (file_ptr) i_ehdrp->e_shoff;
175
176
101
      if (i_ehdrp->e_shoff < sizeof (x_ehdr))
177
6
  goto wrong;
178
179
      /* Seek to the section header table in the file.  */
180
95
      if (bfd_seek (abfd, where, SEEK_SET) != 0)
181
13
  goto fail;
182
183
      /* Read the first section header at index 0, and convert to internal
184
   form.  */
185
82
      if (bfd_read (&x_shdr, sizeof (x_shdr), abfd) != sizeof (x_shdr))
186
10
  goto fail;
187
72
      elf_swap_shdr_in (abfd, &x_shdr, &i_shdr);
188
189
72
      if (i_shdr.sh_info != 0)
190
57
  {
191
57
    i_ehdrp->e_phnum = i_shdr.sh_info;
192
57
    if (i_ehdrp->e_phnum != i_shdr.sh_info)
193
0
      goto wrong;
194
57
  }
195
72
    }
196
197
  /* Sanity check that we can read all of the program headers.
198
     It ought to be good enough to just read the last one.  */
199
3.66k
  if (i_ehdrp->e_phnum > 1)
200
3.01k
    {
201
3.01k
      Elf_External_Phdr x_phdr;
202
3.01k
      Elf_Internal_Phdr i_phdr;
203
3.01k
      file_ptr where;
204
205
      /* Check that we don't have a totally silly number of
206
   program headers.  */
207
3.01k
      if (i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (x_phdr)
208
2.99k
    || i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (i_phdr))
209
24
  goto wrong;
210
211
2.99k
      where = (file_ptr)(i_ehdrp->e_phoff + (i_ehdrp->e_phnum - 1) * sizeof (x_phdr));
212
2.99k
      if ((bfd_size_type) where <= i_ehdrp->e_phoff)
213
9
  goto wrong;
214
215
2.98k
      if (bfd_seek (abfd, where, SEEK_SET) != 0)
216
29
  goto fail;
217
2.95k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
218
72
  goto fail;
219
2.95k
    }
220
221
  /* Move to the start of the program headers.  */
222
3.53k
  if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0)
223
22
    goto wrong;
224
225
  /* Allocate space for the program headers.  */
226
3.50k
  amt = sizeof (*i_phdrp) * i_ehdrp->e_phnum;
227
3.50k
  i_phdrp = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
228
3.50k
  if (!i_phdrp)
229
0
    goto fail;
230
231
3.50k
  elf_tdata (abfd)->phdr = i_phdrp;
232
233
  /* Read and convert to internal form.  */
234
133k
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
235
129k
    {
236
129k
      Elf_External_Phdr x_phdr;
237
238
129k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
239
16
  goto fail;
240
241
129k
      elf_swap_phdr_in (abfd, &x_phdr, i_phdrp + phindex);
242
129k
    }
243
244
  /* Set the machine architecture.  Do this before processing the
245
     program headers since we need to know the architecture type
246
     when processing the notes of some systems' core files.  */
247
3.49k
  if (! bfd_default_set_arch_mach (abfd, ebd->arch, 0)
248
      /* It's OK if this fails for the generic target.  */
249
1.06k
      && ebd->elf_machine_code != EM_NONE)
250
0
    goto fail;
251
252
  /* Let the backend double check the format and override global
253
     information.  We do this before processing the program headers
254
     to allow the correct machine (as opposed to just the default
255
     machine) to be set, making it possible for grok_prstatus and
256
     grok_psinfo to rely on the mach setting.  */
257
3.49k
  if (ebd->elf_backend_object_p != NULL
258
2.39k
      && ! ebd->elf_backend_object_p (abfd))
259
1
    goto wrong;
260
261
  /* Process each program header.  */
262
123k
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
263
120k
    if (! bfd_section_from_phdr (abfd, i_phdrp + phindex, (int) phindex))
264
693
      goto fail;
265
266
  /* Check for core truncation.  */
267
2.79k
  filesize = bfd_get_file_size (abfd);
268
2.79k
  if (filesize != 0)
269
2.79k
    {
270
5.20k
      for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
271
4.94k
  {
272
4.94k
    Elf_Internal_Phdr *p = i_phdrp + phindex;
273
4.94k
    if (p->p_filesz
274
3.79k
        && (p->p_offset >= filesize
275
1.86k
      || p->p_filesz > filesize - p->p_offset))
276
2.53k
      {
277
2.53k
        _bfd_error_handler (_("warning: %pB has a segment "
278
2.53k
            "extending past end of file"), abfd);
279
2.53k
        abfd->read_only = 1;
280
2.53k
        break;
281
2.53k
      }
282
4.94k
      }
283
2.79k
  }
284
285
  /* Save the entry point from the ELF header.  */
286
2.79k
  abfd->start_address = i_ehdrp->e_entry;
287
2.79k
  return _bfd_no_cleanup;
288
289
833k
 wrong:
290
833k
  bfd_set_error (bfd_error_wrong_format);
291
834k
 fail:
292
  return NULL;
293
833k
}
bfd_elf32_core_file_p
Line
Count
Source
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
  bfd_size_type amt;
94
2.76M
  ufile_ptr filesize;
95
96
  /* Read in the ELF header in external format.  */
97
2.76M
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
98
432k
    {
99
432k
      if (bfd_get_error () != bfd_error_system_call)
100
432k
  goto wrong;
101
0
      else
102
0
  goto fail;
103
432k
    }
104
105
  /* Check the magic number.  */
106
2.33M
  if (! elf_file_p (&x_ehdr))
107
856k
    goto wrong;
108
109
  /* FIXME: Check EI_VERSION here !  */
110
111
  /* Check the address size ("class").  */
112
1.47M
  if (x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
113
282k
    goto wrong;
114
115
  /* Check the byteorder.  */
116
1.19M
  switch (x_ehdr.e_ident[EI_DATA])
117
1.19M
    {
118
788k
    case ELFDATA2MSB:   /* Big-endian.  */
119
788k
      if (! bfd_big_endian (abfd))
120
391k
  goto wrong;
121
396k
      break;
122
404k
    case ELFDATA2LSB:   /* Little-endian.  */
123
404k
      if (! bfd_little_endian (abfd))
124
202k
  goto wrong;
125
202k
      break;
126
202k
    default:
127
396
      goto wrong;
128
1.19M
    }
129
130
  /* Give abfd an elf_obj_tdata.  */
131
599k
  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
599k
  i_ehdrp = elf_elfheader (abfd);
136
599k
  elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
137
138
#if DEBUG & 1
139
  elf_debug_file (i_ehdrp);
140
#endif
141
142
599k
  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
599k
  if (ebd->elf_machine_code != i_ehdrp->e_machine
148
581k
      && (ebd->elf_machine_alt1 == 0
149
178k
    || i_ehdrp->e_machine != ebd->elf_machine_alt1)
150
581k
      && (ebd->elf_machine_alt2 == 0
151
26.8k
    || i_ehdrp->e_machine != ebd->elf_machine_alt2)
152
580k
      && ebd->elf_machine_code != EM_NONE)
153
571k
    goto wrong;
154
155
27.5k
  if (ebd->osabi_exact && i_ehdrp->e_ident[EI_OSABI] != ebd->elf_osabi)
156
1.04k
    goto wrong;
157
158
  /* If there is no program header, or the type is not a core file, then
159
     we are hosed.  */
160
26.4k
  if (i_ehdrp->e_phoff == 0 || i_ehdrp->e_type != ET_CORE)
161
3.68k
    goto wrong;
162
163
  /* Does BFD's idea of the phdr size match the size
164
     recorded in the file? */
165
22.7k
  if (i_ehdrp->e_phentsize != sizeof (Elf_External_Phdr))
166
20
    goto wrong;
167
168
  /* If the program header count is PN_XNUM(0xffff), the actual
169
     count is in the first section header.  */
170
22.7k
  if (i_ehdrp->e_shoff != 0 && i_ehdrp->e_phnum == PN_XNUM)
171
108
    {
172
108
      Elf_External_Shdr x_shdr;
173
108
      Elf_Internal_Shdr i_shdr;
174
108
      file_ptr where = (file_ptr) i_ehdrp->e_shoff;
175
176
108
      if (i_ehdrp->e_shoff < sizeof (x_ehdr))
177
10
  goto wrong;
178
179
      /* Seek to the section header table in the file.  */
180
98
      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
98
      if (bfd_read (&x_shdr, sizeof (x_shdr), abfd) != sizeof (x_shdr))
186
14
  goto fail;
187
84
      elf_swap_shdr_in (abfd, &x_shdr, &i_shdr);
188
189
84
      if (i_shdr.sh_info != 0)
190
74
  {
191
74
    i_ehdrp->e_phnum = i_shdr.sh_info;
192
74
    if (i_ehdrp->e_phnum != i_shdr.sh_info)
193
0
      goto wrong;
194
74
  }
195
84
    }
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
22.7k
  if (i_ehdrp->e_phnum > 1)
200
21.8k
    {
201
21.8k
      Elf_External_Phdr x_phdr;
202
21.8k
      Elf_Internal_Phdr i_phdr;
203
21.8k
      file_ptr where;
204
205
      /* Check that we don't have a totally silly number of
206
   program headers.  */
207
21.8k
      if (i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (x_phdr)
208
21.8k
    || i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (i_phdr))
209
23
  goto wrong;
210
211
21.8k
      where = (file_ptr)(i_ehdrp->e_phoff + (i_ehdrp->e_phnum - 1) * sizeof (x_phdr));
212
21.8k
      if ((bfd_size_type) where <= i_ehdrp->e_phoff)
213
0
  goto wrong;
214
215
21.8k
      if (bfd_seek (abfd, where, SEEK_SET) != 0)
216
0
  goto fail;
217
21.8k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
218
72
  goto fail;
219
21.8k
    }
220
221
  /* Move to the start of the program headers.  */
222
22.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
22.6k
  amt = sizeof (*i_phdrp) * i_ehdrp->e_phnum;
227
22.6k
  i_phdrp = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
228
22.6k
  if (!i_phdrp)
229
0
    goto fail;
230
231
22.6k
  elf_tdata (abfd)->phdr = i_phdrp;
232
233
  /* Read and convert to internal form.  */
234
599k
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
235
576k
    {
236
576k
      Elf_External_Phdr x_phdr;
237
238
576k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
239
12
  goto fail;
240
241
576k
      elf_swap_phdr_in (abfd, &x_phdr, i_phdrp + phindex);
242
576k
    }
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
22.6k
  if (! bfd_default_set_arch_mach (abfd, ebd->arch, 0)
248
      /* It's OK if this fails for the generic target.  */
249
6.16k
      && ebd->elf_machine_code != EM_NONE)
250
1
    goto fail;
251
252
  /* Let the backend double check the format and override global
253
     information.  We do this before processing the program headers
254
     to allow the correct machine (as opposed to just the default
255
     machine) to be set, making it possible for grok_prstatus and
256
     grok_psinfo to rely on the mach setting.  */
257
22.6k
  if (ebd->elf_backend_object_p != NULL
258
15.6k
      && ! ebd->elf_backend_object_p (abfd))
259
3.12k
    goto wrong;
260
261
  /* Process each program header.  */
262
509k
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
263
490k
    if (! bfd_section_from_phdr (abfd, i_phdrp + phindex, (int) phindex))
264
561
      goto fail;
265
266
  /* Check for core truncation.  */
267
18.9k
  filesize = bfd_get_file_size (abfd);
268
18.9k
  if (filesize != 0)
269
18.9k
    {
270
25.9k
      for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
271
25.2k
  {
272
25.2k
    Elf_Internal_Phdr *p = i_phdrp + phindex;
273
25.2k
    if (p->p_filesz
274
22.4k
        && (p->p_offset >= filesize
275
8.01k
      || p->p_filesz > filesize - p->p_offset))
276
18.2k
      {
277
18.2k
        _bfd_error_handler (_("warning: %pB has a segment "
278
18.2k
            "extending past end of file"), abfd);
279
18.2k
        abfd->read_only = 1;
280
18.2k
        break;
281
18.2k
      }
282
25.2k
      }
283
18.9k
  }
284
285
  /* Save the entry point from the ELF header.  */
286
18.9k
  abfd->start_address = i_ehdrp->e_entry;
287
18.9k
  return _bfd_no_cleanup;
288
289
2.74M
 wrong:
290
2.74M
  bfd_set_error (bfd_error_wrong_format);
291
2.74M
 fail:
292
  return NULL;
293
2.74M
}
294
295
/* Attempt to find a build-id in a core file from the core file BFD.
296
   OFFSET is the file offset to a PT_LOAD segment that may contain
297
   the build-id note.  Returns TRUE upon success, FALSE otherwise.  */
298
299
bool
300
NAME(_bfd_elf, core_find_build_id)
301
  (bfd *abfd,
302
   bfd_vma offset)
303
10.9k
{
304
10.9k
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.   */
305
10.9k
  Elf_Internal_Ehdr i_ehdr; /* Elf file header, internal form.   */
306
10.9k
  Elf_Internal_Phdr *i_phdr;
307
10.9k
  unsigned int i;
308
10.9k
  size_t amt;
309
310
  /* Seek to the position of the segment at OFFSET.  */
311
10.9k
  if (bfd_seek (abfd, offset, SEEK_SET) != 0)
312
1.13k
    goto fail;
313
314
  /* Read in the ELF header in external format.  */
315
9.84k
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
316
2.40k
    {
317
2.40k
      if (bfd_get_error () != bfd_error_system_call)
318
2.40k
  goto wrong;
319
0
      else
320
0
  goto fail;
321
2.40k
    }
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.43k
  if (! elf_file_p (&x_ehdr)
329
3.69k
      || x_ehdr.e_ident[EI_VERSION] != EV_CURRENT
330
3.47k
      || x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
331
4.00k
    goto wrong;
332
333
  /* Check that file's byte order matches xvec's.  */
334
3.43k
  switch (x_ehdr.e_ident[EI_DATA])
335
3.43k
    {
336
887
    case ELFDATA2MSB:   /* Big-endian.  */
337
887
      if (! bfd_header_big_endian (abfd))
338
21
  goto wrong;
339
866
      break;
340
2.54k
    case ELFDATA2LSB:   /* Little-endian.  */
341
2.54k
      if (! bfd_header_little_endian (abfd))
342
8
  goto wrong;
343
2.53k
      break;
344
2.53k
    case ELFDATANONE:   /* No data encoding specified.  */
345
5
    default:      /* Unknown data encoding specified . */
346
5
      goto wrong;
347
3.43k
    }
348
349
3.39k
  elf_swap_ehdr_in (abfd, &x_ehdr, &i_ehdr);
350
#if DEBUG & 1
351
  elf_debug_file (&i_ehdr);
352
#endif
353
354
3.39k
  if (i_ehdr.e_phentsize != sizeof (Elf_External_Phdr) || i_ehdr.e_phnum == 0)
355
31
    goto fail;
356
357
  /* Read in program headers.  */
358
3.36k
  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.36k
  i_phdr = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
364
3.36k
  if (i_phdr == NULL)
365
0
    goto fail;
366
367
3.36k
  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
473k
  for (i = 0; i < i_ehdr.e_phnum; ++i, ++i_phdr)
372
470k
    {
373
470k
      Elf_External_Phdr x_phdr;
374
375
470k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
376
322
  goto fail;
377
470k
      elf_swap_phdr_in (abfd, &x_phdr, i_phdr);
378
379
470k
      if (i_phdr->p_type == PT_NOTE && i_phdr->p_filesz > 0)
380
3.49k
  {
381
3.49k
    _bfd_elf_read_notes (abfd, offset + i_phdr->p_offset,
382
3.49k
             i_phdr->p_filesz, i_phdr->p_align);
383
384
    /* Make sure ABFD returns to processing the program headers.  */
385
3.49k
    if (bfd_seek (abfd,
386
3.49k
      offset + i_ehdr.e_phoff + (i + 1) * sizeof (x_phdr),
387
3.49k
      SEEK_SET) != 0)
388
0
      goto fail;
389
390
3.49k
    if (abfd->build_id != NULL)
391
19
      return true;
392
3.49k
  }
393
470k
    }
394
395
  /* Having gotten this far, we have a valid ELF section, but no
396
     build-id was found.  */
397
3.02k
  goto fail;
398
399
6.44k
 wrong:
400
6.44k
  bfd_set_error (bfd_error_wrong_format);
401
10.9k
 fail:
402
  return false;
403
6.44k
}
_bfd_elf64_core_find_build_id
Line
Count
Source
303
6.15k
{
304
6.15k
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.   */
305
6.15k
  Elf_Internal_Ehdr i_ehdr; /* Elf file header, internal form.   */
306
6.15k
  Elf_Internal_Phdr *i_phdr;
307
6.15k
  unsigned int i;
308
6.15k
  size_t amt;
309
310
  /* Seek to the position of the segment at OFFSET.  */
311
6.15k
  if (bfd_seek (abfd, offset, SEEK_SET) != 0)
312
1.13k
    goto fail;
313
314
  /* Read in the ELF header in external format.  */
315
5.02k
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
316
1.21k
    {
317
1.21k
      if (bfd_get_error () != bfd_error_system_call)
318
1.21k
  goto wrong;
319
0
      else
320
0
  goto fail;
321
1.21k
    }
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.80k
  if (! elf_file_p (&x_ehdr)
329
2.42k
      || x_ehdr.e_ident[EI_VERSION] != EV_CURRENT
330
2.27k
      || x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
331
1.56k
    goto wrong;
332
333
  /* Check that file's byte order matches xvec's.  */
334
2.24k
  switch (x_ehdr.e_ident[EI_DATA])
335
2.24k
    {
336
20
    case ELFDATA2MSB:   /* Big-endian.  */
337
20
      if (! bfd_header_big_endian (abfd))
338
0
  goto wrong;
339
20
      break;
340
2.22k
    case ELFDATA2LSB:   /* Little-endian.  */
341
2.22k
      if (! bfd_header_little_endian (abfd))
342
0
  goto wrong;
343
2.22k
      break;
344
2.22k
    case ELFDATANONE:   /* No data encoding specified.  */
345
0
    default:      /* Unknown data encoding specified . */
346
0
      goto wrong;
347
2.24k
    }
348
349
2.24k
  elf_swap_ehdr_in (abfd, &x_ehdr, &i_ehdr);
350
#if DEBUG & 1
351
  elf_debug_file (&i_ehdr);
352
#endif
353
354
2.24k
  if (i_ehdr.e_phentsize != sizeof (Elf_External_Phdr) || i_ehdr.e_phnum == 0)
355
10
    goto fail;
356
357
  /* Read in program headers.  */
358
2.23k
  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.23k
  i_phdr = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
364
2.23k
  if (i_phdr == NULL)
365
0
    goto fail;
366
367
2.23k
  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
292k
  for (i = 0; i < i_ehdr.e_phnum; ++i, ++i_phdr)
372
290k
    {
373
290k
      Elf_External_Phdr x_phdr;
374
375
290k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
376
47
  goto fail;
377
290k
      elf_swap_phdr_in (abfd, &x_phdr, i_phdr);
378
379
290k
      if (i_phdr->p_type == PT_NOTE && i_phdr->p_filesz > 0)
380
2.44k
  {
381
2.44k
    _bfd_elf_read_notes (abfd, offset + i_phdr->p_offset,
382
2.44k
             i_phdr->p_filesz, i_phdr->p_align);
383
384
    /* Make sure ABFD returns to processing the program headers.  */
385
2.44k
    if (bfd_seek (abfd,
386
2.44k
      offset + i_ehdr.e_phoff + (i + 1) * sizeof (x_phdr),
387
2.44k
      SEEK_SET) != 0)
388
0
      goto fail;
389
390
2.44k
    if (abfd->build_id != NULL)
391
7
      return true;
392
2.44k
  }
393
290k
    }
394
395
  /* Having gotten this far, we have a valid ELF section, but no
396
     build-id was found.  */
397
2.18k
  goto fail;
398
399
2.77k
 wrong:
400
2.77k
  bfd_set_error (bfd_error_wrong_format);
401
6.14k
 fail:
402
  return false;
403
2.77k
}
_bfd_elf32_core_find_build_id
Line
Count
Source
303
4.82k
{
304
4.82k
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.   */
305
4.82k
  Elf_Internal_Ehdr i_ehdr; /* Elf file header, internal form.   */
306
4.82k
  Elf_Internal_Phdr *i_phdr;
307
4.82k
  unsigned int i;
308
4.82k
  size_t amt;
309
310
  /* Seek to the position of the segment at OFFSET.  */
311
4.82k
  if (bfd_seek (abfd, offset, SEEK_SET) != 0)
312
0
    goto fail;
313
314
  /* Read in the ELF header in external format.  */
315
4.82k
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
316
1.19k
    {
317
1.19k
      if (bfd_get_error () != bfd_error_system_call)
318
1.19k
  goto wrong;
319
0
      else
320
0
  goto fail;
321
1.19k
    }
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.63k
  if (! elf_file_p (&x_ehdr)
329
1.27k
      || x_ehdr.e_ident[EI_VERSION] != EV_CURRENT
330
1.20k
      || x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
331
2.44k
    goto wrong;
332
333
  /* Check that file's byte order matches xvec's.  */
334
1.18k
  switch (x_ehdr.e_ident[EI_DATA])
335
1.18k
    {
336
867
    case ELFDATA2MSB:   /* Big-endian.  */
337
867
      if (! bfd_header_big_endian (abfd))
338
21
  goto wrong;
339
846
      break;
340
846
    case ELFDATA2LSB:   /* Little-endian.  */
341
314
      if (! bfd_header_little_endian (abfd))
342
8
  goto wrong;
343
306
      break;
344
306
    case ELFDATANONE:   /* No data encoding specified.  */
345
5
    default:      /* Unknown data encoding specified . */
346
5
      goto wrong;
347
1.18k
    }
348
349
1.15k
  elf_swap_ehdr_in (abfd, &x_ehdr, &i_ehdr);
350
#if DEBUG & 1
351
  elf_debug_file (&i_ehdr);
352
#endif
353
354
1.15k
  if (i_ehdr.e_phentsize != sizeof (Elf_External_Phdr) || i_ehdr.e_phnum == 0)
355
21
    goto fail;
356
357
  /* Read in program headers.  */
358
1.13k
  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.13k
  i_phdr = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
364
1.13k
  if (i_phdr == NULL)
365
0
    goto fail;
366
367
1.13k
  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
180k
  for (i = 0; i < i_ehdr.e_phnum; ++i, ++i_phdr)
372
180k
    {
373
180k
      Elf_External_Phdr x_phdr;
374
375
180k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
376
275
  goto fail;
377
179k
      elf_swap_phdr_in (abfd, &x_phdr, i_phdr);
378
379
179k
      if (i_phdr->p_type == PT_NOTE && i_phdr->p_filesz > 0)
380
1.05k
  {
381
1.05k
    _bfd_elf_read_notes (abfd, offset + i_phdr->p_offset,
382
1.05k
             i_phdr->p_filesz, i_phdr->p_align);
383
384
    /* Make sure ABFD returns to processing the program headers.  */
385
1.05k
    if (bfd_seek (abfd,
386
1.05k
      offset + i_ehdr.e_phoff + (i + 1) * sizeof (x_phdr),
387
1.05k
      SEEK_SET) != 0)
388
0
      goto fail;
389
390
1.05k
    if (abfd->build_id != NULL)
391
12
      return true;
392
1.05k
  }
393
179k
    }
394
395
  /* Having gotten this far, we have a valid ELF section, but no
396
     build-id was found.  */
397
844
  goto fail;
398
399
3.67k
 wrong:
400
3.67k
  bfd_set_error (bfd_error_wrong_format);
401
4.81k
 fail:
402
  return false;
403
3.67k
}