forked from crosire/blink
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blink.hpp
62 lines (52 loc) · 1.67 KB
/
blink.hpp
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/**
* Copyright (C) 2016 Patrick Mours. All rights reserved.
* License: https://github.com/crosire/blink#license
*/
#pragma once
#include <vector>
#include <string>
#include <filesystem>
#include <unordered_set>
#include <unordered_map>
void print(const char *message, size_t length);
inline void print(std::string message)
{
message += '\n';
print(message.data(), message.size());
}
namespace blink
{
class application
{
public:
application();
void run();
bool link(const std::filesystem::path &object_file);
template <typename T>
T read_symbol(const std::string &name) const
{
if (const auto it = _symbols.find(name); it != _symbols.end())
return *reinterpret_cast<T *>(it->second);
return T();
}
template <typename T = void, typename... Args>
T call_symbol(const std::string &name, Args... args) const
{
if (const auto it = _symbols.find(name); it != _symbols.end())
return reinterpret_cast<T(*)(Args...)>(it->second)(std::forward<Args>(args)...);
return T();
}
private:
template <typename SYMBOL_TYPE, typename HEADER_TYPE>
bool link(void *const object_file, const HEADER_TYPE &header);
bool read_debug_info(const uint8_t *image_base);
void read_import_address_table(const uint8_t *image_base);
std::string build_compile_command_line(const std::filesystem::path &source_file, std::filesystem::path &object_file) const;
uint8_t *_image_base = nullptr;
std::filesystem::path _source_dir;
std::vector<std::filesystem::path> _object_files;
std::vector<std::vector<std::filesystem::path>> _source_files;
std::unordered_map<std::string, void *> _symbols;
std::unordered_map<std::string, uint32_t> _last_modifications;
};
}