I am learning assembly and low-level programming itself and reading a book about it. It is said there that we can put any data inside the .text section of an elf file but of course we can't mutate it because of different permissions of pages/segments. But it was not told there, what was the reason for it, for having data inside .text section. I was also told by many C++ programmers that g++ compiler puts
static const char DATA[] = "SOME DATA";
inside the .text section too. I wonder, why not to put this data inside .rodata section, what is the purpose? And if .text is used, what to store in the .rodata then?
The main question is about this behaviour in long mode.
Also, the text segment is often read-only, to prevent a program from accidentally modifying its instructions.
In software, read-only is a safety measure which protects files and data from accidental or intentional alteration or deletion and may be imposed only for select users or groups of users.
The code segment in memory is typically read-only and has a fixed size, so on embedded systems it can usually be placed in read-only memory (ROM), without the need for loading. If the code segment is not read-only, then the particular architecture allows self-modifying code.
The (somewhat oddly named) text section (segment) usually contains the compiled binary code, as you suspect. An executable often contains other types of data besides the code, like initialized variable values, resources, debug information, relocation data, and so on, which are placed in sections with other names.
Traditionally, read-only data was placed in the text section for two reasons:
On ELF targets, this scheme was modified a bit. Read-only data is now placed in the new .rodata
section which is like the .text
section except it also cannot be executed, preventing certain attack vectors. The advantages remain.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With