1 diff --git a/thread_pthread.c b/thread_pthread.c
2 index 951885ffa0..cf90321d1d 100644
5 @@ -530,9 +530,6 @@ hpux_attr_getstackaddr(const pthread_attr_t *attr, void **addr)
6 # define MAINSTACKADDR_AVAILABLE 0
9 -#if MAINSTACKADDR_AVAILABLE && !defined(get_main_stack)
10 -# define get_main_stack(addr, size) get_stack(addr, size)
13 #ifdef STACKADDR_AVAILABLE
15 @@ -614,6 +611,55 @@ get_stack(void **addr, size_t *size)
20 +#if defined(__linux__) && !defined(__GLIBC__) && defined(HAVE_GETRLIMIT)
24 +#define PAGE_SIZE sysconf(_SC_PAGE_SIZE)
28 +get_main_stack(void **addr, size_t *size)
30 + size_t start, end, limit, prevend = 0;
33 + char buf[PATH_MAX+80], s[8];
35 + STACK_GROW_DIR_DETECTION;
37 + f = fopen("/proc/self/maps", "re");
41 + while (fgets(buf, sizeof buf, f)) {
42 + n = sscanf(buf, "%zx-%zx %*s %*s %*s %*s %7s", &start, &end, s);
44 + if (n == 3 && strcmp(s, "[stack]") == 0)
54 + limit = 100 << 20; /* 100MB stack limit */
55 + if (getrlimit(RLIMIT_STACK, &r)==0 && r.rlim_cur < limit)
56 + limit = r.rlim_cur & -PAGE_SIZE;
57 + if (limit > end) limit = end;
58 + if (prevend < end - limit) prevend = end - limit;
59 + if (start > prevend) start = prevend;
60 + *addr = IS_STACK_DIR_UPPER() ? (void *)start : (void *)end;
61 + *size = end - start;
65 +# define get_main_stack(addr, size) get_stack(addr, size)