aboutsummaryrefslogtreecommitdiff
path: root/src/actual.h
blob: a8605906dd878703ced6c2c958036637efff0f4e (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
34
35
36
#ifndef CHANGE_SRC_ACTUAL_FUNCTION_H_
#define CHANGE_SRC_ACTUAL_FUNCTION_H_

#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif

#include <dlfcn.h>
#include <sys/mman.h>
#include <sys/uio.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_