Object file injection, first part
[centaur.git] / src / model / section-in-segment.c
1 #include <assert.h>
2 #include <libelf/gelf.h>
3 #include <libelfu/libelfu.h>
4
5
6 /*
7  * Returns the first section beginning in the segment.
8  *
9  * Since sections have to be in the file in mapping order to load them
10  * as a continuous segment, we only have to search by start offset.
11  */
12 ElfuScn* elfu_mScnFirstInSegment(ElfuElf *me, ElfuPhdr *mp)
13 {
14   ElfuScn *ms;
15
16   assert(me);
17   assert(mp);
18
19   CIRCLEQ_FOREACH(ms, &me->scnList, elem) {
20     if ((ms->shdr.sh_offset >= mp->phdr.p_offset)
21         && (ms->shdr.sh_offset < mp->phdr.p_offset + mp->phdr.p_filesz)) {
22       return ms;
23     }
24   }
25
26   return NULL;
27 }
28
29
30
31 /*
32  * Returns the last section ending in the segment.
33  *
34  * Since sections have to be in the file in mapping order to load them
35  * as a continuous segment, we only have to search by end offset.
36  */
37 ElfuScn* elfu_mScnLastInSegment(ElfuElf *me, ElfuPhdr *mp)
38 {
39   ElfuScn *last = NULL;
40   ElfuScn *ms;
41
42   assert(me);
43   assert(mp);
44
45   CIRCLEQ_FOREACH(ms, &me->scnList, elem) {
46     /* Get section size on disk - for NOBITS sections that is 0 bytes. */
47     size_t size = elfu_gScnSizeFile(&ms->shdr);
48
49     if (((ms->shdr.sh_offset + size >= mp->phdr.p_offset)
50          && (ms->shdr.sh_offset + size <= mp->phdr.p_offset + mp->phdr.p_filesz))) {
51       last = ms;
52     }
53   }
54
55   return last;
56 }