Commit d9d49ce995

Andrew Kelley <andrew@ziglang.org>
2025-01-10 02:20:20
wasm linker: handle weak globals in relocs
1 parent 7a4d435
Changed files (2)
src
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)),
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).?);