From 908b7b58c3ea31a62287def1ed10a551ce1d5457 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Mon, 15 Sep 2025 06:20:45 +0000 Subject: [PATCH 1/2] [NFC][wasm] Fix incorrect comment about reserved low addresses --- lib/Driver/WebAssemblyToolChains.cpp | 4 ++-- stdlib/public/SwiftShims/swift/shims/System.h | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/Driver/WebAssemblyToolChains.cpp b/lib/Driver/WebAssemblyToolChains.cpp index eb257f1ddbffe..5872ede6cda0b 100644 --- a/lib/Driver/WebAssemblyToolChains.cpp +++ b/lib/Driver/WebAssemblyToolChains.cpp @@ -188,8 +188,8 @@ toolchains::WebAssembly::constructInvocation(const DynamicLinkJobAction &job, // WebAssembly doesn't reserve low addresses But without "extra inhabitants" // of the pointer representation, runtime performance and memory footprint are - // worse. So assume that compiler driver uses wasm-ld and --global-base=1024 - // to reserve low 1KB. + // worse. So assume that compiler driver uses wasm-ld and --global-base=4096 + // to reserve low 4KB. Arguments.push_back("-Xlinker"); Arguments.push_back(context.Args.MakeArgString( Twine("--global-base=") + diff --git a/stdlib/public/SwiftShims/swift/shims/System.h b/stdlib/public/SwiftShims/swift/shims/System.h index 53afedf3eff97..328f81ebece67 100644 --- a/stdlib/public/SwiftShims/swift/shims/System.h +++ b/stdlib/public/SwiftShims/swift/shims/System.h @@ -220,8 +220,8 @@ // WebAssembly doesn't reserve low addresses. But without "extra inhabitants" of // the pointer representation, runtime performance and memory footprint are -// worse. So assume that compiler driver uses wasm-ld and --global-base=1024 to -// reserve low 1KB. +// worse. So assume that compiler driver uses wasm-ld and --global-base=4096 to +// reserve low 4KB. #define SWIFT_ABI_WASM32_LEAST_VALID_POINTER 4096 #endif // SWIFT_STDLIB_SHIMS_ABI_SYSTEM_H From d8ae0124d4e39f522393539ee9a53a5562f64d2c Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Mon, 15 Sep 2025 06:22:20 +0000 Subject: [PATCH 2/2] [Legacy Driver][wasm] Pass `--table-base` to reserve low function addresses WebAssembly does not have a reserved address space by default, so we need to explicitly reserve low addresses for extra inhabitants for enum types with pointer payloads. https://github.com/swiftlang/swift/pull/39300 added `--global-base` to reserve low data addresses, but we also need to reserve low function addresses with `--table-base` for function pointers because WebAssembly uses a separate address space for function pointers. --- lib/Driver/WebAssemblyToolChains.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/Driver/WebAssemblyToolChains.cpp b/lib/Driver/WebAssemblyToolChains.cpp index 5872ede6cda0b..3d09649edd4c5 100644 --- a/lib/Driver/WebAssemblyToolChains.cpp +++ b/lib/Driver/WebAssemblyToolChains.cpp @@ -194,6 +194,10 @@ toolchains::WebAssembly::constructInvocation(const DynamicLinkJobAction &job, Arguments.push_back(context.Args.MakeArgString( Twine("--global-base=") + std::to_string(SWIFT_ABI_WASM32_LEAST_VALID_POINTER))); + Arguments.push_back("-Xlinker"); + Arguments.push_back(context.Args.MakeArgString( + Twine("--table-base=") + + std::to_string(SWIFT_ABI_WASM32_LEAST_VALID_POINTER))); // These custom arguments should be right before the object file at the end. context.Args.AddAllArgs(Arguments, options::OPT_linker_option_Group);