Summary
I'm trying to build the Swift compiler from source on ARM64 Linux (Ubuntu 22.04 running in OrbStack on Apple Silicon). The build fails when linking libSwiftSyntax.so and lib_CompilerSwiftSyntax.so with PIC-related relocation errors. I've tested with both Swift 6.0.3 and Swift 6.2.3 as the host toolchain, and both fail with the same error.
Environment
- OS: Ubuntu 22.04.5 LTS (aarch64)
- Kernel: 6.17.8-orbstack (OrbStack VM on Apple Silicon Mac)
- Host Swift: 6.2.3 (installed via swiftly), also tested with 6.0.3
- Linker: GNU ld 2.38
- Swift source:
release/6.2branch (commit 554156bccc696a7d7cfc044c82a5914a9fc643c7)
Build Command
utils/build-script --release-debuginfo --sccache --bootstrapping=hosttools --reconfigure
Error
When linking SwiftSyntax shared libraries, the build fails with:
FAILED: lib/swift/host/compiler/lib_CompilerSwiftSyntax.so
/usr/bin/ld: _deps/compilerswiftsyntax-build/Sources/SwiftSyntax/CMakeFiles/_CompilerSwiftSyntax.dir/AbsolutePosition.swift.o:
relocation R_AARCH64_ADR_PREL_PG_HI21 against symbol `$s20_CompilerSwiftSyntax16AbsolutePositionVSLAAMc'
which may bind externally can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: _deps/compilerswiftsyntax-build/Sources/SwiftSyntax/CMakeFiles/_CompilerSwiftSyntax.dir/AbsolutePosition.swift.o:
relocation R_AARCH64_PREL32 against symbol `$s20_CompilerSwiftSyntax16AbsolutePositionVMn'
which may bind externally can not be used when making a shared object; recompile with -fPIC
clang: error: linker command failed with exit code 1 (use -v to see invocation)
The error occurs for many symbols across multiple .swift.o files in SwiftSyntax. The linker is complaining that the object files were not compiled with position-independent code (PIC), which is required for shared libraries.
Similar issue: Building Swift on linux, fails at linking of libswiftCore.so
Compiler Invocation
The failing link command uses:
cd /home/kyle/swift-project/swift/SwiftCompilerSources && /home/kyle/.local/share/swiftly/bin/swiftc -target aarch64-unknown-linux-gnu -emit-library -o lib/swift/host/compiler/lib_CompilerSwiftSyntax.so \
-module-name SwiftSyntax -O -g -wmo ... \
-Xlinker -soname -Xlinker lib_CompilerSwiftSyntax.so \
[many .swift source files]
What I've Tried
- Clean build with
--reconfigureflag - Different Swift versions: Both 6.0.3 and 6.2.3 fail identically
- Removed build artifacts and rebuilt from scratch
Questions
-
Is there a known issue with building Swift on ARM64 Linux where PIC code isn't being generated for shared libraries?
-
Is there a build flag I should be passing to ensure PIC code generation?
-
Could this be related to running in an OrbStack VM specifically, or is this a general ARM64 Linux issue?
-
Are there any workarounds, such as building with static libraries instead?
Any guidance would be greatly appreciated!