- char const *base;
- size_t size = rlimit.rlim_cur;
- extern char **environ;
- size_t page_size = sysconf (_SC_PAGESIZE);
- int stack_direction = find_stack_direction (0);
-
-# if HAVE_GETCONTEXT && HAVE_DECL_GETCONTEXT
- ucontext_t context;
- if (getcontext (&context) == 0)
- {
- base = context.uc_stack.ss_sp;
- if (stack_direction < 0)
- base -= size - context.uc_stack.ss_size;
- }
- else
-# endif
- {
- if (stack_direction < 0)
- {
- char const *a = max_address_from_argv (argv);
- char const *b = max_address_from_argv (environ);
- base = (a < b ? b : a) - size;
- base += - (size_t) base % page_size;
- }
- else
- {
- char const *a = min_address_from_argv (argv);
- char const *b = min_address_from_argv (environ);
- base = a < b ? a : b;
- base -= (size_t) base % page_size;
- }
- }
-
- if (size != rlimit.rlim_cur
- || rlimit.rlim_cur < 0
- || base + size < base
-# ifdef RLIM_SAVED_CUR
- || rlimit.rlim_cur == RLIM_SAVED_CUR
-# endif
-# ifdef RLIM_SAVED_MAX
- || rlimit.rlim_cur == RLIM_SAVED_MAX
-# endif
-# ifdef RLIM_INFINITY
- || rlimit.rlim_cur == RLIM_INFINITY
-# endif
- )
- {
- errno = EOVERFLOW;
- return -1;
- }
-
- stack_base = base;
- stack_size = size;
-
-# if DEBUG
- fprintf (stderr, "get_stack_location base=%p size=%lx\n",
- base, (unsigned long) size);
-# endif