I am having problem using LD_PRELOAD to overwrite the __clone() function. It gives me a segmentation fault when executing the original __clone() and pid returned = -1 (unsuccessful). I tried this on both kernel 2.4 and 2.6 (both RedHat and SUSE). My gcc version is 4.1.0. Here is my simple library code. Please help and thanks in advance.

I also include a test program at the end. It is simply a small program that creates thread (using pthread_create() which in turn calls the __clone()).

To try this, following the steps below:

(1) Compile the shared library
gcc -shared -fPIC clone_preload.c -o clone_preload.so -lpthread -ldl
(2) Compile the test program
gcc -lpthread -o pthread2 pthread2.c
(3) Set the LD_PRELOAD environment
setenv LD_PRELOAD ./clone_preload.so
(4) Run the test program
(you should see that pid = -1 (failure) and also a segmentation fault here)

/* Compilation example:
* gcc -shared -fPIC clone_preload.c -o clone_preload.so -lpthread -ldl
* Usage example:
* LD_PRELOAD=./clone_preload.so your_program

#define _GNU_SOURCE
#include <sys/time.h>
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <pthread.h>
#include <sched.h>

static int (*old_clone) (int (*fn) (void * arg),
void *child_stack,
int flags,
void *arg);
/* lib init */
void libinit(void) __attribute__((constructor));

void libinit(void)
old_clone = dlsym(RTLD_NEXT, "__clone");
if(old_clone == NULL)
char *error = dlerror();
if(error == NULL)
error = "old_clone is NULL";
fprintf(stderr, "%s\n", error);
fprintf(stderr, "Inside libinit ....\n");

int __clone (int (*fn) (void *arg),
void *child_stack,
int flags,
void *arg)
int pid;
pid = (*old_clone) (fn, child_stack, flags, arg);
fprintf(stderr, "pid = %d\n", pid);
return pid;

Test program: pthread2.c
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

void *print_message_function( void *ptr );

pthread_t thread1;
char *message1 = "Thread 1";
int iret1;

iret1 = pthread_create( &thread1, NULL, print_message_function, (void*) message1);

pthread_join( thread1, NULL);


void *print_message_function( void *ptr )
char *message;
message = (char *) ptr;
printf("%s \n", message