Commit f3626eb816

Luuk de Gram <luuk@degram.dev>
2023-11-06 17:09:42
wasm-link: ensure TLS global when resolved
When a linked object contains references to the __tls_base symbol, we lazily create this symbol. However, we wouldn't create the corresponding Wasm global. This meant its address wasn't set correctly as well as fail to output it into the `Names` section.
1 parent 2a32264
Changed files (1)
src
src/link/Wasm.zig
@@ -1242,6 +1242,14 @@ fn resolveLazySymbols(wasm: *Wasm) !void {
             if (wasm.undefs.fetchSwapRemove(name_offset)) |kv| {
                 const loc = try wasm.createSyntheticSymbolOffset(name_offset, .global);
                 try wasm.discarded.putNoClobber(wasm.base.allocator, kv.value, loc);
+                _ = wasm.resolved_symbols.swapRemove(kv.value);
+                const symbol = loc.getSymbol(wasm);
+                symbol.setFlag(.WASM_SYM_VISIBILITY_HIDDEN);
+                symbol.index = @intCast(wasm.imported_globals_count + wasm.wasm_globals.items.len);
+                try wasm.wasm_globals.append(wasm.base.allocator, .{
+                    .global_type = .{ .valtype = .i32, .mutable = true },
+                    .init = .{ .i32_const = undefined },
+                });
             }
         }
     }