0

I want to compile a C program and specify at compile time the stack size (below is a sample code).

#include <stdio.h>
#include <sys/resource.h>
static struct rlimit Rl;
int main() {
getrlimit(RLIMIT_STACK,&Rl);
printf("%lu %lu\n",Rl.rlim_cur,Rl.rlim_max);
return 0;
}

I compiled it with the following command

gcc Source.c -Wl,-z,stack-size=value -o program

I tried a lot of values but the program displays the same information: 8388608 4294967295

If I use the ulimit command (ulimit -s 4096) the program displays: 4194304 4194304 (4096 Kb)

But then I must close and reopen the window, otherwise other commands will not work.

Is there any other solution to set the stack size before launching the program ?

I currently use gcc (Ubuntu 7.5.0-3ubuntu1~16.04) 7.5.0

  • Did you investigate https://stackoverflow.com/questions/2279052/increase-stack-size-in-linux-with-setrlimit – Tymenko Maxsim Jun 10 '23 at 23:01
  • I said I want to set the stack size before launching the program. I know how to use setrlimit (as you can see, I use getrlimit). If I use setrlimit (to **reduce** the stack size), and my program calls execv... or execl... the new process gets the new limits. If I want to reduce the stack size and then call exec... the size must be at least about 12 Kb or more, otherwise the process crashes (segmentation fault). **I want to know why stack size is ignored by linker**. – user2538003 Jun 11 '23 at 11:17
  • 1
    Are you confusing actual mapped stack size with the resource limits? You haven't demonstrated what the mapped stack size for your executable actually is, so how do you know your `-Wl,-z,stack-size=...` argument is being ignored? – Andrew Henle Jun 11 '23 at 13:39
  • (From ld manual, stack-size=value) _Specify a stack size for in an ELF "PT_GNU_STACK" segment. Specifying zero will override any default non-zero sized "PT_GNU_STACK" segment creation._ So, if I compile with -Wl,-z,stack-size=4096 then what is the purpose of this option ? Isn't it related with getrlimit ? – user2538003 Jun 12 '23 at 10:12

0 Answers0