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


   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 
  22 /*
  23  *      Copyright (c) 1988 AT&T
  24  *        All Rights Reserved
  25  *
  26  * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.

  27  */
  28 
  29 /* Get definitions for the relocation types supported. */
  30 #define ELF_TARGET_ALL
  31 
  32 #include <stdio.h>
  33 #include <stdlib.h>
  34 #include <locale.h>
  35 #include <unistd.h>
  36 #include <libelf.h>
  37 #include <sys/link.h>
  38 #include <sys/elf.h>
  39 #include <sys/machelf.h>
  40 #include <fcntl.h>
  41 #include <sys/stat.h>
  42 #include <errno.h>
  43 #include <string.h>
  44 #include "sgs.h"
  45 #include "conv.h"
  46 #include "dump.h"


 460 
 461                         if (strlen(sym_name))
 462                                 (void) printf("%-20s", sym_name);
 463                         else {
 464                                 (void) printf("%-20d", sym.st_name);
 465                         }
 466                         (void) printf("%-20s",
 467                             conv_reloc_type(p_ehdr->e_machine,
 468                             type, DUMP_CONVFMT, &inv_buf));
 469                 }
 470                 (void) printf("\n");
 471                 ndx++;
 472         }
 473 }
 474 
 475 /* demangle C++ names */
 476 static char *
 477 demangled_name(char *s)
 478 {
 479         static char     *buf = NULL;
 480         const char      *dn;

 481         size_t          len;
 482 
 483         dn = conv_demangle_name(s);
 484 
 485         /*
 486          * If not demangled, just return the symbol name
 487          */
 488         if (strcmp(s, dn) == 0)
 489                 return (s);
 490 
 491         /*
 492          * Demangled. Format it
 493          */
 494         if (buf != NULL)
 495                 free(buf);
 496 
 497         len = strlen(dn) + strlen(s) + 4;



 498         if ((buf = malloc(len)) == NULL)
 499                 return (s);


 500 
 501         (void) snprintf(buf, len, "%s\t[%s]", dn, s);




 502         return (buf);
 503 }
 504 
 505 /*
 506  * Print the symbol table.  Input is an ELF file descriptor, a
 507  * pointer to the symbol table SCNTAB structure,
 508  * the number of symbols, a range of symbols to print,
 509  * an index which is the number of the
 510  * section in the file, and the filename.  The number of sections,
 511  * the range, and the index are set in
 512  * dump_symbol_table, depending on whether -n or -T were set.
 513  */
 514 static void
 515 print_symtab(Elf *elf_file, SCNTAB *p_symtab, Elf_Data *sym_data,
 516     long range, int index)
 517 {
 518         GElf_Sym sym;
 519         int adj = 0;            /* field adjustment for elf64 */
 520         Elf32_Word      *symshndx = 0;
 521         unsigned int    nosymshndx = 0;
 522         Conv_inv_buf_t  inv_buf;
 523 
 524 
 525         if (gelf_getclass(elf_file) == ELFCLASS64)
 526                 adj = 8;
 527 
 528         while (range > 0) {
 529                 char            *sym_name = (char *)0;
 530                 int             type, bind;
 531                 int             specsec;
 532                 unsigned int    shndx;
 533 
 534                 (void) gelf_getsym(sym_data, index, &sym);
 535                 type = (int)GELF_ST_TYPE(sym.st_info);
 536                 bind = (int)GELF_ST_BIND(sym.st_info);
 537 
 538                 if ((sym.st_shndx == SHN_XINDEX) &&
 539                     (symshndx == 0) && (nosymshndx == 0)) {
 540                         Elf_Scn         *_scn;
 541                         GElf_Shdr       _shdr;
 542                         size_t          symscnndx;
 543 
 544                         symscnndx = elf_ndxscn(p_symtab->p_sd);
 545                         _scn = 0;
 546                         while ((_scn = elf_nextscn(elf_file, _scn)) != 0) {
 547                                 if (gelf_getshdr(_scn, &_shdr) == 0)
 548                                         break;
 549                                 if ((_shdr.sh_type == SHT_SYMTAB_SHNDX) &&




   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 
  22 /*
  23  *      Copyright (c) 1988 AT&T
  24  *        All Rights Reserved
  25  *
  26  * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
  27  * Copyright 2018, Joyent, Inc.
  28  */
  29 
  30 /* Get definitions for the relocation types supported. */
  31 #define ELF_TARGET_ALL
  32 
  33 #include <stdio.h>
  34 #include <stdlib.h>
  35 #include <locale.h>
  36 #include <unistd.h>
  37 #include <libelf.h>
  38 #include <sys/link.h>
  39 #include <sys/elf.h>
  40 #include <sys/machelf.h>
  41 #include <fcntl.h>
  42 #include <sys/stat.h>
  43 #include <errno.h>
  44 #include <string.h>
  45 #include "sgs.h"
  46 #include "conv.h"
  47 #include "dump.h"


 461 
 462                         if (strlen(sym_name))
 463                                 (void) printf("%-20s", sym_name);
 464                         else {
 465                                 (void) printf("%-20d", sym.st_name);
 466                         }
 467                         (void) printf("%-20s",
 468                             conv_reloc_type(p_ehdr->e_machine,
 469                             type, DUMP_CONVFMT, &inv_buf));
 470                 }
 471                 (void) printf("\n");
 472                 ndx++;
 473         }
 474 }
 475 
 476 /* demangle C++ names */
 477 static char *
 478 demangled_name(char *s)
 479 {
 480         static char     *buf = NULL;
 481         size_t          buflen = 0;
 482         char            *dn;
 483         size_t          len;
 484 
 485         dn = (char *)conv_demangle_name(s);
 486 
 487         /*
 488          * If not demangled, just return the symbol name
 489          */
 490         if (dn == s)
 491                 return (s);
 492 






 493         len = strlen(dn) + strlen(s) + 4;
 494 
 495         if (buflen < len) {
 496                 free(buf);
 497                 if ((buf = malloc(len)) == NULL)
 498                         return (s);
 499                 buflen = len;
 500         }
 501 
 502         /*
 503          * Demangled. Format it
 504          */
 505         (void) snprintf(buf, buflen, "%s\t[%s]", dn, s);
 506         free(dn);
 507         return (buf);
 508 }
 509 
 510 /*
 511  * Print the symbol table.  Input is an ELF file descriptor, a
 512  * pointer to the symbol table SCNTAB structure,
 513  * the number of symbols, a range of symbols to print,
 514  * an index which is the number of the
 515  * section in the file, and the filename.  The number of sections,
 516  * the range, and the index are set in
 517  * dump_symbol_table, depending on whether -n or -T were set.
 518  */
 519 static void
 520 print_symtab(Elf *elf_file, SCNTAB *p_symtab, Elf_Data *sym_data,
 521     long range, int index)
 522 {
 523         GElf_Sym sym;
 524         int adj = 0;            /* field adjustment for elf64 */
 525         Elf32_Word      *symshndx = 0;
 526         unsigned int    nosymshndx = 0;
 527         Conv_inv_buf_t  inv_buf;
 528 
 529 
 530         if (gelf_getclass(elf_file) == ELFCLASS64)
 531                 adj = 8;
 532 
 533         while (range > 0) {
 534                 char            *sym_name = NULL;
 535                 int             type, bind;
 536                 int             specsec;
 537                 unsigned int    shndx;
 538 
 539                 (void) gelf_getsym(sym_data, index, &sym);
 540                 type = (int)GELF_ST_TYPE(sym.st_info);
 541                 bind = (int)GELF_ST_BIND(sym.st_info);
 542 
 543                 if ((sym.st_shndx == SHN_XINDEX) &&
 544                     (symshndx == 0) && (nosymshndx == 0)) {
 545                         Elf_Scn         *_scn;
 546                         GElf_Shdr       _shdr;
 547                         size_t          symscnndx;
 548 
 549                         symscnndx = elf_ndxscn(p_symtab->p_sd);
 550                         _scn = 0;
 551                         while ((_scn = elf_nextscn(elf_file, _scn)) != 0) {
 552                                 if (gelf_getshdr(_scn, &_shdr) == 0)
 553                                         break;
 554                                 if ((_shdr.sh_type == SHT_SYMTAB_SHNDX) &&