Skip to content

Instantly share code, notes, and snippets.

@jasonnyberg
Created March 23, 2018 20:05
Show Gist options
  • Select an option

  • Save jasonnyberg/d26ba8efe4fb62e75a30b046eddb85e9 to your computer and use it in GitHub Desktop.

Select an option

Save jasonnyberg/d26ba8efe4fb62e75a30b046eddb85e9 to your computer and use it in GitHub Desktop.
Get debuginfo link filename (debian stretch) from shared lib
#include <libelf.h>
#include <elfutils/libdwelf.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include "util.h"
char *get_debug(char *filename)
{
char *b=mymalloc(256);
if ( elf_version ( EV_CURRENT ) != EV_NONE ) {
int fd=open(filename,O_RDONLY);
if (fd) {
Elf *elf=elf_begin(fd,ELF_C_READ,NULL);
if (elf) {
GElf_Word crc;
const void *buildid;
const char *debuglink=dwelf_elf_gnu_debuglink(elf,&crc);
if (debuglink) {
ssize_t idlen=dwelf_elf_gnu_build_id(elf,&buildid);
//Dwarf *dwarf=dwarf_begin(fd,DWARF_C_READ);
Dwarf *dwarf=dwarf_begin_elf(elf,DWARF_C_READ,NULL);
if (dwarf) {
const char *altname;
int idlen=dwelf_dwarf_gnu_debugaltlink(dwarf,&altname,&buildid);
dwarf_end(dwarf);
}
char *buf=b;
buf+=sprintf(buf,"%02x",*((unsigned char *) buildid++));
buf+=sprintf(buf,"/");
while (--idlen)
buf+=sprintf(buf,"%02x",*((unsigned char *) buildid++));
buf+=sprintf(buf,".debug");
}
elf_end(elf);
}
close(fd);
}
}
return b;
}
int main(int argc,char *argv[])
{
char *debugname=get_debug(argv[1]);
printf("%s\n",debugname);
}
@jasonnyberg
Copy link
Author

utils.h is only used for mymalloc... Replace w/whatever.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment