xref: /aosp_15_r20/external/elfutils/tests/funcretval.c (revision 7304104da70ce23c86437a01be71edd1a2d7f37e)
1 /* Test program for dwfl_module_return_value_location.
2    Copyright (C) 2005 Red Hat, Inc.
3    Copyright (C) 2023 Mark J. Wielaard <[email protected]>
4    This file is part of elfutils.
5 
6    This file is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10 
11    elfutils is distributed in the hope that it will be useful, but
12    WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15 
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
18 
19 #include <config.h>
20 #include <assert.h>
21 #include <inttypes.h>
22 #include ELFUTILS_HEADER(dwfl)
23 #include <dwarf.h>
24 #include <argp.h>
25 #include <stdio.h>
26 #include <stdio_ext.h>
27 #include <locale.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <fnmatch.h>
31 #include "system.h"
32 
33 
34 struct args
35 {
36   Dwfl *dwfl;
37   Dwarf_Die *cu;
38   Dwarf_Addr dwbias;
39   char **argv;
40 };
41 
42 static int
handle_function(Dwarf_Die * funcdie,void * arg)43 handle_function (Dwarf_Die *funcdie, void *arg)
44 {
45   struct args *a = arg;
46 
47   const char *name = dwarf_diename (funcdie);
48   char **argv = a->argv;
49   if (argv[0] != NULL)
50     {
51       bool match;
52       do
53 	match = fnmatch (*argv, name, 0) == 0;
54       while (!match && *++argv);
55       if (!match)
56 	return 0;
57     }
58 
59   printf ("(%s) %s: ", dwfl_module_info (dwfl_cumodule (a->cu), NULL,
60 					 NULL, NULL,
61 					 NULL, NULL,
62 					 NULL, NULL), name);
63 
64   const Dwarf_Op *locops;
65   int nlocops = dwfl_module_return_value_location (dwfl_cumodule (a->cu),
66 						   funcdie, &locops);
67   if (nlocops < 0)
68     error (EXIT_FAILURE, 0, "dwfl_module_return_value_location: %s",
69 	   dwfl_errmsg (-1));
70   else if (nlocops == 0)
71     {
72       // Check if this is the special unspecified type
73       // https://sourceware.org/bugzilla/show_bug.cgi?id=30047
74       Dwarf_Die die_mem, *typedie = &die_mem;
75       Dwarf_Attribute attr_mem, *attr;
76       attr = dwarf_attr_integrate (funcdie, DW_AT_type, &attr_mem);
77       if (dwarf_formref_die (attr, typedie) != NULL
78 	  && dwarf_tag (typedie) == DW_TAG_unspecified_type)
79 	puts ("returns unspecified type");
80       else
81 	puts ("returns no value");
82     }
83   else
84     {
85       printf ("return value location:");
86       for (int i = 0; i < nlocops; ++i)
87 	printf (" {%#x, %#" PRIx64 "}", locops[i].atom, locops[i].number);
88       puts ("");
89     }
90 
91   return 0;
92 }
93 
94 
95 int
main(int argc,char * argv[])96 main (int argc, char *argv[])
97 {
98   int remaining;
99 
100   /* Set locale.  */
101   (void) setlocale (LC_ALL, "");
102 
103   struct args a = { .dwfl = NULL, .cu = NULL };
104 
105   (void) argp_parse (dwfl_standard_argp (), argc, argv, 0, &remaining,
106 		     &a.dwfl);
107   assert (a.dwfl != NULL);
108   a.argv = &argv[remaining];
109 
110   int result = 0;
111 
112   while ((a.cu = dwfl_nextcu (a.dwfl, a.cu, &a.dwbias)) != NULL)
113     dwarf_getfuncs (a.cu, &handle_function, &a, 0);
114 
115   dwfl_end (a.dwfl);
116 
117   return result;
118 }
119