Data segment or Text segment

Hi,

Whether the following piece of code is placed in the read-only memory of code (text) segment or data segment?

char *a = "Hello";

I am getting two different answers while searching in google :frowning: that's why the confusion is

Most likely the text segment but some compilers can be forced to put it in the data segment using comand line switches...and if you really care print out its location using a debugger to find out whether it lies in the text or data segment...difference is that the text segment literal cant be modified but the data segment one can be.

Guess it's compiler/architecture dependent. Look at the produced assembly file.

PS> Why do you are about where it is stored?

Cheers, Lo�c

It is in text segment ... If you want you can check it... like this..
Try to change the value of that variable..

Hi royalibrahim,

The declaration 'char *a = "hello"' is actually allocated in the .rodata (read-only data) and not in the .text section (in an ELF binary). I wrote a small code to verify
this.

char *var = "A"; /* small value makes verification easier */

I compiled it and dumped the .rodata section of the ELF using objdump.

[gaurav@adserver1 ~]$ objdump -s -j .rodata temp.o

temp.o: file format elf32-i386

Contents of section .rodata:
0000 4100 A.

So you see that the value "A" with ascii value '41' is present in there.

Thanks and Regards,
Gaurav.

Sorry, that's wrong -- or at least not strictly correct. In my system, it ends up in the .rodata section, which is read-only despite not being the text section.

The text section doesn't actually mean text as in words -- it means program text, i.e. machine language. It's read-only since it usually maps directly into object files. It's not inconceivable for variables to end up there, but still a funny place to put them.

The data segment would make more sense, being where global variables go, but has the drawback of not being read-only.

So, for systems that don't have any sort of read-only data section available, I guess the text section might be as good a place as any. And if you can write to it, it's probably inside the data segment...

The rodata section is a specific section of the text segment set aside for storing constants and literals for the purpose of efficiency as they can be embedded in the instructions passed to the CPU and so no overhead is incurred to fetch them from memory. Another clue to the location of the literal is the fact that out of the 3 segments text data and stack only the text segment is readonly while data and stack are read and write and as i said before if still in doubt print out the value of a and the start of the data segment in gdb to see where the string literal actually lies.
gdb

Sorry, no. This contradicts itself on many levels. You don't embed something by separating it into its own, separate, independent segment -- that's the exact opposite of "embed". That it's not in the text segment indicates that it's not in the text segment.

It's definitely not a part of CPU instructions here either. There are CPU instructions that take a literal value, usually an integer or pointer of some sort, this isn't one of them. I'd believe that instructions can have embedded pointers to fixed strings, or might embed some data from the read-only section when it's of convenient sizes to fit in instruction literals, but the strings themselves are not embedded in the instructions, and therefore don't need to be embedded in the text segment.

This logic only works if you assume that only text segments are read-only. This is not true.

This isn't to say there aren't architectures where strings might be embedded in the text segment for some reason, but this isn't one of them.