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 (legacy) 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:
79 A double ".." means "::" and a single "." means "-".
81 The only characters allowed in the mangled symbol are a-zA-Z0-9 and _.:$ */
83 static const char *hash_prefix
= "::h";
84 static const size_t hash_prefix_len
= 3;
85 static const size_t hash_len
= 16;
87 static int is_prefixed_hash (const char *start
);
88 static int parse_lower_hex_nibble (char nibble
);
89 static char parse_legacy_escape (const char **in
);
91 /* INPUT: sym: symbol that has been through C++ (gnu v3) demangling
93 This function looks for the following indicators:
95 1. The hash must consist of "h" followed by 16 lowercase hex digits.
97 2. As a sanity check, the hash must use between 5 and 15 of the 16
98 possible hex digits. This is true of 99.9998% of hashes so once
99 in your life you may see a false negative. The point is to
100 notice path components that could be Rust hashes but are
101 probably not, like "haaaaaaaaaaaaaaaa". In this case a false
102 positive (non-Rust symbol has an important path component
103 removed because it looks like a Rust hash) is worse than a false
104 negative (the rare Rust symbol is not demangled) so this sets
105 the balance in favor of false negatives.
107 3. There must be no characters other than a-zA-Z0-9 and _.:$ */
110 rust_is_mangled (const char *sym
)
112 size_t len
, len_without_hash
;
119 if (len
<= hash_prefix_len
+ hash_len
)
120 /* Not long enough to contain "::h" + hash + something else */
123 len_without_hash
= len
- (hash_prefix_len
+ hash_len
);
124 if (!is_prefixed_hash (sym
+ len_without_hash
))
127 end
= sym
+ len_without_hash
;
131 if (*sym
== '$' || *sym
== '.' || *sym
== '_' || *sym
== ':'
141 /* A hash is the prefix "::h" followed by 16 lowercase hex digits. The
142 hex digits must contain at least 5 distinct digits. */
145 is_prefixed_hash (const char *str
)
152 if (strncmp (str
, hash_prefix
, hash_prefix_len
))
154 str
+= hash_prefix_len
;
156 memset (seen
, 0, sizeof(seen
));
157 for (end
= str
+ hash_len
; str
< end
; str
++)
159 nibble
= parse_lower_hex_nibble (*str
);
165 /* Count how many distinct digits seen */
167 for (i
= 0; i
< 16; i
++)
175 INPUT: sym: symbol for which rust_is_mangled(sym) returned 1.
177 The input is demangled in-place because the mangled name is always
178 longer than the demangled one. */
181 rust_demangle_sym (char *sym
)
193 end
= sym
+ strlen (sym
) - (hash_prefix_len
+ hash_len
);
199 unescaped
= parse_legacy_escape (&in
);
203 /* unexpected escape sequence, skip the rest of this segment. */
204 while (in
< end
&& *in
!= ':')
209 /* If this is the start of a path component and the next
210 character is an escape sequence, ignore the underscore. The
211 mangler inserts an underscore to make sure the path
212 component begins with a XID_Start character. */
213 if ((in
== sym
|| in
[-1] == ':') && in
[1] == '$')
222 /* ".." becomes "::" */
229 /* "." becomes "-" */
234 else if (*in
== ':' || ISALNUM (*in
))
238 /* unexpected character in symbol, not rust_is_mangled. */
239 *out
++ = '?'; /* This is pretty lame, but it's hard to do better. */
248 /* Return a 0x0-0xf value if the char is 0-9a-f, and -1 otherwise. */
250 parse_lower_hex_nibble (char nibble
)
252 if ('0' <= nibble
&& nibble
<= '9')
254 if ('a' <= nibble
&& nibble
<= 'f')
255 return 0xa + (nibble
- 'a');
259 /* Return the unescaped character for a "$...$" escape, or 0 if invalid. */
261 parse_legacy_escape (const char **in
)
265 size_t escape_len
= 0;
266 int lo_nibble
= -1, hi_nibble
= -1;
283 if (e
[0] == 'S' && e
[1] == 'P')
285 else if (e
[0] == 'B' && e
[1] == 'P')
287 else if (e
[0] == 'R' && e
[1] == 'F')
289 else if (e
[0] == 'L' && e
[1] == 'T')
291 else if (e
[0] == 'G' && e
[1] == 'T')
293 else if (e
[0] == 'L' && e
[1] == 'P')
295 else if (e
[0] == 'R' && e
[1] == 'P')
297 else if (e
[0] == 'u')
301 hi_nibble
= parse_lower_hex_nibble (e
[1]);
304 lo_nibble
= parse_lower_hex_nibble (e
[2]);
308 /* Only allow non-control ASCII characters. */
311 c
= (hi_nibble
<< 4) | lo_nibble
;
317 if (!c
|| e
[escape_len
] != '$')
320 *in
+= 2 + escape_len
;