Search code examples
linuxassemblymemory-address

Are `.text` and `.data` really the same?


In the documentation for the Linux syscall ptrace, I saw the following text:

--snip--

Linux does not have separate text and data address space, so

--snip--

To what extent is this really the case? Are .text and .data really stored as one? Does that mean that I can go about defining things like I would in data in text?

Thanks.


Solution

  • That's not what they're saying. They're in separate regions of one flat virtual address-space.

    On a Harvard machine, address 0x1000 as a data address would access different bytes than address 0x1000 as a code address, because they're addresses in different address spaces. Having multiple address-spaces is like street addresses, where 123 Church St. is a different house (memory cell) than 123 Turing St.

    But Linux's memory model doesn't work that way; all page addresses unique integers because there's only one virtual memory address-space per process.

    So you can draw a memory map where the .text and .data sections are different parts of the same space. They get mapped with different permissions (read+exec vs. read+write), assuming you didn't use any special linker options, but an unsigned char * can read bytes from either of them.