Print this page
6375 Add native name demangling support
Reviewed by: Robert Mustacchi <rm@joyent.com>
Reviewed by: Richard Lowe <richlowe@richlowe.net>

Split Close
Expand all
Collapse all
          --- old/usr/src/cmd/sgs/libconv/common/demangle.c
          +++ new/usr/src/cmd/sgs/libconv/common/demangle.c
↓ open down ↓ 13 lines elided ↑ open up ↑
  14   14   * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15   15   * If applicable, add the following below this CDDL HEADER, with the
  16   16   * fields enclosed by brackets "[]" replaced with your own identifying
  17   17   * information: Portions Copyright [yyyy] [name of copyright owner]
  18   18   *
  19   19   * CDDL HEADER END
  20   20   */
  21   21  
  22   22  /*
  23   23   * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
       24 + * Copyright 2018, Joyent, Inc.
  24   25   */
  25   26  
  26   27  #include        <stdio.h>
  27      -#include        <demangle.h>
       28 +#include        <demangle-sys.h>
  28   29  #include        "_conv.h"
  29   30  #include        "demangle_msg.h"
  30   31  
  31   32  /*
  32   33   * Demangle C++ symbols.
  33   34   *
  34   35   * This routine acts as a generic routine for use by liblddbg (and hence tools
  35   36   * like elfdump(1) and pvs(1)), ld(1) and ld.so.1(1).
  36   37   *
  37   38   * The C++ ABI-2 places no limits on symbol names, thus when demangling a name
↓ open down ↓ 14 lines elided ↑ open up ↑
  52   53   *              name1 = (const char *)__name1;
  53   54   *      }
  54   55   *      name2 = demangle(name2);
  55   56   *      eprintf(format, name1, name2);
  56   57   */
  57   58  #define SYM_MAX 1000
  58   59  
  59   60  const char *
  60   61  conv_demangle_name(const char *name)
  61   62  {
  62      -        static char     _str[SYM_MAX], *str = _str;
  63      -        static size_t   size = SYM_MAX;
  64      -        static int      again = 1;
  65      -        static int      (*fptr)() = 0;
  66      -        int             error;
       63 +        static char     *(*fptr)() = 0;
       64 +        static volatile int loading = 0;
       65 +        char *d;
  67   66  
  68      -        if (str == 0)
       67 +        if (loading)
  69   68                  return (name);
  70   69  
  71   70          /*
  72   71           * If we haven't located the demangler yet try now (we do this rather
  73   72           * than maintain a static dependency on libdemangle as it's part of an
  74   73           * optional package).  Null the str element out to reject any other
  75   74           * callers until this operation is complete - under ld.so.1 we can get
  76   75           * into serious recursion without this.
  77   76           */
  78   77          if (fptr == 0) {
  79   78                  void    *hdl;
  80   79  
  81      -                str = 0;
       80 +                loading = 1;
  82   81                  if (!(hdl = dlopen(MSG_ORIG(MSG_DEM_LIB), RTLD_LAZY)) ||
  83      -                    !(fptr = (int (*)())dlsym(hdl, MSG_ORIG(MSG_DEM_SYM))))
       82 +                    !(fptr = (char *(*)())dlsym(hdl, MSG_ORIG(MSG_DEM_SYM))))
  84   83                          return (name);
  85      -                str = _str;
       84 +                loading = 0;
  86   85          }
  87   86  
  88      -        if ((error = (*fptr)(name, str, size)) == 0)
  89      -                return ((const char *)str);
       87 +        if ((d = fptr(name, SYSDEM_LANG_AUTO, NULL)) == NULL)
       88 +                return (name);
  90   89  
  91      -        while ((error == DEMANGLE_ESPACE) && again) {
  92      -                char    *_str;
  93      -                size_t  _size = size;
  94      -
  95      -                /*
  96      -                 * If we haven't allocated our maximum try incrementing the
  97      -                 * present buffer size. Use malloc() rather than realloc() so
  98      -                 * that we at least have the old buffer on failure.
  99      -                 */
 100      -                if (((_size += SYM_MAX) > (SYM_MAX * 4)) ||
 101      -                    ((_str = malloc(_size)) == 0)) {
 102      -                        again = 0;
 103      -                        break;
 104      -                }
 105      -                if (size != SYM_MAX) {
 106      -                        free(str);
 107      -                }
 108      -                str = _str;
 109      -                size = _size;
 110      -
 111      -                if ((error = (*fptr)(name, str, size)) == 0)
 112      -                        return ((const char *)str);
 113      -        }
 114      -        return (name);
       90 +        return (d);
 115   91  }
    
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX