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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
#include <stdlib.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_eScnFirstInSegment(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
&& PHDR_CONTAINS_SCN_IN_MEMORY(phdr, &shdr)) {
return scn;
}
scn = elf_nextscn(e, scn);
}
return NULL;
}
/*
* Returns the first section that is contained in the segment and
* ends as close to its memory image of as possible (the "last"
* section in the segment).
*
* If no section fits, NULL is returned.
*/
Elf_Scn* elfu_eScnLastInSegment(Elf *e, GElf_Phdr *phdr)
{
Elf_Scn *last = NULL;
Elf_Scn *scn;
scn = elf_getscn(e, 1);
while (scn) {
GElf_Shdr shdr;
if (gelf_getshdr(scn, &shdr) != &shdr) {
ELFU_WARNELF("gelf_getshdr");
continue;
}
if (PHDR_CONTAINS_SCN_IN_MEMORY(phdr, &shdr)) {
if (!last) {
last = scn;
} else {
GElf_Shdr shdrOld;
if (gelf_getshdr(last, &shdrOld) != &shdrOld) {
continue;
}
if (shdr.sh_offset + shdr.sh_size
> shdrOld.sh_offset + shdrOld.sh_size) {
// TODO: Check (leftover space in memory image) < (p_align)
last = scn;
}
}
}
scn = elf_nextscn(e, scn);
}
return last;
}
|