symbol-processor/data/out.h

174 lines
4.5 KiB
C
Raw Normal View History

2024-07-14 09:03:18 +00:00
#pragma once
// Check Architecture
#ifndef __arm__
#error "Symbols Are ARM-Only"
#endif
// Headers
2024-08-02 01:57:31 +00:00
#include <variant>
#include <functional>
2024-07-14 09:03:18 +00:00
#include <cstddef>
#include <string>
#include <vector>
#include <map>
#include <type_traits>
#include <cstring>
2024-12-18 06:19:11 +00:00
// Internal Macros
#define __PREVENT_DESTRUCTION(self) \
~self() = delete
#define __PREVENT_CONSTRUCTION(self) \
self() = delete; \
__PREVENT_DESTRUCTION(self)
#define __PREVENT_COPY(self) \
self(const self &) = delete; \
self &operator=(const self &) = delete
2024-08-02 01:57:31 +00:00
// Virtual Function Information
2024-12-18 06:19:11 +00:00
struct __VirtualFunctionInfo {
// Constructors
template <typename Ret, typename Self, typename Super, typename... Args>
__VirtualFunctionInfo(Ret (**const addr_)(Self, Args...), Ret (*const parent_)(Super, Args...)):
addr((void **) addr_),
parent((void *) parent_) {}
template <typename T>
__VirtualFunctionInfo(T **const addr_, const std::nullptr_t parent_):
__VirtualFunctionInfo(addr_, (T *) parent_) {}
// Method
2024-08-02 01:57:31 +00:00
[[nodiscard]] bool can_overwrite() const {
2024-12-18 06:19:11 +00:00
return *addr != parent;
2024-08-02 01:57:31 +00:00
}
2024-12-18 06:19:11 +00:00
// Properties
void **const addr;
2024-08-02 01:57:31 +00:00
void *const parent;
};
// Thunks
typedef void *(*thunk_enabler_t)(void *target, void *thunk);
extern thunk_enabler_t thunk_enabler;
// Function Information
2024-12-18 06:19:11 +00:00
template <typename T>
class __Function;
2024-08-02 01:57:31 +00:00
template <typename Ret, typename... Args>
class __Function<Ret(Args...)> {
2024-12-18 06:19:11 +00:00
// Prevent Copying
__PREVENT_COPY(__Function);
__PREVENT_DESTRUCTION(__Function);
2024-08-02 01:57:31 +00:00
public:
// Types
2024-12-18 06:19:11 +00:00
typedef Ret (*ptr_type)(Args...);
typedef std::function<Ret(Args...)> type;
typedef std::function<Ret(const type &, Args...)> overwrite_type;
2024-08-02 01:57:31 +00:00
// Normal Function
2024-12-18 06:19:11 +00:00
__Function(const std::string name_, const ptr_type thunk_, const ptr_type func_):
2024-08-02 01:57:31 +00:00
func(func_),
enabled(true),
name(name_),
backup(func_),
thunk(thunk_) {}
// Virtual Function
2024-12-18 06:19:11 +00:00
template <typename Parent>
__Function(const std::string name_, const ptr_type thunk_, ptr_type *const func_, const Parent parent):
2024-08-02 01:57:31 +00:00
func(__VirtualFunctionInfo(func_, parent)),
2024-12-18 06:19:11 +00:00
enabled(std::get<__VirtualFunctionInfo>(func).can_overwrite()),
2024-08-02 01:57:31 +00:00
name(name_),
backup(*get_vtable_addr()),
thunk(thunk_) {}
// Overwrite Function
[[nodiscard]] bool overwrite(overwrite_type target) {
// Check If Enabled
if (!enabled) {
return false;
}
// Enable Thunk
enable_thunk();
// Overwrite
type original = get_thunk_target();
thunk_target = [original, target](Args... args) {
return target(original, std::forward<Args>(args)...);
};
return true;
}
// Getters
[[nodiscard]] ptr_type get(bool result_will_be_stored) {
if (!enabled) {
return nullptr;
} else {
if (result_will_be_stored) {
enable_thunk();
}
if (is_virtual()) {
return *get_vtable_addr();
} else {
return std::get<ptr_type>(func);
}
}
}
[[nodiscard]] ptr_type *get_vtable_addr() const {
2024-12-18 06:19:11 +00:00
return (ptr_type *) std::get<__VirtualFunctionInfo>(func).addr;
2024-08-02 01:57:31 +00:00
}
[[nodiscard]] type get_thunk_target() const {
if (thunk_target) {
return thunk_target;
} else {
return backup;
}
}
private:
// Current Function
2024-12-18 06:19:11 +00:00
std::variant<ptr_type, __VirtualFunctionInfo> func;
2024-08-02 01:57:31 +00:00
[[nodiscard]] bool is_virtual() const {
return func.index() == 1;
}
public:
// State
const bool enabled;
2024-12-18 06:19:11 +00:00
const std::string name;
2024-08-02 01:57:31 +00:00
// Backup Of Original Function Pointer
const ptr_type backup;
private:
// Thunk
const ptr_type thunk;
type thunk_target;
bool thunk_enabled = false;
void enable_thunk() {
if (!thunk_enabled && enabled) {
ptr_type real_thunk = (ptr_type) thunk_enabler((void *) backup, (void *) thunk);
if (!is_virtual()) {
func = real_thunk;
}
thunk_enabled = true;
}
}
};
2024-07-14 09:03:18 +00:00
// Shortcuts
typedef unsigned char uchar;
typedef unsigned short ushort;
typedef unsigned int uint;
// Forward Declarations
{{ forwardDeclarations }}
// Extra Headers
{{ extraHeaders }}
// Warnings
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Winvalid-offsetof"
#pragma GCC diagnostic ignored "-Wshadow"
{{ main }}
// Cleanup Warnings
#pragma GCC diagnostic pop