From 4bd69a209b90776ea6dfe90d6e3ee9d770a3b16c Mon Sep 17 00:00:00 2001 From: Matt Jacobson Date: Sat, 22 Oct 2022 04:37:50 -0400 Subject: [PATCH] runtime: fix compile failure from use of _Unwind_Backtrace() on FreeBSD FreeBSD's libunwind situation is complex. It uses LLVM's libunwind library, built as part of FreeBSD's libgcc_s.so. (The latter, despite its name, does not actually come from gcc in modern FreeBSD.) However, it *also* uses libcxxrt (not LLVM's libcxxabi). libcxxrt installs an originally from . That surrounds its declaration of _Unwind_Backtrace with _GNU_SOURCE guards. Therefore: on FreeBSD, define _GNU_SOURCE so that withCurrentBacktraceImpl() can access _Unwind_Backtrace. For posterity, a (probably partial) list of the various s (and how they relate to this change): 1. gcc provides one as part of its libgcc (FreeBSD does not use gcc and provides its own libgcc that does not have an ) 2. LLVM's libunwind provides one (but FreeBSD does not install it) 3. provides one (FreeBSD has this one, via its libc++, via its libcxxrt) 4. clang provides one (FreeBSD *has* this one, but the header search path for C++ source files prefers number 3) --- stdlib/public/runtime/Errors.cpp | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/stdlib/public/runtime/Errors.cpp b/stdlib/public/runtime/Errors.cpp index c3d5b0ff5f81c..e027ec6e72dbe 100644 --- a/stdlib/public/runtime/Errors.cpp +++ b/stdlib/public/runtime/Errors.cpp @@ -22,6 +22,22 @@ #include #endif +#if defined(__ELF__) +# if defined(__FreeBSD__) && !defined(_GNU_SOURCE) +// In order to access _Unwind_Backtrace() from (the non-LLVM, non-GCC) +// in FreeBSD, define _GNU_SOURCE around the include. Do this +// before including other headers which may in turn include , which +// has a header guard. +# define _GNU_SOURCE +# define _SHOULD_UNDEFINE_GNU_SOURCE +# endif +# include +# ifdef _SHOULD_UNDEFINE_GNU_SOURCE +# undef _GNU_SOURCE +# undef _SHOULD_UNDEFINE_GNU_SOURCE +# endif +#endif + #include #include #include @@ -56,10 +72,6 @@ #include #endif -#if defined(__ELF__) -#include -#endif - #include #ifdef SWIFT_HAVE_CRASHREPORTERCLIENT