views:

126

answers:

0

I'm on Windows XP with Cygwin, and I've compiled a DLL that overrides some pthread functions, e.g. pthread_create. LD_PRELOADing this library into a Cygwin application should make this application use my functions instead of standard ones. (See [1].) However it does not happen.

I know that the library is loaded, because I've put printf in the constructor function of the library, and I can see that message in the console. But the program is still using original version of pthread_create. On Linux the LD_PRELOAD trick with same library works without a glitch.

Are there additional Cygwin-specific steps needed?

Library source:

#include <stdio.h>

static void __attribute((constructor)) init() {
    printf("hello from agent\n");
}

int pthread_create(pthread_t *arg0, const pthread_attr_t *arg1, void *(*arg2)(void *), void *arg3) {
    printf("pthread_create()\n");
}

Library compilation:

gcc.exe -c -g -fPIC -o agent.o agent.c
gcc.exe -shared -o libAgent.dll -fPIC agent.o

Test program:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

void* threadfunc(void* p) {
    sleep(1);
}
int main(int argc, char** argv) {
    pthread_t t;
    pthread_create(&t, NULL, threadfunc, NULL);
    pthread_join(t, NULL);
    return (EXIT_SUCCESS);
}

Test program compilation:

gcc.exe -c -o main.o main.c
gcc.exe -o prog main.o -lpthread

Run attempt:

$ LD_PRELOAD=./libAgent.dll ./prog.exe
hello from agent