Skip to content

Linking executable target failed when swift-tools-version >= 5.5 #3645

Closed
@kateinoigakukun

Description

@kateinoigakukun

With swift-wasm-5.5-SNAPSHOT-2021-10-02-a

$ swift package init --type executable
$ swift build --triple wasm32-unknown-wasi
wasm-ld: error: /Library/Developer/Toolchains/swift-wasm-5.5-SNAPSHOT-2021-10-02-a.xctoolchain/usr/share/wasi-sysroot/lib/wasm32-wasi/libc.a(__main_argc_argv.o): undefined symbol: main
clang-10: error: linker command failed with exit code 1 (use -v to see invocation)
<unknown>:0: error: link command failed with exit code 1 (use -v to see invocation)
[0/1] Linking tmp.i3oT7EM2.wasm

The latest SwiftPM rename main entry point name of executable target to avoid conflicting "main" with test target since swift-tools-version >= 5.5.
The main symbol is renamed to "{{module_name}}_main" and it's renamed again to be "main" when linking the executable target. The former renaming is done by Swift compiler, and the latter is done by linker, so SwiftPM passes some special linker flags for each platform.
But SwiftPM assumes that wasm-ld supports it by returning an empty array instead of nil even though wasm-ld doesn't support it yet. ​https://github.com/apple/swift-package-manager/blob/1be68e811d0d814ba7abbb8effee45f1e8e6ec0d/Sources/Build/BuildPlan.swift#L117-L126

Current workaround for this issue is to put an explicit main decl using @_cdecl like this:

// the original code is `print("Hello")`
@_cdecl("main")
func main(argc: Int32, argv: Int32) -> Int32 {
    print("Hello")
    return 0
}

For a short-term solution, we need to disable the renaming in the SwiftPM. But for a long-term solution, this renaming feature should be supported on wasm-ld, so we should send a patch for it into lld.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions