summaryrefslogtreecommitdiff
path: root/src/lookup/first-section-in-segment.c
blob: 58065a500423af43ec8dc105bcb9e1586f65da05 (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
#include <stdlib.h>

#include <libelf.h>
#include <gelf.h>

#include <libelfu/libelfu.h>


/*
 * Returns the section that starts at the same point in the file as
 * the segment AND is wholly contained in the memory image.
 *
 * If no section fits, NULL is returned.
 */
Elf_Scn* elfu_firstSectionInSegment(Elf *e, GElf_Phdr *phdr)
{
  Elf_Scn *scn;

  scn = elf_getscn(e, 1);
  while (scn) {
    GElf_Shdr shdr;

    if (gelf_getshdr(scn, &shdr) != &shdr) {
      return NULL;
    }

    if (shdr.sh_offset == phdr->p_offset
        && elfu_segmentContainsSection(phdr, &shdr)) {
      return scn;
    }

    scn = elf_nextscn(e, scn);
  }

  return NULL;
}