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