STM32CubeG0 icon indicating copy to clipboard operation
STM32CubeG0 copied to clipboard

__RAM_FUNC breaks when inlined by GCC LTO

Open philiptaylor opened this issue 4 years ago • 2 comments

When GCC inlines functions, it ignores the section attribute of the inlined function. (This seems to be considered a feature, not a bug: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=31362, https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78903, etc)

Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_def.h has:

#define __RAM_FUNC __attribute__((section(".RamFunc")))

This is used by FLASH_Program_Fast in stm32g0xx_hal_flash.c (which must run from RAM because it needs exclusive access to flash).

When compiling with LTO, GCC will inline functions across different source files. That means FLASH_Program_Fast may be inlined into the application code that calls it, which is typically not in .RamFunc, so the code will run from flash and will fail.

I believe the best way to fix this is with noinline, i.e.

#define __RAM_FUNC __attribute__((section(".RamFunc"), noinline))

Then the function won't be inlined and will remain in the correct section.

(With this change, I was able to successfully build my code with LTO (which significantly reduces code size) and haven't noticed any other problems.)

philiptaylor avatar May 13 '21 11:05 philiptaylor

Hi @philiptaylor,

Thank you for your contribution. Your report will be forwarded to our development team. We will get back to you as soon as we have more details.

Thank you once more for this contribution.

With regards,

RKOUSTM avatar May 19 '21 17:05 RKOUSTM

ST Internal Reference: 108546

RKOUSTM avatar Jun 08 '21 10:06 RKOUSTM