#![allow(clippy::similar_names)]
use std::{
fs,
path::{Path, PathBuf},
process::Command,
};
use common::{crt_file, interpreter, libc_so, which};
use xold::{
elf::{ObjectFile, constants::*},
icf::IcfMode,
linker::link_dyn_exec,
};
mod common;
const CTOR_SRC: &[u8] = b"#include <stdio.h>\n\
#include <stdlib.h>\n\
#include <string.h>\n\
static char *buf;\n\
__attribute__((constructor)) static void init(void){\n\
buf = malloc(4); strcpy(buf, \"ok\");\n\
}\n\
int main(void){ printf(\"buf=%s\\n\", buf); return buf == NULL; }\n";
const DTOR_SRC: &[u8] = b"#include <unistd.h>\n\
__attribute__((destructor)) static void fini(void){\n\
static const char m[] = \"dtor-ran\";\n\
(void)!write(2, m, sizeof(m) - 1);\n\
}\n\
int main(void){ return 0; }\n";
const PREINIT_SRC: &[u8] = b"#include <stdio.h>\n\
static char order[8];\n\
static int at;\n\
static void preinit(int argc, char **argv, char **envp){\n\
(void)argc; (void)argv; (void)envp;\n\
if (at < 7) order[at++] = 'p';\n\
}\n\
__attribute__((section(\".preinit_array\"), used))\n\
static void (*preinit_slot)(int, char **, char **) = preinit;\n\
__attribute__((constructor)) static void ctor(void){\n\
if (at < 7) order[at++] = 'i';\n\
}\n\
int main(void){\n\
if (at < 7) order[at++] = 'm';\n\
printf(\"order=%s\\n\", order); return 0;\n\
}\n";
const PRIO_HI_SRC: &[u8] = b"#include <stdio.h>\n\
__attribute__((constructor(300))) static void c300(void){ puts(\"300\"); }\n\
__attribute__((constructor)) static void hi(void){ puts(\"hi\"); }\n";
const PRIO_LO_SRC: &[u8] = b"#include <stdio.h>\n\
__attribute__((constructor(101))) static void c101(void){ puts(\"101\"); }\n\
__attribute__((constructor)) static void lo(void){ puts(\"lo\"); }\n";
const PRIO_MAIN_SRC: &[u8] =
b"#include <stdio.h>\nint main(void){ puts(\"main\"); return 0; }\n";
const PRIO_EXPECTED: &str = "101\n300\nhi\nlo\nmain\n";
fn compile(src: &[u8], obj: &Path, args: &[&str]) -> Option<()> {
let clang = which("clang")?;
let src_path = obj.with_extension("c");
fs::write(&src_path, src).expect("write source");
let ok = Command::new(clang)
.args(args)
.arg(&src_path)
.arg("-o")
.arg(obj)
.status()
.ok()?
.success();
let _ = fs::remove_file(&src_path);
ok.then_some(())
}
fn workdir(prefix: &str) -> PathBuf {
let dir = std::env::temp_dir().join(format!("xold_ctor_{prefix}"));
let _ = fs::remove_dir_all(&dir);
let _ = fs::create_dir_all(&dir);
dir
}
struct Harness {
crt1: PathBuf,
crti: PathBuf,
crtn: PathBuf,
libc: PathBuf,
interp: Vec<u8>,
}
impl Harness {
fn detect() -> Option<Self> {
if which("clang").is_none() {
eprintln!("skipping init_array tests: clang unavailable");
return None;
}
let crt1 = crt_file("crt1.o")?;
let crti = crt_file("crti.o")?;
let crtn = crt_file("crtn.o")?;
let libc = libc_so()?;
let interp = interpreter()?;
Some(Self {
crt1,
crti,
crtn,
libc,
interp,
})
}
fn link(&self, main_obj: &Path, prog: &Path) {
self.link_all(std::slice::from_ref(&main_obj.to_path_buf()), prog);
}
fn link_all(&self, objs: &[PathBuf], prog: &Path) {
let mut inputs = objs.to_vec();
inputs.extend([
self.crti.clone(),
self.crt1.clone(),
self.crtn.clone(),
self.libc.clone(),
]);
link_dyn_exec(
&inputs,
prog,
b"_start",
&self.interp,
false,
IcfMode::None,
false,
)
.expect("xold libc link must succeed");
}
}
#[test]
#[cfg_attr(miri, ignore = "needs the host toolchain, which Miri cannot spawn")]
fn constructor_runs_non_pic_exec() {
let Some(h) = Harness::detect() else {
return;
};
let dir = workdir("np");
let main_o = dir.join("np.o");
compile(CTOR_SRC, &main_o, &["--target=x86_64-linux-gnu", "-c"])
.expect("host clang compiles non-PIC ctor");
let prog = dir.join("np_prog");
h.link(&main_o, &prog);
let bytes = fs::read(&prog).expect("read output");
let obj = ObjectFile::parse(&bytes).expect("valid ELF");
assert_eq!(
obj.header().e_type.get(),
ET_EXEC,
"non-PIC must be ET_EXEC"
);
let out = Command::new(&prog)
.output()
.expect("linked program must be runnable");
assert!(
out.status.success(),
"non-PIC ctor should exit 0, got {:?}",
out.status
);
assert_eq!(
String::from_utf8_lossy(&out.stdout),
"buf=ok\n",
"constructor should have set buf before main"
);
}
#[test]
#[cfg_attr(miri, ignore = "needs the host toolchain, which Miri cannot spawn")]
fn constructor_runs_pic_dyn() {
let Some(h) = Harness::detect() else {
return;
};
let dir = workdir("pi");
let main_o = dir.join("pi.o");
compile(
CTOR_SRC,
&main_o,
&["--target=x86_64-linux-gnu", "-fPIE", "-c"],
)
.expect("host clang compiles -fPIE ctor");
let prog = dir.join("pi_prog");
h.link(&main_o, &prog);
let bytes = fs::read(&prog).expect("read output");
let obj = ObjectFile::parse(&bytes).expect("valid ELF");
assert_eq!(
obj.header().e_type.get(),
ET_DYN,
"PIC must be ET_DYN (PIE)"
);
let out = Command::new(&prog)
.output()
.expect("linked program must be runnable");
assert!(
out.status.success(),
"PIC ctor should exit 0, got {:?}",
out.status
);
assert_eq!(
String::from_utf8_lossy(&out.stdout),
"buf=ok\n",
"constructor should have set buf before main"
);
}
#[test]
#[cfg_attr(miri, ignore = "needs the host toolchain, which Miri cannot spawn")]
fn destructor_runs_at_exit() {
let Some(h) = Harness::detect() else {
return;
};
let dir = workdir("dt");
let main_o = dir.join("dt.o");
compile(
DTOR_SRC,
&main_o,
&["--target=x86_64-linux-gnu", "-fPIE", "-c"],
)
.expect("host clang compiles destructor");
let prog = dir.join("dt_prog");
h.link(&main_o, &prog);
let out = Command::new(&prog)
.output()
.expect("linked program must be runnable");
assert!(
out.status.success(),
"destructor program should exit 0, got {:?}",
out.status
);
assert!(
String::from_utf8_lossy(&out.stderr).contains("dtor-ran"),
"destructor should have written its marker to stderr"
);
}
#[test]
#[cfg_attr(miri, ignore = "needs the host toolchain, which Miri cannot spawn")]
fn init_array_dynamic_tags_match_section() {
let Some(h) = Harness::detect() else {
return;
};
let dir = workdir("st");
let main_o = dir.join("st.o");
compile(
CTOR_SRC,
&main_o,
&["--target=x86_64-linux-gnu", "-fPIE", "-c"],
)
.expect("host clang compiles ctor");
let prog = dir.join("st_prog");
h.link(&main_o, &prog);
let bytes = fs::read(&prog).expect("read output");
let obj = ObjectFile::parse(&bytes).expect("valid ELF");
let init_sec = obj
.sections()
.iter()
.find(|s| obj.section_name(s) == b".init_array")
.expect(".init_array section must exist");
assert_eq!(init_sec.sh_type.get(), SHT_INIT_ARRAY);
assert_eq!(
init_sec.sh_flags.get() & (SHF_ALLOC | SHF_WRITE),
SHF_ALLOC | SHF_WRITE,
".init_array must be allocated and writable"
);
assert_eq!(
init_sec.sh_size.get(),
8,
".init_array holds one 8-byte function pointer"
);
let tags = dynamic_tags(&bytes);
let init_addr = tags
.iter()
.find(|(t, _)| *t == DT_INIT_ARRAY)
.map(|(_, v)| *v)
.expect("DT_INIT_ARRAY tag must exist");
let init_size = tags
.iter()
.find(|(t, _)| *t == DT_INIT_ARRAYSZ)
.map(|(_, v)| *v)
.expect("DT_INIT_ARRAYSZ tag must exist");
assert_eq!(
init_addr,
init_sec.sh_addr.get(),
"DT_INIT_ARRAY must name the .init_array address"
);
assert_eq!(
init_size,
init_sec.sh_size.get(),
"DT_INIT_ARRAYSZ must match the .init_array byte size"
);
let ref_tag = system_linker_init_array(CTOR_SRC, &dir);
if let Some(ref_addr) = ref_tag {
assert_ne!(ref_addr, 0, "system linker DT_INIT_ARRAY must be non-zero");
}
}
#[test]
#[cfg_attr(miri, ignore = "needs the host toolchain, which Miri cannot spawn")]
fn preinit_runs_before_init() {
let Some(h) = Harness::detect() else {
return;
};
let dir = workdir("pre");
let main_o = dir.join("pre.o");
compile(
PREINIT_SRC,
&main_o,
&["--target=x86_64-linux-gnu", "-fPIE", "-c"],
)
.expect("host clang compiles preinit source");
let prog = dir.join("pre_prog");
h.link(&main_o, &prog);
let out = Command::new(&prog)
.output()
.expect("linked program must be runnable");
assert!(
out.status.success(),
"preinit program should exit 0, got {:?}",
out.status
);
assert_eq!(
String::from_utf8_lossy(&out.stdout),
"order=pim\n",
"preinit must run before the constructor and before main"
);
}
#[test]
#[cfg_attr(miri, ignore = "needs the host toolchain, which Miri cannot spawn")]
fn preinit_array_dynamic_tags_match_section() {
let Some(h) = Harness::detect() else {
return;
};
let dir = workdir("prest");
let main_o = dir.join("prest.o");
compile(
PREINIT_SRC,
&main_o,
&["--target=x86_64-linux-gnu", "-fPIE", "-c"],
)
.expect("host clang compiles preinit source");
let prog = dir.join("prest_prog");
h.link(&main_o, &prog);
let bytes = fs::read(&prog).expect("read output");
let obj = ObjectFile::parse(&bytes).expect("valid ELF");
let section = |want: &[u8]| {
obj.sections()
.iter()
.find(|s| obj.section_name(s) == want)
.expect("section must exist")
};
let pre = section(b".preinit_array");
assert_eq!(pre.sh_type.get(), SHT_PREINIT_ARRAY);
assert_eq!(
pre.sh_flags.get() & (SHF_ALLOC | SHF_WRITE),
SHF_ALLOC | SHF_WRITE,
".preinit_array must be allocated and writable"
);
assert_eq!(
pre.sh_size.get(),
8,
".preinit_array holds one 8-byte function pointer"
);
assert!(
pre.sh_addr.get() < section(b".init_array").sh_addr.get(),
".preinit_array must precede .init_array"
);
let tags = dynamic_tags(&bytes);
let tag = |want: i64| {
tags.iter()
.find(|(t, _)| *t == want)
.map(|(_, v)| *v)
.expect("dynamic tag must exist")
};
assert_eq!(
tag(DT_PREINIT_ARRAY),
pre.sh_addr.get(),
"DT_PREINIT_ARRAY must name the .preinit_array address"
);
assert_eq!(
tag(DT_PREINIT_ARRAYSZ),
pre.sh_size.get(),
"DT_PREINIT_ARRAYSZ must match the .preinit_array byte size"
);
}
#[test]
#[cfg_attr(miri, ignore = "needs the host toolchain, which Miri cannot spawn")]
fn init_priority_orders_constructors_across_files() {
let Some(h) = Harness::detect() else {
return;
};
let dir = workdir("prio");
let args = &["--target=x86_64-linux-gnu", "-c"];
let hi = dir.join("prio_hi.o");
let lo = dir.join("prio_lo.o");
let main_o = dir.join("prio_main.o");
compile(PRIO_HI_SRC, &hi, args).expect("host clang compiles hi");
compile(PRIO_LO_SRC, &lo, args).expect("host clang compiles lo");
compile(PRIO_MAIN_SRC, &main_o, args).expect("host clang compiles main");
let prog = dir.join("prio_prog");
h.link_all(&[hi.clone(), lo.clone(), main_o.clone()], &prog);
let out = Command::new(&prog)
.output()
.expect("linked program must be runnable");
assert!(
out.status.success(),
"priority program should exit 0, got {:?}",
out.status
);
assert_eq!(
String::from_utf8_lossy(&out.stdout),
PRIO_EXPECTED,
"constructors must run in init-priority order"
);
let swapped = dir.join("prio_prog_swapped");
h.link_all(&[lo, hi, main_o], &swapped);
let out = Command::new(&swapped)
.output()
.expect("linked program must be runnable");
assert_eq!(
String::from_utf8_lossy(&out.stdout),
"101\n300\nlo\nhi\nmain\n",
"priority order must hold whichever object comes first"
);
}
fn dynamic_tags(bytes: &[u8]) -> Vec<(i64, u64)> {
let Ok(obj) = ObjectFile::parse(bytes) else {
return Vec::new();
};
let Some(dyn_sec) = obj
.sections()
.iter()
.find(|s| obj.section_name(s) == b".dynamic")
else {
return Vec::new();
};
let Ok(data) = obj.section_data(dyn_sec) else {
return Vec::new();
};
let mut out = Vec::new();
for chunk in data.chunks(16) {
if chunk.len() < 16 {
break;
}
let tag = i64::from_le_bytes(chunk[..8].try_into().unwrap_or([0; 8]));
if tag == DT_NULL {
break;
}
let val = u64::from_le_bytes(chunk[8..16].try_into().unwrap_or([0; 8]));
out.push((tag, val));
}
out
}
fn system_linker_init_array(src: &[u8], dir: &Path) -> Option<u64> {
let clang = which("clang")?;
let src_path = dir.join("ref.c");
fs::write(&src_path, src).expect("write source");
let ref_prog = dir.join("ref_prog");
let ok = Command::new(clang)
.arg(&src_path)
.arg("-o")
.arg(&ref_prog)
.status()
.is_ok();
let _ = fs::remove_file(&src_path);
if !ok {
return None;
}
let bytes = fs::read(&ref_prog).expect("read reference");
dynamic_tags(&bytes)
.into_iter()
.find(|(t, _)| *t == DT_INIT_ARRAY)
.map(|(_, v)| v)
}