0

According to this article, a c program has these sections:

  1. Text segment (i.e. instructions)
  2. Initialized data segment
  3. Uninitialized data segment (bss)
  4. Heap
  5. Stack

The "Text segment" (or "instruction segment") stores the executable instructions of the program.

Is it possible to write a c program that access this memory region and that makes changes on it, changing the instructions that the program will execute?

Zaratruta
  • 2,097
  • 2
  • 20
  • 26
  • You need to ask yourself what the operating system's [program loader](https://en.wikipedia.org/wiki/Loader_(computing)) does and whether the [text segment](https://en.wikipedia.org/wiki/Code_segment) of your binary executable is allocated to system read-only memory. The answer is often "Yes". If so, it cannot modified. This is enforced by the system's memory management hardware. – paulsm4 Oct 31 '21 at 04:08
  • @paulsm4: “Read-only memory” is a term commonly used to mean memory hardware that physically cannot be modified in a supported way, also called ROM. Memory that the system has marked as not writeable by a process is not this type of read-only memory. – Eric Postpischil Oct 31 '21 at 07:51
  • 1
    It may be possible to write a C program that modifies its own text segment on some platforms. This depends on the operating system. For example, on Unix systems, the `mprotect` call can be used to change the text segment, or portions of it, from non-writeable to writeable, and then it can be modified. Other steps may be necessary too, such as invalidating instruction cache after a modification. I am sure this question has been asked repeatedly on Stack Overflow but am not finding a good duplicate at the moment. I suggest searching for “mprotect”. – Eric Postpischil Oct 31 '21 at 07:56
  • However, I suspect systems have been getting more restrictive about this, for safety. [They may limit what pages may be marked as writeable with `mprotect`.](https://stackoverflow.com/questions/60654834/using-mprotect-to-make-text-segment-writable-on-macos) – Eric Postpischil Oct 31 '21 at 08:00
  • 2
    @Eric Postpischil: It's perfectly legitimate to use "Read only memory" as a generic term. Per the Oxford Dictionary: "memory read at high speed but not capable of being changed by program instructions". A ROM chip is merely one (of many, many different!) examples of "Read only Memory". The point I was making is that in a virtual memory-based system, the OS program loader usually assigns a program's "text" segment to a "read only memory segment". Where it CANNOT be modified. – paulsm4 Oct 31 '21 at 17:44
  • 1
    @paulsm4: The memory used for text segments in general-purpose multiuser programs is capable of being changed by program instructions. (In hardware documentation, “program instructions” does not mean just the instructions of **your** program particularly, but any of the instructions the processor executes. The program instructions of the kernel can change this memory.) The processor prohibits processes from changing the memory, but the memory itself is capable of being changed. It is not ROM. ROM is a type of hardware, not a software-protected region. – Eric Postpischil Oct 31 '21 at 17:54

0 Answers0