diff options
author | norly <ny-git@enpas.org> | 2013-01-25 15:24:36 +0000 |
---|---|---|
committer | norly <ny-git@enpas.org> | 2013-02-11 01:24:36 +0000 |
commit | d9eb4398773cbda1dc185f4cf7b1b0e4cb9fb135 (patch) | |
tree | a2421aa140b17db64df439f894fa892833e3ac96 /src/printing/header.c | |
parent | 43e69328d849391abfed996214eed1dca49b3ac3 (diff) |
Print ELF header/segments/sections
Diffstat (limited to 'src/printing/header.c')
-rw-r--r-- | src/printing/header.c | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/src/printing/header.c b/src/printing/header.c new file mode 100644 index 0000000..33f7bdb --- /dev/null +++ b/src/printing/header.c @@ -0,0 +1,62 @@ +#include <stdio.h> + +#include <libelf.h> +#include <gelf.h> + + +void printHeader(Elf *e) +{ + GElf_Ehdr ehdr; + int elfclass; + size_t shdrnum, shdrstrndx, phdrnum; + + + printf("ELF header:\n"); + + + elfclass = gelf_getclass(e); + if (elfclass == ELFCLASSNONE) { + fprintf(stderr, "getclass() failed: %s\n", elf_errmsg(-1)); + } + printf(" * %d-bit ELF object\n", elfclass == ELFCLASS32 ? 32 : 64); + + + if (!gelf_getehdr(e, &ehdr)) { + fprintf(stderr, "getehdr() failed: %s.", elf_errmsg(-1)); + return; + } + + printf(" * type machine version entry phoff shoff flags ehsize phentsize shentsize\n"); + printf(" %8jx %8jx %8jx %8jx %8jx %8jx %8jx %8jx %9jx %9jx\n", + (uintmax_t) ehdr.e_type, + (uintmax_t) ehdr.e_machine, + (uintmax_t) ehdr.e_version, + (uintmax_t) ehdr.e_entry, + (uintmax_t) ehdr.e_phoff, + (uintmax_t) ehdr.e_shoff, + (uintmax_t) ehdr.e_flags, + (uintmax_t) ehdr.e_ehsize, + (uintmax_t) ehdr.e_phentsize, + (uintmax_t) ehdr.e_shentsize); + + + if (elf_getshdrnum(e, &shdrnum) != 0) { + fprintf(stderr, "getshdrnum() failed: %s\n", elf_errmsg(-1)); + } else { + printf(" * (shnum): %u\n", shdrnum); + } + + if (elf_getshdrstrndx(e, &shdrstrndx) != 0) { + fprintf(stderr, "getshdrstrndx() failed: %s\n", elf_errmsg(-1)); + } else { + printf(" * (shstrndx): %u\n", shdrstrndx); + } + + if (elf_getphdrnum(e, &phdrnum) != 0) { + fprintf(stderr, "getphdrnum() failed: %s\n", elf_errmsg(-1)); + } else { + printf(" * (phnum): %u\n", phdrnum); + } + + printf("\n"); +} |