This is the mail archive of the libc-alpha@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

dlsym() questions


Hello all:

I would like to override the stat() function call using an LD_PRELOAD
library. I've done similar work before, remembered it to be relatively
straight-forward (overriding other library calls), but I can't quite
figure out what I'm doing wrong in this instance. Perhaps someone can
point me in the correct direction.

I've provided some simple test code (not even at the shared library
stage).  It can be compiled with
  $ make LDFLAGS=-ldl hooked_stat_call 

If it is run with a command-line argument, then the stat() routine
(linked at application building) is run.  Without a command-line
argument, dlsym() is used to look up the stat() routine address.

First, I don't understand why I must use __xstat as the name lookup.
Previously, I've always used the simple library name.  If I try dlsym()ing
'fstat' I receive a NULL address.  Can someone tell me if I'm using 
the correct name?

Secondly, __xstat() return -1 on my system, not 0 (which is the return
value if I run the system linked stat() call.  Here is some output:

  ssh-agent $ ./hooked_stat_call 
  dlsym-stat: Invalid argument
  hooked( ".", ... ) returns -1
  ssh-agent $ ./hooked_stat_call use_linked_stat
  hooked( ".", ... ) returns 0

If __xstat is the correct symbol for stat, then I'm not sure why one
call works and the other does not.

TIA.

-- 
Keith Hellman                             #include <disclaimer.h>
khellman@mcprogramming.com                from disclaimer import standard
khellman@mines.edu
                                   -*-                                    
                    public key @ pgp.mit.edu B5354B76                     
    Y!M: mcprogramming                           AIM/ICQ: 485403897       
                     gtalk: jabber@mcprogramming.com                      
                                   -*-                                    

"One World, one Web, one Program." - Microsoft(R) promotional ad
"Ein Volk, ein Reich, ein Fuhrer." - Adolf Hitler
/***
 * test app for stat preloading
 */

#include <dlfcn.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <stdio.h>

int hooked( const char* n, struct stat* s, void* stat_function )
{
	static int (*f)( const char*, struct stat* ) = NULL;
	if( stat_function ) {
		f = stat_function;
	} else if( !f ) {
		f = dlsym( RTLD_NEXT, "__xstat" );
		if( !f ) {
			printf( "f = %p: %s\n", f, dlerror() );
		}
	}
	int r = f( n, s );
	if(r) {
		perror(stat_function?"stat":"dlsym-stat");
	}
	return r;
}

int main( int argc, char* argv[] )
{
	struct stat s;
	int r = hooked( ".", &s, argc > 1 ? stat : NULL );
	printf( "hooked( \".\", ... ) returns %d\n", r );
	return 0;
}

Attachment: signature.asc
Description: Digital signature


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]