Skip to content

Commit 8eaef97

Browse files
authored
Rollup merge of rust-lang#111384 - bmisiak:issue-106021-fix, r=petrochenkov
Fix linking Mac Catalyst by including LC_BUILD_VERSION in object files Hello. My first rustc PR! Issue rust-lang#106021 prevents Rust code from being linked into Mac Catalyst applications. Apple's LD has started requiring object files to contain version information about the platform they were built for, such as: * the "deployment target" (minimum supported OS version), * the SDK version * the type of the platform (macOS/iOS/catalyst/tvOS/watchOS all have a different number). This is currently only enforced when building for Mac Catalyst. Rust uses the `object` crate which added support for including this information starting with `0.31.0`. ~~I upgraded it along with `thorin-dwp` so that everything depends on 0.31. Apparently 0.31 [pulls in](gimli-rs/object#463) `ruzstd` due to a [new ELF standard](https://maskray.me/blog/2022-09-09-zstd-compressed-debug-sections) because its `compression` feature is enabled by thorin. If you find this objectionable, let me know what the best way to avoid pulling in those dependencies might be.~~ **(`object` upgraded in rust-lang#111413 I then added two commits: * The first one adds very basic, hard-coded support for calling `set_macho_build_version` for `-macabi` (Catalyst) targets, where it claims deployment target of Catalyst 14.0 and SDK of 16.2. * The second weaves the versioning through `rust_target::spec::TargetOptions`, so that we can stick to specifying all target-related info in one place. Kudos to `@ara4n` for writing [this gist](https://gist.github.com/ara4n/320a53ea768aba51afad4c9ed2168536).
2 parents b35b134 + d816b8b commit 8eaef97

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

compiler/rustc_codegen_ssa/src/back/metadata.rs

+32
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,11 @@ pub(crate) fn create_object_file(sess: &Session) -> Option<write::Object<'static
188188
};
189189

190190
let mut file = write::Object::new(binary_format, architecture, endianness);
191+
if sess.target.is_like_osx {
192+
if let Some(build_version) = macho_object_build_version_for_target(&sess.target) {
193+
file.set_macho_build_version(build_version)
194+
}
195+
}
191196
let e_flags = match architecture {
192197
Architecture::Mips => {
193198
let arch = match sess.target.options.cpu.as_ref() {
@@ -258,6 +263,33 @@ pub(crate) fn create_object_file(sess: &Session) -> Option<write::Object<'static
258263
Some(file)
259264
}
260265

266+
/// Apple's LD, when linking for Mac Catalyst, requires object files to
267+
/// contain information about what they were built for (LC_BUILD_VERSION):
268+
/// the platform (macOS/watchOS etc), minimum OS version, and SDK version.
269+
/// This returns a `MachOBuildVersion` if necessary for the target.
270+
fn macho_object_build_version_for_target(
271+
target: &Target,
272+
) -> Option<object::write::MachOBuildVersion> {
273+
if !target.llvm_target.ends_with("-macabi") {
274+
return None;
275+
}
276+
/// The `object` crate demands "X.Y.Z encoded in nibbles as xxxx.yy.zz"
277+
/// e.g. minOS 14.0 = 0x000E0000, or SDK 16.2 = 0x00100200
278+
fn pack_version((major, minor): (u32, u32)) -> u32 {
279+
(major << 16) | (minor << 8)
280+
}
281+
282+
let platform = object::macho::PLATFORM_MACCATALYST;
283+
let min_os = (14, 0);
284+
let sdk = (16, 2);
285+
286+
let mut build_version = object::write::MachOBuildVersion::default();
287+
build_version.platform = platform;
288+
build_version.minos = pack_version(min_os);
289+
build_version.sdk = pack_version(sdk);
290+
Some(build_version)
291+
}
292+
261293
pub enum MetadataPosition {
262294
First,
263295
Last,

0 commit comments

Comments
 (0)