2

I'm having an interesting problem initialising an array within my header.

I have:

static u32 TxBuffer_Data[MAX_PKT_LEN_WORDS] = { 10 };
static u32 RxBuffer_Data[MAX_DMA_RX_FIFOMODE_WORDS] = { 0 };

Now, I want both to be within the .data section of an embedded processor, i.e. allocated at compile time and initialised, ideally to zeros.

Now, the syntax here is correct as per How to initialize all members of an array to the same value?.

When I run my code, I grab the addresses of these two buffers, the Txbuffer is indeed within the .data region, however the RxBuffer is within .bss which is reserved for non-initialised compile time allocated variables. If I change that { 0 } to { 10 } the RxBuffer is put into the .data section correctly.

Why can't I initialise data to zeros and still have it defined as initialised?

Thanks. Ed

Community
  • 1
  • 1
E. Fisher
  • 35
  • 2
  • 2
    What compiler are you using? What optimization flags do you have? Maybe, because you initialize the whole array to zero, and the BSS is also initialized to zero, it made a small "optimization" by putting the array in BSS? – Some programmer dude Mar 31 '16 at 14:33
  • 1
    Better not to put them in a header anyway but in the module itself, with `extern` declarations in the header. – Weather Vane Mar 31 '16 at 14:43

1 Answers1

2

Data in the bss section is zero-initialized. Declaring the array as:

static u32 RxBuffer_Data[MAX_DMA_RX_FIFOMODE_WORDS] = { 0 };

or

static u32 RxBuffer_Data[MAX_DMA_RX_FIFOMODE_WORDS];

is actually the same. In both cases the array is zero-initialized and most likely the compiler will place the array in bss.

  • Specifically the flags are: 'Invoking: MicroBlaze gcc compiler' mb-gcc -Wall -O0 -g3 -c -fmessage-length=0 -I../../XpsMBprjFlitesHub2_bsp_standalone_2/microblaze_0/include -mno-xl-reorder -mlittle-endian -mcpu=v8.50.c -mno-xl-soft-mul -Wl,--no-relax -ffunction-sections -fdata-sections -MMD -MP -MF"src/FLITES_HUB_MB_Main.d" -MT"src/FLITES_HUB_MB_Main.d" -o "src/FLITES_HUB_MB_Main.o" "../src/FLITES_HUB_MB_Main.c" – E. Fisher Mar 31 '16 at 14:53
  • The reason for the confusion is that the Xilinx documentation states that: ".bss This section contains un-initialized data. This section has the w (read-write) flag and must be mapped to RAM." However as you mentioned, the .bss section is zero-initialised therefore initialised. – E. Fisher Mar 31 '16 at 14:55
  • Just for additional info, placing the array in bss instead of data section is an optimization, variables in the data section require the corresponding initialization values in the binary image, whereas variables in the bss don't. In your case, placing RxBuffer_Data in bss instead of data section saves `4 * MAX_DMA_RX_FIFOMODE_WORDS` bytes of flash – Ettore Barattelli Mar 31 '16 at 15:13
  • @E.Fisher The BSS contains data that *you* don't initialize (and apparently initialize to zero), it's the compiler runtime support code that "initializes" the BSS. It's an important distinction. – Some programmer dude Apr 01 '16 at 06:25