Move demangling utility to stdio

Closes #848
This commit is contained in:
Justine Tunney 2023-07-07 10:26:07 -07:00
parent a1dd777f22
commit 3a8579252d
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
5 changed files with 100 additions and 111 deletions

View file

@ -1,39 +1,49 @@
/*- /*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│
* Copyright (c) 2007 Hyogeol Lee <hyogeollee@gmail.com> vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi
* Copyright (c) 2015-2017 Kai Wang <kaiwang27@gmail.com>
* All rights reserved. Copyright (c) 2007 Hyogeol Lee <hyogeollee@gmail.com>
* Copyright (c) 2015-2017 Kai Wang <kaiwang27@gmail.com>
* Redistribution and use in source and binary forms, with or without All rights reserved.
* modification, are permitted provided that the following conditions
* are met: Redistribution and use in source and binary forms, with or without
* 1. Redistributions of source code must retain the above copyright modification, are permitted provided that the following conditions
* notice, this list of conditions and the following disclaimer are met:
* in this position and unchanged. 1. Redistributions of source code must retain the above copyright
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer
* notice, this list of conditions and the following disclaimer in the in this position and unchanged.
* documentation and/or other materials provided with the distribution. 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR documentation and/or other materials provided with the distribution.
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* https://github.com/freebsd/freebsd-src/blob/2176c9ab71c85efd90a6c7af4a9e04fe8e3d49ca/contrib/libcxxrt/libelftc_dem_gnu3.c THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#include "third_party/libcxx/cxxabi.h" #include "libc/assert.h"
#include "third_party/libcxx/cstdlib" #include "libc/errno.h"
#include "third_party/libcxx/cstdio" #include "libc/fmt/conv.h"
#include "third_party/libcxx/cassert" #include "libc/fmt/fmt.h"
#include "third_party/libcxx/cerrno" #include "libc/mem/mem.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
asm(".ident\t\"\\n\\n\
C++ Demangle (BSD-2)\\n\
Copyright (c) 2007 Hyogeol Lee <hyogeollee@gmail.com>\\n\
Copyright (c) 2015-2017 Kai Wang <kaiwang27@gmail.com>\"");
asm(".include \"libc/disclaimer.inc\"");
// https://github.com/freebsd/freebsd-src/blob/2176c9ab71c85efd90a6c7af4a9e04fe8e3d49ca/contrib/libcxxrt/libelftc_dem_gnu3.c
// clang-format off
/** /**
* @file demangle.cc * @file demangle.c
* @brief Decode IA-64 C++ ABI style implementation. * @brief Decode IA-64 C++ ABI style implementation.
* *
* IA-64 standard ABI(Itanium C++ ABI) references. * IA-64 standard ABI(Itanium C++ ABI) references.
@ -4296,7 +4306,7 @@ vector_type_qualifier_init(struct vector_type_qualifier *v)
static int static int
vector_type_qualifier_push(struct vector_type_qualifier *v, vector_type_qualifier_push(struct vector_type_qualifier *v,
enum type_qualifier t) enum type_qualifier t)
{ {
enum type_qualifier *tmp_ctn; enum type_qualifier *tmp_ctn;
size_t tmp_cap; size_t tmp_cap;
@ -4323,10 +4333,52 @@ vector_type_qualifier_push(struct vector_type_qualifier *v,
return (1); return (1);
} }
extern "C" char* __cxa_demangle(const char* mangled_name, /**
char* buf, * @brief Demangling routine.
size_t* n, * ABI-mandated entry point in the C++ runtime library for demangling.
int* status) *
* @param __mangled_name A NUL-terminated character string
* containing the name to be demangled.
*
* @param __output_buffer A region of memory, allocated with
* malloc, of @a *__length bytes, into which the demangled name is
* stored. If @a __output_buffer is not long enough, it is
* expanded using realloc. @a __output_buffer may instead be null;
* in that case, the demangled name is placed in a region of memory
* allocated with malloc.
*
* @param __length If @a __length is non-null, the length of the
* buffer containing the demangled name is placed in @a *__length.
*
* @param __status If @a __status is non-null, @a *__status is set to
* one of the following values:
* 0: The demangling operation succeeded.
* -1: A memory allocation failure occurred.
* -2: @a mangled_name is not a valid name under the C++ ABI mangling rules.
* -3: One of the arguments is invalid.
*
* @return A pointer to the start of the NUL-terminated demangled
* name, or a null pointer if the demangling fails. The caller is
* responsible for deallocating this memory using @c free.
*
* The demangling is performed using the C++ ABI mangling rules,
* with GNU extensions. For example, this function is used in
* __gnu_cxx::__verbose_terminate_handler.
*
* See https://gcc.gnu.org/onlinedocs/libstdc++/manual/ext_demangling.html
* for other examples of use.
*
* @note The same demangling functionality is available via
* libiberty (@c <libiberty/demangle.h> and @c libiberty.a) in GCC
* 3.1 and later, but that requires explicit installation (@c
* --enable-install-libiberty) and uses a different API, although
* the ABI is unchanged.
*/
char *
__cxa_demangle(const char* mangled_name,
char* buf,
size_t* n,
int* status)
{ {
// TODO: We should probably just be linking against libelf-tc, rather than // TODO: We should probably just be linking against libelf-tc, rather than
// copying their code. This requires them to do an actual release, // copying their code. This requires them to do an actual release,
@ -4339,7 +4391,7 @@ extern "C" char* __cxa_demangle(const char* mangled_name,
size_t len = strlen(demangled); size_t len = strlen(demangled);
if (!buf || (*n < len+1)) if (!buf || (*n < len+1))
{ {
buf = static_cast<char*>(realloc(buf, len+1)); buf = realloc(buf, len+1);
} }
if (0 != buf) if (0 != buf)
{ {
@ -4372,4 +4424,4 @@ extern "C" char* __cxa_demangle(const char* mangled_name,
return NULL; return NULL;
} }
return buf; return buf;
} }

View file

@ -169,6 +169,12 @@ int fprintf_unlocked(FILE *, const char *, ...) printfesque(2)
int vfprintf_unlocked(FILE *, const char *, va_list) int vfprintf_unlocked(FILE *, const char *, va_list)
paramsnonnull() dontthrow nocallback; paramsnonnull() dontthrow nocallback;
/*───────────────────────────────────────────────────────────────────────────│─╗
cosmopolitan § cxxabi
*/
char *__cxa_demangle(const char *, char *, size_t *, int *);
COSMOPOLITAN_C_END_ COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_STDIO_H_ */ #endif /* COSMOPOLITAN_LIBC_STDIO_H_ */

View file

@ -1,55 +0,0 @@
#ifndef _CXXABI_H
#define _CXXABI_H 1
#include "third_party/libcxx/cstddef"
namespace abi {
/**
* @brief Demangling routine.
* ABI-mandated entry point in the C++ runtime library for demangling.
*
* @param __mangled_name A NUL-terminated character string
* containing the name to be demangled.
*
* @param __output_buffer A region of memory, allocated with
* malloc, of @a *__length bytes, into which the demangled name is
* stored. If @a __output_buffer is not long enough, it is
* expanded using realloc. @a __output_buffer may instead be null;
* in that case, the demangled name is placed in a region of memory
* allocated with malloc.
*
* @param __length If @a __length is non-null, the length of the
* buffer containing the demangled name is placed in @a *__length.
*
* @param __status If @a __status is non-null, @a *__status is set to
* one of the following values:
* 0: The demangling operation succeeded.
* -1: A memory allocation failure occurred.
* -2: @a mangled_name is not a valid name under the C++ ABI mangling rules.
* -3: One of the arguments is invalid.
*
* @return A pointer to the start of the NUL-terminated demangled
* name, or a null pointer if the demangling fails. The caller is
* responsible for deallocating this memory using @c free.
*
* The demangling is performed using the C++ ABI mangling rules,
* with GNU extensions. For example, this function is used in
* __gnu_cxx::__verbose_terminate_handler.
*
* See https://gcc.gnu.org/onlinedocs/libstdc++/manual/ext_demangling.html
* for other examples of use.
*
* @note The same demangling functionality is available via
* libiberty (@c <libiberty/demangle.h> and @c libiberty.a) in GCC
* 3.1 and later, but that requires explicit installation (@c
* --enable-install-libiberty) and uses a different API, although
* the ABI is unchanged.
*/
extern "C" char*
__cxa_demangle(const char* __mangled_name, char* __output_buffer,
size_t* __length, int* __status);
}
#endif

View file

@ -67,7 +67,6 @@ THIRD_PARTY_LIBCXX_A_HDRS = \
third_party/libcxx/ctype.h \ third_party/libcxx/ctype.h \
third_party/libcxx/cwchar \ third_party/libcxx/cwchar \
third_party/libcxx/cwctype \ third_party/libcxx/cwctype \
third_party/libcxx/cxxabi.h \
third_party/libcxx/deque \ third_party/libcxx/deque \
third_party/libcxx/errno.h \ third_party/libcxx/errno.h \
third_party/libcxx/exception \ third_party/libcxx/exception \
@ -144,7 +143,6 @@ THIRD_PARTY_LIBCXX_A_SRCS_CC = \
third_party/libcxx/chrono.cc \ third_party/libcxx/chrono.cc \
third_party/libcxx/condition_variable.cc \ third_party/libcxx/condition_variable.cc \
third_party/libcxx/condition_variable_destructor.cc \ third_party/libcxx/condition_variable_destructor.cc \
third_party/libcxx/demangle.cc \
third_party/libcxx/exception.cc \ third_party/libcxx/exception.cc \
third_party/libcxx/functional.cc \ third_party/libcxx/functional.cc \
third_party/libcxx/future.cc \ third_party/libcxx/future.cc \
@ -175,7 +173,6 @@ THIRD_PARTY_LIBCXX_A_SRCS = \
THIRD_PARTY_LIBCXX_A_OBJS = \ THIRD_PARTY_LIBCXX_A_OBJS = \
$(THIRD_PARTY_LIBCXX_A_SRCS_S:%.S=o/$(MODE)/%.o) \ $(THIRD_PARTY_LIBCXX_A_SRCS_S:%.S=o/$(MODE)/%.o) \
$(THIRD_PARTY_LIBCXX_A_SRCS_C:%.c=o/$(MODE)/%.o) \
$(THIRD_PARTY_LIBCXX_A_SRCS_CC:%.cc=o/$(MODE)/%.o) $(THIRD_PARTY_LIBCXX_A_SRCS_CC:%.cc=o/$(MODE)/%.o)
THIRD_PARTY_LIBCXX_A_CHECKS = \ THIRD_PARTY_LIBCXX_A_CHECKS = \
@ -214,16 +211,6 @@ $(THIRD_PARTY_LIBCXX_A_OBJS): private \
-ffunction-sections \ -ffunction-sections \
-fdata-sections -fdata-sections
# Super gross! But I found no way to do -fno-error=permissive
# this steps from the fact that demangle is a C++ file extension
# but the source I copied was written in C.
# The fix would be to go through all the casts and make them correct.
o/$(MODE)/third_party/libcxx/demangle.o: private \
CXXFLAGS += \
-fpermissive \
-Wno-error
THIRD_PARTY_LIBCXX_LIBS = $(foreach x,$(THIRD_PARTY_LIBCXX_ARTIFACTS),$($(x))) THIRD_PARTY_LIBCXX_LIBS = $(foreach x,$(THIRD_PARTY_LIBCXX_ARTIFACTS),$($(x)))
THIRD_PARTY_LIBCXX_SRCS = $(foreach x,$(THIRD_PARTY_LIBCXX_ARTIFACTS),$($(x)_SRCS)) THIRD_PARTY_LIBCXX_SRCS = $(foreach x,$(THIRD_PARTY_LIBCXX_ARTIFACTS),$($(x)_SRCS))
THIRD_PARTY_LIBCXX_HDRS = $(foreach x,$(THIRD_PARTY_LIBCXX_ARTIFACTS),$($(x)_HDRS)) THIRD_PARTY_LIBCXX_HDRS = $(foreach x,$(THIRD_PARTY_LIBCXX_ARTIFACTS),$($(x)_HDRS))
@ -235,4 +222,3 @@ THIRD_PARTY_LIBCXX_OBJS = $(foreach x,$(THIRD_PARTY_LIBCXX_ARTIFACTS),$($(x)_OBJ
o/$(MODE)/third_party/libcxx: \ o/$(MODE)/third_party/libcxx: \
$(THIRD_PARTY_LIBCXX_CHECKS) \ $(THIRD_PARTY_LIBCXX_CHECKS) \
$(THIRD_PARTY_LIBCXX_A) $(THIRD_PARTY_LIBCXX_A)

View file

@ -23,7 +23,7 @@ o/$(MODE)/third_party: \
o/$(MODE)/third_party/make \ o/$(MODE)/third_party/make \
o/$(MODE)/third_party/maxmind \ o/$(MODE)/third_party/maxmind \
o/$(MODE)/third_party/mbedtls \ o/$(MODE)/third_party/mbedtls \
o/$(MODE)/third_party/xxhash \ o/$(MODE)/third_party/xxhash \
o/$(MODE)/third_party/musl \ o/$(MODE)/third_party/musl \
o/$(MODE)/third_party/nsync \ o/$(MODE)/third_party/nsync \
o/$(MODE)/third_party/puff \ o/$(MODE)/third_party/puff \