Skip to content

Selectable library loaders, including link-time #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
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.
  • Loading branch information
bartbes committed Apr 7, 2024
commit 56a1ed18c98714104aea9408094ea9fb74f8c09a
10 changes: 9 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ add_library (https-unix-libraryloader STATIC EXCLUDE_FROM_ALL
generic/UnixLibraryLoader.cpp
)

add_library (https-linktime-libraryloader STATIC EXCLUDE_FROM_ALL
generic/LinktimeLibraryLoader.cpp
)

add_library (https-curl STATIC EXCLUDE_FROM_ALL
generic/CurlClient.cpp
)
Expand Down Expand Up @@ -137,7 +141,7 @@ elseif (ANDROID)
endif ()
option (DEBUG_SCHANNEL "Enable debug output in schannel backend" OFF)
set (LIBRARY_LOADER ${LIBRARY_LOADER_DEFAULT} CACHE STRING "Which method to use to dynamically load libraries")
set_property (CACHE LIBRARY_LOADER PROPERTY STRINGS "unix;windows")
set_property (CACHE LIBRARY_LOADER PROPERTY STRINGS "unix;windows;linktime")

set_target_properties(https PROPERTIES PREFIX "")

Expand All @@ -148,6 +152,7 @@ if (USE_CURL_BACKEND)
find_package (CURL REQUIRED)
include_directories (${CURL_INCLUDE_DIRS})
target_link_libraries (https https-curl)
target_link_libraries (https-linktime-libraryloader ${CURL_LIBRARY})
endif ()

if (USE_OPENSSL_BACKEND)
Expand Down Expand Up @@ -195,6 +200,9 @@ if ("${LIBRARY_LOADER}" STREQUAL "unix")
elseif ("${LIBRARY_LOADER}" STREQUAL "windows")
set(HTTPS_LIBRARY_LOADER_WINDOWS ON)
target_link_libraries (https https-windows-libraryloader)
elseif ("${LIBRARY_LOADER}" STREQUAL "linktime")
set(HTTPS_LIBRARY_LOADER_LINKTIME ON)
target_link_libraries (https https-linktime-libraryloader)
else ()
message(WARNING "No library loader selected, backends that depend on dynamic loading will be broken")
endif ()
Expand Down
1 change: 1 addition & 0 deletions src/common/config-generated.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
#cmakedefine DEBUG_SCHANNEL
#cmakedefine HTTPS_LIBRARY_LOADER_WINDOWS
#cmakedefine HTTPS_LIBRARY_LOADER_UNIX
#cmakedefine HTTPS_LIBRARY_LOADER_LINKTIME
66 changes: 66 additions & 0 deletions src/generic/LinktimeLibraryLoader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include "../common/config.h"
#include "../common/LibraryLoader.h"

#ifdef HTTPS_LIBRARY_LOADER_LINKTIME

#include <cstring>

#ifdef HTTPS_BACKEND_CURL
#include <curl/curl.h>

static char CurlHandle;
#endif

#if defined(HTTPS_BACKEND_OPENSSL) || defined(HTTPS_BACKEND_ANDROID)
# error "Selected backends that are not compatible with this loader"
#endif

namespace LibraryLoader
{
handle *OpenLibrary(const char *name)
{
#ifdef HTTPS_BACKEND_CURL
if (strstr(name, "libcurl") == name)
return reinterpret_cast<handle *>(&CurlHandle);
#endif
return nullptr;
}

void CloseLibrary(handle *)
{
}

handle* GetCurrentProcessHandle()
{
return nullptr;
}

function *GetFunction(handle *handle, const char *name)
{
#define RETURN_MATCHING_FUNCTION(func) \
if (strcmp(name, #func) == 0) \
return reinterpret_cast<function *>(&func);

#ifdef HTTPS_BACKEND_CURL
if (handle == &CurlHandle)
{
RETURN_MATCHING_FUNCTION(curl_global_init);
RETURN_MATCHING_FUNCTION(curl_global_cleanup);
RETURN_MATCHING_FUNCTION(curl_easy_init);
RETURN_MATCHING_FUNCTION(curl_easy_cleanup);
RETURN_MATCHING_FUNCTION(curl_easy_setopt);
RETURN_MATCHING_FUNCTION(curl_easy_perform);
RETURN_MATCHING_FUNCTION(curl_easy_getinfo);
RETURN_MATCHING_FUNCTION(curl_slist_append);
RETURN_MATCHING_FUNCTION(curl_slist_free_all);
}
#endif

#undef RETURN_MATCHING_FUNCTION

return nullptr;
}
}

#endif // HTTPS_LIBRARY_LOADER_LINKTIME