xref: /netbsd-src/external/gpl3/binutils.old/dist/libiberty/stack-limit.c (revision e992f068c547fd6e84b3f104dc2340adcc955732)
116dce513Schristos /* Increase stack size limit if possible.
2*e992f068Schristos    Copyright (C) 2011-2022 Free Software Foundation, Inc.
316dce513Schristos 
416dce513Schristos This file is part of the libiberty library.  This library is free
516dce513Schristos software; you can redistribute it and/or modify it under the
616dce513Schristos terms of the GNU General Public License as published by the
716dce513Schristos Free Software Foundation; either version 2, or (at your option)
816dce513Schristos any later version.
916dce513Schristos 
1016dce513Schristos This library is distributed in the hope that it will be useful,
1116dce513Schristos but WITHOUT ANY WARRANTY; without even the implied warranty of
1216dce513Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1316dce513Schristos GNU General Public License for more details.
1416dce513Schristos 
1516dce513Schristos You should have received a copy of the GNU General Public License
1616dce513Schristos along with GNU CC; see the file COPYING.  If not, write to
1716dce513Schristos the Free Software Foundation, 51 Franklin Street - Fifth Floor,
1816dce513Schristos Boston, MA 02110-1301, USA.
1916dce513Schristos 
2016dce513Schristos As a special exception, if you link this library with files
2116dce513Schristos compiled with a GNU compiler to produce an executable, this does not cause
2216dce513Schristos the resulting executable to be covered by the GNU General Public License.
2316dce513Schristos This exception does not however invalidate any other reasons why
2416dce513Schristos the executable file might be covered by the GNU General Public License. */
2516dce513Schristos 
2616dce513Schristos /*
2716dce513Schristos 
2816dce513Schristos @deftypefn Extension void stack_limit_increase (unsigned long @var{pref})
2916dce513Schristos 
3016dce513Schristos Attempt to increase stack size limit to @var{pref} bytes if possible.
3116dce513Schristos 
3216dce513Schristos @end deftypefn
3316dce513Schristos 
3416dce513Schristos */
3516dce513Schristos 
3616dce513Schristos #include "config.h"
3716dce513Schristos #include "ansidecl.h"
3816dce513Schristos 
3916dce513Schristos #ifdef HAVE_STDINT_H
4016dce513Schristos #include <stdint.h>
4116dce513Schristos #endif
4216dce513Schristos #ifdef HAVE_SYS_RESOURCE_H
4316dce513Schristos #include <sys/resource.h>
4416dce513Schristos #endif
4516dce513Schristos 
4616dce513Schristos void
stack_limit_increase(unsigned long pref ATTRIBUTE_UNUSED)4716dce513Schristos stack_limit_increase (unsigned long pref ATTRIBUTE_UNUSED)
4816dce513Schristos {
4916dce513Schristos #if defined(HAVE_SETRLIMIT) && defined(HAVE_GETRLIMIT) \
5016dce513Schristos     && defined(RLIMIT_STACK) && defined(RLIM_INFINITY)
5116dce513Schristos   struct rlimit rlim;
5216dce513Schristos   if (getrlimit (RLIMIT_STACK, &rlim) == 0
5316dce513Schristos       && rlim.rlim_cur != RLIM_INFINITY
5416dce513Schristos       && rlim.rlim_cur < pref
5516dce513Schristos       && (rlim.rlim_max == RLIM_INFINITY || rlim.rlim_cur < rlim.rlim_max))
5616dce513Schristos     {
5716dce513Schristos       rlim.rlim_cur = pref;
5816dce513Schristos       if (rlim.rlim_max != RLIM_INFINITY && rlim.rlim_cur > rlim.rlim_max)
5916dce513Schristos 	rlim.rlim_cur = rlim.rlim_max;
6016dce513Schristos       setrlimit (RLIMIT_STACK, &rlim);
6116dce513Schristos     }
6216dce513Schristos #endif
6316dce513Schristos }
64