8000 port(wasm): Apply code section index relocations by lapla-cogito · Pull Request #2058 · wild-linker/wild · GitHub
[go: up one dir, main page]

Skip to content

port(wasm): Apply code section index relocations - #2058

Merged
lapla-cogito merged 7 commits into
wild-linker:mainfrom
lapla-cogito:wasm_reloc
Jun 15, 2026
Merged

lapla-cogito merged 7 commits into
wild-linker:mainfrom
lapla-cogito:wasm_reloc

Conversation

@lapla-cogito
Copy link
Copy Markdown
Member

This patch enables the application of code index relocations (R_WASM_TYPE_INDEX_LEB, R_WASM_FUNCTION_INDEX_LEB, R_WASM_FUNCTION_INDEX_I32, R_WASM_GLOBAL_INDEX_LEB, R_WASM_GLOBAL_INDEX_I32). The byte-level encoding primitives (apply_relocation()) were already implemented in a prior patch and this patch adds the symbol-to-output-index resolution, per-body relocation grouping, and replaces the wasm_encoder::CodeSection builder with direct emission into the output buffer.

Part of #1431

@lapla-cogito
Copy link
Copy Markdown
Member Author

With more practical testing cases becoming available, I plan to begin establishing a basic testing infrastructure once this gets merged.

(▰╹◡╹)❯  cat reloc_test.wat

(module
  (type $t0 (func (param i32) (result i32)))
  (type $t1 (func (param i32 i32) (result i32)))

  (func $add (type $t1) (param $a i32) (param $b i32) (result i32)
    local.get $a
    local.get $b
    i32.add
  )

  (func $double (type $t0) (param $x i32) (result i32)
    local.get $x
    local.get $x
    call $add
  )

  (export "add" (func $add))
  (export "double" (func $double))
)

(▰╹◡╹)❯  wat2wasm reloc_test.wat -o reloc_test.wasm --relocatable

(▰╹◡╹)❯  wasm-objdump -rd reloc_test.wasm

reloc_test.wasm:        file format wasm 0x1

Code Disassembly:

000031 func[0] <add>:
 000032: 20 00                      | local.get 0
 000034: 20 01                      | local.get 1
 000036: 6a                         | i32.add
 000037: 0b                         | end
000039 func[1] <double>:
 00003a: 20 00                      | local.get 0
 00003c: 20 00                      | local.get 0
 00003e: 10 80 80 80 80 00          | call 0 <add>
           00003f: R_WASM_FUNCTION_INDEX_LEB 0 <add>
 000044: 0b                         | end

(▰╹◡╹)❯  ~/repos/wild/target/debug/wild -flavor wasm reloc_test.wasm -o reloc_test.wild.wasm

(▰╹◡╹)❯  wasmtime run --invoke add reloc_test.wild.wasm 3 4
warning: using `--invoke` with a function that takes arguments is experimental and may break in the future
warning: using `--invoke` with a function that returns values is experimental and may break in the future
7

(▰╹◡╹)❯  wasmtime run --invoke double reloc_test.wild.wasm 5
warning: using `--invoke` with a function that takes arguments is experimental and may break in the future
warning: using `--invoke` with a function that returns values is experimental and may break in the future
10

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

Nice! Looking forward to seeing some tests in a later PR :)

Comment thread libwild/src/wasm.rs Resolved
Comment thread libwild/src/wasm.rs Resolved
Comment thread libwild/src/wasm.rs Resolved
Comment thread libwild/src/wasm.rs Resolved
Comment thread libwild/src/wasm_writer.rs Resolved
Comment thread libwild/src/wasm_writer.rs Resolved
@lapla-cogito
lapla-cogito force-pushed the wasm_reloc branch 2 times, most recently from da0b940 to da6cefe Compare June 14, 2026 23:59
Comment thread libwild/src/wasm.rs
pub(crate) bytes: &'data [u8],
/// Byte offset of this body (starting at its size prefix) within the code section payload.
pub(crate) code_offset: u32,
pub(crate) relocations: Vec<ResolvedCodeReloc>,
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.

It looks to me like it might be possible to avoid this Vec and store just enough information to find the relocations later on. e.g. a &'data [WasmRelocation] or even just a u32 that represents the index of the first relocation for this function. But it's perfectly reasonable to leave this optimisation until later.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

If we do that, we'll have to propagate relocation value resolution to the writer side, which would require each object to access its own index map and symbols reference, so the current implementation should be kept I think. But of course we can revisit this if allocation overhead becomes a concern.

@lapla-cogito
lapla-cogito merged commit 8a01c40 into wild-linker:main Jun 15, 2026
21 checks passed
@lapla-cogito
lapla-cogito deleted the wasm_reloc branch June 15, 2026 03:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

0