Skip to content

Commit 23cdb50

Browse files
committed
Apple: Improve comments for -arch linker argument
1 parent d6c8169 commit 23cdb50

2 files changed

Lines changed: 36 additions & 11 deletions

File tree

‎compiler/rustc_target/src/spec/base/apple/mod.rs‎

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,25 @@ impl Arch {
3939
}
4040
}
4141

42+
/// The architecture name to forward to the linker.
43+
fnld_arch(self) -> &'staticstr{
44+
// Supported architecture names can be found in the source:
45+
// https://github.com/apple-oss-distributions/ld64/blob/ld64-951.9/src/abstraction/MachOFileAbstraction.hpp#L578-L648
46+
matchself{
47+
Armv7k => "armv7k",
48+
Armv7s => "armv7s",
49+
Arm64 => "arm64",
50+
Arm64e => "arm64e",
51+
Arm64_32 => "arm64_32",
52+
// ld64 doesn't understand i686, so fall back to i386 instead
53+
//
54+
// Same story when linking with cc, since that ends up invoking ld64.
55+
I386 | I686 => "i386",
56+
X86_64 => "x86_64",
57+
X86_64h => "x86_64h",
58+
}
59+
}
60+
4261
pub(crate)fntarget_arch(self) -> Cow<'static,str>{
4362
Cow::Borrowed(matchself{
4463
Armv7k | Armv7s => "arm",
@@ -116,20 +135,29 @@ fn pre_link_args(os: &'static str, arch: Arch, abi: TargetAbi) -> LinkArgs {
116135
};
117136
let sdk_version = min_version.clone();
118137

119-
letmut args = TargetOptions::link_args(
120-
LinkerFlavor::Darwin(Cc::No,Lld::No),
121-
&["-arch", arch.target_name(),"-platform_version"],
122-
);
138+
// From the man page for ld64 (`man ld`):
139+
// > The linker accepts universal (multiple-architecture) input files,
140+
// > but always creates a "thin" (single-architecture), standard Mach-O
141+
// > output file. The architecture for the output file is specified using
142+
// > the -arch option.
143+
//
144+
// The linker has heuristics to determine the desired architecture, but to
145+
// be safe, and to avoid a warning, we set the architecture explicitly.
146+
letmut args =
147+
TargetOptions::link_args(LinkerFlavor::Darwin(Cc::No,Lld::No),&["-arch", arch.ld_arch()]);
148+
123149
add_link_args_iter(
124150
&mut args,
125151
LinkerFlavor::Darwin(Cc::No,Lld::No),
126-
[platform_name, min_version, sdk_version].into_iter(),
152+
["-platform_version".into(),platform_name, min_version, sdk_version].into_iter(),
127153
);
128154
if abi != TargetAbi::MacCatalyst{
155+
// CC forwards the `-arch` to the linker, so we use the same value
156+
// here intentionally.
129157
add_link_args(
130158
&mut args,
131159
LinkerFlavor::Darwin(Cc::Yes,Lld::No),
132-
&["-arch", arch.target_name()],
160+
&["-arch", arch.ld_arch()],
133161
);
134162
}else{
135163
add_link_args_iter(

‎compiler/rustc_target/src/spec/targets/i686_apple_darwin.rs‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ use crate::spec::base::apple::{macos_llvm_target, opts, Arch, TargetAbi};
22
usecrate::spec::{Cc,FramePointer,LinkerFlavor,Lld,Target,TargetOptions};
33

44
pub(crate)fntarget() -> Target{
5-
// ld64 only understands i386 and not i686
6-
let arch = Arch::I386;
5+
let arch = Arch::I686;
76
letmut base = opts("macos", arch,TargetAbi::Normal);
87
base.max_atomic_width = Some(64);
98
base.add_pre_link_args(LinkerFlavor::Darwin(Cc::Yes,Lld::No),&["-m32"]);
@@ -13,9 +12,7 @@ pub(crate) fn target() -> Target {
1312
// Clang automatically chooses a more specific target based on
1413
// MACOSX_DEPLOYMENT_TARGET. To enable cross-language LTO to work
1514
// correctly, we do too.
16-
//
17-
// While ld64 doesn't understand i686, LLVM does.
18-
llvm_target:macos_llvm_target(Arch::I686).into(),
15+
llvm_target:macos_llvm_target(arch).into(),
1916
metadata:crate::spec::TargetMetadata{
2017
description:Some("32-bit macOS (10.12+, Sierra+)".into()),
2118
tier:Some(3),

0 commit comments

Comments
 (0)