Skip to content

Commit 45f5e89

Browse files
committed
ELF Impl
1 parent 3e9ccbc commit 45f5e89

File tree

2 files changed

+64
-4
lines changed

2 files changed

+64
-4
lines changed

src/jni/ElfUtils.cpp

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,17 @@ bool ElfOpen(const std::string& fullModulePath, std::function<void(ElfPack libMa
3737
return true;
3838
}
3939

40+
bool ElfPeekIs64(const std::string& fullModulePath, bool& outResult)
41+
{
42+
outResult = false;
43+
44+
bool elfOpened = ElfOpen(fullModulePath, [&](ElfPack elfPack){
45+
outResult = elfPack.header->e_ident[EI_CLASS] == ELFCLASS64;
46+
});
47+
48+
return elfOpened;
49+
}
50+
4051
Elf32_Shdr* ElfSectionByIndex(ElfPack libMap, unsigned int sectionIdx)
4152
{
4253
if((sectionIdx < libMap.header->e_shnum) == false)
@@ -58,7 +69,7 @@ void ElfForEachSection(ElfPack libMap, std::function<bool(Elf32_Shdr* pCurrentSe
5869
}
5970
}
6071

61-
Elf32_Shdr* ElfLookupSection(ElfPack libMap, uint32_t sectionType)
72+
Elf32_Shdr* ElfLookupSectionByType(ElfPack libMap, uint32_t sectionType)
6273
{
6374
Elf32_Shdr* secHeader = nullptr;
6475

@@ -74,16 +85,60 @@ Elf32_Shdr* ElfLookupSection(ElfPack libMap, uint32_t sectionType)
7485
return secHeader;
7586
}
7687

88+
const char* ElfGetSectionHeadersStringBlob(ElfPack libMap)
89+
{
90+
if(libMap.header->e_shstrndx == SHN_UNDEF)
91+
return nullptr;
92+
93+
Elf32_Shdr* shStrSec = ElfSectionByIndex(libMap, libMap.header->e_shstrndx);
94+
95+
if(shStrSec == nullptr || shStrSec->sh_offset < 1)
96+
return nullptr;
97+
98+
return (const char*)(libMap.base + shStrSec->sh_offset);
99+
}
100+
101+
const char* ElfGetSectionName(ElfPack libMap, Elf32_Shdr* sectionHdr)
102+
{
103+
const char* shStrBlob = ElfGetSectionHeadersStringBlob(libMap);
104+
105+
if(shStrBlob == nullptr)
106+
return nullptr;
107+
108+
return shStrBlob + sectionHdr->sh_name;
109+
}
110+
111+
Elf32_Shdr* ElfLookupSectionByName(ElfPack libMap, const std::string& sectionName)
112+
{
113+
Elf32_Shdr* secHeader = nullptr;
114+
115+
ElfForEachSection(libMap, [&](Elf32_Shdr* currSection){
116+
const char* currSectionName = ElfGetSectionName(libMap, currSection);
117+
118+
if(currSectionName == nullptr)
119+
return true;
120+
121+
if(strcmp(currSectionName, sectionName.c_str()))
122+
return true;
123+
124+
secHeader = currSection;
125+
126+
return false;
127+
});
128+
129+
return secHeader;
130+
}
131+
77132
Elf32_Shdr* ElfGetSymbolSection(ElfPack libMap)
78133
{
79134
Elf32_Shdr* result = nullptr;
80135

81-
result = ElfLookupSection(libMap, SHT_SYMTAB);
136+
result = ElfLookupSectionByType(libMap, SHT_SYMTAB);
82137

83138
if(result)
84139
return result;
85140

86-
result = ElfLookupSection(libMap, SHT_DYNSYM);
141+
result = ElfLookupSectionByType(libMap, SHT_DYNSYM);
87142

88143
if(result)
89144
return result;

src/jni/ElfUtils.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,20 @@
66

77
union ElfPack {
88
Elf32_Ehdr* header;
9+
Elf64_Ehdr* header64;
910
uintptr_t base;
1011
void* baseV;
1112
int res;
1213
};
1314

1415
bool ElfOpen(const std::string& fullModulePath, std::function<void(ElfPack libMap)> callback);
16+
bool ElfPeekIs64(const std::string& fullModulePath, bool& outResult);
1517
Elf32_Shdr* ElfSectionByIndex(ElfPack libMap, unsigned int sectionIdx);
1618
void ElfForEachSection(ElfPack libMap, std::function<bool(Elf32_Shdr* pCurrentSection)> callback);
17-
Elf32_Shdr* ElfLookupSection(ElfPack libMap, uint32_t sectionType);
19+
Elf32_Shdr* ElfLookupSectionByType(ElfPack libMap, uint32_t sectionType);
20+
const char* ElfGetSectionHeadersStringBlob(ElfPack libMap);
21+
const char* ElfGetSectionName(ElfPack libMap, Elf32_Shdr* sectionHdr);
22+
Elf32_Shdr* ElfLookupSectionByName(ElfPack libMap, const std::string& sectionName);
1823
Elf32_Shdr* ElfGetSymbolSection(ElfPack libMap);
1924
bool ElfForEachSymbol(ElfPack libMap, std::function<bool(Elf32_Sym* pCurrentSym, const char* pCurrSymName)> callback);
2025
bool ElfLookupSymbol(ElfPack libMap, const std::string& symbolName, uint64_t* outSymbolOff = nullptr);

0 commit comments

Comments
 (0)