From c6f7a10c2f24852372318e6312ecc34f2a6d0183 Mon Sep 17 00:00:00 2001 From: Owen Voorhees Date: Mon, 29 Sep 2025 10:54:39 -0700 Subject: [PATCH 1/2] Correct -nostdlib handling when linking with swiftc --- Sources/SWBUniversalPlatform/Specs/Ld.xcspec | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/Sources/SWBUniversalPlatform/Specs/Ld.xcspec b/Sources/SWBUniversalPlatform/Specs/Ld.xcspec index cb17257a..43082279 100644 --- a/Sources/SWBUniversalPlatform/Specs/Ld.xcspec +++ b/Sources/SWBUniversalPlatform/Specs/Ld.xcspec @@ -444,9 +444,24 @@ Name = "LINK_WITH_STANDARD_LIBRARIES"; Type = Boolean; DefaultValue = YES; + }, + { + Name = "CLANG_LINK_WITH_STANDARD_LIBRARIES"; + Type = Boolean; + DefaultValue = "$(LINK_WITH_STANDARD_LIBRARIES)"; CommandLineArgs = { NO = ("-nostdlib"); }; + Condition = "$(LINKER_DRIVER) == clang"; + }, + { + Name = "SWIFTC_LINK_WITH_STANDARD_LIBRARIES"; + Type = Boolean; + DefaultValue = "$(LINK_WITH_STANDARD_LIBRARIES)"; + CommandLineArgs = { + NO = ("-Xclang-linker", "-nostdlib"); + }; + Condition = "$(LINKER_DRIVER) == swiftc"; }, { Name = "KEEP_PRIVATE_EXTERNS"; From 7cc3ae46a255eb1c8031dc331aa98215fc4c82e8 Mon Sep 17 00:00:00 2001 From: Owen Voorhees Date: Mon, 29 Sep 2025 11:35:39 -0700 Subject: [PATCH 2/2] Use clang to link relocatable objects when LINKER_DRIVER=auto --- .../SWBCore/SpecImplementations/Tools/LinkerTools.swift | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Sources/SWBCore/SpecImplementations/Tools/LinkerTools.swift b/Sources/SWBCore/SpecImplementations/Tools/LinkerTools.swift index 3c3f5b7e..6a29a58f 100644 --- a/Sources/SWBCore/SpecImplementations/Tools/LinkerTools.swift +++ b/Sources/SWBCore/SpecImplementations/Tools/LinkerTools.swift @@ -362,6 +362,11 @@ public final class LdLinkerSpec : GenericLinkerSpec, SpecIdentifierType, @unchec return runpathSearchPaths } + private static func swiftcSupportsLinkingMachOType(_ type: String) -> Bool { + // swiftc does not currently support linking relocatable objects. + return type != "mh_object" + } + static func resolveLinkerDriver(_ cbc: CommandBuildContext, usedTools: [CommandLineToolSpec: Set]) -> LinkerDriverChoice { switch cbc.scope.evaluate(BuiltinMacros.LINKER_DRIVER) { case .clang: @@ -371,7 +376,7 @@ public final class LdLinkerSpec : GenericLinkerSpec, SpecIdentifierType, @unchec case .swiftc: return .swiftc case .auto: - if Self.isUsingSwift(usedTools) { + if Self.isUsingSwift(usedTools) && Self.swiftcSupportsLinkingMachOType(cbc.scope.evaluate(BuiltinMacros.MACH_O_TYPE)) { return .swiftc } else { return .clang