Take the 2-minute tour ×

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.

share|improve this question
you already have got an answer for your question itself. But there is little chance that gettimeofday is a builtin. So most probably you are looking in the wrong direction. –  Jens Gustedt Dec 11 '12 at 15:28
add comment

2 Answers

up vote 3 down vote accepted

Use the gcc switch -fno-builtin. Quoting from the gcc manual:


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

share|improve this answer
add comment

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

$ LD_PRELOAD=./libgettimeofday.so ./gettimeofday

Note that I tested this in both Ubuntu (64-bit) and CentOS 6.3 (32-bit), which use different C libraries.

share|improve this answer
You are right, I actually forgot to recompile my library after adding the gettimeofday function to it :)! –  user1018562 Dec 12 '12 at 14:29
add comment

Your Answer


