Description
Summary
This R_WASM_MEMORY_ADDR_SELFREL_I32
relocation represents an offset between its relocating address and the symbol address. It's very similar to R_X86_64_PC32
but restricted to be used for only data segments.
S + A - P
A: Represents the addend used to compute the value of the relocatable field.
P: Represents the place of the storage unit being relocated.
S: Represents the value of the symbol whose index resides in the relocation entry.
Motivation
Currently, Swift uses R_X86_64_PC32
relocation in data sections on ELF. The relocation is used to express relative pointer which is used in Swift’s metadata. The relative pointer is a pointer that has a 32 bit offset from its own address to the referent’s address. This technique reduces the pointer size 64 bit to 32 bit on a 64bit addressing mode, and also reduces the load-time relocation on general environments.
Wasm doesn’t have a relocation type like R_X86_64_PC32
and doesn’t have any benefit of using the relative pointer on wasm32 because it doesn’t reduce pointer size and also Wasm doesn’t have load-time relocations. So we, SwiftWasm project, use 32-bit absolute pointers instead of relative pointers now.
However, using absolute pointers instead of relative pointers breaks the metadata layout on wasm64 due to the pointer size. If we can use relative pointers as same as other architectures, we can support wasm64 without changing the data structure. The same data structure will make it simple to debug and implement the compiler pipeline.
So we propose a new relocation type for data segments to use relative pointers.
What do you think about this feature? @sunfishcode @sbc100