Skip to content

Commit d816b8b

Browse files
committed
Fix Mac Catalyst linking by adding build version
Issue rust-lang#106021 Apple LD requires build versions in object files for Catalyst
1 parent 7664dfe commit d816b8b

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)