Better handling of children deletion in delete-lambda.
[sbcl.git] / src / runtime / elf.c
blobbedd5b3f5ea09080f09f53155a19f1ea50099ca1
1 #include <string.h>
2 #include <fcntl.h>
3 #include <unistd.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <elf.h>
8 // Return the offset to a file section named 'lisp.core' if there is one
9 off_t search_for_elf_core(int fd)
11 Elf64_Ehdr ehdr;
13 if (lseek(fd, 0, SEEK_SET) != 0 ||
14 read(fd, &ehdr, sizeof ehdr) != sizeof ehdr) {
15 fprintf(stderr, "failed to read elf header\n");
16 return 0;
18 // Seek to and read the section header of the section header string table
19 Elf64_Shdr shdr;
20 off_t offset = ehdr.e_shoff + ehdr.e_shstrndx * ehdr.e_shentsize;
21 if (lseek(fd, offset, SEEK_SET) != offset ||
22 read(fd, &shdr, sizeof shdr) != sizeof shdr) {
23 fprintf(stderr, "read failed\n");
24 return 0;
26 // Scan the section header string table
27 unsigned long result = 0;
28 char * shstrtab_strbuf = 0;
29 int i;
30 if ((shstrtab_strbuf = malloc(shdr.sh_size)) == NULL ||
31 lseek(fd, shdr.sh_offset, SEEK_SET) != (Elf64_Sxword)shdr.sh_offset ||
32 read(fd, shstrtab_strbuf, shdr.sh_size) != (Elf64_Sxword)shdr.sh_size ||
33 lseek(fd, ehdr.e_shoff, SEEK_SET) != (Elf64_Sxword)ehdr.e_shoff)
34 fprintf(stderr, "read failed\n");
35 else for(i=0;i<ehdr.e_shnum;++i)
36 if (read(fd, &shdr, sizeof shdr) == sizeof shdr
37 && !strcmp(&shstrtab_strbuf[shdr.sh_name], "lisp.core")) {
38 result = shdr.sh_offset;
39 break;
41 if (shstrtab_strbuf)
42 free(shstrtab_strbuf);
43 return result;