Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Android] Link native shared libraries with 16k page alignment #104577

Merged
merged 6 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions eng/native/configureplatform.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,11 @@ The .NET Foundation licenses this file to you under the MIT license.
<LinkerArg Include="@(ExtraLinkerArg->'-Wl,%(Identity)')" />
<LinkerArg Include="@(NativeFramework->'-framework %(Identity)')" Condition="'$(_IsApplePlatform)' == 'true'" />
<LinkerArg Include="-Wl,--eh-frame-hdr" Condition="'$(_IsApplePlatform)' != 'true'" />

<!-- 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.
-->
<LinkerArg Include="-Wl,-z,max-page-size=16384" Condition=" '$(_linuxLibcFlavor)' == 'bionic' and '$(NativeLib)' == 'Shared' and ('$(_targetArchitecture)' == 'x64' or '$(_targetArchitecture)' == 'arm64')" />
</ItemGroup>

<Exec Command="clang --version" Condition="'$(_IsApplePlatform)' == 'true'" IgnoreExitCode="true" StandardOutputImportance="Low" ConsoleToMSBuild="true">
Expand Down
7 changes: 7 additions & 0 deletions src/mono/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down
6 changes: 6 additions & 0 deletions src/tasks/LibraryBuilder/LibraryBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,12 @@ private string BuildAndroidLibrary(List<string> sources, List<string> 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);
Expand Down
Loading