Fix name of elf util (#1930)

This commit is contained in:
LoveSy 2022-05-11 16:11:33 +08:00 committed by GitHub
parent b36c170b8c
commit dce14b953e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 3 deletions

View File

@ -25,6 +25,7 @@
#include <linux/elf.h> #include <linux/elf.h>
#include <sys/types.h> #include <sys/types.h>
#include <link.h> #include <link.h>
#include <vector>
#include "config.h" #include "config.h"
#define SHT_GNU_HASH 0x6ffffff6 #define SHT_GNU_HASH 0x6ffffff6
@ -48,7 +49,7 @@ namespace SandHook {
template<typename T = void*> template<typename T = void*>
requires(std::is_pointer_v<T>) requires(std::is_pointer_v<T>)
constexpr const T getSymbPrefixFirstOffset(std::string_view prefix) const { constexpr const T getSymbPrefixFirstAddress(std::string_view prefix) const {
auto offset = PrefixLookupFirst(prefix); auto offset = PrefixLookupFirst(prefix);
if (offset > 0 && base != nullptr) { if (offset > 0 && base != nullptr) {
return reinterpret_cast<T>(static_cast<ElfW(Addr)>((uintptr_t) base + offset - bias)); return reinterpret_cast<T>(static_cast<ElfW(Addr)>((uintptr_t) base + offset - bias));
@ -57,6 +58,18 @@ namespace SandHook {
} }
} }
template<typename T = void*>
requires(std::is_pointer_v<T>)
const std::vector<T> getAllSymbAddress(std::string_view name) const {
auto offsets = LinearRangeLookup(name);
std::vector<T> res;
res.reserve(offsets.size());
for (const auto &offset : offsets) {
res.emplace_back(reinterpret_cast<T>(static_cast<ElfW(Addr)>((uintptr_t) base + offset - bias)));
}
return res;
}
bool isValid() const { bool isValid() const {
return base != nullptr; return base != nullptr;
} }
@ -76,6 +89,8 @@ namespace SandHook {
ElfW(Addr) LinearLookup(std::string_view name) const; ElfW(Addr) LinearLookup(std::string_view name) const;
std::vector<ElfW(Addr)> LinearRangeLookup(std::string_view name) const;
ElfW(Addr) PrefixLookupFirst(std::string_view prefix) const; ElfW(Addr) PrefixLookupFirst(std::string_view prefix) const;
constexpr static uint32_t ElfHash(std::string_view name); constexpr static uint32_t ElfHash(std::string_view name);

View File

@ -190,6 +190,17 @@ ElfW(Addr) ElfImg::LinearLookup(std::string_view name) const {
} }
} }
std::vector<ElfW(Addr)> ElfImg::LinearRangeLookup(std::string_view name) const {
MayInitLinearMap();
std::vector<ElfW(Addr)> res;
for (auto [i, end] = symtabs_.equal_range(name); i != end; ++i) {
auto offset = i->second->st_value;
res.emplace_back(offset);
LOGD("found {} {:#x} in {} in symtab by linear range lookup", name, offset, elf);
}
return res;
}
ElfW(Addr) ElfImg::PrefixLookupFirst(std::string_view prefix) const { ElfW(Addr) ElfImg::PrefixLookupFirst(std::string_view prefix) const {
MayInitLinearMap(); MayInitLinearMap();
if (auto i = symtabs_.lower_bound(prefix); i != symtabs_.end() && i->first.starts_with(prefix)) { if (auto i = symtabs_.lower_bound(prefix); i != symtabs_.end() && i->first.starts_with(prefix)) {

View File

@ -131,7 +131,7 @@ namespace lspd {
return GetArt()->getSymbAddress(symbol); return GetArt()->getSymbAddress(symbol);
}, },
.art_symbol_prefix_resolver = [](auto symbol) { .art_symbol_prefix_resolver = [](auto symbol) {
return GetArt()->getSymbPrefixFirstOffset(symbol); return GetArt()->getSymbPrefixFirstAddress(symbol);
}, },
}; };
InitHooks(env, initInfo); InitHooks(env, initInfo);
@ -205,7 +205,7 @@ namespace lspd {
return GetArt()->getSymbAddress(symbol); return GetArt()->getSymbAddress(symbol);
}, },
.art_symbol_prefix_resolver = [](auto symbol) { .art_symbol_prefix_resolver = [](auto symbol) {
return GetArt()->getSymbPrefixFirstOffset(symbol); return GetArt()->getSymbPrefixFirstAddress(symbol);
}, },
}; };
auto [dex_fd, size] = instance->RequestLSPDex(env, binder); auto [dex_fd, size] = instance->RequestLSPDex(env, binder);