-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread_elf.cpp
More file actions
85 lines (70 loc) · 2.12 KB
/
read_elf.cpp
File metadata and controls
85 lines (70 loc) · 2.12 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <cstdint>
#include <fstream>
#include <vector>
#define ELF_NIDENT 16
// Program headers with type PT_LOAD must be loaded into the application memory during its loading
#define PT_LOAD 1
// ELF header
struct elf_hdr {
std::uint8_t e_ident[ELF_NIDENT];
std::uint16_t e_type;
std::uint16_t e_machine;
std::uint32_t e_version;
std::uint64_t e_entry;
std::uint64_t e_phoff;
std::uint64_t e_shoff;
std::uint32_t e_flags;
std::uint16_t e_ehsize;
std::uint16_t e_phentsize;
std::uint16_t e_phnum;
std::uint16_t e_shentsize;
std::uint16_t e_shnum;
std::uint16_t e_shstrndx;
} __attribute__((packed));
// ELF program header entry
struct elf_phdr {
std::uint32_t p_type;
std::uint32_t p_flags;
std::uint64_t p_offset;
std::uint64_t p_vaddr;
std::uint64_t p_paddr;
std::uint64_t p_filesz;
std::uint64_t p_memsz;
std::uint64_t p_align;
} __attribute__((packed));
// returns the address of main entry point
std::uintptr_t entry_point(const char *name) {
std::ifstream file(name, std::ios::binary | std::ios::ate);
std::streamsize size = file.tellg();
file.seekg(0, std::ios::beg);
std::vector<char> buffer(size);
if (file.read(buffer.data(), size)) {
auto header = reinterpret_cast<elf_hdr*>(buffer.data());
return header->e_entry;
}
return 0;
}
// returns the size of memory required to load the program
std::size_t space(const char *name) {
std::ifstream file(name, std::ios::binary | std::ios::ate);
std::streamsize size = file.tellg();
file.seekg(0, std::ios::beg);
std::vector<char> buffer(size);
char *const buf = buffer.data();
if (file.read(buf, size)) {
auto header = reinterpret_cast<elf_hdr*>(buf);
const uint16_t program_header_entity_size = header->e_phentsize;
const uint16_t program_header_count = header->e_phnum;
char* phdr_start = buf + header->e_phoff;
elf_phdr phdr {};
uint64_t res = 0;
for (int i = 0; i< program_header_count; ++i) {
phdr = *(reinterpret_cast<elf_phdr*>(phdr_start + i * program_header_entity_size));
if (phdr.p_type == PT_LOAD) {
res += phdr.p_memsz;
}
}
return res;
}
return 0;
}