@@ -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+
4051Elf32_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+
77132Elf32_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;
0 commit comments