Add printing functions for models to ease debugging
[centaur.git] / src / printing / sections.c
1 #include <stdio.h>
2
3 #include <libelf/libelf.h>
4 #include <libelf/gelf.h>
5
6 #include <libelfu/libelfu.h>
7 #include "printing.h"
8
9
10 void printSegmentsWithSection(Elf *e, Elf_Scn *scn)
11 {
12   GElf_Phdr phdr;
13   GElf_Shdr shdr;
14   int i;
15   size_t n;
16
17
18   if (gelf_getshdr(scn, &shdr) != &shdr) {
19     fprintf(stderr, "gelf_getshdr() failed: %s\n", elf_errmsg(-1));
20     return;
21   }
22
23   if (elf_getphdrnum(e, &n)) {
24     fprintf(stderr, "elf_getphdrnum() failed: %s\n", elf_errmsg(-1));
25     return;
26   }
27
28   for (i = 0; i < n; i++) {
29     if (gelf_getphdr(e, i, &phdr) != &phdr) {
30       fprintf(stderr, "getphdr() failed for #%d: %s\n", i, elf_errmsg(-1));
31       continue;
32     }
33
34     if (PHDR_CONTAINS_SCN_IN_MEMORY(&phdr, &shdr)) {
35       printf("     %d %s\n", i, segmentTypeStr(phdr.p_type));
36     }
37   }
38 }
39
40
41 void printSection(Elf *e, Elf_Scn *scn)
42 {
43   printf("  %jd: %s\n",
44           (uintmax_t) elf_ndxscn(scn),
45           elfu_eScnName(e, scn));
46 }
47
48
49 void printSections(Elf *e)
50 {
51   Elf_Scn *scn;
52
53   printf("Sections:\n");
54
55   scn = elf_getscn(e, 0);
56
57   while (scn) {
58     printSection(e, scn);
59     //printSegmentsWithSection(e, scn);
60
61     scn = elf_nextscn(e, scn);
62   }
63 }