forked from Jumboperson/Il2CppDumper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelf_utils.cpp
42 lines (38 loc) · 1.22 KB
/
elf_utils.cpp
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
#include "elf_utils.h"
elf_32_shdr* GetSection(int iSection, elf_header* pHdr)
{
if (iSection > pHdr->get_shnum())
return 0;
return (elf_32_shdr*)((char*)pHdr + pHdr->get_shoff() + (pHdr->get_shentsize() * iSection));
}
char* GetStringFromTable(uint32_t uiString, elf_header* pHdr)
{
elf_32_shdr* pStringSec = GetSection(pHdr->get_shstrndx(), pHdr);
char* pOffset = (char*)pHdr + pStringSec->sh_offset;
return &pOffset[uiString];
}
void* MapVirtualAddressToReal(uint32_t uiAddr, elf_header* pHdr)
{
elf_32_shdr* pFirstSec = GetSection(0, pHdr);
if (uiAddr < pFirstSec->sh_addr)
return 0;
for (uint32_t i = 1; i < pHdr->get_shnum(); ++i)
{
elf_32_shdr* pSection = GetSection(i, pHdr);
if (pSection->sh_addr > uiAddr)
{
elf_32_shdr* pRetSec = GetSection(i - 1, pHdr);
uint32_t uiResOffset = uiAddr - pRetSec->sh_addr;
return (void*)(pRetSec->sh_offset + (char*)pHdr + uiResOffset);
}
}
elf_32_shdr* pRetSec = GetSection(pHdr->get_shnum() - 1, pHdr);
uint32_t uiResOffset = uiAddr - pRetSec->sh_addr;
return (void*)(pRetSec->sh_offset + (char*)pHdr + uiResOffset);
}
uint32_t MapRealToFileOffset(void* addr, elf_header* pHdr)
{
if (addr < pHdr)
return 0xffffffff;
return (uint32_t)addr - (uint32_t)pHdr;
}