Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 0dbee32

Browse files
committedFeb 15, 2025
Adds binary_format to rustc target specs
1 parent 54a0f38 commit 0dbee32

File tree

9 files changed

+84
-38
lines changed

9 files changed

+84
-38
lines changed
 

‎compiler/rustc_codegen_ssa/src/mir/naked_asm.rs

+11-29
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_middle::ty::{Instance, Ty, TyCtxt};
88
use rustc_middle::{bug, span_bug, ty};
99
use rustc_span::sym;
1010
use rustc_target::callconv::{ArgAbi, FnAbi, PassMode};
11-
use rustc_target::spec::WasmCAbi;
11+
use rustc_target::spec::{BinaryFormat, WasmCAbi};
1212

1313
use crate::common;
1414
use crate::traits::{AsmCodegenMethods, BuilderMethods, GlobalAsmOperandRef, MiscCodegenMethods};
@@ -104,27 +104,6 @@ fn inline_to_global_operand<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
104104
}
105105
}
106106

107-
enum AsmBinaryFormat {
108-
Elf,
109-
Macho,
110-
Coff,
111-
Wasm,
112-
}
113-
114-
impl AsmBinaryFormat {
115-
fn from_target(target: &rustc_target::spec::Target) -> Self {
116-
if target.is_like_windows {
117-
Self::Coff
118-
} else if target.is_like_osx {
119-
Self::Macho
120-
} else if target.is_like_wasm {
121-
Self::Wasm
122-
} else {
123-
Self::Elf
124-
}
125-
}
126-
}
127-
128107
fn prefix_and_suffix<'tcx>(
129108
tcx: TyCtxt<'tcx>,
130109
instance: Instance<'tcx>,
@@ -134,7 +113,7 @@ fn prefix_and_suffix<'tcx>(
134113
) -> (String, String) {
135114
use std::fmt::Write;
136115

137-
let asm_binary_format = AsmBinaryFormat::from_target(&tcx.sess.target);
116+
let asm_binary_format = &tcx.sess.target.binary_format;
138117

139118
let is_arm = tcx.sess.target.arch == "arm";
140119
let is_thumb = tcx.sess.unstable_target_features.contains(&sym::thumb_mode);
@@ -178,10 +157,13 @@ fn prefix_and_suffix<'tcx>(
178157
}
179158
Linkage::LinkOnceAny | Linkage::LinkOnceODR | Linkage::WeakAny | Linkage::WeakODR => {
180159
match asm_binary_format {
181-
AsmBinaryFormat::Elf | AsmBinaryFormat::Coff | AsmBinaryFormat::Wasm => {
160+
BinaryFormat::Elf
161+
| BinaryFormat::Coff
162+
| BinaryFormat::Wasm
163+
| BinaryFormat::Xcoff => {
182164
writeln!(w, ".weak {asm_name}")?;
183165
}
184-
AsmBinaryFormat::Macho => {
166+
BinaryFormat::MachO => {
185167
writeln!(w, ".globl {asm_name}")?;
186168
writeln!(w, ".weak_definition {asm_name}")?;
187169
}
@@ -207,7 +189,7 @@ fn prefix_and_suffix<'tcx>(
207189
let mut begin = String::new();
208190
let mut end = String::new();
209191
match asm_binary_format {
210-
AsmBinaryFormat::Elf => {
192+
BinaryFormat::Elf | BinaryFormat::Xcoff => {
211193
let section = link_section.unwrap_or(format!(".text.{asm_name}"));
212194

213195
let progbits = match is_arm {
@@ -239,7 +221,7 @@ fn prefix_and_suffix<'tcx>(
239221
writeln!(end, "{}", arch_suffix).unwrap();
240222
}
241223
}
242-
AsmBinaryFormat::Macho => {
224+
BinaryFormat::MachO => {
243225
let section = link_section.unwrap_or("__TEXT,__text".to_string());
244226
writeln!(begin, ".pushsection {},regular,pure_instructions", section).unwrap();
245227
writeln!(begin, ".balign {align}").unwrap();
@@ -255,7 +237,7 @@ fn prefix_and_suffix<'tcx>(
255237
writeln!(end, "{}", arch_suffix).unwrap();
256238
}
257239
}
258-
AsmBinaryFormat::Coff => {
240+
BinaryFormat::Coff => {
259241
let section = link_section.unwrap_or(format!(".text.{asm_name}"));
260242
writeln!(begin, ".pushsection {},\"xr\"", section).unwrap();
261243
writeln!(begin, ".balign {align}").unwrap();
@@ -272,7 +254,7 @@ fn prefix_and_suffix<'tcx>(
272254
writeln!(end, "{}", arch_suffix).unwrap();
273255
}
274256
}
275-
AsmBinaryFormat::Wasm => {
257+
BinaryFormat::Wasm => {
276258
let section = link_section.unwrap_or(format!(".text.{asm_name}"));
277259

278260
writeln!(begin, ".section {section},\"\",@").unwrap();

‎compiler/rustc_target/src/spec/base/aix.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use rustc_abi::Endian;
22

3-
use crate::spec::{Cc, CodeModel, LinkOutputKind, LinkerFlavor, TargetOptions, crt_objects, cvs};
3+
use crate::spec::{
4+
BinaryFormat, Cc, CodeModel, LinkOutputKind, LinkerFlavor, TargetOptions, crt_objects, cvs,
5+
};
46

57
pub(crate) fn opts() -> TargetOptions {
68
TargetOptions {
@@ -21,6 +23,7 @@ pub(crate) fn opts() -> TargetOptions {
2123
linker: Some("ld".into()),
2224
eh_frame_header: false,
2325
is_like_aix: true,
26+
binary_format: BinaryFormat::Xcoff,
2427
default_dwarf_version: 3,
2528
function_sections: true,
2629
pre_link_objects: crt_objects::new(&[

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use std::borrow::Cow;
22
use std::env;
33

44
use crate::spec::{
5-
Cc, DebuginfoKind, FloatAbi, FramePointer, LinkerFlavor, Lld, RustcAbi, SplitDebuginfo,
6-
StackProbeType, StaticCow, TargetOptions, cvs,
5+
BinaryFormat, Cc, DebuginfoKind, FloatAbi, FramePointer, LinkerFlavor, Lld, RustcAbi,
6+
SplitDebuginfo, StackProbeType, StaticCow, TargetOptions, cvs,
77
};
88

99
#[cfg(test)]
@@ -116,6 +116,7 @@ pub(crate) fn base(
116116
dynamic_linking: true,
117117
families: cvs!["unix"],
118118
is_like_osx: true,
119+
binary_format: BinaryFormat::MachO,
119120
// LLVM notes that macOS 10.11+ and iOS 9+ default
120121
// to v4, so we do the same.
121122
// https://github.com/llvm/llvm-project/blob/378778a0d10c2f8d5df8ceff81f95b6002984a4b/clang/lib/Driver/ToolChains/Darwin.cpp#L1203

‎compiler/rustc_target/src/spec/base/msvc.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::borrow::Cow;
22

3-
use crate::spec::{DebuginfoKind, LinkerFlavor, Lld, SplitDebuginfo, TargetOptions};
3+
use crate::spec::{BinaryFormat, DebuginfoKind, LinkerFlavor, Lld, SplitDebuginfo, TargetOptions};
44

55
pub(crate) fn opts() -> TargetOptions {
66
// Suppress the verbose logo and authorship debugging output, which would needlessly
@@ -12,6 +12,7 @@ pub(crate) fn opts() -> TargetOptions {
1212
dll_tls_export: false,
1313
is_like_windows: true,
1414
is_like_msvc: true,
15+
binary_format: BinaryFormat::Coff,
1516
pre_link_args,
1617
abi_return_struct_as_int: true,
1718
emit_debug_gdb_scripts: false,

‎compiler/rustc_target/src/spec/base/wasm.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::spec::{
2-
Cc, LinkSelfContainedDefault, LinkerFlavor, PanicStrategy, RelocModel, TargetOptions, TlsModel,
3-
add_link_args, cvs,
2+
BinaryFormat, Cc, LinkSelfContainedDefault, LinkerFlavor, PanicStrategy, RelocModel,
3+
TargetOptions, TlsModel, add_link_args, cvs,
44
};
55

66
pub(crate) fn options() -> TargetOptions {
@@ -53,6 +53,7 @@ pub(crate) fn options() -> TargetOptions {
5353

5454
TargetOptions {
5555
is_like_wasm: true,
56+
binary_format: BinaryFormat::Wasm,
5657
families: cvs!["wasm"],
5758

5859
// we allow dynamic linking, but only cdylibs. Basically we allow a

‎compiler/rustc_target/src/spec/base/windows_gnu.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use std::borrow::Cow;
22

33
use crate::spec::{
4-
Cc, DebuginfoKind, LinkSelfContainedDefault, LinkerFlavor, Lld, SplitDebuginfo, TargetOptions,
5-
add_link_args, crt_objects, cvs,
4+
BinaryFormat, Cc, DebuginfoKind, LinkSelfContainedDefault, LinkerFlavor, Lld, SplitDebuginfo,
5+
TargetOptions, add_link_args, crt_objects, cvs,
66
};
77

88
pub(crate) fn opts() -> TargetOptions {
@@ -90,6 +90,7 @@ pub(crate) fn opts() -> TargetOptions {
9090
exe_suffix: ".exe".into(),
9191
families: cvs!["windows"],
9292
is_like_windows: true,
93+
binary_format: BinaryFormat::Coff,
9394
allows_weak_linkage: false,
9495
pre_link_args,
9596
pre_link_objects: crt_objects::pre_mingw(),

‎compiler/rustc_target/src/spec/base/windows_gnullvm.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use std::borrow::Cow;
22

3-
use crate::spec::{Cc, DebuginfoKind, LinkerFlavor, Lld, SplitDebuginfo, TargetOptions, cvs};
3+
use crate::spec::{
4+
BinaryFormat, Cc, DebuginfoKind, LinkerFlavor, Lld, SplitDebuginfo, TargetOptions, cvs,
5+
};
46

57
pub(crate) fn opts() -> TargetOptions {
68
// We cannot use `-nodefaultlibs` because compiler-rt has to be passed
@@ -30,6 +32,7 @@ pub(crate) fn opts() -> TargetOptions {
3032
exe_suffix: ".exe".into(),
3133
families: cvs!["windows"],
3234
is_like_windows: true,
35+
binary_format: BinaryFormat::Coff,
3336
allows_weak_linkage: false,
3437
pre_link_args,
3538
late_link_args,

‎compiler/rustc_target/src/spec/json.rs

+15
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,19 @@ impl Target {
103103
base.$key_name = Some(s);
104104
}
105105
} );
106+
($key_name:ident, BinaryFormat) => ( {
107+
let name = (stringify!($key_name)).replace("_", "-");
108+
obj.remove(&name).and_then(|f| f.as_str().and_then(|s| {
109+
match s.parse::<super::BinaryFormat>() {
110+
Ok(binary_format) => base.$key_name = binary_format,
111+
_ => return Some(Err(format!(
112+
"'{s}' is not a valid value for binary_format. \
113+
Use 'Coff', 'Elf', 'MachO', 'Wasm' or 'Xcoff' "
114+
))),
115+
}
116+
Some(Ok(()))
117+
})).unwrap_or(Ok(()))
118+
} );
106119
($key_name:ident, MergeFunctions) => ( {
107120
let name = (stringify!($key_name)).replace("_", "-");
108121
obj.remove(&name).and_then(|o| o.as_str().and_then(|s| {
@@ -585,6 +598,7 @@ impl Target {
585598
key!(is_like_msvc, bool);
586599
key!(is_like_wasm, bool);
587600
key!(is_like_android, bool);
601+
key!(binary_format, BinaryFormat)?;
588602
key!(default_dwarf_version, u32);
589603
key!(allows_weak_linkage, bool);
590604
key!(has_rpath, bool);
@@ -762,6 +776,7 @@ impl ToJson for Target {
762776
target_option_val!(is_like_msvc);
763777
target_option_val!(is_like_wasm);
764778
target_option_val!(is_like_android);
779+
target_option_val!(binary_format);
765780
target_option_val!(default_dwarf_version);
766781
target_option_val!(allows_weak_linkage);
767782
target_option_val!(has_rpath);

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

+39
Original file line numberDiff line numberDiff line change
@@ -1640,6 +1640,42 @@ impl fmt::Display for StackProtector {
16401640
}
16411641
}
16421642

1643+
#[derive(PartialEq, Clone, Debug)]
1644+
pub enum BinaryFormat {
1645+
Coff,
1646+
Elf,
1647+
MachO,
1648+
Wasm,
1649+
Xcoff,
1650+
}
1651+
1652+
impl FromStr for BinaryFormat {
1653+
type Err = ();
1654+
fn from_str(s: &str) -> Result<Self, Self::Err> {
1655+
match s {
1656+
"coff" => Ok(Self::Coff),
1657+
"elf" => Ok(Self::Elf),
1658+
"mach-o" => Ok(Self::MachO),
1659+
"wasm" => Ok(Self::Wasm),
1660+
"xcoff" => Ok(Self::Xcoff),
1661+
_ => Err(()),
1662+
}
1663+
}
1664+
}
1665+
1666+
impl ToJson for BinaryFormat {
1667+
fn to_json(&self) -> Json {
1668+
match self {
1669+
Self::Coff => "coff",
1670+
Self::Elf => "elf",
1671+
Self::MachO => "mach-o",
1672+
Self::Wasm => "wasm",
1673+
Self::Xcoff => "xcoff",
1674+
}
1675+
.to_json()
1676+
}
1677+
}
1678+
16431679
macro_rules! supported_targets {
16441680
( $(($tuple:literal, $module:ident),)+ ) => {
16451681
mod targets {
@@ -2369,6 +2405,8 @@ pub struct TargetOptions {
23692405
pub is_like_wasm: bool,
23702406
/// Whether a target toolchain is like Android, implying a Linux kernel and a Bionic libc
23712407
pub is_like_android: bool,
2408+
/// Target's binary file format. Defaults to BinaryFormat::Elf
2409+
pub binary_format: BinaryFormat,
23722410
/// Default supported version of DWARF on this platform.
23732411
/// Useful because some platforms (osx, bsd) only want up to DWARF2.
23742412
pub default_dwarf_version: u32,
@@ -2744,6 +2782,7 @@ impl Default for TargetOptions {
27442782
is_like_msvc: false,
27452783
is_like_wasm: false,
27462784
is_like_android: false,
2785+
binary_format: BinaryFormat::Elf,
27472786
default_dwarf_version: 4,
27482787
allows_weak_linkage: true,
27492788
has_rpath: false,

0 commit comments

Comments
 (0)
Failed to load comments.