Skip to content

Commit 56a1ed1

Browse files
committed
Add link-time library loader
Which does not call dlsym or equivalents, but links directly against the library and dispatches on name. This can be useful on platforms without (working) dlsym-implementations, without affecting the structure of lua-https too much.
1 parent ded519c commit 56a1ed1

File tree

3 files changed

+76
-1
lines changed

3 files changed

+76
-1
lines changed

src/CMakeLists.txt

+9-1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ add_library (https-unix-libraryloader STATIC EXCLUDE_FROM_ALL
5151
generic/UnixLibraryLoader.cpp
5252
)
5353

54+
add_library (https-linktime-libraryloader STATIC EXCLUDE_FROM_ALL
55+
generic/LinktimeLibraryLoader.cpp
56+
)
57+
5458
add_library (https-curl STATIC EXCLUDE_FROM_ALL
5559
generic/CurlClient.cpp
5660
)
@@ -137,7 +141,7 @@ elseif (ANDROID)
137141
endif ()
138142
option (DEBUG_SCHANNEL "Enable debug output in schannel backend" OFF)
139143
set (LIBRARY_LOADER ${LIBRARY_LOADER_DEFAULT} CACHE STRING "Which method to use to dynamically load libraries")
140-
set_property (CACHE LIBRARY_LOADER PROPERTY STRINGS "unix;windows")
144+
set_property (CACHE LIBRARY_LOADER PROPERTY STRINGS "unix;windows;linktime")
141145

142146
set_target_properties(https PROPERTIES PREFIX "")
143147

@@ -148,6 +152,7 @@ if (USE_CURL_BACKEND)
148152
find_package (CURL REQUIRED)
149153
include_directories (${CURL_INCLUDE_DIRS})
150154
target_link_libraries (https https-curl)
155+
target_link_libraries (https-linktime-libraryloader ${CURL_LIBRARY})
151156
endif ()
152157

153158
if (USE_OPENSSL_BACKEND)
@@ -195,6 +200,9 @@ if ("${LIBRARY_LOADER}" STREQUAL "unix")
195200
elseif ("${LIBRARY_LOADER}" STREQUAL "windows")
196201
set(HTTPS_LIBRARY_LOADER_WINDOWS ON)
197202
target_link_libraries (https https-windows-libraryloader)
203+
elseif ("${LIBRARY_LOADER}" STREQUAL "linktime")
204+
set(HTTPS_LIBRARY_LOADER_LINKTIME ON)
205+
target_link_libraries (https https-linktime-libraryloader)
198206
else ()
199207
message(WARNING "No library loader selected, backends that depend on dynamic loading will be broken")
200208
endif ()

src/common/config-generated.h.in

+1
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@
88
#cmakedefine DEBUG_SCHANNEL
99
#cmakedefine HTTPS_LIBRARY_LOADER_WINDOWS
1010
#cmakedefine HTTPS_LIBRARY_LOADER_UNIX
11+
#cmakedefine HTTPS_LIBRARY_LOADER_LINKTIME

src/generic/LinktimeLibraryLoader.cpp

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include "../common/config.h"
2+
#include "../common/LibraryLoader.h"
3+
4+
#ifdef HTTPS_LIBRARY_LOADER_LINKTIME
5+
6+
#include <cstring>
7+
8+
#ifdef HTTPS_BACKEND_CURL
9+
#include <curl/curl.h>
10+
11+
static char CurlHandle;
12+
#endif
13+
14+
#if defined(HTTPS_BACKEND_OPENSSL) || defined(HTTPS_BACKEND_ANDROID)
15+
# error "Selected backends that are not compatible with this loader"
16+
#endif
17+
18+
namespace LibraryLoader
19+
{
20+
handle *OpenLibrary(const char *name)
21+
{
22+
#ifdef HTTPS_BACKEND_CURL
23+
if (strstr(name, "libcurl") == name)
24+
return reinterpret_cast<handle *>(&CurlHandle);
25+
#endif
26+
return nullptr;
27+
}
28+
29+
void CloseLibrary(handle *)
30+
{
31+
}
32+
33+
handle* GetCurrentProcessHandle()
34+
{
35+
return nullptr;
36+
}
37+
38+
function *GetFunction(handle *handle, const char *name)
39+
{
40+
#define RETURN_MATCHING_FUNCTION(func) \
41+
if (strcmp(name, #func) == 0) \
42+
return reinterpret_cast<function *>(&func);
43+
44+
#ifdef HTTPS_BACKEND_CURL
45+
if (handle == &CurlHandle)
46+
{
47+
RETURN_MATCHING_FUNCTION(curl_global_init);
48+
RETURN_MATCHING_FUNCTION(curl_global_cleanup);
49+
RETURN_MATCHING_FUNCTION(curl_easy_init);
50+
RETURN_MATCHING_FUNCTION(curl_easy_cleanup);
51+
RETURN_MATCHING_FUNCTION(curl_easy_setopt);
52+
RETURN_MATCHING_FUNCTION(curl_easy_perform);
53+
RETURN_MATCHING_FUNCTION(curl_easy_getinfo);
54+
RETURN_MATCHING_FUNCTION(curl_slist_append);
55+
RETURN_MATCHING_FUNCTION(curl_slist_free_all);
56+
}
57+
#endif
58+
59+
#undef RETURN_MATCHING_FUNCTION
60+
61+
return nullptr;
62+
}
63+
}
64+
65+
#endif // HTTPS_LIBRARY_LOADER_LINKTIME
66+

0 commit comments

Comments
 (0)