GPLv2 release
[centaur.git] / src / libelfu / modelops / check.c
1 /* This file is part of centaur.
2  *
3  * centaur is free software: you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License 2 as
5  * published by the Free Software Foundation.
6
7  * centaur is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11
12  * You should have received a copy of the GNU General Public License
13  * along with centaur.  If not, see <http://www.gnu.org/licenses/>.
14  */
15
16 #include <assert.h>
17 #include <stdlib.h>
18 #include <sys/types.h>
19 #include <libelfu/libelfu.h>
20
21
22 int elfu_mCheck(ElfuElf *me)
23 {
24   size_t numSecs;
25   ElfuScn **sortedSecs;
26   size_t i;
27
28   sortedSecs = elfu_mScnSortedByOffset(me, &numSecs);
29   if (!sortedSecs) {
30     return -1;
31   }
32
33
34   /* Check for overlapping sections */
35   for (i = 0; i < numSecs - 1; i++) {
36     if (sortedSecs[i]->shdr.sh_offset + SCNFILESIZE(&sortedSecs[i]->shdr)
37         > sortedSecs[i+1]->shdr.sh_offset) {
38       ELFU_WARN("elfu_check: Found overlapping sections: %s and %s.\n",
39                 elfu_mScnName(me, sortedSecs[i]),
40                 elfu_mScnName(me, sortedSecs[i+1]));
41     }
42   }
43
44
45   /* Check for sections overlapping with EHDR */
46   for (i = 0; i < numSecs; i++) {
47     if (sortedSecs[i]->shdr.sh_offset < me->ehdr.e_ehsize) {
48       ELFU_WARN("elfu_check: Found section overlapping with EHDR: %s.\n",
49                 elfu_mScnName(me, sortedSecs[i]));
50     }
51   }
52
53
54   /* Check for sections overlapping with PHDRs */
55   for (i = 0; i < numSecs; i++) {
56     if (OVERLAPPING(sortedSecs[i]->shdr.sh_offset,
57                     SCNFILESIZE(&sortedSecs[i]->shdr),
58                     me->ehdr.e_phoff,
59                     me->ehdr.e_phentsize * me->ehdr.e_phnum)) {
60       ELFU_WARN("elfu_check: Found section overlapping with PHDRs: %s.\n",
61                 elfu_mScnName(me, sortedSecs[i]));
62     }
63   }
64
65
66   free(sortedSecs);
67
68   return 0;
69 }