wasm: correctly generate relocations for type index

Previously we could directly write the type index because we used the
index that was known in the final binary. However, as we now process
the Zig module as its own relocatable object file, we must ensure to
generate a relocation for type indexes. This also ensures that we can
later link the relocatable object file as a standalone also.

This also fixes generating indirect function table entries for ZigObject
as it now correctly points to the relocation symbol index rather than
the symbol index that owns the relocation.
This commit is contained in:
Luuk de Gram 2024-02-05 17:17:07 +01:00
parent 5a0f2af7e4
commit 5aec88fa41
No known key found for this signature in database
GPG Key ID: A8CFE58E4DC7D664
3 changed files with 24 additions and 4 deletions

View File

@ -385,7 +385,19 @@ fn emitCallIndirect(emit: *Emit, inst: Mir.Inst.Index) !void {
try emit.code.append(std.wasm.opcode(.call_indirect));
// NOTE: If we remove unused function types in the future for incremental
// linking, we must also emit a relocation for this `type_index`
try leb128.writeULEB128(emit.code.writer(), type_index);
const call_offset = emit.offset();
var buf: [5]u8 = undefined;
leb128.writeUnsignedFixed(5, &buf, type_index);
try emit.code.appendSlice(&buf);
if (type_index != 0) {
const atom_index = emit.bin_file.zigObjectPtr().?.decls_map.get(emit.decl_index).?.atom;
const atom = emit.bin_file.getAtomPtr(atom_index);
try atom.relocs.append(emit.bin_file.base.comp.gpa, .{
.offset = call_offset,
.index = type_index,
.relocation_type = .R_WASM_TYPE_INDEX_LEB,
});
}
try leb128.writeULEB128(emit.code.writer(), @as(u32, 0)); // TODO: Emit relocation for table index
}

View File

@ -3057,7 +3057,9 @@ fn writeToFile(
try leb.writeULEB128(binary_writer, @as(u32, @intCast(wasm.function_table.count())));
var symbol_it = wasm.function_table.keyIterator();
while (symbol_it.next()) |symbol_loc_ptr| {
const sym = symbol_loc_ptr.*.getSymbol(wasm);
const sym = symbol_loc_ptr.getSymbol(wasm);
std.debug.assert(sym.isAlive());
std.debug.assert(sym.index < wasm.functions.count() + wasm.imported_functions_count);
try leb.writeULEB128(binary_writer, sym.index);
}

View File

@ -1137,14 +1137,20 @@ pub fn parseSymbolIntoAtom(zig_object: *ZigObject, wasm_file: *Wasm, index: u32)
.R_WASM_TABLE_INDEX_SLEB,
.R_WASM_TABLE_INDEX_SLEB64,
=> {
try wasm_file.function_table.put(gpa, loc, 0);
try wasm_file.function_table.put(gpa, .{
.file = zig_object.index,
.index = reloc.index,
}, 0);
},
.R_WASM_GLOBAL_INDEX_I32,
.R_WASM_GLOBAL_INDEX_LEB,
=> {
const sym = zig_object.symbol(reloc.index);
if (sym.tag != .global) {
try wasm_file.got_symbols.append(gpa, loc);
try wasm_file.got_symbols.append(gpa, .{
.file = zig_object.index,
.index = reloc.index,
});
}
},
else => {},