Skip to content

Commit 21725c0

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 21725c0

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
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: 5 additions & 4 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

@@ -4610,7 +4611,7 @@ final class SwiftDriverTests: XCTestCase {
46104611
// in addition to the usual flag.
46114612
try withTemporaryDirectory { path in
46124613
let completePath: AbsolutePath = path.appending(component: "profile.profdata")
4613-
4614+
46144615
try localFileSystem.writeFileContents(completePath, bytes: .init())
46154616
var driver = try Driver(args: ["swiftc", "foo.swift",
46164617
"-working-directory", path.pathString,
@@ -7403,7 +7404,7 @@ final class SwiftDriverTests: XCTestCase {
74037404
XCTAssertEqual(try getLibraryLevel(flags), .spi)
74047405
}
74057406
try withTemporaryFile { file in
7406-
try localFileSystem.writeFileContents(file.path, bytes:
7407+
try localFileSystem.writeFileContents(file.path, bytes:
74077408
"// swift-module-flags: -target arm64e-apple-macos12.0"
74087409
)
74097410
let flags = try getAllModuleFlags(VirtualPath.absolute(file.path))
@@ -8219,7 +8220,7 @@ final class SwiftDriverTests: XCTestCase {
82198220
XCTAssertTrue(plannedJobs[0].commandLine.contains(.flag("-load-pass-plugin=/path/to/plugin")))
82208221
#endif
82218222
}
8222-
8223+
82238224
func testSupplementaryOutputFileMapUsage() throws {
82248225
// Ensure filenames are escaped properly when using a supplementary output file map
82258226
try withTemporaryDirectory { path in
@@ -8250,7 +8251,7 @@ final class SwiftDriverTests: XCTestCase {
82508251
struct D {}
82518252
"""
82528253
)
8253-
8254+
82548255
let sdkArgumentsForTesting = (try? Driver.sdkArgumentsForTesting()) ?? []
82558256
let invocationArguments = ["swiftc",
82568257
"-parse-as-library",

0 commit comments

Comments
 (0)