• 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    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 either
7 
8      * the GNU Lesser General Public License as published by the Free
9        Software Foundation; either version 3 of the License, or (at
10        your option) any later version
11 
12    or
13 
14      * the GNU General Public License as published by the Free
15        Software Foundation; either version 2 of the License, or (at
16        your option) any later version
17 
18    or both in parallel, as here.
19 
20    elfutils is distributed in the hope that it will be useful, but
21    WITHOUT ANY WARRANTY; without even the implied warranty of
22    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23    General Public License for more details.
24 
25    You should have received copies of the GNU General Public License and
26    the GNU Lesser General Public License along with this program.  If
27    not, see <http://www.gnu.org/licenses/>.  */
28 
29 #ifdef HAVE_CONFIG_H
30 # include <config.h>
31 #endif
32 
33 #include "libdwflP.h"
34 #include <dlfcn.h>
35 
36 static __typeof__ (debuginfod_begin) *fp_debuginfod_begin;
37 static __typeof__ (debuginfod_find_executable) *fp_debuginfod_find_executable;
38 static __typeof__ (debuginfod_find_debuginfo) *fp_debuginfod_find_debuginfo;
39 static __typeof__ (debuginfod_end) *fp_debuginfod_end;
40 
41 /* NB: this is slightly thread-unsafe */
42 
43 static debuginfod_client *
get_client(Dwfl * dwfl)44 get_client (Dwfl *dwfl)
45 {
46   if (dwfl->debuginfod != NULL)
47     return dwfl->debuginfod;
48 
49   if (fp_debuginfod_begin != NULL)
50     {
51       dwfl->debuginfod = (*fp_debuginfod_begin) ();
52       return dwfl->debuginfod;
53     }
54 
55   return NULL;
56 }
57 
58 int
__libdwfl_debuginfod_find_executable(Dwfl * dwfl,const unsigned char * build_id_bits,size_t build_id_len)59 __libdwfl_debuginfod_find_executable (Dwfl *dwfl,
60 				      const unsigned char *build_id_bits,
61 				      size_t build_id_len)
62 {
63   int fd = -1;
64   if (build_id_len > 0)
65     {
66       debuginfod_client *c = get_client (dwfl);
67       if (c != NULL)
68 	fd = (*fp_debuginfod_find_executable) (c, build_id_bits,
69 					       build_id_len, NULL);
70     }
71 
72   return fd;
73 }
74 
75 int
__libdwfl_debuginfod_find_debuginfo(Dwfl * dwfl,const unsigned char * build_id_bits,size_t build_id_len)76 __libdwfl_debuginfod_find_debuginfo (Dwfl *dwfl,
77 				     const unsigned char *build_id_bits,
78 				     size_t build_id_len)
79 {
80   int fd = -1;
81   if (build_id_len > 0)
82     {
83       debuginfod_client *c = get_client (dwfl);
84       if (c != NULL)
85 	fd = (*fp_debuginfod_find_debuginfo) (c, build_id_bits,
86 					      build_id_len, NULL);
87     }
88 
89   return fd;
90 }
91 
92 void
__libdwfl_debuginfod_end(debuginfod_client * c)93 __libdwfl_debuginfod_end (debuginfod_client *c)
94 {
95   if (c != NULL)
96     (*fp_debuginfod_end) (c);
97 }
98 
99 /* Try to get the libdebuginfod library functions to make sure
100    everything is initialized early.  */
101 void __attribute__ ((constructor))
__libdwfl_debuginfod_init(void)102 __libdwfl_debuginfod_init (void)
103 {
104   void *debuginfod_so = dlopen(DEBUGINFOD_SONAME, RTLD_LAZY);
105 
106   if (debuginfod_so != NULL)
107     {
108       fp_debuginfod_begin = dlsym (debuginfod_so, "debuginfod_begin");
109       fp_debuginfod_find_executable = dlsym (debuginfod_so,
110 					     "debuginfod_find_executable");
111       fp_debuginfod_find_debuginfo = dlsym (debuginfod_so,
112 					    "debuginfod_find_debuginfo");
113       fp_debuginfod_end = dlsym (debuginfod_so, "debuginfod_end");
114 
115       /* We either get them all, or we get none.  */
116       if (fp_debuginfod_begin == NULL
117 	  || fp_debuginfod_find_executable == NULL
118 	  || fp_debuginfod_find_debuginfo == NULL
119 	  || fp_debuginfod_end == NULL)
120 	{
121 	  fp_debuginfod_begin = NULL;
122 	  fp_debuginfod_find_executable = NULL;
123 	  fp_debuginfod_find_debuginfo = NULL;
124 	  fp_debuginfod_end = NULL;
125 	  dlclose (debuginfod_so);
126 	}
127     }
128 }
129