From f8d345f242f5812c4ef36e81707219e061801938 Mon Sep 17 00:00:00 2001 From: Vladimir Sadov Date: Fri, 17 Jul 2020 09:01:18 -0700 Subject: [PATCH] Linking native libraries into superhost (#38684) (#39490) * native libs location * pass artifacts locations as CMake variables. * Link native libs * use LibrariesConfiguration to discover libraries artifacts * redirect to current exe * remove bundled binaries from the netcoreapp/pkg * delete exports file * no bundling of native libs on OSX * only link native libs on Linux * Support linking of coreclr on Linux only * PR feedback * move the shim to dllimport.cpp * Use library names vs. file names * removed ShouldRedirectToCurrentLibrary. NULL library name in PAL_LoadLibrary will be always considered self-referring. --- src/coreclr/src/pal/src/loader/module.cpp | 22 +-- src/coreclr/src/vm/dllimport.cpp | 25 +++ .../corehost/Windows/gen-buildsys-win.bat | 3 +- src/installer/corehost/build.cmd | 1 + src/installer/corehost/build.proj | 2 + src/installer/corehost/build.sh | 8 +- .../cli/apphost/static/CMakeLists.txt | 159 +++++++++++++----- .../cli/apphost/static/configure.cmake | 12 ++ .../static/singlefilehost_unixexports.src | 8 - .../hostpolicy/static/coreclr_resolver.cpp | 2 +- .../netcoreapp/pkg/Directory.Build.props | 10 +- 11 files changed, 177 insertions(+), 75 deletions(-) create mode 100644 src/installer/corehost/cli/apphost/static/configure.cmake delete mode 100644 src/installer/corehost/cli/apphost/static/singlefilehost_unixexports.src diff --git a/src/coreclr/src/pal/src/loader/module.cpp b/src/coreclr/src/pal/src/loader/module.cpp index 68675ac848a476..58126e1cc5a081 100644 --- a/src/coreclr/src/pal/src/loader/module.cpp +++ b/src/coreclr/src/pal/src/loader/module.cpp @@ -603,6 +603,13 @@ PAL_LoadLibraryDirect( lpLibFileName ? lpLibFileName : W16_NULLSTRING, lpLibFileName ? lpLibFileName : W16_NULLSTRING); + // Getting nullptr as name indicates redirection to current library + if (lpLibFileName == nullptr) + { + dl_handle = dlopen(NULL, RTLD_LAZY); + goto done; + } + if (!LOADVerifyLibraryPath(lpLibFileName)) { goto done; @@ -1436,18 +1443,6 @@ static LPWSTR LOADGetModuleFileName(MODSTRUCT *module) return module->lib_name; } -static bool ShouldRedirectToCurrentLibrary(LPCSTR libraryNameOrPath) -{ - if (!g_running_in_exe) - return false; - - // Getting nullptr as name indicates redirection to current library - if (libraryNameOrPath == nullptr) - return true; - - return false; -} - /* Function: LOADLoadLibraryDirect [internal] @@ -1464,7 +1459,8 @@ static NATIVE_LIBRARY_HANDLE LOADLoadLibraryDirect(LPCSTR libraryNameOrPath) { NATIVE_LIBRARY_HANDLE dl_handle; - if (ShouldRedirectToCurrentLibrary(libraryNameOrPath)) + // Getting nullptr as name indicates redirection to current library + if (libraryNameOrPath == nullptr) { dl_handle = dlopen(NULL, RTLD_LAZY); } diff --git a/src/coreclr/src/vm/dllimport.cpp b/src/coreclr/src/vm/dllimport.cpp index 05813cd8af94ce..2fd2fefd1bc8aa 100644 --- a/src/coreclr/src/vm/dllimport.cpp +++ b/src/coreclr/src/vm/dllimport.cpp @@ -51,6 +51,9 @@ #include "clr/fs/path.h" using namespace clr::fs; +// Specifies whether coreclr is embedded or standalone +extern bool g_coreclr_embedded; + // remove when we get an updated SDK #define LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR 0x00000100 #define LOAD_LIBRARY_SEARCH_DEFAULT_DIRS 0x00001000 @@ -6303,6 +6306,28 @@ namespace } #endif // FEATURE_CORESYSTEM && !TARGET_UNIX +#if defined(TARGET_LINUX) + if (g_coreclr_embedded) + { + // this matches exactly the names in Interop.Libraries.cs + static const LPCWSTR toRedirect[] = { + W("libSystem.Native"), + W("libSystem.IO.Compression.Native"), + W("libSystem.Net.Security.Native"), + W("libSystem.Security.Cryptography.Native.OpenSsl") + }; + + int count = lengthof(toRedirect); + for (int i = 0; i < count; ++i) + { + if (wcscmp(wszLibName, toRedirect[i]) == 0) + { + return PAL_LoadLibraryDirect(NULL); + } + } + } +#endif + AppDomain* pDomain = GetAppDomain(); DWORD loadWithAlteredPathFlags = GetLoadWithAlteredSearchPathFlag(); bool libNameIsRelativePath = Path::IsRelative(wszLibName); diff --git a/src/installer/corehost/Windows/gen-buildsys-win.bat b/src/installer/corehost/Windows/gen-buildsys-win.bat index e5935d75901331..454853a24f0864 100644 --- a/src/installer/corehost/Windows/gen-buildsys-win.bat +++ b/src/installer/corehost/Windows/gen-buildsys-win.bat @@ -43,7 +43,8 @@ popd :DoGen set __ExtraCmakeParams=%__ExtraCmakeParams% "-DCMAKE_SYSTEM_VERSION=10.0" "-DCLI_CMAKE_HOST_VER=%__HostVersion%" "-DCLI_CMAKE_COMMON_HOST_VER=%__AppHostVersion%" "-DCLI_CMAKE_HOST_FXR_VER=%__HostFxrVersion%" set __ExtraCmakeParams=%__ExtraCmakeParams% "-DCLI_CMAKE_HOST_POLICY_VER=%__HostPolicyVersion%" "-DCLI_CMAKE_PKG_RID=%cm_BaseRid%" "-DCLI_CMAKE_COMMIT_HASH=%__LatestCommit%" "-DCLR_CMAKE_HOST_ARCH=%__Arch%" -set __ExtraCmakeParams=%__ExtraCmakeParams% "-DCMAKE_INSTALL_PREFIX=%__CMakeBinDir%" "-DCLI_CMAKE_RESOURCE_DIR=%__ResourcesDir%" "-DCLR_ENG_NATIVE_DIR="%__sourceDir%\..\..\..\eng\native" +set __ExtraCmakeParams=%__ExtraCmakeParams% "-DCORECLR_ARTIFACTS=%__CoreClrArtifacts% " "-DNATIVE_LIBS_ARTIFACTS=%__NativeLibsArtifacts%" +set __ExtraCmakeParams=%__ExtraCmakeParams% "-DCMAKE_INSTALL_PREFIX=%__CMakeBinDir%" "-DCLI_CMAKE_RESOURCE_DIR=%__ResourcesDir%" "-DCLR_ENG_NATIVE_DIR=%__sourceDir%\..\..\..\eng\native" echo "%CMakePath%" %__sourceDir% -G "Visual Studio %__VSString%" %__ExtraCmakeParams% "%CMakePath%" %__sourceDir% -G "Visual Studio %__VSString%" %__ExtraCmakeParams% diff --git a/src/installer/corehost/build.cmd b/src/installer/corehost/build.cmd index f494516569130e..3a6e82dc8db27e 100644 --- a/src/installer/corehost/build.cmd +++ b/src/installer/corehost/build.cmd @@ -39,6 +39,7 @@ if /i [%1] == [commit] (set __CommitSha=%2&&shift&&shift&goto Arg_Loop) if /i [%1] == [incremental-native-build] ( set __IncrementalNativeBuild=1&&shift&goto Arg_Loop) if /i [%1] == [rootDir] ( set __rootDir=%2&&shift&&shift&goto Arg_Loop) if /i [%1] == [coreclrartifacts] (set __CoreClrArtifacts=%2&&shift&&shift&goto Arg_Loop) +if /i [%1] == [nativelibsartifacts] (set __NativeLibsArtifacts=%2&&shift&&shift&goto Arg_Loop) shift diff --git a/src/installer/corehost/build.proj b/src/installer/corehost/build.proj index 6a3f561209e143..69927e8304b1d8 100644 --- a/src/installer/corehost/build.proj +++ b/src/installer/corehost/build.proj @@ -34,6 +34,7 @@ $(BuildArgs) $(Compiler) $(BuildArgs) $(CMakeArgs) $(BuildArgs) -coreclrartifacts $(CoreCLRArtifactsPath) + $(BuildArgs) -nativelibsartifacts $(LibrariesArtifactsPath)/bin/native/$(NetCoreAppCurrent)-$(TargetOS)-$(LibrariesConfiguration)-$(TargetArchitecture) - - - - + @@ -25,7 +21,9 @@ - + + +