Skip to content

Reduce the number of sections in R2R PE files - #122511

Merged
jkoritzinsky merged 11 commits into
dotnet:mainfrom
jkoritzinsky:less-pe-sections
Jan 8, 2026
Merged

Reduce the number of sections in R2R PE files#122511
jkoritzinsky merged 11 commits into
dotnet:mainfrom
jkoritzinsky:less-pe-sections

Conversation

@jkoritzinsky

Copy link
Copy Markdown
Member

Use ObjectNodePhase.Ordered + ObjectNodeOrder to identify "well-known" nodes that need to go into PE data directory entries.

Also provide a mechanism to fold together sections in the ObjectWriter so we can fold rdata into text and use the "managed code" section for managed code in general and only fold to text in emit.

In the process, fix some implicit dependencies that were hidden by "good" ClassCode choices.

Fixes#121416

Use ObjectNodePhase.Ordered + ObjectNodeOrder to identify "well-known" nodes that need to go into PE data directory entries.
Also provide a mechanism to fold together sections in the ObjectWriter so we can fold rdata into text and use the "managed code" section for managed code in general and only fold to text in emit.
In the process, fix some implicit dependencies that were hidden by "good" ClassCode choices.

CopilotAI 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 reduces the number of sections in R2R PE files by implementing a section folding mechanism and using ObjectNodePhase.Ordered with ObjectNodeOrder to identify well-known nodes that need to be referenced in PE data directory entries.

Key changes:

  • Introduces GetEmitSection method in ObjectWriter to allow PE files to fold rdata into text and use generic managed code sections that get mapped to text at emit time
  • Implements RecordWellKnownSymbol mechanism to track symbols that correspond to PE directory entries (Win32 resources, debug directory, CLR header, exception table)
  • Simplifies node section assignments by removing format-specific logic and relying on emit-time section folding

Reviewed changes

Copilot reviewed 18 out of 18 changed files in this pull request and generated 3 comments.

Show a summary per file
FileDescription
Win32ResourcesNode.csSimplified to always return ReadOnlyDataSection, removing PE-specific logic
RuntimeFunctionsTableNode.csChanged to return TextSection and added Phase/ClassCode to mark as well-known symbol
RuntimeFunctionsGCInfoNode.csSimplified to return ReadOnlyDataSection and added Phase/ClassCode for ordering
MethodWithGCInfo.csUpdated to return ManagedCodeWindowsContentSection for PE (gets folded to TextSection at emit)
MethodColdCodeNode.csUpdated to return ManagedCodeWindowsContentSection for PE (gets folded to TextSection at emit)
DebugDirectoryNode.csChanged to return TextSection and updated ClassCode to use enum value
CopiedStrongNameSignatureNode.csSimplified to always return ReadOnlyDataSection, removing PE-specific logic
CopiedMethodILNode.csSimplified to always return ReadOnlyDataSection, removing PE-specific logic
CopiedMetadataBlobNode.csSimplified to always return ReadOnlyDataSection, removing PE-specific logic
CopiedManagedResourcesNode.csSimplified to always return ReadOnlyDataSection, removing PE-specific logic
CopiedFieldRvaNode.csSimplified to always return ReadOnlyDataSection, removing PE-specific logic
CopiedCorHeaderNode.csSimplified to always return ReadOnlyDataSection, removing PE-specific logic
CoffObjectWriter.Aot.csDefined local XDataSection and PDataSection constants to replace removed global constants
PEObjectWriter.csImplemented GetEmitSection to fold sections and RecordWellKnownSymbol to track PE directory entries; refactored directory population to use symbol-based lookup
ObjectWriter.csAdded GetEmitSection virtual method and RecordWellKnownSymbol callback mechanism
CoffObjectWriter.csAdded handling for SectionType.UnwindData; removed special DebugDirectorySection handling
ObjectNodeSection.csRemoved unused section constants (XDataSection, DebugDirectorySection, CorMetaSection, Win32ResourcesSection, PDataSection)
SortableDependencyNode.csMade ObjectNodePhase and ObjectNodeOrder enums internal; added new entries for DebugDirectoryNode, RuntimeFunctionsGCInfoNode, and RuntimeFunctionsTableNode

Comment threadsrc/coreclr/tools/Common/Compiler/ObjectWriter/PEObjectWriter.cs Outdated
Comment threadsrc/coreclr/tools/Common/Compiler/ObjectWriter/PEObjectWriter.cs Outdated
Comment threadsrc/coreclr/tools/Common/Compiler/ObjectWriter/PEObjectWriter.cs Outdated
jkoritzinskyand others added 2 commits December 12, 2025 15:29
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Remove now-invalid assert
@jkoritzinsky

Copy link
Copy Markdown
MemberAuthor

I think we may have just pushed some reloc out of range on riscv. @am11 any ideas?

@am11

am11 commented Dec 13, 2025

Copy link
Copy Markdown
Member

This diff fixes the issue: gh-122511_riscv64.diff.txt. Perhaps @tomeksowi and @filipnavara would like to give their review on it. :)

On riscv64, RuyJIT primarily emits PC-relative call and address-materialization sequences using R_RISCV_CALL_PLT and R_RISCV_PCREL_HI20/LO12_* relocations. As with other architectures, although the ELF ABI defines a large number of relocation types, both system libraries and CoreCLR converge on a small, stable subset in practice.

RISC-V’s design separates address materialization from control transfer and treats linker relaxation as a first-class concept, which allows these generic PC-relative sequences to be reused across symbol classes and code models and adapted by the linker as needed. The main trade-off is that these sequences remain fundamentally limited to a ±2 GB PC-relative range.

@tomeksowi

Copy link
Copy Markdown
Member

This diff fixes the issue: gh-122511_riscv64.diff.txt. Perhaps @tomeksowi and @filipnavara would like to give their review on it. :)

LGTM, thanks :)

Comment threadsrc/coreclr/tools/Common/Compiler/ObjectWriter/PEObjectWriter.cs Outdated
Comment threadsrc/coreclr/tools/Common/Compiler/ObjectWriter/PEObjectWriter.cs Outdated
Co-authored-by: Adeel Mujahid <3840695+am11@users.noreply.github.com>

@kotlarmiloskotlarmilos left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, thanks! @elinor-fung or @MichalStrehovsky should sign-off.

Comment threadsrc/coreclr/tools/Common/Compiler/ObjectWriter/PEObjectWriter.cs Outdated

@davidwrightondavidwrighton left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Approved modulo the RiscV detail.

Comment threadsrc/coreclr/tools/Common/Compiler/ObjectWriter/PEObjectWriter.cs Outdated
@jkoritzinsky
jkoritzinsky enabled auto-merge (squash) January 6, 2026 21:53

@elinor-fungelinor-fung left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you also update readytorun-platform-native-envelope.md?

Is it worth running the crossgen2 outerloop?

@jkoritzinsky

Copy link
Copy Markdown
MemberAuthor

I don't think we need to run outerloop on this as we get enough validation from corelib R2R.

@jkoritzinsky

Copy link
Copy Markdown
MemberAuthor

/ba-g ios deadletter

@jkoritzinsky
jkoritzinsky merged commit 198abe0 into dotnet:mainJan 8, 2026
96 of 99 checks passed
@jkoritzinsky
jkoritzinsky deleted the less-pe-sections branch January 8, 2026 21:57
@BrzVladBrzVlad mentioned this pull request Jan 12, 2026
15 tasks
@github-actionsgithub-actionsBot locked and limited conversation to collaborators Feb 8, 2026
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-ReadyToRuntenet-performancePerformance related issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Adjust PEObjectWriter to not emit data into separate sections for separate image data directories

8 participants

@jkoritzinsky@am11@tomeksowi@davidwrighton@kotlarmilos@elinor-fung@steveisok