Skip to content

Commit

Permalink
Merge pull request #149 from chaoticgd/demangler
Browse files Browse the repository at this point in the history
Update the demangler to the version from GCC 13.2.0
  • Loading branch information
chaoticgd authored Jan 11, 2024
2 parents 7ee277d + b44210c commit 8d391c3
Show file tree
Hide file tree
Showing 28 changed files with 8,821 additions and 1,069 deletions.
6 changes: 3 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,12 @@ if(WIN32)
target_sources(tests PUBLIC src/ccc.manifest)
endif()

add_subdirectory(thirdparty/demanglegnu)
add_subdirectory(thirdparty/demanglegnu EXCLUDE_FROM_ALL)

set(gtest_force_shared_crt on)
add_subdirectory(thirdparty/googletest)
add_subdirectory(thirdparty/googletest EXCLUDE_FROM_ALL)

add_subdirectory(thirdparty/rapidjson)
add_subdirectory(thirdparty/rapidjson EXCLUDE_FROM_ALL)

# All the files to be included in a release.
set(RELEASE_CONTENTS
Expand Down
7 changes: 7 additions & 0 deletions thirdparty/demanglegnu/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,10 @@ add_library(demanglegnu STATIC
)
target_include_directories(demanglegnu PUBLIC .)
target_compile_definitions(demanglegnu PRIVATE -DHAVE_LIMITS_H=1 -DHAVE_STDLIB_H=1 -DHAVE_STRING_H=1)

add_executable(demangler_fuzzer testsuite/demangler-fuzzer.c)
add_executable(demangler_test testsuite/test-demangle.c)
target_link_libraries(demangler_fuzzer demanglegnu)
target_link_libraries(demangler_test demanglegnu)
target_compile_definitions(demangler_fuzzer PRIVATE -DHAVE_LIMITS_H=1 -DHAVE_STDLIB_H=1 -DHAVE_STRING_H=1)
target_compile_definitions(demangler_test PRIVATE -DHAVE_LIMITS_H=1 -DHAVE_STDLIB_H=1 -DHAVE_STRING_H=1)
36 changes: 32 additions & 4 deletions thirdparty/demanglegnu/README
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
This code was taken from GNU binutils v2.31 and specifically the libiberty
library which contains files licensed under the GPL and the LGPL.
This code was taken from GCC 13.2.0 and specifically the libiberty library which
contains files licensed under the GPL and the LGPL.

Some modifications have been made to fix a memory safety issue with the
cplus_demangle_opname function.
Support for GCC 2.x-style symbols has been reintroduced by reversing the changes
made by the commit that removed it:

From: Jason Merrill <[email protected]>
Date: Sun, 23 Dec 2018 00:06:34 +0000 (-0500)
Subject: Remove support for demangling GCC 2.x era mangling schemes.
X-Git-Tag: releases/gcc-9.1.0~2159
X-Git-Url: https://gcc.gnu.org/git/?p=gcc.git;a=commitdiff_plain;h=6c8120c5ff130e03d32ff15a8f0d0e703592a2af

Remove support for demangling GCC 2.x era mangling schemes.

libiberty/
* cplus-dem.c: Remove cplus_mangle_opname, cplus_demangle_opname,
internal_cplus_demangle, and all subroutines.
(libiberty_demanglers): Remove entries for ancient GNU (pre-3.0),
Lucid, ARM, HP, and EDG demangling styles.
(cplus_demangle): Remove 'work' variable. Don't call
internal_cplus_demangle.
include/
* demangle.h: Remove support for ancient GNU (pre-3.0), Lucid,
ARM, HP, and EDG demangling styles.

From-SVN: r267363

In addition, the cplus_demangle_opname function has been modified to address a
memory safety issue:

/* CCC: Allocate the result on the heap to prevent buffer overruns. */
extern char *
cplus_demangle_opname (const char *opname, int options);
6 changes: 3 additions & 3 deletions thirdparty/demanglegnu/alloca.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ static header *last_alloca_header = NULL; /* -> last alloca header. */

/* @undocumented C_alloca */

PTR
void *
C_alloca (size_t size)
{
auto char probe; /* Probes stack depth: */
Expand All @@ -181,7 +181,7 @@ C_alloca (size_t size)
{
register header *np = hp->h.next;

free ((PTR) hp); /* Collect garbage. */
free ((void *) hp); /* Collect garbage. */

hp = np; /* -> next header. */
}
Expand Down Expand Up @@ -210,7 +210,7 @@ C_alloca (size_t size)

/* User storage begins just after header. */

return (PTR) ((char *) new_storage + sizeof (header));
return (void *) ((char *) new_storage + sizeof (header));
}
}

Expand Down
128 changes: 40 additions & 88 deletions thirdparty/demanglegnu/ansidecl.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* ANSI and traditional C compatability macros
Copyright (C) 1991-2018 Free Software Foundation, Inc.
/* Compiler compatibility macros
Copyright (C) 1991-2023 Free Software Foundation, Inc.
This file is part of the GNU C Library.
This program is free software; you can redistribute it and/or modify
Expand All @@ -16,18 +16,7 @@ You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */

/* ANSI and traditional C compatibility macros
ANSI C is assumed if __STDC__ is #defined.
Macro ANSI C definition Traditional C definition
----- ---- - ---------- ----------- - ----------
PTR `void *' `char *'
const not defined `'
volatile not defined `'
signed not defined `'
For ease of writing code which uses GCC extensions but needs to be
/* For ease of writing code which uses GCC extensions but needs to be
portable to other compilers, we provide the GCC_VERSION macro that
simplifies testing __GNUC__ and __GNUC_MINOR__ together, and various
wrappers around __attribute__. Also, __extension__ will be #defined
Expand Down Expand Up @@ -62,24 +51,10 @@ So instead we use the macro below and test it against specific values. */
#define GCC_VERSION (__GNUC__ * 1000 + __GNUC_MINOR__)
#endif /* GCC_VERSION */

#if defined (__STDC__) || defined(__cplusplus) || defined (_AIX) || (defined (__mips) && defined (_SYSTYPE_SVR4)) || defined(_WIN32)
/* All known AIX compilers implement these things (but don't always
define __STDC__). The RISC/OS MIPS compiler defines these things
in SVR4 mode, but does not define __STDC__. */
/* [email protected]: The Compaq C++ compiler, unlike many other
C++ compilers, does not define __STDC__, though it acts as if this
was so. (Verified versions: 5.7, 6.2, 6.3, 6.5) */

#define PTR void *

#undef const
#undef volatile
#undef signed

/* inline requires special treatment; it's in C99, and GCC >=2.7 supports
it too, but it's not in C89. */
#undef inline
#if __STDC_VERSION__ >= 199901L || defined(__cplusplus) || (defined(__SUNPRO_C) && defined(__C99FEATURES__))
#if (!defined(__cplusplus) && __STDC_VERSION__ >= 199901L) || defined(__cplusplus) || (defined(__SUNPRO_C) && defined(__C99FEATURES__))
/* it's a keyword */
#else
# if GCC_VERSION >= 2007
Expand All @@ -89,22 +64,6 @@ So instead we use the macro below and test it against specific values. */
# endif
#endif

#else /* Not ANSI C. */

#define PTR char *

/* some systems define these in header files for non-ansi mode */
#undef const
#undef volatile
#undef signed
#undef inline
#define const
#define volatile
#define signed
#define inline

#endif /* ANSI C. */

/* Define macros for some gcc attributes. This permits us to use the
macros freely, and know that they will come into play for the
version of gcc in which they are supported. */
Expand Down Expand Up @@ -292,6 +251,40 @@ So instead we use the macro below and test it against specific values. */
# endif
#endif

/* Attribute `alloc_size' was valid as of gcc 4.3. */
#ifndef ATTRIBUTE_RESULT_SIZE_1
# if (GCC_VERSION >= 4003)
# define ATTRIBUTE_RESULT_SIZE_1 __attribute__ ((alloc_size (1)))
# else
# define ATTRIBUTE_RESULT_SIZE_1
#endif
#endif

#ifndef ATTRIBUTE_RESULT_SIZE_2
# if (GCC_VERSION >= 4003)
# define ATTRIBUTE_RESULT_SIZE_2 __attribute__ ((alloc_size (2)))
# else
# define ATTRIBUTE_RESULT_SIZE_2
#endif
#endif

#ifndef ATTRIBUTE_RESULT_SIZE_1_2
# if (GCC_VERSION >= 4003)
# define ATTRIBUTE_RESULT_SIZE_1_2 __attribute__ ((alloc_size (1, 2)))
# else
# define ATTRIBUTE_RESULT_SIZE_1_2
#endif
#endif

/* Attribute `warn_unused_result' was valid as of gcc 3.3. */
#ifndef ATTRIBUTE_WARN_UNUSED_RESULT
# if GCC_VERSION >= 3003
# define ATTRIBUTE_WARN_UNUSED_RESULT __attribute__ ((__warn_unused_result__))
# else
# define ATTRIBUTE_WARN_UNUSED_RESULT
# endif
#endif

/* We use __extension__ in some places to suppress -pedantic warnings
about GCC extensions. This feature didn't work properly before
gcc 2.8. */
Expand Down Expand Up @@ -322,53 +315,12 @@ So instead we use the macro below and test it against specific values. */
#define ENUM_BITFIELD(TYPE) unsigned int
#endif

#if __cpp_constexpr >= 200704
#if defined(__cplusplus) && __cpp_constexpr >= 200704
#define CONSTEXPR constexpr
#else
#define CONSTEXPR
#endif

/* C++11 adds the ability to add "override" after an implementation of a
virtual function in a subclass, to:
(A) document that this is an override of a virtual function
(B) allow the compiler to issue a warning if it isn't (e.g. a mismatch
of the type signature).
Similarly, it allows us to add a "final" to indicate that no subclass
may subsequently override the vfunc.
Provide OVERRIDE and FINAL as macros, allowing us to get these benefits
when compiling with C++11 support, but without requiring C++11.
For gcc, use "-std=c++11" to enable C++11 support; gcc 6 onwards enables
this by default (actually GNU++14). */

#if defined __cplusplus
# if __cplusplus >= 201103
/* C++11 claims to be available: use it. Final/override were only
implemented in 4.7, though. */
# if GCC_VERSION < 4007
# define OVERRIDE
# define FINAL
# else
# define OVERRIDE override
# define FINAL final
# endif
# elif GCC_VERSION >= 4007
/* G++ 4.7 supports __final in C++98. */
# define OVERRIDE
# define FINAL __final
# else
/* No C++11 support; leave the macros empty. */
# define OVERRIDE
# define FINAL
# endif
#else
/* No C++11 support; leave the macros empty. */
# define OVERRIDE
# define FINAL
#endif

/* A macro to disable the copy constructor and assignment operator.
When building with C++11 and above, the methods are explicitly
deleted, causing a compile-time error if something tries to copy.
Expand All @@ -385,7 +337,7 @@ So instead we use the macro below and test it against specific values. */
so that most attempts at copy are caught at compile-time. */

#if __cplusplus >= 201103
#if defined(__cplusplus) && __cplusplus >= 201103
#define DISABLE_COPY_AND_ASSIGN(TYPE) \
TYPE (const TYPE&) = delete; \
void operator= (const TYPE &) = delete
Expand Down
15 changes: 13 additions & 2 deletions thirdparty/demanglegnu/argv.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* Create and destroy argument vectors (argv's)
Copyright (C) 1992-2018 Free Software Foundation, Inc.
Copyright (C) 1992-2023 Free Software Foundation, Inc.
Written by Fred Fish @ Cygnus Support
This file is part of the libiberty library.
Expand Down Expand Up @@ -327,6 +327,14 @@ writeargv (char * const *argv, FILE *f)
arg++;
}

/* Write out a pair of quotes for an empty argument. */
if (arg == *argv)
if (EOF == fputs ("\"\"", f))
{
status = 1;
goto done;
}

if (EOF == fputc ('\n', f))
{
status = 1;
Expand Down Expand Up @@ -434,7 +442,10 @@ expandargv (int *argcp, char ***argvp)
due to CR/LF->CR translation when reading text files.
That does not in-and-of itself indicate failure. */
&& ferror (f))
goto error;
{
free (buffer);
goto error;
}
/* Add a NUL terminator. */
buffer[len] = '\0';
/* If the file is empty or contains only whitespace, buildargv would
Expand Down
40 changes: 40 additions & 0 deletions thirdparty/demanglegnu/copy_demangler_files.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/bin/bash

set -e

# This script copies all the source files needed for the GNU demangler from a
# copy of GCC to the specified output directory.

if [ "$#" -ne 2 ]; then
echo "usage: $0 <gcc dir> <out dir>"
exit 1
fi

GCC_DIR=$1
OUT_DIR=$2

cp "$GCC_DIR/include/ansidecl.h" "$OUT_DIR/"
cp "$GCC_DIR/include/demangle.h" "$OUT_DIR/"
cp "$GCC_DIR/include/dyn-string.h" "$OUT_DIR/"
cp "$GCC_DIR/include/environ.h" "$OUT_DIR/"
cp "$GCC_DIR/include/getopt.h" "$OUT_DIR/"
cp "$GCC_DIR/include/libiberty.h" "$OUT_DIR/"
cp "$GCC_DIR/include/safe-ctype.h" "$OUT_DIR/"
cp "$GCC_DIR/libiberty/alloca.c" "$OUT_DIR/"
cp "$GCC_DIR/libiberty/argv.c" "$OUT_DIR/"
cp "$GCC_DIR/libiberty/cp-demangle.c" "$OUT_DIR/"
cp "$GCC_DIR/libiberty/cp-demangle.h" "$OUT_DIR/"
cp "$GCC_DIR/libiberty/cplus-dem.c" "$OUT_DIR/"
cp "$GCC_DIR/libiberty/d-demangle.c" "$OUT_DIR/"
cp "$GCC_DIR/libiberty/dyn-string.c" "$OUT_DIR/"
cp "$GCC_DIR/libiberty/getopt.c" "$OUT_DIR/"
cp "$GCC_DIR/libiberty/getopt1.c" "$OUT_DIR/"
cp "$GCC_DIR/libiberty/rust-demangle.c" "$OUT_DIR/"
cp "$GCC_DIR/libiberty/safe-ctype.c" "$OUT_DIR/"
cp "$GCC_DIR/libiberty/xexit.c" "$OUT_DIR/"
cp "$GCC_DIR/libiberty/xmalloc.c" "$OUT_DIR/"
cp "$GCC_DIR/libiberty/xmemdup.c" "$OUT_DIR/"
cp "$GCC_DIR/libiberty/xstrdup.c" "$OUT_DIR/"
cp "$GCC_DIR/libiberty/testsuite/demangle-expected" "$OUT_DIR/testsuite/"
cp "$GCC_DIR/libiberty/testsuite/demangler-fuzzer.c" "$OUT_DIR/testsuite/"
cp "$GCC_DIR/libiberty/testsuite/test-demangle.c" "$OUT_DIR/testsuite/"
Loading

0 comments on commit 8d391c3

Please sign in to comment.