aboutsummaryrefslogtreecommitdiff
path: root/src/actual.h
blob: ae951b904298b6e8ba8d866f1cbdad74c3474d9b (plain)
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
#ifndef CHANGE_SRC_ACTUAL_FUNCTION_H_
#define CHANGE_SRC_ACTUAL_FUNCTION_H_

#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif

#include <dlfcn.h>
#include <memory>
#include <cstring>

#include "init/alloc.h"

namespace actual {

template <class Result, typename... Arguments>
using ptr = Result(*)(Arguments...);

template <typename FunctionPtr>
FunctionPtr get_ptr(const std::string& symbol_name) {
	init::dlsymContext guard;

	const void* symbol_address{ dlsym(RTLD_NEXT, symbol_name.c_str()) };

	FunctionPtr actual_function{};
	std::memcpy(&actual_function, &symbol_address, sizeof(symbol_address));

	return actual_function;
}

}

#endif  // CHANGE_SRC_ACTUAL_FUNCTION_H_