Override a library function for one run with LD_PRELOAD
Replace time() for one run with LD_PRELOAD and a shared object, on glibc 2.40 and Linux 6.12, without touching or rebuilding the program.
You want a dynamically linked program to see a different answer from a libc function, and you cannot change its source. The dynamic loader can put your own shared object ahead of libc in symbol lookup for a single run, so your definition wins. This example pins time() to a fixed value, which is how you make a clock-dependent program repeatable.
#include <time.h>
time_t time(time_t *t)
{
time_t fixed = 1700000000;
if (t)
*t = fixed;
return fixed;
}gcc -shared -fPIC -o fake_time.so fake_time.c
gcc -o app app.c
LD_PRELOAD=./fake_time.so ./app1700000000Here app.c only prints (long)time(NULL). The loader reads LD_PRELOAD (see ld.so(8)) and searches the listed objects first, so the program's call through the PLT lands in fake_time.so. Write ./fake_time.so, not a bare name: a name with no slash is searched in the library path and the load fails.
It does nothing for a statically linked binary, it is ignored for set-user-ID programs, and calls libc makes to its own internal functions are not interposed. To wrap rather than replace, look up the original with dlsym(RTLD_NEXT, "time").