-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction_fetcher.hpp
209 lines (180 loc) · 4.26 KB
/
function_fetcher.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#pragma once
#include <dlfcn.h>
#include <assert.h>
/* functions implemented in GNU libc that we can use to get
* symbols without directly invoking dlopen/dlsym.
*/
extern "C" void *__libc_dlopen_mode(const char *filename, int flag);
extern "C" void *__libc_dlsym(void *handle, const char *symbol);
/* calls the "real" dlsym() by calling the __libc_ functions
* to get the dlsym symbol in libdl.so.
*/
static
void*
real_dlsym(void *handle, const char *symbol)
{
typedef void* (*fptr_type)(void*, const char*);
static fptr_type fptr = nullptr;
if (!fptr)
{
void *libdl;
libdl = __libc_dlopen_mode("libdl.so", RTLD_LOCAL | RTLD_NOW);
if (libdl)
{
fptr = (fptr_type)__libc_dlsym(libdl, "dlsym");
}
if (!fptr)
{
assert(!"Warning: unable to get dlsym unnaturally");
return nullptr;
}
}
return fptr(handle, symbol);
}
namespace
{
class FunctionFetcher
{
public:
void*
dlsym_lib_style(const char *symbol)
{
void *return_value;
if (!m_handle)
{
m_handle = __libc_dlopen_mode(m_name, RTLD_GLOBAL | RTLD_LAZY | RTLD_DEEPBIND);
if (!m_handle)
{
m_handle = __libc_dlopen_mode(m_fallback, RTLD_GLOBAL | RTLD_LAZY | RTLD_DEEPBIND);
m_name = m_fallback;
}
if (m_handle)
{
std::printf("i965-blackbox: Using \"%s\" for %s\n", m_name, m_environ);
}
else
{
return_value = real_dlsym(RTLD_NEXT, symbol);
std::printf("i965-blackbox: Using value RTLD_NEXT for %s\n", m_environ);
m_handle = RTLD_NEXT;
return return_value;
}
}
return real_dlsym(m_handle, symbol);
}
static
FunctionFetcher&
gl(void)
{
static FunctionFetcher r("I965_BLACKBOX_GL_LIB", "libGL.so");
return r;
}
static
FunctionFetcher&
gles(void)
{
static FunctionFetcher r("I965_BLACKBOX_GLES_LIB", "libGLESv2.so");
return r;
}
static
FunctionFetcher&
egl(void)
{
static FunctionFetcher r("I965_BLACKBOX_EGL_LIB", "libEGL.so");
return r;
}
private:
FunctionFetcher(const char *environ_var,
const char *fallback_lib):
m_handle(nullptr),
m_fallback(fallback_lib),
m_environ(environ_var)
{
m_name = std::getenv(m_environ);
if (!m_name)
{
m_name = m_fallback;
}
}
void *m_handle;
const char *m_name;
const char *m_fallback;
const char *m_environ;
};
}
static
void*
gl_dlsym(const char *symbol)
{
typedef void* (*fptr_type)(const char*);
static fptr_type fptr1 = nullptr;
static fptr_type fptr2 = nullptr;
void *return_value = nullptr;
if (!fptr1)
{
fptr1 = (fptr_type)FunctionFetcher::gl().dlsym_lib_style("glXGetProcAddress");
}
if (!fptr2)
{
fptr2 = (fptr_type)FunctionFetcher::gl().dlsym_lib_style("glXGetProcAddressARB");
}
if (fptr1)
{
return_value = fptr1(symbol);
if (return_value)
{
return return_value;
}
}
if (fptr2)
{
return_value = fptr2(symbol);
if (return_value)
{
return return_value;
}
}
return FunctionFetcher::gl().dlsym_lib_style(symbol);
}
static
void*
gles_dlsym(const char *symbol)
{
typedef void* (*fptr_type)(const char*);
static fptr_type fptr = nullptr;
void *return_value = nullptr;
if (!fptr)
{
fptr = (fptr_type)FunctionFetcher::egl().dlsym_lib_style("eglGetProcAddress");
}
if (fptr)
{
return_value = fptr(symbol);
}
if (!return_value)
{
return_value = FunctionFetcher::gles().dlsym_lib_style(symbol);
}
return return_value;
}
static
void*
egl_dlsym(const char *symbol)
{
typedef void* (*fptr_type)(const char*);
static fptr_type fptr = nullptr;
void *return_value = nullptr;
if (!fptr)
{
fptr = (fptr_type)FunctionFetcher::egl().dlsym_lib_style("eglGetProcAddress");
}
if (fptr)
{
return_value = fptr(symbol);
}
if (!return_value)
{
return_value = FunctionFetcher::egl().dlsym_lib_style(symbol);
}
return return_value;
}