Search code examples
cassemblystack-overflowram

Get address of ram sections during runtime


I want to implement a stack usage monitor for my NRF52840-Mikrocontroller with Segger Embedded Studio.

To monitor the maximum stack usage, I need some information during runtime like the end address of the .bss segment, which is the start of my free memory.

My approach is, to fill the ram from the .tbss section until to the stackpointer with a magic word. During runtime, the stack will grow and will overwrite my magic words with data. In a cyclic check, I am able to dedicate the end of the stack. From that information, I can derive the approximate stack usage.

Is it possible to get the addresses from the picture below during runtime in my c (or ASM) Program?

Here is a part of my .map file, where for example the symbol __bss_start is defined. Is it possible to access this symbol from c code?

 *(COMMON)
            0x0000000020020ec4                __bss_end__ = (__bss_start__ + SIZEOF (.bss))
            0x000000000001b8c8                __bss_size__ = SIZEOF (.bss)
            0x0000000020020ec4                __bss_load_end__ = __bss_end__
            0x0000000000000001                . = ASSERT (((__bss_start__ == __bss_end__) || ((__bss_end__ - __RAM_segment_start__) <= __RAM_segment_size__)), error: .bss is too large to fit in RAM memory segment)
            0x0000000020020ec4                __tbss_load_start__ = ALIGN (__bss_end__, 0x4)

Ram Sections of the NRF52840


Solution

  • Thanks for your help, I have solved the Problem now.

    To access the symbols of the .map file during runtime, I used the following code:

    extern char __bss_end__;
    
    int main()
    {
        char * bss = &__bss_end__;
    }
    

    After this line of code, the bss variable contains the start address of the bss-section. With this code, I am able to get the addresses of the RAM segments during runtime to monitor my stack usage.