-
Notifications
You must be signed in to change notification settings - Fork 45
/
loading_main.cpp
41 lines (31 loc) · 1.21 KB
/
loading_main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <dlfcn.h>
#include <string>
void error_message(const std::string& str) {
std::cout << str << "\nbecause of: " << dlerror() << '\n';
}
void* load_function(void* library, const std::string& func) {
void* func_ptr = dlsym(library, func.c_str());
if (!func_ptr) {
error_message("Failed to load " + func);
exit(-1);
}
return func_ptr;
}
int main(int argc, char const *argv[]) {
const std::string library = "libdynamic.so";
const std::string func = "_Z4echoRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE";
using func_type = void (*) (const std::string&);
const std::string hashfunc = "_Z7fnvhashPKc";
using hashfunc_type = int (*) (const char*);
void* loaded_lib = dlopen(library.c_str(), RTLD_LAZY | RTLD_GLOBAL);
if (!loaded_lib) {
error_message("Failed to open " + library);
return -1;
}
func_type echo_ptr = reinterpret_cast<func_type>(load_function(loaded_lib, func));
hashfunc_type fnvhash_ptr = reinterpret_cast<hashfunc_type>(load_function(loaded_lib, hashfunc));
echo_ptr(std::string("linked: ") + argv[0]);
std::cout << "fnv \"check\" hash: " << fnvhash_ptr("check") << '\n';
return 0;
}