Search code examples
carrayslinkersizeofgreenhills

How to declare a C array that takes up all free space in a memory section?


Assume I have a 128KB memory region. In my linker directives I split this region into three sections:

  • .section_text
  • .section_data
  • .section_bss

The size of each section is unknown pre-compilation, but I have constrained .section_bss to use all remaining space within the memory region after .section_text and .section_data are allocated.

Is there any way I can declare a C array that uses up all available space in .region_bss? Assume it is the only thing using .region_bss so it can safely use the entire region. For example purposes but obviously wrong:

char entire_bss[sizeof(.region_bss)];

Here are my pre-answers to some anticipated responses. First, I know sizeof() doesn't work like this. I'm just using it to get an idea across. Second, assume this must be done with an array and not with pointers (solving with pointers is possible and fairly simple). Third, I'm aware I can get the start and end addresses of .region_bss, but I'm not aware of any way to use them to size my array. At least not any way that works in C.

There very well may be no way to do this, but I'm hoping some genius out there has figured it out. Extra credit if you can make it work with the Green Hills toolset.


Solution

  • Is there any way I can declare a C array that uses up all available space in .region_bss?

    Short answer is "no."

    If GreenHills is using GNU toolchain with binutils and allows customizing the linker script, then you can add into an application namespace a variable using PROVIDE to mark the end of the 128K block. (See an example how to access such variables here). You will not have a C array of fixed size that way, but program would be able to find the end of the array thus the size of the array what is generally sufficient for C programs.

    If you want to accomplish that using pure C99, then you might be out of luck as it is highly unreliable as it is not covered by the standard.