Skip to content

WIP Apply Base Relocs For Webcil in Wasm CoreCLR#126436

Merged
davidwrighton merged 22 commits intodotnet:mainfrom
adamperlin:adamperlin/wasm-object-writer-runtime-relocs
Apr 3, 2026
Merged

WIP Apply Base Relocs For Webcil in Wasm CoreCLR#126436
davidwrighton merged 22 commits intodotnet:mainfrom
adamperlin:adamperlin/wasm-object-writer-runtime-relocs

Conversation

@adamperlin
Copy link
Copy Markdown
Contributor

This PR is currently untested, but is a WIP implementation for a runtime base relocation scheme for Webcil.

The WasmObjectWriter has been updated to emit PE-style base relocs into a reloc webcil section (the last section in the webcil payload). The relocations use an encoding identical to that used by PE image base style relocs. New base relocation types have been added to represent "wasm function pointer table base" fixups for wasm32 and wasm64.

The Webcil loader path in CoreCLR has been updated to interpret these relocs. In particular ApplyBaseRelocations will now run in the Webcil case, and will handle applying both standard image base style relocations and the new table base relocs. The real table base used at load time is not yet being plumbed through; this will eventually be set as a field in the webcil header itself and is dependent on #126388.

@dotnet-policy-service
Copy link
Copy Markdown
Contributor

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

@adamperlin adamperlin changed the title WIP Apply Base Relocs in Wasm WIP Apply Base Relocs in Wasm CoreCLR Apr 1, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Implements an initial WebAssembly/Webcil runtime base relocation scheme by emitting PE-style base relocation blocks in the Webcil payload and teaching CoreCLR’s Webcil loader path to apply those relocations (including new Wasm table-base fixups).

Changes:

  • Emit a PE-style .reloc-equivalent Webcil section and encode relocation entries (including new Wasm table-base relocation kinds).
  • Enable ApplyBaseRelocations for Webcil images and apply both image-base and table-base relocation deltas.
  • Extend Webcil decoding and documentation to expose base relocation presence and locate relocation data.

Reviewed changes

Copilot reviewed 9 out of 10 changed files in this pull request and generated 9 comments.

Show a summary per file
File Description
src/coreclr/vm/peimagelayout.inl Adds decoder-dispatched relocation helpers (e.g., SetRelocated, HasBaseRelocations) for PE/Webcil.
src/coreclr/vm/peimagelayout.h Adds temporary table-base offset plumbing and switches SetRelocated to decoder dispatch.
src/coreclr/vm/peimagelayout.cpp Runs base relocations for Webcil and adds handling for Wasm table-base relocation types.
src/coreclr/utilcode/webcildecoder.cpp Exposes Webcil base relocation presence and supports BASERELOC directory-style access.
src/coreclr/tools/Common/Compiler/ObjectWriter/WebcilEncoder.cs Minor formatting adjustments in Webcil section header encoding size helper.
src/coreclr/tools/Common/Compiler/ObjectWriter/WasmObjectWriter.cs Emits Webcil relocation section data and records base relocation entries from certain relocation types.
src/coreclr/tools/Common/Compiler/DependencyAnalysis/Relocation.cs Adds Webcil relocation kinds and maps Wasm table index relocations to file/base-reloc types.
src/coreclr/pal/inc/rt/ntimage.h Defines Webcil-specific base relocation type codes for Wasm table-base fixups.
src/coreclr/inc/webcildecoder.h Adds relocated-state tracking and a HasBaseRelocations API for Webcil.
docs/design/mono/webcil.md Documents Reserved0 usage for base reloc section indexing and the relocation block/entry encoding.

adamperlin and others added 2 commits April 1, 2026 16:22
…r.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings April 1, 2026 23:27
…r.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
adamperlin and others added 2 commits April 1, 2026 16:28
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 9 out of 10 changed files in this pull request and generated 10 comments.

@adamperlin adamperlin changed the title WIP Apply Base Relocs in Wasm CoreCLR WIP Apply Base Relocs For Webcil in Wasm CoreCLR Apr 2, 2026
…r.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings April 2, 2026 16:47
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 9 out of 10 changed files in this pull request and generated 4 comments.

When emitting base relocations, webcil section addresses were not yet
assigned, leading to a bug where base relocations were not properly
offset by the section they apply to. This commit re-orders our base
relocation construction/emission so that we construct our base
relocations AFTER Webcil sections have been assigned addresses
….com:adamperlin/runtime into adamperlin/wasm-object-writer-runtime-relocs
Copilot AI review requested due to automatic review settings April 2, 2026 21:56
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 9 out of 10 changed files in this pull request and generated 5 comments.

- peimagelayout.inl: Use DECODER_DISPATCH for IsRelocated() so Webcil
  images correctly report relocated state instead of always FALSE
- peimagelayout.cpp: Gate PAL_LOADMarkSectionAsNotNeeded with
  !TARGET_WASM to avoid operating on Webcil payload pointers
- webcildecoder.cpp: Delegate HasDirectoryEntry(BASERELOC) to
  HasBaseRelocations() for consistent Reserved0 range validation
- WasmObjectWriter.cs: Use SortedDictionary for _baseRelocMap to
  ensure deterministic reloc block emission order
- WasmObjectWriter.cs: Replace redundant pattern match on
  GetFileRelocationType with direct assignment
- WasmObjectWriter.cs: Use explicit cast instead of as-cast for
  WebcilSection, add Debug.Assert for non-negative offset

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@adamperlin adamperlin marked this pull request as ready for review April 2, 2026 23:28
Copilot AI review requested due to automatic review settings April 2, 2026 23:28
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 9 out of 10 changed files in this pull request and generated 3 comments.

Copy link
Copy Markdown
Member

@davidwrighton davidwrighton left a comment

Choose a reason for hiding this comment

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

LGTM

@davidwrighton
Copy link
Copy Markdown
Member

/ba-g Build analysis seems to have hung. Maybe this will unstick it.

@davidwrighton davidwrighton merged commit d5dc74a into dotnet:main Apr 3, 2026
117 checks passed
radekdoulik pushed a commit to radekdoulik/runtime that referenced this pull request Apr 9, 2026
This PR is currently testable only with future follow on work, but is an implementation for a runtime
base relocation scheme for Webcil.

The WasmObjectWriter has been updated to emit PE-style base relocs into
a `reloc` webcil section (the last section in the webcil payload). The
relocations use an encoding identical to that used by PE image base
style relocs. New base relocation types have been added to represent
"wasm function pointer table base" fixups for wasm32 and wasm64.

The Webcil loader path in CoreCLR has been updated to interpret these
relocs. In particular `ApplyBaseRelocations` will now run in the Webcil
case, and will handle applying both standard image base style
relocations and the new table base relocs. The real table base used at
load time is not yet being plumbed through; this will eventually be set
as a field in the webcil header itself and is dependent on dotnet#126388.

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants