Branch data Line data Source code
1 : : /* Find the split (or skeleton) unit for a given unit.
2 : : Copyright (C) 2018 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 "libdwP.h"
34 : : #include "libelfP.h"
35 : : #include "libdwelfP.h"
36 : : #include "eu-search.h"
37 : :
38 : : #include <limits.h>
39 : : #include <stdlib.h>
40 : : #include <string.h>
41 : : #include <sys/types.h>
42 : : #include <sys/stat.h>
43 : : #include <fcntl.h>
44 : :
45 : : static void
46 : 136 : link_debug_addr (Dwarf *dbg, Dwarf *sdbg)
47 : : {
48 [ + - ]: 136 : if (dbg->sectiondata[IDX_debug_addr] != NULL
49 [ + - ]: 136 : && sdbg->sectiondata[IDX_debug_addr] == NULL)
50 : : {
51 : 136 : sdbg->sectiondata[IDX_debug_addr] = dbg->sectiondata[IDX_debug_addr];
52 : 136 : sdbg->fake_addr_cu = dbg->fake_addr_cu;
53 : : }
54 : 136 : }
55 : :
56 : : static void
57 : 112 : try_split_file (Dwarf_CU *cu, const char *dwo_path)
58 : : {
59 : 112 : int split_fd = open (dwo_path, O_RDONLY);
60 [ + + ]: 112 : if (split_fd != -1)
61 : : {
62 : 110 : Dwarf *split_dwarf = INTUSE(dwarf_begin_type) (split_fd, DWARF_C_READ,
63 : : DWARF_T_DWO);
64 [ + - ]: 110 : if (split_dwarf != NULL)
65 : : {
66 : 110 : Dwarf_CU *split = NULL;
67 : 110 : while (INTUSE(dwarf_get_units) (split_dwarf, split, &split,
68 [ + + ]: 112 : NULL, NULL, NULL, NULL) == 0)
69 : : {
70 [ + - ]: 110 : if (split->unit_type == DW_UT_split_compile
71 [ + + ]: 110 : && cu->unit_id8 == split->unit_id8)
72 : : {
73 : 108 : link_debug_addr (cu->dbg, split->dbg);
74 : :
75 [ - + ]: 108 : if (eu_tsearch (split->dbg, &cu->dbg->split_tree,
76 : : __libdw_finddbg_cb) == NULL)
77 : : {
78 : : /* Something went wrong. Don't link. */
79 : 0 : __libdw_seterrno (DWARF_E_NOMEM);
80 : 0 : break;
81 : : }
82 : :
83 : : /* Link skeleton and split compile units. */
84 : 108 : __libdw_link_skel_split (cu, split);
85 : :
86 : : /* We have everything we need from this ELF
87 : : file. And we are going to close the fd to
88 : : not run out of file descriptors. */
89 : 108 : elf_cntl (split_dwarf->elf, ELF_C_FDDONE);
90 : 108 : break;
91 : : }
92 : : }
93 [ + + ]: 110 : if (cu->split == (Dwarf_CU *) -1)
94 : 2 : INTUSE (dwarf_end) (split_dwarf);
95 : : }
96 : : /* Always close, because we don't want to run out of file
97 : : descriptors. See also the elf_fcntl ELF_C_FDDONE call
98 : : above. */
99 : 110 : close (split_fd);
100 : : }
101 : 112 : }
102 : :
103 : : static void
104 : 198 : try_dwp_file (Dwarf_CU *cu)
105 : : {
106 : 198 : mutex_lock (cu->dbg->dwp_lock);
107 : :
108 [ + + ]: 198 : if (cu->dbg->dwp_dwarf == NULL)
109 : : {
110 : 98 : uint8_t is_dwp;
111 : 98 : const char *filename;
112 : 98 : const uint8_t *dwp_id;
113 : 98 : int dwp_fd = -1;
114 : :
115 : 98 : ssize_t idlen = INTUSE(dwelf_dwarf_debug_dwp) (cu->dbg, NULL, &is_dwp,
116 : : &filename, &dwp_id);
117 [ + - + + : 98 : if (idlen >= 0 && filename != NULL && is_dwp == 0 && *filename != '\0')
+ - + - ]
118 : : {
119 : : /* XXX we need to set standards where/how to find id based
120 : : dwp files, including debuginfod support. For now only use
121 : : the file path. */
122 : 2 : char *path = __libdw_filepath (cu->dbg->debugdir, NULL, filename);
123 [ + - ]: 2 : if (path != NULL)
124 : : {
125 [ - + - - ]: 2 : dwp_fd = TEMP_FAILURE_RETRY (open (path, O_RDONLY));
126 : 2 : free (path);
127 : : }
128 : : }
129 : :
130 [ - + + - ]: 98 : if (dwp_fd == -1 && cu->dbg->elfpath != NULL)
131 : : {
132 : : /* The DWARF 5 standard says "the package file is typically placed in
133 : : the same directory as the application, and is given the same name
134 : : with a '.dwp' extension". */
135 : 96 : size_t elfpath_len = strlen (cu->dbg->elfpath);
136 : 96 : char *dwp_path = malloc (elfpath_len + 5);
137 [ - + ]: 96 : if (dwp_path == NULL)
138 : : {
139 : 0 : __libdw_seterrno (DWARF_E_NOMEM);
140 : 0 : mutex_unlock (cu->dbg->dwp_lock);
141 : 0 : return;
142 : : }
143 : 96 : memcpy (dwp_path, cu->dbg->elfpath, elfpath_len);
144 : 96 : strcpy (dwp_path + elfpath_len, ".dwp");
145 [ + + - + ]: 96 : dwp_fd = TEMP_FAILURE_RETRY (open (dwp_path, O_RDONLY));
146 : 96 : free (dwp_path);
147 : : }
148 : :
149 [ + + ]: 96 : if (dwp_fd != -1)
150 : : {
151 : 28 : Dwarf *dwp_dwarf = INTUSE(dwarf_begin_type) (dwp_fd, DWARF_C_READ,
152 : : DWARF_T_DWO);
153 : : /* There's no way to know whether we got the correct file until
154 : : we look up the unit, but it should at least be a dwp file. */
155 [ + - ]: 28 : if (dwp_dwarf != NULL
156 [ - + ]: 28 : && (dwp_dwarf->sectiondata[IDX_debug_cu_index] != NULL
157 [ # # ]: 0 : || dwp_dwarf->sectiondata[IDX_debug_tu_index] != NULL))
158 : : {
159 : 28 : link_debug_addr (cu->dbg, dwp_dwarf);
160 : 28 : cu->dbg->dwp_dwarf = dwp_dwarf;
161 : 28 : cu->dbg->dwp_fd = dwp_fd;
162 : : }
163 : : else
164 : 0 : close (dwp_fd);
165 : : }
166 [ + + ]: 98 : if (cu->dbg->dwp_dwarf == NULL)
167 : 70 : cu->dbg->dwp_dwarf = (Dwarf *) -1;
168 : : }
169 : :
170 : 198 : Dwarf *dwp_dwarf = cu->dbg->dwp_dwarf;
171 : 198 : mutex_unlock (cu->dbg->dwp_lock);
172 : :
173 [ + + ]: 198 : if (dwp_dwarf != (Dwarf *) -1)
174 : : {
175 : 88 : Dwarf_CU *split = __libdw_dwp_findcu_id (dwp_dwarf, cu->unit_id8);
176 [ + - ]: 88 : if (split != NULL)
177 : : {
178 [ - + ]: 88 : if (eu_tsearch (split->dbg, &cu->dbg->split_tree,
179 : : __libdw_finddbg_cb) == NULL)
180 : : {
181 : : /* Something went wrong. Don't link. */
182 : 0 : __libdw_seterrno (DWARF_E_NOMEM);
183 : 0 : return;
184 : : }
185 : :
186 : : /* Link skeleton and split compile units. */
187 : 88 : __libdw_link_skel_split (cu, split);
188 : : }
189 : : }
190 : : }
191 : :
192 : : Dwarf_CU *
193 : : internal_function
194 : 1108 : __libdw_find_split_unit (Dwarf_CU *cu)
195 : : {
196 : : /* Set result before releasing the lock. */
197 : 1108 : Dwarf_CU *result;
198 : :
199 : 1108 : rwlock_wrlock(cu->split_lock);
200 : :
201 : : /* Only try once. */
202 [ + + ]: 1108 : if (cu->split != (Dwarf_CU *) -1)
203 : : {
204 : 1108 : result = cu->split;
205 : : rwlock_unlock(cu->split_lock);
206 : : return result;
207 : : }
208 : :
209 : : /* We need a skeleton unit with a comp_dir and [GNU_]dwo_name attributes.
210 : : The split unit will be the first in the dwo file and should have the
211 : : same id as the skeleton. */
212 [ + - ]: 198 : if (cu->unit_type == DW_UT_skeleton)
213 : : {
214 : : /* First, try the dwp file. */
215 : 198 : try_dwp_file (cu);
216 : :
217 : 198 : Dwarf_Die cudie = CUDIE (cu);
218 : 198 : Dwarf_Attribute dwo_name;
219 : : /* Try a dwo file. It is fine if dwo_dir doesn't exist, but then
220 : : dwo_name needs to be an absolute path. */
221 [ + + ]: 198 : if (cu->split == (Dwarf_CU *) -1
222 [ + + ]: 110 : && (dwarf_attr (&cudie, DW_AT_dwo_name, &dwo_name) != NULL
223 [ + - ]: 56 : || dwarf_attr (&cudie, DW_AT_GNU_dwo_name, &dwo_name) != NULL))
224 : : {
225 : : /* Try the dwo file name in the same directory
226 : : as we found the skeleton file. */
227 : 110 : const char *dwo_file = dwarf_formstring (&dwo_name);
228 : 110 : const char *debugdir = cu->dbg->debugdir;
229 : 110 : char *dwo_path = __libdw_filepath (debugdir, NULL, dwo_file);
230 [ + - ]: 110 : if (dwo_path != NULL)
231 : : {
232 : 110 : try_split_file (cu, dwo_path);
233 : 110 : free (dwo_path);
234 : : }
235 : :
236 [ + + ]: 110 : if (cu->split == (Dwarf_CU *) -1)
237 : : {
238 : : /* Try compdir plus dwo_name. */
239 : 2 : Dwarf_Attribute compdir;
240 : 2 : dwarf_attr (&cudie, DW_AT_comp_dir, &compdir);
241 : 2 : const char *dwo_dir = dwarf_formstring (&compdir);
242 [ + - ]: 2 : if (dwo_dir != NULL)
243 : : {
244 : 2 : dwo_path = __libdw_filepath (debugdir, dwo_dir, dwo_file);
245 [ + - ]: 2 : if (dwo_path != NULL)
246 : : {
247 : 2 : try_split_file (cu, dwo_path);
248 : 2 : free (dwo_path);
249 : : }
250 : : }
251 : : }
252 : : /* XXX If still not found we could try stripping dirs from the
253 : : comp_dir and adding them from the comp_dir, assuming
254 : : someone moved a whole build tree around. */
255 : : }
256 : : }
257 : :
258 : : /* If we found nothing, make sure we don't try again. */
259 [ + + ]: 198 : if (cu->split == (Dwarf_CU *) -1)
260 : 2 : cu->split = NULL;
261 : :
262 : 198 : result = cu->split;
263 : 198 : rwlock_unlock(cu->split_lock);
264 : 198 : return result;
265 : : }
|