Coverage Report

Created: 2023-08-28 06:26

/src/binutils-gdb/libiberty/cplus-dem.c
Line
Count
Source (jump to first uncovered line)
1
/* Demangler for GNU C++
2
   Copyright (C) 1989-2023 Free Software Foundation, Inc.
3
   Written by James Clark (jjc@jclark.uucp)
4
   Rewritten by Fred Fish (fnf@cygnus.com) for ARM and Lucid demangling
5
   Modified by Satish Pai (pai@apollo.hp.com) for HP demangling
6
7
This file is part of the libiberty library.
8
Libiberty is free software; you can redistribute it and/or
9
modify it under the terms of the GNU Library General Public
10
License as published by the Free Software Foundation; either
11
version 2 of the License, or (at your option) any later version.
12
13
In addition to the permissions in the GNU Library General Public
14
License, the Free Software Foundation gives you unlimited permission
15
to link the compiled version of this file into combinations with other
16
programs, and to distribute those combinations without any restriction
17
coming from the use of this file.  (The Library Public License
18
restrictions do apply in other respects; for example, they cover
19
modification of the file, and distribution when not linked into a
20
combined executable.)
21
22
Libiberty is distributed in the hope that it will be useful,
23
but WITHOUT ANY WARRANTY; without even the implied warranty of
24
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
25
Library General Public License for more details.
26
27
You should have received a copy of the GNU Library General Public
28
License along with libiberty; see the file COPYING.LIB.  If
29
not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
30
Boston, MA 02110-1301, USA.  */
31
32
/* This file lives in both GCC and libiberty.  When making changes, please
33
   try not to break either.  */
34
35
#ifdef HAVE_CONFIG_H
36
#include "config.h"
37
#endif
38
39
#include "safe-ctype.h"
40
41
#include <string.h>
42
43
#ifdef HAVE_STDLIB_H
44
#include <stdlib.h>
45
#else
46
void * malloc ();
47
void * realloc ();
48
#endif
49
50
#include <demangle.h>
51
#undef CURRENT_DEMANGLING_STYLE
52
0
#define CURRENT_DEMANGLING_STYLE options
53
54
#include "libiberty.h"
55
56
enum demangling_styles current_demangling_style = auto_demangling;
57
58
const struct demangler_engine libiberty_demanglers[] =
59
{
60
  {
61
    NO_DEMANGLING_STYLE_STRING,
62
    no_demangling,
63
    "Demangling disabled"
64
  }
65
  ,
66
  {
67
    AUTO_DEMANGLING_STYLE_STRING,
68
      auto_demangling,
69
      "Automatic selection based on executable"
70
  }
71
  ,
72
  {
73
    GNU_V3_DEMANGLING_STYLE_STRING,
74
    gnu_v3_demangling,
75
    "GNU (g++) V3 (Itanium C++ ABI) style demangling"
76
  }
77
  ,
78
  {
79
    JAVA_DEMANGLING_STYLE_STRING,
80
    java_demangling,
81
    "Java style demangling"
82
  }
83
  ,
84
  {
85
    GNAT_DEMANGLING_STYLE_STRING,
86
    gnat_demangling,
87
    "GNAT style demangling"
88
  }
89
  ,
90
  {
91
    DLANG_DEMANGLING_STYLE_STRING,
92
    dlang_demangling,
93
    "DLANG style demangling"
94
  }
95
  ,
96
  {
97
    RUST_DEMANGLING_STYLE_STRING,
98
    rust_demangling,
99
    "Rust style demangling"
100
  }
101
  ,
102
  {
103
    NULL, unknown_demangling, NULL
104
  }
105
};
106
107
/* Add a routine to set the demangling style to be sure it is valid and
108
   allow for any demangler initialization that maybe necessary. */
109
110
enum demangling_styles
111
cplus_demangle_set_style (enum demangling_styles style)
112
0
{
113
0
  const struct demangler_engine *demangler = libiberty_demanglers; 
114
115
0
  for (; demangler->demangling_style != unknown_demangling; ++demangler)
116
0
    if (style == demangler->demangling_style)
117
0
      {
118
0
  current_demangling_style = style;
119
0
  return current_demangling_style;
120
0
      }
121
122
0
  return unknown_demangling;
123
0
}
124
125
/* Do string name to style translation */
126
127
enum demangling_styles
128
cplus_demangle_name_to_style (const char *name)
129
0
{
130
0
  const struct demangler_engine *demangler = libiberty_demanglers; 
131
132
0
  for (; demangler->demangling_style != unknown_demangling; ++demangler)
133
0
    if (strcmp (name, demangler->demangling_style_name) == 0)
134
0
      return demangler->demangling_style;
135
136
0
  return unknown_demangling;
137
0
}
138
139
/* char *cplus_demangle (const char *mangled, int options)
140
141
   If MANGLED is a mangled function name produced by GNU C++, then
142
   a pointer to a @code{malloc}ed string giving a C++ representation
143
   of the name will be returned; otherwise NULL will be returned.
144
   It is the caller's responsibility to free the string which
145
   is returned.
146
147
   Note that any leading underscores, or other such characters prepended by
148
   the compilation system, are presumed to have already been stripped from
149
   MANGLED.  */
150
151
char *
152
cplus_demangle (const char *mangled, int options)
153
0
{
154
0
  char *ret;
155
156
0
  if (current_demangling_style == no_demangling)
157
0
    return xstrdup (mangled);
158
159
0
  if ((options & DMGL_STYLE_MASK) == 0)
160
0
    options |= (int) current_demangling_style & DMGL_STYLE_MASK;
161
162
  /* The Rust demangling is implemented elsewhere.
163
     Legacy Rust symbols overlap with GNU_V3, so try Rust first.  */
164
0
  if (RUST_DEMANGLING || AUTO_DEMANGLING)
165
0
    {
166
0
      ret = rust_demangle (mangled, options);
167
0
      if (ret || RUST_DEMANGLING)
168
0
        return ret;
169
0
    }
170
171
  /* The V3 ABI demangling is implemented elsewhere.  */
172
0
  if (GNU_V3_DEMANGLING || AUTO_DEMANGLING)
173
0
    {
174
0
      ret = cplus_demangle_v3 (mangled, options);
175
0
      if (ret || GNU_V3_DEMANGLING)
176
0
  return ret;
177
0
    }
178
179
0
  if (JAVA_DEMANGLING)
180
0
    {
181
0
      ret = java_demangle_v3 (mangled);
182
0
      if (ret)
183
0
        return ret;
184
0
    }
185
186
0
  if (GNAT_DEMANGLING)
187
0
    return ada_demangle (mangled, options);
188
189
0
  if (DLANG_DEMANGLING)
190
0
    {
191
0
      ret = dlang_demangle (mangled, options);
192
0
      if (ret)
193
0
  return ret;
194
0
    }
195
196
0
  return (ret);
197
0
}
198
199
/* Demangle ada names.  The encoding is documented in gcc/ada/exp_dbug.ads.  */
200
201
char *
202
ada_demangle (const char *mangled, int option ATTRIBUTE_UNUSED)
203
0
{
204
0
  int len0;
205
0
  const char* p;
206
0
  char *d;
207
0
  char *demangled = NULL;
208
  
209
  /* Discard leading _ada_, which is used for library level subprograms.  */
210
0
  if (strncmp (mangled, "_ada_", 5) == 0)
211
0
    mangled += 5;
212
213
  /* All ada unit names are lower-case.  */
214
0
  if (!ISLOWER (mangled[0]))
215
0
    goto unknown;
216
217
  /* Most of the demangling will trivially remove chars.  Operator names
218
     may add one char but because they are always preceeded by '__' which is
219
     replaced by '.', they eventually never expand the size.
220
     A few special names such as '___elabs' add a few chars (at most 7), but
221
     they occur only once.  */
222
0
  len0 = strlen (mangled) + 7 + 1;
223
0
  demangled = XNEWVEC (char, len0);
224
  
225
0
  d = demangled;
226
0
  p = mangled;
227
0
  while (1)
228
0
    {
229
      /* An entity names is expected.  */
230
0
      if (ISLOWER (*p))
231
0
        {
232
          /* An identifier, which is always lower case.  */
233
0
          do
234
0
            *d++ = *p++;
235
0
          while (ISLOWER(*p) || ISDIGIT (*p)
236
0
                 || (p[0] == '_' && (ISLOWER (p[1]) || ISDIGIT (p[1]))));
237
0
        }
238
0
      else if (p[0] == 'O')
239
0
        {
240
          /* An operator name.  */
241
0
          static const char * const operators[][2] =
242
0
            {{"Oabs", "abs"},  {"Oand", "and"},    {"Omod", "mod"},
243
0
             {"Onot", "not"},  {"Oor", "or"},      {"Orem", "rem"},
244
0
             {"Oxor", "xor"},  {"Oeq", "="},       {"One", "/="},
245
0
             {"Olt", "<"},     {"Ole", "<="},      {"Ogt", ">"},
246
0
             {"Oge", ">="},    {"Oadd", "+"},      {"Osubtract", "-"},
247
0
             {"Oconcat", "&"}, {"Omultiply", "*"}, {"Odivide", "/"},
248
0
             {"Oexpon", "**"}, {NULL, NULL}};
249
0
          int k;
250
251
0
          for (k = 0; operators[k][0] != NULL; k++)
252
0
            {
253
0
              size_t slen = strlen (operators[k][0]);
254
0
              if (strncmp (p, operators[k][0], slen) == 0)
255
0
                {
256
0
                  p += slen;
257
0
                  slen = strlen (operators[k][1]);
258
0
                  *d++ = '"';
259
0
                  memcpy (d, operators[k][1], slen);
260
0
                  d += slen;
261
0
                  *d++ = '"';
262
0
                  break;
263
0
                }
264
0
            }
265
          /* Operator not found.  */
266
0
          if (operators[k][0] == NULL)
267
0
            goto unknown;
268
0
        }
269
0
      else
270
0
        {
271
          /* Not a GNAT encoding.  */
272
0
          goto unknown;
273
0
        }
274
275
      /* The name can be directly followed by some uppercase letters.  */
276
0
      if (p[0] == 'T' && p[1] == 'K')
277
0
        {
278
          /* Task stuff.  */
279
0
          if (p[2] == 'B' && p[3] == 0)
280
0
            {
281
              /* Subprogram for task body.  */
282
0
              break;
283
0
            }
284
0
          else if (p[2] == '_' && p[3] == '_')
285
0
            {
286
              /* Inner declarations in a task.  */
287
0
              p += 4;
288
0
              *d++ = '.';
289
0
              continue;
290
0
            }
291
0
          else
292
0
            goto unknown;
293
0
        }
294
0
      if (p[0] == 'E' && p[1] == 0)
295
0
        {
296
          /* Exception name.  */
297
0
          goto unknown;
298
0
        }
299
0
      if ((p[0] == 'P' || p[0] == 'N') && p[1] == 0)
300
0
        {
301
          /* Protected type subprogram.  */
302
0
          break;
303
0
        }
304
0
      if ((*p == 'N' || *p == 'S') && p[1] == 0)
305
0
        {
306
          /* Enumerated type name table.  */
307
0
          goto unknown;
308
0
        }
309
0
      if (p[0] == 'X')
310
0
        {
311
          /* Body nested.  */
312
0
          p++;
313
0
          while (p[0] == 'n' || p[0] == 'b')
314
0
            p++;
315
0
        }
316
0
      if (p[0] == 'S' && p[1] != 0 && (p[2] == '_' || p[2] == 0))
317
0
        {
318
          /* Stream operations.  */
319
0
          const char *name;
320
0
          switch (p[1])
321
0
            {
322
0
            case 'R':
323
0
              name = "'Read";
324
0
              break;
325
0
            case 'W':
326
0
              name = "'Write";
327
0
              break;
328
0
            case 'I':
329
0
              name = "'Input";
330
0
              break;
331
0
            case 'O':
332
0
              name = "'Output";
333
0
              break;
334
0
            default:
335
0
              goto unknown;
336
0
            }
337
0
          p += 2;
338
0
          strcpy (d, name);
339
0
          d += strlen (name);
340
0
        }
341
0
      else if (p[0] == 'D')
342
0
        {
343
          /* Controlled type operation.  */
344
0
          const char *name;
345
0
          switch (p[1])
346
0
            {
347
0
            case 'F':
348
0
              name = ".Finalize";
349
0
              break;
350
0
            case 'A':
351
0
              name = ".Adjust";
352
0
              break;
353
0
            default:
354
0
              goto unknown;
355
0
            }
356
0
          strcpy (d, name);
357
0
          d += strlen (name);
358
0
          break;
359
0
        }
360
361
0
      if (p[0] == '_')
362
0
        {
363
          /* Separator.  */
364
0
          if (p[1] == '_')
365
0
            {
366
              /* Standard separator.  Handled first.  */
367
0
              p += 2;
368
369
0
              if (ISDIGIT (*p))
370
0
                {
371
                  /* Overloading number.  */
372
0
                  do
373
0
                    p++;
374
0
                  while (ISDIGIT (*p) || (p[0] == '_' && ISDIGIT (p[1])));
375
0
                  if (*p == 'X')
376
0
                    {
377
0
                      p++;
378
0
                      while (p[0] == 'n' || p[0] == 'b')
379
0
                        p++;
380
0
                    }
381
0
                }
382
0
              else if (p[0] == '_' && p[1] != '_')
383
0
                {
384
                  /* Special names.  */
385
0
                  static const char * const special[][2] = {
386
0
                    { "_elabb", "'Elab_Body" },
387
0
                    { "_elabs", "'Elab_Spec" },
388
0
                    { "_size", "'Size" },
389
0
                    { "_alignment", "'Alignment" },
390
0
                    { "_assign", ".\":=\"" },
391
0
                    { NULL, NULL }
392
0
                  };
393
0
                  int k;
394
395
0
                  for (k = 0; special[k][0] != NULL; k++)
396
0
                    {
397
0
                      size_t slen = strlen (special[k][0]);
398
0
                      if (strncmp (p, special[k][0], slen) == 0)
399
0
                        {
400
0
                          p += slen;
401
0
                          slen = strlen (special[k][1]);
402
0
                          memcpy (d, special[k][1], slen);
403
0
                          d += slen;
404
0
                          break;
405
0
                        }
406
0
                    }
407
0
                  if (special[k][0] != NULL)
408
0
                    break;
409
0
                  else
410
0
                    goto unknown;
411
0
                }
412
0
              else
413
0
                {
414
0
                  *d++ = '.';
415
0
                  continue;
416
0
                }
417
0
            }
418
0
          else if (p[1] == 'B' || p[1] == 'E')
419
0
            {
420
              /* Entry Body or barrier Evaluation.  */
421
0
              p += 2;
422
0
              while (ISDIGIT (*p))
423
0
                p++;
424
0
              if (p[0] == 's' && p[1] == 0)
425
0
                break;
426
0
              else
427
0
                goto unknown;
428
0
            }
429
0
          else
430
0
            goto unknown;
431
0
        }
432
433
0
      if (p[0] == '.' && ISDIGIT (p[1]))
434
0
        {
435
          /* Nested subprogram.  */
436
0
          p += 2;
437
0
          while (ISDIGIT (*p))
438
0
            p++;
439
0
        }
440
0
      if (*p == 0)
441
0
        {
442
          /* End of mangled name.  */
443
0
          break;
444
0
        }
445
0
      else
446
0
        goto unknown;
447
0
    }
448
0
  *d = 0;
449
0
  return demangled;
450
451
0
 unknown:
452
0
  XDELETEVEC (demangled);
453
0
  len0 = strlen (mangled);
454
0
  demangled = XNEWVEC (char, len0 + 3);
455
456
0
  if (mangled[0] == '<')
457
0
     strcpy (demangled, mangled);
458
0
  else
459
0
    sprintf (demangled, "<%s>", mangled);
460
461
0
  return demangled;
462
0
}