8 // Return the offset to a file section named 'lisp.core' if there is one
9 off_t
search_for_elf_core(int fd
)
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");
18 // Seek to and read the section header of the section header string table
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");
26 // Scan the section header string table
27 unsigned long result
= 0;
28 char * shstrtab_strbuf
= 0;
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
;
42 free(shstrtab_strbuf
);