Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
bb98144
Implement relocation resolution for WASM_MEMORY_ADDR, WASM_TABLE_INDE…
adamperlin Mar 27, 2026
473c46b
WIP relocations for runtime function pointer fixup
adamperlin Mar 27, 2026
cc8a1a8
Webcil native relocations
adamperlin Mar 30, 2026
c33cd2f
Re-use IMAGE_REL_BASED style relocs for Wasm, and collect into
adamperlin Mar 31, 2026
cfd4342
Merge branch 'main' of github.com:dotnet/runtime into adamperlin/wasm…
adamperlin Mar 31, 2026
04c1a48
Fix bug in EncodeSectionHeader, small cleanup of unused code+field re…
adamperlin Mar 31, 2026
f1bebc4
Emit Webcil Reloc section. This means we need to construct the webcil…
adamperlin Mar 31, 2026
9fc1308
Refactor: move cor header/debug directory rva initialization into Bui…
adamperlin Mar 31, 2026
2d81282
Reuse Reserved0 field for index of image base relocs
adamperlin Mar 31, 2026
d207de8
Add ApplyBaseRelocs functionality for webcil in CoreCLR, including ne…
adamperlin Apr 1, 2026
b437a79
Various fixes
adamperlin Apr 1, 2026
a575fae
Update docs
adamperlin Apr 1, 2026
3a341bd
Fix markdown lint errors
adamperlin Apr 1, 2026
397f687
Update src/coreclr/tools/Common/Compiler/ObjectWriter/WasmObjectWrite…
adamperlin Apr 1, 2026
8ec58e8
Update src/coreclr/tools/Common/Compiler/ObjectWriter/WasmObjectWrite…
adamperlin Apr 1, 2026
379833b
Update src/coreclr/utilcode/webcildecoder.cpp
adamperlin Apr 1, 2026
3188b7f
Feedback
adamperlin Apr 1, 2026
43d6f6b
Update src/coreclr/tools/Common/Compiler/ObjectWriter/WasmObjectWrite…
adamperlin Apr 2, 2026
832d585
Fix base relocation address bug;
adamperlin Apr 2, 2026
31308a9
Merge branch 'adamperlin/wasm-object-writer-runtime-relocs' of github…
adamperlin Apr 2, 2026
c67663e
Fix formatting
adamperlin Apr 2, 2026
08737e6
Address PR review feedback
adamperlin Apr 2, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions docs/design/mono/webcil.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,13 @@ struct WebcilHeader {
};
```

#### Webcil Header (V1 Changes)
For Webcil V1, the Reserved0 field may be used to store a 1-based index which corresponds to a
base reloc section.
```
uint16_t Reserved0; // 0, or 1-based index of .reloc webcil section
Comment thread
adamperlin marked this conversation as resolved.
```

The Webcil header starts with the magic characters 'W' 'b' 'I' 'L' followed by the version in major
minor format (must be 0 and 0). Then a count of the section headers and two reserved bytes.

Expand Down Expand Up @@ -184,3 +191,18 @@ Lossless conversion from Webcil back to PE is not intended to be supported. The
documented in order to support diagnostic tooling and utilities such as decompilers, disassemblers,
file identification utilities, dependency analyzers, etc.


### Webcil V1 (Base Relocations)
It is possible to specify base relocations in the standard PE base relocation format in Webcil V1.
Relocations are grouped by page (default 4kb), and each relocation entry is a `uint16_t` containing:

- Relocation Type (upper 4 bits)
- IMAGE_REL_BASED_DIR64 (wasm64)
- IMAGE_REL_BASED_HIGHLOW (wasm32)
- IMAGE_REL_BASED_WASM32_TABLE (wasm32)
- IMAGE_REL_BASED_WASM64_TABLE (wasm64)
- Offset (lower 12 bits)

`IMAGE_REL_BASED_WASM{32, 64}_TABLE` relocations represent a "table base offset" fixup; They should be used to indicate places
where function pointer table indices need to be offset after the Webcil payload has been loaded by the runtime. The offset will
be dependent on the state of the table when an implementation's loader loads a Webcil module.
8 changes: 5 additions & 3 deletions src/coreclr/inc/webcildecoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ struct WebcilHeader
uint16_t VersionMajor; // 0
uint16_t VersionMinor; // 0
uint16_t CoffSections;
uint16_t Reserved0;
uint16_t Reserved0; // 1-based index of the base relocations section, or 0 if none
uint32_t PeCliHeaderRva;
uint32_t PeCliHeaderSize;
uint32_t PeDebugRva;
Expand Down Expand Up @@ -137,7 +137,8 @@ class WebcilDecoder

// Webcil is always flat, never mapped in the PE sense
BOOL IsMapped() const { return FALSE; }
BOOL IsRelocated() const { return FALSE; }
BOOL IsRelocated() const { return m_relocated; }
void SetRelocated() { m_relocated = TRUE; }
BOOL IsFlat() const { return HasContents(); }
Comment thread
adamperlin marked this conversation as resolved.

// ------------------------------------------------------------
Expand All @@ -151,7 +152,7 @@ class WebcilDecoder
BOOL HasNTHeaders() const { return FALSE; }
CHECK CheckNTHeaders() const;
BOOL Has32BitNTHeaders() const { return FALSE; }
BOOL HasBaseRelocations() const { return FALSE; }
BOOL HasBaseRelocations() const;
BOOL HasWriteableSections() const { return FALSE; }
BOOL HasTls() const { return FALSE; }

Expand Down Expand Up @@ -258,6 +259,7 @@ class WebcilDecoder
TADDR m_base;
COUNT_T m_size;
BOOL m_hasContents;
BOOL m_relocated = FALSE;
const WebcilHeader *m_pHeader;
mutable IMAGE_COR20_HEADER *m_pCorHeader;
};
Expand Down
9 changes: 9 additions & 0 deletions src/coreclr/pal/inc/rt/ntimage.h
Original file line number Diff line number Diff line change
Expand Up @@ -1163,6 +1163,15 @@ typedef IMAGE_BASE_RELOCATION UNALIGNED * PIMAGE_BASE_RELOCATION;
#define IMAGE_REL_BASED_ARM_MOV32 5
#define IMAGE_REL_BASED_THUMB_MOV32 7

#ifdef FEATURE_WEBCIL
//
// Webcil-specific based relocation types.
// The fixup adds a WASM table base offset to the value at the relocation address.
//
#define IMAGE_REL_BASED_WASM32_TABLE 12
#define IMAGE_REL_BASED_WASM64_TABLE 13
#endif // FEATURE_WEBCIL

//
// Archive format.
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ public enum RelocType
// COFF relocation types
IMAGE_REL_BASED_ADDR32NB = 0x0B, // The 32-bit address without an image base (RVA)

// Webcil Relocation Types
IMAGE_REL_BASED_WASM32_TABLE = 0x0C,
IMAGE_REL_BASED_WASM64_TABLE = 0x0D,

// General relocation types
IMAGE_REL_BASED_REL32 = 0x10, // 32-bit relative address from byte following reloc
IMAGE_REL_BASED_THUMB_BRANCH24 = 0x13, // Thumb2: based B, BL
Expand Down Expand Up @@ -807,6 +811,12 @@ public static RelocType GetFileRelocationType(RelocType relocationType)
case RelocType.IMAGE_REL_BASED_THUMB_MOV32:
return relocationType;

case RelocType.WASM_TABLE_INDEX_I32:
return RelocType.IMAGE_REL_BASED_WASM32_TABLE;

case RelocType.WASM_TABLE_INDEX_I64:
return RelocType.IMAGE_REL_BASED_WASM64_TABLE;

default:
return RelocType.IMAGE_REL_BASED_ABSOLUTE;
}
Expand Down
Loading
Loading