1212
1313#include " ToolChains.h"
1414
15+ #include " swift/ABI/System.h"
16+ #include " swift/AST/DiagnosticsDriver.h"
1517#include " swift/Basic/Dwarf.h"
1618#include " swift/Basic/LLVM.h"
1719#include " swift/Basic/Platform.h"
@@ -192,6 +194,15 @@ toolchains::WebAssembly::constructInvocation(const DynamicLinkJobAction &job,
192194 Arguments.push_back (" -v" );
193195 }
194196
197+ // WebAssembly doesn't reserve low addresses But without "extra inhabitants"
198+ // of the pointer representation, runtime performance and memory footprint are
199+ // worse. So assume that compiler driver uses wasm-ld and --global-base=1024
200+ // to reserve low 1KB.
201+ Arguments.push_back (" -Xlinker" );
202+ Arguments.push_back (context.Args .MakeArgString (
203+ Twine (" --global-base=" ) +
204+ std::to_string (SWIFT_ABI_WASM32_LEAST_VALID_POINTER)));
205+
195206 // These custom arguments should be right before the object file at the end.
196207 context.Args .AddAllArgs (Arguments, options::OPT_linker_option_Group);
197208 context.Args .AddAllArgs (Arguments, options::OPT_Xlinker);
@@ -208,6 +219,23 @@ toolchains::WebAssembly::constructInvocation(const DynamicLinkJobAction &job,
208219 return II;
209220}
210221
222+ void validateLinkerArguments (DiagnosticEngine &diags,
223+ ArgStringList linkerArgs) {
224+ for (auto arg : linkerArgs) {
225+ if (StringRef (arg).startswith (" --global-base=" )) {
226+ diags.diagnose (SourceLoc (), diag::error_option_not_supported, arg,
227+ " wasm32" );
228+ }
229+ }
230+ }
231+ void toolchains::WebAssembly::validateArguments (DiagnosticEngine &diags,
232+ const llvm::opt::ArgList &args,
233+ StringRef defaultTarget) const {
234+ ArgStringList linkerArgs;
235+ args.AddAllArgValues (linkerArgs, options::OPT_Xlinker);
236+ validateLinkerArguments (diags, linkerArgs);
237+ }
238+
211239ToolChain::InvocationInfo
212240toolchains::WebAssembly::constructInvocation (const StaticLinkJobAction &job,
213241 const JobContext &context) const {
0 commit comments