Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Dear monks,

i'm trying to retrieve library symbols addresses by name using DynaLoader.

#!/usr/bin/perl # use DynaLoader; push(@dl_library_path,'/lib'); my $symbol=$ARGV[0]; @dl_resolve_using = DynaLoader::dl_findfile(qw(-ldl -lc)); my $lib_name='/lib/libc.so.6'; my $libref=DynaLoader::dl_load_file($lib_name,0x00001); if (not defined $libref) { die "load lib: $! \n";} my $symref =DynaLoader::dl_find_symbol($libref, $symbol); #$symref -= 0x74000; printf "%s : %4x \n",$symbol,$symref;
I have compared the result with a C programm using this function"
int search(char *pattern) { void *p; int addr; /* dlopen() the main program */ if ( !(p = dlopen(NULL, RTLD_LAZY)) ) { fprintf(stderr, "%s\n", dlerror()); exit(-1); } /* search for the pattern */ if ( !(addr = (int)dlsym(p, pattern)) ) { fprintf(stderr, "%s\n", dlerror()); exit(-1); } dlclose(p); return(addr); }

i have got an offset beetween the 2 results..

$ ./lib.pl system system : 0x400c52f0 $ ./libcs -s system system: 0x400512f0 $ ./lib.pl printf printf : 0x400d0d44 $ ./libcs -s printf printf: 0x4005cd44
here the value is 0x74000
may someone explain what i'm doing wrong ?

Thanks

Replies are listed 'Best First'.
Re: retrieve libs symbols with DynaLoader
by chromatic (Archbishop) on Jul 12, 2006 at 05:54 UTC

    The documentation for DynaLoader says:

    The exact manner in which the address is returned in $symref is not currently defined.

    I take that to mean "Don't rely on it." That bothers me a bit, but the only time I've used it, I've passed it immediately to DynaLoader::dl_install_xsub(), as the documentation suggests.

      I take that to mean "Don't rely on it."

      how can i be sure to catch the right symbol/function ?

        Write a test case?