user1273684
user1273684

Reputation: 1619

gcc generates binary of size 0 with -Os

I have a simple FreeRTOS application that just toggles an LED in the main loop. When I compile it with -Os, the resulting binary has size 0. Without -Os everything works as expected. What is happening here?

My CFLAGS are:

CPUFLAG = -mthumb -mcpu=cortex-m4
FPUFLAG = -mfpu=fpv4-sp-d16 -mfloat-abi=hard
WFLAG   = -Wall -Wextra -Werror -Wstrict-prototypes

CFLAGS  += -std=gnu99 $(WFLAG) $(CPUFLAG) $(FPUFLAG) -mlittle-endian -mthumb -nostartfiles
CFLAGS  += -ffunction-sections -fdata-sections -fno-builtin
LDFLAGS += -nostdlib --gc-sections -static

main is basically TI's blinky demo:

int main(void)
{
    volatile uint32_t ui32Loop;

    //
    // Enable the GPIO port that is used for the on-board LED.
    //
    SYSCTL_RCGC2_R = SYSCTL_RCGC2_GPIOF;

    //
    // Do a dummy read to insert a few cycles after enabling the peripheral.
    //
    ui32Loop = SYSCTL_RCGC2_R;

    //
    // Enable the GPIO pin for the LED (PF3).  Set the direction as output, and
    // enable the GPIO pin for digital function.
    //
    GPIO_PORTF_DIR_R = 0x08;
    GPIO_PORTF_DEN_R = 0x08;

    //
    // Loop forever.
    //
    while(1)
    {
        //
        // Turn on the LED.
        //
        GPIO_PORTF_DATA_R |= 0x08;

        //
        // Delay for a bit.
        //
        for(ui32Loop = 0; ui32Loop < 200000; ui32Loop++)
        {
        }

        //
        // Turn off the LED.
        //
        GPIO_PORTF_DATA_R &= ~(0x08);

        //
        // Delay for a bit.
        //
        for(ui32Loop = 0; ui32Loop < 200000; ui32Loop++)
        {
        }
    }

    return 0;
}

With -Os this generates

   text    data     bss     dec     hex filename
      0       0       0       0       0 image.elf

otherwise

   text    data     bss     dec     hex filename
   2012       4     728    2744     ab8 image.elf

edit: differences in .map files

Upvotes: 4

Views: 191

Answers (1)

Igor Skochinsky
Igor Skochinsky

Reputation: 25318

Since you specified -nostartfiles, the standard startup and its entrypoint is not used, so there is no reference to your main function, and --gc-sections discards the whole section as unused.

To fix, try to either name your function start or add -e _main to the ld flags.

Upvotes: 4

Related Questions