• [^] # Re: libdl

    Posté par . En réponse au message Une librairie pour convertir les adresses de programmes en nom de fonction ou de fichier et ligne ??. Évalué à 1.

    L'important est ce paragraphe du man : << If the executable was linked with the flag "-rdynamic" (or, synonymously, "--export-dynamic"), then the global symbols in the executable will also be used to resolve refer-ences in a dynamically loaded library. >> Si je compile le programme qui suit :
    /*
     * A compiler avec -rdynamic et linker avec libdl :
     * gcc -o dladdr -rdynamic dladdr.c -ldl
     * cf man dlopen :
     * If the executable was linked with
     * the flag "-rdynamic" (or, synonymously, "--export-dynamic"), then the
     * global symbols in the executable will also be used to resolve refer-
     * ences in a dynamically loaded library.
     */
    #ifndef _GNU_SOURCE
    #define _GNU_SOURCE 1
    #endif
    #include <stdio.h>
    #include <dlfcn.h>
    int main (void)
    {
     void *addr = (void *) (main + 0x42);
     Dl_info info;
     int ret = dladdr (addr, &info);
     printf ("lookup %p:\n", addr);
     printf (" retval: %u\n", ret);
     printf (" fname: %s\n", info.dli_fname);
     printf (" saddr: %p\n", info.dli_saddr);
     printf (" sname: %s\n", info.dli_sname);
     printf (" fbase: %p\n", info.dli_fbase);
     printf (" pretty: \"%s+0x%x\"\n",
    	 info.dli_sname, addr - info.dli_saddr);
     return 0;
    }
    
    de la façon suivante :
    gcc -o dladdr -rdynamic dladdr.c -ldl
    alors j'obtiens ce qu'on attend :
    [pollindd] work/svn/2006/divers >./dladdr 1032
    lookup 0x80486a6:
     retval: 1
     fname: ./dladdr
     saddr: 0x8048664
     sname: main
     fbase: 0x8048000
     pretty: "main+0x42"