#!/usr/bin/env python3 """Exercise the production archive preparer with real assembler/ar/linker inputs.""" import importlib.util from pathlib import Path import subprocess import tempfile import unittest ROOT = Path(__file__).resolve().parents[2] spec = importlib.util.spec_from_file_location("kernel_archive", ROOT / "tools/prepare_kernel_archive.py") module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) class KernelArchiveTest(unittest.TestCase): def setUp(self): self.temporary = tempfile.TemporaryDirectory() self.addCleanup(self.temporary.cleanup) self.root = Path(self.temporary.name) self.source = self.root / "source.a" self.destination = self.root / "kernel.a" def archive(self, objects): paths = [] for name, assembly in objects: path = self.root / name subprocess.run(["as", "-o", str(path)], input=assembly, text=True, check=True) paths.append(str(path)) subprocess.run(["ar", "rcs", str(self.source), *paths], check=True) def symbols(self, path): return subprocess.check_output(["nm", "--format=posix", str(path)], text=True) def test_link_imports_kernel_strlen_and_keeps_other_helpers(self): self.archive([ ("entry.o", ".text\n.globl entry\nentry: call strlen\ncall helper\nret\n"), ("compiler_builtins-any-hash.o", ".text\n.weak strlen\nstrlen: ret\n"), ("compiler_builtins-other.o", ".text\n.globl helper\nhelper: ret\n"), ]) original = self.source.read_bytes() module.prepare(self.source, self.destination) linked = self.root / "linked.o" subprocess.run(["ld", "-r", "--gc-sections", "-u", "entry", "--whole-archive", str(self.destination), "--no-whole-archive", "-o", str(linked)], check=True) symbols = self.symbols(linked) self.assertIn("strlen U", symbols) self.assertIn("helper T", symbols) self.assertNotIn("strlen W", symbols) self.assertEqual(self.source.read_bytes(), original) def test_archive_without_strlen_is_preserved(self): self.archive([("entry.o", ".text\n.globl entry\nentry: ret\n")]) module.prepare(self.source, self.destination) self.assertEqual(self.source.read_bytes(), self.destination.read_bytes()) def test_refuses_to_remove_another_export_or_unknown_provider(self): for name, extra in (("compiler_builtins-mixed.o", ".globl needed\nneeded: ret\n"), ("custom-strlen.o", "")): with self.subTest(name=name): self.source.unlink(missing_ok=True) self.archive([(name, ".text\n.weak strlen\nstrlen: ret\n" + extra)]) self.destination.write_bytes(b"previous good archive") original = self.source.read_bytes() with self.assertRaisesRegex(ValueError, "cannot safely omit"): module.prepare(self.source, self.destination) self.assertEqual(self.source.read_bytes(), original) self.assertEqual(self.destination.read_bytes(), b"previous good archive") def test_refuses_ambiguous_duplicate_members(self): name = "compiler_builtins-duplicate.o" self.archive([(name, ".text\n.weak strlen\nstrlen: ret\n")]) subprocess.run(["ar", "q", str(self.source), str(self.root / name)], check=True) with self.assertRaisesRegex(ValueError, "cannot safely omit"): module.prepare(self.source, self.destination) self.assertFalse(self.destination.exists()) def test_refuses_to_overwrite_rustc_archive(self): self.archive([("entry.o", ".text\n.globl entry\nentry: ret\n")]) original = self.source.read_bytes() with self.assertRaisesRegex(ValueError, "must differ"): module.prepare(self.source, self.source) self.assertEqual(self.source.read_bytes(), original) if __name__ == "__main__": unittest.main()