Add --vdso command line option
[opensuse:ptools.git] / src / test1.c
1 #include <stdio.h>
2 #include <elf.h>
3 #include <link.h>
4
5 int main(int argc, char *argv[], char *envp[])
6 {
7         ElfW(Dyn) *dyn;
8         ElfW(auxv_t) *auxv;
9         struct r_debug *r_debug;
10         struct link_map *map;
11         uint64_t vdso;
12
13         /* *envp = NULL marks end of envp */
14         while(*envp++ != NULL);
15
16         /* auxv->a_type = AT_NULL marks the end of auxv */
17         for (auxv = (ElfW(auxv_t) *)envp; auxv->a_type != AT_NULL; auxv++) {
18                 if( auxv->a_type == AT_SYSINFO_EHDR) {
19                         printf("AT_SYSINFO_EHDR is: 0x%lx\n", auxv->a_un.a_val);
20                         vdso = auxv->a_un.a_val;
21                 }
22         }
23
24         for (dyn = _DYNAMIC; dyn->d_tag != DT_NULL; ++dyn)
25                 if (dyn->d_tag == DT_DEBUG)
26                         r_debug = (struct r_debug *) dyn->d_un.d_ptr;
27
28         for(map = r_debug->r_map; map; map = map->l_next) {
29                 char *strings;
30
31                 printf("l_addr=0x%lx, l_name=%s, l_ld=0x%lx\n", map->l_addr,
32                        map->l_name, (unsigned long)map->l_ld);
33
34                 if (map->l_addr == vdso) {
35                         printf("vDSO detected\n");
36                         continue;
37                 }
38
39                 for (dyn = map->l_ld; dyn->d_tag != DT_NULL; ++dyn)
40                         if (dyn->d_tag == DT_STRTAB) {
41                                 strings = (char *) dyn->d_un.d_ptr;
42                                 printf("DT_STRTAB=%d (0x%lx)\n",
43                                        dyn->d_un.d_val, dyn->d_un.d_ptr);
44                         }
45                 for (dyn = map->l_ld; dyn->d_tag != DT_NULL; ++dyn)
46                         if (dyn->d_tag == DT_SONAME)
47                                 printf("DT_SONAME=%d (0x%lx)\n", dyn->d_un.d_val,
48                                         (char *)strings+dyn->d_un.d_val);
49         }
50 }