• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Try to get an ELF or debug file through the debuginfod.
2    Copyright (C) 2019 Red Hat, Inc.
3    Copyright (C) 2022 Mark J. Wielaard <mark@klomp.org>
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 either
8 
9      * the GNU Lesser General Public License as published by the Free
10        Software Foundation; either version 3 of the License, or (at
11        your option) any later version
12 
13    or
14 
15      * the GNU General Public License as published by the Free
16        Software Foundation; either version 2 of the License, or (at
17        your option) any later version
18 
19    or both in parallel, as here.
20 
21    elfutils is distributed in the hope that it will be useful, but
22    WITHOUT ANY WARRANTY; without even the implied warranty of
23    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
24    General Public License for more details.
25 
26    You should have received copies of the GNU General Public License and
27    the GNU Lesser General Public License along with this program.  If
28    not, see <http://www.gnu.org/licenses/>.  */
29 
30 #ifdef HAVE_CONFIG_H
31 # include <config.h>
32 #endif
33 
34 #include "libdwflP.h"
35 
36 #ifdef ENABLE_LIBDEBUGINFOD
37 
38 #include <pthread.h>
39 #include <dlfcn.h>
40 
41 static __typeof__ (debuginfod_begin) *fp_debuginfod_begin;
42 static __typeof__ (debuginfod_find_executable) *fp_debuginfod_find_executable;
43 static __typeof__ (debuginfod_find_debuginfo) *fp_debuginfod_find_debuginfo;
44 static __typeof__ (debuginfod_end) *fp_debuginfod_end;
45 
46 static void __libdwfl_debuginfod_init (void);
47 
48 static pthread_once_t init_control = PTHREAD_ONCE_INIT;
49 
50 /* NB: this is slightly thread-unsafe */
51 
52 debuginfod_client *
dwfl_get_debuginfod_client(Dwfl * dwfl)53 dwfl_get_debuginfod_client (Dwfl *dwfl)
54 {
55   if (dwfl->debuginfod != NULL)
56     return dwfl->debuginfod;
57 
58   pthread_once (&init_control, __libdwfl_debuginfod_init);
59 
60   if (fp_debuginfod_begin != NULL)
61     {
62       dwfl->debuginfod = (*fp_debuginfod_begin) ();
63       return dwfl->debuginfod;
64     }
65 
66   return NULL;
67 }
INTDEF(dwfl_get_debuginfod_client)68 INTDEF(dwfl_get_debuginfod_client)
69 
70 int
71 __libdwfl_debuginfod_find_executable (Dwfl *dwfl,
72 				      const unsigned char *build_id_bits,
73 				      size_t build_id_len)
74 {
75   int fd = -1;
76   if (build_id_len > 0)
77     {
78       debuginfod_client *c = INTUSE (dwfl_get_debuginfod_client) (dwfl);
79       if (c != NULL)
80 	fd = (*fp_debuginfod_find_executable) (c, build_id_bits,
81 					       build_id_len, NULL);
82     }
83 
84   return fd;
85 }
86 
87 int
__libdwfl_debuginfod_find_debuginfo(Dwfl * dwfl,const unsigned char * build_id_bits,size_t build_id_len)88 __libdwfl_debuginfod_find_debuginfo (Dwfl *dwfl,
89 				     const unsigned char *build_id_bits,
90 				     size_t build_id_len)
91 {
92   int fd = -1;
93   if (build_id_len > 0)
94     {
95       debuginfod_client *c = INTUSE (dwfl_get_debuginfod_client) (dwfl);
96       if (c != NULL)
97 	fd = (*fp_debuginfod_find_debuginfo) (c, build_id_bits,
98 					      build_id_len, NULL);
99     }
100 
101   return fd;
102 }
103 
104 void
__libdwfl_debuginfod_end(debuginfod_client * c)105 __libdwfl_debuginfod_end (debuginfod_client *c)
106 {
107   if (c != NULL)
108     (*fp_debuginfod_end) (c);
109 }
110 
111 /* Try to get the libdebuginfod library functions.
112    Only needs to be called once from dwfl_get_debuginfod_client.  */
113 static void
__libdwfl_debuginfod_init(void)114 __libdwfl_debuginfod_init (void)
115 {
116   void *debuginfod_so = dlopen(DEBUGINFOD_SONAME, RTLD_LAZY);
117 
118   if (debuginfod_so != NULL)
119     {
120       fp_debuginfod_begin = dlsym (debuginfod_so, "debuginfod_begin");
121       fp_debuginfod_find_executable = dlsym (debuginfod_so,
122 					     "debuginfod_find_executable");
123       fp_debuginfod_find_debuginfo = dlsym (debuginfod_so,
124 					    "debuginfod_find_debuginfo");
125       fp_debuginfod_end = dlsym (debuginfod_so, "debuginfod_end");
126 
127       /* We either get them all, or we get none.  */
128       if (fp_debuginfod_begin == NULL
129 	  || fp_debuginfod_find_executable == NULL
130 	  || fp_debuginfod_find_debuginfo == NULL
131 	  || fp_debuginfod_end == NULL)
132 	{
133 	  fp_debuginfod_begin = NULL;
134 	  fp_debuginfod_find_executable = NULL;
135 	  fp_debuginfod_find_debuginfo = NULL;
136 	  fp_debuginfod_end = NULL;
137 	  dlclose (debuginfod_so);
138 	}
139     }
140 }
141 
142 #else // ENABLE_LIBDEBUGINFOD
143 
144 debuginfod_client *
dwfl_get_debuginfod_client(Dwfl * dummy)145 dwfl_get_debuginfod_client (Dwfl *dummy __attribute__ ((unused)))
146 {
147   return NULL;
148 }
149 
150 #endif // ENABLE_LIBDEBUGINFOD
151