Skip to content

Commit 4d0a38e

Browse files
authored
Mach-O: Set LC_BUILD_VERSION (#13131)
* Use OperatingSystem::is_like_darwin helper * Set LC_BUILD_VERSION This makes the linker stop warning about missing platform load command.
1 parent d489988 commit 4d0a38e

File tree

5 files changed

+83
-31
lines changed

5 files changed

+83
-31
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ regalloc2 = "0.15.1"
336336
wasip1 = { version = "1.0.0", default-features = false }
337337

338338
# cap-std family:
339-
target-lexicon = "0.13.0"
339+
target-lexicon = "0.13.3"
340340
cap-std = "3.4.5"
341341
cap-fs-ext = "3.4.5"
342342
cap-net-ext = "3.4.5"

cranelift/codegen/src/isa/aarch64/mod.rs

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ use alloc::string::String;
1818
use alloc::{boxed::Box, vec::Vec};
1919
use core::fmt;
2020
use cranelift_control::ControlPlane;
21-
#[cfg(feature = "unwind")]
22-
use target_lexicon::OperatingSystem;
2321
use target_lexicon::{Aarch64Architecture, Architecture, Triple};
2422

2523
// New backend:
@@ -151,17 +149,9 @@ impl TargetIsa for AArch64Backend {
151149

152150
#[cfg(feature = "unwind")]
153151
fn create_systemv_cie(&self) -> Option<gimli::write::CommonInformationEntry> {
154-
let is_apple_os = match self.triple.operating_system {
155-
OperatingSystem::Darwin(_)
156-
| OperatingSystem::IOS(_)
157-
| OperatingSystem::MacOSX { .. }
158-
| OperatingSystem::TvOS(_) => true,
159-
_ => false,
160-
};
161-
162152
if self.isa_flags.sign_return_address()
163153
&& self.isa_flags.sign_return_address_with_bkey()
164-
&& !is_apple_os
154+
&& !self.triple.operating_system.is_like_darwin()
165155
{
166156
unimplemented!(
167157
"Specifying that the B key is used with pointer authentication instructions in the CIE is not implemented."
@@ -185,19 +175,12 @@ impl TargetIsa for AArch64Backend {
185175
}
186176

187177
fn page_size_align_log2(&self) -> u8 {
188-
use target_lexicon::*;
189-
match self.triple().operating_system {
190-
OperatingSystem::MacOSX { .. }
191-
| OperatingSystem::Darwin(_)
192-
| OperatingSystem::IOS(_)
193-
| OperatingSystem::TvOS(_) => {
194-
debug_assert_eq!(1 << 14, 0x4000);
195-
14
196-
}
197-
_ => {
198-
debug_assert_eq!(1 << 16, 0x10000);
199-
16
200-
}
178+
if self.triple().operating_system.is_like_darwin() {
179+
debug_assert_eq!(1 << 14, 0x4000);
180+
14
181+
} else {
182+
debug_assert_eq!(1 << 16, 0x10000);
183+
16
201184
}
202185
}
203186

cranelift/object/src/backend.rs

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use cranelift_module::{
1010
DataDescription, DataId, FuncId, Init, Linkage, Module, ModuleDeclarations, ModuleError,
1111
ModuleReloc, ModuleRelocTarget, ModuleResult,
1212
};
13-
use log::info;
13+
use log::{info, warn};
1414
use object::write::{
1515
Object, Relocation, SectionId, StandardSection, Symbol, SymbolId, SymbolSection,
1616
};
@@ -21,7 +21,7 @@ use object::{
2121
use std::collections::HashMap;
2222
use std::collections::hash_map::Entry;
2323
use std::mem;
24-
use target_lexicon::PointerWidth;
24+
use target_lexicon::{PointerWidth, Triple};
2525

2626
/// A builder for `ObjectModule`.
2727
pub struct ObjectBuilder {
@@ -137,6 +137,68 @@ impl ObjectBuilder {
137137
}
138138
}
139139

140+
/// See the following for details:
141+
/// <https://github.com/rust-lang/rust/blob/1.95.0/compiler/rustc_codegen_ssa/src/back/metadata.rs#L408-L425>
142+
fn macho_build_version(triple: &Triple) -> Option<object::write::MachOBuildVersion> {
143+
use target_lexicon::{DeploymentTarget, OperatingSystem::*};
144+
145+
fn pack_version(v: DeploymentTarget) -> u32 {
146+
let (major, minor, patch) = (v.major as u32, v.minor as u32, v.patch as u32);
147+
(major << 16) | (minor << 8) | patch
148+
}
149+
150+
match triple.operating_system {
151+
Darwin(v) | MacOSX(v) | IOS(v) | TvOS(v) | VisionOS(v) | WatchOS(v) | XROS(v) => {
152+
use object::macho::*;
153+
use target_lexicon::Environment::*;
154+
// Same as https://github.com/rust-lang/rust/blob/1.95.0/compiler/rustc_codegen_ssa/src/back/apple.rs#L36-L50.
155+
//
156+
// TODO(madsmtm): Properly support simulator after
157+
// https://github.com/bytecodealliance/target-lexicon/pull/130
158+
let platform = match (triple.operating_system, triple.environment) {
159+
(Darwin(_), _) => 0, // PLATFORM_UNKNOWN
160+
(MacOSX(_), _) => PLATFORM_MACOS,
161+
(_, Macabi) => PLATFORM_MACCATALYST,
162+
(IOS(_), Sim) => PLATFORM_IOSSIMULATOR,
163+
(IOS(_), _) => PLATFORM_IOS,
164+
(TvOS(_), Sim) => PLATFORM_TVOSSIMULATOR,
165+
(TvOS(_), _) => PLATFORM_TVOS,
166+
(VisionOS(_) | XROS(_), Sim) => PLATFORM_XROSSIMULATOR,
167+
(VisionOS(_) | XROS(_), _) => PLATFORM_XROS,
168+
(WatchOS(_), Sim) => PLATFORM_WATCHOSSIMULATOR,
169+
(WatchOS(_), _) => PLATFORM_WATCHOS,
170+
_ => {
171+
warn!("unsupported OS/environment: {triple}");
172+
0
173+
}
174+
};
175+
176+
let mut build_version = object::write::MachOBuildVersion::default();
177+
build_version.platform = platform;
178+
179+
build_version.minos = if let Some(v) = v {
180+
pack_version(v)
181+
} else {
182+
// The `minos` in object files is useful for diagnostics, as
183+
// it tells the linker whether the file supports a given OS -
184+
// if the `minos` is higher than what you're linking against,
185+
// that's a signal that something has gone wrong.
186+
//
187+
// Using `0.0.0` here should be fine if we don't have the data
188+
// available.
189+
0
190+
};
191+
192+
// Setting a 0 SDK version is fine, it's only relevant for the
193+
// final linked binary.
194+
build_version.sdk = 0;
195+
196+
Some(build_version)
197+
}
198+
_ => None,
199+
}
200+
}
201+
140202
/// An `ObjectModule` implements `Module` and emits ".o" files using the `object` library.
141203
///
142204
/// See the `ObjectBuilder` for a convenient way to construct `ObjectModule` instances.
@@ -162,6 +224,13 @@ impl ObjectModule {
162224
object.flags = builder.flags;
163225
object.set_subsections_via_symbols();
164226
object.add_file_symbol(builder.name);
227+
if let Some(info) = macho_build_version(builder.isa.triple()) {
228+
// Set LC_BUILD_VERSION.
229+
//
230+
// Required when linking Apple targets to avoid warning, see:
231+
// https://github.com/bytecodealliance/wasmtime/issues/8730
232+
object.set_macho_build_version(info);
233+
}
165234
Self {
166235
isa: builder.isa,
167236
object,

supply-chain/imports.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,8 +1041,8 @@ user-login = "alexcrichton"
10411041
user-name = "Alex Crichton"
10421042

10431043
[[publisher.target-lexicon]]
1044-
version = "0.13.0"
1045-
when = "2024-12-05"
1044+
version = "0.13.3"
1045+
when = "2025-09-09"
10461046
user-id = 6825
10471047
user-login = "sunfishcode"
10481048
user-name = "Dan Gohman"

0 commit comments

Comments
 (0)