authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-01-09 17:20:20-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-01-15 15:11:36-08:00
logd9d49ce995c555286ba4f6041bcca12477f37296
tree69ba962cff8a211ad5e7de3b4919dc9dd6d5d8db
parent7a4d4357e85ddd26c03a49e9bbff7da688f96ca7

wasm linker: handle weak globals in relocs


2 files changed, 10 insertions(+), 2 deletions(-)

src/link/Wasm.zig+8
...@@ -441,6 +441,14 @@ pub const GlobalIndex = enum(u32) {...@@ -441,6 +441,14 @@ pub const GlobalIndex = enum(u32) {
441 return @enumFromInt(wasm.globals.getIndex(.fromObjectGlobal(wasm, i)).?);441 return @enumFromInt(wasm.globals.getIndex(.fromObjectGlobal(wasm, i)).?);
442 }442 }
443443
444 pub fn fromObjectGlobalHandlingWeak(wasm: *const Wasm, index: ObjectGlobalIndex) GlobalIndex {
445 const global = index.ptr(wasm);
446 return if (global.flags.binding == .weak)
447 fromSymbolName(wasm, global.name.unwrap().?)
448 else
449 fromObjectGlobal(wasm, index);
450 }
451
444 pub fn fromSymbolName(wasm: *const Wasm, name: String) GlobalIndex {452 pub fn fromSymbolName(wasm: *const Wasm, name: String) GlobalIndex {
445 const import = wasm.object_global_imports.getPtr(name).?;453 const import = wasm.object_global_imports.getPtr(name).?;
446 return @enumFromInt(wasm.globals.getIndex(import.resolution).?);454 return @enumFromInt(wasm.globals.getIndex(import.resolution).?);
src/link/Wasm/Flush.zig+2-2
...@@ -1477,8 +1477,8 @@ fn applyRelocs(code: []u8, code_offset: u32, relocs: Wasm.ObjectRelocation.Itera...@@ -1477,8 +1477,8 @@ fn applyRelocs(code: []u8, code_offset: u32, relocs: Wasm.ObjectRelocation.Itera
1477 .table_import_index_sleb => @panic("TODO indirect function table needs to support object functions too"),1477 .table_import_index_sleb => @panic("TODO indirect function table needs to support object functions too"),
1478 .table_import_index_sleb64 => @panic("TODO indirect function table needs to support object functions too"),1478 .table_import_index_sleb64 => @panic("TODO indirect function table needs to support object functions too"),
14791479
1480 .global_index_i32 => reloc_u32_global(sliced_code, .fromObjectGlobal(wasm, pointee.global)),1480 .global_index_i32 => reloc_u32_global(sliced_code, .fromObjectGlobalHandlingWeak(wasm, pointee.global)),
1481 .global_index_leb => reloc_leb_global(sliced_code, .fromObjectGlobal(wasm, pointee.global)),1481 .global_index_leb => reloc_leb_global(sliced_code, .fromObjectGlobalHandlingWeak(wasm, pointee.global)),
14821482
1483 .global_import_index_i32 => reloc_u32_global(sliced_code, .fromSymbolName(wasm, pointee.symbol_name)),1483 .global_import_index_i32 => reloc_u32_global(sliced_code, .fromSymbolName(wasm, pointee.symbol_name)),
1484 .global_import_index_leb => reloc_leb_global(sliced_code, .fromSymbolName(wasm, pointee.symbol_name)),1484 .global_import_index_leb => reloc_leb_global(sliced_code, .fromSymbolName(wasm, pointee.symbol_name)),