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;
}
[^] # Re: libdl
Posté par David Decotigny . En réponse au message Une librairie pour convertir les adresses de programmes en nom de fonction ou de fichier et ligne ??. Évalué à 1.
/* * 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 : alors j'obtiens ce qu'on attend :