For example when I have:
const char mesg [] = "Hello World";
it is directly put in the .rodata but when I have:
const char* mesg = "Hello World";
it is put directly in .rodata.str1.4
What is the difference between them and why we use .rodata.str1.4 when we use the pointer ?
I did a couple of experiments, it looks like the compiler places strings in special sections in object files. The interesting thing happens when the binary is compiled, the strings end up in .rodata as expected. Further experiments show that what happens is that if you have the same string in different objects they get unified into the same string in the resulting binary.
So I’d suspect that the reason for this is that the compiler wants to give the linker some information about the read only data other than “it’s read-only”, so that the final link can make more intelligent decisions about how to handle it, including deduplication.