-flto in GCC 7.2.1 cause ISR is removed from result binary (change from GCC 6.3.1)

Bug #1747966 reported by GLX on 2018-02-07
8
This bug affects 1 person
Affects Status Importance Assigned to Milestone
GNU Arm Embedded Toolchain
Undecided
Unassigned

Bug Description

Hi,
I just upgraded GCC (win32 host) toolchain from ver 6.3.1 to 7.2.1 and found that my recompiled project with -flto is not working. I found this is because new GCC thrown away my ISRs code and leave them directed to the default interrupt handler that cause MCU lokckup.
So here are my gcc compile options:
arm-none-eabi-gcc -mthumb -mcpu=cortex-m4 -DSTM32L476xx -I../../CMSIS/CORE/INCLUDE -Wall -Os -flto -ffunction-sections -fdata-sections -g -c -o main.o main.c

arm-none-eabi-gcc -mthumb -mcpu=cortex-m4 -DSTM32L476xx -I../../CMSIS/CORE/INCLUDE -Wall -Os -flto -ffunction-sections -fdata-sections -g --specs=nano.specs --specs=nosys.specs -L. -L../../ldscripts -T STM32L476RGTx_FLASH.ld -Wl,--gc-sections -Wl,-Map=BLINK-CM4.map -o BLINK-CM4.axf main.o system.o uart.o timer.o spi.o display.o ../../startup/startup_stm32l476xx.s arm-none-eabi-size BLINK-CM4.axf

I tried to assign my ISR to .isr_vector section that is defined in my generic linker script
void __attribute__ ((section(".isr_vector"), used)) USART2_IRQHandler(void)

  .isr_vector :
  {
    . = ALIGN(8);
    KEEP(*(.isr_vector)) /* Startup code */
    . = ALIGN(8);
  } >FLASH

and I also tried to add KEEP(*(.isr_vector)) to .text section but all this efforts and attributes that I tried doen's have any effect. The LST file doesn't contain ANY reference to USART2_IRQHandler and the MAP file shows the USART2_IRQHandler is assigned to .text.Default_Handler group instead of my code:

 .text.Default_Handler
                0x08000fec 0x2 C:\DOCUME~1\rehak\LOCALS~1\Temp\ccOwWxwF.o
                0x08000fec RTC_Alarm_IRQHandler
                0x08000fec EXTI2_IRQHandler
...
                0x08000fec USART2_IRQHandler
                0x08000fec DFSDM1_FLT0_IRQHandler

Even if I call the ISR function directly from main() it's discarded too. Is there any other way how to tell GCC 7 to keep it? Again, in GCC 6 it worked fine without any attributes.

The last though directed me to ISR names definitions. It's the part of STM Cube library, the file: startup_stm32l476xx.s where it is defined like this:

     .section .isr_vector,"a",%progbits
    .type g_pfnVectors, %object
    .size g_pfnVectors, .-g_pfnVectors
g_pfnVectors:
    .word _estack
    .word Reset_Handler
...
    .word USART2_IRQHandler
...
    .weak USART2_IRQHandler
    .thumb_set USART2_IRQHandler,Default_Handler

Is it OK? No problem with weak or so? When I rename my ISR to some other name that is NOT listed in this startup file it is NOT discarded so I guess there might be some link but I don't understand GAS asm...

Tejas Belagod (belagod-tejas) wrote :

Thanks for the report. Is the isr section kept if you drop the -flto?

Joey Ye (jinyun-ye) wrote :

Thanks for report. Can you please confirm if the same project works on GCC6 without modification? If yes, it looks a regression or behaviour change in GCC7

GLX (rayer) wrote :

Hi, sure if I remove -flto option the ISRs are not dropped from binary even without use of any mentioned attributes. But the section itself is listed in LST file:
Sections:
Idx Name Size VMA LMA File off Algn
  0 .isr_vector 00000188 08000000 08000000 00010000 2**0
                  CONTENTS, ALLOC, LOAD, READONLY, DATA

Yes, the project compiled fine with GCC 6.3.1 and 5.x that I use before. I discovered the problem with upgrading to 7.2.1. If there's some other GCC 7.x or 8.x available I could try.

To post a comment you must log in.
This report contains Public information  Edit
Everyone can see this information.

Other bug subscribers