Imported Upstream version 0.155
[platform/upstream/elfutils.git] / tests / addrcfi.c
1 /* Test program for CFI handling.
2    Copyright (C) 2009-2010 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(dwfl)
22 #include <dwarf.h>
23 #include <argp.h>
24 #include <stdio.h>
25 #include <stdio_ext.h>
26 #include <locale.h>
27 #include <stdlib.h>
28 #include <error.h>
29 #include <string.h>
30
31
32 static void
33 print_detail (int result, const Dwarf_Op *ops, size_t nops, Dwarf_Addr bias)
34 {
35   if (result < 0)
36     printf ("indeterminate (%s)\n", dwarf_errmsg (-1));
37   else if (nops == 0)
38     printf ("%s\n", result == 0 ? "same_value" : "undefined");
39   else
40     {
41       printf ("%s expression:", result == 0 ? "location" : "value");
42       for (size_t i = 0; i < nops; ++i)
43         {
44           printf (" %#x", ops[i].atom);
45           if (ops[i].number2 == 0)
46             {
47               if (ops[i].atom == DW_OP_addr)
48                 printf ("(%#" PRIx64 ")", ops[i].number + bias);
49               else if (ops[i].number != 0)
50                 printf ("(%" PRId64 ")", ops[i].number);
51             }
52           else
53             printf ("(%" PRId64 ",%" PRId64 ")",
54                     ops[i].number, ops[i].number2);
55         }
56       puts ("");
57     }
58 }
59
60 struct stuff
61 {
62   Dwarf_Frame *frame;
63   Dwarf_Addr bias;
64 };
65
66 static int
67 print_register (void *arg,
68                 int regno,
69                 const char *setname,
70                 const char *prefix,
71                 const char *regname,
72                 int bits __attribute__ ((unused)),
73                 int type __attribute__ ((unused)))
74 {
75   struct stuff *stuff = arg;
76
77   printf ("\t%s reg%u (%s%s): ", setname, regno, prefix, regname);
78
79   Dwarf_Op ops_mem[2];
80   Dwarf_Op *ops;
81   size_t nops;
82   int result = dwarf_frame_register (stuff->frame, regno, ops_mem, &ops, &nops);
83   print_detail (result, ops, nops, stuff->bias);
84
85   return DWARF_CB_OK;
86 }
87
88 static int
89 handle_cfi (Dwfl *dwfl, const char *which, Dwarf_CFI *cfi,
90             GElf_Addr pc, struct stuff *stuff)
91 {
92   int result = dwarf_cfi_addrframe (cfi, pc - stuff->bias, &stuff->frame);
93   if (result != 0)
94     {
95       error (0, 0, "dwarf_cfi_addrframe (%s): %s", which, dwarf_errmsg (-1));
96       return 1;
97     }
98
99   Dwarf_Addr start = pc;
100   Dwarf_Addr end = pc;
101   bool signalp;
102   int ra_regno = dwarf_frame_info (stuff->frame, &start, &end, &signalp);
103   if (ra_regno >= 0)
104     {
105       start += stuff->bias;
106       end += stuff->bias;
107     }
108
109   printf ("%s has %#" PRIx64 " => [%#" PRIx64 ", %#" PRIx64 "):\n",
110           which, pc, start, end);
111
112   if (ra_regno < 0)
113     printf ("\treturn address register unavailable (%s)\n",
114             dwarf_errmsg (0));
115   else
116     printf ("\treturn address in reg%u%s\n",
117             ra_regno, signalp ? " (signal frame)" : "");
118
119   Dwarf_Op *cfa_ops;
120   size_t cfa_nops;
121   result = dwarf_frame_cfa (stuff->frame, &cfa_ops, &cfa_nops);
122
123   printf ("\tCFA ");
124   print_detail (result, cfa_ops, cfa_nops, stuff->bias);
125
126   (void) dwfl_module_register_names (dwfl_addrmodule (dwfl, pc),
127                                      &print_register, stuff);
128
129   return 0;
130 }
131
132 static int
133 handle_address (GElf_Addr pc, Dwfl *dwfl)
134 {
135   Dwfl_Module *mod = dwfl_addrmodule (dwfl, pc);
136
137   struct stuff stuff;
138   return (handle_cfi (dwfl, ".eh_frame",
139                       dwfl_module_eh_cfi (mod, &stuff.bias), pc, &stuff)
140           & handle_cfi (dwfl, ".debug_frame",
141                         dwfl_module_dwarf_cfi (mod, &stuff.bias), pc, &stuff));
142 }
143
144 int
145 main (int argc, char *argv[])
146 {
147   int remaining;
148
149   /* Set locale.  */
150   (void) setlocale (LC_ALL, "");
151
152   Dwfl *dwfl = NULL;
153   (void) argp_parse (dwfl_standard_argp (), argc, argv, 0, &remaining, &dwfl);
154   assert (dwfl != NULL);
155
156   int result = 0;
157
158   /* Now handle the addresses.  In case none are given on the command
159      line, read from stdin.  */
160   if (remaining == argc)
161     {
162       /* We use no threads here which can interfere with handling a stream.  */
163       (void) __fsetlocking (stdin, FSETLOCKING_BYCALLER);
164
165       char *buf = NULL;
166       size_t len = 0;
167       while (!feof_unlocked (stdin))
168         {
169           if (getline (&buf, &len, stdin) < 0)
170             break;
171
172           char *endp;
173           uintmax_t addr = strtoumax (buf, &endp, 0);
174           if (endp != buf)
175             result |= handle_address (addr, dwfl);
176           else
177             result = 1;
178         }
179
180       free (buf);
181     }
182   else
183     {
184       do
185         {
186           char *endp;
187           uintmax_t addr = strtoumax (argv[remaining], &endp, 0);
188           if (endp != argv[remaining])
189             result |= handle_address (addr, dwfl);
190           else
191             result = 1;
192         }
193       while (++remaining < argc);
194     }
195
196   dwfl_end (dwfl);
197
198   return result;
199 }