From 8e6df8de0266f7dc6befeaf9daf14d565fbc0b16 Mon Sep 17 00:00:00 2001 From: Marek Habersack Date: Tue, 16 Jul 2024 15:19:06 +0000 Subject: [PATCH] [Android] Link native shared libraries with 16k page alignment (#104577) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes: https://github.com/dotnet/runtime/issues/103360 Context: https://developer.android.com/guide/practices/page-sizes Context: https://github.com/android/ndk/wiki/Changelog-r27#announcements Sometime next year Google will start requiring that all the `.so` libraries included in applications submitted to the Play Store to be aligned to 16k page boundary (as opposed to the current one of 4k). Make changes to cmake scripts used to build both the BCL native libraries and the MonoVM so that the resulting shared libraries have the correct alignment. Co-authored-by: Michal Strehovský --- eng/native/configureplatform.cmake | 7 +++++++ .../BuildIntegration/Microsoft.NETCore.Native.Unix.targets | 5 +++++ src/mono/CMakeLists.txt | 7 +++++++ src/tasks/LibraryBuilder/LibraryBuilder.cs | 6 ++++++ 4 files changed, 25 insertions(+) diff --git a/eng/native/configureplatform.cmake b/eng/native/configureplatform.cmake index 4589c63e7d5bf..10f623e6d5924 100644 --- a/eng/native/configureplatform.cmake +++ b/eng/native/configureplatform.cmake @@ -494,6 +494,13 @@ if(NOT CLR_CMAKE_TARGET_BROWSER AND NOT CLR_CMAKE_TARGET_WASI) set(CMAKE_POSITION_INDEPENDENT_CODE ON) endif() +if (CLR_CMAKE_TARGET_ANDROID) + # Google requires all the native libraries to be aligned to 16 bytes (for 16k memory page size) + # This applies only to 64-bit binaries + if(CLR_CMAKE_TARGET_ARCH_ARM64 OR CLR_CMAKE_TARGET_ARCH_AMD64) + add_link_options(LINKER:-z,max-page-size=16384) + endif() +endif() string(TOLOWER "${CMAKE_BUILD_TYPE}" LOWERCASE_CMAKE_BUILD_TYPE) if(LOWERCASE_CMAKE_BUILD_TYPE STREQUAL debug) # Clear _FORTIFY_SOURCE=2, if set diff --git a/src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.Unix.targets b/src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.Unix.targets index 0783ab21a46c0..07fba4c26ee89 100644 --- a/src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.Unix.targets +++ b/src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.Unix.targets @@ -240,6 +240,11 @@ The .NET Foundation licenses this file to you under the MIT license. + + + diff --git a/src/mono/CMakeLists.txt b/src/mono/CMakeLists.txt index 8fe845296e035..b8fa61cc56e7a 100644 --- a/src/mono/CMakeLists.txt +++ b/src/mono/CMakeLists.txt @@ -897,6 +897,13 @@ if(CLR_CMAKE_HOST_APPLE) endif() endif() +if(HOST_ANDROID) + if(HOST_AMD64 OR HOST_ARM64) + # Google requires all the native libraries to be aligned to 16 bytes (for 16k memory page size) + # This applies only to 64-bit binaries + add_link_options(LINKER:-z,max-page-size=16384) + endif() +endif() ### End of OS specific checks include_directories("${CLR_SRC_NATIVE_DIR}") diff --git a/src/tasks/LibraryBuilder/LibraryBuilder.cs b/src/tasks/LibraryBuilder/LibraryBuilder.cs index d96b5c4d316e8..fa181eb9c40c8 100644 --- a/src/tasks/LibraryBuilder/LibraryBuilder.cs +++ b/src/tasks/LibraryBuilder/LibraryBuilder.cs @@ -337,6 +337,12 @@ private string BuildAndroidLibrary(List sources, List libs, List buildOptions.CompilerArguments.Add(IsSharedLibrary ? $"-shared -o {libraryName}" : $"-o {libraryName}"); buildOptions.IncludePaths.Add(MonoRuntimeHeaders); buildOptions.LinkerArguments.Add($"--soname={libraryName}"); + + // Google requires all the native libraries to be aligned to 16 bytes (for 16k memory page size) + // This is required only for 64-bit binaries. + if (string.CompareOrdinal ("android-arm64", RuntimeIdentifier) == 0 || string.CompareOrdinal ("android-x64", RuntimeIdentifier) == 0) { + buildOptions.LinkerArguments.Add($"-z,max-page-size=16384"); + } buildOptions.LinkerArguments.AddRange(linkerArgs); buildOptions.NativeLibraryPaths.AddRange(libs); buildOptions.Sources.AddRange(sources);