summaryrefslogtreecommitdiff
path: root/src/model/check.c
diff options
context:
space:
mode:
authornorly <ny-git@enpas.org>2013-05-30 04:01:51 +0100
committernorly <ny-git@enpas.org>2013-05-30 04:36:39 +0100
commit4addee4bda6064926b24cd1ae929303003bd9ff1 (patch)
tree10bad1be5ff2f71981fe87e1c43e171e94dacc8a /src/model/check.c
parenteb5a1daba781013ccf168b95510d0f67f0b9c946 (diff)
Redesign data structures, make basic reladd work.newparser
The memory ELF model is now a tree structure: ELF +--> PHDRs +--> PHDR +--> Section | | +--> Section | | ... | | \--> Section | | | +--> PHDR +--> Section | | ... | ... | \--> Orphaned sections +--> Section ... \--> Section This effectively introduces semantics into the binary blob we are editing, and allows us to re-layout its contents much more easily while keeping as close as possible to what is assumed to be the original semantics. As a side-effect, a first meta-function had to be introduced (elfu_mScnForall) in order to traverse all leaves of the tree. Much old code has been removed given the leaner environment available now, and automated insertion of .text and .data sections from object files into executables now works. However nothing else is inserted (such as string tables or .bss) and no relocation takes place yet.
Diffstat (limited to 'src/model/check.c')
-rw-r--r--src/model/check.c63
1 files changed, 7 insertions, 56 deletions
diff --git a/src/model/check.c b/src/model/check.c
index 6dc0694..5234bef 100644
--- a/src/model/check.c
+++ b/src/model/check.c
@@ -4,66 +4,16 @@
#include <libelfu/libelfu.h>
-
-static int isOverlapping(size_t off1, size_t sz1, size_t off2, size_t sz2)
-{
- size_t end1 = off1 + sz1;
- size_t end2 = off2 + sz2;
-
- if (off2 >= off1 && off2 < end1) {
- return 1;
- } else if (off1 >= off2 && off1 < end2) {
- return 1;
- } else {
- return 0;
- }
-}
-
-
-static int cmpScnOffs(const void *ms1, const void *ms2)
-{
- assert(ms1);
- assert(ms2);
-
- ElfuScn *s1 = *(ElfuScn**)ms1;
- ElfuScn *s2 = *(ElfuScn**)ms2;
-
- assert(s1);
- assert(s2);
-
-
- if (s1->shdr.sh_offset < s2->shdr.sh_offset) {
- return -1;
- } else if (s1->shdr.sh_offset == s2->shdr.sh_offset) {
- return 0;
- } else /* if (s1->shdr.sh_offset > s2->shdr.sh_offset) */ {
- return 1;
- }
-}
-
-
int elfu_mCheck(ElfuElf *me)
{
- ElfuScn *ms;
size_t numSecs;
ElfuScn **sortedSecs;
size_t i;
- /* Sort sections by offset in file */
- numSecs = elfu_mCountScns(me);
- sortedSecs = malloc(numSecs * sizeof(*sortedSecs));
+ sortedSecs = elfu_mScnSortedByOffset(me, &numSecs);
if (!sortedSecs) {
- ELFU_WARN("elfu_check: Failed to allocate memory.\n");
- }
-
- i = 0;
- CIRCLEQ_FOREACH(ms, &me->scnList, elem) {
- sortedSecs[i] = ms;
- i++;
+ return -1;
}
- assert(i == numSecs);
-
- qsort(sortedSecs, numSecs, sizeof(*sortedSecs), cmpScnOffs);
/* Check for overlapping sections */
@@ -88,15 +38,16 @@ int elfu_mCheck(ElfuElf *me)
/* Check for sections overlapping with PHDRs */
for (i = 0; i < numSecs; i++) {
- if (isOverlapping(sortedSecs[i]->shdr.sh_offset,
- SCNFILESIZE(&sortedSecs[i]->shdr),
- me->ehdr.e_phoff,
- me->ehdr.e_phentsize * me->ehdr.e_phnum)) {
+ if (OVERLAPPING(sortedSecs[i]->shdr.sh_offset,
+ SCNFILESIZE(&sortedSecs[i]->shdr),
+ me->ehdr.e_phoff,
+ me->ehdr.e_phentsize * me->ehdr.e_phnum)) {
ELFU_WARN("elfu_check: Found section overlapping with PHDRs: %s.\n",
elfu_mScnName(me, sortedSecs[i]));
}
}
+
free(sortedSecs);
return 0;