diff --git a/src/coreclr/tools/Common/Compiler/DependencyAnalysis/ObjectNodeSection.cs b/src/coreclr/tools/Common/Compiler/DependencyAnalysis/ObjectNodeSection.cs index de6ec995220e4d..6f0c0bec5aba19 100644 --- a/src/coreclr/tools/Common/Compiler/DependencyAnalysis/ObjectNodeSection.cs +++ b/src/coreclr/tools/Common/Compiler/DependencyAnalysis/ObjectNodeSection.cs @@ -47,7 +47,7 @@ public bool IsStandardSection public static readonly ObjectNodeSection FoldableReadOnlyDataSection = new ObjectNodeSection("rdata", SectionType.ReadOnly); public static readonly ObjectNodeSection TextSection = new ObjectNodeSection("text", SectionType.Executable); public static readonly ObjectNodeSection TLSSection = new ObjectNodeSection("TLS", SectionType.Writeable); - public static readonly ObjectNodeSection BssSection = new ObjectNodeSection("bss", SectionType.Writeable); + public static readonly ObjectNodeSection BssSection = new ObjectNodeSection("bss", SectionType.Uninitialized); public static readonly ObjectNodeSection HydrationTargetSection = new ObjectNodeSection("hydrated", SectionType.Uninitialized); public static readonly ObjectNodeSection ManagedCodeWindowsContentSection = new ObjectNodeSection(".managedcode$I", SectionType.Executable); public static readonly ObjectNodeSection FoldableManagedCodeWindowsContentSection = new ObjectNodeSection(".managedcode$I", SectionType.Executable); diff --git a/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/DehydratableObjectNode.cs b/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/DehydratableObjectNode.cs index daf034850e440a..ba527a6be59b9f 100644 --- a/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/DehydratableObjectNode.cs +++ b/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/DehydratableObjectNode.cs @@ -7,16 +7,28 @@ public abstract class DehydratableObjectNode : ObjectNode { public sealed override ObjectNodeSection GetSection(NodeFactory factory) { - return factory.MetadataManager.IsDataDehydrated ? ObjectNodeSection.HydrationTargetSection : GetDehydratedSection(factory); + ObjectNodeSection desiredSection = GetDehydratedSection(factory); + + return factory.MetadataManager.IsDataDehydrated + && desiredSection.Type != SectionType.Uninitialized + ? ObjectNodeSection.HydrationTargetSection : desiredSection; } public sealed override ObjectData GetData(NodeFactory factory, bool relocsOnly = false) { ObjectData result = GetDehydratableData(factory, relocsOnly); - // If we're not generating full data yet, or dehydration is not active, - // return the ObjectData as is. - if (relocsOnly || !factory.MetadataManager.IsDataDehydrated) + // If we're not actually generating data yet, don't dehydrate + bool skipDehydrating = relocsOnly; + + // If dehydration is not active, don't dehydrate + skipDehydrating |= !factory.MetadataManager.IsDataDehydrated; + + // If the data would be placed into an uninitialized section, that's better + // than dehydrating a bunch of zeros. + skipDehydrating |= GetDehydratedSection(factory).Type == SectionType.Uninitialized; + + if (skipDehydrating) return result; // Otherwise return the dehydrated data diff --git a/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/NonGCStaticsNode.cs b/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/NonGCStaticsNode.cs index fef79f3bb9b6f5..08acf8265190a7 100644 --- a/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/NonGCStaticsNode.cs +++ b/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/NonGCStaticsNode.cs @@ -16,7 +16,7 @@ namespace ILCompiler.DependencyAnalysis /// with the class constructor context if the type has a class constructor that /// needs to be triggered before the type members can be accessed. /// - public class NonGCStaticsNode : ObjectNode, ISymbolDefinitionNode, ISortableSymbolNode + public class NonGCStaticsNode : DehydratableObjectNode, ISymbolDefinitionNode, ISortableSymbolNode { private readonly MetadataType _type; private readonly PreinitializationManager _preinitializationManager; @@ -31,7 +31,7 @@ public NonGCStaticsNode(MetadataType type, PreinitializationManager preinitializ protected override string GetName(NodeFactory factory) => this.GetMangledName(factory.NameMangler); - public override ObjectNodeSection GetSection(NodeFactory factory) + protected override ObjectNodeSection GetDehydratedSection(NodeFactory factory) { if (_preinitializationManager.HasLazyStaticConstructor(_type) || _preinitializationManager.IsPreinitialized(_type)) @@ -118,7 +118,7 @@ protected override DependencyList ComputeNonRelocationBasedDependencies(NodeFact return dependencyList; } - public override ObjectData GetData(NodeFactory factory, bool relocsOnly) + protected override ObjectData GetDehydratableData(NodeFactory factory, bool relocsOnly) { ObjectDataBuilder builder = new ObjectDataBuilder(factory, relocsOnly);