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"
@@ -196,6 +198,15 @@ toolchains::WebAssembly::constructInvocation(const DynamicLinkJobAction &job,
196198 Arguments.push_back (" -v" );
197199 }
198200
201+ // WebAssembly doesn't reserve low addresses But without "extra inhabitants"
202+ // of the pointer representation, runtime performance and memory footprint are
203+ // worse. So assume that compiler driver uses wasm-ld and --global-base=1024
204+ // to reserve low 1KB.
205+ Arguments.push_back (" -Xlinker" );
206+ Arguments.push_back (context.Args .MakeArgString (
207+ Twine (" --global-base=" ) +
208+ std::to_string (SWIFT_ABI_WASM32_LEAST_VALID_POINTER)));
209+
199210 // These custom arguments should be right before the object file at the end.
200211 context.Args .AddAllArgs (Arguments, options::OPT_linker_option_Group);
201212 context.Args .AddAllArgs (Arguments, options::OPT_Xlinker);
@@ -212,6 +223,23 @@ toolchains::WebAssembly::constructInvocation(const DynamicLinkJobAction &job,
212223 return II;
213224}
214225
226+ void validateLinkerArguments (DiagnosticEngine &diags,
227+ ArgStringList linkerArgs) {
228+ for (auto arg : linkerArgs) {
229+ if (StringRef (arg).startswith (" --global-base=" )) {
230+ diags.diagnose (SourceLoc (), diag::error_option_not_supported, arg,
231+ " wasm32" );
232+ }
233+ }
234+ }
235+ void toolchains::WebAssembly::validateArguments (DiagnosticEngine &diags,
236+ const llvm::opt::ArgList &args,
237+ StringRef defaultTarget) const {
238+ ArgStringList linkerArgs;
239+ args.AddAllArgValues (linkerArgs, options::OPT_Xlinker);
240+ validateLinkerArguments (diags, linkerArgs);
241+ }
242+
215243ToolChain::InvocationInfo
216244toolchains::WebAssembly::constructInvocation (const StaticLinkJobAction &job,
217245 const JobContext &context) const {
0 commit comments