Skip to content

Instantly share code, notes, and snippets.

@bluca
Created March 26, 2021 15:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bluca/2010f84601ae5ad0f2e097fcc6700896 to your computer and use it in GitHub Desktop.
Save bluca/2010f84601ae5ad0f2e097fcc6700896 to your computer and use it in GitHub Desktop.
dwfl crash
#include <dwarf.h>
#include <elfutils/libdwelf.h>
#include <elfutils/libdwfl.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <assert.h>
#include <errno.h>
static int module_callback (Dwfl_Module *mod, void **userdata, const char *name, Dwarf_Addr start, void *arg)
{
const unsigned char *id;
GElf_Addr id_vaddr;
int id_len;
assert(mod);
id_len = dwfl_module_build_id(mod, &id, &id_vaddr);
printf("build-id of size %d\n", id_len);
GElf_Addr *bias = NULL;
Elf *elf = dwfl_module_getelf(mod, bias);
if (!elf) {
printf("elf_begin() failed: %s\n", elf_errmsg(-1));
return DWARF_CB_ABORT;
}
return DWARF_CB_OK;
}
int main(int argc, char **argv) {
static const Dwfl_Callbacks callbacks = {
.find_elf = dwfl_build_id_find_elf,
.section_address = dwfl_offline_section_address,
.find_debuginfo = dwfl_standard_find_debuginfo,
};
Dwfl *dwfl = NULL;
Elf *elf = NULL;
int r;
int fd;
assert(argc > 1);
fd = open(argv[1], O_CLOEXEC|O_RDONLY);
if (fd < 0)
return -1;
elf_version(EV_CURRENT);
elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
if (!elf) {
r = -EINVAL;
goto finish;
}
dwfl = dwfl_begin(&callbacks);
if (!dwfl) {
r = -EINVAL;
goto finish;
}
if (dwfl_core_file_report(dwfl, elf, "/usr/bin/sleep") < 0) {
r = -EINVAL;
goto finish;
}
if (dwfl_report_end(dwfl, NULL, NULL) != 0) {
r = -EINVAL;
goto finish;
}
if (dwfl_getmodules(dwfl, &module_callback, NULL, 0) < 0) {
r = -EINVAL;
goto finish;
}
r = 0;
finish:
if (dwfl)
dwfl_end(dwfl);
if (elf)
elf_end(elf);
close(fd);
return r;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment