From f54977e2fc84527fe58024ed5537cb63244168e2 Mon Sep 17 00:00:00 2001 Patch-Source: https://github.com/dotnet/runtime/pull/76500 From: Antoine Martin Date: Tue, 13 Sep 2022 14:17:35 +0200 Subject: [PATCH 1/1] Mono musl support --- src/coreclr/pal/src/misc/perfjitdump.cpp | 2 +- src/mono/CMakeLists.txt | 29 ++++++++++++++++++++++++ src/mono/mono.proj | 18 +++++++++------ src/mono/mono/metadata/domain.c | 10 ++++++++ src/mono/mono/mini/CMakeLists.txt | 10 ++++++++ src/mono/mono/utils/mono-context.h | 8 +++++++ 6 files changed, 69 insertions(+), 8 deletions(-) diff --git a/src/runtime/src/coreclr/pal/src/misc/perfjitdump.cpp b/src/runtime/src/coreclr/pal/src/misc/perfjitdump.cpp index d80bd58038c..3488397b0da 100644 --- a/src/runtime/src/coreclr/pal/src/misc/perfjitdump.cpp +++ b/src/runtime/src/coreclr/pal/src/misc/perfjitdump.cpp @@ -25,7 +25,7 @@ #include #include #include -#include +#include #include "../inc/llvm/ELF.h" diff --git a/src/runtime/src/mono/CMakeLists.txt b/src/runtime/src/mono/CMakeLists.txt index 49a73b1b709..7e596d69d35 100644 --- a/src/runtime/src/mono/CMakeLists.txt +++ b/src/runtime/src/mono/CMakeLists.txt @@ -208,6 +208,35 @@ elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux") # Enable the "full RELRO" options (RELRO & BIND_NOW) at link time add_link_options(-Wl,-z,relro) add_link_options(-Wl,-z,now) + # Detect Linux ID + # TODO: Eventually merge with eng/native/configureplatform.cmake + set(LINUX_ID_FILE "/etc/os-release") + if(CMAKE_CROSSCOMPILING) + set(LINUX_ID_FILE "${CMAKE_SYSROOT}${LINUX_ID_FILE}") + endif() + + if(EXISTS ${LINUX_ID_FILE}) + execute_process( + COMMAND bash -c "source ${LINUX_ID_FILE} && echo \$ID" + OUTPUT_VARIABLE CLR_CMAKE_LINUX_ID + OUTPUT_STRIP_TRAILING_WHITESPACE) + + execute_process( + COMMAND bash -c "if strings \"${CMAKE_SYSROOT}/usr/bin/ldd\" 2>&1 | grep -q musl; then echo musl; fi" + OUTPUT_VARIABLE CLR_CMAKE_LINUX_MUSL + OUTPUT_STRIP_TRAILING_WHITESPACE) + endif() + + if(DEFINED CLR_CMAKE_LINUX_ID) + if(CLR_CMAKE_LINUX_ID STREQUAL alpine) + set(CLR_CMAKE_HOST_ALPINE_LINUX 1) + set(CLR_CMAKE_HOST_OS ${CLR_CMAKE_LINUX_ID}) + endif() + + if(CLR_CMAKE_LINUX_MUSL STREQUAL musl) + set(CLR_CMAKE_HOST_LINUX_MUSL 1) + endif() + endif(DEFINED CLR_CMAKE_LINUX_ID) elseif(CMAKE_SYSTEM_NAME STREQUAL "Android") set(HOST_LINUX 1) add_definitions(-D_GNU_SOURCE -D_REENTRANT) diff --git a/src/runtime/src/mono/mono.proj b/src/runtime/src/mono/mono.proj index d6a0c9a8ec2..933ed60cea7 100644 --- a/src/runtime/src/mono/mono.proj +++ b/src/runtime/src/mono/mono.proj @@ -417,11 +417,15 @@ <_MonoCFLAGS Include="-Wl,--build-id=sha1" /> + <_MonoCFLAGS Include="-Wno-strict-prototypes" /> <_MonoCXXFLAGS Include="-Wl,--build-id=sha1" /> + <_MonoCXXFLAGS Include="-Wno-strict-prototypes" /> <_MonoAOTCFLAGS Include="-Wl,--build-id=sha1" /> + <_MonoAOTCFLAGS Include="-Wno-strict-prototypes" /> <_MonoAOTCXXFLAGS Include="-Wl,--build-id=sha1" /> + <_MonoAOTCXXFLAGS Include="-Wno-strict-prototypes" /> @@ -500,15 +504,15 @@ darwin-x86_64 windows-x86_64 <_MonoRuntimeFilePath>$(MonoObjDir)out\lib\$(MonoFileName) - <_LinuxAbi Condition="'$(TargetsAndroid)' != 'true'">gnu - <_LinuxAbi Condition="'$(TargetsAndroid)' == 'true'">android + <_LinuxAbi Condition="'$(TargetsAndroid)' != 'true'">alpine-linux-musl + <_LinuxAbi Condition="'$(TargetsAndroid)' == 'true'">linux-android <_LinuxFloatAbi Condition="'$(TargetsAndroid)' != 'true'">hf <_Objcopy>objcopy - <_Objcopy Condition="'$(Platform)' == 'arm'">arm-linux-$(_LinuxAbi)eabi$(_LinuxFloatAbi)-$(_Objcopy) - <_Objcopy Condition="'$(Platform)' == 'arm64'">aarch64-linux-$(_LinuxAbi)-$(_Objcopy) - <_Objcopy Condition="'$(Platform)' == 's390x'">s390x-linux-$(_LinuxAbi)-$(_Objcopy) - <_Objcopy Condition="'$(Platform)' == 'x64'">x86_64-linux-$(_LinuxAbi)-$(_Objcopy) - <_Objcopy Condition="'$(Platform)' == 'x86'">i686-linux-$(_LinuxAbi)-$(_Objcopy) + <_Objcopy Condition="'$(Platform)' == 'arm' and '$(CrossBuild)' == 'true'">arm-$(_LinuxAbi)eabi$(_LinuxFloatAbi)-$(_Objcopy) + <_Objcopy Condition="'$(Platform)' == 'arm64' and '$(CrossBuild)' == 'true'">aarch64-$(_LinuxAbi)-$(_Objcopy) + <_Objcopy Condition="'$(Platform)' == 's390x' and '$(CrossBuild)' == 'true'">s390x-$(_LinuxAbi)-$(_Objcopy) + <_Objcopy Condition="'$(Platform)' == 'x64' and '$(CrossBuild)' == 'true'">x86_64-$(_LinuxAbi)-$(_Objcopy) + <_Objcopy Condition="'$(Platform)' == 'x86' and '$(CrossBuild)' == 'true'">i686-$(_LinuxAbi)-$(_Objcopy) <_Objcopy Condition="'$(TargetsAndroid)' == 'true'">$(ANDROID_NDK_ROOT)/toolchains/llvm/prebuilt/$(MonoToolchainPrebuiltOS)/bin/$(_Objcopy) diff --git a/src/runtime/src/mono/mono/metadata/domain.c b/src/runtime/src/mono/mono/metadata/domain.c index 4a8e06d28ae..e9868bd1b2c 100644 --- a/src/runtime/src/mono/mono/metadata/domain.c +++ b/src/runtime/src/mono/mono/metadata/domain.c @@ -138,6 +138,14 @@ create_root_domain (void) return domain; } +static MONO_NO_OPTIMIZATION MONO_NEVER_INLINE void +ensure_stack_size (void) +{ + const int default_size = 5 * 1024 * 1024; + volatile uint8_t *s = (uint8_t *)g_alloca(default_size); + *s = 0; +} + /** * mono_init_internal: * @@ -181,6 +189,8 @@ mono_init_internal (const char *filename, const char *exe_filename, const char * mono_counters_register ("Max HashTable Chain Length", MONO_COUNTER_INT|MONO_COUNTER_METADATA, &mono_g_hash_table_max_chain_length); + ensure_stack_size (); + mono_gc_base_init (); mono_thread_info_attach (); diff --git a/src/runtime/src/mono/mono/mini/CMakeLists.txt b/src/runtime/src/mono/mono/mini/CMakeLists.txt index 128ed08f45b..17cbe696c4b 100644 --- a/src/runtime/src/mono/mono/mini/CMakeLists.txt +++ b/src/runtime/src/mono/mono/mini/CMakeLists.txt @@ -364,6 +364,11 @@ if(NOT DISABLE_SHARED_LIBS) set_target_properties(monosgen-shared PROPERTIES IMPORT_SUFFIX ".import.lib") endif() target_link_libraries(monosgen-shared PRIVATE ${OS_LIBS} ${ICONV_LIB} ${LLVM_LIBS} ${ICU_LIBS} ${Z_LIBS}) + # Alpine Linux implements ucontext in a different library + if(CLR_CMAKE_HOST_ALPINE_LINUX AND TARGET_S390X) + target_link_libraries(monosgen-shared PRIVATE ucontext) + endif(CLR_CMAKE_HOST_ALPINE_LINUX AND TARGET_S390X) + if(ICU_LDFLAGS) set_property(TARGET monosgen-shared APPEND_STRING PROPERTY LINK_FLAGS " ${ICU_LDFLAGS}") endif() @@ -488,6 +493,11 @@ if(NOT DISABLE_EXECUTABLES) set_target_properties(mono-sgen PROPERTIES OUTPUT_NAME mono-aot-cross) endif() target_link_libraries(mono-sgen PRIVATE monosgen-static ${OS_LIBS} ${ICONV_LIB} ${LLVM_LIBS} ${ICU_LIBS} ${Z_LIBS}) + # Alpine Linux implements ucontext in a different library + if(CLR_CMAKE_HOST_ALPINE_LINUX AND TARGET_S390X) + target_link_libraries(mono-sgen PRIVATE ucontext) + endif(CLR_CMAKE_HOST_ALPINE_LINUX AND TARGET_S390X) + if(NOT DISABLE_COMPONENTS AND STATIC_COMPONENTS AND NOT DISABLE_LINK_STATIC_COMPONENTS) # if components are built statically, link them into runtime. target_sources(mono-sgen PRIVATE "${mono-components-objects}") diff --git a/src/runtime/src/mono/mono/utils/mono-context.h b/src/runtime/src/mono/mono/utils/mono-context.h index bd1a3cd0104..db4ba452bcb 100644 --- a/src/runtime/src/mono/mono/utils/mono-context.h +++ b/src/runtime/src/mono/mono/utils/mono-context.h @@ -11,6 +11,14 @@ #ifndef __MONO_MONO_CONTEXT_H__ #define __MONO_MONO_CONTEXT_H__ +/* + * Handle non-gnu libc versions with nothing in features.h + * We have no idea what they're compatible with, so always fail. + */ +#ifndef __GLIBC_PREREQ +# define __GLIBC_PREREQ(x,y) 0 +#endif + #include "mono-compiler.h" #include "mono-sigcontext.h" #include "mono-machine.h" -- 2.36.3