Repository navigation
Root DotNetRuntimeContractDescriptor export against linker GC in NativeAOT - #129818
Merged
Merged
Conversation
…veAOT
On Linux/ELF the NativeAOT link exports DotNetRuntimeContractDescriptor via a
version script (global:) plus --export-dynamic, but neither roots the symbol's
section against --gc-sections. Because the contract descriptor is a pure data
symbol with no other references in the image, it is garbage-collected before it
can be exported, so it ends up in neither .dynsym nor .symtab. This breaks
cDAC/SOS contract-descriptor discovery on Linux NativeAOT processes and dumps
(GetExportSymbolAddress("DotNetRuntimeContractDescriptor") returns nothing).
Windows roots the symbol via .def EXPORTS and Apple via -exported_symbols_list,
so only ELF was affected. Force-retain the symbol with -Wl,-u (underscore-prefixed
on Apple), mirroring the single-file apphost's existing
-Wl,-u,DotNetRuntimeContractDescriptor. Gated on DebuggerSupport != false to
match the existing export request.
Co-authored-by: Copilot <[email protected]>
Contributor
|
Tagging subscribers to this area: @agocke, @dotnet/ilc-contrib |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates NativeAOT’s MSBuild link step to ensure the DotNetRuntimeContractDescriptor data symbol is retained by the linker on platforms where --gc-sections can discard otherwise-unreferenced data, enabling contract-based cDAC discovery via exported symbol lookup on Linux/ELF (and aligning behavior across platforms).
Changes:
- Add
-Wl,-u,DotNetRuntimeContractDescriptoron non-Windows, non-Apple, non-browser targets to force-retain the contract descriptor symbol whenDebuggerSupport != false. - Add
-Wl,-u,_DotNetRuntimeContractDescriptoron Apple targets (underscore-prefixed Mach-O symbol) whenDebuggerSupport != false. - Document why the additional rooting is needed (visibility/export options alone don’t prevent section GC for an unreferenced data symbol on ELF).
Apple already roots the descriptor via -exported_symbols_list (the --export-dynamic-symbol IlcArg lands it in the exports file), so the Apple -Wl,-u line was redundant. Keep only the ELF -Wl,-u retention. Co-authored-by: Copilot <[email protected]>
MichalStrehovsky
approved these changes
Jun 24, 2026
max-charlamb
enabled auto-merge (squash)
June 24, 2026 23:32
eiriktsarpalis
pushed a commit
that referenced
this pull request
Jul 15, 2026
…veAOT (#129818) ## Summary On **Linux/ELF**, a NativeAOT binary strips the `DotNetRuntimeContractDescriptor` symbol entirely -- it is present in neither `.dynsym` nor `.symtab` (confirmed with `readelf`). As a result the cDAC's contract-descriptor discovery (`GetExportSymbolAddress("DotNetRuntimeContractDescriptor")`) returns nothing on Linux and contract-based data access is impossible. On Windows the symbol is exported correctly (`dumpbin /exports`). This is a Linux/ELF-only linker issue: ILC asks for the export on every platform, but on ELF the request is not strong enough to survive the link's `--gc-sections`. ## Root cause ILC asks for the export on every platform via `--export-dynamic-symbol`, realized per-platform by `ExportsFileWriter`: | Platform | Export mechanism | Roots the symbol vs `--gc-sections`? | |----------|------------------|--------------------------------------| | Windows | `.def` `EXPORTS` | **Yes** -- PE exports are GC roots | | Apple | `-exported_symbols_list` (+ `-dead_strip`) | **Yes** -- listed symbols are kept | | **Linux**| version script `global:` (+ `--export-dynamic`) | **No** -- only sets visibility/binding *if the symbol survives*; does not keep its section | `DotNetRuntimeContractDescriptor` is a pure data symbol with no other references in the image (unlike `DotNetRuntimeDebugHeader`, which is written by `PopulateDebugHeaders()` at startup and so stays alive on its own). On ELF, `--gc-sections` collects it before the version script / `--export-dynamic` can export it, and it vanishes from the binary. CoreCLR already solved this exact problem for the single-file apphost by force-keeping the same symbol with `-Wl,-u,DotNetRuntimeContractDescriptor` in `src/native/corehost/apphost/static/CMakeLists.txt`. NativeAOT did not do the equivalent. (The apphost needs its own `-u` because it does not use an exports file; NativeAOT does, which is why only ELF -- not Apple -- is affected here.) ## Fix Root the exported contract descriptor against linker dead-code stripping on ELF in the `LinkNative` target of `Microsoft.NETCore.Native.targets`, mirroring the apphost: ```xml <CustomLinkerArg Include="-Wl,-u,DotNetRuntimeContractDescriptor" Condition="'$(_targetOS)' != 'win' and '$(_IsApplePlatform)' != 'true' and '$(_targetOS)' != 'browser' and '$(DebuggerSupport)' != 'false'" /> ``` - **ELF** (non-win, non-Apple, non-browser): `-Wl,-u,DotNetRuntimeContractDescriptor` forces the symbol as an undefined reference at link start, making its section a GC root so it survives `--gc-sections` and is then exported by the version script + `--export-dynamic`. - **Windows** and **Apple** are excluded: `.def` EXPORTS and `-exported_symbols_list` respectively already root the symbol. Gated on `DebuggerSupport != false`, matching the existing `--export-dynamic-symbol` request. The `-u` only affects *retention*; the existing `--export-dynamic-symbol:` IlcArgs still drive export *visibility*. ## Validation 1. Build a NativeAOT console app for `linux-x64`/`linux-arm64` with default `DebuggerSupport`. 2. `readelf -W --dyn-syms <app> | grep DotNetRuntimeContractDescriptor` -> now **present**. 3. Confirm cDAC/SOS `GetExportSymbolAddress("DotNetRuntimeContractDescriptor")` resolves on a Linux dump. 4. Regression: Windows/Apple exports unchanged; an app built with `DebuggerSupport=false` still omits the symbol. > [!NOTE] > This PR description was generated with assistance from GitHub Copilot. --------- Co-authored-by: Max Charlamb <[email protected]> Co-authored-by: Copilot <[email protected]>
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
Summary
On Linux/ELF, a NativeAOT binary strips the
DotNetRuntimeContractDescriptorsymbol entirely -- it is present in neither.dynsymnor.symtab(confirmed withreadelf). As a result the cDAC's contract-descriptor discovery (GetExportSymbolAddress("DotNetRuntimeContractDescriptor")) returns nothing on Linux and contract-based data access is impossible. On Windows the symbol is exported correctly (dumpbin /exports).This is a Linux/ELF-only linker issue: ILC asks for the export on every platform, but on ELF the request is not strong enough to survive the link's
--gc-sections.Root cause
ILC asks for the export on every platform via
--export-dynamic-symbol, realized per-platform byExportsFileWriter:--gc-sections?.defEXPORTS-exported_symbols_list(+-dead_strip)global:(+--export-dynamic)DotNetRuntimeContractDescriptoris a pure data symbol with no other references in the image (unlikeDotNetRuntimeDebugHeader, which is written byPopulateDebugHeaders()at startup and so stays alive on its own). On ELF,--gc-sectionscollects it before the version script /--export-dynamiccan export it, and it vanishes from the binary.CoreCLR already solved this exact problem for the single-file apphost by force-keeping the same symbol with
-Wl,-u,DotNetRuntimeContractDescriptorinsrc/native/corehost/apphost/static/CMakeLists.txt. NativeAOT did not do the equivalent. (The apphost needs its own-ubecause it does not use an exports file; NativeAOT does, which is why only ELF -- not Apple -- is affected here.)Fix
Root the exported contract descriptor against linker dead-code stripping on ELF in the
LinkNativetarget ofMicrosoft.NETCore.Native.targets, mirroring the apphost:-Wl,-u,DotNetRuntimeContractDescriptorforces the symbol as an undefined reference at link start, making its section a GC root so it survives--gc-sectionsand is then exported by the version script +--export-dynamic..defEXPORTS and-exported_symbols_listrespectively already root the symbol.Gated on
DebuggerSupport != false, matching the existing--export-dynamic-symbolrequest. The-uonly affects retention; the existing--export-dynamic-symbol:IlcArgs still drive export visibility.Validation
linux-x64/linux-arm64with defaultDebuggerSupport.readelf -W --dyn-syms <app> | grep DotNetRuntimeContractDescriptor-> now present.GetExportSymbolAddress("DotNetRuntimeContractDescriptor")resolves on a Linux dump.DebuggerSupport=falsestill omits the symbol.Note
This PR description was generated with assistance from GitHub Copilot.