authorgravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2023-03-14 19:51:30+01:00
committergravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2023-03-18 20:13:30+01:00
logff28c8b60080b24ae7d5e9d485b6aa47e8c8de9c
treefb8013cc833fe4946c5e159d00da0a8b33715b24
parent00af3a79aede098c60eeff38ad72fa399b9d3ccf
signaturelock-open Commit is signed but in an unrecognized format.

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).

1 files changed, 42 insertions(+), 2 deletions(-)

src/link/Wasm.zig+42-2
......@@ -443,6 +443,30 @@ pub fn openPath(allocator: Allocator, sub_path: []const u8, options: link.Option
443443 // at the end during `initializeCallCtorsFunction`.
444444 }
445445
446 // shared-memory symbols for TLS support
447 if (wasm_bin.base.options.shared_memory) {
448 {
449 const loc = try wasm_bin.createSyntheticSymbol("__tls_base", .global);
450 const symbol = loc.getSymbol(wasm_bin);
451 symbol.setFlag(.WASM_SYM_VISIBILITY_HIDDEN);
452 }
453 {
454 const loc = try wasm_bin.createSyntheticSymbol("__tls_size", .global);
455 const symbol = loc.getSymbol(wasm_bin);
456 symbol.setFlag(.WASM_SYM_VISIBILITY_HIDDEN);
457 }
458 {
459 const loc = try wasm_bin.createSyntheticSymbol("__tls_align", .global);
460 const symbol = loc.getSymbol(wasm_bin);
461 symbol.setFlag(.WASM_SYM_VISIBILITY_HIDDEN);
462 }
463 {
464 const loc = try wasm_bin.createSyntheticSymbol("__wasm_tls_init", .function);
465 const symbol = loc.getSymbol(wasm_bin);
466 symbol.setFlag(.WASM_SYM_VISIBILITY_HIDDEN);
467 }
468 }
469
446470 // if (!options.strip and options.module != null) {
447471 // wasm_bin.dwarf = Dwarf.init(allocator, &wasm_bin.base, options.target);
448472 // try wasm_bin.initDebugSections();
......@@ -2286,12 +2310,28 @@ fn setupMemory(wasm: *Wasm) !void {
22862310
22872311 // set TLS-related symbols
22882312 if (mem.eql(u8, entry.key_ptr.*, ".tdata")) {
2289 if (wasm.findGlobalSymbol("__tls_base")) |loc| {
2313 if (wasm.findGlobalSymbol("__tls_size")) |loc| {
2314 const sym = loc.getSymbol(wasm);
2315 sym.index = @intCast(u32, wasm.wasm_globals.items.len) + wasm.imported_globals_count;
2316 try wasm.wasm_globals.append(wasm.base.allocator, .{
2317 .global_type = .{ .valtype = .i32, .mutable = false },
2318 .init = .{ .i32_const = @intCast(i32, segment.size) },
2319 });
2320 }
2321 if (wasm.findGlobalSymbol("__tls_align")) |loc| {
22902322 const sym = loc.getSymbol(wasm);
22912323 sym.index = @intCast(u32, wasm.wasm_globals.items.len) + wasm.imported_globals_count;
22922324 try wasm.wasm_globals.append(wasm.base.allocator, .{
22932325 .global_type = .{ .valtype = .i32, .mutable = false },
2294 .init = .{ .i32_const = @intCast(i32, memory_ptr) },
2326 .init = .{ .i32_const = @intCast(i32, segment.alignment) },
2327 });
2328 }
2329 if (wasm.findGlobalSymbol("__tls_base")) |loc| {
2330 const sym = loc.getSymbol(wasm);
2331 sym.index = @intCast(u32, wasm.wasm_globals.items.len) + wasm.imported_globals_count;
2332 try wasm.wasm_globals.append(wasm.base.allocator, .{
2333 .global_type = .{ .valtype = .i32, .mutable = wasm.base.options.shared_memory },
2334 .init = .{ .i32_const = if (wasm.base.options.shared_memory) @as(u32, 0) else @intCast(i32, memory_ptr) },
22952335 });
22962336 }
22972337 }