Re: executable segments -data,text.stack

From: Arto Viitanen (av_at_cs.uta.fi)
Date: 12/27/04


Date: Mon, 27 Dec 2004 13:44:36 +0200


>>>>> "sachin" == sachin mzn <sachin_mzn@yahoo.com> writes:

 sachin> Hi, I am little confused about the different segments in an
 sachin> executable.

 sachin> I know that there are primarily 3 segment, text, data and stack.

 sachin> I know all local varibales goes into stack.

 sachin> But I am confused that what goes into text and data segment.

 sachin> Many say machine instruction goes into text. Can some one clarify me
 sachin> what is it?

 sachin> do global functions go into text segment? If it is true then what
 sachin> about local variables declared in global function, will they go into
 sachin> text ot stack segment?

 sachin> Regards, Sachin

Modern von Neumann computers use memory for both running program and data it
uses. Virtual memory is a way to make a) use more programs than fit together
on to the computer's memory and b) use programs that use more memory than
what computer's memory is. There are two main ways to implement virtual
memory: segmented memory and paged memory. In segmented memory, program is
divided to different segments, that can be load to memory when needed. In
paged memory, the memory is divided to small portions called page, that can
be loaded one at the time (and in different parts of the memory). Actually,
modern Unix systems use paged segmented memory.

Program file is an image of the program to be run on the memory. It is
divided to different segments. Text segment contains the program code to be
run. If program uses dynamic libraries, when the program is loaded to be run,
libraries it refers are also loaded. The data program will be using is
divided into two segments: data and stack. Data contains global variables
(and their default variables), constants etc. Also dynamically allocated
memory (links etc.) is on the data segment. Stack is a segment that is
actually only in running program (that is on program file there is no stack)
and it will contain local variables that are not allocated to registers,
function parameters that are not allocated to registers etc.

Reason for three segments is, that first in the memory space is text, since
it size is known. Then there is data, which is growing upwards. Stack is on
the other size of the memory space (2^word size) and it is growing downwards.

Normally text segment is marked readonly, since there is no need to change
running program. Because of this, text segment might be shared between
several instances of the running program (they are called processes).

-- 
Arto V. Viitanen				                  av@cs.uta.fi
University of Tampere, Department of Computer Sciences
Tampere, Finland				      http://www.cs.uta.fi/~av/


Relevant Pages

  • Re: IAR compiler & MSP430 problem
    ... the linker isn't telling you when you have run our of RAM. ... which produced a prediction of stack use ... chips have different memory resources. ... // segment address range usage ...
    (comp.arch.embedded)
  • Re: Questions about Minix
    ... Of course you still need to allocate chunks of contiguous memory, ... in user mode and at kernel level if you need dynamic ... I read "data segment". ... a stack from growing into the heap (only a possibility of detecting it ...
    (comp.os.minix)
  • Re: Questions about Minix
    ... I think I got to that conclusion that .text shared the same memory chunk ... segment to processes from the beginning, and a data segment that did not ... True, but this would limit your stack size to some predetermined value, ... you still have plenty of heap left. ...
    (comp.os.minix)
  • Re: Questions about Minix
    ... allocating each component the memory it needs can be a real pain). ... This real makes stuff very "static", because the stack ... necessary) the segment when the application demands more heap than was ... address in the data segment, because the data segment contains only data ...
    (comp.os.minix)
  • Re: execve() and heap memory
    ... bss, and stack of the calling process are overwritten by ... This is typically placed in memory above the 'text' (or at ... literals) in the text segment. ... Stack instructions in the 'text', ...
    (comp.unix.programmer)