Source code:
#include <stdio.h>
#include <time.h>
#ifndef TEST
time_t time(time_t* tp){
if (tp)
*tp = 744847200;
return 744847200;
}
#endif
int main(int argc, char* argv[])
{
time_t now = time(NULL);
fputs(ctime(&now), stdout);
return 0;
}
- First, let's produce a binary:
$ gcc-8 time.c -o time -O2 -Wall -DTEST
$ ./time
Mon May 20 18:41:44 2019
- And build a standard shared library for
LD_PRELOAD
, and it works as expected.
$ gcc-8 time.c -o libtime.so.0 -O2 -Wall -shared -fPIC -Wl,-soname,libtime.so.0
$ LD_PRELOAD=`pwd`/libtime.so.0 ./time
Sun Aug 8 18:00:00 1993
- Build a
-static-pie
binary (gcc-8+ required), but it does not work withLD_PRELOAD
$ gcc-8 time.c -o libtime.so.1 -O2 -Wall -static-pie
$ objcopy -Gtime -Gmain time.so libtime.so.1 # keep `time` global
$ ./libtime.so.1
Sun Aug 8 18:00:00 1993
$ LD_PRELOAD=`pwd`/libtime.so.1 ./time
Mon May 20 18:46:51 2019
Note libtime.so.0
and libtime.so.1
both have elf->e_type = ET_DYN
, why LD_PRELOAD
doesn't work with libtime.so.1
(built with -static-pie
)?