VoltMod
C++23 framework for CS2 server plugins
Loading...
Searching...
No Matches
Loader.linux.cpp
Go to the documentation of this file.
1#include "Loader/Loader.hpp"
2
3#include <dlfcn.h>
4
5namespace VoltMod
6{
7
8void* OpenModule(const std::filesystem::path& path)
9{
10 return dlopen(path.c_str(), RTLD_NOW);
11}
12
13void* FindExport(void* module, const char* name)
14{
15 return dlsym(module, name);
16}
17
18std::string LastError()
19{
20 const char* message = dlerror();
21 return message != nullptr ? message : "no message";
22}
23
24std::filesystem::path OwnPath()
25{
26 // Not &CreateInterface: another module's export of that name can interpose it.
27 static const char inThisModule = 0;
28 Dl_info info{};
30 // The engine may load this by a relative path.
31 return std::filesystem::absolute(info.dli_fname).lexically_normal();
32}
33
35{
36 Dl_info info{};
37 if (dladdr(address, &info) == 0)
38 {
39 return nullptr;
40 }
41 void* module = dlopen(info.dli_fname, RTLD_NOW | RTLD_NOLOAD);
42 if (module == nullptr)
43 {
44 return nullptr;
45 }
46 auto* factory = reinterpret_cast<InterfaceFactory>(dlsym(module, "CreateInterface"));
47 dlclose(module); // RTLD_NOLOAD still takes a reference
48 return factory;
49}
50
51} // namespace VoltMod
static std::string LastError()
static std::string ReadFile(const std::filesystem::path &path)
Definition Loader.cpp:56
void *(*)(const char *name, int *returnCode) InterfaceFactory
std::filesystem::path OwnPath()
InterfaceFactory FactoryAt(const void *address)
void * OpenModule(const std::filesystem::path &path)
static void * FindExport(void *handle, const char *name)