wasm linker: handle weak globals in relocs

This commit is contained in:
Andrew Kelley 2025-01-09 17:20:20 -08:00
parent 7a4d4357e8
commit d9d49ce995
2 changed files with 10 additions and 2 deletions

View File

@ -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).?);

View File

@ -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)),