geode/loader/include/Geode/platform/macos.hpp

40 lines
1.1 KiB
C++
Raw Normal View History

2022-07-30 12:24:03 -04:00
#pragma once
2022-10-30 14:59:20 -04:00
#include <cstring>
2022-07-30 12:24:03 -04:00
#include <mach-o/dyld.h>
#include <type_traits>
#include <typeinfo>
#include "ItaniumCast.hpp"
2022-07-30 12:24:03 -04:00
namespace geode {
2022-10-30 14:59:20 -04:00
using dylib_t = void*;
2022-07-30 12:24:03 -04:00
struct PlatformInfo {
2022-10-30 14:59:20 -04:00
dylib_t m_dylib;
2022-07-30 12:24:03 -04:00
};
}
namespace geode::base {
2022-10-30 14:59:20 -04:00
GEODE_NOINLINE inline uintptr_t get() {
2024-06-07 08:04:11 -04:00
// on arm macos, launching through steam seems to set image 0 to some steam helper library
// i don't know why... :(
static uintptr_t base = []() {
// dyld man page says this is unsafe but idc
auto image_count = _dyld_image_count();
for (auto i = 0u; i < image_count; i++) {
std::string_view image_name = _dyld_get_image_name(i);
if (!image_name.ends_with(".dylib")) {
return _dyld_get_image_vmaddr_slide(i) + 0x100000000;
}
}
// we couldn't find the base, so just assume it's 0
// should probably have an error for this, but geode::log isn't available yet
return _dyld_get_image_vmaddr_slide(0) + 0x100000000;
}();
2022-10-30 14:59:20 -04:00
return base;
}
2022-07-30 12:24:03 -04:00
}