Path: blob/main/contrib/elftoolchain/libelftc/elftc_demangle.c
39478 views
/*-1* Copyright (c) 2009 Kai Wang2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer9* in this position and unchanged.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR15* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES16* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.17* IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,18* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT19* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,20* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY21* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT22* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF23* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.24*/2526#include <sys/param.h>27#include <assert.h>28#include <errno.h>29#include <libelftc.h>30#include <stdlib.h>31#include <string.h>3233#include "_libelftc.h"3435ELFTC_VCSID("$Id: elftc_demangle.c 3296 2016-01-09 14:17:28Z jkoshy $");3637static unsigned int38is_mangled(const char *s, unsigned int style)39{4041switch (style) {42case ELFTC_DEM_ARM: return (is_cpp_mangled_ARM(s) ? style : 0);43case ELFTC_DEM_GNU2: return (is_cpp_mangled_gnu2(s) ? style : 0);44case ELFTC_DEM_GNU3: return (is_cpp_mangled_gnu3(s) ? style : 0);45}4647/* No style or invalid style spcified, try to guess. */48if (is_cpp_mangled_gnu3(s))49return (ELFTC_DEM_GNU3);50if (is_cpp_mangled_gnu2(s))51return (ELFTC_DEM_GNU2);52if (is_cpp_mangled_ARM(s))53return (ELFTC_DEM_ARM);5455/* Cannot be demangled. */56return (0);57}5859static char *60demangle(const char *s, unsigned int style, unsigned int rc)61{6263(void) rc; /* XXX */64switch (style) {65case ELFTC_DEM_ARM: return (cpp_demangle_ARM(s));66case ELFTC_DEM_GNU2: return (cpp_demangle_gnu2(s));67case ELFTC_DEM_GNU3: return (cpp_demangle_gnu3(s));68default:69assert(0);70return (NULL);71}72}7374int75elftc_demangle(const char *mangledname, char *buffer, size_t bufsize,76unsigned int flags)77{78unsigned int style, rc;79char *rlt;8081style = flags & 0xFFFF;82rc = flags >> 16;8384if (mangledname == NULL ||85((style = is_mangled(mangledname, style)) == 0)) {86errno = EINVAL;87return (-1);88}8990if ((rlt = demangle(mangledname, style, rc)) == NULL) {91errno = EINVAL;92return (-1);93}9495if (buffer == NULL || bufsize < strlen(rlt) + 1) {96free(rlt);97errno = ENAMETOOLONG;98return (-1);99}100101strncpy(buffer, rlt, bufsize);102buffer[bufsize - 1] = '\0';103free(rlt);104105return (0);106}107108109