-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathElfMemoryFile64.cpp
More file actions
131 lines (112 loc) · 3.43 KB
/
ElfMemoryFile64.cpp
File metadata and controls
131 lines (112 loc) · 3.43 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
* ElfMemoryFile64.cpp
*
* Created on: Dec 8, 2013
* Author: tyler
*/
#include "ElfMemoryFile64.h"
#include "Process.h"
#include <iostream>
namespace Elf
{
ElfMemoryFile64::ElfMemoryFile64(const std::vector<MemoryRegion>& memoryRegions, const Process& process):
m_memoryRegions(memoryRegions), m_process(&process)
{
for(const auto& rgn : m_memoryRegions)
{
m_memoryData.push_back(process.readMemory(rgn.getStartPtr(), rgn.getSize()));
}
int i = 0;
for(const auto& region : m_memoryRegions)
{
//Find the header
if(region.getFileOffset() == 0)
{
uint8_t* data = m_memoryData[i].get();
m_header = *reinterpret_cast<Elf64_Ehdr*>(data);
if(m_header.e_ident[0] != ELFMAG0 || m_header.e_ident[1] != ELFMAG1 || m_header.e_ident[2] != ELFMAG2 || m_header.e_ident[3] != ELFMAG3)
{
throw std::runtime_error("Given data does not represent a valid ELF image");
}
if(m_header.e_ident[4] != ELFCLASS64)
{
throw std::runtime_error("ELF image is not 64-bit");
}
Elf64_Shdr* sectionTablePtr = reinterpret_cast<Elf64_Shdr*>(getMemoryDataPointerFromOffset(getMemoryPointerFromFileOffset(m_header.e_shoff)));
char* stringTable = reinterpret_cast<char*>(getMemoryDataPointerFromOffset(getMemoryPointerFromFileOffset(sectionTablePtr[m_header.e_shstrndx].sh_offset)));
for(int i = 0; i < m_header.e_shnum; ++i)
{
Elf64_Shdr header = sectionTablePtr[i];
std::string name;
if(header.sh_name != 0)
{
name = &stringTable[header.sh_name];
int x = 1;
}
m_sections.push_back(Elf::ElfMemorySection64(header, name));
if(name == ".dynsym")
{
int x = 0;
}
}
}
i++;
}
const ElfMemorySection64* dynamicSymbolSection = getSectionByName(".dynsym");
const ElfMemorySection64* dynamicStringTableSection = getSectionByName(".dynstr");
Elf64_Sym* dynamicSymbolTable = reinterpret_cast<Elf64_Sym*>(getMemoryDataPointerFromOffset(getMemoryPointerFromFileOffset(dynamicSymbolSection->getHeader().sh_offset)));
char* dynamicStringTable = reinterpret_cast<char*>(getMemoryDataPointerFromOffset(getMemoryPointerFromFileOffset(dynamicStringTableSection->getHeader().sh_offset)));
for(int i = 0; i < dynamicSymbolSection->getHeader().sh_size / sizeof(Elf64_Sym); ++i)
{
std::string name;
if(dynamicSymbolTable[i].st_name != 0)
{
name = &dynamicStringTable[dynamicSymbolTable[i].st_name];
}
if(dynamicSymbolTable[i].st_name != 0 && dynamicSymbolTable[i].st_value != 0)
{
m_exports.insert(std::make_pair(name, ElfExport64{dynamicSymbolTable[i].st_value, dynamicSymbolTable[i].st_shndx}));
}
int x = 0;
}
}
ElfMemoryFile64::~ElfMemoryFile64()
{
}
uintptr_t ElfMemoryFile64::getMemoryPointerFromFileOffset(uintptr_t offset) const
{
for(const auto& region : m_memoryRegions)
{
if(region.getFileOffset() <= offset && region.getFileOffset() + region.getSize() >= offset)
{
return offset - region.getFileOffset() + region.getStartPtr();
}
}
return 0;
}
uint8_t* ElfMemoryFile64::getMemoryDataPointerFromOffset(uintptr_t offset) const
{
int i = 0;
for(const auto& region : m_memoryRegions)
{
if(region.getStartPtr() <= offset && region.getEndPtr() >= offset)
{
return &m_memoryData[i][offset - region.getStartPtr()];
}
++i;
}
return nullptr;
}
const ElfMemorySection64* ElfMemoryFile64::getSectionByName(
const std::string& name) const
{
for(auto& section : m_sections)
{
if(section.getName() == name)
{
return §ion;
}
}
return nullptr;
}
}