The segment of an executable file

After a C program is compiled and linked, it generates an executable file with three segments:

  • Text: Program instructions and string constants
  • Data segment(data) : initialized global sumstaticvariable
  • BSS(BSS) : uninitialized global sumstaticvariable

Note that the above only describes the executable as having three segments, not as consisting of them.

Under Linux, we can output the segment information of the executable with the size command.

# memory layout

Linker: a program that copies portions of an executable file into memory

When the executable file is loaded, the linker performs a series of operations to complete the address mapping.

Address mapping

The text segment

Store program instructions and string constants

As we know, the text segment of the executable file contains the instructions of the program, and the linker copies the instructions directly from the executable file into memory to form the text segment.

Data segment

Store initialized global and static variables

The data segment of an executable contains initialized global and static variables and their values, forming the data segment.

BSS

Store uninitialized global and static variables

The size of the BSS segment is obtained from the executable file, and the linker gets a block of memory of this size to follow the data segment, which is zeroed out after entering the program’s address space to form the BSS segment.

The entire segment that contains the data segment and the BSS segment is often referred to as the data segment after loading into memory. This is because in operating system memory management terms a segment is a contiguous piece of virtual address, so adjacent segments are coalesced. In general, the data segment is the largest segment in any process.

The heap

Memory space allocated and freed by the programmer

Heap memory only appears while the program is running, growing toward higher addresses.

The stack

Store automatic variables; Process activity record; Temporary storage

Stack memory occurs only while the program is running, growing to lower addresses.

The resources

  • C Expert programming – chapter 6
  • MEMORY MAP IN C
  • C program memory layout
  • Segments in an executable program (C expert programming)