views:

592

answers:

6

I heard that in c, if I do: char *s = "hello world". The "hello world" is actually stored in read-only memory.

I am not so clear about read-only memory. Can anybody explain? Is that like a flag to compiler that tells compiler that do not write into that section?

+3  A: 

True read-only memory is implemented by the memory subsystem of the OS. The OS can mark certain pages as read-only.

In the binary, the compiler can tell the OS which parts of the executable should be placed in read-only vs read-write memory pages.

R Samuel Klatchko
hmmm... true memory protection is implemented at the processor level.
jldupont
@jldupont: The memory protection is indeed implenmented at the hardware level (in x86 at least), but the initial setup is done by the OS, i.e. it is OS that *marks* read-only pages as such, and then the hardware enforces the read-only marks set up by the OS.
AndreyT
@AndreyT: of course... my point was with regards to @R Samuel. Without **hardware** assistance, there is such much one can do at the software level.
jldupont
@R - actually, on embedded systems read-only memory might actually be implemented using ROM hardware; e.g. using EPROM chips.
Stephen C
+3  A: 

Executables contain two parts: a .data section, containing global variables, and a .text section, containing the actual machine code.

Strings are placed into the .data section. What C does when it sees "Hello world" is it puts the string "Hello world" into the executable itself, and replaces instance of "Hello world" in the program with the address where that string ends up being loaded.

Having said that, I'm not sure why it's read-only - theoretically a program should be able to modify its own memory..

Claudiu
Not all processors and operating systems support self-modifying code. In fact, most modern operating systems contain protection against self-modifying code, as a security feature.
Crashworks
String literals, because they don't have to be modifiable, can and often are stored in the text section.
caf
+11  A: 

That's not a feature of the c-language but a feature of the compiler/linker and the operation system working together.

When you compile such a code the following happens:

  • The compiler will put the string into a read-only data-section.

  • The linker collects all the data in such read-only sections and puts them into a single segment. This segment resides in the executable file and is flagged with a "read only"-attribute.

  • Now comes the operation system executable loader. It loads the executable (or maps it into memory to be more exact). Once this is done the loader walks the sections and sets access-permissions for each segment. For a read-only data segment it will most likely disable code-execute and write access. Code (e.g. your functions) gets execute rights but no write access. Ordinary data like static variables gets read and write access and so on...

That's now modern operation systems do it.

As said it's not a feature of the c-language. If you compile the same problem for DOS for example the program will run but no write protection would be possible because the DOS-loader does not know about read-only sections.

Nils Pipenbrinck
Does constant variable also put same section as "hello world"? ( E.g: const int a = 6 )
tsubasa
"It depends". The only thing you can say for sure about a const int is that the compiler will produce a diagnostic message if you attempt to modify it. It might be stored in a read-only section, or it might never be stored at all, and directly encoded as a constant into the instructions that use it.
Mark Bessey
@tsubasa - probably not, and certainly not if `a` is a local. But whatever the answer, it will also depend on the OS and the loader.
Stephen C
Thank you for the answer. @Stephen C, is the loader part of the OS?
tsubasa
tsubasa, yes, the loader is generally a part of the OS.
Amigable Clark Kant
Also, remember, the constant might even and up in a ROM. (In the case of embedded program.)
Amigable Clark Kant
A: 

You could try something like

s[4] = '0';

and see if it says "hello w0rld" when you call

puts(s);

If it causes an immediate Segmentation Fault or a Data Execution Prevention exception then it is probably read only. (If the system lets you get away with it, that doesn't make it a good idea.)

Jared Updike
+1  A: 

As other folks have mentioned, whether the contents of constant strings are stored in read-only memory is determined by the operating system, compiler, and chip architecture.

More precisely, the C standard specifies that the quoted strings are considered to have "const char[]" type (or words to that effect, I don't have the standard at hand).

Any code that attempts to modify the contents of such a string is invoking undefined behavior. That means that literally anything can happen at that point, and the provider of the compiler isn't even required to document what can happen.

In practice, this means that a C or C++ program that wants to be portable has to avoid modifying constant strings.

In general, the compiler will not allow you to modify the contents of of "const" variables, so you can consider "const" to mean "read only" in most cases. Unfortunately, there's a special exception for char * and const char *, largely for historical reasons. That means that code like this:

char *x = "Hello, World";
*x = 'h';

will compile without error or warning, even though it invokes undefined behavior.

Mark Bessey