Skip to content

Instantly share code, notes, and snippets.

@lovasko
Last active August 29, 2015 14:01
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 lovasko/1e7f9a55b2fac0c687b5 to your computer and use it in GitHub Desktop.
Save lovasko/1e7f9a55b2fac0c687b5 to your computer and use it in GitHub Desktop.
Listing of the ELF sections and matching for the .SUNW_ctf
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <libelf.h>
#include <gelf.h>
int
main (int argc, char **argv)
{
(void) argc;
char *filename = argv[1];
int fd = open(filename, O_RDONLY);
struct stat elf_stats;
fstat(fd, &elf_stats);
char *base_ptr = (char*)malloc(elf_stats.st_size);
read(fd, base_ptr, elf_stats.st_size);
Elf32_Ehdr *elf_header = (Elf32_Ehdr *)base_ptr;
elf_version(EV_CURRENT);
Elf *elf = elf_begin(fd, ELF_C_READ, NULL);
Elf_Scn *section = NULL;
GElf_Shdr section_header;
while ((section = elf_nextscn(elf, section)) != 0)
{
gelf_getshdr(section, &section_header);
char *section_name = elf_strptr(elf, elf_header->e_shstrndx,
section_header.sh_name);
printf("%s\n", section_name);
if (strcmp(section_name, ".SUNW_ctf") == 0)
{
Elf_Data *data = elf_getdata(section, NULL);
for (unsigned int i = 0; i < data->d_size; i++)
printf("%c", ((char*)data->d_buf)[i]);
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment