1 /* Demangler for the Rust programming language
2 Copyright (C) 2016-2019 Free Software Foundation, Inc.
3 Written by David Tolnay (dtolnay@gmail.com).
5 This file is part of the libiberty library.
6 Libiberty is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
11 In addition to the permissions in the GNU Library General Public
12 License, the Free Software Foundation gives you unlimited permission
13 to link the compiled version of this file into combinations with other
14 programs, and to distribute those combinations without any restriction
15 coming from the use of this file. (The Library Public License
16 restrictions do apply in other respects; for example, they cover
17 modification of the file, and distribution when not linked into a
20 Libiberty is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 Library General Public License for more details.
25 You should have received a copy of the GNU Library General Public
26 License along with libiberty; see the file COPYING.LIB.
27 If not, see <http://www.gnu.org/licenses/>. */
34 #include "safe-ctype.h"
36 #include <sys/types.h>
43 extern size_t strlen(const char *s
);
44 extern int strncmp(const char *s1
, const char *s2
, size_t n
);
45 extern void *memset(void *s
, int c
, size_t n
);
49 #include "libiberty.h"
50 #include "rust-demangle.h"
53 /* Mangled Rust symbols look like this:
54 _$LT$std..sys..fd..FileDesc$u20$as$u20$core..ops..Drop$GT$::drop::hc68340e1baa4987a
56 The original symbol is:
57 <std::sys::fd::FileDesc as core::ops::Drop>::drop
59 The last component of the path is a 64-bit hash in lowercase hex,
60 prefixed with "h". Rust does not have a global namespace between
61 crates, an illusion which Rust maintains by using the hash to
62 distinguish things that would otherwise have the same symbol.
64 Any path component not starting with a XID_Start character is
67 The following escape sequences are used:
88 A double ".." means "::" and a single "." means "-".
90 The only characters allowed in the mangled symbol are a-zA-Z0-9 and _.:$ */
92 static const char *hash_prefix
= "::h";
93 static const size_t hash_prefix_len
= 3;
94 static const size_t hash_len
= 16;
96 static int is_prefixed_hash (const char *start
);
97 static int looks_like_rust (const char *sym
, size_t len
);
98 static int unescape (const char **in
, char **out
, const char *seq
, char value
);
100 /* INPUT: sym: symbol that has been through C++ (gnu v3) demangling
102 This function looks for the following indicators:
104 1. The hash must consist of "h" followed by 16 lowercase hex digits.
106 2. As a sanity check, the hash must use between 5 and 15 of the 16
107 possible hex digits. This is true of 99.9998% of hashes so once
108 in your life you may see a false negative. The point is to
109 notice path components that could be Rust hashes but are
110 probably not, like "haaaaaaaaaaaaaaaa". In this case a false
111 positive (non-Rust symbol has an important path component
112 removed because it looks like a Rust hash) is worse than a false
113 negative (the rare Rust symbol is not demangled) so this sets
114 the balance in favor of false negatives.
116 3. There must be no characters other than a-zA-Z0-9 and _.:$
118 4. There must be no unrecognized $-sign sequences.
120 5. There must be no sequence of three or more dots in a row ("..."). */
123 rust_is_mangled (const char *sym
)
125 size_t len
, len_without_hash
;
131 if (len
<= hash_prefix_len
+ hash_len
)
132 /* Not long enough to contain "::h" + hash + something else */
135 len_without_hash
= len
- (hash_prefix_len
+ hash_len
);
136 if (!is_prefixed_hash (sym
+ len_without_hash
))
139 return looks_like_rust (sym
, len_without_hash
);
142 /* A hash is the prefix "::h" followed by 16 lowercase hex digits. The
143 hex digits must comprise between 5 and 15 (inclusive) distinct
147 is_prefixed_hash (const char *str
)
154 if (strncmp (str
, hash_prefix
, hash_prefix_len
))
156 str
+= hash_prefix_len
;
158 memset (seen
, 0, sizeof(seen
));
159 for (end
= str
+ hash_len
; str
< end
; str
++)
160 if (*str
>= '0' && *str
<= '9')
161 seen
[*str
- '0'] = 1;
162 else if (*str
>= 'a' && *str
<= 'f')
163 seen
[*str
- 'a' + 10] = 1;
167 /* Count how many distinct digits seen */
169 for (i
= 0; i
< 16; i
++)
173 return count
>= 5 && count
<= 15;
177 looks_like_rust (const char *str
, size_t len
)
179 const char *end
= str
+ len
;
185 if (!strncmp (str
, "$C$", 3))
187 else if (!strncmp (str
, "$SP$", 4)
188 || !strncmp (str
, "$BP$", 4)
189 || !strncmp (str
, "$RF$", 4)
190 || !strncmp (str
, "$LT$", 4)
191 || !strncmp (str
, "$GT$", 4)
192 || !strncmp (str
, "$LP$", 4)
193 || !strncmp (str
, "$RP$", 4))
195 else if (!strncmp (str
, "$u20$", 5)
196 || !strncmp (str
, "$u22$", 5)
197 || !strncmp (str
, "$u27$", 5)
198 || !strncmp (str
, "$u2b$", 5)
199 || !strncmp (str
, "$u3b$", 5)
200 || !strncmp (str
, "$u5b$", 5)
201 || !strncmp (str
, "$u5d$", 5)
202 || !strncmp (str
, "$u7b$", 5)
203 || !strncmp (str
, "$u7d$", 5)
204 || !strncmp (str
, "$u7e$", 5))
210 /* Do not allow three or more consecutive dots */
211 if (!strncmp (str
, "...", 3))
214 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
215 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
216 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
217 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
219 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
220 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
221 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
222 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
224 case '0': case '1': case '2': case '3': case '4': case '5':
225 case '6': case '7': case '8': case '9':
238 INPUT: sym: symbol for which rust_is_mangled(sym) returned 1.
240 The input is demangled in-place because the mangled name is always
241 longer than the demangled one. */
244 rust_demangle_sym (char *sym
)
255 end
= sym
+ strlen (sym
) - (hash_prefix_len
+ hash_len
);
261 if (!(unescape (&in
, &out
, "$C$", ',')
262 || unescape (&in
, &out
, "$SP$", '@')
263 || unescape (&in
, &out
, "$BP$", '*')
264 || unescape (&in
, &out
, "$RF$", '&')
265 || unescape (&in
, &out
, "$LT$", '<')
266 || unescape (&in
, &out
, "$GT$", '>')
267 || unescape (&in
, &out
, "$LP$", '(')
268 || unescape (&in
, &out
, "$RP$", ')')
269 || unescape (&in
, &out
, "$u20$", ' ')
270 || unescape (&in
, &out
, "$u22$", '\"')
271 || unescape (&in
, &out
, "$u27$", '\'')
272 || unescape (&in
, &out
, "$u2b$", '+')
273 || unescape (&in
, &out
, "$u3b$", ';')
274 || unescape (&in
, &out
, "$u5b$", '[')
275 || unescape (&in
, &out
, "$u5d$", ']')
276 || unescape (&in
, &out
, "$u7b$", '{')
277 || unescape (&in
, &out
, "$u7d$", '}')
278 || unescape (&in
, &out
, "$u7e$", '~'))) {
279 /* unexpected escape sequence, not looks_like_rust. */
284 /* If this is the start of a path component and the next
285 character is an escape sequence, ignore the underscore. The
286 mangler inserts an underscore to make sure the path
287 component begins with a XID_Start character. */
288 if ((in
== sym
|| in
[-1] == ':') && in
[1] == '$')
296 /* ".." becomes "::" */
303 /* "." becomes "-" */
308 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
309 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
310 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
311 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
313 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
314 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
315 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
316 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
318 case '0': case '1': case '2': case '3': case '4': case '5':
319 case '6': case '7': case '8': case '9':
324 /* unexpected character in symbol, not looks_like_rust. */
330 *out
++ = '?'; /* This is pretty lame, but it's hard to do better. */
336 unescape (const char **in
, char **out
, const char *seq
, char value
)
338 size_t len
= strlen (seq
);
340 if (strncmp (*in
, seq
, len
))
This page took 0.047922 seconds and 4 git commands to generate.