-# if HAVE_XSI_STACK_OVERFLOW_HEURISTIC
-# define get_stack_location(argv) 0
-# else
-
-# if defined RLIMIT_STACK && defined _SC_PAGESIZE
-
-/* Return the minimum machine address deducible from ARGV. This
- includes the addresses of all the strings that ARGV points at, as
- well as the address of ARGV itself. */
-
-static char const *
-min_address_from_argv (char * const *argv)
-{
- char const *min = (char const *) argv;
- char const *p;
- while ((p = *argv++))
- if (p < min)
- min = p;
- return min;
-}
-
-/* Return the maximum machine address deducible from ARGV. */
-
-static char const *
-max_address_from_argv (char * const *argv)
-{
- char const *max = *argv;
- char const *max1;
- char const *p;
- while ((p = *argv++))
- if (max < p)
- max = p;
- max1 = (char const *) (argv + 1);
- return max && max1 < max ? max + strlen (max) + 1 : max1;
-}
-# endif
-
-/* The base and size of the stack, determined at startup. */
-static char const * volatile stack_base;
-static size_t volatile stack_size;
-
-/* Store the base and size of the stack into the static variables
- STACK_BASE and STACK_SIZE. The base is the numerically lowest
- address in the stack. Return -1 (setting errno) if this cannot be
- done. */
-
-static int
-get_stack_location (char * const *argv)
-{
-# if ! (defined RLIMIT_STACK && defined _SC_PAGESIZE)
-
- errno = ENOTSUP;
- return -1;
-
-# else
-
- struct rlimit rlimit;
- int r = getrlimit (RLIMIT_STACK, &rlimit);
- if (r == 0)
- {
- 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
- }
-
- return r;
-
-# endif
-}
-# endif
-