I want to know what is the difference between the data section and text section in terms of the entire code in a specific section.
I was trying to run the code I have added here. In the data sectoin it runs and prints "af)a" If I change it to the text section in line 2, it seg fault.
I can't figure out what are the crucial differences. Thanks :)
global _start
section .data
_start: mov ecx, 3
xor byte [_start + 1], 0x02
pushad
mov eax, 4
mov ebx, 1
mov edx, ecx
mov ecx, dword blah
int 0x80
blah: popad
sub bx, ax
loop _start
mov eax, 1
mov ebx, 2
int 0x80
fin:
expected code to yield the same result, but was proven otherwise.
difference between
.textand.data
I don't know about the MASM assembler but the GNU assembler makes two differences:
.text in one case, .data in the other case). However, this name is ignored by the operating system.SHF_ALLOC | SHF_EXECINSTR for .text andSHF_ALLOC | SHF_WRITE for .dataThe "section flags" tell the operating system what kind of data is present in the section and what kind of operations are allowed:
SHF_EXECINSTR means that the section contains code which can be executed. If this flag misses, the program will crash in most OSs if the section contains code. Older 32-bit CPUs did not support this, so this flag was ignored by many OSs when running 32-bit programs. Obviously, your OS ignores this flag; otherwise the .data variant of your program would crash because the code is located in the .data section.
SHF_WRITE means that data in the section can be overwritten. If this flag is missing and you are trying to do a write operation to the data in the section, the program will crash.
Of course it would be possible to manipulate the executable file in a way that the section .text has the SHF_WRITE flag set. (And some assemblers allow setting this flag for the .text section directly.) In this case your program would not crash if you write to the .text section.
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