/src/elfutils/libdw/dwarf_getalt.c
Line | Count | Source |
1 | | /* Retrieves the DWARF descriptor for debugaltlink data. |
2 | | Copyright (C) 2014, 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 "system.h" |
37 | | |
38 | | #include <inttypes.h> |
39 | | #include <fcntl.h> |
40 | | #include <limits.h> |
41 | | #include <stdlib.h> |
42 | | #include <stdio.h> |
43 | | #include <string.h> |
44 | | #include <sys/types.h> |
45 | | #include <sys/stat.h> |
46 | | |
47 | | |
48 | | char * |
49 | | internal_function |
50 | | __libdw_filepath (const char *debugdir, const char *dir, const char *file) |
51 | 0 | { |
52 | 0 | if (file == NULL) |
53 | 0 | return NULL; |
54 | | |
55 | 0 | if (file[0] == '/') |
56 | 0 | return strdup (file); |
57 | | |
58 | 0 | if (dir != NULL && dir[0] == '/') |
59 | 0 | { |
60 | 0 | size_t dirlen = strlen (dir); |
61 | 0 | size_t filelen = strlen (file); |
62 | 0 | size_t len = dirlen + 1 + filelen + 1; |
63 | 0 | char *path = malloc (len); |
64 | 0 | if (path != NULL) |
65 | 0 | { |
66 | 0 | char *c = mempcpy (path, dir, dirlen); |
67 | 0 | if (dir[dirlen - 1] != '/') |
68 | 0 | *c++ = '/'; |
69 | 0 | mempcpy (c, file, filelen + 1); |
70 | 0 | } |
71 | 0 | return path; |
72 | 0 | } |
73 | | |
74 | 0 | if (debugdir != NULL) |
75 | 0 | { |
76 | 0 | size_t debugdirlen = strlen (debugdir); |
77 | 0 | size_t dirlen = dir != NULL ? strlen (dir) : 0; |
78 | 0 | size_t filelen = strlen (file); |
79 | 0 | size_t len = debugdirlen + 1 + dirlen + 1 + filelen + 1; |
80 | 0 | char *path = malloc (len); |
81 | 0 | if (path != NULL) |
82 | 0 | { |
83 | 0 | char *c = mempcpy (path, debugdir, debugdirlen); |
84 | 0 | if (dirlen > 0) |
85 | 0 | { |
86 | 0 | c = mempcpy (c, dir, dirlen); |
87 | 0 | if (dir[dirlen - 1] != '/') |
88 | 0 | *c++ = '/'; |
89 | 0 | } |
90 | 0 | mempcpy (c, file, filelen + 1); |
91 | 0 | return path; |
92 | 0 | } |
93 | 0 | } |
94 | | |
95 | 0 | return NULL; |
96 | 0 | } |
97 | | |
98 | | static void |
99 | | find_debug_altlink (Dwarf *dbg) |
100 | 0 | { |
101 | 0 | const char *altname; |
102 | 0 | const void *build_id; |
103 | 0 | ssize_t build_id_len = INTUSE(dwelf_dwarf_gnu_debugaltlink) (dbg, |
104 | 0 | &altname, |
105 | 0 | &build_id); |
106 | | |
107 | | /* Couldn't even get the debugaltlink. It probably doesn't exist. */ |
108 | 0 | if (build_id_len <= 0) |
109 | 0 | return; |
110 | | |
111 | 0 | const uint8_t *id = (const uint8_t *) build_id; |
112 | 0 | size_t id_len = build_id_len; |
113 | 0 | int fd = -1; |
114 | | |
115 | | /* We only look in the standard path. And relative to the dbg file. */ |
116 | 0 | #define DEBUGINFO_PATH "/usr/lib/debug" |
117 | | |
118 | | /* We don't handle very short or really large build-ids. We need at |
119 | | at least 3 and allow for up to 64 (normally ids are 20 long). */ |
120 | 0 | #define MIN_BUILD_ID_BYTES 3 |
121 | 0 | #define MAX_BUILD_ID_BYTES 64 |
122 | 0 | if (id_len >= MIN_BUILD_ID_BYTES && id_len <= MAX_BUILD_ID_BYTES) |
123 | 0 | { |
124 | | /* Note sizeof a string literal includes the trailing zero. */ |
125 | 0 | char id_path[sizeof DEBUGINFO_PATH - 1 + sizeof "/.build-id/" - 1 |
126 | 0 | + 2 + 1 + (MAX_BUILD_ID_BYTES - 1) * 2 + sizeof ".debug"]; |
127 | 0 | sprintf (&id_path[0], "%s%s", DEBUGINFO_PATH, "/.build-id/"); |
128 | 0 | sprintf (&id_path[sizeof DEBUGINFO_PATH - 1 + sizeof "/.build-id/" - 1], |
129 | 0 | "%02" PRIx8 "/", (uint8_t) id[0]); |
130 | 0 | for (size_t i = 1; i < id_len; ++i) |
131 | 0 | sprintf (&id_path[sizeof DEBUGINFO_PATH - 1 + sizeof "/.build-id/" - 1 |
132 | 0 | + 3 + (i - 1) * 2], "%02" PRIx8, (uint8_t) id[i]); |
133 | 0 | strcpy (&id_path[sizeof DEBUGINFO_PATH - 1 + sizeof "/.build-id/" - 1 |
134 | 0 | + 3 + (id_len - 1) * 2], ".debug"); |
135 | |
|
136 | 0 | fd = TEMP_FAILURE_RETRY (open (id_path, O_RDONLY)); |
137 | 0 | } |
138 | | |
139 | | /* Fall back on (possible relative) alt file path. */ |
140 | 0 | if (fd < 0) |
141 | 0 | { |
142 | 0 | char *altpath = __libdw_filepath (dbg->debugdir, NULL, altname); |
143 | 0 | if (altpath != NULL) |
144 | 0 | { |
145 | 0 | fd = TEMP_FAILURE_RETRY (open (altpath, O_RDONLY)); |
146 | 0 | free (altpath); |
147 | 0 | } |
148 | 0 | } |
149 | |
|
150 | 0 | if (fd >= 0) |
151 | 0 | { |
152 | 0 | Dwarf *alt = dwarf_begin (fd, O_RDONLY); |
153 | 0 | if (alt != NULL) |
154 | 0 | { |
155 | 0 | dbg->alt_dwarf = alt; |
156 | 0 | dbg->alt_fd = fd; |
157 | 0 | } |
158 | 0 | else |
159 | 0 | close (fd); |
160 | 0 | } |
161 | 0 | } |
162 | | |
163 | | /* find_debug_altlink() modifies "dbg->alt_dwarf". |
164 | | dwarf_getalt() reads "main->alt_dwarf". |
165 | | Mutual exclusion is enforced to prevent a race. */ |
166 | | |
167 | | Dwarf * |
168 | | dwarf_getalt (Dwarf *main) |
169 | 0 | { |
170 | 0 | if (main == NULL) |
171 | 0 | return NULL; |
172 | | |
173 | 0 | mutex_lock (main->dwarf_lock); |
174 | | |
175 | | /* Only try once. */ |
176 | 0 | if (main->alt_dwarf == (void *) -1) |
177 | 0 | { |
178 | 0 | mutex_unlock (main->dwarf_lock); |
179 | 0 | return NULL; |
180 | 0 | } |
181 | | |
182 | | /* Assign result before releasing the lock. */ |
183 | 0 | Dwarf *result; |
184 | |
|
185 | 0 | if (main->alt_dwarf != NULL) |
186 | 0 | { |
187 | 0 | result = main->alt_dwarf; |
188 | 0 | mutex_unlock (main->dwarf_lock); |
189 | 0 | return result; |
190 | 0 | } |
191 | | |
192 | 0 | find_debug_altlink (main); |
193 | | |
194 | | /* If we found nothing, make sure we don't try again. */ |
195 | 0 | if (main->alt_dwarf == NULL) |
196 | 0 | { |
197 | 0 | main->alt_dwarf = (void *) -1; |
198 | 0 | mutex_unlock (main->dwarf_lock); |
199 | 0 | return NULL; |
200 | 0 | } |
201 | | |
202 | 0 | result = main->alt_dwarf; |
203 | 0 | mutex_unlock (main->dwarf_lock); |
204 | |
|
205 | 0 | return result; |
206 | 0 | } |
207 | | INTDEF (dwarf_getalt) |