Add pthred_getattr_np / phread_attr_getstrack alternatives for uClibc

Based on https://dev.openwrt.org/log/packages/Xorg/lib/qt4/patches/100-fix-webkit-for-uclibc.patch?rev=20371

Signed-off-by: Johan Sagaert <sagaert.johan@skynet.be>
---
 src/3rdparty/javascriptcore/JavaScriptCore/runtime/Collector.cpp |   61 ++++++++++
 1 file changed, 61 insertions(+)

Index: qt-everywhere-opensource-src-4.8.1/src/3rdparty/javascriptcore/JavaScriptCore/runtime/Collector.cpp
===================================================================
--- qt-everywhere-opensource-src-4.8.1.orig/src/3rdparty/javascriptcore/JavaScriptCore/runtime/Collector.cpp
+++ qt-everywhere-opensource-src-4.8.1/src/3rdparty/javascriptcore/JavaScriptCore/runtime/Collector.cpp
@@ -70,6 +70,23 @@
 #endif
 #include <unistd.h>
 
+#if defined(QT_LINUXBASE)
+#include <dlfcn.h>
+#endif
+
+#if defined(__UCLIBC__)
+// versions of uClibc 0.9.32 and below with linuxthreads.old do not have
+// pthread_getattr_np or pthread_attr_getstack.
+#if __UCLIBC_MAJOR__ == 0 && \
+    (__UCLIBC_MINOR__ < 9 || \
+    (__UCLIBC_MINOR__ == 9 && __UCLIBC_SUBLEVEL__ <= 32)) && \
+    defined(__LINUXTHREADS_OLD__)
+#define UCLIBC_USE_PROC_SELF_MAPS 1
+#include <stdio_ext.h>
+extern int *__libc_stack_end;
+#endif
+#endif
+
 #if OS(SOLARIS)
 #include <thread.h>
 #else
@@ -648,6 +665,37 @@
     get_thread_info(find_thread(NULL), &threadInfo);
     return threadInfo.stack_end;
 #elif OS(UNIX)
+#ifdef UCLIBC_USE_PROC_SELF_MAPS
+    // Read /proc/self/maps and locate the line whose address
+    // range contains __libc_stack_end.
+    FILE *file = fopen("/proc/self/maps", "r");
+    if (!file)
+        return 0;
+    __fsetlocking(file, FSETLOCKING_BYCALLER);
+    char *line = NULL;
+    size_t lineLen = 0;
+    while (!feof_unlocked(file)) {
+        if (getdelim(&line, &lineLen, '\n', file) <= 0)
+            break;
+
+        long from;
+        long to;
+        if (sscanf (line, "%lx-%lx", &from, &to) != 2)
+            continue;
+        if (from <= (long)__libc_stack_end && (long)__libc_stack_end < to) {
+            fclose(file);
+            free(line);
+#ifdef _STACK_GROWS_UP
+            return (void *)from;
+#else
+            return (void *)to;
+#endif
+        }
+    }
+    fclose(file);
+    free(line);
+    return 0;
+#else
     AtomicallyInitializedStatic(Mutex&, mutex = *new Mutex);
     MutexLocker locker(mutex);
     static void* stackBase = 0;
@@ -655,11 +703,23 @@
     static pthread_t stackThread;
     pthread_t thread = pthread_self();
     if (stackBase == 0 || thread != stackThread) {
+
+#if defined(QT_LINUXBASE)
+        // LinuxBase is missing pthread_getattr_np - resolve it once at runtime instead
+        // see http://bugs.linuxbase.org/show_bug.cgi?id=2364
+        typedef int (*GetAttrPtr)(pthread_t, pthread_attr_t *);
+        static int (*pthread_getattr_np_ptr)(pthread_t, pthread_attr_t *) = 0;
+        if (!pthread_getattr_np_ptr)
+            *(void **)&pthread_getattr_np_ptr = dlsym(RTLD_DEFAULT, "pthread_getattr_np");
+#endif
         pthread_attr_t sattr;
         pthread_attr_init(&sattr);
 #if HAVE(PTHREAD_NP_H) || OS(NETBSD)
         // e.g. on FreeBSD 5.4, neundorf@kde.org
         pthread_attr_get_np(thread, &sattr);
+#elif defined(QT_LINUXBASE)
+        if (pthread_getattr_np_ptr)
+            pthread_getattr_np_ptr(thread, &sattr);
 #else
         // FIXME: this function is non-portable; other POSIX systems may have different np alternatives
         pthread_getattr_np(thread, &sattr);
@@ -671,6 +731,7 @@
         stackThread = thread;
     }
     return static_cast<char*>(stackBase) + stackSize;
+#endif
 #else
 #error Need a way to get the stack base on this platform
 #endif
