maccorin
06-01-2004, 06:43 PM
does anybody know a way to change the order that libraries are initialized, as in any function w/ the __attribute__((constructor)) is run.
My problems is this, i have a library with some initialization code that looks like
char*(*___libc_strcpy)(char *, const char *) = NULL;
char*(*___libc_strncpy)(char *, const char *, size_t) = NULL;
int(*___libc_strcmp)(const char *, const char *) = NULL;
size_t(*___libc_strlen)(const char *) = NULL;
char*(*___libc_strstr)(const char *, const char *) = NULL;
....
void __attribute__((constructor)) constructor ( )
{
....
___libc_strstr = dlsym(RTLD_NEXT, "strstr");
___libc_strcpy = dlsym(RTLD_NEXT, "strcpy");
___libc_strncpy = dlsym(RTLD_NEXT, "strncpy");
___libc_strcmp = dlsym(RTLD_NEXT, "strcmp");
___libc_strlen = dlsym(RTLD_NEXT, "strlen");
....
}
i use the library by the LD_PRELOAD env var, I _thought_ that LD_PRELOAD was loaded before anything (except maybe libc). Well, it works fine for most programs. But when I run a program that it is linked to libpthread ('ls' for instance), it segfaults. Looking in gdb i found
Program received signal SIGSEGV, Segmentation fault.
0x00000000 in ?? ()
(gdb) bt
#0 0x00000000 in ?? ()
#1 0x40016d7f in strstr (a=0xbffff23c "#4 Sun May 16 18:38:24 EDT 2004",
b=0x40177624 "SMP") at trace.c:98
#2 0x401716a1 in longjmp () from /lib/libpthread.so.0
#3 0x401775b5 in pthread_atfork () from /lib/libpthread.so.0
#4 0x4016dbd9 in ?? () from /lib/libpthread.so.0
#5 0x40014d80 in ?? () from /lib/ld-linux.so.2
#6 0x40000aa4 in ?? () from /lib/ld-linux.so.2
#7 0xbffff5b4 in ?? ()
#8 0x4000a98e in _dl_catch_error () from /lib/ld-linux.so.2
Previous frame identical to this frame (corrupt stack?)
so obviously libpthread is being initialized first, and calling _my_ strstr(), but that calls ___libc_strstr(), which is still NULL...
Does anyone know of a decent workaround for this?
My problems is this, i have a library with some initialization code that looks like
char*(*___libc_strcpy)(char *, const char *) = NULL;
char*(*___libc_strncpy)(char *, const char *, size_t) = NULL;
int(*___libc_strcmp)(const char *, const char *) = NULL;
size_t(*___libc_strlen)(const char *) = NULL;
char*(*___libc_strstr)(const char *, const char *) = NULL;
....
void __attribute__((constructor)) constructor ( )
{
....
___libc_strstr = dlsym(RTLD_NEXT, "strstr");
___libc_strcpy = dlsym(RTLD_NEXT, "strcpy");
___libc_strncpy = dlsym(RTLD_NEXT, "strncpy");
___libc_strcmp = dlsym(RTLD_NEXT, "strcmp");
___libc_strlen = dlsym(RTLD_NEXT, "strlen");
....
}
i use the library by the LD_PRELOAD env var, I _thought_ that LD_PRELOAD was loaded before anything (except maybe libc). Well, it works fine for most programs. But when I run a program that it is linked to libpthread ('ls' for instance), it segfaults. Looking in gdb i found
Program received signal SIGSEGV, Segmentation fault.
0x00000000 in ?? ()
(gdb) bt
#0 0x00000000 in ?? ()
#1 0x40016d7f in strstr (a=0xbffff23c "#4 Sun May 16 18:38:24 EDT 2004",
b=0x40177624 "SMP") at trace.c:98
#2 0x401716a1 in longjmp () from /lib/libpthread.so.0
#3 0x401775b5 in pthread_atfork () from /lib/libpthread.so.0
#4 0x4016dbd9 in ?? () from /lib/libpthread.so.0
#5 0x40014d80 in ?? () from /lib/ld-linux.so.2
#6 0x40000aa4 in ?? () from /lib/ld-linux.so.2
#7 0xbffff5b4 in ?? ()
#8 0x4000a98e in _dl_catch_error () from /lib/ld-linux.so.2
Previous frame identical to this frame (corrupt stack?)
so obviously libpthread is being initialized first, and calling _my_ strstr(), but that calls ___libc_strstr(), which is still NULL...
Does anyone know of a decent workaround for this?