Use android-native-tools for NativeAOT linking instead of Android NDK#10704
Draft
Use android-native-tools for NativeAOT linking instead of Android NDK#10704
Conversation
This eliminates the NDK dependency for NativeAOT builds by using our custom LLVM build (android-native-tools) that ships with the Android workload. Changes: - Add LinkNativeAotLibrary task that invokes ld.lld from android-native-tools - Set NativeCompilationDuringPublish=false and NativeLib=Static so ILC produces only object files, then we link them ourselves - Add -z nostart-stop-gc linker flag for __start/__stop symbols - Include sysroot libraries (libc++_static.a, libunwind.a, etc.) in NativeAOT runtime pack - Fix duplicate assembly error by removing items before re-adding them to ResolvedFileToPublish - Keep legacy NDK path behind AndroidNativeAotUseNdk=true flag
NativeAOT's DWARF-based stack unwinder (LLVM libunwind) requires the .eh_frame_hdr section to locate Frame Description Entries during GC stack walking. Without this section, the runtime crashes with SIGSEGV when attempting to read unwind information from invalid memory.
Quote the soname argument to handle project names containing spaces. Without this fix, a project named 'Test Me' would cause the linker to fail with 'cannot open Me: No such file or directory' because the unquoted soname would be split into separate arguments.
The sysroot libraries (libc.so, libdl.so, liblog.so, libm.so, libz.so) are needed by ld.lld for symbol resolution during linking, but should not be packaged in the APK - the real implementations are provided by the Android system at runtime. For Mono/CoreCLR, these are removed in _ResolveAssemblies via ProcessRuntimePackLibraryDirectories, but NativeAOT was excluded from that logic. Remove the exclusion so all runtimes use the same removal logic in the outer build. The inner build still runs ProcessRuntimePackLibraryDirectories to get _RuntimePackLibraryDirectory for linking, but doesn't need to handle the removal - that's done by the outer build after it receives the ResolvedFileToPublish items.
The _CopyToPackDirs target was only copying *.a files for NativeAOT, but crtbegin_so.o and crtend_so.o are required for linking. Add *.o glob pattern to NativeAOT section, matching CoreCLR behavior.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This eliminates the NDK dependency for NativeAOT builds by using our custom LLVM build (android-native-tools) that ships with the Android workload.
Changes: