Coverage Report

Created: 2024-05-21 06:29

/src/binutils-gdb/bfd/elfcore.h
Line
Count
Source (jump to first uncovered line)
1
/* ELF core file support for BFD.
2
   Copyright (C) 1995-2024 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
1.53M
{
89
1.53M
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.  */
90
1.53M
  Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form.  */
91
1.53M
  Elf_Internal_Phdr *i_phdrp; /* Elf program header, internal form.  */
92
1.53M
  unsigned int phindex;
93
1.53M
  const struct elf_backend_data *ebd;
94
1.53M
  bfd_size_type amt;
95
1.53M
  ufile_ptr filesize;
96
97
  /* Read in the ELF header in external format.  */
98
1.53M
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
99
246k
    {
100
246k
      if (bfd_get_error () != bfd_error_system_call)
101
245k
  goto wrong;
102
1.24k
      else
103
1.24k
  goto fail;
104
246k
    }
105
106
  /* Check the magic number.  */
107
1.28M
  if (! elf_file_p (&x_ehdr))
108
408k
    goto wrong;
109
110
  /* FIXME: Check EI_VERSION here !  */
111
112
  /* Check the address size ("class").  */
113
878k
  if (x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
114
307k
    goto wrong;
115
116
  /* Check the byteorder.  */
117
571k
  switch (x_ehdr.e_ident[EI_DATA])
118
571k
    {
119
374k
    case ELFDATA2MSB:   /* Big-endian.  */
120
374k
      if (! bfd_big_endian (abfd))
121
186k
  goto wrong;
122
187k
      break;
123
195k
    case ELFDATA2LSB:   /* Little-endian.  */
124
195k
      if (! bfd_little_endian (abfd))
125
95.4k
  goto wrong;
126
99.5k
      break;
127
99.5k
    default:
128
1.38k
      goto wrong;
129
571k
    }
130
131
  /* Give abfd an elf_obj_tdata.  */
132
287k
  if (! (*abfd->xvec->_bfd_set_format[bfd_core]) (abfd))
133
0
    goto fail;
134
135
  /* Swap in the rest of the header, now that we have the byte order.  */
136
287k
  i_ehdrp = elf_elfheader (abfd);
137
287k
  elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
138
139
#if DEBUG & 1
140
  elf_debug_file (i_ehdrp);
141
#endif
142
143
287k
  ebd = get_elf_backend_data (abfd);
144
145
  /* Check that the ELF e_machine field matches what this particular
146
     BFD format expects.  */
147
148
287k
  if (ebd->elf_machine_code != i_ehdrp->e_machine
149
287k
      && (ebd->elf_machine_alt1 == 0
150
277k
    || i_ehdrp->e_machine != ebd->elf_machine_alt1)
151
287k
      && (ebd->elf_machine_alt2 == 0
152
277k
    || i_ehdrp->e_machine != ebd->elf_machine_alt2)
153
287k
      && ebd->elf_machine_code != EM_NONE)
154
272k
    goto wrong;
155
156
14.8k
  if (ebd->elf_machine_code != EM_NONE
157
14.8k
      && i_ehdrp->e_ident[EI_OSABI] != ebd->elf_osabi
158
14.8k
      && ebd->elf_osabi != ELFOSABI_NONE)
159
990
    goto wrong;
160
161
  /* If there is no program header, or the type is not a core file, then
162
     we are hosed.  */
163
13.9k
  if (i_ehdrp->e_phoff == 0 || i_ehdrp->e_type != ET_CORE)
164
3.45k
    goto wrong;
165
166
  /* Does BFD's idea of the phdr size match the size
167
     recorded in the file? */
168
10.4k
  if (i_ehdrp->e_phentsize != sizeof (Elf_External_Phdr))
169
27
    goto wrong;
170
171
  /* If the program header count is PN_XNUM(0xffff), the actual
172
     count is in the first section header.  */
173
10.4k
  if (i_ehdrp->e_shoff != 0 && i_ehdrp->e_phnum == PN_XNUM)
174
19
    {
175
19
      Elf_External_Shdr x_shdr;
176
19
      Elf_Internal_Shdr i_shdr;
177
19
      file_ptr where = (file_ptr) i_ehdrp->e_shoff;
178
179
19
      if (i_ehdrp->e_shoff < sizeof (x_ehdr))
180
0
  goto wrong;
181
182
      /* Seek to the section header table in the file.  */
183
19
      if (bfd_seek (abfd, where, SEEK_SET) != 0)
184
2
  goto fail;
185
186
      /* Read the first section header at index 0, and convert to internal
187
   form.  */
188
17
      if (bfd_read (&x_shdr, sizeof (x_shdr), abfd) != sizeof (x_shdr))
189
2
  goto fail;
190
15
      elf_swap_shdr_in (abfd, &x_shdr, &i_shdr);
191
192
15
      if (i_shdr.sh_info != 0)
193
12
  {
194
12
    i_ehdrp->e_phnum = i_shdr.sh_info;
195
12
    if (i_ehdrp->e_phnum != i_shdr.sh_info)
196
0
      goto wrong;
197
12
  }
198
15
    }
199
200
  /* Sanity check that we can read all of the program headers.
201
     It ought to be good enough to just read the last one.  */
202
10.4k
  if (i_ehdrp->e_phnum > 1)
203
9.72k
    {
204
9.72k
      Elf_External_Phdr x_phdr;
205
9.72k
      Elf_Internal_Phdr i_phdr;
206
9.72k
      file_ptr where;
207
208
      /* Check that we don't have a totally silly number of
209
   program headers.  */
210
9.72k
      if (i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (x_phdr)
211
9.72k
    || i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (i_phdr))
212
3
  goto wrong;
213
214
9.72k
      where = (file_ptr)(i_ehdrp->e_phoff + (i_ehdrp->e_phnum - 1) * sizeof (x_phdr));
215
9.72k
      if ((bfd_size_type) where <= i_ehdrp->e_phoff)
216
5
  goto wrong;
217
218
9.71k
      if (bfd_seek (abfd, where, SEEK_SET) != 0)
219
10
  goto fail;
220
9.70k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
221
26
  goto fail;
222
9.70k
    }
223
224
  /* Move to the start of the program headers.  */
225
10.3k
  if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0)
226
3
    goto wrong;
227
228
  /* Allocate space for the program headers.  */
229
10.3k
  amt = sizeof (*i_phdrp) * i_ehdrp->e_phnum;
230
10.3k
  i_phdrp = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
231
10.3k
  if (!i_phdrp)
232
0
    goto fail;
233
234
10.3k
  elf_tdata (abfd)->phdr = i_phdrp;
235
236
  /* Read and convert to internal form.  */
237
289k
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
238
278k
    {
239
278k
      Elf_External_Phdr x_phdr;
240
241
278k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
242
13
  goto fail;
243
244
278k
      elf_swap_phdr_in (abfd, &x_phdr, i_phdrp + phindex);
245
278k
    }
246
247
  /* Set the machine architecture.  Do this before processing the
248
     program headers since we need to know the architecture type
249
     when processing the notes of some systems' core files.  */
250
10.3k
  if (! bfd_default_set_arch_mach (abfd, ebd->arch, 0)
251
      /* It's OK if this fails for the generic target.  */
252
10.3k
      && ebd->elf_machine_code != EM_NONE)
253
0
    goto fail;
254
255
  /* Let the backend double check the format and override global
256
     information.  We do this before processing the program headers
257
     to allow the correct machine (as opposed to just the default
258
     machine) to be set, making it possible for grok_prstatus and
259
     grok_psinfo to rely on the mach setting.  */
260
10.3k
  if (ebd->elf_backend_object_p != NULL
261
10.3k
      && ! ebd->elf_backend_object_p (abfd))
262
1.10k
    goto wrong;
263
264
  /* Process each program header.  */
265
249k
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
266
240k
    if (! bfd_section_from_phdr (abfd, i_phdrp + phindex, (int) phindex))
267
63
      goto fail;
268
269
  /* Check for core truncation.  */
270
9.19k
  filesize = bfd_get_file_size (abfd);
271
9.19k
  if (filesize != 0)
272
9.19k
    {
273
14.1k
      for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
274
13.5k
  {
275
13.5k
    Elf_Internal_Phdr *p = i_phdrp + phindex;
276
13.5k
    if (p->p_filesz
277
13.5k
        && (p->p_offset >= filesize
278
12.3k
      || p->p_filesz > filesize - p->p_offset))
279
8.61k
      {
280
8.61k
        _bfd_error_handler (_("warning: %pB has a segment "
281
8.61k
            "extending past end of file"), abfd);
282
8.61k
        abfd->read_only = 1;
283
8.61k
        break;
284
8.61k
      }
285
13.5k
      }
286
9.19k
  }
287
288
  /* Save the entry point from the ELF header.  */
289
9.19k
  abfd->start_address = i_ehdrp->e_entry;
290
9.19k
  return _bfd_no_cleanup;
291
292
1.52M
 wrong:
293
1.52M
  bfd_set_error (bfd_error_wrong_format);
294
1.52M
 fail:
295
1.52M
  return NULL;
296
1.52M
}
bfd_elf64_core_file_p
Line
Count
Source
88
378k
{
89
378k
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.  */
90
378k
  Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form.  */
91
378k
  Elf_Internal_Phdr *i_phdrp; /* Elf program header, internal form.  */
92
378k
  unsigned int phindex;
93
378k
  const struct elf_backend_data *ebd;
94
378k
  bfd_size_type amt;
95
378k
  ufile_ptr filesize;
96
97
  /* Read in the ELF header in external format.  */
98
378k
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
99
95.9k
    {
100
95.9k
      if (bfd_get_error () != bfd_error_system_call)
101
95.6k
  goto wrong;
102
308
      else
103
308
  goto fail;
104
95.9k
    }
105
106
  /* Check the magic number.  */
107
282k
  if (! elf_file_p (&x_ehdr))
108
89.4k
    goto wrong;
109
110
  /* FIXME: Check EI_VERSION here !  */
111
112
  /* Check the address size ("class").  */
113
193k
  if (x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
114
137k
    goto wrong;
115
116
  /* Check the byteorder.  */
117
55.9k
  switch (x_ehdr.e_ident[EI_DATA])
118
55.9k
    {
119
14.4k
    case ELFDATA2MSB:   /* Big-endian.  */
120
14.4k
      if (! bfd_big_endian (abfd))
121
7.89k
  goto wrong;
122
6.58k
      break;
123
41.3k
    case ELFDATA2LSB:   /* Little-endian.  */
124
41.3k
      if (! bfd_little_endian (abfd))
125
18.6k
  goto wrong;
126
22.7k
      break;
127
22.7k
    default:
128
176
      goto wrong;
129
55.9k
    }
130
131
  /* Give abfd an elf_obj_tdata.  */
132
29.3k
  if (! (*abfd->xvec->_bfd_set_format[bfd_core]) (abfd))
133
0
    goto fail;
134
135
  /* Swap in the rest of the header, now that we have the byte order.  */
136
29.3k
  i_ehdrp = elf_elfheader (abfd);
137
29.3k
  elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
138
139
#if DEBUG & 1
140
  elf_debug_file (i_ehdrp);
141
#endif
142
143
29.3k
  ebd = get_elf_backend_data (abfd);
144
145
  /* Check that the ELF e_machine field matches what this particular
146
     BFD format expects.  */
147
148
29.3k
  if (ebd->elf_machine_code != i_ehdrp->e_machine
149
29.3k
      && (ebd->elf_machine_alt1 == 0
150
26.7k
    || i_ehdrp->e_machine != ebd->elf_machine_alt1)
151
29.3k
      && (ebd->elf_machine_alt2 == 0
152
26.7k
    || i_ehdrp->e_machine != ebd->elf_machine_alt2)
153
29.3k
      && ebd->elf_machine_code != EM_NONE)
154
25.6k
    goto wrong;
155
156
3.64k
  if (ebd->elf_machine_code != EM_NONE
157
3.64k
      && i_ehdrp->e_ident[EI_OSABI] != ebd->elf_osabi
158
3.64k
      && ebd->elf_osabi != ELFOSABI_NONE)
159
651
    goto wrong;
160
161
  /* If there is no program header, or the type is not a core file, then
162
     we are hosed.  */
163
2.99k
  if (i_ehdrp->e_phoff == 0 || i_ehdrp->e_type != ET_CORE)
164
1.61k
    goto wrong;
165
166
  /* Does BFD's idea of the phdr size match the size
167
     recorded in the file? */
168
1.38k
  if (i_ehdrp->e_phentsize != sizeof (Elf_External_Phdr))
169
15
    goto wrong;
170
171
  /* If the program header count is PN_XNUM(0xffff), the actual
172
     count is in the first section header.  */
173
1.37k
  if (i_ehdrp->e_shoff != 0 && i_ehdrp->e_phnum == PN_XNUM)
174
12
    {
175
12
      Elf_External_Shdr x_shdr;
176
12
      Elf_Internal_Shdr i_shdr;
177
12
      file_ptr where = (file_ptr) i_ehdrp->e_shoff;
178
179
12
      if (i_ehdrp->e_shoff < sizeof (x_ehdr))
180
0
  goto wrong;
181
182
      /* Seek to the section header table in the file.  */
183
12
      if (bfd_seek (abfd, where, SEEK_SET) != 0)
184
2
  goto fail;
185
186
      /* Read the first section header at index 0, and convert to internal
187
   form.  */
188
10
      if (bfd_read (&x_shdr, sizeof (x_shdr), abfd) != sizeof (x_shdr))
189
0
  goto fail;
190
10
      elf_swap_shdr_in (abfd, &x_shdr, &i_shdr);
191
192
10
      if (i_shdr.sh_info != 0)
193
7
  {
194
7
    i_ehdrp->e_phnum = i_shdr.sh_info;
195
7
    if (i_ehdrp->e_phnum != i_shdr.sh_info)
196
0
      goto wrong;
197
7
  }
198
10
    }
199
200
  /* Sanity check that we can read all of the program headers.
201
     It ought to be good enough to just read the last one.  */
202
1.36k
  if (i_ehdrp->e_phnum > 1)
203
773
    {
204
773
      Elf_External_Phdr x_phdr;
205
773
      Elf_Internal_Phdr i_phdr;
206
773
      file_ptr where;
207
208
      /* Check that we don't have a totally silly number of
209
   program headers.  */
210
773
      if (i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (x_phdr)
211
773
    || i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (i_phdr))
212
3
  goto wrong;
213
214
770
      where = (file_ptr)(i_ehdrp->e_phoff + (i_ehdrp->e_phnum - 1) * sizeof (x_phdr));
215
770
      if ((bfd_size_type) where <= i_ehdrp->e_phoff)
216
5
  goto wrong;
217
218
765
      if (bfd_seek (abfd, where, SEEK_SET) != 0)
219
10
  goto fail;
220
755
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
221
10
  goto fail;
222
755
    }
223
224
  /* Move to the start of the program headers.  */
225
1.34k
  if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0)
226
3
    goto wrong;
227
228
  /* Allocate space for the program headers.  */
229
1.33k
  amt = sizeof (*i_phdrp) * i_ehdrp->e_phnum;
230
1.33k
  i_phdrp = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
231
1.33k
  if (!i_phdrp)
232
0
    goto fail;
233
234
1.33k
  elf_tdata (abfd)->phdr = i_phdrp;
235
236
  /* Read and convert to internal form.  */
237
18.8k
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
238
17.5k
    {
239
17.5k
      Elf_External_Phdr x_phdr;
240
241
17.5k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
242
13
  goto fail;
243
244
17.5k
      elf_swap_phdr_in (abfd, &x_phdr, i_phdrp + phindex);
245
17.5k
    }
246
247
  /* Set the machine architecture.  Do this before processing the
248
     program headers since we need to know the architecture type
249
     when processing the notes of some systems' core files.  */
250
1.32k
  if (! bfd_default_set_arch_mach (abfd, ebd->arch, 0)
251
      /* It's OK if this fails for the generic target.  */
252
1.32k
      && ebd->elf_machine_code != EM_NONE)
253
0
    goto fail;
254
255
  /* Let the backend double check the format and override global
256
     information.  We do this before processing the program headers
257
     to allow the correct machine (as opposed to just the default
258
     machine) to be set, making it possible for grok_prstatus and
259
     grok_psinfo to rely on the mach setting.  */
260
1.32k
  if (ebd->elf_backend_object_p != NULL
261
1.32k
      && ! ebd->elf_backend_object_p (abfd))
262
1
    goto wrong;
263
264
  /* Process each program header.  */
265
18.7k
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
266
17.4k
    if (! bfd_section_from_phdr (abfd, i_phdrp + phindex, (int) phindex))
267
23
      goto fail;
268
269
  /* Check for core truncation.  */
270
1.30k
  filesize = bfd_get_file_size (abfd);
271
1.30k
  if (filesize != 0)
272
1.30k
    {
273
2.03k
      for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
274
1.56k
  {
275
1.56k
    Elf_Internal_Phdr *p = i_phdrp + phindex;
276
1.56k
    if (p->p_filesz
277
1.56k
        && (p->p_offset >= filesize
278
1.51k
      || p->p_filesz > filesize - p->p_offset))
279
829
      {
280
829
        _bfd_error_handler (_("warning: %pB has a segment "
281
829
            "extending past end of file"), abfd);
282
829
        abfd->read_only = 1;
283
829
        break;
284
829
      }
285
1.56k
      }
286
1.30k
  }
287
288
  /* Save the entry point from the ELF header.  */
289
1.30k
  abfd->start_address = i_ehdrp->e_entry;
290
1.30k
  return _bfd_no_cleanup;
291
292
377k
 wrong:
293
377k
  bfd_set_error (bfd_error_wrong_format);
294
377k
 fail:
295
377k
  return NULL;
296
377k
}
bfd_elf32_core_file_p
Line
Count
Source
88
1.15M
{
89
1.15M
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.  */
90
1.15M
  Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form.  */
91
1.15M
  Elf_Internal_Phdr *i_phdrp; /* Elf program header, internal form.  */
92
1.15M
  unsigned int phindex;
93
1.15M
  const struct elf_backend_data *ebd;
94
1.15M
  bfd_size_type amt;
95
1.15M
  ufile_ptr filesize;
96
97
  /* Read in the ELF header in external format.  */
98
1.15M
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
99
150k
    {
100
150k
      if (bfd_get_error () != bfd_error_system_call)
101
149k
  goto wrong;
102
938
      else
103
938
  goto fail;
104
150k
    }
105
106
  /* Check the magic number.  */
107
1.00M
  if (! elf_file_p (&x_ehdr))
108
318k
    goto wrong;
109
110
  /* FIXME: Check EI_VERSION here !  */
111
112
  /* Check the address size ("class").  */
113
684k
  if (x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
114
169k
    goto wrong;
115
116
  /* Check the byteorder.  */
117
515k
  switch (x_ehdr.e_ident[EI_DATA])
118
515k
    {
119
360k
    case ELFDATA2MSB:   /* Big-endian.  */
120
360k
      if (! bfd_big_endian (abfd))
121
179k
  goto wrong;
122
181k
      break;
123
181k
    case ELFDATA2LSB:   /* Little-endian.  */
124
153k
      if (! bfd_little_endian (abfd))
125
76.8k
  goto wrong;
126
76.8k
      break;
127
76.8k
    default:
128
1.20k
      goto wrong;
129
515k
    }
130
131
  /* Give abfd an elf_obj_tdata.  */
132
257k
  if (! (*abfd->xvec->_bfd_set_format[bfd_core]) (abfd))
133
0
    goto fail;
134
135
  /* Swap in the rest of the header, now that we have the byte order.  */
136
257k
  i_ehdrp = elf_elfheader (abfd);
137
257k
  elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
138
139
#if DEBUG & 1
140
  elf_debug_file (i_ehdrp);
141
#endif
142
143
257k
  ebd = get_elf_backend_data (abfd);
144
145
  /* Check that the ELF e_machine field matches what this particular
146
     BFD format expects.  */
147
148
257k
  if (ebd->elf_machine_code != i_ehdrp->e_machine
149
257k
      && (ebd->elf_machine_alt1 == 0
150
250k
    || i_ehdrp->e_machine != ebd->elf_machine_alt1)
151
257k
      && (ebd->elf_machine_alt2 == 0
152
250k
    || i_ehdrp->e_machine != ebd->elf_machine_alt2)
153
257k
      && ebd->elf_machine_code != EM_NONE)
154
246k
    goto wrong;
155
156
11.2k
  if (ebd->elf_machine_code != EM_NONE
157
11.2k
      && i_ehdrp->e_ident[EI_OSABI] != ebd->elf_osabi
158
11.2k
      && ebd->elf_osabi != ELFOSABI_NONE)
159
339
    goto wrong;
160
161
  /* If there is no program header, or the type is not a core file, then
162
     we are hosed.  */
163
10.9k
  if (i_ehdrp->e_phoff == 0 || i_ehdrp->e_type != ET_CORE)
164
1.84k
    goto wrong;
165
166
  /* Does BFD's idea of the phdr size match the size
167
     recorded in the file? */
168
9.07k
  if (i_ehdrp->e_phentsize != sizeof (Elf_External_Phdr))
169
12
    goto wrong;
170
171
  /* If the program header count is PN_XNUM(0xffff), the actual
172
     count is in the first section header.  */
173
9.06k
  if (i_ehdrp->e_shoff != 0 && i_ehdrp->e_phnum == PN_XNUM)
174
7
    {
175
7
      Elf_External_Shdr x_shdr;
176
7
      Elf_Internal_Shdr i_shdr;
177
7
      file_ptr where = (file_ptr) i_ehdrp->e_shoff;
178
179
7
      if (i_ehdrp->e_shoff < sizeof (x_ehdr))
180
0
  goto wrong;
181
182
      /* Seek to the section header table in the file.  */
183
7
      if (bfd_seek (abfd, where, SEEK_SET) != 0)
184
0
  goto fail;
185
186
      /* Read the first section header at index 0, and convert to internal
187
   form.  */
188
7
      if (bfd_read (&x_shdr, sizeof (x_shdr), abfd) != sizeof (x_shdr))
189
2
  goto fail;
190
5
      elf_swap_shdr_in (abfd, &x_shdr, &i_shdr);
191
192
5
      if (i_shdr.sh_info != 0)
193
5
  {
194
5
    i_ehdrp->e_phnum = i_shdr.sh_info;
195
5
    if (i_ehdrp->e_phnum != i_shdr.sh_info)
196
0
      goto wrong;
197
5
  }
198
5
    }
199
200
  /* Sanity check that we can read all of the program headers.
201
     It ought to be good enough to just read the last one.  */
202
9.05k
  if (i_ehdrp->e_phnum > 1)
203
8.95k
    {
204
8.95k
      Elf_External_Phdr x_phdr;
205
8.95k
      Elf_Internal_Phdr i_phdr;
206
8.95k
      file_ptr where;
207
208
      /* Check that we don't have a totally silly number of
209
   program headers.  */
210
8.95k
      if (i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (x_phdr)
211
8.95k
    || i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (i_phdr))
212
0
  goto wrong;
213
214
8.95k
      where = (file_ptr)(i_ehdrp->e_phoff + (i_ehdrp->e_phnum - 1) * sizeof (x_phdr));
215
8.95k
      if ((bfd_size_type) where <= i_ehdrp->e_phoff)
216
0
  goto wrong;
217
218
8.95k
      if (bfd_seek (abfd, where, SEEK_SET) != 0)
219
0
  goto fail;
220
8.95k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
221
16
  goto fail;
222
8.95k
    }
223
224
  /* Move to the start of the program headers.  */
225
9.04k
  if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0)
226
0
    goto wrong;
227
228
  /* Allocate space for the program headers.  */
229
9.04k
  amt = sizeof (*i_phdrp) * i_ehdrp->e_phnum;
230
9.04k
  i_phdrp = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
231
9.04k
  if (!i_phdrp)
232
0
    goto fail;
233
234
9.04k
  elf_tdata (abfd)->phdr = i_phdrp;
235
236
  /* Read and convert to internal form.  */
237
270k
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
238
261k
    {
239
261k
      Elf_External_Phdr x_phdr;
240
241
261k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
242
0
  goto fail;
243
244
261k
      elf_swap_phdr_in (abfd, &x_phdr, i_phdrp + phindex);
245
261k
    }
246
247
  /* Set the machine architecture.  Do this before processing the
248
     program headers since we need to know the architecture type
249
     when processing the notes of some systems' core files.  */
250
9.04k
  if (! bfd_default_set_arch_mach (abfd, ebd->arch, 0)
251
      /* It's OK if this fails for the generic target.  */
252
9.04k
      && ebd->elf_machine_code != EM_NONE)
253
0
    goto fail;
254
255
  /* Let the backend double check the format and override global
256
     information.  We do this before processing the program headers
257
     to allow the correct machine (as opposed to just the default
258
     machine) to be set, making it possible for grok_prstatus and
259
     grok_psinfo to rely on the mach setting.  */
260
9.04k
  if (ebd->elf_backend_object_p != NULL
261
9.04k
      && ! ebd->elf_backend_object_p (abfd))
262
1.10k
    goto wrong;
263
264
  /* Process each program header.  */
265
230k
  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
266
222k
    if (! bfd_section_from_phdr (abfd, i_phdrp + phindex, (int) phindex))
267
40
      goto fail;
268
269
  /* Check for core truncation.  */
270
7.89k
  filesize = bfd_get_file_size (abfd);
271
7.89k
  if (filesize != 0)
272
7.89k
    {
273
12.0k
      for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
274
11.9k
  {
275
11.9k
    Elf_Internal_Phdr *p = i_phdrp + phindex;
276
11.9k
    if (p->p_filesz
277
11.9k
        && (p->p_offset >= filesize
278
10.8k
      || p->p_filesz > filesize - p->p_offset))
279
7.78k
      {
280
7.78k
        _bfd_error_handler (_("warning: %pB has a segment "
281
7.78k
            "extending past end of file"), abfd);
282
7.78k
        abfd->read_only = 1;
283
7.78k
        break;
284
7.78k
      }
285
11.9k
      }
286
7.89k
  }
287
288
  /* Save the entry point from the ELF header.  */
289
7.89k
  abfd->start_address = i_ehdrp->e_entry;
290
7.89k
  return _bfd_no_cleanup;
291
292
1.14M
 wrong:
293
1.14M
  bfd_set_error (bfd_error_wrong_format);
294
1.14M
 fail:
295
1.14M
  return NULL;
296
1.14M
}
297
298
/* Attempt to find a build-id in a core file from the core file BFD.
299
   OFFSET is the file offset to a PT_LOAD segment that may contain
300
   the build-id note.  Returns TRUE upon success, FALSE otherwise.  */
301
302
bool
303
NAME(_bfd_elf, core_find_build_id)
304
  (bfd *abfd,
305
   bfd_vma offset)
306
1.56k
{
307
1.56k
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.   */
308
1.56k
  Elf_Internal_Ehdr i_ehdr; /* Elf file header, internal form.   */
309
1.56k
  Elf_Internal_Phdr *i_phdr;
310
1.56k
  unsigned int i;
311
1.56k
  size_t amt;
312
313
  /* Seek to the position of the segment at OFFSET.  */
314
1.56k
  if (bfd_seek (abfd, offset, SEEK_SET) != 0)
315
132
    goto fail;
316
317
  /* Read in the ELF header in external format.  */
318
1.43k
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
319
505
    {
320
505
      if (bfd_get_error () != bfd_error_system_call)
321
505
  goto wrong;
322
0
      else
323
0
  goto fail;
324
505
    }
325
326
  /* Now check to see if we have a valid ELF file, and one that BFD can
327
     make use of.  The magic number must match, the address size ('class')
328
     and byte-swapping must match our XVEC entry, and it must have a
329
     section header table (FIXME: See comments re sections at top of this
330
     file).  */
331
926
  if (! elf_file_p (&x_ehdr)
332
926
      || x_ehdr.e_ident[EI_VERSION] != EV_CURRENT
333
926
      || x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
334
661
    goto wrong;
335
336
  /* Check that file's byte order matches xvec's.  */
337
265
  switch (x_ehdr.e_ident[EI_DATA])
338
265
    {
339
208
    case ELFDATA2MSB:   /* Big-endian.  */
340
208
      if (! bfd_header_big_endian (abfd))
341
0
  goto wrong;
342
208
      break;
343
208
    case ELFDATA2LSB:   /* Little-endian.  */
344
57
      if (! bfd_header_little_endian (abfd))
345
0
  goto wrong;
346
57
      break;
347
57
    case ELFDATANONE:   /* No data encoding specified.  */
348
0
    default:      /* Unknown data encoding specified . */
349
0
      goto wrong;
350
265
    }
351
352
265
  elf_swap_ehdr_in (abfd, &x_ehdr, &i_ehdr);
353
#if DEBUG
354
  elf_debug_file (&i_ehdr);
355
#endif
356
357
265
  if (i_ehdr.e_phentsize != sizeof (Elf_External_Phdr) || i_ehdr.e_phnum == 0)
358
0
    goto fail;
359
360
  /* Read in program headers.  */
361
265
  if (_bfd_mul_overflow (i_ehdr.e_phnum, sizeof (*i_phdr), &amt))
362
0
    {
363
0
      bfd_set_error (bfd_error_file_too_big);
364
0
      goto fail;
365
0
    }
366
265
  i_phdr = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
367
265
  if (i_phdr == NULL)
368
0
    goto fail;
369
370
265
  if (bfd_seek (abfd, offset + i_ehdr.e_phoff, SEEK_SET) != 0)
371
0
    goto fail;
372
373
  /* Read in program headers and parse notes.  */
374
396k
  for (i = 0; i < i_ehdr.e_phnum; ++i, ++i_phdr)
375
395k
    {
376
395k
      Elf_External_Phdr x_phdr;
377
378
395k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
379
0
  goto fail;
380
395k
      elf_swap_phdr_in (abfd, &x_phdr, i_phdr);
381
382
395k
      if (i_phdr->p_type == PT_NOTE && i_phdr->p_filesz > 0)
383
38
  {
384
38
    elf_read_notes (abfd, offset + i_phdr->p_offset,
385
38
        i_phdr->p_filesz, i_phdr->p_align);
386
387
    /* Make sure ABFD returns to processing the program headers.  */
388
38
    if (bfd_seek (abfd,
389
38
      offset + i_ehdr.e_phoff + (i + 1) * sizeof (x_phdr),
390
38
      SEEK_SET) != 0)
391
0
      goto fail;
392
393
38
    if (abfd->build_id != NULL)
394
0
      return true;
395
38
  }
396
395k
    }
397
398
  /* Having gotten this far, we have a valid ELF section, but no
399
     build-id was found.  */
400
265
  goto fail;
401
402
1.16k
 wrong:
403
1.16k
  bfd_set_error (bfd_error_wrong_format);
404
1.56k
 fail:
405
1.56k
  return false;
406
1.16k
}
_bfd_elf64_core_find_build_id
Line
Count
Source
306
303
{
307
303
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.   */
308
303
  Elf_Internal_Ehdr i_ehdr; /* Elf file header, internal form.   */
309
303
  Elf_Internal_Phdr *i_phdr;
310
303
  unsigned int i;
311
303
  size_t amt;
312
313
  /* Seek to the position of the segment at OFFSET.  */
314
303
  if (bfd_seek (abfd, offset, SEEK_SET) != 0)
315
132
    goto fail;
316
317
  /* Read in the ELF header in external format.  */
318
171
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
319
64
    {
320
64
      if (bfd_get_error () != bfd_error_system_call)
321
64
  goto wrong;
322
0
      else
323
0
  goto fail;
324
64
    }
325
326
  /* Now check to see if we have a valid ELF file, and one that BFD can
327
     make use of.  The magic number must match, the address size ('class')
328
     and byte-swapping must match our XVEC entry, and it must have a
329
     section header table (FIXME: See comments re sections at top of this
330
     file).  */
331
107
  if (! elf_file_p (&x_ehdr)
332
107
      || x_ehdr.e_ident[EI_VERSION] != EV_CURRENT
333
107
      || x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
334
67
    goto wrong;
335
336
  /* Check that file's byte order matches xvec's.  */
337
40
  switch (x_ehdr.e_ident[EI_DATA])
338
40
    {
339
0
    case ELFDATA2MSB:   /* Big-endian.  */
340
0
      if (! bfd_header_big_endian (abfd))
341
0
  goto wrong;
342
0
      break;
343
40
    case ELFDATA2LSB:   /* Little-endian.  */
344
40
      if (! bfd_header_little_endian (abfd))
345
0
  goto wrong;
346
40
      break;
347
40
    case ELFDATANONE:   /* No data encoding specified.  */
348
0
    default:      /* Unknown data encoding specified . */
349
0
      goto wrong;
350
40
    }
351
352
40
  elf_swap_ehdr_in (abfd, &x_ehdr, &i_ehdr);
353
#if DEBUG
354
  elf_debug_file (&i_ehdr);
355
#endif
356
357
40
  if (i_ehdr.e_phentsize != sizeof (Elf_External_Phdr) || i_ehdr.e_phnum == 0)
358
0
    goto fail;
359
360
  /* Read in program headers.  */
361
40
  if (_bfd_mul_overflow (i_ehdr.e_phnum, sizeof (*i_phdr), &amt))
362
0
    {
363
0
      bfd_set_error (bfd_error_file_too_big);
364
0
      goto fail;
365
0
    }
366
40
  i_phdr = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
367
40
  if (i_phdr == NULL)
368
0
    goto fail;
369
370
40
  if (bfd_seek (abfd, offset + i_ehdr.e_phoff, SEEK_SET) != 0)
371
0
    goto fail;
372
373
  /* Read in program headers and parse notes.  */
374
864
  for (i = 0; i < i_ehdr.e_phnum; ++i, ++i_phdr)
375
824
    {
376
824
      Elf_External_Phdr x_phdr;
377
378
824
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
379
0
  goto fail;
380
824
      elf_swap_phdr_in (abfd, &x_phdr, i_phdr);
381
382
824
      if (i_phdr->p_type == PT_NOTE && i_phdr->p_filesz > 0)
383
14
  {
384
14
    elf_read_notes (abfd, offset + i_phdr->p_offset,
385
14
        i_phdr->p_filesz, i_phdr->p_align);
386
387
    /* Make sure ABFD returns to processing the program headers.  */
388
14
    if (bfd_seek (abfd,
389
14
      offset + i_ehdr.e_phoff + (i + 1) * sizeof (x_phdr),
390
14
      SEEK_SET) != 0)
391
0
      goto fail;
392
393
14
    if (abfd->build_id != NULL)
394
0
      return true;
395
14
  }
396
824
    }
397
398
  /* Having gotten this far, we have a valid ELF section, but no
399
     build-id was found.  */
400
40
  goto fail;
401
402
131
 wrong:
403
131
  bfd_set_error (bfd_error_wrong_format);
404
303
 fail:
405
303
  return false;
406
131
}
_bfd_elf32_core_find_build_id
Line
Count
Source
306
1.26k
{
307
1.26k
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form.   */
308
1.26k
  Elf_Internal_Ehdr i_ehdr; /* Elf file header, internal form.   */
309
1.26k
  Elf_Internal_Phdr *i_phdr;
310
1.26k
  unsigned int i;
311
1.26k
  size_t amt;
312
313
  /* Seek to the position of the segment at OFFSET.  */
314
1.26k
  if (bfd_seek (abfd, offset, SEEK_SET) != 0)
315
0
    goto fail;
316
317
  /* Read in the ELF header in external format.  */
318
1.26k
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
319
441
    {
320
441
      if (bfd_get_error () != bfd_error_system_call)
321
441
  goto wrong;
322
0
      else
323
0
  goto fail;
324
441
    }
325
326
  /* Now check to see if we have a valid ELF file, and one that BFD can
327
     make use of.  The magic number must match, the address size ('class')
328
     and byte-swapping must match our XVEC entry, and it must have a
329
     section header table (FIXME: See comments re sections at top of this
330
     file).  */
331
819
  if (! elf_file_p (&x_ehdr)
332
819
      || x_ehdr.e_ident[EI_VERSION] != EV_CURRENT
333
819
      || x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
334
594
    goto wrong;
335
336
  /* Check that file's byte order matches xvec's.  */
337
225
  switch (x_ehdr.e_ident[EI_DATA])
338
225
    {
339
208
    case ELFDATA2MSB:   /* Big-endian.  */
340
208
      if (! bfd_header_big_endian (abfd))
341
0
  goto wrong;
342
208
      break;
343
208
    case ELFDATA2LSB:   /* Little-endian.  */
344
17
      if (! bfd_header_little_endian (abfd))
345
0
  goto wrong;
346
17
      break;
347
17
    case ELFDATANONE:   /* No data encoding specified.  */
348
0
    default:      /* Unknown data encoding specified . */
349
0
      goto wrong;
350
225
    }
351
352
225
  elf_swap_ehdr_in (abfd, &x_ehdr, &i_ehdr);
353
#if DEBUG
354
  elf_debug_file (&i_ehdr);
355
#endif
356
357
225
  if (i_ehdr.e_phentsize != sizeof (Elf_External_Phdr) || i_ehdr.e_phnum == 0)
358
0
    goto fail;
359
360
  /* Read in program headers.  */
361
225
  if (_bfd_mul_overflow (i_ehdr.e_phnum, sizeof (*i_phdr), &amt))
362
0
    {
363
0
      bfd_set_error (bfd_error_file_too_big);
364
0
      goto fail;
365
0
    }
366
225
  i_phdr = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
367
225
  if (i_phdr == NULL)
368
0
    goto fail;
369
370
225
  if (bfd_seek (abfd, offset + i_ehdr.e_phoff, SEEK_SET) != 0)
371
0
    goto fail;
372
373
  /* Read in program headers and parse notes.  */
374
395k
  for (i = 0; i < i_ehdr.e_phnum; ++i, ++i_phdr)
375
394k
    {
376
394k
      Elf_External_Phdr x_phdr;
377
378
394k
      if (bfd_read (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
379
0
  goto fail;
380
394k
      elf_swap_phdr_in (abfd, &x_phdr, i_phdr);
381
382
394k
      if (i_phdr->p_type == PT_NOTE && i_phdr->p_filesz > 0)
383
24
  {
384
24
    elf_read_notes (abfd, offset + i_phdr->p_offset,
385
24
        i_phdr->p_filesz, i_phdr->p_align);
386
387
    /* Make sure ABFD returns to processing the program headers.  */
388
24
    if (bfd_seek (abfd,
389
24
      offset + i_ehdr.e_phoff + (i + 1) * sizeof (x_phdr),
390
24
      SEEK_SET) != 0)
391
0
      goto fail;
392
393
24
    if (abfd->build_id != NULL)
394
0
      return true;
395
24
  }
396
394k
    }
397
398
  /* Having gotten this far, we have a valid ELF section, but no
399
     build-id was found.  */
400
225
  goto fail;
401
402
1.03k
 wrong:
403
1.03k
  bfd_set_error (bfd_error_wrong_format);
404
1.26k
 fail:
405
1.26k
  return false;
406
1.03k
}