What is c/c++ data segment and stack size?
Asked Answered
S

3

6

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?

Sperrylite answered 12/9, 2015 at 18:3 Comment(3)
I'd start by looking at the GCC docs.Optimist
Tell your linker to print a map file. The map file will list all the segments and their sizes. There is no standard for telling linkers or compilers how to generate the map file. You will need to consult the documentation for your tools.Cheesecake
Data segment maximum size, dunno if there is a maximum size. Wouldn't make much sense if there is. Max stack sizes: already answeredDunsany
P
6

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.

Palp answered 12/9, 2015 at 20:57 Comment(0)
C
4

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.

Cheesecake answered 12/9, 2015 at 18:28 Comment(0)
F
1

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]

Factious answered 12/9, 2015 at 20:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.