Skip to content

Commit

Permalink
Fix the default binary format for most architectures to be ELF. (#108)
Browse files Browse the repository at this point in the history
Following the [logic in LLVM], make the binary format for most targets
be ELF instead of Unknown. To preserve round-tripping for Rust target
names, this requires some special-casing in the formatting code, but
this isn't new; a lot of targets require special cases to round-trip
properly.

[logic in LLVM]: https://github.com/llvm/llvm-project/blob/d3e7c4ce7a3d7f08cea02cba8f34c590a349688b/llvm/lib/TargetParser/Triple.cpp#L877
  • Loading branch information
sunfishcode committed Jul 2, 2024
1 parent 669004f commit 3a1cc71
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
19 changes: 17 additions & 2 deletions src/targets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1006,7 +1006,9 @@ pub(crate) fn default_binary_format(triple: &Triple) -> BinaryFormat {
| OperatingSystem::Wasi
| OperatingSystem::Unknown => match triple.architecture {
Architecture::Wasm32 | Architecture::Wasm64 => BinaryFormat::Wasm,
_ => BinaryFormat::Unknown,
Architecture::Unknown => BinaryFormat::Unknown,
// Default to ELF, following `getDefaultFormat` in LLVM.
_ => BinaryFormat::Elf,
},
_ => BinaryFormat::Elf,
}
Expand Down Expand Up @@ -1820,6 +1822,19 @@ mod tests {
}
}

#[test]
fn default_format_to_elf() {
let t = Triple::from_str("riscv64").expect("can't parse target");
assert_eq!(
t.architecture,
Architecture::Riscv64(Riscv64Architecture::Riscv64),
);
assert_eq!(t.vendor, Vendor::Unknown);
assert_eq!(t.operating_system, OperatingSystem::Unknown);
assert_eq!(t.environment, Environment::Unknown);
assert_eq!(t.binary_format, BinaryFormat::Elf);
}

#[test]
fn thumbv7em_none_eabihf() {
let t = Triple::from_str("thumbv7em-none-eabihf").expect("can't parse target");
Expand Down Expand Up @@ -1915,7 +1930,7 @@ mod tests {
);
assert_eq!(t.operating_system, OperatingSystem::Unknown);
assert_eq!(t.environment, Environment::Unknown);
assert_eq!(t.binary_format, BinaryFormat::Unknown);
assert_eq!(t.binary_format, BinaryFormat::Elf);

assert_eq!(
Triple::from_str("unknown-foo"),
Expand Down
26 changes: 25 additions & 1 deletion src/triple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,13 +261,37 @@ impl fmt::Display for Triple {
}
}

if self.binary_format != implied_binary_format {
if self.binary_format != implied_binary_format || show_binary_format_with_no_os(self) {
// As a special case, omit a non-default binary format for some
// targets which happen to exclude it.
write!(f, "-{}", self.binary_format)?;
}
Ok(())
}
}

fn show_binary_format_with_no_os(triple: &Triple) -> bool {
if triple.binary_format == BinaryFormat::Unknown {
return false;
}

#[cfg(feature = "arch_zkasm")]
{
if triple.architecture == Architecture::ZkAsm {
return false;
}
}

triple.environment != Environment::Eabi
&& triple.environment != Environment::Eabihf
&& triple.environment != Environment::Sgx
&& triple.architecture != Architecture::Avr
&& triple.architecture != Architecture::Wasm32
&& triple.architecture != Architecture::Wasm64
&& (triple.operating_system == OperatingSystem::None_
|| triple.operating_system == OperatingSystem::Unknown)
}

impl FromStr for Triple {
type Err = ParseError;

Expand Down

0 comments on commit 3a1cc71

Please sign in to comment.