Bug 1389422 - Avoid @GLIBCXX_3.4.22 symbols from the use of std::thread when building with GCC 6. r?froydnj
That the wrapper implementation works has been verified by creating a
dummy program such as:
$ cat test.cc
#include <thread>
int main() {
std::thread([]() {
printf("foo\n");
}).join();
return 0;
}
And compiling it with and without the hack:
$ g++ -fno-rtti -o test test.cc -lpthread
$ objdump -TC test | grep UND.*GLIBCXX_3.4.22
0000000000000000 DF *UND* 0000000000000000 GLIBCXX_3.4.22 std::thread::_State::~_State()
0000000000000000 DF *UND* 0000000000000000 GLIBCXX_3.4.22 std::thread::_M_start_thread(std::unique_ptr<std::thread::_State, std::default_delete<std::thread::_State> >, void (*)())
$ ./test
foo
$ g++ -fno-rtti -o test test.cc $objdir/build/unix/stdc++compat/stdc++compat.o -lpthread
$ objdump -TC test | grep UND.*GLIBCXX_3.4.22
$ ./test
foo
--- a/build/unix/stdc++compat/stdc++compat.cpp
+++ b/build/unix/stdc++compat/stdc++compat.cpp
@@ -18,22 +18,24 @@
GLIBCXX_3.4.14 is from gcc 4.5.0 (151126)
GLIBCXX_3.4.15 is from gcc 4.6.0 (160071)
GLIBCXX_3.4.16 is from gcc 4.6.1 (172240)
GLIBCXX_3.4.17 is from gcc 4.7.0 (174383)
GLIBCXX_3.4.18 is from gcc 4.8.0 (190787)
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)
+ GLIBCXX_3.4.22 is from gcc 6.0 (222482)
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.
+WARNING: all symbols from this file must be defined weak when they
+overlap with libstdc++.
*/
#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>
@@ -102,8 +104,59 @@ extern "C" void __attribute__((weak))
* std::string. */
namespace std {
__attribute__((weak)) runtime_error::runtime_error(char const* s)
: runtime_error(std::string(s))
{
}
}
#endif
+
+#if MOZ_LIBSTDCXX_VERSION >= GLIBCXX_VERSION(3, 4, 21)
+/* Expose the definitions for the old ABI, allowing us to call its functions */
+#define _GLIBCXX_THREAD_ABI_COMPAT 1
+#include <thread>
+
+namespace std {
+ /* The old ABI has a thread::_M_start_thread(shared_ptr<_Impl_base>),
+ * while the new has thread::_M_start_thread(unique_ptr<_State>, void(*)()).
+ * There is an intermediate ABI at version 3.4.21, with
+ * thread::_M_start_thread(shared_ptr<_Impl_base>, void(*)()).
+ * The void(*)() parameter is only there to keep a reference to pthread_create
+ * on the caller side, and is unused in the implementation
+ * We're creating an entry point for the new and intermediate ABIs, and make
+ * them call the old ABI. */
+
+ __attribute__((weak))
+ void thread::_M_start_thread(shared_ptr<_Impl_base> impl, void (*)())
+ {
+ _M_start_thread(std::move(impl));
+ }
+
+#if MOZ_LIBSTDCXX_VERSION >= GLIBCXX_VERSION(3, 4, 22)
+ /* We need a _Impl_base-derived class wrapping a _State to call the old ABI
+ * from what we got by diverting the new API */
+ struct StateWrapper: public thread::_Impl_base {
+ unique_ptr<thread::_State> mState;
+
+ StateWrapper(unique_ptr<thread::_State> aState)
+ : mState(std::move(aState))
+ { }
+
+ void _M_run() override
+ {
+ mState->_M_run();
+ }
+ };
+
+ __attribute__((weak))
+ void thread::_M_start_thread(unique_ptr<_State> aState, void (*)())
+ {
+ auto impl = std::make_shared<StateWrapper>(std::move(aState));
+ _M_start_thread(std::move(impl));
+ }
+
+ /* For some reason this is a symbol exported by new versions of libstdc++,
+ * even though the destructor is default there too */
+ __attribute__((weak)) thread::_State::~_State() = default;
+#endif
+}
+#endif