繁体   English   中英

-Os标志,使通用寄存器正常工作是必需的

[英]-Os flag necessary to make general purpose register behave normally

我目前正在使用TI EK-LM4F120XL板。 该评估板包含一个Cortex-M4F CPU。 我正在使用以下链:

ARM GCC无EABI https://launchpad.net/gcc-arm-embedded/4.8/4.8-2014-q2-update

和以下调试器:

OpenOCD http://openocd.sourceforge.net/

问题是我需要使用-Os标志来防止奇怪的行为。 一个使用TI提供的代码的示例:

默认链接描述文件:

MEMORY
{
    FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 0x00040000
    SRAM (rwx) : ORIGIN = 0x20000000, LENGTH = 0x00008000
}

SECTIONS
{
    .text :
    {
        _text = .;
        KEEP(*(.isr_vector))
        *(.text*)
        *(.rodata*)
        _etext = .;
    } > FLASH

    .data : AT(ADDR(.text) + SIZEOF(.text))
    {
        _data = .;
        *(vtable)
        *(.data*)
        _edata = .;
    } > SRAM

    .bss :
    {
        _bss = .;
        *(.bss*)
        *(COMMON)
        _ebss = .;
    } > SRAM
}

startup_gcc.c文件: pastbin,因为该文件很大

还有一个非常简单的信号灯:

int
main(void)
{
    volatile unsigned long ulLoop;

    //
    // 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.
    //
    ulLoop = 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(ulLoop = 0; ulLoop < 200000; ulLoop++)
        {
        }

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

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

没什么特别的,都是由TI创建的所有默认代码。 编译和链接命令:

~/gcc-arm-none-eabi/bin/arm-none-eabi-gcc blink.c startup_gcc.c -g -mthumb -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=softfp -ffunction-sections -fdata-sections -Os -MD -std=c99 -Wall -pedantic -DPART_LM4F120H5QR -c -I/home/jacko/git/jackoOS/stellaris-exe  -DTARGET_IS_BLIZZARD_RA1
~/gcc-arm-none-eabi/bin/arm-none-eabi-ld -T blink.ld --entry ResetISR -o a.out startup_gcc.o blink.o --gc-sections

如您所见,编译命令包含-Os参数。 如果将其添加到命令中,一切正常,但是如果删除它,寄存器7开始表现得很奇怪:

(gdb) monitor reg
===== arm v7m registers
(0) r0 (/32): 0x00000000
...
(7) r7 (/32): 0x200000F0
...
(13) sp (/32): 0x200000F0
...
(17) msp (/32): 0x200000F0
...
===== Cortex-M DWT registers
...
(36) dwt_3_function (/32)
(gdb) cont
...
(gdb) monitor reg
===== arm v7m registers
...
(7) r7 (/32): 0x200000E0
...
(13) sp (/32): 0x200000E0
...
(17) msp (/32): 0x200000E0
(18) psp (/32): 0x00000000
...
===== Cortex-M DWT registers
...
(36) dwt_3_function (/32)

(完整的转储可以在这里找到)

R7与SP的值相同(MSP =活动SP)! 为什么会那样做?

如果我尝试通过以下方式写到R7:

MOV     R7, R0

该程序只是崩溃为硬故障。

那么,为什么这个-Os标志如此重要? 没有它,为什么R7表现得如此怪异?

GCC在拇指模式下将R7用作FP。 如果未使用任何优化标志来避免该行为,请尝试“ -fomit-frame-pointer”。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM