Skip to content

Root DotNetRuntimeContractDescriptor export against linker GC in NativeAOT - #129818

Merged
max-charlamb merged 2 commits into
dotnet:mainfrom
max-charlamb:ilc-cdac-fix
Jun 25, 2026
Merged

max-charlamb merged 2 commits into
dotnet:mainfrom
max-charlamb:ilc-cdac-fix

Conversation

@max-charlamb

@max-charlamb max-charlamb commented Jun 24, 2026 •

Copy link
Copy Markdown
Member

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:

<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.

…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]>
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @agocke, @dotnet/ilc-contrib
See info in area-owners.md if you want to be subscribed.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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,DotNetRuntimeContractDescriptor on non-Windows, non-Apple, non-browser targets to force-retain the contract descriptor symbol when DebuggerSupport != false.
  • Add -Wl,-u,_DotNetRuntimeContractDescriptor on Apple targets (underscore-prefixed Mach-O symbol) when DebuggerSupport != 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]>
@max-charlamb
max-charlamb marked this pull request as ready for review June 24, 2026 20:27
Copilot AI review requested due to automatic review settings June 24, 2026 20:27

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated no new comments.

@max-charlamb
max-charlamb enabled auto-merge (squash) June 24, 2026 23:32
@max-charlamb
max-charlamb merged commit 64cc129 into dotnet:main Jun 25, 2026
111 of 113 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-preview7 milestone Jun 25, 2026
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]>
@github-actions github-actions Bot locked and limited conversation to collaborators Jul 26, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants