When a native program is built, the linker generates a binary file called an executable.
Executables have a specific format that is known by the target operating system.
The executable data is known as a binary image.
When a program is started, the operating system copies the image in system memory and allocates additional memory for the program.
An image is divided into sections (or segments).
The layout of the sections is platform-dependent.
The most common formats are defined in Windows and Unix.
The text segment (or code segment) contains the machine code for the program.
The data segment contains the initialized global and static variables.
The layout of the data in the image is identical to the layout of the data in system memory.
BSS stands for Block Started by Symbol.
The BSS segment contains the uninitialized global and static variables.
In C and C++, uninitialized variables are set to 0.
In the image, the BSS segment just contains the size of the data.
When the operating systems loads an image, it allocates a block of memory filled with 0 that is large enough for all the uninitialized variables.
Read-Only Data Segment
The read-only data segment ( or rodata segment) contains the data for the constant variables.
The Windows operating system uses the Portable Executable (PE) format.
Executable files have the .exe extension.
- Windows (x86 and ARM) (.exe/.dll)
- Xbox (.xbe)
- Xbox 360 (.xex)
- Xbox One (.xvd/.xvc)
The Unix operating system uses the Executable and Linking Format (ELF).
Executable files have the .elf extension.
- Unix and Linux (.elf)
- macOS and iOS (Mach-O)
- PlayStation 2
- PlayStation 3
- PlayStation 4
- Wii, Wii U, and Switch (.rpx/.rpl)