Skip to content

Commit

Permalink
[Android] Link native shared libraries with 16k page alignment
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
grendello committed Jul 8, 2024
1 parent 101c0da commit db6f208
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/mono/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,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)
Expand Down
6 changes: 6 additions & 0 deletions src/native/libs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,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)
Expand Down

0 comments on commit db6f208

Please sign in to comment.