use std::{fs, path::PathBuf};
use common::elf_fixture;
use rayon::ThreadPoolBuilder;
use xold::{icf::IcfMode, linker::link_to};
mod common;
fn fixture(name: &str) -> PathBuf {
elf_fixture(name)
}
fn temp(name: &str) -> PathBuf {
std::env::temp_dir().join(name)
}
fn link_bytes(
inputs: &[PathBuf],
entry: &[u8],
n: usize,
label: &str,
) -> Vec<u8> {
let pool = ThreadPoolBuilder::new()
.num_threads(n)
.build()
.expect("thread pool builds");
let out = temp(label);
pool.install(|| link_to(inputs, &out, entry, false, IcfMode::None, false))
.expect("link succeeds");
fs::read(&out).expect("output readable")
}
#[test]
#[cfg_attr(miri, ignore = "Miri does not support file-backed memory mappings")]
fn got_link_is_byte_identical_across_thread_counts() {
let inputs = vec![fixture("min.o"), fixture("ext.o")];
let one = link_bytes(&inputs, b"entry", 1, "xold_par_min_1.out");
let eight = link_bytes(&inputs, b"entry", 8, "xold_par_min_8.out");
assert_eq!(
one, eight,
"GOT link output must not depend on thread count"
);
let _ = fs::remove_file(temp("xold_par_min_1.out"));
let _ = fs::remove_file(temp("xold_par_min_8.out"));
}
#[test]
#[cfg_attr(miri, ignore = "Miri does not support file-backed memory mappings")]
fn freestanding_link_is_byte_identical_across_thread_counts() {
let inputs = vec![fixture("prog.o"), fixture("start.o")];
let one = link_bytes(&inputs, b"_start", 1, "xold_par_prog_1.out");
let eight = link_bytes(&inputs, b"_start", 8, "xold_par_prog_8.out");
assert_eq!(
one, eight,
"freestanding link output must not depend on thread count"
);
let _ = fs::remove_file(temp("xold_par_prog_1.out"));
let _ = fs::remove_file(temp("xold_par_prog_8.out"));
}