diff options
author | Andon Andonov <anandono@microsoft.com> | 2018-04-11 23:02:46 +0300 |
---|---|---|
committer | Jan Kotas <jkotas@microsoft.com> | 2018-04-11 23:02:46 +0300 |
commit | 04305ff98c5ad24d7001795b3b39ec52c39c72b8 (patch) | |
tree | fca8d394fc82416e0d1dea7fa443681bccf7b973 /src | |
parent | 3b8b65ee87fd5c5c12fddc7a969a2a987bbbf1a7 (diff) |
Port dynamic load checks from CoreCLR (#5683)
Diffstat (limited to 'src')
3 files changed, 31 insertions, 0 deletions
diff --git a/src/Native/System.Private.CoreLib.Native/config.h.in b/src/Native/System.Private.CoreLib.Native/config.h.in index 80a074dab..c7c3aafd9 100644 --- a/src/Native/System.Private.CoreLib.Native/config.h.in +++ b/src/Native/System.Private.CoreLib.Native/config.h.in @@ -5,3 +5,4 @@ #cmakedefine01 HAVE_CLOCK_MONOTONIC_COARSE #cmakedefine01 HAVE_MACH_ABSOLUTE_TIME #cmakedefine01 HAVE_SCHED_GETCPU +#cmakedefine01 HAVE_GNU_LIBNAMES_H diff --git a/src/Native/System.Private.CoreLib.Native/configure.cmake b/src/Native/System.Private.CoreLib.Native/configure.cmake index f5c6b02a1..aaabb387c 100644 --- a/src/Native/System.Private.CoreLib.Native/configure.cmake +++ b/src/Native/System.Private.CoreLib.Native/configure.cmake @@ -60,6 +60,8 @@ int main(void) }" HAVE_SCHED_GETCPU) set(CMAKE_REQUIRED_LIBRARIES) +check_include_files(gnu/lib-names.h HAVE_GNU_LIBNAMES_H) + configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/config.h.in ${CMAKE_CURRENT_BINARY_DIR}/config.h) diff --git a/src/Native/System.Private.CoreLib.Native/pal_dynamicload.cpp b/src/Native/System.Private.CoreLib.Native/pal_dynamicload.cpp index dfb2917f2..d46e3da95 100644 --- a/src/Native/System.Private.CoreLib.Native/pal_dynamicload.cpp +++ b/src/Native/System.Private.CoreLib.Native/pal_dynamicload.cpp @@ -2,10 +2,38 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#include "pal_common.h" #include <dlfcn.h> +#include <string.h> + +#if HAVE_GNU_LIBNAMES_H +#include <gnu/lib-names.h> +#endif extern "C" void* CoreLibNative_LoadLibrary(const char* filename) { + // Check whether we have been requested to load 'libc'. If that's the case, then: + // * For Linux, use the full name of the library that is defined in <gnu/lib-names.h> by the + // LIBC_SO constant. The problem is that calling dlopen("libc.so") will fail for libc even + // though it works for other libraries. The reason is that libc.so is just linker script + // (i.e. a test file). + // As a result, we have to use the full name (i.e. lib.so.6) that is defined by LIBC_SO. + // * For macOS, use constant value absolute path "/usr/lib/libc.dylib". + // * For FreeBSD, use constant value "libc.so.7". + // * For rest of Unices, use constant value "libc.so". + if (strcmp(filename, "libc") == 0) + { +#if defined(__APPLE__) + filename = "/usr/lib/libc.dylib"; +#elif defined(__FreeBSD__) + filename = "libc.so.7"; +#elif defined(LIBC_SO) + filename = LIBC_SO; +#else + filename = "libc.so"; +#endif + } + return dlopen(filename, RTLD_LAZY); } |