summaryrefslogtreecommitdiff
path: root/src/model/section-in-segment.c
blob: 0e513e23d82409a647afa3a3c3ed8342e6f199f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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 = SCNFILESIZE(&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;
}