Skip to main content
matic
Associate III
October 3, 2016
Question

How to locate stack and heap memory at the predefined region

  • October 3, 2016
  • 6 replies
  • 1513 views
Posted on October 03, 2016 at 14:46

Hi.

I would like to implement MPU for stack overflow detection. I suppose that I should know in advance where the boundaries of stack and heap are.

Is it possible to read these boundaries from C code? Or is it possible to define exact region which will be reserved for stack and heap?

I know I can set stack and heap size in startup file, but there is no definition for top of the stack (or bottom of the heap).

Thank you

#i-accept-paypal
    This topic has been closed for replies.

    6 replies

    Tesla DeLorean
    Guru
    October 3, 2016
    Posted on October 03, 2016 at 14:55

    Define and export symbols for the things you want to see. Doesn't the top of stack get exported as __initial_sp, the base as Stack_Mem ? And the  heap as __heap_base (Heap_Mem), __heap_limit ?

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    matic
    maticAuthor
    Associate III
    October 3, 2016
    Posted on October 03, 2016 at 17:37

    Hmm, I don't really understand what you want to say... Do you have any piece of code to show?

    Tesla DeLorean
    Guru
    October 3, 2016
    Posted on October 03, 2016 at 18:10

    I'd have to write. Please review the linker/symbol stuff provided to you last week. [DEAD LINK /public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/CRC%20calculation%20over%20code%20in%20Flash&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B&TopicsView=https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/AllItems.aspx?Paged%3DTRUE%26p_StickyPost%3D%26p_DiscussionLastUpdated%3D20160930%252013%253a29%253a31%26p_ID%3D70358%26View%3D%257bF47A9ED8%252dE726%252d42BE%252dACED%252d732F13B66581%257d%26FolderCTID%3D0x012001%26PageFirstRow%3D41&currentviews=48]Here

    Review the concepts around assemblers/linkers/loaders in the context of your tool-chain

    http://www.keil.com/support/man/docs/armasm/armasm_dom1361290009343.htm

    http://www.keil.com/support/man/docs/armasm/armasm_dom1361290016692.htm

    et al

    The linker's job is to fix up symbols in the name-space. The C and ASM files share the same name-space, and you can reference them, and have the linker do it's job and associate them automatically. The public symbols are also typically provided in the .MAP and .ELF/AXF object files.

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    Tesla DeLorean
    Guru
    October 3, 2016
    Posted on October 03, 2016 at 18:22

    From startup_stm32fxxx.s, and *EXPORT* the symbols you want to reference elsewhere

    ...
    Stack_Size EQU 0x00000400
    AREA STACK, NOINIT, READWRITE, ALIGN=3
    Stack_Mem SPACE Stack_Size
    __initial_sp
    ; <
    h
    > Heap Configuration
    ; <
    o
    > Heap Size (in Bytes) <
    0x0-0xFFFFFFFF:8
    >
    ; </
    h
    >
    Heap_Size EQU 0x00000200
    AREA HEAP, NOINIT, READWRITE, ALIGN=3
    __heap_base
    Heap_Mem SPACE Heap_Size
    __heap_limit
    PRESERVE8
    THUMB
    ; Vector Table Mapped to Address 0 at Reset
    AREA RESET, DATA, READONLY
    EXPORT __Vectors
    EXPORT __Vectors_End
    EXPORT __Vectors_Size
    __Vectors DCD __initial_sp ; Top of Stack
    DCD Reset_Handler ; Reset Handler
    ...

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    matic
    maticAuthor
    Associate III
    October 3, 2016
    Posted on October 03, 2016 at 20:11

    Hi Clive,

    I know that I was asking similar things previous week. Things which I was asking there works perfectly now. Now, I tried to read stack size and top address in a similar manner, but without success.

    I will read the links that you suggested.

    I appreciate your help every time.

    matic
    maticAuthor
    Associate III
    October 4, 2016
    Posted on October 04, 2016 at 20:05

    Now, I am able to read exported __initial_sp value in c file. There were two things which were causing problems:

    1. I didn't have enabled MicroLIB and there is ''IF DEF'' in startup file which exports__initial_sp only if MicroLIB is enabled.

     IF :DEF:__MICROLIB
    EXPORT __initial_sp
    EXPORT __heap_base
    EXPORT __heap_limit
    ELSE
    IMPORT __use_two_region_memory
    EXPORT __user_initial_stackheap
    __user_initial_stackheap
    LDR R0, = Heap_Mem
    LDR R1, =(Stack_Mem + Stack_Size)
    LDR R2, = (Heap_Mem + Heap_Size)
    LDR R3, = Stack_Mem
    BX LR
    ALIGN
    ENDIF
    END

    Thus, I deleted ''

    IF :DEF:__MICROLIB'' and

    everything between ''ELSE'' and ''ENDIF''. 2. I tried to read__initial_sp as:

    extern
    uint32_t __initial_sp;
    uint32_t Value;
    Value = __initial_sp;

    Instead of:

    extern
    uint32_t __initial_sp;
    uint32_t Value;
    Value = (uint32_t)&__initial_sp;