ns-3 Direct Code Execution
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
readversiondef.c
Go to the documentation of this file.
1 #include <elf.h>
2 #include <link.h>
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <unistd.h>
6 #include <sys/mman.h>
7 #include <fcntl.h>
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <assert.h>
11 #include <string.h>
12 
13 int main (int argc, char *argv[])
14 {
15  const char *filename = argv[1];
16  int fd = open (filename, O_RDONLY);
17  struct stat buf;
18  fstat (fd, &buf);
19  unsigned long file = (unsigned long)mmap (0, buf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
20  if (file == (unsigned long)MAP_FAILED)
21  {
22  exit (1);
23  }
24  ElfW(Ehdr) *header = (ElfW(Ehdr) *)file;
25  ElfW(Shdr) *sh = (ElfW(Shdr)*)(file + header->e_shoff);
26  ElfW(Sym) *symtab = 0;
27  unsigned long n_symtab = 0;
28  ElfW(Half) *versym = 0;
29  ElfW(Verdef) *verdef = 0;
30  char *strtab = 0;
31  int i;
32  for (i = 0; i < header->e_shnum; i++)
33  {
34  if (sh[i].sh_type == SHT_DYNSYM)
35  {
36  symtab = (ElfW(Sym)*)(file + sh[i].sh_offset);
37  n_symtab = sh[i].sh_size / sh[i].sh_entsize;
38  }
39  else if (sh[i].sh_type == SHT_STRTAB && strtab == 0)
40  {
41  // XXX: should check the section name.
42  strtab = (char*)(file+sh[i].sh_offset);
43  }
44  else if (sh[i].sh_type == SHT_GNU_versym)
45  {
46  versym = (ElfW(Half)*)(file+sh[i].sh_offset);
47  }
48  else if (sh[i].sh_type == SHT_GNU_verdef)
49  {
50  verdef = (ElfW(Verdef)*)(file+sh[i].sh_offset);
51  }
52  }
53  if (strtab == 0 || verdef == 0 ||
54  symtab == 0 || n_symtab == 0 || versym == 0)
55  {
56  exit (3);
57  }
58  ElfW(Verdef) *cur, *prev;
59  int local_passthru_printed = 0;
60  for (prev = 0, cur = verdef;
61  cur != prev;
62  prev = cur, cur = (ElfW(Verdef)*)(((unsigned long)cur)+cur->vd_next))
63  {
64  assert (cur->vd_version == 1);
65  assert (cur->vd_cnt == 2 || cur->vd_cnt == 1);
66  ElfW(Verdaux) *first = (ElfW(Verdaux)*)(((unsigned long)cur)+cur->vd_aux);
67  if (cur->vd_flags & VER_FLG_BASE)
68  {
69  continue;
70  }
71  printf ("%s {\n", strtab + first->vda_name);
72  int has_one_symbol = 0;
73  for (i = 0; i < n_symtab; i++)
74  {
75  if (symtab[i].st_name == 0 || symtab[i].st_value == 0)
76  {
77  continue;
78  }
79  ElfW(Half) ver = versym[i];
80  if (cur->vd_ndx == ver)
81  {
82  if (!has_one_symbol)
83  {
84  has_one_symbol = 1;
85  printf ("global:\n");
86  }
87  printf ("\t%s;\n", strtab + symtab[i].st_name);
88  }
89  }
90  if (cur->vd_cnt == 1)
91  {
92  if (!local_passthru_printed)
93  {
94  local_passthru_printed = 1;
95  printf ("local:*;\n};\n");
96  }
97  else
98  {
99  printf ("};\n");
100  }
101  }
102  else
103  {
104  ElfW(Verdaux) *parent = (ElfW(Verdaux)*)(((unsigned long)first)+first->vda_next);
105  printf ("} %s;\n", strtab + parent->vda_name);
106  }
107  }
108  return 0;
109 }