knurling-rs / flip-link

Adds zero-cost stack overflow protection to your embedded programs
Apache License 2.0
270 stars 6 forks source link

Achieving same effect through linker script. #99

Closed liarokapisv closed 1 week ago

liarokapisv commented 2 weeks ago

It seems that it is possible to take advantage of SIZEOF of other sections in order to achieve the same functionality that flip-link achieves through a small custom section and overriding of _stack_start:

  .stack (NOLOAD) : {
    . = ORIGIN(RAM);
    . = ORIGIN(RAM) + LENGTH(RAM) 
                    - SIZEOF(.bss) 
                    - SIZEOF(.data) 
                    - SIZEOF(.uninit) 
                    ;
  } > RAM

 _stack_start = ORIGIN(RAM) + SIZEOF(.stack);

This also has the advantage of working with custom linker sections (by adding a -SIZEOF(section) to the ALIGN and region aliases.

I am posting to discuss possible issues and tradeoffs of this approach.

Urhengulas commented 1 week ago

That has been tried before^1 but it eventually break downs in some cases. But that was also tested years ago; it could be that LLD better supports that these days.

Please discuss that in the linked cortex-m issue.