Skip to content

Commit 48fcebd

Browse files
Merge pull request #1539 from kateinoigakukun/yt/fix-wasm-global-base
2 parents f902c2a + 8de4f72 commit 48fcebd

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

Sources/SwiftDriver/Jobs/WebAssemblyToolchain+LinkerSupport.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,17 @@ extension WebAssemblyToolchain {
137137
// Explicitly pass the target to the linker
138138
commandLine.appendFlag("--target=\(targetTriple.triple)")
139139

140+
// WebAssembly doesn't reserve low addresses as its ABI. But without
141+
// "extra inhabitants" of the pointer representation, runtime performance
142+
// and memory footprint are significantly degraded. So we reserve the
143+
// low addresses to use them as extra inhabitants by telling the lowest
144+
// valid address to the linker.
145+
// The value of lowest valid address, called "global base", must be always
146+
// synchronized with `SWIFT_ABI_WASM32_LEAST_VALID_POINTER` defined in
147+
// apple/swift's runtime library.
148+
commandLine.appendFlag(.Xlinker)
149+
commandLine.appendFlag("--global-base=4096")
150+
140151
// Delegate to Clang for sanitizers. It will figure out the correct linker
141152
// options.
142153
guard sanitizers.isEmpty else {

Tests/SwiftDriverTests/SwiftDriverTests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2315,6 +2315,7 @@ final class SwiftDriverTests: XCTestCase {
23152315
XCTAssertTrue(commandContainsTemporaryPath(cmd, "bar.o"))
23162316
XCTAssertTrue(commandContainsTemporaryResponsePath(cmd, "Test.autolink"))
23172317
XCTAssertTrue(cmd.contains(.responseFilePath(.absolute(path.appending(components: "wasi", "static-executable-args.lnk")))))
2318+
XCTAssertTrue(cmd.contains(subsequence: [.flag("-Xlinker"), .flag("--global-base=4096")]))
23182319
XCTAssertTrue(cmd.contains(.flag("-O3")))
23192320
XCTAssertEqual(linkJob.outputs[0].file, try VirtualPath(path: "Test"))
23202321

0 commit comments

Comments
 (0)