A memory section is a contiguous area when a program is loaded as a process. The concept of memory sections is necessary for several reasons.
Many processors, including the i386, has the concept of segments. Not counting historical reasons from the 8086 days, there are still two main segments that are important. A code segment/section is used to store instructions, and a data segment/section is used to store data and the system stack.
This differentiation is needed to implement certain security features. For example, newer Pentium and AMD equivalent processors can inhibit code execution in the data segment/section. This stops buffer overflow and stack overflow exploits completely (as both need to execute exploit code overwritten in data memory).
The as assembler allows a program to define any number of sections. However, two sections are predefined. The section that is for code (instruction) is called the “text” section, whereas the section that is for data is called the “data” section.
To switch to the text section, use the .text assembly language directive on a line. To switch to the data section, use the .data assembly language directive on a line.
Let us take a look at the code in listing 4.1.
Even though the two .data portions are interleaved by a .text portion in the source code, the assembler actually make the two .data portions contiguous in the data section.
Instructions should always be placed in the text section. However, data definitions can go to the data section (for normal variables that change) or the text section (for const variables).