use xold::elf::constants::{EM_AARCH64, EM_RISCV, EM_X86_64};
const ELF_EMULATIONS: [(&str, u16); 4] = [
("elf_x86_64", EM_X86_64),
("aarch64linux", EM_AARCH64),
("aarch64elf", EM_AARCH64),
("elf64lriscv", EM_RISCV),
];
const COFF_EMULATIONS: [&str; 1] = ["i386pep"];
#[derive(Clone, Copy)]
pub enum Machine {
Elf(u16),
Coff,
MachO,
Unknown,
}
pub fn check(name: &str, machine: Machine) -> Result<(), String> {
if let Some(&(_, want)) =
ELF_EMULATIONS.iter().find(|(emu, _)| *emu == name)
{
return match machine {
Machine::Elf(have) if have == want => Ok(()),
Machine::Elf(have) => Err(mismatch(
name,
&format!(
"the inputs are ELF objects for {}, not the machine \
`{name}` names",
machine_name(have)
),
)),
Machine::Coff => Err(mismatch(name, "the inputs are COFF")),
Machine::MachO => Err(mismatch(name, "the inputs are Mach-O")),
Machine::Unknown => Ok(()),
};
}
if COFF_EMULATIONS.contains(&name) {
return match machine {
Machine::Coff | Machine::Unknown => Ok(()),
Machine::Elf(_) => Err(mismatch(name, "the inputs are ELF")),
Machine::MachO => Err(mismatch(name, "the inputs are Mach-O")),
};
}
Err(format!(
"unknown emulation `{name}`: this linker produces 64-bit ELF \
for x86-64, AArch64 and RISC-V, and PE32+ for x86-64 ({}, {})",
ELF_EMULATIONS
.iter()
.map(|(emu, _)| *emu)
.collect::<Vec<_>>()
.join(", "),
COFF_EMULATIONS.join(", ")
))
}
fn machine_name(machine: u16) -> String {
match machine {
EM_X86_64 => "x86-64".into(),
EM_AARCH64 => "AArch64".into(),
EM_RISCV => "RISC-V".into(),
other => format!("machine {other}"),
}
}
fn mismatch(name: &str, why: &str) -> String {
format!("`-m {name}` does not describe this link: {why}")
}