use std::{
fs,
path::{Path, PathBuf},
process::Command,
};
use common::which;
use xold::{elf::ObjectFile, icf::IcfMode, linker::link_to};
mod common;
const A_SRC: &[u8] = b"double u = 1.5;\ndouble first(void) { return u; }\n";
const B_SRC: &[u8] = b"double u = 2.5;\ndouble second(void) { return u; }\n";
const STRONG_SRC: &[u8] =
b"double u = 7.0;\ndouble strong(void) { return u; }\n";
#[test]
#[cfg_attr(miri, ignore = "needs the host toolchain, which Miri cannot spawn")]
fn the_first_unique_definition_wins() {
let Some(dir) = workdir("first") else {
return;
};
let Some(clang) = which("clang") else {
return;
};
let Some(a) = compile(&dir, &clang, "a", A_SRC, STB_GNU_UNIQUE) else {
return;
};
let Some(b) = compile(&dir, &clang, "b", B_SRC, STB_GNU_UNIQUE) else {
return;
};
let out = dir.join("prog");
let res = link_to(&[a, b], &out, b"first", false, IcfMode::None, false);
assert!(res.is_ok(), "one copy is the whole point: {:?}", res.err());
let (bits, bind) = read_u(&out);
assert_eq!(bits, 1.5_f64.to_bits(), "the first object's copy is kept");
assert_eq!(bind, STB_GNU_UNIQUE, "the binding survives the link");
let _ = fs::remove_dir_all(&dir);
}
#[test]
#[cfg_attr(miri, ignore = "needs the host toolchain, which Miri cannot spawn")]
fn the_winner_follows_the_input_order() {
let Some(dir) = workdir("second") else {
return;
};
let Some(clang) = which("clang") else {
return;
};
let Some(a) = compile(&dir, &clang, "a", A_SRC, STB_GNU_UNIQUE) else {
return;
};
let Some(b) = compile(&dir, &clang, "b", B_SRC, STB_GNU_UNIQUE) else {
return;
};
let out = dir.join("prog");
let res = link_to(&[b, a], &out, b"second", false, IcfMode::None, false);
assert!(res.is_ok(), "the fixture must link: {:?}", res.err());
let (bits, _) = read_u(&out);
assert_eq!(bits, 2.5_f64.to_bits(), "the first object's copy is kept");
let _ = fs::remove_dir_all(&dir);
}
#[test]
#[cfg_attr(miri, ignore = "needs the host toolchain, which Miri cannot spawn")]
fn a_strong_definition_overrides_a_unique_one() {
let Some(dir) = workdir("strong") else {
return;
};
let Some(clang) = which("clang") else {
return;
};
let Some(unique) = compile(&dir, &clang, "u", B_SRC, STB_GNU_UNIQUE) else {
return;
};
let Some(strong) = compile(&dir, &clang, "s", STRONG_SRC, STB_GLOBAL)
else {
return;
};
for inputs in [vec![unique.clone(), strong.clone()], vec![strong, unique]] {
let out = dir.join("prog");
let res =
link_to(&inputs, &out, b"strong", false, IcfMode::None, false);
assert!(res.is_ok(), "global beats unique: {:?}", res.err());
let (bits, bind) = read_u(&out);
assert_eq!(bits, 7.0_f64.to_bits(), "the strong copy is kept");
assert_eq!(bind, STB_GLOBAL, "the winner's binding is what is written");
}
let _ = fs::remove_dir_all(&dir);
}
const STB_GNU_UNIQUE: u8 = 10;
const STB_GLOBAL: u8 = 1;
fn workdir(prefix: &str) -> Option<PathBuf> {
if which("clang").is_none() {
eprintln!("skipping gnu-unique {prefix}: clang unavailable");
return None;
}
let dir = std::env::temp_dir()
.join(format!("xold_gnuunique_{prefix}_{}", std::process::id()));
let _ = fs::remove_dir_all(&dir);
fs::create_dir_all(&dir).ok()?;
Some(dir)
}
fn compile(
dir: &Path,
clang: &Path,
name: &str,
src: &[u8],
bind: u8,
) -> Option<PathBuf> {
let src_path = dir.join(format!("{name}.c"));
let obj = dir.join(format!("{name}.o"));
fs::write(&src_path, src).ok()?;
let built = Command::new(clang)
.args(["--target=x86_64-linux-gnu", "-c", "-fno-pie"])
.arg(&src_path)
.arg("-o")
.arg(&obj)
.status()
.ok()?
.success();
if !built {
eprintln!("skipping gnu-unique: clang cannot build {name}");
return None;
}
if bind != STB_GLOBAL {
patch_binding(&obj, b"u", bind)
.expect("the object carries a row for u");
}
Some(obj)
}
fn patch_binding(obj: &Path, name: &[u8], bind: u8) -> Option<()> {
let mut bytes = fs::read(obj).ok()?;
let shoff = usize::try_from(read_u64(&bytes, 0x28)?).unwrap_or(0);
let shnum = read_u16(&bytes, 0x3c)?;
let sh = |i: usize| shoff + i * 64;
let symtab = (1..usize::from(shnum))
.find(|&i| read_u32(&bytes, sh(i) + 4) == Some(2))?;
let off = read_u64(&bytes, sh(symtab) + 0x18)?;
let size = read_u64(&bytes, sh(symtab) + 0x20)?;
let link = read_u32(&bytes, sh(symtab) + 0x28)? as usize;
let stroff = read_u64(&bytes, sh(link) + 0x18)?;
for at in (0..usize::try_from(size).unwrap_or(0)).step_by(24) {
let row = usize::try_from(off).unwrap_or(0) + at;
let st_name = usize::try_from(read_u32(&bytes, row)?).ok()?;
let start = usize::try_from(stroff).unwrap_or(0) + st_name;
let end = bytes.get(start..)?.iter().position(|&b| b == 0)? + start;
if &bytes[start..end] != name {
continue;
}
let info = bytes[row + 4];
bytes[row + 4] = (bind << 4) | (info & 0xf);
fs::write(obj, &bytes).ok()?;
return Some(());
}
None
}
fn read_u(out: &Path) -> (u64, u8) {
let bytes = fs::read(out).expect("read the image");
let image = ObjectFile::parse(&bytes).expect("parse the image");
let table = image.symbol_table().ok().flatten().expect(".symtab");
let sym = table
.syms
.iter()
.find(|s| table.name(s) == b"u")
.expect("u is defined");
let shndx = usize::from(sym.st_shndx.get());
let sect = image.sections().get(shndx).expect("the holding section");
let data = image.section_data(sect).expect("the section's bytes");
let value = sym.st_value.get().wrapping_sub(sect.sh_addr.get());
let at = usize::try_from(value).expect("u sits in its section");
let cell = data.get(at..at + 8).expect("eight bytes of u");
(
u64::from_le_bytes(<[u8; 8]>::try_from(cell).expect("a cell")),
sym.st_info >> 4,
)
}
fn read_u16(bytes: &[u8], at: usize) -> Option<u16> {
bytes
.get(at..at + 2)
.and_then(|c| <[u8; 2]>::try_from(c).ok())
.map(u16::from_le_bytes)
}
fn read_u32(bytes: &[u8], at: usize) -> Option<u32> {
bytes
.get(at..at + 4)
.and_then(|c| <[u8; 4]>::try_from(c).ok())
.map(u32::from_le_bytes)
}
fn read_u64(bytes: &[u8], at: usize) -> Option<u64> {
bytes
.get(at..at + 8)
.and_then(|c| <[u8; 8]>::try_from(c).ok())
.map(u64::from_le_bytes)
}