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