Reputation: 75
From Wikipedia:
The size of this segment is determined by the values placed there by the programmer before the program was compiled or assembled, and does not change at run-time
So who actually determines the size of the segment? The OS? Additionally, when is it computed? Does "before the program was compiled or assembled" imply that it was done during the preprocessing stage?
Upvotes: 2
Views: 820
Reputation: 222933
The Wikipedia text you cite is wrong or misleading. No data segment exists before assembly or compilation.
The data segment is built in the following way.
In assembly, a programmer may explicitly declare a data section and define values to be placed there. Some assemblers also provide features that automatically add data to a data section when certain features are used by a programmer. E.g., the assembly line ldr r7, =345
might ask the assembler to put 345 in the data section and fill in its address in the ldr
instruction.
In high-level languages, the compiler builds data it needs as it processes the code. This can include constants directly written by the programmer (e.g., the 345
in x = 345;
or values created in the process of transforming the high-level code into assembly. (E.g., if you wrote i = 35; y = x[i*3+7];
, the compiler might calculate the subscript as 112 and store that in the data section).
The linker combines the data sections from the object modules it processes into one data segment. The size is simply the size of the combined data. (Some data sections, such as FORTRAN common segments, may be shared, so that data from multiple object modules is overlapped instead of concatenated.)
When libraries are loaded dynamically, their data segments cannot be combined at link time, and they must be managed separately by the loader. So an executing program may have multiple data segments.
Upvotes: 1
Reputation: 9377
For traditionally compiled languages the answer is, roughly speaking, the linker.
What happens is that each object file contains information about the size (and alignment, etc) of the various segments. When the linker stitches object files together to form an executable, it will aggregate matching segments in a way that respects alignment. The order of the parts, and thus potentially the size of the final segment is thus up to the linker.
The size of a segment may change if padding needs to be added to preserve alignment.
Note that programs that are interpreted or JIT-compiled don't really have segments.
Upvotes: 2
Reputation: 28839
The "before the program was compiled.." part must refer to what variables and/or constants should exist.
The size of the data section/segment is calculated by the linker - the part of the compilation/assembly process that is responsible for actually building the output file, including its header where this type of information is recorded.
The size calculation is in turn based on what the data sections of the participating object modules contain.
Upvotes: 1