You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
PR #11311 measured the following using samples/NativeAOT/NativeAOT.csproj in Release with the trimmable typemap:
Artifact
libc++ baseline
Without libc++
Reduction
arm64 APK
1,575,849 B
1,382,336 B
193,513 B (12.28%)
x64 APK
1,639,459 B
1,439,677 B
199,782 B (12.19%)
arm64 libNativeAOT.so
3,481,880 B
2,943,232 B
538,648 B (15.47%)
x64 libNativeAOT.so
3,404,896 B
2,866,728 B
538,168 B (15.81%)
This removes roughly 540 KB per ABI from the native shared library.
Startup impact
Statically linking libc++ increases startup work through:
additional mapped/decompressed native pages;
additional relocations;
C++ runtime and static-initializer execution;
thread-local/runtime exception infrastructure;
increased page faults and resident memory.
The exact startup improvement still needs measurement, but removing this dependency reduces work on the application startup path by construction.
libunwind collisions
There are currently two distinct unwind implementations involved:
NativeAOT contains a private LLVM libunwind implementation in libRuntime.WorkstationGC.a. It is used by UnixNativeCodeManager and UnwindHelpers to parse DWARF/compact-unwind information and virtually unwind managed frames for GC stack walking, exception handling, stack traces, profiling, and related runtime services.
libc++abi.a references the _Unwind_* ABI, causing the final Android link to extract objects from the Android NDK's libunwind.a.
With NDK r29, these copies can define the same global symbols, producing the duplicate-symbol failure tracked by dotnet/runtime#121172.
The runtime-side fix, dotnet/runtime#128927, privatizes NativeAOT's libunwind symbols in .NET 11 Preview 7. That is still valuable defense-in-depth, especially for third-party native libraries.
Removing libc++ solves a different layer of the problem: it removes the main built-in consumer that causes NDK libunwind to be pulled into our application link at all.
Current dependency inventory
The Android NativeAOT host currently compiles the sources listed in:
src/native/nativeaot/host/CMakeLists.txt
The major libc++ dependency groups in the resulting libnaot-android.*.a are:
Dependency group
Current source
charconv, locale, owning strings and formatting
std::format-based logging
thread, atomic wait, chrono and system errors
std::thread and std::binary_semaphore
hashing and allocation
std::unordered_map and owning std::string state
C++ ABI
new/delete, static guards, pure virtual and terminate
std::string_view, std::array, and simple atomics are generally header-only and are not by themselves reasons to remove code. The work should be driven by the final archive's unresolved symbols rather than by a blanket ban on all std::* types.
Remove nontrivial function-local/static objects that require __cxa_guard_*.
Remove pure-virtual/terminate dependencies where possible.
Add minimal malloc/free-backed new/delete/nothrow shims in src/native/nativeaot/host/cxx-shims.cc.Not needed. The ILC SDK already ships libstdc++compat.a, which defines the five allocation-ABI symbols the NativeAOT runtime itself references (operator delete, operator delete[], both nothrowoperator new overloads, and std::nothrow). Our targets were unconditionally removing that archive because it conflicts with static libc++; with libc++ gone there is no conflict, so we simply keep it. No shim file exists.
llvm-nm --undefined-only on libnaot-android.release-static-release.a now reports no __cxa_*, _ZTV/_ZTI, _Unwind_* or terminate references.
Add an opt-in no-libc++ NativeAOT build — skipped deliberately. A temporary _AndroidNativeAotDropLibCxx property was used during prototyping, but once the link proved clean with zero shims there was no reason to ship a second configuration, so [NativeAOT] Stop linking and shipping libc++ and libunwind #12523 makes the removal unconditional instead.
Remove libc++ from NativeAOT link inputs by default (#12523)
Removed libc++_static.a and libc++abi.a from both the legacy NDK/clang path and the workload NativeLinker path.
libclang_rt.builtins is kept. NDK libunwind.a was dropped at the same time (see below).
Add -nostdlib++ — not applicable. The final .so is linked by LinkNativeAotSharedLibrary using ld.lld directly, with fully explicit inputs and AllowUndefinedSymbols = false. No compiler driver is in a position to add libc++ implicitly, and ld.lld would reject a driver flag. LinkStandardCPlusPlusLibrary=false remains as belt-and-braces for the ILC/SDK path.
Remove libc++ archives from NativeAOT runtime packs (#12524)
Stop copying and packaging libc++_static.a, libc++abi.a and libunwind.a for NativeAOT.
Adds a CplusPlus item kind so CoreCLR keeps them while NativeAOT still gets crtbegin_so.o, crtend_so.o and libclang_rt.builtins-*.a.
Removes 18,398,848 B per ABI, roughly 55 MB across the three shipped ABIs.
libc++abi.a was the only consumer pulling it in. With libc++ gone the archive links with zero unresolved symbols under -Wl,--no-undefined and -Wl,--error-unresolved-symbols.
The published .so no longer contains any _Unwind_Resume reference, whereas the libc++ baseline did.
Measure and lock in size/startup improvements
Application size measured on a default MAUI app (arm64, Release, PublishAot): .so −199,592 B (−0.79%), APK −69,632 B (−0.47%).
Startup timing, page faults, mapped native pages, relocations and working set are still unmeasured.
APK/native-library baselines not yet updated.
Validation requirements
For every supported ABI and both Debug/Release:
link response contains no libc++_static.a or libc++abi.a;
final libNativeAOT.so has no unresolved std::__ndk1::* symbols;
final libNativeAOT.so has no unresolved C++ exception/personality machinery;
APK contains no libc++ payload;
NativeAOT startup succeeds;
GC bridge processing succeeds repeatedly;
managed exceptions, stack walking, and crash handling continue to work;
third-party native-library scenarios continue to link;
both workload NativeLinker and legacy NDK linker paths are covered.
Runtime validation should include arm64 and x64. Where local emulator architecture is unavailable, use Helix rather than leaving the configuration untested.
Status: validated locally on arm64 (workload NativeLinker, Release) — links with no unresolved symbols, no libc++ payload in the APK, and a default MAUI app starts and runs. Remaining ABIs, Debug, and the legacy NDK linker path are covered by CI.
Lessons from previous attempts
PR #11311 proved that removal is feasible, but it touched roughly 34 files and combined:
linker changes;
shared logging redesign;
GC bridge threading changes;
container replacements;
NativeAOT-specific startup workarounds;
unrelated Java peer-shape concerns.
This made the PR difficult to review and caused repeated merge conflicts as NativeAOT work continued landing.
For this effort:
each PR should remove one measurable symbol cluster;
archive symbols should be inspected after every PR;
unrelated typemap/startup/Java peer work should remain separate;
every PR should build and validate independently;
human review should happen incrementally rather than after a single large rewrite.
The previous review also found unsafe %s usage with std::string_view::data(). All replacements must use bounded formatting.
Non-goals
Removing NativeAOT's private libunwind implementation used for managed stack walking.
Summary
Remove the link-time dependency on
libc++_static.aandlibc++abi.afrom Android NativeAOT's finallibNativeAOT.so.This is valuable independently for:
libNativeAOT.soby about 15%.libunwind.agets extracted, avoiding collisions with NativeAOT's private libunwind implementation.Previous feasibility work: #11311.
Motivation
App-size results
PR #11311 measured the following using
samples/NativeAOT/NativeAOT.csprojin Release with the trimmable typemap:libNativeAOT.solibNativeAOT.soThis removes roughly 540 KB per ABI from the native shared library.
Startup impact
Statically linking libc++ increases startup work through:
The exact startup improvement still needs measurement, but removing this dependency reduces work on the application startup path by construction.
libunwind collisions
There are currently two distinct unwind implementations involved:
libRuntime.WorkstationGC.a. It is used byUnixNativeCodeManagerandUnwindHelpersto parse DWARF/compact-unwind information and virtually unwind managed frames for GC stack walking, exception handling, stack traces, profiling, and related runtime services.libc++abi.areferences the_Unwind_*ABI, causing the final Android link to extract objects from the Android NDK'slibunwind.a.With NDK r29, these copies can define the same global symbols, producing the duplicate-symbol failure tracked by dotnet/runtime#121172.
The runtime-side fix, dotnet/runtime#128927, privatizes NativeAOT's libunwind symbols in .NET 11 Preview 7. That is still valuable defense-in-depth, especially for third-party native libraries.
Removing libc++ solves a different layer of the problem: it removes the main built-in consumer that causes NDK libunwind to be pulled into our application link at all.
Current dependency inventory
The Android NativeAOT host currently compiles the sources listed in:
src/native/nativeaot/host/CMakeLists.txtThe major libc++ dependency groups in the resulting
libnaot-android.*.aare:std::format-based loggingstd::threadandstd::binary_semaphorestd::unordered_mapand owningstd::stringstatenew/delete, static guards, pure virtual and terminatestd::string_view,std::array, and simple atomics are generally header-only and are not by themselves reasons to remove code. The work should be driven by the final archive's unresolved symbols rather than by a blanket ban on allstd::*types.Main source areas
Formatting and logging
src/native/clr/include/shared/log_types.hhsrc/native/clr/shared/log_functions.ccsrc/native/common/include/shared/helpers.hhsrc/native/clr/shared/helpers.ccsrc/native/nativeaot/host/bridge-processing.ccsrc/native/nativeaot/host/host.ccsrc/native/clr/host/bridge-processing.ccsrc/native/clr/host/host-shared.ccsrc/native/clr/host/os-bridge.ccsrc/native/clr/runtime-base/android-system-shared.ccsrc/native/clr/runtime-base/util.ccsrc/native/clr/include/runtime-base/util.hhThreading
src/native/clr/include/host/gc-bridge.hhsrc/native/clr/host/gc-bridge.ccContainers and owning state
src/native/clr/include/host/bridge-processing-shared.hhsrc/native/common/include/shared/cpp-util.hhsrc/native/clr/include/runtime-base/android-system.hhsrc/native/clr/runtime-base/android-system.ccsrc/native/clr/runtime-base/android-system-shared.ccsrc/native/clr/runtime-base/logger.ccLinker and packaging
src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.NativeAOT.targetssrc/Xamarin.Android.Build.Tasks/Utilities/NativeRuntimeComponents.cssrc/androidsdk/androidsdk.targetsbuild-tools/create-packs/Microsoft.Android.Runtime.projsrc/native/nativeaot/host/CMakeLists.txtProposed small PR sequence
Introduce printf-style logging primitives (#12140)
log_writev, printf-stylelog_*helpers, andHelpers::abort_applicationf.std::formatpath for Mono/CoreCLR.Migrate NativeAOT-specific formatting call sites (#12140)
src/native/nativeaot/host/*.ccfrom{}formatting to bounded printf-style formatting.%.*sforstd::string_view; never assume.data()is NUL-terminated.Migrate NativeAOT-reachable shared logging (#12148, #12150, #12153, #12155, #12210, #12211, #12218)
os-bridge,logger) to the printf-style API.Remove
std::formatfrom the NativeAOT compilation<format>forXA_HOST_NATIVEAOT.charconv, locale, floating-pointto_chars, and formatting-related symbols disappear fromlibnaot-android.*.a.Replace GC bridge C++ threading primitives (#12141)
std::threadandstd::binary_semaphorewith pthread/POSIX primitives.Replace NativeAOT-reachable owning containers and strings (#12142, #12145)
std::unordered_maptemporary-peer storage with a lightweight/vendored alternative.cpp-util.hhwith direct bounded parsing.Eliminate remaining C++ ABI roots (#12523)
__cxa_guard_*.Add minimal malloc/free-backedNot needed. The ILC SDK already shipsnew/delete/nothrowshims insrc/native/nativeaot/host/cxx-shims.cc.libstdc++compat.a, which defines the five allocation-ABI symbols the NativeAOT runtime itself references (operator delete,operator delete[], bothnothrowoperator newoverloads, andstd::nothrow). Our targets were unconditionally removing that archive because it conflicts with static libc++; with libc++ gone there is no conflict, so we simply keep it. No shim file exists.new char[]path inmonodroid__system_property_get, removed in [NativeAOT] Stop linking and shipping libc++ and libunwind #12523.llvm-nm --undefined-onlyonlibnaot-android.release-static-release.anow reports no__cxa_*,_ZTV/_ZTI,_Unwind_*orterminatereferences.Add an opt-in no-libc++ NativeAOT build— skipped deliberately. A temporary_AndroidNativeAotDropLibCxxproperty was used during prototyping, but once the link proved clean with zero shims there was no reason to ship a second configuration, so [NativeAOT] Stop linking and shipping libc++ and libunwind #12523 makes the removal unconditional instead.Remove libc++ from NativeAOT link inputs by default (#12523)
libc++_static.aandlibc++abi.afrom both the legacy NDK/clang path and the workload NativeLinker path.libclang_rt.builtinsis kept. NDKlibunwind.awas dropped at the same time (see below).Add— not applicable. The final-nostdlib++.sois linked byLinkNativeAotSharedLibraryusingld.llddirectly, with fully explicit inputs andAllowUndefinedSymbols = false. No compiler driver is in a position to add libc++ implicitly, andld.lldwould reject a driver flag.LinkStandardCPlusPlusLibrary=falseremains as belt-and-braces for the ILC/SDK path.Remove libc++ archives from NativeAOT runtime packs (#12524)
libc++_static.a,libc++abi.aandlibunwind.afor NativeAOT.CplusPlusitem kind so CoreCLR keeps them while NativeAOT still getscrtbegin_so.o,crtend_so.oandlibclang_rt.builtins-*.a.Audit whether NDK
libunwind.aremains necessary (#12523)libc++abi.awas the only consumer pulling it in. With libc++ gone the archive links with zero unresolved symbols under-Wl,--no-undefinedand-Wl,--error-unresolved-symbols..sono longer contains any_Unwind_Resumereference, whereas the libc++ baseline did.Measure and lock in size/startup improvements
PublishAot):.so−199,592 B (−0.79%), APK −69,632 B (−0.47%).std::format/charconv/locale payload, so the final cut only harvests what was left. Re-measuringsamples/NativeAOTagainst the Drop libc++ from Android NativeAOT linking #11311 baseline would let us state the cumulative win.Validation requirements
For every supported ABI and both Debug/Release:
libc++_static.aorlibc++abi.a;libNativeAOT.sohas no unresolvedstd::__ndk1::*symbols;libNativeAOT.sohas no unresolved C++ exception/personality machinery;Runtime validation should include arm64 and x64. Where local emulator architecture is unavailable, use Helix rather than leaving the configuration untested.
Status: validated locally on arm64 (workload NativeLinker, Release) — links with no unresolved symbols, no libc++ payload in the APK, and a default MAUI app starts and runs. Remaining ABIs, Debug, and the legacy NDK linker path are covered by CI.
Lessons from previous attempts
PR #11311 proved that removal is feasible, but it touched roughly 34 files and combined:
This made the PR difficult to review and caused repeated merge conflicts as NativeAOT work continued landing.
For this effort:
The previous review also found unsafe
%susage withstd::string_view::data(). All replacements must use bounded formatting.Non-goals
std::string_view,std::array, or simple atomics.Related work