Skip to content

Commit

Permalink
Update elf util & lsplant to support A13B1 arm (LSPosed#1894)
Browse files Browse the repository at this point in the history
  • Loading branch information
yujincheng08 authored Apr 27, 2022
1 parent 177c2cd commit 7d5778a
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 23 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ A Riru / Zygisk module trying to provide an ART hooking framework which delivers
## Supported Versions

Android 8.1 ~ 13 DP2
Android 8.1 ~ 13 Beta 1

## Install

Expand Down
33 changes: 20 additions & 13 deletions core/src/main/jni/include/elf_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#define SANDHOOK_ELF_UTIL_H

#include <string_view>
#include <unordered_map>
#include <map>
#include <linux/elf.h>
#include <sys/types.h>
#include <link.h>
Expand All @@ -35,23 +35,26 @@ namespace SandHook {

ElfImg(std::string_view elf);

constexpr ElfW(Addr) getSymbOffset(std::string_view name) const {
return getSymbOffset(name, GnuHash(name), ElfHash(name));
}

constexpr ElfW(Addr) getSymbAddress(std::string_view name) const {
ElfW(Addr) offset = getSymbOffset(name);
template<typename T = void*>
requires(std::is_pointer_v<T>)
constexpr const T getSymbAddress(std::string_view name) const {
auto offset = getSymbOffset(name, GnuHash(name), ElfHash(name));
if (offset > 0 && base != nullptr) {
return static_cast<ElfW(Addr)>((uintptr_t) base + offset - bias);
return reinterpret_cast<T>(static_cast<ElfW(Addr)>((uintptr_t) base + offset - bias));
} else {
return 0;
return nullptr;
}
}

template<typename T>
template<typename T = void*>
requires(std::is_pointer_v<T>)
constexpr T getSymbAddress(std::string_view name) const {
return reinterpret_cast<T>(getSymbAddress(name));
constexpr const T getSymbPrefixFirstOffset(std::string_view prefix) const {
auto offset = PrefixLookupFirst(prefix);
if (offset > 0 && base != nullptr) {
return reinterpret_cast<T>(static_cast<ElfW(Addr)>((uintptr_t) base + offset - bias));
} else {
return nullptr;
}
}

bool isValid() const {
Expand All @@ -73,12 +76,16 @@ namespace SandHook {

ElfW(Addr) LinearLookup(std::string_view name) const;

ElfW(Addr) PrefixLookupFirst(std::string_view prefix) const;

constexpr static uint32_t ElfHash(std::string_view name);

constexpr static uint32_t GnuHash(std::string_view name);

bool findModuleBase();

void MayInitLinearMap() const;

std::string elf;
void *base = nullptr;
char *buffer = nullptr;
Expand Down Expand Up @@ -111,7 +118,7 @@ namespace SandHook {
uint32_t *gnu_bucket_;
uint32_t *gnu_chain_;

mutable std::unordered_map<std::string_view, ElfW(Sym) *> symtabs_;
mutable std::map<std::string_view, ElfW(Sym) *> symtabs_;
};

constexpr uint32_t ElfImg::ElfHash(std::string_view name) {
Expand Down
17 changes: 15 additions & 2 deletions core/src/main/jni/src/elf_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,8 @@ ElfW(Addr) ElfImg::GnuLookup(std::string_view name, uint32_t hash) const {
return 0;
}

ElfW(Addr) ElfImg::LinearLookup(std::string_view name) const {
void ElfImg::MayInitLinearMap() const {
if (symtabs_.empty()) {
symtabs_.reserve(symtab_count);
if (symtab_start != nullptr && symstr_offset_for_symtab != 0) {
for (ElfW(Off) i = 0; i < symtab_count; i++) {
unsigned int st_type = ELF_ST_TYPE(symtab_start[i].st_info);
Expand All @@ -180,13 +179,27 @@ ElfW(Addr) ElfImg::LinearLookup(std::string_view name) const {
}
}
}
}

ElfW(Addr) ElfImg::LinearLookup(std::string_view name) const {
MayInitLinearMap();
if (auto i = symtabs_.find(name); i != symtabs_.end()) {
return i->second->st_value;
} else {
return 0;
}
}

ElfW(Addr) ElfImg::PrefixLookupFirst(std::string_view prefix) const {
MayInitLinearMap();
if (auto i = symtabs_.lower_bound(prefix); i != symtabs_.end() && i->first.starts_with(prefix)) {
LOGD("found prefix {} of {} {:#x} in {} in symtab by linear lookup", prefix, i->first, i->second->st_value, elf);
return i->second->st_value;
} else {
return 0;
}
}


ElfImg::~ElfImg() {
//open elf file local
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/jni/src/jni/resources_hook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ namespace lspd {
}
return android::ResStringPool::setup(HookHandler{
.art_symbol_resolver = [&](auto s) {
return fw.template getSymbAddress<void*>(s);
return fw.template getSymbAddress(s);
}
});
}
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/jni/src/symbol_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ namespace lspd {
bool FindLibArt() {
auto &art = GetArt();
if (!art->isValid()) return false;
return symbol_cache->setTableOverride = art->getSymbAddress<void *>(
return symbol_cache->setTableOverride = art->getSymbAddress(
"_ZN3art9JNIEnvExt16SetTableOverrideEPK18JNINativeInterface");
}

Expand All @@ -60,7 +60,7 @@ namespace lspd {
return;
}
auto ok = FindLibArt();
symbol_cache->do_dlopen = SandHook::ElfImg("/linker").getSymbAddress<void *>(
symbol_cache->do_dlopen = SandHook::ElfImg("/linker").getSymbAddress(
"__dl__Z9do_dlopenPKciPK17android_dlextinfoPKv");
if (!ok) [[unlikely]] {
GetArt(true);
Expand Down
2 changes: 1 addition & 1 deletion magisk-loader/magisk_module/module.prop
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ name=${api} - LSPosed
version=${versionName} (${versionCode})
versionCode=${versionCode}
author=${authorList}
description=Another enhanced implementation of Xposed Framework. Supports Android 8.1 ~ 13 DP2. ${requirement}.
description=Another enhanced implementation of Xposed Framework. Supports Android 8.1 ~ 13 Beta 1. ${requirement}.
updateJson=${updateJson}
10 changes: 8 additions & 2 deletions magisk-loader/src/main/jni/src/magisk_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,10 @@ namespace lspd {
return UnhookFunction(t) == RT_SUCCESS ;
},
.art_symbol_resolver = [](auto symbol) {
return GetArt()->getSymbAddress<void*>(symbol);
return GetArt()->getSymbAddress(symbol);
},
.art_symbol_prefix_resolver = [](auto symbol) {
return GetArt()->getSymbPrefixFirstOffset(symbol);
},
};
InitHooks(env, initInfo);
Expand Down Expand Up @@ -199,7 +202,10 @@ namespace lspd {
return UnhookFunction(t) == RT_SUCCESS;
},
.art_symbol_resolver = [](auto symbol){
return GetArt()->getSymbAddress<void*>(symbol);
return GetArt()->getSymbAddress(symbol);
},
.art_symbol_prefix_resolver = [](auto symbol) {
return GetArt()->getSymbPrefixFirstOffset(symbol);
},
};
auto [dex_fd, size] = instance->RequestLSPDex(env, binder);
Expand Down

0 comments on commit 7d5778a

Please sign in to comment.