From b6cb8e970a6470da3e6c0f98f033c0c7557f4c61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sondre=20Andr=C3=A9=20Kj=C3=A6r?= Date: Sun, 22 Nov 2026 12:18:36 +0100 Subject: [PATCH 1/3] packer: anchor extension main against dead stripping MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit App extensions link with -e _NSExtensionMain, so nothing references the product's generated `main` at link time. Under -dead_strip (the SwiftBuild default) that strips the whole extension, producing an appex with no __text — which then fails code signing. Emit a weak-import anchor for `main` in the generated stub.c: it keeps the entry point (and everything reachable from it) alive when present and compiles to NULL for extensions without one. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- Sources/PackLib/Packer.swift | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/Sources/PackLib/Packer.swift b/Sources/PackLib/Packer.swift index 6506fe7d..f6e9cb03 100644 --- a/Sources/PackLib/Packer.swift +++ b/Sources/PackLib/Packer.swift @@ -59,7 +59,22 @@ public struct Packer: Sendable { for product in plan.allProducts { let sources: URL = packageDir.appendingPathComponent("Sources/\(product.targetName)", isDirectory: true) try FileManager.default.createDirectory(at: sources, withIntermediateDirectories: true) - try Data().write(to: sources.appendingPathComponent("stub.c", isDirectory: false)) + let stub: String + switch product.type { + case .application: + stub = "" + case .appExtension: + // The extension links with -e _NSExtensionMain, so nothing + // references the user's `main` at link time. Under + // -dead_strip that strips the entire extension. A weak + // reference anchors `main` when it exists and compiles to + // NULL when the extension has none. + stub = """ + extern int main(void) __attribute__((weak_import)); + __attribute__((used)) static void * const xtool_main_anchor = (void * const)&main; + """ + } + try Data(stub.utf8).write(to: sources.appendingPathComponent("stub.c", isDirectory: false)) } let buildConfig = try await buildSettings From 760f9863d54f381329a5fdecff0bb473b12fdf1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sondre=20Andr=C3=A9=20Kj=C3=A6r?= Date: Sun, 22 Nov 2026 13:45:13 +0100 Subject: [PATCH 2/3] packer: enable cross-import overlays in dependency scanning The SwiftBuild backend's dependency scan runs without -enable-cross-import-overlays, so SDK cross-import modules like _PhotosUI_SwiftUI never reach the explicit module map and types such as PhotosPickerItem fail to resolve. Pass the flag through -Xswiftc so overlay modules are wired into the map. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- Sources/PackLib/Packer.swift | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Sources/PackLib/Packer.swift b/Sources/PackLib/Packer.swift index f6e9cb03..9bf88305 100644 --- a/Sources/PackLib/Packer.swift +++ b/Sources/PackLib/Packer.swift @@ -90,6 +90,12 @@ public struct Packer: Sendable { // in order to dump the plan, so we can skip resolution here to skirt // the issue. "--disable-automatic-resolution", + // Dependency scanning does not enable cross-import overlays + // by default, so modules like `_PhotosUI_SwiftUI` never make + // it into the explicit module map and types such as + // `PhotosPickerItem` become unresolvable. Opt in explicitly. + "-Xswiftc", "-Xfrontend", + "-Xswiftc", "-enable-cross-import-overlays", ], ) try await Subprocess.run( From d057296393d8f06a66236f0f05435c1f47e6ff34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sondre=20Andr=C3=A9=20Kj=C3=A6r?= Date: Sun, 22 Nov 2026 14:00:49 +0100 Subject: [PATCH 3/3] packer: extract extension stub source and add regression test Pull the stub.c contents out of build() into a static stubSource(for:) so the dead-strip anchor is covered by a unit test instead of only by end-to-end device deploys. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- Sources/PackLib/Packer.swift | 34 +++++++++++++++++------------- Tests/XToolTests/PackerTests.swift | 12 +++++++++++ 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/Sources/PackLib/Packer.swift b/Sources/PackLib/Packer.swift index 9bf88305..cd7933e0 100644 --- a/Sources/PackLib/Packer.swift +++ b/Sources/PackLib/Packer.swift @@ -59,21 +59,7 @@ public struct Packer: Sendable { for product in plan.allProducts { let sources: URL = packageDir.appendingPathComponent("Sources/\(product.targetName)", isDirectory: true) try FileManager.default.createDirectory(at: sources, withIntermediateDirectories: true) - let stub: String - switch product.type { - case .application: - stub = "" - case .appExtension: - // The extension links with -e _NSExtensionMain, so nothing - // references the user's `main` at link time. Under - // -dead_strip that strips the entire extension. A weak - // reference anchors `main` when it exists and compiles to - // NULL when the extension has none. - stub = """ - extern int main(void) __attribute__((weak_import)); - __attribute__((used)) static void * const xtool_main_anchor = (void * const)&main; - """ - } + let stub = Self.stubSource(for: product.type) try Data(stub.utf8).write(to: sources.appendingPathComponent("stub.c", isDirectory: false)) } @@ -239,6 +225,24 @@ public struct Packer: Sendable { } } + /// The `stub.c` source written into the builder package's target for a product. + static func stubSource(for productType: Plan.ProductType) -> String { + switch productType { + case .application: + return "" + case .appExtension: + // The extension links with -e _NSExtensionMain, so nothing + // references the user's `main` at link time. Under + // -dead_strip that strips the entire extension. A weak + // reference anchors `main` when it exists and compiles to + // NULL when the extension has none. + return """ + extern int main(void) __attribute__((weak_import)); + __attribute__((used)) static void * const xtool_main_anchor = (void * const)&main; + """ + } + } + static func cfBundleSupportedPlatforms(for triple: String) -> [String] { let components = triple.split(separator: "-").map(String.init) guard let appleIndex = components.firstIndex(of: "apple"), diff --git a/Tests/XToolTests/PackerTests.swift b/Tests/XToolTests/PackerTests.swift index 5aabda83..ef128b16 100644 --- a/Tests/XToolTests/PackerTests.swift +++ b/Tests/XToolTests/PackerTests.swift @@ -13,3 +13,15 @@ import Testing #expect(Packer.cfBundleSupportedPlatforms(for: "unknown-triple") == ["iPhoneOS"]) } + +@Test func extensionStubAnchorsMainAgainstDeadStripping() { + #expect(Packer.stubSource(for: .application) == "") + + let stub = Packer.stubSource(for: .appExtension) + // Extensions link with `-e _NSExtensionMain`, so nothing references + // the product's `main`; `-dead_strip` then strips all of its code. + // The stub must weakly anchor `main` without requiring it to exist. + #expect(stub.contains("weak_import")) + #expect(stub.contains("&main")) + #expect(stub.contains("used")) +}