Add printing functions for models to ease debugging
[centaur.git] / src / printing / header.c
1 #include <stdio.h>
2
3 #include <libelf/libelf.h>
4 #include <libelf/gelf.h>
5
6
7 void printHeader(Elf *e)
8 {
9   GElf_Ehdr ehdr;
10   int elfclass;
11   size_t shdrnum, shdrstrndx, phdrnum;
12
13
14   printf("ELF header:\n");
15
16
17   elfclass = gelf_getclass(e);
18   if (elfclass == ELFCLASSNONE) {
19     fprintf(stderr, "getclass() failed: %s\n", elf_errmsg(-1));
20   }
21   printf(" * %d-bit ELF object\n", elfclass == ELFCLASS32 ? 32 : 64);
22
23
24   if (!gelf_getehdr(e, &ehdr)) {
25     fprintf(stderr, "getehdr() failed: %s.", elf_errmsg(-1));
26     return;
27   }
28
29   printf(" *     type  machine  version    entry    phoff    shoff    flags   ehsize phentsize shentsize\n");
30   printf("   %8jx %8jx %8jx %8jx %8jx %8jx %8jx %8jx %9jx %9jx\n",
31           (uintmax_t) ehdr.e_type,
32           (uintmax_t) ehdr.e_machine,
33           (uintmax_t) ehdr.e_version,
34           (uintmax_t) ehdr.e_entry,
35           (uintmax_t) ehdr.e_phoff,
36           (uintmax_t) ehdr.e_shoff,
37           (uintmax_t) ehdr.e_flags,
38           (uintmax_t) ehdr.e_ehsize,
39           (uintmax_t) ehdr.e_phentsize,
40           (uintmax_t) ehdr.e_shentsize);
41
42
43   if (elf_getshdrnum(e, &shdrnum) != 0) {
44     fprintf(stderr, "getshdrnum() failed: %s\n", elf_errmsg(-1));
45   } else {
46     printf(" * (shnum):    %u\n", shdrnum);
47   }
48
49   if (elf_getshdrstrndx(e, &shdrstrndx) != 0) {
50     fprintf(stderr, "getshdrstrndx() failed: %s\n", elf_errmsg(-1));
51   } else {
52     printf(" * (shstrndx): %u\n", shdrstrndx);
53   }
54
55   if (elf_getphdrnum(e, &phdrnum) != 0) {
56     fprintf(stderr, "getphdrnum() failed: %s\n", elf_errmsg(-1));
57   } else {
58     printf(" * (phnum):    %u\n", phdrnum);
59   }
60
61   printf("\n");
62 }