commit d9d49ce995c555286ba4f6041bcca12477f37296 (tree)
parent 7a4d4357e85ddd26c03a49e9bbff7da688f96ca7
Author: Andrew Kelley <andrew@ziglang.org>
Date: Thu, 9 Jan 2025 17:20:20 -0800
wasm linker: handle weak globals in relocs
Diffstat:
2 files changed, 10 insertions(+), 2 deletions(-)
diff --git a/src/link/Wasm.zig b/src/link/Wasm.zig
@@ -441,6 +441,14 @@ pub const GlobalIndex = enum(u32) {
return @enumFromInt(wasm.globals.getIndex(.fromObjectGlobal(wasm, i)).?);
}
+ pub fn fromObjectGlobalHandlingWeak(wasm: *const Wasm, index: ObjectGlobalIndex) GlobalIndex {
+ const global = index.ptr(wasm);
+ return if (global.flags.binding == .weak)
+ fromSymbolName(wasm, global.name.unwrap().?)
+ else
+ fromObjectGlobal(wasm, index);
+ }
+
pub fn fromSymbolName(wasm: *const Wasm, name: String) GlobalIndex {
const import = wasm.object_global_imports.getPtr(name).?;
return @enumFromInt(wasm.globals.getIndex(import.resolution).?);
diff --git a/src/link/Wasm/Flush.zig b/src/link/Wasm/Flush.zig
@@ -1477,8 +1477,8 @@ fn applyRelocs(code: []u8, code_offset: u32, relocs: Wasm.ObjectRelocation.Itera
.table_import_index_sleb => @panic("TODO indirect function table needs to support object functions too"),
.table_import_index_sleb64 => @panic("TODO indirect function table needs to support object functions too"),
- .global_index_i32 => reloc_u32_global(sliced_code, .fromObjectGlobal(wasm, pointee.global)),
- .global_index_leb => reloc_leb_global(sliced_code, .fromObjectGlobal(wasm, pointee.global)),
+ .global_index_i32 => reloc_u32_global(sliced_code, .fromObjectGlobalHandlingWeak(wasm, pointee.global)),
+ .global_index_leb => reloc_leb_global(sliced_code, .fromObjectGlobalHandlingWeak(wasm, pointee.global)),
.global_import_index_i32 => reloc_u32_global(sliced_code, .fromSymbolName(wasm, pointee.symbol_name)),
.global_import_index_leb => reloc_leb_global(sliced_code, .fromSymbolName(wasm, pointee.symbol_name)),