Reputation: 21675
I'm trying to override some standard library functions using LD_PRELOAD. However, I notice that my version is never called for some functions, for example, the gettimeofday
one. I suspect gcc uses an inbuilt version for some of these functions.
Is there a way I can tell gcc to not use inbuilt standard library functions.
Upvotes: 5
Views: 669
Reputation: 39426
You are fixing the wrong problem. I think you have a problem in the code or in how you compile the preloaded library.
I have no problems whatsoever in interposing gettimeofday()
. Consider this libgettimeofday.c
:
#include <sys/time.h>
int gettimeofday(struct timeval *tv, struct timezone *tz __attribute__((unused)) )
{
tv->tv_sec = 1;
tv->tv_usec = 2;
return 0;
}
and this gettimeofday.c
:
#include <stdio.h>
#include <sys/time.h>
int main(void)
{
struct timeval t;
gettimeofday(&t, NULL);
printf("%ld.%06d\n", (long)t.tv_sec, (int)t.tv_usec);
return 0;
}
Compile using
gcc -W -Wall gettimeofday.c -o gettimeofday
gcc -W -Wall -fPIC libgettimeofday.c -ldl -shared -Wl,-soname,libgettimeofday.so -o libgettimeofday.so || exit $?
and test:
$ ./gettimeofday
1355243621.698927
$ LD_PRELOAD=./libgettimeofday.so ./gettimeofday
1.000002
Note that I tested this in both Ubuntu (64-bit) and CentOS 6.3 (32-bit), which use different C libraries.
Upvotes: 3
Reputation: 11095
Use the gcc switch -fno-builtin
. Quoting from the gcc manual:
-fno-builtin
Don't recognize built-in functions that do not begin with `_builtin' as prefix.
More details: http://gcc.gnu.org/onlinedocs/gcc-3.0/gcc_3.html#SEC7
Upvotes: 3