Transferred from: http://www.360doc.com/content/17/1012/11/48326749_694292472.shtml
In addition, DL_ITERATE_PHDR can find all the symbols in the current process, and each one will invoke the callback function you specified.
The following code example how to use DL_ITERATE_PHDR and dladdr
#define _gnu_source
#include <link.h>
#include <stdlib.h>
#include <stdio.h>
static int
Callback (struct Dl_phdr_info *info, size_t size, void *data)
{
Int J;
printf ("name=%s (%d segments) \ n", Info->dlpi_name, Info->dlpi_phnum);
for (j = 0; J < info->dlpi_phnum; J + +) {
void* addr = (void *) (info->dlpi_addr + info->dlpi_phdr[j].p_vaddr);
printf ("\t\t header%2d:address=%10p", J, addr);
Dl_info Dlinfo;
Dladdr (addr, &dlinfo);
printf ("\ t%s:%s\n", Dlinfo.dli_fname, Dlinfo.dli_sname);
}
return 0;
}
Int
Main (int argc, char *argv[])
{
DL_ITERATE_PHDR (callback, NULL);
Exit (exit_success);
}
How to compile:
Gcc-o Test TEST.C-LDL
You need to copy an so file to the current directory, with the name libtest.so, and the output of the program will look like this:
......
......
Name=/lib/libdl.so.2 (9 segments)
Header 0:address=0x40039034/lib/libdl.so.2: _dl_rtld_di_serinfo
Header 1:address=0x4003a9ae/lib/libdl.so.2: (NULL)
Header 2:address=0x40039000/lib/libdl.so.2: __pthread_once
Header 3:address=0x4003bed4/lib/libdl.so.2: (NULL)
Header 4:address=0x4003beec/lib/libdl.so.2: (NULL)
Header 5:address=0x40039154/lib/libdl.so.2: _dl_rtld_di_serinfo
Header 6:address=0x40039174/lib/libdl.so.2: _rtld_global
Header 7:address=0x40039000/lib/libdl.so.2: __pthread_once
Header 8:address=0x4003bed4/lib/libdl.so.2: (NULL)
Name=/lib/tls/libc.so.6 (segments)
Header 0:address=0x4003d034/lib/tls/libc.so.6: _rtld_global
Header 1:address=0x4014a540/lib/tls/libc.so.6: (NULL)
Header 2:address=0x4003d000/lib/tls/libc.so.6:gcc_3.0
Header 3:address=0x401505ec/lib/tls/libc.so.6: (NULL)
Header 4:address=0x40151d3c/lib/tls/libc.so.6: (NULL)
Header 5:address=0x4003d194/lib/tls/libc.so.6: _rtld_global
Header 6:address=0x4003d1b4/lib/tls/libc.so.6: _rtld_global
Header 7:address=0x401505ec/lib/tls/libc.so.6: (NULL)
Header 8:address=0x4014a554/lib/tls/libc.so.6: (NULL)
Header 9:address=0x4003d000/lib/tls/libc.so.6:gcc_3.0
Header 10:address=0x401505f4/lib/tls/libc.so.6: (NULL)
Name=/lib/ld-linux.so.2 (6 segments)
Header 0:address=0x40000000/lib/ld-linux.so.2:glibc_2.1
Header 1:address=0x40016cc0/lib/ld-linux.so.2: _rtld_global_ro
Header 2:address=0x40016f34/lib/ld-linux.so.2: (NULL)
Header 3:address=0x40015abc/lib/ld-linux.so.2: (NULL)
Header 4:address=0x40000000/lib/ld-linux.so.2:glibc_2.1
Header 5:address=0x40016cc0/lib/ld-linux.so.2: _rtld_global_ro
In the Linux program, know a function address, change the function is belong to a dynamic library, how to get this dynamic library full "turn"