xref: /aosp_15_r20/external/elfutils/tests/vdsosyms.c (revision 7304104da70ce23c86437a01be71edd1a2d7f37e)
1*7304104dSAndroid Build Coastguard Worker /* Test program for getting symbol table from vdso module.
2*7304104dSAndroid Build Coastguard Worker    Copyright (C) 2014 Red Hat, Inc.
3*7304104dSAndroid Build Coastguard Worker    This file is part of elfutils.
4*7304104dSAndroid Build Coastguard Worker 
5*7304104dSAndroid Build Coastguard Worker    This file is free software; you can redistribute it and/or modify
6*7304104dSAndroid Build Coastguard Worker    it under the terms of the GNU General Public License as published by
7*7304104dSAndroid Build Coastguard Worker    the Free Software Foundation; either version 3 of the License, or
8*7304104dSAndroid Build Coastguard Worker    (at your option) any later version.
9*7304104dSAndroid Build Coastguard Worker 
10*7304104dSAndroid Build Coastguard Worker    elfutils is distributed in the hope that it will be useful, but
11*7304104dSAndroid Build Coastguard Worker    WITHOUT ANY WARRANTY; without even the implied warranty of
12*7304104dSAndroid Build Coastguard Worker    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13*7304104dSAndroid Build Coastguard Worker    GNU General Public License for more details.
14*7304104dSAndroid Build Coastguard Worker 
15*7304104dSAndroid Build Coastguard Worker    You should have received a copy of the GNU General Public License
16*7304104dSAndroid Build Coastguard Worker    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
17*7304104dSAndroid Build Coastguard Worker 
18*7304104dSAndroid Build Coastguard Worker #include <config.h>
19*7304104dSAndroid Build Coastguard Worker #include <assert.h>
20*7304104dSAndroid Build Coastguard Worker #include <errno.h>
21*7304104dSAndroid Build Coastguard Worker #include <inttypes.h>
22*7304104dSAndroid Build Coastguard Worker #include <stdio.h>
23*7304104dSAndroid Build Coastguard Worker #include <string.h>
24*7304104dSAndroid Build Coastguard Worker #include <sys/types.h>
25*7304104dSAndroid Build Coastguard Worker #include <unistd.h>
26*7304104dSAndroid Build Coastguard Worker #include ELFUTILS_HEADER(dwfl)
27*7304104dSAndroid Build Coastguard Worker #include "system.h"
28*7304104dSAndroid Build Coastguard Worker 
29*7304104dSAndroid Build Coastguard Worker #ifndef __linux__
30*7304104dSAndroid Build Coastguard Worker int
main(int argc,char ** argv)31*7304104dSAndroid Build Coastguard Worker main (int argc __attribute__ ((unused)), char **argv __attribute__ ((unused)))
32*7304104dSAndroid Build Coastguard Worker {
33*7304104dSAndroid Build Coastguard Worker   printf ("Getting the vdso is unsupported.\n");
34*7304104dSAndroid Build Coastguard Worker   return 77;
35*7304104dSAndroid Build Coastguard Worker }
36*7304104dSAndroid Build Coastguard Worker #else /* __linux__ */
37*7304104dSAndroid Build Coastguard Worker static int vdso_syms = 0;
38*7304104dSAndroid Build Coastguard Worker 
39*7304104dSAndroid Build Coastguard Worker static int
module_callback(Dwfl_Module * mod,void ** userdata,const char * name,Dwarf_Addr start,void * arg)40*7304104dSAndroid Build Coastguard Worker module_callback (Dwfl_Module *mod, void **userdata __attribute__((unused)),
41*7304104dSAndroid Build Coastguard Worker 		 const char *name, Dwarf_Addr start __attribute__((unused)),
42*7304104dSAndroid Build Coastguard Worker 		 void *arg __attribute__((unused)))
43*7304104dSAndroid Build Coastguard Worker {
44*7304104dSAndroid Build Coastguard Worker   /* We can only recognize the vdso by inspecting the "magic name".  */
45*7304104dSAndroid Build Coastguard Worker   printf ("module name: %s\n", name);
46*7304104dSAndroid Build Coastguard Worker   if (startswith (name, "[vdso: "))
47*7304104dSAndroid Build Coastguard Worker     {
48*7304104dSAndroid Build Coastguard Worker       vdso_syms = dwfl_module_getsymtab (mod);
49*7304104dSAndroid Build Coastguard Worker       printf ("vdso syms: %d\n", vdso_syms);
50*7304104dSAndroid Build Coastguard Worker       if (vdso_syms < 0)
51*7304104dSAndroid Build Coastguard Worker 	error (2, 0, "dwfl_module_getsymtab: %s", dwfl_errmsg (-1));
52*7304104dSAndroid Build Coastguard Worker 
53*7304104dSAndroid Build Coastguard Worker       for (int i = 0; i < vdso_syms; i++)
54*7304104dSAndroid Build Coastguard Worker 	{
55*7304104dSAndroid Build Coastguard Worker 	  GElf_Sym sym;
56*7304104dSAndroid Build Coastguard Worker 	  GElf_Addr addr;
57*7304104dSAndroid Build Coastguard Worker 	  const char *sname = dwfl_module_getsym_info (mod, i, &sym, &addr,
58*7304104dSAndroid Build Coastguard Worker 						       NULL, NULL, NULL);
59*7304104dSAndroid Build Coastguard Worker 	  assert (sname != NULL);
60*7304104dSAndroid Build Coastguard Worker 	  printf ("%d: '%s' %" PRIx64 " (%" PRIx64 ")\n",
61*7304104dSAndroid Build Coastguard Worker 		  i, sname, sym.st_value, addr);
62*7304104dSAndroid Build Coastguard Worker 	}
63*7304104dSAndroid Build Coastguard Worker     }
64*7304104dSAndroid Build Coastguard Worker 
65*7304104dSAndroid Build Coastguard Worker   return DWARF_CB_OK;
66*7304104dSAndroid Build Coastguard Worker }
67*7304104dSAndroid Build Coastguard Worker 
68*7304104dSAndroid Build Coastguard Worker int
main(int argc,char ** argv)69*7304104dSAndroid Build Coastguard Worker main (int argc __attribute__ ((unused)), char **argv __attribute__ ((unused)))
70*7304104dSAndroid Build Coastguard Worker {
71*7304104dSAndroid Build Coastguard Worker   static char *debuginfo_path;
72*7304104dSAndroid Build Coastguard Worker   static const Dwfl_Callbacks proc_callbacks =
73*7304104dSAndroid Build Coastguard Worker     {
74*7304104dSAndroid Build Coastguard Worker       .find_debuginfo = dwfl_standard_find_debuginfo,
75*7304104dSAndroid Build Coastguard Worker       .debuginfo_path = &debuginfo_path,
76*7304104dSAndroid Build Coastguard Worker 
77*7304104dSAndroid Build Coastguard Worker       .find_elf = dwfl_linux_proc_find_elf,
78*7304104dSAndroid Build Coastguard Worker     };
79*7304104dSAndroid Build Coastguard Worker   Dwfl *dwfl = dwfl_begin (&proc_callbacks);
80*7304104dSAndroid Build Coastguard Worker   if (dwfl == NULL)
81*7304104dSAndroid Build Coastguard Worker     error (2, 0, "dwfl_begin: %s", dwfl_errmsg (-1));
82*7304104dSAndroid Build Coastguard Worker 
83*7304104dSAndroid Build Coastguard Worker   /* Take ourself as "arbitrary" process to inspect.  This should work
84*7304104dSAndroid Build Coastguard Worker      even with "restricted ptrace".  */
85*7304104dSAndroid Build Coastguard Worker   pid_t pid = getpid();
86*7304104dSAndroid Build Coastguard Worker 
87*7304104dSAndroid Build Coastguard Worker   int result = dwfl_linux_proc_report (dwfl, pid);
88*7304104dSAndroid Build Coastguard Worker   if (result < 0)
89*7304104dSAndroid Build Coastguard Worker     error (2, 0, "dwfl_linux_proc_report: %s", dwfl_errmsg (-1));
90*7304104dSAndroid Build Coastguard Worker   else if (result > 0)
91*7304104dSAndroid Build Coastguard Worker     error (2, result, "dwfl_linux_proc_report");
92*7304104dSAndroid Build Coastguard Worker 
93*7304104dSAndroid Build Coastguard Worker   /* Also explicitly attach for older kernels (cannot read vdso otherwise).  */
94*7304104dSAndroid Build Coastguard Worker   result = dwfl_linux_proc_attach (dwfl, pid, false);
95*7304104dSAndroid Build Coastguard Worker   if (result < 0)
96*7304104dSAndroid Build Coastguard Worker     error (2, 0, "dwfl_linux_proc_attach: %s", dwfl_errmsg (-1));
97*7304104dSAndroid Build Coastguard Worker   else if (result > 0)
98*7304104dSAndroid Build Coastguard Worker     error (2, result, "dwfl_linux_proc_attach");
99*7304104dSAndroid Build Coastguard Worker 
100*7304104dSAndroid Build Coastguard Worker   if (dwfl_report_end (dwfl, NULL, NULL) != 0)
101*7304104dSAndroid Build Coastguard Worker     error (2, 0, "dwfl_report_end: %s", dwfl_errmsg (-1));
102*7304104dSAndroid Build Coastguard Worker 
103*7304104dSAndroid Build Coastguard Worker   if (dwfl_getmodules (dwfl, module_callback, NULL, 0) != 0)
104*7304104dSAndroid Build Coastguard Worker     error (1, 0, "dwfl_getmodules: %s", dwfl_errmsg (-1));
105*7304104dSAndroid Build Coastguard Worker 
106*7304104dSAndroid Build Coastguard Worker   dwfl_end (dwfl);
107*7304104dSAndroid Build Coastguard Worker 
108*7304104dSAndroid Build Coastguard Worker   /* No symbols is ok, then we haven't seen the vdso at all on this arch.  */
109*7304104dSAndroid Build Coastguard Worker   return vdso_syms >= 0 ? 0 : -1;
110*7304104dSAndroid Build Coastguard Worker }
111*7304104dSAndroid Build Coastguard Worker 
112*7304104dSAndroid Build Coastguard Worker #endif /* ! __linux__ */
113