Skip to content

Resolve NativeAOT DWARF class declarations to their complete types - #138224

Merged
MichalStrehovsky merged 2 commits into
dotnet:mainfrom
jbevain:jbevain/naot-dsym-types
Nov 25, 2026
Merged

MichalStrehovsky merged 2 commits into
dotnet:mainfrom
jbevain:jbevain/naot-dsym-types

Conversation

@jbevain

@jbevain jbevain commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Every class is described with a forward declaration and a complete type, and references from variables, parameters and fields point to the declaration. Debuggers find the complete type by name, but dsymutil only keeps the DIEs that are referenced from code or data. Complete types that nothing references directly, like interfaces, or classes whose methods are all inlined in optimized builds, are dropped from the dSYM, and lldb shows the variables of those types as incomplete types.

The DWARF writer now resolves references to a forward declaration to its complete type, and doesn't emit the declaration, like clang does for types defined in the same unit. CodeView still uses forward references.

Validated on lldb/gdb on Linux with Dwarf 4 and 5, and on macOS with lldb.

Every class is described with a forward declaration and a complete type,
and references from variables, parameters and fields point to the
declaration. Debuggers find the complete type by name, but dsymutil only
keeps the DIEs that are referenced from code or data. Complete types that
nothing references directly, like interfaces, or classes whose methods are
all inlined in optimized builds, are dropped from the dSYM, and lldb shows
the variables of those types as incomplete types.

The DWARF writer now resolves references to a forward declaration to its
complete type, and doesn't emit the declaration, like clang does for types
defined in the same unit. CodeView still uses forward references.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: ce36e465-0dbf-430e-9290-67490662095a
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Nov 24, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
12 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

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

@MichalStrehovsky

Copy link
Copy Markdown
Member

I think we should be able to apply this diff on top that should achieve the same effect:

diff --git a/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/ObjectWriter/Dwarf/DwarfBuilder.cs b/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/ObjectWriter/Dwarf/DwarfBuilder.cs
index ad527c92f0c..251a7a4c11e 100644
--- a/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/ObjectWriter/Dwarf/DwarfBuilder.cs
+++ b/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/ObjectWriter/Dwarf/DwarfBuilder.cs
@@ -109,21 +109,7 @@ public DwarfBuilder(
         public bool UseDwarf5 => _useDwarf5;
         public int FrameRegister => _frameRegister;
 
-        // References to a forward declaration that has a complete type are resolved to the complete type.
-        public uint ResolveOffset(uint typeIndex)
-        {
-            if (typeIndex == 0)
-            {
-                return 0;
-            }
-
-            if (_dwarfTypes[(int)typeIndex - 1] is DwarfClassTypeInfo { CompleteTypeIndex: not 0 } forwardDeclaration)
-            {
-                typeIndex = forwardDeclaration.CompleteTypeIndex;
-            }
-
-            return _dwarfTypeOffsets[typeIndex - 1];
-        }
+        public uint ResolveOffset(uint typeIndex) => typeIndex == 0 ? 0u : _dwarfTypeOffsets[typeIndex - 1];
 
         public void Write(
             SectionWriter infoSectionWriter,
@@ -218,15 +204,8 @@ public void WriteInfoTable(
                 int typeIndex = 0;
                 foreach (DwarfInfo type in _dwarfTypes)
                 {
-                    // Debuggers find the complete type of a declaration by its name, but tools like dsymutil
-                    // only keep the types that are referenced. The declaration isn't emitted, and its references
-                    // are resolved to the complete type, so that the complete type is kept when it is used.
-                    if (type is not DwarfClassTypeInfo { CompleteTypeIndex: not 0 })
-                    {
-                        _dwarfTypeOffsets[typeIndex] = (uint)dwarfInfoWriter.Position;
-                        type.Dump(dwarfInfoWriter);
-                    }
-
+                    _dwarfTypeOffsets[typeIndex] = (uint)dwarfInfoWriter.Position;
+                    type.Dump(dwarfInfoWriter);
                     typeIndex++;
                 }
 
@@ -466,16 +445,20 @@ public uint GetCompleteClassTypeIndex(
             uint forwardDeclarationTypeIndex = 0)
         {
             var classInfo = new DwarfClassTypeInfo(classTypeDescriptor, classFieldsTypeDescriptor, fields, statics);
-            _dwarfTypes.Add(classInfo);
             _staticFields.AddRange(classInfo.StaticVariables);
-            uint typeIndex = (uint)_dwarfTypes.Count;
 
+            // Debuggers find the complete type of a declaration by its name, but tools like dsymutil
+            // only keep the types that are referenced. The complete type replaces the forward declaration,
+            // so that references to the declaration keep the complete type alive.
             if (forwardDeclarationTypeIndex != 0)
             {
-                ((DwarfClassTypeInfo)_dwarfTypes[(int)forwardDeclarationTypeIndex - 1]).CompleteTypeIndex = typeIndex;
+                Debug.Assert(_dwarfTypes[(int)forwardDeclarationTypeIndex - 1] is DwarfClassTypeInfo);
+                _dwarfTypes[(int)forwardDeclarationTypeIndex - 1] = classInfo;
+                return forwardDeclarationTypeIndex;
             }
 
-            return typeIndex;
+            _dwarfTypes.Add(classInfo);
+            return (uint)_dwarfTypes.Count;
         }
 
         public uint GetMemberFunctionTypeIndex(MemberFunctionTypeDescriptor memberDescriptor, uint[] argumentTypes)
diff --git a/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/ObjectWriter/Dwarf/DwarfInfo.cs b/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/ObjectWriter/Dwarf/DwarfInfo.cs
index 54afdc96518..fa0d68c6d65 100644
--- a/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/ObjectWriter/Dwarf/DwarfInfo.cs
+++ b/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/ObjectWriter/Dwarf/DwarfInfo.cs
@@ -226,9 +226,6 @@ internal sealed class DwarfClassTypeInfo : DwarfInfo
         private readonly List<DwarfStaticVariableInfo> _statics;
         private List<DwarfMemberFunction> _methods;
 
-        // The type index of the complete type of a forward declaration, if any.
-        public uint CompleteTypeIndex { get; set; }
-
         public DwarfClassTypeInfo(ClassTypeDescriptor typeDescriptor)
         {
             _isForwardDecl = true;

Applying Michal's suggestion: the complete type takes the place of its
forward declaration in the type list, so references to the declaration
resolve to the complete type without redirecting them when resolving
offsets.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: ce36e465-0dbf-430e-9290-67490662095a
@jbevain

jbevain commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

@MichalStrehovsky thank you, applied!

We should also probably enable the DwarfDump smoke test on macOS, no?

@MichalStrehovsky

Copy link
Copy Markdown
Member

We should also probably enable the DwarfDump smoke test on macOS, no?

If it works, sure. I don't have a mac to test with.

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.

🟢 Approval recommended

No active review issues are reported for the NativeAOT DWARF type-definition update.

No open issues 🎉

What changed in this PR

This PR updates NativeAOT’s DWARF writer so class references point directly to complete types, preventing dsymutil from dropping definitions needed by debuggers.

Changes:

  • Replaces DWARF forward declarations with complete types at the same type index.
  • Passes declaration indices through the shared writer interface while preserving CodeView’s separate records.
  • Adds a smoke-test check that Program has a class definition without a forward declaration.
File Description
src/​tests/​nativeaot/​SmokeTests/​DwarfDump/​Program.cs Checks class debug information for the absence of a forward declaration.
src/​coreclr/​tools/​Common/​TypeSystem/​TypesDebugInfoWriter/​TypesDebugInfoWriter.cs Adds an optional forward-declaration index parameter.
src/​coreclr/​tools/​aot/​ILCompiler.Compiler/​Compiler/​UserDefinedTypeDescriptor.cs Supplies the declaration index when creating a complete type.
src/​coreclr/​tools/​aot/​ILCompiler.Compiler/​Compiler/​ObjectWriter/​Dwarf/​DwarfBuilder.cs Replaces the declaration record with the complete class record.
src/​coreclr/​tools/​aot/​ILCompiler.Compiler/​Compiler/​ObjectWriter/​CodeView/​CodeViewTypesBuilder.cs Matches the updated interface without changing CodeView behavior.

Review effort: Balanced

@MichalStrehovsky
MichalStrehovsky merged commit aa087f0 into dotnet:main Nov 25, 2026
99 of 101 checks passed
@jbevain
jbevain deleted the jbevain/naot-dsym-types branch November 25, 2026 17:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-NativeAOT-coreclr community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants