Skip to content

Commit

Permalink
Moving the rest of Unix libraries to the 'overrider' mechanism when l…
Browse files Browse the repository at this point in the history
…inked into static host. (#45995)

* System.Net.Security.Native

* System.Native

* fix for OSX

* fix for wasm

* fix for IOS

* Fix for FreeBSD

* System.Security.Cryptography.Native.OpenSsl

* fix OSX

* Add script to validate entrypoints.c against dll entries

* add missing CryptoNative entries

* Added verification in more projects

* Add missing entries in other files

* System.Security.Cryptography.Native.Apple

* fix for IOS/TVOS

* Change .py verification script to bash

* Update the verify_entrypoints.sh to not to use awk and work on macOS

* PR feedback

* reduce exports of System.IO.Compression.Native.dll on Windows to what is actually used and to match Unix.

* unixexports for System.IO.Compression.Native

* trying to fix IOS/TVOS

* move awk and linker flavor detection to configuretools.cmake

Co-authored-by: Jan Vorlicek <[email protected]>
  • Loading branch information
VSadov and janvorli authored Dec 18, 2020
1 parent 75f8346 commit e98fb61
Show file tree
Hide file tree
Showing 33 changed files with 1,137 additions and 189 deletions.
19 changes: 0 additions & 19 deletions eng/native/configurecompiler.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -641,23 +641,4 @@ if (CLR_CMAKE_HOST_WIN32)
else (CLR_CMAKE_HOST_WIN32)
enable_language(ASM)

# Ensure that awk is present
find_program(AWK awk)
if (AWK STREQUAL "AWK-NOTFOUND")
message(FATAL_ERROR "AWK not found")
endif()

# detect linker
set(ldVersion ${CMAKE_C_COMPILER};-Wl,--version)
execute_process(COMMAND ${ldVersion}
ERROR_QUIET
OUTPUT_VARIABLE ldVersionOutput)

if("${ldVersionOutput}" MATCHES "GNU ld" OR "${ldVersionOutput}" MATCHES "GNU gold" OR "${ldVersionOutput}" MATCHES "GNU linkers")
set(LD_GNU 1)
elseif("${ldVersionOutput}" MATCHES "Solaris Link")
set(LD_SOLARIS 1)
else(CLR_CMAKE_HOST_OSX)
set(LD_OSX 1)
endif()
endif(CLR_CMAKE_HOST_WIN32)
22 changes: 22 additions & 0 deletions eng/native/configuretools.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,25 @@ if(NOT WIN32 AND NOT CLR_CMAKE_TARGET_BROWSER)
locate_toolchain_exec(objcopy CMAKE_OBJCOPY)
endif()
endif()

if (NOT CLR_CMAKE_HOST_WIN32)
# Ensure that awk is present
find_program(AWK awk)
if (AWK STREQUAL "AWK-NOTFOUND")
message(FATAL_ERROR "AWK not found")
endif()

# detect linker
set(ldVersion ${CMAKE_C_COMPILER};-Wl,--version)
execute_process(COMMAND ${ldVersion}
ERROR_QUIET
OUTPUT_VARIABLE ldVersionOutput)

if("${ldVersionOutput}" MATCHES "GNU ld" OR "${ldVersionOutput}" MATCHES "GNU gold" OR "${ldVersionOutput}" MATCHES "GNU linkers")
set(LD_GNU 1)
elseif("${ldVersionOutput}" MATCHES "Solaris Link")
set(LD_SOLARIS 1)
else(CLR_CMAKE_HOST_OSX)
set(LD_OSX 1)
endif()
endif()
4 changes: 2 additions & 2 deletions eng/native/functions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,11 @@ function(generate_exports_file)
list(GET INPUT_LIST -1 outputFilename)
list(REMOVE_AT INPUT_LIST -1)

if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
if(CLR_CMAKE_TARGET_OSX OR CLR_CMAKE_TARGET_IOS OR CLR_CMAKE_TARGET_TVOS)
set(AWK_SCRIPT generateexportedsymbols.awk)
else()
set(AWK_SCRIPT generateversionscript.awk)
endif(CMAKE_SYSTEM_NAME STREQUAL Darwin)
endif()

add_custom_command(
OUTPUT ${outputFilename}
Expand Down
21 changes: 0 additions & 21 deletions src/coreclr/vm/dllimport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6158,27 +6158,6 @@ 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.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

if (g_hostpolicy_embedded)
{
#ifdef TARGET_WINDOWS
Expand Down
100 changes: 46 additions & 54 deletions src/installer/corehost/cli/apphost/static/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -102,21 +102,14 @@ if(CLR_CMAKE_TARGET_WIN32)
add_linker_flag("/INCREMENTAL:NO")
endif()

# Specify non-default Windows libs to be used for Arm/Arm64 builds
if (CLR_CMAKE_TARGET_WIN32 AND (CLR_CMAKE_TARGET_ARCH_ARM OR CLR_CMAKE_TARGET_ARCH_ARM64))
target_link_libraries(singlefilehost Advapi32.lib shell32.lib)
endif()

set(NATIVE_LIBS_EXTRA)

# Path like: artifacts/bin/coreclr/windows.x64.Release/lib or
# /root/runtime/artifacts/transport/coreclr/lib
set(CORECLR_STATIC_LIB_LOCATION "${CORECLR_ARTIFACTS}/lib")

message ("Looking for coreclr_static lib at location: '${CORECLR_STATIC_LIB_LOCATION}'.")

if(CLR_CMAKE_TARGET_WIN32)
set(CORECLR_LIBRARIES
set(NATIVE_LIBS
${CORECLR_STATIC_LIB_LOCATION}/coreclr_static.lib
${CORECLR_STATIC_LIB_LOCATION}/System.Globalization.Native.lib
${CORECLR_STATIC_LIB_LOCATION}/System.IO.Compression.Native.lib
Expand All @@ -132,10 +125,13 @@ if(CLR_CMAKE_TARGET_WIN32)
RuntimeObject.lib
)
else()
set(CORECLR_LIBRARIES
set(NATIVE_LIBS
${CORECLR_STATIC_LIB_LOCATION}/libcoreclr_static.a
${CORECLR_STATIC_LIB_LOCATION}/libSystem.Globalization.Native.a
${CORECLR_STATIC_LIB_LOCATION}/libSystem.IO.Compression.Native.a
${CORECLR_STATIC_LIB_LOCATION}/libSystem.Net.Security.Native.a
${CORECLR_STATIC_LIB_LOCATION}/libSystem.Native.a
${CORECLR_STATIC_LIB_LOCATION}/libSystem.Security.Cryptography.Native.OpenSsl.a
${CORECLR_STATIC_LIB_LOCATION}/libpalrt.a
${CORECLR_STATIC_LIB_LOCATION}/libcoreclrpal.a
${CORECLR_STATIC_LIB_LOCATION}/libeventprovider.a
Expand All @@ -144,7 +140,41 @@ else()

# additional requirements for System.IO.Compression.Native
include(${CLR_REPO_ROOT_DIR}/src/libraries/Native/Unix/System.IO.Compression.Native/extra_libs.cmake)
append_extra_compression_libs(NATIVE_LIBS_EXTRA)
append_extra_compression_libs(NATIVE_LIBS)

# Additional requirements for System.Net.Security.Native
include(${CLR_REPO_ROOT_DIR}/src/libraries/Native/Unix/System.Net.Security.Native/extra_libs.cmake)
append_extra_security_libs(NATIVE_LIBS)

# Additional requirements for System.Native
include(${CLR_REPO_ROOT_DIR}/src/libraries/Native/Unix/System.Native/extra_libs.cmake)
append_extra_system_libs(NATIVE_LIBS)

# Additional requirements for System.Security.Cryptography.Native.OpenSsl
include(${CLR_REPO_ROOT_DIR}/src/libraries/Native/Unix/System.Security.Cryptography.Native/extra_libs.cmake)
append_extra_cryptography_libs(NATIVE_LIBS)
endif()

if(CLR_CMAKE_TARGET_OSX)
LIST(APPEND NATIVE_LIBS
${CORECLR_STATIC_LIB_LOCATION}/libSystem.Security.Cryptography.Native.Apple.a
)

# Additional requirements for System.Security.Cryptography.Native.Apple
include(${CLR_REPO_ROOT_DIR}/src/libraries/Native/Unix/System.Security.Cryptography.Native.Apple/extra_libs.cmake)
append_extra_cryptography_apple_libs(NATIVE_LIBS)
endif()

#
# Additional requirements for coreclr
#

# Specify non-default Windows libs to be used for Arm/Arm64 builds
if (CLR_CMAKE_TARGET_WIN32 AND (CLR_CMAKE_TARGET_ARCH_ARM OR CLR_CMAKE_TARGET_ARCH_ARM64))
LIST(APPEND NATIVE_LIBS
Advapi32.lib
shell32.lib
)
endif()

if(CLR_CMAKE_TARGET_OSX)
Expand All @@ -153,7 +183,7 @@ if(CLR_CMAKE_TARGET_OSX)
find_library(SECURITY Security)
find_library(SYSTEM System)

LIST(APPEND CORECLR_LIBRARIES
LIST(APPEND NATIVE_LIBS
${COREFOUNDATION}
${CORESERVICES}
${SECURITY}
Expand All @@ -162,11 +192,11 @@ if(CLR_CMAKE_TARGET_OSX)
elseif(CLR_CMAKE_TARGET_NETBSD)
find_library(KVM kvm)

LIST(APPEND CORECLR_LIBRARIES
LIST(APPEND NATIVE_LIBS
${KVM}
)
elseif (CLR_CMAKE_TARGET_SUNOS)
LIST(APPEND CORECLR_LIBRARIES
LIST(APPEND NATIVE_LIBS
socket
)
endif(CLR_CMAKE_TARGET_OSX)
Expand All @@ -179,50 +209,12 @@ endif()
if(CLR_CMAKE_USE_SYSTEM_LIBUNWIND)
find_unwind_libs(UNWIND_LIBS)

LIST(APPEND CORECLR_LIBRARIES
LIST(APPEND NATIVE_LIBS
${UNWIND_LIBS}
)
endif()

# Path like: artifacts/bin/native/net5.0-Linux-Release-arm/
set(NATIVE_LIBS_LOCATION "${NATIVE_LIBS_ARTIFACTS}")
message ("Looking for native libs at location: '${NATIVE_LIBS_LOCATION}'.")

# These native libs are linked into singlefilehost only on Linux (for now).
if(CLR_CMAKE_TARGET_LINUX)
set(NATIVE_LIBS
${NATIVE_LIBS_LOCATION}/libSystem.Native.a
${NATIVE_LIBS_LOCATION}/libSystem.Net.Security.Native.a
${NATIVE_LIBS_LOCATION}/libSystem.Security.Cryptography.Native.OpenSsl.a
)

# Additional requirements for System.Native
include(${CLR_REPO_ROOT_DIR}/src/libraries/Native/Unix/System.Native/extra_libs.cmake)
append_extra_system_libs(NATIVE_LIBS_EXTRA)

# Additional requirements for System.Net.Security.Native
include(${CLR_REPO_ROOT_DIR}/src/libraries/Native/Unix/System.Net.Security.Native/extra_libs.cmake)
append_extra_security_libs(NATIVE_LIBS_EXTRA)

# Additional requirements for System.Security.Cryptography.Native.OpenSsl
include(${CLR_REPO_ROOT_DIR}/src/libraries/Native/Unix/System.Security.Cryptography.Native/extra_libs.cmake)
append_extra_cryptography_libs(NATIVE_LIBS_EXTRA)
endif()

if(CLR_CMAKE_TARGET_LINUX OR CLR_CMAKE_TARGET_FREEBSD OR CLR_CMAKE_TARGET_NETBSD OR CLR_CMAKE_TARGET_SUNOS)
# These options are used to force every object to be included even if it's unused.
set(START_WHOLE_ARCHIVE -Wl,--whole-archive)
set(END_WHOLE_ARCHIVE -Wl,--no-whole-archive)
endif(CLR_CMAKE_TARGET_LINUX OR CLR_CMAKE_TARGET_FREEBSD OR CLR_CMAKE_TARGET_NETBSD OR CLR_CMAKE_TARGET_SUNOS)

set_property(TARGET singlefilehost PROPERTY ENABLE_EXPORTS 1)

target_link_libraries(singlefilehost
${CORECLR_LIBRARIES}

${START_WHOLE_ARCHIVE}
target_link_libraries(
singlefilehost
${NATIVE_LIBS}
${END_WHOLE_ARCHIVE}

${NATIVE_LIBS_EXTRA}
)
41 changes: 34 additions & 7 deletions src/installer/corehost/cli/hostpolicy/hostpolicy_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,49 @@ namespace

#if defined(NATIVE_LIBS_EMBEDDED)
extern "C" const void* CompressionResolveDllImport(const char* name);

#if defined(_WIN32)
#define COMPRESSION_DLL_NAME "System.IO.Compression.Native"
#else
#define COMPRESSION_DLL_NAME "libSystem.IO.Compression.Native"
#endif
extern "C" const void* SecurityResolveDllImport(const char* name);
extern "C" const void* SystemResolveDllImport(const char* name);
extern "C" const void* CryptoResolveDllImport(const char* name);
extern "C" const void* CryptoAppleResolveDllImport(const char* name);

// pinvoke_override:
// Check if given function belongs to one of statically linked libraries and return a pointer if found.
const void* STDMETHODCALLTYPE pinvoke_override(const char* libraryName, const char* entrypointName)
{
if (strcmp(libraryName, COMPRESSION_DLL_NAME) == 0)
#if defined(_WIN32)
if (strcmp(libraryName, "System.IO.Compression.Native") == 0)
{
return CompressionResolveDllImport(entrypointName);
}
#else
if (strcmp(libraryName, "libSystem.IO.Compression.Native") == 0)
{
return CompressionResolveDllImport(entrypointName);
}

if (strcmp(libraryName, "libSystem.Net.Security.Native") == 0)
{
return SecurityResolveDllImport(entrypointName);
}

if (strcmp(libraryName, "libSystem.Native") == 0)
{
return SystemResolveDllImport(entrypointName);
}

if (strcmp(libraryName, "libSystem.Security.Cryptography.Native.OpenSsl") == 0)
{
return CryptoResolveDllImport(entrypointName);
}
#endif

#if defined(TARGET_OSX)
if (strcmp(libraryName, "libSystem.Security.Cryptography.Native.Apple") == 0)
{
return CryptoAppleResolveDllImport(entrypointName);
}
#endif

return nullptr;
}
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@
<!-- LINUX -->

<!-- OSX -->
<SingleFileHostIncludeFilename Include="libSystem.Native.dylib" />
<SingleFileHostIncludeFilename Include="libSystem.Net.Security.Native.dylib" />
<SingleFileHostIncludeFilename Include="libSystem.Security.Cryptography.Native.Apple.dylib" />
<SingleFileHostIncludeFilename Include="libSystem.Security.Cryptography.Native.OpenSsl.dylib" />

<!-- Windows -->
<SingleFileHostIncludeFilename Include="mscordaccore.dll" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ set(NATIVECOMPRESSION_SOURCES
)

if (NOT CLR_CMAKE_TARGET_BROWSER)
add_definitions(-DBROTLI_SHARED_COMPILATION)

#Include Brotli include files
include_directories("../../AnyOS/brotli/include")

Expand Down Expand Up @@ -58,6 +56,8 @@ if (NOT CLR_CMAKE_TARGET_BROWSER)
endif()

if (GEN_SHARED_LIB)
add_definitions(-DBROTLI_SHARED_COMPILATION)

add_library(System.IO.Compression.Native
SHARED
${NATIVECOMPRESSION_SOURCES}
Expand All @@ -67,6 +67,29 @@ if (GEN_SHARED_LIB)
target_link_libraries(System.IO.Compression.Native
${NATIVE_LIBS_EXTRA}
)

set(DEF_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/System.IO.Compression.Native_unixexports.src)
set(EXPORTS_FILE ${CMAKE_CURRENT_BINARY_DIR}/System.IO.Compression.Native.exports)
generate_exports_file(${DEF_SOURCES} ${EXPORTS_FILE})
set_exports_linker_option(${EXPORTS_FILE})

add_custom_target(System.IO.Compression.Native_exports DEPENDS ${EXPORTS_FILE})
add_dependencies(System.IO.Compression.Native System.IO.Compression.Native_exports)

set_property(TARGET System.IO.Compression.Native APPEND_STRING PROPERTY LINK_FLAGS ${EXPORTS_LINKER_OPTION})
set_property(TARGET System.IO.Compression.Native APPEND_STRING PROPERTY LINK_DEPENDS ${EXPORTS_FILE})

if (NOT CLR_CMAKE_TARGET_IOS AND NOT CLR_CMAKE_TARGET_TVOS AND NOT CLR_CMAKE_TARGET_ANDROID)
add_custom_command(TARGET System.IO.Compression.Native POST_BUILD
COMMENT "Verifying System.IO.Compression.Native entry points against entrypoints.c "
COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/../verify-entrypoints.sh
$<TARGET_FILE:System.IO.Compression.Native>
${CMAKE_CURRENT_SOURCE_DIR}/../../AnyOS/System.IO.Compression.Native/entrypoints.c
${CMAKE_NM}
VERBATIM
)
endif()

install_with_stripped_symbols (System.IO.Compression.Native PROGRAMS .)
endif ()

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
; Licensed to the .NET Foundation under one or more agreements.
; The .NET Foundation licenses this file to you under the MIT license.

BrotliDecoderCreateInstance
BrotliDecoderDecompress
BrotliDecoderDecompressStream
BrotliDecoderDestroyInstance
BrotliDecoderIsFinished
BrotliEncoderCompress
BrotliEncoderCompressStream
BrotliEncoderCreateInstance
BrotliEncoderDestroyInstance
BrotliEncoderHasMoreOutput
BrotliEncoderSetParameter
CompressionNative_Crc32
CompressionNative_Deflate
CompressionNative_DeflateEnd
CompressionNative_DeflateInit2_
CompressionNative_Inflate
CompressionNative_InflateEnd
CompressionNative_InflateInit2_
Loading

0 comments on commit e98fb61

Please sign in to comment.