1*38fd1498Szrj /* Demangler for the Rust programming language
2*38fd1498Szrj    Copyright (C) 2016-2018 Free Software Foundation, Inc.
3*38fd1498Szrj    Written by David Tolnay (dtolnay@gmail.com).
4*38fd1498Szrj 
5*38fd1498Szrj This file is part of the libiberty library.
6*38fd1498Szrj Libiberty is free software; you can redistribute it and/or
7*38fd1498Szrj modify it under the terms of the GNU Library General Public
8*38fd1498Szrj License as published by the Free Software Foundation; either
9*38fd1498Szrj version 2 of the License, or (at your option) any later version.
10*38fd1498Szrj 
11*38fd1498Szrj In addition to the permissions in the GNU Library General Public
12*38fd1498Szrj License, the Free Software Foundation gives you unlimited permission
13*38fd1498Szrj to link the compiled version of this file into combinations with other
14*38fd1498Szrj programs, and to distribute those combinations without any restriction
15*38fd1498Szrj coming from the use of this file.  (The Library Public License
16*38fd1498Szrj restrictions do apply in other respects; for example, they cover
17*38fd1498Szrj modification of the file, and distribution when not linked into a
18*38fd1498Szrj combined executable.)
19*38fd1498Szrj 
20*38fd1498Szrj Libiberty is distributed in the hope that it will be useful,
21*38fd1498Szrj but WITHOUT ANY WARRANTY; without even the implied warranty of
22*38fd1498Szrj MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23*38fd1498Szrj Library General Public License for more details.
24*38fd1498Szrj 
25*38fd1498Szrj You should have received a copy of the GNU Library General Public
26*38fd1498Szrj License along with libiberty; see the file COPYING.LIB.
27*38fd1498Szrj If not, see <http://www.gnu.org/licenses/>.  */
28*38fd1498Szrj 
29*38fd1498Szrj 
30*38fd1498Szrj #ifdef HAVE_CONFIG_H
31*38fd1498Szrj #include "config.h"
32*38fd1498Szrj #endif
33*38fd1498Szrj 
34*38fd1498Szrj #include "safe-ctype.h"
35*38fd1498Szrj 
36*38fd1498Szrj #include <sys/types.h>
37*38fd1498Szrj #include <string.h>
38*38fd1498Szrj #include <stdio.h>
39*38fd1498Szrj 
40*38fd1498Szrj #ifdef HAVE_STRING_H
41*38fd1498Szrj #include <string.h>
42*38fd1498Szrj #else
43*38fd1498Szrj extern size_t strlen(const char *s);
44*38fd1498Szrj extern int strncmp(const char *s1, const char *s2, size_t n);
45*38fd1498Szrj extern void *memset(void *s, int c, size_t n);
46*38fd1498Szrj #endif
47*38fd1498Szrj 
48*38fd1498Szrj #include <demangle.h>
49*38fd1498Szrj #include "libiberty.h"
50*38fd1498Szrj 
51*38fd1498Szrj 
52*38fd1498Szrj /* Mangled Rust symbols look like this:
53*38fd1498Szrj      _$LT$std..sys..fd..FileDesc$u20$as$u20$core..ops..Drop$GT$::drop::hc68340e1baa4987a
54*38fd1498Szrj 
55*38fd1498Szrj    The original symbol is:
56*38fd1498Szrj      <std::sys::fd::FileDesc as core::ops::Drop>::drop
57*38fd1498Szrj 
58*38fd1498Szrj    The last component of the path is a 64-bit hash in lowercase hex,
59*38fd1498Szrj    prefixed with "h". Rust does not have a global namespace between
60*38fd1498Szrj    crates, an illusion which Rust maintains by using the hash to
61*38fd1498Szrj    distinguish things that would otherwise have the same symbol.
62*38fd1498Szrj 
63*38fd1498Szrj    Any path component not starting with a XID_Start character is
64*38fd1498Szrj    prefixed with "_".
65*38fd1498Szrj 
66*38fd1498Szrj    The following escape sequences are used:
67*38fd1498Szrj 
68*38fd1498Szrj    ","  =>  $C$
69*38fd1498Szrj    "@"  =>  $SP$
70*38fd1498Szrj    "*"  =>  $BP$
71*38fd1498Szrj    "&"  =>  $RF$
72*38fd1498Szrj    "<"  =>  $LT$
73*38fd1498Szrj    ">"  =>  $GT$
74*38fd1498Szrj    "("  =>  $LP$
75*38fd1498Szrj    ")"  =>  $RP$
76*38fd1498Szrj    " "  =>  $u20$
77*38fd1498Szrj    "\"" =>  $u22$
78*38fd1498Szrj    "'"  =>  $u27$
79*38fd1498Szrj    "+"  =>  $u2b$
80*38fd1498Szrj    ";"  =>  $u3b$
81*38fd1498Szrj    "["  =>  $u5b$
82*38fd1498Szrj    "]"  =>  $u5d$
83*38fd1498Szrj    "{"  =>  $u7b$
84*38fd1498Szrj    "}"  =>  $u7d$
85*38fd1498Szrj    "~"  =>  $u7e$
86*38fd1498Szrj 
87*38fd1498Szrj    A double ".." means "::" and a single "." means "-".
88*38fd1498Szrj 
89*38fd1498Szrj    The only characters allowed in the mangled symbol are a-zA-Z0-9 and _.:$  */
90*38fd1498Szrj 
91*38fd1498Szrj static const char *hash_prefix = "::h";
92*38fd1498Szrj static const size_t hash_prefix_len = 3;
93*38fd1498Szrj static const size_t hash_len = 16;
94*38fd1498Szrj 
95*38fd1498Szrj static int is_prefixed_hash (const char *start);
96*38fd1498Szrj static int looks_like_rust (const char *sym, size_t len);
97*38fd1498Szrj static int unescape (const char **in, char **out, const char *seq, char value);
98*38fd1498Szrj 
99*38fd1498Szrj /* INPUT: sym: symbol that has been through C++ (gnu v3) demangling
100*38fd1498Szrj 
101*38fd1498Szrj    This function looks for the following indicators:
102*38fd1498Szrj 
103*38fd1498Szrj    1. The hash must consist of "h" followed by 16 lowercase hex digits.
104*38fd1498Szrj 
105*38fd1498Szrj    2. As a sanity check, the hash must use between 5 and 15 of the 16
106*38fd1498Szrj       possible hex digits. This is true of 99.9998% of hashes so once
107*38fd1498Szrj       in your life you may see a false negative. The point is to
108*38fd1498Szrj       notice path components that could be Rust hashes but are
109*38fd1498Szrj       probably not, like "haaaaaaaaaaaaaaaa". In this case a false
110*38fd1498Szrj       positive (non-Rust symbol has an important path component
111*38fd1498Szrj       removed because it looks like a Rust hash) is worse than a false
112*38fd1498Szrj       negative (the rare Rust symbol is not demangled) so this sets
113*38fd1498Szrj       the balance in favor of false negatives.
114*38fd1498Szrj 
115*38fd1498Szrj    3. There must be no characters other than a-zA-Z0-9 and _.:$
116*38fd1498Szrj 
117*38fd1498Szrj    4. There must be no unrecognized $-sign sequences.
118*38fd1498Szrj 
119*38fd1498Szrj    5. There must be no sequence of three or more dots in a row ("...").  */
120*38fd1498Szrj 
121*38fd1498Szrj int
rust_is_mangled(const char * sym)122*38fd1498Szrj rust_is_mangled (const char *sym)
123*38fd1498Szrj {
124*38fd1498Szrj   size_t len, len_without_hash;
125*38fd1498Szrj 
126*38fd1498Szrj   if (!sym)
127*38fd1498Szrj     return 0;
128*38fd1498Szrj 
129*38fd1498Szrj   len = strlen (sym);
130*38fd1498Szrj   if (len <= hash_prefix_len + hash_len)
131*38fd1498Szrj     /* Not long enough to contain "::h" + hash + something else */
132*38fd1498Szrj     return 0;
133*38fd1498Szrj 
134*38fd1498Szrj   len_without_hash = len - (hash_prefix_len + hash_len);
135*38fd1498Szrj   if (!is_prefixed_hash (sym + len_without_hash))
136*38fd1498Szrj     return 0;
137*38fd1498Szrj 
138*38fd1498Szrj   return looks_like_rust (sym, len_without_hash);
139*38fd1498Szrj }
140*38fd1498Szrj 
141*38fd1498Szrj /* A hash is the prefix "::h" followed by 16 lowercase hex digits. The
142*38fd1498Szrj    hex digits must comprise between 5 and 15 (inclusive) distinct
143*38fd1498Szrj    digits.  */
144*38fd1498Szrj 
145*38fd1498Szrj static int
is_prefixed_hash(const char * str)146*38fd1498Szrj is_prefixed_hash (const char *str)
147*38fd1498Szrj {
148*38fd1498Szrj   const char *end;
149*38fd1498Szrj   char seen[16];
150*38fd1498Szrj   size_t i;
151*38fd1498Szrj   int count;
152*38fd1498Szrj 
153*38fd1498Szrj   if (strncmp (str, hash_prefix, hash_prefix_len))
154*38fd1498Szrj     return 0;
155*38fd1498Szrj   str += hash_prefix_len;
156*38fd1498Szrj 
157*38fd1498Szrj   memset (seen, 0, sizeof(seen));
158*38fd1498Szrj   for (end = str + hash_len; str < end; str++)
159*38fd1498Szrj     if (*str >= '0' && *str <= '9')
160*38fd1498Szrj       seen[*str - '0'] = 1;
161*38fd1498Szrj     else if (*str >= 'a' && *str <= 'f')
162*38fd1498Szrj       seen[*str - 'a' + 10] = 1;
163*38fd1498Szrj     else
164*38fd1498Szrj       return 0;
165*38fd1498Szrj 
166*38fd1498Szrj   /* Count how many distinct digits seen */
167*38fd1498Szrj   count = 0;
168*38fd1498Szrj   for (i = 0; i < 16; i++)
169*38fd1498Szrj     if (seen[i])
170*38fd1498Szrj       count++;
171*38fd1498Szrj 
172*38fd1498Szrj   return count >= 5 && count <= 15;
173*38fd1498Szrj }
174*38fd1498Szrj 
175*38fd1498Szrj static int
looks_like_rust(const char * str,size_t len)176*38fd1498Szrj looks_like_rust (const char *str, size_t len)
177*38fd1498Szrj {
178*38fd1498Szrj   const char *end = str + len;
179*38fd1498Szrj 
180*38fd1498Szrj   while (str < end)
181*38fd1498Szrj     switch (*str)
182*38fd1498Szrj       {
183*38fd1498Szrj       case '$':
184*38fd1498Szrj 	if (!strncmp (str, "$C$", 3))
185*38fd1498Szrj 	  str += 3;
186*38fd1498Szrj 	else if (!strncmp (str, "$SP$", 4)
187*38fd1498Szrj 		 || !strncmp (str, "$BP$", 4)
188*38fd1498Szrj 		 || !strncmp (str, "$RF$", 4)
189*38fd1498Szrj 		 || !strncmp (str, "$LT$", 4)
190*38fd1498Szrj 		 || !strncmp (str, "$GT$", 4)
191*38fd1498Szrj 		 || !strncmp (str, "$LP$", 4)
192*38fd1498Szrj 		 || !strncmp (str, "$RP$", 4))
193*38fd1498Szrj 	  str += 4;
194*38fd1498Szrj 	else if (!strncmp (str, "$u20$", 5)
195*38fd1498Szrj 		 || !strncmp (str, "$u22$", 5)
196*38fd1498Szrj 		 || !strncmp (str, "$u27$", 5)
197*38fd1498Szrj 		 || !strncmp (str, "$u2b$", 5)
198*38fd1498Szrj 		 || !strncmp (str, "$u3b$", 5)
199*38fd1498Szrj 		 || !strncmp (str, "$u5b$", 5)
200*38fd1498Szrj 		 || !strncmp (str, "$u5d$", 5)
201*38fd1498Szrj 		 || !strncmp (str, "$u7b$", 5)
202*38fd1498Szrj 		 || !strncmp (str, "$u7d$", 5)
203*38fd1498Szrj 		 || !strncmp (str, "$u7e$", 5))
204*38fd1498Szrj 	  str += 5;
205*38fd1498Szrj 	else
206*38fd1498Szrj 	  return 0;
207*38fd1498Szrj 	break;
208*38fd1498Szrj       case '.':
209*38fd1498Szrj 	/* Do not allow three or more consecutive dots */
210*38fd1498Szrj 	if (!strncmp (str, "...", 3))
211*38fd1498Szrj 	  return 0;
212*38fd1498Szrj 	/* Fall through */
213*38fd1498Szrj       case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
214*38fd1498Szrj       case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
215*38fd1498Szrj       case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
216*38fd1498Szrj       case 's': case 't': case 'u': case 'v': case 'w': case 'x':
217*38fd1498Szrj       case 'y': case 'z':
218*38fd1498Szrj       case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
219*38fd1498Szrj       case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
220*38fd1498Szrj       case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
221*38fd1498Szrj       case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
222*38fd1498Szrj       case 'Y': case 'Z':
223*38fd1498Szrj       case '0': case '1': case '2': case '3': case '4': case '5':
224*38fd1498Szrj       case '6': case '7': case '8': case '9':
225*38fd1498Szrj       case '_':
226*38fd1498Szrj       case ':':
227*38fd1498Szrj 	str++;
228*38fd1498Szrj 	break;
229*38fd1498Szrj       default:
230*38fd1498Szrj 	return 0;
231*38fd1498Szrj       }
232*38fd1498Szrj 
233*38fd1498Szrj   return 1;
234*38fd1498Szrj }
235*38fd1498Szrj 
236*38fd1498Szrj /*
237*38fd1498Szrj   INPUT: sym: symbol for which rust_is_mangled(sym) returned 1.
238*38fd1498Szrj 
239*38fd1498Szrj   The input is demangled in-place because the mangled name is always
240*38fd1498Szrj   longer than the demangled one.  */
241*38fd1498Szrj 
242*38fd1498Szrj void
rust_demangle_sym(char * sym)243*38fd1498Szrj rust_demangle_sym (char *sym)
244*38fd1498Szrj {
245*38fd1498Szrj   const char *in;
246*38fd1498Szrj   char *out;
247*38fd1498Szrj   const char *end;
248*38fd1498Szrj 
249*38fd1498Szrj   if (!sym)
250*38fd1498Szrj     return;
251*38fd1498Szrj 
252*38fd1498Szrj   in = sym;
253*38fd1498Szrj   out = sym;
254*38fd1498Szrj   end = sym + strlen (sym) - (hash_prefix_len + hash_len);
255*38fd1498Szrj 
256*38fd1498Szrj   while (in < end)
257*38fd1498Szrj     switch (*in)
258*38fd1498Szrj       {
259*38fd1498Szrj       case '$':
260*38fd1498Szrj 	if (!(unescape (&in, &out, "$C$", ',')
261*38fd1498Szrj 	      || unescape (&in, &out, "$SP$", '@')
262*38fd1498Szrj 	      || unescape (&in, &out, "$BP$", '*')
263*38fd1498Szrj 	      || unescape (&in, &out, "$RF$", '&')
264*38fd1498Szrj 	      || unescape (&in, &out, "$LT$", '<')
265*38fd1498Szrj 	      || unescape (&in, &out, "$GT$", '>')
266*38fd1498Szrj 	      || unescape (&in, &out, "$LP$", '(')
267*38fd1498Szrj 	      || unescape (&in, &out, "$RP$", ')')
268*38fd1498Szrj 	      || unescape (&in, &out, "$u20$", ' ')
269*38fd1498Szrj 	      || unescape (&in, &out, "$u22$", '\"')
270*38fd1498Szrj 	      || unescape (&in, &out, "$u27$", '\'')
271*38fd1498Szrj 	      || unescape (&in, &out, "$u2b$", '+')
272*38fd1498Szrj 	      || unescape (&in, &out, "$u3b$", ';')
273*38fd1498Szrj 	      || unescape (&in, &out, "$u5b$", '[')
274*38fd1498Szrj 	      || unescape (&in, &out, "$u5d$", ']')
275*38fd1498Szrj 	      || unescape (&in, &out, "$u7b$", '{')
276*38fd1498Szrj 	      || unescape (&in, &out, "$u7d$", '}')
277*38fd1498Szrj 	      || unescape (&in, &out, "$u7e$", '~'))) {
278*38fd1498Szrj 	  /* unexpected escape sequence, not looks_like_rust. */
279*38fd1498Szrj 	  goto fail;
280*38fd1498Szrj 	}
281*38fd1498Szrj 	break;
282*38fd1498Szrj       case '_':
283*38fd1498Szrj 	/* If this is the start of a path component and the next
284*38fd1498Szrj 	   character is an escape sequence, ignore the underscore. The
285*38fd1498Szrj 	   mangler inserts an underscore to make sure the path
286*38fd1498Szrj 	   component begins with a XID_Start character. */
287*38fd1498Szrj 	if ((in == sym || in[-1] == ':') && in[1] == '$')
288*38fd1498Szrj 	  in++;
289*38fd1498Szrj 	else
290*38fd1498Szrj 	  *out++ = *in++;
291*38fd1498Szrj 	break;
292*38fd1498Szrj       case '.':
293*38fd1498Szrj 	if (in[1] == '.')
294*38fd1498Szrj 	  {
295*38fd1498Szrj 	    /* ".." becomes "::" */
296*38fd1498Szrj 	    *out++ = ':';
297*38fd1498Szrj 	    *out++ = ':';
298*38fd1498Szrj 	    in += 2;
299*38fd1498Szrj 	  }
300*38fd1498Szrj 	else
301*38fd1498Szrj 	  {
302*38fd1498Szrj 	    /* "." becomes "-" */
303*38fd1498Szrj 	    *out++ = '-';
304*38fd1498Szrj 	    in++;
305*38fd1498Szrj 	  }
306*38fd1498Szrj 	break;
307*38fd1498Szrj       case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
308*38fd1498Szrj       case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
309*38fd1498Szrj       case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
310*38fd1498Szrj       case 's': case 't': case 'u': case 'v': case 'w': case 'x':
311*38fd1498Szrj       case 'y': case 'z':
312*38fd1498Szrj       case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
313*38fd1498Szrj       case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
314*38fd1498Szrj       case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
315*38fd1498Szrj       case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
316*38fd1498Szrj       case 'Y': case 'Z':
317*38fd1498Szrj       case '0': case '1': case '2': case '3': case '4': case '5':
318*38fd1498Szrj       case '6': case '7': case '8': case '9':
319*38fd1498Szrj       case ':':
320*38fd1498Szrj 	*out++ = *in++;
321*38fd1498Szrj 	break;
322*38fd1498Szrj       default:
323*38fd1498Szrj 	/* unexpected character in symbol, not looks_like_rust.  */
324*38fd1498Szrj 	goto fail;
325*38fd1498Szrj       }
326*38fd1498Szrj   goto done;
327*38fd1498Szrj 
328*38fd1498Szrj fail:
329*38fd1498Szrj   *out++ = '?'; /* This is pretty lame, but it's hard to do better. */
330*38fd1498Szrj done:
331*38fd1498Szrj   *out = '\0';
332*38fd1498Szrj }
333*38fd1498Szrj 
334*38fd1498Szrj static int
unescape(const char ** in,char ** out,const char * seq,char value)335*38fd1498Szrj unescape (const char **in, char **out, const char *seq, char value)
336*38fd1498Szrj {
337*38fd1498Szrj   size_t len = strlen (seq);
338*38fd1498Szrj 
339*38fd1498Szrj   if (strncmp (*in, seq, len))
340*38fd1498Szrj     return 0;
341*38fd1498Szrj 
342*38fd1498Szrj   **out = value;
343*38fd1498Szrj 
344*38fd1498Szrj   *in += len;
345*38fd1498Szrj   *out += 1;
346*38fd1498Szrj 
347*38fd1498Szrj   return 1;
348*38fd1498Szrj }
349