Bug 1356926 - Make all stdc++compat symbols weak. r?froydnj draft
authorMike Hommey <mh+mozilla@glandium.org>
Thu, 03 Aug 2017 11:52:24 +0900
changeset 620116 f6f63a1e1fd051eb0e33aba7cc4233c373226322
parent 620115 73b761c59719b851715f38653bbfe666ef8d22a6
child 620129 3253f1457ebf9c86c82567dd8fcd322e587f9c51
push id71919
push userbmo:mh+mozilla@glandium.org
push dateThu, 03 Aug 2017 03:19:56 +0000
reviewersfroydnj
bugs1356926
milestone57.0a1
Bug 1356926 - Make all stdc++compat symbols weak. r?froydnj In some cases, we can end up linking some things with --static-libstdc++. The notable (only?) example of that is for the clang-plugin, and that happens because it gets some of its flags from llvm-config, which contains --static-libstdc++ because clang itself is built that way. When that happens, the combination of --static-libstdc++ and stdc++compat breaks the build because they have conflicting symbols, which is very much by design. There are two ways out of this: - avoiding either -static-libstdc++ or stdc++compat - work around the symbol conflicts The former is not totally reliable ; we'd have to accurately determine if we're in a potentially conflicting case, and remove one of the two in that case, and while we can do that for the cases we explicitly know about, that's not future-proof, and might fail just as much in the future. So we go with the latter. The way we do this is by defining all the std++compat symbols weak, such that at link time, they're overridden by any symbol with the same name. When building with -static-libstdc++, libstdc++.a provides those symbols so the linker eliminates the weak ones. When not building with -static-libstdc++, the linker keeps the symbols from stdc++compat. That last assertion is validated by the long-standing CHECK_STDCXX test that we run when linking shared libraries and programs. That still leaves the symbols weak in the final shared libraries/programs, which is a change from the current setup, but shouldn't cause problems because when using versions of libstdc++.so that do provide those symbols, it's fine to use the libstdc++.so version anyways.
build/unix/stdc++compat/stdc++compat.cpp
--- a/build/unix/stdc++compat/stdc++compat.cpp
+++ b/build/unix/stdc++compat/stdc++compat.cpp
@@ -23,37 +23,38 @@
    GLIBCXX_3.4.19 is from gcc 4.8.1 (199309)
    GLIBCXX_3.4.20 is from gcc 4.9.0 (199307)
    GLIBCXX_3.4.21 is from gcc 5.0 (210290)
 
 This file adds the necessary compatibility tricks to avoid symbols with
 version GLIBCXX_3.4.16 and bigger, keeping binary compatibility with
 libstdc++ 4.6.1.
 
+WARNING: all symbols from this file must be defined weak.
 */
 
 #define GLIBCXX_VERSION(a, b, c) (((a) << 16) | ((b) << 8) | (c))
 
 #if MOZ_LIBSTDCXX_VERSION >= GLIBCXX_VERSION(3, 4, 18)
 // Implementation of utility functions for the prime rehash policy used in
 // unordered_map and unordered_set.
 #include <unordered_map>
 #include <tr1/unordered_map>
 namespace std
 {
-  size_t
+  size_t __attribute__((weak))
   __detail::_Prime_rehash_policy::_M_next_bkt(size_t __n) const
   {
     tr1::__detail::_Prime_rehash_policy policy(_M_max_load_factor);
     size_t ret = policy._M_next_bkt(__n);
     _M_next_resize = policy._M_next_resize;
     return ret;
   }
 
-  pair<bool, size_t>
+  pair<bool, size_t> __attribute__((weak))
   __detail::_Prime_rehash_policy::_M_need_rehash(size_t __n_bkt,
                                                  size_t __n_elt,
                                                  size_t __n_ins) const
   {
     tr1::__detail::_Prime_rehash_policy policy(_M_max_load_factor);
     policy._M_next_resize = _M_next_resize;
     pair<bool, size_t> ret = policy._M_need_rehash(__n_bkt, __n_elt, __n_ins);
     _M_next_resize = policy._M_next_resize;
@@ -62,17 +63,17 @@ namespace std
 }
 #endif
 
 #if MOZ_LIBSTDCXX_VERSION >= GLIBCXX_VERSION(3, 4, 20)
 namespace std {
 
     /* We shouldn't be throwing exceptions at all, but it sadly turns out
        we call STL (inline) functions that do. */
-    void __throw_out_of_range_fmt(char const* fmt, ...)
+    void __attribute__((weak)) __throw_out_of_range_fmt(char const* fmt, ...)
     {
         va_list ap;
         char buf[1024]; // That should be big enough.
 
         va_start(ap, fmt);
         vsnprintf(buf, sizeof(buf), fmt, ap);
         buf[sizeof(buf) - 1] = 0;
         va_end(ap);
@@ -82,27 +83,27 @@ namespace std {
 
 }
 #endif
 
 #if MOZ_LIBSTDCXX_VERSION >= GLIBCXX_VERSION(3, 4, 20)
 /* Technically, this symbol is not in GLIBCXX_3.4.20, but in CXXABI_1.3.8,
    but that's equivalent, version-wise. Those calls are added by the compiler
    itself on `new Class[n]` calls. */
-extern "C" void
+extern "C" void __attribute__((weak))
 __cxa_throw_bad_array_new_length()
 {
     MOZ_CRASH();
 }
 #endif
 
 #if MOZ_LIBSTDCXX_VERSION >= GLIBCXX_VERSION(3, 4, 21)
 /* While we generally don't build with exceptions, we have some host tools
  * that do use them. libstdc++ from GCC 5.0 added exception constructors with
  * char const* argument. Older versions only have a constructor with
  * std::string. */
 namespace std {
-    runtime_error::runtime_error(char const* s)
+    __attribute__((weak)) runtime_error::runtime_error(char const* s)
     : runtime_error(std::string(s))
     {
     }
 }
 #endif