Skip to content

Commit d1bc468

Browse files
authored
Unrolled build for rust-lang#136637
Rollup merge of rust-lang#136637 - Pyr0de:binary-format, r=Noratrieb Add binary_format to rustc target specs Added binary format field to `TargetOptions` Fixes rust-lang#135724 r? `@Noratrieb`
2 parents f8a913b + 17f2928 commit d1bc468

File tree

11 files changed

+102
-48
lines changed

11 files changed

+102
-48
lines changed

compiler/rustc_codegen_ssa/src/back/metadata.rs

+1-9
Original file line numberDiff line numberDiff line change
@@ -252,15 +252,7 @@ pub(crate) fn create_object_file(sess: &Session) -> Option<write::Object<'static
252252
// Unsupported architecture.
253253
_ => return None,
254254
};
255-
let binary_format = if sess.target.is_like_osx {
256-
BinaryFormat::MachO
257-
} else if sess.target.is_like_windows {
258-
BinaryFormat::Coff
259-
} else if sess.target.is_like_aix {
260-
BinaryFormat::Xcoff
261-
} else {
262-
BinaryFormat::Elf
263-
};
255+
let binary_format = sess.target.binary_format.to_object();
264256

265257
let mut file = write::Object::new(binary_format, architecture, endianness);
266258
file.set_sub_architecture(sub_architecture);

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/cygwin.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
let mut pre_link_args = TargetOptions::link_args(
@@ -32,6 +34,7 @@ pub(crate) fn opts() -> TargetOptions {
3234
exe_suffix: ".exe".into(),
3335
families: cvs!["unix"],
3436
is_like_windows: true,
37+
binary_format: BinaryFormat::Coff,
3538
allows_weak_linkage: false,
3639
pre_link_args,
3740
late_link_args,

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', 'mach-o', '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

+52
Original file line numberDiff line numberDiff line change
@@ -1644,6 +1644,55 @@ impl fmt::Display for StackProtector {
16441644
}
16451645
}
16461646

1647+
#[derive(PartialEq, Clone, Debug)]
1648+
pub enum BinaryFormat {
1649+
Coff,
1650+
Elf,
1651+
MachO,
1652+
Wasm,
1653+
Xcoff,
1654+
}
1655+
1656+
impl BinaryFormat {
1657+
/// Returns [`object::BinaryFormat`] for given `BinaryFormat`
1658+
pub fn to_object(&self) -> object::BinaryFormat {
1659+
match self {
1660+
Self::Coff => object::BinaryFormat::Coff,
1661+
Self::Elf => object::BinaryFormat::Elf,
1662+
Self::MachO => object::BinaryFormat::MachO,
1663+
Self::Wasm => object::BinaryFormat::Wasm,
1664+
Self::Xcoff => object::BinaryFormat::Xcoff,
1665+
}
1666+
}
1667+
}
1668+
1669+
impl FromStr for BinaryFormat {
1670+
type Err = ();
1671+
fn from_str(s: &str) -> Result<Self, Self::Err> {
1672+
match s {
1673+
"coff" => Ok(Self::Coff),
1674+
"elf" => Ok(Self::Elf),
1675+
"mach-o" => Ok(Self::MachO),
1676+
"wasm" => Ok(Self::Wasm),
1677+
"xcoff" => Ok(Self::Xcoff),
1678+
_ => Err(()),
1679+
}
1680+
}
1681+
}
1682+
1683+
impl ToJson for BinaryFormat {
1684+
fn to_json(&self) -> Json {
1685+
match self {
1686+
Self::Coff => "coff",
1687+
Self::Elf => "elf",
1688+
Self::MachO => "mach-o",
1689+
Self::Wasm => "wasm",
1690+
Self::Xcoff => "xcoff",
1691+
}
1692+
.to_json()
1693+
}
1694+
}
1695+
16471696
macro_rules! supported_targets {
16481697
( $(($tuple:literal, $module:ident),)+ ) => {
16491698
mod targets {
@@ -2381,6 +2430,8 @@ pub struct TargetOptions {
23812430
pub is_like_wasm: bool,
23822431
/// Whether a target toolchain is like Android, implying a Linux kernel and a Bionic libc
23832432
pub is_like_android: bool,
2433+
/// Target's binary file format. Defaults to BinaryFormat::Elf
2434+
pub binary_format: BinaryFormat,
23842435
/// Default supported version of DWARF on this platform.
23852436
/// Useful because some platforms (osx, bsd) only want up to DWARF2.
23862437
pub default_dwarf_version: u32,
@@ -2756,6 +2807,7 @@ impl Default for TargetOptions {
27562807
is_like_msvc: false,
27572808
is_like_wasm: false,
27582809
is_like_android: false,
2810+
binary_format: BinaryFormat::Elf,
27592811
default_dwarf_version: 4,
27602812
allows_weak_linkage: true,
27612813
has_rpath: false,

0 commit comments

Comments
 (0)