5234befa1c9fa0d9a22e9c1d6cc5aa430409d9c3
[centaur.git] / src / model / check.c
1 #include <assert.h>
2 #include <stdlib.h>
3 #include <sys/types.h>
4 #include <libelfu/libelfu.h>
5
6
7 int elfu_mCheck(ElfuElf *me)
8 {
9   size_t numSecs;
10   ElfuScn **sortedSecs;
11   size_t i;
12
13   sortedSecs = elfu_mScnSortedByOffset(me, &numSecs);
14   if (!sortedSecs) {
15     return -1;
16   }
17
18
19   /* Check for overlapping sections */
20   for (i = 0; i < numSecs - 1; i++) {
21     if (sortedSecs[i]->shdr.sh_offset + SCNFILESIZE(&sortedSecs[i]->shdr)
22         > sortedSecs[i+1]->shdr.sh_offset) {
23       ELFU_WARN("elfu_check: Found overlapping sections: %s and %s.\n",
24                 elfu_mScnName(me, sortedSecs[i]),
25                 elfu_mScnName(me, sortedSecs[i+1]));
26     }
27   }
28
29
30   /* Check for sections overlapping with EHDR */
31   for (i = 0; i < numSecs; i++) {
32     if (sortedSecs[i]->shdr.sh_offset < me->ehdr.e_ehsize) {
33       ELFU_WARN("elfu_check: Found section overlapping with EHDR: %s.\n",
34                 elfu_mScnName(me, sortedSecs[i]));
35     }
36   }
37
38
39   /* Check for sections overlapping with PHDRs */
40   for (i = 0; i < numSecs; i++) {
41     if (OVERLAPPING(sortedSecs[i]->shdr.sh_offset,
42                     SCNFILESIZE(&sortedSecs[i]->shdr),
43                     me->ehdr.e_phoff,
44                     me->ehdr.e_phentsize * me->ehdr.e_phnum)) {
45       ELFU_WARN("elfu_check: Found section overlapping with PHDRs: %s.\n",
46                 elfu_mScnName(me, sortedSecs[i]));
47     }
48   }
49
50
51   free(sortedSecs);
52
53   return 0;
54 }