use dladdr to get base on android

This commit is contained in:
qimiko 2024-01-12 19:26:24 -07:00
parent 9661693e72
commit b072c6bc31
No known key found for this signature in database
GPG key ID: D2D404DD810FE0E3

View file

@ -6,13 +6,11 @@
#if defined(GEODE_IS_ANDROID32)
static auto constexpr NEW_SYM = "_Znwj";
static constexpr uintptr_t MENULAYER_SCENE = 0x309068 - 0x10000;
static constexpr uintptr_t STRING_EMPTY = 0xaa1c48 - 0x10000 - 0xc; // the internal struct size
#elif defined(GEODE_IS_ANDROID64)
static auto constexpr NEW_SYM = "_Znwm";
static constexpr uintptr_t MENULAYER_SCENE = 0x6a62ec - 0x100000;
static constexpr uintptr_t STRING_EMPTY = 0x12d8568 - 0x100000 - 0x18; // the internal struct size
#endif
@ -22,19 +20,32 @@ static auto constexpr DELETE_SYM = "_ZdlPv";
// 2.2 addition
// zmx please fix this
namespace geode::base {
uintptr_t get() {
static uintptr_t base = (reinterpret_cast<uintptr_t>(&MenuLayer::scene) - MENULAYER_SCENE) & (~0x1);
// static uintptr_t base = reinterpret_cast<uintptr_t>(dlopen("libcocos2dcpp.so", RTLD_NOW));
return base;
}
}
static void* getLibHandle() {
static void* handle = dlopen("libcocos2dcpp.so", RTLD_LAZY | RTLD_NOLOAD);
return handle;
}
namespace geode::base {
uintptr_t get() {
static std::uintptr_t basePtr = 0u;
if (basePtr == 0u) {
auto handle = getLibHandle();
// JNI_OnLoad is present on all versions of GD
auto sym = dlsym(handle, "JNI_OnLoad");
assert(sym != nullptr);
Dl_info p;
auto dlAddrRes = dladdr(sym, &p);
assert(dlAddrRes != 0);
basePtr = reinterpret_cast<std::uintptr_t>(p.dli_fbase);
}
return basePtr;
}
}
static void* gdOperatorNew(size_t size) {
static auto fnPtr = reinterpret_cast<void*(*)(size_t)>(dlsym(getLibHandle(), NEW_SYM));
return fnPtr(size);