diff options
author | norly <ny-git@enpas.org> | 2013-05-24 07:23:41 +0100 |
---|---|---|
committer | norly <ny-git@enpas.org> | 2013-05-24 07:23:41 +0100 |
commit | 264965b9323f90f1a834fb2d80c362aa91fbb5e5 (patch) | |
tree | 09fbfa898d674060970f5658b0c93b9a502d1693 /src/model/section-in-segment.c | |
parent | b7ab2674931f1bb9a7e6da594f587ae84c293238 (diff) |
Object file injection, first part
Diffstat (limited to 'src/model/section-in-segment.c')
-rw-r--r-- | src/model/section-in-segment.c | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/src/model/section-in-segment.c b/src/model/section-in-segment.c new file mode 100644 index 0000000..ad7f037 --- /dev/null +++ b/src/model/section-in-segment.c @@ -0,0 +1,56 @@ +#include <assert.h> +#include <libelf/gelf.h> +#include <libelfu/libelfu.h> + + +/* + * Returns the first section beginning in the segment. + * + * Since sections have to be in the file in mapping order to load them + * as a continuous segment, we only have to search by start offset. + */ +ElfuScn* elfu_mScnFirstInSegment(ElfuElf *me, ElfuPhdr *mp) +{ + ElfuScn *ms; + + assert(me); + assert(mp); + + CIRCLEQ_FOREACH(ms, &me->scnList, elem) { + if ((ms->shdr.sh_offset >= mp->phdr.p_offset) + && (ms->shdr.sh_offset < mp->phdr.p_offset + mp->phdr.p_filesz)) { + return ms; + } + } + + return NULL; +} + + + +/* + * Returns the last section ending in the segment. + * + * Since sections have to be in the file in mapping order to load them + * as a continuous segment, we only have to search by end offset. + */ +ElfuScn* elfu_mScnLastInSegment(ElfuElf *me, ElfuPhdr *mp) +{ + ElfuScn *last = NULL; + ElfuScn *ms; + + assert(me); + assert(mp); + + CIRCLEQ_FOREACH(ms, &me->scnList, elem) { + /* Get section size on disk - for NOBITS sections that is 0 bytes. */ + size_t size = elfu_gScnSizeFile(&ms->shdr); + + if (((ms->shdr.sh_offset + size >= mp->phdr.p_offset) + && (ms->shdr.sh_offset + size <= mp->phdr.p_offset + mp->phdr.p_filesz))) { + last = ms; + } + } + + return last; +} |