summaryrefslogtreecommitdiff
path: root/src/printing
diff options
context:
space:
mode:
Diffstat (limited to 'src/printing')
-rw-r--r--src/printing/header.c62
-rw-r--r--src/printing/sections.c60
-rw-r--r--src/printing/segments.c76
3 files changed, 198 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");
+}
diff --git a/src/printing/sections.c b/src/printing/sections.c
new file mode 100644
index 0000000..6241a73
--- /dev/null
+++ b/src/printing/sections.c
@@ -0,0 +1,60 @@
+#include <stdio.h>
+
+#include <libelf.h>
+#include <gelf.h>
+
+#include <libelfu/libelfu.h>
+#include "printing.h"
+
+
+void printSegmentsWithSection(Elf *e, Elf_Scn *scn)
+{
+ GElf_Phdr phdr;
+ int i;
+ size_t n;
+
+
+ if (elf_getphdrnum(e, &n)) {
+ fprintf(stderr, "elf_getphdrnum() failed: %s\n", elf_errmsg(-1));
+ return;
+ }
+
+ for (i = 0; i < n; i++) {
+ ELFU_BOOL isInSeg;
+
+ if (gelf_getphdr(e, i, &phdr) != &phdr) {
+ fprintf(stderr, "getphdr() failed for #%d: %s\n", i, elf_errmsg(-1));
+ continue;
+ }
+
+ isInSeg = elfu_segmentContainsSection(&phdr, scn);
+ if (isInSeg == ELFU_TRUE) {
+ printf(" %d %s\n", i, segmentTypeStr(phdr.p_type));
+ }
+ }
+}
+
+
+void printSection(Elf *e, Elf_Scn *scn)
+{
+ printf(" %jd: %s\n",
+ (uintmax_t) elf_ndxscn(scn),
+ elfu_sectionName(e, scn));
+}
+
+
+void printSections(Elf *e)
+{
+ Elf_Scn *scn;
+
+ printf("Sections:\n");
+
+ scn = elf_getscn(e, 0);
+
+ while (scn) {
+ printSection(e, scn);
+ //printSegmentsWithSection(e, scn);
+
+ scn = elf_nextscn(e, scn);
+ }
+}
diff --git a/src/printing/segments.c b/src/printing/segments.c
new file mode 100644
index 0000000..6d8cac7
--- /dev/null
+++ b/src/printing/segments.c
@@ -0,0 +1,76 @@
+#include <stdio.h>
+
+#include <libelf.h>
+#include <gelf.h>
+
+#include <libelfu/libelfu.h>
+
+
+static char *ptstr[] = {"NULL", "LOAD", "DYNAMIC", "INTERP", "NOTE", "SHLIB", "PHDR", "TLS", "NUM"};
+
+
+char* segmentTypeStr(size_t pt)
+{
+ if (pt >= 0 && pt <= PT_NUM) {
+ return ptstr[pt];
+ }
+
+ return "-?-";
+}
+
+
+void printSectionsInSegment(Elf *e, GElf_Phdr *phdr)
+{
+ Elf_Scn *scn;
+
+ scn = elf_getscn(e, 0);
+
+ while (scn) {
+ ELFU_BOOL isInSeg;
+
+ isInSeg = elfu_segmentContainsSection(phdr, scn);
+ if (isInSeg == ELFU_TRUE) {
+ printf(" %10u %s\n", elf_ndxscn(scn), elfu_sectionName(e, scn));
+ }
+
+ scn = elf_nextscn(e, scn);
+ }
+}
+
+
+void printSegments(Elf *e)
+{
+ size_t i, n;
+
+ if (elf_getphdrnum(e, &n)) {
+ fprintf(stderr, "elf_getphdrnum() failed: %s.", elf_errmsg(-1));
+ }
+
+ printf("Segments:\n");
+ printf(" # typeStr type offset vaddr paddr filesz memsz flags align\n");
+
+ for (i = 0; i < n; i++) {
+ GElf_Phdr phdr;
+
+ if (gelf_getphdr(e, i, &phdr) != &phdr) {
+ fprintf(stderr, "getphdr() failed for #%d: %s.", i, elf_errmsg(-1));
+ continue;
+ }
+
+ printf(" * %4d: %8s ", i, segmentTypeStr(phdr.p_type));
+
+ printf("%8jx %8jx %8jx %8jx %8jx %8jx %8jx %8jx\n",
+ (uintmax_t) phdr.p_type,
+ (uintmax_t) phdr.p_offset,
+ (uintmax_t) phdr.p_vaddr,
+ (uintmax_t) phdr.p_paddr,
+ (uintmax_t) phdr.p_filesz,
+ (uintmax_t) phdr.p_memsz,
+ (uintmax_t) phdr.p_flags,
+ (uintmax_t) phdr.p_align);
+
+ printSectionsInSegment(e, &phdr);
+ }
+
+ printf("\n");
+}