/* * 810_dyn — end-to-end test of w6l's dynamic linker. * * Drives `ww build -L /usr/lib -l c` over a small ww program that * binds libc symbols via @symbol, then runs the produced binary and * checks the exit status. Verifies: * * - the .so loader (dyn.c) reads ET_DYN and extracts exports * - PLT/GOT generation and the JUMP_SLOT relocation * - PT_INTERP/PT_DYNAMIC emission * - symbol versioning (.gnu.version + .gnu.version_r) — clock_gettime * has both GLIBC_2.2.5 (compat) and GLIBC_2.17 (default) on glibc; * the loader requires the right .gnu.version_r entry to bind * the default vDSO-aware impl. * * Reports the harness skip status on systems without a supported libc.so.6. */ #include #include #include #include #include #include #include "wwtestpkg.h" static int runwait(const char *cmd) { int rc = system(cmd); if (rc == -1) return -1; if (WIFEXITED(rc)) return WEXITSTATUS(rc); return 1; } struct row { const char *src; int want_exit; }; static const struct row rows[] = { /* 1. _exit via dynamically-linked libc. Exit code is the plumbing * of choice — proves the PLT entry calls into libc.so.6. */ { "@symbol(\"_exit\") fn libc_exit(c: i32) void;\n" "export fn main() i32 = { libc_exit(42); return 0; };", 42 }, /* 2. Multiple dyn syms in one binary: write + _exit. */ { "@symbol(\"write\") fn libc_write(fd: i32, b: *u8, n: u64) i64;\n" "@symbol(\"_exit\") fn libc_exit(c: i32) void;\n" "export fn main() i32 = {\n" " libc_write(1, \"x\".ptr, 1u64);\n" " libc_exit(7);\n" " return 0;\n" "};", 7 }, /* 3. Symbol versioning: clock_gettime. Without DT_VERSYM/VERNEED * pointing at a Vernaux entry for GLIBC_2.17, the loader either * picks the wrong impl or fails outright on modern glibc. */ { "@symbol(\"clock_gettime\") fn clock_gettime(c: i32, ts: *u8) i32;\n" "@symbol(\"_exit\") fn libc_exit(c: i32) void;\n" "export fn main() i32 = {\n" " let ts: [16]u8;\n" " let r: i32 = clock_gettime(1, ts.ptr);\n" /* CLOCK_MONOTONIC */ " libc_exit(r);\n" " return 0;\n" "};", 0 }, /* 4. Take the address of an FFI binding and call it indirectly. * Codegen must apply @symbol resolution at the LEAQ site (so the * stub address is `write`, not the ww-side ident `libc_write`), * and the local-variable call must be CALL *AX, not CALL fp(SB). */ { "@symbol(\"write\") fn libc_write(fd: i32, b: *u8, n: u64) i64;\n" "@symbol(\"_exit\") fn libc_exit(c: i32) void;\n" "export fn main() i32 = {\n" " let fp: fn(fd: i32, b: *u8, n: u64) i64 = libc_write;\n" " fp(1, \"X\".ptr, 1u64);\n" " libc_exit(5);\n" " return 0;\n" "};", 5 }, /* 5. Writable global on the dyn-link path. .data now lives in the * shared R+W segment with .got.plt and .dynamic; read and write * exercise the new layout's data_va. */ { "@symbol(\"_exit\") fn libc_exit(c: i32) void;\n" "let counter: i32 = 0;\n" "export fn main() i32 = {\n" " counter = 11;\n" " libc_exit(counter);\n" " return 0;\n" "};", 11 }, /* 6. Zero-init slot under dyn-link: the BSS trailing-zero scan * should still produce a valid p_filesz