From: norly Date: Fri, 28 Jun 2013 02:00:05 +0000 (+0100) Subject: Implement memory management TODOs X-Git-Url: https://git.enpas.org/?p=centaur.git;a=commitdiff_plain;h=88a99f6bafe7140711fa0095043c856fd8c44dc7 Implement memory management TODOs --- diff --git a/src/elfucli.c b/src/elfucli.c index d5f55be..b01c91a 100644 --- a/src/elfucli.c +++ b/src/elfucli.c @@ -151,13 +151,16 @@ int main(int argc, char **argv) printf("--reladd: Injecting %s...\n", optarg); if (elfu_mCheck(mrel)) { printf("--reladd: Check for input file failed.\n"); + elfu_mElfDestroy(mrel); goto ERR; } if (elfu_mReladd(me, mrel)) { printf("--reladd: Failed.\n"); + elfu_mElfDestroy(mrel); goto ERR; } printf("--reladd: Injected %s.\n", optarg); + elfu_mElfDestroy(mrel); } } break; diff --git a/src/libelfu/model/elf.c b/src/libelfu/model/elf.c index a25aa89..11346a5 100644 --- a/src/libelfu/model/elf.c +++ b/src/libelfu/model/elf.c @@ -29,22 +29,30 @@ ElfuElf* elfu_mElfAlloc() void elfu_mElfDestroy(ElfuElf* me) { - ElfuPhdr *mp; - ElfuScn *ms; - assert(me); - CIRCLEQ_INIT(&me->phdrList); - CIRCLEQ_INIT(&me->orphanScnList); + if (!CIRCLEQ_EMPTY(&me->phdrList)) { + ElfuPhdr *nextmp; - CIRCLEQ_FOREACH(mp, &me->phdrList, elem) { - CIRCLEQ_REMOVE(&me->phdrList, mp, elem); - elfu_mPhdrDestroy(mp); + nextmp = CIRCLEQ_FIRST(&me->phdrList); + while ((void*)nextmp != (void*)&me->phdrList) { + ElfuPhdr *curmp = nextmp; + nextmp = CIRCLEQ_NEXT(curmp, elem); + CIRCLEQ_REMOVE(&me->phdrList, curmp, elem); + elfu_mPhdrDestroy(curmp); + } } - CIRCLEQ_FOREACH(ms, &me->orphanScnList, elemChildScn) { - CIRCLEQ_REMOVE(&me->orphanScnList, ms, elemChildScn); - elfu_mScnDestroy(ms); + if (!CIRCLEQ_EMPTY(&me->orphanScnList)) { + ElfuScn *nextms; + + nextms = CIRCLEQ_FIRST(&me->orphanScnList); + while ((void*)nextms != (void*)&me->orphanScnList) { + ElfuScn *curms = nextms; + nextms = CIRCLEQ_NEXT(curms, elemChildScn); + CIRCLEQ_REMOVE(&me->orphanScnList, curms, elemChildScn); + elfu_mScnDestroy(curms); + } } free(me); diff --git a/src/libelfu/model/phdr.c b/src/libelfu/model/phdr.c index b694eb9..6f31e6b 100644 --- a/src/libelfu/model/phdr.c +++ b/src/libelfu/model/phdr.c @@ -194,19 +194,30 @@ ElfuPhdr* elfu_mPhdrAlloc() void elfu_mPhdrDestroy(ElfuPhdr* mp) { - ElfuPhdr *mp2; - ElfuScn *ms; - assert(mp); - CIRCLEQ_FOREACH(mp2, &mp->childPhdrList, elemChildPhdr) { - CIRCLEQ_REMOVE(&mp->childPhdrList, mp2, elemChildPhdr); - elfu_mPhdrDestroy(mp2); + if (!CIRCLEQ_EMPTY(&mp->childPhdrList)) { + ElfuPhdr *nextmp; + + nextmp = CIRCLEQ_FIRST(&mp->childPhdrList); + while ((void*)nextmp != (void*)&mp->childPhdrList) { + ElfuPhdr *curmp = nextmp; + nextmp = CIRCLEQ_NEXT(curmp, elemChildPhdr); + CIRCLEQ_REMOVE(&mp->childPhdrList, curmp, elemChildPhdr); + elfu_mPhdrDestroy(curmp); + } } - CIRCLEQ_FOREACH(ms, &mp->childScnList, elemChildScn) { - CIRCLEQ_REMOVE(&mp->childScnList, ms, elemChildScn); - elfu_mScnDestroy(ms); + if (!CIRCLEQ_EMPTY(&mp->childScnList)) { + ElfuScn *nextms; + + nextms = CIRCLEQ_FIRST(&mp->childScnList); + while ((void*)nextms != (void*)&mp->childScnList) { + ElfuScn *curms = nextms; + nextms = CIRCLEQ_NEXT(curms, elemChildScn); + CIRCLEQ_REMOVE(&mp->childScnList, curms, elemChildScn); + elfu_mScnDestroy(curms); + } } free(mp); diff --git a/src/libelfu/model/section.c b/src/libelfu/model/section.c index 74fbc3a..d29b510 100644 --- a/src/libelfu/model/section.c +++ b/src/libelfu/model/section.c @@ -11,8 +11,6 @@ void* elfu_mScnForall(ElfuElf *me, SectionHandlerFunc f, void *aux1, void *aux2) ElfuPhdr *mp; ElfuScn *ms; - // TODO: Sort PHDRs by offset before interating - CIRCLEQ_FOREACH(mp, &me->phdrList, elem) { if (mp->phdr.p_type != PT_LOAD) { continue; @@ -248,7 +246,29 @@ void elfu_mScnDestroy(ElfuScn* ms) { assert(ms); - // TODO: Free symtab/reltab + if (!CIRCLEQ_EMPTY(&ms->symtab.syms)) { + ElfuSym *nextsym; + + nextsym = CIRCLEQ_FIRST(&ms->symtab.syms); + while ((void*)nextsym != (void*)&ms->symtab.syms) { + ElfuSym *cursym = nextsym; + nextsym = CIRCLEQ_NEXT(cursym, elem); + CIRCLEQ_REMOVE(&ms->symtab.syms, cursym, elem); + free(cursym); + } + } + + if (!CIRCLEQ_EMPTY(&ms->reltab.rels)) { + ElfuRel *nextrel; + + nextrel = CIRCLEQ_FIRST(&ms->reltab.rels); + while ((void*)nextrel != (void*)&ms->reltab.rels) { + ElfuRel *currel = nextrel; + nextrel = CIRCLEQ_NEXT(currel, elem); + CIRCLEQ_REMOVE(&ms->reltab.rels, currel, elem); + free(currel); + } + } if (ms->databuf) { free(ms->databuf); diff --git a/src/libelfu/modelops/fromFile.c b/src/libelfu/modelops/fromFile.c index ef7add9..a4f0384 100644 --- a/src/libelfu/modelops/fromFile.c +++ b/src/libelfu/modelops/fromFile.c @@ -460,7 +460,9 @@ ElfuElf* elfu_mFromElf(Elf *e) } } - + if (secArray) { + free(secArray); + } return me; diff --git a/src/libelfu/modelops/layout.c b/src/libelfu/modelops/layout.c index 2fd610c..182ed2e 100644 --- a/src/libelfu/modelops/layout.c +++ b/src/libelfu/modelops/layout.c @@ -345,7 +345,6 @@ int elfu_mLayoutAuto(ElfuElf *me) ElfuPhdr *highestOffsEnd; ElfuPhdr *mp; ElfuScn *ms; - ElfuPhdr **phdrArr; GElf_Off lastend = 0; assert(me); @@ -354,12 +353,6 @@ int elfu_mLayoutAuto(ElfuElf *me) elfu_mPhdrLoadLowestHighest(me, &lowestAddr, &highestAddr, &lowestOffs, &highestOffsEnd); - phdrArr = malloc(elfu_mPhdrCount(me) * sizeof(*phdrArr)); - if (!phdrArr) { - ELFU_WARN("elfu_mLayoutAuto: malloc failed for phdrArr.\n"); - return 1; - } - lastend = OFFS_END(highestOffsEnd->phdr.p_offset, highestOffsEnd->phdr.p_filesz); diff --git a/src/libelfu/modelops/reladd.c b/src/libelfu/modelops/reladd.c index 1980c76..4bc6e40 100644 --- a/src/libelfu/modelops/reladd.c +++ b/src/libelfu/modelops/reladd.c @@ -130,7 +130,7 @@ static ElfuScn* insertSection(ElfuElf *me, ElfuElf *mrel, ElfuScn *oldscn) ERROR: if (newscn) { - // TODO: Destroy newscn + elfu_mScnDestroy(newscn); } return NULL; }