Skip to content

Commit 8e6df8d

Browse files
[Android] Link native shared libraries with 16k page alignment (#104577)
Fixes: #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ý <MichalStrehovsky@users.noreply.github.com>
1 parent 6954e80 commit 8e6df8d

File tree

4 files changed

+25
-0
lines changed

4 files changed

+25
-0
lines changed

eng/native/configureplatform.cmake

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,13 @@ if(NOT CLR_CMAKE_TARGET_BROWSER AND NOT CLR_CMAKE_TARGET_WASI)
494494
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
495495
endif()
496496

497+
if (CLR_CMAKE_TARGET_ANDROID)
498+
# Google requires all the native libraries to be aligned to 16 bytes (for 16k memory page size)
499+
# This applies only to 64-bit binaries
500+
if(CLR_CMAKE_TARGET_ARCH_ARM64 OR CLR_CMAKE_TARGET_ARCH_AMD64)
501+
add_link_options(LINKER:-z,max-page-size=16384)
502+
endif()
503+
endif()
497504
string(TOLOWER "${CMAKE_BUILD_TYPE}" LOWERCASE_CMAKE_BUILD_TYPE)
498505
if(LOWERCASE_CMAKE_BUILD_TYPE STREQUAL debug)
499506
# Clear _FORTIFY_SOURCE=2, if set

src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.Unix.targets

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,11 @@ The .NET Foundation licenses this file to you under the MIT license.
240240
<LinkerArg Include="@(ExtraLinkerArg->'-Wl,%(Identity)')" />
241241
<LinkerArg Include="@(NativeFramework->'-framework %(Identity)')" Condition="'$(_IsApplePlatform)' == 'true'" />
242242
<LinkerArg Include="-Wl,--eh-frame-hdr" Condition="'$(_IsApplePlatform)' != 'true'" />
243+
244+
<!-- Google requires all the native libraries to be aligned to 16 bytes (for 16k memory page size)
245+
This is required only for 64-bit binaries.
246+
-->
247+
<LinkerArg Include="-Wl,-z,max-page-size=16384" Condition=" '$(_linuxLibcFlavor)' == 'bionic' and '$(NativeLib)' == 'Shared' and ('$(_targetArchitecture)' == 'x64' or '$(_targetArchitecture)' == 'arm64')" />
243248
</ItemGroup>
244249

245250
<Exec Command="clang --version" Condition="'$(_IsApplePlatform)' == 'true'" IgnoreExitCode="true" StandardOutputImportance="Low" ConsoleToMSBuild="true">

src/mono/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,13 @@ if(CLR_CMAKE_HOST_APPLE)
897897
endif()
898898
endif()
899899

900+
if(HOST_ANDROID)
901+
if(HOST_AMD64 OR HOST_ARM64)
902+
# Google requires all the native libraries to be aligned to 16 bytes (for 16k memory page size)
903+
# This applies only to 64-bit binaries
904+
add_link_options(LINKER:-z,max-page-size=16384)
905+
endif()
906+
endif()
900907
### End of OS specific checks
901908

902909
include_directories("${CLR_SRC_NATIVE_DIR}")

src/tasks/LibraryBuilder/LibraryBuilder.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,12 @@ private string BuildAndroidLibrary(List<string> sources, List<string> libs, List
337337
buildOptions.CompilerArguments.Add(IsSharedLibrary ? $"-shared -o {libraryName}" : $"-o {libraryName}");
338338
buildOptions.IncludePaths.Add(MonoRuntimeHeaders);
339339
buildOptions.LinkerArguments.Add($"--soname={libraryName}");
340+
341+
// Google requires all the native libraries to be aligned to 16 bytes (for 16k memory page size)
342+
// This is required only for 64-bit binaries.
343+
if (string.CompareOrdinal ("android-arm64", RuntimeIdentifier) == 0 || string.CompareOrdinal ("android-x64", RuntimeIdentifier) == 0) {
344+
buildOptions.LinkerArguments.Add($"-z,max-page-size=16384");
345+
}
340346
buildOptions.LinkerArguments.AddRange(linkerArgs);
341347
buildOptions.NativeLibraryPaths.AddRange(libs);
342348
buildOptions.Sources.AddRange(sources);

0 commit comments

Comments
 (0)