Skip to content

Commit

Permalink
[mono] Fix libcoreclr.so build incompatibilities on Linux (dotnet#59988)
Browse files Browse the repository at this point in the history
* Link with -z relro -z now to enable full RELRO mode
  Fixes dotnet#59904

* Generate and link in version.c to provide version string
  Fixes dotnet/source-build#2484
  • Loading branch information
uweigand authored Oct 6, 2021
1 parent a442df0 commit e645b51
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
15 changes: 10 additions & 5 deletions src/mono/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
set(HOST_LINUX 1)
add_definitions(-D_GNU_SOURCE -D_REENTRANT)
add_definitions(-D_THREAD_SAFE)
# Enable the "full RELRO" options (RELRO & BIND_NOW) at link time
add_link_options(-Wl,-z,relro)
add_link_options(-Wl,-z,now)
elseif(CMAKE_SYSTEM_NAME STREQUAL "Android")
set(HOST_LINUX 1)
add_definitions(-D_GNU_SOURCE -D_REENTRANT)
Expand Down Expand Up @@ -737,11 +740,13 @@ endif()
######################################
# EXTRACT VERSION
######################################
if(NOT EXISTS "${CMAKE_CURRENT_BINARY_DIR}/_version.h")
if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")
if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")
if(NOT EXISTS "${CMAKE_CURRENT_BINARY_DIR}/_version.h")
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/_version.h" "#undef VER_PRODUCTVERSION_STR\n#define VER_PRODUCTVERSION_STR \"42.42.42.42424\"\n")
else()
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/_version.h" "static char sccsid[] __attribute__((used)) = \"@(#)Version 42.42.42.42424 @Commit: AAA\";\n")
endif()
else()
if(NOT EXISTS "${CMAKE_CURRENT_BINARY_DIR}/version.c")
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/version.c" "static char sccsid[] __attribute__((used)) = \"@(#)Version 42.42.42.42424 @Commit: AAA\";\n")
endif()
endif()
if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows" AND NOT EXISTS "${CMAKE_CURRENT_BINARY_DIR}/NativeVersion.rc")
Expand All @@ -763,7 +768,7 @@ if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")
file(STRINGS "${CMAKE_CURRENT_BINARY_DIR}/_version.h" product_version_string_line REGEX "VER_PRODUCTVERSION_STR ")
string(REGEX REPLACE ".*VER_PRODUCTVERSION_STR *(.*)" "\\1" product_version_string ${product_version_string_line})
else()
file(STRINGS "${CMAKE_CURRENT_BINARY_DIR}/_version.h" product_version_string_line REGEX "sccsid")
file(STRINGS "${CMAKE_CURRENT_BINARY_DIR}/version.c" product_version_string_line REGEX "sccsid")
string(REGEX REPLACE ".*Version *(.*)\";" "\"\\1\"" product_version_string ${product_version_string_line})
endif()

Expand Down
9 changes: 7 additions & 2 deletions src/mono/mono.proj
Original file line number Diff line number Diff line change
Expand Up @@ -736,13 +736,18 @@
</Target>

<Target Name="GenerateMonoVersionFile" DependsOnTargets="InitializeGenerateRuntimeVersionFile">
<PropertyGroup>
<NativeVersionFile Condition="'$(OS)' == 'Windows_NT'">_version.h</NativeVersionFile>
<NativeVersionFile Condition="'$(OS)' != 'Windows_NT'">version.c</NativeVersionFile>
</PropertyGroup>

<MSBuild Projects="$(RepoRoot)eng\empty.csproj"
Targets="GenerateRuntimeVersionFile"
Properties="NativeVersionFile=$(MonoObjDir)_version.h;RuntimeVersionFile=$(MonoObjDir)runtime_version.h" />
Properties="NativeVersionFile=$(MonoObjDir)$(NativeVersionFile);RuntimeVersionFile=$(MonoObjDir)runtime_version.h" />
<MSBuild Condition="'$(BuildMonoAOTCrossCompiler)' == 'true'"
Projects="$(RepoRoot)eng\empty.csproj"
Targets="GenerateRuntimeVersionFile"
Properties="NativeVersionFile=$(MonoObjCrossDir)_version.h;RuntimeVersionFile=$(MonoObjCrossDir)runtime_version.h" />
Properties="NativeVersionFile=$(MonoObjCrossDir)$(NativeVersionFile);RuntimeVersionFile=$(MonoObjCrossDir)runtime_version.h" />
</Target>

<!-- The standard set of targets that need to run before the BuildMono target runs -->
Expand Down
2 changes: 2 additions & 0 deletions src/mono/mono/mini/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,8 @@ endif()

if(HOST_WIN32)
set(mini_sources "${mini_sources};${PROJECT_BINARY_DIR}/../../NativeVersion.rc") # this is generated by GenerateMonoVersionFile in mono.proj
elseif(NOT HOST_BROWSER)
set(mini_sources "${mini_sources};${PROJECT_BINARY_DIR}/../../version.c") # this is generated by GenerateMonoVersionFile in mono.proj
endif()

set(monosgen-sources "${metadata_sources};${utils_sources};${sgen_sources};${icu_shim_sources};${mini_sources};${zlib_sources}")
Expand Down

0 comments on commit e645b51

Please sign in to comment.