5 #include <libelfu/libelfu.h>
9 static int isOverlapping(size_t off1, size_t sz1, size_t off2, size_t sz2)
11 size_t end1 = off1 + sz1;
12 size_t end2 = off2 + sz2;
14 if (off2 >= off1 && off2 < end1) {
16 } else if (off1 >= off2 && off1 < end2) {
24 static int cmpScnOffs(const void *ms1, const void *ms2)
29 ElfuScn *s1 = *(ElfuScn**)ms1;
30 ElfuScn *s2 = *(ElfuScn**)ms2;
36 if (s1->shdr.sh_offset < s2->shdr.sh_offset) {
38 } else if (s1->shdr.sh_offset == s2->shdr.sh_offset) {
40 } else /* if (s1->shdr.sh_offset > s2->shdr.sh_offset) */ {
46 int elfu_modelCheck(ElfuElf *me)
53 /* Sort sections by offset in file */
54 numSecs = elfu_countSections(me);
55 sortedSecs = malloc(numSecs * sizeof(*sortedSecs));
57 fprintf(stderr, "elfu_check: Failed to allocate memory.\n");
61 CIRCLEQ_FOREACH(ms, &me->scnList, elem) {
67 qsort(sortedSecs, numSecs, sizeof(*sortedSecs), cmpScnOffs);
70 /* Check for overlapping sections */
71 for (i = 0; i < numSecs - 1; i++) {
72 if (sortedSecs[i]->shdr.sh_offset + elfu_gScnSizeFile(&sortedSecs[i]->shdr)
73 > sortedSecs[i+1]->shdr.sh_offset) {
74 fprintf(stderr, "elfu_check: Found overlapping sections: %s and %s.\n",
75 elfu_modelScnName(me, sortedSecs[i]),
76 elfu_modelScnName(me, sortedSecs[i+1]));
81 /* Check for sections overlapping with EHDR */
82 for (i = 0; i < numSecs; i++) {
83 if (sortedSecs[i]->shdr.sh_offset < me->ehdr.e_ehsize) {
84 fprintf(stderr, "elfu_check: Found section overlapping with EHDR: %s.\n",
85 elfu_modelScnName(me, sortedSecs[i]));
90 /* Check for sections overlapping with PHDRs */
91 for (i = 0; i < numSecs; i++) {
92 if (isOverlapping(sortedSecs[i]->shdr.sh_offset,
93 elfu_gScnSizeFile(&sortedSecs[i]->shdr),
95 me->ehdr.e_phentsize * me->ehdr.e_phnum)) {
96 fprintf(stderr, "elfu_check: Found section overlapping with PHDRs: %s.\n",
97 elfu_modelScnName(me, sortedSecs[i]));