I read that it depends on the compiler and operating system architecture. How do I find out the data segment and stack max size on a Linux system using GCC as compiler?
Let me experiment with you: create file ``test.c'' like this:
int main (void) { return 0; }
Now compile it, specifying max stack size (just to easy lookup this number in map file and determine symbol name, refering to it):
gcc test.c -o test.x -Wl,--stack=0x20000 -Wl,-Map=output.map
Determining data size is simple:
size -A -d test.x
You will get something like this:
section size addr
.text 1880 4299165696
.data 104 4299169792
...
Also ``objdump -h test.x'' will work fine but with less verbose results.
There is more sections here (not just code and data) but there is no stack information here. Why? Because stack size is not ELF section, it is reserved only after your program is loaded to be executed. You should read it from some (platform dependent) symbol in your file like this:
$ nm test.x | grep __size_of_stack_reserve__
0000000000020000 A __size_of_stack_reserve__
It is not surprising, that size is 0x20000, as it was stated when compiling.
I determined symbol name by looking into output.map file that was generated during compilation. I recommend you also to start from looking at it.
Next when you do have some unknown file a.out, just repeat sequence:
size -A -d a.out
nm a.out | grep __size_of_stack_reserve__
Substituting a platform dependent symbol to that, you determined in experiment, described above.
Segments are a method for organizing stuff that your executable needs.
The data segment is usually for any data that your executable uses (without inputting from external sources). Some data segments may contain string literals or numeric constants.
Many executables use a stack for storing function local variables, statement block local variables, return addresses and function parameters. A stack is not required by the C or C++ languages; it's just a handy data structure.
The stack size can either be the capacity allocated to the stack or the number of elements residing on the stack or the amount of memory occupied by the stack.
Many platforms have a default size for the stack. Since platforms vary, you will need to read the documentation for your tools to see how to set stack size and what the default capacity is.
How do I find out the data segment and stack max size on a Linux system using GCC as compiler?
These limits can be read as RLIMIT_DATA
and RLIMIT_STACK
resource limits of getrlimit
.
In the command line you can use ulimit
command to find these limit of your system:
$ ulimit -s # stack
8515
$ ulimit -d # data
unlimited
You can change the system limits by modifying limits.conf.
And more in man pthread_create
:
On Linux/x86-32, the default stack size for a new thread is 2 megabytes. Under the NPTL threading implementation, if the RLIMIT_STACK soft resource limit at the time the program started has any value other than "unlimited", then it determines the default stack size of new threads. Using pthread_attr_setstacksize(3), the stack size attribute can be explicitly set in the attr argument used to create a thread, in order to obtain a stack size other than the default.
And in man ld
:
--stack reserve
--stack reserve,commit
Specify the number of bytes of memory to reserve (and optionally commit) to be used as stack for this program. The default is 2MB reserved, 4K committed. [This option is specific to the i386 PE targeted port of the linker]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With