In the post about adopting Xcode’s Enhanced Security I mentioned, in one sentence near the end of the pointer authentication part, that the capability’s compiler settings only reach the app target’s own C-family code, and not the Swift packages or the prebuilt Rust library. In the reading app I’m working on that’s where all of the C and C++ is, so this is a short write-up of what it took to get the settings there, and of a Mac Catalyst link failure that showed up as soon as they did.
What the compiler half is
Besides the entitlements, ENABLE_ENHANCED_SECURITY = YES cascades a few clang settings, and these are the ones this post is about:
| Setting | What clang gets |
|---|---|
| Stack zero-initialization | -ftrivial-auto-var-init=zero |
| Typed allocators | -ftyped-memory-operations, and for C++ -ftyped-cxx-new-delete -ftyped-cxx-delete |
| C++ standard library hardening | -D_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_FAST in optimized builds (Debug builds get _LIBCPP_HARDENING_MODE_DEBUG) |
| Security warnings | a set of -W flags |
They’re all clang flags, so Swift code doesn’t care about them, and the app target in my case is Swift only. The C and C++ in the process comes from Swift packages (mlx-swift’s Cmlx, yyjson, minizip, the C and Objective-C parts of Sentry) and from the Rust core, whose crates compile vendored C and C++ through the cc crate. I haven’t measured what the flags cost at runtime, and allocation-heavy C++ like Cmlx is where a cost would show.
Package targets don’t see the app target’s settings
Xcode writes the arguments it passes clang for each target into response files next to the object files, so the easiest way for us to see what a package was compiled with is to grep those. Building into a fresh folder of its own keeps an older build’s files out of the answer:
xcodebuild build -scheme App -configuration Release -destination 'generic/platform=iOS' \
-derivedDataPath DerivedData
cd DerivedData/Build/Intermediates.noindex
grep -l -r --include='*-common-args.resp' 'ftrivial-auto-var-init=zero' . \
| sed 's|/Objects-normal.*||' | sort -u # targets that have it
grep -L -r --include='*-common-args.resp' 'ftrivial-auto-var-init=zero' . # compiles that don't
With the setting only on the app target, the first grep printed nothing at all. The packages resolved ENABLE_ENHANCED_SECURITY to NO, because a Swift package target takes its settings from its own Package.swift and from the defaults, and doesn’t inherit anything from the project or from the target that depends on it. What does reach every target in the build, packages included, is a setting given to xcodebuild itself, so that’s where we pass it:
xcodebuild build -scheme App -configuration Release -destination 'generic/platform=iOS' \
-derivedDataPath DerivedData \
ENABLE_ENHANCED_SECURITY=YES ENABLE_POINTER_AUTHENTICATION=NO
The pointer authentication override has to come along, since this way the cascade would turn it on for every package as well (and the Rust library has no arm64e slice, which is the whole reason it’s off in the first place). An xcconfig file works the same, either passed with -xcconfig or named in the XCODE_XCCONFIG_FILE environment variable, and a small test project showed both reaching a package target. With the pair in place, the first grep lists Cmlx, yyjson, minizip, Sentry’s targets and a few small shims, the second one prints nothing, and the Cmlx C++ response file has everything from the table above:
'-D_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_FAST'
'-ftrivial-auto-var-init=zero'
-ftyped-memory-operations
-ftyped-cxx-new-delete
-ftyped-cxx-delete
(The C-only packages don’t get the libc++ hardening define, which is expected.) These settings apply to everything in the build, so the list also includes swift-syntax’s C shims built for the Mac, which only end up in the macros that run during the build, and that’s harmless.
Swift package targets only get the settings you give to xcodebuild itself, never the ones on a target.
That has two consequences. A build or archive started from Xcode’s Product menu gets none of them (unless you manage to get XCODE_XCCONFIG_FILE into Xcode’s own environment, which I haven’t tried), so if you archive that way you ship the packages without the hardening, and the release archive should be an xcodebuild archive with the same pair (I kept the setting on the app target too, so that C added to it later is covered either way). And every build that shares a DerivedData folder has to agree on the settings, Xcode’s own builds included, because xcodebuild without -derivedDataPath uses the same folder as the Xcode app, and a build with different settings recompiles every C and C++ target the other one built. I put the pair in one shell array that every build script sources and still left one script out the first time, and the next test run recompiled every C and C++ target in the app, so it’s easiest to give the scripts a -derivedDataPath of their own and pass the pair from all of them.
The Rust crate’s C and C++
Cargo never sees any of Xcode’s settings. The Rust core is built by a script into an XCFramework before Xcode runs, and the C and C++ inside it is compiled by cc from the crates’ build scripts. I had assumed the packages were all the C there was, until a review pointed at the vendored UnRAR, which is C++ that parses the RAR and CBR files the user imports, so it’s about the most untrusted input the app has, along with a handful of smaller C libraries.
cc reads extra flags from environment variables, and we can give them per target, as CFLAGS_<target> and CXXFLAGS_<target>. It accepts the triple with dashes or with underscores, and the underscores are the form the shell will export. With the per-target form only the code built for the Apple targets gets the flags, and anything a build script compiles for the Mac running the build stays as it was. The flags are copied from what Xcode passed Cmlx:
ENHANCED_SECURITY_CFLAGS="-ftrivial-auto-var-init=zero -ftyped-memory-operations"
ENHANCED_SECURITY_CXXFLAGS="$ENHANCED_SECURITY_CFLAGS -ftyped-cxx-new-delete -ftyped-cxx-delete -D_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_FAST"
for rust_target in aarch64_apple_ios aarch64_apple_ios_sim aarch64_apple_ios_macabi x86_64_apple_ios_macabi; do
export "CFLAGS_$rust_target=$ENHANCED_SECURITY_CFLAGS"
export "CXXFLAGS_$rust_target=$ENHANCED_SECURITY_CXXFLAGS"
done
cargo build --release --target aarch64-apple-ios # and the other targets
Cargo reruns those build scripts by itself when the variables change, since cc tells it which variables it read. What doesn’t notice is a wrapper script that skips the build when a framework already exists, or a habit of rebuilding the framework only when the Rust sources change, so the flags have to be part of whatever decides that, or an old unhardened framework keeps getting linked.
There are two places to check that the flags took. cc writes each compile command into its build script’s output, so this lists every crate that compiled C or C++ with them (five here):
grep -l -e '-ftrivial-auto-var-init=zero' target/aarch64-apple-ios/release/build/*/output
The other is the framework Xcode links, since a stale one is the case to catch. Typed C++ allocation calls operator new and operator delete overloads that take an extra std::__type_descriptor_t argument, so we can look for those in each slice:
for a in Core.xcframework/*/*.a; do
printf '%s ' "$a"; nm -u -arch all "$a" 2>/dev/null | grep -c '__type_descriptor_t'
done
nm -u Core.xcframework/ios-arm64/*.a 2>/dev/null | grep '__type_descriptor_t' | sort -u | c++filt
The 2>/dev/null is there because Apple’s nm can’t read the LLVM bitcode a newer rustc embeds in its own object files and prints a few hundred errors about them, while the C and C++ objects are plain Mach-O and read fine. -arch all matters for the Mac Catalyst slice, which holds two architectures. Here the count went from 0 to 48 references per architecture (96 for the Catalyst slice), all of them in UnRAR’s objects, and the second command prints the four overloads (operator new, operator new[] and their delete pairs).
A Mac Catalyst link that Xcode can’t do
With the pair in place, the iOS builds and the Release Mac Catalyst build were fine, and the Mac Catalyst test build stopped linking Cmlx:
ld: building for 'macCatalyst', but linking in object file
(…/MacOSX.sdk/usr/lib/libc++polyfills.a[arm64][2](tmo.cpp.o)) built for 'macOS'
tmo.cpp.o holds the typed operator new and operator delete from above. The system libc++ only has them from iOS 18 and macOS 15 on, so for an older deployment target clang links a small static shim, libc++polyfills.a, that uses the system’s typed operators where they exist and falls back to the plain ones where they don’t. Clang adds it when two things hold, the link has typed C++ allocation on and the deployment target is below that cutoff (it took me a while of looking for some other flag in Xcode’s link line before I tried changing only the triple). We can see it with a one-line C++ file and no project:
echo 'struct A{int x;}; int main(){ A* a = new A; delete a; }' > t.cpp
for t in arm64-apple-ios17.0-macabi arm64-apple-ios18.0-macabi arm64-apple-macos14 arm64-apple-macos15; do
printf '%s ' $t
xcrun -sdk macosx clang++ -### -target $t -dynamiclib -ftyped-cxx-new-delete -ftyped-cxx-delete \
t.cpp -o t.dylib 2>&1 | grep -c c++polyfills
done
# the same Catalyst link without the typed flags
xcrun -sdk macosx clang++ -### -target arm64-apple-ios17.0-macabi -dynamiclib t.cpp -o t.dylib 2>&1 | grep -c c++polyfills
# and iOS 17 on a device
xcrun -sdk iphoneos clang++ -### -target arm64-apple-ios17.0 -dynamiclib -ftyped-cxx-new-delete -ftyped-cxx-delete \
t.cpp -o t.dylib 2>&1 | grep -c c++polyfills
arm64-apple-ios17.0-macabi 1
arm64-apple-ios18.0-macabi 0
arm64-apple-macos14 1
arm64-apple-macos15 0
0
1
So clang asks for the polyfill on iOS too, and there the SDK has it. Xcode 27.2 ships libc++polyfills.a in the iPhoneOS, iPhoneSimulator and MacOSX SDKs (and the tvOS, watchOS, visionOS and DriverKit ones), but there’s nothing under MacOSX.sdk/System/iOSSupport/usr/lib, so a Catalyst link picks the macOS one from MacOSX.sdk/usr/lib and refuses it.
Only the test build hit it because of how the packages get linked. Release links them statically into the app, and the app’s floor is iOS 18, so the link doesn’t ask for the polyfill and the typed operators come from the system libc++. In the test build the test bundle links the same packages the app does, and Xcode then builds each of those packages as a dynamic framework of its own, linked at the package’s own deployment target, and mlx-swift declares iOS 17. (In a small test project the switch came from a second target linking the package, not from it being a test build, so an app extension that links the same C++ package should end up the same way.) An app whose own floor is below iOS 18 would hit it in Release too. Raising every package to iOS 18 on the command line broke swift-perception (its Bindable became unavailable), and forcing static linking failed with the same error, so what was left was turning off typed C++ allocation for that one build:
xcodebuild build-for-testing -scheme App -destination 'platform=macOS,variant=Mac Catalyst' \
-derivedDataPath DerivedData-catalyst-tests \
ENABLE_ENHANCED_SECURITY=YES ENABLE_POINTER_AUTHENTICATION=NO \
CLANG_ENABLE_CPLUSPLUS_TYPED_ALLOCATOR_SUPPORT=NO
With the setting off, Xcode passes -fno-typed-cxx-new-delete to the compiles and leaves the typed flags off the link, so the link stops asking for the polyfill, and the build keeps zero-initialized stack variables, typed C allocation and libc++ hardening. It has a DerivedData folder of its own, because by the rule above a build with an extra setting would otherwise recompile the C++ the other builds share. It looks like a gap in Xcode rather than anything a project can fix, so the override can come out once Xcode ships a Catalyst polyfill, or once every C++ package in that build declares iOS 18, and the probe above is the thing to rerun after an Xcode update (the ios17.0-macabi line printing 0, or a libc++polyfills.a turning up under iOSSupport).
Summary
The compiler half of Enhanced Security is a set of build settings, and Swift package targets don’t get settings from the targets that depend on them, so they have to be given to xcodebuild itself (on the command line, with -xcconfig or through XCODE_XCCONFIG_FILE), by every build that shares the DerivedData folder, with the release archive made the same way. Rust crates that compile C or C++ through cc need the same flags in CFLAGS_<target> and CXXFLAGS_<target>, and the response files, cc’s build script output and nm -u on the XCFramework you link are how you check that it all took. On Mac Catalyst in Xcode 27.2, linking C++ below iOS 18 with typed C++ allocation on fails because there’s no Catalyst copy of the polyfill clang asks for, and turning off CLANG_ENABLE_CPLUSPLUS_TYPED_ALLOCATOR_SUPPORT for just that build keeps the rest of the hardening.