Why gcc place a large constant array to ".rwdata" section rather than ".rodata" section?

/* 0xFFFFFFFF * 256*/

After compiling, the const array is placed in ".rwdata" section rather than ".rodata" section, and I don't know why.

After google and searching the stackoverflow site, there is no specified answer. Does any one know why or how to ask compiler(or linker) to output warning message when "placing constant data into non-read-only section"? thanks.

PS. I resolve my problem by add attribure((section(".rodata")))

__attribure__((section(".rodata"))) volatile const int TEST_ro[512] = {test_256X, test_256X};

PS. I use linaor-gcc compiler for arm core

asked on Stack Overflow Nov 7, 2014 by carl • edited Nov 7, 2014 by artless noise

1 Answer


Answer: If I want a variable to be "volatile" and place in read-only section, use

__attribute__((section(".rodata"))) /* default read-only data section*/

is the best way.


  1. That's not what volatile is for. It indicates to the compiler that the memory may be changed asynchronously so it doesn't used cached results. It's inconsistent with const. Try static which tells the compiler that other modules cannot access it. – luser droog

  2. If want to place variable to specific section, then using the explicit section placement is the way to go. C standard gives very limited set of tools for this, so you are at the mercy of what compiler provides. attribute is not pretty, but it's most likely what you need to do.

answered on Stack Overflow Nov 7, 2014 by carl • edited May 21, 2020 by vulcan raven

User contributions licensed under CC BY-SA 3.0