From 11b63830f62db068a917c079726574b300663a38 Mon Sep 17 00:00:00 2001 From: Marek Habersack Date: Mon, 8 Jul 2024 23:18:33 +0200 Subject: [PATCH] [Android] Link native shared libraries with 16k page alignment 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. --- src/mono/CMakeLists.txt | 10 ++++++++++ src/native/libs/CMakeLists.txt | 6 ++++++ 2 files changed, 16 insertions(+) diff --git a/src/mono/CMakeLists.txt b/src/mono/CMakeLists.txt index 8fe845296e035..75afda01083e8 100644 --- a/src/mono/CMakeLists.txt +++ b/src/mono/CMakeLists.txt @@ -503,6 +503,16 @@ else() message(FATAL_ERROR "TARGET_ARCH='${TARGET_ARCH}' not supported.") endif() +if(TARGET_ANDROID AND (TARGET_AMD64 OR TARGET_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 + message("Android: TARGET_ARCH == ${TARGET_ARCH}") + if((TARGET_ARCH STREQUAL "arm64") OR (TARGET_ARCH STREQUAL "x86_64")) + message("Android: adding 16k alignment options") + add_link_options(LINKER:-z,max-page-size=16384) + endif() +endif() + # arm64 MacCatalyst runtime host or AOT target is more like Apple mobile targets than x64 if ((HOST_MACCAT AND HOST_ARM64) OR (TARGET_MACCAT AND TARGET_ARM64)) set(TARGET_APPLE_MOBILE 1) diff --git a/src/native/libs/CMakeLists.txt b/src/native/libs/CMakeLists.txt index eb1df8fa2a210..d9105b4751429 100644 --- a/src/native/libs/CMakeLists.txt +++ b/src/native/libs/CMakeLists.txt @@ -62,6 +62,12 @@ if (CLR_CMAKE_TARGET_UNIX OR CLR_CMAKE_TARGET_BROWSER OR CLR_CMAKE_TARGET_WASI) if (CROSS_ROOTFS) include_directories(SYSTEM "${CROSS_ROOTFS}/usr/include") endif () + + # 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(-Wl,-z,max-page-size=16384) + endif() endif () string(TOUPPER ${CMAKE_BUILD_TYPE} UPPERCASE_CMAKE_BUILD_TYPE)