|
| 1 | +#ifndef MODLOADER_CUTILS_H |
| 2 | +#define MODLOADER_CUTILS_H |
| 3 | + |
| 4 | +#include <modloader/hook.h> |
| 5 | + |
| 6 | +#include <stdlib.h> |
| 7 | +#include <stdint.h> |
| 8 | +#include <stdbool.h> |
| 9 | +#include <string.h> |
| 10 | + |
| 11 | +#ifndef RTLD_DEFAULT |
| 12 | +#define RTLD_DEFAULT NULL |
| 13 | +#endif |
| 14 | + |
| 15 | +#define SYM(sym) \ |
| 16 | + dlsym(RTLD_DEFAULT, sym) |
| 17 | + |
| 18 | + |
| 19 | +#define SYMCALL(sym, func_proto, ...) \ |
| 20 | + ((func_proto) \ |
| 21 | + (SYM(sym))) \ |
| 22 | + (__VA_ARGS__) |
| 23 | + |
| 24 | + |
| 25 | +#define VIRTUAL_CALL(ptr, func_proto, ...) \ |
| 26 | + ((func_proto) \ |
| 27 | + ((void *)ptr)) \ |
| 28 | + (__VA_ARGS__) |
| 29 | + |
| 30 | + |
| 31 | +#define DEREFERENCE(type, ptr, offset) \ |
| 32 | + (*(type*)((uintptr_t)ptr + offset)) |
| 33 | + |
| 34 | + |
| 35 | +#define REFERENCE(type, ptr, offset) \ |
| 36 | + (type*)((uintptr_t)ptr + offset) |
| 37 | + |
| 38 | + |
| 39 | +// for uintptr_t |
| 40 | +#define PTR_OFFSET(ptr, offset) \ |
| 41 | + ((uintptr_t)ptr + offset) |
| 42 | + |
| 43 | + |
| 44 | +#define THOOK(name, ret_type, sym, ...) \ |
| 45 | + typedef ret_type (*_##name##_t)(__VA_ARGS__); \ |
| 46 | + _##name##_t _original_##name = NULL; \ |
| 47 | + ret_type _detour_##name(__VA_ARGS__); \ |
| 48 | + void _install_##name(void) __attribute__((constructor));\ |
| 49 | + void _destroy_##name(void); \ |
| 50 | + \ |
| 51 | + struct _##name { \ |
| 52 | + _##name##_t hook; \ |
| 53 | + _##name##_t detour; \ |
| 54 | + _##name##_t original; \ |
| 55 | + void (*install)(void); \ |
| 56 | + void (*destroy)(void); \ |
| 57 | + } name = {NULL, NULL, NULL, \ |
| 58 | + _install_##name, _destroy_##name}; \ |
| 59 | + \ |
| 60 | + void _install_##name(void) \ |
| 61 | + { \ |
| 62 | + modloader_hook_t *_hook_##name = \ |
| 63 | + modloader_hook(SYM(sym), \ |
| 64 | + _detour_##name, \ |
| 65 | + (void**)&_original_##name); \ |
| 66 | + name.hook = _hook_##name; \ |
| 67 | + name.detour = _detour_##name; \ |
| 68 | + name.original = _original_##name; \ |
| 69 | + } \ |
| 70 | + \ |
| 71 | + void _destroy_##name(void) \ |
| 72 | + { \ |
| 73 | + modloader_destroy_hook(name.hook); \ |
| 74 | + } \ |
| 75 | + \ |
| 76 | + ret_type _detour_##name(__VA_ARGS__) |
| 77 | + |
| 78 | +#ifdef __cplusplus |
| 79 | +extern "C" { |
| 80 | +#endif |
| 81 | + |
| 82 | + |
| 83 | +// std::string::basic_string(const char *c_str) |
| 84 | +void *std_string_string(const char *c_str); |
| 85 | + |
| 86 | +// std::string::c_str() |
| 87 | +const char *std_string_c_str(void *str); |
| 88 | + |
| 89 | +// std::string::~basic_string() |
| 90 | +void std_string_destroy(void *str); |
| 91 | + |
| 92 | +#ifdef __cplusplus |
| 93 | +} |
| 94 | +#endif |
| 95 | + |
| 96 | +#endif |
0 commit comments