My current solution for this (as discussed in comments with @KamilCuk):
cp $(cc -print-file-name=libc.a) libcmy.a
ar x libcmy open.lo close.lo read.lo stat.lo fstat.lo lseek.lo access.lo fopen.lo fileno.lo
objcopy --redefine-sym open=orig_open open.lo
objcopy --redefine-sym close=orig_close close.lo
objcopy --redefine-sym read=orig_read read.lo
objcopy --redefine-sym stat=orig_stat stat.lo
objcopy --redefine-sym fstat=orig_fstat fstat.lo
objcopy --redefine-sym lseek=orig_lseek lseek.lo
objcopy --redefine-sym access=orig_access access.lo
objcopy --redefine-sym fopen=orig_fopen fopen.lo
objcopy --redefine-sym fileno=orig_fileno fileno.lo
ar rs libcmy.a open.lo close.lo read.lo stat.lo fstat.lo lseek.lo access.lo fopen.lo fileno.lo
And then linking with libcmy.a
instead of plain -lc
.
Then in the code we can add prototypes for these orig functions and define new functions using the orig functions.