Skip to content

Commit ed96649

Browse files
[wasm] Pass --table-base to linker 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. swiftlang/swift#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.
1 parent e4a9efa commit ed96649

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

Sources/SwiftDriver/Jobs/WebAssemblyToolchain+LinkerSupport.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,11 @@ extension WebAssemblyToolchain {
160160
// The value of lowest valid address, called "global base", must be always
161161
// synchronized with `SWIFT_ABI_WASM32_LEAST_VALID_POINTER` defined in
162162
// apple/swift's runtime library.
163+
let SWIFT_ABI_WASM32_LEAST_VALID_POINTER = 4096
163164
commandLine.appendFlag(.Xlinker)
164-
commandLine.appendFlag("--global-base=4096")
165+
commandLine.appendFlag("--global-base=\(SWIFT_ABI_WASM32_LEAST_VALID_POINTER)")
166+
commandLine.appendFlag(.Xlinker)
167+
commandLine.appendFlag("--table-base=\(SWIFT_ABI_WASM32_LEAST_VALID_POINTER)")
165168

166169
// Delegate to Clang for sanitizers. It will figure out the correct linker
167170
// options.

Tests/SwiftDriverTests/SwiftDriverTests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2498,6 +2498,7 @@ final class SwiftDriverTests: XCTestCase {
24982498
XCTAssertTrue(commandContainsTemporaryResponsePath(cmd, "Test.autolink"))
24992499
XCTAssertTrue(cmd.contains(.responseFilePath(.absolute(path.appending(components: "wasi", "static-executable-args.lnk")))))
25002500
XCTAssertTrue(cmd.contains(subsequence: [.flag("-Xlinker"), .flag("--global-base=4096")]))
2501+
XCTAssertTrue(cmd.contains(subsequence: [.flag("-Xlinker"), .flag("--table-base=4096")]))
25012502
XCTAssertTrue(cmd.contains(.flag("-O3")))
25022503
XCTAssertEqual(linkJob.outputs[0].file, try toPath("Test"))
25032504

0 commit comments

Comments
 (0)