3 #include <libelf/libelf.h>
4 #include <libelf/gelf.h>
6 #include <libelfu/libelfu.h>
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.
13 * If no section fits, NULL is returned.
15 Elf_Scn* elfu_eScnFirstInSegment(Elf *e, GElf_Phdr *phdr)
19 scn = elf_getscn(e, 1);
23 if (gelf_getshdr(scn, &shdr) != &shdr) {
27 if (shdr.sh_offset == phdr->p_offset
28 && elfu_ePhdrContainsScn(phdr, &shdr)) {
32 scn = elf_nextscn(e, scn);
41 * Returns the first section that is contained in the segment and
42 * ends as close to its memory image of as possible (the "last"
43 * section in the segment).
45 * If no section fits, NULL is returned.
47 Elf_Scn* elfu_eScnLastInSegment(Elf *e, GElf_Phdr *phdr)
53 scn = elf_getscn(e, 1);
57 if (gelf_getshdr(scn, &shdr) != &shdr) {
58 ELFU_WARNELF("gelf_getshdr");
62 if (elfu_ePhdrContainsScn(phdr, &shdr)) {
68 if (gelf_getshdr(last, &shdrOld) != &shdrOld) {
72 if (shdr.sh_offset + shdr.sh_size
73 > shdrOld.sh_offset + shdrOld.sh_size) {
74 // TODO: Check (leftover space in memory image) < (p_align)
80 scn = elf_nextscn(e, scn);