-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathElf64.cpp
More file actions
70 lines (48 loc) · 1.69 KB
/
Elf64.cpp
File metadata and controls
70 lines (48 loc) · 1.69 KB
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
#include <elf.h>
#include "Elf.hpp"
Elf64_Ehdr* elf::arch64::GetHeader(void* data) {
Elf64_Ehdr* ret = (Elf64_Ehdr*) data;
if(memcmp(ret->e_ident, ELFMAG, SELFMAG) != 0)
return NULL; // Invalid ELF
if(GetClass(data) != ELFCLASS64)
return NULL; // x64 only
return ret;
}
Elf64_Shdr* elf::arch64::GetSectionHeaderEntry(void* data, size_t index) {
Elf64_Ehdr* header = GetHeader(data);
if(header == NULL)
return NULL;
//return (Elf64_Shdr*)((uint8_t *) data + (header->e_shoff + (header->e_shentsize * index)));
return &((Elf64_Shdr*)((char*) data + header->e_shoff))[index];
}
Elf64_Phdr* elf::arch64::GetProgramHeaderEntry(void *data, size_t index) {
Elf64_Ehdr* header = GetHeader(data);
if(header == NULL)
return NULL;
return &((Elf64_Phdr*)((char*) data + header->e_phoff))[index];
}
const char* elf::arch64::GetNameOfSectionAtIndex(void* data, Elf64_Shdr* section) {
uint8_t* sectionData = GetSectionData(data, section);
if(sectionData == NULL)
return NULL;
if(section->sh_name == 0)
return "<unnamed>";
return (const char*)((uint8_t*) data + section->sh_name);
}
uint8_t* elf::arch64::GetSectionData(void* data, Elf64_Shdr* section) {
if(data == NULL || section == NULL)
return NULL;
return ((uint8_t*) data + section->sh_offset);
}
size_t elf::arch64::GetSectionHeaderEntryCount(void* data) {
Elf64_Ehdr* header = GetHeader(data);
if(header == NULL)
return 0;
return header->e_shnum;
}
size_t elf::arch64::GetProgramHeaderEntryCount(void* data) {
Elf64_Ehdr* header = GetHeader(data);
if(header == NULL)
return 0;
return header->e_phnum;
}