diff options
author | Davide Beatrici <git@davidebeatrici.dev> | 2020-10-01 22:24:11 +0300 |
---|---|---|
committer | Davide Beatrici <git@davidebeatrici.dev> | 2020-10-01 22:24:11 +0300 |
commit | 4eee619eb19bcd1b9af186330f333d6eb5dece30 (patch) | |
tree | 164ae795ef5a2c74a252c4b218eb6143605ce968 /plugins | |
parent | e00af604ce26148e28b7b4bf55279889580bf8e7 (diff) |
REFAC(client): move common stuff from getExportedSymbol() to a template function
Thanks to @ZeroAbility for the idea.
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/mumble_plugin_linux.h | 115 |
1 files changed, 40 insertions, 75 deletions
diff --git a/plugins/mumble_plugin_linux.h b/plugins/mumble_plugin_linux.h index 461d58bb4..e4f3b2316 100644 --- a/plugins/mumble_plugin_linux.h +++ b/plugins/mumble_plugin_linux.h @@ -213,74 +213,38 @@ static inline bool peekProc(const procptr_t &addr, void *dest, const size_t &len return (nread != -1 && static_cast< size_t >(nread) == in.iov_len); } -static inline procptr_t getExportedSymbol(const std::string &symbol, const procptr_t module) { - if (isWin32) { - return getWin32ExportedSymbol(symbol, module); - } - +template< typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Dyn, typename Elf_Sym > +static inline procptr_t getExportedSymbolInternal(const std::string &symbol, const procptr_t module) { procptr_t hashTable = 0; procptr_t strTable = 0; procptr_t symTable = 0; - if (is64Bit) { - const auto ehdr = peekProc< Elf64_Ehdr >(module); - const auto phdr = peekProcVector< Elf64_Phdr >(module + ehdr.e_phoff, ehdr.e_phnum); - - for (size_t i = 0; i < phdr.size(); ++i) { - if (phdr[i].p_type == PT_DYNAMIC) { - const auto dyn = - peekProcVector< Elf64_Dyn >(module + phdr[i].p_vaddr, phdr[i].p_memsz / sizeof(Elf64_Dyn)); - - for (size_t j = 0; j < dyn.size(); ++j) { - switch (dyn[j].d_tag) { - case DT_HASH: - hashTable = dyn[j].d_un.d_ptr; - break; - case DT_STRTAB: - strTable = dyn[j].d_un.d_ptr; - break; - case DT_SYMTAB: - symTable = dyn[j].d_un.d_ptr; - break; - } - - if (hashTable && strTable && symTable) { - break; - } - } + const auto ehdr = peekProc< Elf_Ehdr >(module); + const auto phdr = peekProcVector< Elf_Phdr >(module + ehdr.e_phoff, ehdr.e_phnum); - break; - } - } - } else { - const auto ehdr = peekProc< Elf32_Ehdr >(module); - const auto phdr = peekProcVector< Elf32_Phdr >(module + ehdr.e_phoff, ehdr.e_phnum); - - for (size_t i = 0; i < phdr.size(); ++i) { - if (phdr[i].p_type == PT_DYNAMIC) { - const auto dyn = - peekProcVector< Elf32_Dyn >(module + phdr[i].p_vaddr, phdr[i].p_memsz / sizeof(Elf32_Dyn)); - - for (size_t j = 0; j < dyn.size(); ++j) { - switch (dyn[j].d_tag) { - case DT_HASH: - hashTable = dyn[j].d_un.d_ptr; - break; - case DT_STRTAB: - strTable = dyn[j].d_un.d_ptr; - break; - case DT_SYMTAB: - symTable = dyn[j].d_un.d_ptr; - break; - } - - if (hashTable && strTable && symTable) { + for (size_t i = 0; i < phdr.size(); ++i) { + if (phdr[i].p_type == PT_DYNAMIC) { + const auto dyn = peekProcVector< Elf_Dyn >(module + phdr[i].p_vaddr, phdr[i].p_memsz / sizeof(Elf_Dyn)); + + for (size_t j = 0; j < dyn.size(); ++j) { + switch (dyn[j].d_tag) { + case DT_HASH: + hashTable = dyn[j].d_un.d_ptr; + break; + case DT_STRTAB: + strTable = dyn[j].d_un.d_ptr; + break; + case DT_SYMTAB: + symTable = dyn[j].d_un.d_ptr; break; - } } - break; + if (hashTable && strTable && symTable) { + break; + } } + + break; } } @@ -291,29 +255,30 @@ static inline procptr_t getExportedSymbol(const std::string &symbol, const procp // uint32_t chain[nChain]; const auto nChain = peekProc< uint32_t >(hashTable + sizeof(uint32_t)); - if (is64Bit) { - for (uint32_t i = 0; i < nChain; ++i) { - const auto sym = peekProc< Elf64_Sym >(symTable + sizeof(Elf64_Sym) * i); - const auto name = peekProcString(strTable + sym.st_name, symbol.size()); + for (uint32_t i = 0; i < nChain; ++i) { + const auto sym = peekProc< Elf_Sym >(symTable + sizeof(Elf_Sym) * i); + const auto name = peekProcString(strTable + sym.st_name, symbol.size()); - if (name == symbol) { - return module + sym.st_value; - } - } - } else { - for (uint32_t i = 0; i < nChain; ++i) { - const auto sym = peekProc< Elf32_Sym >(symTable + sizeof(Elf32_Sym) * i); - const auto name = peekProcString(strTable + sym.st_name, symbol.size()); - - if (name == symbol) { - return module + sym.st_value; - } + if (name == symbol) { + return module + sym.st_value; } } return 0; } +static inline procptr_t getExportedSymbol(const std::string &symbol, const procptr_t module) { + if (isWin32) { + return getWin32ExportedSymbol(symbol, module); + } + + if (is64Bit) { + return getExportedSymbolInternal< Elf64_Ehdr, Elf64_Phdr, Elf64_Dyn, Elf64_Sym >(symbol, module); + } else { + return getExportedSymbolInternal< Elf32_Ehdr, Elf32_Phdr, Elf32_Dyn, Elf32_Sym >(symbol, module); + } +} + static void generic_unlock() { pModule = 0; pPid = 0; |