Analyze the results of the above "Programming Challenge" to convince yourself that:
- the data segment is kept in the object file
- the BSS segment isn't kept in the object file (except for a note of its runtime size
requirements) - the text segment is the one most affected by optimization
- the a.out file size is affected by compiling for debugging, but the segments are not.
What the OS Does with Your a.out
Now we see why the a.out file is organized into segments. The segments conveniently map into
objects that the runtime linker can load directly! The loader just takes each segment image in the file
and puts it directly into memory. The segments essentially become memory areas of an executing
program, each with a dedicated purpose. This is shown in Figure 6-2.
Figure 6-2. How the Segments of an Executable are Laid Out in Memory
The text segment contains the program instructions. The loader copies that directly from the file into
memory (typically with the mmap() system call), and need never worry about it again, as program
text typically never changes in value nor size. Some operating systems and linkers can even assign
appropriate permissions to the different sections in segments, for example, text can be made read-and-
execute-only, some data can be made read-write-no-execute, other data made read-only, and so on.
The data segment contains the initialized global and static variables, complete with their assigned
values. The size of the BSS segment is then obtained from the executable, and the loader obtains a