Skip to content

Commit

Permalink
Fix ID regression in Alpine Linux builds (#86804)
Browse files Browse the repository at this point in the history
Co-authored-by: Jan Vorlicek <[email protected]>
  • Loading branch information
am11 and janvorli authored Jun 21, 2023
1 parent dda20d5 commit 9fc5fcb
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 68 deletions.
23 changes: 5 additions & 18 deletions eng/native/configureplatform.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -75,24 +75,15 @@ if(CLR_CMAKE_HOST_OS STREQUAL linux)
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 tizen)
set(CLR_CMAKE_TARGET_TIZEN_LINUX 1)
set(CLR_CMAKE_HOST_OS ${CLR_CMAKE_LINUX_ID})
elseif(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)
set(CLR_CMAKE_HOST_OS ${CLR_CMAKE_LINUX_ID})
endif()
endif(DEFINED CLR_CMAKE_LINUX_ID)
endif(CLR_CMAKE_HOST_OS STREQUAL linux)
Expand Down Expand Up @@ -359,21 +350,17 @@ if(CLR_CMAKE_TARGET_OS STREQUAL linux)
set(CLR_CMAKE_TARGET_LINUX 1)
endif(CLR_CMAKE_TARGET_OS STREQUAL linux)

if(CLR_CMAKE_HOST_LINUX_MUSL)
set(CLR_CMAKE_TARGET_LINUX_MUSL 1)
endif(CLR_CMAKE_HOST_LINUX_MUSL)

if(CLR_CMAKE_TARGET_OS STREQUAL tizen)
set(CLR_CMAKE_TARGET_UNIX 1)
set(CLR_CMAKE_TARGET_LINUX 1)
set(CLR_CMAKE_TARGET_TIZEN_LINUX 1)
endif(CLR_CMAKE_TARGET_OS STREQUAL tizen)

if(CLR_CMAKE_TARGET_OS STREQUAL alpine)
if(CLR_CMAKE_HOST_LINUX_MUSL OR CLR_CMAKE_TARGET_OS STREQUAL alpine)
set(CLR_CMAKE_TARGET_UNIX 1)
set(CLR_CMAKE_TARGET_LINUX 1)
set(CLR_CMAKE_TARGET_ALPINE_LINUX 1)
endif(CLR_CMAKE_TARGET_OS STREQUAL alpine)
set(CLR_CMAKE_TARGET_LINUX_MUSL 1)
endif(CLR_CMAKE_HOST_LINUX_MUSL OR CLR_CMAKE_TARGET_OS STREQUAL alpine)

if(CLR_CMAKE_TARGET_OS STREQUAL android)
set(CLR_CMAKE_TARGET_UNIX 1)
Expand Down Expand Up @@ -483,7 +470,7 @@ if (NOT (CLR_CMAKE_TARGET_OS STREQUAL CLR_CMAKE_HOST_OS) AND NOT CLR_CMAKE_TARGE
if(NOT (CLR_CMAKE_HOST_OS STREQUAL windows))
message(FATAL_ERROR "Invalid host and target os/arch combination. Host OS: ${CLR_CMAKE_HOST_OS}")
endif()
if(NOT (CLR_CMAKE_TARGET_LINUX OR CLR_CMAKE_TARGET_ALPINE_LINUX))
if(NOT (CLR_CMAKE_TARGET_LINUX OR CLR_CMAKE_TARGET_LINUX_MUSL))
message(FATAL_ERROR "Invalid host and target os/arch combination. Target OS: ${CLR_CMAKE_TARGET_OS}")
endif()
if(NOT ((CLR_CMAKE_HOST_ARCH_AMD64 AND (CLR_CMAKE_TARGET_ARCH_AMD64 OR CLR_CMAKE_TARGET_ARCH_ARM64)) OR (CLR_CMAKE_HOST_ARCH_I386 AND CLR_CMAKE_TARGET_ARCH_ARM)))
Expand Down
16 changes: 16 additions & 0 deletions eng/native/configuretools.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ if(NOT WIN32 AND NOT CLR_CMAKE_TARGET_BROWSER AND NOT CLR_CMAKE_TARGET_WASI)
locate_toolchain_exec(ar CMAKE_AR YES)
locate_toolchain_exec(nm CMAKE_NM YES)
locate_toolchain_exec(ranlib CMAKE_RANLIB YES)
locate_toolchain_exec(strings CMAKE_STRINGS YES)

if(CMAKE_C_COMPILER_ID MATCHES "Clang")
locate_toolchain_exec(link CMAKE_LINKER YES)
Expand Down Expand Up @@ -91,3 +92,18 @@ if (NOT CLR_CMAKE_HOST_WIN32)
set(LD_OSX 1)
endif()
endif()

# This introspection depends on CMAKE_STRINGS, which is why it's in this file instead of configureplatform
if (CLR_CMAKE_HOST_LINUX)
execute_process(
COMMAND bash -c "if ${CMAKE_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)

if(CLR_CMAKE_LINUX_MUSL STREQUAL musl)
set(CLR_CMAKE_HOST_LINUX_MUSL 1)
set(CLR_CMAKE_TARGET_UNIX 1)
set(CLR_CMAKE_TARGET_LINUX 1)
set(CLR_CMAKE_TARGET_LINUX_MUSL 1)
endif()
endif()
7 changes: 6 additions & 1 deletion eng/native/init-distro-rid.sh
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,14 @@ initDistroRidGlobal()
if [ -z "${__PortableTargetOS:-}" ]; then
__PortableTargetOS="$targetOs"

STRINGS="$(command -v strings || true)"
if [ -z "$STRINGS" ]; then
STRINGS="$(command -v llvm-strings || true)"
fi

# Check for musl-based distros (e.g Alpine Linux, Void Linux).
if "${rootfsDir}/usr/bin/ldd" --version 2>&1 | grep -q musl ||
strings "${rootfsDir}/usr/bin/ldd" 2>&1 | grep -q musl; then
( [ -n "$STRINGS" ] && "$STRINGS" "${rootfsDir}/usr/bin/ldd" 2>&1 | grep -q musl ); then
__PortableTargetOS="linux-musl"
fi
fi
Expand Down
6 changes: 3 additions & 3 deletions src/coreclr/clrdefinitions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ if (CLR_CMAKE_TARGET_APPLE AND CLR_CMAKE_TARGET_ARCH_ARM64)
add_compile_definitions($<$<NOT:$<BOOL:$<TARGET_PROPERTY:IGNORE_DEFAULT_TARGET_ARCH>>>:OSX_ARM64_ABI>)
endif(CLR_CMAKE_TARGET_APPLE AND CLR_CMAKE_TARGET_ARCH_ARM64)

if(CLR_CMAKE_TARGET_ALPINE_LINUX)
# Alpine Linux doesn't have fixed stack limit, this define disables some stack pointer
if(CLR_CMAKE_TARGET_LINUX_MUSL)
# musl-libc doesn't have fixed stack limit, this define disables some stack pointer
# sanity checks in debug / checked build that rely on a fixed stack limit
add_definitions(-DNO_FIXED_STACK_LIMIT)
endif(CLR_CMAKE_TARGET_ALPINE_LINUX)
endif(CLR_CMAKE_TARGET_LINUX_MUSL)

add_definitions(-D_BLD_CLR)
add_definitions(-DDEBUGGING_SUPPORTED)
Expand Down
6 changes: 3 additions & 3 deletions src/coreclr/debug/dbgutil/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ endif(CLR_CMAKE_HOST_WIN32 OR CLR_CMAKE_HOST_OSX)

add_definitions(-DPAL_STDCPP_COMPAT)

if(CLR_CMAKE_TARGET_ALPINE_LINUX)
add_definitions(-DTARGET_ALPINE_LINUX)
endif(CLR_CMAKE_TARGET_ALPINE_LINUX)
if(CLR_CMAKE_TARGET_LINUX_MUSL)
add_definitions(-DTARGET_LINUX_MUSL)
endif(CLR_CMAKE_TARGET_LINUX_MUSL)

set(DBGUTIL_SOURCES
dbgutil.cpp
Expand Down
10 changes: 5 additions & 5 deletions src/coreclr/debug/dbgutil/elfreader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,11 @@ ElfReader::PopulateForSymbolLookup(uint64_t baseAddress)
// Enumerate program headers searching for the PT_DYNAMIC header, etc.
if (!EnumerateProgramHeaders(
baseAddress,
#ifdef TARGET_ALPINE_LINUX
// On Alpine, the below dynamic entries for hash, string table, etc. are
// RVAs instead of absolute address like on all other Linux distros. Get
// the "loadbias" (basically the base address of the module) and add to
// these RVAs.
#ifdef TARGET_LINUX_MUSL
// On musl based platforms (Alpine), the below dynamic entries for hash,
// string table, etc. are RVAs instead of absolute address like on all
// other Linux distros. Get the "loadbias" (basically the base address
// of the module) and add to these RVAs.
&loadbias,
#else
nullptr,
Expand Down
12 changes: 6 additions & 6 deletions src/coreclr/pal/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,18 @@ add_definitions(-DLP64COMPATIBLE)
add_definitions(-DCORECLR)
add_definitions(-DPIC)

if(CLR_CMAKE_HOST_ARCH_AMD64 AND CLR_CMAKE_TARGET_LINUX AND NOT CLR_CMAKE_HOST_ALPINE_LINUX)
if(CLR_CMAKE_HOST_ARCH_AMD64 AND CLR_CMAKE_TARGET_LINUX AND NOT CLR_CMAKE_TARGET_LINUX_MUSL)
# Currently the _xstate is not available on Alpine Linux
add_definitions(-DXSTATE_SUPPORTED)
endif(CLR_CMAKE_HOST_ARCH_AMD64 AND CLR_CMAKE_TARGET_LINUX AND NOT CLR_CMAKE_HOST_ALPINE_LINUX)
endif(CLR_CMAKE_HOST_ARCH_AMD64 AND CLR_CMAKE_TARGET_LINUX AND NOT CLR_CMAKE_TARGET_LINUX_MUSL)

if(CLR_CMAKE_HOST_ALPINE_LINUX)
if(CLR_CMAKE_TARGET_LINUX_MUSL)
# Setting RLIMIT_NOFILE breaks debugging of coreclr on Alpine Linux for some reason
add_definitions(-DDONT_SET_RLIMIT_NOFILE)
# On Alpine Linux, we need to ensure that the reported stack range for the primary thread is
# larger than the initial committed stack size.
add_definitions(-DENSURE_PRIMARY_STACK_SIZE)
endif(CLR_CMAKE_HOST_ALPINE_LINUX)
endif(CLR_CMAKE_TARGET_LINUX_MUSL)

# turn off capability to remove unused functions (which was enabled in debug build with sanitizers)
set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${CMAKE_SHARED_LINKER_FLAGS_DEBUG} -Wl,--no-gc-sections")
Expand Down Expand Up @@ -316,9 +316,9 @@ if(CLR_CMAKE_TARGET_LINUX)
endif(CLR_CMAKE_USE_SYSTEM_LIBUNWIND)

# bundled libunwind requires using libucontext on alpine and x86 and ppc64le
if(CLR_CMAKE_TARGET_ALPINE_LINUX AND (CLR_CMAKE_TARGET_ARCH_I386 OR CLR_CMAKE_TARGET_ARCH_POWERPC64))
if(CLR_CMAKE_TARGET_LINUX_MUSL AND (CLR_CMAKE_TARGET_ARCH_I386 OR CLR_CMAKE_TARGET_ARCH_POWERPC64))
target_link_libraries(coreclrpal PUBLIC ucontext)
endif(CLR_CMAKE_TARGET_ALPINE_LINUX AND (CLR_CMAKE_TARGET_ARCH_I386 OR CLR_CMAKE_TARGET_ARCH_POWERPC64))
endif(CLR_CMAKE_TARGET_LINUX_MUSL AND (CLR_CMAKE_TARGET_ARCH_I386 OR CLR_CMAKE_TARGET_ARCH_POWERPC64))

endif(CLR_CMAKE_TARGET_LINUX)

Expand Down
30 changes: 0 additions & 30 deletions src/mono/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -218,36 +218,6 @@ elseif(CLR_CMAKE_HOST_OS 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(CLR_CMAKE_HOST_OS STREQUAL "android")
set(HOST_LINUX 1)
add_definitions(-D_GNU_SOURCE -D_REENTRANT)
Expand Down
2 changes: 1 addition & 1 deletion src/native/libs/System.Globalization.Native/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ endif()

install (TARGETS System.Globalization.Native-Static DESTINATION ${STATIC_LIB_DESTINATION} COMPONENT libs)

if(NOT CLR_CMAKE_TARGET_APPLE AND NOT CLR_CMAKE_TARGET_ANDROID AND NOT CLR_CMAKE_TARGET_ALPINE_LINUX)
if(NOT CLR_CMAKE_TARGET_APPLE AND NOT CLR_CMAKE_TARGET_ANDROID AND NOT CLR_CMAKE_TARGET_LINUX_MUSL)
if (GEN_SHARED_LIB)
add_custom_command(TARGET System.Globalization.Native POST_BUILD
COMMENT "Verifying System.Globalization.Native.so dependencies"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ if (GEN_SHARED_LIB)
#
# on Linux, the build will succeed with undefined symbols, then the script reports them
# and fails the build for us.
if (NOT APPLE AND NOT CLR_CMAKE_TARGET_ALPINE_LINUX)
if (NOT APPLE AND NOT CLR_CMAKE_TARGET_LINUX_MUSL)
add_custom_command(TARGET System.Security.Cryptography.Native.OpenSsl POST_BUILD
COMMENT "Verifying System.Security.Cryptography.Native.OpenSsl.so dependencies"
COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/../verify-so.sh
Expand Down

0 comments on commit 9fc5fcb

Please sign in to comment.