2
/* 0xFFFFFFFF * 256*/
#define test_256X0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, ... 0xFFFFFFFF

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 anyone know why or how to ask the compiler (or linker) to output a warning message when "placing constant data into non-read-only section"? thanks.

PS. I resolve my problem by adding __attribute__((section(".rodata")))

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

PS. I use linaro-gcc compiler for ARM core

AJM
  • 1,317
  • 2
  • 15
  • 30
carl
  • 305
  • 3
  • 13
  • 4
    Why do you declare it volatile? What happens if you don't? – rici Nov 07 '14 at 08:20
  • 1
    What you have there declares an array of `volatile const int`. Why would a `const` need to be `volatile`? – T.J. Crowder Nov 07 '14 at 08:21
  • I want to reserve a special space in read-only section and do not want the array be removed by compiler optimization. I use "int test= TEST_ro[x]" in a function. But the compiler still removing the array and assign 0xFFFFFFFF to test. e.g. "int test= 0xFFFFFFFF". Therefore, I have to add volatile to avoid optimization. – carl Nov 07 '14 at 08:29
  • 4
    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 Nov 07 '14 at 08:35
  • 5
    @luserdroog `const` is not necessarily inconsistent with `volatile`. `const` doesn't mean variable cannot change, it means it cannot be *changed*. – user694733 Nov 07 '14 at 08:52
  • @carl If you solved your issue, you might want to add that as an answer and select it, so that this question is not left open. – user694733 Nov 07 '14 at 08:58
  • 3
    Why do you need to place the array in `.rodata` section? – Nikolai Nov 07 '14 at 09:26
  • @luser droog sorry, remove volatile is workable, but it still fail with "static volatile const". – carl Nov 07 '14 at 10:43
  • @user694733 add __attribure__((section(".rodata"))) is wrokable but not an intuitive solution. BTW, how to assign a comment as a solution – carl Nov 07 '14 at 10:46
  • @carl: You're supposed to write an _answer_ and accept it (yes, it's perfectly fine to accept your own answer to your question). – DarkDust Nov 07 '14 at 10:48
  • 1
    @carl 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. – user694733 Nov 07 '14 at 11:10

1 Answers1

2

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

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

is the best way.

reference:

  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.

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

AJM
  • 1,317
  • 2
  • 15
  • 30
carl
  • 305
  • 3
  • 13