Created
March 23, 2018 20:05
-
-
Save jasonnyberg/d26ba8efe4fb62e75a30b046eddb85e9 to your computer and use it in GitHub Desktop.
Get debuginfo link filename (debian stretch) from shared lib
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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); | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
utils.h is only used for mymalloc... Replace w/whatever.