Move headers for ELF-based operations together
[centaur.git] / src / elfops / first-section-in-segment.c
1 #include <stdlib.h>
2
3 #include <libelf.h>
4 #include <gelf.h>
5
6 #include <libelfu/libelfu.h>
7
8
9 /*
10  * Returns the section that starts at the same point in the file as
11  * the segment AND is wholly contained in the memory image.
12  *
13  * If no section fits, NULL is returned.
14  */
15 Elf_Scn* elfu_firstSectionInSegment(Elf *e, GElf_Phdr *phdr)
16 {
17   Elf_Scn *scn;
18
19   scn = elf_getscn(e, 1);
20   while (scn) {
21     GElf_Shdr shdr;
22
23     if (gelf_getshdr(scn, &shdr) != &shdr) {
24       return NULL;
25     }
26
27     if (shdr.sh_offset == phdr->p_offset
28         && elfu_segmentContainsSection(phdr, &shdr)) {
29       return scn;
30     }
31
32     scn = elf_nextscn(e, scn);
33   }
34
35   return NULL;
36 }