1 /* Copyright (C) 2012, 2013, 2015 Red Hat, Inc.
2    This file is part of elfutils.
3 
4    This file is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3 of the License, or
7    (at your option) any later version.
8 
9    elfutils is distributed in the hope that it will be useful, but
10    WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13 
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
16 
17 #ifdef HAVE_CONFIG_H
18 # include <config.h>
19 #endif
20 
21 #include <fcntl.h>
22 #include ELFUTILS_HEADER(dw)
23 #include <stdio.h>
24 #include <unistd.h>
25 #include <dwarf.h>
26 #include <inttypes.h>
27 
28 int
main(int argc,char * argv[])29 main (int argc, char *argv[])
30 {
31   for (int i = 1; i < argc; ++i)
32     {
33       int fd = open (argv[i], O_RDONLY);
34 
35       Dwarf *dbg = dwarf_begin (fd, DWARF_C_READ);
36       if (dbg != NULL)
37 	{
38 	  Dwarf_Off off = 0;
39 	  size_t cuhl;
40 	  Dwarf_Off noff;
41 	  uint64_t type_sig;
42 
43 	  while (dwarf_next_unit (dbg, off, &noff, &cuhl, NULL, NULL, NULL,
44 				  NULL, &type_sig, NULL) == 0)
45 	    {
46 	      Dwarf_Die die_mem;
47 	      dwarf_offdie_types (dbg, off + cuhl, &die_mem);
48 	      off = noff;
49 	    }
50 
51 	  off = 0;
52 
53 	  while (dwarf_nextcu (dbg, off, &noff, &cuhl, NULL, NULL, NULL) == 0)
54 	    {
55 	      Dwarf_Die die_mem;
56 	      Dwarf_Die *die = dwarf_offdie (dbg, off + cuhl, &die_mem);
57 
58 	      Dwarf_Die iter_mem;
59 	      Dwarf_Die *iter = &iter_mem;
60 	      dwarf_child (die, &iter_mem);
61 
62 	      while (1)
63 		{
64 		  if (dwarf_tag (iter) == DW_TAG_variable)
65 		    {
66 		      Dwarf_Attribute attr_mem;
67 		      Dwarf_Die form_mem, *form;
68 		      form = dwarf_formref_die (dwarf_attr (iter, DW_AT_type,
69 							    &attr_mem),
70 						&form_mem);
71 
72 		      if (form == NULL)
73 			printf ("fail\n");
74 		      else
75 			printf ("ok %s [%" PRIx64 "]\n",
76 				dwarf_diename (form), dwarf_dieoffset (form));
77 		    }
78 
79 		  if (dwarf_siblingof (iter, &iter_mem) != 0)
80 		    break;
81 		}
82 
83 	      off = noff;
84 	    }
85 
86 	  dwarf_end (dbg);
87 	}
88 
89       close (fd);
90     }
91 }
92