4 #include <libelfu/libelfu.h>
8 static int isOverlapping(size_t off1, size_t sz1, size_t off2, size_t sz2)
10 size_t end1 = off1 + sz1;
11 size_t end2 = off2 + sz2;
13 if (off2 >= off1 && off2 < end1) {
15 } else if (off1 >= off2 && off1 < end2) {
23 static int cmpScnOffs(const void *ms1, const void *ms2)
28 ElfuScn *s1 = *(ElfuScn**)ms1;
29 ElfuScn *s2 = *(ElfuScn**)ms2;
35 if (s1->shdr.sh_offset < s2->shdr.sh_offset) {
37 } else if (s1->shdr.sh_offset == s2->shdr.sh_offset) {
39 } else /* if (s1->shdr.sh_offset > s2->shdr.sh_offset) */ {
45 int elfu_mCheck(ElfuElf *me)
52 /* Sort sections by offset in file */
53 numSecs = elfu_mCountScns(me);
54 sortedSecs = malloc(numSecs * sizeof(*sortedSecs));
56 ELFU_WARN("elfu_check: Failed to allocate memory.\n");
60 CIRCLEQ_FOREACH(ms, &me->scnList, elem) {
66 qsort(sortedSecs, numSecs, sizeof(*sortedSecs), cmpScnOffs);
69 /* Check for overlapping sections */
70 for (i = 0; i < numSecs - 1; i++) {
71 if (sortedSecs[i]->shdr.sh_offset + SCNFILESIZE(&sortedSecs[i]->shdr)
72 > sortedSecs[i+1]->shdr.sh_offset) {
73 ELFU_WARN("elfu_check: Found overlapping sections: %s and %s.\n",
74 elfu_mScnName(me, sortedSecs[i]),
75 elfu_mScnName(me, sortedSecs[i+1]));
80 /* Check for sections overlapping with EHDR */
81 for (i = 0; i < numSecs; i++) {
82 if (sortedSecs[i]->shdr.sh_offset < me->ehdr.e_ehsize) {
83 ELFU_WARN("elfu_check: Found section overlapping with EHDR: %s.\n",
84 elfu_mScnName(me, sortedSecs[i]));
89 /* Check for sections overlapping with PHDRs */
90 for (i = 0; i < numSecs; i++) {
91 if (isOverlapping(sortedSecs[i]->shdr.sh_offset,
92 SCNFILESIZE(&sortedSecs[i]->shdr),
94 me->ehdr.e_phentsize * me->ehdr.e_phnum)) {
95 ELFU_WARN("elfu_check: Found section overlapping with PHDRs: %s.\n",
96 elfu_mScnName(me, sortedSecs[i]));