From ff28c8b60080b24ae7d5e9d485b6aa47e8c8de9c Mon Sep 17 00:00:00 2001 From: Luuk de Gram Date: Tue, 14 Mar 2023 19:51:30 +0100 Subject: wasm-linker: create TLS symbols Initialize TLS symbols when shared-memory is enabled. Those symbols will be called by synthetic functions created by the linker. (TODO). --- src/link/Wasm.zig | 44 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/link/Wasm.zig b/src/link/Wasm.zig index 720e10fe03..d66facb912 100644 --- a/src/link/Wasm.zig +++ b/src/link/Wasm.zig @@ -443,6 +443,30 @@ pub fn openPath(allocator: Allocator, sub_path: []const u8, options: link.Option // at the end during `initializeCallCtorsFunction`. } + // shared-memory symbols for TLS support + if (wasm_bin.base.options.shared_memory) { + { + const loc = try wasm_bin.createSyntheticSymbol("__tls_base", .global); + const symbol = loc.getSymbol(wasm_bin); + symbol.setFlag(.WASM_SYM_VISIBILITY_HIDDEN); + } + { + const loc = try wasm_bin.createSyntheticSymbol("__tls_size", .global); + const symbol = loc.getSymbol(wasm_bin); + symbol.setFlag(.WASM_SYM_VISIBILITY_HIDDEN); + } + { + const loc = try wasm_bin.createSyntheticSymbol("__tls_align", .global); + const symbol = loc.getSymbol(wasm_bin); + symbol.setFlag(.WASM_SYM_VISIBILITY_HIDDEN); + } + { + const loc = try wasm_bin.createSyntheticSymbol("__wasm_tls_init", .function); + const symbol = loc.getSymbol(wasm_bin); + symbol.setFlag(.WASM_SYM_VISIBILITY_HIDDEN); + } + } + // if (!options.strip and options.module != null) { // wasm_bin.dwarf = Dwarf.init(allocator, &wasm_bin.base, options.target); // try wasm_bin.initDebugSections(); @@ -2286,12 +2310,28 @@ fn setupMemory(wasm: *Wasm) !void { // set TLS-related symbols if (mem.eql(u8, entry.key_ptr.*, ".tdata")) { - if (wasm.findGlobalSymbol("__tls_base")) |loc| { + if (wasm.findGlobalSymbol("__tls_size")) |loc| { + const sym = loc.getSymbol(wasm); + sym.index = @intCast(u32, wasm.wasm_globals.items.len) + wasm.imported_globals_count; + try wasm.wasm_globals.append(wasm.base.allocator, .{ + .global_type = .{ .valtype = .i32, .mutable = false }, + .init = .{ .i32_const = @intCast(i32, segment.size) }, + }); + } + if (wasm.findGlobalSymbol("__tls_align")) |loc| { const sym = loc.getSymbol(wasm); sym.index = @intCast(u32, wasm.wasm_globals.items.len) + wasm.imported_globals_count; try wasm.wasm_globals.append(wasm.base.allocator, .{ .global_type = .{ .valtype = .i32, .mutable = false }, - .init = .{ .i32_const = @intCast(i32, memory_ptr) }, + .init = .{ .i32_const = @intCast(i32, segment.alignment) }, + }); + } + if (wasm.findGlobalSymbol("__tls_base")) |loc| { + const sym = loc.getSymbol(wasm); + sym.index = @intCast(u32, wasm.wasm_globals.items.len) + wasm.imported_globals_count; + try wasm.wasm_globals.append(wasm.base.allocator, .{ + .global_type = .{ .valtype = .i32, .mutable = wasm.base.options.shared_memory }, + .init = .{ .i32_const = if (wasm.base.options.shared_memory) @as(u32, 0) else @intCast(i32, memory_ptr) }, }); } } -- cgit v1.2.3