]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elfcore.h
Update the config.guess and config.sub files from the master repository and regenerat...
[thirdparty/binutils-gdb.git] / bfd / elfcore.h
CommitLineData
252b5132 1/* ELF core file support for BFD.
a2c58332 2 Copyright (C) 1995-2022 Free Software Foundation, Inc.
252b5132 3
3193e234 4 This file is part of BFD, the Binary File Descriptor library.
252b5132 5
3193e234
NC
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
cd123cb7 8 the Free Software Foundation; either version 3 of the License, or
3193e234 9 (at your option) any later version.
252b5132 10
3193e234
NC
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.
252b5132 15
3193e234
NC
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
cd123cb7
NC
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
252b5132 20
252b5132 21char*
268b6b39 22elf_core_file_failing_command (bfd *abfd)
252b5132 23{
228e534f 24 return elf_tdata (abfd)->core->command;
252b5132
RH
25}
26
27int
268b6b39 28elf_core_file_failing_signal (bfd *abfd)
252b5132 29{
228e534f 30 return elf_tdata (abfd)->core->signal;
252b5132
RH
31}
32
261b8d08
PA
33int
34elf_core_file_pid (bfd *abfd)
35{
228e534f 36 return elf_tdata (abfd)->core->pid;
261b8d08
PA
37}
38
0a1b45a2 39bool
268b6b39 40elf_core_file_matches_executable_p (bfd *core_bfd, bfd *exec_bfd)
252b5132
RH
41{
42 char* corename;
43
3e932841 44 /* xvecs must match if both are ELF files for the same target. */
252b5132
RH
45
46 if (core_bfd->xvec != exec_bfd->xvec)
47 {
48 bfd_set_error (bfd_error_system_call);
0a1b45a2 49 return false;
252b5132
RH
50 }
51
864619bb
KS
52 /* If both BFDs have identical build-ids, then they match. */
53 if (core_bfd->build_id != NULL
54 && exec_bfd->build_id != NULL
55 && core_bfd->build_id->size == exec_bfd->build_id->size
56 && memcmp (core_bfd->build_id->data, exec_bfd->build_id->data,
57 core_bfd->build_id->size) == 0)
0a1b45a2 58 return true;
864619bb 59
3e932841 60 /* See if the name in the corefile matches the executable name. */
228e534f 61 corename = elf_tdata (core_bfd)->core->program;
252b5132
RH
62 if (corename != NULL)
63 {
765cf5f6 64 const char* execname = strrchr (bfd_get_filename (exec_bfd), '/');
3193e234 65
765cf5f6 66 execname = execname ? execname + 1 : bfd_get_filename (exec_bfd);
252b5132 67
268b6b39 68 if (strcmp (execname, corename) != 0)
0a1b45a2 69 return false;
252b5132
RH
70 }
71
0a1b45a2 72 return true;
252b5132
RH
73}
74
252b5132
RH
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
3193e234 84 floating point registers (.reg2). */
252b5132 85
cb001c0d 86bfd_cleanup
268b6b39 87elf_core_file_p (bfd *abfd)
252b5132 88{
3193e234
NC
89 Elf_External_Ehdr x_ehdr; /* Elf file header, external form. */
90 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */
91 Elf_Internal_Phdr *i_phdrp; /* Elf program header, internal form. */
252b5132 92 unsigned int phindex;
9c5bfbb7 93 const struct elf_backend_data *ebd;
dc810e39 94 bfd_size_type amt;
c45c3dba 95 ufile_ptr filesize;
252b5132
RH
96
97 /* Read in the ELF header in external format. */
268b6b39 98 if (bfd_bread (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
252b5132
RH
99 {
100 if (bfd_get_error () != bfd_error_system_call)
a7f84125
AM
101 goto wrong;
102 else
103 goto fail;
252b5132
RH
104 }
105
3e932841 106 /* Check the magic number. */
82e51918 107 if (! elf_file_p (&x_ehdr))
60bcf0fa 108 goto wrong;
252b5132 109
3193e234 110 /* FIXME: Check EI_VERSION here ! */
252b5132 111
3e932841 112 /* Check the address size ("class"). */
252b5132
RH
113 if (x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
114 goto wrong;
115
3e932841 116 /* Check the byteorder. */
252b5132
RH
117 switch (x_ehdr.e_ident[EI_DATA])
118 {
3193e234 119 case ELFDATA2MSB: /* Big-endian. */
252b5132
RH
120 if (! bfd_big_endian (abfd))
121 goto wrong;
122 break;
3193e234 123 case ELFDATA2LSB: /* Little-endian. */
252b5132
RH
124 if (! bfd_little_endian (abfd))
125 goto wrong;
126 break;
127 default:
128 goto wrong;
129 }
130
0c83546a
AM
131 /* Give abfd an elf_obj_tdata. */
132 if (! (*abfd->xvec->_bfd_set_format[bfd_core]) (abfd))
133 goto fail;
a7f84125 134
3e932841 135 /* Swap in the rest of the header, now that we have the byte order. */
252b5132
RH
136 i_ehdrp = elf_elfheader (abfd);
137 elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
138
139#if DEBUG & 1
140 elf_debug_file (i_ehdrp);
141#endif
142
143 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 if (ebd->elf_machine_code != i_ehdrp->e_machine
149 && (ebd->elf_machine_alt1 == 0
150 || i_ehdrp->e_machine != ebd->elf_machine_alt1)
151 && (ebd->elf_machine_alt2 == 0
152 || i_ehdrp->e_machine != ebd->elf_machine_alt2))
153 {
154 const bfd_target * const *target_ptr;
155
156 if (ebd->elf_machine_code != EM_NONE)
157 goto wrong;
158
159 /* This is the generic ELF target. Let it match any ELF target
160 for which we do not have a specific backend. */
161
162 for (target_ptr = bfd_target_vector; *target_ptr != NULL; target_ptr++)
163 {
9c5bfbb7 164 const struct elf_backend_data *back;
252b5132
RH
165
166 if ((*target_ptr)->flavour != bfd_target_elf_flavour)
167 continue;
f7231afc 168 back = xvec_get_elf_backend_data (*target_ptr);
11a7ae4f
AM
169 if (back->s->arch_size != ARCH_SIZE)
170 continue;
3193e234
NC
171 if (back->elf_machine_code == i_ehdrp->e_machine
172 || (back->elf_machine_alt1 != 0
07d6d2b8 173 && i_ehdrp->e_machine == back->elf_machine_alt1)
3193e234 174 || (back->elf_machine_alt2 != 0
07d6d2b8 175 && i_ehdrp->e_machine == back->elf_machine_alt2))
252b5132
RH
176 {
177 /* target_ptr is an ELF backend which matches this
178 object file, so reject the generic ELF target. */
179 goto wrong;
180 }
181 }
182 }
183
184 /* If there is no program header, or the type is not a core file, then
3e932841 185 we are hosed. */
252b5132
RH
186 if (i_ehdrp->e_phoff == 0 || i_ehdrp->e_type != ET_CORE)
187 goto wrong;
188
189 /* Does BFD's idea of the phdr size match the size
190 recorded in the file? */
191 if (i_ehdrp->e_phentsize != sizeof (Elf_External_Phdr))
192 goto wrong;
193
ecd12bc1
AM
194 /* If the program header count is PN_XNUM(0xffff), the actual
195 count is in the first section header. */
196 if (i_ehdrp->e_shoff != 0 && i_ehdrp->e_phnum == PN_XNUM)
197 {
198 Elf_External_Shdr x_shdr;
199 Elf_Internal_Shdr i_shdr;
b32a5c16 200 file_ptr where = (file_ptr) i_ehdrp->e_shoff;
ecd12bc1
AM
201
202 /* Seek to the section header table in the file. */
b32a5c16 203 if (bfd_seek (abfd, where, SEEK_SET) != 0)
ecd12bc1
AM
204 goto fail;
205
206 /* Read the first section header at index 0, and convert to internal
207 form. */
208 if (bfd_bread (&x_shdr, sizeof (x_shdr), abfd) != sizeof (x_shdr))
209 goto fail;
210 elf_swap_shdr_in (abfd, &x_shdr, &i_shdr);
211
212 if (i_shdr.sh_info != 0)
213 {
214 i_ehdrp->e_phnum = i_shdr.sh_info;
215 if (i_ehdrp->e_phnum != i_shdr.sh_info)
216 goto wrong;
217 }
218 }
219
220 /* Sanity check that we can read all of the program headers.
221 It ought to be good enough to just read the last one. */
222 if (i_ehdrp->e_phnum > 1)
223 {
224 Elf_External_Phdr x_phdr;
225 Elf_Internal_Phdr i_phdr;
b32a5c16 226 file_ptr where;
ecd12bc1
AM
227
228 /* Check that we don't have a totally silly number of
229 program headers. */
230 if (i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (x_phdr)
231 || i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (i_phdr))
232 goto wrong;
233
b32a5c16 234 where = (file_ptr)(i_ehdrp->e_phoff + (i_ehdrp->e_phnum - 1) * sizeof (x_phdr));
ecd12bc1
AM
235 if ((bfd_size_type) where <= i_ehdrp->e_phoff)
236 goto wrong;
237
b32a5c16 238 if (bfd_seek (abfd, where, SEEK_SET) != 0)
ecd12bc1
AM
239 goto fail;
240 if (bfd_bread (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
241 goto fail;
242 }
243
d20966a7 244 /* Move to the start of the program headers. */
dc810e39 245 if (bfd_seek (abfd, (file_ptr) i_ehdrp->e_phoff, SEEK_SET) != 0)
d20966a7 246 goto wrong;
3e932841
KH
247
248 /* Allocate space for the program headers. */
dc810e39 249 amt = sizeof (*i_phdrp) * i_ehdrp->e_phnum;
a50b1753 250 i_phdrp = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
252b5132 251 if (!i_phdrp)
0ab2f69a 252 goto fail;
252b5132
RH
253
254 elf_tdata (abfd)->phdr = i_phdrp;
255
3e932841 256 /* Read and convert to internal form. */
252b5132
RH
257 for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
258 {
259 Elf_External_Phdr x_phdr;
3193e234 260
268b6b39 261 if (bfd_bread (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
0ab2f69a 262 goto fail;
252b5132
RH
263
264 elf_swap_phdr_in (abfd, &x_phdr, i_phdrp + phindex);
265 }
266
702248bb
JT
267 /* Set the machine architecture. Do this before processing the
268 program headers since we need to know the architecture type
269 when processing the notes of some systems' core files. */
5cba516c 270 if (! bfd_default_set_arch_mach (abfd, ebd->arch, 0)
252b5132 271 /* It's OK if this fails for the generic target. */
5cba516c
NC
272 && ebd->elf_machine_code != EM_NONE)
273 goto fail;
274
275 /* Let the backend double check the format and override global
276 information. We do this before processing the program headers
277 to allow the correct machine (as opposed to just the default
278 machine) to be set, making it possible for grok_prstatus and
279 grok_psinfo to rely on the mach setting. */
280 if (ebd->elf_backend_object_p != NULL
281 && ! ebd->elf_backend_object_p (abfd))
282 goto wrong;
252b5132 283
a94cef6a
JT
284 /* Process each program header. */
285 for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
3193e234
NC
286 if (! bfd_section_from_phdr (abfd, i_phdrp + phindex, (int) phindex))
287 goto fail;
a94cef6a 288
536d0ff4 289 /* Check for core truncation. */
c45c3dba
AM
290 filesize = bfd_get_file_size (abfd);
291 if (filesize != 0)
292 {
293 for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
294 {
295 Elf_Internal_Phdr *p = i_phdrp + phindex;
296 if (p->p_filesz
297 && (p->p_offset >= filesize
298 || p->p_filesz > filesize - p->p_offset))
299 {
300 _bfd_error_handler (_("warning: %pB has a segment "
301 "extending past end of file"), abfd);
302 abfd->read_only = 1;
303 break;
304 }
536d0ff4
AM
305 }
306 }
68ffbac6 307
3e932841 308 /* Save the entry point from the ELF header. */
ed48ec2e 309 abfd->start_address = i_ehdrp->e_entry;
cb001c0d 310 return _bfd_no_cleanup;
0ab2f69a 311
dc1e8a47 312 wrong:
0ab2f69a 313 bfd_set_error (bfd_error_wrong_format);
dc1e8a47 314 fail:
0ab2f69a 315 return NULL;
252b5132 316}
864619bb
KS
317
318/* Attempt to find a build-id in a core file from the core file BFD.
319 OFFSET is the file offset to a PT_LOAD segment that may contain
320 the build-id note. Returns TRUE upon success, FALSE otherwise. */
321
0a1b45a2 322bool
864619bb
KS
323NAME(_bfd_elf, core_find_build_id)
324 (bfd *abfd,
325 bfd_vma offset)
326{
327 Elf_External_Ehdr x_ehdr; /* Elf file header, external form. */
328 Elf_Internal_Ehdr i_ehdr; /* Elf file header, internal form. */
329 Elf_Internal_Phdr *i_phdr;
330 unsigned int i;
1f4361a7 331 size_t amt;
864619bb
KS
332
333 /* Seek to the position of the segment at OFFSET. */
334 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
335 goto fail;
336
337 /* Read in the ELF header in external format. */
338 if (bfd_bread (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
339 {
340 if (bfd_get_error () != bfd_error_system_call)
341 goto wrong;
342 else
343 goto fail;
344 }
345
346 /* Now check to see if we have a valid ELF file, and one that BFD can
347 make use of. The magic number must match, the address size ('class')
348 and byte-swapping must match our XVEC entry, and it must have a
349 section header table (FIXME: See comments re sections at top of this
350 file). */
351 if (! elf_file_p (&x_ehdr)
352 || x_ehdr.e_ident[EI_VERSION] != EV_CURRENT
353 || x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
354 goto wrong;
355
356 /* Check that file's byte order matches xvec's. */
357 switch (x_ehdr.e_ident[EI_DATA])
358 {
359 case ELFDATA2MSB: /* Big-endian. */
360 if (! bfd_header_big_endian (abfd))
361 goto wrong;
362 break;
363 case ELFDATA2LSB: /* Little-endian. */
364 if (! bfd_header_little_endian (abfd))
365 goto wrong;
366 break;
367 case ELFDATANONE: /* No data encoding specified. */
368 default: /* Unknown data encoding specified . */
369 goto wrong;
370 }
371
372 elf_swap_ehdr_in (abfd, &x_ehdr, &i_ehdr);
373#if DEBUG
374 elf_debug_file (&i_ehdr);
375#endif
376
377 if (i_ehdr.e_phentsize != sizeof (Elf_External_Phdr) || i_ehdr.e_phnum == 0)
378 goto fail;
379
380 /* Read in program headers. */
1f4361a7
AM
381 if (_bfd_mul_overflow (i_ehdr.e_phnum, sizeof (*i_phdr), &amt))
382 {
383 bfd_set_error (bfd_error_file_too_big);
384 goto fail;
385 }
386 i_phdr = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
864619bb
KS
387 if (i_phdr == NULL)
388 goto fail;
389
390 if (bfd_seek (abfd, (file_ptr) (offset + i_ehdr.e_phoff), SEEK_SET) != 0)
391 goto fail;
392
393 /* Read in program headers and parse notes. */
394 for (i = 0; i < i_ehdr.e_phnum; ++i, ++i_phdr)
395 {
396 Elf_External_Phdr x_phdr;
397
398 if (bfd_bread (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
399 goto fail;
400 elf_swap_phdr_in (abfd, &x_phdr, i_phdr);
401
402 if (i_phdr->p_type == PT_NOTE && i_phdr->p_filesz > 0)
403 {
404 elf_read_notes (abfd, offset + i_phdr->p_offset,
405 i_phdr->p_filesz, i_phdr->p_align);
ff5b3e14
KS
406
407 /* Make sure ABFD returns to processing the program headers. */
408 if (bfd_seek (abfd, (file_ptr) (offset + i_ehdr.e_phoff
409 + (i + 1) * sizeof (x_phdr)),
410 SEEK_SET) != 0)
411 goto fail;
412
864619bb 413 if (abfd->build_id != NULL)
0a1b45a2 414 return true;
864619bb
KS
415 }
416 }
417
418 /* Having gotten this far, we have a valid ELF section, but no
419 build-id was found. */
420 goto fail;
421
dc1e8a47 422 wrong:
864619bb 423 bfd_set_error (bfd_error_wrong_format);
dc1e8a47 424 fail:
0a1b45a2 425 return false;
864619bb 426}