Patchwork Use libbacktrace for libsanitizer symbolization (take 2, PR sanitizer/59136)

login
register
mail settings
Submitter Jakub Jelinek
Date Dec. 5, 2013, 7:59 p.m.
Message ID <20131205195911.GA892@tucnak.redhat.com>
Download mbox | patch
Permalink /patch/297382/
State New
Headers show

Comments

Jakub Jelinek - Dec. 5, 2013, 7:59 p.m.
Hi!

This is a second attempt at libsanitizer symbolization using
libbacktrace.  The compiler-rt maintained bit have been
already added by the recent merge from compiler-rt, so this
patch is mostly configury/Makefile stuff.  Rather than using
libbacktrace.la built in libbacktrace directory directly this
patch builds libsanitizer's own copy of a subset of libbacktrace
that it actually needs (only everything required for
backtrace_{{sym,pc}info,create_state}), renames the symbols
to __asan_backtrace_* so that when it is e.g. through -static-libasan
etc. it doesn't clash with user symbols or other projects using
libbacktrace (and, as libasan isn't yet symbol versioned, also doesn't
export backtrace_* symbols from the DSO).

I have been trying earlier today to make it work with Internal{Alloc,Free},
but that gives up on larger allocations (e.g. 150KB), so this just
uses Ian's mmap allocator.

Regtested on x86_64-linux (--target_board=unix\{-m32,-m64\}), ok for
trunk (will do full bootstrap/regtest momentarily)?

2013-11-18  Jakub Jelinek  <jakub@redhat.com>

	PR sanitizer/59136
libsanitizer/
	* sanitizer_common/Makefile.am (AM_CXXFLAGS): If
	LIBBACKTRACE_SUPPORTED add -DSANITIZER_LIBBACKTRACE
	and -I/-include flags.
	* lsan/Makefile.am (liblsan_la_LIBADD): Add
	libsanitizer_libbacktrace.la if LIBBACKTRACE_SUPPORTED.
	* tsan/Makefile.am (libtsan_la_LIBADD): Likewise.
	* ubsan/Makefile.am (libubsan_la_LIBADD): Likewise.
	* asan/Makefile.am (libasan_la_LIBADD): Likewise.
	* Makefile.am (SUBDIRS): If LIBBACKTRACE_SUPPORTED add
	libbacktrace.
	* README.gcc: Document that also lsan and ubsan are
	maintained in compiler-rt upstream.
	* libbacktrace/Makefile.am: New file.
	* libbacktrace/backtrace-rename.h: New file.
	* libbacktrace/backtrace-supported.h.in: New file.
	* libbacktrace/bridge.cc: New file.
	* configure.ac: Add tests needed for libbacktrace build
	within libsanitizer.
	* sanitizer_common/Makefile.in: Regenerated.
	* lsan/Makefile.in: Regenerated.
	* tsan/Makefile.in: Regenerated.
	* ubsan/Makefile.in: Regenerated.
	* libbacktrace/Makefile.in: Generated.
	* config.h.in: Regenerated.
	* configure: Regenerated.
	* Makefile.in: Regenerated.
	* interception/Makefile.in: Regenerated.
	* asan/Makefile.in: Regenerated.
	* aclocal.m4: Regenerated.
testsuite/
	* c-c++-common/asan/strip-path-prefix-1.c: Allow also the
	filename:line instead of (modulename+offset) form with stripped
	initial / from the filename.


	Jakub
Dodji Seketeli - Jan. 9, 2014, 9:02 a.m.
Jakub Jelinek <jakub@redhat.com> writes:

> This is a second attempt at libsanitizer symbolization using
> libbacktrace.  The compiler-rt maintained bit have been
> already added by the recent merge from compiler-rt, so this
> patch is mostly configury/Makefile stuff.  Rather than using
> libbacktrace.la built in libbacktrace directory directly this
> patch builds libsanitizer's own copy of a subset of libbacktrace
> that it actually needs (only everything required for
> backtrace_{{sym,pc}info,create_state}),

OK.

> renames the symbols to __asan_backtrace_* so that when it is
> e.g. through -static-libasan etc. it doesn't clash with user symbols
> or other projects using libbacktrace (and, as libasan isn't yet symbol
> versioned, also doesn't export backtrace_* symbols from the DSO).

So we are carrying a "light" fork for libbacktrace then.  I am little
bit concerned about the maintenance cost of this over time.  I guess we
can figure out a way to factorize libbacktrace if the cost of its
maintenance really rises.


> Regtested on x86_64-linux (--target_board=unix\{-m32,-m64\}), ok for
> trunk (will do full bootstrap/regtest momentarily)?

Looks good to me.

Thank you.

Patch

--- libsanitizer/sanitizer_common/Makefile.am.jj	2013-12-05 20:07:59.907420834 +0100
+++ libsanitizer/sanitizer_common/Makefile.am	2013-12-05 20:14:45.044345516 +0100
@@ -1,4 +1,4 @@ 
-AM_CPPFLAGS = -I $(top_srcdir)/include 
+AM_CPPFLAGS = -I $(top_srcdir)/include
  
 # May be used by toolexeclibdir.
 gcc_version := $(shell cat $(top_srcdir)/../gcc/BASE-VER)
@@ -6,6 +6,11 @@  gcc_version := $(shell cat $(top_srcdir)
 DEFS = -D_GNU_SOURCE -D_DEBUG -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS 
 AM_CXXFLAGS = -Wall -W -Wno-unused-parameter -Wwrite-strings -pedantic -Wno-long-long  -fPIC -fno-builtin -fno-exceptions -fno-rtti -fomit-frame-pointer -funwind-tables -fvisibility=hidden -Wno-variadic-macros
 AM_CXXFLAGS += $(LIBSTDCXX_RAW_CXX_CXXFLAGS)
+if LIBBACKTRACE_SUPPORTED
+AM_CXXFLAGS += -DSANITIZER_LIBBACKTRACE -I $(top_srcdir)/../libbacktrace \
+	       -I $(top_builddir)/libbacktrace \
+	       -include $(top_srcdir)/libbacktrace/backtrace-rename.h
+endif
 ACLOCAL_AMFLAGS = -I m4
 
 noinst_LTLIBRARIES = libsanitizer_common.la
--- libsanitizer/lsan/Makefile.am.jj	2013-12-05 20:07:59.919420773 +0100
+++ libsanitizer/lsan/Makefile.am	2013-12-05 20:14:45.044345516 +0100
@@ -28,7 +28,11 @@  lsan_files = \
 libsanitizer_lsan_la_SOURCES = $(sanitizer_lsan_files)
 
 liblsan_la_SOURCES = $(lsan_files)
-liblsan_la_LIBADD = $(top_builddir)/sanitizer_common/libsanitizer_common.la $(top_builddir)/interception/libinterception.la $(LIBSTDCXX_RAW_CXX_LDFLAGS)
+liblsan_la_LIBADD = $(top_builddir)/sanitizer_common/libsanitizer_common.la $(top_builddir)/interception/libinterception.la
+if LIBBACKTRACE_SUPPORTED
+liblsan_la_LIBADD += $(top_builddir)/libbacktrace/libsanitizer_libbacktrace.la
+endif
+liblsan_la_LIBADD += $(LIBSTDCXX_RAW_CXX_LDFLAGS)
 liblsan_la_LDFLAGS = -version-info `grep -v '^\#' $(srcdir)/libtool-version` $(link_liblsan)
 
 # Work around what appears to be a GNU make bug handling MAKEFLAGS
--- libsanitizer/tsan/Makefile.am.jj	2013-12-05 20:07:59.921420762 +0100
+++ libsanitizer/tsan/Makefile.am	2013-12-05 20:14:45.044345516 +0100
@@ -39,7 +39,11 @@  tsan_files = \
         tsan_rtl_amd64.S
 
 libtsan_la_SOURCES = $(tsan_files) 
-libtsan_la_LIBADD = $(top_builddir)/sanitizer_common/libsanitizer_common.la $(top_builddir)/interception/libinterception.la $(LIBSTDCXX_RAW_CXX_LDFLAGS)
+libtsan_la_LIBADD = $(top_builddir)/sanitizer_common/libsanitizer_common.la $(top_builddir)/interception/libinterception.la
+if LIBBACKTRACE_SUPPORTED
+libtsan_la_LIBADD += $(top_builddir)/libbacktrace/libsanitizer_libbacktrace.la
+endif
+libtsan_la_LIBADD += $(LIBSTDCXX_RAW_CXX_LDFLAGS)
 libtsan_la_LDFLAGS = -version-info `grep -v '^\#' $(srcdir)/libtool-version` $(link_libtsan)
 
 # Work around what appears to be a GNU make bug handling MAKEFLAGS
--- libsanitizer/ubsan/Makefile.am.jj	2013-12-05 20:07:59.934420693 +0100
+++ libsanitizer/ubsan/Makefile.am	2013-12-05 20:14:45.045345511 +0100
@@ -22,6 +22,9 @@  libubsan_la_LIBADD = $(top_builddir)/san
 if !USING_MAC_INTERPOSE
 libubsan_la_LIBADD += $(top_builddir)/interception/libinterception.la
 endif
+if LIBBACKTRACE_SUPPORTED
+libubsan_la_LIBADD += $(top_builddir)/libbacktrace/libsanitizer_libbacktrace.la
+endif
 libubsan_la_LIBADD += $(LIBSTDCXX_RAW_CXX_LDFLAGS)
 libubsan_la_LDFLAGS = -version-info `grep -v '^\#' $(srcdir)/libtool-version` $(link_libubsan)
 
--- libsanitizer/Makefile.am.jj	2013-12-05 20:07:59.934420693 +0100
+++ libsanitizer/Makefile.am	2013-12-05 20:14:45.045345511 +0100
@@ -1,13 +1,15 @@ 
 ACLOCAL_AMFLAGS = -I .. -I ../config
 
-if TSAN_SUPPORTED
-SUBDIRS = interception sanitizer_common lsan asan tsan ubsan
-else
-SUBDIRS = interception sanitizer_common lsan asan ubsan
+SUBDIRS = sanitizer_common
+if !USING_MAC_INTERPOSE
+SUBDIRS += interception
 endif
-
-if USING_MAC_INTERPOSE
-SUBDIRS = sanitizer_common lsan asan ubsan
+if LIBBACKTRACE_SUPPORTED
+SUBDIRS += libbacktrace
+endif
+SUBDIRS += lsan asan ubsan
+if TSAN_SUPPORTED
+SUBDIRS += tsan
 endif
 
 ## May be used by toolexeclibdir.
--- libsanitizer/README.gcc.jj	2013-12-05 20:07:59.918420778 +0100
+++ libsanitizer/README.gcc	2013-12-05 20:14:45.045345511 +0100
@@ -9,6 +9,8 @@  http://llvm.org/svn/llvm-project/compile
   lib/interception
   lib/asan
   lib/tsan
+  lib/lsan
+  lib/ubsan
 
 Trivial and urgent fixes (portability, build fixes, etc.) may go directly to the
 GCC tree.  All non-trivial changes, functionality improvements, etc. should go
--- libsanitizer/libbacktrace/Makefile.am.jj	2013-12-05 20:14:45.046345506 +0100
+++ libsanitizer/libbacktrace/Makefile.am	2013-12-05 20:23:52.509541870 +0100
@@ -0,0 +1,78 @@ 
+# Makefile.am -- Backtrace in libsanitizer Makefile.
+# Copyright (C) 2012-2013 Free Software Foundation, Inc.
+
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are
+# met:
+
+#     (1) Redistributions of source code must retain the above copyright
+#     notice, this list of conditions and the following disclaimer. 
+
+#     (2) Redistributions in binary form must reproduce the above copyright
+#     notice, this list of conditions and the following disclaimer in
+#     the documentation and/or other materials provided with the
+#     distribution.  
+
+#     (3) The name of the author may not be used to
+#     endorse or promote products derived from this software without
+#     specific prior written permission.
+
+# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+# DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
+# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
+# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+
+ACLOCAL_AMFLAGS = -I ../.. -I ../../config
+
+AM_CPPFLAGS = -I $(top_srcdir)/../include -I $(top_srcdir)/../libgcc \
+	-I ../../libgcc -I .. -I $(top_srcdir) -I $(top_srcdir)/../libbacktrace
+
+WARN_FLAGS = -W -Wall -Wwrite-strings -Wmissing-format-attribute \
+	     -Wcast-qual -Werror
+C_WARN_FLAGS = $(WARN_FLAGS) -Wstrict-prototypes -Wmissing-prototypes -Wold-style-definition
+CXX_WARN_FLAGS = $(WARN_FLAGS) -Wno-unused-parameter
+AM_CFLAGS = $(C_WARN_FLAGS)
+AM_CXXFLAGS = $(CXX_WARN_FLAGS) -fno-rtti -fno-exceptions
+
+noinst_LTLIBRARIES = libsanitizer_libbacktrace.la
+
+libsanitizer_libbacktrace_la_SOURCES = \
+	../../libbacktrace/backtrace.h \
+	../../libbacktrace/atomic.c \
+	../../libbacktrace/dwarf.c \
+	../../libbacktrace/fileline.c \
+	../../libbacktrace/internal.h \
+	../../libbacktrace/posix.c \
+	../../libbacktrace/state.c \
+	bridge.cc
+
+FORMAT_FILES = \
+	../../libbacktrace/elf.c \
+	../../libbacktrace/unknown.c
+
+VIEW_FILES = \
+	../../libbacktrace/read.c \
+	../../libbacktrace/mmapio.c
+
+ALLOC_FILES = \
+	../../libbacktrace/alloc.c \
+	../../libbacktrace/mmap.c
+
+EXTRA_libsanitizer_libbacktrace_la_SOURCES = \
+	$(FORMAT_FILES) \
+	$(VIEW_FILES) \
+	$(ALLOC_FILES)
+
+libsanitizer_libbacktrace_la_LIBADD = \
+	$(FORMAT_FILE) \
+	$(VIEW_FILE) \
+	$(ALLOC_FILE)
+
+libsanitizer_libbacktrace_la_DEPENDENCIES = $(libsanitizer_libbacktrace_la_LIBADD)
--- libsanitizer/libbacktrace/backtrace-rename.h.jj	2013-12-05 20:14:45.046345506 +0100
+++ libsanitizer/libbacktrace/backtrace-rename.h	2013-12-05 20:18:00.268346235 +0100
@@ -0,0 +1,41 @@ 
+/* Ensure we don't pollute application namespace.  */
+#define backtrace_alloc __asan_backtrace_alloc
+#define backtrace_close __asan_backtrace_close
+#define backtrace_create_state __asan_backtrace_create_state
+#define backtrace_dwarf_add __asan_backtrace_dwarf_add
+#define backtrace_free __asan_backtrace_free
+#define backtrace_get_view __asan_backtrace_get_view
+#define backtrace_initialize __asan_backtrace_initialize
+#define backtrace_open __asan_backtrace_open
+#define backtrace_pcinfo __asan_backtrace_pcinfo
+#define backtrace_release_view __asan_backtrace_release_view
+#define backtrace_syminfo __asan_backtrace_syminfo
+#define backtrace_vector_finish __asan_backtrace_vector_finish
+#define backtrace_vector_grow __asan_backtrace_vector_grow
+#define backtrace_vector_release __asan_backtrace_vector_release
+
+#ifndef __cplusplus
+
+#include <string.h>
+
+extern void *__asan_internal_memcpy (void *, const void *, size_t);
+extern void *__asan_internal_memset (void *, int, size_t);
+extern int __asan_internal_strcmp (const char *, const char *);
+extern size_t __asan_internal_strlen (const char *);
+extern size_t __asan_internal_strnlen (const char *, size_t);
+
+#undef memcpy
+#undef memset
+#undef strcmp
+#undef strlen
+#undef strnlen
+
+#define memcpy(x,y,z) __asan_internal_memcpy (x, y, z)
+#define memset(x,y,z) __asan_internal_memset (x, y, z)
+#define strcmp(x,y) __asan_internal_strcmp (x, y)
+#define strlen(x) __asan_internal_strlen (x)
+#ifdef HAVE_DECL_STRNLEN
+#define strnlen(x,y) __asan_internal_strnlen (x, y)
+#endif
+
+#endif
--- libsanitizer/libbacktrace/backtrace-supported.h.in.jj	2013-12-05 20:14:45.046345506 +0100
+++ libsanitizer/libbacktrace/backtrace-supported.h.in	2013-12-05 20:14:45.046345506 +0100
@@ -0,0 +1,61 @@ 
+/* backtrace-supported.h.in -- Whether stack backtrace is supported.
+   Copyright (C) 2012-2013 Free Software Foundation, Inc.
+   Written by Ian Lance Taylor, Google.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are
+met:
+
+    (1) Redistributions of source code must retain the above copyright
+    notice, this list of conditions and the following disclaimer. 
+
+    (2) Redistributions in binary form must reproduce the above copyright
+    notice, this list of conditions and the following disclaimer in
+    the documentation and/or other materials provided with the
+    distribution.  
+    
+    (3) The name of the author may not be used to
+    endorse or promote products derived from this software without
+    specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
+INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
+IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.  */
+
+/* The file backtrace-supported.h.in is used by configure to generate
+   the file backtrace-supported.h.  The file backtrace-supported.h may
+   be #include'd to see whether the backtrace library will be able to
+   get a backtrace and produce symbolic information.  */
+
+
+/* BACKTRACE_SUPPORTED will be #define'd as 1 if the backtrace library
+   should work, 0 if it will not.  Libraries may #include this to make
+   other arrangements.  */
+
+#define BACKTRACE_SUPPORTED @BACKTRACE_SUPPORTED@
+
+/* BACKTRACE_USES_MALLOC will be #define'd as 1 if the backtrace
+   library will call malloc as it works, 0 if it will call mmap
+   instead.  This may be used to determine whether it is safe to call
+   the backtrace functions from a signal handler.  In general this
+   only applies to calls like backtrace and backtrace_pcinfo.  It does
+   not apply to backtrace_simple, which never calls malloc.  It does
+   not apply to backtrace_print, which always calls fprintf and
+   therefore malloc.  */
+
+#define BACKTRACE_USES_MALLOC @BACKTRACE_USES_MALLOC@
+
+/* BACKTRACE_SUPPORTS_THREADS will be #define'd as 1 if the backtrace
+   library is configured with threading support, 0 if not.  If this is
+   0, the threaded parameter to backtrace_create_state must be passed
+   as 0.  */
+
+#define BACKTRACE_SUPPORTS_THREADS @BACKTRACE_SUPPORTS_THREADS@
--- libsanitizer/libbacktrace/bridge.cc.jj	2013-12-05 20:14:45.047345501 +0100
+++ libsanitizer/libbacktrace/bridge.cc	2013-12-05 20:18:10.215296258 +0100
@@ -0,0 +1,72 @@ 
+/* bridge.cc -- extern "C" wrappers around sanitizer_common APIs.
+   Copyright (C) 2013 Free Software Foundation, Inc.
+   Written by Jakub Jelinek, Red Hat, Inc.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are
+met:
+
+    (1) Redistributions of source code must retain the above copyright
+    notice, this list of conditions and the following disclaimer. 
+
+    (2) Redistributions in binary form must reproduce the above copyright
+    notice, this list of conditions and the following disclaimer in
+    the documentation and/or other materials provided with the
+    distribution.  
+    
+    (3) The name of the author may not be used to
+    endorse or promote products derived from this software without
+    specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
+INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
+IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.  */
+
+#include "config.h"
+
+#include <string.h>
+
+#include "sanitizer_common/sanitizer_allocator_internal.h"
+
+extern "C"
+{
+
+void *
+__asan_internal_memcpy (void *dest, const void *src, size_t n)
+{
+  return __sanitizer::internal_memcpy (dest, src, n);
+}
+
+void *
+__asan_internal_memset (void *dest, int c, size_t n)
+{
+  return __sanitizer::internal_memset (dest, c, n);
+}
+
+int
+__asan_internal_strcmp (const char *s1, const char *s2)
+{
+  return __sanitizer::internal_strcmp (s1, s2);
+}
+
+size_t
+__asan_internal_strlen (const char *str)
+{
+  return __sanitizer::internal_strlen (str);
+}
+
+size_t
+__asan_internal_strnlen (const char *str, size_t n)
+{
+  return __sanitizer::internal_strnlen (str, n);
+}
+
+}
--- libsanitizer/configure.ac.jj	2013-12-05 20:08:00.292418861 +0100
+++ libsanitizer/configure.ac	2013-12-05 20:25:18.446103691 +0100
@@ -16,6 +16,8 @@  AC_ARG_ENABLE(version-specific-runtime-l
 [version_specific_libs=no])
 AC_MSG_RESULT($version_specific_libs)
 
+AC_USE_SYSTEM_EXTENSIONS
+
 # Do not delete or change the following two lines.  For why, see
 # http://gcc.gnu.org/ml/libstdc++/2003-07/msg00451.html
 AC_CANONICAL_SYSTEM
@@ -61,10 +63,16 @@  AC_SUBST(toolexeclibdir)
 AC_PROG_CC
 AC_PROG_CXX
 AM_PROG_AS
+AC_PROG_RANLIB
 
 AC_LIBTOOL_DLOPEN
 AM_PROG_LIBTOOL
 
+AC_PROG_AWK
+case "$AWK" in
+"") AC_MSG_ERROR([can't build without awk]) ;;
+esac
+
 AC_SUBST(enable_shared)
 AC_SUBST(enable_static)
 
@@ -122,9 +130,177 @@  case "$host" in
 esac
 AM_CONDITIONAL(USING_MAC_INTERPOSE, $MAC_INTERPOSE)
 
-AC_CONFIG_FILES([Makefile libsanitizer.spec])
+backtrace_supported=yes
+
+# Test for __sync support.
+AC_CACHE_CHECK([__sync extensions],
+[libsanitizer_cv_sys_sync],
+[if test -n "${with_target_subdir}"; then
+   libsanitizer_cv_sys_sync=yes
+ else
+   AC_LINK_IFELSE(
+     [AC_LANG_PROGRAM([int i;],
+                      [__sync_bool_compare_and_swap (&i, i, i);
+                       __sync_lock_test_and_set (&i, 1);
+                       __sync_lock_release (&i);])],
+     [libsanitizer_cv_sys_sync=yes],
+     [libsanitizer_cv_sys_sync=no])
+ fi])
+if test "$libsanitizer_cv_sys_sync" = "yes"; then
+  AC_DEFINE([HAVE_SYNC_FUNCTIONS], 1,
+	    [Define to 1 if you have the __sync functions])
+fi
+
+# Test for __atomic support.
+AC_CACHE_CHECK([__atomic extensions],
+[libsanitizer_cv_sys_atomic],
+[if test -n "${with_target_subdir}"; then
+   libsanitizer_cv_sys_atomic=yes
+ else
+   AC_LINK_IFELSE(
+     [AC_LANG_PROGRAM([int i;],
+     		      [__atomic_load_n (&i, __ATOMIC_ACQUIRE);
+		       __atomic_store_n (&i, 1, __ATOMIC_RELEASE);])],
+     [libsanitizer_cv_sys_atomic=yes],
+     [libsanitizer_cv_sys_atomic=no])
+ fi])
+if test "$libsanitizer_cv_sys_atomic" = "yes"; then
+  AC_DEFINE([HAVE_ATOMIC_FUNCTIONS], 1,
+	    [Define to 1 if you have the __atomic functions])
+fi
+
+# The library needs to be able to read the executable itself.  Compile
+# a file to determine the executable format.  The awk script
+# filetype.awk prints out the file type.
+AC_CACHE_CHECK([output filetype],
+[libsanitizer_cv_sys_filetype],
+[filetype=
+AC_COMPILE_IFELSE(
+  [AC_LANG_PROGRAM([int i;], [int j;])],
+  [filetype=`${AWK} -f $srcdir/../libbacktrace/filetype.awk conftest.$ac_objext`],
+  [AC_MSG_FAILURE([compiler failed])])
+libsanitizer_cv_sys_filetype=$filetype])
+
+# Match the file type to decide what files to compile.
+FORMAT_FILE=
+case "$libsanitizer_cv_sys_filetype" in
+elf*) FORMAT_FILE="elf.lo" ;;
+*) AC_MSG_WARN([could not determine output file type])
+   FORMAT_FILE="unknown.lo"
+   backtrace_supported=no
+   ;;
+esac
+AC_SUBST(FORMAT_FILE)
+
+# ELF defines.
+elfsize=
+case "$libsanitizer_cv_sys_filetype" in
+elf32) elfsize=32 ;;
+elf64) elfsize=64 ;;
+esac
+AC_DEFINE_UNQUOTED([BACKTRACE_ELF_SIZE], [$elfsize], [ELF size: 32 or 64])
+
+BACKTRACE_SUPPORTED=0
+if test "$backtrace_supported" = "yes"; then
+  BACKTRACE_SUPPORTED=1
+fi
+AC_SUBST(BACKTRACE_SUPPORTED)
+
+GCC_HEADER_STDINT(gstdint.h)
+
+AC_CHECK_HEADERS(sys/mman.h)
+if test "$ac_cv_header_sys_mman_h" = "no"; then
+  have_mmap=no
+else
+  if test -n "${with_target_subdir}"; then
+    # When built as a GCC target library, we can't do a link test.  We
+    # simply assume that if we have mman.h, we have mmap.
+    have_mmap=yes
+  else
+    AC_CHECK_FUNC(mmap, [have_mmap=yes], [have_mmap=no])
+  fi
+fi
+if test "$have_mmap" = "no"; then
+  VIEW_FILE=read.lo
+  ALLOC_FILE=alloc.lo
+else
+  VIEW_FILE=mmapio.lo
+  AC_PREPROC_IFELSE([
+#include <sys/mman.h>
+#if !defined(MAP_ANONYMOUS) && !defined(MAP_ANON)
+  #error no MAP_ANONYMOUS
+#endif
+], [ALLOC_FILE=mmap.lo], [ALLOC_FILE=alloc.lo])
+fi
+AC_SUBST(VIEW_FILE)
+AC_SUBST(ALLOC_FILE)
+
+BACKTRACE_USES_MALLOC=0
+if test "$ALLOC_FILE" = "alloc.lo"; then
+  BACKTRACE_USES_MALLOC=1
+fi
+AC_SUBST(BACKTRACE_USES_MALLOC)
+
+# Don't care about thread support
+BACKTRACE_SUPPORTS_THREADS=0
+AC_SUBST(BACKTRACE_SUPPORTS_THREADS)
+
+# Check for dl_iterate_phdr.
+AC_CHECK_HEADERS(link.h)
+if test "$ac_cv_header_link_h" = "no"; then
+  have_dl_iterate_phdr=no
+else
+  # When built as a GCC target library, we can't do a link test.
+  AC_EGREP_HEADER([dl_iterate_phdr], [link.h], [have_dl_iterate_phdr=yes],
+		  [have_dl_iterate_phdr=no])
+  case "${host}" in
+  *-*-solaris2.10*)
+    # Avoid dl_iterate_phdr on Solaris 10, where it is in the
+    # header file but is only in -ldl.
+    have_dl_iterate_phdr=no ;;
+  esac
+fi
+if test "$have_dl_iterate_phdr" = "yes"; then
+  AC_DEFINE(HAVE_DL_ITERATE_PHDR, 1, [Define if dl_iterate_phdr is available.])
+fi
+
+# Check for the fcntl function.
+if test -n "${with_target_subdir}"; then
+   case "${host}" in
+   *-*-mingw*) have_fcntl=no ;;
+   *) have_fcntl=yes ;;
+   esac
+else
+  AC_CHECK_FUNC(fcntl, [have_fcntl=yes], [have_fcntl=no])
+fi
+if test "$have_fcntl" = "yes"; then
+  AC_DEFINE([HAVE_FCNTL], 1,
+	    [Define to 1 if you have the fcntl function])
+fi
+
+AC_CHECK_DECLS(strnlen)
+
+# Check for getexecname function.
+if test -n "${with_target_subdir}"; then
+   case "${host}" in
+   *-*-solaris2*) have_getexecname=yes ;;
+   *) have_getexecname=no ;;
+   esac
+else
+  AC_CHECK_FUNC(getexecname, [have_getexecname=yes], [have_getexecname=no])
+fi
+if test "$have_getexecname" = "yes"; then
+  AC_DEFINE(HAVE_GETEXECNAME, 1, [Define if getexecname is available.])
+fi
+
+AM_CONDITIONAL(LIBBACKTRACE_SUPPORTED,
+	       [test "x${BACKTRACE_SUPPORTED}x${BACKTRACE_USES_MALLOC}" = "x1x0"])
+
+AH_BOTTOM([#include "libbacktrace/backtrace-rename.h"])
+AC_CONFIG_FILES([Makefile libsanitizer.spec libbacktrace/backtrace-supported.h])
+AC_CONFIG_HEADER(config.h)
 
-AC_CONFIG_FILES(AC_FOREACH([DIR], [interception sanitizer_common lsan asan ubsan], [DIR/Makefile ]),
+AC_CONFIG_FILES(AC_FOREACH([DIR], [interception sanitizer_common libbacktrace lsan asan ubsan], [DIR/Makefile ]),
   [cat > vpsed$$ << \_EOF
 s!`test -f '$<' || echo '$(srcdir)/'`!!
 _EOF
--- libsanitizer/asan/Makefile.am.jj	2013-12-05 20:07:59.915420793 +0100
+++ libsanitizer/asan/Makefile.am	2013-12-05 20:14:45.044345516 +0100
@@ -36,10 +36,12 @@  asan_files = \
 	asan_win.cc
 
 libasan_la_SOURCES = $(asan_files)
-if USING_MAC_INTERPOSE
 libasan_la_LIBADD = $(top_builddir)/sanitizer_common/libsanitizer_common.la $(top_builddir)/lsan/libsanitizer_lsan.la
-else
-libasan_la_LIBADD = $(top_builddir)/sanitizer_common/libsanitizer_common.la $(top_builddir)/lsan/libsanitizer_lsan.la $(top_builddir)/interception/libinterception.la
+if !USING_MAC_INTERPOSE
+libasan_la_LIBADD += $(top_builddir)/interception/libinterception.la
+endif
+if LIBBACKTRACE_SUPPORTED
+libasan_la_LIBADD += $(top_builddir)/libbacktrace/libsanitizer_libbacktrace.la
 endif
 libasan_la_LIBADD += $(LIBSTDCXX_RAW_CXX_LDFLAGS)
 
--- gcc/testsuite/c-c++-common/asan/strip-path-prefix-1.c.jj	2012-12-13 00:02:50.000000000 +0100
+++ gcc/testsuite/c-c++-common/asan/strip-path-prefix-1.c	2013-11-18 13:11:16.538581367 +0100
@@ -12,4 +12,4 @@  int main() {
 }
 
 /* { dg-output "heap-use-after-free.*(\n|\r\n|\r)" } */
-/* { dg-output "    #0 0x\[0-9a-f\]+ \[(\]\[^/\]\[^\n\r]*(\n|\r\n|\r)" } */
+/* { dg-output "    #0 0x\[0-9a-f\]+ \[(\]?\[^/\]\[^\n\r]*(\n|\r\n|\r)" } */