I would like to access the address of the end of the bss section in my code and do operations with it at compile time. I am compile with avr-gcc for an ATmega328p. (Of course :))
The purpose is to partition remaining RAM (on an Arduino) into even segments. I have the end of RAM, just not the start of what isn't taken up by globals. The program assumes no dynamic memory allocation.
I know the location isn't available at preprocessor time or compile time. But, it should be available before the code is uploaded.
Right now I have
extern uint16_t __bss_end;
and then later
Serial.println((uint16_t) &__bss_end);
Which works. It prints 0x0F00 (RAMSTART) + bytes used. However, that would force me to calculate the partition locations at runtime, which then requires me to store the partition locations into variables, wasting precious RAM.
So I guess my point is: How can I calculate these constants (the partition locations) at compile time using __bss_end? (Or, really, any equivalent).
This is just a fun project, not for a practical sake.
__bss_end is calculated by the linker as one of the the final stages of creating the executable, when it fits all the object code together.
Since the linker calculation happens after all the compilation steps, it is obviously not available at compile time directly, however, one option would be to take the value of __bss_end from one build and apply the value you got to another as a compile time constant and then assert at start up that runtime __bss_end in your new version is equal to the compile time version you calculated.