Stack size calculation and memory inyernals

Hello, as a programmer that is very much interested in the low level details of my job, I would love to know the answer to some questions regarding the R32C processor standard compiler.

1. When I compile the program, i can see that the resulting binary's (MOT file analyzed with ida and similar tools) stack begins at a low offset, and grows downwards. Is it then not possible that with continuous recursion the address 0 can be reached? On modern OSs we have guard pages, but without an OS, it seems that this condition is inevitable here.

2. How is this address for the beggining of the stack determined?

  1. Thank you very much, would love to hear back from you.
Parents
  • Hi There,

    > 1. When I compile the program, i can see that the resulting binary's

    > (MOT file analyzed with ida and similar tools) stack begins at a low offset,

    > and grows downwards. Is it then not possible that with continuous recursion

    > the address 0 can be reached? On modern OSs we have guard pages, but without

    > an OS, it seems that this condition is inevitable here.

    By repeating function calls recursively, the address pointed to by the stack pointer (USP and ISP) can reach zero or exceed zero (e.g. 0xFFFFFFFC).

    There is also a problem of modifying outside the stack area before the above problem occurs.

    The reason for this is that the function is called with the stack pointer pointing outside the stack area, or the outside of the stack area is changed by changing an automatic variable.

    Therefore, the stack area with the size required by the program must be defined as a section (stack, istack).

    Program the recursive stack consumption of the function so that it does not exceed the defined stack size.

     

    > 2. How is this address for the beggining of the stack determined?

    Immediately after the reset, the startup program uses the machine instruction LDC to transfer the address to the ISP and USP.

    If you are using the High-performance Embedded Workshop, the address is set automatically by setting the required stack space size.

    If you use the startup program ncrt0.a30, the size of the stack area is set as the values of __USTACKSIZE__ and __ISTACKSIZE__ by using the assembler option -D.

    If you use the startup program resetprg.c, the size of the stack area is set as the values of __STACKSIZE__ and __ISTACKSIZE__ by the -D compile option.

     

    [Reference]

    R32C/100 Series C Compiler Package V.1.02 C Compiler User's Manual

    https://www.renesas.com/us/en/document/mat/r32c100-series-c-compiler-package-v102-c-compiler-users-manual

      Figure 2.7 Startup Program List (2) (ncrt0.a30)

        (2) defines the user stack size.

        (3) defines the interrupt stack size

      Figure 2.9 Startup Program List (4) (ncrt0.a30)

        (7) Sets IPL and each flags.

     

    C Compiler Package for R32C/100 Series V.1.02 Release 01 Release Notes

    https://www.renesas.com/us/en/document/rln/c-compiler-package-r32c100-series-v102-release-01-release-notes

      8.2.11. stackdef.h

        (1) Indicates the default size of the user stack.

        (2) Outputs a user stack section and reserves storage for it.

        (3) Indicates the default size of the interrupt stack.

        (4) Outputs an interrupt stack section and reserves storage for it.

    BR,

    PM_Renesas

  • Hello again and thank you for your answer!

    I have been wondering about the following issue as well:

    Say I have a program which is loaded from flash into memory at address 0xFFF00000.

    If I then run code that edits the program itself, for example, overwrites an absolute jump in the code to another location, will it:

    a. Not work, because the code is somehow read only.

    b. Work, the jump will be edited but when I restart the device it will come back to the old jump.

    c. Work, and it will permanently edit the jump in flash memory, and thus every reboot I will have the modified jump.

    Please let me know as i am very curious, waiting to hear back from you guys :)

Reply
  • Hello again and thank you for your answer!

    I have been wondering about the following issue as well:

    Say I have a program which is loaded from flash into memory at address 0xFFF00000.

    If I then run code that edits the program itself, for example, overwrites an absolute jump in the code to another location, will it:

    a. Not work, because the code is somehow read only.

    b. Work, the jump will be edited but when I restart the device it will come back to the old jump.

    c. Work, and it will permanently edit the jump in flash memory, and thus every reboot I will have the modified jump.

    Please let me know as i am very curious, waiting to hear back from you guys :)

Children
No Data