简体   繁体   English

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

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

I am currently working with a TI EK-LM4F120XL board. 我目前正在使用TI EK-LM4F120XL板。 This board contains a Cortex-M4F cpu. 该评估板包含一个Cortex-M4F CPU。 I am using the following chain: 我正在使用以下链:

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

And the following debugger: 和以下调试器:

OpenOCD http://openocd.sourceforge.net/ OpenOCD http://openocd.sourceforge.net/

The problem is that I need to use the -Os flag to prevent strange behaviour. 问题是我需要使用-Os标志来防止奇怪的行为。 An example, using code provided by TI: 一个使用TI提供的代码的示例:

Default linker script: 默认链接描述文件:

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
}

The startup_gcc.c file: pastbin, because the file is large startup_gcc.c文件: pastbin,因为该文件很大

And a very simple blinker: 还有一个非常简单的信号灯:

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++)
        {
        }
    }
}

Nothing special, all default code as created by TI. 没什么特别的,都是由TI创建的所有默认代码。 Compilation and linking commands: 编译和链接命令:

~/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

As you can see the compiling command contains a -Os param. 如您所见,编译命令包含-Os参数。 If I add this to the command everything works fine, but if I remove it, register 7 starts to act very weird: 如果将其添加到命令中,一切正常,但是如果删除它,寄存器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)

(full dump can be found here ) (完整的转储可以在这里找到)

R7 has the same value as the SP (MSP = active SP)! R7与SP的值相同(MSP =活动SP)! Why would it do that? 为什么会那样做?

If I try to write to R7 with: 如果我尝试通过以下方式写到R7:

MOV     R7, R0

The program just crashes into a hard fault. 该程序只是崩溃为硬故障。

So, why is this -Os flag so important? 那么,为什么这个-Os标志如此重要? Why does R7 act so weird without it? 没有它,为什么R7表现得如此怪异?

GCC uses R7 as FP under thumb mode. GCC在拇指模式下将R7用作FP。 Try "-fomit-frame-pointer" if you are not using any optimization flag to avoid that behavior. 如果未使用任何优化标志来避免该行为,请尝试“ -fomit-frame-pointer”。

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

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