Skip to content

Commit 229ca82

Browse files
committed
Add hook macro & std::string api for C
1 parent 4b8088a commit 229ca82

File tree

3 files changed

+116
-2
lines changed

3 files changed

+116
-2
lines changed

CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ set(MODLOADER_VERSION "Preview 3")
1616
add_subdirectory(dep/funchook)
1717

1818
add_library(server_modloader SHARED include/modloader/hook.h include/modloader/statichook.h include/modloader/log.h
19-
include/modloader/loader.h
20-
src/hook.cpp src/log.cpp src/loader.h src/loader.cpp src/elf_helper.cpp src/elf_helper.h src/main.cpp)
19+
include/modloader/loader.h include/modloader/cutils.h
20+
src/hook.cpp src/log.cpp src/loader.h src/loader.cpp src/elf_helper.cpp src/elf_helper.h src/main.cpp
21+
src/cutils.cpp)
2122
target_link_libraries(server_modloader funchook)
2223
target_compile_definitions(server_modloader PRIVATE MODLOADER_VERSION="${MODLOADER_VERSION}")
2324
target_include_directories(server_modloader PUBLIC include/)

include/modloader/cutils.h

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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

src/cutils.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <modloader/cutils.h>
2+
#include <string>
3+
4+
void *std_string_string(const char *c_str) {
5+
auto str = new std::string;
6+
str->assign(c_str);
7+
return (void*)str;
8+
}
9+
10+
const char *std_string_c_str(void *str) {
11+
return ((std::string*)str)->c_str();
12+
}
13+
14+
void std_string_destroy(void *str) {
15+
((std::string*)str)->~basic_string();
16+
delete (std::string*)str;
17+
}

0 commit comments

Comments
 (0)