1From 1fc21976be58987f036caa1103b821d51449af05 Mon Sep 17 00:00:00 2001 2From: David 'Digit' Turner <digit@google.com> 3Date: Fri, 17 Feb 2012 19:38:08 +0100 4Subject: gcc: prevent crash on Eclair and older platforms. 5 6The point of this patch is to work-around a bug in the Eclair 7dynamic linker, which doesn't support weak symbols. By default, 8libsupc++ and libstdc++ generate static C++ constructors that 9reference weak symbols. 10 11When they are statically linked into shared libraries, the 12corresponding code is referenced in its .init_array section 13and run when the shared library is loaded. 14 15On Eclair and previous release, the weak symbol is not resolved 16before the constructor are launched, resulting in a crash when 17the PLT entry tries to jump to address 0. 18 19By not generating weak symbol references, we avoid the problem 20completely. And we don't need them because the pthread symbols 21are all in the C library on Android, unlike legacy Linux systems 22which put them in libpthread.so (and provide weak stubs in libc.so). 23--- 24 gcc-4.6/gcc/gthr-posix.h | 13 +++++++++++++ 25 gcc-4.8/libgcc/gthr-posix.h | 13 +++++++++++++ 26 gcc-4.9/libgcc/gthr-posix.h | 13 +++++++++++++ 27 3 files changed, 39 insertions(+) 28 29diff --git a/gcc-4.6/gcc/gthr-posix.h b/gcc-4.6/gcc/gthr-posix.h 30index ecb06e2..8372c64 100644 31--- a/gcc-4.6/gcc/gthr-posix.h 32+++ b/gcc-4.6/gcc/gthr-posix.h 33@@ -38,6 +38,19 @@ see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 34 #define _REENTRANT 1 35 #endif 36 37+/* The following should normally be in a different header file, 38+ * but I couldn't find the right location. The point of the macro 39+ * definition below is to prevent libsupc++ and libstdc++ to reference 40+ * weak symbols in their static C++ constructors. Such code crashes 41+ * when a shared object linked statically to these libraries is 42+ * loaded on Android 2.1 (Eclair) and older platform releases, due 43+ * to a dynamic linker bug. 44+ */ 45+#ifdef __ANDROID__ 46+#undef GTHREAD_USE_WEAK 47+#define GTHREAD_USE_WEAK 0 48+#endif 49+ 50 #include <pthread.h> 51 #include <unistd.h> 52 53diff --git a/gcc-4.8/libgcc/gthr-posix.h b/gcc-4.8/libgcc/gthr-posix.h 54index f0d8cd7..1d03af0 100644 55--- a/gcc-4.8/libgcc/gthr-posix.h 56+++ b/gcc-4.8/libgcc/gthr-posix.h 57@@ -32,6 +32,19 @@ see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 58 #define __GTHREADS 1 59 #define __GTHREADS_CXX0X 1 60 61+/* The following should normally be in a different header file, 62+ * but I couldn't find the right location. The point of the macro 63+ * definition below is to prevent libsupc++ and libstdc++ to reference 64+ * weak symbols in their static C++ constructors. Such code crashes 65+ * when a shared object linked statically to these libraries is 66+ * loaded on Android 2.1 (Eclair) and older platform releases, due 67+ * to a dynamic linker bug. 68+ */ 69+#ifdef __ANDROID__ 70+#undef GTHREAD_USE_WEAK 71+#define GTHREAD_USE_WEAK 0 72+#endif 73+ 74 #include <pthread.h> 75 76 #if ((defined(_LIBOBJC) || defined(_LIBOBJC_WEAK)) \ 77diff --git a/gcc-4.9/libgcc/gthr-posix.h b/gcc-4.9/libgcc/gthr-posix.h 78index 7003a91..19d7d5f 100644 79--- a/gcc-4.9/libgcc/gthr-posix.h 80+++ b/gcc-4.9/libgcc/gthr-posix.h 81@@ -32,6 +32,19 @@ see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 82 #define __GTHREADS 1 83 #define __GTHREADS_CXX0X 1 84 85+/* The following should normally be in a different header file, 86+ * but I couldn't find the right location. The point of the macro 87+ * definition below is to prevent libsupc++ and libstdc++ to reference 88+ * weak symbols in their static C++ constructors. Such code crashes 89+ * when a shared object linked statically to these libraries is 90+ * loaded on Android 2.1 (Eclair) and older platform releases, due 91+ * to a dynamic linker bug. 92+ */ 93+#ifdef __ANDROID__ 94+#undef GTHREAD_USE_WEAK 95+#define GTHREAD_USE_WEAK 0 96+#endif 97+ 98 #include <pthread.h> 99 100 #if ((defined(_LIBOBJC) || defined(_LIBOBJC_WEAK)) \ 101-- 1021.9.1.423.g4596e3a 103 104