1 /* Test program for DWARF (.debug_frame) CFI handling.
2    Copyright (C) 2009-2010, 2013, 2015, 2018 Red Hat, Inc.
3    This file is part of elfutils.
4 
5    This file is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 3 of the License, or
8    (at your option) any later version.
9 
10    elfutils is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14 
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
17 
18 #include <config.h>
19 #include <assert.h>
20 #include <inttypes.h>
21 #include ELFUTILS_HEADER(dw)
22 #include <dwarf.h>
23 #include <argp.h>
24 #include <stdio.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <fcntl.h>
28 #include <locale.h>
29 #include <stdlib.h>
30 #include <string.h>
31 
32 #include "system.h"
33 #include "../libdw/known-dwarf.h"
34 
35 static const char *
op_name(unsigned int code)36 op_name (unsigned int code)
37 {
38   static const char *const known[] =
39     {
40 #define DWARF_ONE_KNOWN_DW_OP(NAME, CODE) [CODE] = #NAME,
41       DWARF_ALL_KNOWN_DW_OP
42 #undef DWARF_ONE_KNOWN_DW_OP
43     };
44 
45   if (likely (code < sizeof (known) / sizeof (known[0])))
46     return known[code];
47 
48   return NULL;
49 }
50 
51 static void
print_detail(int result,const Dwarf_Op * ops,size_t nops)52 print_detail (int result, const Dwarf_Op *ops, size_t nops)
53 {
54   if (result < 0)
55     printf ("indeterminate (%s)\n", dwarf_errmsg (-1));
56   else if (nops == 0)
57     printf ("%s\n", ops == NULL ? "same_value" : "undefined");
58   else
59     {
60       printf ("%s expression:", result == 0 ? "location" : "value");
61       for (size_t i = 0; i < nops; ++i)
62 	{
63 	  printf (" %s", op_name(ops[i].atom));
64 	  if (ops[i].number2 == 0)
65 	    {
66 	      if (ops[i].atom == DW_OP_addr)
67 		printf ("(%#" PRIx64 ")", ops[i].number);
68 	      else if (ops[i].number != 0)
69 		printf ("(%" PRId64 ")", ops[i].number);
70 	    }
71 	  else
72 	    printf ("(%" PRId64 ",%" PRId64 ")",
73 		    ops[i].number, ops[i].number2);
74 	}
75       puts ("");
76     }
77 }
78 
79 static int
handle_address(Dwarf_CFI * cfi,GElf_Addr pc)80 handle_address (Dwarf_CFI *cfi, GElf_Addr pc)
81 {
82   Dwarf_Frame *frame;
83   int result = dwarf_cfi_addrframe (cfi, pc, &frame);
84   if (result != 0)
85     {
86       printf ("dwarf_cfi_addrframe: %s\n", dwarf_errmsg (-1));
87       return 1;
88     }
89 
90   Dwarf_Addr start = pc;
91   Dwarf_Addr end = pc;
92   bool signalp;
93   int ra_regno = dwarf_frame_info (frame, &start, &end, &signalp);
94 
95   printf ("%#" PRIx64 " => [%#" PRIx64 ", %#" PRIx64 "):\n",
96 	  pc, start, end);
97 
98   if (ra_regno < 0)
99     printf ("\treturn address register unavailable (%s)\n",
100 	    dwarf_errmsg (-1));
101   else
102     printf ("\treturn address in reg%u%s\n",
103 	    ra_regno, signalp ? " (signal frame)" : "");
104 
105   // Point cfa_ops to dummy to match print_detail expectations.
106   // (nops == 0 && cfa_ops != NULL => "undefined")
107   Dwarf_Op dummy;
108   Dwarf_Op *cfa_ops = &dummy;
109   size_t cfa_nops;
110   result = dwarf_frame_cfa (frame, &cfa_ops, &cfa_nops);
111 
112   printf ("\tCFA ");
113   print_detail (result, cfa_ops, cfa_nops);
114 
115   // Print the location of the first 10 (DWARF nr) registers
116   for (int r = 0; r < 10; r++)
117     {
118       Dwarf_Op ops_mem[3];
119       Dwarf_Op *ops;
120       size_t nops;
121       printf ("\treg%d: ", r);
122       int reg_result = dwarf_frame_register (frame, r, ops_mem, &ops, &nops);
123       print_detail (reg_result, ops, nops);
124       result |= reg_result;
125     }
126 
127   free (frame);
128   return result;
129 }
130 
131 int
main(int argc,char * argv[])132 main (int argc, char *argv[])
133 {
134   if (argc <= 2)
135     error (EXIT_FAILURE, 0, "need file name argument and addresses");
136 
137   int fd = open (argv[1], O_RDONLY);
138   if (fd == -1)
139     error (EXIT_FAILURE, errno, "cannot open input file `%s'", argv[1]);
140 
141   elf_version (EV_CURRENT);
142 
143   Elf *elf = elf_begin (fd, ELF_C_READ, NULL);
144   if (elf == NULL)
145     error (EXIT_FAILURE, 0, "cannot create ELF descriptor: %s",
146 	   elf_errmsg (-1));
147 
148   Dwarf *dwarf = dwarf_begin_elf (elf, DWARF_C_READ, NULL);
149   if (dwarf == NULL)
150     error (EXIT_FAILURE, 0, "cannot create DWARF descriptor: %s",
151 	   dwarf_errmsg (-1));
152 
153   Dwarf_CFI *cfi = dwarf_getcfi (dwarf);
154   if (cfi == NULL)
155     error (EXIT_FAILURE, 0, "cannot get DWARF CFI from .dwarf_frame: %s",
156 	   dwarf_errmsg (-1));
157 
158   int result = 0;
159   int args = 2;
160   do
161     {
162       char *endp;
163       uintmax_t addr = strtoumax (argv[args], &endp, 0);
164       if (endp != argv[args])
165 	result |= handle_address (cfi, addr);
166       else
167 	result = 1;
168     }
169   while (args++ < argc - 1);
170 
171   dwarf_end (dwarf);
172   elf_end (elf);
173 
174   return result;
175 }
176