Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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>
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 *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,
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