diff options
| author | Luuk de Gram <luuk@degram.dev> | 2023-03-14 19:51:30 +0100 |
|---|---|---|
| committer | Luuk de Gram <luuk@degram.dev> | 2023-03-18 20:13:30 +0100 |
| commit | ff28c8b60080b24ae7d5e9d485b6aa47e8c8de9c (patch) | |
| tree | fb8013cc833fe4946c5e159d00da0a8b33715b24 /src | |
| parent | 00af3a79aede098c60eeff38ad72fa399b9d3ccf (diff) | |
| download | zig-ff28c8b60080b24ae7d5e9d485b6aa47e8c8de9c.tar.gz zig-ff28c8b60080b24ae7d5e9d485b6aa47e8c8de9c.zip | |
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).
Diffstat (limited to 'src')
| -rw-r--r-- | src/link/Wasm.zig | 44 |
1 files changed, 42 insertions, 2 deletions
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) }, }); } } |
