Skip to content

[Linux] (--build-system=swiftbuild): swift run fails to run executable #8416

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
1 task done
kcieplak opened this issue Mar 26, 2025 · 3 comments
Open
1 task done
Labels
bug swift build Changes impacting `swift build`

Comments

@kcieplak
Copy link
Contributor

kcieplak commented Mar 26, 2025

Is it reproducible with SwiftPM command-line tools: swift build, swift test, swift package etc?

  • Confirmed reproduction steps with SwiftPM CLI. The description text must include reproduction steps with either of command-line SwiftPM commands, swift build, swift test, swift package etc.

Description

On Linux when running a binary using 'swift run' if the environment does not have a the swift runtime libraries in the LD_LIBRARY_PATH, the executable will fail to run.

.build/x86_64-unknown-linux-gnu/Products/Debug-linux/test_package: error while loading shared libraries: libswiftCore.so: cannot open shared object file: No such file or directory

Expected behavior

built binary executes and prints greeting

Actual behavior

error while loading shared libraries: libswiftCore.so: cannot open shared object file: No such file or directory

Steps to reproduce

Remove any swift runtime libraries from LD_LIBRARY_PATH and /usr/lib/swift/

swift-package init --type executable
swift-run --build-system swiftbuild
@kcieplak kcieplak added the bug label Mar 26, 2025
@kcieplak
Copy link
Contributor Author

This issue occurs due to a difference in RUNPATH embedded in the binary.

--build-system native

/test-swiftpm/swift-package-manager/.build/x86_64-unknown-linux-gnu/debug/swift-build --build-system native
/test-swiftpm/swift-package-manager/.build/x86_64-unknown-linux-gnu/debug/swift-run --build-system native
[1/1] Planning build
Building for debugging...
[1/1] Write swift-version--1CEC03321F1812F6.txt
Build of product 'test_package' complete! (0.63s)
Hello, world!

root@5ff52252beab:/test-swiftpm/test_package# readelf -d .build/x86_64-unknown-linux-gnu/debug/test_package

Dynamic section at offset 0x5d70 contains 31 entries:
  Tag        Type                         Name/Value

 0x000000000000001d (RUNPATH)            Library runpath: [/test-swiftpm/swift_toolchain/swift-DEVELOPMENT-SNAPSHOT-2024-10-08-a-ubuntu20.04/usr/lib/swift/linux:$ORIGIN]

--build-system swiftbuild

/test-swiftpm/swift-package-manager/.build/x86_64-unknown-linux-gnu/debug/swift-build --build-system swiftbuild
root@5ff52252beab:/test-swiftpm/test_package# /test-swiftpm/swift-package-manager/.build/x86_64-unknown-linux-gnu/debug/swift-run --build-system swiftbuild
Building for debugging...

 0% [----------------------------------------------------------------------------------------------------------------------------------------------------------------------------]
warning: path("/test-swiftpm/test_package/.build/x86_64-unknown-linux-gnu/Intermediates.noindex/test_package.build/Debug-linux/test_package.build/Objects-normal/x86_64/test_package-master-em
82% [=============================================================================================================================================-------------------------------]
test_package_3A07B9D2CAF5B1F7_PackageProduct 4 / 5
100% [===========================================================================================================================================================================]


Build complete! (2.322615876 seconds)
/test-swiftpm/test_package/.build/x86_64-unknown-linux-gnu/Products/Debug-linux/test_package: error while loading shared libraries: libswiftCore.so: cannot open shared object file: No such file or directory

root@5ff52252beab:/test-swiftpm/test_package# readelf -d .build/x86_64-unknown-linux-gnu/Products/Debug-linux/test_package

Dynamic section at offset 0x5d70 contains 31 entries:
  Tag        Type                         Name/Value

 0x000000000000001d (RUNPATH)            Library runpath: [@executable_path/../lib]
 0x000000006ffffff0 (VERSYM)             0x764
 0x000000006ffffffe (VERNEED)            0x794
 0x000000006fffffff (VERNEEDNUM)         1
 0x0000000000000000 (NULL)               0x0

The reason for the difference is how the final link occurs.

--build-system native uses swiftc to do the link i.e.

/test-swiftpm/swift_toolchain/swift-DEVELOPMENT-SNAPSHOT-2024-10-08-a-ubuntu20.04/usr/bin/swiftc -module-name test_package -emit-dependencies -emit-module -emit-module-path /test-swiftpm/test_package/.build/x86_64-unknown-linux-gnu/debug/Modules/test_package.swiftmodule -output-file-map /test-swiftpm/test_package/.build/x86_64-unknown-linux-gnu/debug/test_package.build/output-file-map.json -incremental -c @/test-swiftpm/test_package/.build/x86_64-unknown-linux-gnu/debug/test_package.build/sources -I /test-swiftpm/test_package/.build/x86_64-unknown-linux-gnu/debug/Modules -target x86_64-unknown-linux-gnu -v -enable-batch-mode -index-store-path /test-swiftpm/test_package/.build/x86_64-unknown-linux-gnu/debug/index/store -Onone -enable-testing -j4 -DSWIFT_PACKAGE -DDEBUG -module-cache-path /test-swiftpm/test_package/.build/x86_64-unknown-linux-gnu/debug/ModuleCache -parseable-output -Xfrontend -entry-point-function-name -Xfrontend test_package_main -color-diagnostics -swift-version 6 -g -Xcc -fPIC -Xcc -g -package-name test_package -Xcc -fno-omit-frame-pointer
Swift version 6.1-dev (LLVM 95f3fb07f8f5294, Swift 8ae66ec1a522af4)
Target: x86_64-unknown-linux-gnu
/test-swiftpm/swift_toolchain/swift-DEVELOPMENT-SNAPSHOT-2024-10-08-a-ubuntu20.04/usr/bin/swift-frontend -frontend -emit-module -experimental-skip-non-inlinable-function-bodies-without-types /test-swiftpm/test_package/Sources/main.swift -target x86_64-unknown-linux-gnu -disable-objc-interop -I /test-swiftpm/test_package/.build/x86_64-unknown-linux-gnu/debug/Modules -color-diagnostics -enable-testing -g -debug-info-format=dwarf -dwarf-version=4 -module-cache-path /test-swiftpm/test_package/.build/x86_64-unknown-linux-gnu/debug/ModuleCache -swift-version 6 -Onone -D SWIFT_PACKAGE -D DEBUG -entry-point-function-name test_package_main -empty-abi-descriptor -Xcc -working-directory -Xcc /test-swiftpm/test_package -resource-dir /test-swiftpm/swift_toolchain/swift-DEVELOPMENT-SNAPSHOT-2024-10-08-a-ubuntu20.04/usr/lib/swift -enable-anonymous-context-mangled-names -file-compilation-dir /test-swiftpm/test_package -Xcc -fPIC -Xcc -g -Xcc -fno-omit-frame-pointer -module-name test_package -package-name test_package -in-process-plugin-server-path /test-swiftpm/swift_toolchain/swift-DEVELOPMENT-SNAPSHOT-2024-10-08-a-ubuntu20.04/usr/lib/swift/host/libSwiftInProcPluginServer.so -plugin-path /test-swiftpm/swift_toolchain/swift-DEVELOPMENT-SNAPSHOT-2024-10-08-a-ubuntu20.04/usr/lib/swift/host/plugins -plugin-path /test-swiftpm/swift_toolchain/swift-DEVELOPMENT-SNAPSHOT-2024-10-08-a-ubuntu20.04/usr/local/lib/swift/host/plugins -emit-module-doc-path /test-swiftpm/test_package/.build/x86_64-unknown-linux-gnu/debug/Modules/test_package.swiftdoc -emit-module-source-info-path /test-swiftpm/test_package/.build/x86_64-unknown-linux-gnu/debug/Modules/test_package.swiftsourceinfo -emit-dependencies-path /test-swiftpm/test_package/.build/x86_64-unknown-linux-gnu/debug/test_package.build/test_package.emit-module.d -o /test-swiftpm/test_package/.build/x86_64-unknown-linux-gnu/debug/Modules/test_package.swiftmodule
/test-swiftpm/swift_toolchain/swift-DEVELOPMENT-SNAPSHOT-2024-10-08-a-ubuntu20.04/usr/bin/swift-frontend -frontend -c -primary-file /test-swiftpm/test_package/Sources/main.swift -emit-dependencies-path /test-swiftpm/test_package/.build/x86_64-unknown-linux-gnu/debug/test_package.build/main.d -emit-reference-dependencies-path /test-swiftpm/test_package/.build/x86_64-unknown-linux-gnu/debug/test_package.build/main.swiftdeps -target x86_64-unknown-linux-gnu -disable-objc-interop -I /test-swiftpm/test_package/.build/x86_64-unknown-linux-gnu/debug/Modules -color-diagnostics -enable-testing -g -debug-info-format=dwarf -dwarf-version=4 -module-cache-path /test-swiftpm/test_package/.build/x86_64-unknown-linux-gnu/debug/ModuleCache -swift-version 6 -Onone -D SWIFT_PACKAGE -D DEBUG -entry-point-function-name test_package_main -empty-abi-descriptor -Xcc -working-directory -Xcc /test-swiftpm/test_package -resource-dir /test-swiftpm/swift_toolchain/swift-DEVELOPMENT-SNAPSHOT-2024-10-08-a-ubuntu20.04/usr/lib/swift -enable-anonymous-context-mangled-names -file-compilation-dir /test-swiftpm/test_package -Xcc -fPIC -Xcc -g -Xcc -fno-omit-frame-pointer -module-name test_package -package-name test_package -in-process-plugin-server-path /test-swiftpm/swift_toolchain/swift-DEVELOPMENT-SNAPSHOT-2024-10-08-a-ubuntu20.04/usr/lib/swift/host/libSwiftInProcPluginServer.so -plugin-path /test-swiftpm/swift_toolchain/swift-DEVELOPMENT-SNAPSHOT-2024-10-08-a-ubuntu20.04/usr/lib/swift/host/plugins -plugin-path /test-swiftpm/swift_toolchain/swift-DEVELOPMENT-SNAPSHOT-2024-10-08-a-ubuntu20.04/usr/local/lib/swift/host/plugins -o /test-swiftpm/test_package/.build/x86_64-unknown-linux-gnu/debug/test_package.build/main.swift.o -index-store-path /test-swiftpm/test_package/.build/x86_64-unknown-linux-gnu/debug/index/store -index-system-modules
/test-swiftpm/swift_toolchain/swift-DEVELOPMENT-SNAPSHOT-2024-10-08-a-ubuntu20.04/usr/bin/swift-frontend -modulewrap /test-swiftpm/test_package/.build/x86_64-unknown-linux-gnu/debug/Modules/test_package.swiftmodule -target x86_64-unknown-linux-gnu -o /test-swiftpm/test_package/.build/x86_64-unknown-linux-gnu/debug/Modules/test_package.o
/test-swiftpm/swift_toolchain/swift-DEVELOPMENT-SNAPSHOT-2024-10-08-a-ubuntu20.04/usr/bin/swiftc -modulewrap /test-swiftpm/test_package/.build/x86_64-unknown-linux-gnu/debug/Modules/test_package.swiftmodule -o /test-swiftpm/test_package/.build/x86_64-unknown-linux-gnu/debug/test_package.build/test_package.swiftmodule.o -target x86_64-unknown-linux-gnu
Write auxiliary file /test-swiftpm/test_package/.build/x86_64-unknown-linux-gnu/debug/test_package.product/Objects.LinkFileList
/test-swiftpm/swift_toolchain/swift-DEVELOPMENT-SNAPSHOT-2024-10-08-a-ubuntu20.04/usr/bin/swiftc -v -L /test-swiftpm/test_package/.build/x86_64-unknown-linux-gnu/debug -o /test-swiftpm/test_package/.build/x86_64-unknown-linux-gnu/debug/test_package -module-name test_package -emit-executable -Xlinker --defsym -Xlinker main=test_package_main -Xlinker '-rpath=$ORIGIN' @/test-swiftpm/test_package/.build/x86_64-unknown-linux-gnu/debug/test_package.product/Objects.LinkFileList -target x86_64-unknown-linux-gnu -g
Swift version 6.1-dev (LLVM 95f3fb07f8f5294, Swift 8ae66ec1a522af4)
Target: x86_64-unknown-linux-gnu
/test-swiftpm/swift_toolchain/swift-DEVELOPMENT-SNAPSHOT-2024-10-08-a-ubuntu20.04/usr/bin/swift-autolink-extract /test-swiftpm/test_package/.build/x86_64-unknown-linux-gnu/debug/test_package.build/main.swift.o /test-swiftpm/test_package/.build/x86_64-unknown-linux-gnu/debug/test_package.build/test_package.swiftmodule.o -o /test-swiftpm/test_package/.build/x86_64-unknown-linux-gnu/debug/test_package.build/test_package.autolink
/test-swiftpm/swift_toolchain/swift-DEVELOPMENT-SNAPSHOT-2024-10-08-a-ubuntu20.04/usr/bin/clang -pie -Xlinker --build-id -Xlinker -rpath -Xlinker /test-swiftpm/swift_toolchain/swift-DEVELOPMENT-SNAPSHOT-2024-10-08-a-ubuntu20.04/usr/lib/swift/linux /test-swiftpm/swift_toolchain/swift-DEVELOPMENT-SNAPSHOT-2024-10-08-a-ubuntu20.04/usr/lib/swift/linux/x86_64/swiftrt.o /test-swiftpm/test_package/.build/x86_64-unknown-linux-gnu/debug/test_package.build/main.swift.o /test-swiftpm/test_package/.build/x86_64-unknown-linux-gnu/debug/test_package.build/test_package.swiftmodule.o @/test-swiftpm/test_package/.build/x86_64-unknown-linux-gnu/debug/test_package.build/test_package.autolink -L /test-swiftpm/swift_toolchain/swift-DEVELOPMENT-SNAPSHOT-2024-10-08-a-ubuntu20.04/usr/lib/swift/linux -lswiftCore --target=x86_64-unknown-linux-gnu -v -L /test-swiftpm/test_package/.build/x86_64-unknown-linux-gnu/debug -L /test-swiftpm/test_package/.build/x86_64-unknown-linux-gnu/debug -Xlinker --defsym -Xlinker main=test_package_main -Xlinker '-rpath=$ORIGIN' -o /test-swiftpm/test_package/.build/x86_64-unknown-linux-gnu/debug/test_package
clang version 17.0.0 (https://github.com/swiftlang/llvm-project.git 95f3fb07f8f52940912fdb0ba6f58fd0fe0f2511)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /test-swiftpm/swift_toolchain/swift-DEVELOPMENT-SNAPSHOT-2024-10-08-a-ubuntu20.04/usr/bin
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/9
Selected GCC installation: /usr/lib/gcc/x86_64-linux-gnu/9
Candidate multilib: .;@m64
Selected multilib: .;@m64
 "/usr/bin/ld.gold" -pie -z relro --hash-style=gnu --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o /test-swiftpm/test_package/.build/x86_64-unknown-linux-gnu/debug/test_package /lib/x86_64-linux-gnu/Scrt1.o /lib/x86_64-linux-gnu/crti.o /usr/lib/gcc/x86_64-linux-gnu/9/crtbeginS.o -L/test-swiftpm/swift_toolchain/swift-DEVELOPMENT-SNAPSHOT-2024-10-08-a-ubuntu20.04/usr/lib/swift/linux -L/test-swiftpm/test_package/.build/x86_64-unknown-linux-gnu/debug -L/test-swiftpm/test_package/.build/x86_64-unknown-linux-gnu/debug -L/usr/lib/gcc/x86_64-linux-gnu/9 -L/usr/lib/gcc/x86_64-linux-gnu/9/../../../../lib64 -L/lib/x86_64-linux-gnu -L/lib/../lib64 -L/usr/lib/x86_64-linux-gnu -L/usr/lib/../lib64 -L/lib -L/usr/lib --build-id -rpath /test-swiftpm/swift_toolchain/swift-DEVELOPMENT-SNAPSHOT-2024-10-08-a-ubuntu20.04/usr/lib/swift/linux /test-swiftpm/swift_toolchain/swift-DEVELOPMENT-SNAPSHOT-2024-10-08-a-ubuntu20.04/usr/lib/swift/linux/x86_64/swiftrt.o /test-swiftpm/test_package/.build/x86_64-unknown-linux-gnu/debug/test_package.build/main.swift.o /test-swiftpm/test_package/.build/x86_64-unknown-linux-gnu/debug/test_package.build/test_package.swiftmodule.o -lswiftSwiftOnoneSupport -lswiftCore -lswift_Concurrency -lswift_StringProcessing -lswift_RegexParser -lswiftCore --defsym main=test_package_main "-rpath=\$ORIGIN" -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc/x86_64-linux-gnu/9/crtendS.o /lib/x86_64-linux-gnu/crtn.o
Build complete! (5.82s)

swiftc adds the rpath to the swift runtime within the SDK.

--build-system swiftbuild calls clang to do the final link which does not add the RUNPATH to the swift runtime.

    /test-swiftpm/swift_toolchain/swift-DEVELOPMENT-SNAPSHOT-2024-10-08-a-ubuntu20.04/usr/bin/clang -target x86_64-unknown-linux-gnu --sysroot / -O0 -L/test-swiftpm/test_package/.build/x86_64-unknown-linux-gnu/Intermediates.noindex/EagerLinkingTBDs/Debug-linux -L/test-swiftpm/test_package/.build/x86_64-unknown-linux-gnu/Products/Debug-linux -L/test-swiftpm/swift_toolchain/swift-DEVELOPMENT-SNAPSHOT-2024-10-08-a-ubuntu20.04/usr/lib @/test-swiftpm/test_package/.build/x86_64-unknown-linux-gnu/Intermediates.noindex/test_package.build/Debug-linux/test_package.build/Objects-normal/x86_64/test_package.LinkFileList -Xlinker -rpath -Xlinker @executable_path/../lib -rdynamic -lswiftCore -lswiftSwiftOnoneSupport -L/test-swiftpm/swift_toolchain/swift-DEVELOPMENT-SNAPSHOT-2024-10-08-a-ubuntu20.04/usr/lib/swift/linux -L/usr/lib/swift @/test-swiftpm/test_package/.build/x86_64-unknown-linux-gnu/Intermediates.noindex/test_package.build/Debug-linux/test_package.build/Objects-normal/x86_64/test_package.autolink -v -o /test-swiftpm/test_package/.build/x86_64-unknown-linux-gnu/Products/Debug-linux/test_package
info: clang version 17.0.0 (https://github.com/swiftlang/llvm-project.git 95f3fb07f8f52940912fdb0ba6f58fd0fe0f2511)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /test-swiftpm/swift_toolchain/swift-DEVELOPMENT-SNAPSHOT-2024-10-08-a-ubuntu20.04/usr/bin
Found candidate GCC installation: /lib/gcc/x86_64-linux-gnu/9
Selected GCC installation: /lib/gcc/x86_64-linux-gnu/9
Candidate multilib: .;@m64
Selected multilib: .;@m64
 "/usr/bin/ld.gold" --sysroot=/ -pie -export-dynamic -z relro --hash-style=gnu --eh-frame-hdr -m elf_x86_64 -export-dynamic -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o /test-swiftpm/test_package/.build/x86_64-unknown-linux-gnu/Products/Debug-linux/test_package /lib/x86_64-linux-gnu/Scrt1.o /lib/x86_64-linux-gnu/crti.o /lib/gcc/x86_64-linux-gnu/9/crtbeginS.o -L/test-swiftpm/test_package/.build/x86_64-unknown-linux-gnu/Intermediates.noindex/EagerLinkingTBDs/Debug-linux -L/test-swiftpm/test_package/.build/x86_64-unknown-linux-gnu/Products/Debug-linux -L/test-swiftpm/swift_toolchain/swift-DEVELOPMENT-SNAPSHOT-2024-10-08-a-ubuntu20.04/usr/lib -L/test-swiftpm/swift_toolchain/swift-DEVELOPMENT-SNAPSHOT-2024-10-08-a-ubuntu20.04/usr/lib/swift/linux -L/usr/lib/swift -L/lib/gcc/x86_64-linux-gnu/9 -L/lib/gcc/x86_64-linux-gnu/9/../../../../lib64 -L/lib/x86_64-linux-gnu -L/lib/../lib64 -L/usr/lib/x86_64-linux-gnu -L/usr/lib/../lib64 -L/lib -L/usr/lib /test-swiftpm/test_package/.build/x86_64-unknown-linux-gnu/Intermediates.noindex/test_package.build/Debug-linux/test_package.build/Objects-normal/x86_64/Modules/test_package.o /test-swiftpm/test_package/.build/x86_64-unknown-linux-gnu/Intermediates.noindex/test_package.build/Debug-linux/test_package.build/Objects-normal/x86_64/main.o -rpath @executable_path/../lib -lswiftCore -lswiftSwiftOnoneSupport -lswiftSwiftOnoneSupport -lswiftCore -lswift_Concurrency -lswift_StringProcessing -lswift_RegexParser -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /lib/gcc/x86_64-linux-gnu/9/crtendS.o /lib/x86_64-linux-gnu/crtn.o

@kcieplak kcieplak changed the title [Linux] swift run using --buildsystem swiftbuild fails to run executable [Linux] swift run using --build-system swiftbuild fails to run executable Mar 26, 2025
@cmcgee1024 cmcgee1024 added the swift build Changes impacting `swift build` label Mar 28, 2025
@kcieplak kcieplak changed the title [Linux] swift run using --build-system swiftbuild fails to run executable [Linux] (--build-system=swiftbuild): swift run fails to run executable Apr 2, 2025
@cmcgee1024
Copy link
Member

Running XCTests also run into this problem now that the missing main symbol has been fixed: swiftlang/swift-build#390

@cmcgee1024
Copy link
Member

cmcgee1024 commented Apr 10, 2025

A potential solution to this is to use swiftc as the linker driver instead of clang. See #8477 for the setting that changes it.

See swiftlang/swift-build#395 for the error that occurs when swiftc is used as the linker driver.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug swift build Changes impacting `swift build`
Projects
None yet
Development

No branches or pull requests

2 participants